Skip to content

Commit

Permalink
- added BugFix for #1872
Browse files Browse the repository at this point in the history
   Bugfix is for now a workaround that prevents the collation of STMT_WHEN conditions. 
   Since I want to rewrite some of that helpVar stuff with regarding to Ticket #1866, this should work for now.



git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@13276 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Willi Braun committed Oct 9, 2012
1 parent 101924f commit 219dce8
Show file tree
Hide file tree
Showing 2 changed files with 339 additions and 3 deletions.
91 changes: 88 additions & 3 deletions Compiler/BackEnd/BackendDAEUtil.mo
Expand Up @@ -2975,13 +2975,98 @@ algorithm
local list<DAE.Statement> statementLst;
case(DAE.ALGORITHM_STMTS(statementLst=statementLst),_)
equation
(statementLst,_) = DAEUtil.traverseDAEEquationsStmts(statementLst, collateArrExp, infuncs);
(statementLst,_) = DAEUtil.traverseDAEStmts(statementLst, collateArrExpStmt, infuncs);
then
DAE.ALGORITHM_STMTS(statementLst);
case (_,_) then inAlg;
end matchcontinue;
end collateAlgorithm;

protected function collateArrExpStmt
" Author: Frenkel TUD 2010-07
wbraun: added as workaround for when condition.
As long as we don't support fully array helpVars, we
we can't collate the expression of a when condition.
"
input tuple<DAE.Exp, DAE.Statement, Option<DAE.FunctionTree>> itpl;
output tuple<DAE.Exp, Option<DAE.FunctionTree>> otpl;
algorithm
otpl := matchcontinue itpl
local
DAE.Exp e;
DAE.Statement x;
Option<DAE.FunctionTree> funcs;
case ((e, x, funcs))
equation
((e, (_, _))) = Expression.traverseExp(e, traversingcollateArrExpStmt, (x, funcs));
then ((e,funcs));
case ((e, x, funcs)) then ((e,funcs));
end matchcontinue;
end collateArrExpStmt;

protected function traversingcollateArrExpStmt "
Author: Frenkel TUD 2010-07.
wbraun: added as workaround for when condition.
As long as we don't support fully array helpVars, we
we can't collate the expression of a when condition.
"
input tuple<DAE.Exp, tuple<DAE.Statement, Option<DAE.FunctionTree>> > inExp;
output tuple<DAE.Exp, tuple<DAE.Statement, Option<DAE.FunctionTree>> > outExp;
algorithm outExp := matchcontinue(inExp)
local
Option<DAE.FunctionTree> funcs;
DAE.ComponentRef cr;
DAE.Type ty;
Integer i;
DAE.Exp e,e1,e1_1,e1_2;
Boolean b;
DAE.Statement x;
// do nothing if try to collate when codition expression
case ((e as DAE.MATRIX(ty=ty,integer=i,matrix=((e1 as DAE.CREF(componentRef = cr))::_)::_), (x as DAE.STMT_WHEN(exp=_), funcs)))
then
((e,(x,funcs)));
case ((e as DAE.MATRIX(ty=ty,integer=i,matrix=(((e1 as DAE.UNARY(exp = DAE.CREF(componentRef = cr))))::_)::_), (x as DAE.STMT_WHEN(exp=_), funcs)))
then
((e,(x,funcs)));
case ((e as DAE.ARRAY(ty=ty,scalar=b,array=(e1 as DAE.CREF(componentRef = cr))::_), (x as DAE.STMT_WHEN(exp=_), funcs)))
then
((e,(x,funcs)));
case ((e as DAE.ARRAY(ty=ty,scalar=b,array=(e1 as DAE.UNARY(exp = DAE.CREF(componentRef = cr)))::_), (x as DAE.STMT_WHEN(exp=_), funcs)))
then
((e,(x,funcs)));
// collate in other cases
case ((e as DAE.MATRIX(ty=ty,integer=i,matrix=((e1 as DAE.CREF(componentRef = cr))::_)::_), (x, funcs)))
equation
e1_1 = Expression.expStripLastSubs(e1);
((e1_2,(_,true))) = extendArrExp((e1_1,(funcs,false)));
true = Expression.expEqual(e,e1_2);
then
((e1_1,(x,funcs)));
case ((e as DAE.MATRIX(ty=ty,integer=i,matrix=(((e1 as DAE.UNARY(exp = DAE.CREF(componentRef = cr))))::_)::_), (x, funcs)))
equation
e1_1 = Expression.expStripLastSubs(e1);
((e1_2,(_,true))) = extendArrExp((e1_1,(funcs,false)));
true = Expression.expEqual(e,e1_2);
then
((e1_1,(x,funcs)));
case ((e as DAE.ARRAY(ty=ty,scalar=b,array=(e1 as DAE.CREF(componentRef = cr))::_), (x, funcs)))
equation
e1_1 = Expression.expStripLastSubs(e1);
((e1_2,(_,true))) = extendArrExp((e1_1,(funcs,false)));
true = Expression.expEqual(e,e1_2);
then
((e1_1,(x,funcs)));
case ((e as DAE.ARRAY(ty=ty,scalar=b,array=(e1 as DAE.UNARY(exp = DAE.CREF(componentRef = cr)))::_), (x, funcs)))
equation
e1_1 = Expression.expStripLastSubs(e1);
((e1_2,(_,true))) = extendArrExp((e1_1,(funcs,false)));
true = Expression.expEqual(e,e1_2);
then
((e1_1,(x,funcs)));
case _ then inExp;
end matchcontinue;
end traversingcollateArrExpStmt;

