Skip to content

Commit

Permalink
Tail-recursive instArray2 for DIM_INTEGER (at the cost of moving some…
Browse files Browse the repository at this point in the history
… equations around)

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@21891 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Aug 17, 2014
1 parent c5a0fed commit 2b24682
Showing 1 changed file with 121 additions and 48 deletions.
169 changes: 121 additions & 48 deletions Compiler/FrontEnd/InstVar.mo
Expand Up @@ -1366,56 +1366,12 @@ algorithm
then
fail();

case
(cache,env,ih,store,_,_,_,_,(_,_),_,i,DAE.DIM_INTEGER(integer = stop),_,_,_,_,_,_,graph,csets)
// Handle DIM_INTEGER, where the dimension is >0
case (cache,env,ih,store,ci_state,_,_,_,(_,_),_,_,DAE.DIM_INTEGER(integer = stop),_,_,_,_,_,_,graph,csets)
equation
true = (i > stop);
(cache,env,ih,store,dae,csets,ty,graph) = instArrayDimInteger(cache,env,ih,store,ci_state,inMod,inPrefix,inIdent,inElement,inPrefixes,stop,inDimensionLst,inIntegerLst,inInstDims,inBoolean,inComment,info,graph,csets,DAE.emptyDae);
then
(cache,env,ih,store,DAE.emptyDae,csets,DAE.T_UNKNOWN_DEFAULT,graph);

// adrpo: if a class is derived WITH AN ARRAY DIMENSION we should instVar2 the derived from type not the actual type!!!
case (cache,env,ih,store,ci_state,mod,pre,n,
(cl as SCode.CLASS(classDef=SCode.DERIVED(typeSpec=Absyn.TPATH(path,SOME(_)),
modifications=scodeMod,attributes=_)),
attr),
pf,i,DAE.DIM_INTEGER(integer = stop),dims,idxs,_,impl,comment,_,graph, _)
equation
(_,clBase,_) = Lookup.lookupClass(cache, env, path, true);
/* adrpo: TODO: merge also the attributes, i.e.:
type A = input discrete flow Integer[3];
A x; <-- input discrete flow IS NOT propagated even if it should. FIXME!
*/
//SOME(attr3) = SCode.mergeAttributes(attr,SOME(absynAttr));

scodeMod = InstUtil.chainRedeclares(mod, scodeMod);

(_,mod2) = Mod.elabMod(cache, env, ih, pre, scodeMod, impl,info);
mod3 = Mod.merge(mod, mod2, env, pre);
mod_1 = Mod.lookupIdxModification(mod3, i);
s = DAE.INDEX(DAE.ICONST(i));
(cache,env_1,ih,store,dae1,csets,ty,graph) =
instVar2(cache,env,ih, store,ci_state, mod_1, pre, n, clBase, attr,
pf,dims, (s :: idxs), {} /* inst_dims */, impl, comment,info,graph, inSets);
i_1 = i + 1;
(cache,_,ih,store,dae2,csets,_,graph) =
instArray2(cache,env,ih,store, ci_state, mod, pre, n, (cl,attr), pf,
i_1, DAE.DIM_INTEGER(stop), dims, idxs, {} /* inst_dims */, impl, comment,info,graph, csets);
daeLst = DAEUtil.joinDaeLst({dae1, dae2});
then
(cache,env_1,ih,store,daeLst,csets,ty,graph);

case (cache,env,ih,store,ci_state,mod,pre,n,(cl,attr),pf,i,DAE.DIM_INTEGER(integer = stop),dims,idxs,inst_dims,impl,comment,_,graph,csets)
equation
mod_1 = Mod.lookupIdxModification(mod, i);
s = DAE.INDEX(DAE.ICONST(i));
(cache,env_1,ih,store,dae1,csets,ty,graph) =
instVar2(cache,env,ih, store,ci_state, mod_1, pre, n, cl, attr, pf,dims, (s :: idxs), inst_dims, impl, comment,info,graph, csets);
i_1 = i + 1;
(cache,_,ih,store,dae2,csets,_,graph) =
instArray2(cache,env,ih,store, ci_state, mod, pre, n, (cl,attr), pf, i_1, DAE.DIM_INTEGER(stop), dims, idxs, inst_dims, impl, comment,info,graph, csets);
daeLst = DAEUtil.joinDaes(dae1, dae2);
then
(cache,env_1,ih,store,daeLst,csets,ty,graph);
(cache,env,ih,store,dae,csets,ty,graph);

