Skip to content
This repository was archived by the owner on May 18, 2019. It is now read-only.

Commit 6dc1882

Browse files
perostOpenModelica-Hudson
authored andcommitted
[NF] Replace package constants by default.
- 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]: - #2047 - OpenModelica/OpenModelica-testsuite#791
1 parent d9b2126 commit 6dc1882

File tree

7 files changed

+353
-254
lines changed

7 files changed

+353
-254
lines changed

Compiler/NFFrontEnd/NFEquation.mo

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ public
191191
then
192192
if referenceEq(e1, eq.exp) then eq else NORETCALL(e1, eq.info);
193193

194+
else eq;
194195
end match;
195196
end mapExp;
196197

@@ -211,5 +212,107 @@ public
211212
branch := (cond, eql);
212213
end mapExpBranch;
213214

215+
function foldExpList<ArgT>
216+
input list<Equation> eq;
217+
input FoldFunc func;
218+
input output ArgT arg;
219+
220+
partial function FoldFunc
221+
input Expression exp;
222+
input output ArgT arg;
223+
end FoldFunc;
224+
algorithm
225+
for e in eq loop
226+
arg := foldExp(e, func, arg);
227+
end for;
228+
end foldExpList;
229+
230+
function foldExp<ArgT>
231+
input Equation eq;
232+
input FoldFunc func;
233+
input output ArgT arg;
234+
235+
partial function FoldFunc
236+
input Expression exp;
237+
input output ArgT arg;
238+
end FoldFunc;
239+
algorithm
240+
() := match eq
241+
case Equation.EQUALITY()
242+
algorithm
243+
arg := func(eq.lhs, arg);
244+
arg := func(eq.rhs, arg);
245+
then
246+
();
247+
248+
case Equation.ARRAY_EQUALITY()
249+
algorithm
250+
arg := func(eq.lhs, arg);
251+
arg := func(eq.rhs, arg);
252+
then
253+
();
254+
255+
case Equation.CONNECT()
256+
algorithm
257+
arg := func(eq.lhs, arg);
258+
arg := func(eq.rhs, arg);
259+
then
260+
();
261+
262+
case Equation.FOR()
263+
algorithm
264+
arg := foldExpList(eq.body, func, arg);
265+
then
266+
();
267+
268+
case Equation.IF()
269+
algorithm
270+
for b in eq.branches loop
271+
arg := func(Util.tuple21(b), arg);
272+
arg := foldExpList(Util.tuple22(b), func, arg);
273+
end for;
274+
then
275+
();
276+
277+
case Equation.WHEN()
278+
algorithm
279+
for b in eq.branches loop
280+
arg := func(Util.tuple21(b), arg);
281+
arg := foldExpList(Util.tuple22(b), func, arg);
282+
end for;
283+
then
284+
();
285+
286+
case Equation.ASSERT()
287+
algorithm
288+
arg := func(eq.condition, arg);
289+
arg := func(eq.message, arg);
290+
arg := func(eq.level, arg);
291+
then
292+
();
293+
294+
case Equation.TERMINATE()
295+
algorithm
296+
arg := func(eq.message, arg);
297+
then
298+
();
299+
300+
case Equation.REINIT()
301+
algorithm
302+
arg := func(eq.cref, arg);
303+
arg := func(eq.reinitExp, arg);
304+
then
305+
();
306+
307+
case Equation.NORETCALL()
308+
algorithm
309+
arg := func(eq.exp, arg);
310+
then
311+
();
312+
313+
else ();
314+
end match;
315+
end foldExp;
316+
214317
annotation(__OpenModelica_Interface="frontend");
215318
end NFEquation;

Compiler/NFFrontEnd/NFFlatten.mo

