Emit return_direct_with_clo when possible.

This speeds up compilation slightly but the real benefit (I hope) will be when we can leverage it to eliminate certain closures.
This commit is contained in:
Justin Ethier 2018-09-17 13:12:15 -04:00
parent 189728d571
commit 9c377cd872

View file

@ -131,6 +131,7 @@
) )
(emit-c-arity-macros (+ arity 1)))) (emit-c-arity-macros (+ arity 1))))
;; Generate macros to call a closures
(define (c-macro-return-closcall num-args) (define (c-macro-return-closcall num-args)
(let ((args (c-macro-n-prefix num-args ",a")) (let ((args (c-macro-n-prefix num-args ",a"))
(n (number->string num-args)) (n (number->string num-args))
@ -149,6 +150,7 @@
" } \\\n" " } \\\n"
"}\n"))) "}\n")))
;; Generate macros to directly call a lambda function
(define (c-macro-return-direct num-args) (define (c-macro-return-direct num-args)
(let ((args (c-macro-n-prefix num-args ",a")) (let ((args (c-macro-n-prefix num-args ",a"))
(n (number->string num-args)) (n (number->string num-args))
@ -166,6 +168,7 @@
" (_fn)(td, " n ", (closure)_fn" args "); \\\n" " (_fn)(td, " n ", (closure)_fn" args "); \\\n"
" }}\n"))) " }}\n")))
;; Generate hybrid macros that can call a function directly but also receives a closure
(define (c-macro-return-direct-with-closure num-args) (define (c-macro-return-direct-with-closure num-args)
(let ((args (c-macro-n-prefix num-args ",a")) (let ((args (c-macro-n-prefix num-args ",a"))
(n (number->string num-args)) (n (number->string num-args))
@ -913,23 +916,25 @@
(else ;; CPS, IE normal behavior (else ;; CPS, IE normal behavior
(set-c-call-arity! num-cargs) (set-c-call-arity! num-cargs)
(with-fnc (ast:lambda-id (closure->lam fun)) (lambda (fnc) (with-fnc (ast:lambda-id (closure->lam fun)) (lambda (fnc)
(if (and #f (adbf:well-known fnc)) (if (adbf:well-known fnc)
#f (let* ((lid (adbf:cgen-id fnc))
;; TODO: raw lambda is called: (c-lambda-fnc-str (string-append "__lambda_" (number->string lid)))
;; use adbf:cgen-id to get it - )
;(this-cont (string-append "__lambda_" (number->string lid))) (c-code
;;(c-code (string-append
;; (string-append (c:allocs->str (c:allocs cfun) "\n")
;; (c:allocs->str (c:allocs cfun) "\n") (c:allocs->str (c:allocs cargs) "\n")
;; (c:allocs->str (c:allocs cargs) "\n") "return_direct_with_clo" (number->string num-cargs)
;; "return_direct_with_clo" (number->string num-cargs) "(data,"
;; "(data," this-cont
;; this-cont ","
;; "," c-lambda-fnc-str
;; // TODO: fnc name, twice ","
;; (if (> num-cargs 0) "," "") c-lambda-fnc-str
;; (c:body cargs) (if (> num-cargs 0) "," "")
;; ");")) (c:body cargs)
");"))
)
(c-code (c-code
(string-append (string-append
(c:allocs->str (c:allocs cfun) "\n") (c:allocs->str (c:allocs cfun) "\n")
@ -1116,11 +1121,8 @@
(define lambdas '()) (define lambdas '())
(define inline-lambdas '()) (define inline-lambdas '())
;; TODO: may need to pass in AST lambda ID and store a mapping ;; allocate-lambda : (Either ast:lambda boolean) -> (string -> string) -> integer
;; of cgen lambda ID to it, in order to use data from the ;; Create/store/return a unique lambda-id for the given function.
;; analysis DB later on during code generation.
;;
; allocate-lambda : (Either ast boolean) -> (string -> string) -> lambda-id
(define (allocate-lambda ast:lam lam . cps?) (define (allocate-lambda ast:lam lam . cps?)
(let ((id num-lambdas)) (let ((id num-lambdas))
(set! num-lambdas (+ 1 num-lambdas)) (set! num-lambdas (+ 1 num-lambdas))
@ -1128,7 +1130,7 @@
(if (equal? cps? '(#f)) (if (equal? cps? '(#f))
(set! inline-lambdas (cons id inline-lambdas))) (set! inline-lambdas (cons id inline-lambdas)))
(when ast:lam (when ast:lam
(with-fnc! ast:lam (lambda (fnc) (with-fnc! (ast:lambda-id ast:lam) (lambda (fnc)
(adbf:set-cgen-id! fnc id)))) (adbf:set-cgen-id! fnc id))))
id)) id))