3 ;; Copyright (C) 2009, 2010, 2011 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 3, 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 elisp bindings)
24 #:use-module (language elisp runtime)
25 #:use-module (language tree-il)
26 #:use-module (system base pmatch)
27 #:use-module (system base compile)
28 #:use-module (srfi srfi-1)
29 #:use-module (srfi srfi-8)
30 #:use-module (srfi srfi-11)
31 #:use-module (srfi srfi-26)
32 #:export (compile-tree-il
44 compile-without-void-checks
45 compile-with-always-lexical
47 compile-guile-primitive
55 ;;; Certain common parameters (like the bindings data structure or
56 ;;; compiler options) are not always passed around but accessed using
57 ;;; fluids to simulate dynamic binding (hey, this is about elisp).
59 ;;; The bindings data structure to keep track of symbol binding related
62 (define bindings-data (make-fluid))
64 ;;; Store for which symbols (or all/none) void checks are disabled.
66 (define disable-void-check (make-fluid))
68 ;;; Store which symbols (or all/none) should always be bound lexically,
69 ;;; even with ordinary let and as lambda arguments.
71 (define always-lexical (make-fluid))
73 ;;; Find the source properties of some parsed expression if there are
74 ;;; any associated with it.
78 (let ((props (source-properties x)))
79 (and (not (null? props))
82 ;;; Values to use for Elisp's nil and t.
84 (define (nil-value loc)
85 (make-const loc (@ (language elisp runtime) nil-value)))
88 (make-const loc (@ (language elisp runtime) t-value)))
90 ;;; Modules that contain the value and function slot bindings.
92 (define runtime '(language elisp runtime))
94 (define value-slot (@ (language elisp runtime) value-slot-module))
96 (define function-slot (@ (language elisp runtime) function-slot-module))
98 ;;; The backquoting works the same as quasiquotes in Scheme, but the
99 ;;; forms are named differently; to make easy adaptions, we define these
100 ;;; predicates checking for a symbol being the car of an
101 ;;; unquote/unquote-splicing/backquote form.
103 (define (unquote? sym)
104 (and (symbol? sym) (eq? sym '#{,}#)))
106 (define (unquote-splicing? sym)
107 (and (symbol? sym) (eq? sym '#{,@}#)))
109 ;;; Build a call to a primitive procedure nicely.
111 (define (call-primitive loc sym . args)
112 (make-primcall loc sym args))
114 ;;; Error reporting routine for syntax/compilation problems or build
115 ;;; code for a runtime-error output.
117 (define (report-error loc . args)
120 (define (runtime-error loc msg . args)
121 (make-primcall loc 'error
122 (cons (make-const loc msg) args)))
124 ;;; Generate code to ensure a global symbol is there for further use of
125 ;;; a given symbol. In general during the compilation, those needed are
126 ;;; only tracked with the bindings data structure. Afterwards, however,
127 ;;; for all those needed symbols the globals are really generated with
130 (define (generate-ensure-global loc sym module)
132 (make-module-ref loc runtime 'ensure-fluid! #t)
133 (list (make-const loc module)
134 (make-const loc sym))))
136 (define (ensuring-globals loc bindings body)
139 `(,@(map-globals-needed (fluid-ref bindings)
141 (generate-ensure-global loc sym mod)))
144 ;;; Build a construct that establishes dynamic bindings for certain
145 ;;; variables. We may want to choose between binding with fluids and
146 ;;; with-fluids* and using just ordinary module symbols and
147 ;;; setting/reverting their values with a dynamic-wind.
149 (define (let-dynamic loc syms module vals body)
153 (make-primcall loc 'list
155 (make-module-ref loc module sym #t))
157 (make-primcall loc 'list vals)
160 (make-lambda-case #f '() #f #f #f '() '() body #f))))
162 ;;; Handle access to a variable (reference/setting) correctly depending
163 ;;; on whether it is currently lexically or dynamically bound. lexical
164 ;;; access is done only for references to the value-slot module!
166 (define (access-variable loc
172 (let ((lexical (get-lexical-binding (fluid-ref bindings-data) sym)))
174 (lexical (handle-lexical lexical))
175 ((equal? module function-slot) (handle-global))
176 (else (handle-dynamic)))))
178 ;;; Generate code to reference a variable. For references in the
179 ;;; value-slot module, we may want to generate a lexical reference
180 ;;; instead if the variable has a lexical binding.
182 (define (reference-variable loc sym module)
187 (lambda () (make-module-ref loc module sym #t))
188 (lambda (lexical) (make-lexical-ref loc lexical lexical))
190 (mark-global-needed! (fluid-ref bindings-data) sym module)
193 (make-module-ref loc module sym #t)))))
195 ;;; Generate code to set a variable. Just as with reference-variable, in
196 ;;; case of a reference to value-slot, we want to generate a lexical set
197 ;;; when the variable has a lexical binding.
199 (define (set-variable! loc sym module value)
207 (make-module-ref loc runtime 'set-variable! #t)
208 (list (make-const loc module) (make-const loc sym) value)))
209 (lambda (lexical) (make-lexical-set loc lexical lexical value))
211 (mark-global-needed! (fluid-ref bindings-data) sym module)
214 (make-module-ref loc module sym #t)
217 ;;; Process the bindings part of a let or let* expression; that is,
218 ;;; check for correctness and bring it to the form ((sym1 . val1) (sym2
221 (define (process-let-bindings loc bindings)
226 (if (or (not (list? b))
227 (not (= (length b) 2)))
230 "expected symbol or list of 2 elements in let")
231 (if (not (symbol? (car b)))
232 (report-error loc "expected symbol in let")
233 (cons (car b) (cadr b))))))
236 ;;; Split the let bindings into a list to be done lexically and one
237 ;;; dynamically. A symbol will be bound lexically if and only if: We're
238 ;;; processing a lexical-let (i.e. module is 'lexical), OR we're
239 ;;; processing a value-slot binding AND the symbol is already lexically
240 ;;; bound or is always lexical, OR we're processing a function-slot
243 (define (bind-lexically? sym module)
244 (or (eq? module 'lexical)
245 (eq? module function-slot)
246 (and (equal? module value-slot)
247 (let ((always (fluid-ref always-lexical)))
248 (or (eq? always 'all)
250 (get-lexical-binding (fluid-ref bindings-data) sym))))))
252 (define (split-let-bindings bindings module)
253 (let iterate ((tail bindings)
257 (values (reverse lexical) (reverse dynamic))
258 (if (bind-lexically? (caar tail) module)
259 (iterate (cdr tail) (cons (car tail) lexical) dynamic)
260 (iterate (cdr tail) lexical (cons (car tail) dynamic))))))
262 ;;; Compile let and let* expressions. The code here is used both for
263 ;;; let/let* and flet/flet*, just with a different bindings module.
265 ;;; A special module value 'lexical means that we're doing a lexical-let
266 ;;; instead and the bindings should not be saved to globals at all but
267 ;;; be done with the lexical framework instead.
269 ;;; Let is done with a single call to let-dynamic binding them locally
270 ;;; to new values all "at once". If there is at least one variable to
271 ;;; bind lexically among the bindings, we first do a let for all of them
272 ;;; to evaluate all values before any bindings take place, and then call
273 ;;; let-dynamic for the variables to bind dynamically.
275 (define (generate-let loc module bindings body)
276 (let ((bind (process-let-bindings loc bindings)))
278 (lambda () (split-let-bindings bind module))
279 (lambda (lexical dynamic)
280 (for-each (lambda (sym)
281 (mark-global-needed! (fluid-ref bindings-data)
285 (let ((make-values (lambda (for)
286 (map (lambda (el) (compile-expr (cdr el)))
288 (make-body (lambda ()
289 (list->seq loc (map compile-expr body)))))
291 (let-dynamic loc (map car dynamic) module
292 (make-values dynamic) (make-body))
293 (let* ((lexical-syms (map (lambda (el) (gensym)) lexical))
294 (dynamic-syms (map (lambda (el) (gensym)) dynamic))
295 (all-syms (append lexical-syms dynamic-syms))
296 (vals (append (make-values lexical)
297 (make-values dynamic))))
302 (with-lexical-bindings
303 (fluid-ref bindings-data)
304 (map car lexical) lexical-syms
313 (make-lexical-ref loc
317 (make-body)))))))))))))
319 ;;; Let* is compiled to a cascaded set of "small lets" for each binding
320 ;;; in turn so that each one already sees the preceding bindings.
322 (define (generate-let* loc module bindings body)
323 (let ((bind (process-let-bindings loc bindings)))
325 (for-each (lambda (sym)
326 (if (not (bind-lexically? sym module))
327 (mark-global-needed! (fluid-ref bindings-data)
331 (let iterate ((tail bind))
333 (list->seq loc (map compile-expr body))
334 (let ((sym (caar tail))
335 (value (compile-expr (cdar tail))))
336 (if (bind-lexically? sym module)
337 (let ((target (gensym)))
342 (with-lexical-bindings
343 (fluid-ref bindings-data)
346 (lambda () (iterate (cdr tail))))))
351 (iterate (cdr tail))))))))))
353 ;;; Split the argument list of a lambda expression into required,
354 ;;; optional and rest arguments and also check it is actually valid.
355 ;;; Additionally, we create a list of all "local variables" (that is,
356 ;;; required, optional and rest arguments together) and also this one
357 ;;; split into those to be bound lexically and dynamically. Returned is
358 ;;; as multiple values: required optional rest lexical dynamic
360 (define (bind-arg-lexical? arg)
361 (let ((always (fluid-ref always-lexical)))
362 (or (eq? always 'all)
365 (define (split-lambda-arguments loc args)
366 (let iterate ((tail args)
374 (let ((final-required (reverse required))
375 (final-optional (reverse optional))
376 (final-lexical (reverse lexical))
377 (final-dynamic (reverse dynamic)))
378 (values final-required
383 ((and (eq? mode 'required)
384 (eq? (car tail) '&optional))
385 (iterate (cdr tail) 'optional required optional lexical dynamic))
386 ((eq? (car tail) '&rest)
387 (if (or (null? (cdr tail))
388 (not (null? (cddr tail))))
389 (report-error loc "expected exactly one symbol after &rest")
390 (let* ((rest (cadr tail))
391 (rest-lexical (bind-arg-lexical? rest))
392 (final-required (reverse required))
393 (final-optional (reverse optional))
394 (final-lexical (reverse (if rest-lexical
397 (final-dynamic (reverse (if rest-lexical
399 (cons rest dynamic)))))
400 (values final-required
406 (if (not (symbol? (car tail)))
408 "expected symbol in argument list, got"
410 (let* ((arg (car tail))
411 (bind-lexical (bind-arg-lexical? arg))
412 (new-lexical (if bind-lexical
415 (new-dynamic (if bind-lexical
417 (cons arg dynamic))))
419 ((required) (iterate (cdr tail) mode
420 (cons arg required) optional
421 new-lexical new-dynamic))
422 ((optional) (iterate (cdr tail) mode
423 required (cons arg optional)
424 new-lexical new-dynamic))
426 (error "invalid mode in split-lambda-arguments"
429 ;;; Compile a lambda expression. One thing we have to be aware of is
430 ;;; that lambda arguments are usually dynamically bound, even when a
431 ;;; lexical binding is intact for a symbol. For symbols that are marked
432 ;;; as 'always lexical,' however, we lexically bind here as well, and
433 ;;; thus we get them out of the let-dynamic call and register a lexical
434 ;;; binding for them (the lexical target variable is already there,
435 ;;; namely the real lambda argument from TreeIL).
437 (define (compile-lambda loc args body)
438 (if (not (list? args))
439 (report-error loc "expected list for argument-list" args))
441 (report-error loc "function body must not be empty"))
442 (receive (required optional rest lexical dynamic)
443 (split-lambda-arguments loc args)
444 (define (process-args args)
445 (define (find-pairs pairs filter)
446 (lset-intersection (lambda (name+sym x)
447 (eq? (car name+sym) x))
450 (let* ((syms (map (lambda (x) (gensym)) args))
451 (pairs (map cons args syms))
452 (lexical-pairs (find-pairs pairs lexical))
453 (dynamic-pairs (find-pairs pairs dynamic)))
454 (values syms pairs lexical-pairs dynamic-pairs)))
455 (let*-values (((required-syms
459 (process-args required))
464 (process-args optional))
465 ((rest-syms rest-pairs rest-lex-pairs rest-dyn-pairs)
466 (process-args (if rest (list rest) '())))
467 ((the-rest-sym) (if rest (car rest-syms) #f))
468 ((all-syms) (append required-syms
471 ((all-lex-pairs) (append required-lex-pairs
474 ((all-dyn-pairs) (append required-dyn-pairs
477 (for-each (lambda (sym)
478 (mark-global-needed! (fluid-ref bindings-data)
482 (with-dynamic-bindings
483 (fluid-ref bindings-data)
486 (with-lexical-bindings
487 (fluid-ref bindings-data)
488 (map car all-lex-pairs)
489 (map cdr all-lex-pairs)
500 (map (lambda (x) (nil-value loc)) optional)
503 (list->seq loc (map compile-expr body))))
511 (make-lexical-ref loc
514 (make-lexical-set loc
525 (map (lambda (name-sym)
534 ;;; Handle the common part of defconst and defvar, that is, checking for
535 ;;; a correct doc string and arguments as well as maybe in the future
536 ;;; handling the docstring somehow.
538 (define (handle-var-def loc sym doc)
540 ((not (symbol? sym)) (report-error loc "expected symbol, got" sym))
541 ((> (length doc) 1) (report-error loc "too many arguments to defvar"))
542 ((and (not (null? doc)) (not (string? (car doc))))
543 (report-error loc "expected string as third argument of defvar, got"
545 ;; TODO: Handle doc string if present.
548 ;;; Handle macro and special operator bindings.
550 (define (find-operator sym type)
553 (module-defined? (resolve-interface function-slot) sym)
554 (let* ((op (module-ref (resolve-module function-slot) sym))
555 (op (if (fluid? op) (fluid-ref op) op)))
556 (if (and (pair? op) (eq? (car op) type))
560 ;;; See if a (backquoted) expression contains any unquotes.
562 (define (contains-unquotes? expr)
564 (if (or (unquote? (car expr)) (unquote-splicing? (car expr)))
566 (or (contains-unquotes? (car expr))
567 (contains-unquotes? (cdr expr))))
570 ;;; Process a backquoted expression by building up the needed
571 ;;; cons/append calls. For splicing, it is assumed that the expression
572 ;;; spliced in evaluates to a list. The emacs manual does not really
573 ;;; state either it has to or what to do if it does not, but Scheme
574 ;;; explicitly forbids it and this seems reasonable also for elisp.
576 (define (unquote-cell? expr)
577 (and (list? expr) (= (length expr) 2) (unquote? (car expr))))
579 (define (unquote-splicing-cell? expr)
580 (and (list? expr) (= (length expr) 2) (unquote-splicing? (car expr))))
582 (define (process-backquote loc expr)
583 (if (contains-unquotes? expr)
585 (if (or (unquote-cell? expr) (unquote-splicing-cell? expr))
586 (compile-expr (cadr expr))
587 (let* ((head (car expr))
588 (processed-tail (process-backquote loc (cdr expr)))
589 (head-is-list-2 (and (list? head)
590 (= (length head) 2)))
591 (head-unquote (and head-is-list-2
592 (unquote? (car head))))
593 (head-unquote-splicing (and head-is-list-2
596 (if head-unquote-splicing
599 (compile-expr (cadr head))
601 (call-primitive loc 'cons
603 (compile-expr (cadr head))
604 (process-backquote loc head))
607 "non-pair expression contains unquotes"
609 (make-const loc expr)))
611 ;;; Temporarily update a list of symbols that are handled specially
612 ;;; (disabled void check or always lexical) for compiling body. We need
613 ;;; to handle special cases for already all / set to all and the like.
615 (define (with-added-symbols loc fluid syms body)
617 (report-error loc "symbol-list construct has empty body"))
618 (if (not (or (eq? syms 'all)
619 (and (list? syms) (and-map symbol? syms))))
620 (report-error loc "invalid symbol list" syms))
621 (let ((old (fluid-ref fluid))
622 (make-body (lambda ()
623 (list->seq loc (map compile-expr body)))))
626 (let ((new (if (eq? syms 'all)
629 (with-fluids ((fluid new))
632 ;;; Special operators
634 (defspecial progn (loc args)
635 (list->seq loc (map compile-expr args)))
637 (defspecial if (loc args)
639 ((,cond ,then . ,else)
640 (make-conditional loc
645 (list->seq loc (map compile-expr else)))))))
647 (defspecial defconst (loc args)
649 ((,sym ,value . ,doc)
650 (if (handle-var-def loc sym doc)
655 (compile-expr value))
656 (make-const loc sym))))))
658 (defspecial defvar (loc args)
660 ((,sym) (make-const loc sym))
661 ((,sym ,value . ,doc)
662 (if (handle-var-def loc sym doc)
674 (make-const loc value-slot))
675 (make-const loc sym))
678 (make-module-ref loc value-slot sym #t))
681 (set-variable! loc sym value-slot (compile-expr value)))
682 (make-const loc sym))))))
684 (defspecial setq (loc args)
685 (define (car* x) (if (null? x) '() (car x)))
686 (define (cdr* x) (if (null? x) '() (cdr x)))
687 (define (cadr* x) (car* (cdr* x)))
688 (define (cddr* x) (cdr* (cdr* x)))
691 (let loop ((args args) (last (nil-value loc)))
694 (let ((sym (car args))
695 (val (compile-expr (cadr* args))))
696 (if (not (symbol? sym))
697 (report-error loc "expected symbol in setq")
699 (set-variable! loc sym value-slot val)
701 (reference-variable loc sym value-slot)))))))))
703 (defspecial let (loc args)
706 (generate-let loc value-slot bindings body))))
708 (defspecial lexical-let (loc args)
711 (generate-let loc 'lexical bindings body))))
713 (defspecial flet (loc args)
716 (generate-let loc function-slot bindings body))))
718 (defspecial let* (loc args)
721 (generate-let* loc value-slot bindings body))))
723 (defspecial lexical-let* (loc args)
726 (generate-let* loc 'lexical bindings body))))
728 (defspecial flet* (loc args)
731 (generate-let* loc function-slot bindings body))))
733 ;;; Temporarily set symbols as always lexical only for the lexical scope
736 (defspecial with-always-lexical (loc args)
739 (with-added-symbols loc always-lexical syms body))))
741 ;;; guile-ref allows building TreeIL's module references from within
742 ;;; elisp as a way to access data within the Guile universe. The module
743 ;;; and symbol referenced are static values, just like (@ module symbol)
746 (defspecial guile-ref (loc args)
748 ((,module ,sym) (guard (and (list? module) (symbol? sym)))
749 (make-module-ref loc module sym #t))))
751 ;;; guile-primitive allows to create primitive references, which are
752 ;;; still a little faster.
754 (defspecial guile-primitive (loc args)
757 (make-primitive-ref loc sym))))
759 ;;; A while construct is transformed into a tail-recursive loop like
762 ;;; (letrec ((iterate (lambda ()
769 ;;; As letrec is not directly accessible from elisp, while is
770 ;;; implemented here instead of with a macro.
772 (defspecial while (loc args)
774 ((,condition . ,body)
775 (let* ((itersym (gensym))
776 (compiled-body (map compile-expr body))
777 (iter-call (make-call loc
778 (make-lexical-ref loc
782 (full-body (list->seq loc `(,@compiled-body ,iter-call)))
783 (lambda-body (make-conditional loc
784 (compile-expr condition)
787 (iter-thunk (make-lambda loc
805 (defspecial function (loc args)
807 (((lambda ,args . ,body))
808 (compile-lambda loc args body))
809 ((,sym) (guard (symbol? sym))
810 (reference-variable loc sym function-slot))))
812 (defspecial defmacro (loc args)
814 ((,name ,args . ,body)
815 (if (not (symbol? name))
816 (report-error loc "expected symbol as macro name" name)
824 (make-primcall loc 'cons
825 (list (make-const loc 'macro)
826 (compile-lambda loc args body))))
827 (make-const loc name))))
828 (compile (ensuring-globals loc bindings-data tree-il)
833 (defspecial defun (loc args)
835 ((,name ,args . ,body)
836 (if (not (symbol? name))
837 (report-error loc "expected symbol as function name" name)
845 (make-const loc name))))))
847 (defspecial #{`}# (loc args)
850 (process-backquote loc val))))
852 (defspecial quote (loc args)
855 (make-const loc val))))
857 ;;; Compile a compound expression to Tree-IL.
859 (define (compile-pair loc expr)
860 (let ((operator (car expr))
861 (arguments (cdr expr)))
863 ((find-operator operator 'special-operator)
864 => (lambda (special-operator-function)
865 (special-operator-function loc arguments)))
866 ((find-operator operator 'macro)
867 => (lambda (macro-function)
868 (compile-expr (apply macro-function arguments))))
871 (if (symbol? operator)
872 (reference-variable loc
875 (compile-expr operator))
876 (map compile-expr arguments))))))
878 ;;; Compile a symbol expression. This is a variable reference or maybe
879 ;;; some special value like nil.
881 (define (compile-symbol loc sym)
883 ((nil) (nil-value loc))
885 (else (reference-variable loc sym value-slot))))
887 ;;; Compile a single expression to TreeIL.
889 (define (compile-expr expr)
890 (let ((loc (location expr)))
893 (compile-symbol loc expr))
895 (compile-pair loc expr))
896 (else (make-const loc expr)))))
898 ;;; Process the compiler options.
899 ;;; FIXME: Why is '(()) passed as options by the REPL?
901 (define (valid-symbol-list-arg? value)
903 (and (list? value) (and-map symbol? value))))
905 (define (process-options! opt)
906 (if (and (not (null? opt))
907 (not (equal? opt '(()))))
908 (if (null? (cdr opt))
909 (report-error #f "Invalid compiler options" opt)
910 (let ((key (car opt))
913 ((#:warnings) ; ignore
916 (if (valid-symbol-list-arg? value)
917 (fluid-set! always-lexical value)
919 "Invalid value for #:always-lexical"
921 (else (report-error #f
922 "Invalid compiler option"
925 ;;; Entry point for compilation to TreeIL. This creates the bindings
926 ;;; data structure, and after compiling the main expression we need to
927 ;;; make sure all globals for symbols used during the compilation are
928 ;;; created using the generate-ensure-global function.
930 (define (compile-tree-il expr env opts)
932 (with-fluids ((bindings-data (make-bindings))
933 (disable-void-check '())
934 (always-lexical '()))
935 (process-options! opts)
936 (let ((compiled (compile-expr expr)))
937 (ensuring-globals (location expr) bindings-data compiled)))