Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Conflicts:
	Compiler/Template/CodegenCpp.tpl
  • Loading branch information
niklwors committed Sep 25, 2015
2 parents 19c8d32 + 6b5d3a2 commit 9aa4b65
Show file tree
Hide file tree
Showing 56 changed files with 1,891 additions and 802 deletions.
29 changes: 17 additions & 12 deletions Compiler/BackEnd/BackendDAECreate.mo
Expand Up @@ -60,6 +60,7 @@ protected import DynamicOptimization;
protected import Error;
protected import Expression;
protected import ExpressionDump;
protected import ExpressionSolve;
protected import Flags;
protected import Global;
protected import HashTableExpToExp;
Expand Down Expand Up @@ -1760,8 +1761,7 @@ protected function lowerWhenEqn2
protected
Inline.Functiontuple fns = (SOME(functionTree), {DAE.NORM_INLINE()});
algorithm
(outEquationLst, outREquationLst):=
matchcontinue inDAEElementLst
(outEquationLst, outREquationLst) := matchcontinue inDAEElementLst
local
Integer size;
list<BackendDAE.Equation> eqnl;
Expand Down Expand Up @@ -1795,6 +1795,7 @@ algorithm

case DAE.DEFINE(componentRef = cr, exp = e, source = source)::xs
equation
(e, _) = ExpressionSolve.solve(Expression.crefExp(cr), e, Expression.crefExp(cr));
(DAE.PARTIAL_EQUATION(e), source) = Inline.simplifyAndInlineEquationExp(DAE.PARTIAL_EQUATION(e), fns, source);
whenOp = BackendDAE.ASSIGN(cr, e, source);
whenEq = BackendDAE.WHEN_STMTS(inCond, {whenOp}, NONE());
Expand All @@ -1811,15 +1812,19 @@ algorithm
then
(eqnl, reqnl);

case DAE.EQUATION(exp = (cre as DAE.CREF(componentRef = cr)), scalar = e, source = source)::xs
equation
(DAE.EQUALITY_EXPS(_,e), source) = Inline.simplifyAndInlineEquationExp(DAE.EQUALITY_EXPS(cre,e), fns, source);
whenOp = BackendDAE.ASSIGN(cr, e, source);
whenEq = BackendDAE.WHEN_STMTS(inCond, {whenOp}, NONE());
eq = BackendDAE.WHEN_EQUATION(1, whenEq, source, BackendDAE.EQ_ATTR_DEFAULT_DYNAMIC);
(eqnl, reqnl) = lowerWhenEqn2(xs, inCond, functionTree, eq::iEquationLst, iREquationLst);
then
(eqnl, reqnl);
case (el as DAE.EQUATION(exp = (cre as DAE.CREF(componentRef = cr)), scalar = e, source = source))::xs algorithm
try
e := ExpressionSolve.solve(cre, e, cre);
else
Error.addCompilerError("Failed to solve " + DAEDump.dumpElementsStr({el}));
fail();
end try;
(DAE.EQUALITY_EXPS(_,e), source) := Inline.simplifyAndInlineEquationExp(DAE.EQUALITY_EXPS(cre,e), fns, source);
whenOp := BackendDAE.ASSIGN(cr, e, source);
whenEq := BackendDAE.WHEN_STMTS(inCond, {whenOp}, NONE());
eq := BackendDAE.WHEN_EQUATION(1, whenEq, source, BackendDAE.EQ_ATTR_DEFAULT_DYNAMIC);
(eqnl, reqnl) := lowerWhenEqn2(xs, inCond, functionTree, eq::iEquationLst, iREquationLst);
then (eqnl, reqnl);

case DAE.COMPLEX_EQUATION(lhs = (cre as DAE.CREF(componentRef = cr)), rhs = e, source = source)::xs
equation
Expand All @@ -1840,7 +1845,7 @@ algorithm
then
(eqnl, reqnl);

