Skip to content

Commit 17d636b

Browse files
committed
- first MetaModelica GC draft, disabled for now
- see more in c_runtime/meta_modelica_gc.h - added GC bindings to: Compiler/susan_codegen/SimCode/SimCodeC.tpl, commented out for now - made all structures non-constant to allow marking/unmarking. git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@8092 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent e6ddb2e commit 17d636b

16 files changed

+1693
-44
lines changed

Compiler/susan_codegen/SimCode/SimCodeC.tpl

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3227,6 +3227,16 @@ template functionBody(Function fn, Boolean inFunc)
32273227
case fn as RECORD_CONSTRUCTOR(__) then functionBodyRecordConstructor(fn)
32283228
end functionBody;
32293229

3230+
template addRoots(Variable var)
3231+
"template to add the roots, only the meta-types are added!"
3232+
::=
3233+
match var
3234+
case var as VARIABLE(__) then
3235+
match ty
3236+
case ET_METATYPE(__)
3237+
case ET_BOXED(__) then 'mmc_GC_add_root(<%contextCref(var.name,contextFunction)%>);'
3238+
end match
3239+
end addRoots;
32303240

32313241
template functionBodyRegularFunction(Function fn, Boolean inFunc)
32323242
"Generates the body for a Modelica/MetaModelica function."
@@ -3243,6 +3253,8 @@ case FUNCTION(__) then
32433253
let _ = (variableDeclarations |> var hasindex i1 from 1 =>
32443254
varInit(var, "", i1, &varDecls /*BUFD*/, &varInits /*BUFC*/)
32453255
)
3256+
let addRootsInputs = (functionArguments |> var => addRoots(var) ;separator="\n")
3257+
let addRootsOutputs = (outVars |> var => addRoots(var) ;separator="\n")
32463258
let funArgs = (functionArguments |> var => functionArg(var, &varInits) ;separator="\n")
32473259
let bodyPart = (body |> stmt => funStatement(stmt, &varDecls /*BUFD*/) ;separator="\n")
32483260
let &outVarInits = buffer ""
@@ -3256,6 +3268,11 @@ case FUNCTION(__) then
32563268
<<
32573269
<%retType%> _<%fname%>(<%functionArguments |> var => funArgDefinition(var) ;separator=", "%>)
32583270
{
3271+
/* disable GC for now
3272+
<%if acceptMetaModelicaGrammar() then '<%addRootsInputs%>'%>
3273+
<%if acceptMetaModelicaGrammar() then if addRootsInputs then 'mmc_GC_push_roots_mark();'%>
3274+
*/
3275+
32593276
<%funArgs%>
32603277
<%varDecls%>
32613278
<%outVarInits%>
@@ -3269,6 +3286,21 @@ case FUNCTION(__) then
32693286
<%outVarCopy%>
32703287
<%if not acceptMetaModelicaGrammar() then 'restore_memory_state(<%stateVar%>);'%>
32713288
<%outVarAssign%>
3289+
3290+
/* disable GC for now
3291+
<%if acceptMetaModelicaGrammar() then '<%addRootsOutputs%>'%>
3292+
<%if acceptMetaModelicaGrammar() then if addRootsOutputs then 'mmc_GC_push_roots_mark();'%>
3293+
<%
3294+
if acceptMetaModelicaGrammar()
3295+
then if addRootsInputs
3296+
then 'mmc_GC_collect();'
3297+
else if addRootsOutputs
3298+
then 'mmc_GC_collect();'
3299+
%>
3300+
<%if acceptMetaModelicaGrammar() then if addRootsOutputs then 'mmc_GC_pop_roots_mark();'%>
3301+
<%if acceptMetaModelicaGrammar() then if addRootsInputs then 'mmc_GC_pop_roots_mark();'%>
3302+
*/
3303+
32723304
return<%if outVars then ' <%retVar%>' %>;
32733305
}
32743306

