Skip to content

Commit baab5bb

Browse files
committed
Some tail recursion
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@15238 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent 4e1a57a commit baab5bb

File tree

2 files changed

+27
-25
lines changed

2 files changed

+27
-25
lines changed

Compiler/FrontEnd/InstSection.mo

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2124,7 +2124,7 @@ algorithm
21242124
(cache, e_1) = Ceval.cevalRangeIfConstant(cache, env, e_1, prop, impl, info);
21252125
(cache,e_2) = PrefixUtil.prefixExp(cache,env, ih, e_1, pre);
21262126
env_1 = addForLoopScope(env, i, t, SCode.VAR(), SOME(cnst));
2127-
(cache,sl_1) = instStatements(cache, env_1, ih, pre, ci_state, sl, source, initial_, impl, unrollForLoops);
2127+
(cache,sl_1) = instStatements(cache, env_1, ih, pre, ci_state, sl, source, initial_, impl, unrollForLoops, {});
21282128
source = DAEUtil.addElementSourceFileInfo(source,info);
21292129
stmt = Algorithm.makeFor(i, e_2, prop, sl_1, source);
21302130
then
@@ -2142,7 +2142,7 @@ algorithm
21422142
(cache, e_1) = Ceval.cevalRangeIfConstant(cache, env, e_1, prop, impl, info);
21432143
(cache,e_2) = PrefixUtil.prefixExp(cache, env, ih, e_1, pre);
21442144
env_1 = addForLoopScope(env, i, t, SCode.VAR(), SOME(cnst));
2145-
(cache,sl_1) = instStatements(cache,env_1,ih,pre,ci_state,sl,source,initial_,impl,unrollForLoops);
2145+
(cache,sl_1) = instStatements(cache,env_1,ih,pre,ci_state,sl,source,initial_,impl,unrollForLoops,{});
21462146
source = DAEUtil.addElementSourceFileInfo(source,info);
21472147
stmt = Algorithm.makeFor(i, e_2, prop, sl_1, source);
21482148
then
@@ -2262,7 +2262,7 @@ algorithm
22622262
ci_state = ClassInf.trans(ci_state,ClassInf.FOUND_ALGORITHM());
22632263
source = DAEUtil.createElementSource(Absyn.dummyInfo, Env.getEnvPath(env), PrefixUtil.prefixToCrefOpt(pre), NONE(), NONE());
22642264

2265-
(cache,statements_1) = instStatements(cache, env, ih, pre, ci_state, statements, source, SCode.NON_INITIAL(), impl, unrollForLoops);
2265+
(cache,statements_1) = instStatements(cache, env, ih, pre, ci_state, statements, source, SCode.NON_INITIAL(), impl, unrollForLoops, {});
22662266
(statements_1,_) = DAEUtil.traverseDAEEquationsStmts(statements_1,Expression.traverseSubexpressionsHelper,(ExpressionSimplify.simplifyWork,(false,ExpressionSimplify.optionSimplifyOnly)));
22672267

22682268
dae = DAE.DAE({DAE.ALGORITHM(DAE.ALGORITHM_STMTS(statements_1),source)});
@@ -2330,7 +2330,7 @@ algorithm
23302330
// set the source of this element
23312331
source = DAEUtil.createElementSource(Absyn.dummyInfo, Env.getEnvPath(env), PrefixUtil.prefixToCrefOpt(pre), NONE(), NONE());
23322332

2333-
(cache,statements_1) = instStatements(cache, env, ih, pre, ci_state, statements, source, SCode.INITIAL(), impl, unrollForLoops);
2333+
(cache,statements_1) = instStatements(cache, env, ih, pre, ci_state, statements, source, SCode.INITIAL(), impl, unrollForLoops, {});
23342334
(statements_1,_) = DAEUtil.traverseDAEEquationsStmts(statements_1,Expression.traverseSubexpressionsHelper,(ExpressionSimplify.simplifyWork,(false,ExpressionSimplify.optionSimplifyOnly)));
23352335

