Skip to content

Commit

Permalink
- Changed the range of a for-loop to Option<Exp> to account for impli…
Browse files Browse the repository at this point in the history
…cit ranges in a more visible manner

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@11983 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Jun 5, 2012
1 parent e3731e0 commit 1bb2a31
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 29 deletions.
7 changes: 4 additions & 3 deletions Compiler/FrontEnd/InstExtends.mo
Expand Up @@ -1051,6 +1051,7 @@ algorithm
list<list<SCode.EEquation>> eqll;
list<tuple<Absyn.Exp, list<SCode.EEquation>>> whenlst;
Option<SCode.Comment> comment;
Option<Absyn.Exp> optExp;
Absyn.FunctionArgs fargs;
Absyn.Info info;
Env.Cache cache;
Expand All @@ -1074,11 +1075,11 @@ algorithm
(cache,cref1) = fixCref(cache,env,cref1,ht);
(cache,cref2) = fixCref(cache,env,cref2,ht);
then (cache,SCode.EQ_CONNECT(cref1,cref2,comment,info));
case (cache,env,SCode.EQ_FOR(id,exp,eql,comment,info),ht)
case (cache,env,SCode.EQ_FOR(id,optExp,eql,comment,info),ht)
equation
(cache,exp) = fixExp(cache,env,exp,ht);
(cache,optExp) = fixOption(cache,env,optExp,ht,fixExp);
(cache,eql) = fixList(cache,env,eql,ht,fixEEquation);
then (cache,SCode.EQ_FOR(id,exp,eql,comment,info));
then (cache,SCode.EQ_FOR(id,optExp,eql,comment,info));
case (cache,env,SCode.EQ_WHEN(exp,eql,whenlst,comment,info),ht)
equation
(cache,exp) = fixExp(cache,env,exp,ht);
Expand Down
12 changes: 6 additions & 6 deletions Compiler/FrontEnd/InstSection.mo
Expand Up @@ -644,15 +644,15 @@ algorithm
// The loop expression is evaluated to a constant array of integers, and then the loop is unrolled.

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

// for i loop ... end for; NOTE: This construct is encoded as range being Absyn.END()
case (cache,env,ih,mod,pre,csets,ci_state,SCode.EQ_FOR(index = i,range = Absyn.END(),eEquationLst = el, info=info),initial_,impl,graph)
// for i loop ... end for; NOTE: This construct is encoded as range being NONE()
case (cache,env,ih,mod,pre,csets,ci_state,SCode.EQ_FOR(index = i,range = NONE(),eEquationLst = el, info=info),initial_,impl,graph)
equation
(lst as _::_)=SCode.findIteratorInEEquationLst(i,el);
tpl=List.first(lst);
Expand All @@ -667,7 +667,7 @@ algorithm
(cache,env,ih,dae,csets_1,ci_state_1,graph);

// for i in <expr> loop .. end for;
case (cache,env,ih,mod,pre,csets,ci_state,SCode.EQ_FOR(index = i,range = e,eEquationLst = el,info=info),initial_,impl,graph)
case (cache,env,ih,mod,pre,csets,ci_state,SCode.EQ_FOR(index = i,range = SOME(e),eEquationLst = el,info=info),initial_,impl,graph)
equation
(cache,e_1,DAE.PROP(type_ = DAE.T_ARRAY(ty = id_t), constFlag = cnst),_) = Static.elabExp(cache,env, e, impl,NONE(),true, pre, info);
env_1 = addForLoopScope(env, i, id_t, SCode.VAR(), SOME(cnst));
Expand All @@ -679,7 +679,7 @@ algorithm

// A for-equation with a parameter range without binding, which is ok when
// doing checkModel. Use a range {1} to check that the loop can be instantiated.
case (cache, env, ih, mod, pre, csets, ci_state, SCode.EQ_FOR(index = i, range = e, eEquationLst = el,info=info), initial_, impl, graph)
case (cache, env, ih, mod, pre, csets, ci_state, SCode.EQ_FOR(index = i, range = SOME(e), eEquationLst = el,info=info), initial_, impl, graph)
equation
true = Flags.getConfigBool(Flags.CHECK_MODEL);
(cache, e_1, DAE.PROP(type_ = DAE.T_ARRAY(ty = id_t), constFlag = cnst as DAE.C_PARAM()), _) =
Expand All @@ -693,7 +693,7 @@ algorithm

