Skip to content

Commit

Permalink
[NF] Better handling of constants.
Browse files Browse the repository at this point in the history
- Take the subscripts into account when determining the variability
  of a cref, so we don't try to e.g. evaluate a constant with
  non-constant subscripts.
- Turn on collection of package constants again, since they might be
  used in the flat model due to the above change.

Belonging to [master]:
  - OpenModelica/OMCompiler#2175
  • Loading branch information
perost authored and OpenModelica-Hudson committed Feb 7, 2018
1 parent 5cc04d9 commit 70ef32b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 23 deletions.
13 changes: 4 additions & 9 deletions Compiler/NFFrontEnd/NFInst.mo
Expand Up @@ -141,15 +141,10 @@ algorithm
// Flatten and convert the class into a DAE.
(flat_model, funcs) := Flatten.flatten(inst_cls, name);

// The backend doesn't handle constants well, so for now we just replace all
// constants in Typing.typeExp.
//// Replace or collect package constants depending on the
//// replacePackageConstants debug flag.
//if Flags.isSet(Flags.REPLACE_PACKAGE_CONSTS) then
// (flat_model, funcs) := Package.replaceConstants(flat_model, funcs);
//else
// flat_model := Package.collectConstants(flat_model, funcs);
//end if;
// Collect package constants that couldn't be substituted with their values
// (e.g. because they where used with non-constants subscripts), and add them
// to the model.
flat_model := Package.collectConstants(flat_model, funcs);

flat_model := Scalarize.scalarize(flat_model, name);
(dae, daeFuncs) := ConvertDAE.convert(flat_model, funcs, name, InstNode.info(inst_cls));
Expand Down
31 changes: 21 additions & 10 deletions Compiler/NFFrontEnd/NFTyping.mo
Expand Up @@ -1316,6 +1316,8 @@ function typeCref
input SourceInfo info;
output Type ty;
output Variability variability;
protected
Variability subs_var;
algorithm
// Check that time isn't used in a function context.
// TODO: Fix NFBuiltin.TIME_CREF so that the compiler treats it like an actual
Expand All @@ -1326,36 +1328,39 @@ algorithm
fail();
end if;

cref := typeCref2(cref, origin, info);
(cref, subs_var) := typeCref2(cref, origin, info);
ty := ComponentRef.getSubscriptedType(cref);
variability := ComponentRef.getVariability(cref);
variability := Prefixes.variabilityMax(ComponentRef.getVariability(cref), subs_var);
end typeCref;

function typeCref2
input output ComponentRef cref;
input ExpOrigin.Type origin;
input SourceInfo info;
output Variability subsVariability;

import NFComponentRef.Origin;
algorithm
cref := match cref
(cref, subsVariability) := match cref
local
ComponentRef rest_cr;
Type node_ty;
list<Subscript> subs;
Variability subs_var, rest_var;

case ComponentRef.CREF(origin = Origin.SCOPE)
then cref;
then (cref, Variability.CONSTANT);

case ComponentRef.CREF(node = InstNode.COMPONENT_NODE())
algorithm
node_ty := typeComponent(cref.node, origin);
subs := typeSubscripts(cref.subscripts, node_ty, cref, origin, info);
rest_cr := typeCref2(cref.restCref, origin, info);
(subs, subs_var) := typeSubscripts(cref.subscripts, node_ty, cref, origin, info);
(rest_cr, rest_var) := typeCref2(cref.restCref, origin, info);
subsVariability := Prefixes.variabilityMax(subs_var, rest_var);
then
ComponentRef.CREF(cref.node, subs, node_ty, cref.origin, rest_cr);
(ComponentRef.CREF(cref.node, subs, node_ty, cref.origin, rest_cr), subsVariability);

else cref;
else (cref, Variability.CONSTANT);
end match;
end typeCref2;

Expand All @@ -1366,10 +1371,13 @@ function typeSubscripts
input ExpOrigin.Type origin;
input SourceInfo info;
output list<Subscript> typedSubs;
output Variability variability = Variability.CONSTANT;
protected
list<Dimension> dims;
Dimension dim;
Integer next_origin, i;
Subscript sub;
Variability var;
algorithm
dims := Type.arrayDims(crefType);

Expand All @@ -1386,7 +1394,9 @@ algorithm

for s in subscripts loop
dim :: dims := dims;
typedSubs := typeSubscript(s, dim, cref, i, origin, info) :: typedSubs;
(sub, var) := typeSubscript(s, dim, cref, i, origin, info);
typedSubs := sub :: typedSubs;
variability := Prefixes.variabilityMax(variability, var);
i := i + 1;
end for;

Expand All @@ -1401,6 +1411,7 @@ function typeSubscript
input ExpOrigin.Type origin;
input SourceInfo info;
output Subscript outSubscript = subscript;
output Variability variability = Variability.CONSTANT;
protected
Expression e;
Type ty, ety;
Expand All @@ -1411,7 +1422,7 @@ algorithm
case Subscript.UNTYPED()
algorithm
e := evaluateEnd(subscript.exp, dimension, cref, index, origin, info);
(e, ty, _) := typeExp(e, origin, info);
(e, ty, variability) := typeExp(e, origin, info);

if Type.isArray(ty) then
outSubscript := Subscript.SLICE(e);
Expand Down
5 changes: 1 addition & 4 deletions Compiler/Util/Flags.mo
Expand Up @@ -526,8 +526,6 @@ constant DebugFlag SUSAN_MATCHCONTINUE_DEBUG = DEBUG_FLAG(175, "susanDebug", fal
Util.gettext("Makes Susan generate code using try/else to better debug which function broke the expected match semantics."));
constant DebugFlag OLD_FE_UNITCHECK = DEBUG_FLAG(176, "oldFrontEndUnitCheck", false,
Util.gettext("Checks the consistency of units in equation (for the old front-end)."));
constant DebugFlag REPLACE_PACKAGE_CONSTS = DEBUG_FLAG(177, "replacePackageConstants", true,
Util.gettext("Replaces package constants with their evaluated values if set."));

// This is a list of all debug flags, to keep track of which flags are used. A
// flag can not be used unless it's in this list, and the list is checked at
Expand Down Expand Up @@ -710,8 +708,7 @@ constant list<DebugFlag> allDebugFlags = {
IGNORE_CYCLES,
ALIAS_CONFLICTS,
SUSAN_MATCHCONTINUE_DEBUG,
OLD_FE_UNITCHECK,
REPLACE_PACKAGE_CONSTS
OLD_FE_UNITCHECK
};

public
Expand Down

0 comments on commit 70ef32b

Please sign in to comment.