Skip to content

Commit 8da9012

Browse files
vwaurichOpenModelica-Hudson
authored andcommitted
- even mroe fixes
1 parent abf4da5 commit 8da9012

File tree

1 file changed

+60
-51
lines changed

1 file changed

+60
-51
lines changed

Compiler/BackEnd/EvaluateFunctions.mo

Lines changed: 60 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,15 @@ algorithm
100100
equation
101101
true = Flags.isSet(Flags.EVALUATE_CONST_FUNCTIONS);
102102
BackendDAE.DAE(eqs = eqSysts,shared = shared) = inDAE;
103+
//BackendDump.dumpBackendDAE(inDAE,"inDAE");
103104
(eqSysts,(shared,_,changed)) = List.mapFold(eqSysts,evalFunctions_main,(shared,1,false));
104105
//shared = evaluateShared(shared);
105106
if changed then
106107
outDAE = updateVarKinds(RemoveSimpleEquations.fastAcausal(BackendDAE.DAE(eqSysts,shared)));
107108
else
108109
outDAE = inDAE;
109110
end if;
111+
//BackendDump.dumpBackendDAE(outDAE,"outDAE");
110112
then
111113
outDAE;
112114
else
@@ -276,10 +278,10 @@ algorithm
276278
list<DAE.ComponentRef> allInputCrefs, outputCrefs, allOutputCrefs, constInputCrefs, constCrefs, varScalarCrefsInFunc,constComplexCrefs,varComplexCrefs,varScalarCrefs,constScalarCrefs;
277279
list<list<DAE.ComponentRef>> scalarInputs, scalarOutputs;
278280
list<DAE.Element> elements, protectVars, algs, allInputs, allOutputs;
279-
list<DAE.Exp> exps, sub, allInputExps, constInputExps, constExps, constComplexExps, constScalarExps;
281+
list<DAE.Exp> exps, exps0, sub, allInputExps, constInputExps, constExps, constComplexExps, constScalarExps;
280282
list<list<DAE.Exp>> scalarExp;
281283

282-
case(DAE.CALL(path=path, expLst=exps, attr=attr1),_)
284+
case(DAE.CALL(path=path, expLst=exps0, attr=attr1),_)
283285
equation
284286
if Flags.isSet(Flags.EVAL_FUNC_DUMP) then
285287
print("\nStart constant evaluation of expression: "+ExpressionDump.printExpStr(expIn)+"\n\n");
@@ -290,7 +292,7 @@ algorithm
290292
elements = DAEUtil.getFunctionElements(func);
291293

292294
// get the input exps from the call
293-
exps = List.map1(exps,evaluateConstantFunctionCallExp,funcsIn);
295+
exps = List.map1(exps0,evaluateConstantFunctionCallExp,funcsIn);
294296
scalarExp = List.map1(exps,expandComplexEpressions,funcsIn);
295297
allInputExps = List.flatten(scalarExp);
296298
//print("allInputExps\n"+stringDelimitList(List.map(allInputExps,ExpressionDump.printExpStr),"\n")+"\n");
@@ -358,7 +360,7 @@ algorithm
358360
(constScalarCrefs,constScalarExps) = List.filter1OnTrueSync(constCrefs,ComponentReference.crefInLst,constScalarCrefs,constExps);
359361
(constComplexCrefs,constComplexExps) = List.filter1OnTrueSync(constCrefs,ComponentReference.crefInLst,constComplexCrefs,constExps);
360362

361-
//print("constComplexCrefs\n"+stringDelimitList(List.map(constComplexCrefs,ComponentReference.printComponentRefStr),"\n")+"\n");
363+
//print("constComplexCrefs\n"+stringDelimitList(List.map(constComplexCrefs,ComponentReference.printComponentRefStr),"\n")+"\n");
362364
//print("varComplexCrefs\n"+stringDelimitList(List.map(varComplexCrefs,ComponentReference.printComponentRefStr),"\n")+"\n");
363365
//print("constScalarCrefs\n"+stringDelimitList(List.map(constScalarCrefs,ComponentReference.printComponentRefStr),"\n")+"\n");
364366
//print("varScalarCrefs\n"+stringDelimitList(List.map(varScalarCrefs,ComponentReference.printComponentRefStr),"\n")+"\n");
@@ -371,7 +373,7 @@ algorithm
371373
// there is a constant complex expression
372374
if listLength(constComplexCrefs)==1 then expOut = listHead(constComplexExps);
373375
else expOut = DAE.TUPLE(constComplexExps); end if;
374-
else fail();
376+
else expOut = expIn;
375377
end if;
376378
end if;
377379