public function collateArrExpList
"function collateArrExpList
author Frenkel TUD:
Expand All @@ -2996,7 +3081,7 @@ algorithm
list<DAE.Exp> expl1,expl;

case({},_) then {};

case(e::expl,_) equation
((e1,_)) = collateArrExp((e,optfunc));
expl1 = collateArrExpList(expl,optfunc);
Expand All @@ -3007,7 +3092,7 @@ end collateArrExpList;

public function collateArrExp "
Author: Frenkel TUD 2010-07"
input tuple<DAE.Exp,Option<DAE.FunctionTree>> itpl;
input tuple<DAE.Exp, Option<DAE.FunctionTree>> itpl;
output tuple<DAE.Exp,Option<DAE.FunctionTree>> otpl;
algorithm
otpl := matchcontinue itpl
Expand Down
251 changes: 251 additions & 0 deletions Compiler/FrontEnd/DAEUtil.mo
Expand Up @@ -4348,6 +4348,257 @@ algorithm
end match;
end traverseDAEEquationsStmtsElse;

public function traverseDAEStmts
"function: traverseDAEStmts
Author: BZ, 2008-12, wbraun 2012-09
Traversing statemeant and provide current statement
to FuncExptype
Handles the traversing of DAE.Statement."
input list<DAE.Statement> inStmts;
input FuncExpType func;
input Type_a iextraArg;
output list<DAE.Statement> outStmts;
output Type_a oextraArg;
partial function FuncExpType
input tuple<DAE.Exp, DAE.Statement, Type_a> arg;
output tuple<DAE.Exp, Type_a> oarg;
end FuncExpType;
replaceable type Type_a subtypeof Any;
algorithm
(outStmts,oextraArg) := matchcontinue(inStmts,func,iextraArg)
local
DAE.Exp e_1,e_2,e,e2,e3,e_3;
list<DAE.Exp> expl1,expl2;
DAE.ComponentRef cr_1,cr;
list<DAE.Statement> xs_1,xs,stmts,stmts1,stmts2;
DAE.Type tp;
DAE.Statement x,ew,ew_1;
Boolean b1;
String id1,str;
list<Integer> li;
Integer ix;
DAE.ElementSource source;
Algorithm.Else algElse;
Type_a extraArg;
list<tuple<DAE.ComponentRef,Absyn.Info>> loopPrlVars "list of parallel variables used/referenced in the parfor loop";

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

