Skip to content

Commit

Permalink
[NF] Replace package constants by default.
Browse files Browse the repository at this point in the history
- Implemented new phase that replaces package constants.
- Added debug flag replacePackageConstants to toggle whether to
  replace or collect package constants (default replace).
- Added more generic traversal functions for Equation and Statement,
  and removed some duplicated code.

Belonging to [master]:
  - OpenModelica/OMCompiler#2047
  - OpenModelica/OpenModelica-testsuite#791
  • Loading branch information
perost authored and OpenModelica-Hudson committed Nov 22, 2017
1 parent d9b2126 commit 6dc1882
Show file tree
Hide file tree
Showing 7 changed files with 353 additions and 254 deletions.
103 changes: 103 additions & 0 deletions Compiler/NFFrontEnd/NFEquation.mo
Expand Up @@ -191,6 +191,7 @@ public
then
if referenceEq(e1, eq.exp) then eq else NORETCALL(e1, eq.info);

else eq;
end match;
end mapExp;

Expand All @@ -211,5 +212,107 @@ public
branch := (cond, eql);
end mapExpBranch;

function foldExpList<ArgT>
input list<Equation> eq;
input FoldFunc func;
input output ArgT arg;

partial function FoldFunc
input Expression exp;
input output ArgT arg;
end FoldFunc;
algorithm
for e in eq loop
arg := foldExp(e, func, arg);
end for;
end foldExpList;

function foldExp<ArgT>
input Equation eq;
input FoldFunc func;
input output ArgT arg;

partial function FoldFunc
input Expression exp;
input output ArgT arg;
end FoldFunc;
algorithm
() := match eq
case Equation.EQUALITY()
algorithm
arg := func(eq.lhs, arg);
arg := func(eq.rhs, arg);
then
();

case Equation.ARRAY_EQUALITY()
algorithm
arg := func(eq.lhs, arg);
arg := func(eq.rhs, arg);
then
();

case Equation.CONNECT()
algorithm
arg := func(eq.lhs, arg);
arg := func(eq.rhs, arg);
then
();

case Equation.FOR()
algorithm
arg := foldExpList(eq.body, func, arg);
then
();

case Equation.IF()
algorithm
for b in eq.branches loop
arg := func(Util.tuple21(b), arg);
arg := foldExpList(Util.tuple22(b), func, arg);
end for;
then
();

case Equation.WHEN()
algorithm
for b in eq.branches loop
arg := func(Util.tuple21(b), arg);
arg := foldExpList(Util.tuple22(b), func, arg);
end for;
then
();

case Equation.ASSERT()
algorithm
arg := func(eq.condition, arg);
arg := func(eq.message, arg);
arg := func(eq.level, arg);
then
();

case Equation.TERMINATE()
algorithm
arg := func(eq.message, arg);
then
();

case Equation.REINIT()
algorithm
arg := func(eq.cref, arg);
arg := func(eq.reinitExp, arg);
then
();

case Equation.NORETCALL()
algorithm
arg := func(eq.exp, arg);
then
();

else ();
end match;
end foldExp;

annotation(__OpenModelica_Interface="frontend");
end NFEquation;
93 changes: 2 additions & 91 deletions Compiler/NFFrontEnd/NFFlatten.mo
Expand Up @@ -556,99 +556,10 @@ function flattenAlgorithms
input output list<list<Statement>> algorithms;
input ComponentRef prefix;
algorithm
algorithms := listReverse(flattenStatements(alg, prefix) for alg in algorithms);
algorithms := listReverse(
Statement.mapExpList(alg, function flattenExp(prefix = prefix)) for alg in algorithms);
end flattenAlgorithms;

function flattenStatements
input output list<Statement> statements;
input ComponentRef prefix;
algorithm
statements := list(flattenStatement(s, prefix) for s in statements);
end flattenStatements;

function flattenStatement
input output Statement stmt;
input ComponentRef prefix;
algorithm
stmt := match stmt
local
Expression e1, e2, e3;

case Statement.ASSIGNMENT()
algorithm
e1 := flattenExp(stmt.lhs, prefix);
e2 := flattenExp(stmt.rhs, prefix);
then
Statement.ASSIGNMENT(e1, e2, stmt.info);

case Statement.FOR()
algorithm
stmt.body := flattenStatements(stmt.body, prefix);
then
stmt;

case Statement.IF()
algorithm
stmt.branches := list(flattenStmtBranch(b, prefix) for b in stmt.branches);
then
stmt;

case Statement.WHEN()
algorithm
stmt.branches := list(flattenStmtBranch(b, prefix) for b in stmt.branches);
then
stmt;

case Statement.ASSERT()
algorithm
e1 := flattenExp(stmt.condition, prefix);
e2 := flattenExp(stmt.message, prefix);
e3 := flattenExp(stmt.level, prefix);
then
Statement.ASSERT(e1, e2, e3, stmt.info);

case Statement.TERMINATE()
algorithm
e1 := flattenExp(stmt.message, prefix);
then
Statement.TERMINATE(e1, stmt.info);

case Statement.NORETCALL()
algorithm
e1 := flattenExp(stmt.exp, prefix);
then
Statement.NORETCALL(e1, stmt.info);

case Statement.WHILE()
algorithm
stmt.condition := flattenExp(stmt.condition, prefix);
stmt.body := flattenStatements(stmt.body, prefix);
then
stmt;

case Statement.FAILURE()
algorithm
stmt.body := flattenStatements(stmt.body, prefix);
then
stmt;

else stmt;
end match;
end flattenStatement;

function flattenStmtBranch
input output tuple<Expression, list<Statement>> branch;
input ComponentRef prefix;
protected
Expression exp;
list<Statement> stmtl;
algorithm
(exp, stmtl) := branch;
exp := flattenExp(exp, prefix);
stmtl := flattenStatements(stmtl, prefix);
branch := (exp, stmtl);
end flattenStmtBranch;

function resolveConnections
input output Elements elems;
input String name;
Expand Down
9 changes: 8 additions & 1 deletion Compiler/NFFrontEnd/NFInst.mo
Expand Up @@ -136,7 +136,14 @@ algorithm

// Flatten and convert the class into a DAE.
(elems, funcs) := Flatten.flatten(inst_cls, name);
elems := Package.collectConstants(elems);

// Replace or collect package constants depending on the
// replacePackageConstants debug flag.
if Flags.isSet(Flags.REPLACE_PACKAGE_CONSTS) then
elems := Package.replaceConstants(elems);
else
elems := Package.collectConstants(elems);
end if;

elems := Scalarize.scalarize(elems, name);
(dae, daeFuncs) := ConvertDAE.convert(elems, funcs, name, InstNode.info(inst_cls));
Expand Down

0 comments on commit 6dc1882

Please sign in to comment.