Skip to content

Commit

Permalink
- update to C# target template
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@16538 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
pavolpr committed Jul 3, 2013
1 parent 9d2c48c commit 84f3982
Showing 1 changed file with 93 additions and 77 deletions.
170 changes: 93 additions & 77 deletions Compiler/Template/CodegenCSharp.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -586,8 +586,8 @@ template aliasVarInfos(String regionName, list<SimVar> varsLst, /*Integer varsCn
let aliasLookup =
match aliasvar
case NOALIAS(__) then
error(sourceInfo(), "Unexpected non-alias variable amongst aliases.")
case ALIAS(__) then
error(sourceInfo(), "Unexpected non-alias variable amongst aliases.")
case ALIAS(__) then
'fvd["<%crefStrWithDerOnLast(varName, simCode)%>"]'
case NEGATEDALIAS(__) then
'-fvd["<%crefStrWithDerOnLast(varName, simCode)%>"]'
Expand Down Expand Up @@ -1506,55 +1506,19 @@ end equationElseWhen;

// SECTION: GENERAL TEMPLATES, COMPONENT REFERENCES

template cref(ComponentRef cr, SimCode simCode) ::=
match cr
case CREF_IDENT(ident = "xloc") then crefStr(cr, simCode) //TODO: ??xloc
case CREF_IDENT(ident = "time") then "Time"
case CREF_IDENT(ident = "$_lambda") then "_lambda"
case CREF_QUAL(ident = "$PRE") then preCref(componentRef, simCode)
else '/*<% crefStr(cr, simCode) %>*/<% representationCref(cr, simCode) %>'
// not needed ... //the space at the start is important to avoid ambiguity with "/" operator to become "//" line comment
/*
template crefRepresentationArrayAndIndex(ComponentRef cr, Text &indexTxt, SimCode simCode) ::=
match cr
//TODO: ?? case CREF_IDENT(ident = "xloc") then crefStr(cr)
case CREF_IDENT(__) then replaceDollarWorkaround(ident)
case _ then "CREF_NOT_IDENT"
*/
end cref;

template representationCref(ComponentRef inCref, SimCode simCode) ::=
cref2simvar(inCref, simCode) |> SIMVAR(__) =>
'<%representationArrayName(varKind, type_)%>[<% index %>]'
end representationCref;

//TODO: a HACK ?
template crefToReal(ComponentRef cr, SimCode simCode) ::=
<</*(double)<% crefStr(cr, simCode)
%>*/<% cref2simvar(cr, simCode) |> SIMVAR(__) => //representationCref(cr, simCode)
'<%representationArrayName(varKind, type_)%>[<% index %>]'
+ (match type_ case T_BOOL(__)
then "?1.0:0.0")
%>
>>
end crefToReal;

//TODO: a HACK ?
template daeExpRealConversionPostfix(Exp exp, SimCode simCode) ::=
match typeof(exp)
case T_BOOL(__) then
"?1.0:0.0"
//TODO: make conversion also for other expressions
end daeExpRealConversionPostfix;

//TODO: a HACK ?
template convertRealExpForCref(Text realExp, ComponentRef cr, SimCode simCode) ::=
cref2simvar(cr, simCode) |> SIMVAR(__) =>
match type_
case T_BOOL(__) then '(<%realExp%>) != 0.0'
case T_INTEGER(__) then '(int)(<%realExp%>)'
else realExp

end convertRealExpForCref;
//deprecated: case CREF_IDENT(ident = "xloc") then crefStr(cr, simCode) //TODO: ??xloc
case CREF_IDENT(ident = "time") then "Time" //no index
case CREF_IDENT(ident = "$_lambda") then "_lambda" //no index
//??is this a HACK (on the SimCode level) ??
case CREF_QUAL(ident = "$PRE") then
'pre<%crefRepresentationArrayAndIndex(componentRef, &indexTxt, simCode)%>'
else
(cref2simvar(cr, simCode) |> SIMVAR(__) =>
let &indexTxt += index
representationArrayName(varKind, type_))
end crefRepresentationArrayAndIndex;

template representationArrayName(VarKind varKind, Type type_) ::=
match varKind
Expand All @@ -1580,6 +1544,33 @@ template representationArrayNameTypePostfix(Type type_) ::=
else "BAD_ARRAY_NAME_POSTFIX"
end representationArrayNameTypePostfix;


template representationCref(ComponentRef inCref, SimCode simCode) ::=
let &indexTxt = buffer ""
let arrAndIdx = crefRepresentationArrayAndIndex(inCref, &indexTxt, simCode)
if indexTxt then
'<%arrAndIdx%>[<% indexTxt %>]'
else
arrAndIdx
//cref2simvar(inCref, simCode) |> SIMVAR(__) =>
// '<%representationArrayName(inCref, varKind, type_)%>[<% index %>]'
end representationCref;

template cref(ComponentRef cr, SimCode simCode) ::=
'/*<% crefStr(cr, simCode) %>*/<% representationCref(cr, simCode) %>'

/*
match cr
//deprecated: case CREF_IDENT(ident = "xloc") then crefStr(cr, simCode) //TODO: ??xloc
case CREF_IDENT(ident = "time") then "Time"
case CREF_IDENT(ident = "$_lambda") then "_lambda"
//$PRE is handled by representationCref ...
// case CREF_QUAL(ident = "$PRE") then preCref(componentRef, simCode)
else '/*<% crefStr(cr, simCode) %>*/<% representationCref(cr, simCode) %>'
// not needed ... //the space at the start is important to avoid ambiguity with "/" operator to become "//" line comment
*/
end cref;

template preCref(ComponentRef cr, SimCode simCode) ::=
'/*pre(<%crefStr(cr, simCode)%>)*/pre<%representationCref(cr, simCode)%>'
end preCref;
Expand All @@ -1588,29 +1579,63 @@ template derCref(ComponentRef cr, SimCode simCode) ::=
'/*derCall!!(<% crefStr(cr, simCode) %>)*/<%representationCref(derComponentRef(cr), simCode)%>'
end derCref;

/*** not used
template oldCref(ComponentRef cr, SimCode simCode) ::=
'/*old(<%crefStr(cr, simCode)%>)*/old<%representationCref(cr, simCode)%>'
end oldCref;

template old2Cref(ComponentRef cr, SimCode simCode) ::=
'/*old2(<%crefStr(cr, simCode)%>)*/old2<%representationCref(cr, simCode)%>'
end old2Cref;
***/

//this one should be used only in InitialResidual( ..., double[][] startValues)
template startCref(ComponentRef cr, SimCode simCode) ::=
//'/*start(<%crefStr(cr, simCode)%>)*/start<%representationCref(cr, simCode)%>'

match cref2simvar(cr, simCode)
case sv as SIMVAR(__) then
let fviIndex =
match varKind
case STATE(__) then "State" //sv.index
case STATE_DER(__) then "StateDer" //'NX+<%sv.index%>'
case VARIABLE(__) then "Algebraic" // '(2*NX)+<%sv.index%>'
case PARAM(__) then "Parameter" //'(2*NX+NY)+<%sv.index%>'
case STATE(__) then "State"
case STATE_DER(__) then "StateDer"
case VARIABLE(__) then "Algebraic"
case PARAM(__) then "Parameter"
else /*error(sourceInfo(),*/ "UNEXPECTED_variable_varKind_in_startCref_template" //)
'startValues[(int)SimVarType.<%fviIndex%>][<%sv.index%>]'
end startCref;


//TODO: a HACK ? ... used in mixed system only
template crefToReal(ComponentRef cr, SimCode simCode) ::=
<</*(double)<% crefStr(cr, simCode)
%>*/<% cref2simvar(cr, simCode) |> SIMVAR(__) => //representationCref(cr, simCode)
'<%representationArrayName(varKind, type_)%>[<% index %>]'
+ (match type_ case T_BOOL(__)
then "?1.0:0.0")
%>
>>
end crefToReal;

//TODO: a HACK ?
template daeExpRealConversionPostfix(Exp exp, SimCode simCode) ::=
match typeof(exp)
case T_BOOL(__) then
"?1.0:0.0"
//TODO: make conversion also for other expressions
end daeExpRealConversionPostfix;

//TODO: a HACK ?
template convertRealExpForCref(Text realExp, ComponentRef cr, SimCode simCode) ::=
cref2simvar(cr, simCode) |> SIMVAR(__) =>
match type_
case T_BOOL(__) then '(<%realExp%>) != 0.0'
case T_INTEGER(__) then '(int)(<%realExp%>)'
else realExp

end convertRealExpForCref;


template contextCref(ComponentRef cr, Context context, SimCode simCode)
"Generates code for a component reference depending on which context we're in."
::=
Expand All @@ -1625,7 +1650,8 @@ template crefStr(ComponentRef cr, SimCode simCode)
::=
match cr
case CREF_IDENT(__) then csharpIdent(ident) + subscriptsStr(subscriptLst, simCode)
case CREF_QUAL(ident = "$DER") then 'der(<%crefStr(componentRef, simCode)%>)'
case CREF_QUAL(ident = "$DER") then '$der(<%crefStr(componentRef, simCode)%>)'
case CREF_QUAL(ident = "$PRE") then '$pre(<%crefStr(componentRef, simCode)%>)'
case CREF_QUAL(__) then '<%ident%><%subscriptsStr(subscriptLst, simCode)%>.<%crefStr(componentRef, simCode)%>'
else "CREF_NOT_IDENT_OR_QUAL"
end crefStr;
Expand Down Expand Up @@ -1677,25 +1703,6 @@ template subscriptsStr(list<Subscript> subscripts, SimCode simCode)
end subscriptsStr;


/*
template crefSubscript(ComponentRef it, SimCode simCode) ::=
match it
case CREF_IDENT(__) then replaceDollarWorkaround(ident) + subscripts(subscriptLst)
case _ then "CREF_NOT_IDENT"
end crefSubscript;

template subscripts(list<Subscript> subscriptsLst) ::=
if subscriptsLst then '[<%subscriptsLst |> it => subscript(it) ;separator=","%>]'
end subscripts;

template subscript(Subscript it) ::=
match it
case INDEX(exp = ICONST(__)) then exp.integer
case _ then "SUBSCRIPT_NOT_CONSTANT"
end subscript;

*/


// SECTION: GENERAL TEMPLATES, PATHS

Expand Down Expand Up @@ -2086,8 +2093,11 @@ case ecr as CREF(ty=T_ARRAY(ty=aty,dims=dims)) then
let &tmpArr = buffer ""
let arrType = expTypeArray(aty, listLength(dims))
let dimsValuesStr = (dims |> dim => dimension(dim) ;separator=", ")
let &preExp += match cref2simvar(ecr.componentRef, simCode) case SIMVAR(__) then
'<%tempDecl("var", &tmpArr)%> = new <%arrType%>(<%dimsValuesStr%>, <%index%>-1, /*<%crefStr(ecr.componentRef, simCode)%>*/<%representationArrayName(varKind,type_)%>);<%\n%>'
let &indexTxt = buffer ""
let reprArray = crefRepresentationArrayAndIndex(ecr.componentRef, &indexTxt, simCode)
let &preExp +=
//match cref2simvar(ecr.componentRef, simCode) case SIMVAR(__) then
'<%tempDecl("var", &tmpArr)%> = new <%arrType%>(<%dimsValuesStr%>, <%indexTxt%>-1, /*<%crefStr(ecr.componentRef, simCode)%>*/<%reprArray%>);<%\n%>'
tmpArr
end daeExpCrefRhsArrayBox;

Expand Down Expand Up @@ -2140,6 +2150,11 @@ template daeExpAsub(Exp aexp, Context context, Text &preExp, SimCode simCode)
else //SIMULATION or OTHER contexts
match ecr.ty
case T_ARRAY(ty = T_REAL(__), dims = dims) then //daeExpCrefRhsArrayBox
let &constSum = buffer ""
let arrayRepr = crefRepresentationArrayAndIndex(cr, &constSum, simCode)
let baseSub = asubSubsripts(dims, subs, &constSum, context, &preExp, simCode)
'/*<%crefStr(cr, simCode)%>[]*/<%arrayRepr%>[<%constSum%><%baseSub%>]'
/*
<<
/*<% crefStr(cr, simCode)
%>[!]*/<% cref2simvar(cr, simCode) |> SIMVAR(__) =>
Expand All @@ -2149,7 +2164,8 @@ template daeExpAsub(Exp aexp, Context context, Text &preExp, SimCode simCode)
<%representationArrayName(varKind,type_)%>[<%constSum%><%baseSub%>]
>>
%>
>>
>>
*/
else "ASUB_SIMULATION_OTHER_ERROR"


Expand Down

0 comments on commit 84f3982

Please sign in to comment.