Skip to content

Commit

Permalink
- Improved BackendDAEUtil.treeGet slightly for bootstrapping (more ef…
Browse files Browse the repository at this point in the history
…ficient, but not tail-recursive)

- Made BackendDAECreate.lower tail-recursive


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@7627 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Jan 3, 2011
1 parent 4292e84 commit f93f6ba
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 53 deletions.
54 changes: 26 additions & 28 deletions Compiler/BackEnd/BackendDAECreate.mo
Expand Up @@ -111,7 +111,7 @@ algorithm
vars = BackendDAEUtil.emptyVars();
knvars = BackendDAEUtil.emptyVars();
extVars = BackendDAEUtil.emptyVars();
(vars,knvars,extVars,eqns,reqns,ieqns,aeqns,iaeqns,algs,ialgs,whenclauses,extObjCls,s) = lower2(elems, functionTree, s, vars, knvars, extVars, {});
(vars,knvars,extVars,eqns,reqns,ieqns,aeqns,iaeqns,algs,ialgs,whenclauses,extObjCls,s) = lower2(listReverse(elems), functionTree, vars, knvars, extVars, {}, {}, {}, {}, {}, {}, {}, {}, {}, s);

daeContainsNoStates = hasNoStates(s); // check if the DAE has states
// adrpo: add the dummy derivative state ONLY IF the DAE contains
Expand Down Expand Up @@ -151,7 +151,7 @@ algorithm
vars = BackendDAEUtil.emptyVars();
knvars = BackendDAEUtil.emptyVars();
extVars = BackendDAEUtil.emptyVars();
(vars,knvars,extVars,eqns,reqns,ieqns,aeqns,iaeqns,algs,ialgs,whenclauses,extObjCls,s) = lower2(elems, functionTree, s, vars, knvars, extVars, {});
(vars,knvars,extVars,eqns,reqns,ieqns,aeqns,iaeqns,algs,ialgs,whenclauses,extObjCls,s) = lower2(listReverse(elems), functionTree, vars, knvars, extVars, {}, {}, {}, {}, {}, {}, {}, {}, {}, s);

daeContainsNoStates = hasNoStates(s); // check if the DAE has states
// adrpo: add the dummy derivative state ONLY IF the DAE contains
Expand Down Expand Up @@ -189,13 +189,21 @@ end lower;

protected function lower2
"Helper function to lower."
input list<DAE.Element> inElements;
input list<DAE.Element> inElements "input is in reverse order. this is faster than reversing all accumulators at the end";
input DAE.FunctionTree functionTree;
input BackendDAE.BinTree inStatesBinTree;
input BackendDAE.Variables inVariables;
input BackendDAE.Variables inKnownVariables;
input BackendDAE.Variables inExternalVariables;
input list<BackendDAE.WhenClause> inWhenClauseLst;
input list<BackendDAE.Equation> inEquationLst3;
input list<BackendDAE.Equation> inEquationLst4;
input list<BackendDAE.Equation> inEquationLst5;
input list<BackendDAE.MultiDimEquation> inMultiDimEquationLst6;
input list<BackendDAE.MultiDimEquation> inMultiDimEquationLst7;
input list<DAE.Algorithm> inAlgorithmAlgorithmLst8;
input list<DAE.Algorithm> inAlgorithmAlgorithmLst9;
input list<BackendDAE.WhenClause> inWhenClauseLst10;
input BackendDAE.ExternalObjectClasses inExtObjClasses;
input BackendDAE.BinTree inStatesBinTree;
output BackendDAE.Variables outVariables;
output BackendDAE.Variables outKnownVariables;
output BackendDAE.Variables outExternalVariables;
Expand All @@ -212,7 +220,8 @@ protected function lower2
algorithm
(outVariables,outKnownVariables,outExternalVariables,outEquationLst3,outEquationLst4,outEquationLst5,
outMultiDimEquationLst6,outMultiDimEquationLst7,outAlgorithmAlgorithmLst8,outAlgorithmAlgorithmLst9,outWhenClauseLst10,outExtObjClasses,outStatesBinTree):=
match (inElements,functionTree,inStatesBinTree,inVariables,inKnownVariables,inExternalVariables,inWhenClauseLst)
match (inElements,functionTree,inVariables,inKnownVariables,inExternalVariables,inEquationLst3,inEquationLst4,inEquationLst5,
inMultiDimEquationLst6,inMultiDimEquationLst7,inAlgorithmAlgorithmLst8,inAlgorithmAlgorithmLst9,inWhenClauseLst10,inExtObjClasses,inStatesBinTree)
local
BackendDAE.Variables v1,v2,v3,vars,knvars,extVars,extVars1,extVars2,vars_1,knvars_1,vars1,vars2,knvars1,knvars2,kv;
list<BackendDAE.WhenClause> whenclauses,whenclauses_1,whenclauses_2;
Expand Down Expand Up @@ -256,19 +265,19 @@ algorithm
DAE.ExpType expTy;

