Skip to content

Commit

Permalink
Generalize FOR_EQUATION in backend from left=right to any equation
Browse files Browse the repository at this point in the history
  • Loading branch information
rfranke authored and OpenModelica-Hudson committed Oct 28, 2018
1 parent 01a2928 commit a588c55
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 43 deletions.
3 changes: 1 addition & 2 deletions Compiler/BackEnd/BackendDAE.mo
Expand Up @@ -394,8 +394,7 @@ uniontype Equation
.DAE.Exp iter "the iterator variable";
.DAE.Exp start "start of iteration";
.DAE.Exp stop "end of iteration";
.DAE.Exp left;
.DAE.Exp right;
Equation body "iterated equation";
.DAE.ElementSource source "origin of equation";
EquationAttributes attr;
end FOR_EQUATION;
Expand Down
13 changes: 3 additions & 10 deletions Compiler/BackEnd/BackendDAECreate.mo
Expand Up @@ -1459,19 +1459,12 @@ function lowerForEquation
protected
DAE.Exp iterExp, start, stop;
DAE.Type ty;
DAE.Exp left, right;
DAE.ElementSource source;
BackendDAE.EquationAttributes attr;
algorithm
DAE.RANGE(ty=ty, start=start, stop=stop) := range;
iterExp := DAE.CREF(DAE.CREF_IDENT(iter, ty, {}), ty);
forEq := match eq
case BackendDAE.EQUATION(exp=left, scalar=right, source=source, attr=attr)
then BackendDAE.FOR_EQUATION(iterExp, start, stop, left, right, source, attr);
else algorithm
Error.addSourceMessage(Error.INTERNAL_ERROR, {"BackendDAECreate.lowerForEquation: unsupported equation " + BackendDump.equationString(eq)}, sourceInfo());
then fail();
end match;
forEq := BackendDAE.FOR_EQUATION(iterExp, start, stop, eq,
BackendEquation.equationSource(eq),
BackendEquation.getEquationAttributes(eq));
end lowerForEquation;

protected function lowerIfEquation
Expand Down
14 changes: 6 additions & 8 deletions Compiler/BackEnd/BackendDAETransform.mo
Expand Up @@ -569,7 +569,7 @@ algorithm
DAE.ComponentRef cr, cr1;
Integer size;
list<DAE.Exp> expl;
BackendDAE.Equation res;
BackendDAE.Equation eqn;
BackendDAE.WhenEquation elsepartRes;
BackendDAE.WhenEquation elsepart;
Option<BackendDAE.WhenEquation> oelsepart;
Expand Down Expand Up @@ -597,11 +597,9 @@ algorithm
source = List.foldr(ops, ElementSource.addSymbolicTransformation, source);
then (BackendDAE.ARRAY_EQUATION(dimSize, e1_1, e2_1, source, eqAttr), ext_arg_2);

case BackendDAE.FOR_EQUATION(iter = iter, start = start, stop = stop, left = e1, right = e2, source = source, attr = eqAttr) equation
(e1_1, (ops, ext_arg_1)) = func(e1, ({}, inTypeA));
(e2_1, (ops, ext_arg_2)) = func(e2, (ops, ext_arg_1));
source = List.foldr(ops, ElementSource.addSymbolicTransformation, source);
then (BackendDAE.FOR_EQUATION(iter, start, stop, e1_1, e2_1, source, eqAttr), ext_arg_2);
case BackendDAE.FOR_EQUATION(iter = iter, start = start, stop = stop, body = eqn, source = source, attr = eqAttr) equation
(eqn, outTypeA) = traverseBackendDAEExpsEqnWithSymbolicOperation(eqn, func, inTypeA);
then (BackendDAE.FOR_EQUATION(iter, start, stop, eqn, source, eqAttr), outTypeA);

