Skip to content

Commit

Permalink
[NF] Flat modelica output prototype.
Browse files Browse the repository at this point in the history
  • Loading branch information
perost committed Apr 6, 2020
1 parent 4f16050 commit 4a327ea
Show file tree
Hide file tree
Showing 38 changed files with 1,020 additions and 98 deletions.
2 changes: 1 addition & 1 deletion OMCompiler/Compiler/Main/Main.mo
Expand Up @@ -444,7 +444,7 @@ algorithm

Print.clearBuf();
execStat("Transformations before Dump");
s := if Config.silent() then "" else DAEDump.dumpStr(d, funcs);
s := if Config.silent() or Flags.getConfigBool(Flags.FLAT_MODELICA) then "" else DAEDump.dumpStr(d, funcs);
execStat("DAEDump done");
Print.printBuf(s);
if Flags.isSet(Flags.DAE_DUMP_GRAPHV) then
Expand Down
16 changes: 16 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFBinding.mo
Expand Up @@ -476,6 +476,22 @@ public
end match;
end toString;

function toFlatString
input Binding binding;
input String prefix = "";
output String string;
algorithm
string := match binding
case UNBOUND() then "";
case RAW_BINDING() then prefix + Dump.printExpStr(binding.bindingExp);
case UNTYPED_BINDING() then prefix + Expression.toFlatString(binding.bindingExp);
case TYPED_BINDING() then prefix + Expression.toFlatString(binding.bindingExp);
case FLAT_BINDING() then prefix + Expression.toFlatString(binding.bindingExp);
case CEVAL_BINDING() then prefix + Expression.toFlatString(binding.bindingExp);
case INVALID_BINDING() then toFlatString(binding.binding, prefix);
end match;
end toFlatString;

function isEqual
input Binding binding1;
input Binding binding2;
Expand Down
87 changes: 85 additions & 2 deletions OMCompiler/Compiler/NFFrontEnd/NFCall.mo
Expand Up @@ -180,7 +180,6 @@ uniontype Call
input SourceInfo info;
output Expression callExp;
algorithm

callExp := match functionArgs
case Absyn.FUNCTIONARGS() then instNormalCall(functionName, functionArgs, scope, info);
case Absyn.FOR_ITER_FARG() then instIteratorCall(functionName, functionArgs, scope, info);
Expand Down Expand Up @@ -669,6 +668,57 @@ uniontype Call
end match;
end toString;

function toFlatString
input Call call;
output String str;
protected
String name, arg_str,c;
Expression argexp;
list<InstNode> iters;
algorithm
str := match call
case TYPED_CALL()
algorithm
name := AbsynUtil.pathString(Function.name(call.fn));
arg_str := stringDelimitList(list(Expression.toFlatString(arg) for arg in call.arguments), ", ");
then
if Function.isBuiltin(call.fn) then
stringAppendList({name, "(", arg_str, ")"})
else
stringAppendList({"'", name, "'(", arg_str, ")"});

case TYPED_ARRAY_CONSTRUCTOR()
algorithm
if isVectorized(call) then
// Vectorized calls contains iterators with illegal Modelica names
// (to avoid name conflicts), to make the flat output legal such
// calls are reverted to their original form here.
str := toFlatString(devectorizeCall(call));
else
name := AbsynUtil.pathString(Function.name(NFBuiltinFuncs.ARRAY_FUNC));
arg_str := Expression.toFlatString(call.exp);
c := stringDelimitList(list(InstNode.name(Util.tuple21(iter)) + " in " +
Expression.toFlatString(Util.tuple22(iter)) for iter in call.iters), ", ");
str := stringAppendList({"{", arg_str, " for ", c, "}"});
end if;
then
str;

case TYPED_REDUCTION()
algorithm
name := AbsynUtil.pathString(Function.name(call.fn));
arg_str := Expression.toFlatString(call.exp);
c := stringDelimitList(list(InstNode.name(Util.tuple21(iter)) + " in " +
Expression.toFlatString(Util.tuple22(iter)) for iter in call.iters), ", ");
then
if Function.isBuiltin(call.fn) then
stringAppendList({name, "(", arg_str, " for ", c, ")"})
else
stringAppendList({"'", name, "'(", arg_str, " for ", c, ")"});

end match;
end toFlatString;

function typedString
"Like toString, but prefixes each argument with its type as a comment."
input Call call;
Expand Down Expand Up @@ -1392,6 +1442,40 @@ protected
end match;
end vectorizeCall;

function isVectorized
input Call call;
output Boolean vectorized;
algorithm
vectorized := match call
// A call is considered to be vectorized if the first iterator has a name
// beginning with $.
case TYPED_ARRAY_CONSTRUCTOR(exp = Expression.CALL())
then stringGet(InstNode.name(Util.tuple21(listHead(call.iters))), 1) == 36; /* $ */
else false;
end match;
end isVectorized;

