Skip to content

Commit

Permalink
-Partly fixed instantiation of recursive functions (need to check in …
Browse files Browse the repository at this point in the history
…so others can continue working on MultiBody). Still some problem left with invalid cache.

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@4905 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Peter Aronsson committed Feb 2, 2010
1 parent 2c0a421 commit 0cd7fe7
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 31 deletions.
3 changes: 2 additions & 1 deletion Compiler/Ceval.mo
Expand Up @@ -1101,13 +1101,14 @@ algorithm

// adrpo: 2009-11-17 re-enable the Cevalfunc after dealing with record constructors!
case (cache,env,(e as DAE.CALL(path = funcpath,expLst = expl,builtin = builtin)),vallst,impl,st,dim,msg)
local Env.Cache garbageCache;
equation
false = RTOpts.debugFlag("noevalfunc");
failure(cevalIsExternalObjectConstructor(cache,funcpath,env));
// make sure is NOT used for records !
(cache,sc as SCode.CLASS(_,false,_,SCode.R_FUNCTION(),cdef ),env1) =
Lookup.lookupClass(cache,env,funcpath,true);
(cache,env1,_,daeList) =
(garbageCache,env1,_,daeList) =
Inst.implicitFunctionInstantiation(
cache,
env1,
Expand Down
71 changes: 57 additions & 14 deletions Compiler/Env.mo
Expand Up @@ -84,14 +84,16 @@ public import Absyn;
public import ClassInf;
public import DAE;
public import SCode;
public import HashTable5;

public
type Ident = String " An identifier is just a string " ;

public uniontype Cache
record CACHE
Option<EnvCache>[:] envCache "The cache consists of environments from which classes can be found";
Option<EnvCache>[:] envCache "The cache contains of environments from which classes can be found";
Option<Env> initialEnv "and the initial environment";
HashTable5.HashTable instantiatedFuncs "and a hashtable to indicated already instantiated functions (to break inst. of recursive function calls)";
end CACHE;
end Cache;

Expand Down Expand Up @@ -204,11 +206,12 @@ public function emptyCache
"returns an empty cache"
output Cache cache;
protected
Option<EnvCache>[:] arr;
Option<EnvCache>[:] arr; HashTable5.HashTable instFuncs;
algorithm
//print("EMPTYCACHE\n");
arr := listArray({NONE});
cache := CACHE(arr,NONE);
instFuncs := HashTable5.emptyHashTable();
cache := CACHE(arr,NONE,instFuncs);
end emptyCache;

public
Expand Down Expand Up @@ -1165,7 +1168,7 @@ public function getCachedInitialEnv "get the initial environment from the cache"
algorithm
env := matchcontinue(cache)
//case (_) then fail();
case (CACHE(_,SOME(env))) equation
case (CACHE(_,SOME(env),_)) equation
// print("getCachedInitialEnv\n");
then env;
end matchcontinue;
Expand All @@ -1179,12 +1182,47 @@ algorithm
outCache := matchcontinue(inCache,env)
local
Option<EnvCache>[:] envCache;
HashTable5.HashTable ef;

case (CACHE(envCache,_),env) equation
case (CACHE(envCache,_,ef),env) equation
// print("setCachedInitialEnv\n");
then CACHE(envCache,SOME(env));
then CACHE(envCache,SOME(env),ef);
end matchcontinue;
end setCachedInitialEnv;

public function addCachedInstFunc "adds the FQ path to the set of instantiated functions"
input Cache inCache;
input Absyn.Path func "fully qualified function name";
output Cache outCache;
algorithm
outCache := matchcontinue(inCache,func)
local
Option<EnvCache>[:] envCache;
HashTable5.HashTable ef;
Absyn.ComponentRef cr;
Option<Env> ienv;

case (CACHE(envCache,ienv,ef),func) equation
cr = Absyn.pathToCref(func);
ef = HashTable5.add((cr,0),ef);
then CACHE(envCache,ienv,ef);
end matchcontinue;
end addCachedInstFunc;

function getCachedInstFunc "returns the integer value 0 if the FQ function is in the set of already instantiated functions. If not, this function fails"
input Cache inCache;
input Absyn.Path path;
output Integer res;
algorithm
res := matchcontinue(inCache,path)
local HashTable5.HashTable ef; Absyn.ComponentRef cr;
Integer v;
case(CACHE(instantiatedFuncs=ef),path) equation
cr = Absyn.pathToCref(path);
v = HashTable5.get(cr,ef);
then v;
end matchcontinue;
end getCachedInstFunc;

public function cacheGet "Get an environment from the cache."
input Absyn.Path scope;
Expand All @@ -1194,7 +1232,8 @@ public function cacheGet "Get an environment from the cache."
algorithm
env:= matchcontinue(scope,path,cache)
local CacheTree tree; Option<EnvCache>[:] arr;
case (scope,path,CACHE(arr ,_))
HashTable5.HashTable ef;
case (scope,path,CACHE(arr ,_,ef))
equation
true = OptManager.getOption("envCache");
SOME(ENVCACHE(tree)) = arr[1];
Expand All @@ -1215,18 +1254,19 @@ algorithm
local CacheTree tree;
Option<Env> ie;
Option<EnvCache>[:] arr;
HashTable5.HashTable ef;
case(_,inCache,env) equation
false = OptManager.getOption("envCache");
then inCache;

case (fullpath,CACHE(arr,ie),env)
case (fullpath,CACHE(arr,ie,ef),env)
equation
NONE = arr[1];
tree = cacheAddEnv(fullpath,CACHETREE("$global",emptyEnv,{}),env);
//print("Adding ");print(Absyn.pathString(fullpath));print(" to empty cache\n");
arr = arrayUpdate(arr,1,SOME(ENVCACHE(tree)));
then CACHE(arr,ie);
case (fullpath,CACHE(arr,ie),env)
then CACHE(arr,ie,ef);
case (fullpath,CACHE(arr,ie,ef),env)
equation
SOME(ENVCACHE(tree))=arr[1];
// print(" about to Adding ");print(Absyn.pathString(fullpath));print(" to cache:\n");
Expand All @@ -1235,7 +1275,7 @@ algorithm
//print("Adding ");print(Absyn.pathString(fullpath));print(" to cache\n");
//print(printCacheStr(CACHE(SOME(ENVCACHE(tree)),ie)));
arr = arrayUpdate(arr,1,SOME(ENVCACHE(tree)));
then CACHE(arr,ie);
then CACHE(arr,ie,ef);
case (_,_,_) equation
true = OptManager.getOption("envCache");
print("cacheAdd failed\n");
Expand Down Expand Up @@ -1481,14 +1521,17 @@ algorithm
str := matchcontinue(cache)
local CacheTree tree;
Option<EnvCache>[:] arr;
case CACHE(arr,_)
local String s;
HashTable5.HashTable ef;
case CACHE(arr,_,ef)
local String s,s2;
equation
SOME(ENVCACHE(tree)) = arr[1];
s = printCacheTreeStr(tree,1);
str = Util.stringAppendList({"Cache:\n",s,"\n"});
s2 = HashTable5.dumpHashTableStr(ef);
str = str +& "\nInstantiated funcs: " +& s2 +&"\n";
then str;
case CACHE(_,_) then "EMPTY CACHE\n";
case CACHE(_,_,_) then "EMPTY CACHE\n";
end matchcontinue;
end printCacheStr;

Expand Down
6 changes: 6 additions & 0 deletions Compiler/HashTable5.mo
Expand Up @@ -54,6 +54,12 @@ algorithm
res := Absyn.crefEqual(key1,key2);
end keyEqual;

public function dumpHashTableStr "dump hashtable to a string"
input HashTable t;
output String str;
algorithm
str := "HashTable:\n"+&Util.stringDelimitList(Util.listMap(hashTableList(t),dumpTuple),"\n")+&"\n";
end dumpHashTableStr;

protected function dumpHashTable
input HashTable t;
Expand Down
3 changes: 2 additions & 1 deletion Compiler/Inst.mo
Expand Up @@ -9990,13 +9990,14 @@ algorithm
Env.Cache cache;
InstanceHierarchy ih;
list<SCode.Annotation> annotationLst;
Env.Cache garbageCache;

/* The function type can be determined without the body. Annotations need to be preserved though. */
case (cache,env,ih,SCode.CLASS(name = id,partialPrefix = p,encapsulatedPrefix = e,restriction = r,
classDef = SCode.PARTS(elementLst = elts,annotationLst=annotationLst,externalDecl=extDecl)))
equation
stripped_class = SCode.CLASS(id,p,e,r,SCode.PARTS(elts,{},{},{},{},extDecl,annotationLst,NONE()));
(cache,env_1,ih,_) = implicitFunctionInstantiation(cache,env,ih, DAE.NOMOD(), Prefix.NOPRE(), Connect.emptySet, stripped_class, {});
(garbageCache,env_1,ih,_) = implicitFunctionInstantiation(cache,env,ih, DAE.NOMOD(), Prefix.NOPRE(), Connect.emptySet, stripped_class, {});
then
(cache,env_1,ih);

Expand Down
8 changes: 5 additions & 3 deletions Compiler/Lookup.mo
Expand Up @@ -1735,13 +1735,15 @@ algorithm

/* Found function */
case (cache,Env.CLASS((cdef as SCode.CLASS(_,_,_,restr,_)),cenv),env,id)
local SCode.Restriction restr;
local SCode.Restriction restr; Env.Cache garbageCache;
equation
true = SCode.isFunctionOrExtFunction(restr);
(cache,env_1,_,_) =

/* Since function is added to cache, but dae here is not propagated, throw away cache from this call */
(garbageCache ,env_1,_,_) =
Inst.implicitFunctionInstantiation(
cache,cenv,InstanceHierarchy.emptyInstHierarchy,
DAE.NOMOD(), Prefix.NOPRE(), Connect.emptySet, cdef, {});
DAE.NOMOD(), Prefix.NOPRE(), Connect.emptySet, cdef, {});
(cache,ty,env_3) = lookupTypeInEnv(cache,env_1, Absyn.IDENT(id));
then
(cache,ty,env_3);
Expand Down
36 changes: 24 additions & 12 deletions Compiler/Static.mo
Expand Up @@ -7863,7 +7863,7 @@ algorithm
Debug.fprintln("sei", "generate_compiled_function: elaborated");
(cache,cls,env_1) = Lookup.lookupClass(cache,env, path, false) " Inst.instantiate_implicit(p\') => d & message" ;
Debug.fprintln("sei", "generate_compiled_function: class looked up");
(cache,env_2,_,d) = Inst.implicitFunctionInstantiation(cache, env_1, InstanceHierarchy.emptyInstHierarchy,
(_,env_2,_,d) = Inst.implicitFunctionInstantiation(cache, env_1, InstanceHierarchy.emptyInstHierarchy,
DAE.NOMOD(), Prefix.NOPRE(), Connect.emptySet, cls, {});
Debug.fprintln("sei", "generate_compiled_function: function instantiated");
Print.clearBuf();
Expand Down Expand Up @@ -8335,7 +8335,7 @@ algorithm
(call_exp,prop_1) = vectorizeCall(DAE.CALL(fn,args_2,false,false,tp,DAE.NO_INLINE), outtype, vect_dims, newslots2, prop);
//print(" RECORD CONSTRUCT("+&Absyn.pathString(fn)+&")= "+&Exp.printExpStr(call_exp)+&"\n");
/* Instantiate the function and add to dae function tree*/
dae = instantiateDaeFunction(cache,recordEnv,fn,false/*record constructor never builtin*/,SOME(recordCl));
(cache,dae) = instantiateDaeFunction(cache,recordEnv,fn,false/*record constructor never builtin*/,SOME(recordCl));
dae = DAEUtil.joinDaes(dae,dae1);
then
(cache,call_exp,prop_1,dae);
Expand Down Expand Up @@ -8407,7 +8407,7 @@ algorithm
(call_exp,prop_1) = vectorizeCall(callExp, restype, vect_dims, slots2, prop);

/* Instantiate the function and add to dae function tree*/
dae2 = instantiateDaeFunction(cache,env,fn_1,builtin,NONE);
(cache,dae2) = instantiateDaeFunction(cache,env,fn_1,builtin,NONE);
dae = DAEUtil.joinDaes(dae1,dae2);
then
(cache,call_exp,prop_1,dae);
Expand Down Expand Up @@ -8469,37 +8469,49 @@ functiontree of a newly created dae"
input Absyn.Path name;
input Boolean builtin "builtin functions create empty dae";
input option<SCode.Class> clOpt "if not present, looked up by name in environment";
output Env.Cache outCache;
output DAE.DAElist outDae;
algorithm
outDae := matchcontinue(inCache,env,name,builtin,clOpt)
(outCache,outDae) := matchcontinue(inCache,env,name,builtin,clOpt)
local Env.Cache cache;
SCode.Class cl; DAE.DAElist dae;
String id,id2;
/* Builtin functions skipped*/
case(cache,env,name,true,_) then DAEUtil.emptyDae;
case(cache,env,name,true,_) then (cache,DAEUtil.emptyDae);

/* External object functions skipped*/
case(cache,env,name,_,_) equation
(_,true) = isExternalObjectFunction(cache,env,name);
then DAEUtil.emptyDae;
then (cache,DAEUtil.emptyDae);

/* Recursive calls skipped */
/* Recursive calls (by looking at envinronment) skipped */
case(cache,env,name,false,NONE) equation
true = Absyn.pathSuffixOf(name,Env.getEnvName(env));
then DAEUtil.emptyDae;

then (cache,DAEUtil.emptyDae);

/* Recursive calls (by looking in cache) skipped */
case(cache,env,name,false,_) equation
(cache,cl,env) = Lookup.lookupClass(cache,env,name,false);
(cache,name) = Inst.makeFullyQualified(cache,env,name);
_ = Env.getCachedInstFunc(cache,name);
then (cache,DAEUtil.emptyDae);


/* Class must be looked up*/
case(cache,env,name,false,NONE) equation
(cache,cl,env) = Lookup.lookupClass(cache,env,name,false);
(cache,name) = Inst.makeFullyQualified(cache,env,name);
cache = Env.addCachedInstFunc(cache,name);
(cache,env,_,dae) = Inst.implicitFunctionInstantiation(cache,env,InstanceHierarchy.emptyInstHierarchy,DAE.NOMOD(),Prefix.NOPRE(),Connect.emptySet,cl,{});
dae = DAEUtil.addDaeFunction(dae);
then dae;
then (cache,dae);

/* class already available*/
case(cache,env,name,false,SOME(cl)) equation
(cache,env,_,dae) = Inst.implicitFunctionInstantiation(cache,env,InstanceHierarchy.emptyInstHierarchy,DAE.NOMOD(),Prefix.NOPRE(),Connect.emptySet,cl,{});
(cache,name) = Inst.makeFullyQualified(cache,env,name);
(cache,env,_,dae) = Inst.implicitFunctionInstantiation(cache,env,InstanceHierarchy.emptyInstHierarchy,DAE.NOMOD(),Prefix.NOPRE(),Connect.emptySet,cl,{});
dae = DAEUtil.addDaeFunction(dae);
then dae;
then (cache,dae);

case(cache,env,name,_,_) equation
print("instantiateDaeFunction failed for "+&Absyn.pathString(name)+&"\n");
Expand Down

0 comments on commit 0cd7fe7

Please sign in to comment.