case BackendDAE.SOLVED_EQUATION(componentRef = cr, exp = e2, source=source, attr=eqAttr) equation
e1 = Expression.crefExp(cr);
Expand Down Expand Up @@ -633,8 +631,8 @@ algorithm
oelsepart = NONE();
ext_arg_3 = ext_arg_2;
end if;
res = BackendDAE.WHEN_EQUATION(size, BackendDAE.WHEN_STMTS(cond, whenStmtLst, oelsepart), source, eqAttr);
then (res, ext_arg_3);
eqn = BackendDAE.WHEN_EQUATION(size, BackendDAE.WHEN_STMTS(cond, whenStmtLst, oelsepart), source, eqAttr);
then (eqn, ext_arg_3);

case BackendDAE.COMPLEX_EQUATION(size=size, left = e1, right = e2, source = source, attr=eqAttr) equation
(e1_1, (ops, ext_arg_1)) = func(e1, ({}, inTypeA));
Expand Down
11 changes: 4 additions & 7 deletions Compiler/BackEnd/BackendDAEUtil.mo
Expand Up @@ -2511,6 +2511,7 @@ algorithm
list<DAE.Statement> statementLst;
list<list<BackendDAE.Equation>> eqnslst;
list<BackendDAE.Equation> eqns;
BackendDAE.Equation eqn;
list<BackendDAE.WhenOperator> whenStmtLst;

// EQUATION
Expand Down Expand Up @@ -2539,16 +2540,12 @@ algorithm
(res,size);

// FOR_EQUATION
case BackendDAE.FOR_EQUATION(left = e1, right = e2, start = e, stop = cond,
iter = DAE.CREF(componentRef = DAE.CREF_IDENT(ident = str)))
case BackendDAE.FOR_EQUATION(body = eqn, iter = DAE.CREF(componentRef = DAE.CREF_IDENT(ident = str)))
equation
// assume one equation defining a whole array var (no NF_SCALARIZE)
e1 = Expression.traverseExpTopDown(e1, stripIterSub, str);
e2 = Expression.traverseExpTopDown(e2, stripIterSub, str);
lst1 = incidenceRowExp(e1, vars, iRow, functionTree, inIndexType);
res = incidenceRowExp(e2, vars, lst1, functionTree, inIndexType);
eqn = BackendEquation.traverseExpsOfEquation(eqn, function Expression.traverseExpTopDown(func = stripIterSub), str);
then
(res,1);
incidenceRow(eqn, vars, inIndexType, functionTree, iRow);

// SOLVED_EQUATION
case BackendDAE.SOLVED_EQUATION(componentRef = cr,exp = e)
Expand Down
7 changes: 4 additions & 3 deletions Compiler/BackEnd/BackendDump.mo
Expand Up @@ -1582,6 +1582,7 @@ algorithm
DAE.Exp e1,e2,e,cond, start, stop, iter;
list<DAE.Exp> expl;
DAE.ComponentRef cr;
BackendDAE.Equation eqn;
BackendDAE.WhenEquation weqn;
BackendDAE.EquationAttributes attr;
DAE.Algorithm alg;
Expand Down Expand Up @@ -1641,11 +1642,11 @@ algorithm
res = ifequationString(expl,eqnstrue,eqnsfalse,s3);
then
res;
case (BackendDAE.FOR_EQUATION(iter=iter, start=start, stop=stop, left=e1, right=e2))
case BackendDAE.FOR_EQUATION(iter = iter, start = start, stop = stop, body = eqn)
equation
s1 = ExpressionDump.printExpStr(iter) + " in " + ExpressionDump.printExpStr(start) + " : " + ExpressionDump.printExpStr(stop);
s2 = ExpressionDump.printExpStr(e1) + "=" + ExpressionDump.printExpStr(e2);
res = stringAppendList({"for ",s1," loop \n ",s2, "; end for; "});
s2 = equationString(eqn);
res = stringAppendList({"for ", s1, " loop \n ", s2, "; end for; "});
then
res;
end match;
Expand Down
15 changes: 7 additions & 8 deletions Compiler/BackEnd/BackendEquation.mo
Expand Up @@ -796,6 +796,7 @@ algorithm
list<DAE.Exp> expl;
DAE.Type tp;
DAE.ComponentRef cr, cr1;
BackendDAE.Equation eqn;
BackendDAE.WhenEquation we, we_1;
DAE.ElementSource source;
Integer size;
Expand Down Expand Up @@ -848,10 +849,9 @@ algorithm
(eqns, extArg) = List.map1Fold(eqns, traverseExpsOfEquation, inFunc, extArg);
then (BackendDAE.IF_EQUATION(expl, eqnslst, eqns, source, attr), extArg);