// the empty case
case ({},functionTree,states,v1,v2,v3,whenclauses)
case ({},functionTree,vars,knvars,extVars,eqns,reqns,ieqns,aeqns,iaeqns,algs,ialgs,whenclauses,extObjCls,states)
then
(v1,v2,v3,{},{},{},{},{},{},{},whenclauses,{},states);
(vars,knvars,extVars,eqns,reqns,ieqns,aeqns,iaeqns,algs,ialgs,whenclauses,extObjCls,states);

case (daeEl::daeLstRest,functionTree,states,vars,knvars,extVars,whenclauses)
case (daeEl::daeLstRest,functionTree,vars,knvars,extVars,eqns,reqns,ieqns,aeqns,iaeqns,algs,ialgs,whenclauses,extObjCls,states)
equation
(vars,knvars,extVars,eqns,reqns,ieqns,aeqns,iaeqns,algs,ialgs,whenclauses_1,extObjCls,states) =
lower2(daeLstRest, functionTree, states, vars, knvars, extVars, whenclauses);
(vars,knvars,extVars,eqns,reqns,ieqns,aeqns,iaeqns,algs,ialgs,whenclauses_1,extObjCls,states) =
lower3(daeEl,functionTree,vars,knvars,extVars,eqns,reqns,ieqns,aeqns,iaeqns,algs,ialgs,whenclauses_1,extObjCls,states);
(vars,knvars,extVars,eqns,reqns,ieqns,aeqns,iaeqns,algs,ialgs,whenclauses,extObjCls,states) =
lower3(daeEl,functionTree,vars,knvars,extVars,eqns,reqns,ieqns,aeqns,iaeqns,algs,ialgs,whenclauses,extObjCls,states);

(vars,knvars,extVars,eqns,reqns,ieqns,aeqns,iaeqns,algs,ialgs,whenclauses,extObjCls,states) =
lower2(daeLstRest,functionTree,vars,knvars,extVars,eqns,reqns,ieqns,aeqns,iaeqns,algs,ialgs,whenclauses,extObjCls,states);
then
(vars,knvars,extVars,eqns,reqns,ieqns,aeqns,iaeqns,algs,ialgs,whenclauses_1,extObjCls,states);
(vars,knvars,extVars,eqns,reqns,ieqns,aeqns,iaeqns,algs,ialgs,whenclauses,extObjCls,states);
end match;
end lower2;

Expand Down Expand Up @@ -543,20 +552,9 @@ algorithm
// flat class / COMP
case (DAE.COMP(dAElist = daeElts),functionTree,vars,knvars,extVars,eqns,reqns,ieqns,aeqns,iaeqns,algs,ialgs,whenclauses,extObjCls,states)
equation
(vars1,knvars1,extVars1,eqns1,reqns1,ieqns1,aeqns1,iaeqns1,algs1,ialgs1,whenclauses_1,extObjCls1,states) = lower2(daeElts, functionTree, states, vars, knvars, extVars, whenclauses);
vars = vars1; // vars = BackendVariable.mergeVariables(vars1, vars2);
knvars = knvars1; // knvars = BackendVariable.mergeVariables(knvars1, knvars2);
extVars = extVars1; // extVars = BackendVariable.mergeVariables(extVars1,extVars2);
eqns = listAppend(eqns1, eqns);
ieqns = listAppend(ieqns1, ieqns);
reqns = listAppend(reqns1, reqns);
aeqns = listAppend(aeqns1, aeqns);
iaeqns = listAppend(iaeqns1, iaeqns);
algs = listAppend(algs1, algs);
ialgs = listAppend(ialgs1, ialgs);
extObjCls = listAppend(extObjCls1,extObjCls);
(vars,knvars,extVars,eqns,reqns,ieqns,aeqns,iaeqns,algs,ialgs,whenclauses,extObjCls,states) = lower2(listReverse(daeElts),functionTree,vars,knvars,extVars,eqns,reqns,ieqns,aeqns,iaeqns,algs,ialgs,whenclauses,extObjCls,states);
then
(vars,knvars,extVars,eqns,reqns,ieqns,aeqns,iaeqns,algs,ialgs,whenclauses_1,extObjCls,states);
(vars,knvars,extVars,eqns,reqns,ieqns,aeqns,iaeqns,algs,ialgs,whenclauses,extObjCls,states);

