Skip to content

Commit

Permalink
NFInst improvements.
Browse files Browse the repository at this point in the history
- Collect/replace package constants in functions too.
- Fix naming of functions by appending the scope path.

Belonging to [master]:
  - OpenModelica/OMCompiler#2059
  - OpenModelica/OpenModelica-testsuite#797
  • Loading branch information
perost authored and OpenModelica-Hudson committed Nov 29, 2017
1 parent b9cba5b commit 12d1ede
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 16 deletions.
25 changes: 25 additions & 0 deletions Compiler/NFFrontEnd/NFComponent.mo
Expand Up @@ -346,6 +346,31 @@ uniontype Component
end match;
end getBinding;

function setBinding
input Binding binding;
input output Component component;
algorithm
() := match component
case UNTYPED_COMPONENT()
algorithm
component.binding := binding;
then
();

case TYPED_COMPONENT()
algorithm
component.binding := binding;
then
();

case ITERATOR()
algorithm
component.binding := binding;
then
();
end match;
end setBinding;

function hasBinding
input Component component;
output Boolean b;
Expand Down
11 changes: 11 additions & 0 deletions Compiler/NFFrontEnd/NFEquation.mo
Expand Up @@ -108,6 +108,17 @@ public
SourceInfo info;
end NORETCALL;

function mapExpList
input output list<Equation> eql;
input MapFn func;

partial function MapFn
input output Expression exp;
end MapFn;
algorithm
eql := list(mapExp(eq, func) for eq in eql);
end mapExpList;

function mapExp
input output Equation eq;
input MapFn func;
Expand Down
41 changes: 41 additions & 0 deletions Compiler/NFFrontEnd/NFFlatten.mo
Expand Up @@ -936,8 +936,49 @@ algorithm
if not Function.isCollected(fn) then
Function.collect(fn);
funcs := FunctionTree.add(funcs, Function.name(fn), fn);
funcs := collectClassFunctions(fn.node, funcs);
end if;
end flattenFunction;

function collectClassFunctions
input InstNode clsNode;
input output FunctionTree funcs;
protected
Class cls;
ClassTree cls_tree;
Sections sections;
Component comp;
Binding binding;
algorithm
cls := InstNode.getClass(clsNode);

() := match cls
case Class.INSTANCED_CLASS(elements = cls_tree as ClassTree.FLAT_TREE(), sections = sections)
algorithm
for c in cls_tree.components loop
comp := InstNode.component(c);
binding := Component.getBinding(comp);

if Binding.isBound(binding) then
funcs := collectExpFuncs(Binding.getTypedExp(binding), funcs);
end if;
end for;

() := match sections
case Sections.SECTIONS()
algorithm
funcs := List.fold(sections.algorithms, collectAlgorithmFuncs, funcs);
then
();

else ();
end match;
then
();

else ();
end match;
end collectClassFunctions;

annotation(__OpenModelica_Interface="frontend");
end NFFlatten;
5 changes: 3 additions & 2 deletions Compiler/NFFrontEnd/NFFunction.mo
Expand Up @@ -196,6 +196,7 @@ uniontype Function
InstNode found_scope;
LookupState state;
Absyn.Path functionPath;
ComponentRef prefix;
algorithm
try
// Make sure the name is a path.
Expand All @@ -206,8 +207,8 @@ uniontype Function
end try;

(functionRef, found_scope) := Lookup.lookupCallableName(functionName, scope, info);
// (functionRef, found_scope, state) := Lookup.lookupCref(functionName, scope, info);

prefix := ComponentRef.fromNodeList(InstNode.scopeList(found_scope));
functionRef := ComponentRef.append(functionRef, prefix);
end lookupFunction;

function instFunc
Expand Down
4 changes: 2 additions & 2 deletions Compiler/NFFrontEnd/NFInst.mo
Expand Up @@ -141,9 +141,9 @@ algorithm
// Replace or collect package constants depending on the
// replacePackageConstants debug flag.
if Flags.isSet(Flags.REPLACE_PACKAGE_CONSTS) then
elems := Package.replaceConstants(elems);
(elems, funcs) := Package.replaceConstants(elems, funcs);
else
elems := Package.collectConstants(elems);
elems := Package.collectConstants(elems, funcs);
end if;

elems := Scalarize.scalarize(elems, name);
Expand Down
115 changes: 103 additions & 12 deletions Compiler/NFFrontEnd/NFPackage.mo
Expand Up @@ -31,6 +31,7 @@

encapsulated package NFPackage
import NFFlatten.Elements;
import NFFlatten.FunctionTree;

protected
import ExecStat.execStat;
Expand All @@ -44,6 +45,10 @@ protected
import NFInstNode.InstNode;
import Typing = NFTyping;
import Ceval = NFCeval;
import NFFunction.Function;
import NFClass.Class;
import Sections = NFSections;
import ClassTree = NFClassTree;

public
type Constants = ConstantsSetImpl.Tree;
Expand All @@ -69,6 +74,7 @@ public
public
function collectConstants
input output Elements elements;
input FunctionTree functions;
protected
list<tuple<ComponentRef, Binding>> comps = {};
Binding binding;
Expand All @@ -78,14 +84,9 @@ public
constants := List.fold(elements.components, collectComponentConstants, constants);
constants := Equation.foldExpList(elements.equations, collectExpConstants, constants);
constants := Equation.foldExpList(elements.initialEquations, collectExpConstants, constants);