case BackendDAE.FOR_EQUATION(iter=iter,start=start,stop=stop,left=e1, right=e2, source=source, attr=attr) equation
(e_1, extArg) = inFunc(e1, inTypeA);
(e_2, extArg) = inFunc(e2, extArg);
then (if referenceEq(e1,e_1) and referenceEq(e2,e_2) then inEquation else BackendDAE.FOR_EQUATION(iter,start,stop,e_1,e_2,source,attr), extArg);
case BackendDAE.FOR_EQUATION(iter = iter, start = start, stop = stop, source = source, attr = attr) equation
(eqn, extArg) = traverseExpsOfEquation(inEquation.body, inFunc, inTypeA);
then (BackendDAE.FOR_EQUATION(iter, start, stop, eqn, source, attr), extArg);
end match;
end traverseExpsOfEquation;

Expand Down Expand Up @@ -1694,7 +1694,7 @@ algorithm
then size;

case BackendDAE.FOR_EQUATION(start = DAE.ICONST(start), stop = DAE.ICONST(stop)) equation
size = stop - start + 1;
size = (stop - start + 1) * equationSize(eq.body);
then size;

else equation
Expand Down Expand Up @@ -1970,7 +1970,6 @@ algorithm
list<Integer> dimSize;
DAE.Exp lhs;
DAE.Exp rhs;
DAE.Exp iter, start, stop;
DAE.ComponentRef componentRef;
Integer size;
DAE.Algorithm alg;
Expand All @@ -1986,8 +1985,8 @@ algorithm
case (BackendDAE.ARRAY_EQUATION(dimSize=dimSize, left=lhs, right=rhs, source=source), _)
then BackendDAE.ARRAY_EQUATION(dimSize, lhs, rhs, source, inAttr);

case (BackendDAE.FOR_EQUATION(iter=iter, start=start, stop=stop, left=lhs, right=rhs, source=source), _)
then BackendDAE.FOR_EQUATION(iter, start, stop, lhs, rhs, source, inAttr);
case (BackendDAE.FOR_EQUATION(), _)
then BackendDAE.FOR_EQUATION(inEqn.iter, inEqn.start, inEqn.stop, inEqn.body, inEqn.source, inAttr);

