Skip to content

Commit

Permalink
avoid recursions
Browse files Browse the repository at this point in the history
Belonging to [master]:
  - OpenModelica/OMCompiler#2085
  • Loading branch information
hkiel authored and OpenModelica-Hudson committed Dec 19, 2017
1 parent 926d8af commit b91fa8f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 62 deletions.
4 changes: 2 additions & 2 deletions Compiler/BackEnd/Initialization.mo
Expand Up @@ -439,7 +439,7 @@ protected
algorithm
outEqns := match(inWEqn)
case BackendDAE.WHEN_STMTS(condition=condition,whenStmtLst=whenStmtLst) algorithm
active := Expression.containsInitialCall(condition, false);
active := Expression.containsInitialCall(condition);
for stmt in whenStmtLst loop
_ := match stmt
case BackendDAE.ASSIGN(left = DAE.CREF(componentRef = cr), right = e) equation
Expand Down Expand Up @@ -531,7 +531,7 @@ algorithm

// active when equation during initialization
case DAE.STMT_WHEN(exp=condition, statementLst=stmts) guard
Expression.containsInitialCall(condition, false)
Expression.containsInitialCall(condition)
equation
stmts = List.foldr(stmts, List.consr, inAcc);
then (stmts, inLeftCrs);
Expand Down
41 changes: 14 additions & 27 deletions Compiler/FrontEnd/Ceval.mo
Expand Up @@ -1419,29 +1419,18 @@ protected function cevalMatrixElt "Evaluates the expression of a matrix construc
input Boolean inBoolean "impl";
input Absyn.Msg inMsg;
input Integer numIter;
output FCore.Cache outCache;
output list<Values.Value> outValues;
algorithm
(outCache, outValues) :=
match (inCache, inEnv, inMatrix, inBoolean, inMsg)
local
Values.Value v;
list<Values.Value> vl;
FCore.Graph env;
list<DAE.Exp> expl;
list<list<DAE.Exp>> expll;
Boolean impl;
Absyn.Msg msg;
FCore.Cache cache;
case (cache,env,(expl :: expll),impl,msg)
equation
(cache,vl,_) = cevalList(cache,env,expl,impl,NONE(),msg,numIter);
v = ValuesUtil.makeArray(vl);
(cache,vl)= cevalMatrixElt(cache,env, expll, impl,msg,numIter);
then
(cache,v :: vl);
case (cache,_,{},_,_) then (cache,{});
end match;
output FCore.Cache outCache = inCache;
output list<Values.Value> outValues = {};
protected
Values.Value v;
list<Values.Value> vl;
algorithm
for expl in inMatrix loop
(outCache,vl,_) := cevalList(outCache,inEnv,expl,inBoolean,NONE(),inMsg,numIter);
v := ValuesUtil.makeArray(vl);
outValues := v::outValues;
end for;
outValues := listReverseInPlace(outValues);
end cevalMatrixElt;

protected function cevalBuiltinSize "Evaluates the size operator."
Expand Down Expand Up @@ -4490,16 +4479,14 @@ public function cevalList "This function does constant
output Option<GlobalScript.SymbolTable> outInteractiveInteractiveSymbolTableOption;
protected
list<DAE.Exp> expLstNew = inExpExpLst;
DAE.Exp exp;
Values.Value v;
Option<GlobalScript.SymbolTable> st = inST;

algorithm
while not listEmpty(expLstNew) loop
exp::expLstNew := expLstNew;
for exp in expLstNew loop
(outCache, v, st) := ceval(outCache, inEnv, exp, inBoolean, st, inMsg, numIter+1);
outValuesValueLst := v :: outValuesValueLst;
end while;
end for;
outValuesValueLst := listReverseInPlace(outValuesValueLst);
outInteractiveInteractiveSymbolTableOption := st;
end cevalList;
Expand Down
2 changes: 1 addition & 1 deletion Compiler/FrontEnd/DAEUtil.mo
Expand Up @@ -3091,7 +3091,7 @@ protected function verifyBoolWhenEquationBranch
input list<DAE.Element> inEqs;
output list<DAE.ComponentRef> crefs;
protected
Boolean initCond = Expression.containsInitialCall(inCond, false);
Boolean initCond = Expression.containsInitialCall(inCond);
algorithm
crefs := verifyBoolWhenEquation1(inEqs, initCond);
end verifyBoolWhenEquationBranch;
Expand Down
14 changes: 4 additions & 10 deletions Compiler/FrontEnd/Expression.mo
Expand Up @@ -4983,23 +4983,17 @@ public function containsInitialCall "public function containsInitialCall
only in one of the two forms when initial() then or when {…,initial(),…}
then. [...]"
input DAE.Exp condition; // expression of a when-clause
input Boolean inB; // use false for primary calls - it us for internal use only
output Boolean res;
algorithm
res := match(condition, inB)
res := match(condition)
local
Boolean b;
list<Exp> array;

case (_, true) equation
then true;

case (DAE.CALL(path = Absyn.IDENT(name = "initial")), _) equation
case (DAE.CALL(path = Absyn.IDENT(name = "initial")))
then true;

case (DAE.ARRAY(array=array), _) equation
b = List.fold(array, containsInitialCall, inB);
then b;
case (DAE.ARRAY(array=array))
then List.mapBoolOr(array, containsInitialCall);

else false;
end match;
Expand Down
31 changes: 9 additions & 22 deletions Compiler/FrontEnd/PrefixUtil.mo
Expand Up @@ -1042,29 +1042,16 @@ public function prefixExpList "This function prefixes a list of expressions usin
input InnerOuter.InstHierarchy inIH;
input list<DAE.Exp> inExpExpLst;
input Prefix.Prefix inPrefix;
output FCore.Cache outCache;
output list<DAE.Exp> outExpExpLst;
output FCore.Cache outCache = inCache;
output list<DAE.Exp> outExpExpLst = {};
protected
DAE.Exp e_1;
algorithm
(outCache,outExpExpLst) := match (inCache,inEnv,inIH,inExpExpLst,inPrefix)
local
DAE.Exp e_1,e;
list<DAE.Exp> es_1,es;
FCore.Graph env;
Prefix.Prefix p;
FCore.Cache cache;
InstanceHierarchy ih;

// handle empty case
case (cache,_,_,{},_) then (cache,{});

// yuppie! we have a list of expressions
case (cache,env,ih,(e :: es),p)
equation
(cache,e_1) = prefixExp(cache, env, ih, e, p);
(cache,es_1) = prefixExpList(cache, env, ih, es, p);
then
(cache,e_1 :: es_1);
end match;
for e in inExpExpLst loop
(outCache,e_1) := prefixExp(outCache, inEnv, inIH, e, inPrefix);
outExpExpLst := e_1::outExpExpLst;
end for;
outExpExpLst := Dangerous.listReverseInPlace(outExpExpLst);
end prefixExpList;

//--------------------------------------------
Expand Down

0 comments on commit b91fa8f

Please sign in to comment.