Commit | Line | Data |
---|---|---|
17e90c5e KN |
1 | ;;; GHIL -> GLIL compiler |
2 | ||
3 | ;; Copyright (C) 2001 Free Software Foundation, Inc. | |
4 | ||
5 | ;; This program is free software; you can redistribute it and/or modify | |
6 | ;; it under the terms of the GNU General Public License as published by | |
7 | ;; the Free Software Foundation; either version 2, or (at your option) | |
8 | ;; any later version. | |
9 | ;; | |
10 | ;; This program 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 | |
13 | ;; GNU General Public License for more details. | |
14 | ;; | |
15 | ;; You should have received a copy of the GNU General Public License | |
16 | ;; along with this program; see the file COPYING. If not, write to | |
17 | ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
18 | ;; Boston, MA 02111-1307, USA. | |
19 | ||
20 | ;;; Code: | |
21 | ||
22 | (define-module (system il compile) | |
1a1a10d3 AW |
23 | #:use-syntax (system base syntax) |
24 | #:use-module (system il glil) | |
25 | #:use-module (system il ghil) | |
26 | #:use-module (ice-9 common-list) | |
27 | #:export (compile)) | |
17e90c5e KN |
28 | |
29 | (define (compile x e . opts) | |
1a1a10d3 | 30 | (if (memq #:O opts) (set! x (optimize x))) |
17e90c5e KN |
31 | (codegen x)) |
32 | ||
33 | \f | |
34 | ;;; | |
35 | ;;; Stage 2: Optimization | |
36 | ;;; | |
37 | ||
d51406fe AW |
38 | (define (lift-variables! env) |
39 | (let ((parent-env (ghil-env-parent env))) | |
40 | (for-each (lambda (v) | |
41 | (case (ghil-var-kind v) | |
42 | ((argument) (set! (ghil-var-kind v) 'local))) | |
43 | (set! (ghil-var-env v) parent-env) | |
44 | (ghil-env-add! parent-env v)) | |
45 | (ghil-env-variables env)))) | |
46 | ||
17e90c5e | 47 | (define (optimize x) |
67169b29 | 48 | (record-case x |
61dc81d9 | 49 | ((<ghil-set> env loc var val) |
849cefac | 50 | (make-ghil-set env var (optimize val))) |
3616e9e9 | 51 | |
d51406fe AW |
52 | ((<ghil-define> env loc var val) |
53 | (make-ghil-define env var (optimize val))) | |
54 | ||
61dc81d9 | 55 | ((<ghil-if> env loc test then else) |
22bcbe8c | 56 | (make-ghil-if env loc (optimize test) (optimize then) (optimize else))) |
3616e9e9 | 57 | |
d51406fe AW |
58 | ((<ghil-and> env loc exps) |
59 | (make-ghil-and env loc (map optimize exps))) | |
60 | ||
61 | ((<ghil-or> env loc exps) | |
62 | (make-ghil-or env loc (map optimize exps))) | |
63 | ||
61dc81d9 | 64 | ((<ghil-begin> env loc exps) |
22bcbe8c | 65 | (make-ghil-begin env loc (map optimize exps))) |
3616e9e9 | 66 | |
61dc81d9 | 67 | ((<ghil-bind> env loc vars vals body) |
22bcbe8c | 68 | (make-ghil-bind env loc vars (map optimize vals) (optimize body))) |
3616e9e9 | 69 | |
fbde2b91 AW |
70 | ((<ghil-lambda> env loc vars rest meta body) |
71 | (make-ghil-lambda env loc vars rest meta (optimize body))) | |
3616e9e9 | 72 | |
22bcbe8c AW |
73 | ((<ghil-inline> env loc instruction args) |
74 | (make-ghil-inline env loc instruction (map optimize args))) | |
3616e9e9 | 75 | |
61dc81d9 AW |
76 | ((<ghil-call> env loc proc args) |
77 | (let ((parent-env env)) | |
78 | (record-case proc | |
79 | ;; ((@lambda (VAR...) BODY...) ARG...) => | |
80 | ;; (@let ((VAR ARG) ...) BODY...) | |
fbde2b91 | 81 | ((<ghil-lambda> env loc vars rest meta body) |
61dc81d9 AW |
82 | (cond |
83 | ((not rest) | |
d51406fe AW |
84 | (lift-variables! env) |
85 | (make-ghil-bind parent-env loc (map optimize args))) | |
61dc81d9 | 86 | (else |
22bcbe8c | 87 | (make-ghil-call parent-env loc (optimize proc) (map optimize args))))) |
61dc81d9 | 88 | (else |
22bcbe8c | 89 | (make-ghil-call parent-env loc (optimize proc) (map optimize args)))))) |
61dc81d9 | 90 | |
d51406fe AW |
91 | ((<ghil-mv-call> env loc producer consumer) |
92 | (record-case consumer | |
93 | ;; (mv-call PRODUCER (lambda ARGS BODY...)) => | |
94 | ;; (mv-let PRODUCER ARGS BODY...) | |
95 | ((<ghil-lambda> env loc vars rest meta body) | |
96 | (lift-variables! env) | |
97 | (make-ghil-mv-bind producer vars rest body)) | |
98 | (else | |
99 | (make-ghil-mv-call env loc (optimize producer) (optimize consumer))))) | |
100 | ||
17e90c5e KN |
101 | (else x))) |
102 | ||
103 | \f | |
104 | ;;; | |
105 | ;;; Stage 3: Code generation | |
106 | ;;; | |
107 | ||
849cefac AW |
108 | (define *ia-void* (make-glil-void)) |
109 | (define *ia-drop* (make-glil-call 'drop 0)) | |
110 | (define *ia-return* (make-glil-call 'return 0)) | |
17e90c5e KN |
111 | |
112 | (define (make-label) (gensym ":L")) | |
113 | ||
114 | (define (make-glil-var op env var) | |
aa0a011b | 115 | (case (ghil-var-kind var) |
17e90c5e | 116 | ((argument) |
aa0a011b | 117 | (make-glil-argument op (ghil-var-index var))) |
17e90c5e | 118 | ((local) |
aa0a011b | 119 | (make-glil-local op (ghil-var-index var))) |
17e90c5e KN |
120 | ((external) |
121 | (do ((depth 0 (1+ depth)) | |
aa0a011b AW |
122 | (e env (ghil-env-parent e))) |
123 | ((eq? e (ghil-var-env var)) | |
124 | (make-glil-external op depth (ghil-var-index var))))) | |
a1122f8c AW |
125 | ((toplevel) |
126 | (make-glil-toplevel op (ghil-var-name var))) | |
fd358575 AW |
127 | ((public private) |
128 | (make-glil-module op (ghil-var-env var) (ghil-var-name var) | |
129 | (eq? (ghil-var-kind var) 'public))) | |
17e90c5e KN |
130 | (else (error "Unknown kind of variable:" var)))) |
131 | ||
1b79210a AW |
132 | (define (constant? x) |
133 | (cond ((or (number? x) (string? x) (symbol? x) (keyword? x) (boolean? x)) #t) | |
134 | ((pair? x) (and (constant? (car x)) | |
135 | (constant? (cdr x)))) | |
136 | ((vector? x) (let lp ((i (vector-length x))) | |
137 | (or (zero? i) | |
138 | (and (constant? (vector-ref x (1- i))) | |
139 | (lp (1- i)))))))) | |
140 | ||
17e90c5e KN |
141 | (define (codegen ghil) |
142 | (let ((stack '())) | |
96969dc1 | 143 | (define (push-code! loc code) |
d0168f3d AW |
144 | (set! stack (cons code stack)) |
145 | (if loc (set! stack (cons (make-glil-source loc) stack)))) | |
d51406fe AW |
146 | (define (var->binding var) |
147 | (list (ghil-var-name var) (ghil-var-kind var) (ghil-var-index var))) | |
96969dc1 | 148 | (define (push-bindings! loc vars) |
aa0a011b | 149 | (if (not (null? vars)) |
d51406fe | 150 | (push-code! loc (make-glil-bind (map var->binding vars))))) |
17e90c5e | 151 | (define (comp tree tail drop) |
cb4cca12 | 152 | (define (push-label! label) |
96969dc1 AW |
153 | (push-code! #f (make-glil-label label))) |
154 | (define (push-branch! loc inst label) | |
155 | (push-code! loc (make-glil-branch inst label))) | |
ac99cb0c | 156 | (define (push-call! loc inst args) |
cb4cca12 | 157 | (for-each comp-push args) |
96969dc1 | 158 | (push-code! loc (make-glil-call inst (length args)))) |
17e90c5e KN |
159 | ;; possible tail position |
160 | (define (comp-tail tree) (comp tree tail drop)) | |
161 | ;; push the result | |
162 | (define (comp-push tree) (comp tree #f #f)) | |
163 | ;; drop the result | |
164 | (define (comp-drop tree) (comp tree #f #t)) | |
cb4cca12 KN |
165 | ;; drop the result if unnecessary |
166 | (define (maybe-drop) | |
96969dc1 | 167 | (if drop (push-code! #f *ia-drop*))) |
cb4cca12 KN |
168 | ;; return here if necessary |
169 | (define (maybe-return) | |
96969dc1 | 170 | (if tail (push-code! #f *ia-return*))) |
17e90c5e | 171 | ;; return this code if necessary |
96969dc1 AW |
172 | (define (return-code! loc code) |
173 | (if (not drop) (push-code! loc code)) | |
cb4cca12 | 174 | (maybe-return)) |
17e90c5e | 175 | ;; return void if necessary |
cb4cca12 | 176 | (define (return-void!) |
96969dc1 | 177 | (return-code! #f *ia-void*)) |
cb4cca12 | 178 | ;; return object if necessary |
96969dc1 AW |
179 | (define (return-object! loc obj) |
180 | (return-code! loc (make-glil-const #:obj obj))) | |
17e90c5e KN |
181 | ;; |
182 | ;; dispatch | |
67169b29 AW |
183 | (record-case tree |
184 | ((<ghil-void>) | |
17e90c5e KN |
185 | (return-void!)) |
186 | ||
67169b29 | 187 | ((<ghil-quote> env loc obj) |
96969dc1 | 188 | (return-object! loc obj)) |
cb4cca12 | 189 | |
67169b29 | 190 | ((<ghil-quasiquote> env loc exp) |
cb4cca12 | 191 | (let loop ((x exp)) |
67169b29 AW |
192 | (cond |
193 | ((list? x) | |
194 | (push-call! #f 'mark '()) | |
195 | (for-each loop x) | |
196 | (push-call! #f 'list-mark '())) | |
197 | ((pair? x) | |
198 | (loop (car x)) | |
199 | (loop (cdr x)) | |
96969dc1 | 200 | (push-code! #f (make-glil-call 'cons 2))) |
67169b29 AW |
201 | ((record? x) |
202 | (record-case x | |
203 | ((<ghil-unquote> env loc exp) | |
204 | (comp-push exp)) | |
205 | ((<ghil-unquote-splicing> env loc exp) | |
206 | (comp-push exp) | |
207 | (push-call! #f 'list-break '())))) | |
1b79210a AW |
208 | ((constant? x) |
209 | (push-code! #f (make-glil-const #:obj x))) | |
67169b29 | 210 | (else |
1b79210a | 211 | (error "element of quasiquote can't be compiled" x)))) |
cb4cca12 KN |
212 | (maybe-drop) |
213 | (maybe-return)) | |
17e90c5e | 214 | |
67169b29 | 215 | ((<ghil-ref> env loc var) |
96969dc1 | 216 | (return-code! loc (make-glil-var 'ref env var))) |
17e90c5e | 217 | |
67169b29 | 218 | ((<ghil-set> env loc var val) |
ac99cb0c | 219 | (comp-push val) |
96969dc1 | 220 | (push-code! loc (make-glil-var 'set env var)) |
ac99cb0c KN |
221 | (return-void!)) |
222 | ||
67169b29 | 223 | ((<ghil-define> env loc var val) |
17e90c5e | 224 | (comp-push val) |
96969dc1 | 225 | (push-code! loc (make-glil-var 'define env var)) |
17e90c5e KN |
226 | (return-void!)) |
227 | ||
67169b29 | 228 | ((<ghil-if> env loc test then else) |
17e90c5e KN |
229 | ;; TEST |
230 | ;; (br-if-not L1) | |
231 | ;; THEN | |
41f248a8 | 232 | ;; (br L2) |
17e90c5e KN |
233 | ;; L1: ELSE |
234 | ;; L2: | |
235 | (let ((L1 (make-label)) (L2 (make-label))) | |
236 | (comp-push test) | |
96969dc1 | 237 | (push-branch! loc 'br-if-not L1) |
17e90c5e | 238 | (comp-tail then) |
96969dc1 | 239 | (if (not tail) (push-branch! #f 'br L2)) |
cb4cca12 | 240 | (push-label! L1) |
17e90c5e | 241 | (comp-tail else) |
cb4cca12 KN |
242 | (if (not tail) (push-label! L2)))) |
243 | ||
67169b29 | 244 | ((<ghil-and> env loc exps) |
cb4cca12 KN |
245 | ;; EXP |
246 | ;; (br-if-not L1) | |
247 | ;; ... | |
248 | ;; TAIL | |
249 | ;; (br L2) | |
250 | ;; L1: (const #f) | |
251 | ;; L2: | |
7e4760e4 AW |
252 | (cond ((null? exps) (return-object! loc #t)) |
253 | ((null? (cdr exps)) (comp-tail (car exps))) | |
254 | (else | |
255 | (let ((L1 (make-label)) (L2 (make-label))) | |
256 | (let lp ((exps exps)) | |
257 | (cond ((null? (cdr exps)) | |
258 | (comp-tail (car exps)) | |
259 | (push-branch! #f 'br L2) | |
260 | (push-label! L1) | |
261 | (return-object! #f #f) | |
262 | (push-label! L2) | |
263 | (maybe-return)) | |
264 | (else | |
265 | (comp-push (car exps)) | |
266 | (push-branch! #f 'br-if-not L1) | |
267 | (lp (cdr exps))))))))) | |
cb4cca12 | 268 | |
67169b29 | 269 | ((<ghil-or> env loc exps) |
cb4cca12 KN |
270 | ;; EXP |
271 | ;; (dup) | |
272 | ;; (br-if L1) | |
273 | ;; (drop) | |
274 | ;; ... | |
275 | ;; TAIL | |
276 | ;; L1: | |
7e4760e4 AW |
277 | (cond ((null? exps) (return-object! loc #f)) |
278 | ((null? (cdr exps)) (comp-tail (car exps))) | |
279 | (else | |
280 | (let ((L1 (make-label))) | |
281 | (let lp ((exps exps)) | |
282 | (cond ((null? (cdr exps)) | |
283 | (comp-tail (car exps)) | |
284 | (push-label! L1) | |
285 | (maybe-return)) | |
286 | (else | |
287 | (comp-push (car exps)) | |
535ed4d0 AW |
288 | (if (not drop) |
289 | (push-call! #f 'dup '())) | |
7e4760e4 | 290 | (push-branch! #f 'br-if L1) |
535ed4d0 AW |
291 | (if (not drop) |
292 | (push-call! #f 'drop '())) | |
7e4760e4 | 293 | (lp (cdr exps))))))))) |
17e90c5e | 294 | |
67169b29 | 295 | ((<ghil-begin> env loc exps) |
17e90c5e KN |
296 | ;; EXPS... |
297 | ;; TAIL | |
298 | (if (null? exps) | |
299 | (return-void!) | |
300 | (do ((exps exps (cdr exps))) | |
301 | ((null? (cdr exps)) | |
302 | (comp-tail (car exps))) | |
303 | (comp-drop (car exps))))) | |
304 | ||
67169b29 | 305 | ((<ghil-bind> env loc vars vals body) |
17e90c5e KN |
306 | ;; VALS... |
307 | ;; (set VARS)... | |
308 | ;; BODY | |
309 | (for-each comp-push vals) | |
96969dc1 AW |
310 | (push-bindings! loc vars) |
311 | (for-each (lambda (var) (push-code! #f (make-glil-var 'set env var))) | |
a6df585a | 312 | (reverse vars)) |
ac99cb0c | 313 | (comp-tail body) |
96969dc1 | 314 | (push-code! #f (make-glil-unbind))) |
17e90c5e | 315 | |
d51406fe AW |
316 | ((<ghil-mv-bind> env loc producer vars rest body) |
317 | ;; VALS... | |
318 | ;; (set VARS)... | |
319 | ;; BODY | |
320 | (let ((MV (make-label))) | |
321 | (comp-push producer) | |
322 | (push-code! loc (make-glil-mv-call 0 MV)) | |
323 | (push-code! #f (make-glil-const #:obj 1)) | |
324 | (push-label! MV) | |
325 | (push-code! #f (make-glil-mv-bind (map var->binding vars) rest)) | |
326 | (for-each (lambda (var) (push-code! #f (make-glil-var 'set env var))) | |
327 | (reverse vars))) | |
328 | (comp-tail body) | |
329 | (push-code! #f (make-glil-unbind))) | |
330 | ||
fbde2b91 | 331 | ((<ghil-lambda> env loc vars rest meta body) |
96969dc1 | 332 | (return-code! loc (codegen tree))) |
17e90c5e | 333 | |
f540e327 | 334 | ((<ghil-inline> env loc inline args) |
46cd9a34 KN |
335 | ;; ARGS... |
336 | ;; (INST NARGS) | |
76282387 AW |
337 | (let ((tail-table '((call . goto/args) |
338 | (apply . goto/apply) | |
339 | (call/cc . goto/cc)))) | |
340 | (cond ((and tail (assq-ref tail-table inline)) | |
341 | => (lambda (tail-inst) | |
342 | (push-call! loc tail-inst args))) | |
343 | (else | |
344 | (push-call! loc inline args) | |
345 | (maybe-drop) | |
346 | (maybe-return))))) | |
46cd9a34 | 347 | |
a222b0fa AW |
348 | ((<ghil-values> env loc values) |
349 | (cond (tail ;; (lambda () (values 1 2)) | |
350 | (push-call! loc 'return/values values)) | |
351 | (drop ;; (lambda () (values 1 2) 3) | |
352 | (for-each comp-drop values)) | |
353 | (else ;; (lambda () (list (values 10 12) 1)) | |
354 | (push-code! #f (make-glil-const #:obj 'values)) | |
355 | (push-code! #f (make-glil-call #:inst 'link-now #:nargs 1)) | |
356 | (push-code! #f (make-glil-call #:inst 'variable-ref #:nargs 0)) | |
357 | (push-call! loc 'call values)))) | |
358 | ||
ef24c01b AW |
359 | ((<ghil-values*> env loc values) |
360 | (cond (tail ;; (lambda () (apply values '(1 2))) | |
361 | (push-call! loc 'return/values* values)) | |
362 | (drop ;; (lambda () (apply values '(1 2)) 3) | |
363 | (for-each comp-drop values)) | |
364 | (else ;; (lambda () (list (apply values '(10 12)) 1)) | |
365 | (push-code! #f (make-glil-const #:obj 'values)) | |
366 | (push-code! #f (make-glil-call #:inst 'link-now #:nargs 1)) | |
367 | (push-code! #f (make-glil-call #:inst 'variable-ref #:nargs 0)) | |
368 | (push-call! loc 'apply values)))) | |
369 | ||
67169b29 | 370 | ((<ghil-call> env loc proc args) |
17e90c5e | 371 | ;; PROC |
3616e9e9 | 372 | ;; ARGS... |
17e90c5e | 373 | ;; ([tail-]call NARGS) |
17e90c5e | 374 | (comp-push proc) |
f03c31db | 375 | (push-call! loc (if tail 'goto/args 'call) args) |
efbd5892 AW |
376 | (maybe-drop)) |
377 | ||
378 | ((<ghil-mv-call> env loc producer consumer) | |
379 | ;; CONSUMER | |
380 | ;; PRODUCER | |
381 | ;; (mv-call MV) | |
382 | ;; ([tail]-call 1) | |
383 | ;; goto POST | |
384 | ;; MV: [tail-]call/nargs | |
385 | ;; POST: (maybe-drop) | |
386 | (let ((MV (make-label)) (POST (make-label))) | |
387 | (comp-push consumer) | |
388 | (comp-push producer) | |
389 | (push-code! loc (make-glil-mv-call 0 MV)) | |
390 | (push-code! loc (make-glil-call (if tail 'goto/args 'call) 1)) | |
391 | (cond ((not tail) | |
392 | (push-branch! #f 'br POST))) | |
393 | (push-label! MV) | |
394 | (push-code! loc (make-glil-call (if tail 'goto/nargs 'call/nargs) 0)) | |
395 | (cond ((not tail) | |
396 | (push-label! POST) | |
397 | (maybe-drop))))))) | |
17e90c5e KN |
398 | ;; |
399 | ;; main | |
67169b29 | 400 | (record-case ghil |
fbde2b91 | 401 | ((<ghil-lambda> env loc vars rest meta body) |
f540e327 AW |
402 | (let* ((evars (ghil-env-variables env)) |
403 | (locs (pick (lambda (v) (eq? (ghil-var-kind v) 'local)) evars)) | |
404 | (exts (pick (lambda (v) (eq? (ghil-var-kind v) 'external)) evars))) | |
17e90c5e | 405 | ;; initialize variable indexes |
f540e327 | 406 | (finalize-index! vars) |
17e90c5e KN |
407 | (finalize-index! locs) |
408 | (finalize-index! exts) | |
ac99cb0c | 409 | ;; meta bindings |
96969dc1 | 410 | (push-bindings! #f vars) |
17e90c5e | 411 | ;; export arguments |
061f7fae | 412 | (do ((n 0 (1+ n)) |
f540e327 | 413 | (l vars (cdr l))) |
17e90c5e KN |
414 | ((null? l)) |
415 | (let ((v (car l))) | |
aa0a011b AW |
416 | (case (ghil-var-kind v) |
417 | ((external) | |
96969dc1 AW |
418 | (push-code! #f (make-glil-argument 'ref n)) |
419 | (push-code! #f (make-glil-external 'set 0 (ghil-var-index v))))))) | |
17e90c5e KN |
420 | ;; compile body |
421 | (comp body #t #f) | |
422 | ;; create GLIL | |
1a1a10d3 AW |
423 | (let ((vars (make-glil-vars #:nargs (length vars) |
424 | #:nrest (if rest 1 0) | |
425 | #:nlocs (length locs) | |
426 | #:nexts (length exts)))) | |
fbde2b91 | 427 | (make-glil-asm vars meta (reverse! stack)))))))) |
17e90c5e KN |
428 | |
429 | (define (finalize-index! list) | |
430 | (do ((n 0 (1+ n)) | |
431 | (l list (cdr l))) | |
432 | ((null? l)) | |
aa0a011b | 433 | (let ((v (car l))) (set! (ghil-var-index v) n)))) |