@@ -392,9 +394,6 @@ algorithm
392394
else
393395
// could not evaluate it
394396
equation
395-
if Flags.isSet(Flags.EVAL_FUNC_DUMP) then
396-
print("could not been evaluated to a constant expression!\n");
397-
end if;
398397
then expIn;
399398
end matchcontinue;
400399
end evaluateConstantFunctionCallExp;
@@ -428,13 +427,13 @@ algorithm
428427
list<BackendDAE.Equation> constEqs;
429428
list<DAE.ComponentRef> inputCrefs, outputCrefs, allInputCrefs, allOutputCrefs, constInputCrefs, constCrefs, varScalarCrefsInFunc, constScalarCrefsOut,constComplexCrefs,varComplexCrefs,varScalarCrefs,constScalarCrefs;
430429
list<DAE.Element> elements, algs, allInputs, protectVars, allOutputs, updatedVarOutputs, newOutputVars;
431-
list<DAE.Exp> exps, inputExps, complexExp, allInputExps, constInputExps, constExps, constComplexExps, constScalarExps, lhsExps, sub;
430+
list<DAE.Exp> exps, expsIn, inputExps, complexExp, allInputExps, constInputExps, constExps, constComplexExps, constScalarExps, lhsExps, sub;
432431
list<list<DAE.Exp>> scalarExp;
433432
list<DAE.Statement> stmts;
434433
list<DAE.Type> outputVarTypes;
435434
list<String> outputVarNames;
436435
list<list<DAE.ComponentRef>> scalarInputs, scalarOutputs;
437-
case(DAE.CALL(path=path, expLst=exps, attr=attr1),_,_,_)
436+
case(DAE.CALL(path=path, expLst=expsIn, attr=attr1),_,_,_)
438437
equation
439438

440439
if Flags.isSet(Flags.EVAL_FUNC_DUMP) then
@@ -450,7 +449,7 @@ algorithm
450449
elements = DAEUtil.getFunctionElements(func);
451450
protectVars = List.filterOnTrue(elements,DAEUtil.isProtectedVar);
452451
algs = List.filterOnTrue(elements,DAEUtil.isAlgorithm);
453-
algs = listAppend(protectVars,algs);
452+
//algs = listAppend(protectVars,algs);
454453
//print("elements: "+DAEDump.dumpElementsStr(elements)+"\n");
455454

456455
// some exceptions
@@ -463,34 +462,34 @@ algorithm
463462
end if;
464463

465464
false = listEmpty(elements); // its a record
466-
false = listLength(protectVars)+listLength(algs)==0; // its a built in function
465+
false = listEmpty(algs); // its a built in function
467466

468467
// get the input exps from the call
469-
exps = List.map1(exps,evaluateConstantFunctionCallExp,funcsIn);
468+
exps = List.map1(expsIn,evaluateConstantFunctionCallExp,funcsIn);
470469
scalarExp = List.map1(exps,expandComplexEpressions,funcsIn);//these exps are evaluated as well
471470
allInputExps = List.flatten(scalarExp);
472-
//print("allInputExps\n"+stringDelimitList(List.map(allInputExps,ExpressionDump.printExpStr),"\n")+"\n");
471+
//print("allInputExps\n"+stringDelimitList(List.map(allInputExps,ExpressionDump.printExpStr),"\n")+"\n");
473472

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

480479
// get all output crefs (complex and scalar)
481480
allOutputs = List.filterOnTrue(elements,DAEUtil.isOutputVar);
482481
outputCrefs = List.map(allOutputs,DAEUtil.varCref);
483482
scalarOutputs = List.map(allOutputs,getScalarsForComplexVar);
484483
allOutputCrefs = listAppend(outputCrefs,List.flatten(scalarOutputs));
485-
//print("\ncomplex OutputCrefs\n"+stringDelimitList(List.map(outputCrefs,ComponentReference.printComponentRefStr),"\n")+"\n");
486-
//print("\nscalarOutputs\n"+stringDelimitList(List.map(List.flatten(scalarOutputs),ComponentReference.printComponentRefStr),"\n")+"\n");
484+
//print("\ncomplex OutputCrefs\n"+stringDelimitList(List.map(outputCrefs,ComponentReference.printComponentRefStr),"\n")+"\n");
485+
//print("\nscalarOutputs\n"+stringDelimitList(List.map(List.flatten(scalarOutputs),ComponentReference.printComponentRefStr),"\n")+"\n");
487486

