1 ;;; GHIL -> GLIL compiler
3 ;; Copyright (C) 2001 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 ghil compile-glil)
22 #:use-module (system base syntax)
23 #:use-module (language glil)
24 #:use-module (language ghil)
25 #:use-module (ice-9 common-list)
26 #:export (compile-glil))
28 (define (compile-glil x e opts)
29 (if (memq #:O opts) (set! x (optimize x)))
31 (and e (cons (car e) (cddr e)))
36 ;;; Stage 2: Optimization
39 (define (lift-variables! env)
40 (let ((parent-env (ghil-env-parent env)))
42 (case (ghil-var-kind v)
43 ((argument) (set! (ghil-var-kind v) 'local)))
44 (set! (ghil-var-env v) parent-env)
45 (ghil-env-add! parent-env v))
46 (ghil-env-variables env))))
48 ;; The premise of this, unused, approach to optimization is that you can
49 ;; determine the environment of a variable lexically, because they have
50 ;; been alpha-renamed. It makes the transformations *much* easier.
51 ;; Unfortunately it doesn't work yet.
53 (transform-record (<ghil> env loc) x
55 (define (optimize-qq x)
56 (cond ((list? x) (map optimize-qq x))
57 ((pair? x) (cons (optimize-qq (car x)) (optimize-qq (cdr x))))
58 ((record? x) (optimize x))
60 (-> (quasiquote (optimize-qq x))))
63 (-> (unquote (optimize exp))))
65 ((unquote-splicing exp)
66 (-> (unquote-splicing (optimize exp))))
69 (-> (set var (optimize val))))
72 (-> (define var (optimize val))))
75 (-> (if (optimize test) (optimize then) (optimize else))))
78 (-> (and (map optimize exps))))
81 (-> (or (map optimize exps))))
84 (-> (begin (map optimize exps))))
86 ((bind vars vals body)
87 (-> (bind vars (map optimize vals) (optimize body))))
89 ((mv-bind producer vars rest body)
90 (-> (mv-bind (optimize producer) vars rest (optimize body))))
93 (-> (inline inst (map optimize args))))
95 ((call (proc (lambda vars (rest #f) meta body)) args)
96 (-> (bind vars (optimize args) (optimize body))))
99 (-> (call (optimize proc) (map optimize args))))
101 ((lambda vars rest meta body)
102 (-> (lambda vars rest meta (optimize body))))
104 ((mv-call producer (consumer (lambda vars rest meta body)))
105 (-> (mv-bind (optimize producer) vars rest (optimize body))))
107 ((mv-call producer consumer)
108 (-> (mv-call (optimize producer) (optimize consumer))))
111 (-> (values (map optimize values))))
114 (-> (values* (map optimize values))))
117 (error "unrecognized GHIL" x))))
121 ((<ghil-set> env loc var val)
122 (make-ghil-set env var (optimize val)))
124 ((<ghil-define> env loc var val)
125 (make-ghil-define env var (optimize val)))
127 ((<ghil-if> env loc test then else)
128 (make-ghil-if env loc (optimize test) (optimize then) (optimize else)))
130 ((<ghil-and> env loc exps)
131 (make-ghil-and env loc (map optimize exps)))
133 ((<ghil-or> env loc exps)
134 (make-ghil-or env loc (map optimize exps)))
136 ((<ghil-begin> env loc exps)
137 (make-ghil-begin env loc (map optimize exps)))
139 ((<ghil-bind> env loc vars vals body)
140 (make-ghil-bind env loc vars (map optimize vals) (optimize body)))
142 ((<ghil-lambda> env loc vars rest meta body)
143 (make-ghil-lambda env loc vars rest meta (optimize body)))
145 ((<ghil-inline> env loc instruction args)
146 (make-ghil-inline env loc instruction (map optimize args)))
148 ((<ghil-call> env loc proc args)
149 (let ((parent-env env))
151 ;; ((@lambda (VAR...) BODY...) ARG...) =>
152 ;; (@let ((VAR ARG) ...) BODY...)
153 ((<ghil-lambda> env loc vars rest meta body)
156 (lift-variables! env)
157 (make-ghil-bind parent-env loc (map optimize args)))
159 (make-ghil-call parent-env loc (optimize proc) (map optimize args)))))
161 (make-ghil-call parent-env loc (optimize proc) (map optimize args))))))
163 ((<ghil-mv-call> env loc producer consumer)
164 (record-case consumer
165 ;; (mv-call PRODUCER (lambda ARGS BODY...)) =>
166 ;; (mv-let PRODUCER ARGS BODY...)
167 ((<ghil-lambda> env loc vars rest meta body)
168 (lift-variables! env)
169 (make-ghil-mv-bind producer vars rest body))
171 (make-ghil-mv-call env loc (optimize producer) (optimize consumer)))))
177 ;;; Stage 3: Code generation
180 (define *ia-void* (make-glil-void))
181 (define *ia-drop* (make-glil-call 'drop 1))
182 (define *ia-return* (make-glil-call 'return 1))
184 (define (make-label) (gensym ":L"))
186 (define (make-glil-var op env var)
187 (case (ghil-var-kind var)
189 (make-glil-local op (ghil-var-index var)))
191 (make-glil-local op (ghil-var-index var)))
193 (do ((depth 0 (1+ depth))
194 (e env (ghil-env-parent e)))
195 ((eq? e (ghil-var-env var))
196 (make-glil-external op depth (ghil-var-index var)))))
198 (make-glil-toplevel op (ghil-var-name var)))
200 (make-glil-module op (ghil-var-env var) (ghil-var-name var)
201 (eq? (ghil-var-kind var) 'public)))
202 (else (error "Unknown kind of variable:" var))))
204 (define (constant? x)
205 (cond ((or (number? x) (string? x) (symbol? x) (keyword? x) (boolean? x)) #t)
206 ((pair? x) (and (constant? (car x))
207 (constant? (cdr x))))
208 ((vector? x) (let lp ((i (vector-length x)))
210 (and (constant? (vector-ref x (1- i)))
213 (define (codegen ghil)
215 (define (push-code! loc code)
216 (set! stack (cons code stack))
217 (if loc (set! stack (cons (make-glil-source loc) stack))))
218 (define (var->binding var)
219 (list (ghil-var-name var) (let ((kind (ghil-var-kind var)))
220 (case kind ((argument) 'local) (else kind)))
221 (ghil-var-index var)))
222 (define (push-bindings! loc vars)
223 (if (not (null? vars))
224 (push-code! loc (make-glil-bind (map var->binding vars)))))
225 (define (comp tree tail drop)
226 (define (push-label! label)
227 (push-code! #f (make-glil-label label)))
228 (define (push-branch! loc inst label)
229 (push-code! loc (make-glil-branch inst label)))
230 (define (push-call! loc inst args)
231 (for-each comp-push args)
232 (push-code! loc (make-glil-call inst (length args))))
233 ;; possible tail position
234 (define (comp-tail tree) (comp tree tail drop))
236 (define (comp-push tree) (comp tree #f #f))
238 (define (comp-drop tree) (comp tree #f #t))
239 ;; drop the result if unnecessary
241 (if drop (push-code! #f *ia-drop*)))
242 ;; return here if necessary
243 (define (maybe-return)
244 (if tail (push-code! #f *ia-return*)))
245 ;; return this code if necessary
246 (define (return-code! loc code)
247 (if (not drop) (push-code! loc code))
249 ;; return void if necessary
250 (define (return-void!)
251 (return-code! #f *ia-void*))
252 ;; return object if necessary
253 (define (return-object! loc obj)
254 (return-code! loc (make-glil-const obj)))
261 ((<ghil-quote> env loc obj)
262 (return-object! loc obj))
264 ((<ghil-quasiquote> env loc exp)
265 (let loop ((x exp) (in-car? #f))
268 (push-call! #f 'mark '())
269 (for-each (lambda (x) (loop x #t)) x)
270 (push-call! #f 'list-mark '()))
272 (push-call! #f 'mark '())
275 (push-call! #f 'cons-mark '()))
278 ((<ghil-unquote> env loc exp)
280 ((<ghil-unquote-splicing> env loc exp)
282 (error "unquote-splicing in the cdr of a pair" exp))
284 (push-call! #f 'list-break '()))))
286 (push-code! #f (make-glil-const x)))
288 (error "element of quasiquote can't be compiled" x))))
292 ((<ghil-unquote> env loc exp)
293 (error "unquote outside of quasiquote" exp))
295 ((<ghil-unquote-splicing> env loc exp)
296 (error "unquote-splicing outside of quasiquote" exp))
298 ((<ghil-ref> env loc var)
299 (return-code! loc (make-glil-var 'ref env var)))
301 ((<ghil-set> env loc var val)
303 (push-code! loc (make-glil-var 'set env var))
306 ((<ghil-define> env loc var val)
308 (push-code! loc (make-glil-var 'define env var))
311 ((<ghil-if> env loc test then else)
318 (let ((L1 (make-label)) (L2 (make-label)))
320 (push-branch! loc 'br-if-not L1)
322 (if (not tail) (push-branch! #f 'br L2))
325 (if (not tail) (push-label! L2))))
327 ((<ghil-and> env loc exps)
335 (cond ((null? exps) (return-object! loc #t))
336 ((null? (cdr exps)) (comp-tail (car exps)))
338 (let ((L1 (make-label)) (L2 (make-label)))
339 (let lp ((exps exps))
340 (cond ((null? (cdr exps))
341 (comp-tail (car exps))
342 (push-branch! #f 'br L2)
344 (return-object! #f #f)
348 (comp-push (car exps))
349 (push-branch! #f 'br-if-not L1)
350 (lp (cdr exps)))))))))
352 ((<ghil-or> env loc exps)
360 (cond ((null? exps) (return-object! loc #f))
361 ((null? (cdr exps)) (comp-tail (car exps)))
363 (let ((L1 (make-label)))
364 (let lp ((exps exps))
365 (cond ((null? (cdr exps))
366 (comp-tail (car exps))
370 (comp-push (car exps))
372 (push-call! #f 'dup '()))
373 (push-branch! #f 'br-if L1)
375 (push-code! loc (make-glil-call 'drop 1)))
376 (lp (cdr exps)))))))))
378 ((<ghil-begin> env loc exps)
383 (do ((exps exps (cdr exps)))
385 (comp-tail (car exps)))
386 (comp-drop (car exps)))))
388 ((<ghil-bind> env loc vars vals body)
392 (for-each comp-push vals)
393 (push-bindings! loc vars)
394 (for-each (lambda (var) (push-code! #f (make-glil-var 'set env var)))
397 (push-code! #f (make-glil-unbind)))
399 ((<ghil-mv-bind> env loc producer vars rest body)
403 (let ((MV (make-label)))
405 (push-code! loc (make-glil-mv-call 0 MV))
406 (push-code! #f (make-glil-const 1))
408 (push-code! #f (make-glil-mv-bind (map var->binding vars) rest))
409 (for-each (lambda (var) (push-code! #f (make-glil-var 'set env var)))
412 (push-code! #f (make-glil-unbind)))
414 ((<ghil-lambda> env loc vars rest meta body)
415 (return-code! loc (codegen tree)))
417 ((<ghil-inline> env loc inline args)
420 (let ((tail-table '((call . goto/args)
422 (call/cc . goto/cc))))
423 (cond ((and tail (assq-ref tail-table inline))
424 => (lambda (tail-inst)
425 (push-call! loc tail-inst args)))
427 (push-call! loc inline args)
431 ((<ghil-values> env loc values)
432 (cond (tail ;; (lambda () (values 1 2))
433 (push-call! loc 'return/values values))
434 (drop ;; (lambda () (values 1 2) 3)
435 (for-each comp-drop values))
436 (else ;; (lambda () (list (values 10 12) 1))
437 (push-code! #f (make-glil-const 'values))
438 (push-code! #f (make-glil-call 'link-now 1))
439 (push-code! #f (make-glil-call 'variable-ref 0))
440 (push-call! loc 'call values))))
442 ((<ghil-values*> env loc values)
443 (cond (tail ;; (lambda () (apply values '(1 2)))
444 (push-call! loc 'return/values* values))
445 (drop ;; (lambda () (apply values '(1 2)) 3)
446 (for-each comp-drop values))
447 (else ;; (lambda () (list (apply values '(10 12)) 1))
448 (push-code! #f (make-glil-const 'values))
449 (push-code! #f (make-glil-call 'link-now 1))
450 (push-code! #f (make-glil-call 'variable-ref 0))
451 (push-call! loc 'apply values))))
453 ((<ghil-call> env loc proc args)
456 ;; ([tail-]call NARGS)
458 (let ((nargs (length args)))
460 (push-call! loc (if tail 'goto/args 'call) args))
462 (push-call! loc 'mark '())
463 (for-each comp-push args)
464 (push-call! loc 'list-mark '())
465 (push-code! loc (make-glil-call (if tail 'goto/apply 'apply) 2)))))
468 ((<ghil-mv-call> env loc producer consumer)
474 ;; MV: [tail-]call/nargs
475 ;; POST: (maybe-drop)
476 (let ((MV (make-label)) (POST (make-label)))
479 (push-code! loc (make-glil-mv-call 0 MV))
480 (push-code! loc (make-glil-call (if tail 'goto/args 'call) 1))
482 (push-branch! #f 'br POST)))
484 (push-code! loc (make-glil-call (if tail 'goto/nargs 'call/nargs) 0))
489 ((<ghil-reified-env> env loc)
490 (return-object! loc (ghil-env-reify env)))))
495 ((<ghil-lambda> env loc vars rest meta body)
496 (let* ((evars (ghil-env-variables env))
497 (locs (pick (lambda (v) (eq? (ghil-var-kind v) 'local)) evars))
498 (exts (pick (lambda (v) (eq? (ghil-var-kind v) 'external)) evars))
499 (nargs (allocate-indices-linearly! vars))
500 (nlocs (allocate-locals! locs body nargs))
501 (nexts (allocate-indices-linearly! exts)))
503 (push-bindings! #f vars)
504 ;; push on definition source location
505 (if loc (set! stack (cons (make-glil-source loc) stack)))
506 ;; copy args to the heap if they're marked as external
511 (case (ghil-var-kind v)
513 (push-code! #f (make-glil-local 'ref n))
514 (push-code! #f (make-glil-external 'set 0 (ghil-var-index v)))))))
518 (make-glil-program nargs (if rest 1 0) nlocs nexts meta
519 (reverse! stack)))))))
521 (define (allocate-indices-linearly! vars)
525 (let ((v (car l))) (set! (ghil-var-index v) n))))
527 (define (allocate-locals! vars body nargs)
528 (let ((free '()) (nlocs nargs))
529 (define (allocate! var)
532 (set! (ghil-var-index var) (car free))
533 (set! free (cdr free)))
535 (set! (ghil-var-index var) nlocs)
536 (set! nlocs (1+ nlocs)))))
537 (define (deallocate! var)
538 (set! free (cons (ghil-var-index var) free)))
543 ((<ghil-quasiquote> exp)
545 (cond ((list? x) (for-each qlp x))
546 ((pair? x) (qlp (car x)) (qlp (cdr x)))
549 ((<ghil-unquote> exp) (lp exp))
550 ((<ghil-unquote-splicing> exp) (lp exp)))))))
551 ((<ghil-unquote> exp)
553 ((<ghil-unquote-splicing> exp)
555 ((<ghil-reified-env>))
561 ((<ghil-if> test then else)
562 (lp test) (lp then) (lp else))
569 ((<ghil-bind> vars vals body)
570 (for-each allocate! vars)
573 (for-each deallocate! vars))
574 ((<ghil-mv-bind> vars producer body)
576 (for-each allocate! vars)
578 (for-each deallocate! vars))
579 ((<ghil-inline> args)
581 ((<ghil-call> proc args)
585 ((<ghil-mv-call> producer consumer)
588 ((<ghil-values> values)
589 (for-each lp values))
590 ((<ghil-values*> values)
591 (for-each lp values))))