1 ;;; TREE-IL -> GLIL compiler
3 ;; Copyright (C) 2001,2008,2009 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 (ice-9 receive)
24 #:use-module (language glil)
25 #:use-module (system vm instruction)
26 #:use-module (language tree-il)
27 #:use-module (language tree-il optimize)
28 #:use-module (language tree-il analyze)
29 #:export (compile-glil))
33 ;; call-with-values -> mv-bind
34 ;; basic degenerate-case reduction
37 ;; sym -> (local . index) | (heap level . index)
38 ;; lambda -> (nlocs . nexts)
40 (define *comp-module* (make-fluid))
42 (define (compile-glil x e opts)
43 (let* ((x (make-lambda (tree-il-src x) '() '() '() x))
44 (x (optimize! x e opts))
45 (allocation (analyze-lexicals x)))
46 (with-fluid* *comp-module* (or (and e (car e)) (current-module))
48 (values (flatten-lambda x -1 allocation)
49 (and e (cons (car e) (cddr e)))
54 (define *primcall-ops* (make-hash-table))
56 (lambda (x) (hash-set! *primcall-ops* (car x) (cdr x)))
59 ((equal? . 2) . equal?)
69 ((quotient . 2) . quo)
70 ((remainder . 2) . rem)
77 ((set-car! . 2) . set-car!)
78 ((set-cdr! . 2) . set-cdr!)
83 ((@slot-ref . 2) . slot-ref)
84 ((@slot-set! . 3) . slot-set)
85 ((vector-ref . 2) . vector-ref)
86 ((vector-set! . 3) . vector-set)
88 ((bytevector-u8-ref . 2) . bv-u8-ref)
89 ((bytevector-u8-set! . 3) . bv-u8-set)
90 ((bytevector-s8-ref . 2) . bv-s8-ref)
91 ((bytevector-s8-set! . 3) . bv-s8-set)
93 ((bytevector-u16-ref . 3) . bv-u16-ref)
94 ((bytevector-u16-set! . 4) . bv-u16-set)
95 ((bytevector-u16-native-ref . 2) . bv-u16-native-ref)
96 ((bytevector-u16-native-set! . 3) . bv-u16-native-set)
97 ((bytevector-s16-ref . 3) . bv-s16-ref)
98 ((bytevector-s16-set! . 4) . bv-s16-set)
99 ((bytevector-s16-native-ref . 2) . bv-s16-native-ref)
100 ((bytevector-s16-native-set! . 3) . bv-s16-native-set)
102 ((bytevector-u32-ref . 3) . bv-u32-ref)
103 ((bytevector-u32-set! . 4) . bv-u32-set)
104 ((bytevector-u32-native-ref . 2) . bv-u32-native-ref)
105 ((bytevector-u32-native-set! . 3) . bv-u32-native-set)
106 ((bytevector-s32-ref . 3) . bv-s32-ref)
107 ((bytevector-s32-set! . 4) . bv-s32-set)
108 ((bytevector-s32-native-ref . 2) . bv-s32-native-ref)
109 ((bytevector-s32-native-set! . 3) . bv-s32-native-set)
111 ((bytevector-u64-ref . 3) . bv-u64-ref)
112 ((bytevector-u64-set! . 4) . bv-u64-set)
113 ((bytevector-u64-native-ref . 2) . bv-u64-native-ref)
114 ((bytevector-u64-native-set! . 3) . bv-u64-native-set)
115 ((bytevector-s64-ref . 3) . bv-s64-ref)
116 ((bytevector-s64-set! . 4) . bv-s64-set)
117 ((bytevector-s64-native-ref . 2) . bv-s64-native-ref)
118 ((bytevector-s64-native-set! . 3) . bv-s64-native-set)
120 ((bytevector-ieee-single-ref . 3) . bv-f32-ref)
121 ((bytevector-ieee-single-set! . 4) . bv-f32-set)
122 ((bytevector-ieee-single-native-ref . 2) . bv-f32-native-ref)
123 ((bytevector-ieee-single-native-set! . 3) . bv-f32-native-set)
124 ((bytevector-ieee-double-ref . 3) . bv-f64-ref)
125 ((bytevector-ieee-double-set! . 4) . bv-f64-set)
126 ((bytevector-ieee-double-native-ref . 2) . bv-f64-native-ref)
127 ((bytevector-ieee-double-native-set! . 3) . bv-f64-native-set)))
132 (define (make-label) (gensym ":L"))
134 (define (vars->bind-list ids vars allocation)
136 (let ((loc (hashq-ref allocation v)))
138 ((stack) (list id 'local (cdr loc)))
139 ((heap) (list id 'external (cddr loc)))
140 (else (error "badness" id v loc)))))
144 (define (emit-bindings src ids vars allocation emit-code)
146 (emit-code src (make-glil-bind
147 (vars->bind-list ids vars allocation)))))
149 (define (with-output-to-code proc)
151 (define (emit-code src x)
152 (set! out (cons x out))
154 (set! out (cons (make-glil-source src) out))))
158 (define (flatten-lambda x level allocation)
159 (receive (ids vars nargs nrest)
160 (let lp ((ids (lambda-names x)) (vars (lambda-vars x))
161 (oids '()) (ovars '()) (n 0))
162 (cond ((null? vars) (values (reverse oids) (reverse ovars) n 0))
163 ((pair? vars) (lp (cdr ids) (cdr vars)
164 (cons (car ids) oids) (cons (car vars) ovars)
166 (else (values (reverse (cons ids oids))
167 (reverse (cons vars ovars))
169 (let ((nlocs (car (hashq-ref allocation x)))
170 (nexts (cdr (hashq-ref allocation x))))
172 nargs nrest nlocs nexts (lambda-meta x)
175 ;; write bindings and source debugging info
176 (emit-bindings #f ids vars allocation emit-code)
178 (emit-code #f (make-glil-source (lambda-src x))))
180 ;; copy args to the heap if necessary
181 (let lp ((in vars) (n 0))
183 (let ((loc (hashq-ref allocation (car in))))
186 (emit-code #f (make-glil-local 'ref n))
187 (emit-code #f (make-glil-external 'set 0 (cddr loc)))))
188 (lp (cdr in) (1+ n)))))
190 ;; and here, here, dear reader: we compile.
191 (flatten (lambda-body x) (1+ level) allocation emit-code)))))))
193 (define (flatten x level allocation emit-code)
194 (define (emit-label label)
195 (emit-code #f (make-glil-label label)))
196 (define (emit-branch src inst label)
197 (emit-code src (make-glil-branch inst label)))
199 ;; LMVRA == "let-values MV return address"
200 (let comp ((x x) (context 'tail) (LMVRA #f))
201 (define (comp-tail tree) (comp tree context LMVRA))
202 (define (comp-push tree) (comp tree 'push #f))
203 (define (comp-drop tree) (comp tree 'drop #f))
204 (define (comp-vals tree LMVRA) (comp tree 'vals LMVRA))
209 ((push vals) (emit-code #f (make-glil-void)))
211 (emit-code #f (make-glil-void))
212 (emit-code #f (make-glil-call 'return 1)))))
216 ((push vals) (emit-code src (make-glil-const exp)))
218 (emit-code src (make-glil-const exp))
219 (emit-code #f (make-glil-call 'return 1)))))
221 ;; FIXME: should represent sequence as exps tail
222 ((<sequence> src exps)
223 (let lp ((exps exps))
224 (if (null? (cdr exps))
225 (comp-tail (car exps))
227 (comp-drop (car exps))
230 ((<application> src proc args)
231 ;; FIXME: need a better pattern-matcher here
233 ((and (primitive-ref? proc)
234 (eq? (primitive-ref-name proc) '@apply)
235 (>= (length args) 1))
236 (let ((proc (car args))
239 ((and (primitive-ref? proc) (eq? (primitive-ref-name proc) 'values)
240 (not (eq? context 'push)) (not (eq? context 'vals)))
241 ;; tail: (lambda () (apply values '(1 2)))
242 ;; drop: (lambda () (apply values '(1 2)) 3)
243 ;; push: (lambda () (list (apply values '(10 12)) 1))
245 ((drop) (for-each comp-drop args))
247 (for-each comp-push args)
248 (emit-code src (make-glil-call 'return/values* (length args))))))
254 (for-each comp-push args)
255 (emit-code src (make-glil-call 'goto/apply (1+ (length args)))))
258 (for-each comp-push args)
259 (emit-code src (make-glil-call 'apply (1+ (length args)))))
262 (make-application src (make-primitive-ref #f 'apply)
266 ;; Well, shit. The proc might return any number of
267 ;; values (including 0), since it's in a drop context,
268 ;; yet apply does not create a MV continuation. So we
269 ;; mv-call out to our trampoline instead.
271 (make-application src (make-primitive-ref #f 'apply)
272 (cons proc args)))))))))
274 ((and (primitive-ref? proc) (eq? (primitive-ref-name proc) 'values)
275 (not (eq? context 'push)))
276 ;; tail: (lambda () (values '(1 2)))
277 ;; drop: (lambda () (values '(1 2)) 3)
278 ;; push: (lambda () (list (values '(10 12)) 1))
279 ;; vals: (let-values (((a b ...) (values 1 2 ...))) ...)
281 ((drop) (for-each comp-drop args))
283 (for-each comp-push args)
284 (emit-code #f (make-glil-const (length args)))
285 (emit-branch src 'br LMVRA))
287 (for-each comp-push args)
288 (emit-code src (make-glil-call 'return/values (length args))))))
290 ((and (primitive-ref? proc)
291 (eq? (primitive-ref-name proc) '@call-with-values)
298 ;; MV: [tail-]call/nargs
299 ;; POST: (maybe-drop)
304 (make-application src (make-primitive-ref #f 'call-with-values)
308 (let ((MV (make-label)) (POST (make-label))
309 (producer (car args)) (consumer (cadr args)))
312 (emit-code src (make-glil-mv-call 0 MV))
314 ((tail) (emit-code src (make-glil-call 'goto/args 1)))
315 (else (emit-code src (make-glil-call 'call 1))
316 (emit-branch #f 'br POST)))
319 ((tail) (emit-code src (make-glil-call 'goto/nargs 0)))
320 (else (emit-code src (make-glil-call 'call/nargs 0))
322 (if (eq? context 'drop)
323 (emit-code #f (make-glil-call 'drop 1)))))))))
325 ((and (primitive-ref? proc)
326 (eq? (primitive-ref-name proc) '@call-with-current-continuation)
330 (comp-push (car args))
331 (emit-code src (make-glil-call 'goto/cc 1)))
335 src (make-primitive-ref #f 'call-with-current-continuation)
339 (comp-push (car args))
340 (emit-code src (make-glil-call 'call/cc 1)))
342 ;; Crap. Just like `apply' in drop context.
345 src (make-primitive-ref #f 'call-with-current-continuation)
348 ((and (primitive-ref? proc)
349 (or (hash-ref *primcall-ops*
350 (cons (primitive-ref-name proc) (length args)))
351 (hash-ref *primcall-ops* (primitive-ref-name proc))))
353 (for-each comp-push args)
354 (emit-code src (make-glil-call op (length args)))
355 (case (instruction-pushes op)
358 ((tail) (emit-code #f (make-glil-void))
359 (emit-code #f (make-glil-call 'return 1)))
360 ((push vals) (emit-code #f (make-glil-void)))))
363 ((tail) (emit-code #f (make-glil-call 'return 1)))
364 ((drop) (emit-code #f (make-glil-call 'drop 1)))))
366 (error "bad primitive op: too many pushes"
367 op (instruction-pushes op))))))
371 (for-each comp-push args)
372 (let ((len (length args)))
374 ((tail) (emit-code src (make-glil-call 'goto/args len)))
375 ((push) (emit-code src (make-glil-call 'call len)))
376 ((vals) (emit-code src (make-glil-call 'mv-call len LMVRA)))
378 (let ((MV (make-label)) (POST (make-label)))
379 (emit-code src (make-glil-mv-call len MV))
380 (emit-code #f (make-glil-call 'drop 1))
381 (emit-branch #f 'br POST)
383 (emit-code #f (make-glil-mv-bind '() #f))
384 (emit-code #f (make-glil-unbind))
385 (emit-label POST))))))))
387 ((<conditional> src test then else)
394 (let ((L1 (make-label)) (L2 (make-label)))
396 (emit-branch src 'br-if-not L1)
398 (if (not (eq? context 'tail))
399 (emit-branch #f 'br L2))
402 (if (not (eq? context 'tail))
405 ((<primitive-ref> src name)
407 ((eq? (module-variable (fluid-ref *comp-module*) name)
408 (module-variable the-root-module name))
411 (emit-code src (make-glil-toplevel 'ref name)))
413 (emit-code src (make-glil-toplevel 'ref name))
414 (emit-code #f (make-glil-call 'return 1)))))
416 (pk 'ew-the-badness x (current-module) (fluid-ref *comp-module*))
419 (emit-code src (make-glil-module 'ref '(guile) name #f)))
421 (emit-code src (make-glil-module 'ref '(guile) name #f))
422 (emit-code #f (make-glil-call 'return 1)))))))
424 ((<lexical-ref> src name gensym)
427 (let ((loc (hashq-ref allocation gensym)))
430 (emit-code src (make-glil-local 'ref (cdr loc))))
432 (emit-code src (make-glil-external
433 'ref (- level (cadr loc)) (cddr loc))))
434 (else (error "badness" x loc)))
435 (if (eq? context 'tail)
436 (emit-code #f (make-glil-call 'return 1)))))))
438 ((<lexical-set> src name gensym exp)
440 (let ((loc (hashq-ref allocation gensym)))
443 (emit-code src (make-glil-local 'set (cdr loc))))
445 (emit-code src (make-glil-external
446 'set (- level (cadr loc)) (cddr loc))))
447 (else (error "badness" x loc))))
450 (emit-code #f (make-glil-void)))
452 (emit-code #f (make-glil-void))
453 (emit-code #f (make-glil-call 'return 1)))))
455 ((<module-ref> src mod name public?)
456 (emit-code src (make-glil-module 'ref mod name public?))
458 ((drop) (emit-code #f (make-glil-call 'drop 1)))
459 ((tail) (emit-code #f (make-glil-call 'return 1)))))
461 ((<module-set> src mod name public? exp)
463 (emit-code src (make-glil-module 'set mod name public?))
466 (emit-code #f (make-glil-void)))
468 (emit-code #f (make-glil-void))
469 (emit-code #f (make-glil-call 'return 1)))))
471 ((<toplevel-ref> src name)
472 (emit-code src (make-glil-toplevel 'ref name))
474 ((drop) (emit-code #f (make-glil-call 'drop 1)))
475 ((tail) (emit-code #f (make-glil-call 'return 1)))))
477 ((<toplevel-set> src name exp)
479 (emit-code src (make-glil-toplevel 'set name))
482 (emit-code #f (make-glil-void)))
484 (emit-code #f (make-glil-void))
485 (emit-code #f (make-glil-call 'return 1)))))
487 ((<toplevel-define> src name exp)
489 (emit-code src (make-glil-toplevel 'define name))
492 (emit-code #f (make-glil-void)))
494 (emit-code #f (make-glil-void))
495 (emit-code #f (make-glil-call 'return 1)))))
500 (emit-code #f (flatten-lambda x level allocation)))
502 (emit-code #f (flatten-lambda x level allocation))
503 (emit-code #f (make-glil-call 'return 1)))))
505 ((<let> src names vars vals body)
506 (for-each comp-push vals)
507 (emit-bindings src names vars allocation emit-code)
508 (for-each (lambda (v)
509 (let ((loc (hashq-ref allocation v)))
512 (emit-code src (make-glil-local 'set (cdr loc))))
514 (emit-code src (make-glil-external 'set 0 (cddr loc))))
515 (else (error "badness" x loc)))))
518 (emit-code #f (make-glil-unbind)))
520 ((<letrec> src names vars vals body)
521 (for-each comp-push vals)
522 (emit-bindings src names vars allocation emit-code)
523 (for-each (lambda (v)
524 (let ((loc (hashq-ref allocation v)))
527 (emit-code src (make-glil-local 'set (cdr loc))))
529 (emit-code src (make-glil-external 'set 0 (cddr loc))))
530 (else (error "badness" x loc)))))
533 (emit-code #f (make-glil-unbind)))
535 ((<let-values> src names vars exp body)
536 (let lp ((names '()) (vars '()) (inames names) (ivars vars) (rest? #f))
539 (lp (cons (car inames) names) (cons (car ivars) vars)
540 (cdr inames) (cdr ivars) #f))
541 ((not (null? inames))
542 (lp (cons inames names) (cons ivars vars) '() '() #t))
544 (let ((names (reverse! names))
545 (vars (reverse! vars))
548 (emit-code #f (make-glil-const 1))
550 (emit-code src (make-glil-mv-bind
551 (vars->bind-list names vars allocation)
553 (for-each (lambda (v)
554 (let ((loc (hashq-ref allocation v)))
557 (emit-code src (make-glil-local 'set (cdr loc))))
559 (emit-code src (make-glil-external 'set 0 (cddr loc))))
560 (else (error "badness" x loc)))))
563 (emit-code #f (make-glil-unbind))))))))))