Skip to content

Commit

Permalink
- first MetaModelica GC draft, disabled for now
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
adrpo committed Mar 4, 2011
1 parent e6ddb2e commit 17d636b
Show file tree
Hide file tree
Showing 16 changed files with 1,693 additions and 44 deletions.
48 changes: 40 additions & 8 deletions Compiler/susan_codegen/SimCode/SimCodeC.tpl
Expand Up @@ -3227,6 +3227,16 @@ template functionBody(Function fn, Boolean inFunc)
case fn as RECORD_CONSTRUCTOR(__) then functionBodyRecordConstructor(fn)
end functionBody;

template addRoots(Variable var)
"template to add the roots, only the meta-types are added!"
::=
match var
case var as VARIABLE(__) then
match ty
case ET_METATYPE(__)
case ET_BOXED(__) then 'mmc_GC_add_root(<%contextCref(var.name,contextFunction)%>);'
end match
end addRoots;

template functionBodyRegularFunction(Function fn, Boolean inFunc)
"Generates the body for a Modelica/MetaModelica function."
Expand All @@ -3243,6 +3253,8 @@ case FUNCTION(__) then
let _ = (variableDeclarations |> var hasindex i1 from 1 =>
varInit(var, "", i1, &varDecls /*BUFD*/, &varInits /*BUFC*/)
)
let addRootsInputs = (functionArguments |> var => addRoots(var) ;separator="\n")
let addRootsOutputs = (outVars |> var => addRoots(var) ;separator="\n")
let funArgs = (functionArguments |> var => functionArg(var, &varInits) ;separator="\n")
let bodyPart = (body |> stmt => funStatement(stmt, &varDecls /*BUFD*/) ;separator="\n")
let &outVarInits = buffer ""
Expand All @@ -3256,6 +3268,11 @@ case FUNCTION(__) then
<<
<%retType%> _<%fname%>(<%functionArguments |> var => funArgDefinition(var) ;separator=", "%>)
{
/* disable GC for now
<%if acceptMetaModelicaGrammar() then '<%addRootsInputs%>'%>
<%if acceptMetaModelicaGrammar() then if addRootsInputs then 'mmc_GC_push_roots_mark();'%>
*/
<%funArgs%>
<%varDecls%>
<%outVarInits%>
Expand All @@ -3269,6 +3286,21 @@ case FUNCTION(__) then
<%outVarCopy%>
<%if not acceptMetaModelicaGrammar() then 'restore_memory_state(<%stateVar%>);'%>
<%outVarAssign%>
/* disable GC for now
<%if acceptMetaModelicaGrammar() then '<%addRootsOutputs%>'%>
<%if acceptMetaModelicaGrammar() then if addRootsOutputs then 'mmc_GC_push_roots_mark();'%>
<%
if acceptMetaModelicaGrammar()
then if addRootsInputs
then 'mmc_GC_collect();'
else if addRootsOutputs
then 'mmc_GC_collect();'
%>
<%if acceptMetaModelicaGrammar() then if addRootsOutputs then 'mmc_GC_pop_roots_mark();'%>
<%if acceptMetaModelicaGrammar() then if addRootsInputs then 'mmc_GC_pop_roots_mark();'%>
*/
return<%if outVars then ' <%retVar%>' %>;
}

