Skip to content

Commit

Permalink
Merge template CodegenCppCommonOld with CodegenCppCommon (#7580)
Browse files Browse the repository at this point in the history
* Merge changes of #7574 (pre of arrays) with CodegenCppCommon

* Remove CodegenCppCommonOld.tpl -- use CodegenCppCommon.tpl instead

* Unify use of const refs introduced with #7574 (pre of arrays)
  • Loading branch information
rfranke committed Jun 21, 2021
1 parent e0055b2 commit 75ceb38
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 3,184 deletions.
1 change: 0 additions & 1 deletion OMCompiler/Compiler/.cmake/meta_modelica_source_list.cmake
Expand Up @@ -392,7 +392,6 @@ set(OMC_MM_BACKEND_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/Template/CodegenC.mo
${CMAKE_CURRENT_SOURCE_DIR}/Template/CodegenEmbeddedC.mo
${CMAKE_CURRENT_SOURCE_DIR}/Template/CodegenCppCommon.mo
${CMAKE_CURRENT_SOURCE_DIR}/Template/CodegenCppCommonOld.mo
${CMAKE_CURRENT_SOURCE_DIR}/Template/CodegenCpp.mo
${CMAKE_CURRENT_SOURCE_DIR}/Template/CodegenCppOld.mo
${CMAKE_CURRENT_SOURCE_DIR}/Template/CodegenCppHpcom.mo
Expand Down
9 changes: 3 additions & 6 deletions OMCompiler/Compiler/.cmake/template_compilation.cmake
Expand Up @@ -144,9 +144,6 @@ omc_add_template_target(SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/Template/CodegenFMU2.
omc_add_template_target(SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/Template/CodegenCppCommon.tpl
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/Template/SimCodeTV.mo)

omc_add_template_target(SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/Template/CodegenCppCommonOld.tpl
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/Template/SimCodeTV.mo)

omc_add_template_target(SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/Template/CodegenCpp.tpl
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/Template/SimCodeTV.mo
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/Template/CodegenUtil.tpl
Expand All @@ -157,7 +154,7 @@ omc_add_template_target(SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/Template/CodegenCppOl
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/Template/SimCodeTV.mo
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/Template/CodegenUtil.tpl
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/Template/CodegenCppInit.tpl
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/Template/CodegenCppCommonOld.tpl)
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/Template/CodegenCppCommon.tpl)

omc_add_template_target(SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/Template/CodegenCppHpcom.tpl
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/Template/SimCodeTV.mo
Expand Down Expand Up @@ -186,7 +183,7 @@ omc_add_template_target(SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/Template/CodegenFMUCp
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/Template/CodegenC.tpl
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/Template/CodegenUtil.tpl
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/Template/CodegenCppOld.tpl
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/Template/CodegenCppCommonOld.tpl
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/Template/CodegenCppCommon.tpl
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/Template/CodegenFMU.tpl)

omc_add_template_target(SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/Template/CodegenOMSI_common.tpl
Expand Down Expand Up @@ -235,7 +232,7 @@ omc_add_template_target(SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/Template/CodegenFMUCp
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/Template/SimCodeBackendTV.mo
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/Template/CodegenCppHpcomOld.tpl
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/Template/CodegenUtil.tpl
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/Template/CodegenCppCommonOld.tpl
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/Template/CodegenCppCommon.tpl
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/Template/CodegenCppOld.tpl
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/Template/CodegenFMU.tpl)

Expand Down
81 changes: 39 additions & 42 deletions OMCompiler/Compiler/Template/CodegenCppCommon.tpl
Expand Up @@ -145,17 +145,13 @@ template subscriptStr(Subscript subscript)
else "UNKNOWN_SUBSCRIPT"
end subscriptStr;

template contextCref(ComponentRef cr, Context context, SimCode simCode, Text& extraFuncs, Text& extraFuncsDecl, Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation)
template contextCref(ComponentRef cr, Context context,SimCode simCode ,Text& extraFuncs,Text& extraFuncsDecl,Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation)
"Generates code for a component reference depending on which context we're in."
::=
match cr
case CREF_QUAL(ident = "$PRE") then
'_discrete_events->pre(<%contextCref(componentRef, context, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)%>)'
else
let &varDeclsCref = buffer "" /*BUFD*/
match context
case FUNCTION_CONTEXT(__) then crefStr(cr)
else '<%cref1(cr, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, context, varDeclsCref, stateDerVectorName, useFlatArrayNotation)%>'
let &varDeclsCref = buffer "" /*BUFD*/
match context
case FUNCTION_CONTEXT(__) then crefStr(cr)
else '<%cref1(cr,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace,context,varDeclsCref,stateDerVectorName,useFlatArrayNotation)%>'
end contextCref;

template contextCref2(ComponentRef cr, Context context)
Expand Down Expand Up @@ -212,7 +208,10 @@ end crefToCStrWithIndex;

template cref1(ComponentRef cr, SimCode simCode ,Text& extraFuncs,Text& extraFuncsDecl,Text extraFuncsNamespace, Context context, Text &varDecls, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation) ::=
match cr
case CREF_IDENT(ident = "xloc") then '<%representationCref(cr, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, context, varDecls, stateDerVectorName, useFlatArrayNotation)%>'
case CREF_QUAL(ident = "$PRE") then
'_discrete_events->pre(<%cref1(componentRef, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, context, varDecls, stateDerVectorName, useFlatArrayNotation)%>)'
case CREF_IDENT(ident = "xloc") then
'<%representationCref(cr, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, context, varDecls, stateDerVectorName, useFlatArrayNotation)%>'
case CREF_IDENT(ident = "time") then
match context
case ALGLOOP_CONTEXT(genInitialisation=false)
Expand Down Expand Up @@ -350,20 +349,20 @@ template daeExpCrefRhs(Exp exp, Context context, Text &preExp, Text &varDecls, S
// by daeExpRecordCrefRhs only in a simulation context, not in a function.
case CREF(componentRef = cr, ty = t as T_COMPLEX(complexClassType = RECORD(path = _))) then
match context case FUNCTION_CONTEXT(__) then
'<%daeExpCrefRhs2(exp, context, &preExp, &varDecls,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)%>'
'<%daeExpCref(false, exp, context, &preExp, &varDecls,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)%>'
else
daeExpRecordCrefRhs(t, cr, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)
case CREF(ty = T_FUNCTION_REFERENCE_FUNC(functionType=t)) then
functionClosure(underscorePath(crefToPathIgnoreSubs(componentRef)), "", t, t, context, &extraFuncsDecl)
case CREF(componentRef = CREF_IDENT(ident=ident), ty = T_FUNCTION_REFERENCE_VAR(__)) then
contextFunName(ident, context)
else
daeExpCrefRhs2(exp, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)
daeExpCref(false, exp, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)
end daeExpCrefRhs;


template daeExpCrefRhs2(Exp ecr, Context context, Text &preExp, Text &varDecls, SimCode simCode, Text& extraFuncs,
Text& extraFuncsDecl, Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation)
template daeExpCref(Boolean isLhs, Exp ecr, Context context, Text &preExp, Text &varDecls, SimCode simCode, Text& extraFuncs,
Text& extraFuncsDecl, Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation)
"Generates code for a component reference."
::=
match ecr
Expand All @@ -376,10 +375,10 @@ case component as CREF(componentRef=cr, ty=ty) then
else
if boolAnd(intEq(listLength(crefSubs(cr)), listLength(crefDims(cr))), crefSubIsScalar(cr)) then
// The array subscript results in a scalar
let arrName = contextCref(crefStripLastSubs(cr), context, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)
let arrName = contextCref(crefStripLastSubs(cr), context,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)
let arrayType = expTypeShort(ty)
let dimsValuesStr = (crefSubs(cr) |> INDEX(__) =>
daeExp(exp, context, &preExp /*BUFC*/, &varDecls /*BUFD*/, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)
daeExp(exp, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)
;separator=",")
match arrayType
case "metatype_array" then
Expand All @@ -391,13 +390,14 @@ case component as CREF(componentRef=cr, ty=ty) then
else
// The array subscript denotes a slice
let arrName = contextArrayCref(cr, context)
let typeStr = expTypeShort(ty)
let arrTypeStr = if isLhs then 'ArraySlice' else 'ArraySliceConst'
let elTypeStr = expTypeShort(ty)
let slice = daeExpCrefIndexSpec(crefSubs(cr), context, &preExp,
&varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace,
stateDerVectorName, useFlatArrayNotation)
let &preExp += 'ArraySlice<<%typeStr%>> <%slice%>_as(<%arrName%>, <%slice%>);<%\n%>'
let &preExp += '<%arrTypeStr%><<%elTypeStr%>> <%slice%>_as(<%arrName%>, <%slice%>);<%\n%>'
'<%slice%>_as'
end daeExpCrefRhs2;
end daeExpCref;


template daeExpCrefIndexSpec(list<Subscript> subs, Context context,
Expand Down Expand Up @@ -728,44 +728,41 @@ template expTypeFlag(DAE.Type ty, Integer flag)
end match
case 5 then
match ty
/* previous multiarray
case T_ARRAY(dims=dims) then 'multi_array_ref<<%expTypeShort(ty)%>,<%listLength(dims)%>>'
*/
case T_ARRAY(dims=dims) then
//let testbasearray = dims |> dim => '<%testdimension(dim)%>' ;separator=''
let dimstr = checkDimension(dims)
match dimstr
case "" then 'DynArrayDim<%listLength(dims)%><<%expTypeShort(ty)%>>'
else 'StatArrayDim<%listLength(dims)%><<%expTypeShort(ty)%>,<%dimstr%>>&'
else expTypeFlag(ty, 2)
case T_ARRAY(dims=dims) then
//let testbasearray = dims |> dim => '<%testdimension(dim)%>' ;separator=''
let dimstr = checkDimension(dims)
match dimstr
case "" then 'const DynArrayDim<%listLength(dims)%><<%expTypeShort(ty)%>>&'
else 'const StatArrayDim<%listLength(dims)%><<%expTypeShort(ty)%>,<%dimstr%>>&'
else expTypeFlag(ty, 2)
end match

case 6 then
expTypeFlag(ty, 4)

case 7 then
match ty
match ty
case T_ARRAY(dims=dims)
then
'multi_array<<%expTypeShort(ty)%>,<%listLength(dims)%>>'
end match

case 8 then
match ty
case T_ARRAY(dims=dims) then'BaseArray<<%expTypeShort(ty)%>>&'
else expTypeFlag(ty, 9)
case T_ARRAY(dims=dims) then 'const BaseArray<<%expTypeShort(ty)%>>&'
else expTypeFlag(ty, 9)
end match

case 9 then
// we want the "modelica type"
match ty case T_COMPLEX(complexClassType=EXTERNAL_OBJ(__)) then
'<%expTypeShort(ty)%>'
else match ty case T_COMPLEX(complexClassType=RECORD(path=rname)) then
'<%underscorePath(rname)%>Type&'
else match ty case T_COMPLEX(__) then
'<%underscorePath(ClassInf.getStateName(complexClassType))%>&'
else
'<%expTypeShort(ty)%>'
// we want the "modelica type"
match ty case T_COMPLEX(complexClassType=EXTERNAL_OBJ(__)) then
'<%expTypeShort(ty)%>'
else match ty case T_COMPLEX(complexClassType=RECORD(path=rname)) then
'const <%underscorePath(rname)%>Type&'
else match ty case T_COMPLEX(__) then
'const <%underscorePath(ClassInf.getStateName(complexClassType))%>&'
else
'<%expTypeShort(ty)%>'

end expTypeFlag;

Expand Down Expand Up @@ -1042,7 +1039,7 @@ template daeExpReduction(Exp exp, Context context, Text &preExp,
let &rangeExpPre = buffer ""
let arrayTypeResult = expTypeFromExpArray(r)
let arrIndex = match ri.path case IDENT(name="array") then tempDecl("int",&tmpVarDecls)
let foundFirst = if not ri.defaultValue then tempDecl("int",&tmpVarDecls)
let foundFirst = match ri.path case IDENT(name="array") then "" else (if not ri.defaultValue then tempDecl("int",&tmpVarDecls))
let resType = expTypeArrayIf(typeof(exp))
let res = contextCref(makeUntypedCrefIdent(ri.resultName), context,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)
let &tmpVarDecls += '<%resType%> <%res%>;<%\n%>'
Expand Down

0 comments on commit 75ceb38

Please sign in to comment.