Skip to content

Commit

Permalink
[NF] Minor fixes.
Browse files Browse the repository at this point in the history
- Flatten the class tree of functions before creating the actual
  Function, so that duplicate elements are handled correctly.
- Use Ceval when evaluating 'fill' instead of deprecated SimplifyExp.

Belonging to [master]:
  - OpenModelica/OMCompiler#2410
  - OpenModelica/OpenModelica-testsuite#937
  • Loading branch information
perost authored and OpenModelica-Hudson committed May 4, 2018
1 parent ace473c commit 89eacbb
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Compiler/NFFrontEnd/NFCall.mo
Expand Up @@ -1911,7 +1911,7 @@ protected
(arg, arg_ty, arg_var) := Typing.typeExp(arg, origin, info);

if arg_var <= Variability.STRUCTURAL_PARAMETER then
arg := SimplifyExp.simplifyExp(arg);
arg := Ceval.evalExp(arg);
arg_ty := Expression.typeOf(arg);
end if;

Expand Down
2 changes: 1 addition & 1 deletion Compiler/NFFrontEnd/NFClass.mo
Expand Up @@ -163,7 +163,7 @@ uniontype Class
ClassTree tree;
algorithm
tree := ClassTree.fromRecordConstructor(inputs, locals, out);
cls := EXPANDED_CLASS(tree, Modifier.NOMOD(), DEFAULT_PREFIXES, Restriction.FUNCTION());
cls := INSTANCED_CLASS(Type.UNKNOWN(), tree, Sections.EMPTY(), Restriction.FUNCTION());
end makeRecordConstructor;

function initExpandedClass
Expand Down
14 changes: 6 additions & 8 deletions Compiler/NFFrontEnd/NFClassTree.mo
Expand Up @@ -660,28 +660,26 @@ public
protected
LookupTree.Tree ltree = LookupTree.new();
Integer i = 1;
array<Mutable<InstNode>> comps;
array<InstNode> comps;
algorithm
comps := arrayCreateNoInit(listLength(inputs) + listLength(locals) + 1,
Mutable.create(InstNode.EMPTY_NODE()));
comps := arrayCreateNoInit(listLength(inputs) + listLength(locals) + 1, InstNode.EMPTY_NODE());

for ci in inputs loop
comps[i] := Mutable.create(ci);
comps[i] := ci;
ltree := addLocalElement(InstNode.name(ci), LookupTree.Entry.COMPONENT(i), tree, ltree);
i := i + 1;
end for;

for cl in locals loop
comps[i] := Mutable.create(cl);
comps[i] := cl;
ltree := addLocalElement(InstNode.name(cl), LookupTree.Entry.COMPONENT(i), tree, ltree);
i := i + 1;
end for;

comps[i] := Mutable.create(out);
comps[i] := out;
ltree := addLocalElement(InstNode.name(out), LookupTree.Entry.COMPONENT(i), tree, ltree);

tree := INSTANTIATED_TREE(ltree, listArray({}), comps, List.intRange(i),
listArray({}), listArray({}), DuplicateTree.new());
tree := FLAT_TREE(ltree, listArray({}), comps, listArray({}), DuplicateTree.new());
end fromRecordConstructor;

function mapRedeclareChains
Expand Down
4 changes: 4 additions & 0 deletions Compiler/NFFrontEnd/NFFlatten.mo
Expand Up @@ -1069,6 +1069,10 @@ algorithm
case Class.INSTANCED_CLASS(elements = cls_tree as ClassTree.FLAT_TREE(), sections = sections)
algorithm
for c in cls_tree.components loop
if InstNode.isEmpty(c) then
continue;
end if;

comp := InstNode.component(c);
funcs := collectTypeFuncs(Component.getType(comp), funcs);
binding := Component.getBinding(comp);
Expand Down
20 changes: 16 additions & 4 deletions Compiler/NFFrontEnd/NFFunction.mo
Expand Up @@ -300,7 +300,7 @@ uniontype Function
case CachedData.FUNCTION() then (fn_node, cache.specialBuiltin);
else algorithm
(fn_node, specialBuiltin) := instFunc2(ComponentRef.toPath(fn_ref), fn_node, info);
instFuncExpressions(fn_node);
//instFuncExpressions(fn_node);
then (fn_node, specialBuiltin);
end match;
end instFuncRef;
Expand Down Expand Up @@ -357,6 +357,8 @@ uniontype Function
algorithm
// fnNode := InstNode.setNodeType(NFInstNode.InstNodeType.ROOT_CLASS(), fnNode);
fnNode := Inst.instantiate(fnNode);
InstNode.cacheInitFunc(fnNode);
Inst.instExpressions(fnNode);
fnNode := Record.instConstructors(fnPath, fnNode, info);
then
(fnNode, false);
Expand All @@ -365,6 +367,8 @@ uniontype Function
algorithm
// fnNode := InstNode.setNodeType(NFInstNode.InstNodeType.ROOT_CLASS(), fnNode);
fnNode := Inst.instantiate(fnNode);
InstNode.cacheInitFunc(fnNode);
Inst.instExpressions(fnNode);
fnNode := Record.instOperatorFunctions(fnNode, info);
then
(fnNode, false);
Expand All @@ -385,6 +389,10 @@ uniontype Function
algorithm
fnNode := InstNode.setNodeType(NFInstNode.InstNodeType.ROOT_CLASS(), fnNode);
fnNode := Inst.instantiate(fnNode);
// Set up an empty function cache to signal that this function is
// currently being instantiated, so recursive functions can be handled.
InstNode.cacheInitFunc(fnNode);
Inst.instExpressions(fnNode);
fn := Function.new(fnPath, fnNode);
specialBuiltin := isSpecialBuiltin(fn);
fnNode := InstNode.cacheAddFunc(fnNode, fn, specialBuiltin);
Expand Down Expand Up @@ -1151,17 +1159,21 @@ protected
input output list<InstNode> locals = {};
protected
Class cls;
array<Mutable<InstNode>> comps;
array<InstNode> comps;
InstNode n;
algorithm
Error.assertion(InstNode.isClass(node), getInstanceName() + " got non-class node", sourceInfo());
cls := InstNode.getClass(node);

