Skip to content

Commit

Permalink
- enhance partFuncEval for initValues
Browse files Browse the repository at this point in the history
  • Loading branch information
vwaurich authored and OpenModelica-Hudson committed Sep 1, 2015
1 parent 15d6851 commit b19dc57
Show file tree
Hide file tree
Showing 3 changed files with 269 additions and 24 deletions.
260 changes: 239 additions & 21 deletions Compiler/BackEnd/EvaluateFunctions.mo
Expand Up @@ -249,6 +249,44 @@ algorithm
end matchcontinue;
end evalFunctions_findFuncs;

/*
protected function getInputReplacements
input list<DAE.Exp> inputExps;
input list<DAE.Element> inputElements;
input DAE.FunctionTree funcTree;
input BackendVarTransform.VariableReplacements replIn;
output BackendVarTransform.VariableReplacements replOut;
protected
Boolean isComplex;
BackendVarTransform.VariableReplacements repl;
DAE.ComponentRef cref;
DAE.Exp exp;
DAE.Element inp;
list<DAE.Element> elems, scalarElements;
list<DAE.Exp> scalarExps;
algorithm
elems := inputElements;
repl := replIn;
for exp in inputExps loop
inp::elems := elems;
cref := DAEUtil.varCref(inp);
//print("One Input Exp\n"+stringDelimitList(List.map1({exp},ExpressionDump.dumpExpStr,1),"\n")+"\n");
//print("\nOne Input Element \n"+DAEDump.dumpElementsStr({inp})+"\n");
scalarExps := expandComplexEpressions(exp,funcTree);
//print("Scalar Exp\n"+stringDelimitList(List.map1(scalarExps,ExpressionDump.dumpExpStr,1),"\n")+"\n");
scalarElements := expandComplexVar(inp);
//print("\nScalar Elements \n"+DAEDump.dumpElementsStr(scalarElements)+"\n");
isComplex := not (listLength(scalarExps) == 1 and Expression.expEqual(listHead(scalarExps),exp));
if isComplex then
repl := BackendVarTransform.addReplacement(repl,cref,exp,NONE());
repl := getInputReplacements(scalarExps,scalarElements,funcTree,repl);
elseif Expression.isConst(exp) then
repl := BackendVarTransform.addReplacement(repl,cref,exp,NONE());
end if;
end for;
replOut := repl;
end getInputReplacements;
*/

public function evaluateConstantFunctionCallExp"checks if the expression is a call and can be evaluated to a constant value.
the output is either a constant expression or the input exp. no partial evaluation is performed in here."
Expand Down Expand Up @@ -285,24 +323,27 @@ algorithm
// get the input exps from the call
exps = List.map1(exps0,evaluateConstantFunctionCallExp,funcsIn);
scalarExp = List.map1(exps,expandComplexEpressions,funcsIn);
allInputExps = List.flatten(scalarExp);
//print("allInputExps\n"+stringDelimitList(List.map(allInputExps,ExpressionDump.printExpStr),"\n")+"\n");
allInputExps = List.flatten(scalarExp);
//print("allInputExps\n"+stringDelimitList(List.map(allInputExps,ExpressionDump.printExpStr),"\n")+"\n");

if listEmpty(elements) then // its a record
//-----------------------its a record-----------------------
expOut = DAE.TUPLE(allInputExps);
if Flags.isSet(Flags.EVAL_FUNC_DUMP) then print("\nIts a record.\n");
end if;
else
//-----------------------its a function call-----------------------

// get all input crefs (from function body) (scalar and one dimensioanl)
allInputs = List.filterOnTrue(elements,DAEUtil.isInputVar);
scalarInputs = List.map(allInputs,expandComplexElementsToCrefs);
allInputCrefs = List.flatten(scalarInputs);
//print("\nallInputCrefs\n"+stringDelimitList(List.map(allInputCrefs,ComponentReference.printComponentRefStr),"\n")+"\n");

if listEmpty(elements) then // its a record
expOut = DAE.TUPLE(allInputExps);
else

protectVars = List.filterOnTrue(elements,DAEUtil.isProtectedVar);
algs = List.filterOnTrue(elements,DAEUtil.isAlgorithm);
algs = listAppend(protectVars,algs);


// get all output crefs (complex and scalar)
allOutputs = List.filterOnTrue(elements,DAEUtil.isOutputVar);
outputCrefs = List.map(allOutputs,DAEUtil.varCref);
Expand Down Expand Up @@ -370,7 +411,7 @@ algorithm
end if;

if Flags.isSet(Flags.EVAL_FUNC_DUMP) then
print("\nevaluted1 to: "+ExpressionDump.printExpStr(expOut)+"\n\n");
print("\nevaluated to: "+ExpressionDump.printExpStr(expOut)+"\n\n");
end if;

