Skip to content

Commit

Permalink
[NF] Flat Modelica improvements.
Browse files Browse the repository at this point in the history
- Simplify away subscripts when devectorizing calls if possible.
- Recheck whether a subscript is a slice or not after modifying the
  contained expression via e.g. Subscript.map.
- Only dump 'input' prefix for top-level components.
- Dump type attributes for components in e.g. functions.
- Dump 'public' for public elements, otherwise everything will be
  private after the first private element.
- Fix dumping of array dimensions which was using normal toString.
- Change more dump functions to use IOStream.
  • Loading branch information
perost committed May 29, 2020
1 parent 777e83a commit e293f5a
Show file tree
Hide file tree
Showing 12 changed files with 278 additions and 75 deletions.
6 changes: 4 additions & 2 deletions OMCompiler/Compiler/NFFrontEnd/NFCall.mo
Expand Up @@ -42,8 +42,10 @@ import Type = NFType;
import Record = NFRecord;

protected
import Binding = NFBinding;
import BuiltinCall = NFBuiltinCall;
import Ceval = NFCeval;
import Component = NFComponent;
import ComponentRef = NFComponentRef;
import Config;
import Dimension = NFDimension;
Expand All @@ -54,9 +56,7 @@ import Inst = NFInst;
import List;
import Lookup = NFLookup;
import MetaModelica.Dangerous.listReverseInPlace;
import Binding = NFBinding;
import Class = NFClass;
import Component = NFComponent;
import NFFunction.Function;
import NFFunction.FunctionMatchKind;
import NFFunction.MatchedFunction;
Expand All @@ -68,6 +68,7 @@ import NFTyping.ExpOrigin;
import Operator = NFOperator;
import Prefixes = NFPrefixes;
import SCodeUtil;
import SimplifyExp = NFSimplifyExp;
import Subscript = NFSubscript;
import TypeCheck = NFTypeCheck;
import Typing = NFTyping;
Expand Down Expand Up @@ -1427,6 +1428,7 @@ protected
exp := Expression.replaceIterator(exp, iter_node, iter_exp);
end for;

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

Expand Down
108 changes: 93 additions & 15 deletions OMCompiler/Compiler/NFFrontEnd/NFComponent.mo
Expand Up @@ -50,6 +50,7 @@ import Prefixes = NFPrefixes;
import SCodeUtil;
import Restriction = NFRestriction;
import Component = NFComponent;
import IOStream;

public
constant Attributes DEFAULT_ATTR =
Expand Down Expand Up @@ -155,14 +156,22 @@ public
Prefixes.unparseDirection(attr.direction);
end toString;

function toFlatString
function toFlatStream
input Attributes attr;
input Type ty;
output String str;
input output IOStream.IOStream s;
input Boolean isTopLevel = true;
algorithm
str := Prefixes.unparseVariability(attr.variability, ty) +
Prefixes.unparseDirection(attr.direction);
end toFlatString;
if attr.isFinal then
s := IOStream.append(s, "final ");
end if;

s := IOStream.append(s, Prefixes.unparseVariability(attr.variability, ty));

if isTopLevel then
s := IOStream.append(s, Prefixes.unparseDirection(attr.direction));
end if;
end toFlatStream;
end Attributes;

record COMPONENT_DEF
Expand Down Expand Up @@ -851,23 +860,92 @@ public
end match;
end toString;

function toFlatString
function toFlatStream
input String name;
input Component component;
output String str;
input output IOStream.IOStream s;
protected
list<tuple<String, Binding>> ty_attrs;
algorithm
str := match component
local
SCode.Element def;

() := match component
case TYPED_COMPONENT()
then Attributes.toFlatString(component.attributes, component.ty) +
Type.toFlatString(component.ty) + " '" + name + "'" +
Binding.toFlatString(component.binding, " = ");
algorithm
s := Attributes.toFlatStream(component.attributes, component.ty, s);
s := IOStream.append(s, Type.toFlatString(component.ty));
s := IOStream.append(s, " '");
s := IOStream.append(s, name);
s := IOStream.append(s, "'");

ty_attrs := list((Modifier.name(a), Modifier.binding(a)) for a in
Class.getTypeAttributes(InstNode.getClass(component.classInst)));
s := typeAttrsToFlatStream(ty_attrs, component.ty, s);

s := IOStream.append(s, Binding.toFlatString(component.binding, " = "));
then
();