// for i in <expr> loop .. end for;
// where <expr> is not constant or parameter expression
case (cache,env,ih,mod,pre,csets,ci_state,SCode.EQ_FOR(index = i,range = e,eEquationLst = el,info=info),initial_,impl,graph)
case (cache,env,ih,mod,pre,csets,ci_state,SCode.EQ_FOR(index = i,range = SOME(e),eEquationLst = el,info=info),initial_,impl,graph)
equation
(cache,e_1,DAE.PROP(type_ = DAE.T_ARRAY(ty = _), constFlag = DAE.C_VAR()),_)
= Static.elabExp(cache,env, e, impl,NONE(),true,pre,info);
Expand Down
2 changes: 1 addition & 1 deletion Compiler/FrontEnd/InstTypes.mo
Expand Up @@ -243,7 +243,7 @@ public uniontype Equation
record FOR_EQUATION
String index "The name of the index/iterator variable.";
DAE.Type indexType "The type of the index/iterator variable.";
DAE.Exp range "The range expression to loop over.";
Option<DAE.Exp> range "The range expression to loop over.";
list<Equation> body "The body of the for loop.";
Absyn.Info info;
end FOR_EQUATION;
Expand Down
35 changes: 26 additions & 9 deletions Compiler/FrontEnd/SCode.mo
Expand Up @@ -294,8 +294,8 @@ uniontype EEquation
end EQ_CONNECT;

record EQ_FOR "the for equation"
Ident index "the index name";
Absyn.Exp range "the range of the index";
Ident index "the index name";
Option<Absyn.Exp> range "the range of the index";
list<EEquation> eEquationLst "the equation list";
Option<Comment> comment;
Absyn.Info info;
Expand Down Expand Up @@ -1432,14 +1432,21 @@ algorithm
then
true;

case (EQ_FOR(index = id1, range = exp1, eEquationLst = eql1),EQ_FOR(index = id2, range = exp2, eEquationLst = eql2))
case (EQ_FOR(index = id1, range = SOME(exp1), eEquationLst = eql1),EQ_FOR(index = id2, range = SOME(exp2), eEquationLst = eql2))
equation
List.threadMapAllValue(eql1,eql2,equationEqual2,true);
true = Absyn.expEqual(exp1,exp2);
true = stringEq(id1,id2);
then
true;

case (EQ_FOR(index = id1, range = NONE(), eEquationLst = eql1),EQ_FOR(index = id2, range = NONE(), eEquationLst = eql2))
equation
List.threadMapAllValue(eql1,eql2,equationEqual2,true);
true = stringEq(id1,id2);
then
true;

case (EQ_WHEN(condition = cond1, eEquationLst = elst1),EQ_WHEN(condition = cond2, eEquationLst = elst2)) // TODO: elsewhen not checked yet.
equation
List.threadMapAllValue(elst1,elst2,equationEqual2,true);
Expand Down Expand Up @@ -1831,18 +1838,27 @@ algorithm
lst_2=Absyn.findIteratorInCRef(id,cr_2);
lst=listAppend(lst_1,lst_2);
then lst;
case (id,EQ_FOR(index = id_1, range = e_1, eEquationLst = eeqLst))
case (id,EQ_FOR(index = id_1, range = SOME(e_1), eEquationLst = eeqLst))
equation
false = stringEq(id, id_1);
lst_1=Absyn.findIteratorInExp(id,e_1);
lst_2=findIteratorInEEquationLst(id,eeqLst);
lst=listAppend(lst_1,lst_2);
then lst;
case (id,EQ_FOR(index = id_1, range = e_1, eEquationLst = eeqLst))
case (id,EQ_FOR(index = id_1, range = SOME(e_1), eEquationLst = eeqLst))
equation
true = stringEq(id, id_1);
lst=Absyn.findIteratorInExp(id,e_1);
then lst;
case (id,EQ_FOR(index = id_1, range = NONE(), eEquationLst = eeqLst))
equation
false = stringEq(id, id_1);
lst=findIteratorInEEquationLst(id,eeqLst);
then lst;
case (id,EQ_FOR(index = id_1, range = NONE(), eEquationLst = eeqLst))
equation
true = stringEq(id, id_1);
then {};
case (id,EQ_WHEN(condition = e_1, eEquationLst = eeqLst, elseBranches = ew))
equation
lst_1=Absyn.findIteratorInExp(id,e_1);
Expand Down Expand Up @@ -2375,6 +2391,7 @@ algorithm
Argument arg;
tuple<TraverseFunc, Argument> tup;
Absyn.Exp e1, e2;
Option<Absyn.Exp> oe1;
list<Absyn.Exp> expl1;
list<list<EEquation>> then_branch;
list<EEquation> else_branch, eql;
Expand All @@ -2394,11 +2411,11 @@ algorithm
then
(EQ_IF(expl1, then_branch, else_branch, comment, info), tup);