@@ -3992,7 +4024,7 @@ template algStatement(DAE.Statement stmt, Context context, Text &varDecls /*BUFP
39924024
case s as STMT_THROW(__) then 'MMC_THROW();<%\n%>'
39934025
case s as STMT_RETURN(__) then 'goto _return;<%\n%>'
39944026
case s as STMT_NORETCALL(__) then algStmtNoretcall(s, context, &varDecls /*BUFD*/)
3995-
case s as STMT_REINIT(__) then algStmtReinit(s, context, &varDecls /*BUFD*/)
4027+
case s as STMT_REINIT(__) then algStmtReinit(s, context, &varDecls /*BUFD*/)
39964028
else "#error NOT_IMPLEMENTED_ALG_STATEMENT"
39974029
<<
39984030
/*#modelicaLine <%statementInfoString(stmt)%>*/
@@ -6390,7 +6422,7 @@ template literalExpConst(Exp lit, Integer index) "These should all be declared s
63906422
::=
63916423
let name = '_OMC_LIT<%index%>'
63926424
let tmp = '_OMC_LIT_STRUCT<%index%>'
6393-
let meta = 'static modelica_metatype const <%name%>'
6425+
let meta = 'static modelica_metatype /* const */ <%name%>'
63946426
match lit
63956427
case SCONST(__) then
63966428
let escstr = Util.escapeModelicaStringToCString(string)
@@ -6403,7 +6435,7 @@ template literalExpConst(Exp lit, Integer index) "These should all be declared s
64036435
<<
64046436
#define <%name%>_data "<%escstr%>"
64056437
static const size_t <%name%>_strlen = <%unescapedStringLength(escstr)%>;
6406-
static const MMC_DEFSTRINGLIT(<%tmp%>,<%name%>_strlen,<%name%>_data);
6438+
static /* const */ MMC_DEFSTRINGLIT(<%tmp%>,<%name%>_strlen,<%name%>_data);
64076439
<%meta%> = MMC_REFSTRINGLIT(<%tmp%>);
64086440
>>
64096441
else
@@ -6422,28 +6454,28 @@ template literalExpConst(Exp lit, Integer index) "These should all be declared s
64226454
>>
64236455
case BOX(exp=exp as RCONST(__)) then
64246456
<<
6425-
static const MMC_DEFREALLIT(<%tmp%>,<%exp.real%>);
6457+
static /* const */ MMC_DEFREALLIT(<%tmp%>,<%exp.real%>);
64266458
<%meta%> = MMC_REFREALLIT(<%tmp%>);
64276459
>>
64286460
case CONS(__) then
64296461
<<
6430-
static const MMC_DEFSTRUCTLIT(<%tmp%>,2,1) {<%literalExpConstBoxedVal(car)%>,<%literalExpConstBoxedVal(cdr)%>}};
6462+
static /* const */ MMC_DEFSTRUCTLIT(<%tmp%>,2,1) {<%literalExpConstBoxedVal(car)%>,<%literalExpConstBoxedVal(cdr)%>}};
64316463
<%meta%> = MMC_REFSTRUCTLIT(<%tmp%>);
64326464
>>
64336465
case META_TUPLE(__) then
64346466
<<
6435-
static const MMC_DEFSTRUCTLIT(<%tmp%>,<%listLength(listExp)%>,0) {<%listExp |> exp => literalExpConstBoxedVal(exp); separator=","%>}};
6467+
static /* const */ MMC_DEFSTRUCTLIT(<%tmp%>,<%listLength(listExp)%>,0) {<%listExp |> exp => literalExpConstBoxedVal(exp); separator=","%>}};
64366468
<%meta%> = MMC_REFSTRUCTLIT(<%tmp%>);
64376469
>>
64386470
case META_OPTION(exp=SOME(exp)) then
64396471
<<
6440-
static const MMC_DEFSTRUCTLIT(<%tmp%>,1,1) {<%literalExpConstBoxedVal(exp)%>}};
6472+
static /* const */ MMC_DEFSTRUCTLIT(<%tmp%>,1,1) {<%literalExpConstBoxedVal(exp)%>}};
64416473
<%meta%> = MMC_REFSTRUCTLIT(<%tmp%>);
64426474
>>
64436475
case METARECORDCALL(__) then
64446476
let newIndex = getValueCtor(index)
64456477
<<
6446-
static const MMC_DEFSTRUCTLIT(<%tmp%>,<%intAdd(1,listLength(args))%>,<%newIndex%>) {&<%underscorePath(path)%>__desc,<%args |> exp => literalExpConstBoxedVal(exp); separator=","%>}};
6478+
static /* const */ MMC_DEFSTRUCTLIT(<%tmp%>,<%intAdd(1,listLength(args))%>,<%newIndex%>) {&<%underscorePath(path)%>__desc,<%args |> exp => literalExpConstBoxedVal(exp); separator=","%>}};
64476479
<%meta%> = MMC_REFSTRUCTLIT(<%tmp%>);
64486480
>>
64496481
else '<%\n%>#error "literalExpConst failed: <%printExpStr(lit)%>"<%\n%>'

