Skip to content

Commit

Permalink
Implement simple branch selection in nfinst.
Browse files Browse the repository at this point in the history
  • Loading branch information
perost authored and OpenModelica-Hudson committed Nov 7, 2017
1 parent 67d63de commit 1e05283
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 21 deletions.
10 changes: 10 additions & 0 deletions Compiler/NFFrontEnd/NFExpression.mo
Expand Up @@ -181,6 +181,16 @@ public
end match;
end isTrue;

function isFalse
input Expression exp;
output Boolean isTrue;
algorithm
isTrue := match exp
case BOOLEAN(false) then true;
else false;
end match;
end isFalse;

function isEqual
"Returns true if the two expressions are equal, otherwise false."
input Expression exp1;
Expand Down
51 changes: 43 additions & 8 deletions Compiler/NFFrontEnd/NFScalarize.mo
Expand Up @@ -112,6 +112,17 @@ algorithm
end if;
end scalarizeComponent;

function scalarizeEquations
input list<Equation> eql;
output list<Equation> equations = {};
algorithm
for eq in eql loop
equations := scalarizeEquation(eq, equations);
end for;

equations := listReverseInPlace(equations);
end scalarizeEquations;

function scalarizeEquation
input Equation eq;
input output list<Equation> equations;
Expand Down Expand Up @@ -145,7 +156,7 @@ algorithm
Equation.ARRAY_EQUALITY(eq.lhs, rhs, eq.ty, eq.info) :: equations;

case Equation.IF()
then Equation.IF(list(scalarizeBranch(b) for b in eq.branches), eq.info) :: equations;
then scalarizeIfEquation(eq.branches, eq.info, equations);

case Equation.WHEN()
then Equation.WHEN(list(scalarizeBranch(b) for b in eq.branches), eq.info) :: equations;
Expand All @@ -154,19 +165,43 @@ algorithm
end match;
end scalarizeEquation;

function scalarizeIfEquation
input list<tuple<Expression, list<Equation>>> branches;
input SourceInfo info;
input output list<Equation> equations;
protected
list<tuple<Expression, list<Equation>>> bl = {};
Expression cond;
list<Equation> eql;
algorithm
for b in branches loop
(cond, eql) := b;
eql := scalarizeEquations(eql);

if Expression.isTrue(cond) and listEmpty(bl) then
// If the condition is literal true and we haven't collected any other
// branches yet, replace the if equation with this branch.
equations := listAppend(eql, equations);
return;
elseif not Expression.isFalse(cond) then
// Only add the branch to the list of branches if the condition is not
// literal false, otherwise just drop it since it will never trigger.
bl := (cond, eql) :: bl;
end if;
end for;

// Add the scalarized if equation to the list of equations if we got this far.
equations := Equation.IF(listReverseInPlace(bl), info) :: equations;
end scalarizeIfEquation;

function scalarizeBranch
input output tuple<Expression, list<Equation>> branch;
protected
Expression exp;
list<Equation> eql, scalar_eql = {};
list<Equation> eql;
algorithm
(exp, eql) := branch;

for eq in eql loop
scalar_eql := scalarizeEquation(eq, scalar_eql);
end for;

branch := (exp, listReverseInPlace(scalar_eql));
branch := (exp, scalarizeEquations(eql));
end scalarizeBranch;

annotation(__OpenModelica_Interface="frontend");
Expand Down
45 changes: 32 additions & 13 deletions Compiler/NFFrontEnd/NFTyping.mo
Expand Up @@ -76,7 +76,7 @@ import Types;
import NFSections.Sections;
import List;
import DAEUtil;
import MetaModelica.Dangerous;
import MetaModelica.Dangerous.listReverseInPlace;
import ComplexType = NFComplexType;
import Restriction = NFRestriction;

Expand Down Expand Up @@ -1370,6 +1370,7 @@ algorithm
list<tuple<Expression, list<Equation>>> tybrs;
InstNode iterator;
MatchKind mk;
Variability var, bvar;

case Equation.EQUALITY()
algorithm
Expand All @@ -1396,18 +1397,7 @@ algorithm
then
Equation.FOR(eq.iterator, body, eq.info);

case Equation.IF()
algorithm
tybrs := list(
match br case(cond, body)
algorithm
e1 := typeCondition(cond, eq.info, Error.IF_CONDITION_TYPE_ERROR);
eqs1 := list(typeEquation(beq) for beq in body);
then (e1, eqs1);
end match
for br in eq.branches);
then
Equation.IF(tybrs, eq.info);
case Equation.IF() then typeIfEquation(eq.branches, eq.info);

case Equation.WHEN()
algorithm
Expand Down Expand Up @@ -1679,6 +1669,35 @@ algorithm
end if;
end typeCondition;

function typeIfEquation
input list<tuple<Expression, list<Equation>>> branches;
input SourceInfo info;
output Equation ifEq;
protected
Expression cond;
list<Equation> eql;
Variability accum_var = Variability.CONSTANT, var;
list<tuple<Expression, list<Equation>>> bl = {};
algorithm
for b in branches loop
(cond, eql) := b;
(cond, var) := typeCondition(cond, info, Error.IF_CONDITION_TYPE_ERROR);
accum_var := Prefixes.variabilityMax(accum_var, var);

if var <= Variability.PARAMETER then
cond := Ceval.evalExp(cond, Ceval.EvalTarget.IGNORE_ERRORS());
cond := SimplifyExp.simplifyExp(cond);
end if;

eql := list(typeEquation(e) for e in eql);
bl := (cond, eql) :: bl;
end for;

// TODO: If accum_var <= PARAMETER, then each branch must have the same number
// of equations.
ifEq := Equation.IF(listReverseInPlace(bl), info);
end typeIfEquation;

function typeOperatorArg
input output Expression arg;
input Type expectedType;
Expand Down

0 comments on commit 1e05283

Please sign in to comment.