case (((x as DAE.STMT_ASSIGN(type_ = tp,exp1 = e2,exp = e, source = source)) :: xs),_,extraArg)
equation
((e_1, extraArg)) = func((e, x, extraArg));
((e_2, extraArg)) = func((e2, x, extraArg));
(xs_1,extraArg) = traverseDAEStmts(xs, func, extraArg);
then (DAE.STMT_ASSIGN(tp,e_2,e_1,source) :: xs_1,extraArg);

case (((x as DAE.STMT_TUPLE_ASSIGN(type_ = tp,expExpLst = expl1, exp = e, source = source)) :: xs),_,extraArg)
equation
((e_1, extraArg)) = func((e, x, extraArg));
(expl2, extraArg) = traverseDAEExpListStmt(expl1,func, x, extraArg);
(xs_1, extraArg) = traverseDAEStmts(xs, func, extraArg);
then ((DAE.STMT_TUPLE_ASSIGN(tp,expl2,e_1,source) :: xs_1),extraArg);

case (((x as DAE.STMT_ASSIGN_ARR(type_ = tp,componentRef = cr, exp = e, source = source)) :: xs),_,extraArg)
equation
((e_1, extraArg)) = func((e, x, extraArg));
((e_2 as DAE.CREF(cr_1,_), extraArg)) = func((Expression.crefExp(cr), x, extraArg));
(xs_1, extraArg) = traverseDAEStmts(xs, func, extraArg);
then (DAE.STMT_ASSIGN_ARR(tp,cr_1,e_1,source) :: xs_1,extraArg);

case (((x as DAE.STMT_ASSIGN_ARR(type_ = tp,componentRef = cr, exp = e, source = source)) :: xs),_,extraArg)
equation
((e_1, extraArg)) = func((e, x, extraArg));
failure(((DAE.CREF(_,_), _)) = func((Expression.crefExp(cr), x, extraArg)));
// We need to pass this through because simplify/etc may scalarize the cref...
// true = Flags.isSet(Flags.FAILTRACE);
// print(DAEDump.ppStatementStr(x));
// print("Warning, not allowed to set the componentRef to a expression in DAEUtil.traverseDAEEquationsStmts\n");
(xs_1, extraArg) = traverseDAEStmts(xs, func, extraArg);
then (DAE.STMT_ASSIGN_ARR(tp,cr,e_1,source) :: xs_1,extraArg);

case (((x as DAE.STMT_IF(exp=e,statementLst=stmts,else_ = algElse, source = source)) :: xs),_,extraArg)
equation
(algElse,extraArg) = traverseDAEStmtsElse(algElse,func, x, extraArg);
(stmts2,extraArg) = traverseDAEStmts(stmts,func,extraArg);
((e_1,extraArg)) = func((e, x, extraArg));
(xs_1,extraArg) = traverseDAEStmts(xs, func, extraArg);
stmts1 = Algorithm.optimizeIf(e_1,stmts2,algElse,source);
then (listAppend(stmts1, xs_1),extraArg);

case (((x as DAE.STMT_FOR(type_=tp,iterIsArray=b1,iter=id1,index=ix,range=e,statementLst=stmts, source = source)) :: xs),_,extraArg)
equation
(stmts2, extraArg) = traverseDAEStmts(stmts,func,extraArg);
((e_1, extraArg)) = func((e, x, extraArg));
(xs_1, extraArg) = traverseDAEStmts(xs, func, extraArg);
then (DAE.STMT_FOR(tp,b1,id1,ix,e_1,stmts2,source) :: xs_1,extraArg);

case (((x as DAE.STMT_PARFOR(type_=tp,iterIsArray=b1,iter=id1,index=ix,range=e,statementLst=stmts, loopPrlVars=loopPrlVars, source = source)) :: xs),_,extraArg)
equation
(stmts2, extraArg) = traverseDAEStmts(stmts,func,extraArg);
((e_1, extraArg)) = func((e, x, extraArg));
(xs_1, extraArg) = traverseDAEStmts(xs, func, extraArg);
then (DAE.STMT_PARFOR(tp,b1,id1,ix,e_1,stmts2,loopPrlVars,source) :: xs_1,extraArg);

