Skip to content

Commit

Permalink
[NB] update lowering (#10837)
Browse files Browse the repository at this point in the history
- add array state select parsing
 - update iterator and cref lowering to not use name nodes
  • Loading branch information
kabdelhak committed Jun 14, 2023
1 parent b2f623e commit 5ba077a
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 53 deletions.
66 changes: 43 additions & 23 deletions OMCompiler/Compiler/NBackEnd/Classes/NBackendDAE.mo
Expand Up @@ -61,6 +61,7 @@ protected
import FlatModel = NFFlatModel;
import InstNode = NFInstNode.InstNode;
import Prefixes = NFPrefixes;
import Statement = NFStatement;
import Subscript = NFSubscript;
import Type = NFType;
import Variable = NFVariable;
Expand Down Expand Up @@ -584,18 +585,23 @@ protected
input VariablePointers variables;
input Pointer<list<Pointer<Variable>>> binding_iter_lst;
algorithm
var := match var
local
Binding binding;
case Variable.VARIABLE(binding = binding as Binding.TYPED_BINDING()) algorithm
// collect all iterators (only locally known) so that they have a respective variable
BackendExtension.BackendInfo.map(var.backendinfo, function collectBindingIterators(variables = variables, binding_iter_lst = binding_iter_lst));
Expression.map(binding.bindingExp, function collectBindingIterators(variables = variables, binding_iter_lst = binding_iter_lst));
then var;
else algorithm
BackendExtension.BackendInfo.map(var.backendinfo, function collectBindingIterators(variables = variables, binding_iter_lst = binding_iter_lst));
then var;
end match;
try
var := match var
local
Binding binding;
case Variable.VARIABLE(binding = binding as Binding.TYPED_BINDING()) algorithm
// collect all iterators (only locally known) so that they have a respective variable
BackendExtension.BackendInfo.map(var.backendinfo, function collectBindingIterators(variables = variables, binding_iter_lst = binding_iter_lst));
Expression.map(binding.bindingExp, function collectBindingIterators(variables = variables, binding_iter_lst = binding_iter_lst));
then var;
else algorithm
BackendExtension.BackendInfo.map(var.backendinfo, function collectBindingIterators(variables = variables, binding_iter_lst = binding_iter_lst));
then var;
end match;
else
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed for:\n" + Variable.toString(var)});
fail();
end try;
end collectVariableBindingIterators;

function lowerRecordChildren
Expand Down Expand Up @@ -715,6 +721,8 @@ protected
list<FEquation.Branch> branches;
EquationAttributes attr;
Integer rec_size;
Statement stmt;
Algorithm alg;

case FEquation.ARRAY_EQUALITY(lhs = lhs, rhs = rhs, ty = ty, source = source)
guard(Type.isArray(ty)) algorithm
Expand Down Expand Up @@ -778,12 +786,19 @@ protected
then result;

// if equation
case FEquation.IF() then {Pointer.create(lowerIfEquation(frontend_equation, init))};
case FEquation.IF() then {Pointer.create(lowerIfEquation(frontend_equation, init))};

// When equation cases
case FEquation.WHEN() then lowerWhenEquation(frontend_equation, init);
case FEquation.ASSERT() then lowerWhenEquation(frontend_equation, init);

// wrap no return call in algorithm
case FEquation.NORETCALL() algorithm
stmt := Statement.NORETCALL(frontend_equation.exp, frontend_equation.source);
alg := Algorithm.ALGORITHM({stmt}, {}, {}, InstNode.EMPTY_NODE(), frontend_equation.source);
alg := Algorithm.setInputsOutputs(alg);
then {lowerAlgorithm(alg, init)};

// These have to be called inside a when equation body since they need
// to get passed a condition from surrounding when equation.
case FEquation.TERMINATE() algorithm
Expand All @@ -792,9 +807,6 @@ protected
case FEquation.REINIT() algorithm
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed for REINIT expression without condition:\n" + FEquation.toString(frontend_equation)});
then fail();
case FEquation.NORETCALL() algorithm
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed for NORETCALL expression without condition:\n" + FEquation.toString(frontend_equation)});
then fail();

else algorithm
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed for\n" + FEquation.toString(frontend_equation)});
Expand Down Expand Up @@ -862,7 +874,8 @@ protected
// We should never get an empty list here since the last condition has to
// be TRUE. If-Equations have to have a plain else case for consistency!
else algorithm
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed."});
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed for:\n"
+ List.toString(branches, function FEquation.Branch.toString(indent = ""), "", "\t", "\n", "\n")});
then fail();

