Skip to content

Commit 7357a72

Browse files
lochelsjoelund
authored andcommitted
Replace recursions with loops
1 parent 89e8201 commit 7357a72

File tree

1 file changed

+53
-83
lines changed

1 file changed

+53
-83
lines changed

Compiler/BackEnd/Initialization.mo

Lines changed: 53 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -502,29 +502,19 @@ protected function generateInactiveWhenEquationForInitialization "author: lochel
502502
input list<DAE.ComponentRef> inCrLst;
503503
input DAE.ElementSource inSource;
504504
input list<BackendDAE.Equation> inEqns;
505-
output list<BackendDAE.Equation> outEqns;
505+
output list<BackendDAE.Equation> outEqns = inEqns;
506+
protected
507+
DAE.Type identType;
508+
DAE.Exp crefExp, crefPreExp;
509+
BackendDAE.Equation eqn;
506510
algorithm
507-
outEqns := match (inCrLst)
508-
local
509-
DAE.Type identType;
510-
DAE.Exp crefExp, crefPreExp;
511-
DAE.ComponentRef cr;
512-
list<DAE.ComponentRef> rest;
513-
BackendDAE.Equation eqn;
514-
list<BackendDAE.Equation> eqns;
515-
BackendDAE.Variables vars;
516-
517-
case {}
518-
then inEqns;
519-
520-
case cr::rest equation
521-
identType = ComponentReference.crefTypeConsiderSubs(cr);
522-
crefExp = DAE.CREF(cr, identType);
523-
crefPreExp = Expression.makePureBuiltinCall("pre", {crefExp}, DAE.T_BOOL_DEFAULT);
524-
eqn = BackendDAE.EQUATION(crefExp, crefPreExp, inSource, BackendDAE.EQ_ATTR_DEFAULT_DYNAMIC);
525-
eqns = generateInactiveWhenEquationForInitialization(rest, inSource, eqn::inEqns);
526-
then eqns;
527-
end match;
511+
for cr in inCrLst loop
512+
identType := ComponentReference.crefTypeConsiderSubs(cr);
513+
crefExp := DAE.CREF(cr, identType);
514+
crefPreExp := Expression.makePureBuiltinCall("pre", {crefExp}, DAE.T_BOOL_DEFAULT);
515+
eqn := BackendDAE.EQUATION(crefExp, crefPreExp, inSource, BackendDAE.EQ_ATTR_DEFAULT_DYNAMIC);
516+
outEqns := eqn::outEqns;
517+
end for;
528518
end generateInactiveWhenEquationForInitialization;
529519

530520
// =============================================================================
@@ -1074,14 +1064,14 @@ protected function preBalanceInitialSystem1 "author: lochel"
10741064
input BackendDAE.IncidenceMatrix mt;
10751065
input BackendDAE.Variables inVars;
10761066
input BackendDAE.EquationArray inEqs;
1077-
input Boolean iB;
1067+
input Boolean inB;
10781068
input list<BackendDAE.Var> inDumpVars;
10791069
output BackendDAE.Variables outVars;
10801070
output BackendDAE.EquationArray outEqs;
1081-
output Boolean oB;
1071+
output Boolean outB;
10821072
output list<BackendDAE.Var> outDumpVars;
10831073
algorithm
1084-
(outVars, outEqs, oB, outDumpVars) := match (n, mt, inVars, inEqs, iB, inDumpVars)
1074+
(outVars, outEqs, outB, outDumpVars) := match (n, inB)
10851075
local
10861076
list<Integer> row;
10871077
Boolean b, useHomotopy;
@@ -1092,16 +1082,16 @@ algorithm
10921082
DAE.ComponentRef cref;
10931083
list<BackendDAE.Var> dumpVars;
10941084

1095-
case (0, _, _, _, false, _)
1085+
case (0, false)
10961086
then (inVars, inEqs, false, inDumpVars);
10971087

1098-
case (0, _, _, _, true, _) equation
1088+
case (0, true) equation
10991089
vars = BackendVariable.listVar1(BackendVariable.varList(inVars));
11001090
then (vars, inEqs, true, inDumpVars);
11011091

1102-
case (_, _, _, _, _, _) equation
1092+
else equation
11031093
true = n > 0;
1104-
(vars, eqs, b, dumpVars) = preBalanceInitialSystem2(n, mt, inVars, inEqs, iB, inDumpVars);
1094+
(vars, eqs, b, dumpVars) = preBalanceInitialSystem2(n, mt, inVars, inEqs, inB, inDumpVars);
11051095
(vars, eqs, b, dumpVars) = preBalanceInitialSystem1(n-1, mt, vars, eqs, b, dumpVars);
11061096
then (vars, eqs, b, dumpVars);
11071097

@@ -1113,14 +1103,14 @@ protected function preBalanceInitialSystem2 "author: lochel"
11131103
input BackendDAE.IncidenceMatrix mt;
11141104
input BackendDAE.Variables inVars;
11151105
input BackendDAE.EquationArray inEqs;
1116-
input Boolean iB;
1106+
input Boolean inB;
11171107
input list<BackendDAE.Var> inDumpVars;
11181108
output BackendDAE.Variables outVars;
11191109
output BackendDAE.EquationArray outEqs;
1120-
output Boolean oB;
1110+
output Boolean outB;
11211111
output list<BackendDAE.Var> outDumpVars;
11221112
algorithm
1123-
(outVars, outEqs, oB, outDumpVars) := matchcontinue(n, mt, inVars, inEqs, iB, inDumpVars)
1113+
(outVars, outEqs, outB, outDumpVars) := matchcontinue(n, mt, inVars, inEqs, inB, inDumpVars)
11241114
local
11251115
list<Integer> row;
11261116
Boolean b, useHomotopy;
@@ -1156,7 +1146,7 @@ algorithm
11561146
case (_, _, _, _, _, _) equation
11571147
row = mt[n];
11581148
false = listEmpty(row);
1159-
then (inVars, inEqs, iB, inDumpVars);
1149+
then (inVars, inEqs, inB, inDumpVars);
11601150