488487
// get the constant inputs
489488
(constInputExps,constInputCrefs) = List.filterOnTrueSync(allInputExps,Expression.isConst,allInputCrefs);
490-
//print("\nallInputExps\n"+stringDelimitList(List.map(allInputExps,ExpressionDump.printExpStr),"\n")+"\n");
491-
//print("\nconstInputExps\n"+stringDelimitList(List.map(constInputExps,ExpressionDump.printExpStr),"\n")+"\n");
492-
//print("\nconstInputCrefs\n"+stringDelimitList(List.map(constInputCrefs,ComponentReference.printComponentRefStr),"\n")+"\n");
493-
//print("\nall algs "+intString(listLength(algs))+"\n"+DAEDump.dumpElementsStr(algs)+"\n");
489+
//print("\nallInputExps\n"+stringDelimitList(List.map(allInputExps,ExpressionDump.printExpStr),"\n")+"\n");
490+
//print("\nconstInputExps\n"+stringDelimitList(List.map(constInputExps,ExpressionDump.printExpStr),"\n")+"\n");
491+
//print("\nconstInputCrefs\n"+stringDelimitList(List.map(constInputCrefs,ComponentReference.printComponentRefStr),"\n")+"\n");
492+
//print("\nall algs "+intString(listLength(algs))+"\n"+DAEDump.dumpElementsStr(algs)+"\n");
494493

495494
//------------------------------------------------
496495
//evaluate function call
@@ -508,7 +507,6 @@ algorithm
508507
hasTerminate = List.fold(algs,hasReturnFold,false);
509508
hasReinit = List.fold(algs,hasReinitFold,false);
510509
abort = hasReturn or hasTerminate or hasReinit;
511-
512510
// go through all algorithms and replace the variables with constants if possible, extend the ht after each algorithm
513511
(algs,(funcs,repl,idx)) = List.mapFold(algs,evaluateFunctions_updateAlgElements,(funcsIn,repl,1));
514512
//print("\nall algs after"+intString(listLength(algs))+"\n"+DAEDump.dumpElementsStr(algs)+"\n");
@@ -529,8 +527,8 @@ algorithm
529527

530528
//print("constComplexCrefs\n"+stringDelimitList(List.map(constComplexCrefs,ComponentReference.printComponentRefStr),"\n")+"\n");
531529
//print("varComplexCrefs\n"+stringDelimitList(List.map(varComplexCrefs,ComponentReference.printComponentRefStr),"\n")+"\n");
532-
//print("constScalarCrefs 1\n"+stringDelimitList(List.map(constScalarCrefs,ComponentReference.printComponentRefStr),"\n")+"\n");
533-
//print("varScalarCrefs 1\n"+stringDelimitList(List.map(varScalarCrefs,ComponentReference.printComponentRefStr),"\n")+"\n");
530+
//print("constScalarCrefs\n"+stringDelimitList(List.map(constScalarCrefs,ComponentReference.printComponentRefStr),"\n")+"\n");
531+
//print("varScalarCrefs\n"+stringDelimitList(List.map(varScalarCrefs,ComponentReference.printComponentRefStr),"\n")+"\n");
534532

535533
//------------------------------------------------
536534
//evaluate the result and build new function call accordingly
@@ -559,22 +557,21 @@ algorithm
559557
funcIsConst = if (hasAssert and funcIsConst) or abort then false else funcIsConst; // quit if there is a return or terminate or use partial evaluation if there is an assert
560558
funcIsPartConst = if hasAssert and funcIsConst then true else funcIsPartConst;
561559
funcIsPartConst = if abort then false else funcIsPartConst; // quit if there is a return or terminate
562-
563560
true = funcIsPartConst or funcIsConst;
564561
changed = funcIsPartConst or funcIsConst;
565562

566563
// build the new lhs, the new statements for the function, the constant parts...
567564
(updatedVarOutputs,outputExp,varScalarCrefsInFunc) = buildVariableFunctionParts(scalarOutputs,constComplexCrefs,varComplexCrefs,constScalarCrefs,varScalarCrefs,allOutputs,lhsExpIn);
568565
(constScalarCrefsOut,constComplexCrefs) = buildConstFunctionCrefs(constScalarCrefs,constComplexCrefs,allOutputCrefs,lhsExpIn);
566+
//print("constScalarCrefsOut\n"+stringDelimitList(List.map(constScalarCrefsOut,ComponentReference.printComponentRefStr),"\n")+"\n");
567+
//print("constComplexCrefs\n"+stringDelimitList(List.map(constComplexCrefs,ComponentReference.printComponentRefStr),"\n")+"\n");
568+
569569
if not funcIsConst then
570570
(algs,constEqs) = buildPartialFunction((varScalarCrefsInFunc,algs),(constScalarCrefs,constScalarExps,constComplexCrefs,constComplexExps,constScalarCrefsOut),repl);
571571
else
572572
constEqs = {};
573573
end if;
574574