then expOut;
Expand All @@ -380,7 +421,6 @@ algorithm
//this ASUB stuff occurs in the flattened DAE, check this special case because of removeSimpleEquations
exp = evaluateConstantFunctionCallExp(DAE.CALL(path=path, expLst=exps, attr=attr1),funcsIn);
(exp,_) = ExpressionSimplify.simplify(DAE.ASUB(exp,sub));

if not Expression.isConst(exp) then exp = expIn; end if;
then exp;

Expand Down Expand Up @@ -627,6 +667,131 @@ algorithm
end matchcontinue;
end evaluateConstantFunction;

/*
protected function makeVarElement"builds an DAE.Element of uniontype var from cref and type"
input DAE.ComponentRef cref;
input DAE.Type ty;
output DAE.Element elem;
algorithm
elem := DAE.VAR(cref,DAE.VARIABLE(),DAE.BIDIR(),DAE.NON_PARALLEL(),DAE.PUBLIC(),ty,NONE(),Types.getDimensions(ty),DAE.NON_CONNECTOR(),DAE.emptyElementSource,NONE(),NONE(),Absyn.NOT_INNER_OUTER());
end makeVarElement;
protected function expandComplexVar "gets var-elements for the scalar values of complex vars and multidimensional vars.
author: Waurich TUD 2014-03"
input DAE.Element inElem;
output list<DAE.Element> varLstOut={};
algorithm
varLstOut := matchcontinue(inElem)
local
list<Integer> dim;
list<String> names;
list<DAE.Dimension> dims;
DAE.ComponentRef cref;
DAE.Dimensions dimensions, dimensions2;
DAE.Type ty;
DAE.Element elem;
DAE.Exp exp;
list<DAE.Exp> exps;
list<DAE.Element> varsFold = {};
list<DAE.ComponentRef> crefs;
list<DAE.Type> types;
list<DAE.Var> varLst;
list<DAE.Subscript> subs, subs2;
list<list<DAE.Subscript>> subslst, subslst1, subslst2;
case(DAE.VAR(componentRef = cref,ty=DAE.T_COMPLEX(varLst = varLst)))
algorithm
names := List.map(varLst,DAEUtil.typeVarIdent);
types := List.map(varLst,DAEUtil.varType);
crefs := List.map1(names,ComponentReference.appendStringCref,cref);
crefs := setTypesForScalarCrefs(crefs,types);
for ty in types loop
cref::crefs := crefs;
elem := makeVarElement(cref,ty);
varsFold := elem::varsFold;
end for;
then
listReverse(varsFold);
case(DAE.VAR(dims={}))
algorithm
then {inElem};
case(DAE.VAR(componentRef=cref,ty=DAE.T_REAL(), dims=dims ))
algorithm
dim := List.map(dims, Expression.dimensionSize);
subslst := expandDimension(dims,{});
crefs := List.map1r(subslst,ComponentReference.subscriptCref,cref);
for cref in crefs loop
elem := makeVarElement(cref,DAE.T_REAL_DEFAULT);
varsFold := elem::varsFold;
end for;
then
listReverse(varsFold);
case(DAE.VAR(componentRef=cref,ty=DAE.T_INTEGER(), dims=dims ))
algorithm
subslst := expandDimension(dims,{});
crefs := List.map1r(subslst,ComponentReference.subscriptCref,cref);
for cref in crefs loop
elem := makeVarElement(cref,DAE.T_INTEGER_DEFAULT);
varsFold := elem::varsFold;
end for;
then
listReverse(varsFold);
case(DAE.VAR(componentRef=cref,ty=DAE.T_ARRAY(ty=DAE.T_ARRAY(ty=ty, dims=dimensions2), dims=dimensions)))// a 2-dim array
algorithm
subslst1 := expandDimension(dimensions,{});
subslst2 := expandDimension(dimensions2,{});
subslst := {};
for subs in subslst1 loop
for subs2 in subslst2 loop
subslst := listAppend(subs,subs2)::subslst;
end for;
end for;
crefs := List.map1r(subslst,ComponentReference.subscriptCref,cref);
for cref in crefs loop
elem := makeVarElement(cref,ty);
varsFold := elem::varsFold;
end for;
then
varsFold;
case(DAE.VAR(componentRef=cref,ty=DAE.T_ARRAY(ty=ty, dims=dimensions))) // a 1-dim array
algorithm
subslst := expandDimension(dimensions,{});
crefs := List.map1r(subslst,ComponentReference.subscriptCref,cref);
for cref in crefs loop
elem := makeVarElement(cref,ty);
varsFold := elem::varsFold;
end for;
then
listReverse(varsFold);
case(DAE.VAR(componentRef=cref,ty=DAE.T_ENUMERATION()))
equation
if Flags.isSet(Flags.EVAL_FUNC_DUMP) then
print("update expandComplexVar for enumerations: the enum cref is :"+stringDelimitList(List.map({cref},ComponentReference.printComponentRefStr),"\n")+"\n");
end if;
then
{};
case(DAE.VAR(componentRef=cref,ty=DAE.T_TUPLE()))
equation
if Flags.isSet(Flags.EVAL_FUNC_DUMP) then
print("update expandComplexVar for tuple types: the tupl cref is :\n"+stringDelimitList(List.map({cref},ComponentReference.printComponentRefStr),"\n")+"\n");
end if;
then
{};
else
then {inElem};
end matchcontinue;
end expandComplexVar;
*/

