1 ;;; TREE-IL -> GLIL compiler
3 ;; Copyright (C) 2001,2008,2009,2010,2011 Free Software Foundation, Inc.
5 ;;;; This library is free software; you can redistribute it and/or
6 ;;;; modify it under the terms of the GNU Lesser General Public
7 ;;;; License as published by the Free Software Foundation; either
8 ;;;; version 3 of the License, or (at your option) any later version.
10 ;;;; This library is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;;;; Lesser General Public License for more details.
15 ;;;; You should have received a copy of the GNU Lesser General Public
16 ;;;; License along with this library; if not, write to the Free Software
17 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 (define-module (language tree-il compile-glil)
22 #:use-module (system base syntax)
23 #:use-module (system base pmatch)
24 #:use-module (system base message)
25 #:use-module (ice-9 receive)
26 #:use-module (language glil)
27 #:use-module (system vm instruction)
28 #:use-module (language tree-il)
29 #:use-module (language tree-il optimize)
30 #:use-module (language tree-il canonicalize)
31 #:use-module (language tree-il analyze)
32 #:use-module ((srfi srfi-1) #:select (filter-map))
33 #:export (compile-glil))
36 ;; sym -> {lambda -> address}
37 ;; lambda -> (labels . free-locs)
38 ;; lambda-case -> (gensym . nlocs)
40 ;; address ::= (local? boxed? . index)
41 ;; labels ::= ((sym . lambda) ...)
42 ;; free-locs ::= ((sym0 . address0) (sym1 . address1) ...)
43 ;; free variable addresses are relative to parent proc.
45 (define *comp-module* (make-fluid))
47 (define %warning-passes
48 `((unused-variable . ,unused-variable-analysis)
49 (unused-toplevel . ,unused-toplevel-analysis)
50 (unbound-variable . ,unbound-variable-analysis)
51 (arity-mismatch . ,arity-analysis)
52 (format . ,format-analysis)))
54 (define (compile-glil x e opts)
56 (or (and=> (memq #:warnings opts) cadr)
59 ;; Go through the warning passes.
60 (let ((analyses (filter-map (lambda (kind)
61 (assoc-ref %warning-passes kind))
63 (analyze-tree analyses x e))
65 (let* ((x (make-lambda (tree-il-src x) '()
66 (make-lambda-case #f '() #f #f #f '() '() x #f)))
67 (x (optimize! x e opts))
69 (allocation (analyze-lexicals x)))
71 (with-fluids ((*comp-module* e))
72 (values (flatten-lambda x #f allocation)
78 (define *primcall-ops* (make-hash-table))
80 (lambda (x) (hash-set! *primcall-ops* (car x) (cdr x)))
83 ((equal? . 2) . equal?)
95 ((quotient . 2) . quo)
96 ((remainder . 2) . rem)
99 ((logand . 2) . logand)
100 ((logior . 2) . logior)
101 ((logxor . 2) . logxor)
103 ((pair? . 1) . pair?)
107 ((set-car! . 2) . set-car!)
108 ((set-cdr! . 2) . set-cdr!)
109 ((null? . 1) . null?)
110 ((list? . 1) . list?)
111 ((symbol? . 1) . symbol?)
112 ((vector? . 1) . vector?)
115 ((class-of . 1) . class-of)
116 ((@slot-ref . 2) . slot-ref)
117 ((@slot-set! . 3) . slot-set)
118 ((vector-ref . 2) . vector-ref)
119 ((vector-set! . 3) . vector-set)
120 ((variable-ref . 1) . variable-ref)
121 ;; nb, *not* variable-set! -- the args are switched
122 ((variable-bound? . 1) . variable-bound?)
123 ((struct? . 1) . struct?)
124 ((struct-vtable . 1) . struct-vtable)
125 ((struct-ref . 2) . struct-ref)
126 ((struct-set! . 3) . struct-set)
127 (make-struct/no-tail . make-struct)
129 ;; hack for javascript
130 ((return . 1) . return)
132 (return/values . return/values)
134 ((bytevector-u8-ref . 2) . bv-u8-ref)
135 ((bytevector-u8-set! . 3) . bv-u8-set)
136 ((bytevector-s8-ref . 2) . bv-s8-ref)
137 ((bytevector-s8-set! . 3) . bv-s8-set)
139 ((bytevector-u16-ref . 3) . bv-u16-ref)
140 ((bytevector-u16-set! . 4) . bv-u16-set)
141 ((bytevector-u16-native-ref . 2) . bv-u16-native-ref)
142 ((bytevector-u16-native-set! . 3) . bv-u16-native-set)
143 ((bytevector-s16-ref . 3) . bv-s16-ref)
144 ((bytevector-s16-set! . 4) . bv-s16-set)
145 ((bytevector-s16-native-ref . 2) . bv-s16-native-ref)
146 ((bytevector-s16-native-set! . 3) . bv-s16-native-set)
148 ((bytevector-u32-ref . 3) . bv-u32-ref)
149 ((bytevector-u32-set! . 4) . bv-u32-set)
150 ((bytevector-u32-native-ref . 2) . bv-u32-native-ref)
151 ((bytevector-u32-native-set! . 3) . bv-u32-native-set)
152 ((bytevector-s32-ref . 3) . bv-s32-ref)
153 ((bytevector-s32-set! . 4) . bv-s32-set)
154 ((bytevector-s32-native-ref . 2) . bv-s32-native-ref)
155 ((bytevector-s32-native-set! . 3) . bv-s32-native-set)
157 ((bytevector-u64-ref . 3) . bv-u64-ref)
158 ((bytevector-u64-set! . 4) . bv-u64-set)
159 ((bytevector-u64-native-ref . 2) . bv-u64-native-ref)
160 ((bytevector-u64-native-set! . 3) . bv-u64-native-set)
161 ((bytevector-s64-ref . 3) . bv-s64-ref)
162 ((bytevector-s64-set! . 4) . bv-s64-set)
163 ((bytevector-s64-native-ref . 2) . bv-s64-native-ref)
164 ((bytevector-s64-native-set! . 3) . bv-s64-native-set)
166 ((bytevector-ieee-single-ref . 3) . bv-f32-ref)
167 ((bytevector-ieee-single-set! . 4) . bv-f32-set)
168 ((bytevector-ieee-single-native-ref . 2) . bv-f32-native-ref)
169 ((bytevector-ieee-single-native-set! . 3) . bv-f32-native-set)
170 ((bytevector-ieee-double-ref . 3) . bv-f64-ref)
171 ((bytevector-ieee-double-set! . 4) . bv-f64-set)
172 ((bytevector-ieee-double-native-ref . 2) . bv-f64-native-ref)
173 ((bytevector-ieee-double-native-set! . 3) . bv-f64-native-set)))
178 (define (make-label) (gensym ":L"))
180 (define (vars->bind-list ids vars allocation proc)
182 (pmatch (hashq-ref (hashq-ref allocation v) proc)
185 (,x (error "bad var list element" id v x))))
189 (define (emit-bindings src ids vars allocation proc emit-code)
190 (emit-code src (make-glil-bind
191 (vars->bind-list ids vars allocation proc))))
193 (define (with-output-to-code proc)
195 (define (emit-code src x)
196 (set! out (cons x out))
198 (set! out (cons (make-glil-source src) out))))
202 (define (flatten-lambda x self-label allocation)
204 ((<lambda> src meta body)
209 ;; write source info for proc
210 (if src (emit-code #f (make-glil-source src)))
211 ;; compile the body, yo
212 (flatten-lambda-case body allocation x self-label
213 (car (hashq-ref allocation x))
216 (define (flatten-lambda-case lcase allocation self self-label fix-labels
218 (define (emit-label label)
219 (emit-code #f (make-glil-label label)))
220 (define (emit-branch src inst label)
221 (emit-code src (make-glil-branch inst label)))
223 ;; RA: "return address"; #f unless we're in a non-tail fix with labels
224 ;; MVRA: "multiple-values return address"; #f unless we're in a let-values
225 (let comp ((x lcase) (context 'tail) (RA #f) (MVRA #f))
226 (define (comp-tail tree) (comp tree context RA MVRA))
227 (define (comp-push tree) (comp tree 'push #f #f))
228 (define (comp-drop tree) (comp tree 'drop #f #f))
229 (define (comp-vals tree MVRA) (comp tree 'vals #f MVRA))
230 (define (comp-fix tree RA) (comp tree context RA MVRA))
232 ;; A couple of helpers. Note that if we are in tail context, we
234 (define (maybe-emit-return)
236 (emit-branch #f 'br RA)
237 (if (eq? context 'tail)
238 (emit-code #f (make-glil-call 'return 1)))))
244 (emit-code #f (make-glil-void))))
250 (emit-code src (make-glil-const exp))))
253 ;; FIXME: should represent sequence as exps tail
255 (let lp ((exps exps))
256 (if (null? (cdr exps))
257 (comp-tail (car exps))
259 (comp-drop (car exps))
262 ((<application> src proc args)
263 ;; FIXME: need a better pattern-matcher here
265 ((and (primitive-ref? proc)
266 (eq? (primitive-ref-name proc) '@apply)
267 (>= (length args) 1))
268 (let ((proc (car args))
271 ((and (primitive-ref? proc) (eq? (primitive-ref-name proc) 'values)
272 (not (eq? context 'push)) (not (eq? context 'vals)))
273 ;; tail: (lambda () (apply values '(1 2)))
274 ;; drop: (lambda () (apply values '(1 2)) 3)
275 ;; push: (lambda () (list (apply values '(10 12)) 1))
277 ((drop) (for-each comp-drop args) (maybe-emit-return))
279 (for-each comp-push args)
280 (emit-code src (make-glil-call 'return/values* (length args))))))
286 (for-each comp-push args)
287 (emit-code src (make-glil-call 'tail-apply (1+ (length args)))))
289 (emit-code src (make-glil-call 'new-frame 0))
291 (for-each comp-push args)
292 (emit-code src (make-glil-call 'apply (1+ (length args))))
296 (make-application src (make-primitive-ref #f 'apply)
301 ;; Well, shit. The proc might return any number of
302 ;; values (including 0), since it's in a drop context,
303 ;; yet apply does not create a MV continuation. So we
304 ;; mv-call out to our trampoline instead.
306 (make-application src (make-primitive-ref #f 'apply)
308 (maybe-emit-return)))))))
310 ((and (primitive-ref? proc) (eq? (primitive-ref-name proc) 'values))
311 ;; tail: (lambda () (values '(1 2)))
312 ;; drop: (lambda () (values '(1 2)) 3)
313 ;; push: (lambda () (list (values '(10 12)) 1))
314 ;; vals: (let-values (((a b ...) (values 1 2 ...))) ...)
316 ((drop) (for-each comp-drop args) (maybe-emit-return))
320 ;; FIXME: This is surely an error. We need to add a
321 ;; values-mismatch warning pass.
322 (emit-code src (make-glil-call 'new-frame 0))
324 (emit-code src (make-glil-call 'call 0))
327 (comp-push (car args)))
329 ;; Taking advantage of unspecified order of evaluation of
331 (for-each comp-drop (cdr args))
332 (comp-push (car args)))))
334 (for-each comp-push args)
335 (emit-code #f (make-glil-const (length args)))
336 (emit-branch src 'br MVRA))
338 (for-each comp-push args)
339 (emit-code src (let ((len (length args)))
341 (make-glil-call 'return 1)
342 (make-glil-call 'return/values len)))))))
344 ((and (primitive-ref? proc)
345 (eq? (primitive-ref-name proc) '@call-with-values)
352 ;; MV: [tail-]call/nargs
353 ;; POST: (maybe-drop)
358 (make-application src (make-primitive-ref #f 'call-with-values)
363 (let ((MV (make-label)) (POST (make-label))
364 (producer (car args)) (consumer (cadr args)))
365 (if (not (eq? context 'tail))
366 (emit-code src (make-glil-call 'new-frame 0)))
368 (emit-code src (make-glil-call 'new-frame 0))
370 (emit-code src (make-glil-mv-call 0 MV))
372 ((tail) (emit-code src (make-glil-call 'tail-call 1)))
373 (else (emit-code src (make-glil-call 'call 1))
374 (emit-branch #f 'br POST)))
377 ((tail) (emit-code src (make-glil-call 'tail-call/nargs 0)))
378 (else (emit-code src (make-glil-call 'call/nargs 0))
380 (if (eq? context 'drop)
381 (emit-code #f (make-glil-call 'drop 1)))
382 (maybe-emit-return)))))))
384 ((and (primitive-ref? proc)
385 (eq? (primitive-ref-name proc) '@call-with-current-continuation)
389 (comp-push (car args))
390 (emit-code src (make-glil-call 'tail-call/cc 1)))
394 src (make-primitive-ref #f 'call-with-current-continuation)
399 (comp-push (car args))
400 (emit-code src (make-glil-call 'call/cc 1))
403 ;; Crap. Just like `apply' in drop context.
406 src (make-primitive-ref #f 'call-with-current-continuation)
408 (maybe-emit-return))))
410 ;; A hack for variable-set, the opcode for which takes its args
411 ;; reversed, relative to the variable-set! function
412 ((and (primitive-ref? proc)
413 (eq? (primitive-ref-name proc) 'variable-set!)
415 (comp-push (cadr args))
416 (comp-push (car args))
417 (emit-code src (make-glil-call 'variable-set 2))
419 ((tail push vals) (emit-code #f (make-glil-void))))
422 ((and (primitive-ref? proc)
423 (or (hash-ref *primcall-ops*
424 (cons (primitive-ref-name proc) (length args)))
425 (hash-ref *primcall-ops* (primitive-ref-name proc))))
427 (for-each comp-push args)
428 (emit-code src (make-glil-call op (length args)))
429 (case (instruction-pushes op)
432 ((tail push vals) (emit-code #f (make-glil-void))))
436 ((drop) (emit-code #f (make-glil-call 'drop 1))))
439 ;; A control instruction, like return/values. Here we
440 ;; just have to hope that the author of the tree-il
441 ;; knew what they were doing.
444 (error "bad primitive op: too many pushes"
445 op (instruction-pushes op))))))
447 ;; call to the same lambda-case in tail position
448 ((and (lexical-ref? proc)
449 self-label (eq? (lexical-ref-gensym proc) self-label)
451 (not (lambda-case-kw lcase))
452 (not (lambda-case-rest lcase))
454 (+ (length (lambda-case-req lcase))
455 (or (and=> (lambda-case-opt lcase) length) 0))))
456 (for-each comp-push args)
457 (for-each (lambda (sym)
458 (pmatch (hashq-ref (hashq-ref allocation sym) self)
459 ((#t #f . ,index) ; unboxed
460 (emit-code #f (make-glil-lexical #t #f 'set index)))
461 ((#t #t . ,index) ; boxed
463 (emit-code #f (make-glil-lexical #t #t 'box index)))
464 (,x (error "bad lambda-case arg allocation" x))))
465 (reverse (lambda-case-gensyms lcase)))
466 (emit-branch src 'br (car (hashq-ref allocation lcase))))
468 ;; lambda, the ultimate goto
469 ((and (lexical-ref? proc)
470 (assq (lexical-ref-gensym proc) fix-labels))
471 ;; like the self-tail-call case, though we can handle "drop"
472 ;; contexts too. first, evaluate new values, pushing them on
474 (for-each comp-push args)
475 ;; find the specific case, rename args, and goto the case label
476 (let lp ((lcase (lambda-body
477 (assq-ref fix-labels (lexical-ref-gensym proc)))))
479 ((and (lambda-case? lcase)
480 (not (lambda-case-kw lcase))
481 (not (lambda-case-opt lcase))
482 (not (lambda-case-rest lcase))
483 (= (length args) (length (lambda-case-req lcase))))
484 ;; we have a case that matches the args; rename variables
485 ;; and goto the case label
486 (for-each (lambda (sym)
487 (pmatch (hashq-ref (hashq-ref allocation sym) self)
488 ((#t #f . ,index) ; unboxed
489 (emit-code #f (make-glil-lexical #t #f 'set index)))
490 ((#t #t . ,index) ; boxed
491 (emit-code #f (make-glil-lexical #t #t 'box index)))
492 (,x (error "bad lambda-case arg allocation" x))))
493 (reverse (lambda-case-gensyms lcase)))
494 (emit-branch src 'br (car (hashq-ref allocation lcase))))
495 ((lambda-case? lcase)
496 ;; no match, try next case
497 (lp (lambda-case-alternate lcase)))
499 ;; no cases left. we can't really handle this currently.
500 ;; ideally we would push on a new frame, then do a "local
501 ;; call" -- which doesn't require consing up a program
502 ;; object. but for now error, as this sort of case should
503 ;; preclude label allocation.
504 (error "couldn't find matching case for label call" x)))))
507 (if (not (eq? context 'tail))
508 (emit-code src (make-glil-call 'new-frame 0)))
510 (for-each comp-push args)
511 (let ((len (length args)))
513 ((tail) (emit-code src (make-glil-call 'tail-call len)))
514 ((push) (emit-code src (make-glil-call 'call len))
516 ((vals) (emit-code src (make-glil-mv-call len MVRA))
518 ((drop) (let ((MV (make-label)) (POST (make-label)))
519 (emit-code src (make-glil-mv-call len MV))
520 (emit-code #f (make-glil-call 'drop 1))
521 (emit-branch #f 'br (or RA POST))
523 (emit-code #f (make-glil-mv-bind 0 #f))
525 (emit-branch #f 'br RA)
526 (emit-label POST)))))))))
528 ((<conditional> src test consequent alternate)
535 (let ((L1 (make-label)) (L2 (make-label)))
536 ;; need a pattern matcher
538 ((<application> proc args)
540 ((<primitive-ref> name)
541 (let ((len (length args)))
544 ((and (eq? name 'eq?) (= len 2))
545 (comp-push (car args))
546 (comp-push (cadr args))
547 (emit-branch src 'br-if-not-eq L1))
549 ((and (eq? name 'null?) (= len 1))
550 (comp-push (car args))
551 (emit-branch src 'br-if-not-null L1))
553 ((and (eq? name 'not) (= len 1))
554 (let ((app (car args)))
556 ((<application> proc args)
557 (let ((len (length args)))
559 ((<primitive-ref> name)
562 ((and (eq? name 'eq?) (= len 2))
563 (comp-push (car args))
564 (comp-push (cadr args))
565 (emit-branch src 'br-if-eq L1))
567 ((and (eq? name 'null?) (= len 1))
568 (comp-push (car args))
569 (emit-branch src 'br-if-null L1))
573 (emit-branch src 'br-if L1))))
576 (emit-branch src 'br-if L1)))))
579 (emit-branch src 'br-if L1)))))
583 (emit-branch src 'br-if-not L1)))))
586 (emit-branch src 'br-if-not L1))))
589 (emit-branch src 'br-if-not L1)))
591 (comp-tail consequent)
592 ;; if there is an RA, comp-tail will cause a jump to it -- just
593 ;; have to clean up here if there is no RA.
594 (if (and (not RA) (not (eq? context 'tail)))
595 (emit-branch #f 'br L2))
597 (comp-tail alternate)
598 (if (and (not RA) (not (eq? context 'tail)))
601 ((<primitive-ref> src name)
603 ((eq? (module-variable (fluid-ref *comp-module*) name)
604 (module-variable the-root-module name))
607 (emit-code src (make-glil-toplevel 'ref name))))
609 ((module-variable the-root-module name)
612 (emit-code src (make-glil-module 'ref '(guile) name #f))))
617 (emit-code src (make-glil-module
618 'ref (module-name (fluid-ref *comp-module*)) name #f))))
619 (maybe-emit-return))))
621 ((<lexical-ref> src gensym)
624 (pmatch (hashq-ref (hashq-ref allocation gensym) self)
625 ((,local? ,boxed? . ,index)
626 (emit-code src (make-glil-lexical local? boxed? 'ref index)))
628 (error "bad lexical allocation" x loc)))))
631 ((<lexical-set> src gensym exp)
633 (pmatch (hashq-ref (hashq-ref allocation gensym) self)
634 ((,local? ,boxed? . ,index)
635 (emit-code src (make-glil-lexical local? boxed? 'set index)))
637 (error "bad lexical allocation" x loc)))
640 (emit-code #f (make-glil-void))))
643 ((<module-ref> src mod name public?)
644 (emit-code src (make-glil-module 'ref mod name public?))
646 ((drop) (emit-code #f (make-glil-call 'drop 1))))
649 ((<module-set> src mod name public? exp)
651 (emit-code src (make-glil-module 'set mod name public?))
654 (emit-code #f (make-glil-void))))
657 ((<toplevel-ref> src name)
658 (emit-code src (make-glil-toplevel 'ref name))
660 ((drop) (emit-code #f (make-glil-call 'drop 1))))
663 ((<toplevel-set> src name exp)
665 (emit-code src (make-glil-toplevel 'set name))
668 (emit-code #f (make-glil-void))))
671 ((<toplevel-define> src name exp)
673 (emit-code src (make-glil-toplevel 'define name))
676 (emit-code #f (make-glil-void))))
680 (let ((free-locs (cdr (hashq-ref allocation x))))
683 (emit-code #f (flatten-lambda x #f allocation))
684 (if (not (null? free-locs))
689 ((,local? ,boxed? . ,n)
690 (emit-code #f (make-glil-lexical local? #f 'ref n)))
691 (else (error "bad lambda free var allocation" x loc))))
693 (emit-code #f (make-glil-call 'make-closure
694 (length free-locs))))))))
697 ((<lambda-case> src req opt rest kw inits gensyms alternate body)
698 ;; o/~ feature on top of feature o/~
700 ;; opt := (name ...) | #f
702 ;; kw: (allow-other-keys? (keyword name var) ...) | #f
703 ;; gensyms: (sym ...)
704 ;; init: tree-il in context of gensyms
705 ;; gensyms map to named arguments in the following order:
706 ;; required, optional (positional), rest, keyword.
707 (let* ((nreq (length req))
708 (nopt (if opt (length opt) 0))
709 (rest-idx (and rest (+ nreq nopt)))
710 (opt-names (or opt '()))
711 (allow-other-keys? (if kw (car kw) #f))
712 (kw-indices (map (lambda (x)
715 (cons key (list-index gensyms var)))
716 (else (error "bad kwarg" x))))
717 (if kw (cdr kw) '())))
718 (nargs (apply max (+ nreq nopt (if rest 1 0))
719 (map 1+ (map cdr kw-indices))))
720 (nlocs (cdr (hashq-ref allocation x)))
721 (alternate-label (and alternate (make-label))))
724 (+ nreq (length inits) (if rest 1 0)))
725 (error "lambda-case gensyms don't correspond to args"
726 req opt rest kw inits gensyms nreq nopt kw-indices nargs))
727 ;; the prelude, to check args & reset the stack pointer,
728 ;; allowing room for locals
733 (make-glil-kw-prelude nreq nopt rest-idx kw-indices
734 allow-other-keys? nlocs alternate-label))
736 (make-glil-opt-prelude nreq nopt rest-idx nlocs alternate-label))
738 (make-glil-std-prelude nreq nlocs alternate-label))))
739 ;; box args if necessary
742 (pmatch (hashq-ref (hashq-ref allocation v) self)
744 (emit-code #f (make-glil-lexical #t #f 'ref n))
745 (emit-code #f (make-glil-lexical #t #t 'box n)))))
747 ;; write bindings info
748 (if (not (null? gensyms))
751 (let lp ((kw (if kw (cdr kw) '()))
752 (names (append (reverse opt-names) (reverse req)))
753 (gensyms (list-tail gensyms (+ nreq nopt
757 ;; fixme: check that gensyms is empty
758 (reverse (if rest (cons rest names) names)))
759 (((,key ,name ,var) . ,kw)
760 (if (memq var gensyms)
761 (lp kw (cons name names) (delq var gensyms))
762 (lp kw names gensyms)))
763 (,kw (error "bad keywords, yo" kw))))
764 gensyms allocation self emit-code))
765 ;; init optional/kw args
766 (let lp ((inits inits) (n nreq) (gensyms (list-tail gensyms nreq)))
768 ((null? inits)) ; done
769 ((and rest-idx (= n rest-idx))
770 (lp inits (1+ n) (cdr gensyms)))
772 (pmatch (hashq-ref (hashq-ref allocation (car gensyms)) self)
773 ((#t ,boxed? . ,n*) (guard (= n* n))
774 (let ((L (make-label)))
775 (emit-code #f (make-glil-lexical #t boxed? 'bound? n))
776 (emit-code #f (make-glil-branch 'br-if L))
777 (comp-push (car inits))
778 (emit-code #f (make-glil-lexical #t boxed? 'set n))
780 (lp (cdr inits) (1+ n) (cdr gensyms))))
781 (#t (error "bad arg allocation" (car gensyms) inits))))))
782 ;; post-prelude case label for label calls
783 (emit-label (car (hashq-ref allocation x)))
785 (if (not (null? gensyms))
786 (emit-code #f (make-glil-unbind)))
789 (emit-label alternate-label)
790 (flatten-lambda-case alternate allocation self self-label
791 fix-labels emit-code)))))
793 ((<let> src names gensyms vals body)
794 (for-each comp-push vals)
795 (emit-bindings src names gensyms allocation self emit-code)
796 (for-each (lambda (v)
797 (pmatch (hashq-ref (hashq-ref allocation v) self)
799 (emit-code src (make-glil-lexical #t #f 'set n)))
801 (emit-code src (make-glil-lexical #t #t 'box n)))
802 (,loc (error "bad let var allocation" x loc))))
805 (emit-code #f (make-glil-unbind)))
807 ((<letrec> src in-order? names gensyms vals body)
808 ;; First prepare heap storage slots.
809 (for-each (lambda (v)
810 (pmatch (hashq-ref (hashq-ref allocation v) self)
812 (emit-code src (make-glil-lexical #t #t 'empty-box n)))
813 (,loc (error "bad letrec var allocation" x loc))))
815 ;; Even though the slots are empty, the bindings are valid.
816 (emit-bindings src names gensyms allocation self emit-code)
819 ;; For letrec*, bind values in order.
820 (for-each (lambda (name v val)
821 (pmatch (hashq-ref (hashq-ref allocation v) self)
824 (emit-code src (make-glil-lexical #t #t 'set n)))
825 (,loc (error "bad letrec var allocation" x loc))))
828 ;; But for letrec, eval all values, then bind.
829 (for-each comp-push vals)
830 (for-each (lambda (v)
831 (pmatch (hashq-ref (hashq-ref allocation v) self)
833 (emit-code src (make-glil-lexical #t #t 'set n)))
834 (,loc (error "bad letrec var allocation" x loc))))
837 (emit-code #f (make-glil-unbind)))
839 ((<fix> src names gensyms vals body)
840 ;; The ideal here is to just render the lambda bodies inline, and
841 ;; wire the code together with gotos. We can do that if
842 ;; analyze-lexicals has determined that a given var has "label"
843 ;; allocation -- which is the case if it is in `fix-labels'.
845 ;; But even for closures that we can't inline, we can do some
846 ;; tricks to avoid heap-allocation for the binding itself. Since
847 ;; we know the vals are lambdas, we can set them to their local
848 ;; var slots first, then capture their bindings, mutating them in
850 (let ((new-RA (if (or (eq? context 'tail) RA) #f (make-label))))
854 ((hashq-ref allocation x)
855 ;; allocating a closure
856 (emit-code #f (flatten-lambda x v allocation))
857 (let ((free-locs (cdr (hashq-ref allocation x))))
858 (if (not (null? free-locs))
859 ;; Need to make-closure first, so we have a fresh closure on
860 ;; the heap, but with a temporary free values.
862 (for-each (lambda (loc)
863 (emit-code #f (make-glil-const #f)))
865 (emit-code #f (make-glil-call 'make-closure
866 (length free-locs))))))
867 (pmatch (hashq-ref (hashq-ref allocation v) self)
869 (emit-code src (make-glil-lexical #t #f 'set n)))
870 (,loc (error "bad fix var allocation" x loc))))
872 ;; labels allocation: emit label & body, but jump over it
873 (let ((POST (make-label)))
874 (emit-branch #f 'br POST)
875 (let lp ((lcase (lambda-body x)))
878 ((<lambda-case> src req gensyms body alternate)
879 (emit-label (car (hashq-ref allocation lcase)))
880 ;; FIXME: opt & kw args in the bindings
881 (emit-bindings #f req gensyms allocation self emit-code)
883 (emit-code #f (make-glil-source src)))
884 (comp-fix body (or RA new-RA))
885 (emit-code #f (make-glil-unbind))
887 (emit-label POST)))))))
890 ;; Emit bindings metadata for closures
891 (let ((binds (let lp ((out '()) (gensyms gensyms) (names names))
892 (cond ((null? gensyms) (reverse! out))
893 ((assq (car gensyms) fix-labels)
894 (lp out (cdr gensyms) (cdr names)))
896 (lp (acons (car gensyms) (car names) out)
897 (cdr gensyms) (cdr names)))))))
898 (emit-bindings src (map cdr binds) (map car binds)
899 allocation self emit-code))
900 ;; Now go back and fix up the bindings for closures.
903 (let ((free-locs (if (hashq-ref allocation x)
904 (cdr (hashq-ref allocation x))
905 ;; can hit this latter case for labels allocation
907 (if (not (null? free-locs))
912 ((,local? ,boxed? . ,n)
913 (emit-code #f (make-glil-lexical local? #f 'ref n)))
914 (else (error "bad free var allocation" x loc))))
916 (pmatch (hashq-ref (hashq-ref allocation v) self)
918 (emit-code #f (make-glil-lexical #t #f 'fix n)))
919 (,loc (error "bad fix var allocation" x loc)))))))
925 (emit-code #f (make-glil-unbind))))
927 ((<let-values> src exp body)
929 ((<lambda-case> req opt kw rest gensyms body alternate)
930 (if (or opt kw alternate)
931 (error "unexpected lambda-case in let-values" x))
932 (let ((MV (make-label)))
934 (emit-code #f (make-glil-const 1))
936 (emit-code src (make-glil-mv-bind
938 (append req (if rest (list rest) '()))
939 gensyms allocation self)
941 (for-each (lambda (v)
942 (pmatch (hashq-ref (hashq-ref allocation v) self)
944 (emit-code src (make-glil-lexical #t #f 'set n)))
946 (emit-code src (make-glil-lexical #t #t 'box n)))
947 (,loc (error "bad let-values var allocation" x loc))))
950 (emit-code #f (make-glil-unbind))))))
952 ;; much trickier than i thought this would be, at first, due to the need
953 ;; to have body's return value(s) on the stack while the unwinder runs,
954 ;; then proceed with returning or dropping or what-have-you, interacting
955 ;; with RA and MVRA. What have you, I say.
956 ((<dynwind> src body winder unwinder)
959 (comp-drop (make-application src winder '()))
960 (emit-code #f (make-glil-call 'wind 2))
964 (let ((MV (make-label)))
966 ;; one value: unwind...
967 (emit-code #f (make-glil-call 'unwind 0))
968 (comp-drop (make-application src unwinder '()))
969 ;; ...and return the val
970 (emit-code #f (make-glil-call 'return 1))
973 ;; multiple values: unwind...
974 (emit-code #f (make-glil-call 'unwind 0))
975 (comp-drop (make-application src unwinder '()))
976 ;; and return the values.
977 (emit-code #f (make-glil-call 'return/nvalues 1))))
980 ;; we only want one value. so ask for one value
982 ;; and unwind, leaving the val on the stack
983 (emit-code #f (make-glil-call 'unwind 0))
984 (comp-drop (make-application src unwinder '())))
987 (let ((MV (make-label)))
989 ;; one value: push 1 and fall through to MV case
990 (emit-code #f (make-glil-const 1))
993 ;; multiple values: unwind...
994 (emit-code #f (make-glil-call 'unwind 0))
995 (comp-drop (make-application src unwinder '()))
996 ;; and goto the MVRA.
997 (emit-branch #f 'br MVRA)))
1000 ;; compile body, discarding values. then unwind...
1002 (emit-code #f (make-glil-call 'unwind 0))
1003 (comp-drop (make-application src unwinder '()))
1004 ;; and fall through, or goto RA if there is one.
1006 (emit-branch #f 'br RA)))))
1008 ((<dynlet> src fluids vals body)
1009 (for-each comp-push fluids)
1010 (for-each comp-push vals)
1011 (emit-code #f (make-glil-call 'wind-fluids (length fluids)))
1015 (let ((MV (make-label)))
1016 ;; NB: in tail case, it is possible to preserve asymptotic tail
1017 ;; recursion, via merging unwind-fluids structures -- but we'd need
1018 ;; to compile in the body twice (once in tail context, assuming the
1019 ;; caller unwinds, and once with this trampoline thing, unwinding
1022 ;; one value: unwind and return
1023 (emit-code #f (make-glil-call 'unwind-fluids 0))
1024 (emit-code #f (make-glil-call 'return 1))
1027 ;; multiple values: unwind and return values
1028 (emit-code #f (make-glil-call 'unwind-fluids 0))
1029 (emit-code #f (make-glil-call 'return/nvalues 1))))
1033 (emit-code #f (make-glil-call 'unwind-fluids 0)))
1036 (let ((MV (make-label)))
1038 ;; one value: push 1 and fall through to MV case
1039 (emit-code #f (make-glil-const 1))
1042 ;; multiple values: unwind and goto MVRA
1043 (emit-code #f (make-glil-call 'unwind-fluids 0))
1044 (emit-branch #f 'br MVRA)))
1047 ;; compile body, discarding values. then unwind...
1049 (emit-code #f (make-glil-call 'unwind-fluids 0))
1050 ;; and fall through, or goto RA if there is one.
1052 (emit-branch #f 'br RA)))))
1054 ((<dynref> src fluid)
1060 (emit-code #f (make-glil-call 'fluid-ref 1))))
1061 (maybe-emit-return))
1063 ((<dynset> src fluid exp)
1066 (emit-code #f (make-glil-call 'fluid-set 2))
1069 (emit-code #f (make-glil-void))))
1070 (maybe-emit-return))
1072 ;; What's the deal here? The deal is that we are compiling the start of a
1073 ;; delimited continuation. We try to avoid heap allocation in the normal
1074 ;; case; so the body is an expression, not a thunk, and we try to render
1075 ;; the handler inline. Also we did some analysis, in analyze.scm, so that
1076 ;; if the continuation isn't referenced, we don't reify it. This makes it
1077 ;; possible to implement catch and throw with delimited continuations,
1078 ;; without any overhead.
1079 ((<prompt> src tag body handler)
1080 (let ((H (make-label))
1082 (escape-only? (hashq-ref allocation x)))
1083 ;; First, set up the prompt.
1085 (emit-code src (make-glil-prompt H escape-only?))
1087 ;; Then we compile the body, with its normal return path, unwinding
1088 ;; before proceeding.
1091 (let ((MV (make-label)))
1093 ;; one value: unwind and return
1094 (emit-code #f (make-glil-call 'unwind 0))
1095 (emit-code #f (make-glil-call 'return 1))
1096 ;; multiple values: unwind and return
1098 (emit-code #f (make-glil-call 'unwind 0))
1099 (emit-code #f (make-glil-call 'return/nvalues 1))))
1102 ;; we only want one value. so ask for one value, unwind, and jump to
1105 (emit-code #f (make-glil-call 'unwind 0))
1106 (emit-branch #f 'br (or RA POST)))
1109 (let ((MV (make-label)))
1111 ;; one value: push 1 and fall through to MV case
1112 (emit-code #f (make-glil-const 1))
1113 ;; multiple values: unwind and goto MVRA
1115 (emit-code #f (make-glil-call 'unwind 0))
1116 (emit-branch #f 'br MVRA)))
1119 ;; compile body, discarding values, then unwind & fall through.
1121 (emit-code #f (make-glil-call 'unwind 0))
1122 (emit-branch #f 'br (or RA POST))))
1125 ;; Now the handler. The stack is now made up of the continuation, and
1126 ;; then the args to the continuation (pushed separately), and then the
1127 ;; number of args, including the continuation.
1128 (record-case handler
1129 ((<lambda-case> req opt kw rest gensyms body alternate)
1130 (if (or opt kw alternate)
1131 (error "unexpected lambda-case in prompt" x))
1132 (emit-code src (make-glil-mv-bind
1134 (append req (if rest (list rest) '()))
1135 gensyms allocation self)
1137 (for-each (lambda (v)
1138 (pmatch (hashq-ref (hashq-ref allocation v) self)
1140 (emit-code src (make-glil-lexical #t #f 'set n)))
1142 (emit-code src (make-glil-lexical #t #t 'box n)))
1144 (error "bad prompt handler arg allocation" x loc))))
1147 (emit-code #f (make-glil-unbind))))
1150 (or (eq? context 'push) (eq? context 'drop)))
1151 (emit-label POST))))
1153 ((<abort> src tag args tail)
1155 (for-each comp-push args)
1157 (emit-code src (make-glil-call 'abort (length args)))
1158 ;; so, the abort can actually return. if it does, the values will be on
1159 ;; the stack, then the MV marker, just as in an MV context.
1163 (emit-code #f (make-glil-call 'return/nvalues 1)))
1165 ;; Drop all values and goto RA, or otherwise fall through.
1166 (emit-code #f (make-glil-mv-bind 0 #f))
1167 (if RA (emit-branch #f 'br RA)))
1169 ;; Truncate to one value.
1170 (emit-code #f (make-glil-mv-bind 1 #f)))
1173 (emit-branch #f 'br MVRA)))))))