Skip to content

Commit

Permalink
[NF] Add list of unused inputs to DAE.FUNCTION.
Browse files Browse the repository at this point in the history
  • Loading branch information
perost committed Mar 6, 2020
1 parent 7d77258 commit 2b9188a
Show file tree
Hide file tree
Showing 14 changed files with 303 additions and 111 deletions.
48 changes: 21 additions & 27 deletions OMCompiler/Compiler/BackEnd/BackendDAECreate.mo
Expand Up @@ -3497,34 +3497,28 @@ algorithm
end matchcontinue;
end renameFunctionParameter;

protected function renameFunctionParameter1"
author:Waurich TUD 2014-10"
input tuple<DAE.AvlTreePathFunction.Key,DAE.AvlTreePathFunction.Value> funcIn;
output tuple<DAE.AvlTreePathFunction.Key,DAE.AvlTreePathFunction.Value> funcOut;
protected function renameFunctionParameter1
input tuple<DAE.AvlTreePathFunction.Key, DAE.AvlTreePathFunction.Value> funcIn;
output tuple<DAE.AvlTreePathFunction.Key, DAE.AvlTreePathFunction.Value> funcOut;
protected
DAE.AvlTreePathFunction.Key key;
DAE.AvlTreePathFunction.Value value;
String pathName;
DAE.Function fn;
algorithm
funcOut := matchcontinue(funcIn)
local
Boolean pPref;
Boolean isImpure;
String pathName;
Absyn.Path path;
DAE.AvlTreePathFunction.Key key;
DAE.ElementSource source;
DAE.Function func;
DAE.InlineType iType;
DAE.Type type_;
SCode.Visibility vis;
list<DAE.FunctionDefinition> functions;
Option<SCode.Comment> comment;
case((key,SOME(DAE.FUNCTION(path=path,functions=functions,type_=type_,visibility=vis,partialPrefix=pPref,isImpure=isImpure,inlineType=iType,source=source,comment=comment))))
equation
pathName = AbsynUtil.pathString(path);
pathName = Util.stringReplaceChar(pathName,".","_")+"_";
functions = List.map1(functions,renameFunctionParameter2,pathName);
then((key,SOME(DAE.FUNCTION(path,functions,type_,vis,pPref,isImpure,iType,source,comment))));
else
then funcIn;
end matchcontinue;
(key, value) := funcIn;

funcOut := match value
case SOME(fn as DAE.FUNCTION())
algorithm
pathName := AbsynUtil.pathString(fn.path);
pathName := Util.stringReplaceChar(pathName, ".", "_") + "_";
fn.functions := list(renameFunctionParameter2(fn_def, pathName) for fn_def in fn.functions);
then
(key, SOME(fn));

else funcIn;
end match;
end renameFunctionParameter1;

protected function renameFunctionParameter2"
Expand Down
2 changes: 1 addition & 1 deletion OMCompiler/Compiler/BackEnd/Differentiate.mo
Expand Up @@ -2735,7 +2735,7 @@ algorithm

isImpure = DAEUtil.getFunctionImpureAttribute(func);
dinl = DAEUtil.getFunctionInlineType(func);
dfunc = DAE.FUNCTION(dpath, {DAE.FUNCTION_DEF(funcbodyDer)}, dtp, visibility, false, isImpure, dinl, DAE.emptyElementSource, NONE());
dfunc = DAE.FUNCTION(dpath, {DAE.FUNCTION_DEF(funcbodyDer)}, dtp, visibility, false, isImpure, dinl, {}, DAE.emptyElementSource, NONE());
then (dfunc, functions, blst);

else
Expand Down
63 changes: 26 additions & 37 deletions OMCompiler/Compiler/BackEnd/EvaluateFunctions.mo
Expand Up @@ -56,7 +56,6 @@ protected import ExpressionSimplify;
protected import Flags;
protected import List;
protected import RemoveSimpleEquations;
protected import SCode;
protected import Util;


Expand Down Expand Up @@ -1541,52 +1540,42 @@ algorithm
end match;
end generateProtectedElements;