Expand Down Expand Up @@ -3992,7 +4024,7 @@ template algStatement(DAE.Statement stmt, Context context, Text &varDecls /*BUFP
case s as STMT_THROW(__) then 'MMC_THROW();<%\n%>'
case s as STMT_RETURN(__) then 'goto _return;<%\n%>'
case s as STMT_NORETCALL(__) then algStmtNoretcall(s, context, &varDecls /*BUFD*/)
case s as STMT_REINIT(__) then algStmtReinit(s, context, &varDecls /*BUFD*/)
case s as STMT_REINIT(__) then algStmtReinit(s, context, &varDecls /*BUFD*/)
else "#error NOT_IMPLEMENTED_ALG_STATEMENT"
<<
/*#modelicaLine <%statementInfoString(stmt)%>*/
Expand Down Expand Up @@ -6390,7 +6422,7 @@ template literalExpConst(Exp lit, Integer index) "These should all be declared s
::=
let name = '_OMC_LIT<%index%>'
let tmp = '_OMC_LIT_STRUCT<%index%>'
let meta = 'static modelica_metatype const <%name%>'
let meta = 'static modelica_metatype /* const */ <%name%>'
match lit
case SCONST(__) then
let escstr = Util.escapeModelicaStringToCString(string)
Expand All @@ -6403,7 +6435,7 @@ template literalExpConst(Exp lit, Integer index) "These should all be declared s
<<
#define <%name%>_data "<%escstr%>"
static const size_t <%name%>_strlen = <%unescapedStringLength(escstr)%>;
static const MMC_DEFSTRINGLIT(<%tmp%>,<%name%>_strlen,<%name%>_data);
static /* const */ MMC_DEFSTRINGLIT(<%tmp%>,<%name%>_strlen,<%name%>_data);
<%meta%> = MMC_REFSTRINGLIT(<%tmp%>);
>>
else
Expand All @@ -6422,28 +6454,28 @@ template literalExpConst(Exp lit, Integer index) "These should all be declared s
>>
case BOX(exp=exp as RCONST(__)) then
<<
static const MMC_DEFREALLIT(<%tmp%>,<%exp.real%>);
static /* const */ MMC_DEFREALLIT(<%tmp%>,<%exp.real%>);
<%meta%> = MMC_REFREALLIT(<%tmp%>);
>>
case CONS(__) then
<<
static const MMC_DEFSTRUCTLIT(<%tmp%>,2,1) {<%literalExpConstBoxedVal(car)%>,<%literalExpConstBoxedVal(cdr)%>}};
static /* const */ MMC_DEFSTRUCTLIT(<%tmp%>,2,1) {<%literalExpConstBoxedVal(car)%>,<%literalExpConstBoxedVal(cdr)%>}};
<%meta%> = MMC_REFSTRUCTLIT(<%tmp%>);
>>
case META_TUPLE(__) then
<<
static const MMC_DEFSTRUCTLIT(<%tmp%>,<%listLength(listExp)%>,0) {<%listExp |> exp => literalExpConstBoxedVal(exp); separator=","%>}};
static /* const */ MMC_DEFSTRUCTLIT(<%tmp%>,<%listLength(listExp)%>,0) {<%listExp |> exp => literalExpConstBoxedVal(exp); separator=","%>}};
<%meta%> = MMC_REFSTRUCTLIT(<%tmp%>);
>>
case META_OPTION(exp=SOME(exp)) then
<<
static const MMC_DEFSTRUCTLIT(<%tmp%>,1,1) {<%literalExpConstBoxedVal(exp)%>}};
static /* const */ MMC_DEFSTRUCTLIT(<%tmp%>,1,1) {<%literalExpConstBoxedVal(exp)%>}};
<%meta%> = MMC_REFSTRUCTLIT(<%tmp%>);
>>
case METARECORDCALL(__) then
let newIndex = getValueCtor(index)
<<
static const MMC_DEFSTRUCTLIT(<%tmp%>,<%intAdd(1,listLength(args))%>,<%newIndex%>) {&<%underscorePath(path)%>__desc,<%args |> exp => literalExpConstBoxedVal(exp); separator=","%>}};
static /* const */ MMC_DEFSTRUCTLIT(<%tmp%>,<%intAdd(1,listLength(args))%>,<%newIndex%>) {&<%underscorePath(path)%>__desc,<%args |> exp => literalExpConstBoxedVal(exp); separator=","%>}};
<%meta%> = MMC_REFSTRUCTLIT(<%tmp%>);
>>
else '<%\n%>#error "literalExpConst failed: <%printExpStr(lit)%>"<%\n%>'
Expand Down
8 changes: 8 additions & 0 deletions c_runtime/Makefile.common
Expand Up @@ -17,6 +17,8 @@ daux.o dlamch.o dpmpar.o hybrd.o lsame.o newuob.o r1mpyq.o update.o
java_interface.o meta_modelica.o meta_modelica_builtin.o meta_modelica_real.o \
meta_modelica_string_lit.o meta_modelica_catch.o rtclock.o ModelicaUtilities.o \
simulation_varinfo.o read_matlab4.o \
meta_modelica_gc.o meta_modelica_gc_list.o meta_modelica_gc_stack.o \
meta_modelica_gc_roots.o meta_modelica_gc_pages.o meta_modelica_gc_stats.o \
$(EXTRA_OBJS)

SIMOBJS = $(FOBJS) simulation_runtime.o simulation_init.o simulation_input.o \
Expand Down Expand Up @@ -45,6 +47,7 @@ matrix.h \
memory_pool.h \
meta_modelica_builtin.h \
meta_modelica_builtin_boxptr.h \
meta_modelica_gc.h \
meta_modelica_real.h \
meta_modelica_string_lit.h \
meta_modelica.h \
Expand Down Expand Up @@ -74,6 +77,11 @@ simulation_varinfo.h \
solver_dasrt.h \
solver_main.h \
string_array.h \
meta_modelica_gc_stack.h \
meta_modelica_gc_list.h \
meta_modelica_gc_roots.h \
meta_modelica_gc_pages.h \
meta_modelica_gc_stats.h \
utility.h \
linearize.h \
fmu_model_interface.h \
Expand Down
27 changes: 0 additions & 27 deletions c_runtime/meta_modelica.c
Expand Up @@ -29,37 +29,10 @@
*
*/


#include "modelica.h"

jmp_buf *mmc_jumper;

