Skip to content

Commit

Permalink
Cleanup codegen cref handling (#8328)
Browse files Browse the repository at this point in the history
  - Cleanup codegen cref handling

  - Separate the parallel cref generation.

    - To simplify cleaning up for now. The parallel cref handling needs to
       be updated to look like the normal one.
       The normal cref handling has been improved a while back to handle
       records and arrays better. The parallel one was not updated. So they
       do not full match right now.

  - Error out if we find a prefix cref of array type.
  • Loading branch information
mahge committed Dec 16, 2021
1 parent d6cdbc5 commit f66698d
Showing 1 changed file with 56 additions and 44 deletions.
100 changes: 56 additions & 44 deletions OMCompiler/Compiler/Template/CodegenCFunctions.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -4504,6 +4504,12 @@ match cr
let newpref = fullname_i + '.'
functionContextCref(cr.componentRef, context, newpref, &preExp, &varDecls, &auxFunction)

case cr as CREF_QUAL(identType = T_ARRAY(), subscriptLst = {}) then
error(sourceInfo(), 'functionContextCref got a prefix cref with array type and no subs. <%crefStrNoUnderscore(cr)%>')
// let fullname = pref + '_' + System.unquoteIdentifier(cr.ident)
// let newpref = fullname + '.'
// functionContextCref(cr.componentRef, context, newpref, &preExp, &varDecls, &auxFunction)

case cr as CREF_QUAL() then
let fullname = pref + '_' + System.unquoteIdentifier(cr.ident)
let newpref = fullname + '.'
Expand All @@ -4523,7 +4529,7 @@ match cr
fullname

else
error(sourceInfo(), 'crefNonSimVar got a cref it does not know how to handle <%crefStrNoUnderscore(cr)%>')
error(sourceInfo(), 'functionContextCref got a cref it does not know how to handle <%crefStrNoUnderscore(cr)%>')
end match
end functionContextCref;

Expand Down Expand Up @@ -5198,6 +5204,7 @@ template daeExpCrefRhs(Exp exp, Context context, Text &preExp,
'(MMC_FETCH(MMC_OFFSET(MMC_UNTAGPTR(_<%cr.ident%>), <%offset%>)))'
else
match context
case FUNCTION_CONTEXT(is_parallel = true) then daeExpCrefRhsFunContextParallel(exp, context, &preExp, &varDecls, &auxFunction)
case FUNCTION_CONTEXT(__) then daeExpCrefRhsFunContext(exp, context, &preExp, &varDecls, &auxFunction)
else daeExpCrefRhsSimContext(exp, context, &preExp, &varDecls, &auxFunction)
end daeExpCrefRhs;
Expand Down Expand Up @@ -5254,58 +5261,63 @@ template daeExpCrefRhsFunContext(Exp ecr, Context context, Text &preExp,
::=
match ecr
case ecr as CREF(componentRef=cr, ty=ty) then
if crefIsScalar(cr, context) then
if boolNot(isArrayType(ty)) then
let cast = typeCastContextInt(context, ty)
'<%cast%><%contextCref(cr, context, &preExp, &varDecls, &auxFunction)%>'
else if crefSubIsScalar(cr) then
// The array subscript results in a scalar
let cast = typeCastContextInt(context, ty)
'<%cast%><%contextCref(cr, context, &preExp, &varDecls, &auxFunction)%>'
else
if crefSubIsScalar(cr) then
// The array subscript results in a scalar
let arrName = contextCref(crefStripLastSubs(cr), context, &preExp, &varDecls, &auxFunction)
match context
case FUNCTION_CONTEXT(__) then
// The array subscript denotes a slice
// let &preExp += '/* daeExpCrefRhsFunContext SLICE(<%ExpressionDumpTpl.dumpExp(ecr,"\"")%>) preExp */<%\n%>'
let arrName = contextCref(crefStripSubs(cr), context, &preExp, &varDecls, &auxFunction)
let arrayType = expTypeArray(ty)
let subsLenStr = listLength(crefSubs(cr))
let subsValuesStr = (crefSubs(cr) |> INDEX(__) =>
daeSubscriptExp(exp, context, &preExp, &varDecls, &auxFunction)
;separator=", ")
match cr
case CREF_IDENT(identType = T_METATYPE(ty = T_METAARRAY()))
case CREF_IDENT(identType = T_METAARRAY()) then
'arrayGet(<%arrName%>, <%subsValuesStr%>)'
else
match context
case FUNCTION_CONTEXT(is_parallel = false) then
match ty
case (T_ARRAY(ty = T_COMPLEX(complexClassType = record_state)))
case (T_COMPLEX(complexClassType = record_state)) then
<<
<%arrayType%>_get(<%arrName%>, <%subsLenStr%>, <%subsValuesStr%>)
>>
else
<<
<%arrayType%>_get<%match listLength(crefSubs(cr)) case 1 case 2 then subsLenStr%>(<%arrName%>, <%subsLenStr%>, <%subsValuesStr%>)
>>
case FUNCTION_CONTEXT(__) then
<<
(*<%arrayType%>_element_addr_c99_<%subsLenStr%>(&<%arrName%>, <%subsLenStr%>, <%subsValuesStr%>))
>>
else
error(sourceInfo(),'This should have been handled in the new daeExpCrefRhsSimContext function. <%ExpressionDumpTpl.dumpExp(ecr,"\"")%>')
let tmp = tempDecl(arrayType, &varDecls)
let spec1 = daeExpCrefIndexSpec(crefSubs(cr), context, &preExp, &varDecls, &auxFunction)
let &preExp += 'index_alloc_<%arrayType%>(&<%arrName%>, &<%spec1%>, &<%tmp%>);<%\n%>'
tmp
else
match context
case FUNCTION_CONTEXT(__) then
// The array subscript denotes a slice
// let &preExp += '/* daeExpCrefRhsFunContext SLICE(<%ExpressionDumpTpl.dumpExp(ecr,"\"")%>) preExp */<%\n%>'
let arrName = contextCref(crefStripLastSubs(cr), context, &preExp, &varDecls, &auxFunction)
let arrayType = expTypeArray(ty)
let tmp = tempDecl(arrayType, &varDecls)
let spec1 = daeExpCrefIndexSpec(crefSubs(cr), context, &preExp, &varDecls, &auxFunction)
let &preExp += 'index_alloc_<%arrayType%>(&<%arrName%>, &<%spec1%>, &<%tmp%>);<%\n%>'
tmp
else
error(sourceInfo(),'daeExpCrefRhsFunContext: Slice in simulation context: <%ExpressionDumpTpl.dumpExp(ecr,"\"")%>')
error(sourceInfo(),'daeExpCrefRhsFunContext: Slice in simulation context: <%ExpressionDumpTpl.dumpExp(ecr,"\"")%>')
case ecr then
error(sourceInfo(),'daeExpCrefRhsFunContext: UNHANDLED EXPRESSION: <%ExpressionDumpTpl.dumpExp(ecr,"\"")%>')
end daeExpCrefRhsFunContext;

template daeExpCrefRhsFunContextParallel(Exp ecr, Context context, Text &preExp,
Text &varDecls, Text &auxFunction)
"Generates code for a component reference."
::=
match ecr
case ecr as CREF(componentRef=cr, ty=ty) then
if crefIsScalar(cr, context) then
let cast = typeCastContextInt(context, ty)
'<%cast%><%contextCref(cr, context, &preExp, &varDecls, &auxFunction)%>'
else if crefSubIsScalar(cr) then
// The array subscript results in a scalar
let arrName = contextCref(crefStripLastSubs(cr), context, &preExp, &varDecls, &auxFunction)
let arrayType = expTypeArray(ty)
let subsLenStr = listLength(crefSubs(cr))
let subsValuesStr = (crefSubs(cr) |> INDEX(__) =>
daeSubscriptExp(exp, context, &preExp, &varDecls, &auxFunction)
;separator=", ")
<<
(*<%arrayType%>_element_addr_c99_<%subsLenStr%>(&<%arrName%>, <%subsLenStr%>, <%subsValuesStr%>))
>>
else
// The array subscript denotes a slice
// let &preExp += '/* daeExpCrefRhsFunContext SLICE(<%ExpressionDumpTpl.dumpExp(ecr,"\"")%>) preExp */<%\n%>'
let arrName = contextCref(crefStripLastSubs(cr), context, &preExp, &varDecls, &auxFunction)
let arrayType = expTypeArray(ty)
let tmp = tempDecl(arrayType, &varDecls)
let spec1 = daeExpCrefIndexSpec(crefSubs(cr), context, &preExp, &varDecls, &auxFunction)
let &preExp += 'index_alloc_<%arrayType%>(&<%arrName%>, &<%spec1%>, &<%tmp%>);<%\n%>'
tmp
case ecr then
error(sourceInfo(),'daeExpCrefRhsFunContext: UNHANDLED EXPRESSION: <%ExpressionDumpTpl.dumpExp(ecr,"\"")%>')
end daeExpCrefRhsFunContextParallel;

// TODO: Optimize as in Codegen
// TODO: Use this function in other places where almost the same thing is hard
// coded
Expand Down

0 comments on commit f66698d

Please sign in to comment.