Skip to content

Commit

Permalink
- move a fiew functions to CodegenUtil.mo
Browse files Browse the repository at this point in the history
- add some dependencies in makefile

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@12160 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Jens Frenkel committed Jun 20, 2012
1 parent 4df27f0 commit 1e1e6f8
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 71 deletions.
65 changes: 0 additions & 65 deletions Compiler/Template/CodegenC.tpl
Expand Up @@ -2361,17 +2361,6 @@ template initVal(Exp initialValue)
else error(sourceInfo(), 'initial value of unknown type: <%printExpStr(initialValue)%>')
end initVal;

template initValXml(Exp initialValue)
::=
match initialValue
case ICONST(__) then integer
case RCONST(__) then real
case SCONST(__) then '<%Util.escapeModelicaStringToXmlString(string)%>'
case BCONST(__) then if bool then "true" else "false"
case ENUM_LITERAL(__) then '<%index%> /*ENUM:<%dotPath(name)%>*/'
else error(sourceInfo(), 'initial value of unknown type: <%printExpStr(initialValue)%>')
end initValXml;

template commonHeader()
::=
<<
Expand Down Expand Up @@ -2524,17 +2513,6 @@ template subscriptToCStr(Subscript subscript)
else "UNKNOWN_SUBSCRIPT"
end subscriptToCStr;

template crefStr(ComponentRef cr)
"Generates the name of a variable for variable name array."
::=
match cr
case CREF_IDENT(__) then '<%ident%><%subscriptsStr(subscriptLst)%>'
// Are these even needed? Function context should only have CREF_IDENT :)
case CREF_QUAL(ident = "$DER") then 'der(<%crefStr(componentRef)%>)'
case CREF_QUAL(__) then '<%ident%><%subscriptsStr(subscriptLst)%>.<%crefStr(componentRef)%>'
else "CREF_NOT_IDENT_OR_QUAL"
end crefStr;

template crefM(ComponentRef cr)
"Generates Modelica equivalent name for component reference."
::=
Expand Down Expand Up @@ -2603,25 +2581,6 @@ template arrayCrefStr(ComponentRef cr)
else "CREF_NOT_IDENT_OR_QUAL"
end arrayCrefStr;

template subscriptsStr(list<Subscript> subscripts)
"Generares subscript part of the name."
::=
if subscripts then
'[<%subscripts |> s => subscriptStr(s) ;separator=","%>]'
end subscriptsStr;

template subscriptStr(Subscript subscript)
"Generates a single subscript.
Only works for constant integer indicies."

::=
match subscript
case INDEX(exp=ICONST(integer=i)) then i
case SLICE(exp=ICONST(integer=i)) then i
case WHOLEDIM(__) then "WHOLEDIM"
else "UNKNOWN_SUBSCRIPT"
end subscriptStr;

template expCref(DAE.Exp ecr)
::=
match ecr
Expand Down Expand Up @@ -6979,30 +6938,6 @@ template equationInfo(list<SimEqSystem> eqs, Text &eqnsDefines)
>>
end equationInfo;


template error(Absyn.Info srcInfo, String errMessage)
"Example source template error reporting template to be used together with the sourceInfo() magic function.
Usage: error(sourceInfo(), <<message>>) "
::=
let() = Tpl.addSourceTemplateError(errMessage, srcInfo)
<<

#error "<% Error.infoStr(srcInfo) %> <% errMessage %>"<%\n%>
>>
end error;

//for completeness; although the error() template above is preferable
template errorMsg(String errMessage)
"Example template error reporting template
that is reporting only the error message without the usage of source infotmation."
::=
let() = Tpl.addTemplateError(errMessage)
<<

#error "<% errMessage %>"<%\n%>
>>
end errorMsg;

template ModelVariables(ModelInfo modelInfo)
"Generates code for ModelVariables file for FMU target."
::=
Expand Down
2 changes: 1 addition & 1 deletion Compiler/Template/CodegenFMU.tpl
Expand Up @@ -494,7 +494,7 @@ template dervativeNameCStyle(ComponentRef cr)
"Generates the name of a derivative in c style, replaces ( with _"
::=
match cr
case CREF_QUAL(ident = "$DER") then 'der_<%CodegenC.crefStr(componentRef)%>_'
case CREF_QUAL(ident = "$DER") then 'der_<%crefStr(componentRef)%>_'
end dervativeNameCStyle;

template DefineVariables(SimVar simVar)
Expand Down
76 changes: 76 additions & 0 deletions Compiler/Template/CodegenUtil.tpl
Expand Up @@ -47,6 +47,53 @@ package CodegenUtil

import interface SimCodeTV;

template crefStr(ComponentRef cr)
"Generates the name of a variable for variable name array."
::=
match cr
case CREF_IDENT(__) then '<%ident%><%subscriptsStr(subscriptLst)%>'
// Are these even needed? Function context should only have CREF_IDENT :)
case CREF_QUAL(ident = "$DER") then 'der(<%crefStr(componentRef)%>)'
case CREF_QUAL(__) then '<%ident%><%subscriptsStr(subscriptLst)%>.<%crefStr(componentRef)%>'
else "CREF_NOT_IDENT_OR_QUAL"
end crefStr;