protected function updateFunctionBody "udpates the function with the new elementsm update the type and create a new path name.
author:Waurich TUD 2014-04"
protected function updateFunctionBody
"Updates the function with the new elements, updates the type, and creates a
new path name."
input DAE.Function funcIn;
input list<DAE.Element> body;
input Integer idx;
input list<DAE.Element> outputs;
input list<DAE.Element> origOutputs;
output DAE.Function funcOut;
output DAE.Function funcOut = funcIn;
output Absyn.Path pathOut;
algorithm
(funcOut,pathOut) := match(funcIn, body, idx, outputs, origOutputs)
(funcOut, pathOut) := match funcOut
local
String s;
list<String> chars;
Absyn.Path path;
list<DAE.FunctionDefinition> funcs;
DAE.Type typ;
Boolean pP, iI;
DAE.InlineType iType;
DAE.ElementSource source;
DAE.Function func;
Option<SCode.Comment> comment;
SCode.Visibility visibility;
case (DAE.FUNCTION(path,_,typ,visibility,pP,iI,iType,source,comment),_,_,_,_)
equation
//print("the pathname before: "+AbsynUtil.pathString(path)+"\n");
//print("THE FUNCTION BEFORE \n"+DAEDump.dumpFunctionStr(funcIn)+"\n");
// assemble the path-name
s = AbsynUtil.pathString(path);
chars = stringListStringChar(s);
chars = listDelete(chars, 1);
s = stringCharListString(chars);
path = AbsynUtil.stringPath(s+"_eval"+intString(idx));
// update the type
//print("the old type: "+Types.unparseType(typ)+"\n");
typ = updateFunctionType(typ,outputs,origOutputs);
//print("the new type: "+Types.unparseType(typ)+"\n");
func = DAE.FUNCTION(path,{DAE.FUNCTION_DEF(body)},typ,visibility,pP,iI,iType,source,comment);
//print("THE FUNCTION AFTER \n"+DAEDump.dumpFunctionStr(func)+"\n");
//print("the pathname after: "+AbsynUtil.pathString(path)+"\n");
then (func,path);

case DAE.FUNCTION()
algorithm
// Update the path.
s := AbsynUtil.pathLastIdent(funcOut.path);
s := s + "_eval" + intString(idx);
funcOut.path := AbsynUtil.pathReplaceIdent(AbsynUtil.makeNotFullyQualified(funcOut.path), s);

// Update the type.
funcOut.type_ := updateFunctionType(funcOut.type_, outputs, origOutputs);

// Update the function definition.
funcOut.functions := {DAE.FUNCTION_DEF(body)};
then
(funcOut, funcOut.path);

else
equation
print("updateFunctionBody failed \n");
then
fail();
algorithm
Error.assertion(false, getInstanceName() + " failed", sourceInfo());
then
fail();

end match;
end updateFunctionBody;

Expand Down
1 change: 1 addition & 0 deletions OMCompiler/Compiler/FrontEnd/DAE.mo
Expand Up @@ -404,6 +404,7 @@ public uniontype Function
Boolean partialPrefix "MetaModelica extension";
Boolean isImpure "Modelica 3.3 impure/pure, by default isImpure = false all the time only if prefix *impure* function is specified";
InlineType inlineType;
list<Integer> unusedInputs "The indices of any inputs not used in the function.";
ElementSource source "the origin of the component/equation/algorithm" ;
Option<SCode.Comment> comment;
end FUNCTION;
Expand Down
66 changes: 26 additions & 40 deletions OMCompiler/Compiler/FrontEnd/Inline.mo
Expand Up @@ -130,48 +130,34 @@ public function inlineCallsInFunctions
"inlines calls in DAEElements"
input list<DAE.Function> inElementList;
input Functiontuple inFunctions;
input list<DAE.Function> iAcc;
output list<DAE.Function> outElementList;
protected
list<DAE.Element> body;
DAE.ExternalDecl ext_decl;
DAE.FunctionDefinition fn_def;
list<DAE.FunctionDefinition> fn_defs;
algorithm
outElementList := matchcontinue(inElementList,inFunctions,iAcc)
local
list<DAE.Function> cdr;
list<DAE.Element> elist,elist_1;
DAE.Function el,res;
DAE.Type t;
Boolean partialPrefix, isImpure;
Absyn.Path p;
DAE.ExternalDecl ext;
DAE.InlineType inlineType;
list<DAE.FunctionDefinition> funcDefs;
DAE.ElementSource source;
Option<SCode.Comment> cmt;
SCode.Visibility visibility;

case({},_,_) then listReverse(iAcc);

