Skip to content

Commit

Permalink
[NF] More flat Modelica improvements.
Browse files Browse the repository at this point in the history
- Collect types from functions too.
- Quote enumeration names.
- Handle subscripted general expressions by generating subscripting
  functions.
  • Loading branch information
perost committed Apr 8, 2020
1 parent 0d25be9 commit a2d6db2
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 9 deletions.
33 changes: 32 additions & 1 deletion OMCompiler/Compiler/NFFrontEnd/NFExpression.mo
Expand Up @@ -1701,7 +1701,8 @@ public
case UNBOX() then "UNBOX(" + toFlatString(exp.exp) + ")";
case BOX() then "BOX(" + toFlatString(exp.exp) + ")";
case CAST() then toFlatString(exp.exp);
case SUBSCRIPTED_EXP() then toFlatString(exp.exp) + Subscript.toFlatStringList(exp.subscripts);

case SUBSCRIPTED_EXP() then toFlatSubscriptedString(exp.exp, exp.subscripts);
case TUPLE_ELEMENT() then toFlatString(exp.tupleExp) + "[" + intString(exp.index) + "]";
case RECORD_ELEMENT() then toFlatString(exp.recordExp) + "[field: " + exp.fieldName + "]";
case MUTABLE() then toFlatString(Mutable.access(exp.exp));
Expand All @@ -1715,6 +1716,36 @@ public
end match;
end toFlatString;

function toFlatSubscriptedString
input Expression exp;
input list<Subscript> subs;
output String str;
protected
Type exp_ty;
list<Type> sub_tyl;
list<Dimension> dims;
list<String> strl;
String name;
algorithm
exp_ty := typeOf(exp);
dims := List.firstN(Type.arrayDims(exp_ty), listLength(subs));
sub_tyl := list(Dimension.subscriptType(d) for d in dims);
name := Type.subscriptedTypeName(exp_ty, sub_tyl);

strl := {")"};

for s in subs loop
strl := Subscript.toFlatString(s) :: strl;
strl := "," :: strl;
end for;

strl := toFlatString(exp) :: strl;
strl := "'(" :: strl;
strl := name :: strl;
strl := "'" :: strl;
str := stringAppendList(strl);
end toFlatSubscriptedString;

function operandString
"Helper function to toString, prints an operator and adds parentheses as needed."
input Expression operand;
Expand Down
66 changes: 61 additions & 5 deletions OMCompiler/Compiler/NFFrontEnd/NFFlatModel.mo
Expand Up @@ -44,6 +44,10 @@ protected
import ComplexType = NFComplexType;
import NFInstNode.InstNode;
import IOStream;
import NFSubscript.Subscript;
import NFClass.Class;
import NFClassTree.ClassTree;
import NFComponent.Component;

import FlatModel = NFFlatModel;

Expand Down Expand Up @@ -172,7 +176,7 @@ public
end if;
end for;

for ty in TypeTree.listValues(collectFlatTypes(flatModel)) loop
for ty in TypeTree.listValues(collectFlatTypes(flatModel, functions)) loop
s := Type.toFlatDeclarationStream(ty, s);
s := IOStream.append(s, ";\n\n");
end for;
Expand Down Expand Up @@ -216,17 +220,19 @@ public

function collectFlatTypes
input FlatModel flatModel;
input list<Function> functions;
output TypeTree types;
algorithm
types := TypeTree.new();
types := List.fold(flatModel.variables, collectComponentFlatTypes, types);
types := List.fold(flatModel.variables, collectVariableFlatTypes, types);
types := List.fold(flatModel.equations, collectEquationFlatTypes, types);
types := List.fold(flatModel.initialEquations, collectEquationFlatTypes, types);
types := List.fold(flatModel.algorithms, collectAlgorithmFlatTypes, types);
types := List.fold(flatModel.initialAlgorithms, collectAlgorithmFlatTypes, types);
types := List.fold(functions, collectFunctionFlatTypes, types);
end collectFlatTypes;

function collectComponentFlatTypes
function collectVariableFlatTypes
input Variable var;
input output TypeTree types;
algorithm
Expand All @@ -236,7 +242,7 @@ public
for attr in var.typeAttributes loop
types := collectBindingFlatTypes(Util.tuple22(attr), types);
end for;
end collectComponentFlatTypes;
end collectVariableFlatTypes;

function collectFlatType
input Type ty;
Expand Down Expand Up @@ -449,8 +455,58 @@ public
input Expression exp;
input output TypeTree types;
algorithm
types := collectFlatType(Expression.typeOf(exp), types);
types := match exp
case Expression.SUBSCRIPTED_EXP()
algorithm
types := collectSubscriptedFlatType(exp.exp, exp.subscripts, exp.ty, types);
then
types;

else collectFlatType(Expression.typeOf(exp), types);
end match;
end collectExpFlatTypes_traverse;

