Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
- Fixed indexing using non-constant subscript in simulation context
  - Also, made this fast by indexing the double* directly instead of creating an intermediate array+vararg addressing


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@9535 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Jul 26, 2011
1 parent c4986d0 commit c8f6880
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
47 changes: 41 additions & 6 deletions Compiler/susan_codegen/SimCode/SimCodeC.tpl
Expand Up @@ -4371,16 +4371,28 @@ template daeExpCrefRhs2(Exp ecr, Context context, Text &preExp /*BUFP*/,
let arrName = contextCref(crefStripLastSubs(cr), context)
let arrayType = expTypeArray(ty)
let dimsLenStr = listLength(crefSubs(cr))
let dimsValuesStr = (crefSubs(cr) |> INDEX(__) =>
daeExp(exp, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
;separator=", ")
match arrayType
case "metatype_array" then
let dimsValuesStr = (crefSubs(cr) |> INDEX(__) =>
daeExp(exp, context, &preExp, &varDecls)
;separator=", ")
'arrayGet(<%arrName%>,<%dimsValuesStr%>) /* DAE.CREF */'
else
<<
(*<%arrayType%>_element_addr(&<%arrName%>, <%dimsLenStr%>, <%dimsValuesStr%>))
>>
match context
case FUNCTION_CONTEXT(__) then
let dimsValuesStr = (crefSubs(cr) |> INDEX(__) =>
daeExp(exp, context, &preExp, &varDecls)
;separator=", ")
<<
(*<%arrayType%>_element_addr(&<%arrName%>, <%dimsLenStr%>, <%dimsValuesStr%>))
>>
else
match crefLastType(cr)
case et as ET_ARRAY(__) then
<<
(&<%arrName%>)[<%threadDimSubList(et.arrayDimensions,crefSubs(cr),context,&preExp,&varDecls)%>]
>>
else error(sourceInfo(),'Indexing non-array <%printExpStr(ecr)%>')
else
// The array subscript denotes a slice
let &preExp += '/* daeExpCrefRhs2 SLICE(<%ExpressionDump.printExpStr(ecr)%>) preExp */<%\n%>'
Expand All @@ -4400,6 +4412,28 @@ template daeExpCrefRhs2(Exp ecr, Context context, Text &preExp /*BUFP*/,
>>
end daeExpCrefRhs2;

template threadDimSubList(list<Dimension> dims, list<Subscript> subs, Context context, Text &preExp, Text &varDecls)
"Do direct indexing since sizes are known during compile-time"
::=
match subs
case {} then error(sourceInfo(),"Empty dimensions in indexing cref?")
case (sub as INDEX(__))::subrest
then
match dims
case _::dimrest
then
let estr = daeExp(sub.exp, context, &preExp, &varDecls)
'((<%estr%>)<%
dimrest |> dim =>
match dim
case DIM_INTEGER(__) then '*<%integer%>'
case DIM_ENUM(__) then '*<%size%>'
else error(sourceInfo(),"Non-constant dimension in simulation context")
%>)<%match subrest case {} then "" else '+<%threadDimSubList(dimrest,subrest,context,&preExp,&varDecls)%>'%>'
else error(sourceInfo(),"Less subscripts that dimensions in indexing cref? That's odd!")
else error(sourceInfo(),"Non-index subscript in indexing cref? That's odd!")
end threadDimSubList;

template daeExpCrefRhsIndexSpec(list<Subscript> subs, Context context,
Text &preExp /*BUFP*/, Text &varDecls /*BUFP*/)
"Helper to daeExpCrefRhs."
Expand Down Expand Up @@ -5146,6 +5180,7 @@ template daeExpCall(Exp call, Context context, Text &preExp /*BUFP*/,

case CALL(path=IDENT(name="delay"), expLst={ICONST(integer=index), e, d, delayMax}) then
let tvar = tempDecl("modelica_real", &varDecls /*BUFD*/)

let var1 = daeExp(e, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
let var2 = daeExp(d, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
let var3 = daeExp(delayMax, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
Expand Down
5 changes: 5 additions & 0 deletions Compiler/susan_codegen/SimCode/SimCodeTV.mo
Expand Up @@ -1933,6 +1933,11 @@ package ComponentReference
output list<DAE.Subscript> subs;
end crefSubs;

function crefLastType
input DAE.ComponentRef inRef;
output DAE.ExpType res;
end crefLastType;

end ComponentReference;

package Expression
Expand Down

0 comments on commit c8f6880

Please sign in to comment.