Skip to content

Commit

Permalink
NFInst improvements.
Browse files Browse the repository at this point in the history
- Improved flattening of bindings, equations and statements.
- Changed flattening of bindings to generate array equations
  when applicable like the old instantiation.
- Added prefixing of statements during flattening.
- Improved typing of component references.
- Implemented basic support for tuples.
- Added constant evaluation of size expressions.
  • Loading branch information
perost authored and OpenModelica-Hudson committed Sep 20, 2017
1 parent 0c97493 commit e59152f
Show file tree
Hide file tree
Showing 17 changed files with 1,076 additions and 150 deletions.
12 changes: 11 additions & 1 deletion Compiler/NFFrontEnd/NFCall.mo
Expand Up @@ -355,7 +355,17 @@ uniontype Call

end matchFunctions;

protected
function typeOf
input Call call;
output Type ty;
algorithm
ty := match call
case TYPED_CALL(attributes = CallAttributes.CALL_ATTR(ty = ty)) then ty;
else Type.UNKNOWN();
end match;
end typeOf;

protected
function matchFunction
input Function func;
input list<TypedArg> args;
Expand Down
12 changes: 10 additions & 2 deletions Compiler/NFFrontEnd/NFCeval.mo
Expand Up @@ -87,6 +87,7 @@ algorithm
Component comp;
Option<Expression> oexp;
ComponentRef cref;
Dimension dim;

case Expression.CREF(cref = cref as ComponentRef.CREF(node = c as InstNode.COMPONENT_NODE()))
algorithm
Expand Down Expand Up @@ -129,10 +130,17 @@ algorithm
then
Expression.CALL(call);

case Expression.SIZE(dimIndex = SOME(exp1))
algorithm
dim := listGet(Type.arrayDims(Expression.typeOf(exp.exp)), Expression.toInteger(evalExp(exp1, target)));
then
Expression.INTEGER(Dimension.size(dim));

case Expression.SIZE()
algorithm
assert(false, "Unimplemented case for " + Expression.toString(exp) + " in " + getInstanceName());
then fail();
expl := list(Expression.INTEGER(Dimension.size(d)) for d in Type.arrayDims(Expression.typeOf(exp.exp)));
then
Expression.ARRAY(Type.ARRAY(Type.INTEGER(), {Dimension.INTEGER(listLength(expl))}), expl);

case Expression.BINARY()
algorithm
Expand Down
10 changes: 10 additions & 0 deletions Compiler/NFFrontEnd/NFComponent.mo
Expand Up @@ -378,6 +378,16 @@ uniontype Component
end match;
end isConst;

function isVar
input Component component;
output Boolean isConst;
algorithm
isConst := match variability(component)
case DAE.VarKind.VARIABLE() then true;
else false;
end match;
end isVar;

function visibility
input Component component;
output DAE.VarVisibility visibility;
Expand Down
77 changes: 76 additions & 1 deletion Compiler/NFFrontEnd/NFComponentRef.mo
Expand Up @@ -36,6 +36,9 @@ protected
import Subscript = NFSubscript;
import Type = NFType;
import NFInstNode.InstNode;
import RangeIterator = NFRangeIterator;
import Dimension = NFDimension;
import Expression = NFExpression;

import ComponentRef = NFComponentRef;

Expand Down Expand Up @@ -66,6 +69,13 @@ public
output ComponentRef cref = CREF(node, subs, ty, origin, EMPTY());
end fromNode;

function rest
input ComponentRef cref;
output ComponentRef restCref;
algorithm
CREF(restCref = restCref) := cref;
end rest;

function getType
input ComponentRef cref;
output Type ty;
Expand Down Expand Up @@ -121,6 +131,32 @@ public
end match;
end addSubscript;

function fillSubscripts
"This function takes a list of subscript lists and a cref, and adds the
first subscript list to the first part of the cref, the second list to the
second part, and so on. This function is meant to be used to fill in all
subscripts in a cref such that it becomes a scalar, so the type of each
cref part is set to the array element type for that part."
input list<list<Subscript>> subscripts;
input output ComponentRef cref;
algorithm
cref := match (subscripts, cref)
local
list<Subscript> subs;
list<list<Subscript>> rest_subs;
ComponentRef rest_cref;

case (subs :: rest_subs, CREF())
algorithm
rest_cref := fillSubscripts(rest_subs, cref.restCref);
then
CREF(cref.node, listAppend(cref.subscripts, subs),
Type.arrayElementType(cref.ty), cref.origin, rest_cref);