() := match cls
case Class.EXPANDED_CLASS(elements = ClassTree.INSTANTIATED_TREE(components = comps))
case Class.INSTANCED_CLASS(elements = ClassTree.FLAT_TREE(components = comps))
algorithm
for i in arrayLength(comps):-1:1 loop
n := Mutable.access(comps[i]);
n := comps[i];

if InstNode.isEmpty(n) then
continue;
end if;

// Sort the components based on their direction.
() := match paramDirection(n)
Expand Down
23 changes: 23 additions & 0 deletions Compiler/NFFrontEnd/NFInstNode.mo
Expand Up @@ -129,6 +129,20 @@ uniontype CachedData
output array<CachedData> cache = arrayCreate(NUMBER_OF_CACHES, NO_CACHE());
end empty;

function initFunc
input array<CachedData> caches;
protected
CachedData func_cache;
algorithm
func_cache := getFuncCache(caches);
func_cache := match func_cache
case NO_CACHE() then FUNCTION({}, false, false);
case FUNCTION() then func_cache;
end match;

setFuncCache(caches, func_cache);
end initFunc;

function addFunc
input Function fn;
input Boolean specialBuiltin;
Expand Down Expand Up @@ -913,6 +927,15 @@ uniontype InstNode
end match;
end resolveOuter;

function cacheInitFunc
input output InstNode node;
algorithm
() := match node
case CLASS_NODE() algorithm CachedData.initFunc(node.caches); then ();
else algorithm Error.assertion(false, getInstanceName() + " got node without cache", sourceInfo()); then fail();
end match;
end cacheInitFunc;

function cacheAddFunc
input output InstNode node;
input Function fn;
Expand Down
6 changes: 4 additions & 2 deletions Compiler/NFFrontEnd/NFPackage.mo
Expand Up @@ -178,8 +178,10 @@ public
sections = sections)
algorithm
for c in comps loop
constants := collectBindingConstants(
Component.getBinding(InstNode.component(c)), constants);
if not InstNode.isEmpty(c) then
constants := collectBindingConstants(
Component.getBinding(InstNode.component(c)), constants);
end if;
end for;

() := match sections
Expand Down
17 changes: 11 additions & 6 deletions Compiler/NFFrontEnd/NFRecord.mo
Expand Up @@ -148,7 +148,7 @@ function collectRecordParams
protected

Class cls;
array<Mutable<InstNode>> components;
array<InstNode> components;
InstNode n;
Component comp;
algorithm
Expand All @@ -157,10 +157,15 @@ algorithm
cls := InstNode.getClass(recNode);

() := match cls
case Class.EXPANDED_CLASS(elements = ClassTree.INSTANTIATED_TREE(components = components))
case Class.INSTANCED_CLASS(elements = ClassTree.FLAT_TREE(components = components))
algorithm
for i in arrayLength(components):-1:1 loop
n := Mutable.access(components[i]);
n := components[i];

if InstNode.isEmpty(n) then
continue;
end if;

comp := InstNode.component(n);

if InstNode.isProtected(n) or
Expand All @@ -187,7 +192,7 @@ function instOperatorFunctions
input SourceInfo info;
protected
Class cls;
array<Mutable<InstNode>> mclss;
array<InstNode> mclss;
InstNode op;
Absyn.Path path;
list<Function> allfuncs = {}, funcs;
Expand All @@ -196,9 +201,9 @@ algorithm
cls := InstNode.getClass(node);

() := match cls
case Class.EXPANDED_CLASS(elements = ClassTree.INSTANTIATED_TREE(classes = mclss)) algorithm
case Class.INSTANCED_CLASS(elements = ClassTree.FLAT_TREE(classes = mclss)) algorithm
for i in arrayLength(mclss):-1:1 loop
op := Mutable.access(mclss[i]);
op := mclss[i];
path := InstNode.scopePath(op);
Function.instFunc2(path, op, info);
funcs := Function.getCachedFuncs(op);
Expand Down

0 comments on commit 89eacbb

Please sign in to comment.