1 ;;; TREE-IL -> GLIL compiler
3 ;; Copyright (C) 2001,2008,2009,2010,2011,2012 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?)
116 ((class-of . 1) . class-of)
117 ((@slot-ref . 2) . slot-ref)
118 ((@slot-set! . 3) . slot-set)
119 ((string-length . 1) . string-length)
120 ((string-ref . 2) . string-ref)
121 ((vector-length . 1) . vector-length)
122 ((vector-ref . 2) . vector-ref)
123 ((vector-set! . 3) . vector-set)
124 ((variable-ref . 1) . variable-ref)
125 ;; nb, *not* variable-set! -- the args are switched
126 ((variable-bound? . 1) . variable-bound?)
127 ((struct? . 1) . struct?)
128 ((struct-vtable . 1) . struct-vtable)
129 ((struct-ref . 2) . struct-ref)
130 ((struct-set! . 3) . struct-set)
131 (make-struct/no-tail . make-struct)
133 ;; hack for javascript
134 ((return . 1) . return)
136 (return/values . return/values)
138 ((bytevector-u8-ref . 2) . bv-u8-ref)
139 ((bytevector-u8-set! . 3) . bv-u8-set)
140 ((bytevector-s8-ref . 2) . bv-s8-ref)
141 ((bytevector-s8-set! . 3) . bv-s8-set)
143 ((bytevector-u16-ref . 3) . bv-u16-ref)
144 ((bytevector-u16-set! . 4) . bv-u16-set)
145 ((bytevector-u16-native-ref . 2) . bv-u16-native-ref)
146 ((bytevector-u16-native-set! . 3) . bv-u16-native-set)
147 ((bytevector-s16-ref . 3) . bv-s16-ref)
148 ((bytevector-s16-set! . 4) . bv-s16-set)
149 ((bytevector-s16-native-ref . 2) . bv-s16-native-ref)
150 ((bytevector-s16-native-set! . 3) . bv-s16-native-set)
152 ((bytevector-u32-ref . 3) . bv-u32-ref)
153 ((bytevector-u32-set! . 4) . bv-u32-set)
154 ((bytevector-u32-native-ref . 2) . bv-u32-native-ref)
155 ((bytevector-u32-native-set! . 3) . bv-u32-native-set)
156 ((bytevector-s32-ref . 3) . bv-s32-ref)
157 ((bytevector-s32-set! . 4) . bv-s32-set)
158 ((bytevector-s32-native-ref . 2) . bv-s32-native-ref)
159 ((bytevector-s32-native-set! . 3) . bv-s32-native-set)
161 ((bytevector-u64-ref . 3) . bv-u64-ref)
162 ((bytevector-u64-set! . 4) . bv-u64-set)
163 ((bytevector-u64-native-ref . 2) . bv-u64-native-ref)
164 ((bytevector-u64-native-set! . 3) . bv-u64-native-set)
165 ((bytevector-s64-ref . 3) . bv-s64-ref)
166 ((bytevector-s64-set! . 4) . bv-s64-set)
167 ((bytevector-s64-native-ref . 2) . bv-s64-native-ref)
168 ((bytevector-s64-native-set! . 3) . bv-s64-native-set)
170 ((bytevector-ieee-single-ref . 3) . bv-f32-ref)
171 ((bytevector-ieee-single-set! . 4) . bv-f32-set)
172 ((bytevector-ieee-single-native-ref . 2) . bv-f32-native-ref)
173 ((bytevector-ieee-single-native-set! . 3) . bv-f32-native-set)
174 ((bytevector-ieee-double-ref . 3) . bv-f64-ref)
175 ((bytevector-ieee-double-set! . 4) . bv-f64-set)
176 ((bytevector-ieee-double-native-ref . 2) . bv-f64-native-ref)
177 ((bytevector-ieee-double-native-set! . 3) . bv-f64-native-set)))
182 (define (make-label) (gensym ":L"))
184 (define (vars->bind-list ids vars allocation proc)
186 (pmatch (hashq-ref (hashq-ref allocation v) proc)
189 (,x (error "bad var list element" id v x))))
193 (define (emit-bindings src ids vars allocation proc emit-code)
194 (emit-code src (make-glil-bind
195 (vars->bind-list ids vars allocation proc))))
197 (define (with-output-to-code proc)
199 (define (emit-code src x)
200 (set! out (cons x out))
202 (set! out (cons (make-glil-source src) out))))
206 (define (flatten-lambda x self-label allocation)
208 ((<lambda> src meta body)
213 ;; write source info for proc
214 (if src (emit-code #f (make-glil-source src)))
215 ;; compile the body, yo
216 (flatten-lambda-case body allocation x self-label
217 (car (hashq-ref allocation x))
220 (define (flatten-lambda-case lcase allocation self self-label fix-labels
222 (define (emit-label label)
223 (emit-code #f (make-glil-label label)))
224 (define (emit-branch src inst label)
225 (emit-code src (make-glil-branch inst label)))
227 ;; RA: "return address"; #f unless we're in a non-tail fix with labels
228 ;; MVRA: "multiple-values return address"; #f unless we're in a let-values
229 (let comp ((x lcase) (context 'tail) (RA #f) (MVRA #f))
230 (define (comp-tail tree) (comp tree context RA MVRA))
231 (define (comp-push tree) (comp tree 'push #f #f))
232 (define (comp-drop tree) (comp tree 'drop #f #f))
233 (define (comp-vals tree MVRA) (comp tree 'vals #f MVRA))
234 (define (comp-fix tree RA) (comp tree context RA MVRA))
236 ;; A couple of helpers. Note that if we are in tail context, we
238 (define (maybe-emit-return)
240 (emit-branch #f 'br RA)
241 (if (eq? context 'tail)
242 (emit-code #f (make-glil-call 'return 1)))))
244 ;; After lexical binding forms in non-tail context, call this
245 ;; function to clear stack slots, allowing their previous values to
247 (define (clear-stack-slots context syms)
250 (for-each (lambda (v)
252 ;; Can be #f if the var is labels-allocated.
253 (hashq-ref allocation v)
255 (pmatch (hashq-ref h self)
257 (emit-code #f (make-glil-void))
258 (emit-code #f (make-glil-lexical #t #f 'set n)))
259 (,loc (error "bad let var allocation" x loc))))))
266 (emit-code #f (make-glil-void))))
272 (emit-code src (make-glil-const exp))))
279 ((<call> src proc args)
281 ;; call to the same lambda-case in tail position
282 ((and (lexical-ref? proc)
283 self-label (eq? (lexical-ref-gensym proc) self-label)
285 (not (lambda-case-kw lcase))
286 (not (lambda-case-rest lcase))
288 (+ (length (lambda-case-req lcase))
289 (or (and=> (lambda-case-opt lcase) length) 0))))
290 (for-each comp-push args)
291 (for-each (lambda (sym)
292 (pmatch (hashq-ref (hashq-ref allocation sym) self)
293 ((#t #f . ,index) ; unboxed
294 (emit-code #f (make-glil-lexical #t #f 'set index)))
295 ((#t #t . ,index) ; boxed
297 (emit-code #f (make-glil-lexical #t #t 'box index)))
298 (,x (error "bad lambda-case arg allocation" x))))
299 (reverse (lambda-case-gensyms lcase)))
300 (emit-branch src 'br (car (hashq-ref allocation lcase))))
302 ;; lambda, the ultimate goto
303 ((and (lexical-ref? proc)
304 (assq (lexical-ref-gensym proc) fix-labels))
305 ;; like the self-tail-call case, though we can handle "drop"
306 ;; contexts too. first, evaluate new values, pushing them on
308 (for-each comp-push args)
309 ;; find the specific case, rename args, and goto the case label
310 (let lp ((lcase (lambda-body
311 (assq-ref fix-labels (lexical-ref-gensym proc)))))
313 ((and (lambda-case? lcase)
314 (not (lambda-case-kw lcase))
315 (not (lambda-case-opt lcase))
316 (not (lambda-case-rest lcase))
317 (= (length args) (length (lambda-case-req lcase))))
318 ;; we have a case that matches the args; rename variables
319 ;; and goto the case label
320 (for-each (lambda (sym)
321 (pmatch (hashq-ref (hashq-ref allocation sym) self)
322 ((#t #f . ,index) ; unboxed
323 (emit-code #f (make-glil-lexical #t #f 'set index)))
324 ((#t #t . ,index) ; boxed
325 (emit-code #f (make-glil-lexical #t #t 'box index)))
326 (,x (error "bad lambda-case arg allocation" x))))
327 (reverse (lambda-case-gensyms lcase)))
328 (emit-branch src 'br (car (hashq-ref allocation lcase))))
329 ((lambda-case? lcase)
330 ;; no match, try next case
331 (lp (lambda-case-alternate lcase)))
333 ;; no cases left. we can't really handle this currently.
334 ;; ideally we would push on a new frame, then do a "local
335 ;; call" -- which doesn't require consing up a program
336 ;; object. but for now error, as this sort of case should
337 ;; preclude label allocation.
338 (error "couldn't find matching case for label call" x)))))
341 (if (not (eq? context 'tail))
342 (emit-code src (make-glil-call 'new-frame 0)))
344 (for-each comp-push args)
345 (let ((len (length args)))
347 ((tail) (emit-code src (make-glil-call 'tail-call len)))
348 ((push) (emit-code src (make-glil-call 'call len))
350 ((vals) (emit-code src (make-glil-mv-call len MVRA))
352 ((drop) (let ((MV (make-label)) (POST (make-label)))
353 (emit-code src (make-glil-mv-call len MV))
354 (emit-code #f (make-glil-call 'drop 1))
355 (emit-branch #f 'br (or RA POST))
357 (emit-code #f (make-glil-mv-bind 0 #f))
359 (emit-branch #f 'br RA)
360 (emit-label POST)))))))))
362 ((<primcall> src name args)
363 (pmatch (cons name args)
364 ((@apply ,proc . ,args)
366 ((and (primitive-ref? proc) (eq? (primitive-ref-name proc) 'values)
367 (not (eq? context 'push)) (not (eq? context 'vals)))
368 ;; tail: (lambda () (apply values '(1 2)))
369 ;; drop: (lambda () (apply values '(1 2)) 3)
370 ;; push: (lambda () (list (apply values '(10 12)) 1))
372 ((drop) (for-each comp-drop args) (maybe-emit-return))
374 (for-each comp-push args)
375 (emit-code src (make-glil-call 'return/values* (length args))))))
381 (for-each comp-push args)
382 (emit-code src (make-glil-call 'tail-apply (1+ (length args)))))
384 (emit-code src (make-glil-call 'new-frame 0))
386 (for-each comp-push args)
387 (emit-code src (make-glil-call 'apply (1+ (length args))))
390 (comp-tail (make-primcall src 'apply (cons proc args))))))))
393 ;; tail: (lambda () (values '(1 2)))
394 ;; drop: (lambda () (values '(1 2)) 3)
395 ;; push: (lambda () (list (values '(10 12)) 1))
396 ;; vals: (let-values (((a b ...) (values 1 2 ...))) ...)
398 ((drop) (for-each comp-drop args) (maybe-emit-return))
402 ;; FIXME: This is surely an error. We need to add a
403 ;; values-mismatch warning pass.
404 (comp-push (make-call src (make-primitive-ref #f 'values)
407 ;; Taking advantage of unspecified order of evaluation of
409 (for-each comp-drop (cdr args))
410 (comp-push (car args))
411 (maybe-emit-return))))
413 (for-each comp-push args)
414 (emit-code #f (make-glil-const (length args)))
415 (emit-branch src 'br MVRA))
417 (for-each comp-push args)
418 (emit-code src (let ((len (length args)))
420 (make-glil-call 'return 1)
421 (make-glil-call 'return/values len)))))))
423 ((@call-with-values ,producer ,consumer)
429 ;; MV: [tail-]call/nargs
430 ;; POST: (maybe-drop)
434 (comp-tail (make-primcall src 'call-with-values args)))
436 (let ((MV (make-label)) (POST (make-label)))
437 (if (not (eq? context 'tail))
438 (emit-code src (make-glil-call 'new-frame 0)))
440 (emit-code src (make-glil-call 'new-frame 0))
442 (emit-code src (make-glil-mv-call 0 MV))
444 ((tail) (emit-code src (make-glil-call 'tail-call 1)))
445 (else (emit-code src (make-glil-call 'call 1))
446 (emit-branch #f 'br POST)))
449 ((tail) (emit-code src (make-glil-call 'tail-call/nargs 0)))
450 (else (emit-code src (make-glil-call 'call/nargs 0))
452 (if (eq? context 'drop)
453 (emit-code #f (make-glil-call 'drop 1)))
454 (maybe-emit-return)))))))
456 ((@call-with-current-continuation ,proc)
460 (emit-code src (make-glil-call 'tail-call/cc 1)))
463 (make-primcall src 'call-with-current-continuation args)
468 (emit-code src (make-glil-call 'call/cc 1))
473 (make-primcall src 'call-with-current-continuation args)))))
475 ;; A hack for variable-set, the opcode for which takes its args
476 ;; reversed, relative to the variable-set! function
477 ((variable-set! ,var ,val)
480 (emit-code src (make-glil-call 'variable-set 2))
482 ((tail push vals) (emit-code #f (make-glil-void))))
487 ((or (hash-ref *primcall-ops* (cons name (length args)))
488 (hash-ref *primcall-ops* name))
490 (for-each comp-push args)
491 (emit-code src (make-glil-call op (length args)))
492 (case (instruction-pushes op)
495 ((tail push vals) (emit-code #f (make-glil-void))))
499 ((drop) (emit-code #f (make-glil-call 'drop 1))))
502 ;; A control instruction, like return/values. Here we
503 ;; just have to hope that the author of the tree-il
504 ;; knew what they were doing.
507 (error "bad primitive op: too many pushes"
508 op (instruction-pushes op))))))
510 ;; Fall back to the normal compilation strategy.
511 (comp-tail (make-call src (make-primitive-ref #f name) args)))))))
513 ((<conditional> src test consequent alternate)
520 (let ((L1 (make-label)) (L2 (make-label)))
522 ((<primcall> name args)
523 (pmatch (cons name args)
527 (emit-branch src 'br-if-not-eq L1))
530 (emit-branch src 'br-if-not-null L1))
533 (emit-branch src 'br-if-not-nil L1))
536 ((<primcall> name args)
537 (pmatch (cons name args)
541 (emit-branch src 'br-if-eq L1))
544 (emit-branch src 'br-if-null L1))
547 (emit-branch src 'br-if-nil L1))
550 (emit-branch src 'br-if L1))))
553 (emit-branch src 'br-if L1))))
556 (emit-branch src 'br-if-not L1))))
559 (emit-branch src 'br-if-not L1)))
561 (comp-tail consequent)
562 ;; if there is an RA, comp-tail will cause a jump to it -- just
563 ;; have to clean up here if there is no RA.
564 (if (and (not RA) (not (eq? context 'tail)))
565 (emit-branch #f 'br L2))
567 (comp-tail alternate)
568 (if (and (not RA) (not (eq? context 'tail)))
571 ((<primitive-ref> src name)
573 ((eq? (module-variable (fluid-ref *comp-module*) name)
574 (module-variable the-root-module name))
577 (emit-code src (make-glil-toplevel 'ref name))))
579 ((module-variable the-root-module name)
582 (emit-code src (make-glil-module 'ref '(guile) name #f))))
587 (emit-code src (make-glil-module
588 'ref (module-name (fluid-ref *comp-module*)) name #f))))
589 (maybe-emit-return))))
591 ((<lexical-ref> src gensym)
594 (pmatch (hashq-ref (hashq-ref allocation gensym) self)
595 ((,local? ,boxed? . ,index)
596 (emit-code src (make-glil-lexical local? boxed? 'ref index)))
598 (error "bad lexical allocation" x loc)))))
601 ((<lexical-set> src gensym exp)
603 (pmatch (hashq-ref (hashq-ref allocation gensym) self)
604 ((,local? ,boxed? . ,index)
605 (emit-code src (make-glil-lexical local? boxed? 'set index)))
607 (error "bad lexical allocation" x loc)))
610 (emit-code #f (make-glil-void))))
613 ((<module-ref> src mod name public?)
614 (emit-code src (make-glil-module 'ref mod name public?))
616 ((drop) (emit-code #f (make-glil-call 'drop 1))))
619 ((<module-set> src mod name public? exp)
621 (emit-code src (make-glil-module 'set mod name public?))
624 (emit-code #f (make-glil-void))))
627 ((<toplevel-ref> src name)
628 (emit-code src (make-glil-toplevel 'ref name))
630 ((drop) (emit-code #f (make-glil-call 'drop 1))))
633 ((<toplevel-set> src name exp)
635 (emit-code src (make-glil-toplevel 'set name))
638 (emit-code #f (make-glil-void))))
641 ((<toplevel-define> src name exp)
643 (emit-code src (make-glil-toplevel 'define name))
646 (emit-code #f (make-glil-void))))
650 (let ((free-locs (cdr (hashq-ref allocation x))))
653 (emit-code #f (flatten-lambda x #f allocation))
654 (if (not (null? free-locs))
659 ((,local? ,boxed? . ,n)
660 (emit-code #f (make-glil-lexical local? #f 'ref n)))
661 (else (error "bad lambda free var allocation" x loc))))
663 (emit-code #f (make-glil-call 'make-closure
664 (length free-locs))))))))
667 ((<lambda-case> src req opt rest kw inits gensyms alternate body)
668 ;; o/~ feature on top of feature o/~
670 ;; opt := (name ...) | #f
672 ;; kw: (allow-other-keys? (keyword name var) ...) | #f
673 ;; gensyms: (sym ...)
674 ;; init: tree-il in context of gensyms
675 ;; gensyms map to named arguments in the following order:
676 ;; required, optional (positional), rest, keyword.
677 (let* ((nreq (length req))
678 (nopt (if opt (length opt) 0))
679 (rest-idx (and rest (+ nreq nopt)))
680 (opt-names (or opt '()))
681 (allow-other-keys? (if kw (car kw) #f))
682 (kw-indices (map (lambda (x)
685 (cons key (list-index gensyms var)))
686 (else (error "bad kwarg" x))))
687 (if kw (cdr kw) '())))
688 (nargs (apply max (+ nreq nopt (if rest 1 0))
689 (map 1+ (map cdr kw-indices))))
690 (nlocs (cdr (hashq-ref allocation x)))
691 (alternate-label (and alternate (make-label))))
694 (+ nreq (length inits) (if rest 1 0)))
695 (error "lambda-case gensyms don't correspond to args"
696 req opt rest kw inits gensyms nreq nopt kw-indices nargs))
697 ;; the prelude, to check args & reset the stack pointer,
698 ;; allowing room for locals
703 (make-glil-kw-prelude nreq nopt rest-idx kw-indices
704 allow-other-keys? nlocs alternate-label))
706 (make-glil-opt-prelude nreq nopt rest-idx nlocs alternate-label))
708 (make-glil-std-prelude nreq nlocs alternate-label))))
709 ;; box args if necessary
712 (pmatch (hashq-ref (hashq-ref allocation v) self)
714 (emit-code #f (make-glil-lexical #t #f 'ref n))
715 (emit-code #f (make-glil-lexical #t #t 'box n)))))
717 ;; write bindings info
718 (if (not (null? gensyms))
721 (let lp ((kw (if kw (cdr kw) '()))
722 (names (append (reverse opt-names) (reverse req)))
723 (gensyms (list-tail gensyms (+ nreq nopt
727 ;; fixme: check that gensyms is empty
728 (reverse (if rest (cons rest names) names)))
729 (((,key ,name ,var) . ,kw)
730 (if (memq var gensyms)
731 (lp kw (cons name names) (delq var gensyms))
732 (lp kw names gensyms)))
733 (,kw (error "bad keywords, yo" kw))))
734 gensyms allocation self emit-code))
735 ;; init optional/kw args
736 (let lp ((inits inits) (n nreq) (gensyms (list-tail gensyms nreq)))
738 ((null? inits)) ; done
739 ((and rest-idx (= n rest-idx))
740 (lp inits (1+ n) (cdr gensyms)))
742 (pmatch (hashq-ref (hashq-ref allocation (car gensyms)) self)
743 ((#t ,boxed? . ,n*) (guard (= n* n))
744 (let ((L (make-label)))
745 (emit-code #f (make-glil-lexical #t boxed? 'bound? n))
746 (emit-code #f (make-glil-branch 'br-if L))
747 (comp-push (car inits))
748 (emit-code #f (make-glil-lexical #t boxed? 'set n))
750 (lp (cdr inits) (1+ n) (cdr gensyms))))
751 (#t (error "bad arg allocation" (car gensyms) inits))))))
752 ;; post-prelude case label for label calls
753 (emit-label (car (hashq-ref allocation x)))
755 (if (not (null? gensyms))
756 (emit-code #f (make-glil-unbind)))
759 (emit-label alternate-label)
760 (flatten-lambda-case alternate allocation self self-label
761 fix-labels emit-code)))))
763 ((<let> src names gensyms vals body)
764 (for-each comp-push vals)
765 (emit-bindings src names gensyms allocation self emit-code)
766 (for-each (lambda (v)
767 (pmatch (hashq-ref (hashq-ref allocation v) self)
769 (emit-code src (make-glil-lexical #t #f 'set n)))
771 (emit-code src (make-glil-lexical #t #t 'box n)))
772 (,loc (error "bad let var allocation" x loc))))
775 (clear-stack-slots context gensyms)
776 (emit-code #f (make-glil-unbind)))
778 ((<letrec> src in-order? names gensyms vals body)
779 ;; First prepare heap storage slots.
780 (for-each (lambda (v)
781 (pmatch (hashq-ref (hashq-ref allocation v) self)
783 (emit-code src (make-glil-lexical #t #t 'empty-box n)))
784 (,loc (error "bad letrec var allocation" x loc))))
786 ;; Even though the slots are empty, the bindings are valid.
787 (emit-bindings src names gensyms allocation self emit-code)
790 ;; For letrec*, bind values in order.
791 (for-each (lambda (name v val)
792 (pmatch (hashq-ref (hashq-ref allocation v) self)
795 (emit-code src (make-glil-lexical #t #t 'set n)))
796 (,loc (error "bad letrec var allocation" x loc))))
799 ;; But for letrec, eval all values, then bind.
800 (for-each comp-push vals)
801 (for-each (lambda (v)
802 (pmatch (hashq-ref (hashq-ref allocation v) self)
804 (emit-code src (make-glil-lexical #t #t 'set n)))
805 (,loc (error "bad letrec var allocation" x loc))))
808 (clear-stack-slots context gensyms)
809 (emit-code #f (make-glil-unbind)))
811 ((<fix> src names gensyms vals body)
812 ;; The ideal here is to just render the lambda bodies inline, and
813 ;; wire the code together with gotos. We can do that if
814 ;; analyze-lexicals has determined that a given var has "label"
815 ;; allocation -- which is the case if it is in `fix-labels'.
817 ;; But even for closures that we can't inline, we can do some
818 ;; tricks to avoid heap-allocation for the binding itself. Since
819 ;; we know the vals are lambdas, we can set them to their local
820 ;; var slots first, then capture their bindings, mutating them in
822 (let ((new-RA (if (or (eq? context 'tail) RA) #f (make-label))))
826 ((hashq-ref allocation x)
827 ;; allocating a closure
828 (emit-code #f (flatten-lambda x v allocation))
829 (let ((free-locs (cdr (hashq-ref allocation x))))
830 (if (not (null? free-locs))
831 ;; Need to make-closure first, so we have a fresh closure on
832 ;; the heap, but with a temporary free values.
834 (for-each (lambda (loc)
835 (emit-code #f (make-glil-const #f)))
837 (emit-code #f (make-glil-call 'make-closure
838 (length free-locs))))))
839 (pmatch (hashq-ref (hashq-ref allocation v) self)
841 (emit-code src (make-glil-lexical #t #f 'set n)))
842 (,loc (error "bad fix var allocation" x loc))))
844 ;; labels allocation: emit label & body, but jump over it
845 (let ((POST (make-label)))
846 (emit-branch #f 'br POST)
847 (let lp ((lcase (lambda-body x)))
850 ((<lambda-case> src req gensyms body alternate)
851 (emit-label (car (hashq-ref allocation lcase)))
852 ;; FIXME: opt & kw args in the bindings
853 (emit-bindings #f req gensyms allocation self emit-code)
855 (emit-code #f (make-glil-source src)))
856 (comp-fix body (or RA new-RA))
857 (emit-code #f (make-glil-unbind))
859 (emit-label POST)))))))
862 ;; Emit bindings metadata for closures
863 (let ((binds (let lp ((out '()) (gensyms gensyms) (names names))
864 (cond ((null? gensyms) (reverse! out))
865 ((assq (car gensyms) fix-labels)
866 (lp out (cdr gensyms) (cdr names)))
868 (lp (acons (car gensyms) (car names) out)
869 (cdr gensyms) (cdr names)))))))
870 (emit-bindings src (map cdr binds) (map car binds)
871 allocation self emit-code))
872 ;; Now go back and fix up the bindings for closures.
875 (let ((free-locs (if (hashq-ref allocation x)
876 (cdr (hashq-ref allocation x))
877 ;; can hit this latter case for labels allocation
879 (if (not (null? free-locs))
884 ((,local? ,boxed? . ,n)
885 (emit-code #f (make-glil-lexical local? #f 'ref n)))
886 (else (error "bad free var allocation" x loc))))
888 (pmatch (hashq-ref (hashq-ref allocation v) self)
890 (emit-code #f (make-glil-lexical #t #f 'fix n)))
891 (,loc (error "bad fix var allocation" x loc)))))))
897 (clear-stack-slots context gensyms)
898 (emit-code #f (make-glil-unbind))))
900 ((<let-values> src exp body)
902 ((<lambda-case> req opt kw rest gensyms body alternate)
903 (if (or opt kw alternate)
904 (error "unexpected lambda-case in let-values" x))
905 (let ((MV (make-label)))
907 (emit-code #f (make-glil-const 1))
909 (emit-code src (make-glil-mv-bind
911 (append req (if rest (list rest) '()))
912 gensyms allocation self)
914 (for-each (lambda (v)
915 (pmatch (hashq-ref (hashq-ref allocation v) self)
917 (emit-code src (make-glil-lexical #t #f 'set n)))
919 (emit-code src (make-glil-lexical #t #t 'box n)))
920 (,loc (error "bad let-values var allocation" x loc))))
923 (clear-stack-slots context gensyms)
924 (emit-code #f (make-glil-unbind))))))
926 ;; much trickier than i thought this would be, at first, due to the need
927 ;; to have body's return value(s) on the stack while the unwinder runs,
928 ;; then proceed with returning or dropping or what-have-you, interacting
929 ;; with RA and MVRA. What have you, I say.
930 ((<dynwind> src winder pre body post unwinder)
933 (null? (lambda-case-gensyms (lambda-body x)))))
934 (define (make-wrong-type-arg x)
935 (make-primcall src 'scm-error
937 (make-const #f 'wrong-type-arg)
938 (make-const #f "dynamic-wind")
939 (make-const #f "Wrong type (expecting thunk): ~S")
940 (make-primcall #f 'list (list x))
941 (make-primcall #f 'list (list x)))))
942 (define (emit-thunk-check x)
943 (comp-drop (make-conditional
945 (make-primcall src 'thunk? (list x))
947 (make-wrong-type-arg x))))
949 ;; We know at this point that `winder' and `unwinder' are
950 ;; constant expressions and can be duplicated.
951 (if (not (thunk? winder))
952 (emit-thunk-check winder))
954 (if (not (thunk? unwinder))
955 (emit-thunk-check unwinder))
958 (emit-code #f (make-glil-call 'wind 2))
962 (let ((MV (make-label)))
964 ;; one value: unwind...
965 (emit-code #f (make-glil-call 'unwind 0))
967 ;; ...and return the val
968 (emit-code #f (make-glil-call 'return 1))
971 ;; multiple values: unwind...
972 (emit-code #f (make-glil-call 'unwind 0))
974 ;; and return the values.
975 (emit-code #f (make-glil-call 'return/nvalues 1))))
978 ;; we only want one value. so ask for one value
980 ;; and unwind, leaving the val on the stack
981 (emit-code #f (make-glil-call 'unwind 0))
985 (let ((MV (make-label)))
987 ;; one value: push 1 and fall through to MV case
988 (emit-code #f (make-glil-const 1))
991 ;; multiple values: unwind...
992 (emit-code #f (make-glil-call 'unwind 0))
994 ;; and goto the MVRA.
995 (emit-branch #f 'br MVRA)))
998 ;; compile body, discarding values. then unwind...
1000 (emit-code #f (make-glil-call 'unwind 0))
1002 ;; and fall through, or goto RA if there is one.
1004 (emit-branch #f 'br RA)))))
1006 ((<dynlet> src fluids vals body)
1007 (for-each comp-push fluids)
1008 (for-each comp-push vals)
1009 (emit-code #f (make-glil-call 'wind-fluids (length fluids)))
1013 (let ((MV (make-label)))
1014 ;; NB: in tail case, it is possible to preserve asymptotic tail
1015 ;; recursion, via merging unwind-fluids structures -- but we'd need
1016 ;; to compile in the body twice (once in tail context, assuming the
1017 ;; caller unwinds, and once with this trampoline thing, unwinding
1020 ;; one value: unwind and return
1021 (emit-code #f (make-glil-call 'unwind-fluids 0))
1022 (emit-code #f (make-glil-call 'return 1))
1025 ;; multiple values: unwind and return values
1026 (emit-code #f (make-glil-call 'unwind-fluids 0))
1027 (emit-code #f (make-glil-call 'return/nvalues 1))))
1031 (emit-code #f (make-glil-call 'unwind-fluids 0)))
1034 (let ((MV (make-label)))
1036 ;; one value: push 1 and fall through to MV case
1037 (emit-code #f (make-glil-const 1))
1040 ;; multiple values: unwind and goto MVRA
1041 (emit-code #f (make-glil-call 'unwind-fluids 0))
1042 (emit-branch #f 'br MVRA)))
1045 ;; compile body, discarding values. then unwind...
1047 (emit-code #f (make-glil-call 'unwind-fluids 0))
1048 ;; and fall through, or goto RA if there is one.
1050 (emit-branch #f 'br RA)))))
1052 ((<dynref> src fluid)
1058 (emit-code #f (make-glil-call 'fluid-ref 1))))
1059 (maybe-emit-return))
1061 ((<dynset> src fluid exp)
1064 (emit-code #f (make-glil-call 'fluid-set 2))
1067 (emit-code #f (make-glil-void))))
1068 (maybe-emit-return))
1070 ;; What's the deal here? The deal is that we are compiling the start of a
1071 ;; delimited continuation. We try to avoid heap allocation in the normal
1072 ;; case; so the body is an expression, not a thunk, and we try to render
1073 ;; the handler inline. Also we did some analysis, in analyze.scm, so that
1074 ;; if the continuation isn't referenced, we don't reify it. This makes it
1075 ;; possible to implement catch and throw with delimited continuations,
1076 ;; without any overhead.
1077 ((<prompt> src tag body handler)
1078 (let ((H (make-label))
1080 (escape-only? (hashq-ref allocation x)))
1081 ;; First, set up the prompt.
1083 (emit-code src (make-glil-prompt H escape-only?))
1085 ;; Then we compile the body, with its normal return path, unwinding
1086 ;; before proceeding.
1089 (let ((MV (make-label)))
1091 ;; one value: unwind and return
1092 (emit-code #f (make-glil-call 'unwind 0))
1093 (emit-code #f (make-glil-call 'return 1))
1094 ;; multiple values: unwind and return
1096 (emit-code #f (make-glil-call 'unwind 0))
1097 (emit-code #f (make-glil-call 'return/nvalues 1))))
1100 ;; we only want one value. so ask for one value, unwind, and jump to
1103 (emit-code #f (make-glil-call 'unwind 0))
1104 (emit-branch #f 'br (or RA POST)))
1107 (let ((MV (make-label)))
1109 ;; one value: push 1 and fall through to MV case
1110 (emit-code #f (make-glil-const 1))
1111 ;; multiple values: unwind and goto MVRA
1113 (emit-code #f (make-glil-call 'unwind 0))
1114 (emit-branch #f 'br MVRA)))
1117 ;; compile body, discarding values, then unwind & fall through.
1119 (emit-code #f (make-glil-call 'unwind 0))
1120 (emit-branch #f 'br (or RA POST))))
1123 ;; Now the handler. The stack is now made up of the continuation, and
1124 ;; then the args to the continuation (pushed separately), and then the
1125 ;; number of args, including the continuation.
1126 (record-case handler
1127 ((<lambda-case> req opt kw rest gensyms body alternate)
1128 (if (or opt kw alternate)
1129 (error "unexpected lambda-case in prompt" x))
1130 (emit-code src (make-glil-mv-bind
1132 (append req (if rest (list rest) '()))
1133 gensyms allocation self)
1135 (for-each (lambda (v)
1136 (pmatch (hashq-ref (hashq-ref allocation v) self)
1138 (emit-code src (make-glil-lexical #t #f 'set n)))
1140 (emit-code src (make-glil-lexical #t #t 'box n)))
1142 (error "bad prompt handler arg allocation" x loc))))
1145 (emit-code #f (make-glil-unbind))))
1148 (or (eq? context 'push) (eq? context 'drop)))
1149 (emit-label POST))))
1151 ((<abort> src tag args tail)
1153 (for-each comp-push args)
1155 (emit-code src (make-glil-call 'abort (length args)))
1156 ;; so, the abort can actually return. if it does, the values will be on
1157 ;; the stack, then the MV marker, just as in an MV context.
1161 (emit-code #f (make-glil-call 'return/nvalues 1)))
1163 ;; Drop all values and goto RA, or otherwise fall through.
1164 (emit-code #f (make-glil-mv-bind 0 #f))
1165 (if RA (emit-branch #f 'br RA)))
1167 ;; Truncate to one value.
1168 (emit-code #f (make-glil-mv-bind 1 #f)))
1171 (emit-branch #f 'br MVRA)))))))