protected function expandComplexEpressions "gets the complex contents or if its not complex, then the exp itself, if its a call, get the scalar outputs.
it would be possible to evaluate the exp before.
author:Waurich TUD 2014-05"
Expand Down Expand Up @@ -1567,7 +1732,7 @@ algorithm

case(DAE.VAR(componentRef=cref,binding=SOME(exp)),(funcs,repl,i))
equation
//print("VARIN "+DAEDump.dumpElementsStr({algIn})+"\n");
//print("VARIN1 "+DAEDump.dumpElementsStr({algIn})+"\n");
(exp,_) = BackendVarTransform.replaceExp(exp, repl,NONE());
(exp,_) = ExpressionSimplify.simplify(exp);
if Expression.isConst(exp) then
Expand All @@ -1583,7 +1748,15 @@ algorithm
//print("bind "+ExpressionDump.printExpStr(exp)+"\n");
//update binding in var
then (DAEUtil.replaceBindungInVar(exp,algIn),(funcs,repl,i));

case(DAE.VAR(componentRef=cref,binding=NONE()),(funcs,repl,i))
equation
//print("VARIN2 "+DAEDump.dumpElementsStr({algIn})+"\n");
then (algIn,tplIn);

else
equation
print("unknown type in evaluateFunctions_updateAlgElements "+DAEDump.dumpElementsStr({algIn})+"\n");
then(algIn,tplIn);
end matchcontinue;
end evaluateFunctions_updateAlgElements;
Expand Down Expand Up @@ -1620,7 +1793,7 @@ algorithm
BackendVarTransform.VariableReplacements repl, replIn;
DAE.ComponentRef cref;
DAE.ElementSource source;
DAE.Exp exp0, exp1, exp2, range;
DAE.Exp exp0, exp1, exp2, range, cond, msg, lvl;
DAE.Else else_;
DAE.FunctionTree funcTree,funcTree2;
DAE.Statement alg, alg2;
Expand All @@ -1638,19 +1811,19 @@ algorithm
case(DAE.STMT_ASSIGN(type_=typ, exp1=exp1, exp=exp2, source=source)::rest,(funcTree,replIn,idx),_)
equation
// replace, evaluate, simplify the assignment
//print("the STMT_ASSIGN before: "+DAEDump.ppStatementStr(listHead(algsIn)));
//print("the STMT_ASSIGN before: "+DAEDump.ppStatementStr(listHead(algsIn)));
if Flags.isSet(Flags.EVAL_FUNC_DUMP) then
print("assignment:\n"+DAEDump.ppStatementStr(listHead(algsIn)));
end if;
cref = Expression.expCref(exp1);
scalars = getRecordScalars(cref);
(exp2,_) = BackendVarTransform.replaceExp(exp2,replIn,NONE());
(exp2,_) = ExpressionSimplify.simplify(exp2);
(exp2,_) = ExpressionSimplify.simplify(exp2);

(exp2,(exp1,funcTree,idx,addStmts)) = Expression.traverseExpTopDown(exp2,evaluateConstantFunctionWrapper,(exp1,funcTree,idx,{}));

// (exp2,changed) = bcallret1_2(changed,ExpressionSimplify.simplify,exp2,exp2,changed); This does nothing useful with the statement below...
//(exp2,_) = ExpressionSimplify.simplify(exp2);
(exp2,_) = ExpressionSimplify.simplify(exp2);
(exp2,_) = Expression.traverseExpBottomUp(exp2,unboxExp,false);// for metamodelica/meta/omc
expLst = Expression.getComplexContents(exp2);

Expand All @@ -1665,8 +1838,7 @@ algorithm
eqDim = listLength(scalars) == listLength(expLst); // so it can be partly constant
isRec = ComponentReference.isRecord(cref) or Expression.isCall(exp2);
isTpl = Expression.isTuple(exp1) and Expression.isTuple(exp2);
_ = Expression.isCall(exp2);
//print("is it const? "+boolString(isCon)+" ,is it rec: "+boolString(isRec)+" ,is it tpl: "+boolString(isTpl)+" ,is it call: "+boolString(isCall)+"\n");
//print("is it const? "+boolString(isCon)+" ,is it rec: "+boolString(isRec)+" ,is it tpl: "+boolString(isTpl)+"\n");