case TYPE_ATTRIBUTE()
then name + Modifier.toFlatString(component.modifier, printName = false);
algorithm
s := IOStream.append(s, name);
s := IOStream.append(s, Modifier.toFlatString(component.modifier, printName = false));
then
();
end match;
end toFlatStream;

function typeAttrsToFlatStream
input list<tuple<String, Binding>> typeAttrs;
input Type componentType;
input output IOStream.IOStream s;
protected
Integer var_dims, binding_dims;
list<tuple<String, Binding>> ty_attrs = typeAttrs;
String name;
Binding binding;
Expression bind_exp;
algorithm
if listEmpty(ty_attrs) then
return;
end if;

s := IOStream.append(s, "(");
var_dims := Type.dimensionCount(componentType);

while true loop
(name, binding) := listHead(ty_attrs);
bind_exp := Expression.getBindingExp(Binding.getExp(binding));
binding_dims := Type.dimensionCount(Expression.typeOf(bind_exp));

if var_dims > binding_dims then
s := IOStream.append(s, "each ");
end if;

s := IOStream.append(s, name);
s := IOStream.append(s, " = ");
s := IOStream.append(s, Binding.toFlatString(binding));

ty_attrs := listRest(ty_attrs);
if listEmpty(ty_attrs) then
break;
else
s := IOStream.append(s, ", ");
end if;
end while;

s := IOStream.append(s, ")");
end typeAttrsToFlatStream;

function toFlatString
input String name;
input Component component;
output String str;
protected
IOStream.IOStream s;
algorithm
s := IOStream.create(name, IOStream.IOStreamType.LIST());
s := toFlatStream(name, component, s);
str := IOStream.string(s);
IOStream.delete(s);
end toFlatString;

function setDimensions
Expand Down
2 changes: 1 addition & 1 deletion OMCompiler/Compiler/NFFrontEnd/NFComponentRef.mo
Expand Up @@ -857,7 +857,7 @@ public

case CREF(origin = Origin.CREF)
algorithm
subs := list(Subscript.simplify(s) for s in cref.subscripts);
subs := Subscript.simplifyList(cref.subscripts, Type.arrayDims(cref.ty));
then
CREF(cref.node, subs, cref.ty, cref.origin, simplifySubscripts(cref.restCref));

Expand Down
45 changes: 45 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFDimension.mo
Expand Up @@ -286,6 +286,20 @@ public
end if;
end toStringList;

function toFlatString
input Dimension dim;
output String str;
algorithm
str := match dim
case INTEGER() then String(dim.size);
case BOOLEAN() then "Boolean";
case ENUM() then Type.toFlatString(dim.enumType);
case EXP() then Expression.toFlatString(dim.exp);
case UNKNOWN() then ":";
case UNTYPED() then Expression.toFlatString(dim.dimension);
end match;
end toFlatString;

function endExp
"Returns an expression for the last index in a dimension."
input Dimension dim;
Expand Down Expand Up @@ -325,6 +339,37 @@ public
end match;
end sizeExp;

function expIsLowerBound
"Returns true if the expression represents the lower bound of a dimension."
input Expression exp;
output Boolean isStart;
algorithm
isStart := match exp
case Expression.INTEGER() then exp.value == 1;
case Expression.BOOLEAN() then exp.value == false;
case Expression.ENUM_LITERAL() then exp.index == 1;
else false;
end match;
end expIsLowerBound;

function expIsUpperBound
"Returns true if the expression represents the upper bound of the given dimension."
input Expression exp;
input Dimension dim;
output Boolean isEnd;
algorithm
isEnd := match (exp, dim)
local
Type ty;

case (Expression.INTEGER(), INTEGER()) then exp.value == dim.size;
case (Expression.BOOLEAN(), _) then exp.value == true;
case (Expression.ENUM_LITERAL(), ENUM(enumType = ty as Type.ENUMERATION()))
then exp.index == listLength(ty.literals);
else false;
end match;
end expIsUpperBound;

