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