case (DAE.IF_EQUATION(condition1=expl, equations2=eqnslst, equations3=eqns, source = source))::xs
case DAE.IF_EQUATION(condition1=expl, equations2=eqnslst, equations3=eqns, source = source)::xs
equation
(expl, source, _) = Inline.inlineExps(expl, fns, source);
// transform if eqution
Expand Down
14 changes: 14 additions & 0 deletions Compiler/BackEnd/BackendDAEOptimize.mo
Expand Up @@ -87,8 +87,22 @@ protected import ValuesUtil;
public function simplifyAllExpressions "author: lochel"
input BackendDAE.BackendDAE inDAE;
output BackendDAE.BackendDAE outDAE = inDAE;
protected
list<BackendDAE.Equation> removedEqsList = {};
BackendDAE.Shared shared;
algorithm
_ := BackendDAEUtil.traverseBackendDAEExpsNoCopyWithUpdate(outDAE, ExpressionSimplify.simplifyTraverseHelper, 0);

// filter empty algorithms
shared := outDAE.shared;
for eq in BackendEquation.equationList(shared.removedEqs) loop
removedEqsList := match eq
case BackendDAE.ALGORITHM(alg=DAE.ALGORITHM_STMTS(statementLst={})) then removedEqsList;
else eq::removedEqsList;
end match;
end for;
shared.removedEqs := BackendEquation.listEquation(listReverse(removedEqsList));
outDAE.shared := shared;
end simplifyAllExpressions;

// =============================================================================
Expand Down
38 changes: 17 additions & 21 deletions Compiler/BackEnd/BackendDAEUtil.mo
Expand Up @@ -5830,27 +5830,22 @@ public function traverseBackendDAEExpsNoCopyWithUpdate<A>
output DAE.Exp outExp;
output A outA;
end FuncExpType;
protected
list<BackendDAE.EqSystem> systs;
BackendDAE.Shared shared;
String name;
algorithm
outTypeA := matchcontinue inBackendDAE
local
list<BackendDAE.EqSystem> systs;
BackendDAE.Shared shared;
String name;

case BackendDAE.DAE(systs, shared)
equation
outTypeA = List.fold1(systs, traverseBackendDAEExpsEqSystemWithUpdate, func, inTypeA);
outTypeA = traverseBackendDAEExpsVarsWithUpdate(shared.knownVars, func, outTypeA);
outTypeA = traverseBackendDAEExpsEqnsWithUpdate(shared.initialEqs, func, outTypeA);
outTypeA = traverseBackendDAEExpsEqnsWithUpdate(shared.removedEqs, func, outTypeA);
then
outTypeA;

else equation
(_, _, name) = System.dladdr(func);
Error.addInternalError("traverseBackendDAEExpsNoCopyWithUpdate failed for " + name, sourceInfo());
then fail();
end matchcontinue;
try
BackendDAE.DAE(systs, shared) := inBackendDAE;
outTypeA := List.fold1(systs, traverseBackendDAEExpsEqSystemWithUpdate, func, inTypeA);
outTypeA := traverseBackendDAEExpsVarsWithUpdate(shared.knownVars, func, outTypeA);
outTypeA := traverseBackendDAEExpsEqnsWithUpdate(shared.initialEqs, func, outTypeA);
outTypeA := traverseBackendDAEExpsEqnsWithUpdate(shared.removedEqs, func, outTypeA);
else
(_, _, name) := System.dladdr(func);
Error.addInternalError("traverseBackendDAEExpsNoCopyWithUpdate failed for " + name, sourceInfo());
fail();
end try;
end traverseBackendDAEExpsNoCopyWithUpdate;

public function traverseBackendDAEExpsEqSystem "This function goes through the BackendDAE structure and finds all the
Expand Down Expand Up @@ -7697,7 +7692,8 @@ algorithm
case {}
then (inConditionVarList, inInitialCall);

case DAE.BCONST(false)::conditionList equation
// filter constant conditions
case exp::conditionList guard(Expression.isConst(exp)) equation
(conditionVarList, initialCall) = getConditionList1(conditionList, inConditionVarList, inInitialCall);
then (conditionVarList, initialCall);