end match;
Expand Down Expand Up @@ -1100,14 +1113,16 @@ protected
algorithm
exp := match exp
local
Type ty;
ComponentRef cref;
Call call;
case Expression.CREF(ty = ty, cref = cref) then Expression.CREF(ty, lowerComponentReference(cref, variables));
case Expression.CREF() guard(not ComponentRef.isNameNode(exp.cref))
then Expression.CREF(exp.ty, lowerComponentReference(exp.cref, variables));
case Expression.CALL(call = call as Call.TYPED_ARRAY_CONSTRUCTOR()) algorithm
call.iters := list(Util.applyTuple21(tpl, function lowerInstNode(variables = variables)) for tpl in call.iters);
exp.call := call;
then exp;
case Expression.CALL(call = call as Call.TYPED_REDUCTION()) algorithm
call.iters := list(Util.applyTuple21(tpl, function lowerInstNode(variables = variables)) for tpl in call.iters);
exp.call := call;
Expand Down Expand Up @@ -1143,13 +1158,14 @@ protected
input VariablePointers variables;
input Pointer<list<Pointer<Variable>>> binding_iter_lst;
algorithm
try
() := match exp
local
ComponentRef cref;
Call call;
case Expression.CREF(cref = cref) guard(not VariablePointers.containsCref(cref, variables)) algorithm
Pointer.update(binding_iter_lst, lowerIterator(cref) :: Pointer.access(binding_iter_lst));
case Expression.CREF() guard(not (VariablePointers.containsCref(exp.cref, variables)
or ComponentRef.isNameNode(exp.cref))) algorithm
Pointer.update(binding_iter_lst, lowerIterator(exp.cref) :: Pointer.access(binding_iter_lst));
then ();
case Expression.CALL(call = call as Call.TYPED_ARRAY_CONSTRUCTOR()) algorithm
Expand All @@ -1165,6 +1181,10 @@ protected
then ();
else ();
end match;
else
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed for " + Expression.toString(exp)});
fail();
end try;
end collectBindingIterators;
function collectIterator
Expand Down
70 changes: 40 additions & 30 deletions OMCompiler/Compiler/NFFrontEnd/NFBackendExtension.mo
Expand Up @@ -942,15 +942,15 @@ public
for attr in attrs loop
(name, b) := attr;
() := match name
case "displayUnit" algorithm displayUnit := createAttribute(b); then ();
case "fixed" algorithm fixed := createAttribute(b); then ();
case "max" algorithm max := createAttribute(b); then ();
case "min" algorithm min := createAttribute(b); then ();
case "nominal" algorithm nominal := createAttribute(b); then ();
case "quantity" algorithm quantity := createAttribute(b); then ();
case "start" algorithm start := createAttribute(b); then ();
case "stateSelect" algorithm state_select := createStateSelect(b); then ();
// TODO: VAR_ATTR_REAL has no field for unbounded.
case "displayUnit" algorithm displayUnit := createAttribute(b); then ();
case "fixed" algorithm fixed := createAttribute(b); then ();
case "max" algorithm max := createAttribute(b); then ();
case "min" algorithm min := createAttribute(b); then ();
case "nominal" algorithm nominal := createAttribute(b); then ();
case "quantity" algorithm quantity := createAttribute(b); then ();
case "start" algorithm start := createAttribute(b); then ();
case "stateSelect" algorithm state_select := createStateSelect(b); then ();
// TODO: VAR_ATTR_REAL has no field for unbounded (which should be named unbound).
case "unbounded" then ();
case "unit" algorithm unit := createAttribute(b); then ();

