flesh out glil support for optional and keyword arguments
[bpt/guile.git] / module / language / tree-il / compile-glil.scm
1 ;;; TREE-IL -> GLIL compiler
2
3 ;; Copyright (C) 2001,2008,2009 Free Software Foundation, Inc.
4
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.
9 ;;;;
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.
14 ;;;;
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
18
19 ;;; Code:
20
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 analyze)
31 #:export (compile-glil))
32
33 ;; allocation:
34 ;; sym -> {lambda -> address}
35 ;; lambda -> (nlocs labels . free-locs)
36 ;;
37 ;; address := (local? boxed? . index)
38 ;; free-locs ::= ((sym0 . address0) (sym1 . address1) ...)
39 ;; free variable addresses are relative to parent proc.
40
41 (define *comp-module* (make-fluid))
42
43 (define %warning-passes
44 `((unused-variable . ,report-unused-variables)
45 (unbound-variable . ,report-possibly-unbound-variables)))
46
47 (define (compile-glil x e opts)
48 (define warnings
49 (or (and=> (memq #:warnings opts) cadr)
50 '()))
51
52 ;; Go through the warning passes.
53 (for-each (lambda (kind)
54 (let ((warn (assoc-ref %warning-passes kind)))
55 (and (procedure? warn)
56 (warn x e))))
57 warnings)
58
59 (let* ((x (make-lambda (tree-il-src x) '() '() '() x))
60 (x (optimize! x e opts))
61 (allocation (analyze-lexicals x)))
62
63 (with-fluid* *comp-module* e
64 (lambda ()
65 (values (flatten-lambda x #f allocation)
66 e
67 e)))))
68
69 \f
70
71 (define *primcall-ops* (make-hash-table))
72 (for-each
73 (lambda (x) (hash-set! *primcall-ops* (car x) (cdr x)))
74 '(((eq? . 2) . eq?)
75 ((eqv? . 2) . eqv?)
76 ((equal? . 2) . equal?)
77 ((= . 2) . ee?)
78 ((< . 2) . lt?)
79 ((> . 2) . gt?)
80 ((<= . 2) . le?)
81 ((>= . 2) . ge?)
82 ((+ . 2) . add)
83 ((- . 2) . sub)
84 ((1+ . 1) . add1)
85 ((1- . 1) . sub1)
86 ((* . 2) . mul)
87 ((/ . 2) . div)
88 ((quotient . 2) . quo)
89 ((remainder . 2) . rem)
90 ((modulo . 2) . mod)
91 ((not . 1) . not)
92 ((pair? . 1) . pair?)
93 ((cons . 2) . cons)
94 ((car . 1) . car)
95 ((cdr . 1) . cdr)
96 ((set-car! . 2) . set-car!)
97 ((set-cdr! . 2) . set-cdr!)
98 ((null? . 1) . null?)
99 ((list? . 1) . list?)
100 (list . list)
101 (vector . vector)
102 ((@slot-ref . 2) . slot-ref)
103 ((@slot-set! . 3) . slot-set)
104 ((vector-ref . 2) . vector-ref)
105 ((vector-set! . 3) . vector-set)
106
107 ((bytevector-u8-ref . 2) . bv-u8-ref)
108 ((bytevector-u8-set! . 3) . bv-u8-set)
109 ((bytevector-s8-ref . 2) . bv-s8-ref)
110 ((bytevector-s8-set! . 3) . bv-s8-set)
111
112 ((bytevector-u16-ref . 3) . bv-u16-ref)
113 ((bytevector-u16-set! . 4) . bv-u16-set)
114 ((bytevector-u16-native-ref . 2) . bv-u16-native-ref)
115 ((bytevector-u16-native-set! . 3) . bv-u16-native-set)
116 ((bytevector-s16-ref . 3) . bv-s16-ref)
117 ((bytevector-s16-set! . 4) . bv-s16-set)
118 ((bytevector-s16-native-ref . 2) . bv-s16-native-ref)
119 ((bytevector-s16-native-set! . 3) . bv-s16-native-set)
120
121 ((bytevector-u32-ref . 3) . bv-u32-ref)
122 ((bytevector-u32-set! . 4) . bv-u32-set)
123 ((bytevector-u32-native-ref . 2) . bv-u32-native-ref)
124 ((bytevector-u32-native-set! . 3) . bv-u32-native-set)
125 ((bytevector-s32-ref . 3) . bv-s32-ref)
126 ((bytevector-s32-set! . 4) . bv-s32-set)
127 ((bytevector-s32-native-ref . 2) . bv-s32-native-ref)
128 ((bytevector-s32-native-set! . 3) . bv-s32-native-set)
129
130 ((bytevector-u64-ref . 3) . bv-u64-ref)
131 ((bytevector-u64-set! . 4) . bv-u64-set)
132 ((bytevector-u64-native-ref . 2) . bv-u64-native-ref)
133 ((bytevector-u64-native-set! . 3) . bv-u64-native-set)
134 ((bytevector-s64-ref . 3) . bv-s64-ref)
135 ((bytevector-s64-set! . 4) . bv-s64-set)
136 ((bytevector-s64-native-ref . 2) . bv-s64-native-ref)
137 ((bytevector-s64-native-set! . 3) . bv-s64-native-set)
138
139 ((bytevector-ieee-single-ref . 3) . bv-f32-ref)
140 ((bytevector-ieee-single-set! . 4) . bv-f32-set)
141 ((bytevector-ieee-single-native-ref . 2) . bv-f32-native-ref)
142 ((bytevector-ieee-single-native-set! . 3) . bv-f32-native-set)
143 ((bytevector-ieee-double-ref . 3) . bv-f64-ref)
144 ((bytevector-ieee-double-set! . 4) . bv-f64-set)
145 ((bytevector-ieee-double-native-ref . 2) . bv-f64-native-ref)
146 ((bytevector-ieee-double-native-set! . 3) . bv-f64-native-set)))
147
148
149 \f
150
151 (define (make-label) (gensym ":L"))
152
153 (define (vars->bind-list ids vars allocation proc)
154 (map (lambda (id v)
155 (pmatch (hashq-ref (hashq-ref allocation v) proc)
156 ((#t ,boxed? . ,n)
157 (list id boxed? n))
158 (,x (error "badness" x))))
159 ids
160 vars))
161
162 (define (emit-bindings src ids vars allocation proc emit-code)
163 (emit-code src (make-glil-bind
164 (vars->bind-list ids vars allocation proc))))
165
166 (define (with-output-to-code proc)
167 (let ((out '()))
168 (define (emit-code src x)
169 (set! out (cons x out))
170 (if src
171 (set! out (cons (make-glil-source src) out))))
172 (proc emit-code)
173 (reverse out)))
174
175 (define (flatten-lambda x self-label allocation)
176 (receive (ids vars nargs nrest)
177 (let lp ((ids (lambda-names x)) (vars (lambda-vars x))
178 (oids '()) (ovars '()) (n 0))
179 (cond ((null? vars) (values (reverse oids) (reverse ovars) n 0))
180 ((pair? vars) (lp (cdr ids) (cdr vars)
181 (cons (car ids) oids) (cons (car vars) ovars)
182 (1+ n)))
183 (else (values (reverse (cons ids oids))
184 (reverse (cons vars ovars))
185 (1+ n) 1))))
186 (let ((nlocs (car (hashq-ref allocation x)))
187 (labels (cadr (hashq-ref allocation x))))
188 (make-glil-program
189 (lambda-meta x)
190 (with-output-to-code
191 (lambda (emit-code)
192 ;; write source info for proc
193 (if (lambda-src x)
194 (emit-code #f (make-glil-source (lambda-src x))))
195 ;; the prelude, to check args & reset the stack pointer,
196 ;; allowing room for locals
197 (if (zero? nrest)
198 (emit-code #f (make-glil-std-prelude nargs nlocs #f))
199 (emit-code #f (make-glil-opt-prelude (1- nargs) 0 #t nlocs #f)))
200 ;; write bindings info
201 (if (not (null? ids))
202 (emit-bindings #f ids vars allocation x emit-code))
203 ;; post-prelude label for self tail calls
204 (if self-label
205 (emit-code #f (make-glil-label self-label)))
206 ;; box args if necessary
207 (for-each
208 (lambda (v)
209 (pmatch (hashq-ref (hashq-ref allocation v) x)
210 ((#t #t . ,n)
211 (emit-code #f (make-glil-lexical #t #f 'ref n))
212 (emit-code #f (make-glil-lexical #t #t 'box n)))))
213 vars)
214 ;; and here, here, dear reader: we compile.
215 (flatten (lambda-body x) allocation x self-label
216 labels emit-code)))))))
217
218 (define (flatten x allocation self self-label fix-labels emit-code)
219 (define (emit-label label)
220 (emit-code #f (make-glil-label label)))
221 (define (emit-branch src inst label)
222 (emit-code src (make-glil-branch inst label)))
223
224 ;; RA: "return address"; #f unless we're in a non-tail fix with labels
225 ;; MVRA: "multiple-values return address"; #f unless we're in a let-values
226 (let comp ((x x) (context 'tail) (RA #f) (MVRA #f))
227 (define (comp-tail tree) (comp tree context RA MVRA))
228 (define (comp-push tree) (comp tree 'push #f #f))
229 (define (comp-drop tree) (comp tree 'drop #f #f))
230 (define (comp-vals tree MVRA) (comp tree 'vals #f MVRA))
231 (define (comp-fix tree RA) (comp tree context RA MVRA))
232
233 ;; A couple of helpers. Note that if we are in tail context, we
234 ;; won't have an RA.
235 (define (maybe-emit-return)
236 (if RA
237 (emit-branch #f 'br RA)
238 (if (eq? context 'tail)
239 (emit-code #f (make-glil-call 'return 1)))))
240
241 (record-case x
242 ((<void>)
243 (case context
244 ((push vals tail)
245 (emit-code #f (make-glil-void))))
246 (maybe-emit-return))
247
248 ((<const> src exp)
249 (case context
250 ((push vals tail)
251 (emit-code src (make-glil-const exp))))
252 (maybe-emit-return))
253
254 ;; FIXME: should represent sequence as exps tail
255 ((<sequence> exps)
256 (let lp ((exps exps))
257 (if (null? (cdr exps))
258 (comp-tail (car exps))
259 (begin
260 (comp-drop (car exps))
261 (lp (cdr exps))))))
262
263 ((<application> src proc args)
264 ;; FIXME: need a better pattern-matcher here
265 (cond
266 ((and (primitive-ref? proc)
267 (eq? (primitive-ref-name proc) '@apply)
268 (>= (length args) 1))
269 (let ((proc (car args))
270 (args (cdr args)))
271 (cond
272 ((and (primitive-ref? proc) (eq? (primitive-ref-name proc) 'values)
273 (not (eq? context 'push)) (not (eq? context 'vals)))
274 ;; tail: (lambda () (apply values '(1 2)))
275 ;; drop: (lambda () (apply values '(1 2)) 3)
276 ;; push: (lambda () (list (apply values '(10 12)) 1))
277 (case context
278 ((drop) (for-each comp-drop args) (maybe-emit-return))
279 ((tail)
280 (for-each comp-push args)
281 (emit-code src (make-glil-call 'return/values* (length args))))))
282
283 (else
284 (case context
285 ((tail)
286 (comp-push proc)
287 (for-each comp-push args)
288 (emit-code src (make-glil-call 'goto/apply (1+ (length args)))))
289 ((push)
290 (emit-code src (make-glil-call 'new-frame 0))
291 (comp-push proc)
292 (for-each comp-push args)
293 (emit-code src (make-glil-call 'apply (1+ (length args))))
294 (maybe-emit-return))
295 ((vals)
296 (comp-vals
297 (make-application src (make-primitive-ref #f 'apply)
298 (cons proc args))
299 MVRA)
300 (maybe-emit-return))
301 ((drop)
302 ;; Well, shit. The proc might return any number of
303 ;; values (including 0), since it's in a drop context,
304 ;; yet apply does not create a MV continuation. So we
305 ;; mv-call out to our trampoline instead.
306 (comp-drop
307 (make-application src (make-primitive-ref #f 'apply)
308 (cons proc args)))
309 (maybe-emit-return)))))))
310
311 ((and (primitive-ref? proc) (eq? (primitive-ref-name proc) 'values)
312 (not (eq? context 'push)))
313 ;; tail: (lambda () (values '(1 2)))
314 ;; drop: (lambda () (values '(1 2)) 3)
315 ;; push: (lambda () (list (values '(10 12)) 1))
316 ;; vals: (let-values (((a b ...) (values 1 2 ...))) ...)
317 (case context
318 ((drop) (for-each comp-drop args) (maybe-emit-return))
319 ((vals)
320 (for-each comp-push args)
321 (emit-code #f (make-glil-const (length args)))
322 (emit-branch src 'br MVRA))
323 ((tail)
324 (for-each comp-push args)
325 (emit-code src (make-glil-call 'return/values (length args))))))
326
327 ((and (primitive-ref? proc)
328 (eq? (primitive-ref-name proc) '@call-with-values)
329 (= (length args) 2))
330 ;; CONSUMER
331 ;; PRODUCER
332 ;; (mv-call MV)
333 ;; ([tail]-call 1)
334 ;; goto POST
335 ;; MV: [tail-]call/nargs
336 ;; POST: (maybe-drop)
337 (case context
338 ((vals)
339 ;; Fall back.
340 (comp-vals
341 (make-application src (make-primitive-ref #f 'call-with-values)
342 args)
343 MVRA)
344 (maybe-emit-return))
345 (else
346 (let ((MV (make-label)) (POST (make-label))
347 (producer (car args)) (consumer (cadr args)))
348 (if (not (eq? context 'tail))
349 (emit-code src (make-glil-call 'new-frame 0)))
350 (comp-push consumer)
351 (emit-code src (make-glil-call 'new-frame 0))
352 (comp-push producer)
353 (emit-code src (make-glil-mv-call 0 MV))
354 (case context
355 ((tail) (emit-code src (make-glil-call 'goto/args 1)))
356 (else (emit-code src (make-glil-call 'call 1))
357 (emit-branch #f 'br POST)))
358 (emit-label MV)
359 (case context
360 ((tail) (emit-code src (make-glil-call 'goto/nargs 0)))
361 (else (emit-code src (make-glil-call 'call/nargs 0))
362 (emit-label POST)
363 (if (eq? context 'drop)
364 (emit-code #f (make-glil-call 'drop 1)))
365 (maybe-emit-return)))))))
366
367 ((and (primitive-ref? proc)
368 (eq? (primitive-ref-name proc) '@call-with-current-continuation)
369 (= (length args) 1))
370 (case context
371 ((tail)
372 (comp-push (car args))
373 (emit-code src (make-glil-call 'goto/cc 1)))
374 ((vals)
375 (comp-vals
376 (make-application
377 src (make-primitive-ref #f 'call-with-current-continuation)
378 args)
379 MVRA)
380 (maybe-emit-return))
381 ((push)
382 (comp-push (car args))
383 (emit-code src (make-glil-call 'call/cc 1))
384 (maybe-emit-return))
385 ((drop)
386 ;; Crap. Just like `apply' in drop context.
387 (comp-drop
388 (make-application
389 src (make-primitive-ref #f 'call-with-current-continuation)
390 args))
391 (maybe-emit-return))))
392
393 ((and (primitive-ref? proc)
394 (or (hash-ref *primcall-ops*
395 (cons (primitive-ref-name proc) (length args)))
396 (hash-ref *primcall-ops* (primitive-ref-name proc))))
397 => (lambda (op)
398 (for-each comp-push args)
399 (emit-code src (make-glil-call op (length args)))
400 (case (instruction-pushes op)
401 ((0)
402 (case context
403 ((tail push vals) (emit-code #f (make-glil-void))))
404 (maybe-emit-return))
405 ((1)
406 (case context
407 ((drop) (emit-code #f (make-glil-call 'drop 1))))
408 (maybe-emit-return))
409 (else
410 (error "bad primitive op: too many pushes"
411 op (instruction-pushes op))))))
412
413 ;; da capo al fine
414 ((and (lexical-ref? proc)
415 self-label (eq? (lexical-ref-gensym proc) self-label)
416 ;; self-call in tail position is a goto
417 (eq? context 'tail)
418 ;; make sure the arity is right
419 (list? (lambda-vars self))
420 (= (length args) (length (lambda-vars self))))
421 ;; evaluate new values
422 (for-each comp-push args)
423 ;; rename & goto
424 (for-each (lambda (sym)
425 (pmatch (hashq-ref (hashq-ref allocation sym) self)
426 ((#t ,boxed? . ,index)
427 ;; set unboxed, as the proc prelude will box if needed
428 (emit-code #f (make-glil-lexical #t #f 'set index)))
429 (,x (error "what" x))))
430 (reverse (lambda-vars self)))
431 (emit-branch src 'br self-label))
432
433 ;; lambda, the ultimate goto
434 ((and (lexical-ref? proc)
435 (assq (lexical-ref-gensym proc) fix-labels))
436 ;; evaluate new values, assuming that analyze-lexicals did its
437 ;; job, and that the arity was right
438 (for-each comp-push args)
439 ;; rename
440 (for-each (lambda (sym)
441 (pmatch (hashq-ref (hashq-ref allocation sym) self)
442 ((#t #f . ,index)
443 (emit-code #f (make-glil-lexical #t #f 'set index)))
444 ((#t #t . ,index)
445 (emit-code #f (make-glil-lexical #t #t 'box index)))
446 (,x (error "what" x))))
447 (reverse (assq-ref fix-labels (lexical-ref-gensym proc))))
448 ;; goto!
449 (emit-branch src 'br (lexical-ref-gensym proc)))
450
451 (else
452 (if (not (eq? context 'tail))
453 (emit-code src (make-glil-call 'new-frame 0)))
454 (comp-push proc)
455 (for-each comp-push args)
456 (let ((len (length args)))
457 (case context
458 ((tail) (emit-code src (make-glil-call 'goto/args len)))
459 ((push) (emit-code src (make-glil-call 'call len))
460 (maybe-emit-return))
461 ((vals) (emit-code src (make-glil-mv-call len MVRA))
462 (maybe-emit-return))
463 ((drop) (let ((MV (make-label)) (POST (make-label)))
464 (emit-code src (make-glil-mv-call len MV))
465 (emit-code #f (make-glil-call 'drop 1))
466 (emit-branch #f 'br (or RA POST))
467 (emit-label MV)
468 (emit-code #f (make-glil-mv-bind '() #f))
469 (emit-code #f (make-glil-unbind))
470 (if RA
471 (emit-branch #f 'br RA)
472 (emit-label POST)))))))))
473
474 ((<conditional> src test then else)
475 ;; TEST
476 ;; (br-if-not L1)
477 ;; THEN
478 ;; (br L2)
479 ;; L1: ELSE
480 ;; L2:
481 (let ((L1 (make-label)) (L2 (make-label)))
482 (comp-push test)
483 (emit-branch src 'br-if-not L1)
484 (comp-tail then)
485 ;; if there is an RA, comp-tail will cause a jump to it -- just
486 ;; have to clean up here if there is no RA.
487 (if (and (not RA) (not (eq? context 'tail)))
488 (emit-branch #f 'br L2))
489 (emit-label L1)
490 (comp-tail else)
491 (if (and (not RA) (not (eq? context 'tail)))
492 (emit-label L2))))
493
494 ((<primitive-ref> src name)
495 (cond
496 ((eq? (module-variable (fluid-ref *comp-module*) name)
497 (module-variable the-root-module name))
498 (case context
499 ((tail push vals)
500 (emit-code src (make-glil-toplevel 'ref name))))
501 (maybe-emit-return))
502 ((module-variable the-root-module name)
503 (case context
504 ((tail push vals)
505 (emit-code src (make-glil-module 'ref '(guile) name #f))))
506 (maybe-emit-return))
507 (else
508 (case context
509 ((tail push vals)
510 (emit-code src (make-glil-module
511 'ref (module-name (fluid-ref *comp-module*)) name #f))))
512 (maybe-emit-return))))
513
514 ((<lexical-ref> src gensym)
515 (case context
516 ((push vals tail)
517 (pmatch (hashq-ref (hashq-ref allocation gensym) self)
518 ((,local? ,boxed? . ,index)
519 (emit-code src (make-glil-lexical local? boxed? 'ref index)))
520 (,loc
521 (error "badness" x loc)))))
522 (maybe-emit-return))
523
524 ((<lexical-set> src gensym exp)
525 (comp-push exp)
526 (pmatch (hashq-ref (hashq-ref allocation gensym) self)
527 ((,local? ,boxed? . ,index)
528 (emit-code src (make-glil-lexical local? boxed? 'set index)))
529 (,loc
530 (error "badness" x loc)))
531 (case context
532 ((tail push vals)
533 (emit-code #f (make-glil-void))))
534 (maybe-emit-return))
535
536 ((<module-ref> src mod name public?)
537 (emit-code src (make-glil-module 'ref mod name public?))
538 (case context
539 ((drop) (emit-code #f (make-glil-call 'drop 1))))
540 (maybe-emit-return))
541
542 ((<module-set> src mod name public? exp)
543 (comp-push exp)
544 (emit-code src (make-glil-module 'set mod name public?))
545 (case context
546 ((tail push vals)
547 (emit-code #f (make-glil-void))))
548 (maybe-emit-return))
549
550 ((<toplevel-ref> src name)
551 (emit-code src (make-glil-toplevel 'ref name))
552 (case context
553 ((drop) (emit-code #f (make-glil-call 'drop 1))))
554 (maybe-emit-return))
555
556 ((<toplevel-set> src name exp)
557 (comp-push exp)
558 (emit-code src (make-glil-toplevel 'set name))
559 (case context
560 ((tail push vals)
561 (emit-code #f (make-glil-void))))
562 (maybe-emit-return))
563
564 ((<toplevel-define> src name exp)
565 (comp-push exp)
566 (emit-code src (make-glil-toplevel 'define name))
567 (case context
568 ((tail push vals)
569 (emit-code #f (make-glil-void))))
570 (maybe-emit-return))
571
572 ((<lambda>)
573 (let ((free-locs (cddr (hashq-ref allocation x))))
574 (case context
575 ((push vals tail)
576 (emit-code #f (flatten-lambda x #f allocation))
577 (if (not (null? free-locs))
578 (begin
579 (for-each
580 (lambda (loc)
581 (pmatch loc
582 ((,local? ,boxed? . ,n)
583 (emit-code #f (make-glil-lexical local? #f 'ref n)))
584 (else (error "what" x loc))))
585 free-locs)
586 (emit-code #f (make-glil-call 'vector (length free-locs)))
587 (emit-code #f (make-glil-call 'make-closure 2)))))))
588 (maybe-emit-return))
589
590 ((<let> src names vars vals body)
591 (for-each comp-push vals)
592 (emit-bindings src names vars allocation self emit-code)
593 (for-each (lambda (v)
594 (pmatch (hashq-ref (hashq-ref allocation v) self)
595 ((#t #f . ,n)
596 (emit-code src (make-glil-lexical #t #f 'set n)))
597 ((#t #t . ,n)
598 (emit-code src (make-glil-lexical #t #t 'box n)))
599 (,loc (error "badness" x loc))))
600 (reverse vars))
601 (comp-tail body)
602 (emit-code #f (make-glil-unbind)))
603
604 ((<letrec> src names vars vals body)
605 (for-each (lambda (v)
606 (pmatch (hashq-ref (hashq-ref allocation v) self)
607 ((#t #t . ,n)
608 (emit-code src (make-glil-lexical #t #t 'empty-box n)))
609 (,loc (error "badness" x loc))))
610 vars)
611 (for-each comp-push vals)
612 (emit-bindings src names vars allocation self emit-code)
613 (for-each (lambda (v)
614 (pmatch (hashq-ref (hashq-ref allocation v) self)
615 ((#t #t . ,n)
616 (emit-code src (make-glil-lexical #t #t 'set n)))
617 (,loc (error "badness" x loc))))
618 (reverse vars))
619 (comp-tail body)
620 (emit-code #f (make-glil-unbind)))
621
622 ((<fix> src names vars vals body)
623 ;; The ideal here is to just render the lambda bodies inline, and
624 ;; wire the code together with gotos. We can do that if
625 ;; analyze-lexicals has determined that a given var has "label"
626 ;; allocation -- which is the case if it is in `fix-labels'.
627 ;;
628 ;; But even for closures that we can't inline, we can do some
629 ;; tricks to avoid heap-allocation for the binding itself. Since
630 ;; we know the vals are lambdas, we can set them to their local
631 ;; var slots first, then capture their bindings, mutating them in
632 ;; place.
633 (let ((new-RA (if (or (eq? context 'tail) RA) #f (make-label))))
634 (for-each
635 (lambda (x v)
636 (cond
637 ((hashq-ref allocation x)
638 ;; allocating a closure
639 (emit-code #f (flatten-lambda x v allocation))
640 (if (not (null? (cddr (hashq-ref allocation x))))
641 ;; Need to make-closure first, but with a temporary #f
642 ;; free-variables vector, so we are mutating fresh
643 ;; closures on the heap.
644 (begin
645 (emit-code #f (make-glil-const #f))
646 (emit-code #f (make-glil-call 'make-closure 2))))
647 (pmatch (hashq-ref (hashq-ref allocation v) self)
648 ((#t #f . ,n)
649 (emit-code src (make-glil-lexical #t #f 'set n)))
650 (,loc (error "badness" x loc))))
651 (else
652 ;; labels allocation: emit label & body, but jump over it
653 (let ((POST (make-label)))
654 (emit-branch #f 'br POST)
655 (emit-label v)
656 ;; we know the lambda vars are a list
657 (emit-bindings #f (lambda-names x) (lambda-vars x)
658 allocation self emit-code)
659 (if (lambda-src x)
660 (emit-code #f (make-glil-source (lambda-src x))))
661 (comp-fix (lambda-body x) (or RA new-RA))
662 (emit-code #f (make-glil-unbind))
663 (emit-label POST)))))
664 vals
665 vars)
666 ;; Emit bindings metadata for closures
667 (let ((binds (let lp ((out '()) (vars vars) (names names))
668 (cond ((null? vars) (reverse! out))
669 ((assq (car vars) fix-labels)
670 (lp out (cdr vars) (cdr names)))
671 (else
672 (lp (acons (car vars) (car names) out)
673 (cdr vars) (cdr names)))))))
674 (emit-bindings src (map cdr binds) (map car binds)
675 allocation self emit-code))
676 ;; Now go back and fix up the bindings for closures.
677 (for-each
678 (lambda (x v)
679 (let ((free-locs (if (hashq-ref allocation x)
680 (cddr (hashq-ref allocation x))
681 ;; can hit this latter case for labels allocation
682 '())))
683 (if (not (null? free-locs))
684 (begin
685 (for-each
686 (lambda (loc)
687 (pmatch loc
688 ((,local? ,boxed? . ,n)
689 (emit-code #f (make-glil-lexical local? #f 'ref n)))
690 (else (error "what" x loc))))
691 free-locs)
692 (emit-code #f (make-glil-call 'vector (length free-locs)))
693 (pmatch (hashq-ref (hashq-ref allocation v) self)
694 ((#t #f . ,n)
695 (emit-code #f (make-glil-lexical #t #f 'fix n)))
696 (,loc (error "badness" x loc)))))))
697 vals
698 vars)
699 (comp-tail body)
700 (if new-RA
701 (emit-label new-RA))
702 (emit-code #f (make-glil-unbind))))
703
704 ((<let-values> src names vars exp body)
705 (let lp ((names '()) (vars '()) (inames names) (ivars vars) (rest? #f))
706 (cond
707 ((pair? inames)
708 (lp (cons (car inames) names) (cons (car ivars) vars)
709 (cdr inames) (cdr ivars) #f))
710 ((not (null? inames))
711 (lp (cons inames names) (cons ivars vars) '() '() #t))
712 (else
713 (let ((names (reverse! names))
714 (vars (reverse! vars))
715 (MV (make-label)))
716 (comp-vals exp MV)
717 (emit-code #f (make-glil-const 1))
718 (emit-label MV)
719 (emit-code src (make-glil-mv-bind
720 (vars->bind-list names vars allocation self)
721 rest?))
722 (for-each (lambda (v)
723 (pmatch (hashq-ref (hashq-ref allocation v) self)
724 ((#t #f . ,n)
725 (emit-code src (make-glil-lexical #t #f 'set n)))
726 ((#t #t . ,n)
727 (emit-code src (make-glil-lexical #t #t 'box n)))
728 (,loc (error "badness" x loc))))
729 (reverse vars))
730 (comp-tail body)
731 (emit-code #f (make-glil-unbind))))))))))