case (EQ_FOR(index, e1, eql, comment, info), tup)
case (EQ_FOR(index, oe1, eql, comment, info), tup)
equation
(eql, tup) = traverseEEquationsList(eql, tup);
then
(EQ_FOR(index, e1, eql, comment, info), tup);
(EQ_FOR(index, oe1, eql, comment, info), tup);

case (EQ_WHEN(e1, eql, else_when, comment, info), tup)
equation
Expand Down Expand Up @@ -2508,11 +2525,11 @@ algorithm
then
(EQ_CONNECT(cr1, cr2, comment, info), tup);

case (EQ_FOR(index, e1, eql, comment, info), (traverser, arg))
case (EQ_FOR(index, SOME(e1), eql, comment, info), (traverser, arg))
equation
((e1, arg)) = traverser((e1, arg));
then
(EQ_FOR(index, e1, eql, comment, info), (traverser, arg));
(EQ_FOR(index, SOME(e1), eql, comment, info), (traverser, arg));

case (EQ_WHEN(e1, eql, else_when, comment, info), (traverser, arg))
equation
Expand Down
12 changes: 10 additions & 2 deletions Compiler/FrontEnd/SCodeInst.mo
Expand Up @@ -2356,15 +2356,23 @@ algorithm
InstTypes.CONNECT_EQUATION(dcref1, Connect.NO_FACE(),
dcref2, Connect.NO_FACE(), inPrefix, info);

