Skip to content

Commit 446f0e8

Browse files
author
Jens Frenkel
committed
- Expression: check type before add,sub,mul,div expression
- ExpressionSolve.mo: cases for cr = exp, exp=cr, -cr=exp,exp=-cr,!cr=exp,exp=!cr - ExpressionSimplify: pre(a+b) -> pre(a)+pre(b) - move some test to working group git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@13597 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent 18167cd commit 446f0e8

File tree

3 files changed

+92
-1
lines changed

3 files changed

+92
-1
lines changed

Compiler/BackEnd/ExpressionSolve.mo

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ algorithm
264264
(outExp,outAsserts) := matchcontinue (inExp1,inExp2,inExp3,linearExps)
265265
local
266266
DAE.Exp lhs,lhsder,lhsder_1,lhszero,lhszero_1,rhs,rhs_1,e1,e2,crexp,e,a,z;
267-
DAE.ComponentRef cr;
267+
DAE.ComponentRef cr,cr1;
268268
DAE.Exp invCr;
269269
list<DAE.Exp> factors;
270270
Boolean linExp;
@@ -337,6 +337,67 @@ algorithm
337337
then
338338
(rhs_1,DAE.STMT_ASSERT(DAE.RELATION(a,DAE.NEQUAL(tp),z,-1,NONE()),DAE.SCONST(estr),DAE.ASSERTIONLEVEL_ERROR,DAE.emptyElementSource)::asserts);
339339

340+
// cr = exp
341+
case (DAE.CREF(componentRef = cr1),e2,(crexp as DAE.CREF(componentRef = cr)),linExp)
342+
equation
343+
true = ComponentReference.crefEqual(cr1,cr);
344+
// cr not in e2
345+
false = Expression.expHasCref(e2,cr);
346+
then
347+
(e2,{});
348+
349+
// exp = cr
350+
case (e1,DAE.CREF(componentRef = cr1),(crexp as DAE.CREF(componentRef = cr)),linExp)
351+
equation
352+
true = ComponentReference.crefEqual(cr1,cr);
353+
// cr not in e2
354+
false = Expression.expHasCref(e1,cr);
355+
then
356+
(e1,{});
357+
358+
// -cr = exp
359+
case (DAE.UNARY(operator = DAE.UMINUS(ty=_), exp = DAE.CREF(componentRef = cr1)),e2,(crexp as DAE.CREF(componentRef = cr)),linExp)
360+
equation
361+
true = ComponentReference.crefEqual(cr1,cr);
362+
// cr not in e2
363+
false = Expression.expHasCref(e2,cr);
364+
then
365+
(Expression.negate(e2),{});
366+
case (DAE.UNARY(operator = DAE.UMINUS_ARR(ty=_), exp = DAE.CREF(componentRef = cr1)),e2,(crexp as DAE.CREF(componentRef = cr)),linExp)
367+
equation
368+
true = ComponentReference.crefEqual(cr1,cr);
369+
// cr not in e2
370+
false = Expression.expHasCref(e2,cr);
371+
then
372+
(Expression.negate(e2),{});
373+
374+
// exp = -cr
375+
case (e1,DAE.LUNARY(operator = DAE.UMINUS_ARR(ty=_), exp = DAE.CREF(componentRef = cr1)),(crexp as DAE.CREF(componentRef = cr)),linExp)
376+
equation
377+
true = ComponentReference.crefEqual(cr1,cr);
378+
// cr not in e2
379+
false = Expression.expHasCref(e1,cr);
380+
then
381+
(Expression.negate(e1),{});
382+
383+
// !cr = exp
384+
case (DAE.LUNARY(operator = DAE.NOT(ty=_), exp = DAE.CREF(componentRef = cr1)),e2,(crexp as DAE.CREF(componentRef = cr)),linExp)
385+
equation
386+
true = ComponentReference.crefEqual(cr1,cr);
387+
// cr not in e2
388+
false = Expression.expHasCref(e2,cr);
389+
then
390+
(Expression.negate(e2),{});
391+
392+
// exp = !cr
393+
case (e1,DAE.LUNARY(operator = DAE.NOT(ty=_), exp = DAE.CREF(componentRef = cr1)),(crexp as DAE.CREF(componentRef = cr)),linExp)
394+
equation
395+
true = ComponentReference.crefEqual(cr1,cr);
396+
// cr not in e2
397+
false = Expression.expHasCref(e1,cr);
398+
then
399+
(Expression.negate(e1),{});
400+
340401
case (e1,e2,(crexp as DAE.CREF(componentRef = cr)), linExp)
341402
equation
342403
lhs = Expression.makeDiff(e1,e2);

