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

Commit

Permalink
NFInst improvements.
Browse files Browse the repository at this point in the history
- Added Expression.TYPENAME to handle typenames better.
- Use RangeIterator more to simplify scalarization.
- Improved handling of for loops.
- Improved iteration ranges.
- Cleaned up type checking:
  - Removed erroneous cases for Type.FUNCTION.
  - Changed MatchKind into an enumeration.
  - Removed boolean return value from type matching functions,
    not needed when we also return MatchKind.
  • Loading branch information
perost authored and OpenModelica-Hudson committed Mar 20, 2017
1 parent d74861a commit 6deba45
Show file tree
Hide file tree
Showing 12 changed files with 505 additions and 507 deletions.
13 changes: 13 additions & 0 deletions Compiler/NFFrontEnd/NFBinding.mo
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,19 @@ public
end match;
end typedExp;

function setTypedExp
input Expression exp;
input output Binding binding;
algorithm
() := match binding
case TYPED_BINDING()
algorithm
binding.bindingExp := exp;
then
();
end match;
end setTypedExp;

function variability
input Binding binding;
output DAE.Const var;
Expand Down
58 changes: 49 additions & 9 deletions Compiler/NFFrontEnd/NFCeval.mo
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ import NFInstNode.InstNode;
import Operator = NFOperator;
import Typing = NFTyping;
import NFCall.Call;
import Dimension = NFDimension;
import Type = NFType;

uniontype EvalTarget
record DIMENSION
Expand All @@ -54,6 +56,10 @@ uniontype EvalTarget
SourceInfo info;
end ATTRIBUTE;

record RANGE
SourceInfo info;
end RANGE;

record IGNORE_ERRORS end IGNORE_ERRORS;
end EvalTarget;

Expand All @@ -69,16 +75,17 @@ algorithm
list<Expression> expl = {};
Call call;
Component comp;
Option<Expression> oexp;

case Expression.CREF(cref=ComponentRef.CREF(node=c as InstNode.COMPONENT_NODE()))
case Expression.CREF(cref=ComponentRef.CREF(node = c as InstNode.COMPONENT_NODE()))
algorithm
Typing.typeComponentBinding(c, InstNode.parent(c));
binding := Component.getBinding(InstNode.component(c));
then
evalBinding(binding, exp, target);

case Expression.CREF(cref=ComponentRef.CREF(node=c as InstNode.CLASS_NODE()))
then evalTypename(c, exp, target);
case Expression.TYPENAME()
then evalTypename(exp.ty, exp, target);

case Expression.ARRAY()
algorithm
Expand All @@ -88,10 +95,16 @@ algorithm
end for;
then Expression.ARRAY(exp.ty, listReverse(expl));

// Ranges could be evaluated into arrays, but that's less efficient in some
// cases. So here we just evaluate the range's expressions, and let the
// caller worry about vectorization.
case Expression.RANGE()
algorithm
assert(false, "Unimplemented case for " + Expression.toString(exp) + " in " + getInstanceName());
then fail();
exp1 := evalExp(exp.start, target);
oexp := evalExpOpt(exp.step, target);
exp3 := evalExp(exp.stop, target);
then
Expression.RANGE(exp.ty, exp1, oexp, exp3);

case Expression.RECORD()
algorithm
Expand Down Expand Up @@ -158,6 +171,19 @@ algorithm
end match;
end evalExp;

function evalExpOpt
input output Option<Expression> oexp;
input EvalTarget target;
algorithm
oexp := match oexp
local
Expression e;

case SOME(e) then SOME(evalExp(e, target));
else oexp;
end match;
end evalExpOpt;

protected

function evalBinding
Expand Down Expand Up @@ -198,15 +224,29 @@ algorithm
end printUnboundError;

function evalTypename
input InstNode node;
input Type ty;
input Expression originExp;
input EvalTarget target;
output Expression exp;
protected
list<Expression> lits;
algorithm
exp := match target
case EvalTarget.DIMENSION() then originExp;
exp := match ty
case Type.ARRAY(elementType = Type.BOOLEAN())
then Expression.ARRAY(ty, {Expression.BOOLEAN(false), Expression.BOOLEAN(true)});