case (((x as DAE.STMT_WHILE(exp = e,statementLst=stmts, source = source)) :: xs),_,extraArg)
equation
(stmts2, extraArg) = traverseDAEStmts(stmts,func,extraArg);
((e_1, extraArg)) = func((e, x, extraArg));
(xs_1, extraArg) = traverseDAEStmts(xs, func, extraArg);
then (DAE.STMT_WHILE(e_1,stmts2,source) :: xs_1,extraArg);

case (((x as DAE.STMT_WHEN(exp = e,statementLst=stmts,elseWhen=NONE(),helpVarIndices=li, source = source)) :: xs),_,extraArg)
equation
(stmts2, extraArg) = traverseDAEStmts(stmts,func,extraArg);
((e_1, extraArg)) = func((e, x, extraArg));
(xs_1, extraArg) = traverseDAEStmts(xs, func, extraArg);
then (DAE.STMT_WHEN(e_1,stmts2,NONE(),li,source) :: xs_1,extraArg);

case (((x as DAE.STMT_WHEN(exp = e,statementLst=stmts,elseWhen=SOME(ew),helpVarIndices=li, source = source)) :: xs),_,extraArg)
equation
({ew_1}, extraArg) = traverseDAEStmts({ew},func,extraArg);
(stmts2, extraArg) = traverseDAEStmts(stmts,func,extraArg);
((e_1, extraArg)) = func((e, x, extraArg));
(xs_1, extraArg) = traverseDAEStmts(xs, func, extraArg);
then (DAE.STMT_WHEN(e_1,stmts2,SOME(ew),li,source) :: xs_1,extraArg);

case (((x as DAE.STMT_ASSERT(cond = e, msg=e2, level=e3, source = source)) :: xs),_,extraArg)
equation
((e_1, extraArg)) = func((e, x, extraArg));
((e_2, extraArg)) = func((e2, x, extraArg));
((e_3, extraArg)) = func((e3, x, extraArg));
(xs_1, extraArg) = traverseDAEStmts(xs, func, extraArg);
then (DAE.STMT_ASSERT(e_1,e_2,e_3,source) :: xs_1,extraArg);

case (((x as DAE.STMT_TERMINATE(msg = e, source = source)) :: xs),_,extraArg)
equation
((e_1, extraArg)) = func((e, x, extraArg));
(xs_1, extraArg) = traverseDAEStmts(xs, func, extraArg);
then (DAE.STMT_TERMINATE(e_1,source) :: xs_1,extraArg);

case (((x as DAE.STMT_REINIT(var = e,value=e2, source = source)) :: xs),_,extraArg)
equation
((e_1, extraArg)) = func((e, x, extraArg));
((e_2, extraArg)) = func((e2, x, extraArg));
(xs_1, extraArg) = traverseDAEStmts(xs, func, extraArg);
then (DAE.STMT_REINIT(e_1,e_2,source) :: xs_1,extraArg);

case (((x as DAE.STMT_NORETCALL(exp = e, source = source)) :: xs),_,extraArg)
equation
((e_1, extraArg)) = func((e, x, extraArg));
(xs_1, extraArg) = traverseDAEStmts(xs, func, extraArg);
then (DAE.STMT_NORETCALL(e_1,source) :: xs_1,extraArg);

case (((x as DAE.STMT_RETURN(source = source)) :: xs),_,extraArg)
equation
(xs_1, extraArg) = traverseDAEStmts(xs, func, extraArg);
then (x :: xs_1,extraArg);

case (((x as DAE.STMT_BREAK(source = source)) :: xs),_,extraArg)
equation
(xs_1, extraArg) = traverseDAEStmts(xs, func, extraArg);
then (x :: xs_1,extraArg);

// MetaModelica extension. KS
case (((x as DAE.STMT_FAILURE(body=stmts, source = source)) :: xs),_,extraArg)
equation
(stmts2, extraArg) = traverseDAEStmts(stmts,func,extraArg);
(xs_1, extraArg) = traverseDAEStmts(xs, func, extraArg);
then (DAE.STMT_FAILURE(stmts2,source) :: xs_1,extraArg);