c_runtime/Makefile.common

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ daux.o dlamch.o dpmpar.o hybrd.o lsame.o newuob.o r1mpyq.o update.o
1717
java_interface.o meta_modelica.o meta_modelica_builtin.o meta_modelica_real.o \
1818
meta_modelica_string_lit.o meta_modelica_catch.o rtclock.o ModelicaUtilities.o \
1919
simulation_varinfo.o read_matlab4.o \
20+
meta_modelica_gc.o meta_modelica_gc_list.o meta_modelica_gc_stack.o \
21+
meta_modelica_gc_roots.o meta_modelica_gc_pages.o meta_modelica_gc_stats.o \
2022
$(EXTRA_OBJS)
2123

2224
SIMOBJS = $(FOBJS) simulation_runtime.o simulation_init.o simulation_input.o \
@@ -45,6 +47,7 @@ matrix.h \
4547
memory_pool.h \
4648
meta_modelica_builtin.h \
4749
meta_modelica_builtin_boxptr.h \
50+
meta_modelica_gc.h \
4851
meta_modelica_real.h \
4952
meta_modelica_string_lit.h \
5053
meta_modelica.h \
@@ -74,6 +77,11 @@ simulation_varinfo.h \
7477
solver_dasrt.h \
7578
solver_main.h \
7679
string_array.h \
80+
meta_modelica_gc_stack.h \
81+
meta_modelica_gc_list.h \
82+
meta_modelica_gc_roots.h \
83+
meta_modelica_gc_pages.h \
84+
meta_modelica_gc_stats.h \
7785
utility.h \
7886
linearize.h \
7987
fmu_model_interface.h \

c_runtime/meta_modelica.c

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -29,37 +29,10 @@
2929
*
3030
*/
3131

32-
3332
#include "modelica.h"
3433

3534
jmp_buf *mmc_jumper;
3635

