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

Commit

Permalink
[NF] Various fixes.
Browse files Browse the repository at this point in the history
- Simplify variables too in SimplifyModel.
- Implemented simplification of size.
- Evaluate subscripts when evaluating component references.
- Fix origin in Call.typeMapIteratorCall.

Belonging to [master]:
  - #2450
  - OpenModelica/OpenModelica-testsuite#953
  • Loading branch information
perost authored and OpenModelica-Hudson committed May 22, 2018
1 parent 0c48541 commit 6b41403
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Compiler/NFFrontEnd/NFCall.mo
Expand Up @@ -510,7 +510,7 @@ uniontype Call

for i in call.iters loop
(iter, range) := i;
(range, iter_ty, iter_var) := Typing.typeIterator(iter, range, ExpOrigin.FUNCTION, structural = false);
(range, iter_ty, iter_var) := Typing.typeIterator(iter, range, origin, structural = false);
dims := listAppend(Type.arrayDims(iter_ty), dims);
variability := Variability.variabilityMax(variability, iter_var);
iters := (iter, range) :: iters;
Expand Down
17 changes: 15 additions & 2 deletions Compiler/NFFrontEnd/NFCeval.mo
Expand Up @@ -48,6 +48,7 @@ import ExpressionSimplify;
import NFPrefixes.Variability;
import NFClassTree.ClassTree;
import ComplexType = NFComplexType;
import Subscript = NFSubscript;

protected
import NFFunction.Function;
Expand Down Expand Up @@ -143,7 +144,7 @@ algorithm
algorithm
exp1 := evalComponentBinding(c, exp, target);
then
Expression.applySubscripts(cref.subscripts, exp1);
Expression.applySubscripts(list(evalSubscript(s, target) for s in cref.subscripts), exp1);

case Expression.TYPENAME()
then evalTypename(exp.ty, exp, target);
Expand Down Expand Up @@ -180,7 +181,7 @@ algorithm

case Expression.SIZE()
algorithm
expl := list(Expression.INTEGER(Dimension.size(d)) for d in Type.arrayDims(Expression.typeOf(exp.exp)));
expl := list(Dimension.sizeExp(d) for d in Type.arrayDims(Expression.typeOf(exp.exp)));
dim := Dimension.INTEGER(listLength(expl), Variability.PARAMETER);
then
Expression.ARRAY(Type.ARRAY(Type.INTEGER(), {dim}), expl);
Expand Down Expand Up @@ -2349,5 +2350,17 @@ algorithm
end for;
end evalSubscriptedExp;

function evalSubscript
input Subscript subscript;
input EvalTarget target;
output Subscript outSubscript;
algorithm
outSubscript := match subscript
case Subscript.INDEX() then Subscript.INDEX(evalExp(subscript.index, target));
case Subscript.SLICE() then Subscript.SLICE(evalExp(subscript.slice, target));
else subscript;
end match;
end evalSubscript;

annotation(__OpenModelica_Interface="frontend");
end NFCeval;
44 changes: 44 additions & 0 deletions Compiler/NFFrontEnd/NFSimplifyExp.mo
Expand Up @@ -70,6 +70,7 @@ algorithm
exp;

case Expression.CALL() then simplifyCall(exp);
case Expression.SIZE() then simplifySize(exp);
case Expression.BINARY() then simplifyBinary(exp);
case Expression.UNARY() then simplifyUnary(exp);
case Expression.LBINARY() then simplifyLogicBinary(exp);
Expand Down Expand Up @@ -122,6 +123,49 @@ algorithm
end match;
end simplifyCall;

function simplifySize
input output Expression sizeExp;
algorithm
sizeExp := match sizeExp
local
Expression exp, index;
Dimension dim;
list<Dimension> dims;

case Expression.SIZE(exp, dimIndex = SOME(index))
algorithm
index := simplify(index);

if Expression.isLiteral(index) then
dim := listGet(Type.arrayDims(Expression.typeOf(exp)), Expression.toInteger(index));

if Dimension.isKnown(dim) then
exp := Expression.INTEGER(Dimension.size(dim));
else
exp := Expression.SIZE(exp, SOME(index));
end if;
else
exp := Expression.SIZE(exp, SOME(index));
end if;
then
exp;

case Expression.SIZE()
algorithm
dims := Type.arrayDims(Expression.typeOf(sizeExp.exp));

if List.all(dims, function Dimension.isKnown(allowExp = true)) then
exp := Expression.ARRAY(Type.ARRAY(Type.INTEGER(), {Dimension.fromInteger(listLength(dims))}),
list(Dimension.sizeExp(d) for d in dims));
else
exp := sizeExp;
end if;
then
exp;

end match;
end simplifySize;

function simplifyBinary
input output Expression binaryExp;
protected
Expand Down
39 changes: 39 additions & 0 deletions Compiler/NFFrontEnd/NFSimplifyModel.mo
Expand Up @@ -42,6 +42,8 @@ import NFClass.Class;
import NFInstNode.InstNode;
import NFFunction.Function;
import Sections = NFSections;
import NFBinding.Binding;
import Variable = NFVariable;

protected
import MetaModelica.Dangerous.*;
Expand All @@ -53,6 +55,7 @@ function simplify
input output FlatModel flatModel;
input output FunctionTree functions;
algorithm
flatModel.variables := list(simplifyVariable(v) for v in flatModel.variables);
flatModel.equations := simplifyEquations(flatModel.equations);
flatModel.initialEquations := simplifyEquations(flatModel.initialEquations);

Expand All @@ -61,6 +64,42 @@ algorithm
execStat(getInstanceName());
end simplify;

function simplifyVariable
input output Variable var;
algorithm
var.binding := simplifyBinding(var.binding);
var.typeAttributes := list(simplifyTypeAttribute(a) for a in var.typeAttributes);
end simplifyVariable;

function simplifyBinding
input output Binding binding;
protected
Expression exp, sexp;
algorithm
if Binding.isBound(binding) then
exp := Binding.getTypedExp(binding);
sexp := SimplifyExp.simplify(exp);

if not referenceEq(exp, sexp) then
binding := Binding.setTypedExp(sexp, binding);
end if;
end if;
end simplifyBinding;

function simplifyTypeAttribute
input output tuple<String, Binding> attribute;
protected
String name;
Binding binding, sbinding;
algorithm
(name, binding) := attribute;
sbinding := simplifyBinding(binding);

if not referenceEq(binding, sbinding) then
attribute := (name, sbinding);
end if;
end simplifyTypeAttribute;

function simplifyEquations
input list<Equation> eql;
output list<Equation> outEql = {};
Expand Down

0 comments on commit 6b41403

Please sign in to comment.