Skip to content

Commit 9862bdc

Browse files
author
Peter Aronsson
committed
-Added boolean to DAEUtil.transformIfEqToExpr to only "constant evaluate" if-equations and not rewrite to if-expr. That way MathCore can use the functionality too.
-Fixed bug with for iterator over complex subscripted componentrefs, see testcase ForIterator2.mo git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@4789 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent 7eb1e7f commit 9862bdc

File tree

5 files changed

+92
-30
lines changed

5 files changed

+92
-30
lines changed

Compiler/CevalScript.mo

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ algorithm
253253
p_1 = SCodeUtil.translateAbsyn2SCode(ptot);
254254
(cache,env,_, dae as DAE.DAE(dael)) =
255255
Inst.instantiateClass(cache,InstanceHierarchy.emptyInstanceHierarchy,p_1, path);
256-
((dae as DAE.DAE(dael))) = DAEUtil.transformIfEqToExpr(dae);
256+
((dae as DAE.DAE(dael))) = DAEUtil.transformIfEqToExpr(dae,false);
257257
ic_1 = Interactive.addInstantiatedClass(ic, Interactive.INSTCLASS(path,dael,env));
258258
/*((daelow as DAELow.DAELOW(orderedVars=vars,orderedEqs=eqnarr,complexEqns = DAELow.COMPLEX_EQUATIONS(arrayEqs=ae,ifEqns=ifeqns)))) = DAELow.lower(dae, false, true) "no dummy state" ;*/
259259
((daelow as DAELow.DAELOW(vars,_,_,eqnarr,_,_,ae,_,_,_))) = DAELow.lower(dae, false, true) "no dummy state" ;
@@ -651,7 +651,7 @@ algorithm
651651
p_1 = SCodeUtil.translateAbsyn2SCode(ptot);
652652
(cache,env,_,(dae as DAE.DAE(dael))) =
653653
Inst.instantiateClass(cache,InstanceHierarchy.emptyInstanceHierarchy,p_1,className);
654-
// ((dae as DAE.DAE(dael))) = DAEUtil.transformIfEqToExpr(dael);
654+
// ((dae as DAE.DAE(dael))) = DAEUtil.transformIfEqToExpr(dael,false);
655655
ic_1 = Interactive.addInstantiatedClass(ic, Interactive.INSTCLASS(className,dael,env));
656656
str = DAEUtil.dumpStr(dae);
657657
then
@@ -2230,7 +2230,7 @@ algorithm
22302230
p_1 = SCodeUtil.translateAbsyn2SCode(p);
22312231
(cache,env,_,dae_1) =
22322232
Inst.instantiateClass(cache,InstanceHierarchy.emptyInstanceHierarchy,p_1,className);
2233-
((dae as DAE.DAE(dael))) = DAEUtil.transformIfEqToExpr(dae_1);
2233+
((dae as DAE.DAE(dael))) = DAEUtil.transformIfEqToExpr(dae_1,false);
22342234
ic_1 = Interactive.addInstantiatedClass(ic, Interactive.INSTCLASS(className,dael,env));
22352235
a_cref = Absyn.pathToCref(className);
22362236
file_dir = getFileDir(a_cref, p);
@@ -2313,7 +2313,7 @@ algorithm
23132313
p_1 = SCodeUtil.translateAbsyn2SCode(ptot);
23142314
(cache,env,_,dae as DAE.DAE(dael)) =
23152315
Inst.instantiateClass(cache,InstanceHierarchy.emptyInstanceHierarchy,p_1,className);
2316-
((dae as DAE.DAE(dael))) = DAEUtil.transformIfEqToExpr(dae);
2316+
((dae as DAE.DAE(dael))) = DAEUtil.transformIfEqToExpr(dae,false);
23172317
ic_1 = Interactive.addInstantiatedClass(ic, Interactive.INSTCLASS(className,dael,env));
23182318
dlow = DAELow.lower(dae, addDummy, true);
23192319
Debug.fprint("bltdump", "Lowered DAE:\n");
@@ -3277,7 +3277,7 @@ algorithm
32773277

