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

Commit c68f433

Browse files
perostOpenModelica-Hudson
authored andcommitted
[NF] Ceval/SimplifyExp improvements.
- When building record bindings, only evaluate structural parameter fields and not the whole generated expression (which might contain non-constant expressions). - Added simplification of and/or expressions where only one side is true/false (for example 'true or e => true'). Belonging to [master]: - #2861
1 parent ef2fed9 commit c68f433

File tree

2 files changed

+76
-7
lines changed

2 files changed

+76
-7
lines changed

Compiler/NFFrontEnd/NFCeval.mo

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,7 @@ protected
605605
Type ty;
606606
InstNode c;
607607
ComponentRef cr;
608+
Expression field_exp;
608609
algorithm
609610
tree := Class.classTree(InstNode.getClass(typeNode));
610611
comps := ClassTree.getComponents(tree);
@@ -615,13 +616,18 @@ algorithm
615616
c := comps[i];
616617
ty := InstNode.getType(c);
617618
cr := ComponentRef.CREF(c, {}, ty, NFComponentRef.Origin.CREF, cref);
618-
fields := Expression.CREF(ty, cr) :: fields;
619+
field_exp := Expression.CREF(ty, cr);
620+
621+
if Component.variability(InstNode.component(c)) <= Variability.STRUCTURAL_PARAMETER then
622+
field_exp := evalExp(field_exp);
623+
end if;
624+
625+
fields := field_exp :: fields;
619626
field_names := InstNode.name(c) :: field_names;
620627
end for;
621628

622629
ty := Type.setRecordFields(field_names, recordType);
623630
exp := Expression.RECORD(InstNode.scopePath(recordNode), ty, fields);
624-
exp := evalExp(exp);
625631
end makeRecordBindingExp;
626632

627633
function splitRecordArrayExp

Compiler/NFFrontEnd/NFSimplifyExp.mo

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -536,13 +536,76 @@ algorithm
536536
se1 := simplify(e1);
537537
se2 := simplify(e2);
538538

539-
if Expression.isLiteral(se1) and Expression.isLiteral(se2) then
540-
binaryExp := Ceval.evalLogicBinaryOp(se1, op, se2, EvalTarget.IGNORE_ERRORS());
541-
elseif not (referenceEq(e1, se1) and referenceEq(e2, se2)) then
542-
binaryExp := Expression.LBINARY(se1, op, se2);
543-
end if;
539+
binaryExp := match op.op
540+
case Op.AND then simplifyLogicBinaryAnd(se1, op, se2);
541+
case Op.OR then simplifyLogicBinaryOr(se1, op, se2);
542+
end match;
544543
end simplifyLogicBinary;
545544

545+
function simplifyLogicBinaryAnd
546+
input Expression exp1;
547+
input Operator op;
548+
input Expression exp2;
549+
output Expression exp;
550+
algorithm
551+
exp := match (exp1, exp2)
552+
local
553+
list<Expression> expl;
554+
Operator o;
555+
556+
// false and e => false
557+
case (Expression.BOOLEAN(false), _) then exp1;
558+
// e and false => false
559+
case (_, Expression.BOOLEAN(false)) then exp2;
560+
// true and e => e
561+
case (Expression.BOOLEAN(true), _) then exp2;
562+
// e and true => e
563+
case (_, Expression.BOOLEAN(true)) then exp1;
564+
565+
case (Expression.ARRAY(), Expression.ARRAY())
566+
algorithm
567+
o := Operator.unlift(op);
568+
expl := list(simplifyLogicBinaryAnd(e1, o, e2)
569+
threaded for e1 in exp1.elements, e2 in exp2.elements);
570+
then
571+
Expression.makeArray(Operator.typeOf(op), expl);
572+
573+
else Expression.LBINARY(exp1, op, exp2);
574+
end match;
575+
end simplifyLogicBinaryAnd;
576+
577+
function simplifyLogicBinaryOr
578+
input Expression exp1;
579+
input Operator op;
580+
input Expression exp2;
581+
output Expression exp;
582+
algorithm
583+
exp := match (exp1, exp2)
584+
local
585+
list<Expression> expl;
586+
Operator o;
587+
588+
// true or e => true
589+
case (Expression.BOOLEAN(true), _) then exp1;
590+
// e or true => true
591+
case (_, Expression.BOOLEAN(true)) then exp2;
592+
// false or e => e
593+
case (Expression.BOOLEAN(false), _) then exp2;
594+
// e or false => e
595+
case (_, Expression.BOOLEAN(false)) then exp1;
596+
597+
case (Expression.ARRAY(), Expression.ARRAY())
598+
algorithm
599+
o := Operator.unlift(op);
600+
expl := list(simplifyLogicBinaryAnd(e1, o, e2)
601+
threaded for e1 in exp1.elements, e2 in exp2.elements);
602+
then
603+
Expression.makeArray(Operator.typeOf(op), expl);
604+
605+
else Expression.LBINARY(exp1, op, exp2);
606+
end match;
607+
end simplifyLogicBinaryOr;
608+
546609
function simplifyLogicUnary
547610
input output Expression unaryExp;
548611
protected

0 commit comments

Comments
 (0)