Expand Down Expand Up @@ -1150,32 +1150,42 @@ public
protected
Expression exp = Binding.getTypedExp(binding);
String name;
function getStateSelectName
input Expression exp;
output String name;
protected
Expression arg;
InstNode node;
Call call;
algorithm
name := match exp
case Expression.ENUM_LITERAL() then exp.name;
case Expression.CREF(cref = ComponentRef.CREF(node = node)) then InstNode.name(node);
case Expression.CALL(call = call as Call.TYPED_ARRAY_CONSTRUCTOR()) then getStateSelectName(call.exp);
case Expression.CALL(call = call as Call.TYPED_CALL(arguments = arg::_))
guard(AbsynUtil.pathString(Function.nameConsiderBuiltin(call.fn)) == "fill")
then getStateSelectName(arg);
else algorithm
Error.assertion(false, getInstanceName() +
" got invalid StateSelect expression " + Expression.toString(exp), sourceInfo());
then fail();
end match;
end getStateSelectName;
algorithm
name := getStateSelectName(exp);
stateSelect := SOME(lookupStateSelectMember(name));
end createStateSelect;

function getStateSelectName
input Expression exp;
output String name;
protected
Expression arg;
InstNode node;
Call call;
list<Expression> rest;
algorithm
name := match exp
case Expression.ENUM_LITERAL() then exp.name;
case Expression.CREF(cref = ComponentRef.CREF(node = node)) then InstNode.name(node);
case Expression.CALL(call = call as Call.TYPED_ARRAY_CONSTRUCTOR()) then getStateSelectName(call.exp);
case Expression.CALL(call = call as Call.TYPED_CALL(arguments = arg::_))
guard(AbsynUtil.pathString(Function.nameConsiderBuiltin(call.fn)) == "fill")
then getStateSelectName(arg);
case Expression.ARRAY() algorithm
arg :: rest := arrayList(exp.elements);
if not (listEmpty(rest) or List.all(rest, function Expression.isEqual(exp2=arg))) then
Error.assertion(false, getInstanceName() +
" cannot handle array StateSelect with different values yet:" + Expression.toString(exp), sourceInfo());
fail();
end if;
then getStateSelectName(arg);
else algorithm
Error.assertion(false, getInstanceName() +
" got invalid StateSelect expression " + Expression.toString(exp), sourceInfo());
then fail();
end match;
end getStateSelectName;

function createTearingSelect
"tearingSelect is an annotation and has to be extracted from the comment."
input Option<SCode.Comment> optComment;
Expand Down
10 changes: 10 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFComponentRef.mo
Expand Up @@ -212,6 +212,16 @@ public
end match;
end isOutput;

function isNameNode
input ComponentRef cref;
output Boolean res;
algorithm
res := match cref
case CREF(node = InstNode.NAME_NODE()) then true;
else false;
end match;
end isNameNode;

function node
input ComponentRef cref;
output InstNode node;
Expand Down
13 changes: 13 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFEquation.mo
Expand Up @@ -140,6 +140,19 @@ public
end match;
end toFlatStream;

function toString
input Branch branch;
input String indent;
output String str;
protected
IOStream.IOStream s;
algorithm
s := IOStream.create(getInstanceName(), IOStream.IOStreamType.LIST());
s := toStream(branch, indent, s);
str := IOStream.string(s);
IOStream.delete(s);
end toString;

function triggerErrors
input Branch branch;
algorithm
Expand Down

0 comments on commit 5ba077a

Please sign in to comment.