template subscriptsStr(list<Subscript> subscripts)
"Generares subscript part of the name."
::=
if subscripts then
'[<%subscripts |> s => subscriptStr(s) ;separator=","%>]'
end subscriptsStr;

template subscriptStr(Subscript subscript)
"Generates a single subscript.
Only works for constant integer indicies."

::=
match subscript
case INDEX(exp=ICONST(integer=i)) then i
case SLICE(exp=ICONST(integer=i)) then i
case WHOLEDIM(__) then "WHOLEDIM"
else "UNKNOWN_SUBSCRIPT"
end subscriptStr;


template initValXml(Exp initialValue)
::=
match initialValue
case ICONST(__) then integer
case RCONST(__) then real
case SCONST(__) then '<%Util.escapeModelicaStringToXmlString(string)%>'
case BCONST(__) then if bool then "true" else "false"
case ENUM_LITERAL(__) then '<%index%> /*ENUM:<%dotPath(name)%>*/'
else error(sourceInfo(), 'initial value of unknown type: <%printExpStr(initialValue)%>')
end initValXml;

/*********************************************************************
*********************************************************************
* Paths
*********************************************************************
*********************************************************************/

template dotPath(Path path)
"Generates paths with components separated by dots."
Expand All @@ -58,6 +105,35 @@ template dotPath(Path path)
end dotPath;


/*********************************************************************
*********************************************************************
* Error
*********************************************************************
*********************************************************************/

template error(Absyn.Info srcInfo, String errMessage)
"Example source template error reporting template to be used together with the sourceInfo() magic function.
Usage: error(sourceInfo(), <<message>>) "
::=
let() = Tpl.addSourceTemplateError(errMessage, srcInfo)
<<

#error "<% Error.infoStr(srcInfo) %> <% errMessage %>"<%\n%>
>>
end error;

//for completeness; although the error() template above is preferable
template errorMsg(String errMessage)
"Example template error reporting template
that is reporting only the error message without the usage of source infotmation."
::=
let() = Tpl.addTemplateError(errMessage)
<<

#error "<% errMessage %>"<%\n%>
>>
end errorMsg;

end CodegenUtil;

// vim: filetype=susan sw=2 sts=2
8 changes: 4 additions & 4 deletions Compiler/Template/Makefile
Expand Up @@ -19,7 +19,7 @@ CodegenUtil.mo : CodegenUtil.tpl SimCodeTV.mo
$(OMC) $< > $@.log || (cat $@.log && false)
@echo " "

CodegenC.mo : CodegenC.tpl SimCodeTV.mo
CodegenC.mo : CodegenC.tpl SimCodeTV.mo CodegenUtil.tpl
@echo " ** CodegenC template compilation ** "
$(OMC) $< > $@.log || (cat $@.log && false)
@echo " "
Expand All @@ -29,7 +29,7 @@ CodegenCSharp.mo : CodegenCSharp.tpl SimCodeTV.mo
$(OMC) $< > $@.log || (cat $@.log && false)
@echo " "

CodegenFMU.mo : CodegenFMU.tpl SimCodeTV.mo CodegenC.tpl
CodegenFMU.mo : CodegenFMU.tpl SimCodeTV.mo CodegenC.tpl CodegenUtil.tpl
@echo " ** CodegenFMU template compilation ** "
$(OMC) $< > $@.log || (cat $@.log && false)
@echo " "
Expand All @@ -44,12 +44,12 @@ Unparsing.mo : Unparsing.tpl SimCodeTV.mo
$(OMC) $< > $@.log || (cat $@.log && false)
@echo " "

SimCodeDump.mo : SimCodeDump.tpl SimCodeTV.mo CodegenC.tpl
SimCodeDump.mo : SimCodeDump.tpl SimCodeTV.mo CodegenUtil.tpl
@echo " ** SimCodeDump template compilation ** "
$(OMC) $< > $@.log || (cat $@.log && false)
@echo " "

CodegenQSS.mo : CodegenQSS.tpl SimCodeTV.mo CodegenC.tpl
CodegenQSS.mo : CodegenQSS.tpl SimCodeTV.mo CodegenC.tpl CodegenUtil.tpl
@echo " ** CodegenQSS template compilation ** "
$(OMC) $< > $@.log || (cat $@.log && false)
@echo " "
Expand Down
1 change: 0 additions & 1 deletion Compiler/Template/SimCodeDump.tpl
Expand Up @@ -2,7 +2,6 @@ package SimCodeDump

import interface SimCodeTV;
import CodegenUtil.*;
import CodegenC.*;

template dumpSimCode(SimCode code)
::=
Expand Down

0 comments on commit 1e1e6f8

Please sign in to comment.