case (BackendDAE.SOLVED_EQUATION(componentRef=componentRef, exp=rhs, source=source), _)
then BackendDAE.SOLVED_EQUATION(componentRef, rhs, source, inAttr);
Expand Down
5 changes: 3 additions & 2 deletions Compiler/BackEnd/SynchronousFeatures.mo
Expand Up @@ -275,7 +275,8 @@ algorithm
then BackendDAE.FOR_EQUATION(
DAE.CREF(DAE.CREF_IDENT("i", DAE.T_INTEGER_DEFAULT, {}), DAE.T_INTEGER_DEFAULT),
DAE.ICONST(1), DAEUtil.dimExp(dim),
exp, exp2, var.source, BackendDAE.EQ_ATTR_DEFAULT_DYNAMIC);
BackendDAE.EQUATION(exp, exp2, var.source, BackendDAE.EQ_ATTR_DEFAULT_DYNAMIC),
var.source, BackendDAE.EQ_ATTR_DEFAULT_DYNAMIC);
else BackendDAE.EQUATION(exp, exp2, var.source, BackendDAE.EQ_ATTR_DEFAULT_DYNAMIC);
end match;
lstEqs := eq :: lstEqs;
Expand Down Expand Up @@ -2735,7 +2736,7 @@ algorithm
SourceInfo info;
case BackendDAE.EQUATION(scalar = e) then isClockExp(e);
case BackendDAE.ARRAY_EQUATION(right = e) then isClockExp(e);
case BackendDAE.FOR_EQUATION(right = e) then isClockExp(e);
case BackendDAE.FOR_EQUATION(body = eq) then isClockEquation(eq);
case BackendDAE.SOLVED_EQUATION(exp = e) then isClockExp(e);
case BackendDAE.RESIDUAL_EQUATION(exp = e) then isClockExp(e);
case BackendDAE.ALGORITHM() then false;
Expand Down
4 changes: 2 additions & 2 deletions Compiler/BackEnd/Vectorization.mo
Expand Up @@ -732,7 +732,7 @@ case(eq::rest,_)
iterator := DAE.CREF(DAE.CREF_IDENT("i",DAE.T_INTEGER_DEFAULT,{}),DAE.T_INTEGER_DEFAULT);
//lhs := BackendArrayVarTransform.replaceSubExp(Expression.crefExp(cref1),DAE.INDEX(iterator));
//rhs := BackendArrayVarTransform.replaceSubExp(Expression.crefExp(cref1),DAE.INDEX(DAE.ICONST(listLength(similarEqs)+1)));
eq := BackendDAE.FOR_EQUATION(iterator,DAE.ICONST(1),DAE.ICONST(listLength(similarEqs)),lhs,rhs,source,attr);
eq := BackendDAE.FOR_EQUATION(iterator,DAE.ICONST(1),DAE.ICONST(listLength(similarEqs)),BackendDAE.EQUATION(lhs,rhs,source,attr),source,attr);
//BackendDump.dumpEquationList({eq},"got eq assignment");
foldEqs := buildBackendDAEForEquations(rest,(eq::foldIn));
then
Expand Down Expand Up @@ -761,7 +761,7 @@ case(eq::rest,_)
// update crefs in equation
iterator := DAE.CREF(DAE.CREF_IDENT("i",DAE.T_INTEGER_DEFAULT,{}),DAE.T_INTEGER_DEFAULT);
(BackendDAE.EQUATION(exp=lhs,scalar=rhs),_) := BackendEquation.traverseExpsOfEquation(eq,setIteratorSubscriptCrefinEquation,(crefMinMax,iterator,crefs2));
eq := BackendDAE.FOR_EQUATION(iterator,DAE.ICONST(min),DAE.ICONST(max),lhs,rhs,source,attr);
eq := BackendDAE.FOR_EQUATION(iterator,DAE.ICONST(min),DAE.ICONST(max),BackendDAE.EQUATION(lhs,rhs,source,attr),source,attr);
//BackendDump.dumpEquationList({eq},"got eq");
foldEqs := buildBackendDAEForEquations(rest,(eq::foldIn));
then
Expand Down
11 changes: 10 additions & 1 deletion Compiler/SimCode/SimCodeUtil.mo
Expand Up @@ -2111,8 +2111,17 @@ algorithm
({SimCode.SES_SIMPLE_ASSIGN(iuniqueEqIndex, cr, e2, source, eqAttr)}, iuniqueEqIndex + 1, itempvars);

// for equation that may result from -d=-nfScalarize
case BackendDAE.FOR_EQUATION(iter = varexp, start = start, stop = cond, left = e1, right = e2, source = source, attr = eqAttr)
case BackendDAE.FOR_EQUATION(iter = varexp, start = start, stop = cond, source = source, attr = eqAttr)
algorithm
(e1, e2) := match eqn.body
case BackendDAE.EQUATION(exp = e1, scalar = e2) then
(e1, e2);
case BackendDAE.ARRAY_EQUATION(left = e1, right = e2) then
(e1, e2);
else algorithm
Error.addInternalError("Unsupported FOR_EQUATION: " + BackendDump.equationString(eqn) + " ToDo: generalize SimEqSystem.SES_FOR_LOOP with embedded SimEqSystem.", sourceInfo());
then fail();
end match;
DAE.CREF(componentRef = DAE.CREF_IDENT(ident = iter)) := varexp;
cr := ComponentReference.crefSetLastSubs(v.varName, {DAE.INDEX(DAE.CREF(DAE.CREF_IDENT(iter, DAE.T_INTEGER_DEFAULT, {}), DAE.T_INTEGER_DEFAULT))});
BackendDAE.SHARED(functionTree = funcs) := shared;
Expand Down

0 comments on commit a588c55

Please sign in to comment.