From 29d64124a8e5cb1b306927531aadcb421b48dd90 Mon Sep 17 00:00:00 2001 From: Jens Frenkel Date: Thu, 28 Oct 2010 09:48:50 +0000 Subject: [PATCH] - fix some bugs check for records improved rename BackendDAEUtil.traverseExp to traverseAlgorithmExps - Expression.mo delete makeCrefExpNoType use crefExp improve isConst git-svn-id: https://openmodelica.org/svn/OpenModelica/branches/sjoelund-functiontree@6677 f25d12d1-65f4-0310-ae8a-bbce733d8d8e --- Compiler/BackendDAECreate.mo | 16 +- Compiler/BackendDAEOptimize.mo | 1 - Compiler/BackendDAEUtil.mo | 18 +- Compiler/ComponentReference.mo | 6 +- Compiler/DAEUtil.mo | 2 +- Compiler/Expression.mo | 339 +++++++++++++++++++-------------- 6 files changed, 204 insertions(+), 178 deletions(-) diff --git a/Compiler/BackendDAECreate.mo b/Compiler/BackendDAECreate.mo index 6ba2ee62036..5b4ccf16dfb 100644 --- a/Compiler/BackendDAECreate.mo +++ b/Compiler/BackendDAECreate.mo @@ -2861,16 +2861,6 @@ algorithm case (((e as DAE.RELATION(exp1 = e1,operator = op,exp2 = e2)),(zeroCrossings,(vars,knvars)))) equation then ((e,((e :: zeroCrossings),(vars,knvars)))); /* All other functions generate zerocrossing. */ - case (((e as DAE.ARRAY(array = {})),(zeroCrossings,(vars,knvars)))) - equation - then ((e,(zeroCrossings,(vars,knvars)))); - case ((e1 as DAE.ARRAY(ty = tp,scalar = scalar,array = (e :: el)),(zeroCrossings,(vars,knvars)))) - equation - ((_,(zeroCrossings_1,(vars,knvars)))) = Expression.traverseExp(e, collectZeroCrossings, (zeroCrossings,(vars,knvars))); - ((e_1,(zeroCrossings_2,(vars,knvars)))) = collectZeroCrossings((DAE.ARRAY(tp,scalar,el),(zeroCrossings,(vars,knvars)))); - zeroCrossings_3 = listAppend(zeroCrossings_1, zeroCrossings_2); - then - ((e1,(zeroCrossings_3,(vars,knvars)))); case ((e,(zeroCrossings,(vars,knvars)))) equation then ((e,(zeroCrossings,(vars,knvars)))); @@ -3057,10 +3047,9 @@ algorithm list expLst; list> exptpllst; // a=b - case (BackendDAE.COMPLEX_EQUATION(index=i,lhs = DAE.CREF(componentRef=cr1), rhs = DAE.CREF(componentRef=cr2),source = source),funcs) + case (BackendDAE.COMPLEX_EQUATION(index=i,lhs = DAE.CREF(componentRef=cr1,ty= DAE.ET_COMPLEX(varLst=varLst,complexClassType=ClassInf.RECORD(_))), rhs = DAE.CREF(componentRef=cr2),source = source),funcs) equation // create as many equations as the dimension of the record - DAE.ET_COMPLEX(varLst=varLst) = ComponentReference.crefLastType(cr1); e1lst = Util.listMap1(varLst,Expression.generateCrefsExpFromExpVar,cr1); e2lst = Util.listMap1(varLst,Expression.generateCrefsExpFromExpVar,cr2); exptpllst = Util.listThreadTuple(e1lst,e2lst); @@ -3079,11 +3068,10 @@ algorithm then ((complexEqs1,multiEqs2)); // a=Record() - case (BackendDAE.COMPLEX_EQUATION(index=i,lhs = DAE.CREF(componentRef=cr1), rhs = DAE.CALL(path=path,expLst=expLst),source = source),funcs) + case (BackendDAE.COMPLEX_EQUATION(index=i,lhs = DAE.CREF(componentRef=cr1,ty= DAE.ET_COMPLEX(varLst=varLst,complexClassType=ClassInf.RECORD(_))), rhs = DAE.CALL(path=path,expLst=expLst),source = source),funcs) equation SOME(DAE.RECORD_CONSTRUCTOR(path=fname)) = DAEUtil.avlTreeGet(funcs,path); // create as many equations as the dimension of the record - DAE.ET_COMPLEX(varLst=varLst) = ComponentReference.crefLastType(cr1); e1lst = Util.listMap1(varLst,Expression.generateCrefsExpFromExpVar,cr1); exptpllst = Util.listThreadTuple(e1lst,expLst); compmultilistlst = Util.listMap2(exptpllst,generateextendedRecordEqn,source,funcs); diff --git a/Compiler/BackendDAEOptimize.mo b/Compiler/BackendDAEOptimize.mo index 58923f03d44..b64ab5b831c 100644 --- a/Compiler/BackendDAEOptimize.mo +++ b/Compiler/BackendDAEOptimize.mo @@ -58,7 +58,6 @@ protected import DAEUtil; protected import Debug; protected import Expression; protected import ExpressionSolve; -protected import ExpressionSimplify; protected import Derive; protected import Error; protected import RTOpts; diff --git a/Compiler/BackendDAEUtil.mo b/Compiler/BackendDAEUtil.mo index 408bee90485..9085a7ed514 100644 --- a/Compiler/BackendDAEUtil.mo +++ b/Compiler/BackendDAEUtil.mo @@ -168,7 +168,7 @@ algorithm list>> lstExpCrefs; case (exp,vars) equation - ((_,(_,crefs))) = Expression.traverseExpTopDown(exp,traversecheckBackendDAEExp,((vars,{}))); + ((_,(_,crefs))) = Expression.traverseExp(exp,traversecheckBackendDAEExp,((vars,{}))); lstExpCrefs = Util.if_(listLength(crefs)>0,{(exp,crefs)},{}); then lstExpCrefs; @@ -190,7 +190,7 @@ algorithm case ((e as DAE.CREF(componentRef = DAE.CREF_IDENT(ident="time")),(vars,crefs))) then ((e, (vars,crefs))); /* Special Case for Records */ - case ((e as DAE.CREF(componentRef = cr),(vars,crefs))) + case ((e as DAE.CREF(componentRef = cr,ty= DAE.ET_COMPLEX(varLst=varLst,complexClassType=ClassInf.RECORD(_))),(vars,crefs))) local list>>> expcreflstlst; list>> expcreflst; @@ -198,7 +198,6 @@ algorithm list crlst; list varLst; equation - DAE.ET_COMPLEX(varLst=varLst) = ComponentReference.crefLastType(cr); expl = Util.listMap1(varLst,Expression.generateCrefsExpFromExpVar,cr); expcreflstlst = Util.listMap1(expl,checkBackendDAEExp,vars); expcreflst = Util.listFlatten(expcreflstlst); @@ -233,7 +232,7 @@ algorithm failure((_,_) = BackendVariable.getVar(cr, vars)); then ((e, (vars,cr::crefs))); - case (_) then inTuple; + case inTuple then inTuple; end matchcontinue; end traversecheckBackendDAEExp; @@ -1405,9 +1404,8 @@ algorithm list>> mexp; list varLst; /* Special Case for Records */ - case ((DAE.CREF(componentRef = cr)),vars) + case ((DAE.CREF(componentRef = cr,ty= DAE.ET_COMPLEX(varLst=varLst,complexClassType=ClassInf.RECORD(_)))),vars) equation - DAE.ET_COMPLEX(varLst=varLst) = ComponentReference.crefLastType(cr); expl = Util.listMap1(varLst,Expression.generateCrefsExpFromExpVar,cr); lst = Util.listMap1(expl, statesAndVarsExp, vars); res = Util.listListUnionOnTrue(lst, Expression.expEqual); @@ -4514,7 +4512,7 @@ algorithm exps5 = traverseDAELowExpsEqns(ieqns,func,inTypeA); exps6 = traverseDAELowExpsArrayEqns(ae,func,inTypeA); alglst = arrayList(algs); - exps7 = Util.listMapFlat2(alglst, traverseExps,func,inTypeA); + exps7 = Util.listMapFlat2(alglst, traverseAlgorithmExps,func,inTypeA); exps = Util.listFlatten({exps1,exps2,exps3,exps4,exps5,exps6,exps7}); then exps; @@ -4837,7 +4835,7 @@ algorithm end matchcontinue; end traverseDAELowExpsArrayEqn; -public function traverseExps "function: traverseExps +public function traverseAlgorithmExps "function: traverseAlgorithmExps This function goes through the Algorithm structure and finds all the expressions and performs the function on them @@ -4865,11 +4863,11 @@ algorithm then talst; end matchcontinue; -end traverseExps; +end traverseAlgorithmExps; protected function traverseExpsStmts "function: traverseExps - helper for traverseExps. + helper for traverseAlgorithmExps. " replaceable type Type_a subtypeof Any; replaceable type Type_b subtypeof Any; diff --git a/Compiler/ComponentReference.mo b/Compiler/ComponentReference.mo index 8e98de1a4f3..0dde3f2818f 100644 --- a/Compiler/ComponentReference.mo +++ b/Compiler/ComponentReference.mo @@ -881,14 +881,10 @@ algorithm b := matchcontinue(cr) local DAE.ComponentRef comp; - Boolean b; case(DAE.CREF_IDENT(identType = DAE.ET_COMPLEX(complexClassType=ClassInf.RECORD(_)))) then true; /* this case is false because it is not the last ident. case(DAE.CREF_QUAL(identType = DAE.ET_COMPLEX(complexClassType=ClassInf.RECORD(_)))) then true;*/ - case(DAE.CREF_QUAL(componentRef=comp)) - equation - b = isRecord(comp); - then b; + case(DAE.CREF_QUAL(componentRef=comp)) then isRecord(comp); case(_) then false; end matchcontinue; end isRecord; diff --git a/Compiler/DAEUtil.mo b/Compiler/DAEUtil.mo index 29315e089af..3bfed403025 100644 --- a/Compiler/DAEUtil.mo +++ b/Compiler/DAEUtil.mo @@ -3747,7 +3747,7 @@ algorithm case ((DAE.STMT_ASSIGN_ARR(type_ = tp,componentRef = cr, exp = e, source = source) :: xs),func,extraArg) equation (e_1, extraArg) = func(e, extraArg); - (e_2 as DAE.CREF(cr_1,_), extraArg) = func(DAE.CREF(cr,DAE.ET_OTHER()), extraArg); + (e_2 as DAE.CREF(cr_1,_), extraArg) = func(DAE.CREF(cr,tp), extraArg); (xs_1, extraArg) = traverseDAEEquationsStmts(xs, func, extraArg); then (DAE.STMT_ASSIGN_ARR(tp,cr_1,e_1,source) :: xs_1,extraArg); diff --git a/Compiler/Expression.mo b/Compiler/Expression.mo index c6a3e545d30..1e699b3802a 100644 --- a/Compiler/Expression.mo +++ b/Compiler/Expression.mo @@ -2095,13 +2095,6 @@ algorithm e:= DAE.CREF(cref,tp); end makeCrefExp; -public function makeCrefExpNoType "similar to makeCrefExp but picks type from componentref" -input ComponentRef cref; -output DAE.Exp e; -algorithm - e := makeCrefExp(cref,ComponentReference.crefTypeConsiderSubs(cref)); -end makeCrefExpNoType; - public function crefExp " Author: BZ, 2008-08 generate an DAE.CREF(ComponentRef, Type) from a ComponenRef, make array type correct from subs" @@ -3132,8 +3125,8 @@ public function traverseExp input Type_a inTypeA; output tuple outTplExpTypeA; partial function FuncExpType - input tuple inTplExpTypeA; - output tuple outTplExpTypeA; + input tuple inTpl; + output tuple outTpl; end FuncExpType; algorithm outTplExpTypeA:= @@ -3317,6 +3310,130 @@ algorithm end matchcontinue; end traverseExp; +protected function traverseExpMatrix +"function: traverseExpMatrix + author: PA + Helper function to traverseExp, traverses matrix expressions." + replaceable type Type_a subtypeof Any; + input list>> inTplExpBooleanLstLst; + input traversefuncType tfunc "use traverseExp ore traverseExpTopDown"; + input FuncExpType func; + input Type_a inTypeA; + output list>> outTplExpBooleanLstLst; + output Type_a outTypeA; + partial function FuncExpType + input tuple inTplExpTypeA; + output tuple outTplExpTypeA; + replaceable type Type_a subtypeof Any; + end FuncExpType; + partial function traversefuncType + input DAE.Exp inExp; + input funcType func; + input Type_a inTypeA; + output tuple outTpl; + partial function funcType + input tuple tpl1; + output tuple tpl2; + end funcType; + end traversefuncType; +algorithm + (outTplExpBooleanLstLst,outTypeA):= + matchcontinue (inTplExpBooleanLstLst,tfunc,func,inTypeA) + local + FuncExpType rel; + Type_a e_arg,e_arg_1,e_arg_2; + list> row_1,row; + list>> rows_1,rows; + case ({},_,_,e_arg) then ({},e_arg); + case ((row :: rows),tfunc,rel,e_arg) + equation + (row_1,e_arg_1) = traverseExpMatrix2(row, tfunc, rel, e_arg); + (rows_1,e_arg_2) = traverseExpMatrix(rows, tfunc, rel, e_arg_1); + then + ((row_1 :: rows_1),e_arg_2); + end matchcontinue; +end traverseExpMatrix; + +protected function traverseExpMatrix2 +"function: traverseExpMatrix2 + author: PA + Helper function to traverseExpMatrix." + replaceable type Type_a subtypeof Any; + input list> inTplExpBooleanLst; + input traversefuncType tfunc "use traverseExp ore traverseExpTopDown"; + input FuncExpType func; + input Type_a inTypeA; + output list> outTplExpBooleanLst; + output Type_a outTypeA; + partial function FuncExpType + input tuple inTplExpTypeA; + output tuple outTplExpTypeA; + replaceable type Type_a subtypeof Any; + end FuncExpType; + partial function traversefuncType + input DAE.Exp inExp; + input funcType func; + input Type_a inTypeA; + output tuple outTpl; + partial function funcType + input tuple tpl1; + output tuple tpl2; + end funcType; + end traversefuncType; +algorithm + (outTplExpBooleanLst,outTypeA):= + matchcontinue (inTplExpBooleanLst,tfunc,func,inTypeA) + local + Type_a e_arg,e_arg_1,e_arg_2; + DAE.Exp e_1,e; + list> rest_1,rest; + Boolean b; + FuncExpType rel; + case ({},_,_,e_arg) then ({},e_arg); + case (((e,b) :: rest),tfunc,rel,e_arg) + equation + ((e_1,e_arg_1)) = tfunc(e, rel, e_arg); + (rest_1,e_arg_2) = traverseExpMatrix2(rest, tfunc, rel, e_arg_1); + then + (((e_1,b) :: rest_1),e_arg_2); + end matchcontinue; +end traverseExpMatrix2; + +public function traverseExpList +"function traverseExpList + author PA: + Calls traverseExp for each element of list." + replaceable type Type_a subtypeof Any; + input list expl; + input traversefuncType tfunc "use traverseExp ore traverseExpTopDown"; + input funcType rel; + input Type_a ext_arg; + output tuple, Type_a> outTpl; + partial function funcType + input tuple tpl1; + output tuple tpl2; + end funcType; + partial function traversefuncType + input DAE.Exp inExp; + input funcType func; + input Type_a inTypeA; + output tuple outTplExpTypeA; + partial function funcType + input tuple tpl1; + output tuple tpl2; + end funcType; + end traversefuncType; +algorithm + outTpl := matchcontinue(expl,tfunc,rel,ext_arg) + local DAE.Exp e,e1; list expl1; + case({},_,_,ext_arg) then (({},ext_arg)); + case(e::expl,tfunc,rel,ext_arg) equation + ((e1,ext_arg)) = tfunc(e, rel, ext_arg); + ((expl1,ext_arg)) = traverseExpList(expl,tfunc,rel,ext_arg); + then ((e1::expl1,ext_arg)); + end matchcontinue; +end traverseExpList; + public function traverseExpTopDown "function traverseExpTopDown Traverses all subexpressions of an expression. @@ -3330,8 +3447,8 @@ public function traverseExpTopDown input Type_a inTypeA; output tuple outTplExpTypeA; partial function FuncExpType - input tuple inTplExpTypeA; - output tuple outTplExpTypeA; + input tuple inTplExpBoolTypeA; + output tuple outTplExpBoolTypeA; end FuncExpType; protected DAE.Exp e; @@ -3350,8 +3467,8 @@ protected function traverseExpTopDown1 input Type_a inTypeA; output tuple outTplExpTypeA; partial function FuncExpType - input tuple inTplExpTypeA; - output tuple outTplExpTypeA; + input tuple inTpl; + output tuple outTpl; end FuncExpType; algorithm outTplExpTypeA:= @@ -3508,130 +3625,6 @@ algorithm end matchcontinue; end traverseExpTopDown1; -protected function traverseExpMatrix -"function: traverseExpMatrix - author: PA - Helper function to traverseExp, traverses matrix expressions." - replaceable type Type_a subtypeof Any; - input list>> inTplExpBooleanLstLst; - input traversefuncType tfunc "use traverseExp ore traverseExpTopDown"; - input FuncExpType func; - input Type_a inTypeA; - output list>> outTplExpBooleanLstLst; - output Type_a outTypeA; - partial function FuncExpType - input tuple inTplExpTypeA; - output tuple outTplExpTypeA; - replaceable type Type_a subtypeof Any; - end FuncExpType; - partial function traversefuncType - input DAE.Exp inExp; - input funcType func; - input Type_a inTypeA; - output tuple outTplExpTypeA; - partial function funcType - input tuple tpl1; - output tuple tpl2; - end funcType; - end traversefuncType; -algorithm - (outTplExpBooleanLstLst,outTypeA):= - matchcontinue (inTplExpBooleanLstLst,tfunc,func,inTypeA) - local - FuncExpType rel; - Type_a e_arg,e_arg_1,e_arg_2; - list> row_1,row; - list>> rows_1,rows; - case ({},_,_,e_arg) then ({},e_arg); - case ((row :: rows),tfunc,rel,e_arg) - equation - (row_1,e_arg_1) = traverseExpMatrix2(row, tfunc, rel, e_arg); - (rows_1,e_arg_2) = traverseExpMatrix(rows, tfunc, rel, e_arg_1); - then - ((row_1 :: rows_1),e_arg_2); - end matchcontinue; -end traverseExpMatrix; - -protected function traverseExpMatrix2 -"function: traverseExpMatrix2 - author: PA - Helper function to traverseExpMatrix." - replaceable type Type_a subtypeof Any; - input list> inTplExpBooleanLst; - input traversefuncType tfunc "use traverseExp ore traverseExpTopDown"; - input FuncExpType func; - input Type_a inTypeA; - output list> outTplExpBooleanLst; - output Type_a outTypeA; - partial function FuncExpType - input tuple inTplExpTypeA; - output tuple outTplExpTypeA; - replaceable type Type_a subtypeof Any; - end FuncExpType; - partial function traversefuncType - input DAE.Exp inExp; - input funcType func; - input Type_a inTypeA; - output tuple outTplExpTypeA; - partial function funcType - input tuple tpl1; - output tuple tpl2; - end funcType; - end traversefuncType; -algorithm - (outTplExpBooleanLst,outTypeA):= - matchcontinue (inTplExpBooleanLst,tfunc,func,inTypeA) - local - Type_a e_arg,e_arg_1,e_arg_2; - DAE.Exp e_1,e; - list> rest_1,rest; - Boolean b; - FuncExpType rel; - case ({},_,_,e_arg) then ({},e_arg); - case (((e,b) :: rest),tfunc,rel,e_arg) - equation - ((e_1,e_arg_1)) = tfunc(e, rel, e_arg); - (rest_1,e_arg_2) = traverseExpMatrix2(rest, tfunc, rel, e_arg_1); - then - (((e_1,b) :: rest_1),e_arg_2); - end matchcontinue; -end traverseExpMatrix2; - -public function traverseExpList -"function traverseExpList - author PA: - Calls traverseExp for each element of list." - replaceable type Type_a subtypeof Any; - input list expl; - input traversefuncType tfunc "use traverseExp ore traverseExpTopDown"; - input funcType rel; - input Type_a ext_arg; - output tuple, Type_a> outTpl; - partial function funcType - input tuple tpl1; - output tuple tpl2; - end funcType; - partial function traversefuncType - input DAE.Exp inExp; - input funcType func; - input Type_a inTypeA; - output tuple outTplExpTypeA; - partial function funcType - input tuple tpl1; - output tuple tpl2; - end funcType; - end traversefuncType; -algorithm - outTpl := matchcontinue(expl,tfunc,rel,ext_arg) - local DAE.Exp e,e1; list expl1; - case({},_,_,ext_arg) then (({},ext_arg)); - case(e::expl,tfunc,rel,ext_arg) equation - ((e1,ext_arg)) = tfunc(e, rel, ext_arg); - ((expl1,ext_arg)) = traverseExpList(expl,tfunc,rel,ext_arg); - then ((e1::expl1,ext_arg)); - end matchcontinue; -end traverseExpList; - public function traverseExpOpt "Calls traverseExp for SOME(exp) and does nothing for NONE" input Option inExp; input FuncExpType func; @@ -4093,18 +4086,30 @@ algorithm res; case (DAE.BINARY(e1,op,e2)) equation - b1 = isConst(e1); - b2 = isConst(e2); - res = boolAnd(b1,b2); + true = isConst(e1); + true = isConst(e2); then - res; - case (DAE.IFEXP(_,e1,e2)) + true; + case (DAE.IFEXP(e,e1,e2)) equation - b1 = isConst(e1); - b2 = isConst(e2); - res = boolAnd(b1,b2); + true = isConst(e); + true = isConst(e1); + true = isConst(e2); then - res; + true; + case (DAE.LBINARY(exp1=e1,exp2=e2)) + equation + true = isConst(e1); + true = isConst(e2); + then + true; + case (DAE.LUNARY(exp=e)) then isConst(e); + case (DAE.RELATION(exp1=e1,exp2=e2)) + equation + true = isConst(e1); + true = isConst(e2); + then + true; case (DAE.ARRAY(array = ae)) equation ab = Util.listMap(ae,isConst); @@ -4118,7 +4123,47 @@ algorithm ab = Util.listMap(ae,isConst); res = Util.boolAndList(ab); then - res; + res; + case (DAE.RANGE(exp=e,expOption=NONE(),range=e1)) + equation + true = isConst(e); + true = isConst(e1); + then + true; + case (DAE.RANGE(exp=e,expOption=SOME(e1),range=e2)) + equation + true = isConst(e); + true = isConst(e1); + true = isConst(e2); + then + true; + case (DAE.PARTEVALFUNCTION(expList = ae)) + equation + ab = Util.listMap(ae,isConst); + res = Util.boolAndList(ab); + then + res; + case (DAE.TUPLE(PR = ae)) + equation + ab = Util.listMap(ae,isConst); + res = Util.boolAndList(ab); + then + res; + case (DAE.ASUB(exp=e,sub=ae)) + equation + true = isConst(e); + ab = Util.listMap(ae,isConst); + res = Util.boolAndList(ab); + then + res; + case (DAE.SIZE(exp=e,sz=NONE())) then isConst(e); + case (DAE.SIZE(exp=e,sz=SOME(e1))) + equation + true = isConst(e); + true = isConst(e1); + then + true; + case (DAE.END()) then true; case (_) then false; end matchcontinue; end isConst;