for alg in elements.algorithms loop
constants := Statement.foldExpList(alg, collectExpConstants, constants);
end for;

for alg in elements.initialAlgorithms loop
constants := Statement.foldExpList(alg, collectExpConstants, constants);
end for;
constants := Statement.foldExpListList(elements.algorithms, collectExpConstants, constants);
constants := Statement.foldExpListList(elements.initialAlgorithms, collectExpConstants, constants);
constants := FunctionTree.fold(functions, collectFuncConstants, constants);

for c in Constants.listKeys(constants) loop
binding := Component.getBinding(InstNode.component(ComponentRef.node(c)));
Expand All @@ -99,12 +100,14 @@ public

function replaceConstants
input output Elements elements;
input output FunctionTree functions;
algorithm
elements.components := list(replaceComponentConstants(c) for c in elements.components);
elements.equations := list(Equation.mapExp(eq, replaceExpConstants) for eq in elements.equations);
elements.initialEquations := list(Equation.mapExp(eq, replaceExpConstants) for eq in elements.initialEquations);
elements.algorithms := list(Statement.mapExpList(s, replaceExpConstants) for s in elements.algorithms);
elements.initialAlgorithms := list(Statement.mapExpList(s, replaceExpConstants) for s in elements.initialAlgorithms);
elements.equations := Equation.mapExpList(elements.equations, replaceExpConstants);
elements.initialEquations := Equation.mapExpList(elements.initialEquations, replaceExpConstants);
elements.algorithms := Statement.mapExpListList(elements.algorithms, replaceExpConstants);
elements.initialAlgorithms := Statement.mapExpListList(elements.initialAlgorithms, replaceExpConstants);
functions := FunctionTree.map(functions, replaceFuncConstants);
execStat(getInstanceName());
end replaceConstants;

Expand Down Expand Up @@ -165,6 +168,50 @@ public
end match;
end collectExpConstants_traverser;

function collectFuncConstants
input Absyn.Path name;
input Function func;
input output Constants constants;
protected
Class cls;
array<InstNode> comps;
Sections sections;
algorithm
cls := InstNode.getClass(func.node);

() := match cls
case Class.INSTANCED_CLASS(elements = ClassTree.FLAT_TREE(components = comps),
sections = sections)
algorithm
for c in comps loop
constants := collectBindingConstants(
Component.getBinding(InstNode.component(c)), constants);
end for;

() := match sections
case Sections.SECTIONS()
algorithm
constants := Statement.foldExpListList(sections.algorithms, collectExpConstants, constants);
then
();

case Sections.EXTERNAL()
algorithm
for arg in sections.args loop
constants := collectExpConstants(arg, constants);
end for;
then
();

else ();
end match;
then
();

else ();
end match;
end collectFuncConstants;

function replaceComponentConstants
input output tuple<ComponentRef, Binding> component;
protected
Expand Down Expand Up @@ -209,5 +256,49 @@ public
end match;
end replaceExpConstants_traverser;

function replaceFuncConstants
input Absyn.Path name;
input output Function func;
protected
Class cls;
array<InstNode> comps;
Sections sections;
Component comp;
Binding binding, eval_binding;
algorithm
cls := InstNode.getClass(func.node);

() := match cls
case Class.INSTANCED_CLASS(elements = ClassTree.FLAT_TREE(components = comps),
sections = sections)
algorithm
for c in comps loop
comp := InstNode.component(c);
binding := Component.getBinding(comp);
eval_binding := replaceBindingConstants(binding);

if not referenceEq(binding, eval_binding) then
comp := Component.setBinding(eval_binding, comp);
InstNode.updateComponent(comp, c);
end if;
end for;

() := match sections
case Sections.SECTIONS()
algorithm
sections.algorithms := Statement.mapExpListList(sections.algorithms, replaceExpConstants);
cls.sections := sections;
InstNode.updateClass(cls, func.node);
then
();

else ();
end match;
then
();

end match;
end replaceFuncConstants;

annotation(__OpenModelica_Interface="frontend");
end NFPackage;
26 changes: 26 additions & 0 deletions Compiler/NFFrontEnd/NFStatement.mo
Expand Up @@ -125,6 +125,17 @@ public
end match;
end info;

function mapExpListList
input output list<list<Statement>> stmtl;
input MapFunc func;

partial function MapFunc
input output Expression exp;
end MapFunc;
algorithm
stmtl := list(mapExpList(s, func) for s in stmtl);
end mapExpListList;

function mapExpList
input output list<Statement> stmtl;
input MapFunc func;
Expand Down Expand Up @@ -204,6 +215,21 @@ public
end match;
end mapExp;

function foldExpListList<ArgT>
input list<list<Statement>> stmt;
input FoldFunc func;
input output ArgT arg;

partial function FoldFunc
input Expression exp;
input output ArgT arg;
end FoldFunc;
algorithm
for s in stmt loop
arg := foldExpList(s, func, arg);
end for;
end foldExpListList;

function foldExpList<ArgT>
input list<Statement> stmt;
input FoldFunc func;
Expand Down

0 comments on commit 12d1ede

Please sign in to comment.