Skip to content

Commit

Permalink
- changes by Alexey Lebedev [alexey dot lebedev at equa dot se] to su…
Browse files Browse the repository at this point in the history
…pport implicit range in for loops

- more tests for loops

git-svn-id: https://openmodelica.org/svn/OpenModelica/branches/OpenModelica1.5.0@4272 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
adrpo committed Sep 28, 2009
1 parent 7ae9668 commit 4aaf6b1
Show file tree
Hide file tree
Showing 3 changed files with 260 additions and 22 deletions.
76 changes: 63 additions & 13 deletions Compiler/Absyn.mo
Expand Up @@ -1789,7 +1789,7 @@ algorithm crefs := matchcontinue(subs)
list<ComponentRef> crefs1;
Exp exp;
case({}) then {};
case(NOSUB::subs) then getCrefsFromSubs(subs);
case(NOSUB::subs) then getCrefsFromSubs(subs);
case(SUBSCRIPT(exp)::subs)
equation
crefs1 = getCrefsFromSubs(subs);
Expand Down Expand Up @@ -2729,6 +2729,7 @@ algorithm
list<ForIterator> forIterators;
FunctionArgs funcArgs;
AlgorithmItem algItem;
Boolean bool;

case (id,ALG_ASSIGN(e_1,e_2))
equation
Expand All @@ -2744,7 +2745,7 @@ algorithm
lst_4=findIteratorInAlgorithmItemLst(id,algLst_2);
lst=Util.listFlatten({lst_1,lst_2,lst_3,lst_4});
then lst;
case (id, ALG_FOR(forIterators,algLst_1))
/* case (id, ALG_FOR(forIterators,algLst_1))
equation
true=iteratorPresentAmongIterators(id,forIterators);
lst=findIteratorInForIteratorsBounds(id,forIterators);
Expand All @@ -2755,6 +2756,13 @@ algorithm
lst_1=findIteratorInAlgorithmItemLst(id,algLst_1);
lst_2=findIteratorInForIteratorsBounds(id,forIterators);
lst=listAppend(lst_1,lst_2);
then lst; */
case (id, ALG_FOR(forIterators,algLst_1))
equation
lst_1=findIteratorInAlgorithmItemLst(id,algLst_1);
(bool,lst_2)=findIteratorInForIteratorsBounds2(id,forIterators);
lst_1=Util.if_(bool, {}, lst_1);
lst=listAppend(lst_1,lst_2);
then lst;
case (id, ALG_WHILE(e_1,algLst_1))
equation
Expand Down Expand Up @@ -2862,7 +2870,7 @@ algorithm
end matchcontinue;
end findIteratorInElseIfExpBranch;

protected function findIteratorInFunctionArgs
public function findIteratorInFunctionArgs
input String inString;
input FunctionArgs inFunctionArgs;
output list<tuple<ComponentRef, Integer>> outLst;
Expand All @@ -2875,13 +2883,14 @@ algorithm
list<NamedArg> namedArgs;
Exp exp;
list<ForIterator> forIterators;
Boolean bool;
case (id,FUNCTIONARGS(expLst,namedArgs))
equation
lst_1=findIteratorInExpLst(id,expLst);
lst_2=findIteratorInNamedArgs(id,namedArgs);
lst=listAppend(lst_1,lst_2);
then lst;
case (id, FOR_ITER_FARG(exp,forIterators))
/* case (id, FOR_ITER_FARG(exp,forIterators))
equation
true=iteratorPresentAmongIterators(id,forIterators);
lst=findIteratorInForIteratorsBounds(id,forIterators);
Expand All @@ -2892,11 +2901,18 @@ algorithm
lst_1=findIteratorInExp(id,exp);
lst_2=findIteratorInForIteratorsBounds(id,forIterators);
lst=listAppend(lst_1,lst_2);
then lst; */
case (id, FOR_ITER_FARG(exp,forIterators))
equation
lst_1=findIteratorInExp(id,exp);
(bool,lst_2)=findIteratorInForIteratorsBounds2(id,forIterators);
lst_1=Util.if_(bool, {}, lst_1);
lst=listAppend(lst_1,lst_2);
then lst;
end matchcontinue;
end findIteratorInFunctionArgs;

protected function iteratorPresentAmongIterators
/*protected function iteratorPresentAmongIterators
input String inString;
input list<ForIterator> inForIterators;
output Boolean outBool;
Expand All @@ -2917,9 +2933,9 @@ algorithm
bool=iteratorPresentAmongIterators(id,rest);
then bool;
end matchcontinue;
end iteratorPresentAmongIterators;
end iteratorPresentAmongIterators; */

