From 285d2d5ec5e13a358ba36be27f6ee61ee89783c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Sj=C3=B6lund?= Date: Mon, 26 Mar 2018 15:02:21 +0200 Subject: [PATCH 1/2] [NF] Simplify Integer/Real/Boolean ranges --- Compiler/NFFrontEnd/NFSimplifyExp.mo | 55 +++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/Compiler/NFFrontEnd/NFSimplifyExp.mo b/Compiler/NFFrontEnd/NFSimplifyExp.mo index 126d945bfe..71a8067a5b 100644 --- a/Compiler/NFFrontEnd/NFSimplifyExp.mo +++ b/Compiler/NFFrontEnd/NFSimplifyExp.mo @@ -36,6 +36,13 @@ import Operator = NFOperator; import Type = NFType; import NFCall.Call; +protected + +import Dimension = NFDimension; +import ExpressionSimplify; + +public + function simplifyExp input output Expression exp; algorithm @@ -274,6 +281,9 @@ algorithm case Expression.IF(condition=Expression.BOOLEAN(value=b1)) then if b1 then exp.trueBranch else exp.falseBranch; + case Expression.RANGE() + then simplifyRange(exp.start, exp.step, exp.stop, exp.ty); + case Expression.CAST() then simplifyCast(exp.ty, exp.exp); @@ -300,11 +310,54 @@ algorithm else algorithm - Error.assertion(false, getInstanceName() + " failed on " + Expression.toString(exp), sourceInfo()); + Error.assertion(false, getInstanceName() + " failed on " + Expression.toString(exp) + " to type: " + Type.toString(ty), sourceInfo()); then fail(); end match; end simplifyCast; +function simplifyRange + input Expression start; + input Option optStep; + input Expression stop; + input Type ty; + output Expression exp; +protected + Expression step; + Integer i1, i2, i3; + Real r1, r2, r3; + Boolean b1, b2; + list exps; +algorithm + step := match (optStep, ty) + case (SOME(step),_) then step; + case (_,Type.ARRAY(elementType=Type.INTEGER())) then Expression.INTEGER(1); + case (_,Type.ARRAY(elementType=Type.REAL())) then Expression.REAL(1.0); + case (_,Type.ARRAY(elementType=Type.BOOLEAN())) then Expression.INTEGER(1); // dummy + else + algorithm + Error.assertion(false, getInstanceName() + " failed to type: " + Type.toString(ty), sourceInfo()); + then fail(); + end match; + exp := match (start, step, stop) + case (Expression.INTEGER(i1),Expression.INTEGER(i2),Expression.INTEGER(i3)) + algorithm + exps := list(Expression.INTEGER(i) for i in ExpressionSimplify.simplifyRange(i1, i2, i3)); + then Expression.ARRAY(Type.ARRAY(Type.INTEGER(), {Dimension.fromInteger(listLength(exps))}), exps); + case (Expression.REAL(r1),Expression.REAL(r2),Expression.REAL(r3)) + algorithm + exps := list(Expression.REAL(r) for r in ExpressionSimplify.simplifyRangeReal(r1, r2, r3)); + then Expression.ARRAY(Type.ARRAY(Type.REAL(), {Dimension.fromInteger(listLength(exps))}), exps); + case (Expression.BOOLEAN(b1),_,Expression.BOOLEAN(b2)) + algorithm + exps := list(Expression.BOOLEAN(b) for b in ExpressionSimplify.simplifyRangeBool(b1, b2)); + then Expression.ARRAY(Type.ARRAY(Type.BOOLEAN(), {Dimension.fromInteger(listLength(exps))}), exps); + else + algorithm + Error.assertion(false, getInstanceName() + " failed to type: " + Type.toString(ty), sourceInfo()); + then fail(); + end match; +end simplifyRange; + annotation(__OpenModelica_Interface="frontend"); end NFSimplifyExp; From 390415ac5d265a6b22fc0653f501af0da206368a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Sj=C3=B6lund?= Date: Wed, 11 Apr 2018 14:22:40 +0200 Subject: [PATCH 2/2] [NF] Simplify enumeration ranges --- Compiler/NFFrontEnd/NFSimplifyExp.mo | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Compiler/NFFrontEnd/NFSimplifyExp.mo b/Compiler/NFFrontEnd/NFSimplifyExp.mo index 71a8067a5b..245c839c57 100644 --- a/Compiler/NFFrontEnd/NFSimplifyExp.mo +++ b/Compiler/NFFrontEnd/NFSimplifyExp.mo @@ -328,12 +328,15 @@ protected Real r1, r2, r3; Boolean b1, b2; list exps; + list literals; + Type enumType; algorithm step := match (optStep, ty) case (SOME(step),_) then step; case (_,Type.ARRAY(elementType=Type.INTEGER())) then Expression.INTEGER(1); case (_,Type.ARRAY(elementType=Type.REAL())) then Expression.REAL(1.0); case (_,Type.ARRAY(elementType=Type.BOOLEAN())) then Expression.INTEGER(1); // dummy + case (_,Type.ARRAY(elementType=Type.ENUMERATION())) then Expression.INTEGER(1); // dummy else algorithm Error.assertion(false, getInstanceName() + " failed to type: " + Type.toString(ty), sourceInfo()); @@ -352,6 +355,10 @@ algorithm algorithm exps := list(Expression.BOOLEAN(b) for b in ExpressionSimplify.simplifyRangeBool(b1, b2)); then Expression.ARRAY(Type.ARRAY(Type.BOOLEAN(), {Dimension.fromInteger(listLength(exps))}), exps); + case (Expression.ENUM_LITERAL(ty=enumType as Type.ENUMERATION(literals=literals), index=i1),_,Expression.ENUM_LITERAL(index=i2)) + algorithm + exps := list(Expression.ENUM_LITERAL(enumType, listGet(literals, i), i) for i in i1:i2); + then Expression.ARRAY(Type.ARRAY(enumType, {Dimension.fromInteger(listLength(exps))}), exps); else algorithm Error.assertion(false, getInstanceName() + " failed to type: " + Type.toString(ty), sourceInfo());