mirror of
https://github.com/justinethier/cyclone.git
synced 2025-05-24 04:25:06 +02:00
Reassign args when doing C iteration
Reassign arguments when optimizing to use C iteration in place of recursive function calls.
This commit is contained in:
parent
d0564e991e
commit
49c2c093fd
1 changed files with 39 additions and 34 deletions
|
@ -760,6 +760,7 @@
|
||||||
; 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 ast-id trace cps?)
|
(define (c-compile-args args append-preamble prefix cont ast-id trace cps?)
|
||||||
(letrec ((num-args 0)
|
(letrec ((num-args 0)
|
||||||
|
(cp-lis '())
|
||||||
(_c-compile-args
|
(_c-compile-args
|
||||||
(lambda (args append-preamble prefix cont)
|
(lambda (args append-preamble prefix cont)
|
||||||
(cond
|
(cond
|
||||||
|
@ -767,17 +768,26 @@
|
||||||
(c-code ""))
|
(c-code ""))
|
||||||
(else
|
(else
|
||||||
;(trace:debug `(c-compile-args ,(car args)))
|
;(trace:debug `(c-compile-args ,(car args)))
|
||||||
|
(let ((cp (c-compile-exp (car args)
|
||||||
|
append-preamble cont ast-id trace cps?)))
|
||||||
(set! num-args (+ 1 num-args))
|
(set! num-args (+ 1 num-args))
|
||||||
|
(set! cp-lis (cons cp cp-lis))
|
||||||
(c:append/prefix
|
(c:append/prefix
|
||||||
prefix
|
prefix
|
||||||
(c-compile-exp (car args)
|
cp
|
||||||
append-preamble cont ast-id trace cps?)
|
|
||||||
(_c-compile-args (cdr args)
|
(_c-compile-args (cdr args)
|
||||||
append-preamble ", " cont)))))))
|
append-preamble ", " cont))))))))
|
||||||
|
;; Pass back a container with:
|
||||||
|
;; - Appened body (string)
|
||||||
|
;; - Appended allocs (string)
|
||||||
|
;; - Number of args (numeric)
|
||||||
|
;; - Remaining args - Actual CP objects (lists of body/alloc) from above
|
||||||
|
(append
|
||||||
(c:tuple/args
|
(c:tuple/args
|
||||||
(_c-compile-args args
|
(_c-compile-args args
|
||||||
append-preamble prefix cont)
|
append-preamble prefix cont)
|
||||||
num-args)))
|
num-args)
|
||||||
|
(reverse cp-lis))))
|
||||||
|
|
||||||
;; c-compile-app : app-exp (string -> void) -> string
|
;; c-compile-app : app-exp (string -> void) -> string
|
||||||
(define (c-compile-app exp append-preamble cont ast-id trace cps?)
|
(define (c-compile-app exp append-preamble cont ast-id trace cps?)
|
||||||
|
@ -924,6 +934,7 @@
|
||||||
(let* ((cfun (c-compile-args (list (car args)) append-preamble " " cont ast-id 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 ast-id trace cps?))
|
(cargs (c-compile-args (cdr args) append-preamble " " this-cont ast-id trace cps?))
|
||||||
|
(raw-cargs (cdddr cargs)) ;; Same as above but with lists instead of appended strings
|
||||||
(num-cargs (c:num-args cargs)))
|
(num-cargs (c:num-args cargs)))
|
||||||
(cond
|
(cond
|
||||||
((not cps?)
|
((not cps?)
|
||||||
|
@ -947,12 +958,14 @@
|
||||||
(adbf:calls-self? ast-fnc)
|
(adbf:calls-self? ast-fnc)
|
||||||
(self-closure-call? fun (car (adbf:all-params ast-fnc)))
|
(self-closure-call? fun (car (adbf:all-params ast-fnc)))
|
||||||
)
|
)
|
||||||
(let* ((params (map mangle (cdr (adbf:all-params ast-fnc))))
|
(let* ((params (map mangle (cdr (adbf:all-params ast-fnc))))
|
||||||
;; TODO: doesn't work, arg may contain non-CPS functions which have their own args...
|
(args (map car raw-cargs))
|
||||||
(args (map (lambda (s)
|
(reassignments
|
||||||
(string-replace-all s " " ""))
|
;; TODO: may need to detect cases where an arg is reassigned before
|
||||||
(string-split (c:body cargs) #\,)))
|
;; another one is assigned to that arg's old value, for example:
|
||||||
(reassignments (apply string-append
|
;; a = 1, b = 2, c = a
|
||||||
|
;; In this case the code would need to assign to a temporary variable
|
||||||
|
(apply string-append
|
||||||
(map
|
(map
|
||||||
(lambda (param arg)
|
(lambda (param arg)
|
||||||
(cond
|
(cond
|
||||||
|
@ -961,19 +974,11 @@
|
||||||
(string-append
|
(string-append
|
||||||
param " = " arg ";\n"))))
|
param " = " arg ";\n"))))
|
||||||
params
|
params
|
||||||
args))
|
args))))
|
||||||
))
|
|
||||||
;(for-each
|
|
||||||
; (lambda (param arg)
|
|
||||||
; (trace:error `(JAE ,param = ,arg)))
|
|
||||||
; params
|
|
||||||
; args)
|
|
||||||
|
|
||||||
(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")
|
||||||
;; TODO: reassign args
|
|
||||||
reassignments
|
reassignments
|
||||||
;; TODO: consider passing in a "top" instead of always calling alloca in macro below:
|
;; TODO: consider passing in a "top" instead of always calling alloca in macro below:
|
||||||
"continue_or_gc" (number->string (c:num-args cargs))
|
"continue_or_gc" (number->string (c:num-args cargs))
|
||||||
|
|
Loading…
Add table
Reference in a new issue