Skip to content

Commit

Permalink
Tail recursion
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@16845 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Aug 19, 2013
1 parent 26b7ec3 commit 7e9628c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 52 deletions.
59 changes: 23 additions & 36 deletions Compiler/FrontEnd/CheckModel.mo
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ algorithm
DAE.DAE(lst) := inDAELst;
hs := HashSet.emptyHashSet();
(varSize, eqnSize, eqns, hs) := countVarEqnSize(lst, 0, 0, {}, hs);
simpleEqnSize := countSympleEqnSize(eqns, 0, hs);
simpleEqnSize := countSimpleEqnSize(eqns, 0, hs);
end checkModel;

protected function countVarEqnSize
Expand Down Expand Up @@ -585,68 +585,55 @@ algorithm
end matchcontinue;
end statementOutputsCrefFinder;

protected function countSympleEqnSize
protected function countSimpleEqnSize
input list<DAE.Element> inEqns;
input Integer isimpleEqnSize;
input HashSet.HashSet ihs;
output Integer osimpleEqnSize;
algorithm
osimpleEqnSize := matchcontinue(inEqns, isimpleEqnSize, ihs)
osimpleEqnSize := List.fold(List.map1(inEqns, countSimpleEqnSizeWork, ihs), intAdd, 0);
end countSimpleEqnSize;

protected function countSimpleEqnSizeWork
input DAE.Element inEqns;
input HashSet.HashSet ihs;
output Integer osimpleEqnSize;
algorithm
osimpleEqnSize := matchcontinue(inEqns, ihs)
local
DAE.Element elem;
list<DAE.Element> rest;
DAE.Exp e1, e2;
DAE.ComponentRef cr;
DAE.Type tp;
Integer size;
DAE.Dimensions dims;

case ({}, _, _) then isimpleEqnSize;

// equations
case((elem as DAE.EQUATION(exp=e1, scalar=e2))::rest, _, _)
equation
size = simpleEquation(e1, e2, ihs);
then
countSympleEqnSize(rest, isimpleEqnSize+size, ihs);
case(DAE.EQUATION(exp=e1, scalar=e2), _)
then simpleEquation(e1, e2, ihs);

// effort variable equality equations
case ((elem as DAE.EQUEQUATION(cr1 = cr))::rest, _, _)
case (DAE.EQUEQUATION(cr1 = cr), _)
equation
tp = ComponentReference.crefTypeConsiderSubs(cr);
size = Expression.sizeOf(tp);
then
countSympleEqnSize(rest, isimpleEqnSize+size, ihs);
then Expression.sizeOf(tp);

// a solved equation
case ((elem as DAE.DEFINE(componentRef = cr, exp=e2))::rest, _, _)
case (DAE.DEFINE(componentRef = cr, exp=e2), _)
equation
//tp = ComponentReference.crefLastType(cr);
//size = Expression.sizeOf(tp);
e1 = Expression.crefExp(cr);
size = simpleEquation(e1, e2, ihs);
then
countSympleEqnSize(rest, isimpleEqnSize+size, ihs);
then simpleEquation(e1, e2, ihs);

// complex equations
case ((elem as DAE.COMPLEX_EQUATION(lhs = e1, rhs = e2))::rest, _, _)
equation
size = simpleEquation(e1, e2, ihs);
then
countSympleEqnSize(rest, isimpleEqnSize+size, ihs);
case (DAE.COMPLEX_EQUATION(lhs = e1, rhs = e2), _)
then simpleEquation(e1, e2, ihs);

// array equations
case ((elem as DAE.ARRAY_EQUATION(dimension=dims, exp = e1, array = e2))::rest, _, _)
equation
size = simpleEquation(e1, e2, ihs);
then
countSympleEqnSize(rest, isimpleEqnSize+size, ihs);
case (DAE.ARRAY_EQUATION(dimension=dims, exp = e1, array = e2), _)
then simpleEquation(e1, e2, ihs);

case (_::rest, _, _)
then
countSympleEqnSize(rest, isimpleEqnSize, ihs);
else 0;
end matchcontinue;
end countSympleEqnSize;
end countSimpleEqnSizeWork;

protected function simpleEquation
input DAE.Exp e1;
Expand Down
31 changes: 15 additions & 16 deletions Compiler/FrontEnd/ConnectUtil.mo
Original file line number Diff line number Diff line change
Expand Up @@ -399,34 +399,33 @@ protected function daeHasExpandableConnectors
algorithm
hasExpandable := matchcontinue(inDAE)
local
list<DAE.Element> rest_vars;
list<DAE.Element> vars;
DAE.ComponentRef name;
Boolean b;

// if we didn't detect any there aren't any
case (_)
equation
false = System.getHasExpandableConnectors();
then
false;
then false;

case (DAE.DAE({})) then false;

case (DAE.DAE(DAE.VAR(componentRef = name) :: rest_vars))
equation
true = isExpandable(name);
then
true;

case (DAE.DAE(_::rest_vars))
equation
b = daeHasExpandableConnectors(DAE.DAE(rest_vars));
then
b;
case (DAE.DAE(vars)) then List.exist(vars, isVarExpandable);

end matchcontinue;
end daeHasExpandableConnectors;

protected function isVarExpandable
input DAE.Element var;
output Boolean b;
algorithm
b := match var
local
DAE.ComponentRef name;
case DAE.VAR(componentRef = name) then isExpandable(name);
else false;
end match;
end isVarExpandable;

protected function getExpandableVariables
"Goes through a list of variables and returns their crefs"
input list<DAE.Element> inVariables;
Expand Down

0 comments on commit 7e9628c

Please sign in to comment.