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

Commit cce54d9

Browse files
perostOpenModelica-Hudson
authored andcommitted
[NF] Tuple and array equation improvements.
- Simplify `cat` calls. - Simplify "tuple subscripted" arrays by making each array element a tuple subscript. - Move the check whether an array equation should be scalarized or not from the typing to the scalarization, so it's done after simplification (which might affect the decision). - Evaluate "tuple subscripted" expressions in Ceval. Belonging to [master]: - #2532 - OpenModelica/OpenModelica-testsuite#983
1 parent 37e7177 commit cce54d9

File tree

8 files changed

+77
-36
lines changed

8 files changed

+77
-36
lines changed

Compiler/NFFrontEnd/NFCeval.mo

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,12 @@ algorithm
160160
if EvalTarget.isRange(target) then
161161
Expression.RANGE(exp.ty, exp1, oexp, exp3) else evalRange(exp1, oexp, exp3);
162162

163+
case Expression.TUPLE()
164+
algorithm
165+
exp.elements := list(evalExp(e, target) for e in exp.elements);
166+
then
167+
exp;
168+
163169
case Expression.RECORD()
164170
algorithm
165171
exp.elements := list(evalExp(e, target) for e in exp.elements);
@@ -224,15 +230,21 @@ algorithm
224230
exp1 := evalExp(exp.exp, target);
225231
then Expression.UNBOX(exp1, exp.ty);
226232

233+
case Expression.SUBSCRIPTED_EXP()
234+
then evalSubscriptedExp(exp.exp, exp.subscripts, target);
235+
236+
case Expression.TUPLE_ELEMENT()
237+
algorithm
238+
exp1 := evalExp(exp.tupleExp, target);
239+
then
240+
Expression.tupleElement(exp1, exp.ty, exp.index);
241+
227242
case Expression.MUTABLE()
228243
algorithm
229244
exp1 := evalExp(Mutable.access(exp.exp), target);
230245
then
231246
exp1;
232247

233-
case Expression.SUBSCRIPTED_EXP()
234-
then evalSubscriptedExp(exp.exp, exp.subscripts, target);
235-
236248
else exp;
237249
end match;
238250
end evalExp;

Compiler/NFFrontEnd/NFExpandExp.mo

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ public
107107
end match;
108108
end expandCref;
109109

110-
protected
111110
function expandCref2
112111
input ComponentRef cref;
113112
input output list<list<list<Subscript>>> subs = {};

Compiler/NFFrontEnd/NFExpression.mo

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3333,5 +3333,28 @@ public
33333333
end match;
33343334
end toScalar;
33353335

3336+
function tupleElement
3337+
input Expression exp;
3338+
input Type ty;
3339+
input Integer index;
3340+
output Expression tupleElem;
3341+
algorithm
3342+
tupleElem := match exp
3343+
local
3344+
Type ety;
3345+
3346+
case Expression.TUPLE() then listGet(exp.elements, index);
3347+
3348+
case Expression.ARRAY()
3349+
algorithm
3350+
ety := Type.unliftArray(ty);
3351+
exp.elements := list(tupleElement(e, ety, index) for e in exp.elements);
3352+
then
3353+
exp;
3354+
3355+
else Expression.TUPLE_ELEMENT(exp, index, ty);
3356+
end match;
3357+
end tupleElement;
3358+
33363359
annotation(__OpenModelica_Interface="frontend");
33373360
end NFExpression;

Compiler/NFFrontEnd/NFScalarize.mo

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -193,22 +193,26 @@ algorithm
193193
SourceInfo info;
194194
list<Equation> eql;
195195

196-
case Equation.EQUALITY(ty = ty, source = src) guard Type.isArray(ty)
196+
case Equation.EQUALITY(lhs = lhs, rhs = rhs, ty = ty, source = src) guard Type.isArray(ty)
197197
algorithm
198-
lhs_iter := ExpressionIterator.fromExp(eq.lhs);
199-
rhs_iter := ExpressionIterator.fromExp(eq.rhs);
200-
ty := Type.arrayElementType(ty);
201-
202-
while ExpressionIterator.hasNext(lhs_iter) loop
203-
if not ExpressionIterator.hasNext(rhs_iter) then
204-
Error.addInternalError(getInstanceName() + " could not expand rhs " +
205-
Expression.toString(eq.rhs), ElementSource.getInfo(src));
206-
end if;
207-
208-
(lhs_iter, lhs) := ExpressionIterator.next(lhs_iter);
209-
(rhs_iter, rhs) := ExpressionIterator.next(rhs_iter);
210-
equations := Equation.EQUALITY(lhs, rhs, ty, src) :: equations;
211-
end while;
198+
if Expression.hasArrayCall(lhs) or Expression.hasArrayCall(rhs) then
199+
equations := Equation.ARRAY_EQUALITY(lhs, rhs, ty, src) :: equations;
200+
else
201+
lhs_iter := ExpressionIterator.fromExp(lhs);
202+
rhs_iter := ExpressionIterator.fromExp(rhs);
203+
ty := Type.arrayElementType(ty);
204+
205+
while ExpressionIterator.hasNext(lhs_iter) loop
206+
if not ExpressionIterator.hasNext(rhs_iter) then
207+
Error.addInternalError(getInstanceName() + " could not expand rhs " +
208+
Expression.toString(eq.rhs), ElementSource.getInfo(src));
209+
end if;
210+
211+
(lhs_iter, lhs) := ExpressionIterator.next(lhs_iter);
212+
(rhs_iter, rhs) := ExpressionIterator.next(rhs_iter);
213+
equations := Equation.EQUALITY(lhs, rhs, ty, src) :: equations;
214+
end while;
215+
end if;
212216
then
213217
equations;
214218

