Skip to content
This repository was archived by the owner on May 18, 2019. It is now read-only.

Commit a588c55

Browse files
rfrankeOpenModelica-Hudson
authored andcommitted
Generalize FOR_EQUATION in backend from left=right to any equation
Belonging to [master]: - #2746 - OpenModelica/OpenModelica-testsuite#1065
1 parent 01a2928 commit a588c55

File tree

9 files changed

+40
-43
lines changed

9 files changed

+40
-43
lines changed

Compiler/BackEnd/BackendDAE.mo

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,8 +394,7 @@ uniontype Equation
394394
.DAE.Exp iter "the iterator variable";
395395
.DAE.Exp start "start of iteration";
396396
.DAE.Exp stop "end of iteration";
397-
.DAE.Exp left;
398-
.DAE.Exp right;
397+
Equation body "iterated equation";
399398
.DAE.ElementSource source "origin of equation";
400399
EquationAttributes attr;
401400
end FOR_EQUATION;

Compiler/BackEnd/BackendDAECreate.mo

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,19 +1459,12 @@ function lowerForEquation
14591459
protected
14601460
DAE.Exp iterExp, start, stop;
14611461
DAE.Type ty;
1462-
DAE.Exp left, right;
1463-
DAE.ElementSource source;
1464-
BackendDAE.EquationAttributes attr;
14651462
algorithm
14661463
DAE.RANGE(ty=ty, start=start, stop=stop) := range;
14671464
iterExp := DAE.CREF(DAE.CREF_IDENT(iter, ty, {}), ty);
1468-
forEq := match eq
1469-
case BackendDAE.EQUATION(exp=left, scalar=right, source=source, attr=attr)
1470-
then BackendDAE.FOR_EQUATION(iterExp, start, stop, left, right, source, attr);
1471-
else algorithm
1472-
Error.addSourceMessage(Error.INTERNAL_ERROR, {"BackendDAECreate.lowerForEquation: unsupported equation " + BackendDump.equationString(eq)}, sourceInfo());
1473-
then fail();
1474-
end match;
1465+
forEq := BackendDAE.FOR_EQUATION(iterExp, start, stop, eq,
1466+
BackendEquation.equationSource(eq),
1467+
BackendEquation.getEquationAttributes(eq));
14751468
end lowerForEquation;
14761469

14771470
protected function lowerIfEquation

Compiler/BackEnd/BackendDAETransform.mo

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ algorithm
569569
DAE.ComponentRef cr, cr1;
570570
Integer size;
571571
list<DAE.Exp> expl;
572-
BackendDAE.Equation res;
572+
BackendDAE.Equation eqn;
573573
BackendDAE.WhenEquation elsepartRes;
574574
BackendDAE.WhenEquation elsepart;
575575
Option<BackendDAE.WhenEquation> oelsepart;
@@ -597,11 +597,9 @@ algorithm
597597
source = List.foldr(ops, ElementSource.addSymbolicTransformation, source);
598598
then (BackendDAE.ARRAY_EQUATION(dimSize, e1_1, e2_1, source, eqAttr), ext_arg_2);
599599

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

606604
case BackendDAE.SOLVED_EQUATION(componentRef = cr, exp = e2, source=source, attr=eqAttr) equation
607605
e1 = Expression.crefExp(cr);
@@ -633,8 +631,8 @@ algorithm
633631
oelsepart = NONE();
634632
ext_arg_3 = ext_arg_2;
635633
end if;
636-
res = BackendDAE.WHEN_EQUATION(size, BackendDAE.WHEN_STMTS(cond, whenStmtLst, oelsepart), source, eqAttr);
637-
then (res, ext_arg_3);
634+
eqn = BackendDAE.WHEN_EQUATION(size, BackendDAE.WHEN_STMTS(cond, whenStmtLst, oelsepart), source, eqAttr);
635+
then (eqn, ext_arg_3);
638636

639637
case BackendDAE.COMPLEX_EQUATION(size=size, left = e1, right = e2, source = source, attr=eqAttr) equation
640638
(e1_1, (ops, ext_arg_1)) = func(e1, ({}, inTypeA));

Compiler/BackEnd/BackendDAEUtil.mo

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2511,6 +2511,7 @@ algorithm
25112511
list<DAE.Statement> statementLst;
25122512
list<list<BackendDAE.Equation>> eqnslst;
25132513
list<BackendDAE.Equation> eqns;
2514+
BackendDAE.Equation eqn;
25142515
list<BackendDAE.WhenOperator> whenStmtLst;
25152516

25162517
// EQUATION
@@ -2539,16 +2540,12 @@ algorithm
25392540
(res,size);
25402541

