1 ;;; Guile High Intermediate Language
3 ;; Copyright (C) 2001 Free Software Foundation, Inc.
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)
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.
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.
22 (define-module (language ghil def)
23 #:use-module (system base syntax)
24 #:use-module (system base pmatch)
25 #:use-module (ice-9 regex)
27 (<ghil-void> make-ghil-void ghil-void?
28 ghil-void-env ghil-void-loc
30 <ghil-quote> make-ghil-quote ghil-quote?
31 ghil-quote-env ghil-quote-loc ghil-quote-obj
33 <ghil-quasiquote> make-ghil-quasiquote ghil-quasiquote?
34 ghil-quasiquote-env ghil-quasiquote-loc ghil-quasiquote-exp
36 <ghil-unquote> make-ghil-unquote ghil-unquote?
37 ghil-unquote-env ghil-unquote-loc ghil-unquote-exp
39 <ghil-unquote-splicing> make-ghil-unquote-splicing ghil-unquote-splicing?
40 ghil-unquote-env ghil-unquote-loc ghil-unquote-exp
42 <ghil-ref> make-ghil-ref ghil-ref?
43 ghil-ref-env ghil-ref-loc ghil-ref-var
45 <ghil-set> make-ghil-set ghil-set?
46 ghil-set-env ghil-set-loc ghil-set-var ghil-set-val
48 <ghil-define> make-ghil-define ghil-define?
49 ghil-define-env ghil-define-loc ghil-define-var ghil-define-val
51 <ghil-if> make-ghil-if ghil-if?
52 ghil-if-env ghil-if-loc ghil-if-test ghil-if-then ghil-if-else
54 <ghil-and> make-ghil-and ghil-and?
55 ghil-and-env ghil-and-loc ghil-and-exps
57 <ghil-or> make-ghil-or ghil-or?
58 ghil-or-env ghil-or-loc ghil-or-exps
60 <ghil-begin> make-ghil-begin ghil-begin?
61 ghil-begin-env ghil-begin-loc ghil-begin-exps
63 <ghil-bind> make-ghil-bind ghil-bind?
64 ghil-bind-env ghil-bind-loc ghil-bind-vars ghil-bind-vals ghil-bind-body
66 <ghil-mv-bind> make-ghil-mv-bind ghil-mv-bind?
67 ghil-mv-bind-env ghil-mv-bind-loc ghil-mv-bind-producer ghil-mv-bind-vars ghil-mv-bind-rest ghil-mv-bind-body
69 <ghil-lambda> make-ghil-lambda ghil-lambda?
70 ghil-lambda-env ghil-lambda-loc ghil-lambda-vars ghil-lambda-rest
71 ghil-lambda-meta ghil-lambda-body
73 <ghil-inline> make-ghil-inline ghil-inline?
74 ghil-inline-env ghil-inline-loc ghil-inline-inline ghil-inline-args
76 <ghil-call> make-ghil-call ghil-call?
77 ghil-call-env ghil-call-loc ghil-call-proc ghil-call-args
79 <ghil-mv-call> make-ghil-mv-call ghil-mv-call?
80 ghil-mv-call-env ghil-mv-call-loc ghil-mv-call-producer ghil-mv-call-consumer
82 <ghil-values> make-ghil-values ghil-values?
83 ghil-values-env ghil-values-loc ghil-values-values
85 <ghil-values*> make-ghil-values* ghil-values*?
86 ghil-values*-env ghil-values*-loc ghil-values*-values
88 <ghil-var> make-ghil-var ghil-var?
89 ghil-var-env ghil-var-name ghil-var-kind ghil-var-index
91 <ghil-toplevel-env> make-ghil-toplevel-env ghil-toplevel-env?
92 ghil-toplevel-env-table
94 <ghil-env> make-ghil-env ghil-env?
95 ghil-env-parent ghil-env-table ghil-env-variables
97 <ghil-reified-env> make-ghil-reified-env ghil-reified-env?
98 ghil-reified-env-env ghil-reified-env-loc
101 ghil-env-reify ghil-env-dereify
102 ghil-var-is-bound? ghil-var-for-ref! ghil-var-for-set! ghil-var-define!
104 call-with-ghil-environment call-with-ghil-bindings
106 parse-ghil unparse-ghil))
113 (define (print-ghil x port)
114 (format port "#<ghil ~s>" (unparse-ghil x)))
116 (define-type (<ghil> #:printer print-ghil)
118 (<ghil-void> env loc)
119 (<ghil-quote> env loc obj)
120 (<ghil-quasiquote> env loc exp)
121 (<ghil-unquote> env loc exp)
122 (<ghil-unquote-splicing> env loc exp)
124 (<ghil-ref> env loc var)
125 (<ghil-set> env loc var val)
126 (<ghil-define> env loc var val)
128 (<ghil-if> env loc test then else)
129 (<ghil-and> env loc exps)
130 (<ghil-or> env loc exps)
131 (<ghil-begin> env loc exps)
132 (<ghil-bind> env loc vars vals body)
133 (<ghil-mv-bind> env loc producer vars rest body)
134 (<ghil-lambda> env loc vars rest meta body)
135 (<ghil-call> env loc proc args)
136 (<ghil-mv-call> env loc producer consumer)
137 (<ghil-inline> env loc inline args)
138 (<ghil-values> env loc values)
139 (<ghil-values*> env loc values)
140 (<ghil-reified-env> env loc))
148 (define-record <ghil-var> env name kind (index #f))
160 (define-record <ghil-env> parent (table '()) (variables '()))
161 (define-record <ghil-toplevel-env> (table '()))
163 (define (ghil-env-ref env sym)
164 (assq-ref (ghil-env-table env) sym))
166 (define-macro (push! item loc)
167 `(set! ,loc (cons ,item ,loc)))
168 (define-macro (apush! k v loc)
169 `(set! ,loc (acons ,k ,v ,loc)))
170 (define-macro (apopq! k loc)
171 `(set! ,loc (assq-remove! ,loc ,k)))
173 (define (ghil-env-add! env var)
174 (apush! (ghil-var-name var) var (ghil-env-table env))
175 (push! var (ghil-env-variables env)))
177 (define (ghil-env-remove! env var)
178 (apopq! (ghil-var-name var) (ghil-env-table env)))
180 (define (force-heap-allocation! var)
181 (set! (ghil-var-kind var) 'external))
189 ;; The following four functions used to be one, in ghil-lookup. Now they
190 ;; are four, to reflect the different intents. A bit of duplication, but
191 ;; that's OK. The common current is to find out where a variable will be
192 ;; stored at runtime.
194 ;; These functions first search the lexical environments. If the
195 ;; variable is not in the innermost environment, make sure the variable
196 ;; is marked as being "external" so that it goes on the heap. If the
197 ;; variable is being modified (via a set!), also make sure it's on the
198 ;; heap, so that other continuations see the changes to the var.
200 ;; If the variable is not found lexically, it is a toplevel variable,
201 ;; which will be looked up at runtime with respect to the module that
202 ;; was current when the lambda was bound, at runtime. The variable will
203 ;; be resolved when it is first used.
204 (define (ghil-var-is-bound? env sym)
207 ((<ghil-toplevel-env> table)
208 (let ((key (cons (module-name (current-module)) sym)))
209 (assoc-ref table key)))
210 ((<ghil-env> parent table variables)
211 (and (not (assq-ref table sym))
214 (define (ghil-var-for-ref! env sym)
217 ((<ghil-toplevel-env> table)
218 (let ((key (cons (module-name (current-module)) sym)))
219 (or (assoc-ref table key)
220 (let ((var (make-ghil-var (car key) (cdr key) 'toplevel)))
221 (apush! key var (ghil-toplevel-env-table e))
223 ((<ghil-env> parent table variables)
225 ((assq-ref table sym)
228 (force-heap-allocation! var))
233 (define (ghil-var-for-set! env sym)
236 ((<ghil-toplevel-env> table)
237 (let ((key (cons (module-name (current-module)) sym)))
238 (or (assoc-ref table key)
239 (let ((var (make-ghil-var (car key) (cdr key) 'toplevel)))
240 (apush! key var (ghil-toplevel-env-table e))
242 ((<ghil-env> parent table variables)
244 ((assq-ref table sym)
246 (force-heap-allocation! var)
251 (define (ghil-var-at-module! env modname sym interface?)
254 ((<ghil-toplevel-env> table)
255 (let ((key (list modname sym interface?)))
256 (or (assoc-ref table key)
257 (let ((var (make-ghil-var modname sym
258 (if interface? 'public 'private))))
259 (apush! key var (ghil-toplevel-env-table e))
261 ((<ghil-env> parent table variables)
264 (define (ghil-var-define! toplevel sym)
265 (let ((key (cons (module-name (current-module)) sym)))
266 (or (assoc-ref (ghil-toplevel-env-table toplevel) key)
267 (let ((var (make-ghil-var (car key) (cdr key) 'toplevel)))
268 (apush! key var (ghil-toplevel-env-table toplevel))
271 (define (call-with-ghil-environment e syms func)
272 (let* ((e (make-ghil-env e))
273 (vars (map (lambda (s)
274 (let ((v (make-ghil-var e s 'argument)))
275 (ghil-env-add! e v) v))
279 (define (call-with-ghil-bindings e syms func)
280 (let* ((vars (map (lambda (s)
281 (let ((v (make-ghil-var e s 'local)))
282 (ghil-env-add! e v) v))
285 (for-each (lambda (v) (ghil-env-remove! e v)) vars)
288 (define (ghil-env-reify env)
289 (let loop ((e env) (out '()))
291 ((<ghil-toplevel-env> table)
293 (cons (ghil-var-name v)
294 (or (ghil-var-index v)
295 (error "reify called before indices finalized"))))
297 ((<ghil-env> parent table variables)
300 (filter (lambda (v) (eq? (ghil-var-kind v) 'external))
303 (define (ghil-env-dereify name-index-alist)
304 (let* ((e (make-ghil-env (make-ghil-toplevel-env)))
305 (vars (map (lambda (pair)
306 (make-ghil-var e (car pair) 'external (cdr pair)))
308 (set! (ghil-env-table e)
309 (map (lambda (v) (cons (ghil-var-name v) v)) vars))
310 (set! (ghil-env-variables e) vars)
320 (let ((props (source-properties x)))
321 (and (not (null? props))
322 (vector (assq-ref props 'line)
323 (assq-ref props 'column)
324 (assq-ref props 'filename))))))
326 (define (parse-quasiquote e x level)
327 (cond ((not (pair? x)) x)
328 ((memq (car x) '(unquote unquote-splicing))
329 (let ((l (location x)))
334 (if (eq? (car x) 'unquote)
335 (make-ghil-unquote e l (parse-ghil e obj))
336 (make-ghil-unquote-splicing e l (parse-ghil e obj))))
338 (list (car x) (parse-quasiquote e obj (1- level))))))
339 (else (syntax-error l (format #f "bad ~A" (car x)) x)))))
340 ((eq? (car x) 'quasiquote)
341 (let ((l (location x)))
343 ((,obj) (list 'quasiquote (parse-quasiquote e obj (1+ level))))
344 (else (syntax-error l (format #f "bad ~A" (car x)) x)))))
345 (else (cons (parse-quasiquote e (car x) level)
346 (parse-quasiquote e (cdr x) level)))))
348 (define (parse-ghil env exp)
349 (let ((loc (location exp))
350 (retrans (lambda (x) (parse-ghil env x))))
352 (,exp (guard (symbol? exp))
353 (make-ghil-ref env #f (ghil-var-for-ref! env exp)))
355 (,exp (guard (not (pair? exp)))
356 (make-ghil-quote #:env env #:loc #f #:obj exp))
358 (('quote ,exp) (make-ghil-quote #:env env #:loc loc #:obj exp))
360 ((void) (make-ghil-void env loc))
362 ((lambda ,syms ,rest ,meta . ,body)
363 (call-with-ghil-environment env syms
365 (make-ghil-lambda env loc vars rest meta
366 (parse-ghil env `(begin ,@body))))))
369 (make-ghil-begin env loc (map retrans body)))
371 ((bind ,syms ,exprs . ,body)
372 (let ((vals (map retrans exprs)))
373 (call-with-ghil-bindings env syms
375 (make-ghil-bind env loc vars vals (retrans `(begin ,@body)))))))
377 ((bindrec ,syms ,exprs . ,body)
378 (call-with-ghil-bindings env syms
380 (let ((vals (map (lambda (exp) (parse-ghil env exp)) exprs)))
381 (make-ghil-bind env loc vars vals (retrans `(begin ,@body)))))))
384 (make-ghil-set env loc (ghil-var-for-set! env sym) (retrans val)))
387 (make-ghil-define env loc (ghil-var-define! env sym) (retrans val)))
389 ((if ,test ,then ,else)
390 (make-ghil-if env loc (retrans test) (retrans then) (retrans else)))
393 (make-ghil-and env loc (map retrans exps)))
396 (make-ghil-or env loc (map retrans exps)))
398 ((mv-bind ,syms ,rest ,producer . ,body)
399 (call-with-ghil-bindings env syms
401 (make-ghil-mv-bind env loc (retrans producer) vars rest
402 (map retrans body)))))
404 ((call ,proc . ,args)
405 (make-ghil-call env loc (retrans proc) (map retrans args)))
407 ((mv-call ,producer . ,consumer)
408 (make-ghil-mv-call env loc (retrans producer) (retrans consumer)))
410 ((inline ,op . ,args)
411 (make-ghil-inline env loc op (map retrans args)))
414 (make-ghil-values env loc (map retrans values)))
417 (make-ghil-values* env loc (map retrans values)))
419 ((compile-time-environment)
420 (make-ghil-reified-env env loc))
423 (make-ghil-quasiquote env loc #:exp (parse-quasiquote env exp 0)))
426 (error "unrecognized GHIL" exp)))))
428 (define (unparse-ghil ghil)
430 ((<ghil-void> env loc)
432 ((<ghil-quote> env loc obj)
436 ((<ghil-quasiquote> env loc exp)
437 `(,'quasiquote ,(map unparse-ghil exp)))
438 ((<ghil-unquote> env loc exp)
439 `(,'unquote ,(unparse-ghil exp)))
440 ((<ghil-unquote-splicing> env loc exp)
441 `(,'unquote-splicing ,(unparse-ghil exp)))
443 ((<ghil-ref> env loc var)
445 ((<ghil-set> env loc var val)
446 `(set! ,(ghil-var-name var) ,(unparse-ghil val)))
447 ((<ghil-define> env loc var val)
448 `(define ,(ghil-var-name var) ,(unparse-ghil val)))
450 ((<ghil-if> env loc test then else)
451 `(if ,(unparse-ghil test) ,(unparse-ghil then) ,(unparse-ghil else)))
452 ((<ghil-and> env loc exps)
453 `(and ,@(map unparse-ghil exps)))
454 ((<ghil-or> env loc exps)
455 `(or ,@(map unparse-ghil exps)))
456 ((<ghil-begin> env loc exps)
457 `(begin ,@(map unparse-ghil exps)))
458 ((<ghil-bind> env loc vars vals body)
459 `(bind ,(map ghil-var-name vars) ,(map unparse-ghil vals)
460 ,@(map unparse-ghil body)))
461 ((<ghil-mv-bind> env loc producer vars rest body)
462 `(mv-bind ,(map ghil-var-name vars) ,rest
463 ,(unparse-ghil producer) ,@(map unparse-ghil body)))
464 ((<ghil-lambda> env loc vars rest meta body)
465 `(lambda ,(map ghil-var-name vars) ,rest ,meta
466 ,(unparse-ghil body)))
467 ((<ghil-call> env loc proc args)
468 `(call ,(unparse-ghil proc) ,@(map unparse-ghil args)))
469 ((<ghil-mv-call> env loc producer consumer)
470 `(mv-call ,(unparse-ghil producer) ,(unparse-ghil consumer)))
471 ((<ghil-inline> env loc inline args)
472 `(inline ,inline ,@(map unparse-ghil args)))
473 ((<ghil-values> env loc values)
474 `(values ,@(map unparse-ghil values)))
475 ((<ghil-values*> env loc values)
476 `(values* ,@(map unparse-ghil values)))
477 ((<ghil-reified-env> env loc)
478 `(compile-time-environment))))