Skip to content

Commit

Permalink
fix in cpp template for wrong StatArray dimension size
Browse files Browse the repository at this point in the history
 added therefore a dimension check function in Expression.mo
  • Loading branch information
niklwors committed Aug 26, 2015
1 parent 1cb6082 commit f6a3cc1
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 16 deletions.
38 changes: 35 additions & 3 deletions Compiler/FrontEnd/Expression.mo
Expand Up @@ -11238,17 +11238,28 @@ algorithm
outExp := DAE.BINARY(inLhs, inOp, inRhs);
end makeBinaryExp;

public function checkExpDimensionSizes
"Extracts an integer from an exp"
input DAE.Exp dim;
output Boolean value;
algorithm
value := matchcontinue(dim)
case DAE.ICONST() then if dim.integer > 0 then true else false;
else
false;
end matchcontinue;
end checkExpDimensionSizes;

public function checkDimensionSizes
"Extracts an integer from an array dimension. Also handles DIM_EXP and
"Extracts an integer from an array dimension. Also handles DIM_EXP and
DIM_UNKNOWN if checkModel is used."
input DAE.Dimension dim;
input DAE.Dimension dim;
output Boolean value;
protected
Integer i;
DAE.Exp e;
algorithm
value := matchcontinue(dim)

case DAE.DIM_INTEGER(integer = i) then true;
case DAE.DIM_ENUM(size = i) then true;
case DAE.DIM_BOOLEAN() then true;
Expand Down Expand Up @@ -11282,6 +11293,27 @@ algorithm
end matchcontinue;
end dimensionsList;


public function expDimensionsList
"Extracts a list of integers from a list of expressions"
input list<DAE.Exp> inDims;
output list<Integer> outValues;
protected
list<Boolean> boolHelperList;
list<Integer> dims;
algorithm
outValues := matchcontinue(inDims)
case (_)
equation
boolHelperList = List.map(inDims, checkExpDimensionSizes);
true = List.reduce(boolHelperList,boolAnd);
dims = List.map(inDims, expInt);
then dims;
case (_)
then {};
end matchcontinue;
end expDimensionsList;

