Skip to content

Commit 58a87b7

Browse files
committed
Avoid buffers when generating constant literals
This is used to test future enhancements in ticket:3356. One of the limitations for writing directly to files is that no temporary buffers or texts are created.
1 parent dd4ad77 commit 58a87b7

File tree

2 files changed

+32
-28
lines changed

2 files changed

+32
-28
lines changed

Compiler/Template/CodegenC.tpl

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5077,18 +5077,15 @@ end equationIfEquationAssign;
50775077
"Generates the content of the C file for literals in the simulation case.
50785078
used in Compiler/Template/CodegenFMU.tpl"
50795079
::=
5080-
let &preLit = buffer ""
5081-
let res = literals |> literal hasindex i0 fromindex 0 =>
5082-
(if typeinfo() then '/* <%Util.escapeModelicaStringToCString(printExpStr(literal))%> */<%\n%>') +
5083-
literalExpConst(literal,i0, &preLit)
5084-
; separator="\n";empty
50855080
<<
50865081
#ifdef __cplusplus
50875082
extern "C" {
50885083
#endif
50895084
5090-
<%preLit%>
5091-
<%res%>
5085+
<%literals |> literal hasindex i0 fromindex 0 =>
5086+
(if typeinfo() then '/* <%Util.escapeModelicaStringToCString(printExpStr(literal))%> */<%\n%>') +
5087+
literalExpConst(literal,i0)
5088+
; separator="\n"; empty %>
50925089
50935090
#ifdef __cplusplus
50945091
}

Compiler/Template/CodegenCFunctions.tpl

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,10 @@ template functionsFile(String filePrefix,
148148
Text &staticPrototypes)
149149
"Generates the contents of the main C file for the function case."
150150
::=
151-
let &preLit = buffer ""
152-
let literalsRes = literals |> literal hasindex i0 fromindex 0 => literalExpConst(literal,i0,&preLit) ; separator="\n";empty
153151
<<
154152
#include "<%filePrefix%>.h"
155-
<% preLit %>
156153
<% /* Note: The literals may not be part of the header due to separate compilation */
157-
literalsRes
154+
literals |> literal hasindex i0 fromindex 0 => literalExpConst(literal,i0) ; separator="\n";empty
158155
%>
159156
#include "util/modelica.h"
160157

@@ -3344,7 +3341,7 @@ end functionsParModelicaKernelsFile;
33443341
/* adpro: leave a newline at the end of file to get rid of warnings! */
33453342
end recordsFile;
33463343

3347-
template literalExpConst(Exp lit, Integer litindex, Text &preLit) "These should all be declared static X const"
3344+
template literalExpConst(Exp lit, Integer litindex) "These should all be declared static X const"
33483345
::=
33493346
let name = '_OMC_LIT<%litindex%>'
33503347
let tmp = '_OMC_LIT_STRUCT<%litindex%>'
@@ -3404,56 +3401,53 @@ template literalExpConst(Exp lit, Integer litindex, Text &preLit) "These should
34043401
case CONS(__) then
34053402
/* We need to use #define's to be C-compliant. Yea, total crap :) */
34063403
<<
3407-
static const MMC_DEFSTRUCTLIT(<%tmp%>,2,1) {<%literalExpConstBoxedVal(car,litindex + "_car", &preLit)%>,<%literalExpConstBoxedVal(cdr, litindex + "_cdr", &preLit)%>}};
3404+
<% literalExpConstBoxedValPreLit(car, litindex + "_car") %><% literalExpConstBoxedValPreLit(cdr, litindex + "_cdr")
3405+
%>static const MMC_DEFSTRUCTLIT(<%tmp%>,2,1) {<%literalExpConstBoxedVal(car,litindex + "_car")%>,<%literalExpConstBoxedVal(cdr, litindex + "_cdr")%>}};
34083406
#define <%name%> MMC_REFSTRUCTLIT(<%tmp%>)
34093407
>>
34103408
case LIST(__) then
34113409
let x = listReverse(valList) |> v hasindex i fromindex 1 =>
34123410
/* We need to use #define's to be C-compliant. Yea, total crap :) */
3413-
'static const MMC_DEFSTRUCTLIT(<%tmp + "_cons_" + i%>,2,1) {<%literalExpConstBoxedVal(v,tmp + "_elt_" + i, &preLit)%>,MMC_REFSTRUCTLIT(<% match i case 1 then "mmc_nil" else (tmp + "_cons_" + intSub(i,1))%>)}};<%\n%>'
3411+
'<%literalExpConstBoxedValPreLit(v,tmp + "_elt_" + i)%>static const MMC_DEFSTRUCTLIT(<%tmp + "_cons_" + i%>,2,1) {<%literalExpConstBoxedVal(v,tmp + "_elt_" + i)%>,MMC_REFSTRUCTLIT(<% match i case 1 then "mmc_nil" else (tmp + "_cons_" + intSub(i,1))%>)}};<%\n%>'
34143412
<<
34153413
<%x%>
34163414
#define <%name%> MMC_REFSTRUCTLIT(<%tmp%>_cons_<%listLength(valList)%>)
34173415
>>
34183416
case META_TUPLE(__) then
34193417
/* We need to use #define's to be C-compliant. Yea, total crap :) */
34203418
<<
3421-
static const <%
3419+
<%listExp |> exp hasindex i0 => literalExpConstBoxedValPreLit(exp,litindex+"_"+i0) ; empty
3420+
%>static const <%
34223421
if listEmpty(listExp) then 'MMC_DEFSTRUCT0LIT(<%tmp%>,0)' else 'MMC_DEFSTRUCTLIT(<%tmp%>,<%listLength(listExp)%>,0)'
3423-
%> {<%listExp |> exp hasindex i0 => literalExpConstBoxedVal(exp,litindex+"_"+i0, &preLit); separator=","%>}};
3422+
%> {<%listExp |> exp hasindex i0 => literalExpConstBoxedVal(exp,litindex+"_"+i0); separator=","%>}};
34243423
#define <%name%> MMC_REFSTRUCTLIT(<%tmp%>)
34253424
>>
34263425
case META_OPTION(exp=SOME(exp)) then
34273426
/* We need to use #define's to be C-compliant. Yea, total crap :) */
34283427
<<
3429-
static const MMC_DEFSTRUCTLIT(<%tmp%>,1,1) {<%literalExpConstBoxedVal(exp,litindex+"_1", &preLit)%>}};
3428+
<%literalExpConstBoxedValPreLit(exp,litindex+"_1")
3429+
%>static const MMC_DEFSTRUCTLIT(<%tmp%>,1,1) {<%literalExpConstBoxedVal(exp,litindex+"_1")%>}};
34303430
#define <%name%> MMC_REFSTRUCTLIT(<%tmp%>)
34313431
>>
34323432
case METARECORDCALL(__) then
34333433
/* We need to use #define's to be C-compliant. Yea, total crap :) */
34343434
let newIndex = getValueCtor(index)
34353435
<<
3436-
static const MMC_DEFSTRUCTLIT(<%tmp%>,<%intAdd(1,listLength(args))%>,<%newIndex%>) {&<%underscorePath(path)%>__desc,<%args |> exp hasindex i0 => literalExpConstBoxedVal(exp,litindex+"_"+i0, &preLit); separator=","%>}};
3436+
<%args |> exp hasindex i0 => literalExpConstBoxedValPreLit(exp,litindex+"_"+i0) ; empty
3437+
%>static const MMC_DEFSTRUCTLIT(<%tmp%>,<%intAdd(1,listLength(args))%>,<%newIndex%>) {&<%underscorePath(path)%>__desc,<%args |> exp hasindex i0 => literalExpConstBoxedVal(exp,litindex+"_"+i0); separator=","%>}};
34373438
#define <%name%> MMC_REFSTRUCTLIT(<%tmp%>)
34383439
>>
34393440
else error(sourceInfo(), 'literalExpConst failed: <%printExpStr(lit)%>')
34403441
end literalExpConst;
34413442