// remove the variable crefs and add the constant crefs to the replacements
//print("scalars\n"+stringDelimitList(List.map(scalars,ComponentReference.printComponentRefStr),"\n")+"\n");
Expand Down Expand Up @@ -1930,11 +2102,24 @@ algorithm
stmts2 = alg::lstIn;
(rest,(funcTree,repl,idx)) = evaluateFunctions_updateStatement(rest,(funcTree,repl,idx),stmts2);
then (rest,(funcTree,repl,idx));
case(DAE.STMT_ASSERT()::rest,(funcTree,replIn,idx),_)
case(DAE.STMT_ASSERT(cond=cond,msg=msg,level=lvl)::rest,(funcTree,replIn,idx),_)
equation
alg = listHead(algsIn);
(cond,_) = BackendVarTransform.replaceExp(cond,replIn,NONE());
(cond) = evaluateConstantFunctionCallExp(cond,funcTree);
(cond,_) = ExpressionSimplify.simplify(cond);
(msg,_) = BackendVarTransform.replaceExp(msg,replIn,NONE());
(msg) = evaluateConstantFunctionCallExp(msg,funcTree);
(msg,_) = ExpressionSimplify.simplify(msg);
if Expression.expEqual(cond,DAE.BCONST(false)) and Expression.expString(lvl)=="AssertionLevel.error" then
print("ERROR: "+ExpressionDump.printExpStr(msg)+"\n");
fail();
elseif Expression.expEqual(cond,DAE.BCONST(false)) and Expression.expString(lvl)=="AssertionLevel.warning" then
print("WARNING: "+ExpressionDump.printExpStr(msg)+"\n");
end if;

if Flags.isSet(Flags.EVAL_FUNC_DUMP) then
print("assert-statement (not evaluated):\n"+DAEDump.ppStatementStr(alg));
print("assert-statement:\n"+DAEDump.ppStatementStr(alg));
end if;
stmts2 = alg::lstIn;
(rest,(funcTree,repl,idx)) = evaluateFunctions_updateStatement(rest,(funcTree,replIn,idx),stmts2);
Expand Down Expand Up @@ -2430,9 +2615,9 @@ algorithm
list<Integer> dim;
list<list<Integer>> ranges;
list<DAE.Dimension> dims;
list<list<DAE.Subscript>> subslst;
list<list<DAE.Subscript>> subslst, subslst1, subslst2;
DAE.ComponentRef cref;
DAE.Dimensions dimensions;
DAE.Dimensions dimensions, dimensions2;
DAE.Type ty;
DAE.Exp exp;
list<DAE.Exp> exps;
Expand All @@ -2452,13 +2637,35 @@ algorithm
crefs = List.flatten(crefLst);
then
crefs;

case(DAE.VAR(componentRef=cref,ty=DAE.T_REAL(), dims=dims ))
algorithm
dim := List.map(dims, Expression.dimensionSize);
subslst := expandDimension(dims,{});
crefs := List.map1r(subslst,ComponentReference.subscriptCref,cref);
then
crefs;

case(DAE.VAR(componentRef=cref,ty=DAE.T_INTEGER(), dims=dims ))
algorithm
subslst := expandDimension(dims,{});
crefs := List.map1r(subslst,ComponentReference.subscriptCref,cref);
then
crefs;

case(DAE.VAR(componentRef=cref,ty=DAE.T_ARRAY(ty=DAE.T_ARRAY(ty=ty, dims=dimensions2), dims=dimensions)))// a 2-dim array
algorithm
subslst1 := expandDimension(dimensions,{});
subslst2 := expandDimension(dimensions2,{});
subslst := {};
for subs in subslst1 loop
for subs2 in subslst2 loop
subslst := listAppend(subs,subs2)::subslst;
end for;
end for;
crefs := List.map1r(subslst,ComponentReference.subscriptCref,cref);
then
crefs;

case(DAE.VAR(componentRef=cref,ty=DAE.T_ARRAY(dims=dimensions)))
equation
if Flags.isSet(Flags.EVAL_FUNC_DUMP) then
Expand All @@ -2482,6 +2689,10 @@ algorithm
then
{};
else
equation
print("test5\n");

then
{};
end matchcontinue;
end getScalarsForComplexVar;
Expand Down Expand Up @@ -2544,6 +2755,13 @@ algorithm
then
false;

case (DAE.VAR(ty=DAE.T_INTEGER(_), dims=dims))
equation
dimints = List.map(dims, Expression.dimensionSize);
true = listHead(dimints) <> 0;
then
false;

case (DAE.VAR(ty=DAE.T_ARRAY(_))) then false;
else true;
end matchcontinue;
Expand Down

0 comments on commit b19dc57

Please sign in to comment.