Expand Down
66 changes: 27 additions & 39 deletions Compiler/BackEnd/BackendDump.mo
Expand Up @@ -251,7 +251,6 @@ end printClassAttributes;
public function printShared "This function dumps the BackendDAE.Shared representation to stdout."
input BackendDAE.Shared inShared;
algorithm

print("\nBackendDAEType: ");
printBackendDAEType(inShared.backendDAEType);
print("\n\n");
Expand All @@ -272,6 +271,10 @@ algorithm
dumpConstraintList(inShared.constraints, "Constraints");
dumpBasePartitions(inShared.partitionsInfo.basePartitions, "Base partitions");
dumpSubPartitions(inShared.partitionsInfo.subPartitions, "Sub partitions");

if Flags.isSet(Flags.DUMP_FUNCTIONS) then
DAEDump.dumpFunctionTree(inShared.functionTree, "Functions");
end if;
end printShared;

public function printBasePartitions
Expand Down Expand Up @@ -567,7 +570,7 @@ algorithm
Error.addMessage(Error.BACKEND_DAE_TO_MODELICA, {suffix, str});
end dumpBackendDAEToModelica;

public function dumpEqSystem "function dumpEqSystem"
public function dumpEqSystem
input BackendDAE.EqSystem inEqSystem;
input String heading;
algorithm
Expand Down Expand Up @@ -1399,47 +1402,32 @@ algorithm
end match;
end strongComponentString;

public function whenEquationString "Helper function to equationString"
public function whenEquationString
input BackendDAE.WhenEquation inWhenEqn;
input Boolean inStart;
output String outString;
algorithm
outString := match (inWhenEqn, inStart)
local
String s1,s2,res,s3,cs;
DAE.Exp e2,cond;
DAE.ComponentRef cr;
BackendDAE.WhenEquation weqn;
Option<BackendDAE.WhenEquation> oweqn;
list<BackendDAE.WhenOperator> whenStmtLst;
case (BackendDAE.WHEN_STMTS(condition=cond, whenStmtLst = whenStmtLst, elsewhenPart = oweqn), true)
equation
s1 = ExpressionDump.printExpStr(cond);
s2 = stringDelimitList(List.map(whenStmtLst, dumpWhenOperatorStr), " ");
if isSome(oweqn) then
SOME(weqn) = oweqn;
s3 = whenEquationString(weqn, false);
else
s3 = "";
end if;
res = stringAppendList({"when ",s1," then\n ",s2,"\n",s3,"\nend when"});
then
res;
protected
String conditionStr, whenStmtStr, elseWhenStr;
DAE.Exp cond;
BackendDAE.WhenEquation weqn;
Option<BackendDAE.WhenEquation> oweqn;
list<BackendDAE.WhenOperator> whenStmtLst;
algorithm
BackendDAE.WHEN_STMTS(condition=cond, whenStmtLst=whenStmtLst, elsewhenPart=oweqn) := inWhenEqn;
conditionStr := ExpressionDump.printExpStr(cond);
whenStmtStr := stringDelimitList(List.map(whenStmtLst, dumpWhenOperatorStr), ";\n ") + ";\n";
if isSome(oweqn) then
SOME(weqn) := oweqn;
elseWhenStr := whenEquationString(weqn, false);
else
elseWhenStr := "";
end if;

case (BackendDAE.WHEN_STMTS(condition=cond, whenStmtLst = whenStmtLst, elsewhenPart = oweqn), false)
equation
s1 = ExpressionDump.printExpStr(cond);
s2 = stringDelimitList(List.map(whenStmtLst, dumpWhenOperatorStr), " ");
if isSome(oweqn) then
SOME(weqn) = oweqn;
s3 = whenEquationString(weqn, false);
else
s3 = "";
end if;
res = stringAppendList({"elsewhen ",s1," then\n ",s2,"\n",s3});
then
res;
end match;
if inStart then
outString := "when " + conditionStr + " then\n " + whenStmtStr + elseWhenStr + "end when;";
else
outString := "elsewhen " + conditionStr + " then\n " + whenStmtStr + elseWhenStr;
end if;
end whenEquationString;

public function equationString "Helper function to e.g. dump."
Expand Down

0 comments on commit 9aa4b65

Please sign in to comment.