25412542
// FOR_EQUATION
2542-
case BackendDAE.FOR_EQUATION(left = e1, right = e2, start = e, stop = cond,
2543-
iter = DAE.CREF(componentRef = DAE.CREF_IDENT(ident = str)))
2543+
case BackendDAE.FOR_EQUATION(body = eqn, iter = DAE.CREF(componentRef = DAE.CREF_IDENT(ident = str)))
25442544
equation
25452545
// assume one equation defining a whole array var (no NF_SCALARIZE)
2546-
e1 = Expression.traverseExpTopDown(e1, stripIterSub, str);
2547-
e2 = Expression.traverseExpTopDown(e2, stripIterSub, str);
2548-
lst1 = incidenceRowExp(e1, vars, iRow, functionTree, inIndexType);
2549-
res = incidenceRowExp(e2, vars, lst1, functionTree, inIndexType);
2546+
eqn = BackendEquation.traverseExpsOfEquation(eqn, function Expression.traverseExpTopDown(func = stripIterSub), str);
25502547
then
2551-
(res,1);
2548+
incidenceRow(eqn, vars, inIndexType, functionTree, iRow);
25522549

25532550
// SOLVED_EQUATION
25542551
case BackendDAE.SOLVED_EQUATION(componentRef = cr,exp = e)

Compiler/BackEnd/BackendDump.mo

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1582,6 +1582,7 @@ algorithm
15821582
DAE.Exp e1,e2,e,cond, start, stop, iter;
15831583
list<DAE.Exp> expl;
15841584
DAE.ComponentRef cr;
1585+
BackendDAE.Equation eqn;
15851586
BackendDAE.WhenEquation weqn;
15861587
BackendDAE.EquationAttributes attr;
15871588
DAE.Algorithm alg;
@@ -1641,11 +1642,11 @@ algorithm
16411642
res = ifequationString(expl,eqnstrue,eqnsfalse,s3);
16421643
then
16431644
res;
1644-
case (BackendDAE.FOR_EQUATION(iter=iter, start=start, stop=stop, left=e1, right=e2))
1645+
case BackendDAE.FOR_EQUATION(iter = iter, start = start, stop = stop, body = eqn)
16451646
equation
16461647
s1 = ExpressionDump.printExpStr(iter) + " in " + ExpressionDump.printExpStr(start) + " : " + ExpressionDump.printExpStr(stop);
1647-
s2 = ExpressionDump.printExpStr(e1) + "=" + ExpressionDump.printExpStr(e2);
1648-
res = stringAppendList({"for ",s1," loop \n ",s2, "; end for; "});
1648+
s2 = equationString(eqn);
1649+
res = stringAppendList({"for ", s1, " loop \n ", s2, "; end for; "});
16491650
then
16501651
res;
16511652
end match;

Compiler/BackEnd/BackendEquation.mo

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,7 @@ algorithm
796796
list<DAE.Exp> expl;
797797
DAE.Type tp;
798798
DAE.ComponentRef cr, cr1;
799+
BackendDAE.Equation eqn;
799800
BackendDAE.WhenEquation we, we_1;
800801
DAE.ElementSource source;
801802
Integer size;
@@ -848,10 +849,9 @@ algorithm
848849
(eqns, extArg) = List.map1Fold(eqns, traverseExpsOfEquation, inFunc, extArg);
849850
then (BackendDAE.IF_EQUATION(expl, eqnslst, eqns, source, attr), extArg);
850851

851-
case BackendDAE.FOR_EQUATION(iter=iter,start=start,stop=stop,left=e1, right=e2, source=source, attr=attr) equation
852-
(e_1, extArg) = inFunc(e1, inTypeA);
853-
(e_2, extArg) = inFunc(e2, extArg);
854-
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);
852+
case BackendDAE.FOR_EQUATION(iter = iter, start = start, stop = stop, source = source, attr = attr) equation
853+
(eqn, extArg) = traverseExpsOfEquation(inEquation.body, inFunc, inTypeA);
854+
then (BackendDAE.FOR_EQUATION(iter, start, stop, eqn, source, attr), extArg);
855855
end match;
856856
end traverseExpsOfEquation;
857857

@@ -1694,7 +1694,7 @@ algorithm
16941694
then size;
16951695

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

17001700
else equation
@@ -1970,7 +1970,6 @@ algorithm
19701970
list<Integer> dimSize;
19711971
DAE.Exp lhs;
19721972
DAE.Exp rhs;
1973-
DAE.Exp iter, start, stop;
19741973
DAE.ComponentRef componentRef;
19751974
Integer size;
19761975
DAE.Algorithm alg;
@@ -1986,8 +1985,8 @@ algorithm
19861985
case (BackendDAE.ARRAY_EQUATION(dimSize=dimSize, left=lhs, right=rhs, source=source), _)
19871986
then BackendDAE.ARRAY_EQUATION(dimSize, lhs, rhs, source, inAttr);
19881987

