From a93c1e8cdeb892786a8151748c855b51d2379465 Mon Sep 17 00:00:00 2001 From: Justin Ethier Date: Thu, 23 Mar 2017 04:47:47 -0400 Subject: [PATCH] Added so generation and dl lib --- Makefile.config | 4 ++-- cyclone.scm | 18 +++++++++++++++--- include/cyclone/types.h | 2 ++ runtime.c | 18 ++++++++++++++++++ scheme/cyclone/libraries.sld | 15 +++++++++++++++ 5 files changed, 52 insertions(+), 5 deletions(-) diff --git a/Makefile.config b/Makefile.config index 4de3fe88..801bb5a4 100644 --- a/Makefile.config +++ b/Makefile.config @@ -12,13 +12,13 @@ COMP_CFLAGS ?= -O2 -fPIC -Wall -I$(PREFIX)/include -L$(PREFIX)/lib #CFLAGS = -g -Wall #CFLAGS = -g -pg -Wall CC ?= cc -LIBS = -pthread -lcyclone -lck -lm -ltommath +LIBS = -pthread -lcyclone -lck -lm -ltommath -ldl # Commands "baked into" cyclone for invoking the C compiler CC_PROG ?= "$(CC) ~src-file~ $(COMP_CFLAGS) -c -o ~exec-file~.o" CC_EXEC ?= "$(CC) ~exec-file~.o ~obj-files~ $(LIBS) $(COMP_CFLAGS) -o ~exec-file~" CC_LIB ?= "$(CC) ~src-file~ $(COMP_CFLAGS) -c -o ~exec-file~.o" -CC_SO ?= "$(CC) -shared -o ~exec-file~.so -o ~exec-file~.o" +CC_SO ?= "$(CC) -shared -o ~exec-file~.so ~exec-file~.o" AR ?= ar #CD ?= cd diff --git a/cyclone.scm b/cyclone.scm index 8e2a61f0..4b904c40 100644 --- a/cyclone.scm +++ b/cyclone.scm @@ -370,7 +370,7 @@ (read-all port)))) ;; Compile and emit: -(define (run-compiler args cc? cc-prog cc-exec cc-lib append-dirs prepend-dirs) +(define (run-compiler args cc? cc-prog cc-exec cc-lib cc-so append-dirs prepend-dirs) (let* ((in-file (car args)) (expander (base-expander)) (in-prog-raw (read-file in-file)) @@ -456,14 +456,23 @@ (string-replace-all (get-comp-env 'cc-lib cc-lib) "~src-file~" src-file) - "~exec-file~" exec-file))) + "~exec-file~" exec-file)) + (comp-so-cmd + (string-replace-all + (string-replace-all + (get-comp-env 'cc-so cc-so) + "~src-file~" src-file) + "~exec-file~" exec-file)) + ) (cond (cc? (system comp-lib-cmd) + (system comp-so-cmd) ) (else (display comp-lib-cmd) (newline) + (display comp-so-cmd) (newline)))))))) ;; Collect values for the given command line arguments and option. @@ -500,6 +509,7 @@ (cc-prog (apply string-append (collect-opt-values args "-CP"))) (cc-exec (apply string-append (collect-opt-values args "-CE"))) (cc-lib (apply string-append (collect-opt-values args "-CL"))) + (cc-so (apply string-append (collect-opt-values args "-CS"))) (append-dirs (collect-opt-values args "-A")) (prepend-dirs (collect-opt-values args "-I"))) ;; Set optimization level(s) @@ -526,6 +536,8 @@ an executable. -CL cc-commands Specify a custom command line for the C compiler to compile a library module. + -CS cc-commands Specify a custom command line for the C compiler to compile + a shared object module. -Ox Optimization level, higher means more optimizations will be used. Set to 0 to disable optimizations. -d Only generate intermediate C files, do not compile them @@ -548,5 +560,5 @@ (display "cyclone: no input file") (newline)) (else - (run-compiler non-opts compile? cc-prog cc-exec cc-lib append-dirs prepend-dirs)))) + (run-compiler non-opts compile? cc-prog cc-exec cc-lib cc-so append-dirs prepend-dirs)))) diff --git a/include/cyclone/types.h b/include/cyclone/types.h index f59f73e6..86ed9f9d 100644 --- a/include/cyclone/types.h +++ b/include/cyclone/types.h @@ -18,6 +18,7 @@ #include #include #include +#include #include "tommath.h" /** @@ -1059,4 +1060,5 @@ void *gc_alloc_from_bignum(gc_thread_data *data, bignum_type *src); int gc_minor(void *data, object low_limit, object high_limit, closure cont, object * args, int num_args); +void Cyc_import_shared_object(void *data, object cont, object filename, object entry_pt_fnc); #endif /* CYCLONE_TYPES_H */ diff --git a/runtime.c b/runtime.c index 3e8600e9..6a37f83f 100644 --- a/runtime.c +++ b/runtime.c @@ -5592,3 +5592,21 @@ double MRG32k3a (double seed) return ((p1 - p2) * norm); } /* END RNG */ + + +/** Dynamic loading */ +void Cyc_import_shared_object(void *data, object cont, object filename, object entry_pt_fnc) +{ + void *handle; + function_type entry_pt; + Cyc_check_str(data, filename); + Cyc_check_str(data, entry_pt_fnc); + handle = dlopen(string_str(filename), RTLD_LAZY); + if (handle == NULL) { + Cyc_rt_raise2(data, "Unable to import library from", filename); + } + entry_pt = (function_type) dlsym(handle, string_str(entry_pt_fnc)); + mclosure1(clo, entry_pt, cont); + entry_pt(data, 0, &clo, &clo); +} + diff --git a/scheme/cyclone/libraries.sld b/scheme/cyclone/libraries.sld index a3650373..47f956b5 100644 --- a/scheme/cyclone/libraries.sld +++ b/scheme/cyclone/libraries.sld @@ -58,6 +58,8 @@ lib:idb:lookup lib:idb:entry->library-name lib:idb:entry->library-id + ;; Dynamic import + lib:dyn-load ) (begin @@ -589,4 +591,17 @@ (deps (reverse (cdr (get-cell resolved))))) ;; cdr to get rid of master list (map car deps))) + +(define (lib:dyn-load import) + (let ((lib-name (lib:list->import-set import))) + (c:dyn-load + (lib:import->filename lib-name ".so") + (string-append + " c_" (lib:name->string lib-name) "_entry_pt_first_lambda")))) + +(define-c c:dyn-load + "(void *data, int argc, closure _, object k, object fn, object entry_fnc)" + " Cyc_import_shared_object(data, k, fn, entry_fnc); ") + + ))