575-
//print("constScalarCrefsOut\n"+stringDelimitList(List.map(constScalarCrefsOut,ComponentReference.printComponentRefStr),"\n")+"\n");
576-
//print("constComplexCrefs\n"+stringDelimitList(List.map(constComplexCrefs,ComponentReference.printComponentRefStr),"\n")+"\n");
577-
578575
// build the new partial function
579576
elements = listAppend(allInputs,updatedVarOutputs);
580577
elements = listAppend(elements,protectVars);
@@ -597,21 +594,21 @@ algorithm
597594
singleOutputType = if not listEmpty(newOutputVars) then listHead(outputVarTypes) else singleOutputType;//if the function is evaluated completely
598595
attr1 = DAEUtil.replaceCallAttrType(attr1,singleOutputType);
599596
attr2 = if intEq(listLength(newOutputVars),1) then attr1 else attr2;
600-
//DAEDump.dumpCallAttr(attr2);
597+
DAEDump.dumpCallAttr(attr2);
601598

602599
exp2 = if List.hasOneElement(constComplexExps) and funcIsConst then listHead(constComplexExps) else DAE.TUPLE(constComplexExps); // either a single equation or a tuple equation
603600
exp = if funcIsConst then exp2 else rhsExpIn;
604-
exp = if funcIsPartConst then DAE.CALL(path, exps, attr2) else exp;
601+
exp = if funcIsPartConst then DAE.CALL(path, expsIn, attr2) else exp;
605602
exp = if isConstRec then DAE.TUPLE(constScalarExps) else exp;
606-
607-
//BackendDump.dumpEquationList(constEqs,"the additional equations\n");
608-
//print("LHS EXP:\n");
609-
//ExpressionDump.dumpExp(outputExp);
603+
604+
//BackendDump.dumpEquationList(constEqs,"the additional equations\n");
605+
//print("LHS EXP:\n");
606+
//ExpressionDump.dumpExp(outputExp);
610607
outputExp = setRecordTypes(outputExp,FUNCINFO(repl,funcs,idx));
611-
//print("RHS EXP:\n");
612-
//ExpressionDump.dumpExp(exp);
608+
//print("RHS EXP:\n");
609+
//ExpressionDump.dumpExp(exp);
613610
if Flags.isSet(Flags.EVAL_FUNC_DUMP) then
614-
print("Finish evaluation in:\n"+ExpressionDump.printExpStr(outputExp)+" := "+ExpressionDump.printExpStr(exp)+"\n");
611+
print("Finish evaluation:\n of: \n"+ExpressionDump.printExpStr(rhsExpIn)+"\nto:\n"+ExpressionDump.printExpStr(outputExp)+" := "+ExpressionDump.printExpStr(exp)+"\n");
615612
if not listEmpty(constEqs) then
616613
BackendDump.dumpEquationList(constEqs,"including the additional equations:\n");
617614
end if;
@@ -696,9 +693,13 @@ protected
696693
list<Boolean> bLst;
697694
list<DAE.Statement> stmtLst;
698695
algorithm
699-
stmtLst := DAEUtil.getStatement(stmt);
700-
bLst := List.map(stmtLst,DAEUtil.isStmtAssert);
701-
bOut := List.fold(bLst,boolOr,bIn);
696+
try
697+
stmtLst := DAEUtil.getStatement(stmt);
698+
bLst := List.map(stmtLst,DAEUtil.isStmtAssert);
699+
bOut := List.fold(bLst,boolOr,bIn);
700+
else
701+
bOut := false;
702+
end try;
702703
end hasAssertFold;
703704

704705
protected function hasReturnFold "fold function to check if a list of stmts has an return stmt.
@@ -710,9 +711,13 @@ protected
710711
list<Boolean> bLst;
711712
list<DAE.Statement> stmtLst;
712713
algorithm
713-
stmtLst := DAEUtil.getStatement(stmt);
714-
bLst := List.map(stmtLst,DAEUtil.isStmtReturn);
715-
bOut := List.fold(bLst,boolOr,bIn);
714+
try
715+
stmtLst := DAEUtil.getStatement(stmt);
716+
bLst := List.map(stmtLst,DAEUtil.isStmtReturn);
717+
bOut := List.fold(bLst,boolOr,bIn);
718+
else
719+
bOut := false;
720+
end try;
716721
end hasReturnFold;
717722