protected function findIteratorInExpLst//This function is not tail-recursive, and I don't know how to fix it -- alleb
public function findIteratorInExpLst//This function is not tail-recursive, and I don't know how to fix it -- alleb
input String inString;
input list<Exp> inExpLst;
output list<tuple<ComponentRef, Integer>> outLst;
Expand Down Expand Up @@ -2982,7 +2998,7 @@ algorithm
end matchcontinue;
end findIteratorInNamedArgs;

protected function findIteratorInForIteratorsBounds
/*protected function findIteratorInForIteratorsBounds
input String inString;
input list<ForIterator> inForIterators;
output list<tuple<ComponentRef, Integer>> outLst;
Expand All @@ -3005,9 +3021,43 @@ algorithm
lst=listAppend(lst_1,lst_2);
then lst;
end matchcontinue;
end findIteratorInForIteratorsBounds;
end findIteratorInForIteratorsBounds; */

protected function findIteratorInForIteratorsBounds2 "
This is a fixed version of the function; it stops looking for the iterator when it finds another iterator
with the same name. It also returns information about whether it has found such an iterator"
input String inString;
input list<ForIterator> inForIterators;
output Boolean outBool;
output list<tuple<ComponentRef, Integer>> outLst;
algorithm
(outBool,outLst):=matchcontinue(inString,inForIterators)
local
list<tuple<ComponentRef, Integer>> lst,lst_1,lst_2;
Boolean bool;
String id, id_1;
list<ForIterator> rest;
Exp exp;
case (_,{}) then (false,{});
case (id,(id_1,_)::_)
equation
equality(id=id_1);
then
(true,{});
case (id,(_,NONE)::rest)
equation
(bool,lst)=findIteratorInForIteratorsBounds2(id,rest);
then (bool,lst);
case (id,(_,SOME(exp))::rest)
equation
lst_1=findIteratorInExp(id,exp);
(bool,lst_2)=findIteratorInForIteratorsBounds2(id,rest);
lst=listAppend(lst_1,lst_2);
then (bool,lst);
end matchcontinue;
end findIteratorInForIteratorsBounds2;

protected function findIteratorInExp
public function findIteratorInExp
input String inString;
input Exp inExp;
output list<tuple<ComponentRef, Integer>> outLst;
Expand Down Expand Up @@ -3111,7 +3161,7 @@ algorithm
end matchcontinue;
end findIteratorInExpOpt;

protected function findIteratorInCRef "
public function findIteratorInCRef "
The most important among \"findIteratorIn...\" functions -- they all use this one in the end
"
input String inString;
Expand Down Expand Up @@ -3208,8 +3258,8 @@ algorithm
lst=qualifyCRefIntLst(name,subLst,rest);
then (CREF_QUAL(name,subLst,cref),i)::lst;
end matchcontinue;
end qualifyCRefIntLst;

end qualifyCRefIntLst;
public function setBuildTimeInInfo
input Real buildTime;
input Info inInfo;
Expand Down
42 changes: 34 additions & 8 deletions Compiler/Inst.mo
Expand Up @@ -275,8 +275,8 @@ public function instantiateImplicit
Implicit instantiation of a program can be used for e.g. code generation
of functions, since a function must be implicitly instantiated in order to
generate code from it."
input Env.Cache inCache;
input InstanceHierarchy inIH;
input Env.Cache inCache;
input InstanceHierarchy inIH;
input SCode.Program inProgram;
output Env.Cache outCache;
output InstanceHierarchy outIH;
Expand Down Expand Up @@ -371,8 +371,8 @@ public function instantiatePartialClass
This is a function for instantiating partial 'top' classes.
It does so by converting the partial class into a non partial class.
Currently used by: MathCore.modelEquations, CevalScript.checkModel"
input Env.Cache inCache;
input InstanceHierarchy inIH;
input Env.Cache inCache;
input InstanceHierarchy inIH;
input SCode.Program inProgram;
input SCode.Path inPath;
output Env.Cache outCache;
Expand Down Expand Up @@ -9540,6 +9540,8 @@ algorithm
Absyn.ForIterators rangeIdList;
ConnectionGraph.ConnectionGraph graph;
InstanceHierarchy ih;
list<tuple<Absyn.ComponentRef, Integer>> lst;
tuple<Absyn.ComponentRef, Integer> tpl;

/* connect statements */
case (cache,env,ih,mods,pre,csets,ci_state,SCode.EQ_CONNECT(crefLeft = c1,crefRight = c2),initial_,impl,graph)
Expand Down Expand Up @@ -9740,6 +9742,30 @@ algorithm
then
(cache,env,ih,dae,csets_1,ci_state_1,graph);

