Pass enclosing lambda AST ID when compiling expr's

This commit is contained in:
Justin Ethier 2018-09-19 13:24:04 -04:00
parent 5d1ed67c48
commit 7cb83e17d8

View file

@ -311,7 +311,7 @@
(let* ((preamble "") (let* ((preamble "")
(append-preamble (lambda (s) (append-preamble (lambda (s)
(set! preamble (string-append preamble " " s "\n")))) (set! preamble (string-append preamble " " s "\n"))))
(body (c-compile-exp exp append-preamble "cont" (list src-file) #t))) (body (c-compile-exp exp append-preamble "cont" -1 (list src-file) #t)))
;(write `(DEBUG ,body)) ;(write `(DEBUG ,body))
(string-append (string-append
preamble preamble
@ -327,6 +327,7 @@
;; append-preamble - ?? ;; append-preamble - ??
;; cont - name of the next continuation ;; cont - name of the next continuation
;; this is experimental and probably needs refinement ;; this is experimental and probably needs refinement
;; ast-id - The AST lambda ID of the function containing the expression
;; trace - trace information. presently a pair containing: ;; trace - trace information. presently a pair containing:
;; * source file ;; * source file
;; * function name (or NULL if none) ;; * function name (or NULL if none)
@ -336,13 +337,17 @@
;; be set to false to change the type of compilation. ;; be set to false to change the type of compilation.
;; NOTE: this field is not passed everywhere because a lot of forms ;; NOTE: this field is not passed everywhere because a lot of forms
;; require CPS, so this flag is not applicable to them. ;; require CPS, so this flag is not applicable to them.
(define (c-compile-exp exp append-preamble cont trace cps?) (define (c-compile-exp exp append-preamble cont ast-id trace cps?)
(cond (cond
; Special case - global function w/out a closure. Create an empty closure ; Special case - global function w/out a closure. Create an empty closure
((ast:lambda? exp) ((ast:lambda? exp)
(c-compile-exp (c-compile-exp
`(%closure ,exp) `(%closure ,exp)
append-preamble cont trace cps?)) append-preamble
cont
ast-id
trace
cps?))
; Core forms: ; Core forms:
((const? exp) (c-compile-const exp)) ((const? exp) (c-compile-const exp))
((prim? exp) ((prim? exp)
@ -350,7 +355,7 @@
(c-code (string-append "primitive_" (mangle exp)))) (c-code (string-append "primitive_" (mangle exp))))
((ref? exp) (c-compile-ref exp)) ((ref? exp) (c-compile-ref exp))
((quote? exp) (c-compile-quote exp)) ((quote? exp) (c-compile-quote exp))
((if? exp) (c-compile-if exp append-preamble cont trace cps?)) ((if? exp) (c-compile-if exp append-preamble cont ast-id trace cps?))
; IR (2): ; IR (2):
((tagged-list? '%closure exp) ((tagged-list? '%closure exp)
@ -362,7 +367,7 @@
(c-compile-raw-global-lambda exp append-preamble cont trace)) (c-compile-raw-global-lambda exp append-preamble cont trace))
; Application: ; Application:
((app? exp) (c-compile-app exp append-preamble cont trace cps?)) ((app? exp) (c-compile-app exp append-preamble cont ast-id trace cps?))
(else (error "unknown exp in c-compile-exp: " exp)))) (else (error "unknown exp in c-compile-exp: " exp))))
(define (c-compile-quote qexp) (define (c-compile-quote qexp)
@ -715,7 +720,7 @@
(mangle exp)))) (mangle exp))))
; c-compile-args : list[exp] (string -> void) -> string ; c-compile-args : list[exp] (string -> void) -> string
(define (c-compile-args args append-preamble prefix cont trace cps?) (define (c-compile-args args append-preamble prefix cont ast-id trace cps?)
(letrec ((num-args 0) (letrec ((num-args 0)
(_c-compile-args (_c-compile-args
(lambda (args append-preamble prefix cont) (lambda (args append-preamble prefix cont)
@ -728,7 +733,7 @@
(c:append/prefix (c:append/prefix
prefix prefix
(c-compile-exp (car args) (c-compile-exp (car args)
append-preamble cont trace cps?) append-preamble cont ast-id trace cps?)
(_c-compile-args (cdr args) (_c-compile-args (cdr args)
append-preamble ", " cont))))))) append-preamble ", " cont)))))))
(c:tuple/args (c:tuple/args
@ -737,7 +742,7 @@
num-args))) num-args)))
;; c-compile-app : app-exp (string -> void) -> string ;; c-compile-app : app-exp (string -> void) -> string
(define (c-compile-app exp append-preamble cont trace cps?) (define (c-compile-app exp append-preamble cont ast-id trace cps?)
;;(trace:info `(c-compile-app: ,exp ,trace)) ;;(trace:info `(c-compile-app: ,exp ,trace))
(let (($tmp (mangle (gensym 'tmp)))) (let (($tmp (mangle (gensym 'tmp))))
(let* ((args (app->args exp)) (let* ((args (app->args exp))
@ -753,6 +758,7 @@
append-preamble append-preamble
"" ""
this-cont this-cont
ast-id
trace trace
cps?)) cps?))
(num-cargs (c:num-args cgen))) (num-cargs (c:num-args cgen)))
@ -779,7 +785,7 @@
(let* ((cgen-lis (let* ((cgen-lis
(map (map
(lambda (e) (lambda (e)
(c-compile-exp e append-preamble "" "" cps?)) (c-compile-exp e append-preamble "" ast-id "" cps?))
(cddr args)) ;; Skip the closure (cddr args)) ;; Skip the closure
) )
(cgen-allocs (cgen-allocs
@ -822,7 +828,7 @@
(let* ((c-fun (let* ((c-fun
(c-compile-prim fun cont)) (c-compile-prim fun cont))
(c-args (c-args
(c-compile-args args append-preamble "" "" trace cps?)) (c-compile-args args append-preamble "" "" ast-id trace cps?))
(num-args (length args)) (num-args (length args))
(num-args-str (num-args-str
(string-append (string-append
@ -872,9 +878,9 @@
;; TODO: may not be good enough, closure app could be from an element ;; TODO: may not be good enough, closure app could be from an element
((tagged-list? '%closure-ref fun) ((tagged-list? '%closure-ref fun)
(let* ((cfun (c-compile-args (list (car args)) append-preamble " " cont trace cps?)) (let* ((cfun (c-compile-args (list (car args)) append-preamble " " cont ast-id trace cps?))
(this-cont (c:body cfun)) (this-cont (c:body cfun))
(cargs (c-compile-args (cdr args) append-preamble " " this-cont trace cps?))) (cargs (c-compile-args (cdr args) append-preamble " " this-cont ast-id trace cps?)))
(cond (cond
((not cps?) ((not cps?)
(c-code (c-code
@ -902,7 +908,7 @@
fun append-preamble cont trace cps?)) fun append-preamble cont trace cps?))
(this-cont (string-append "(closure)" (c:body cfun))) (this-cont (string-append "(closure)" (c:body cfun)))
(cargs (c-compile-args (cargs (c-compile-args
args append-preamble " " this-cont trace cps?)) args append-preamble " " this-cont ast-id trace cps?))
(num-cargs (c:num-args cargs))) (num-cargs (c:num-args cargs)))
(cond (cond
((not cps?) ((not cps?)
@ -953,7 +959,7 @@
(let ((cp1 (if (ref? expr) (let ((cp1 (if (ref? expr)
; Ignore lone ref to avoid C warning ; Ignore lone ref to avoid C warning
(c-code/vars "" '()) (c-code/vars "" '())
(c-compile-exp expr append-preamble cont trace cps?))) (c-compile-exp expr append-preamble cont ast-id trace cps?)))
(cp2 acc)) (cp2 acc))
(c-code/vars (c-code/vars
(let ((cp1-body (c:body cp1))) (let ((cp1-body (c:body cp1)))
@ -968,9 +974,9 @@
(error `(Unsupported function application ,exp))))))) (error `(Unsupported function application ,exp)))))))
; c-compile-if : if-exp -> string ; c-compile-if : if-exp -> string
(define (c-compile-if exp append-preamble cont trace cps?) (define (c-compile-if exp append-preamble cont ast-id trace cps?)
(let* ((compile (lambda (exp) (let* ((compile (lambda (exp)
(c-compile-exp exp append-preamble cont trace cps?))) (c-compile-exp exp append-preamble cont ast-id trace cps?)))
(test (compile (if->condition exp))) (test (compile (if->condition exp)))
(then (compile (if->then exp))) (then (compile (if->then exp)))
(els (compile (if->else exp)))) (els (compile (if->else exp))))
@ -1016,6 +1022,9 @@
(ast:lambda? body) (ast:lambda? body)
(c-compile-exp (c-compile-exp
body append-preamble cont body append-preamble cont
(if (ast:lambda? body)
(ast:lambda-id body)
-1)
(st:add-function! trace var) #t)) (st:add-function! trace var) #t))
;; Add inline global definition also, if applicable ;; Add inline global definition also, if applicable
@ -1034,6 +1043,7 @@
#t ;; always a lambda #t ;; always a lambda
(c-compile-exp (c-compile-exp
body append-preamble cont body append-preamble cont
(ast:lambda-id body)
(st:add-function! trace var) (st:add-function! trace var)
#f ;; inline, so disable CPS on this pass #f ;; inline, so disable CPS on this pass
) )
@ -1246,8 +1256,8 @@
"1" ;; Special case, need to change runtime checks for call/cc "1" ;; Special case, need to change runtime checks for call/cc
(number->string (compute-num-args lam)))) (number->string (compute-num-args lam))))
(create-object (lambda () (create-object (lambda ()
TODO: seems broken, why are we getting NULL in the generated code??? ;JAE - this is fine, now need to handle other side (actually reading the value without a closure obj
(trace:error `(create-object free-vars ,free-vars)) ;(trace:error `(create-object free-vars ,free-vars ,(car free-vars)))
(c-code/vars (c-code/vars
(car free-vars) (car free-vars)
(list)))) (list))))
@ -1294,7 +1304,7 @@
))))) )))))
;(trace:info (list 'JAE-DEBUG trace macro?)) ;(trace:info (list 'JAE-DEBUG trace macro?))
(cond (cond
(use-obj-instead-of-closure? (#f ;;TODO: use-obj-instead-of-closure?
(create-object)) (create-object))
(else (else
(c-code/vars (c-code/vars
@ -1369,6 +1379,7 @@
(car (ast:lambda-body exp)) ;; car ==> assume single expr in lambda body after CPS (car (ast:lambda-body exp)) ;; car ==> assume single expr in lambda body after CPS
append-preamble append-preamble
(mangle env-closure) (mangle env-closure)
(ast:lambda-id exp)
trace trace
cps?))) cps?)))
(cons (cons