32783278
(cache, env, _, dae as DAE.DAE(dael)) =
32793279
Inst.instantiateClass(inCache, InstanceHierarchy.emptyInstanceHierarchy, p_1, className);
3280-
((dae as DAE.DAE(dael))) = DAEUtil.transformIfEqToExpr(dae);
3280+
((dae as DAE.DAE(dael))) = DAEUtil.transformIfEqToExpr(dae,false);
32813281
ic_1 = Interactive.addInstantiatedClass(ic, Interactive.INSTCLASS(className,dael,env));
32823282
elimLevel = RTOpts.eliminationLevel();
32833283
RTOpts.setEliminationLevel(0); // No variable elimination
@@ -3625,7 +3625,7 @@ algorithm
36253625
cname_str = Absyn.pathString(classname);
36263626
p_1 = SCodeUtil.translateAbsyn2SCode(p);
36273627
(cache,env,_,dae_1) = Inst.instantiateClass(cache, InstanceHierarchy.emptyInstanceHierarchy, p_1, classname);
3628-
((dae as DAE.DAE(dael))) = DAEUtil.transformIfEqToExpr(dae_1);
3628+
((dae as DAE.DAE(dael))) = DAEUtil.transformIfEqToExpr(dae_1,false);
36293629
ic_1 = Interactive.addInstantiatedClass(ic, Interactive.INSTCLASS(classname,dael,env));
36303630
dlow = DAELow.lower(dae, true, true);
36313631
m = DAELow.incidenceMatrix(dlow);
@@ -3668,7 +3668,7 @@ algorithm
36683668
cname_str = Absyn.pathString(classname);
36693669
p_1 = SCodeUtil.translateAbsyn2SCode(p);
36703670
(cache,env,_,dae_1) = Inst.instantiateClass(cache, InstanceHierarchy.emptyInstanceHierarchy, p_1, classname);
3671-
((dae as DAE.DAE(dael))) = DAEUtil.transformIfEqToExpr(dae_1);
3671+
((dae as DAE.DAE(dael))) = DAEUtil.transformIfEqToExpr(dae_1,false);
36723672
ic_1 = Interactive.addInstantiatedClass(ic, Interactive.INSTCLASS(classname,dael,env));
36733673
dlow = DAELow.lower(dae, true, true);
36743674
m = DAELow.incidenceMatrix(dlow);

