Skip to content

Commit

Permalink
[NF] Expand complex array crefs.
Browse files Browse the repository at this point in the history
- Expand crefs where one of the prefix nodes is an array, i.e.
  a.b.c => {a.b[1].c, a.b[2].c}, to work around backend issues.
- Fix order of equations in if-equations.

Belonging to [master]:
  - OpenModelica/OMCompiler#2919
  - OpenModelica/OpenModelica-testsuite#1119
  • Loading branch information
perost authored and OpenModelica-Hudson committed Feb 5, 2019
1 parent 71414e4 commit 8017c67
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
24 changes: 24 additions & 0 deletions Compiler/NFFrontEnd/NFComponentRef.mo
Expand Up @@ -850,5 +850,29 @@ public
end match;
end depth;

function isComplexArray
input ComponentRef cref;
output Boolean complexArray;
algorithm
complexArray := match cref
case CREF() then isComplexArray2(cref.restCref);
else false;
end match;
end isComplexArray;

function isComplexArray2
input ComponentRef cref;
output Boolean complexArray;
algorithm
complexArray := match cref
case CREF(ty = Type.ARRAY())
guard Type.isArray(Type.subscript(cref.ty, cref.subscripts))
then true;

case CREF() then isComplexArray2(cref.restCref);
else false;
end match;
end isComplexArray2;

annotation(__OpenModelica_Interface="frontend");
end NFComponentRef;
2 changes: 1 addition & 1 deletion Compiler/NFFrontEnd/NFFlatten.mo
Expand Up @@ -997,7 +997,7 @@ algorithm
algorithm
// Flatten the condition and body of the branch.
cond := flattenExp(cond, prefix);
eql := flattenEquations(eql, prefix);
eql := listReverseInPlace(flattenEquations(eql, prefix));

// Evaluate structural conditions.
if var <= Variability.STRUCTURAL_PARAMETER then
Expand Down
31 changes: 30 additions & 1 deletion Compiler/NFFrontEnd/NFScalarize.mo
Expand Up @@ -70,7 +70,9 @@ algorithm
end for;

flatModel.variables := listReverseInPlace(vars);
flatModel.equations := Equation.mapExpList(flatModel.equations, expandComplexCref);
flatModel.equations := scalarizeEquations(flatModel.equations);
flatModel.initialEquations := Equation.mapExpList(flatModel.initialEquations, expandComplexCref);
flatModel.initialEquations := scalarizeEquations(flatModel.initialEquations);
flatModel.algorithms := list(scalarizeAlgorithm(a) for a in flatModel.algorithms);
flatModel.initialAlgorithms := list(scalarizeAlgorithm(a) for a in flatModel.initialAlgorithms);
Expand Down Expand Up @@ -111,7 +113,7 @@ algorithm
(ty_attr_names, ty_attr_iters) := scalarizeTypeAttributes(ty_attr);

if Binding.isBound(binding) then
binding_iter := ExpressionIterator.fromExp(Binding.getTypedExp(binding));
binding_iter := ExpressionIterator.fromExp(expandComplexCref(Binding.getTypedExp(binding)));
bind_var := Binding.variability(binding);

for cr in crefs loop
Expand All @@ -127,6 +129,7 @@ algorithm
end for;
end if;
else
var.binding := Binding.mapExp(var.binding, expandComplexCref);
vars := var :: vars;
end if;
end scalarizeVariable;
Expand Down Expand Up @@ -169,6 +172,32 @@ algorithm
end for;
end nextTypeAttributes;

function expandComplexCref
input output Expression exp;
algorithm
exp := Expression.map(exp, expandComplexCref_traverser);
end expandComplexCref;

function expandComplexCref_traverser
input output Expression exp;
algorithm
() := match exp
case Expression.CREF(ty = Type.ARRAY())
algorithm
// Expand crefs where any of the prefix nodes are arrays. For example if
// b in a.b.c is SomeType[2] we expand it into {a.b[1].c, a.b[2].c}.
// TODO: This is only done due to backend issues and shouldn't be
// necessary.
if ComponentRef.isComplexArray(exp.cref) then
exp := ExpandExp.expand(exp);
end if;
then
();

else ();
end match;
end expandComplexCref_traverser;

function scalarizeEquations
input list<Equation> eql;
output list<Equation> equations = {};
Expand Down

0 comments on commit 8017c67

Please sign in to comment.