case (SCode.EQ_FOR(index = for_index, range = exp1, eEquationLst = eql,
case (SCode.EQ_FOR(index = for_index, range = SOME(exp1), eEquationLst = eql,
info = info), _, _)
equation
env = SCodeEnv.extendEnvWithIterators(
{Absyn.ITERATOR(for_index, NONE(), NONE())}, inEnv);
dexp1 = instExp(exp1, env, inPrefix);
ieql = instEEquations(eql, env, inPrefix);
then
InstTypes.FOR_EQUATION(for_index, DAE.T_UNKNOWN_DEFAULT, dexp1, ieql, info);
InstTypes.FOR_EQUATION(for_index, DAE.T_UNKNOWN_DEFAULT, SOME(dexp1), ieql, info);

case (SCode.EQ_FOR(index = for_index, range = NONE(), eEquationLst = eql,
info = info), _, _)
equation
env = SCodeEnv.extendEnvWithIterators({Absyn.ITERATOR(for_index, NONE(), NONE())}, inEnv);
ieql = instEEquations(eql, env, inPrefix);
then
InstTypes.FOR_EQUATION(for_index, DAE.T_UNKNOWN_DEFAULT, NONE(), ieql, info);

case (SCode.EQ_IF(condition = if_condition, thenBranch = if_branches,
elseBranch = eql, info = info), _, _)
Expand Down
8 changes: 4 additions & 4 deletions Compiler/FrontEnd/SCodeUtil.mo
Expand Up @@ -1755,25 +1755,25 @@ algorithm
equation
l_1 = translateEEquations(l, inIsInitial);
then
SCode.EQ_FOR(i,e,l_1,com,info);
SCode.EQ_FOR(i,SOME(e),l_1,com,info);

case (Absyn.EQ_FOR(iterators = {Absyn.ITERATOR(i,NONE(),NONE())},forEquations = l),com,info,_) /* for loop with a single iterator with implicit range */
equation
l_1 = translateEEquations(l, inIsInitial);
then
SCode.EQ_FOR(i,Absyn.END(),l_1,com,info);
SCode.EQ_FOR(i,NONE(),l_1,com,info);

case (Absyn.EQ_FOR(iterators = Absyn.ITERATOR(i,NONE(),SOME(e))::(restIterators as _::_),forEquations = l),com,info,_) /* for loop with multiple iterators */
equation
eq = translateEquation(Absyn.EQ_FOR(restIterators,l),com,info, inIsInitial);
then
SCode.EQ_FOR(i,e,{eq},com,info);
SCode.EQ_FOR(i,SOME(e),{eq},com,info);

case (Absyn.EQ_FOR(iterators = Absyn.ITERATOR(i,NONE(),NONE())::(restIterators as _::_),forEquations = l),com,info,_) /* for loop with multiple iterators */
equation
eq = translateEquation(Absyn.EQ_FOR(restIterators,l),com,info, inIsInitial);
then
SCode.EQ_FOR(i,Absyn.END(),{eq},com,info);
SCode.EQ_FOR(i,NONE(),{eq},com,info);

case (Absyn.EQ_FOR(iterators = Absyn.ITERATOR(guardExp=SOME(_))::_,forEquations = l),_,info,_)
equation
Expand Down
9 changes: 7 additions & 2 deletions Compiler/FrontEnd/Typing.mo
Expand Up @@ -993,7 +993,7 @@ algorithm
then
acc_el;

case (InstTypes.FOR_EQUATION(index, _, exp1, eql, info), st, acc_el)
case (InstTypes.FOR_EQUATION(index, _, SOME(exp1), eql, info), st, acc_el)
equation
(exp1, ty, _) = typeExp(exp1, EVAL_CONST_PARAM(), st);
ty = rangeToIteratorType(ty, exp1, info);
Expand All @@ -1002,7 +1002,12 @@ algorithm
st = InstSymbolTable.addIterator(iter_name, iter, st);
eql = typeEquations(eql, st);
then
InstTypes.FOR_EQUATION(index, ty, exp1, eql, info) :: acc_el;
InstTypes.FOR_EQUATION(index, ty, SOME(exp1), eql, info) :: acc_el;

case (InstTypes.FOR_EQUATION(index, _, NONE(), eql, info), st, acc_el)
equation
Error.addSourceMessage(Error.INTERNAL_ERROR,{"Implicit for ranges are not yet implemented"},info);
then fail();

case (InstTypes.IF_EQUATION(branches, info), st, acc_el)
equation
Expand Down
10 changes: 9 additions & 1 deletion Compiler/susan_codegen/SimCode/SCodeDumpTpl.tpl
Expand Up @@ -372,7 +372,7 @@ end dumpElseIfEEquation;
template dumpForEEquation(SCode.EEquation for_equation)
::=
match for_equation
case EQ_FOR(__) then
case EQ_FOR(range=SOME(range)) then
let range_str = AbsynDumpTpl.dumpExp(range)
let eq_str = (eEquationLst |> e => dumpEEquation(e) ;separator="\n")
let cmt_str = dumpCommentOpt(comment)
Expand All @@ -381,6 +381,14 @@ match for_equation
<%eq_str%>
end for;
>>
case EQ_FOR(__) then
let eq_str = (eEquationLst |> e => dumpEEquation(e) ;separator="\n")
let cmt_str = dumpCommentOpt(comment)
<<
for <%index%> loop<%cmt_str%>
<%eq_str%>
end for;
>>
end dumpForEEquation;

template dumpWhenEEquation(SCode.EEquation when_equation)
Expand Down
2 changes: 1 addition & 1 deletion Compiler/susan_codegen/SimCode/SCodeTV.mo
Expand Up @@ -513,7 +513,7 @@ package SCode

record EQ_FOR
Ident index;
Absyn.Exp range;
Option<Absyn.Exp> range;
list<EEquation> eEquationLst;
Option<Comment> comment;
Absyn.Info info;
Expand Down

0 comments on commit 1bb2a31

Please sign in to comment.