Compiler/DAEUtil.mo

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5103,35 +5103,35 @@ public function transformIfEqToExpr
51035103
"function: transformIfEqToExpr
51045104
transform all if equations to ordinary equations involving if-expressions"
51055105
input DAE.DAElist inDAElist;
5106+
input Boolean onlyConstantEval "if true, only perform the constant evaluation part, not transforming to if-expr";
51065107
output DAE.DAElist outDAElist;
51075108
algorithm
5108-
outDAElist := matchcontinue (inDAElist)
5109+
outDAElist := matchcontinue (inDAElist,onlyConstantEval)
51095110
local
51105111
DAE.DAElist sublist_result,result,sublist;
51115112
list<DAE.Element> rest_result,rest,res2,res1,res;
51125113
DAE.Element subresult,el;
51135114
String name;
51145115
DAE.ElementSource source "the origin of the element";
5115-
5116-
case (DAE.DAE(elementLst = {})) then DAE.DAE({});
5117-
case (DAE.DAE(elementLst = (DAE.COMP(ident = name,dAElist = sublist,source = source) :: rest)))
5116+
case (DAE.DAE(elementLst = {}),onlyConstantEval) then DAE.DAE({});
5117+
case (DAE.DAE(elementLst = (DAE.COMP(ident = name,dAElist = sublist,source=source) :: rest)),onlyConstantEval)
51185118
equation
5119-
sublist_result = transformIfEqToExpr(sublist);
5120-
DAE.DAE(rest_result) = transformIfEqToExpr(DAE.DAE(rest));
5119+
sublist_result = transformIfEqToExpr(sublist,onlyConstantEval);
5120+
DAE.DAE(rest_result) = transformIfEqToExpr(DAE.DAE(rest),onlyConstantEval);
51215121
subresult = DAE.COMP(name,sublist_result,source);
51225122
result = DAE.DAE((subresult :: rest_result));
51235123
then
51245124
result;
5125-
case (DAE.DAE(elementLst = (el :: rest)))
5125+
case (DAE.DAE(elementLst = (el :: rest)),onlyConstantEval)
51265126
equation
5127-
res1 = ifEqToExpr(el);
5128-
DAE.DAE(res2) = transformIfEqToExpr(DAE.DAE(rest));
5127+
res1 = ifEqToExpr(el,onlyConstantEval);
5128+
DAE.DAE(res2) = transformIfEqToExpr(DAE.DAE(rest),onlyConstantEval);
51295129
res = listAppend(res1, res2);
51305130
then
51315131
DAE.DAE(res);
5132-
case (DAE.DAE(elementLst = (el :: rest)))
5132+
case (DAE.DAE(elementLst = (el :: rest)),onlyConstantEval)
51335133
equation
5134-
DAE.DAE(res) = transformIfEqToExpr(DAE.DAE(rest));
5134+
DAE.DAE(res) = transformIfEqToExpr(DAE.DAE(rest),onlyConstantEval);
51355135
then
51365136
DAE.DAE((el :: res));
51375137
end matchcontinue;
@@ -5176,10 +5176,11 @@ protected function ifEqToExpr
51765176
"function: ifEqToExpr
51775177
Transform one if-equation into equations involving if-expressions"
51785178
input DAE.Element inElement;
5179+
input Boolean onlyConstantEval;
51795180
output list<DAE.Element> outElementLst;
51805181
algorithm
51815182
outElementLst:=
5182-
matchcontinue (inElement)
5183+
matchcontinue (inElement,onlyConstantEval)
51835184
local
51845185
Integer true_eq,false_eq;
51855186
String elt_str;
@@ -5192,33 +5193,46 @@ algorithm
51925193
// adrpo: handle selection of branches if conditions are boolean literals
51935194
// this is needed as Connections.isRoot becomes true/false at the
51945195
// end of instantiation.
5195-
case ((elt as DAE.IF_EQUATION(condition1 = cond,equations2 = true_branch,equations3 = false_branch)))
5196+
case ((elt as DAE.IF_EQUATION(condition1 = cond,equations2 = true_branch,equations3 = false_branch)),onlyConstantEval)
51965197
equation
51975198
equations = selectBranches(cond, true_branch, false_branch);
51985199
// transform further if needed
5199-
DAE.DAE(equations) = transformIfEqToExpr(DAE.DAE(equations));
5200+
DAE.DAE(equations) = transformIfEqToExpr(DAE.DAE(equations),onlyConstantEval);
52005201
then
52015202
equations;
52025203
// handle the erroneous case where the number of equations are not equal in different branches
5203-
case ((elt as DAE.IF_EQUATION(condition1 = cond,equations2 = true_branch,equations3 = false_branch)))
5204+
/* BUG: The comparison of # equations in different branches below is wrong.
5205+
The Modelica.Blocks.Examples.PID_Controller shows why. if an assert is present in one of the branches, the number
5206+
does not match, but the "counting of equations" is still the same
5207+
Therfore I comment this out for now.
5208+
/PA
5209+
*/
5210+
5211+
/*case ((elt as DAE.IF_EQUATION(condition1 = cond,equations2 = true_branch,equations3 = false_branch)),onlyConstantEval)
52045212
equation
52055213
true_eq = ifEqToExpr2(true_branch);
52065214
false_eq = listLength(false_branch);
5207-
(true_eq == false_eq) = false;
5215+
(true_eq == false_eq) = false; // Bug here, must count the equations properly...
52085216
elt_str = dumpEquationsStr({elt});
52095217
Error.addMessage(Error.DIFFERENT_NO_EQUATION_IF_BRANCHES, {elt_str});
52105218
then
5211-
{};
5219+
{};*/
5220+
5221+
52125222
// handle the default case.
5213-
case (DAE.IF_EQUATION(condition1 = cond,equations2 = true_branch,equations3 = false_branch,source = source))
5223+
case (DAE.IF_EQUATION(condition1 = cond,equations2 = true_branch,equations3 = false_branch,source=source),onlyConstantEval as false)
52145224
equation
52155225
true_eq = ifEqToExpr2(true_branch);
52165226
false_eq = listLength(false_branch);
52175227
(true_eq == false_eq) = true;
52185228
equations = makeEquationsFromIf(cond, true_branch, false_branch, source);
52195229
then
52205230
equations;
5221-
case (elt as DAE.IF_EQUATION(condition1 = _)) // only display failure on if equation
5231+
case (elt as DAE.IF_EQUATION(condition1=_),onlyConstantEval as true)
5232+
5233+
then
5234+
{elt};
5235+
case (elt as DAE.IF_EQUATION(condition1=_),onlyConstantEval) // only display failure on if equation
52225236
equation
52235237
elt_str = dumpElementsStr({elt});
52245238
Debug.fprintln("failtrace", "- DAE.ifEqToExpr failed " +& elt_str);

Compiler/Exp.mo

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7261,9 +7261,11 @@ algorithm
72617261
list<Integer> cnt;
72627262
Absyn.Path path,p;
72637263
Boolean t;
7264-
Type tp;
7264+
Type tp,ety;
72657265
Absyn.CodeNode a;
72667266
Ident id;
7267+
ComponentRef cr;
7268+
list<Subscript> subs;
72677269