function collectFunctionFlatTypes
input Function fn;
input output TypeTree types;
protected
list<Statement> body;
algorithm
types := ClassTree.foldComponents(Class.classTree(InstNode.getClass(fn.node)),
collectComponentFlatTypes, types);
body := Function.getBody(fn);
types := List.fold(body, collectStatementFlatTypes, types);
end collectFunctionFlatTypes;

function collectComponentFlatTypes
input InstNode component;
input output TypeTree types;
protected
Component comp;
algorithm
comp := InstNode.component(component);
types := collectFlatType(Component.getType(comp), types);
types := collectBindingFlatTypes(Component.getBinding(comp), types);
end collectComponentFlatTypes;

function collectSubscriptedFlatType
input Expression exp;
input list<Subscript> subs;
input Type subscriptedTy;
input output TypeTree types;
protected
Type exp_ty;
list<Type> sub_tyl;
list<Dimension> dims;
list<String> strl;
String name;
algorithm
exp_ty := Expression.typeOf(exp);
dims := List.firstN(Type.arrayDims(exp_ty), listLength(subs));
sub_tyl := list(Dimension.subscriptType(d) for d in dims);
name := Type.subscriptedTypeName(exp_ty, sub_tyl);
types := TypeTree.add(types, Absyn.IDENT(name), Type.SUBSCRIPTED(name, exp_ty, sub_tyl, subscriptedTy));
end collectSubscriptedFlatType;

annotation(__OpenModelica_Interface="frontend");
end NFFlatModel;
63 changes: 60 additions & 3 deletions OMCompiler/Compiler/NFFrontEnd/NFType.mo
Expand Up @@ -113,6 +113,13 @@ public
record ANY
end ANY;

record SUBSCRIPTED
String name;
Type ty;
list<Type> subs;
Type subscriptedTy;
end SUBSCRIPTED;

// TODO: Fix constants in uniontypes and use these wherever applicable to
// speed up comparisons using referenceEq.
//constant Type INTEGER_DEFAULT = NFType.INTEGER();
Expand Down Expand Up @@ -745,7 +752,7 @@ public
case Type.STRING() then "String";
case Type.BOOLEAN() then "Boolean";
case Type.CLOCK() then "Clock";
case Type.ENUMERATION() then AbsynUtil.pathString(ty.typePath);
case Type.ENUMERATION() then "'" + AbsynUtil.pathString(ty.typePath) + "'";
case Type.ENUMERATION_ANY() then "enumeration(:)";
case Type.ARRAY() then toString(ty.elementType) + "[" + stringDelimitList(List.map(ty.dimensions, Dimension.toString), ", ") + "]";
case Type.TUPLE() then "(" + stringDelimitList(List.map(ty.types, toString), ", ") + ")";
Expand All @@ -769,7 +776,10 @@ public
input output IOStream.IOStream s;
algorithm
s := match ty
case Type.ENUMERATION()
local
Integer index;

case ENUMERATION()
algorithm
s := IOStream.append(s, "type '");
s := IOStream.append(s, AbsynUtil.pathString(ty.typePath));
Expand All @@ -788,9 +798,42 @@ public
then
s;

case Type.COMPLEX(complexTy = ComplexType.RECORD())
case COMPLEX(complexTy = ComplexType.RECORD())
then InstNode.toFlatStream(ty.cls, s);

case SUBSCRIPTED()
algorithm
s := IOStream.append(s, "function '");
s := IOStream.append(s, ty.name);
s := IOStream.append(s, "'\n");

s := IOStream.append(s, "input ");
s := IOStream.append(s, toString(ty.ty));
s := IOStream.append(s, " exp;\n");

index := 1;
for sub in ty.subs loop
s := IOStream.append(s, "input ");
s := IOStream.append(s, toString(sub));
s := IOStream.append(s, " s");
s := IOStream.append(s, String(index));
s := IOStream.append(s, ";\n");
index := index + 1;
end for;

s := IOStream.append(s, "output ");
s := IOStream.append(s, toString(ty.subscriptedTy));
s := IOStream.append(s, " result = exp[");
s := IOStream.append(s,
stringDelimitList(list("s" + String(i) for i in 1:listLength(ty.subs)), ","));
s := IOStream.append(s, "];\n");

s := IOStream.append(s, "end '");
s := IOStream.append(s, ty.name);
s := IOStream.append(s, "'");
then
s;

else s;
end match;
end toFlatDeclarationStream;
Expand Down Expand Up @@ -1027,5 +1070,19 @@ public
end if;
end sizeType;

function subscriptedTypeName
input Type expType;
input list<Type> subscriptTypes;
output String str;
protected
list<String> strl;
algorithm
strl := list(toString(t) for t in subscriptTypes);
strl := "_" :: strl;
strl := toString(expType) :: strl;
strl := "subscript" :: strl;
str := stringAppendList(strl);
end subscriptedTypeName;

annotation(__OpenModelica_Interface="frontend");
end NFType;

0 comments on commit a2d6db2

Please sign in to comment.