case ({}, _) then cref;
end match;
end fillSubscripts;

function setSubscripts
input list<Subscript> subscripts;
input output ComponentRef cref;
Expand Down Expand Up @@ -152,6 +188,7 @@ public
algorithm
cref := match (srcCref, dstCref)
case (EMPTY(), _) then dstCref;
case (_, EMPTY()) then dstCref;
case (_, CREF(origin = Origin.ITERATOR)) then dstCref;

case (CREF(), CREF(origin = Origin.CREF))
Expand All @@ -164,7 +201,7 @@ public
algorithm
cref := transferSubscripts(srcCref.restCref, dstCref.restCref);
then
CREF(dstCref.node, srcCref.subscripts, dstCref.ty, dstCref.origin, cref);
CREF(dstCref.node, srcCref.subscripts, srcCref.ty, dstCref.origin, cref);

else
algorithm
Expand Down Expand Up @@ -265,5 +302,43 @@ public
end for;
end fromNodeList;

function vectorize
input ComponentRef cref;
output list<ComponentRef> crefs;
algorithm
crefs := match cref
local
list<Dimension> dims;
RangeIterator iter;
Expression exp;
list<list<Subscript>> subs;
list<list<Subscript>> new_subs;
Subscript sub;

case CREF()
algorithm
dims := Type.arrayDims(cref.ty);
subs := {cref.subscripts};

for dim in listReverse(dims) loop
iter := RangeIterator.fromDim(dim);
new_subs := {};

while RangeIterator.hasNext(iter) loop
(iter, exp) := RangeIterator.next(iter);
sub := Subscript.INDEX(exp);
new_subs := listAppend(list(sub :: s for s in subs), new_subs);
end while;

subs := new_subs;
end for;

then
listReverse(setSubscripts(s, cref) for s in subs);

else {cref};
end match;
end vectorize;

annotation(__OpenModelica_Interface="frontend");
end NFComponentRef;
49 changes: 34 additions & 15 deletions Compiler/NFFrontEnd/NFDimension.mo
Expand Up @@ -97,6 +97,11 @@ public
end match;
end fromExp;

function fromExpList
input list<Expression> expl;
output Dimension dim = INTEGER(listLength(expl));
end fromExpList;

function toDAE
input Dimension dim;
output DAE.Dimension daeDim;
Expand Down Expand Up @@ -143,21 +148,21 @@ public
end isEqual;

public function allEqual
input list<Dimension> dims1;
input list<Dimension> dims2;
output Boolean allEqual;
algorithm
allEqual := match(dims1, dims2)
local
Dimension dim1, dim2;
list<Dimension> rest1, rest2;

case ({}, {}) then true;
case (dim1::rest1, dim2::rest2) guard isEqual(dim1, dim2)
then allEqual(rest1, rest2);
else false;
end match;
end allEqual;
input list<Dimension> dims1;
input list<Dimension> dims2;
output Boolean allEqual;
algorithm
allEqual := match(dims1, dims2)
local
Dimension dim1, dim2;
list<Dimension> rest1, rest2;

case ({}, {}) then true;
case (dim1::rest1, dim2::rest2) guard isEqual(dim1, dim2)
then allEqual(rest1, rest2);
else false;
end match;
end allEqual;

function isEqualKnown
input Dimension dim1;
Expand All @@ -168,10 +173,24 @@ end allEqual;
case (UNKNOWN(), _) then false;
case (_, UNKNOWN()) then false;
case (EXP(), EXP()) then Expression.isEqual(dim1.exp, dim2.exp);
case (EXP(), _) then false;
case (_, EXP()) then false;
else Dimension.size(dim1) == Dimension.size(dim2);
end match;
end isEqualKnown;

function isKnown
input Dimension dim;
output Boolean known;
algorithm
known := match dim
case INTEGER() then true;
case BOOLEAN() then true;
case ENUM() then true;
else false;
end match;
end isKnown;

function toString
input Dimension dim;
output String str;
Expand Down
7 changes: 7 additions & 0 deletions Compiler/NFFrontEnd/NFEquation.mo
Expand Up @@ -44,6 +44,13 @@ public uniontype Equation
SourceInfo info;
end EQUALITY;

record ARRAY_EQUALITY
Expression lhs;
Expression rhs;
Type ty;
SourceInfo info;
end ARRAY_EQUALITY;

record CONNECT
Expression lhs "The left hand side component.";
//NFConnect2.Face lhsFace "The face of the lhs component, inside or outside.";
Expand Down

0 comments on commit e59152f

Please sign in to comment.