72687270
case (expr,source,target) /* expr source expr target expr */
72697271
equation
@@ -7401,6 +7403,18 @@ algorithm
74017403
c = c1 + c2;
74027404
then
74037405
(DAE.REDUCTION(p,e_1,id,r_1),c);
7406+
/* Qualified componentreferences, replace subscripts */
7407+
case(DAE.CREF(DAE.CREF_QUAL(id,tp,subs,cr),ety),source,target) equation
7408+
(subs,c1) = replaceExpSubs(subs,source,target);
7409+
(DAE.CREF(cr,_),c2) = replaceExp(DAE.CREF(cr,ety),source,target);
7410+
c = c1+c2;
7411+
then (DAE.CREF(DAE.CREF_QUAL(id,tp,subs,cr),ety),c);
7412+
7413+
/* simple componentreference, replace subscripts */
7414+
case(DAE.CREF(DAE.CREF_IDENT(id,tp,subs),ety),source,target) equation
7415+
(subs,c1) = replaceExpSubs(subs,source,target);
7416+
then (DAE.CREF(DAE.CREF_IDENT(id,tp,subs),ety),c1);
7417+
74047418
case(DAE.CREF(cr as DAE.CREF_IDENT(id,t2,ssl),ety),_,_)
74057419
local
74067420
Type ety,t2;
@@ -7419,6 +7433,40 @@ algorithm
74197433
end matchcontinue;
74207434
end replaceExp;
74217435

7436+
protected function replaceExpSubs
7437+
"function: replaceExpSubs
7438+
help function to replaceExp. replaces expressions in subscript list
7439+
"
7440+
input list<Subscript> subs;
7441+
input Exp source;
7442+
input Exp target;
7443+
output list<Subscript> outSubs;
7444+
output Integer cnt;
7445+
algorithm
7446+
(outSubs,cnt) := matchcontinue(subs,source,target)
7447+
local Exp e; Integer cnt1,cnt2;
7448+
case({},_,_) then ({},0);
7449+
/* WHOLEDIM == ':' */
7450+
case(DAE.WHOLEDIM()::subs,source,target) equation
7451+
(subs,cnt) = replaceExpSubs(subs,source,target);
7452+
then (DAE.WHOLEDIM()::subs,cnt);
7453+
7454+
/* Slice e.g. a[{1,5,7}] */
7455+
case(DAE.SLICE(e)::subs,source,target) equation
7456+
(e,cnt1) = replaceExp(e,source,target);
7457+
(subs,cnt2) = replaceExpSubs(subs,source,target);
7458+
cnt = cnt1 + cnt2;
7459+
then (DAE.SLICE(e)::subs,cnt);
7460+
7461+
/* Index, e.g. a[i+1] */
7462+
case(DAE.INDEX(e)::subs,source,target) equation
7463+
(e,cnt1) = replaceExp(e,source,target);
7464+
(subs,cnt2) = replaceExpSubs(subs,source,target);
7465+
cnt = cnt1 + cnt2;
7466+
then (DAE.INDEX(e)::subs,cnt);
7467+
end matchcontinue;
7468+
end replaceExpSubs;
7469+
74227470
protected function replaceExpMatrix
74237471
"function: replaceExpMatrix
74247472
author: PA

Compiler/Main.mo

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ algorithm
565565
Debug.fcall("daedumpdebug", DAEUtil.dumpDebug, d);
566566
Debug.fcall("daedumpgraphv", DAEUtil.dumpGraphviz, d);
567567
// transform if equations to if expression before going into code generation
568-
d = DAEUtil.transformIfEqToExpr(d);
568+
d = DAEUtil.transformIfEqToExpr(d,false);
569569
cname = Absyn.lastClassname(p);
570570
str = Print.getString();
571571
silent = RTOpts.silent();
@@ -638,7 +638,7 @@ algorithm
638638
Debug.fcall("daedumpdebug", DAEUtil.dumpDebug, d);
639639
Debug.fcall("daedumpgraphv", DAEUtil.dumpGraphviz, d);
640640
// transform if equations to if expression before going into code generation
641-
d = DAEUtil.transformIfEqToExpr(d);
641+
d = DAEUtil.transformIfEqToExpr(d,false);
642642
cname = Absyn.lastClassname(p);
643643
str = Print.getString();
644644
silent = RTOpts.silent();

Compiler/SimCode.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ algorithm
665665
p_1 = SCodeUtil.translateAbsyn2SCode(ptot);
666666
(cache,env,_,dae as DAE.DAE(dael)) =
667667
Inst.instantiateClass(cache,InstanceHierarchy.emptyInstanceHierarchy,p_1,className);
668-
((dae as DAE.DAE(dael))) = DAEUtil.transformIfEqToExpr(dae);
668+
((dae as DAE.DAE(dael))) = DAEUtil.transformIfEqToExpr(dae,false);
669669
ic_1 = Interactive.addInstantiatedClass(ic, Interactive.INSTCLASS(className,dael,env));
670670
dlow = DAELow.lower(dae, addDummy, true);
671671
Debug.fprint("bltdump", "Lowered DAE:\n");

0 commit comments

Comments
 (0)