Skip to content

Commit

Permalink
Do not use preprocessor for attributes
Browse files Browse the repository at this point in the history
Use the index stored in the SimVar directly instead of using the C
preprocessor to find the correct memory location for attributes.

Also changed SimCode to be accessible as a global root in the templates
since it is used in so many places.
  • Loading branch information
sjoelund authored and OpenModelica-Hudson committed May 20, 2016
1 parent 04496ac commit f098371
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 67 deletions.
1 change: 1 addition & 0 deletions Compiler/Global/Global.mo
Expand Up @@ -58,6 +58,7 @@ constant Integer gcProfilingIndex = 20;
constant Integer inlineHashTable = 21; // TODO: Should be a local root?
constant Integer currentInstVar = 22;
constant Integer operatorOverloadingCache = 23;
constant Integer optionSimCode = 24;

// indexes in System.tick
// ----------------------
Expand Down
2 changes: 2 additions & 0 deletions Compiler/SimCode/SimCodeFunction.mo
Expand Up @@ -44,6 +44,7 @@ import SimCode;
protected
import BaseHashTable;
import CodegenCFunctions;
import Global;
import SimCodeFunctionUtil;

public function translateFunctions "
Expand All @@ -56,6 +57,7 @@ public function translateFunctions "
input list<DAE.Type> metarecordTypes;
input list<String> inIncludes;
algorithm
setGlobalRoot(Global.optionSimCode, NONE());
_ := match (program, name, optMainFunction, idaeElements, metarecordTypes, inIncludes)
local
DAE.Function daeMainFunction;
Expand Down
12 changes: 12 additions & 0 deletions Compiler/SimCode/SimCodeFunctionUtil.mo
Expand Up @@ -2905,5 +2905,17 @@ algorithm
outdef := stringDelimitList(List.threadMap(List.fill("i_", nrdims), idxstrlst, stringAppend), ",");
end generateSubPalceholders;

public function getSimCode
output SimCode.SimCode code;
protected
Option<SimCode.SimCode> ocode;
algorithm
ocode := getGlobalRoot(Global.optionSimCode);
code := match ocode
case SOME(code) then code;
else algorithm Error.addInternalError("Tried to generate code that requires the SimCode structure, but this is not set (function context?)", sourceInfo()); then fail();
end match;
end getSimCode;

annotation(__OpenModelica_Interface="backendInterface");
end SimCodeFunctionUtil;
5 changes: 5 additions & 0 deletions Compiler/SimCode/SimCodeMain.mo
Expand Up @@ -587,6 +587,7 @@ protected
output Boolean res;
end PartialRunTpl;
algorithm
setGlobalRoot(Global.optionSimCode, SOME(simCode));
_ := match target
local
String str, guid;
Expand Down Expand Up @@ -655,6 +656,7 @@ algorithm
else
true := max(l for l in System.launchParallelTasks(numThreads, codegenFuncs, runCodegenFunc));
end if;

then ();

case "JavaScript" equation
Expand Down Expand Up @@ -682,6 +684,7 @@ algorithm
Error.addMessage(Error.INTERNAL_ERROR, {str});
then fail();
end match;
setGlobalRoot(Global.optionSimCode, NONE());
end callTargetTemplates;

protected function dumpTaskSystemIfFlag
Expand Down Expand Up @@ -709,6 +712,7 @@ protected function callTargetTemplatesFMU
input String FMUVersion;
input String FMUType;
algorithm
setGlobalRoot(Global.optionSimCode, SOME(simCode));
_ := match (simCode,target)
local
String str;
Expand Down Expand Up @@ -743,6 +747,7 @@ algorithm
Error.addMessage(Error.INTERNAL_ERROR, {str});
then fail();
end match;
setGlobalRoot(Global.optionSimCode, NONE());
end callTargetTemplatesFMU;


Expand Down
104 changes: 44 additions & 60 deletions Compiler/Template/CodegenC.tpl

Large diffs are not rendered by default.

