diff --git a/Makefile b/Makefile index 54fc87c8..7076cc1c 100644 --- a/Makefile +++ b/Makefile @@ -43,7 +43,7 @@ test: $(TESTFILES) cyclone .PHONY: test2 test2: examples/hello-library/int-test/hello.c libcyclone.a # ./cyclone -t examples/hello-library/hello.scm -# ./cyclone -t examples/hello-library/libs/lib2.sld + ./cyclone -t examples/hello-library/libs/lib2.sld gcc examples/hello-library/int-test/lib2.c -I. -g -c -o lib2.o gcc examples/hello-library/int-test/hello.c -I. -g -c -o hello.o gcc hello.o lib2.o -L. -lcyclone -lm -o hello diff --git a/cgen.scm b/cgen.scm index 3f590408..79d5fd19 100644 --- a/cgen.scm +++ b/cgen.scm @@ -164,17 +164,6 @@ assign (number->string n) ";") "")) -(define (c-macro-GC-globals) - ; emit directly to be more efficient - ; TODO: convert all c-macro functions to direct emit??? - (for-each - (lambda (global) - (emits "\n add_global((object *) &") - (emits (mangle-global (car global))) - (emits ");")) - *globals*) - (emit "")) - (define (c-macro-declare-globals) (for-each (lambda (global) @@ -927,19 +916,18 @@ "}\n")) formals*)))) -(define (mta:code-gen input-program globals program? lib-exports lib-imports) +(define (mta:code-gen input-program globals program? lib-name lib-exports lib-imports) (set! *global-syms* globals) (let ((compiled-program (apply string-append (map c-compile-program input-program)))) - (if (member 'eval globals) - (emit "#define CYC_EVAL")) - (emit-c-arity-macros 0) (emit "#include \"cyclone.h\"") (c-macro-declare-globals) (emit "#include \"runtime.h\"") - (emit "#include \"runtime-main.h\"") + + (if program? + (emit "#include \"runtime-main.h\"")) ;; Emit symbols (for-each @@ -968,11 +956,19 @@ (emit ((caadr l) (string-append "__lambda_" (number->string (car l)))))) lambdas) - (emit " - static void c_entry_pt(argc, env,cont) int argc; closure env,cont; { ") + ; Emit entry point + (if program? + (emit "static void c_entry_pt(argc, env,cont) int argc; closure env,cont; { ") + (emit (string-append "void c_" (lib:name->string lib-name) "_entry_pt(argc, env,cont) int argc; closure env,cont; { "))) ;; Initialize global table - (c-macro-GC-globals) + (for-each + (lambda (global) + (emits "\n add_global((object *) &") + (emits (mangle-global (car global))) + (emits ");")) + *globals*) + (emit "") ;; Initialize symbol table (for-each @@ -1023,7 +1019,7 @@ (emits str)) code)) ((null? (cdr ps)) - (loop (cons (string-append "make_cons(" (car cs) ", &" (car ps) ",nil);\n") code) + (loop (cons (string-append "make_cons(" (car cs) ", &" (car ps) ",Cyc_global_variables);\n") code) (cdr ps) (cdr cs))) (else @@ -1036,9 +1032,11 @@ (emits (string-append "Cyc_global_variables = &" head-pair ";")))) - (emit compiled-program) + (if program? + (emit compiled-program)) (emit "}") - (emit *c-main-function*))) + (if program? + (emit *c-main-function*)))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; diff --git a/cyclone.scm b/cyclone.scm index 920d98f2..4f37f026 100644 --- a/cyclone.scm +++ b/cyclone.scm @@ -28,6 +28,9 @@ (define (library? ast) (tagged-list? 'define-library ast)) (define (lib:name ast) (cadr ast)) +;; Convert name (as list of symbols) to a mangled string +(define (lib:name->string name) + (apply string-append (map mangle name))) (define (lib:exports ast) (and-let* ((code (assoc 'export (cddr ast)))) (cdr code))) @@ -49,6 +52,7 @@ (lambda (return) (define globals '()) (define program? #t) ;; Are we building a program or a library? + (define lib-name '()) (define lib-exports '()) (define lib-imports '()) @@ -60,6 +64,7 @@ (cond ((library? (car input-program)) (set! program? #f) + (set! lib-name (lib:name (car input-program))) (set! lib-exports (lib:exports (car input-program))) (set! lib-imports (lib:imports (car input-program))) (set! input-program (lib:body (car input-program))) @@ -103,18 +108,24 @@ (trace:info "---------------- after alpha conversion:") (trace:info input-program) ;pretty-print - (set! globals (cons 'call/cc globals)) - (set! input-program - (cons - ;; call/cc must be written in CPS form, so it is added here - ;; TODO: prevents this from being optimized-out - ;; TODO: will this cause issues if another var is assigned to call/cc? - '(define call/cc - (lambda (k f) (f k (lambda (_ result) (k result))))) - (map - (lambda (expr) - (cps-convert expr)) - input-program))) + (let ((cps (map + (lambda (expr) + (cps-convert expr)) + input-program))) + (cond + (program? + (set! globals (cons 'call/cc globals)) + (set! input-program + (cons + ;; call/cc must be written in CPS form, so it is added here + ;; TODO: prevents this from being optimized-out + ;; TODO: will this cause issues if another var is assigned to call/cc? + '(define call/cc + (lambda (k f) (f k (lambda (_ result) (k result))))) + cps))) + (else + ;; Compiling a library, no need for call/cc yet + (set! input-program cps)))) (trace:info "---------------- after CPS:") (trace:info input-program) ;pretty-print @@ -168,7 +179,7 @@ (exit))) (trace:info "---------------- C code:") - (mta:code-gen input-program globals program? lib-exports lib-imports) + (mta:code-gen input-program globals program? lib-name lib-exports lib-imports) (return '())))) ;; No codes to return ;; TODO: longer-term, will be used to find where cyclone's data is installed