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@17740 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Oct 17, 2013
1 parent b19b90d commit 63d8680
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 52 deletions.
90 changes: 53 additions & 37 deletions Compiler/BackEnd/BackendDAETransform.mo
Expand Up @@ -1070,23 +1070,7 @@ Use +d=failtrace for more information."});
end matchcontinue;
end tarjanAlgorithm;

public function strongConnectMain "author: PA

Helper function to strong_components

inputs: (IncidenceMatrix,
IncidenceMatrixT,
int vector, /* Assignment */
int vector, /* Assignment */
int vector, /* Number */
int vector, /* Lowlink */
int, /* n - number of equations */
int, /* i */
int, /* w */
int list, /* stack */
int list list /* components */)
outputs: (int /* i */, int list /* stack */, int list list /* components */)
"
public function strongConnectMain
input BackendDAE.IncidenceMatrixT mt;
input array<Integer> a2;
input array<Integer> number;
Expand All @@ -1099,34 +1083,66 @@ public function strongConnectMain "author: PA
output list<Integer> ostack;
output list<list<Integer>> ocomps;
algorithm
(ostack,ocomps):=
matchcontinue (mt,a2,number,lowlink,stackflag,n,w,istack,icomps)
(ostack,ocomps) := strongConnectMain2(w>n,mt,a2,number,lowlink,stackflag,n,w,istack,icomps);
end strongConnectMain;

protected function strongConnectMain2
input Boolean stop;
input BackendDAE.IncidenceMatrixT mt;
input array<Integer> a2;
input array<Integer> number;
input array<Integer> lowlink;
input array<Boolean> stackflag;
input Integer n;
input Integer w;
input list<Integer> istack;
input list<list<Integer>> icomps;
output list<Integer> ostack;
output list<list<Integer>> ocomps;
algorithm
(ostack,ocomps) := match (stop,mt,a2,number,lowlink,stackflag,n,w,istack,icomps)
local
Integer num;
list<Integer> stack;
list<list<Integer>> comps;

case (_,_,_,_,_,_,_,_,_)
case (true,_,_,_,_,_,_,_,_,_)
then (istack,icomps);
else
equation
(w > n) = true;
then
(istack,icomps);
case (_,_,_,_,_,_,_,_,_)
(stack,comps) = strongConnectMain3(intEq(number[w],0),mt,a2,number,lowlink,stackflag,n,w,istack,icomps);
(stack,comps) = strongConnectMain2(w+1>n,mt,a2,number,lowlink, stackflag, n, w + 1, stack, comps);
then (stack,comps);
end match;
end strongConnectMain2;

protected function strongConnectMain3
input Boolean doCalc;
input BackendDAE.IncidenceMatrixT mt;
input array<Integer> a2;
input array<Integer> number;
input array<Integer> lowlink;
input array<Boolean> stackflag;
input Integer n;
input Integer w;
input list<Integer> istack;
input list<list<Integer>> icomps;
output list<Integer> ostack;
output list<list<Integer>> ocomps;
algorithm
(ostack,ocomps) := match (doCalc,mt,a2,number,lowlink,stackflag,n,w,istack,icomps)
local
Integer num;
list<Integer> stack;
list<list<Integer>> comps;

case (true,_,_,_,_,_,_,_,_,_)
equation
true = intEq(number[w],0);
(_,stack,comps) = strongConnect(mt,a2,number,lowlink,stackflag,0,w,istack,icomps);
(stack,comps) = strongConnectMain(mt,a2,number,lowlink,stackflag,n,w + 1,stack,comps);
then
(stack,comps);
else
equation
num = number[w];
(num == 0) = false;
(stack,comps) = strongConnectMain(mt,a2,number,lowlink, stackflag, n, w + 1, istack, icomps);
then
(stack,comps);
end matchcontinue;
end strongConnectMain;
then (stack,comps);
else (istack,icomps);
end match;
end strongConnectMain3;

protected function strongConnect "author: PA

Expand Down
39 changes: 24 additions & 15 deletions Compiler/FrontEnd/Inline.mo
Expand Up @@ -431,32 +431,41 @@ end updateArrayCond;
protected function inlineVarOptArray
"functio: inlineVarOptArray
inlines calls in a variable option"
input Integer Index;
input Integer index;
input array<Option<BackendDAE.Var>> inVarArray;
input Integer arraysize;
input Functiontuple fns;
input Boolean iInlined;
output Boolean oInlined;
algorithm
oInlined := inlineVarOptArrayWork(index > arraysize,index,inVarArray,arraysize,fns,iInlined);
end inlineVarOptArray;

protected function inlineVarOptArrayWork
"functio: inlineVarOptArray
inlines calls in a variable option"
input Boolean stop;
input Integer index;
input array<Option<BackendDAE.Var>> inVarArray;
input Integer arraysize;
input Functiontuple fns;
input Boolean iInlined;
output Boolean oInlined;
algorithm
oInlined := matchcontinue(Index,inVarArray,arraysize,fns,iInlined)
oInlined := match (stop,index,inVarArray,arraysize,fns,iInlined)
local
Option<BackendDAE.Var> var;
Boolean b;
case(_,_,_,_,_)
case (true,_,_,_,_,_)
then iInlined;
else
equation
true = intLe(Index,arraysize);
var = inVarArray[Index];
var = inVarArray[index];
(var,b) = inlineVarOpt(var,fns);
updateArrayCond(b,inVarArray,Index,var);
then
inlineVarOptArray(Index+1,inVarArray,arraysize,fns,b or iInlined);
case(_,_,_,_,_)
equation
false = intLe(Index,arraysize);
then
iInlined;
end matchcontinue;
end inlineVarOptArray;
updateArrayCond(b,inVarArray,index,var);
then inlineVarOptArrayWork(index+1 > arraysize,index+1,inVarArray,arraysize,fns,b or iInlined);
end match;
end inlineVarOptArrayWork;

protected function inlineVarOpt
"functio: inlineVarOpt
Expand Down

0 comments on commit 63d8680

Please sign in to comment.