37-
void *mmc_alloc_bytes(unsigned nbytes)
38-
{
39-
static char *mmc_cur_malloc_buf = NULL;
40-
static long mmc_cur_malloc_buf_ix=0;
41-
/* Until we have GC, we simply allocate in 256MB chunks... */
42-
const long mmc_cur_malloc_buf_sz=256*1024*1024; /* 256MB chunks */
43-
void *p=0;
44-
/* fprintf(stderr, "1 mmc_alloc_bytes(%ld): %ld,%ld\n", nbytes, mmc_cur_malloc_buf, mmc_cur_malloc_buf_ix); */
45-
if (mmc_cur_malloc_buf == NULL || nbytes>(mmc_cur_malloc_buf_sz-mmc_cur_malloc_buf_ix)) {
46-
if ( (mmc_cur_malloc_buf = malloc(mmc_cur_malloc_buf_sz)) == 0 ) {
47-
fflush(NULL);
48-
fprintf(stderr, "malloc(%u) failed: %s\n", nbytes, strerror(errno));
49-
assert(p != 0);
50-
}
51-
mmc_cur_malloc_buf_ix = 0;
52-
assert(nbytes <= mmc_cur_malloc_buf_sz);
53-
}
54-
p = mmc_cur_malloc_buf + mmc_cur_malloc_buf_ix;
55-
56-
/* Force 16-byte alignment, like malloc... TODO: Check if this is needed :) */
57-
mmc_cur_malloc_buf_ix += nbytes; /* + ((nbytes%16) ? 16-(nbytes%16): 0); */
58-
59-
/* fprintf(stderr, "2 mmc_alloc_bytes(%ld): %ld,%ld => %ld\n", nbytes, mmc_cur_malloc_buf, mmc_cur_malloc_buf_ix, p); */
60-
return p;
61-
}
62-
6336
void* mmc_mk_rcon(double d)
6437
{
6538
struct mmc_real *p = mmc_alloc_words(sizeof(struct mmc_real)/MMC_SIZE_INT);

c_runtime/meta_modelica.h

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#define META_MODELICA_H_
4040

4141
#include "modelica.h"
42+
#include "meta_modelica_gc.h"
4243
#include "meta_modelica_string_lit.h"
4344

4445
#if defined(__cplusplus)
@@ -77,6 +78,7 @@ typedef int mmc_sint_t;
7778
#define MMC_CDR(X) MMC_FETCH(MMC_OFFSET(MMC_UNTAGPTR(X),2))
7879
#define MMC_NILTEST(x) (MMC_GETHDR(x) == MMC_NILHDR)
7980
#define MMC_IMMEDIATE(i) ((void*)(i))
81+
#define MMC_IS_IMMEDIATE(x) (!((mmc_uint_t)(x) & 1))
8082
#define MMC_TAGFIXNUM(i) ((i) << 1)
8183
#define MMC_UNTAGFIXNUM(X) (((mmc_sint_t) X) >> 1)
8284
#define MMC_REALHDR (((MMC_SIZE_DBL/MMC_SIZE_INT) << 10) + 9)
@@ -93,6 +95,20 @@ typedef int mmc_sint_t;
9395
#define MMC_ARRAY_TAG 255
9496
#define MMC_STRLEN(x) (MMC_HDRSTRLEN(MMC_GETHDR(x)))
9597

98+
/*
99+
* adrpo: if a structure has pointers
100+
* Bit 0 is zero if the node contains pointers, 1 otherwise.
101+
*/
102+
#define MMC_HDRHASPTRS(hdr) (!((hdr) & 1))
103+
/*
104+
* adrpo: if this object was marked, used by GC!
105+
* [xxxxxxxx1x] (used during garbage collection) a marked node;
106+
*/
107+
#define MMC_HDRISMARKED(hdr) ((hdr) & 2)
108+
#define MMC_HDR_MARK(hdr) ((hdr) | 2)
109+
#define MMC_HDR_UNMARK(hdr) ((hdr) & ~2)
110+
111+
96112
#define MMC_INT_MAX ((1<<30)-1)
97113
#define MMC_INT_MIN (-(1<<30))
98114

@@ -138,13 +154,6 @@ struct mmc_string {
138154
char data[1]; /* `bytes' elements + terminating '\0' */
139155
};
140156

141-
void *mmc_alloc_bytes(unsigned nbytes);
142-
143-
static void *mmc_alloc_words(unsigned nwords)
144-
{
145-
return mmc_alloc_bytes(nwords * sizeof(void*));
146-
}
147-
148157
#define MMC_FALSE (mmc_mk_icon(0))
149158
#define MMC_TRUE (mmc_mk_icon(1))
150159
#define mmc_mk_bcon(X) ((X) != 0 ? MMC_TRUE : MMC_FALSE)
@@ -305,8 +314,8 @@ static inline void *mmc_mk_box(int slots, unsigned ctor, ...)
305314
return MMC_TAGPTR(p);
306315
}
307316

308-
static const MMC_DEFSTRUCT0LIT(mmc_nil,0);
309-
static const MMC_DEFSTRUCT0LIT(mmc_none,1);
317+
static /* const */ MMC_DEFSTRUCT0LIT(mmc_nil,0);
318+
static /* const */ MMC_DEFSTRUCT0LIT(mmc_none,1);
310319

311320
#define mmc_mk_nil() MMC_REFSTRUCTLIT(mmc_nil)
312321
#define mmc_mk_none() MMC_REFSTRUCTLIT(mmc_none)

0 commit comments

Comments
 (0)