1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-06-22 19:44:10 +02:00

compile-js uses the new cps representation

* module/language/cps/compile-js.scm: Rewrite to use cps
This commit is contained in:
Ian Price 2017-06-14 23:07:40 +01:00
parent 23f829b175
commit 0e4fb0920f

View file

@ -1,48 +1,52 @@
(define-module (language cps compile-js) (define-module (language cps compile-js)
#:use-module (language cps) #:use-module (language cps)
#:use-module (language cps intmap)
#:use-module (language cps utils)
#:use-module ((language js-il) #:use-module ((language js-il)
#:renamer (lambda (x) (if (eqv? x 'make-prompt) 'make-prompt* x))) #:renamer (lambda (x) (if (eqv? x 'make-prompt) 'make-prompt* x)))
#:use-module (ice-9 match) #:use-module (ice-9 match)
#:export (compile-js)) #:export (compile-js))
(define intmap-select (@@ (language cps compile-bytecode) intmap-select))
(define lower-cps (@@ (language cps compile-bytecode) lower-cps)) (define lower-cps (@@ (language cps compile-bytecode) lower-cps))
(define (compile-js exp env opts) (define (compile-js exp env opts)
(set! exp (lower-cps exp opts)) ;; TODO: I should special case the compilation for the initial fun,
(match exp ;; as this is the entry point for the program, and shouldn't get a
(($ $program (($ $cont ks funs) ...)) ;; "self" argument, for now, I add "undefined" as the first
;; TODO: I should special case the compilation for the initial fun, ;; argument in the call to it.
;; as this is the entry point for the program, and shouldn't get a ;; see compile-exp in (language js-il compile-javascript)
;; "self" argument, for now, I add "undefined" as the first (define (intmap->program map)
;; argument in the call to it. (intmap-fold-right (lambda (kfun body accum)
;; see compile-exp in (language js-il compile-javascript) (acons (make-kid kfun)
(values (make-program (compile-fun (intmap-select map body) kfun)
(map (lambda (k fun) accum))
(cons (make-kid k) (compile-fun fun))) (compute-reachable-functions map 0)
ks '()))
funs)) (values (make-program (intmap->program (lower-cps exp opts))) env env))
env
env))))
(define (compile-fun fun)
(match fun (define (compile-fun cps kfun)
(($ $kfun _ _ self ($ $cont tail ($ $ktail)) clause) (match (intmap-ref cps kfun)
(($ $kfun src meta self tail clause)
(make-function (make-function
(make-id self) (make-id self)
(make-kid tail) (make-kid tail)
(compile-clauses clause self))))) (compile-clauses cps clause self)))))
(define (compile-clauses clause self)
(match clause (define (compile-clauses cps clause self)
(($ $cont k ($ $kclause arity body #f)) (match (intmap-ref cps clause)
`((,(make-kid k) (($ $kclause arity body #f)
`((,(make-kid clause)
,(arity->params arity self) ,(arity->params arity self)
,(compile-clause arity body self)))) ,(compile-clause cps arity body self))))
(($ $cont k ($ $kclause arity body next)) (($ $kclause arity body next)
`((,(make-kid k) `((,(make-kid clause)
,(arity->params arity self) ,(arity->params arity self)
,(compile-clause arity body self)) ,(compile-clause cps arity body self))
. ,(compile-clauses next self))))) . ,(compile-clauses cps next self)))))
(define (arity->params arity self) (define (arity->params arity self)
(match arity (match arity
@ -58,34 +62,23 @@
kw-syms) kw-syms)
allow-other-keys?)))) allow-other-keys?))))
(define (compile-clause arity body self)
(define (compile-clause cps arity body self)
(match arity (match arity
(($ $arity req opt rest ((_ _ kw-syms) ...) _) (($ $arity req opt rest ((_ _ kw-syms) ...) _)
(let ((ids (map make-id (let ((ids (map make-id
(append req opt kw-syms (if rest (list rest) '()))))) (append req opt kw-syms (if rest (list rest) '())))))
(make-continuation (make-continuation
(cons (make-id self) ids) (cons (make-id self) ids)
(match body (make-local `((,(make-kid body) . ,(compile-cont cps body)))
(($ $cont k cont) (make-continue (make-kid body) ids)))))))
(make-local `((,(make-kid k) . ,(compile-cont cont)))
(make-continue (make-kid k) ids)))))))))
(define (compile-term term)
(match term
(($ $letk (($ $cont ks conts) ...) body)
(make-local (map (lambda (k cont)
(cons (make-kid k)
(compile-cont cont)))
ks
conts)
(compile-term body)))
(($ $continue k src exp)
(compile-exp exp k))))
(define (compile-cont cont) (define (compile-cont cps cont)
(match cont (match (intmap-ref cps cont)
(($ $kargs names syms body) ;; The term in a $kargs is always a $continue
(make-continuation (map make-id syms) (compile-term body))) (($ $kargs names syms ($ $continue k src exp))
(make-continuation (map make-id syms) (compile-exp exp k)))
(($ $kreceive ($ $arity req _ (? symbol? rest) _ _) k2) (($ $kreceive ($ $arity req _ (? symbol? rest) _ _) k2)
(let ((ids (map make-id (append req (list rest))))) (let ((ids (map make-id (append req (list rest)))))
(make-continuation ids (make-continue (make-kid k2) ids)))) (make-continuation ids (make-continue (make-kid k2) ids))))