mirror of
https://github.com/justinethier/cyclone.git
synced 2025-05-25 04:55:04 +02:00
First start at CPS optimization
This commit is contained in:
parent
f6c79dd05f
commit
4e9a209f43
1 changed files with 49 additions and 0 deletions
|
@ -125,6 +125,7 @@
|
||||||
wrap-mutables
|
wrap-mutables
|
||||||
alpha-convert
|
alpha-convert
|
||||||
cps-convert
|
cps-convert
|
||||||
|
cps-optimize-01
|
||||||
pos-in-list
|
pos-in-list
|
||||||
closure-convert
|
closure-convert
|
||||||
)
|
)
|
||||||
|
@ -1458,7 +1459,55 @@
|
||||||
(else
|
(else
|
||||||
(cps ast '%halt)))))
|
(cps ast '%halt)))))
|
||||||
ast-cps))
|
ast-cps))
|
||||||
|
;; TODO: use for temporary testing of optimizations
|
||||||
|
; (cps-optimize-01 ast-cps)))
|
||||||
|
|
||||||
|
;; CPS optimizations
|
||||||
|
|
||||||
|
;; TODO: don't think we can assume lambda body is single expr, if we want
|
||||||
|
;; to do optimizations such as inlining
|
||||||
|
(define (cps-optimize-01 exp)
|
||||||
|
(define (opt-lambda exp)
|
||||||
|
(let ((body (car (lambda->exp exp)))) ;; Single expr after CPS
|
||||||
|
(trace:error `(DEBUG
|
||||||
|
,exp
|
||||||
|
,body
|
||||||
|
,(if (and (pair? body) (app? body) (lambda? (car body)))
|
||||||
|
(list (app->args body)
|
||||||
|
(lambda->formals exp))
|
||||||
|
#f)))
|
||||||
|
(cond
|
||||||
|
;; Does the function just call its continuation?
|
||||||
|
((and (pair? body)
|
||||||
|
(app? body)
|
||||||
|
(lambda? (car body))
|
||||||
|
;; TODO: need to check body length if we allow >1 expr in a body
|
||||||
|
;; TODO: not sure this is good enough for all cases
|
||||||
|
(equal? (app->args body)
|
||||||
|
;(lambda->formals (car body))
|
||||||
|
(lambda->formals exp)
|
||||||
|
))
|
||||||
|
(cps-optimize-01 (car body)))
|
||||||
|
(else
|
||||||
|
`(lambda ,(lambda->formals exp)
|
||||||
|
,(cps-optimize-01 (car (lambda->exp exp)))) ;; Assume single expr in lambda body, since after CPS phase
|
||||||
|
))))
|
||||||
|
(cond
|
||||||
|
; Core forms:
|
||||||
|
((const? exp) exp)
|
||||||
|
((ref? exp) exp)
|
||||||
|
((prim? exp) exp)
|
||||||
|
((quote? exp) exp)
|
||||||
|
((lambda? exp) (opt-lambda exp))
|
||||||
|
((set!? exp) `(set!
|
||||||
|
,(set!->var exp)
|
||||||
|
,(cps-optimize-01 (set!->exp exp))))
|
||||||
|
((if? exp) `(if ,(cps-optimize-01 (if->condition exp))
|
||||||
|
,(cps-optimize-01 (if->then exp))
|
||||||
|
,(cps-optimize-01 (if->else exp))))
|
||||||
|
; Application:
|
||||||
|
((app? exp) (map (lambda (e) (cps-optimize-01 e)) exp))
|
||||||
|
(else (error "CPS optimize unknown expression type: " exp))))
|
||||||
|
|
||||||
;; Closure-conversion.
|
;; Closure-conversion.
|
||||||
;;
|
;;
|
||||||
|
|
Loading…
Add table
Reference in a new issue