Compiler/FrontEnd/Expression.mo

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2698,6 +2698,7 @@ algorithm
26982698
case (_,_)
26992699
equation
27002700
tp = typeof(e1);
2701+
true = Types.isIntegerOrRealOrSubTypeOfEither(tp);
27012702
b = DAEUtil.expTypeArray(tp) " array_elt_type(tp) => tp\'" ;
27022703
op = Util.if_(b,DAE.ADD_ARR(tp),DAE.ADD(tp));
27032704
then
@@ -2762,6 +2763,7 @@ algorithm
27622763
case (_,_)
27632764
equation
27642765
tp = typeof(e1);
2766+
true = Types.isIntegerOrRealOrSubTypeOfEither(tp);
27652767
b = DAEUtil.expTypeArray(tp);
27662768
op = Util.if_(b,DAE.SUB_ARR(tp),DAE.SUB(tp));
27672769
then
@@ -2963,8 +2965,10 @@ algorithm
29632965
case (_,_)
29642966
equation
29652967
tp = typeof(e1);
2968+
true = Types.isIntegerOrRealOrSubTypeOfEither(tp);
29662969
b1 = DAEUtil.expTypeArray(tp);
29672970
tp = typeof(e2);
2971+
true = Types.isIntegerOrRealOrSubTypeOfEither(tp);
29682972
b2 = DAEUtil.expTypeArray(tp);
29692973
/* swap e1 and e2 if we have scalar mul array */
29702974
(e1_1,e2_1) = Util.swap((not b1) and b2, e1, e2);
@@ -3136,6 +3140,7 @@ protected
31363140
Operator op;
31373141
algorithm
31383142
tp := typeof(e1);
3143+
true := Types.isIntegerOrRealOrSubTypeOfEither(tp);
31393144
b := DAEUtil.expTypeArray(tp);
31403145
op := Util.if_(b,DAE.DIV_ARR(tp),DAE.DIV(tp));
31413146
outExp := DAE.BINARY(e1,op,e2);

Compiler/FrontEnd/ExpressionSimplify.mo

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,12 +229,23 @@ algorithm
229229
// TODO: Do this better?
230230
then
231231
((e3,(b,options)));
232+
233+
case ((e as DAE.CALL(path=Absyn.IDENT("pre"), expLst={DAE.CREF(componentRef=_)}), (b, options)))
234+
then
235+
((e,(b,options)));
232236

233237
case ((DAE.CALL(path=Absyn.IDENT("pre"), expLst={e as DAE.ASUB(exp = exp)}), (b, options)))
234238
equation
235239
true = Expression.isConst(exp);
236240
then
237241
((e,(true,options)));
242+
243+
// move pre inside
244+
case ((DAE.CALL(path=Absyn.IDENT("pre"), expLst={e}), (b, options)))
245+
equation
246+
((e,_)) = Expression.traverseExp(e,preCref,false);
247+
then
248+
((e,(true,options)));
238249

239250
// normal call
240251
case ((e as DAE.CALL(expLst=expl),(_,options)))
@@ -385,6 +396,20 @@ algorithm
385396
end matchcontinue;
386397
end simplifyWork;
387398

399+
protected function preCref
400+
input tuple<DAE.Exp,Boolean> iExp;
401+
output tuple<DAE.Exp,Boolean> oExp;
402+
algorithm
403+
oExp := match(iExp)
404+
local
405+
DAE.Exp e;
406+
Boolean b;
407+
DAE.Type ty;
408+
case ((e as DAE.CREF(ty=ty),_)) then ((DAE.CALL(Absyn.IDENT("pre"),{e},DAE.CALL_ATTR(ty,false,true,DAE.NO_INLINE(),DAE.NO_TAIL())),true));
409+
else then iExp;
410+
end match;
411+
end preCref;
412+
388413
public function simplify1
389414
"function: simplify1
390415
This function does some very basic simplification

0 commit comments

Comments
 (0)