23362336
dae = DAE.DAE({DAE.INITIALALGORITHM(DAE.ALGORITHM_STMTS(statements_1),source)});
@@ -2413,10 +2413,11 @@ public function instStatements
24132413
input SCode.Initial initial_;
24142414
input Boolean inBoolean;
24152415
input Boolean unrollForLoops "we should unroll for loops if they are part of an algorithm in a model";
2416+
input list<list<DAE.Statement>> acc;
24162417
output Env.Cache outCache;
24172418
output list<DAE.Statement> outAlgorithmStatementLst;
24182419
algorithm
2419-
(outCache,outAlgorithmStatementLst) := match (inCache,inEnv,inIH,inPre,ci_state,inAbsynAlgorithmLst,source,initial_,inBoolean,unrollForLoops)
2420+
(outCache,outAlgorithmStatementLst) := match (inCache,inEnv,inIH,inPre,ci_state,inAbsynAlgorithmLst,source,initial_,inBoolean,unrollForLoops,acc)
24202421
local
24212422
list<Env.Frame> env;
24222423
Boolean impl;
@@ -2428,16 +2429,17 @@ algorithm
24282429
InstanceHierarchy ih;
24292430

24302431
// empty case
2431-
case (cache,_,_,_,_,{},_,_,_,_) then (cache,{});
2432+
case (cache,_,_,_,_,{},_,_,_,_,_)
2433+
equation
2434+
stmts = List.flatten(listReverse(acc));
2435+
then (cache,stmts);
24322436

24332437
// general case
2434-
case (cache,env,ih,pre,_,(x :: xs),_,_,impl,_)
2438+
case (cache,env,ih,pre,_,(x :: xs),_,_,impl,_,_)
24352439
equation
2436-
(cache,stmts1) = instStatement(cache, env, ih, pre, ci_state, x, source, initial_, impl, unrollForLoops);
2437-
(cache,stmts2) = instStatements(cache, env, ih, pre, ci_state, xs, source, initial_, impl, unrollForLoops);
2438-
stmts = listAppend(stmts1, stmts2);
2439-
then
2440-
(cache,stmts);
2440+
(cache,stmts) = instStatement(cache, env, ih, pre, ci_state, x, source, initial_, impl, unrollForLoops);
2441+
(cache,stmts) = instStatements(cache, env, ih, pre, ci_state, xs, source, initial_, impl, unrollForLoops, stmts::acc);
2442+
then (cache,stmts);
24412443
end match;
24422444
end instStatements;
24432445