718723
protected function hasReinitFold "fold function to check if a list of stmts has an reinit stmt.
@@ -724,9 +729,13 @@ protected
724729
list<Boolean> bLst;
725730
list<DAE.Statement> stmtLst;
726731
algorithm
727-
stmtLst := DAEUtil.getStatement(stmt);
728-
bLst := List.map(stmtLst,DAEUtil.isStmtReturn);
729-
bOut := List.fold(bLst,boolOr,bIn);
732+
try
733+
stmtLst := DAEUtil.getStatement(stmt);
734+
bLst := List.map(stmtLst,DAEUtil.isStmtReturn);
735+
bOut := List.fold(bLst,boolOr,bIn);
736+
else
737+
bOut := false;
738+
end try;
730739
end hasReinitFold;
731740

732741
protected function setRecordTypes "This is somehow a hack for FourBitBinaryAdder because there are function calls in the daelow on the lhs of a function call and this leads to an error in simcode creation
@@ -1025,8 +1034,8 @@ algorithm
10251034
equation
10261035
scalars = getScalarsForComplexVar(elem);
10271036
// function outputs a record, its either constCompl or constScalar and varScalar
1028-
print("the cref\n"+stringDelimitList(List.map({DAEUtil.varCref(elem)},ComponentReference.printComponentRefStr),"\n")+"\n");
1029-
print("scalars to check\n"+stringDelimitList(List.map(scalars,ComponentReference.printComponentRefStr),"\n")+"\n");
1037+
//print("the cref\n"+stringDelimitList(List.map({DAEUtil.varCref(elem)},ComponentReference.printComponentRefStr),"\n")+"\n");
1038+
//print("scalars to check\n"+stringDelimitList(List.map(scalars,ComponentReference.printComponentRefStr),"\n")+"\n");
10301039

10311040
false = listEmpty(scalars);
10321041
(constVars,_,_) = List.intersection1OnTrue(scalars,constCrefs,ComponentReference.crefEqual);
@@ -1266,7 +1275,7 @@ protected
12661275
algorithm
12671276
(varScalarCrefs,funcAlgs) := varPart;
12681277
(constScalarCrefs,constScalarExps,constComplCrefs,constComplExps,constScalarCrefsOut) := constPart;
1269-
1278+
funcAlgs := List.filterOnTrue(funcAlgs,DAEUtil.isAlgorithm);// get only the algs, not protected vars or stuff
12701279
// generate the additional equations for the constant scalar values and the constant complex ones
12711280
lhsExps1 := List.map(constScalarCrefsOut,Expression.crefExp);
12721281
lhsExps2 := List.map(constComplCrefs,Expression.crefExp);
@@ -1609,10 +1618,10 @@ algorithm
16091618
scalars = getRecordScalars(cref);
16101619
(exp2,_) = BackendVarTransform.replaceExp(exp2,replIn,NONE());
16111620
(exp2,(exp1,funcTree,idx,addStmts)) = Expression.traverseExpTopDown(exp2,evaluateConstantFunctionWrapper,(exp1,funcTree,idx,{}));
1621+
16121622
// (exp2,changed) = bcallret1_2(changed,ExpressionSimplify.simplify,exp2,exp2,changed); This does nothing useful with the statement below...
16131623
(exp2,_) = ExpressionSimplify.simplify(exp2);
16141624
expLst = Expression.getComplexContents(exp2);
1615-
//print("SIMPLIFIED\n"+stringDelimitList(List.map({exp2},ExpressionDump.printExpStr),"\n")+"\n");
16161625

16171626
// add the replacements for the addStmts and remove the replacements for the variable outputs
16181627
repl = List.fold(addStmts,addReplacementRuleForAssignment,replIn);
@@ -1623,7 +1632,7 @@ algorithm
16231632
// check if its constant, a record or a tuple
16241633
isCon = Expression.isConst(exp2);
16251634
eqDim = listLength(scalars) == listLength(expLst); // so it can be partly constant
1626-
isRec = ComponentReference.isRecord(cref);
1635+
isRec = ComponentReference.isRecord(cref) or Expression.isCall(exp2);
16271636
isTpl = Expression.isTuple(exp1) and Expression.isTuple(exp2);
16281637
_ = Expression.isCall(exp2);
16291638
//print("is it const? "+boolString(isCon)+" ,is it rec: "+boolString(isRec)+" ,is it tpl: "+boolString(isTpl)+" ,is it call: "+boolString(isCall)+"\n");

0 commit comments

Comments
 (0)