Skip to content

Commit

Permalink
+ Fix #2688. Removed replacing of loop iterator dependent crefs with …
Browse files Browse the repository at this point in the history
…ASUB expressions.

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@21592 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
mahge committed Jul 22, 2014
1 parent 0bf696c commit 48a7df6
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 103 deletions.
24 changes: 24 additions & 0 deletions Compiler/FrontEnd/ComponentReference.mo
Expand Up @@ -1583,6 +1583,30 @@ algorithm
end match;
end crefLastType;

public function crefDims "
function: crefDims
Return the all dimension (contained in the types) of a ComponentRef"
input DAE.ComponentRef inComponentRef;
output list<DAE.Dimension> outDimensionLst;
algorithm
outDimensionLst := match (inComponentRef)
local
list<DAE.Dimension> dims,res;
DAE.Type idType;
DAE.ComponentRef cr;

case (DAE.CREF_IDENT(identType = idType)) then Types.getDimensions(idType);

case (DAE.CREF_QUAL(componentRef = cr, identType = idType))
equation
dims = Types.getDimensions(idType);
res = crefDims(cr);
res = listAppend(dims,res);
then
res;
end match;
end crefDims;

public function crefSubs "
function: crefSubs
Return the all subscripts of a ComponentRef"
Expand Down
95 changes: 0 additions & 95 deletions Compiler/FrontEnd/InstSection.mo
Expand Up @@ -1962,106 +1962,12 @@ algorithm
// do not unroll if it doesn't contain a when statement!
false = containsWhenStatements(sl);
(cache,stmts) = instForStatement_dispatch(cache,env,ih,pre,ci_state,iterator,range,sl,info,source,initial_,impl,unrollForLoops);
stmts = replaceLoopDependentCrefs(stmts, iterator, range);
then
(cache,stmts);

end matchcontinue;
end instForStatement;

protected function replaceLoopDependentCrefs
"Replaces all DAE.CREFs that are dependent on a loop variable with a
DAE.ASUB."
input list<DAE.Statement> inStatements;
input String iterator;
input Option<Absyn.Exp> range;
output list<DAE.Statement> outStatements;
algorithm
(outStatements, _) := DAEUtil.traverseDAEEquationsStmts(inStatements,
replaceLoopDependentCrefInExp, {Absyn.ITERATOR(iterator,NONE(),range)});
end replaceLoopDependentCrefs;

protected function replaceLoopDependentCrefInExp
"Helper function for replaceLoopDependentCrefs."
input tuple<DAE.Exp,Absyn.ForIterators> itpl;
output tuple<DAE.Exp,Absyn.ForIterators> otpl;
algorithm
otpl := matchcontinue itpl
local
DAE.Exp cr_exp,expCref;
DAE.ComponentRef cr;
DAE.Type cr_type;
list<DAE.Subscript> cref_subs;
list<DAE.Exp> exp_subs;
Absyn.ForIterators fi;

case ((DAE.CREF(componentRef = cr), fi))
equation
cref_subs = ComponentReference.crefSubs(cr);
exp_subs = List.map(cref_subs, Expression.subscriptIndexExp);
true = isSubsLoopDependent(exp_subs, fi);
cr = ComponentReference.crefStripSubs(cr);
cr_type = ComponentReference.crefLastType(cr);
expCref = Expression.makeCrefExp(cr, cr_type);
then
((Expression.makeASUB(expCref, exp_subs), fi));
case _ then itpl;
end matchcontinue;
end replaceLoopDependentCrefInExp;

protected function isSubsLoopDependent
"Checks if a list of subscripts contain any of a list of iterators."
input list<DAE.Exp> subscripts;
input Absyn.ForIterators iterators;
output Boolean loopDependent;
algorithm
loopDependent := matchcontinue(subscripts, iterators)
local
Absyn.Ident iter_name;
DAE.Exp iter_exp;
DAE.ComponentRef cref_;
Absyn.ForIterators rest_iters;
Boolean res;
case (_, {}) then false;
case (_, Absyn.ITERATOR(name = iter_name) :: _)
equation
cref_ = ComponentReference.makeCrefIdent(iter_name, DAE.T_INTEGER_DEFAULT, {});
iter_exp = Expression.makeCrefExp(cref_, DAE.T_INTEGER_DEFAULT);
true = isSubsLoopDependentHelper(subscripts, iter_exp);
then
true;
case (_, _ :: rest_iters)
equation
res = isSubsLoopDependent(subscripts, rest_iters);
then
res;
end matchcontinue;
end isSubsLoopDependent;

protected function isSubsLoopDependentHelper
"Helper for isLoopDependent.
Checks if a list of subscripts contains a certain iterator expression."
input list<DAE.Exp> subscripts;
input DAE.Exp iteratorExp;
output Boolean isDependent;
algorithm
isDependent := matchcontinue(subscripts, iteratorExp)
local
DAE.Exp subscript;
list<DAE.Exp> rest;
case ({}, _) then false;
case (subscript :: _, _)
equation
true = Expression.expContains(subscript, iteratorExp);
then true;
case (_ :: rest, _)
equation
true = isSubsLoopDependentHelper(rest, iteratorExp);
then true;
else false;
end matchcontinue;
end isSubsLoopDependentHelper;