Compiler/NFFrontEnd/NFSimplifyExp.mo

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import Ceval = NFCeval;
4444
import NFCeval.EvalTarget;
4545
import NFFunction.Function;
4646
import ComponentRef = NFComponentRef;
47+
import ExpandExp = NFExpandExp;
4748

4849
public
4950

@@ -121,7 +122,7 @@ algorithm
121122
if builtin and not Function.isImpure(call.fn) and List.all(args, Expression.isLiteral) then
122123
callExp := Ceval.evalCall(call, EvalTarget.IGNORE_ERRORS());
123124
else
124-
callExp := Expression.CALL(call);
125+
callExp := simplifyBuiltinCall(Function.nameConsiderBuiltin(call.fn), args, call);
125126
end if;
126127
then
127128
callExp;
@@ -137,6 +138,18 @@ algorithm
137138
end match;
138139
end simplifyCall;
139140

141+
function simplifyBuiltinCall
142+
input Absyn.Path name;
143+
input list<Expression> args;
144+
input Call call;
145+
output Expression exp;
146+
algorithm
147+
exp := match Absyn.pathFirstIdent(name)
148+
case "cat" then ExpandExp.expandBuiltinCat(args);
149+
else Expression.CALL(call);
150+
end match;
151+
end simplifyBuiltinCall;
152+
140153
function simplifySize
141154
input output Expression sizeExp;
142155
algorithm
@@ -319,11 +332,7 @@ protected
319332
algorithm
320333
Expression.TUPLE_ELEMENT(e, index, ty) := tupleExp;
321334
e := simplify(e);
322-
323-
tupleExp := match e
324-
case Expression.TUPLE() then listGet(e.elements, index);
325-
else Expression.TUPLE_ELEMENT(e, index, ty);
326-
end match;
335+
tupleExp := Expression.tupleElement(e, ty, index);
327336
end simplifyTupleElement;
328337

329338
annotation(__OpenModelica_Interface="frontend");

Compiler/NFFrontEnd/NFType.mo

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ public
436436
algorithm
437437
outTy := match ty
438438
case TUPLE() then listHead(ty.types);
439+
case ARRAY() then Type.ARRAY(firstTupleType(ty.elementType), ty.dimensions);
439440
else ty;
440441
end match;
441442
end firstTupleType;

Compiler/NFFrontEnd/NFTypeCheck.mo

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1967,7 +1967,8 @@ algorithm
19671967
if isCompatibleMatch(matchKind) then
19681968
expression := match expression
19691969
case Expression.TUPLE() then listHead(expression.elements);
1970-
else Expression.TUPLE_ELEMENT(expression, 1, compatibleType);
1970+
else Expression.TUPLE_ELEMENT(expression, 1,
1971+
Type.setArrayElementType(Expression.typeOf(expression), compatibleType));
19711972
end match;
19721973

19731974
matchKind := MatchKind.CAST;

Compiler/NFFrontEnd/NFTyping.mo

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,7 +1033,7 @@ algorithm
10331033
// equation/algorithm, select the first output.
10341034
if Type.isTuple(ty) and not ExpOrigin.isSingleExpression(origin) then
10351035
ty := Type.firstTupleType(ty);
1036-
e1 := Expression.TUPLE_ELEMENT(e1, 1, ty);
1036+
e1 := Expression.tupleElement(e1, ty, 1);
10371037
end if;
10381038
then
10391039
(e1, ty, var1);
@@ -2188,7 +2188,6 @@ algorithm
21882188
MatchKind mk;
21892189
Variability var, bvar;
21902190
Integer next_origin;
2191-
Equation tyeq;
21922191
SourceInfo info;
21932192

21942193
case Equation.EQUALITY()
@@ -2204,15 +2203,8 @@ algorithm
22042203
Type.toString(ty1) + " = " + Type.toString(ty2)}, info);
22052204
fail();
22062205
end if;
2207-
2208-
// Array equations containing function calls should not be scalarized.
2209-
if Type.isArray(ty) and (Expression.hasArrayCall(e1) or Expression.hasArrayCall(e2)) then
2210-
tyeq := Equation.ARRAY_EQUALITY(e1, e2, ty, eq.source);
2211-
else
2212-
tyeq := Equation.EQUALITY(e1, e2, ty, eq.source);
2213-
end if;
22142206
then
2215-
tyeq;
2207+
Equation.EQUALITY(e1, e2, ty, eq.source);
22162208

22172209
case Equation.CONNECT()
22182210
then typeConnect(eq.lhs, eq.rhs, origin, eq.source);

0 commit comments

Comments
 (0)