case (((x as DAE.STMT_TRY(tryBody=stmts, source = source)) :: xs),_,extraArg)
equation
(stmts2, extraArg) = traverseDAEStmts(stmts,func,extraArg);
(xs_1, extraArg) = traverseDAEStmts(xs, func, extraArg);
then (DAE.STMT_TRY(stmts2,source) :: xs_1,extraArg);

case (((x as DAE.STMT_CATCH(catchBody=stmts, source = source)) :: xs),_,extraArg)
equation
(stmts2, extraArg) = traverseDAEStmts(stmts,func,extraArg);
(xs_1, extraArg) = traverseDAEStmts(xs, func, extraArg);
then (DAE.STMT_CATCH(stmts2,source) :: xs_1,extraArg);

case (((x as DAE.STMT_THROW(source = source)) :: xs),_,extraArg)
equation
(xs_1, extraArg) = traverseDAEStmts(xs, func, extraArg);
then (x :: xs_1,extraArg);

case ((x :: xs),_,extraArg)
equation
str = DAEDump.ppStatementStr(x);
str = "DAEUtil.traverseDAEStmts not implemented correctly: " +& str;
Error.addMessage(Error.INTERNAL_ERROR, {str});
then fail();
end matchcontinue;
end traverseDAEStmts;

protected function traverseDAEStmtsElse "
Author: BZ, 2008-12
Helper function for traverseDAEEquationsStmts
"
input Algorithm.Else inElse;
input FuncExpType func;
input DAE.Statement istmt;
input Type_a iextraArg;
output Algorithm.Else outElse;
output Type_a oextraArg;
partial function FuncExpType
input tuple<DAE.Exp, DAE.Statement, Type_a> arg;
output tuple<DAE.Exp, Type_a> oarg;
end FuncExpType;
replaceable type Type_a subtypeof Any;
algorithm
(outElse,oextraArg) := match(inElse, func, istmt, iextraArg)
local
DAE.Exp e,e_1;
list<DAE.Statement> st,st_1;
Algorithm.Else el,el_1;
Type_a extraArg;
case (DAE.NOELSE(),_,_,extraArg) then (DAE.NOELSE(),extraArg);
case (DAE.ELSEIF(e,st,el),_,_,extraArg)
equation
(el_1,extraArg) = traverseDAEStmtsElse(el,func,istmt,extraArg);
(st_1,extraArg) = traverseDAEStmts(st,func,extraArg);
((e_1,extraArg)) = func((e, istmt, extraArg));
then (Algorithm.optimizeElseIf(e_1,st_1,el_1),extraArg);
case(DAE.ELSE(st),_,_,extraArg)
equation
(st_1,extraArg) = traverseDAEStmts(st,func,extraArg);
then (DAE.ELSE(st_1),extraArg);
end match;
end traverseDAEStmtsElse;

protected function traverseDAEExpListStmt "
Author: BZ, 2008-12
Traverse an list of expressions, helper function for traverseDAE"
input list<DAE.Exp> iexps;
input FuncExpType func;
input DAE.Statement istmt;
input Type_a iextraArg;
output list<DAE.Exp> oexps;
output Type_a oextraArg;
partial function FuncExpType
input tuple<DAE.Exp, DAE.Statement, Type_a> arg;
output tuple<DAE.Exp,Type_a> oarg;
end FuncExpType;
replaceable type Type_a subtypeof Any;
algorithm
(oexps,oextraArg) := match(iexps, func, istmt, iextraArg)
local
DAE.Exp e;
Type_a extraArg;
list<DAE.Exp> exps;

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

case(e::exps,_,_,extraArg)
equation
((e,extraArg)) = func((e, istmt, extraArg));
(oexps,extraArg) = traverseDAEExpListStmt(exps, func, istmt, extraArg);
then
(e::oexps,extraArg);
end match;
end traverseDAEExpListStmt;

protected function traverseDAEVarAttr "
Author: BZ, 2008-12
Help function to traverseDAE
Expand Down

0 comments on commit 219dce8

Please sign in to comment.