protected function instForStatement_dispatch
"function for instantiating a for statement"
input Env.Cache inCache;
Expand Down Expand Up @@ -5297,7 +5203,6 @@ algorithm
// do not unroll if it doesn't contain a when statement!
false = containsWhenStatements(sl);
(cache,stmts) = instParForStatement_dispatch(cache,env,ih,pre,ci_state,iterator,range,sl,info,source,initial_,impl,unrollForLoops);
stmts = replaceLoopDependentCrefs(stmts, iterator, range);
then
(cache,stmts);

Expand Down
35 changes: 27 additions & 8 deletions Compiler/Template/CodegenC.tpl
Expand Up @@ -7666,8 +7666,14 @@ template scalarLhsCref(Exp ecr, Context context, Text &preExp, Text &varDecls, T
contextCref(ecr.componentRef, context, &auxFunction)
else
daeExpCrefLhs(ecr, context, &preExp, &varDecls, &auxFunction)
case ecr as CREF(componentRef=CREF_QUAL(__)) then
contextCref(ecr.componentRef, context, &auxFunction)
case ecr as CREF(componentRef=cr as CREF_QUAL(__)) then
if crefIsScalar(cr, context) then
contextCref(cr, context, &auxFunction)
else
let arrName = contextCref(crefStripSubs(cr), context, &auxFunction)
<<
(&<%arrName%>)[<%threadDimSubList(crefDims(cr),crefSubs(cr),context,&preExp,&varDecls,&auxFunction)%> - 1]
>>
case ecr as CREF(componentRef=WILD(__)) then
''
else
Expand Down Expand Up @@ -7878,7 +7884,7 @@ template daeExpCrefRhs2(Exp ecr, Context context, Text &preExp,
case et as T_ARRAY(__) then
/* subtract one for indexing a C-array*/
<<
(&<%arrName%>)[<%threadDimSubList(et.dims,crefSubs(cr),context,&preExp,&varDecls,&auxFunction)%> - 1]
(&<%arrName%>)[<%threadDimSubList(crefDims(cr),crefSubs(cr),context,&preExp,&varDecls,&auxFunction)%> - 1]
>>
else error(sourceInfo(),'Indexing non-array <%printExpStr(ecr)%>')
else
Expand All @@ -7899,16 +7905,23 @@ template threadDimSubList(list<Dimension> dims, list<Subscript> subs, Context co
::=
match subs
case {} then error(sourceInfo(),"Empty dimensions in indexing cref?")
case (sub as INDEX(__))::subrest
then

case {sub as INDEX(__)} then
match dims
case {dim} then
let estr = daeExp(sub.exp, context, &preExp, &varDecls, &auxFunction)
'<%estr%>'
else error(sourceInfo(),"Less subscripts that dimensions in indexing cref? That's odd!")

case (sub as INDEX(__))::subrest then
match dims
case _::dimrest
then
let estr = daeExp(sub.exp, context, &preExp, &varDecls, &auxFunction)
'((<%estr%>)<%
'((<%estr%><%
dimrest |> dim =>
match dim
case DIM_INTEGER(__) then '*<%integer%>'
case DIM_INTEGER(__) then '-1)*<%integer%>'
case DIM_BOOLEAN(__) then '*2'
case DIM_ENUM(__) then '*<%size%>'
else error(sourceInfo(),"Non-constant dimension in simulation context")
Expand Down Expand Up @@ -8053,9 +8066,15 @@ template daeExpCrefLhs2(Exp ecr, Context context, Text &afterExp,
(*<%arrayType%>_element_addr(&<%arrName%>, <%dimsLenStr%>, <%dimsValuesStr%>))
>>
else
match crefLastType(cr)
case et as T_ARRAY(__) then
/* subtract one for indexing a C-array*/
/*FIXME: preExp should come from outside. generation for indeices might need it*/
let &preExp = buffer ""
<<
_<%arrName%>(<%dimsValuesStr%>)
(&<%arrName%>)[<%threadDimSubList(crefDims(cr),crefSubs(cr),context,&preExp,&varDecls,&auxFunction)%> - 1]
>>
else error(sourceInfo(),'Indexing non-array <%printExpStr(ecr)%>')

else
// The array subscript denotes a slice
Expand Down
10 changes: 10 additions & 0 deletions Compiler/Template/SimCodeTV.mo
Expand Up @@ -2586,11 +2586,21 @@ package ComponentReference
input String ident;
output DAE.ComponentRef outCrefIdent;
end makeUntypedCrefIdent;

function crefDims
input DAE.ComponentRef cref;
output list<DAE.Dimension> dims;
end crefDims;

function crefStripLastSubs
input DAE.ComponentRef inComponentRef;
output DAE.ComponentRef outComponentRef;
end crefStripLastSubs;

function crefStripSubs
input DAE.ComponentRef inComponentRef;
output DAE.ComponentRef outComponentRef;
end crefStripSubs;

function crefSubs
input DAE.ComponentRef cref;
Expand Down

0 comments on commit 48a7df6

Please sign in to comment.