diff --git a/cgen.scm b/cgen.scm index f5d62e80..65e62294 100644 --- a/cgen.scm +++ b/cgen.scm @@ -1083,7 +1083,14 @@ (else ;; Do not use funcall1 macro as it might not have been defined (emit "cont = ((closure1_type *)cont)->elt1;") - (emit "((cont)->fn)(1, cont, cont);"))) + (emit "((cont)->fn)(1, cont, cont);") + ;; TODO: + ; need to call into computed func, EG: __glo_lib_91init_117schemeread + ; using cont->elt1 as k. +; (emit "}") +; (emit (string-append "static void c_" (lib:name->string lib-name) "_entry_pt_inits(int argc, closure " +; (emit compiled-program) + )) (emit "}") (if program? diff --git a/cyclone.scm b/cyclone.scm index fee67d36..c390b1a5 100644 --- a/cyclone.scm +++ b/cyclone.scm @@ -51,7 +51,10 @@ (let ((includes (lib:includes (car input-program)))) (set! program? #f) (set! lib-name (lib:name (car input-program))) - (set! lib-exports (lib:exports (car input-program))) + (set! lib-exports + (cons + (lib:name->symbol lib-name) + (lib:exports (car input-program)))) (set! imports (lib:imports (car input-program))) (set! input-program (lib:body (car input-program))) ;; Prepend any included files into the begin section @@ -93,7 +96,7 @@ ;; Separate global definitions from the rest of the top-level code (set! input-program - (isolate-globals input-program)) + (isolate-globals input-program program? lib-name)) ;; Optimize-out unused global variables ;; For now, do not do this if eval is used. diff --git a/runtime.c b/runtime.c index ac277d74..3bb2a8b2 100644 --- a/runtime.c +++ b/runtime.c @@ -421,7 +421,12 @@ list assq(x,l) object x; list l; return boolean_f;} list assoc(x,l) object x; list l; -{for (; !nullp(l) && type_of(l) == cons_tag; l = cdr(l)) +{ + printf("JAE DEBUG, assoc received: "); + Cyc_display(l); + printf("\n"); + if (type_of(l) != cons_tag) return boolean_f; + for (; !nullp(l); l = cdr(l)) {register list la = car(l); if (boolean_f != equalp(x,car(la))) return la;} return boolean_f;} diff --git a/trans.scm b/trans.scm index 44da5151..5bb42278 100644 --- a/trans.scm +++ b/trans.scm @@ -890,7 +890,7 @@ ; This function extracts out non-define statements, and adds them to ; a "main" after the defines. ; -(define (isolate-globals exp) +(define (isolate-globals exp program? lib-name) (let loop ((top-lvl exp) (globals '()) (exprs '())) @@ -899,8 +899,17 @@ (append (reverse globals) (expand - ;; 0 to ensure we always create a meaningful top-level - `((begin 0 ,@(reverse exprs)))))) + (cond + (program? + ;; This is the main program, keep top level. + ;; Use 0 here (and below) to ensure a meaningful top-level + `((begin 0 ,@(reverse exprs))) + ) + (else + ;; This is a library, keep inits in their own function + `((define ,(lib:name->symbol lib-name) + (lambda () 0 ,@(reverse exprs)))))) + ))) (else (cond ((define? (car top-lvl)) @@ -1557,6 +1566,11 @@ ;; Convert name (as list of symbols) to a mangled string (define (lib:name->string name) (apply string-append (map mangle name))) +(define (lib:name->symbol name) + (string->symbol + (string-append + "lib-init:" + (lib:name->string name)))) ;; Helper function that returns an empty list as a default value (define (lib:result result) (if result result '()))