fix bug in compilation of `and' and `or'; more robust underflow detection.
[bpt/guile.git] / module / system / il / compile.scm
CommitLineData
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)
17e90c5e 23 :use-syntax (system base syntax)
17e90c5e
KN
24 :use-module (system il glil)
25 :use-module (system il ghil)
26 :use-module (ice-9 common-list)
27 :export (compile))
28
29(define (compile x e . opts)
17e90c5e
KN
30 (if (memq :O opts) (set! x (optimize x)))
31 (codegen x))
32
33\f
34;;;
35;;; Stage 2: Optimization
36;;;
37
38(define (optimize x)
67169b29 39 (record-case x
61dc81d9 40 ((<ghil-set> env loc var val)
849cefac 41 (make-ghil-set env var (optimize val)))
3616e9e9 42
61dc81d9 43 ((<ghil-if> env loc test then else)
22bcbe8c 44 (make-ghil-if env loc (optimize test) (optimize then) (optimize else)))
3616e9e9 45
61dc81d9 46 ((<ghil-begin> env loc exps)
22bcbe8c 47 (make-ghil-begin env loc (map optimize exps)))
3616e9e9 48
61dc81d9 49 ((<ghil-bind> env loc vars vals body)
22bcbe8c 50 (make-ghil-bind env loc vars (map optimize vals) (optimize body)))
3616e9e9 51
fbde2b91
AW
52 ((<ghil-lambda> env loc vars rest meta body)
53 (make-ghil-lambda env loc vars rest meta (optimize body)))
3616e9e9 54
22bcbe8c
AW
55 ((<ghil-inline> env loc instruction args)
56 (make-ghil-inline env loc instruction (map optimize args)))
3616e9e9 57
61dc81d9
AW
58 ((<ghil-call> env loc proc args)
59 (let ((parent-env env))
60 (record-case proc
61 ;; ((@lambda (VAR...) BODY...) ARG...) =>
62 ;; (@let ((VAR ARG) ...) BODY...)
fbde2b91 63 ((<ghil-lambda> env loc vars rest meta body)
61dc81d9
AW
64 (cond
65 ((not rest)
66 (for-each (lambda (v)
67 (case (ghil-var-kind v)
68 ((argument) (set! (ghil-var-kind v) 'local)))
69 (set! (ghil-var-env v) parent-env)
70 (ghil-env-add! parent-env v))
71 (ghil-env-variables env)))
72 (else
22bcbe8c 73 (make-ghil-call parent-env loc (optimize proc) (map optimize args)))))
61dc81d9 74 (else
22bcbe8c 75 (make-ghil-call parent-env loc (optimize proc) (map optimize args))))))
61dc81d9 76
17e90c5e
KN
77 (else x)))
78
79\f
80;;;
81;;; Stage 3: Code generation
82;;;
83
849cefac
AW
84(define *ia-void* (make-glil-void))
85(define *ia-drop* (make-glil-call 'drop 0))
86(define *ia-return* (make-glil-call 'return 0))
17e90c5e
KN
87
88(define (make-label) (gensym ":L"))
89
90(define (make-glil-var op env var)
aa0a011b 91 (case (ghil-var-kind var)
17e90c5e 92 ((argument)
aa0a011b 93 (make-glil-argument op (ghil-var-index var)))
17e90c5e 94 ((local)
aa0a011b 95 (make-glil-local op (ghil-var-index var)))
17e90c5e
KN
96 ((external)
97 (do ((depth 0 (1+ depth))
aa0a011b
AW
98 (e env (ghil-env-parent e)))
99 ((eq? e (ghil-var-env var))
100 (make-glil-external op depth (ghil-var-index var)))))
17e90c5e 101 ((module)
6167de4f 102 (let ((env (ghil-var-env var)))
6297d229 103 (make-glil-module op (ghil-mod-module (ghil-env-mod env))
6167de4f 104 (ghil-var-name var))))
17e90c5e
KN
105 (else (error "Unknown kind of variable:" var))))
106
107(define (codegen ghil)
108 (let ((stack '()))
96969dc1 109 (define (push-code! loc code)
d0168f3d
AW
110 (set! stack (cons code stack))
111 (if loc (set! stack (cons (make-glil-source loc) stack))))
96969dc1 112 (define (push-bindings! loc vars)
aa0a011b
AW
113 (if (not (null? vars))
114 (push-code!
96969dc1 115 loc
aa0a011b
AW
116 (make-glil-bind
117 (map list
118 (map ghil-var-name vars)
119 (map ghil-var-kind vars)
120 (map ghil-var-index vars))))))
17e90c5e 121 (define (comp tree tail drop)
cb4cca12 122 (define (push-label! label)
96969dc1
AW
123 (push-code! #f (make-glil-label label)))
124 (define (push-branch! loc inst label)
125 (push-code! loc (make-glil-branch inst label)))
ac99cb0c 126 (define (push-call! loc inst args)
cb4cca12 127 (for-each comp-push args)
96969dc1 128 (push-code! loc (make-glil-call inst (length args))))
17e90c5e
KN
129 ;; possible tail position
130 (define (comp-tail tree) (comp tree tail drop))
131 ;; push the result
132 (define (comp-push tree) (comp tree #f #f))
133 ;; drop the result
134 (define (comp-drop tree) (comp tree #f #t))
cb4cca12
KN
135 ;; drop the result if unnecessary
136 (define (maybe-drop)
96969dc1 137 (if drop (push-code! #f *ia-drop*)))
cb4cca12
KN
138 ;; return here if necessary
139 (define (maybe-return)
96969dc1 140 (if tail (push-code! #f *ia-return*)))
17e90c5e 141 ;; return this code if necessary
96969dc1
AW
142 (define (return-code! loc code)
143 (if (not drop) (push-code! loc code))
cb4cca12 144 (maybe-return))
17e90c5e 145 ;; return void if necessary
cb4cca12 146 (define (return-void!)
96969dc1 147 (return-code! #f *ia-void*))
cb4cca12 148 ;; return object if necessary
96969dc1
AW
149 (define (return-object! loc obj)
150 (return-code! loc (make-glil-const #:obj obj)))
17e90c5e
KN
151 ;;
152 ;; dispatch
67169b29
AW
153 (record-case tree
154 ((<ghil-void>)
17e90c5e
KN
155 (return-void!))
156
67169b29 157 ((<ghil-quote> env loc obj)
96969dc1 158 (return-object! loc obj))
cb4cca12 159
67169b29 160 ((<ghil-quasiquote> env loc exp)
cb4cca12 161 (let loop ((x exp))
67169b29
AW
162 (cond
163 ((list? x)
164 (push-call! #f 'mark '())
165 (for-each loop x)
166 (push-call! #f 'list-mark '()))
167 ((pair? x)
168 (loop (car x))
169 (loop (cdr x))
96969dc1 170 (push-code! #f (make-glil-call 'cons 2)))
67169b29
AW
171 ((record? x)
172 (record-case x
173 ((<ghil-unquote> env loc exp)
174 (comp-push exp))
175 ((<ghil-unquote-splicing> env loc exp)
176 (comp-push exp)
177 (push-call! #f 'list-break '()))))
178 (else
96969dc1 179 (push-code! #f (make-glil-const #:obj x)))))
cb4cca12
KN
180 (maybe-drop)
181 (maybe-return))
17e90c5e 182
67169b29 183 ((<ghil-ref> env loc var)
96969dc1 184 (return-code! loc (make-glil-var 'ref env var)))
17e90c5e 185
67169b29 186 ((<ghil-set> env loc var val)
ac99cb0c 187 (comp-push val)
96969dc1 188 (push-code! loc (make-glil-var 'set env var))
ac99cb0c
KN
189 (return-void!))
190
67169b29 191 ((<ghil-define> env loc var val)
17e90c5e 192 (comp-push val)
96969dc1 193 (push-code! loc (make-glil-var 'define env var))
17e90c5e
KN
194 (return-void!))
195
67169b29 196 ((<ghil-if> env loc test then else)
17e90c5e
KN
197 ;; TEST
198 ;; (br-if-not L1)
199 ;; THEN
41f248a8 200 ;; (br L2)
17e90c5e
KN
201 ;; L1: ELSE
202 ;; L2:
203 (let ((L1 (make-label)) (L2 (make-label)))
204 (comp-push test)
96969dc1 205 (push-branch! loc 'br-if-not L1)
17e90c5e 206 (comp-tail then)
96969dc1 207 (if (not tail) (push-branch! #f 'br L2))
cb4cca12 208 (push-label! L1)
17e90c5e 209 (comp-tail else)
cb4cca12
KN
210 (if (not tail) (push-label! L2))))
211
67169b29 212 ((<ghil-and> env loc exps)
cb4cca12
KN
213 ;; EXP
214 ;; (br-if-not L1)
215 ;; ...
216 ;; TAIL
217 ;; (br L2)
218 ;; L1: (const #f)
219 ;; L2:
7e4760e4
AW
220 (cond ((null? exps) (return-object! loc #t))
221 ((null? (cdr exps)) (comp-tail (car exps)))
222 (else
223 (let ((L1 (make-label)) (L2 (make-label)))
224 (let lp ((exps exps))
225 (cond ((null? (cdr exps))
226 (comp-tail (car exps))
227 (push-branch! #f 'br L2)
228 (push-label! L1)
229 (return-object! #f #f)
230 (push-label! L2)
231 (maybe-return))
232 (else
233 (comp-push (car exps))
234 (push-branch! #f 'br-if-not L1)
235 (lp (cdr exps)))))))))
cb4cca12 236
67169b29 237 ((<ghil-or> env loc exps)
cb4cca12
KN
238 ;; EXP
239 ;; (dup)
240 ;; (br-if L1)
241 ;; (drop)
242 ;; ...
243 ;; TAIL
244 ;; L1:
7e4760e4
AW
245 (cond ((null? exps) (return-object! loc #f))
246 ((null? (cdr exps)) (comp-tail (car exps)))
247 (else
248 (let ((L1 (make-label)))
249 (let lp ((exps exps))
250 (cond ((null? (cdr exps))
251 (comp-tail (car exps))
252 (push-label! L1)
253 (maybe-return))
254 (else
255 (comp-push (car exps))
256 (push-call! #f 'dup '())
257 (push-branch! #f 'br-if L1)
258 (push-call! #f 'drop '())
259 (lp (cdr exps)))))))))
17e90c5e 260
67169b29 261 ((<ghil-begin> env loc exps)
17e90c5e
KN
262 ;; EXPS...
263 ;; TAIL
264 (if (null? exps)
265 (return-void!)
266 (do ((exps exps (cdr exps)))
267 ((null? (cdr exps))
268 (comp-tail (car exps)))
269 (comp-drop (car exps)))))
270
67169b29 271 ((<ghil-bind> env loc vars vals body)
17e90c5e
KN
272 ;; VALS...
273 ;; (set VARS)...
274 ;; BODY
275 (for-each comp-push vals)
96969dc1
AW
276 (push-bindings! loc vars)
277 (for-each (lambda (var) (push-code! #f (make-glil-var 'set env var)))
a6df585a 278 (reverse vars))
ac99cb0c 279 (comp-tail body)
96969dc1 280 (push-code! #f (make-glil-unbind)))
17e90c5e 281
fbde2b91 282 ((<ghil-lambda> env loc vars rest meta body)
96969dc1 283 (return-code! loc (codegen tree)))
17e90c5e 284
f540e327 285 ((<ghil-inline> env loc inline args)
46cd9a34
KN
286 ;; ARGS...
287 ;; (INST NARGS)
f540e327 288 (push-call! loc inline args)
cb4cca12
KN
289 (maybe-drop)
290 (maybe-return))
46cd9a34 291
67169b29 292 ((<ghil-call> env loc proc args)
17e90c5e 293 ;; PROC
3616e9e9 294 ;; ARGS...
17e90c5e 295 ;; ([tail-]call NARGS)
17e90c5e 296 (comp-push proc)
ac99cb0c 297 (push-call! loc (if tail 'tail-call 'call) args)
cb4cca12 298 (maybe-drop))))
17e90c5e
KN
299 ;;
300 ;; main
67169b29 301 (record-case ghil
fbde2b91 302 ((<ghil-lambda> env loc vars rest meta body)
f540e327
AW
303 (let* ((evars (ghil-env-variables env))
304 (locs (pick (lambda (v) (eq? (ghil-var-kind v) 'local)) evars))
305 (exts (pick (lambda (v) (eq? (ghil-var-kind v) 'external)) evars)))
17e90c5e 306 ;; initialize variable indexes
f540e327 307 (finalize-index! vars)
17e90c5e
KN
308 (finalize-index! locs)
309 (finalize-index! exts)
ac99cb0c 310 ;; meta bindings
96969dc1 311 (push-bindings! #f vars)
17e90c5e 312 ;; export arguments
061f7fae 313 (do ((n 0 (1+ n))
f540e327 314 (l vars (cdr l)))
17e90c5e
KN
315 ((null? l))
316 (let ((v (car l)))
aa0a011b
AW
317 (case (ghil-var-kind v)
318 ((external)
96969dc1
AW
319 (push-code! #f (make-glil-argument 'ref n))
320 (push-code! #f (make-glil-external 'set 0 (ghil-var-index v)))))))
17e90c5e
KN
321 ;; compile body
322 (comp body #t #f)
323 ;; create GLIL
f540e327 324 (let ((vars (make-glil-vars :nargs (length vars)
849cefac
AW
325 :nrest (if rest 1 0)
326 :nlocs (length locs)
327 :nexts (length exts))))
fbde2b91 328 (make-glil-asm vars meta (reverse! stack))))))))
17e90c5e
KN
329
330(define (finalize-index! list)
331 (do ((n 0 (1+ n))
332 (l list (cdr l)))
333 ((null? l))
aa0a011b 334 (let ((v (car l))) (set! (ghil-var-index v) n))))