Skip to content

Commit

Permalink
- Added test for case guard-conditions
Browse files Browse the repository at this point in the history
- Fixed a bug where tail-recursion rec(i-1,i) would cause rec(i-1,i-1) to be used instead


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@12526 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Aug 15, 2012
1 parent 0bab035 commit 2d23f00
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 5 deletions.
38 changes: 38 additions & 0 deletions Compiler/FrontEnd/Expression.mo
Original file line number Diff line number Diff line change
Expand Up @@ -4862,6 +4862,44 @@ algorithm
end matchcontinue;
end traversingexpHasCref;

public function expHasCrefName "Returns a true if the exp contains a cref that starts with the given name"
input DAE.Exp inExp;
input String name;
output Boolean hasCref;
algorithm
((_,(_,hasCref))) := traverseExpTopDown(inExp, traversingexpHasName, (name,false));
end expHasCrefName;

public function anyExpHasCrefName "Returns a true if any exp contains a cref that starts with the given name"
input list<DAE.Exp> inExps;
input String name;
output Boolean hasCref;
algorithm
hasCref := List.fold(List.map1(inExps, expHasCrefName, name), boolOr, false);
end anyExpHasCrefName;

public function traversingexpHasName "Returns a true if the exp contains a cref that starts with the given name"
input tuple<DAE.Exp, tuple<String,Boolean>> inExp;
output tuple<DAE.Exp, Boolean, tuple<String,Boolean>> outExp;
algorithm
outExp := matchcontinue(inExp)
local
Boolean b;
String name;
DAE.ComponentRef cr;
DAE.Exp e;

case ((e as DAE.CREF(componentRef = cr), (name,false)))
equation
b = name ==& ComponentReference.crefFirstIdent(cr);
then
((e,not b,(name,b)));

case (((e,(name,b)))) then ((e,not b,(name,b)));

end matchcontinue;
end traversingexpHasName;

public function expHasDerCref "
@author: Frenkel TUD 2012-06
returns true if the expression contains the cref in function der"
Expand Down
18 changes: 13 additions & 5 deletions Compiler/Template/CodegenC.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -6280,9 +6280,11 @@ template daeExpCall(Exp call, Context context, Text &preExp /*BUFP*/, Text &varD
unboxRecord(argStr, ty, &preExp, &varDecls)

case exp as CALL(attr=attr as CALL_ATTR(tailCall=tail as TAIL(__))) then
let &postExp = buffer ""
let tail = daeExpTailCall(expLst,tail.vars,context,&preExp,&postExp,&varDecls)
let res = <<
/* Tail recursive call <%printExpStr(exp)%> */
<%daeExpTailCall(expLst,tail.vars,context,&preExp,&varDecls)%>goto _tailrecursive;
<%tail%><%&postExp%>goto _tailrecursive;
/* TODO: Make sure any eventual dead code below is never generated */
>>
let &preExp += res
Expand Down Expand Up @@ -6329,7 +6331,7 @@ template daeExpCall(Exp call, Context context, Text &preExp /*BUFP*/, Text &varD
'<%retVar%>'
end daeExpCall;

template daeExpTailCall(list<DAE.Exp> es, list<String> vs, Context context, Text &preExp, Text &varDecls)
template daeExpTailCall(list<DAE.Exp> es, list<String> vs, Context context, Text &preExp, Text &postExp, Text &varDecls)
::=
match es
case e::erest then
Expand All @@ -6340,10 +6342,16 @@ template daeExpTailCall(list<DAE.Exp> es, list<String> vs, Context context, Text
case CREF(componentRef = cr, ty = T_FUNCTION_REFERENCE_VAR(__)) then
// adrpo: ignore _x = _x!
if stringEq(v, crefStr(cr))
then '<%daeExpTailCall(erest, vrest, context, &preExp, &varDecls)%>'
else '_<%v%> = <%exp%>;<%\n%><%daeExpTailCall(erest, vrest, context, &preExp, &varDecls)%>'
then '<%daeExpTailCall(erest, vrest, context, &preExp, &postExp, &varDecls)%>'
else '_<%v%> = <%exp%>;<%\n%><%daeExpTailCall(erest, vrest, context, &preExp, &postExp, &varDecls)%>'
case _ then
'_<%v%> = <%exp%>;<%\n%><%daeExpTailCall(erest, vrest, context, &preExp, &varDecls)%>'
(if anyExpHasCrefName(erest, v) then
/* We might overwrite a value with something else, so make an extra copy of it */
let tmp = tempDecl(expTypeFromExpModelica(e),&varDecls)
let &postExp += '_<%v%> = <%tmp%>;<%\n%>'
'<%tmp%> = <%exp%>;<%\n%><%daeExpTailCall(erest, vrest, context, &preExp, &postExp, &varDecls)%>'
else
'_<%v%> = <%exp%>;<%\n%><%daeExpTailCall(erest, vrest, context, &preExp, &postExp, &varDecls)%>')
end daeExpTailCall;

template daeExpCallBuiltinPrefix(Boolean builtin)
Expand Down
13 changes: 13 additions & 0 deletions Compiler/Template/SimCodeTV.mo
Original file line number Diff line number Diff line change
Expand Up @@ -2183,6 +2183,7 @@ package ComponentReference
input DAE.ComponentRef cr;
output DAE.ComponentRef ocr;
end appendStringCref;

end ComponentReference;

package Expression
Expand Down Expand Up @@ -2217,6 +2218,18 @@ package Expression
output Boolean b;
end isArrayType;

function expHasCrefName "Returns a true if the exp contains a cref that starts with the given name"
input DAE.Exp inExp;
input String name;
output Boolean hasCref;
end expHasCrefName;

function anyExpHasCrefName "Returns a true if the exp contains a cref that starts with the given name"
input list<DAE.Exp> inExps;
input String name;
output Boolean hasCref;
end anyExpHasCrefName;

end Expression;

package ExpressionDump
Expand Down

0 comments on commit 2d23f00

Please sign in to comment.