mirror of
https://github.com/justinethier/cyclone.git
synced 2025-05-23 20:15:05 +02:00
Create a function for library inits
This is done at the top-level for programs, but libraries need a dedicated function for it so we can chain inits across multiple libraries on program startup.
This commit is contained in:
parent
a99b3d571a
commit
a804e048fd
4 changed files with 36 additions and 7 deletions
9
cgen.scm
9
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?
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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;}
|
||||
|
||||
|
|
20
trans.scm
20
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 '()))
|
||||
|
|
Loading…
Add table
Reference in a new issue