From a63f9a729e6082b689d58159e2fdb5ad35d41694 Mon Sep 17 00:00:00 2001 From: Justin Ethier Date: Fri, 1 Apr 2016 21:48:01 -0400 Subject: [PATCH] Refactoring --- gc.c | 6 +++--- include/cyclone/types.h | 17 ++++------------- runtime.c | 32 ++++++++++++++++---------------- scheme/cyclone/cgen.sld | 28 ++++++++++++++-------------- 4 files changed, 37 insertions(+), 46 deletions(-) diff --git a/gc.c b/gc.c index ab1c13a8..f0da6eb3 100644 --- a/gc.c +++ b/gc.c @@ -752,8 +752,8 @@ int gc_is_stack_obj(gc_thread_data *thd, object obj) char tmp; object low_limit = &tmp; object high_limit = thd->stack_start; - return (check_overflow(low_limit, obj) && - check_overflow(obj, high_limit)); + return (stack_overflow(low_limit, obj) && + stack_overflow(obj, high_limit)); } /** @@ -1270,7 +1270,7 @@ void gc_thread_data_init(gc_thread_data *thd, int mut_num, char *stack_base, lon #else thd->stack_limit = stack_base + stack_size; #endif - if (check_overflow(stack_base, &stack_ref)){ + if (stack_overflow(stack_base, &stack_ref)){ fprintf(stderr, "Error: Stack is growing in the wrong direction! Rebuild with STACK_GROWTH_IS_DOWNWARD changed to %d\n", (1 - STACK_GROWTH_IS_DOWNWARD)); diff --git a/include/cyclone/types.h b/include/cyclone/types.h index 442a93ea..7dca0edb 100644 --- a/include/cyclone/types.h +++ b/include/cyclone/types.h @@ -160,9 +160,9 @@ typedef long tag_type; /* Determine if stack has overflowed */ #if STACK_GROWTH_IS_DOWNWARD -#define check_overflow(x,y) ((x) < (y)) +#define stack_overflow(x,y) ((x) < (y)) #else -#define check_overflow(x,y) ((x) > (y)) +#define stack_overflow(x,y) ((x) > (y)) #endif /* Define object tag values. Could be an enum... @@ -207,7 +207,6 @@ typedef long tag_type; * 0x10 - char */ -// TODO: does this break negative numbers (IE, overwrite sign bit in 2's comp?)? may need a more sophisticated scheme to handle 31-bit numbers. also, ideally want to use 63 bits on a 64-bit system #define obj_is_int(x) ((unsigned long)(x) & (unsigned long)1) #define obj_obj2int(x) ((long)(x)>>1) #define obj_int2obj(c) ((void *)((((long)c)<<1) | 1)) @@ -219,7 +218,7 @@ typedef long tag_type; #define is_value_type(x) ((unsigned long)(x) & (unsigned long)3) #define is_object_type(x) (x && !is_value_type(x)) -/* Define function type. */ +/* Function type */ typedef void (*function_type)(); typedef void (*function_type_va)(int, object, object, object, ...); @@ -305,7 +304,7 @@ typedef bytevector_type *bytevector; #define make_empty_bytevector(v) bytevector_type v; v.hdr.mark = gc_color_red; v.hdr.grayed = 0; v.tag = bytevector_tag; v.len = 0; v.data = NULL; -/* Define cons type. */ +/* Pair (cons) type */ typedef struct {gc_header_type hdr; tag_type tag; object cons_car,cons_cdr;} cons_type; typedef cons_type *list; @@ -362,14 +361,6 @@ typedef closure0_type *macro; #define mclosure1(c,f,a) closure1_type c; c.hdr.mark = gc_color_red; c.hdr.grayed = 0; c.tag = closure1_tag; \ c.fn = f; c.num_args = -1; c.elt1 = a; -#define mlist1(e1) (mcons(e1,nil)) -#define mlist2(e2,e1) (mcons(e2,mlist1(e1))) -#define mlist3(e3,e2,e1) (mcons(e3,mlist2(e2,e1))) -#define mlist4(e4,e3,e2,e1) (mcons(e4,mlist3(e3,e2,e1))) -#define mlist5(e5,e4,e3,e2,e1) (mcons(e5,mlist4(e4,e3,e2,e1))) -#define mlist6(e6,e5,e4,e3,e2,e1) (mcons(e6,mlist5(e5,e4,e3,e2,e1))) -#define mlist7(e7,e6,e5,e4,e3,e2,e1) (mcons(e7,mlist6(e6,e5,e4,e3,e2,e1))) - #define make_cell(n,a) make_cons(n,a,nil); /* Primitive types */ diff --git a/runtime.c b/runtime.c index d5869c92..5f62c349 100644 --- a/runtime.c +++ b/runtime.c @@ -76,22 +76,22 @@ void Cyc_check_bounds(void *data, const char *label, int len, int index) { /* END error checking */ /* These macros are hardcoded here to support functions in this module. */ -#define closcall1(td,cfn,a1) if (type_of(cfn) == cons_tag || prim(cfn)) { Cyc_apply(td,0, (closure)(a1), cfn); } else { ((cfn)->fn)(td,1,cfn,a1);} +#define closcall1(td,clo,a1) if (type_of(clo) == cons_tag || prim(clo)) { Cyc_apply(td,0, (closure)(a1), clo); } else { ((clo)->fn)(td,1,clo,a1);} /* Return to continuation after checking for stack overflow. */ -#define return_closcall1(td,cfn,a1) \ -{char stack; \ - if (check_overflow(&stack,(((gc_thread_data *)data)->stack_limit))) { \ +#define return_closcall1(td,clo,a1) \ +{char top; \ + if (stack_overflow(&top,(((gc_thread_data *)data)->stack_limit))) { \ object buf[1]; buf[0] = a1;\ - GC(td,cfn,buf,1); return; \ - } else {closcall1(td,(closure) (cfn),a1); return;}} -#define closcall2(td,cfn,a1,a2) if (type_of(cfn) == cons_tag || prim(cfn)) { Cyc_apply(td,1, (closure)(a1), cfn,a2); } else { ((cfn)->fn)(td,2,cfn,a1,a2);} + GC(td,clo,buf,1); return; \ + } else {closcall1(td,(closure) (clo),a1); return;}} +#define closcall2(td,clo,a1,a2) if (type_of(clo) == cons_tag || prim(clo)) { Cyc_apply(td,1, (closure)(a1), clo,a2); } else { ((clo)->fn)(td,2,clo,a1,a2);} /* Return to continuation after checking for stack overflow. */ -#define return_closcall2(td,cfn,a1,a2) \ -{char stack; \ - if (check_overflow(&stack,(((gc_thread_data *)data)->stack_limit))) { \ +#define return_closcall2(td,clo,a1,a2) \ +{char top; \ + if (stack_overflow(&top,(((gc_thread_data *)data)->stack_limit))) { \ object buf[2]; buf[0] = a1;buf[1] = a2;\ - GC(td,cfn,buf,2); return; \ - } else {closcall2(td,(closure) (cfn),a1,a2); return;}} + GC(td,clo,buf,2); return; \ + } else {closcall2(td,(closure) (clo),a1,a2); return;}} /*END closcall section */ /* Global variables. */ @@ -2680,8 +2680,8 @@ char *gc_move(char *obj, gc_thread_data *thd, int *alloci, int *heap_grown) { #define gc_move2heap(obj) { \ temp = obj; \ - if (check_overflow(low_limit, temp) && \ - check_overflow(temp, high_limit)){ \ + if (stack_overflow(low_limit, temp) && \ + stack_overflow(temp, high_limit)){ \ (obj) = (object) gc_move(temp, (gc_thread_data *)data, &alloci, &heap_grown); \ } \ } @@ -3225,8 +3225,8 @@ object copy2heap(void *data, object obj) { char stack_pos; gc_thread_data *thd = (gc_thread_data *)data; - int on_stack = check_overflow((object)(&stack_pos), obj) && - check_overflow(obj, (object)thd->stack_start); + int on_stack = stack_overflow((object)(&stack_pos), obj) && + stack_overflow(obj, (object)thd->stack_start); if (!is_object_type(obj) || !on_stack) { return obj; } diff --git a/scheme/cyclone/cgen.sld b/scheme/cyclone/cgen.sld index 3ed0777f..f4fb2051 100644 --- a/scheme/cyclone/cgen.sld +++ b/scheme/cyclone/cgen.sld @@ -122,26 +122,26 @@ (n (number->string num-args)) (arry-assign (c-macro-array-assign num-args "buf" "a"))) (string-append - "/* Check for GC, then call given continuation closure */\n" - "#define return_closcall" n "(td,cfn" args ") \\\n" - "{char stack; \\\n" - " if (check_overflow(&stack,(((gc_thread_data *)data)->stack_limit))) { \\\n" + ;"/* Check for GC, then call given continuation closure */\n" + "#define return_closcall" n "(td, clo" args ") \\\n" + "{char top; \\\n" + " if (stack_overflow(&top, (((gc_thread_data *)data)->stack_limit))) { \\\n" " object buf[" n "]; " arry-assign "\\\n" - " GC(td,cfn,buf," n "); return; \\\n" - " } else {closcall" n "(td,(closure) (cfn)" args "); return;}}\n"))) + " GC(td,clo,buf," n "); return; \\\n" + " } else {closcall" n "(td,(closure) (clo)" args "); return;}}\n"))) (define (c-macro-return-direct num-args) (let ((args (c-macro-n-prefix num-args ",a")) (n (number->string num-args)) (arry-assign (c-macro-array-assign num-args "buf" "a"))) (string-append - "/* Check for GC, then call C function directly */\n" - "#define return_direct" n "(td,_fn" args ") { \\\n" - " char stack; \\\n" - " if (check_overflow(&stack,(((gc_thread_data *)data)->stack_limit))) { \\\n" + ;"/* Check for GC, then call C function directly */\n" + "#define return_direct" n "(td, _fn" args ") { \\\n" + " char top; \\\n" + " if (stack_overflow(&top, (((gc_thread_data *)data)->stack_limit))) { \\\n" " object buf[" n "]; " arry-assign " \\\n" " mclosure0(c1, _fn); \\\n" - " GC(td,&c1, buf, " n "); return; \\\n" + " GC(td, &c1, buf, " n "); return; \\\n" " } else { (_fn)(td," n ",(closure)_fn" args "); }}\n"))) (define (c-macro-closcall num-args) @@ -150,10 +150,10 @@ (n-1 (number->string (if (> num-args 0) (- num-args 1) 0))) (wrap (lambda (s) (if (> num-args 0) s "")))) (string-append - "#define closcall" n "(td,cfn" args ") " - (wrap (string-append "if (type_of(cfn) == cons_tag || prim(cfn)) { Cyc_apply(td," n-1 ", (closure)(a1), cfn" (if (> num-args 1) (substring args 3 (string-length args)) "") "); }")) + "#define closcall" n "(td,clo" args ") " + (wrap (string-append "if (type_of(clo) == cons_tag || prim(clo)) { Cyc_apply(td," n-1 ", (closure)(a1), clo" (if (> num-args 1) (substring args 3 (string-length args)) "") "); }")) (wrap " else { ") - "((cfn)->fn)(td," n ",cfn" args ")" + "((clo)->fn)(td," n ",clo" args ")" (wrap ";}") )))