@@ -2520,9 +2522,9 @@ algorithm
25202522
(cache,e_1,prop,_) = Static.elabExp(cache,env, e, impl,NONE(),true,pre,info);
25212523
(cache, e_1, prop) = Ceval.cevalIfConstant(cache, env, e_1, prop, impl, info);
25222524
(cache,e_2) = PrefixUtil.prefixExp(cache, env, ih, e_1, pre);
2523-
(cache,tb_1)= instStatements(cache,env,ih,pre, ci_state, tb, source, initial_,impl,unrollForLoops);
2525+
(cache,tb_1)= instStatements(cache,env,ih,pre, ci_state, tb, source, initial_,impl,unrollForLoops,{});
25242526
(cache,eib_1) = instElseIfs(cache,env,ih,pre, ci_state, eib, source, initial_,impl,unrollForLoops,info);
2525-
(cache,fb_1) = instStatements(cache,env,ih,pre, ci_state, fb, source, initial_,impl,unrollForLoops);
2527+
(cache,fb_1) = instStatements(cache,env,ih,pre, ci_state, fb, source, initial_,impl,unrollForLoops,{});
25262528
source = DAEUtil.addElementSourceFileInfo(source, info);
25272529
stmts = Algorithm.makeIf(e_2, prop, tb_1, eib_1, fb_1, source);
25282530
then
@@ -2548,7 +2550,7 @@ algorithm
25482550
(cache,e_1,prop,_) = Static.elabExp(cache, env, e, impl,NONE(), true,pre,info);
25492551
(cache, e_1, prop) = Ceval.cevalIfConstant(cache, env, e_1, prop, impl, info);
25502552
(cache,e_2) = PrefixUtil.prefixExp(cache, env, ih, e_1, pre);
2551-
(cache,sl_1) = instStatements(cache,env,ih,pre,ci_state,sl,source,initial_,impl,unrollForLoops);
2553+
(cache,sl_1) = instStatements(cache,env,ih,pre,ci_state,sl,source,initial_,impl,unrollForLoops,{});
25522554
source = DAEUtil.addElementSourceFileInfo(source, info);
25532555
stmt = Algorithm.makeWhile(e_2, prop, sl_1, source);
25542556
then
@@ -2562,7 +2564,7 @@ algorithm
25622564
(cache,e_1,prop,_) = Static.elabExp(cache, env, e, impl, NONE(), true, pre, info);
25632565
(cache, e_1, prop) = Ceval.cevalIfConstant(cache, env, e_1, prop, impl, info);
25642566
(cache,e_2) = PrefixUtil.prefixExp(cache, env, ih, e_1, pre);
2565-
(cache,sl_1) = instStatements(cache, env, ih, pre, ci_state, sl, source, initial_, impl, unrollForLoops);
2567+
(cache,sl_1) = instStatements(cache, env, ih, pre, ci_state, sl, source, initial_, impl, unrollForLoops, {});
25662568
source = DAEUtil.addElementSourceFileInfo(source, info);
25672569
stmt = Algorithm.makeWhenA(e_2, prop, sl_1, NONE(), source);
25682570
then
@@ -2577,7 +2579,7 @@ algorithm
25772579
(cache,e_1,prop,_) = Static.elabExp(cache, env, e, impl, NONE(), true, pre, info);
25782580
(cache, e_1, prop) = Ceval.cevalIfConstant(cache, env, e_1, prop, impl, info);
25792581
(cache,e_2) = PrefixUtil.prefixExp(cache, env, ih, e_1, pre);
2580-
(cache,sl_1) = instStatements(cache, env, ih, pre, ci_state, sl, source, initial_, impl, unrollForLoops);
2582+
(cache,sl_1) = instStatements(cache, env, ih, pre, ci_state, sl, source, initial_, impl, unrollForLoops, {});
25812583
source = DAEUtil.addElementSourceFileInfo(source, info);
25822584
stmt = Algorithm.makeWhenA(e_2, prop, sl_1, SOME(stmt1), source);
25832585
then
@@ -2689,7 +2691,7 @@ algorithm
26892691
case (cache,env,ih,pre,_,SCode.ALG_FAILURE(stmts = sl, comment = comment, info = info),source,_,impl,_,_)
26902692
equation
26912693
true = Config.acceptMetaModelicaGrammar();
2692-
(cache,sl_1) = instStatements(cache,env,ih,pre,ci_state,sl,source,initial_,impl,unrollForLoops);
2694+
(cache,sl_1) = instStatements(cache,env,ih,pre,ci_state,sl,source,initial_,impl,unrollForLoops,{});
26932695
source = DAEUtil.addElementSourceFileInfo(source, info);
26942696
stmt = DAE.STMT_FAILURE(sl_1,source);
26952697
then
@@ -2699,7 +2701,7 @@ algorithm
26992701
case (cache,env,ih,pre,_,SCode.ALG_TRY(tryBody = sl, comment = comment, info = info),source,_,impl,_,_)
27002702
equation
27012703
true = Config.acceptMetaModelicaGrammar();
2702-
(cache,sl_1) = instStatements(cache, env, ih, pre, ci_state, sl, source, initial_, impl, unrollForLoops);
2704+
(cache,sl_1) = instStatements(cache, env, ih, pre, ci_state, sl, source, initial_, impl, unrollForLoops, {});
27032705
source = DAEUtil.addElementSourceFileInfo(source, info);
27042706
stmt = DAE.STMT_TRY(sl_1,source);
27052707
then
@@ -2709,7 +2711,7 @@ algorithm
27092711
case (cache,env,ih,pre,_,SCode.ALG_CATCH(catchBody = sl, comment = comment, info = info),source,_,impl,_,_)
27102712
equation
27112713
true = Config.acceptMetaModelicaGrammar();
2712-
(cache,sl_1) = instStatements(cache, env, ih, pre, ci_state, sl, source, initial_, impl, unrollForLoops);
2714+
(cache,sl_1) = instStatements(cache, env, ih, pre, ci_state, sl, source, initial_, impl, unrollForLoops, {});
27132715
source = DAEUtil.addElementSourceFileInfo(source, info);
27142716
stmt = DAE.STMT_CATCH(sl_1,source);
27152717
then
@@ -2895,7 +2897,7 @@ algorithm
28952897
// the iterator is not constant but the range is constant
28962898
env_2 = Env.extendFrameForIterator(env_1, i, DAE.T_INTEGER_DEFAULT, DAE.VALBOUND(fst, DAE.BINDING_FROM_DEFAULT_VALUE()), SCode.CONST(), SOME(DAE.C_CONST()));
28972899
/* use instEEquation*/
2898-
(cache,stmts1) = instStatements(cache, env_2, ih, pre, ci_state, algs, source, initial_, impl, unrollForLoops);
2900+
(cache,stmts1) = instStatements(cache, env_2, ih, pre, ci_state, algs, source, initial_, impl, unrollForLoops, {});
28992901
(cache,stmts2) = loopOverRange(cache, env, ih, pre, ci_state, i, Values.ARRAY(rest,dims), algs, source, initial_, impl, unrollForLoops);
29002902
stmts = listAppend(stmts1, stmts2);
29012903
then
@@ -3041,7 +3043,7 @@ algorithm
30413043
(cache,e_1,prop,_) = Static.elabExp(cache, env, e, impl,NONE(), true,pre,info);
30423044
(cache, e_1, prop) = Ceval.cevalIfConstant(cache, env, e_1, prop, impl, info);
30433045
(cache,e_2) = PrefixUtil.prefixExp(cache, env, ih, e_1, pre);
3044-
(cache,stmts) = instStatements(cache, env, ih, pre, ci_state, l, source, initial_, impl, unrollForLoops);
3046+
(cache,stmts) = instStatements(cache, env, ih, pre, ci_state, l, source, initial_, impl, unrollForLoops, {});
30453047
(cache,tail_1) = instElseIfs(cache,env,ih,pre,ci_state,tail, source, initial_, impl, unrollForLoops,info);
30463048
then
30473049
(cache,(e_2,prop,stmts) :: tail_1);
@@ -5147,7 +5149,7 @@ algorithm
51475149
(cache, e_1) = Ceval.cevalRangeIfConstant(cache, env, e_1, prop, impl, info);
51485150
(cache,e_2) = PrefixUtil.prefixExp(cache,env, ih, e_1, pre);
51495151
env_1 = addParForLoopScope(env, i, t, SCode.VAR(), SOME(cnst));
5150-
(cache,sl_1) = instStatements(cache, env_1, ih, pre, ci_state, sl, source, initial_, impl, unrollForLoops);
5152+
(cache,sl_1) = instStatements(cache, env_1, ih, pre, ci_state, sl, source, initial_, impl, unrollForLoops, {});
51515153