case Type.ARRAY(elementType = Type.ENUMERATION())
algorithm
lits := Expression.makeEnumLiterals(ty.elementType);
then
Expression.ARRAY(ty, lits);

else
algorithm
assert(false, getInstanceName() + " got invalid typename");
then
fail();

else originExp;
end match;
end evalTypename;

Expand Down
33 changes: 11 additions & 22 deletions Compiler/NFFrontEnd/NFDimension.mo
Original file line number Diff line number Diff line change
Expand Up @@ -74,32 +74,21 @@ public
local
Class cls;
ComponentRef cref;
Type ty;

case Expression.INTEGER() then INTEGER(exp.value);

case Expression.CREF(cref = cref as ComponentRef.CREF())
algorithm
if InstNode.isClass(cref.node) then
cls := InstNode.getClass(cref.node);

dim := match cls
case Class.PARTIAL_BUILTIN(ty = Type.BOOLEAN())
then BOOLEAN();

case Class.PARTIAL_BUILTIN(ty = Type.ENUMERATION())
then ENUM(cls.ty);

else
algorithm
assert(false, getInstanceName() + " got non-typename class");
then
fail();
end match;
else
dim := Dimension.EXP(exp);
end if;
case Expression.TYPENAME(ty = Type.ARRAY(elementType = ty))
then
dim;
match ty
case Type.BOOLEAN() then BOOLEAN();
case Type.ENUMERATION() then ENUM(ty);
else
algorithm
assert(false, getInstanceName() + " got invalid typename");
then
fail();
end match;

else Dimension.EXP(exp);
end match;
Expand Down
25 changes: 25 additions & 0 deletions Compiler/NFFrontEnd/NFExpression.mo
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ public
ComponentRef cref;
end CREF;

record TYPENAME "Represents a type used as a range, e.g. Boolean."
Type ty;
end TYPENAME;

record ARRAY
Type ty;
list<Expression> elements;
Expand Down Expand Up @@ -229,6 +233,12 @@ public
then
ComponentRef.compare(exp1.cref, cr);

case TYPENAME()
algorithm
TYPENAME(ty = ty) := exp2;
then
valueCompare(exp1.ty, ty);

case ARRAY()
algorithm
ARRAY(ty = ty, elements = expl) := exp2;
Expand Down Expand Up @@ -544,6 +554,17 @@ public
outExp := arrayFromList(newlst, ty, restdims);
end arrayFromList_impl;

function makeEnumLiterals
input Type enumType;
output list<Expression> literals;
protected
list<String> lits;
algorithm
Type.ENUMERATION(literals = lits) := enumType;
literals := list(ENUM_LITERAL(enumType, l, i)
threaded for l in lits, i in 1:listLength(lits));
end makeEnumLiterals;

function toInteger
input Expression exp;
output Integer i;
Expand Down Expand Up @@ -572,6 +593,7 @@ public
then Absyn.pathString(t.typePath) + "." + exp.name;

case CREF() then ComponentRef.toString(exp.cref);
case TYPENAME() then Type.toString(exp.ty);
case ARRAY() then "{" + stringDelimitList(List.map(exp.elements, toString), ", ") + "}";

case RANGE() then toString(exp.start) +
Expand Down Expand Up @@ -622,6 +644,9 @@ public
case CREF()
then DAE.CREF(ComponentRef.toDAE(exp.cref), DAE.T_UNKNOWN_DEFAULT);

// TYPENAME() doesn't have a DAE representation, and shouldn't need to be
// converted anyway.

case ARRAY()
then DAE.ARRAY(Type.toDAE(exp.ty), Type.isScalarArray(exp.ty),
list(toDAE(e) for e in exp.elements));
Expand Down

0 comments on commit 6deba45

Please sign in to comment.