diff --git a/lib/infer.cora b/lib/infer.cora index 6403bdf..5e76238 100644 --- a/lib/infer.cora +++ b/lib/infer.cora @@ -262,7 +262,7 @@ (declare '- `(int -> (int -> int))) (declare '* `(int -> (int -> int))) (declare '= (let a (tvar) b (tvar) `(,a -> (,b -> bool)))) -(declare 'set (let a (tvar) ['symbol '-> [a '-> a]])) +;; (declare 'set (let a (tvar) ['symbol '-> [a '-> a]])) (declare 'null? [(tvar) '-> 'bool]) (declare 'cons? [(tvar) '-> 'bool]) (declare '*type-env* ['list ['tuple 'symbol (tvar)]]) diff --git a/lib/toc.cora b/lib/toc.cora index b0920a2..268bd57 100644 --- a/lib/toc.cora +++ b/lib/toc.cora @@ -516,27 +516,27 @@ (set 'cora/lib/infer.*typecheck* false) -(defun .preprocess (sexp) - (let sexp1 (if (and (pair? sexp) (= 'begin (car sexp))) - (cdr sexp) - (cons sexp ())) - (begin - (.handle-import-eagerly sexp1) - (.split-type-and-code sexp1 () () - (lambda (type code) - (if cora/lib/infer.*typecheck* - (cons 'begin (append type code)) - (cons 'begin code))))))) +(defun .preprocess (file-path pkg-str) + (let sexp (read-file-as-sexp file-path pkg-str) + (let sexp1 (if (and (pair? sexp) (= 'begin (car sexp))) + (cdr sexp) + (cons sexp ())) + (begin + (.handle-import-eagerly sexp1) + (.split-type-and-code sexp1 () () + (lambda (type code) + (if cora/lib/infer.*typecheck* + (cons 'begin (append type code)) + (cons 'begin code)))))))) (defun .compile-to-c (from to pkg-str) - (let sexp (read-file-as-sexp from pkg-str) - (let sexp1 (.preprocess sexp) - (let input (macroexpand sexp1) - (let bc (.compile input) - (let stream (io.open-output-file to) - (begin - (.generate-c stream bc) - (io.close-output-file stream)))))))) + (let sexp1 (.preprocess from pkg-str) + (let input (macroexpand sexp1) + (let bc (.compile input) + (let stream (io.open-output-file to) + (begin + (.generate-c stream bc) + (io.close-output-file stream))))))) ;; ============== utilities of eval0 ========== diff --git a/src/gc.c b/src/gc.c index 835e91c..79892e6 100644 --- a/src/gc.c +++ b/src/gc.c @@ -190,8 +190,11 @@ blockReset(struct Block *block) { } } +__thread struct GC* threadLocalGC; + void -gcInit(struct GC *gc, uintptr_t *baseStackAddr) { +gcInit(uintptr_t *baseStackAddr) { + struct GC *gc = malloc(sizeof(struct GC)); gc->heap = NULL; gc->state = gcStateNone; gc->baseStackAddr = baseStackAddr; @@ -211,6 +214,13 @@ gcInit(struct GC *gc, uintptr_t *baseStackAddr) { for (int i=0; isizeClass[i] = NULL; } + + threadLocalGC = gc; +} + +struct GC* +getGC() { + return threadLocalGC; } static struct heapArena* @@ -324,8 +334,6 @@ gcRegistForType(uint8_t idx, gcFunc fn) { return true; } -struct GC gc; - static void gcQueueInit(struct GC *gc) { struct Block *b = blockNew(gc); @@ -550,11 +558,11 @@ gcInuseSizeInc(struct GC *gc, int size) { } void -writeBarrier(uintptr_t *slot, uintptr_t val) { +writeBarrier(struct GC *gc, uintptr_t *slot, uintptr_t val) { // Yuasa-style deletion barrier - if (gc.state == gcStateIncremental) { + if (gc->state == gcStateIncremental) { uintptr_t old = *slot; - gcMark(&gc, old); + gcMark(gc, old); } *slot = val; } diff --git a/src/gc.h b/src/gc.h index 223af0f..78b8df5 100644 --- a/src/gc.h +++ b/src/gc.h @@ -37,17 +37,18 @@ typedef struct { #define ptr(x) ((void*)((x)&~TAG_PTR)) #define tag(x) ((x) & TAG_MASK) -extern struct GC gc; -void gcInit(struct GC* gc, uintptr_t* mark); +struct GC; + +void gcInit(uintptr_t* mark); +struct GC *getGC(); + void* gcAlloc(struct GC* gc, int size); -void writeBarrier(uintptr_t *slot, uintptr_t val); +void writeBarrier(struct GC *gc, uintptr_t *slot, uintptr_t val); void gcMark(struct GC *gc, uintptr_t head); void gcInuseSizeInc(struct GC *gc, int size); typedef void (*gcFunc)(struct GC *gc, void* from); bool gcRegistForType(uint8_t type, gcFunc fn); -extern struct GC gc; - #endif diff --git a/src/runtime.c b/src/runtime.c index bade6ed..64b2a6c 100644 --- a/src/runtime.c +++ b/src/runtime.c @@ -856,7 +856,7 @@ registerAPI(struct registerModule* m, str pkg) { void coraInit(uintptr_t *mark) { - gcInit(&gc, mark); + gcInit(mark); typesInit(); symQuote = intern("quote"); symBackQuote = intern("backquote"); diff --git a/src/types.c b/src/types.c index f3355f2..45f23e2 100644 --- a/src/types.c +++ b/src/types.c @@ -30,7 +30,7 @@ const char* typeNameX[8] = { void* newObj(scmHeadType tp, int sz) { // scmHead* p = malloc(sz); - scmHead* p = gcAlloc(&gc, sz); + scmHead* p = gcAlloc(getGC(), sz); assert(((Obj)p & TAG_PTR) == 0); p->type = tp; /* printf("alloc object -- %p %s\n", p, typeNameX[tp]); */ @@ -338,7 +338,7 @@ vectorSet(Obj vec, int idx, Obj val) { struct scmVector* v = ptr(vec); assert(v->head.type == scmHeadVector); assert(idx >= 0 && idx < v->size); - writeBarrier(&v->data[idx], val); + writeBarrier(getGC(), &v->data[idx], val); return vec; }