45 changes: 43 additions & 2 deletions Compiler/Template/CodegenCFunctions.tpl
Expand Up @@ -6338,11 +6338,11 @@ template daeExpCallStart(Exp exp, Context context, Text &preExp,
::=
match exp
case cr as CREF(__) then
'$P$ATTRIBUTE<%cref(cr.componentRef)%>.start'
'<%crefAttributes(cr.componentRef)%>.start'
case ASUB(exp = cr as CREF(__), sub = {sub_exp}) then
let offset = daeExp(sub_exp, context, &preExp, &varDecls, &auxFunction)
let cref = cref(cr.componentRef)
'*(&$P$ATTRIBUTE<%cref(cr.componentRef)%>.start + <%offset%>)'
'*(&<%crefAttributes(cr.componentRef)%>.start + <%offset%>)'
else
error(sourceInfo(), 'Code generation does not support start(<%ExpressionDumpTpl.dumpExp(exp,"\"")%>)')
end daeExpCallStart;
Expand Down Expand Up @@ -6840,6 +6840,47 @@ template daeSubscriptExp(Exp exp, Context context, Text &preExp, Text &varDecls,
else '<%res%>' /* <%expTypeFromExpModelica(exp)%> */
end daeSubscriptExp;

template crefShortType(ComponentRef cr) "template crefType
Like cref but with cast if type is integer."
::=
match cr
case CREF_IDENT(__) then '<%expTypeShort(identType)%>'
case CREF_QUAL(__) then '<%crefShortType(componentRef)%>'
else "crefType:ERROR"
end match
end crefShortType;

template varArrayName(SimVar var)
::=
match var
case SIMVAR(varKind=PARAM()) then '<%crefShortType(name)%>Parameter'
case SIMVAR(__) then '<%crefShortType(name)%>Vars'
end varArrayName;

template crefVarInfo(ComponentRef cr)
::=
match cref2simvar(cr, getSimCode())
case var as SIMVAR(__) then
'data->modelData-><%varArrayName(var)%>Data[<%index%>].info /* <%Util.escapeModelicaStringToCString(crefStr(name))%> */'
end crefVarInfo;

template varAttributes(SimVar var)
::=
match var
case SIMVAR(index=-1) then crefAttributes(name) // input variable?
case SIMVAR(__) then
'data->modelData-><%varArrayName(var)%>Data[<%index%>].attribute /* <%Util.escapeModelicaStringToCString(crefStr(name))%> */'
end varAttributes;

template crefAttributes(ComponentRef cr)
::=
match cref2simvar(cr, getSimCode())
case var as SIMVAR(index=-1, varKind=JAC_VAR()) then "dummyREAL_ATTRIBUTE"
case var as SIMVAR(index=-1) then error(sourceInfo(), 'varAttributes got index=-1 for <%crefStr(name)%>')
case var as SIMVAR(__) then
'data->modelData-><%varArrayName(var)%>Data[<%index%>].attribute /* <%Util.escapeModelicaStringToCString(crefStr(name))%> */'
end crefAttributes;

annotation(__OpenModelica_Interface="backend");
end CodegenCFunctions;

Expand Down
13 changes: 8 additions & 5 deletions Compiler/Template/SimCodeTV.mo
Expand Up @@ -403,12 +403,11 @@ package SimCode
end SIMULATION_CONTEXT;
record FUNCTION_CONTEXT
end FUNCTION_CONTEXT;
record JACOBIAN_CONTEXT
end JACOBIAN_CONTEXT;

record JACOBIAN_CONTEXT
end JACOBIAN_CONTEXT;
record ALGLOOP_CONTEXT
Boolean genInitialisation;
Boolean genJacobian;
Boolean genInitialisation;
Boolean genJacobian;
end ALGLOOP_CONTEXT;
record OTHER_CONTEXT
end OTHER_CONTEXT;
Expand Down Expand Up @@ -1042,6 +1041,10 @@ package SimCodeFunctionUtil
output String outdef;
end generateSubPalceholders;

function getSimCode
output SimCode.SimCode code;
end getSimCode;

end SimCodeFunctionUtil;

package BackendDAE
Expand Down

0 comments on commit f098371

Please sign in to comment.