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 elisp compile-tree-il)
23 #:use-module (language tree-il)
24 #:use-module (system base pmatch)
25 #:export (compile-tree-il))
28 ; Find the source properties of some parsed expression if there are any
33 (let ((props (source-properties x)))
34 (and (not (null? props))
38 ; Value to use for Elisp's nil and t.
40 (define (nil-value loc) (make-const loc #f))
41 (define (t-value loc) (make-const loc #t))
44 ; Modules that contain the value and function slot bindings.
46 (define runtime '(language elisp runtime))
47 (define value-slot '(language elisp runtime value-slot))
48 (define function-slot '(language elisp runtime function-slot))
51 ; Build a call to a primitive procedure nicely.
53 (define (call-primitive loc sym . args)
54 (make-application loc (make-primitive-ref loc sym) args))
57 ; Error reporting routine for syntax/compilation problems or build code for
58 ; a runtime-error output.
60 (define (report-error loc . args)
63 (define (runtime-error loc msg . args)
64 (make-application loc (make-primitive-ref loc 'error)
65 (cons (make-const loc msg) args)))
68 ; Generate code to ensure a fluid is there for further use of a given symbol.
70 (define (ensure-fluid! loc sym module)
75 ; Generate code to reference a fluid saved variable.
77 (define (reference-variable loc sym module)
79 (list (ensure-fluid! loc sym module)
80 (call-primitive loc 'fluid-ref
81 (make-module-ref loc module sym #t)))))
84 ; Reference a variable and error if the value is void.
86 (define (reference-with-check loc sym module)
88 (make-let loc '(value) `(,var) `(,(reference-variable loc sym module))
90 (call-primitive loc 'eq?
91 (make-module-ref loc runtime 'void #t)
92 (make-lexical-ref loc 'value var))
93 (runtime-error loc "variable is void:" (make-const loc sym))
94 (make-lexical-ref loc 'value var)))))
97 ; Generate code to set a fluid saved variable.
99 (define (set-variable! loc sym module value)
101 (list (ensure-fluid! loc sym module)
102 (call-primitive loc 'fluid-set!
103 (make-module-ref loc module sym #t)
107 ; Process the bindings part of a let or let* expression; that is, check for
108 ; correctness and bring it to the form ((sym1 . val1) (sym2 . val2) ...).
110 (define (process-let-bindings loc bindings)
114 (if (or (not (list? b))
115 (not (= (length b) 2)))
116 (report-error loc "expected symbol or list of 2 elements in let")
117 (if (not (symbol? (car b)))
118 (report-error loc "expected symbol in let")
119 (cons (car b) (cadr b))))))
123 ; Split the argument list of a lambda expression into required, optional and
124 ; rest arguments and also check it is actually valid.
126 (define (split-lambda-arguments loc args)
127 (let iterate ((tail args)
134 (values (reverse required) (reverse optional) #f))
136 ((and (eq? mode 'required)
137 (eq? (car tail) '&optional))
138 (iterate (cdr tail) 'optional required optional))
140 ((eq? (car tail) '&rest)
141 (if (or (null? (cdr tail))
142 (not (null? (cddr tail))))
143 (report-error loc "expected exactly one symbol after &rest")
144 (values (reverse required) (reverse optional) (cadr tail))))
147 (if (not (symbol? (car tail)))
148 (report-error loc "expected symbol in argument list, got" (car tail))
150 ((required) (iterate (cdr tail) mode
151 (cons (car tail) required) optional))
152 ((optional) (iterate (cdr tail) mode
153 required (cons (car tail) optional)))
154 ((else) (error "invalid mode in split-lambda-arguments" mode))))))))
157 ; Compile a lambda expression. Things get a little complicated because TreeIL
158 ; does not allow optional arguments but only one rest argument, and also the
159 ; rest argument should be nil instead of '() for no values given. Because of
160 ; this, we have to do a little preprocessing to get everything done before the
161 ; real body is called.
163 ; (lambda (a &optional b &rest c) body) should become:
164 ; (lambda (a_ . rest_)
165 ; (with-fluids* (list a b c) (list a_ nil nil)
167 ; (if (not (null? rest_))
169 ; (fluid-set! b (car rest_))
170 ; (set! rest_ (cdr rest_))
171 ; (if (not (null? rest_))
172 ; (fluid-set! c rest_))))
175 ; This is formulated quite imperatively, but I think in this case that is quite
176 ; clear and better than creating a lot of nested let's.
178 (define (compile-lambda loc args body)
181 (split-lambda-arguments loc args))
182 (lambda (required optional rest)
183 ; FIXME: Ensure fluids there!
184 (let ((required-sym (map (lambda (sym) (gensym)) required))
185 (rest-sym (if (or rest (not (null? optional))) (gensym) '())))
186 (let ((real-args (append required-sym rest-sym)))
188 real-args real-args '()
189 (call-primitive loc 'with-fluids*
190 (make-application loc (make-primitive-ref loc 'list)
191 (map (lambda (sym) (make-module-ref loc value-slot sym #t))
192 (append (append required optional)
193 (if rest (list rest) '()))))
194 (make-application loc (make-primitive-ref loc 'list)
195 (append (map (lambda (sym) (make-lexical-ref loc sym sym))
197 (map (lambda (sym) (nil-value loc))
200 (append optional (list rest-sym))))))
201 (make-lambda loc '() '() '()
203 (cons (process-optionals loc optional rest-sym)
204 (cons (process-rest loc rest rest-sym)
205 (map compile-expr body))))))))))))
207 ; Build the code to handle setting of optional arguments that are present
208 ; and updating the rest list.
209 (define (process-optionals loc optional rest-sym)
210 (let iterate ((tail optional))
213 (make-conditional loc
214 (call-primitive loc 'null? (make-lexical-ref loc rest-sym rest-sym))
217 (list (set-variable! loc (car tail) value-slot
218 (call-primitive loc 'car
219 (make-lexical-ref loc rest-sym rest-sym)))
220 (make-lexical-set loc rest-sym rest-sym
221 (call-primitive loc 'cdr
222 (make-lexical-ref loc rest-sym rest-sym)))
223 (iterate (cdr tail))))))))
225 ; This builds the code to set the rest variable to nil if it is empty.
226 (define (process-rest loc rest rest-sym)
227 (let ((rest-empty (call-primitive loc 'null?
228 (make-lexical-ref loc rest-sym rest-sym))))
231 (make-conditional loc rest-empty
233 (set-variable! loc rest value-slot
234 (make-lexical-ref loc rest-sym rest-sym))))
235 ((not (null? rest-sym))
236 (make-conditional loc rest-empty
238 (runtime-error loc "too many arguments and no rest argument")))
239 (else (make-void loc)))))
242 ; Compile a symbol expression. This is a variable reference or maybe some
243 ; special value like nil.
245 (define (compile-symbol loc sym)
248 ((nil) (nil-value loc))
253 (reference-with-check loc sym value-slot))))
256 ; Compile a pair-expression (that is, any structure-like construct).
258 (define (compile-pair loc expr)
262 (make-sequence loc (map compile-expr forms)))
264 ((if ,condition ,ifclause)
265 (make-conditional loc (compile-expr condition)
266 (compile-expr ifclause)
268 ((if ,condition ,ifclause ,elseclause)
269 (make-conditional loc (compile-expr condition)
270 (compile-expr ifclause)
271 (compile-expr elseclause)))
272 ((if ,condition ,ifclause . ,elses)
273 (make-conditional loc (compile-expr condition)
274 (compile-expr ifclause)
275 (make-sequence loc (map compile-expr elses))))
277 ; For (cond ...) forms, a special case is a (condition) clause without
278 ; body. In this case, the value of condition itself should be returned,
279 ; and thus is saved in a local variable for testing and returning, if it
281 ((cond . ,clauses) (guard (and-map (lambda (el)
282 (and (list? el) (not (null? el))))
284 (let iterate ((tail clauses))
287 (let ((cur (car tail)))
288 (if (null? (cdr cur))
289 (let ((var (gensym)))
291 '(condition) `(,var) `(,(compile-expr (car cur)))
292 (make-conditional loc
293 (make-lexical-ref loc 'condition var)
294 (make-lexical-ref loc 'condition var)
295 (iterate (cdr tail)))))
296 (make-conditional loc
297 (compile-expr (car cur))
298 (make-sequence loc (map compile-expr (cdr cur)))
299 (iterate (cdr tail))))))))
301 ((and) (t-value loc))
302 ((and . ,expressions)
303 (let iterate ((tail expressions))
304 (if (null? (cdr tail))
305 (compile-expr (car tail))
306 (make-conditional loc
307 (compile-expr (car tail))
312 (let iterate ((tail expressions))
315 (let ((var (gensym)))
317 '(condition) `(,var) `(,(compile-expr (car tail)))
318 (make-conditional loc
319 (make-lexical-ref loc 'condition var)
320 (make-lexical-ref loc 'condition var)
321 (iterate (cdr tail))))))))
323 ; Build a set form for possibly multiple values. The code is not formulated
324 ; tail recursive because it is clearer this way and large lists of symbol
325 ; expression pairs are very unlikely.
328 (let iterate ((tail args))
330 (list (make-void loc))
331 (let ((sym (car tail))
332 (tailtail (cdr tail)))
333 (if (not (symbol? sym))
334 (report-error loc "expected symbol in setq")
336 (report-error loc "missing value for symbol in setq" sym)
337 (let* ((val (compile-expr (car tailtail)))
338 (op (set-variable! loc sym value-slot val)))
339 (cons op (iterate (cdr tailtail)))))))))))
341 ; Let is done with a single call to with-fluids* binding them locally to new
343 ((let ,bindings . ,body) (guard (and (list? bindings)
345 (not (null? bindings))
347 (let ((bind (process-let-bindings loc bindings)))
348 (call-primitive loc 'with-fluids*
349 (make-application loc (make-primitive-ref loc 'list)
351 (make-module-ref loc value-slot (car el) #t))
353 (make-application loc (make-primitive-ref loc 'list)
355 (compile-expr (cdr el)))
357 (make-lambda loc '() '() '()
358 (make-sequence loc (map compile-expr body))))))
360 ; Let* is compiled to a cascaded set of with-fluid* for each binding in turn
361 ; so that each one already sees the preceding bindings.
362 ((let* ,bindings . ,body) (guard (and (list? bindings)
364 (not (null? bindings))
366 (let ((bind (process-let-bindings loc bindings)))
367 (let iterate ((tail bind))
369 (make-sequence loc (map compile-expr body))
370 (call-primitive loc 'with-fluid*
371 (make-module-ref loc value-slot (caar tail) #t)
372 (compile-expr (cdar tail))
373 (make-lambda loc '() '() '() (iterate (cdr tail))))))))
375 ; A while construct is transformed into a tail-recursive loop like this:
376 ; (letrec ((iterate (lambda ()
382 ((while ,condition . ,body)
383 (let* ((itersym (gensym))
384 (compiled-body (map compile-expr body))
385 (iter-call (make-application loc
386 (make-lexical-ref loc 'iterate itersym)
388 (full-body (make-sequence loc
389 (append compiled-body (list iter-call))))
390 (lambda-body (make-conditional loc
391 (compile-expr condition)
394 (iter-thunk (make-lambda loc '() '() '() lambda-body)))
395 (make-letrec loc '(iterate) (list itersym) (list iter-thunk)
398 ; Either (lambda ...) or (function (lambda ...)) denotes a lambda-expression
399 ; that should be compiled.
400 ((lambda ,args . ,body) (guard (not (null? body)))
401 (compile-lambda loc args body))
402 ((function (lambda ,args . ,body)) (guard (not (null? body)))
403 (compile-lambda loc args body))
406 (make-const loc val))
409 (report-error loc "unrecognized elisp" expr))))
412 ; Compile a single expression to TreeIL.
414 (define (compile-expr expr)
415 (let ((loc (location expr)))
418 (compile-symbol loc expr))
420 (compile-pair loc expr))
421 (else (make-const loc expr)))))
424 ; Entry point for compilation to TreeIL.
426 (define (compile-tree-il expr env opts)