Skip to content

Commit

Permalink
Updated C# template to the no-helpVars changes
Browse files Browse the repository at this point in the history
- fix: "inline" keyword macroizing is forbidden in C++ from VS 2012, see omc_inline.h (hope it'll work with VS2010)
- fix: copy/paste errors when checking the results of map.find() in simulation_result_mat.cpp
- fix: C++ string emptiness should be tested via size() (an error when run with the debug version of STL)

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@14899 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
pavolpr committed Jan 23, 2013
1 parent 05295c9 commit 2cc65e1
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 54 deletions.
141 changes: 92 additions & 49 deletions Compiler/Template/CodegenCSharp.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ namespace Bodylight.Models<%modelNameSpace(modelInfo.name)%>
{
<%modelDataMembers(modelInfo, simCode)%>
<%sharedLiteralDefinitions(literals, simCode)%>
<%let fbody = simulationFunctionsBody(simCode)
if fbody then
<<
Expand Down Expand Up @@ -94,6 +96,48 @@ template lastIdentOfPath(Path modelName) ::=
case FULLYQUALIFIED(__) then lastIdentOfPath(path)
end lastIdentOfPath;

template sharedLiteralDefinitions(list<Exp> literals, SimCode simCode)
::=
<<
<%literals |> literal hasindex i0 fromindex 0 => literalExpConst(literal, i0, simCode) ; separator="\n";empty%>
>>
end sharedLiteralDefinitions;

template literalExpConst(Exp lit, Integer index, SimCode simCode)
::=
let name = '_OMC_LIT<%index%>'

match lit
case SCONST(__) then
let escstr = Util.escapeModelicaStringToCString(string)
<<
const string <%name%> = "<%Util.escapeModelicaStringToCString(string)%>";
>>
case lit as MATRIX(ty=ty as T_ARRAY(__))
case lit as ARRAY(ty=ty as T_ARRAY(__)) then
let arrType = expTypeArray(ty, listLength(getDimensionSizes(ty)))
let sty = expTypeShort(ty)
let dims = (getDimensionSizes(ty) |> dim => dim ;separator=", ")
let data = flattenArrayExpToList(lit) |> exp => literalExpConstArrayVal(exp) ; separator=", "
<<
static <%arrType%> <%name%> = new <%arrType%>(<%dims%>,-1, new <%sty%>[] { <%data%> });
>>
else error(sourceInfo(), 'literalExpConst failed: <%printExpStr(lit)%>')
end literalExpConst;

//should be identical to call daeExp()
//but it is more specific, maybe can catch an illegal exp which is expected to be a constant
template literalExpConstArrayVal(Exp lit)
::=
match lit
case ICONST(__) then integer
case BCONST(__) then if bool then "true" else "false"
case RCONST(__) then real
case ENUM_LITERAL(__) then '<%index%>/*ENUM:<%dotPath(name)%>*/'
case SHARED_LITERAL(__) then '_OMC_LIT<%index%>'
else error(sourceInfo(), 'literalExpConstArrayVal failed: <%printExpStr(lit)%>')
end literalExpConstArrayVal;

template simulationFunctionsBody(SimCode simCode) ::=
match simCode
case SIMCODE(modelInfo = modelInfo as MODELINFO(__)) then
Expand Down Expand Up @@ -626,21 +670,28 @@ public override bool FunDAE()
>>
end functionDAE;

template whenConditions(list<ComponentRef> conditions, SimCode simCode)
" Generates reinit statemeant"
::=
if conditions then
(conditions |> cr => '<%cref(cr, simCode)%> && !<%preCref(cr, simCode)%> /* edge */';separator=" || ")
else "false"
end whenConditions;

template genreinits(SimWhenClause whenClauses, Integer widx, SimCode simCode)
" Generates reinit statemeant"
::=

match whenClauses
case SIM_WHEN_CLAUSE(__) then
if reinits then
let helpIf = (conditions |> e => ' || <%cref(e, simCode)%> && !$P$PRE<%cref(e, simCode)%> /* edge */')
if reinits then
<<
//For whenclause index: <%widx%>
if (false<%helpIf%>) {
if (<%whenConditions(conditions, simCode)%>) {
<%functionWhenReinitStatementThen(reinits, simCode)%>
}
>>

end genreinits;


Expand Down Expand Up @@ -1132,11 +1183,10 @@ case SES_NONLINEAR(__) then
>>

case SES_WHEN(__) then
let helpIf = (conditions |> e => ' || <%cref(e, simCode)%> && !$P$PRE<%cref(e, simCode)%> /* edge */')
let &preExp = buffer ""
let rightExp = daeExp(right, context, &preExp, simCode)
<<
if (false<%helpIf%>) {
if (<%whenConditions(conditions, simCode)%>) {
<%preExp%>
<%cref(left, simCode)%> = <%rightExp%>;
} else {
Expand All @@ -1160,6 +1210,7 @@ template cref(ComponentRef cr, SimCode simCode) ::=
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
/*
Expand All @@ -1175,6 +1226,36 @@ template representationCref(ComponentRef inCref, SimCode simCode) ::=
'<%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 exp
case CREF(__) then
match cref2simvar(componentRef, simCode)
case SIMVAR(type_ = 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 representationArrayName(VarKind varKind, Type type_) ::=
match varKind
case VARIABLE(__) then "Y" + representationArrayNameTypePostfix(type_)
Expand All @@ -1198,36 +1279,6 @@ template representationArrayNameTypePostfix(Type type_) ::=
else "BAD_ARRAY_NAME_POSTFIX"
end representationArrayNameTypePostfix;

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

//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 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 preCref(ComponentRef cr, SimCode simCode) ::=
'/*pre(<%crefStr(cr, simCode)%>)*/pre<%representationCref(cr, simCode)%>'
end preCref;
Expand Down Expand Up @@ -1502,9 +1553,8 @@ match context
case SIMULATION(genDiscrete=true) then
match when
case STMT_WHEN(__) then
let helpIf = (conditions |> e => ' || (<%cref(e, simCode)%> && !$P$PRE<%cref(e, simCode)%> /* edge */)')
<<
if (false<%helpIf%>) {
if (<%whenConditions(conditions, simCode)%>) {
<% statementLst |> stmt => algStatement(stmt, context, simCode)
;separator="\n" %>
}
Expand All @@ -1519,9 +1569,8 @@ template algStatementWhenElse(Option<DAE.Statement> stmt, SimCode simCode)
::=
match stmt
case SOME(when as STMT_WHEN(__)) then
let elseCondStr = (when.conditions |> e => ' || (<%cref(e, simCode)%> && !$P$PRE<%cref(e, simCode)%> /* edge */)')
<<
else if (false<%elseCondStr%>) {
else if (<%whenConditions(when.conditions, simCode)%>) {
<% when.statementLst |> stmt => algStatement(stmt, contextSimulationDiscrete, simCode)
;separator="\n"%>
}
Expand Down Expand Up @@ -1608,13 +1657,7 @@ template daeExp(Exp inExp, Context context, Text &preExp, SimCode simCode) ::=
//case VALUEBLOCK(__) then "VALUEBLOCK_NOT_IMPLEMENTED"
case LIST(__) then "LIST_NOT_IMPLEMENTED"
case CONS(__) then "CONS_NOT_IMPLEMENTED"
case sl as SHARED_LITERAL(__) then
match simCode
case SIMCODE(__) then
match listNth(literals, sl.index)
case DAE.SCONST(__) then string
else "TemplErr:SHARED_LITERAL() expected to be a string"
end match
case SHARED_LITERAL(__) then '_OMC_LIT<%index%>'
// META_TUPLE
// META_OPTION
// METARECORDCALL
Expand Down Expand Up @@ -2008,11 +2051,11 @@ template daeExpCall(Exp it, Context context, Text &preExp, SimCode simCode) ::=
//else
'<%var1%> / <%var2%>'
case _ then
let string = daeExp(e3, context, &preExp, simCode)
let msg = Util.escapeModelicaStringToCString(string)
let msg = daeExp(e3, context, &preExp, simCode)
//let msg = Util.escapeModelicaStringToCString(string)
let &tmpVar2 = buffer ""
let &preExp +=
'<%tempDecl("var", &tmpVar2)%> = <%var2%>; if (<%tmpVar2%> == 0.0) throw new DivideByZeroException("<%msg%>");<%\n%>'
'<%tempDecl("var", &tmpVar2)%> = <%var2%>; if (<%tmpVar2%> == 0.0) throw new DivideByZeroException(<%msg%>);<%\n%>'
'<%var1%> / <%tmpVar2%>'
end match

Expand Down
6 changes: 5 additions & 1 deletion SimulationRuntime/c/omc_inline.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,14 @@
#define INLINE_H_

#if defined(_MSC_VER)
/* Visual C++ */
/* Visual C/C++
"inline" is a proper keyword in Visual C++ and it is an error to macroize it from VS2012 (_MSC_VER=1700);
so, macroize it only for Visual C where "inline" is not available */
#ifndef __cplusplus
# ifndef inline
# define inline __inline
# endif
#endif
#elif defined(__GNUC__)
/* GCC */
# ifndef inline
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ void simulation_result_mat::generateDataInfo(int32_t* &dataInfo, int& rows, int&
else if(mdl_data->realAlias[i].aliasType == 1) /* parameter */
{
it = r_indx_parammap.find(mdl_data->realAlias[i].nameID);
if(it != r_indx_map.end())
if(it != r_indx_parammap.end())
{
table = 1;
aliascol = it->second+1;
Expand Down Expand Up @@ -461,7 +461,7 @@ void simulation_result_mat::generateDataInfo(int32_t* &dataInfo, int& rows, int&
else if(mdl_data->integerAlias[i].aliasType == 1) /* parameter */
{
it = i_indx_parammap.find(mdl_data->integerAlias[i].nameID);
if(it != i_indx_map.end())
if(it != i_indx_parammap.end())
table = 1;
}
if(table)
Expand Down Expand Up @@ -499,7 +499,7 @@ void simulation_result_mat::generateDataInfo(int32_t* &dataInfo, int& rows, int&
else if(mdl_data->booleanAlias[i].aliasType == 1) /* parameter */
{
it = b_indx_parammap.find(mdl_data->booleanAlias[i].nameID);
if(it != b_indx_map.end())
if(it != b_indx_parammap.end())
table = 1;
}
}
Expand Down
2 changes: 1 addition & 1 deletion SimulationRuntime/c/simulation/simulation_runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ int callSolver(DATA* simData, string result_file_cstr, string init_initMethod,
string init_optiMethod, string init_file, double init_time, string outputVariablesAtEnd)
{
int retVal = -1;
const char* outVars = (outputVariablesAtEnd[0] == '\0') ? NULL : outputVariablesAtEnd.c_str();
const char* outVars = (outputVariablesAtEnd.size() == 0) ? NULL : outputVariablesAtEnd.c_str();

long maxSteps = 4 * simData->simulationInfo.numSteps;
if(isInteractiveSimulation() || sim_noemit || 0 == strcmp("empty", simData->simulationInfo.outputFormat)) {
Expand Down

0 comments on commit 2cc65e1

Please sign in to comment.