11611151
else equation
11621152
Error.addInternalError("function preBalanceInitialSystem1 failed", sourceInfo());
@@ -1466,66 +1456,46 @@ protected function addStartValueEquations "author: lochel"
14661456
input list<BackendDAE.Var> inVarLst;
14671457
input BackendDAE.EquationArray inEqns;
14681458
input list<BackendDAE.Var> inDumpVars;
1469-
output BackendDAE.EquationArray outEqns;
1470-
output list<BackendDAE.Var> outDumpVars "this are the variables that get fixed (not the same as inVarLst!)";
1459+
output BackendDAE.EquationArray outEqns = inEqns;
1460+
output list<BackendDAE.Var> outDumpVars = inDumpVars "this are the variables that get fixed (not the same as inVarLst!)";
1461+
protected
1462+
BackendDAE.Var dumpVar;
1463+
BackendDAE.Equation eqn;
1464+
DAE.Exp e, crefExp, startExp;
1465+
DAE.ComponentRef cref;
1466+
DAE.Type tp;
1467+
Boolean isPreCref;
14711468
algorithm
1472-
(outEqns, outDumpVars) := matchcontinue(inVarLst, inEqns, inDumpVars)
1473-
local
1474-
BackendDAE.Var var, dumpVar;
1475-
list<BackendDAE.Var> vars, dumpVars;
1476-
BackendDAE.Equation eqn;
1477-
BackendDAE.EquationArray eqns;
1478-
DAE.Exp e, crefExp, startExp;
1479-
DAE.ComponentRef cref, preCref;
1480-
DAE.Type tp;
1481-
1482-
case ({}, _, _) then (inEqns, inDumpVars);
1483-
1484-
case (var::vars, _, _) equation
1485-
preCref = BackendVariable.varCref(var);
1486-
true = ComponentReference.isPreCref(preCref);
1487-
cref = ComponentReference.popPreCref(preCref);
1488-
tp = BackendVariable.varType(var);
1489-
1490-
crefExp = DAE.CREF(preCref, tp);
1469+
for var in inVarLst loop
1470+
cref := BackendVariable.varCref(var);
1471+
tp := BackendVariable.varType(var);
1472+
crefExp := DAE.CREF(cref, tp);
1473+
isPreCref := ComponentReference.isPreCref(cref);
1474+
1475+
if isPreCref then
1476+
cref := ComponentReference.popPreCref(cref);
1477+
end if;
14911478

1492-
e = Expression.crefExp(cref);
1493-
tp = Expression.typeof(e);
1494-
startExp = Expression.makePureBuiltinCall("$_start", {e}, tp);
1479+
e := Expression.crefExp(cref);
1480+
tp := Expression.typeof(e);
1481+
startExp := Expression.makePureBuiltinCall("$_start", {e}, tp);
14951482

1496-
eqn = BackendDAE.EQUATION(crefExp, startExp, DAE.emptyElementSource, BackendDAE.EQ_ATTR_DEFAULT_INITIAL);
1497-
eqns = BackendEquation.addEquation(eqn, inEqns);
1483+
eqn := BackendDAE.EQUATION(crefExp, startExp, DAE.emptyElementSource, BackendDAE.EQ_ATTR_DEFAULT_INITIAL);
1484+
outEqns := BackendEquation.addEquation(eqn, outEqns);
14981485

1499-
dumpVar = BackendVariable.copyVarNewName(cref, var);
1486+
if isPreCref then
1487+
dumpVar := BackendVariable.copyVarNewName(cref, var);
15001488
// crStr = BackendDump.varString(dumpVar);
15011489
// fcall(Flags.INITIALIZATION, Error.addCompilerWarning, " " + crStr);
15021490

1503-
(eqns, dumpVars) = addStartValueEquations(vars, eqns, inDumpVars);
1504-
then (eqns, dumpVar::dumpVars);
1505-
1506-
case (var::vars, _, _) equation
1507-
cref = BackendVariable.varCref(var);
1508-
tp = BackendVariable.varType(var);
1509-
1510-
crefExp = DAE.CREF(cref, tp);
1511-
1512-
e = Expression.crefExp(cref);
1513-
tp = Expression.typeof(e);
1514-
startExp = Expression.makePureBuiltinCall("$_start", {e}, tp);
1515-
1516-
eqn = BackendDAE.EQUATION(crefExp, startExp, DAE.emptyElementSource, BackendDAE.EQ_ATTR_DEFAULT_INITIAL);
1517-
eqns = BackendEquation.addEquation(eqn, inEqns);
1518-
1491+
outDumpVars := dumpVar::outDumpVars;
1492+
else
15191493
// crStr = BackendDump.varString(var);
15201494
// fcall(Flags.INITIALIZATION, Error.addCompilerWarning, " " + crStr);
15211495

1522-
(eqns, dumpVars) = addStartValueEquations(vars, eqns, inDumpVars);
1523-
then (eqns, var::dumpVars);
1524-
1525-
else equation
1526-
Error.addInternalError("function addStartValueEquations failed", sourceInfo());
1527-
then fail();
1528-
end matchcontinue;
1496+
outDumpVars := var::outDumpVars;
1497+
end if;
1498+
end for;
15291499
end addStartValueEquations;
15301500

15311501
// =============================================================================

0 commit comments

Comments
 (0)