case (DAE.FUNCTION(p,DAE.FUNCTION_DEF(body = elist)::funcDefs,t,visibility,partialPrefix,isImpure,inlineType,source,cmt) :: cdr,_,_)
equation
(elist_1,true)= inlineDAEElements(elist,inFunctions,{},false);
res = DAE.FUNCTION(p,DAE.FUNCTION_DEF(elist_1)::funcDefs,t,visibility,partialPrefix,isImpure,inlineType,source,cmt);
then
inlineCallsInFunctions(cdr,inFunctions,res::iAcc);
// external functions
case (DAE.FUNCTION(p,DAE.FUNCTION_EXT(elist,ext)::funcDefs,t,visibility,partialPrefix,isImpure,inlineType,source,cmt) :: cdr,_,_)
equation
(elist_1,true)= inlineDAEElements(elist,inFunctions,{},false);
res = DAE.FUNCTION(p,DAE.FUNCTION_EXT(elist_1,ext)::funcDefs,t,visibility,partialPrefix,isImpure,inlineType,source,cmt);
then
inlineCallsInFunctions(cdr,inFunctions,res::iAcc);

case(el :: cdr,_,_)
then
inlineCallsInFunctions(cdr,inFunctions,el::iAcc);
else
equation
Error.addMessage(Error.INTERNAL_ERROR,{"Inline.inlineCallsInFunctions failed"});
then fail();
end matchcontinue;
outElementList := list(
matchcontinue fn
case DAE.FUNCTION(functions = (fn_def as DAE.FUNCTION_DEF()) :: fn_defs)
algorithm
(body, true) := inlineDAEElements(fn_def.body, inFunctions, {}, false);
fn_def.body := body;
fn.functions := fn_def :: fn_defs;
then
fn;

case DAE.FUNCTION(functions = (fn_def as DAE.FUNCTION_EXT()) :: fn_defs)
algorithm
(body, true) := inlineDAEElements(fn_def.body, inFunctions, {}, false);
fn_def.body := body;
fn.functions := fn_def :: fn_defs;
then
fn;

else fn;
end matchcontinue
for fn in inElementList);
end inlineCallsInFunctions;


Expand Down
8 changes: 4 additions & 4 deletions OMCompiler/Compiler/FrontEnd/InstFunction.mo
Expand Up @@ -269,7 +269,7 @@ algorithm
case (cache,env,ih,mod,pre,(c as SCode.CLASS(name = n,restriction = SCode.R_RECORD(_), partialPrefix = pPrefix)),inst_dims)
equation
(cache,c,cenv) = Lookup.lookupRecordConstructorClass(cache,env,Absyn.IDENT(n));
(cache,env,ih,{DAE.FUNCTION(fpath,_,ty1,_,_,_,_,source,_)}) = implicitFunctionInstantiation2(cache,cenv,ih,mod,pre,c,inst_dims,true);
(cache,env,ih,{DAE.FUNCTION(path = fpath, type_ = ty1, source = source)}) = implicitFunctionInstantiation2(cache,cenv,ih,mod,pre,c,inst_dims,true);
// fpath = AbsynUtil.makeFullyQualified(fpath);
fun = DAE.RECORD_CONSTRUCTOR(fpath,ty1,source);
cache = InstUtil.addFunctionsToDAE(cache, {fun}, pPrefix);
Expand Down Expand Up @@ -383,7 +383,7 @@ algorithm
InstUtil.checkFunctionInputUsed(daeElts,NONE(),AbsynUtil.pathString(fpath));
end if;
then
(cache,env_1,ih,{DAE.FUNCTION(fpath,DAE.FUNCTION_DEF(list(e for e guard not DAEUtil.isComment(e) in daeElts))::derFuncs,ty1,visibility,partialPrefixBool,isImpure,inlineType,source,SOME(cmt))});
(cache,env_1,ih,{DAE.FUNCTION(fpath,DAE.FUNCTION_DEF(list(e for e guard not DAEUtil.isComment(e) in daeElts))::derFuncs,ty1,visibility,partialPrefixBool,isImpure,inlineType,{},source,SOME(cmt))});