1989-
case (BackendDAE.FOR_EQUATION(iter=iter, start=start, stop=stop, left=lhs, right=rhs, source=source), _)
1990-
then BackendDAE.FOR_EQUATION(iter, start, stop, lhs, rhs, source, inAttr);
1988+
case (BackendDAE.FOR_EQUATION(), _)
1989+
then BackendDAE.FOR_EQUATION(inEqn.iter, inEqn.start, inEqn.stop, inEqn.body, inEqn.source, inAttr);
19911990

19921991
case (BackendDAE.SOLVED_EQUATION(componentRef=componentRef, exp=rhs, source=source), _)
19931992
then BackendDAE.SOLVED_EQUATION(componentRef, rhs, source, inAttr);

Compiler/BackEnd/SynchronousFeatures.mo

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,8 @@ algorithm
275275
then BackendDAE.FOR_EQUATION(
276276
DAE.CREF(DAE.CREF_IDENT("i", DAE.T_INTEGER_DEFAULT, {}), DAE.T_INTEGER_DEFAULT),
277277
DAE.ICONST(1), DAEUtil.dimExp(dim),
278-
exp, exp2, var.source, BackendDAE.EQ_ATTR_DEFAULT_DYNAMIC);
278+
BackendDAE.EQUATION(exp, exp2, var.source, BackendDAE.EQ_ATTR_DEFAULT_DYNAMIC),
279+
var.source, BackendDAE.EQ_ATTR_DEFAULT_DYNAMIC);
279280
else BackendDAE.EQUATION(exp, exp2, var.source, BackendDAE.EQ_ATTR_DEFAULT_DYNAMIC);
280281
end match;
281282
lstEqs := eq :: lstEqs;
@@ -2735,7 +2736,7 @@ algorithm
27352736
SourceInfo info;
27362737
case BackendDAE.EQUATION(scalar = e) then isClockExp(e);
27372738
case BackendDAE.ARRAY_EQUATION(right = e) then isClockExp(e);
2738-
case BackendDAE.FOR_EQUATION(right = e) then isClockExp(e);
2739+
case BackendDAE.FOR_EQUATION(body = eq) then isClockEquation(eq);
27392740
case BackendDAE.SOLVED_EQUATION(exp = e) then isClockExp(e);
27402741
case BackendDAE.RESIDUAL_EQUATION(exp = e) then isClockExp(e);
27412742
case BackendDAE.ALGORITHM() then false;

Compiler/BackEnd/Vectorization.mo

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ case(eq::rest,_)
732732
iterator := DAE.CREF(DAE.CREF_IDENT("i",DAE.T_INTEGER_DEFAULT,{}),DAE.T_INTEGER_DEFAULT);
733733
//lhs := BackendArrayVarTransform.replaceSubExp(Expression.crefExp(cref1),DAE.INDEX(iterator));
734734
//rhs := BackendArrayVarTransform.replaceSubExp(Expression.crefExp(cref1),DAE.INDEX(DAE.ICONST(listLength(similarEqs)+1)));
735-
eq := BackendDAE.FOR_EQUATION(iterator,DAE.ICONST(1),DAE.ICONST(listLength(similarEqs)),lhs,rhs,source,attr);
735+
eq := BackendDAE.FOR_EQUATION(iterator,DAE.ICONST(1),DAE.ICONST(listLength(similarEqs)),BackendDAE.EQUATION(lhs,rhs,source,attr),source,attr);
736736
//BackendDump.dumpEquationList({eq},"got eq assignment");
737737
foldEqs := buildBackendDAEForEquations(rest,(eq::foldIn));
738738
then
@@ -761,7 +761,7 @@ case(eq::rest,_)
761761
// update crefs in equation
762762
iterator := DAE.CREF(DAE.CREF_IDENT("i",DAE.T_INTEGER_DEFAULT,{}),DAE.T_INTEGER_DEFAULT);
763763
(BackendDAE.EQUATION(exp=lhs,scalar=rhs),_) := BackendEquation.traverseExpsOfEquation(eq,setIteratorSubscriptCrefinEquation,(crefMinMax,iterator,crefs2));
764-
eq := BackendDAE.FOR_EQUATION(iterator,DAE.ICONST(min),DAE.ICONST(max),lhs,rhs,source,attr);
764+
eq := BackendDAE.FOR_EQUATION(iterator,DAE.ICONST(min),DAE.ICONST(max),BackendDAE.EQUATION(lhs,rhs,source,attr),source,attr);
765765
//BackendDump.dumpEquationList({eq},"got eq");
766766
foldEqs := buildBackendDAEForEquations(rest,(eq::foldIn));
767767
then

Compiler/SimCode/SimCodeUtil.mo

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2111,8 +2111,17 @@ algorithm
21112111
({SimCode.SES_SIMPLE_ASSIGN(iuniqueEqIndex, cr, e2, source, eqAttr)}, iuniqueEqIndex + 1, itempvars);
21122112

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

0 commit comments

Comments
 (0)