// Instantiate an array whose dimension is determined by an enumeration.
case (cache, env, ih, store, ci_state, mod, pre, n, (cl, attr), pf,
Expand Down Expand Up @@ -1477,4 +1433,121 @@ algorithm
end matchcontinue;
end instArray2;

protected function instArrayDimInteger
"When an array is instantiated by instVar, this function is used to go through all the array elements and instantiate each array element separately.
Special case for DIM_INTEGER: tail-recursive implementation since the number of dimensions may grow arbitrarily large."
input Env.Cache inCache;
input Env.Env inEnv;
input InnerOuter.InstHierarchy inIH;
input UnitAbsyn.InstStore inStore;
input ClassInf.State inState;
input DAE.Mod inMod;
input Prefix.Prefix inPrefix;
input String inIdent;
input tuple<SCode.Element, SCode.Attributes> inElement;
input SCode.Prefixes inPrefixes;
input Integer thisDim;
input DAE.Dimensions inDimensionLst;
input list<DAE.Subscript> inIntegerLst;
input list<list<DAE.Subscript>>inInstDims;
input Boolean inBoolean;
input SCode.Comment inComment;
input Absyn.Info info;
input ConnectionGraph.ConnectionGraph inGraph;
input Connect.Sets inSets;
input DAE.DAElist accDae;
output Env.Cache outCache;
output Env.Env outEnv;
output InnerOuter.InstHierarchy outIH;
output UnitAbsyn.InstStore outStore;
output DAE.DAElist outDae;
output Connect.Sets outSets;
output DAE.Type outType;
output ConnectionGraph.ConnectionGraph outGraph;
algorithm
(outCache,outEnv,outIH,outStore,outDae,outSets,outType,outGraph) :=
match (inCache,inEnv,inIH,inStore,inState,inMod,inPrefix,inIdent,inElement,inPrefixes,thisDim,inDimensionLst,inIntegerLst,inInstDims,inBoolean,inComment,info,inGraph,inSets,accDae)
local
DAE.Exp e,lhs,rhs;
DAE.Properties p;
Env.Cache cache;
Env.Env env_1,env_2,env,compenv;
Connect.Sets csets;
DAE.Type ty;
ClassInf.State st,ci_state;
DAE.ComponentRef cr;
DAE.Type ty_1;
DAE.Mod mod,mod_1,mod_2;
Prefix.Prefix pre;
String n, str1, str2, str3, str4;
SCode.Element cl;
SCode.Attributes attr;
Integer i,stop,i_1;
DAE.Dimension dim;
DAE.Dimensions dims;
list<DAE.Subscript> idxs;
InstDims inst_dims;
Boolean impl;
SCode.Comment comment;
DAE.DAElist dae,dae1,dae2,daeLst;
ConnectionGraph.ConnectionGraph graph;
InstanceHierarchy ih;
DAE.ElementSource source "the origin of the element";
DAE.Subscript s;
SCode.Element clBase;
Absyn.Path path;
SCode.Attributes absynAttr;
SCode.Mod scodeMod;
DAE.Mod mod2, mod3;
String lit;
list<String> l;
Integer enum_size;
Absyn.Path enum_type, enum_lit;
SCode.Prefixes pf;
UnitAbsyn.InstStore store;

// Stop=true
case (cache,env,ih,store,_,mod,pre,n,(cl,_),_,0,_,_,_,_,_,_,graph,csets,dae) then (cache,env,ih,store,dae,csets,DAE.T_UNKNOWN_DEFAULT,graph);

// Stop=false

// adrpo: if a class is derived WITH AN ARRAY DIMENSION we should instVar2 the derived from type not the actual type!!!
case (cache,env,ih,store,ci_state,mod,pre,n,
(cl as SCode.CLASS(classDef=SCode.DERIVED(typeSpec=Absyn.TPATH(path,SOME(_)),
modifications=scodeMod,attributes=_)),
attr),
pf,i,dims,idxs,_,impl,comment,_,graph, _, _)
equation
true = i > 0;
(_,clBase,_) = Lookup.lookupClass(cache, env, path, true);
/* adrpo: TODO: merge also the attributes, i.e.:
type A = input discrete flow Integer[3];
A x; <-- input discrete flow IS NOT propagated even if it should. FIXME!
*/
//SOME(attr3) = SCode.mergeAttributes(attr,SOME(absynAttr));

scodeMod = InstUtil.chainRedeclares(mod, scodeMod);

(_,mod2) = Mod.elabMod(cache, env, ih, pre, scodeMod, impl,info);
mod3 = Mod.merge(mod, mod2, env, pre);
mod_1 = Mod.lookupIdxModification(mod3, i);
s = DAE.INDEX(DAE.ICONST(i));
(cache,env_1,ih,store,dae1,csets,ty,graph) = instVar2(cache,env,ih, store,ci_state, mod_1, pre, n, clBase, attr, pf, dims, (s :: idxs), {} /* inst_dims */, impl, comment, info, graph, inSets);
(cache,_,ih,store,daeLst,csets,_,graph) = instArrayDimInteger(cache, env, ih, store, ci_state, mod, pre, n, (cl,attr), pf, i - 1, dims, idxs, {} /* inst_dims */, impl, comment,info,graph, csets, DAEUtil.joinDaes(dae1, accDae));
then
(cache,env_1,ih,store,daeLst,csets,ty,graph);

case (cache,env,ih,store,ci_state,mod,pre,n,(cl,attr),pf,i,dims,idxs,inst_dims,impl,comment,_,graph,csets,_)
equation
true = i > 0;
mod_1 = Mod.lookupIdxModification(mod, i);
s = DAE.INDEX(DAE.ICONST(i));
(cache,env_1,ih,store,dae1,csets,ty,graph) = instVar2(cache,env,ih, store,ci_state, mod_1, pre, n, cl, attr, pf,dims, (s :: idxs), inst_dims, impl, comment,info,graph, csets);
(cache,_,ih,store,daeLst,csets,_,graph) = instArrayDimInteger(cache,env,ih,store, ci_state, mod, pre, n, (cl,attr), pf, i - 1, dims, idxs, inst_dims, impl, comment,info,graph, csets, DAEUtil.joinDaes(dae1, accDae));
then
(cache,env_1,ih,store,daeLst,csets,ty,graph);

end match;
end instArrayDimInteger;

end InstVar;

0 comments on commit 2b24682

Please sign in to comment.