// External functions should also have their type in env, but no dae.
case (cache,env,ih,mod,pre,(c as SCode.CLASS(partialPrefix=partialPrefix, prefixes=SCode.PREFIXES(visibility=visibility), name = n,restriction = (restr as SCode.R_FUNCTION(SCode.FR_EXTERNAL_FUNCTION(isImpure))),
Expand Down Expand Up @@ -419,15 +419,15 @@ algorithm
partialPrefixBool = SCodeUtil.partialBool(partialPrefix);
InstUtil.checkExternalFunction(daeElts,extdecl,AbsynUtil.pathString(fpath));
then
(cache,env_1,ih,{DAE.FUNCTION(fpath,DAE.FUNCTION_EXT(daeElts,extdecl)::derFuncs,ty1,visibility,partialPrefixBool,isImpure,DAE.NO_INLINE(),source,SOME(cmt))});
(cache,env_1,ih,{DAE.FUNCTION(fpath,DAE.FUNCTION_EXT(daeElts,extdecl)::derFuncs,ty1,visibility,partialPrefixBool,isImpure,DAE.NO_INLINE(),{},source,SOME(cmt))});

// Instantiate overloaded functions
case (cache,env,ih,_,pre,(SCode.CLASS(name = n, prefixes=SCode.PREFIXES(visibility=visibility), restriction = (SCode.R_FUNCTION(SCode.FR_NORMAL_FUNCTION(isImpure))),
classDef = SCode.OVERLOAD(pathLst = funcnames),cmt=cmt)),_,_)
equation
(cache,env,ih,resfns) = instOverloadedFunctions(cache,env,ih,pre,funcnames,inClass.info) "Overloaded functions" ;
(cache,fpath) = Inst.makeFullyQualifiedIdent(cache,env,n);
resfns = DAE.FUNCTION(fpath,{DAE.FUNCTION_DEF({})},DAE.T_UNKNOWN_DEFAULT,visibility,true,isImpure,DAE.NO_INLINE(),DAE.emptyElementSource,SOME(cmt))::resfns;
resfns = DAE.FUNCTION(fpath,{DAE.FUNCTION_DEF({})},DAE.T_UNKNOWN_DEFAULT,visibility,true,isImpure,DAE.NO_INLINE(),{},DAE.emptyElementSource,SOME(cmt))::resfns;
then
(cache,env,ih,resfns);

Expand Down
19 changes: 19 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFBinding.mo
Expand Up @@ -660,6 +660,25 @@ public
end match;
end mapExpShallow;

function foldExp<ArgT>
input Binding binding;
input FoldFunc foldFn;
input output ArgT arg;

partial function FoldFunc
input Expression exp;
input output ArgT arg;
end FoldFunc;
algorithm
arg := match binding
case UNTYPED_BINDING() then Expression.fold(binding.bindingExp, foldFn, arg);
case TYPED_BINDING() then Expression.fold(binding.bindingExp, foldFn, arg);
case FLAT_BINDING() then Expression.fold(binding.bindingExp, foldFn, arg);
case CEVAL_BINDING() then Expression.fold(binding.bindingExp, foldFn, arg);
else arg;
end match;
end foldExp;

function containsExp
input Binding binding;
input PredFunc predFn;
Expand Down
1 change: 1 addition & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFClass.mo
Expand Up @@ -270,6 +270,7 @@ uniontype Class
case Class.INSTANCED_CLASS() then cls.elements;
case Class.INSTANCED_BUILTIN() then cls.elements;
case Class.TYPED_DERIVED() then classTree(InstNode.getClass(cls.baseClass));
else ClassTree.EMPTY_TREE();
end match;
end classTree;

Expand Down
47 changes: 47 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFClassTree.mo
Expand Up @@ -1234,6 +1234,53 @@ public
end match;
end applyComponents;

function foldComponents<ArgT>
input ClassTree tree;
input FuncT func;
input output ArgT arg;

partial function FuncT
input InstNode component;
input output ArgT arg;
end FuncT;
algorithm
() := match tree
case PARTIAL_TREE()
algorithm
for c in tree.components loop
arg := func(c, arg);
end for;
then
();

case EXPANDED_TREE()
algorithm
for c in tree.components loop
arg := func(c, arg);
end for;
then
();

case INSTANTIATED_TREE()
algorithm
for c in tree.components loop
arg := func(Mutable.access(c), arg);
end for;
then
();

case FLAT_TREE()
algorithm
for c in tree.components loop
arg := func(c, arg);
end for;
then
();

else ();
end match;
end foldComponents;

function classCount
input ClassTree tree;
output Integer count;
Expand Down
12 changes: 12 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFComponentRef.mo
Expand Up @@ -178,6 +178,18 @@ public
CREF(node = node) := cref;
end node;

function containsNode
input ComponentRef cref;
input InstNode node;
output Boolean res;
algorithm
res := match cref
case CREF()
then InstNode.refEqual(cref.node, node) or containsNode(cref.restCref, node);
else false;
end match;
end containsNode;

function nodeType
input ComponentRef cref;
output Type ty;
Expand Down

0 comments on commit 2b9188a

Please sign in to comment.