function variability
input Dimension dim;
output Variability var;
Expand Down
2 changes: 1 addition & 1 deletion OMCompiler/Compiler/NFFrontEnd/NFEvalFunction.mo
Expand Up @@ -495,7 +495,7 @@ algorithm
case Statement.FOR()
algorithm
// Make a mutable expression with a placeholder value.
iter_exp := Expression.makeMutable(Expression.EMPTY(Type.UNKNOWN()));
iter_exp := Expression.makeMutable(Expression.EMPTY(InstNode.getType(stmt.iterator)));
// Replace the iterator with the expression in the body of the for loop.
stmt.body := list(
Statement.mapExp(s, function Expression.replaceIterator(
Expand Down
2 changes: 2 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFFlatModel.mo
Expand Up @@ -201,6 +201,8 @@ public
s := IOStream.append(s, ";\n");
end for;

s := IOStream.append(s, "public\n");

if not listEmpty(flat_model.initialEquations) then
s := IOStream.append(s, "initial equation\n");
s := Equation.toFlatStreamList(flat_model.initialEquations, " ", s);
Expand Down
1 change: 1 addition & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFInstNode.mo
Expand Up @@ -1366,6 +1366,7 @@ uniontype InstNode
input output IOStream.IOStream s;
algorithm
s := match node
case COMPONENT_NODE() then Component.toFlatStream(node.name, Pointer.access(node.component), s);
case CLASS_NODE() then Class.toFlatStream(Pointer.access(node.cls), node, s);
else IOStream.append(s, toFlatString(node));
end match;
Expand Down
65 changes: 53 additions & 12 deletions OMCompiler/Compiler/NFFrontEnd/NFModifier.mo
Expand Up @@ -52,6 +52,7 @@ protected
import Error;
import List;
import SCodeUtil;
import IOStream;

constant Modifier EMPTY_MOD = NOMOD();

Expand Down Expand Up @@ -540,33 +541,73 @@ public
end match;
end toString;

function toFlatString
function toFlatStreamList
input list<Modifier> modifiers;
input output IOStream.IOStream s;
input String delimiter = ", ";
protected
list<Modifier> mods = modifiers;
algorithm
if listEmpty(mods) then
return;
end if;

while true loop
s := toFlatStream(listHead(mods), s);
mods := listRest(mods);

if listEmpty(mods) then
break;
else
s := IOStream.append(s, delimiter);
end if;
end while;
end toFlatStreamList;

function toFlatStream
input Modifier mod;
input output IOStream.IOStream s;
input Boolean printName = true;
output String string;
protected
list<Modifier> submods;
String subs_str, binding_str, binding_sep;
algorithm
string := match mod
local
list<Modifier> submods;
String subs_str, binding_str, binding_sep;

() := match mod
case MODIFIER()
algorithm
if printName then
s := IOStream.append(s, mod.name);
end if;

submods := ModTable.listValues(mod.subModifiers);
if not listEmpty(submods) then
subs_str := "(" + stringDelimitList(list(toFlatString(s) for s in submods), ", ") + ")";
s := IOStream.append(s, "(");
s := toFlatStreamList(submods, s);
s := IOStream.append(s, ")");
binding_sep := " = ";
else
subs_str := "";
binding_sep := if printName then " = " else "= ";
end if;

binding_str := Binding.toFlatString(mod.binding, binding_sep);
s := IOStream.append(s, Binding.toFlatString(mod.binding, binding_sep));
then
if printName then mod.name + subs_str + binding_str else subs_str + binding_str;
();

else "";
else ();
end match;
end toFlatStream;

function toFlatString
input Modifier mod;
input Boolean printName = true;
output String str;
protected
IOStream.IOStream s;
algorithm
s := IOStream.create(getInstanceName(), IOStream.IOStreamType.LIST());
s := toFlatStream(mod, s, printName);
str := IOStream.string(s);
IOStream.delete(s);
end toFlatString;

protected
Expand Down
3 changes: 2 additions & 1 deletion OMCompiler/Compiler/NFFrontEnd/NFSimplifyExp.mo
Expand Up @@ -718,7 +718,8 @@ protected
algorithm
Expression.SUBSCRIPTED_EXP(e, subs, ty) := subscriptedExp;
subscriptedExp := simplify(e);
subscriptedExp := Expression.applySubscripts(list(Subscript.simplify(s) for s in subs), subscriptedExp);
subs := Subscript.simplifyList(subs, Type.arrayDims(Expression.typeOf(e)));
subscriptedExp := Expression.applySubscripts(subs, subscriptedExp);
end simplifySubscriptedExp;

function simplifyTupleElement
Expand Down

0 comments on commit e293f5a

Please sign in to comment.