void *mmc_alloc_bytes(unsigned nbytes)
{
static char *mmc_cur_malloc_buf = NULL;
static long mmc_cur_malloc_buf_ix=0;
/* Until we have GC, we simply allocate in 256MB chunks... */
const long mmc_cur_malloc_buf_sz=256*1024*1024; /* 256MB chunks */
void *p=0;
/* fprintf(stderr, "1 mmc_alloc_bytes(%ld): %ld,%ld\n", nbytes, mmc_cur_malloc_buf, mmc_cur_malloc_buf_ix); */
if (mmc_cur_malloc_buf == NULL || nbytes>(mmc_cur_malloc_buf_sz-mmc_cur_malloc_buf_ix)) {
if ( (mmc_cur_malloc_buf = malloc(mmc_cur_malloc_buf_sz)) == 0 ) {
fflush(NULL);
fprintf(stderr, "malloc(%u) failed: %s\n", nbytes, strerror(errno));
assert(p != 0);
}
mmc_cur_malloc_buf_ix = 0;
assert(nbytes <= mmc_cur_malloc_buf_sz);
}
p = mmc_cur_malloc_buf + mmc_cur_malloc_buf_ix;

/* Force 16-byte alignment, like malloc... TODO: Check if this is needed :) */
mmc_cur_malloc_buf_ix += nbytes; /* + ((nbytes%16) ? 16-(nbytes%16): 0); */

/* fprintf(stderr, "2 mmc_alloc_bytes(%ld): %ld,%ld => %ld\n", nbytes, mmc_cur_malloc_buf, mmc_cur_malloc_buf_ix, p); */
return p;
}

void* mmc_mk_rcon(double d)
{
struct mmc_real *p = mmc_alloc_words(sizeof(struct mmc_real)/MMC_SIZE_INT);
Expand Down
27 changes: 18 additions & 9 deletions c_runtime/meta_modelica.h
Expand Up @@ -39,6 +39,7 @@
#define META_MODELICA_H_

#include "modelica.h"
#include "meta_modelica_gc.h"
#include "meta_modelica_string_lit.h"

#if defined(__cplusplus)
Expand Down Expand Up @@ -77,6 +78,7 @@ typedef int mmc_sint_t;
#define MMC_CDR(X) MMC_FETCH(MMC_OFFSET(MMC_UNTAGPTR(X),2))
#define MMC_NILTEST(x) (MMC_GETHDR(x) == MMC_NILHDR)
#define MMC_IMMEDIATE(i) ((void*)(i))
#define MMC_IS_IMMEDIATE(x) (!((mmc_uint_t)(x) & 1))
#define MMC_TAGFIXNUM(i) ((i) << 1)
#define MMC_UNTAGFIXNUM(X) (((mmc_sint_t) X) >> 1)
#define MMC_REALHDR (((MMC_SIZE_DBL/MMC_SIZE_INT) << 10) + 9)
Expand All @@ -93,6 +95,20 @@ typedef int mmc_sint_t;
#define MMC_ARRAY_TAG 255
#define MMC_STRLEN(x) (MMC_HDRSTRLEN(MMC_GETHDR(x)))

/*
* adrpo: if a structure has pointers
* Bit 0 is zero if the node contains pointers, 1 otherwise.
*/
#define MMC_HDRHASPTRS(hdr) (!((hdr) & 1))
/*
* adrpo: if this object was marked, used by GC!
* [xxxxxxxx1x] (used during garbage collection) a marked node;
*/
#define MMC_HDRISMARKED(hdr) ((hdr) & 2)
#define MMC_HDR_MARK(hdr) ((hdr) | 2)
#define MMC_HDR_UNMARK(hdr) ((hdr) & ~2)


#define MMC_INT_MAX ((1<<30)-1)
#define MMC_INT_MIN (-(1<<30))

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

void *mmc_alloc_bytes(unsigned nbytes);

static void *mmc_alloc_words(unsigned nwords)
{
return mmc_alloc_bytes(nwords * sizeof(void*));
}

#define MMC_FALSE (mmc_mk_icon(0))
#define MMC_TRUE (mmc_mk_icon(1))
#define mmc_mk_bcon(X) ((X) != 0 ? MMC_TRUE : MMC_FALSE)
Expand Down Expand Up @@ -305,8 +314,8 @@ static inline void *mmc_mk_box(int slots, unsigned ctor, ...)
return MMC_TAGPTR(p);
}

static const MMC_DEFSTRUCT0LIT(mmc_nil,0);
static const MMC_DEFSTRUCT0LIT(mmc_none,1);
static /* const */ MMC_DEFSTRUCT0LIT(mmc_nil,0);
static /* const */ MMC_DEFSTRUCT0LIT(mmc_none,1);

#define mmc_mk_nil() MMC_REFSTRUCTLIT(mmc_nil)
#define mmc_mk_none() MMC_REFSTRUCTLIT(mmc_none)
Expand Down

0 comments on commit 17d636b

Please sign in to comment.