Lines changed: 2 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -556,99 +556,10 @@ function flattenAlgorithms
556556
input output list<list<Statement>> algorithms;
557557
input ComponentRef prefix;
558558
algorithm
559-
algorithms := listReverse(flattenStatements(alg, prefix) for alg in algorithms);
559+
algorithms := listReverse(
560+
Statement.mapExpList(alg, function flattenExp(prefix = prefix)) for alg in algorithms);
560561
end flattenAlgorithms;
561562

562-
function flattenStatements
563-
input output list<Statement> statements;
564-
input ComponentRef prefix;
565-
algorithm
566-
statements := list(flattenStatement(s, prefix) for s in statements);
567-
end flattenStatements;
568-
569-
function flattenStatement
570-
input output Statement stmt;
571-
input ComponentRef prefix;
572-
algorithm
573-
stmt := match stmt
574-
local
575-
Expression e1, e2, e3;
576-
577-
case Statement.ASSIGNMENT()
578-
algorithm
579-
e1 := flattenExp(stmt.lhs, prefix);
580-
e2 := flattenExp(stmt.rhs, prefix);
581-
then
582-
Statement.ASSIGNMENT(e1, e2, stmt.info);
583-
584-
case Statement.FOR()
585-
algorithm
586-
stmt.body := flattenStatements(stmt.body, prefix);
587-
then
588-
stmt;
589-
590-
case Statement.IF()
591-
algorithm
592-
stmt.branches := list(flattenStmtBranch(b, prefix) for b in stmt.branches);
593-
then
594-
stmt;
595-
596-
case Statement.WHEN()
597-
algorithm
598-
stmt.branches := list(flattenStmtBranch(b, prefix) for b in stmt.branches);
599-
then
600-
stmt;
601-
602-
case Statement.ASSERT()
603-
algorithm
604-
e1 := flattenExp(stmt.condition, prefix);
605-
e2 := flattenExp(stmt.message, prefix);
606-
e3 := flattenExp(stmt.level, prefix);
607-
then
608-
Statement.ASSERT(e1, e2, e3, stmt.info);
609-
610-
case Statement.TERMINATE()
611-
algorithm
612-
e1 := flattenExp(stmt.message, prefix);
613-
then
614-
Statement.TERMINATE(e1, stmt.info);
615-
616-
case Statement.NORETCALL()
617-
algorithm
618-
e1 := flattenExp(stmt.exp, prefix);
619-
then
620-
Statement.NORETCALL(e1, stmt.info);
621-
622-
case Statement.WHILE()
623-
algorithm
624-
stmt.condition := flattenExp(stmt.condition, prefix);
625-
stmt.body := flattenStatements(stmt.body, prefix);
626-
then
627-
stmt;
628-
629-
case Statement.FAILURE()
630-
algorithm
631-
stmt.body := flattenStatements(stmt.body, prefix);
632-
then
633-
stmt;
634-
635-
else stmt;
636-
end match;
637-
end flattenStatement;
638-
639-
function flattenStmtBranch
640-
input output tuple<Expression, list<Statement>> branch;
641-
input ComponentRef prefix;
642-
protected
643-
Expression exp;
644-
list<Statement> stmtl;
645-
algorithm
646-
(exp, stmtl) := branch;
647-
exp := flattenExp(exp, prefix);
648-
stmtl := flattenStatements(stmtl, prefix);
649-
branch := (exp, stmtl);
650-
end flattenStmtBranch;
651-
652563
function resolveConnections
653564
input output Elements elems;
654565
input String name;

Compiler/NFFrontEnd/NFInst.mo

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,14 @@ algorithm
136136

137137
// Flatten and convert the class into a DAE.
138138
(elems, funcs) := Flatten.flatten(inst_cls, name);
139-
elems := Package.collectConstants(elems);
139+
140+
// Replace or collect package constants depending on the
141+
// replacePackageConstants debug flag.
142+
if Flags.isSet(Flags.REPLACE_PACKAGE_CONSTS) then
143+
elems := Package.replaceConstants(elems);
144+
else
145+
elems := Package.collectConstants(elems);
146+
end if;
140147

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

0 commit comments

Comments
 (0)