public function isCrefListWithEqualIdents
"Checks if all expressions in the given list are crefs with the same identifiers.
e.g. {A[1],A[2],…,A[n]} -> true
Expand Down
19 changes: 10 additions & 9 deletions Compiler/Template/CodegenCpp.tpl
Expand Up @@ -2907,7 +2907,7 @@ template funParamDecl3(Variable var,String varName, list<DAE.Exp> instDims, SimC
let type = '<%varType(var)%>'
let testinstDimsInit = (instDims |> exp => testDaeDimensionExp(exp);separator="")
let instDimsInit = (instDims |> exp => daeExp(exp, contextFunction, &varInits , &varDecls,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation);separator=",")
let arrayexpression1 = (if instDims then 'StatArrayDim<%listLength(instDims)%><<%expTypeShort(var.ty)%>,<%instDimsInit%>> <%varName%>;<%\n%>'
let arrayexpression1 = (if instDims then 'StatArrayDim<%listLength(instDims)%><<%expTypeShort(var.ty)%>,<%instDimsInit%>> <%varName%>;/*testarray*/<%\n%>'
else '<%type%> <%varName%>')
let arrayexpression2 = (if instDims then 'DynArrayDim<%listLength(instDims)%><<%expTypeShort(var.ty)%>> <%varName%>;<%\n%>'
else '<%type%> <%varName%>')
Expand Down Expand Up @@ -5346,26 +5346,27 @@ end varOutputTuple;

template varDeclForVarInit(Variable var,String varName, list<DAE.Exp> instDims, Text &varDecls, Text &varInits, SimCode simCode, Text& extraFuncs, Text& extraFuncsDecl, Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation)
::=
let instDimsInit = (instDims |> exp => daeExp(exp, contextFunction, &varInits, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation);separator=",")
//let instDimsInit = (instDims |> exp => daeExp(exp, contextFunction, &varInits, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation);separator=",")
let instDimsInit = checkExpDimension(instDims)
match var
case var as VARIABLE(__) then
let type = '<%varType(var)%>'
let initVar = match type case "modelica_metatype" then ' = NULL' else ''
let addRoot = match type case "modelica_metatype" then ' mmc_GC_add_root(&<%varName%>, mmc_GC_local_state, "<%varName%>");' else ''
let testinstDimsInit = (instDims |> exp => testDaeDimensionExp(exp);separator="")
let instDimsInit = (instDims |> exp => daeExp(exp, contextFunction, &varInits, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation);separator=",")
let arrayexpression1 = (if instDims then 'StatArrayDim<%listLength(instDims)%><<%expTypeShort(var.ty)%>,<%instDimsInit%>> <%varName%>;<%\n%>'
//let testinstDimsInit = (instDims |> exp => testDaeDimensionExp(exp);separator="")
//let instDimsInit = (instDims |> exp => daeExp(exp, contextFunction, &varInits, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation);separator=",")
let arrayexpression1 = (if instDims then 'StatArrayDim<%listLength(instDims)%><<%expTypeShort(var.ty)%>,<%instDimsInit%>> <%varName%>;/*testarray5*/<%\n%>'
else '<%type%> <%varName%><%initVar%>;<%addRoot%><%\n%>')
let arrayexpression2 = (if instDims then 'DynArrayDim<%listLength(instDims)%><<%expTypeShort(var.ty)%>> <%varName%>;<%\n%>'
else '<%type%> <%varName%><%initVar%>;<%addRoot%><%\n%>'
)

match testinstDimsInit
match instDimsInit
case "" then
let &varDecls += arrayexpression1
let &varDecls += arrayexpression2
""
else
let &varDecls += arrayexpression2
let &varDecls += arrayexpression1
""
end varDeclForVarInit;

Expand Down Expand Up @@ -5658,7 +5659,7 @@ case var as VARIABLE(__) then
match testinstDimsInit
case "" then
let instDimsInit = (instDims |> exp => daeDimensionExp(exp);separator=",")
if instDims then 'StatArrayDim<%listLength(instDims)%>< <%expTypeShort(var.ty)%>, <%instDimsInit%>> ' else expTypeFlag(var.ty, 6)
if instDims then 'StatArrayDim<%listLength(instDims)%>< <%expTypeShort(var.ty)%>, <%instDimsInit%>> /*testarray2*/' else expTypeFlag(var.ty, 6)
else
if instDims then 'DynArrayDim<%listLength(instDims)%><<%expTypeShort(var.ty)%>> ' else expTypeFlag(var.ty, 6)

Expand Down
13 changes: 9 additions & 4 deletions Compiler/Template/CodegenCppCommon.tpl
Expand Up @@ -517,7 +517,7 @@ template daeExpCrefRhsArrayBox2(Text var,DAE.Type type, Context context, Text &p

let arraytype = match dimstr
case "" then 'DynArrayDim<%listLength(dims)%><<%expTypeShort(type)%>>'
else 'StatArrayDim<%listLength(dims)%><<%expTypeShort(type)%>,<%dimstr%>>'
else 'StatArrayDim<%listLength(dims)%><<%expTypeShort(type)%>,<%dimstr%>> /*testarray3*/'
end match
let &tmpdecl = buffer "" /*BUFD*/
let arrayVar = tempDecl(arraytype, &tmpdecl /*BUFD*/)
Expand Down Expand Up @@ -680,6 +680,11 @@ template checkDimension(Dimensions dims)

end checkDimension;

template checkExpDimension(list<DAE.Exp> dims)
::=
expDimensionsList(dims) |> dim as Integer => '<%dim%>';separator=","
end checkExpDimension;


template expTypeShort(DAE.Type type)

Expand Down Expand Up @@ -757,7 +762,7 @@ template expTypeFlag(DAE.Type ty, Integer flag)
match dimstr
case "" then 'DynArrayDim<%listLength(dims)%><<%expTypeShort(type)%>>'
//case
else 'StatArrayDim<%listLength(dims)%><<%expTypeShort(type)%>,<%dimstr%>>'
else 'StatArrayDim<%listLength(dims)%><<%expTypeShort(type)%>,<%dimstr%>>/*testarray4*/'
end match
else expTypeFlag(ty, 2)
end match
Expand Down Expand Up @@ -1126,7 +1131,7 @@ template daeExpReduction(Exp exp, Context context, Text &preExp,
let loopHeadIter = (iterators |> iter as REDUCTIONITER(__) =>
let identType = expTypeFromExpModelica(iter.exp)
let ty_str = expTypeArray(ty)
let arrayType = 'DynArrayDim1<<%ty_str%>>'//expTypeFromExpArray(iter.exp)
let arrayType = 'DynArrayDim1<<%identType%>>'//expTypeFromExpArray(iter.exp)
let loopVar = '<%iter.id%>_loopVar'
let &guardExpPre = buffer ""
let &tmpVarDecls += '<%arrayType%> <%loopVar%>;/*testloopvar*/<%\n%>'
Expand Down Expand Up @@ -2708,7 +2713,7 @@ case ARRAY(__) then
*/
let &preExp += '
//tmp array
<%StatArrayDim%><%arrayVar%>;<%\n%>'
<%StatArrayDim%><%arrayVar%>;/*testarray6*/<%\n%>'
arrayVar
else
match typeof(exp)
Expand Down
6 changes: 6 additions & 0 deletions Compiler/Template/SimCodeTV.mo
Expand Up @@ -3073,6 +3073,12 @@ package Expression
output list<Integer> outValues;
end dimensionsList;

function expDimensionsList
input list<DAE.Exp> inDims;
output list<Integer> outValues;
end expDimensionsList;


function isMetaArray
input DAE.Exp inExp;
output Boolean outB;
Expand Down

0 comments on commit f6a3cc1

Please sign in to comment.