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 #:use-module (system base compile)
26 #:export (compile-tree-il))
29 ; Find the source properties of some parsed expression if there are any
34 (let ((props (source-properties x)))
35 (and (not (null? props))
39 ; Values to use for Elisp's nil and t.
41 (define (nil-value loc) (make-const loc (@ (language elisp runtime) nil-value)))
42 (define (t-value loc) (make-const loc (@ (language elisp runtime) t-value)))
45 ; Modules that contain the value and function slot bindings.
47 (define runtime '(language elisp runtime))
48 (define value-slot '(language elisp runtime value-slot))
49 (define function-slot '(language elisp runtime function-slot))
50 (define macro-slot '(language elisp runtime macro-slot))
53 ; The backquoting works the same as quasiquotes in Scheme, but the forms are
54 ; named differently; to make easy adaptions, we define these predicates checking
55 ; for a symbol being the car of an unquote/unquote-splicing/backquote form.
57 ; FIXME: Remove the quasiquote/unquote/unquote-splicing symbols when real elisp
60 (define (backquote? sym)
61 (and (symbol? sym) (or (eq? sym 'quasiquote)
64 (define (unquote? sym)
65 (and (symbol? sym) (or (eq? sym 'unquote)
68 (define (unquote-splicing? sym)
69 (and (symbol? sym) (or (eq? sym 'unquote-splicing)
73 ; Build a call to a primitive procedure nicely.
75 (define (call-primitive loc sym . args)
76 (make-application loc (make-primitive-ref loc sym) args))
79 ; Error reporting routine for syntax/compilation problems or build code for
80 ; a runtime-error output.
82 (define (report-error loc . args)
85 (define (runtime-error loc msg . args)
86 (make-application loc (make-primitive-ref loc 'error)
87 (cons (make-const loc msg) args)))
90 ; Generate code to ensure a fluid is there for further use of a given symbol.
91 ; ensure-fluids-for does the same for a list of symbols and builds a sequence
92 ; that executes the fluid-insurances first, followed by all body commands; this
93 ; is a routine for convenience (needed with let, let*, lambda).
95 (define (ensure-fluid! loc sym module)
96 (let ((resolved-module (call-primitive loc 'resolve-module
97 (make-const loc module)))
98 (resolved-intf (call-primitive loc 'resolve-interface
99 (make-const loc module))))
100 (make-conditional loc
101 (call-primitive loc 'module-defined? resolved-intf (make-const loc sym))
104 (list (call-primitive loc 'module-define!
105 resolved-module (make-const loc sym)
106 (call-primitive loc 'make-fluid))
107 (call-primitive loc 'module-export!
109 (call-primitive loc 'list (make-const loc sym)))
110 (call-primitive loc 'fluid-set!
111 (make-module-ref loc module sym #t)
112 (make-module-ref loc runtime 'void #t)))))))
115 (define (ensure-fluids-for loc syms module . body)
117 `(,@(map (lambda (sym) (ensure-fluid! loc sym module)) syms)
121 ; Generate code to reference a fluid saved variable.
123 (define (reference-variable loc sym module)
125 (list (ensure-fluid! loc sym module)
126 (call-primitive loc 'fluid-ref
127 (make-module-ref loc module sym #t)))))
130 ; Reference a variable and error if the value is void.
132 (define (reference-with-check loc sym module)
133 (let ((var (gensym)))
134 (make-let loc '(value) `(,var) `(,(reference-variable loc sym module))
135 (make-conditional loc
136 (call-primitive loc 'eq?
137 (make-module-ref loc runtime 'void #t)
138 (make-lexical-ref loc 'value var))
139 (runtime-error loc "variable is void:" (make-const loc sym))
140 (make-lexical-ref loc 'value var)))))
143 ; Generate code to set a fluid saved variable.
145 (define (set-variable! loc sym module value)
147 (list (ensure-fluid! loc sym module)
148 (call-primitive loc 'fluid-set!
149 (make-module-ref loc module sym #t)
153 ; Process the bindings part of a let or let* expression; that is, check for
154 ; correctness and bring it to the form ((sym1 . val1) (sym2 . val2) ...).
156 (define (process-let-bindings loc bindings)
160 (if (or (not (list? b))
161 (not (= (length b) 2)))
162 (report-error loc "expected symbol or list of 2 elements in let")
163 (if (not (symbol? (car b)))
164 (report-error loc "expected symbol in let")
165 (cons (car b) (cadr b))))))
169 ; Split the argument list of a lambda expression into required, optional and
170 ; rest arguments and also check it is actually valid.
172 (define (split-lambda-arguments loc args)
173 (let iterate ((tail args)
180 (values (reverse required) (reverse optional) #f))
182 ((and (eq? mode 'required)
183 (eq? (car tail) '&optional))
184 (iterate (cdr tail) 'optional required optional))
186 ((eq? (car tail) '&rest)
187 (if (or (null? (cdr tail))
188 (not (null? (cddr tail))))
189 (report-error loc "expected exactly one symbol after &rest")
190 (values (reverse required) (reverse optional) (cadr tail))))
193 (if (not (symbol? (car tail)))
194 (report-error loc "expected symbol in argument list, got" (car tail))
196 ((required) (iterate (cdr tail) mode
197 (cons (car tail) required) optional))
198 ((optional) (iterate (cdr tail) mode
199 required (cons (car tail) optional)))
200 ((else) (error "invalid mode in split-lambda-arguments" mode))))))))
203 ; Compile a lambda expression. Things get a little complicated because TreeIL
204 ; does not allow optional arguments but only one rest argument, and also the
205 ; rest argument should be nil instead of '() for no values given. Because of
206 ; this, we have to do a little preprocessing to get everything done before the
207 ; real body is called.
209 ; (lambda (a &optional b &rest c) body) should become:
210 ; (lambda (a_ . rest_)
211 ; (with-fluids* (list a b c) (list a_ nil nil)
213 ; (if (not (null? rest_))
215 ; (fluid-set! b (car rest_))
216 ; (set! rest_ (cdr rest_))
217 ; (if (not (null? rest_))
218 ; (fluid-set! c rest_))))
221 ; This is formulated quite imperatively, but I think in this case that is quite
222 ; clear and better than creating a lot of nested let's.
224 (define (compile-lambda loc args body)
225 (if (not (list? args))
226 (error "expected list for argument-list" args))
228 (error "function body might not be empty"))
231 (split-lambda-arguments loc args))
232 (lambda (required optional rest)
233 (let ((required-sym (map (lambda (sym) (gensym)) required))
234 (rest-sym (if (or rest (not (null? optional))) (gensym) '())))
235 (let ((real-args (append required-sym rest-sym))
236 (locals `(,@required ,@optional ,@(if rest (list rest) '()))))
238 real-args real-args '()
239 (ensure-fluids-for loc locals value-slot
240 (call-primitive loc 'with-fluids*
241 (make-application loc (make-primitive-ref loc 'list)
242 (map (lambda (sym) (make-module-ref loc value-slot sym #t))
244 (make-application loc (make-primitive-ref loc 'list)
245 (append (map (lambda (sym) (make-lexical-ref loc sym sym))
247 (map (lambda (sym) (nil-value loc))
249 `(,@optional ,rest-sym)
251 (make-lambda loc '() '() '()
253 `(,(process-optionals loc optional rest-sym)
254 ,(process-rest loc rest rest-sym)
255 ,@(map compile-expr body))))))))))))
257 ; Build the code to handle setting of optional arguments that are present
258 ; and updating the rest list.
259 (define (process-optionals loc optional rest-sym)
260 (let iterate ((tail optional))
263 (make-conditional loc
264 (call-primitive loc 'null? (make-lexical-ref loc rest-sym rest-sym))
267 (list (set-variable! loc (car tail) value-slot
268 (call-primitive loc 'car
269 (make-lexical-ref loc rest-sym rest-sym)))
270 (make-lexical-set loc rest-sym rest-sym
271 (call-primitive loc 'cdr
272 (make-lexical-ref loc rest-sym rest-sym)))
273 (iterate (cdr tail))))))))
275 ; This builds the code to set the rest variable to nil if it is empty.
276 (define (process-rest loc rest rest-sym)
277 (let ((rest-empty (call-primitive loc 'null?
278 (make-lexical-ref loc rest-sym rest-sym))))
281 (make-conditional loc rest-empty
283 (set-variable! loc rest value-slot
284 (make-lexical-ref loc rest-sym rest-sym))))
285 ((not (null? rest-sym))
286 (make-conditional loc rest-empty
288 (runtime-error loc "too many arguments and no rest argument")))
289 (else (make-void loc)))))
292 ; Handle the common part of defconst and defvar, that is, checking for a correct
293 ; doc string and arguments as well as maybe in the future handling the docstring
296 (define (handle-var-def loc sym doc)
298 ((not (symbol? sym)) (report-error loc "expected symbol, got" sym))
299 ((> (length doc) 1) (report-error loc "too many arguments to defvar"))
300 ((and (not (null? doc)) (not (string? (car doc))))
301 (report-error loc "expected string as third argument of defvar, got"
303 ; TODO: Handle doc string if present.
307 ; Handle macro bindings.
309 (define (is-macro? sym)
310 (module-defined? (resolve-interface macro-slot) sym))
312 (define (define-macro! loc sym definition)
313 (let ((resolved (resolve-module macro-slot)))
315 (report-error loc "macro is already defined" sym)
317 (module-define! resolved sym definition)
318 (module-export! resolved (list sym))))))
320 (define (get-macro sym)
321 (module-ref (resolve-module macro-slot) sym))
324 ; See if a (backquoted) expression contains any unquotes.
326 (define (contains-unquotes? expr)
328 (if (or (unquote? (car expr)) (unquote-splicing? (car expr)))
330 (or (contains-unquotes? (car expr))
331 (contains-unquotes? (cdr expr))))
335 ; Process a backquoted expression by building up the needed cons/append calls.
336 ; For splicing, it is assumed that the expression spliced in evaluates to a
337 ; list. The emacs manual does not really state either it has to or what to do
338 ; if it does not, but Scheme explicitly forbids it and this seems reasonable
341 (define (unquote-cell? expr)
342 (and (list? expr) (= (length expr) 2) (unquote? (car expr))))
343 (define (unquote-splicing-cell? expr)
344 (and (list? expr) (= (length expr) 2) (unquote-splicing? (car expr))))
346 (define (process-backquote loc expr)
347 (if (contains-unquotes? expr)
349 (if (or (unquote-cell? expr) (unquote-splicing-cell? expr))
350 (compile-expr (cadr expr))
351 (let* ((head (car expr))
352 (processed-tail (process-backquote loc (cdr expr)))
353 (head-is-list-2 (and (list? head) (= (length head) 2)))
354 (head-unquote (and head-is-list-2 (unquote? (car head))))
355 (head-unquote-splicing (and head-is-list-2
356 (unquote-splicing? (car head)))))
357 (if head-unquote-splicing
358 (call-primitive loc 'append
359 (compile-expr (cadr head)) processed-tail)
360 (call-primitive loc 'cons
362 (compile-expr (cadr head))
363 (process-backquote loc head))
365 (error "non-pair expression contains unquotes" expr))
366 (make-const loc expr)))
369 ; Compile a symbol expression. This is a variable reference or maybe some
370 ; special value like nil.
372 (define (compile-symbol loc sym)
374 ((nil) (nil-value loc))
376 (else (reference-with-check loc sym value-slot))))
379 ; Compile a pair-expression (that is, any structure-like construct).
381 (define (compile-pair loc expr)
385 (make-sequence loc (map compile-expr forms)))
387 ((if ,condition ,ifclause)
388 (make-conditional loc (compile-expr condition)
389 (compile-expr ifclause)
391 ((if ,condition ,ifclause ,elseclause)
392 (make-conditional loc (compile-expr condition)
393 (compile-expr ifclause)
394 (compile-expr elseclause)))
395 ((if ,condition ,ifclause . ,elses)
396 (make-conditional loc (compile-expr condition)
397 (compile-expr ifclause)
398 (make-sequence loc (map compile-expr elses))))
400 ; For (cond ...) forms, a special case is a (condition) clause without
401 ; body. In this case, the value of condition itself should be returned,
402 ; and thus is saved in a local variable for testing and returning, if it
404 ((cond . ,clauses) (guard (and-map (lambda (el)
405 (and (list? el) (not (null? el))))
407 (let iterate ((tail clauses))
410 (let ((cur (car tail)))
411 (if (null? (cdr cur))
412 (let ((var (gensym)))
414 '(condition) `(,var) `(,(compile-expr (car cur)))
415 (make-conditional loc
416 (make-lexical-ref loc 'condition var)
417 (make-lexical-ref loc 'condition var)
418 (iterate (cdr tail)))))
419 (make-conditional loc
420 (compile-expr (car cur))
421 (make-sequence loc (map compile-expr (cdr cur)))
422 (iterate (cdr tail))))))))
424 ((and) (t-value loc))
425 ((and . ,expressions)
426 (let iterate ((tail expressions))
427 (if (null? (cdr tail))
428 (compile-expr (car tail))
429 (make-conditional loc
430 (compile-expr (car tail))
435 (let iterate ((tail expressions))
438 (let ((var (gensym)))
440 '(condition) `(,var) `(,(compile-expr (car tail)))
441 (make-conditional loc
442 (make-lexical-ref loc 'condition var)
443 (make-lexical-ref loc 'condition var)
444 (iterate (cdr tail))))))))
446 ((defconst ,sym ,value . ,doc)
447 (if (handle-var-def loc sym doc)
449 (list (set-variable! loc sym value-slot (compile-expr value))
450 (make-const loc sym)))))
452 ((defvar ,sym) (make-const loc sym))
453 ((defvar ,sym ,value . ,doc)
454 (if (handle-var-def loc sym doc)
456 (list (make-conditional loc
457 (call-primitive loc 'eq?
458 (make-module-ref loc runtime 'void #t)
459 (reference-variable loc sym value-slot))
460 (set-variable! loc sym value-slot (compile-expr value))
462 (make-const loc sym)))))
464 ; Build a set form for possibly multiple values. The code is not formulated
465 ; tail recursive because it is clearer this way and large lists of symbol
466 ; expression pairs are very unlikely.
469 (let iterate ((tail args))
471 (list (make-void loc))
472 (let ((sym (car tail))
473 (tailtail (cdr tail)))
474 (if (not (symbol? sym))
475 (report-error loc "expected symbol in setq")
477 (report-error loc "missing value for symbol in setq" sym)
478 (let* ((val (compile-expr (car tailtail)))
479 (op (set-variable! loc sym value-slot val)))
480 (cons op (iterate (cdr tailtail)))))))))))
482 ; Let is done with a single call to with-fluids* binding them locally to new
484 ((let ,bindings . ,body) (guard (and (list? bindings)
486 (not (null? bindings))
488 (let ((bind (process-let-bindings loc bindings)))
489 (ensure-fluids-for loc (map car bind) value-slot
490 (call-primitive loc 'with-fluids*
491 (make-application loc (make-primitive-ref loc 'list)
493 (make-module-ref loc value-slot (car el) #t))
495 (make-application loc (make-primitive-ref loc 'list)
497 (compile-expr (cdr el)))
499 (make-lambda loc '() '() '()
500 (make-sequence loc (map compile-expr body)))))))
502 ; Let* is compiled to a cascaded set of with-fluid* for each binding in turn
503 ; so that each one already sees the preceding bindings.
504 ((let* ,bindings . ,body) (guard (and (list? bindings)
506 (not (null? bindings))
508 (let ((bind (process-let-bindings loc bindings)))
509 (ensure-fluids-for loc (map car bind) value-slot
510 (let iterate ((tail bind))
512 (make-sequence loc (map compile-expr body))
513 (call-primitive loc 'with-fluid*
514 (make-module-ref loc value-slot (caar tail) #t)
515 (compile-expr (cdar tail))
516 (make-lambda loc '() '() '() (iterate (cdr tail)))))))))
518 ; A while construct is transformed into a tail-recursive loop like this:
519 ; (letrec ((iterate (lambda ()
525 ((while ,condition . ,body)
526 (let* ((itersym (gensym))
527 (compiled-body (map compile-expr body))
528 (iter-call (make-application loc
529 (make-lexical-ref loc 'iterate itersym)
531 (full-body (make-sequence loc
532 `(,@compiled-body ,iter-call)))
533 (lambda-body (make-conditional loc
534 (compile-expr condition)
537 (iter-thunk (make-lambda loc '() '() '() lambda-body)))
538 (make-letrec loc '(iterate) (list itersym) (list iter-thunk)
541 ; Either (lambda ...) or (function (lambda ...)) denotes a lambda-expression
542 ; that should be compiled.
543 ((lambda ,args . ,body)
544 (compile-lambda loc args body))
545 ((function (lambda ,args . ,body))
546 (compile-lambda loc args body))
548 ; Build a lambda and also assign it to the function cell of some symbol.
549 ((defun ,name ,args . ,body)
550 (if (not (symbol? name))
551 (error "expected symbol as function name" name)
553 (list (set-variable! loc name function-slot
554 (compile-lambda loc args body))
555 (make-const loc name)))))
557 ; Define a macro (this is done directly at compile-time!).
558 ; FIXME: Recursive macros don't work!
559 ((defmacro ,name ,args . ,body)
560 (if (not (symbol? name))
561 (error "expected symbol as macro name" name)
562 (let* ((tree-il (compile-lambda loc args body))
563 (object (compile tree-il #:from 'tree-il #:to 'value)))
564 (define-macro! loc name object)
565 (make-const loc name))))
567 ((,backq ,val) (guard (backquote? backq))
568 (process-backquote loc val))
570 ; XXX: Why do we need 'quote here instead of quote?
572 (make-const loc val))
574 ; Macro calls are simply expanded and recursively compiled.
575 ((,macro . ,args) (guard (and (symbol? macro) (is-macro? macro)))
576 (let ((expander (get-macro macro)))
577 (compile-expr (apply expander args))))
579 ; Function calls using (function args) standard notation; here, we have to
580 ; take the function value of a symbol if it is one. It seems that functions
581 ; in form of uncompiled lists are not supported in this syntax, so we don't
582 ; have to care for them.
584 (make-application loc
586 (reference-with-check loc func function-slot)
588 (map compile-expr args)))
591 (report-error loc "unrecognized elisp" expr))))
594 ; Compile a single expression to TreeIL.
596 (define (compile-expr expr)
597 (let ((loc (location expr)))
600 (compile-symbol loc expr))
602 (compile-pair loc expr))
603 (else (make-const loc expr)))))
606 ; Entry point for compilation to TreeIL.
608 (define (compile-tree-il expr env opts)