// assert in equation section is converted to ALGORITHM
case (DAE.ASSERT(cond,msg,source),functionTree,vars,knvars,extVars,eqns,reqns,ieqns,aeqns,iaeqns,algs,ialgs,whenclauses_1,extObjCls,states)
Expand Down
54 changes: 29 additions & 25 deletions Compiler/BackEnd/BackendDAEUtil.mo
Expand Up @@ -2608,7 +2608,7 @@ protected function treeGet2 "function: treeGet2
input String inString;
output BackendDAE.Value outValue;
algorithm
outValue := matchcontinue (inBinTree,inString)
outValue := match (inBinTree,inString)
local
String rkeystr,keystr;
DAE.ComponentRef rkey;
Expand All @@ -2617,34 +2617,38 @@ algorithm
BackendDAE.BinTree right, left;

// found it
case (BackendDAE.TREENODE(value = SOME(BackendDAE.TREEVALUE(rkey,rval))),keystr)
case (BackendDAE.TREENODE(value = SOME(BackendDAE.TREEVALUE(key=rkey))),keystr)
equation
rkeystr = ComponentReference.printComponentRefStr(rkey);
0 = stringCompare(rkeystr, keystr);
then
rval;

then treeGet3(inBinTree, keystr, stringCompare(rkeystr, keystr));
end match;
end treeGet2;

protected function treeGet3
"Helper function to treeGet"
input BackendDAE.BinTree inBinTree;
input String inString;
input Integer compResult;
output BackendDAE.Value outValue;
algorithm
outValue := match (inBinTree,inString,compResult)
local
String rkeystr,keystr;
DAE.ComponentRef rkey;
BackendDAE.Value rval,cmpval,res;
Option<BackendDAE.BinTree> optLeft,optRight;
BackendDAE.BinTree right, left;

// found it
case (BackendDAE.TREENODE(value = SOME(BackendDAE.TREEVALUE(value=rval))),keystr,0) then rval;
// search right
case (BackendDAE.TREENODE(value = SOME(BackendDAE.TREEVALUE(rkey,rval)),rightSubTree = SOME(right)),keystr)
equation
rkeystr = ComponentReference.printComponentRefStr(rkey) "Search to the right" ;
cmpval = stringCompare(rkeystr, keystr);
(cmpval > 0) = true;
res = treeGet2(right, keystr);
then
res;

case (BackendDAE.TREENODE(rightSubTree = SOME(right)),keystr,1)
then treeGet2(right, keystr);
// search left
case (BackendDAE.TREENODE(value = SOME(BackendDAE.TREEVALUE(rkey,rval)),leftSubTree = SOME(left)),keystr)
equation
rkeystr = ComponentReference.printComponentRefStr(rkey) "Search to the left" ;
cmpval = stringCompare(rkeystr, keystr);
(cmpval > 0) = false;
res = treeGet2(left, keystr);
then
res;
end matchcontinue;
end treeGet2;
case (BackendDAE.TREENODE(leftSubTree = SOME(left)),keystr,-1)
then treeGet2(left, keystr);
end match;
end treeGet3;

public function treeAddList "function: treeAddList
author: Frenkel TUD"
Expand Down

0 comments on commit f93f6ba

Please sign in to comment.