51525154
// this is where we check the parfor loop for data parallel specific
51535155
// situations. Start with empty list and collect all variables cref'ed
@@ -5180,7 +5182,7 @@ algorithm
51805182
(cache, e_1) = Ceval.cevalRangeIfConstant(cache, env, e_1, prop, impl, info);
51815183
(cache,e_2) = PrefixUtil.prefixExp(cache, env, ih, e_1, pre);
51825184
env_1 = addParForLoopScope(env, i, t, SCode.VAR(), SOME(cnst));
5183-
(cache,sl_1) = instStatements(cache,env_1,ih,pre,ci_state,sl,source,initial_,impl,unrollForLoops);
5185+
(cache,sl_1) = instStatements(cache,env_1,ih,pre,ci_state,sl,source,initial_,impl,unrollForLoops,{});
51845186
source = DAEUtil.addElementSourceFileInfo(source,info);
51855187
stmt = Algorithm.makeFor(i, e_2, prop, sl_1, source);
51865188
then

Compiler/FrontEnd/Patternm.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1657,7 +1657,7 @@ algorithm
16571657
(cache,elabPatterns) = elabPatternTuple(cache, env, patterns, tys, patternInfo, pattern);
16581658
(cache,eqAlgs) = Static.fromEquationsToAlgAssignments(eq1,{},cache,env,pre);
16591659
algs = SCodeUtil.translateClassdefAlgorithmitems(eqAlgs);
1660-
(cache,body) = InstSection.instStatements(cache, env, InnerOuter.emptyInstHierarchy, pre, ClassInf.FUNCTION(Absyn.IDENT("match")), algs, DAEUtil.addElementSourceFileInfo(DAE.emptyElementSource,patternInfo), SCode.NON_INITIAL(), true, Inst.neverUnroll);
1660+
(cache,body) = InstSection.instStatements(cache, env, InnerOuter.emptyInstHierarchy, pre, ClassInf.FUNCTION(Absyn.IDENT("match")), algs, DAEUtil.addElementSourceFileInfo(DAE.emptyElementSource,patternInfo), SCode.NON_INITIAL(), true, Inst.neverUnroll, {});
16611661
(cache,body,elabResult,resultInfo,resType,st) = elabResultExp(cache,env,body,result,impl,st,performVectorization,pre,resultInfo);
16621662
(cache,dPatternGuard,st) = elabPatternGuard(cache,env,patternGuard,impl,st,performVectorization,pre,patternInfo);
16631663
then (cache,DAE.CASE(elabPatterns, dPatternGuard, caseDecls, body, elabResult, resultInfo, 0, info),elabResult,resType,st);

0 commit comments

Comments
 (0)