case (cache,env,ih,mod,pre,csets,ci_state,SCode.EQ_FOR(index = i,range = Absyn.END(),eEquationLst = el),initial_,impl,graph) // Implicit range
equation
lst=SCode.findIteratorInEEquationLst(i,el);
equality(lst={});
Error.addMessage(Error.IMPLICIT_ITERATOR_NOT_FOUND_IN_LOOP_BODY,{i});
then
fail();

case (cache,env,ih,mod,pre,csets,ci_state,SCode.EQ_FOR(index = i,range = Absyn.END(),eEquationLst = el),initial_,impl,graph)
equation
lst=SCode.findIteratorInEEquationLst(i,el);
failure(equality(lst={}));
tpl=Util.listFirst(lst);
e=rangeExpression(tpl);
(cache,e_1,Types.PROP((Types.T_ARRAY(Types.DIM(_),id_t),_),_),_) = Static.elabExp(cache,env, e, impl, NONE,true) "//Debug.fprintln (\"insttr\", \"inst_equation_common_eqfor_1\") &" ;
env_1 = addForLoopScope(env, i, id_t) "//Debug.fprintln (\"insti\", \"for expression elaborated\") &" ;
(cache,Types.ATTR(false,false,SCode.RW(),SCode.VAR(),_,_),(Types.T_INTEGER(_),_),Types.UNBOUND(),_,_)
= Lookup.lookupVar(cache,env_1, Exp.CREF_IDENT(i,Exp.OTHER(),{})) " //Debug.fprintln (\"insti\", \"loop-variable added to scope\") &" ;
(cache,v,_) = Ceval.ceval(cache,env, e_1, impl, NONE, NONE, Ceval.MSG()) " //Debug.fprintln (\"insti\", \"loop variable looked up\") & FIXME: Check bounds" ;
(cache,dae,csets_1,graph) = unroll(cache,env_1, mod, pre, csets, ci_state, i, v, el, initial_, impl,graph) " //Debug.fprintln (\"insti\", \"for expression evaluated\") &" ;
ci_state_1 = instEquationCommonCiTrans(ci_state, initial_) " //Debug.fprintln (\"insti\", \"for expression unrolled\") & & //Debug.fprintln (\"insttr\", \"inst_equation_common_eqfor_1 succeeded\")" ;
then
(cache,env,ih,dae,csets_1,ci_state_1,graph);

case (cache,env,ih,mod,pre,csets,ci_state,SCode.EQ_FOR(index = i,range = e,eEquationLst = el),initial_,impl,graph)
equation
(cache,e_1,Types.PROP((Types.T_ARRAY(Types.DIM(_),id_t),_),_),_) = Static.elabExp(cache,env, e, impl, NONE,true) "//Debug.fprintln (\"insttr\", \"inst_equation_common_eqfor_1\") &" ;
Expand Down Expand Up @@ -11062,7 +11088,7 @@ algorithm
stmt = Algorithm.makeFor(i, e_2, prop, sl_1);
then
(cache,stmt);
case (cache,env,pre,{(i,NONE)},sl,initial_,impl)
/* case (cache,env,pre,{(i,NONE)},sl,initial_,impl)
equation
lst=Absyn.findIteratorInAlgorithmItemLst(i,sl);
// len=listLength(lst);
Expand All @@ -11071,7 +11097,7 @@ algorithm
equality(lst={});
Error.addMessage(Error.IMPLICIT_ITERATOR_NOT_FOUND_IN_LOOP_BODY,{i});
then
fail();
fail();*/
case (cache,env,pre,(i,NONE)::restIterators,sl,initial_,impl)
equation
lst=Absyn.findIteratorInAlgorithmItemLst(i,sl);
Expand Down Expand Up @@ -11911,7 +11937,7 @@ protected function connectArrayComponents "
Help functino to connectComponents
Traverses arrays of complex connectors and calls connectComponents for each index
"
input Env.Cache inCache;
input Env.Cache inCache;
input Env inEnv;
input InstanceHierarchy inIH;
input Connect.Sets inSets;
Expand Down Expand Up @@ -14996,7 +15022,7 @@ algorithm

/*
* adrpo 2009-05-15: also T_COMPLEX that is NOT record but TYPE should be allowed
as is used in Modelica.Mechanics.MultiBody (Orientation type)
* as is used in Modelica.Mechanics.MultiBody (Orientation type)
*/
case(lhs,rhs,tp,initial_) equation
// adrpo: TODO! check if T_COMPLEX(ClassInf.TYPE)!
Expand Down

0 comments on commit 4aaf6b1

Please sign in to comment.