From d2049f0f7ec49582f6180c6b59966c597f87344b Mon Sep 17 00:00:00 2001 From: Justin Ethier Date: Mon, 18 May 2015 18:08:45 -0400 Subject: [PATCH] Adding code to call into library entry pt's --- cgen.scm | 41 +++++++++++++++++++++++++++++++++++------ cyclone.scm | 7 ++++--- 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/cgen.scm b/cgen.scm index f245a614..68cf643a 100644 --- a/cgen.scm +++ b/cgen.scm @@ -911,7 +911,8 @@ lib-name lib-exports imported-globals - globals) + globals + required-libs) (set! *global-syms* (append globals imported-globals)) (let ((compiled-program (apply string-append @@ -966,9 +967,15 @@ lambdas) ; 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; { "))) + (cond + (program? + (for-each + (lambda (lib-name) + (emit (string-append "extern void c_" (lib:name->string lib-name) "_entry_pt(int argc, closure env, closure cont);"))) + required-libs) + (emit "static void c_entry_pt(argc, env,cont) int argc; closure env,cont; { ")) + (else + (emit (string-append "void c_" (lib:name->string lib-name) "_entry_pt(argc, env,cont) int argc; closure env,cont; { ")))) ;; Initialize global table (for-each @@ -1041,8 +1048,30 @@ (emits (string-append "Cyc_global_variables = &" head-pair ";")))) - (if program? - (emit compiled-program)) + (cond + (program? + ;; Emit code to initialize each module (compiled Scheme library) + (let ((this-clo "c_done") + (prev-clo "c_done")) + ;; TODO: need to wrap this in a well-known function (c_program_main??) + ;; and call into it from the closure chain + (emit compiled-program) + + (for-each + (lambda (lib-name) + (emit + (string-append + "mclosure1(" this-clo + ", c_" (lib:name->string lib-name) "_entry_pt" + ", &" prev-clo ");")) + (set! prev-clo this-clo) + (set! this-clo (mangle (gensym "c"))) + ) + required-libs) + (emit + (string-append "(" prev-clo ".fn)(0, &" prev-clo ");")) + ))) + (emit "}") (if program? (emit *c-main-function*)))) diff --git a/cyclone.scm b/cyclone.scm index 12cfbb71..1d381a93 100644 --- a/cyclone.scm +++ b/cyclone.scm @@ -30,7 +30,7 @@ ;; Code emission. ; c-compile-and-emit : (string -> A) exp -> void -(define (c-compile-and-emit input-program) +(define (c-compile-and-emit input-program lib-deps) (call/cc (lambda (return) (define globals '()) @@ -196,7 +196,8 @@ lib-name lib-exports imported-vars - module-globals) + module-globals + lib-deps) (return '())))) ;; No codes to return ;; TODO: longer-term, will be used to find where cyclone's data is installed @@ -228,7 +229,7 @@ (with-output-to-file src-file (lambda () - (c-compile-and-emit program))))) + (c-compile-and-emit program lib-deps))))) (result (create-c-file in-prog))) ;; Load other modules if necessary (cond