3442-
template literalExpConstBoxedVal(Exp lit, Text index, Text &preLit)
3443+
template literalExpConstBoxedVal(Exp lit, Text index)
34433444
::=
34443445
let name = '_OMC_LIT<%index%>'
3445-
let tmp = '_OMC_LIT_STRUCT<%index%>'
34463446
match lit
34473447
case ICONST(__) then 'MMC_IMMEDIATE(MMC_TAGFIXNUM(<%integer%>))'
34483448
case ENUM_LITERAL(__) then 'MMC_IMMEDIATE(MMC_TAGFIXNUM(<%index%>))'
34493449
case lit as BCONST(__) then 'MMC_IMMEDIATE(MMC_TAGFIXNUM(<%boolStrC(lit.bool)%>))'
3450-
case lit as RCONST(__) then
3451-
let &preLit +=
3452-
<<
3453-
static const MMC_DEFREALLIT(<%tmp%>,<%lit.real%>);
3454-
#define <%name%> MMC_REFREALLIT(<%tmp%>)<%\n%>
3455-
>>
3456-
name
3450+
case lit as RCONST(__) then name
34573451
case LIST(valList={}) then
34583452
<<
34593453
MMC_REFSTRUCTLIT(mmc_nil)
@@ -3462,11 +3456,24 @@ template literalExpConstBoxedVal(Exp lit, Text index, Text &preLit)
34623456
<<
34633457
MMC_REFSTRUCTLIT(mmc_none)
34643458
>>
3465-
case lit as BOX(__) then literalExpConstBoxedVal(lit.exp, index, &preLit)
3459+
case lit as BOX(__) then literalExpConstBoxedVal(lit.exp, index)
34663460
case lit as SHARED_LITERAL(__) then '_OMC_LIT<%lit.index%>'
34673461
else error(sourceInfo(), 'literalExpConstBoxedVal failed: <%printExpStr(lit)%>')
34683462
end literalExpConstBoxedVal;
34693463

3464+
template literalExpConstBoxedValPreLit(Exp lit, Text index)
3465+
::=
3466+
match lit
3467+
case lit as RCONST(__) then
3468+
let tmp = '_OMC_LIT_STRUCT<%index%>'
3469+
let name = '_OMC_LIT<%index%>'
3470+
<<
3471+
static const MMC_DEFREALLIT(<%tmp%>,<%lit.real%>);
3472+
#define <%name%> MMC_REFREALLIT(<%tmp%>)<%\n%>
3473+
>>
3474+
case lit as BOX(__) then literalExpConstBoxedValPreLit(lit.exp, index)
3475+
end literalExpConstBoxedValPreLit;
3476+
34703477
template literalExpConstArrayVal(Exp lit)
34713478
::=
34723479
match lit

0 commit comments

Comments
 (0)