function devectorizeCall
"Transforms a vectorized call into a non-vectorized one. This function is
used as a helper to output valid flat Modelica, and should probably not
be used where e.g. correct types are required."
input Call call;
output Call outCall;
protected
Expression exp, iter_exp;
list<tuple<InstNode, Expression>> iters;
InstNode iter_node;
algorithm
TYPED_ARRAY_CONSTRUCTOR(exp = exp, iters = iters) := call;

for i in iters loop
(iter_node, iter_exp) := i;
exp := Expression.replaceIterator(exp, iter_node, iter_exp);
end for;

Expression.CALL(call = outCall) := exp;
end devectorizeCall;

function evaluateCallType
input output Type ty;
input Function fn;
Expand Down Expand Up @@ -1532,7 +1616,6 @@ protected
fail();
end match;
end getSpecialReturnType;

end Call;

annotation(__OpenModelica_Interface="frontend");
Expand Down
28 changes: 28 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFComponent.mo
Expand Up @@ -155,6 +155,15 @@ uniontype Component
Prefixes.unparseVariability(attr.variability, ty) +
Prefixes.unparseDirection(attr.direction);
end toString;

function toFlatString
input Attributes attr;
input Type ty;
output String str;
algorithm
str := Prefixes.unparseVariability(attr.variability, ty) +
Prefixes.unparseDirection(attr.direction);
end toFlatString;
end Attributes;

record COMPONENT_DEF
Expand Down Expand Up @@ -818,6 +827,25 @@ uniontype Component
end match;
end toString;

function toFlatString
input String name;
input Component component;
output String str;
algorithm
str := match component
local
SCode.Element def;

case TYPED_COMPONENT()
then Attributes.toFlatString(component.attributes, component.ty) +
Type.toFlatString(component.ty) + " '" + name + "'" +
Binding.toFlatString(component.binding, " = ");

case TYPE_ATTRIBUTE()
then name + Modifier.toFlatString(component.modifier, printName = false);
end match;
end toFlatString;

function setDimensions
input list<Dimension> dims;
input output Component component;
Expand Down
66 changes: 53 additions & 13 deletions OMCompiler/Compiler/NFFrontEnd/NFComponentRef.mo
Expand Up @@ -661,22 +661,61 @@ public
input ComponentRef cref;
output String str;
algorithm
str := match cref
case CREF(restCref = EMPTY())
then InstNode.name(cref.node) + Subscript.toStringList(cref.subscripts);
str := stringDelimitList(toString_impl(cref, {}), ".");
end toString;

function toString_impl
input ComponentRef cref;
input output list<String> strl;
algorithm
strl := match cref
local
String str;

case CREF()
algorithm
str := toString(cref.restCref);
str := InstNode.name(cref.node) + Subscript.toStringList(cref.subscripts);
then
str + "." + InstNode.name(cref.node) + Subscript.toStringList(cref.subscripts);
toString_impl(cref.restCref, str :: strl);

case WILD() then "_";
case STRING(restCref = EMPTY()) then cref.name;
case STRING() then toString(cref.restCref) + "." + cref.name;
else "EMPTY_CREF";
case WILD() then "_" :: strl;
case STRING() then toString_impl(cref.restCref, cref.name :: strl);
else strl;
end match;
end toString;
end toString_impl;

function toFlatString
input ComponentRef cref;
output String str;
protected
ComponentRef cr;
list<Subscript> subs;
list<String> strl = {};
algorithm
(cr, subs) := stripSubscripts(cref);
strl := toFlatString_impl(cr, strl);
str := stringAppendList({"'", stringDelimitList(strl, "."), "'", Subscript.toFlatStringList(subs)});
end toFlatString;

function toFlatString_impl
input ComponentRef cref;
input output list<String> strl;
algorithm
strl := match cref
local
String str;

case CREF()
algorithm
str := InstNode.name(cref.node) + Subscript.toFlatStringList(cref.subscripts);
then
toFlatString_impl(cref.restCref, str :: strl);

case WILD() then "_" :: strl;
case STRING() then toFlatString_impl(cref.restCref, cref.name :: strl);
else strl;
end match;
end toFlatString_impl;

function listToString
input list<ComponentRef> crs;
Expand Down Expand Up @@ -770,11 +809,12 @@ public
"Strips the subscripts from the last name in a cref, e.g. a[2].b[3] => a[2].b"
input ComponentRef cref;
output ComponentRef strippedCref;
output list<Subscript> subs;
algorithm
strippedCref := match cref
(strippedCref, subs) := match cref
case CREF()
then CREF(cref.node, {}, cref.ty, cref.origin, cref.restCref);
else cref;
then (CREF(cref.node, {}, cref.ty, cref.origin, cref.restCref), cref.subscripts);
else (cref, {});
end match;
end stripSubscripts;

Expand Down

0 comments on commit 4a327ea

Please sign in to comment.