3 ;; Copyright (C) 2009, 2010 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-application loc (make-primitive-ref 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-application loc
122 (make-primitive-ref loc 'error)
123 (cons (make-const loc msg) args)))
125 ;;; Generate code to ensure a global symbol is there for further use of
126 ;;; a given symbol. In general during the compilation, those needed are
127 ;;; only tracked with the bindings data structure. Afterwards, however,
128 ;;; for all those needed symbols the globals are really generated with
131 (define (generate-ensure-global loc sym module)
132 (make-application loc
133 (make-module-ref loc runtime 'ensure-fluid! #t)
134 (list (make-const loc module)
135 (make-const loc sym))))
137 (define (ensuring-globals loc bindings body)
140 `(,@(map-globals-needed (fluid-ref bindings)
142 (generate-ensure-global loc sym mod)))
145 ;;; See if we should do a void-check for a given variable. That means,
146 ;;; check that this check is not disabled via the compiler options for
147 ;;; this symbol. Disabling of void check is only done for the value-slot
150 (define (want-void-check? sym module)
151 (let ((disabled (fluid-ref disable-void-check)))
152 (or (not (equal? module value-slot))
153 (and (not (eq? disabled 'all))
154 (not (memq sym disabled))))))
156 ;;; Build a construct that establishes dynamic bindings for certain
157 ;;; variables. We may want to choose between binding with fluids and
158 ;;; with-fluids* and using just ordinary module symbols and
159 ;;; setting/reverting their values with a dynamic-wind.
161 (define (let-dynamic loc syms module vals body)
165 (make-application loc
166 (make-primitive-ref loc 'list)
168 (make-module-ref loc module sym #t))
170 (make-application loc (make-primitive-ref loc 'list) vals)
173 (make-lambda-case #f '() #f #f #f '() '() body #f))))
175 ;;; Handle access to a variable (reference/setting) correctly depending
176 ;;; on whether it is currently lexically or dynamically bound. lexical
177 ;;; access is done only for references to the value-slot module!
179 (define (access-variable loc sym module handle-lexical handle-dynamic)
180 (let ((lexical (get-lexical-binding (fluid-ref bindings-data) sym)))
181 (if (and lexical (equal? module value-slot))
182 (handle-lexical lexical)
185 ;;; Generate code to reference a variable. For references in the
186 ;;; value-slot module, we may want to generate a lexical reference
187 ;;; instead if the variable has a lexical binding.
189 (define (reference-variable loc sym module)
194 (lambda (lexical) (make-lexical-ref loc lexical lexical))
196 (mark-global-needed! (fluid-ref bindings-data) sym module)
199 (make-module-ref loc module sym #t)))))
201 ;;; Reference a variable and error if the value is void.
203 (define (reference-with-check loc sym module)
204 (if (want-void-check? sym module)
205 (let ((var (gensym)))
210 `(,(reference-variable loc sym module))
215 (make-module-ref loc runtime 'void #t)
216 (make-lexical-ref loc 'value var))
217 (runtime-error loc "variable is void:" (make-const loc sym))
218 (make-lexical-ref loc 'value var))))
219 (reference-variable loc sym module)))
221 ;;; Generate code to set a variable. Just as with reference-variable, in
222 ;;; case of a reference to value-slot, we want to generate a lexical set
223 ;;; when the variable has a lexical binding.
225 (define (set-variable! loc sym module value)
230 (lambda (lexical) (make-lexical-set loc lexical lexical value))
232 (mark-global-needed! (fluid-ref bindings-data) sym module)
235 (make-module-ref loc module sym #t)
238 ;;; Process the bindings part of a let or let* expression; that is,
239 ;;; check for correctness and bring it to the form ((sym1 . val1) (sym2
242 (define (process-let-bindings loc bindings)
247 (if (or (not (list? b))
248 (not (= (length b) 2)))
251 "expected symbol or list of 2 elements in let")
252 (if (not (symbol? (car b)))
253 (report-error loc "expected symbol in let")
254 (cons (car b) (cadr b))))))
257 ;;; Split the let bindings into a list to be done lexically and one
258 ;;; dynamically. A symbol will be bound lexically if and only if: We're
259 ;;; processing a lexical-let (i.e. module is 'lexical), OR we're
260 ;;; processing a value-slot binding AND the symbol is already lexically
261 ;;; bound or it is always lexical.
263 (define (bind-lexically? sym module)
264 (or (eq? module 'lexical)
265 (and (equal? module value-slot)
266 (let ((always (fluid-ref always-lexical)))
267 (or (eq? always 'all)
269 (get-lexical-binding (fluid-ref bindings-data) sym))))))
271 (define (split-let-bindings bindings module)
272 (let iterate ((tail bindings)
276 (values (reverse lexical) (reverse dynamic))
277 (if (bind-lexically? (caar tail) module)
278 (iterate (cdr tail) (cons (car tail) lexical) dynamic)
279 (iterate (cdr tail) lexical (cons (car tail) dynamic))))))
281 ;;; Compile let and let* expressions. The code here is used both for
282 ;;; let/let* and flet/flet*, just with a different bindings module.
284 ;;; A special module value 'lexical means that we're doing a lexical-let
285 ;;; instead and the bindings should not be saved to globals at all but
286 ;;; be done with the lexical framework instead.
288 ;;; Let is done with a single call to let-dynamic binding them locally
289 ;;; to new values all "at once". If there is at least one variable to
290 ;;; bind lexically among the bindings, we first do a let for all of them
291 ;;; to evaluate all values before any bindings take place, and then call
292 ;;; let-dynamic for the variables to bind dynamically.
294 (define (generate-let loc module bindings body)
295 (let ((bind (process-let-bindings loc bindings)))
297 (lambda () (split-let-bindings bind module))
298 (lambda (lexical dynamic)
299 (for-each (lambda (sym)
300 (mark-global-needed! (fluid-ref bindings-data)
304 (let ((make-values (lambda (for)
305 (map (lambda (el) (compile-expr (cdr el)))
307 (make-body (lambda ()
308 (make-sequence loc (map compile-expr body)))))
310 (let-dynamic loc (map car dynamic) module
311 (make-values dynamic) (make-body))
312 (let* ((lexical-syms (map (lambda (el) (gensym)) lexical))
313 (dynamic-syms (map (lambda (el) (gensym)) dynamic))
314 (all-syms (append lexical-syms dynamic-syms))
315 (vals (append (make-values lexical)
316 (make-values dynamic))))
321 (with-lexical-bindings
322 (fluid-ref bindings-data)
323 (map car lexical) lexical-syms
332 (make-lexical-ref loc
336 (make-body)))))))))))))
338 ;;; Let* is compiled to a cascaded set of "small lets" for each binding
339 ;;; in turn so that each one already sees the preceding bindings.
341 (define (generate-let* loc module bindings body)
342 (let ((bind (process-let-bindings loc bindings)))
344 (for-each (lambda (sym)
345 (if (not (bind-lexically? sym module))
346 (mark-global-needed! (fluid-ref bindings-data)
350 (let iterate ((tail bind))
352 (make-sequence loc (map compile-expr body))
353 (let ((sym (caar tail))
354 (value (compile-expr (cdar tail))))
355 (if (bind-lexically? sym module)
356 (let ((target (gensym)))
361 (with-lexical-bindings
362 (fluid-ref bindings-data)
365 (lambda () (iterate (cdr tail))))))
370 (iterate (cdr tail))))))))))
372 ;;; Split the argument list of a lambda expression into required,
373 ;;; optional and rest arguments and also check it is actually valid.
374 ;;; Additionally, we create a list of all "local variables" (that is,
375 ;;; required, optional and rest arguments together) and also this one
376 ;;; split into those to be bound lexically and dynamically. Returned is
377 ;;; as multiple values: required optional rest lexical dynamic
379 (define (bind-arg-lexical? arg)
380 (let ((always (fluid-ref always-lexical)))
381 (or (eq? always 'all)
384 (define (split-lambda-arguments loc args)
385 (let iterate ((tail args)
393 (let ((final-required (reverse required))
394 (final-optional (reverse optional))
395 (final-lexical (reverse lexical))
396 (final-dynamic (reverse dynamic)))
397 (values final-required
402 ((and (eq? mode 'required)
403 (eq? (car tail) '&optional))
404 (iterate (cdr tail) 'optional required optional lexical dynamic))
405 ((eq? (car tail) '&rest)
406 (if (or (null? (cdr tail))
407 (not (null? (cddr tail))))
408 (report-error loc "expected exactly one symbol after &rest")
409 (let* ((rest (cadr tail))
410 (rest-lexical (bind-arg-lexical? rest))
411 (final-required (reverse required))
412 (final-optional (reverse optional))
413 (final-lexical (reverse (if rest-lexical
416 (final-dynamic (reverse (if rest-lexical
418 (cons rest dynamic)))))
419 (values final-required
425 (if (not (symbol? (car tail)))
427 "expected symbol in argument list, got"
429 (let* ((arg (car tail))
430 (bind-lexical (bind-arg-lexical? arg))
431 (new-lexical (if bind-lexical
434 (new-dynamic (if bind-lexical
436 (cons arg dynamic))))
438 ((required) (iterate (cdr tail) mode
439 (cons arg required) optional
440 new-lexical new-dynamic))
441 ((optional) (iterate (cdr tail) mode
442 required (cons arg optional)
443 new-lexical new-dynamic))
445 (error "invalid mode in split-lambda-arguments"
448 ;;; Compile a lambda expression. One thing we have to be aware of is
449 ;;; that lambda arguments are usually dynamically bound, even when a
450 ;;; lexical binding is intact for a symbol. For symbols that are marked
451 ;;; as 'always lexical,' however, we lexically bind here as well, and
452 ;;; thus we get them out of the let-dynamic call and register a lexical
453 ;;; binding for them (the lexical target variable is already there,
454 ;;; namely the real lambda argument from TreeIL).
456 (define (compile-lambda loc args body)
457 (if (not (list? args))
458 (report-error loc "expected list for argument-list" args))
460 (report-error loc "function body must not be empty"))
461 (receive (required optional rest lexical dynamic)
462 (split-lambda-arguments loc args)
463 (define (process-args args)
464 (define (find-pairs pairs filter)
465 (lset-intersection (lambda (name+sym x)
466 (eq? (car name+sym) x))
469 (let* ((syms (map (lambda (x) (gensym)) args))
470 (pairs (map cons args syms))
471 (lexical-pairs (find-pairs pairs lexical))
472 (dynamic-pairs (find-pairs pairs dynamic)))
473 (values syms pairs lexical-pairs dynamic-pairs)))
474 (let*-values (((required-syms
478 (process-args required))
483 (process-args optional))
484 ((rest-syms rest-pairs rest-lex-pairs rest-dyn-pairs)
485 (process-args (if rest (list rest) '())))
486 ((the-rest-sym) (if rest (car rest-syms) #f))
487 ((all-syms) (append required-syms
490 ((all-lex-pairs) (append required-lex-pairs
493 ((all-dyn-pairs) (append required-dyn-pairs
496 (for-each (lambda (sym)
497 (mark-global-needed! (fluid-ref bindings-data)
501 (with-dynamic-bindings
502 (fluid-ref bindings-data)
505 (with-lexical-bindings
506 (fluid-ref bindings-data)
507 (map car all-lex-pairs)
508 (map cdr all-lex-pairs)
519 (map (lambda (x) (nil-value loc)) optional)
522 (make-sequence loc (map compile-expr body))))
531 (make-lexical-ref loc
534 (make-lexical-set loc
545 (map (lambda (name-sym)
554 ;;; Handle the common part of defconst and defvar, that is, checking for
555 ;;; a correct doc string and arguments as well as maybe in the future
556 ;;; handling the docstring somehow.
558 (define (handle-var-def loc sym doc)
560 ((not (symbol? sym)) (report-error loc "expected symbol, got" sym))
561 ((> (length doc) 1) (report-error loc "too many arguments to defvar"))
562 ((and (not (null? doc)) (not (string? (car doc))))
563 (report-error loc "expected string as third argument of defvar, got"
565 ;; TODO: Handle doc string if present.
568 ;;; Handle macro and special operator bindings.
570 (define (find-operator sym type)
573 (module-defined? (resolve-interface function-slot) sym)
574 (let* ((op (module-ref (resolve-module function-slot) sym))
575 (op (if (fluid? op) (fluid-ref op) op)))
576 (if (and (pair? op) (eq? (car op) type))
580 ;;; See if a (backquoted) expression contains any unquotes.
582 (define (contains-unquotes? expr)
584 (if (or (unquote? (car expr)) (unquote-splicing? (car expr)))
586 (or (contains-unquotes? (car expr))
587 (contains-unquotes? (cdr expr))))
590 ;;; Process a backquoted expression by building up the needed
591 ;;; cons/append calls. For splicing, it is assumed that the expression
592 ;;; spliced in evaluates to a list. The emacs manual does not really
593 ;;; state either it has to or what to do if it does not, but Scheme
594 ;;; explicitly forbids it and this seems reasonable also for elisp.
596 (define (unquote-cell? expr)
597 (and (list? expr) (= (length expr) 2) (unquote? (car expr))))
599 (define (unquote-splicing-cell? expr)
600 (and (list? expr) (= (length expr) 2) (unquote-splicing? (car expr))))
602 (define (process-backquote loc expr)
603 (if (contains-unquotes? expr)
605 (if (or (unquote-cell? expr) (unquote-splicing-cell? expr))
606 (compile-expr (cadr expr))
607 (let* ((head (car expr))
608 (processed-tail (process-backquote loc (cdr expr)))
609 (head-is-list-2 (and (list? head)
610 (= (length head) 2)))
611 (head-unquote (and head-is-list-2
612 (unquote? (car head))))
613 (head-unquote-splicing (and head-is-list-2
616 (if head-unquote-splicing
619 (compile-expr (cadr head))
621 (call-primitive loc 'cons
623 (compile-expr (cadr head))
624 (process-backquote loc head))
627 "non-pair expression contains unquotes"
629 (make-const loc expr)))
631 ;;; Temporarily update a list of symbols that are handled specially
632 ;;; (disabled void check or always lexical) for compiling body. We need
633 ;;; to handle special cases for already all / set to all and the like.
635 (define (with-added-symbols loc fluid syms body)
637 (report-error loc "symbol-list construct has empty body"))
638 (if (not (or (eq? syms 'all)
639 (and (list? syms) (and-map symbol? syms))))
640 (report-error loc "invalid symbol list" syms))
641 (let ((old (fluid-ref fluid))
642 (make-body (lambda ()
643 (make-sequence loc (map compile-expr body)))))
646 (let ((new (if (eq? syms 'all)
649 (with-fluids ((fluid new))
652 ;;; Special operators
654 (defspecial progn (loc args)
655 (make-sequence loc (map compile-expr args)))
657 (defspecial if (loc args)
659 ((,cond ,then . ,else)
660 (make-conditional loc
666 (map compile-expr else)))))))
668 (defspecial defconst (loc args)
670 ((,sym ,value . ,doc)
671 (if (handle-var-def loc sym doc)
673 (list (set-variable! loc
676 (compile-expr value))
677 (make-const loc sym)))))))
679 (defspecial defvar (loc args)
681 ((,sym) (make-const loc sym))
682 ((,sym ,value . ,doc)
683 (if (handle-var-def loc sym doc)
686 (list (make-conditional
690 (make-module-ref loc runtime 'void #t)
691 (reference-variable loc sym value-slot))
692 (set-variable! loc sym value-slot (compile-expr value))
694 (make-const loc sym)))))))
696 (defspecial setq (loc args)
697 (define (car* x) (if (null? x) '() (car x)))
698 (define (cdr* x) (if (null? x) '() (cdr x)))
699 (define (cadr* x) (car* (cdr* x)))
700 (define (cddr* x) (cdr* (cdr* x)))
703 (let loop ((args args) (last (nil-value loc)))
706 (let ((sym (car args))
707 (val (compile-expr (cadr* args))))
708 (if (not (symbol? sym))
709 (report-error loc "expected symbol in setq")
711 (set-variable! loc sym value-slot val)
713 (reference-variable loc sym value-slot)))))))))
715 (defspecial let (loc args)
718 (generate-let loc value-slot bindings body))))
720 (defspecial lexical-let (loc args)
723 (generate-let loc 'lexical bindings body))))
725 (defspecial flet (loc args)
728 (generate-let loc function-slot bindings body))))
730 (defspecial let* (loc args)
733 (generate-let* loc value-slot bindings body))))
735 (defspecial lexical-let* (loc args)
738 (generate-let* loc 'lexical bindings body))))
740 (defspecial flet* (loc args)
743 (generate-let* loc function-slot bindings body))))
745 ;;; Temporarily disable void checks or set symbols as always lexical
746 ;;; only for the lexical scope of a construct.
748 (defspecial without-void-checks (loc args)
751 (with-added-symbols loc disable-void-check syms body))))
753 (defspecial with-always-lexical (loc args)
756 (with-added-symbols loc always-lexical syms body))))
758 ;;; guile-ref allows building TreeIL's module references from within
759 ;;; elisp as a way to access data within the Guile universe. The module
760 ;;; and symbol referenced are static values, just like (@ module symbol)
763 (defspecial guile-ref (loc args)
765 ((,module ,sym) (guard (and (list? module) (symbol? sym)))
766 (make-module-ref loc module sym #t))))
768 ;;; guile-primitive allows to create primitive references, which are
769 ;;; still a little faster.
771 (defspecial guile-primitive (loc args)
774 (make-primitive-ref loc sym))))
776 ;;; A while construct is transformed into a tail-recursive loop like
779 ;;; (letrec ((iterate (lambda ()
786 ;;; As letrec is not directly accessible from elisp, while is
787 ;;; implemented here instead of with a macro.
789 (defspecial while (loc args)
791 ((,condition . ,body)
792 (let* ((itersym (gensym))
793 (compiled-body (map compile-expr body))
794 (iter-call (make-application loc
795 (make-lexical-ref loc
799 (full-body (make-sequence loc
800 `(,@compiled-body ,iter-call)))
801 (lambda-body (make-conditional loc
802 (compile-expr condition)
805 (iter-thunk (make-lambda loc
823 (defspecial function (loc args)
825 (((lambda ,args . ,body))
826 (compile-lambda loc args body))
827 ((,sym) (guard (symbol? sym))
828 (reference-with-check loc sym function-slot))))
830 (defspecial defmacro (loc args)
832 ((,name ,args . ,body)
833 (if (not (symbol? name))
834 (report-error loc "expected symbol as macro name" name)
845 (make-module-ref loc '(guile) 'cons #t)
846 (list (make-const loc 'macro)
847 (compile-lambda loc args body))))
848 (make-const loc name)))))
849 (compile (ensuring-globals loc bindings-data tree-il)
854 (defspecial defun (loc args)
856 ((,name ,args . ,body)
857 (if (not (symbol? name))
858 (report-error loc "expected symbol as function name" name)
860 (list (set-variable! loc
866 (make-const loc name)))))))
868 (defspecial #{`}# (loc args)
871 (process-backquote loc val))))
873 (defspecial quote (loc args)
876 (make-const loc val))))
878 ;;; Compile a compound expression to Tree-IL.
880 (define (compile-pair loc expr)
881 (let ((operator (car expr))
882 (arguments (cdr expr)))
884 ((find-operator operator 'special-operator)
885 => (lambda (special-operator-function)
886 (special-operator-function loc arguments)))
887 ((find-operator operator 'macro)
888 => (lambda (macro-function)
889 (compile-expr (apply macro-function arguments))))
891 (make-application loc
892 (if (symbol? operator)
893 (reference-with-check loc
896 (compile-expr operator))
897 (map compile-expr arguments))))))
899 ;;; Compile a symbol expression. This is a variable reference or maybe
900 ;;; some special value like nil.
902 (define (compile-symbol loc sym)
904 ((nil) (nil-value loc))
906 (else (reference-with-check loc sym value-slot))))
908 ;;; Compile a single expression to TreeIL.
910 (define (compile-expr expr)
911 (let ((loc (location expr)))
914 (compile-symbol loc expr))
916 (compile-pair loc expr))
917 (else (make-const loc expr)))))
919 ;;; Process the compiler options.
920 ;;; FIXME: Why is '(()) passed as options by the REPL?
922 (define (valid-symbol-list-arg? value)
924 (and (list? value) (and-map symbol? value))))
926 (define (process-options! opt)
927 (if (and (not (null? opt))
928 (not (equal? opt '(()))))
929 (if (null? (cdr opt))
930 (report-error #f "Invalid compiler options" opt)
931 (let ((key (car opt))
934 ((#:warnings) ; ignore
936 ((#:disable-void-check)
937 (if (valid-symbol-list-arg? value)
938 (fluid-set! disable-void-check value)
940 "Invalid value for #:disable-void-check"
943 (if (valid-symbol-list-arg? value)
944 (fluid-set! always-lexical value)
946 "Invalid value for #:always-lexical"
948 (else (report-error #f
949 "Invalid compiler option"
952 ;;; Entry point for compilation to TreeIL. This creates the bindings
953 ;;; data structure, and after compiling the main expression we need to
954 ;;; make sure all globals for symbols used during the compilation are
955 ;;; created using the generate-ensure-global function.
957 (define (compile-tree-il expr env opts)
959 (with-fluids ((bindings-data (make-bindings))
960 (disable-void-check '())
961 (always-lexical '()))
962 (process-options! opts)
963 (let ((compiled (compile-expr expr)))
964 (ensuring-globals (location expr) bindings-data compiled)))