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 ;;; Build a construct that establishes dynamic bindings for certain
146 ;;; variables. We may want to choose between binding with fluids and
147 ;;; with-fluids* and using just ordinary module symbols and
148 ;;; setting/reverting their values with a dynamic-wind.
150 (define (let-dynamic loc syms module vals body)
154 (make-application loc
155 (make-primitive-ref loc 'list)
157 (make-module-ref loc module sym #t))
159 (make-application loc (make-primitive-ref loc 'list) vals)
162 (make-lambda-case #f '() #f #f #f '() '() body #f))))
164 ;;; Handle access to a variable (reference/setting) correctly depending
165 ;;; on whether it is currently lexically or dynamically bound. lexical
166 ;;; access is done only for references to the value-slot module!
168 (define (access-variable loc
174 (let ((lexical (get-lexical-binding (fluid-ref bindings-data) sym)))
176 (lexical (handle-lexical lexical))
177 ((equal? module function-slot) (handle-global))
178 (else (handle-dynamic)))))
180 ;;; Generate code to reference a variable. For references in the
181 ;;; value-slot module, we may want to generate a lexical reference
182 ;;; instead if the variable has a lexical binding.
184 (define (reference-variable loc sym module)
189 (lambda () (make-module-ref loc module sym #t))
190 (lambda (lexical) (make-lexical-ref loc lexical lexical))
192 (mark-global-needed! (fluid-ref bindings-data) sym module)
195 (make-module-ref loc module sym #t)))))
197 ;;; Generate code to set a variable. Just as with reference-variable, in
198 ;;; case of a reference to value-slot, we want to generate a lexical set
199 ;;; when the variable has a lexical binding.
201 (define (set-variable! loc sym module value)
209 (make-module-ref loc runtime 'set-variable! #t)
210 (list (make-const loc module) (make-const loc sym) value)))
211 (lambda (lexical) (make-lexical-set loc lexical lexical value))
213 (mark-global-needed! (fluid-ref bindings-data) sym module)
216 (make-module-ref loc module sym #t)
219 ;;; Process the bindings part of a let or let* expression; that is,
220 ;;; check for correctness and bring it to the form ((sym1 . val1) (sym2
223 (define (process-let-bindings loc bindings)
228 (if (or (not (list? b))
229 (not (= (length b) 2)))
232 "expected symbol or list of 2 elements in let")
233 (if (not (symbol? (car b)))
234 (report-error loc "expected symbol in let")
235 (cons (car b) (cadr b))))))
238 ;;; Split the let bindings into a list to be done lexically and one
239 ;;; dynamically. A symbol will be bound lexically if and only if: We're
240 ;;; processing a lexical-let (i.e. module is 'lexical), OR we're
241 ;;; processing a value-slot binding AND the symbol is already lexically
242 ;;; bound or is always lexical, OR we're processing a function-slot
245 (define (bind-lexically? sym module)
246 (or (eq? module 'lexical)
247 (eq? module function-slot)
248 (and (equal? module value-slot)
249 (let ((always (fluid-ref always-lexical)))
250 (or (eq? always 'all)
252 (get-lexical-binding (fluid-ref bindings-data) sym))))))
254 (define (split-let-bindings bindings module)
255 (let iterate ((tail bindings)
259 (values (reverse lexical) (reverse dynamic))
260 (if (bind-lexically? (caar tail) module)
261 (iterate (cdr tail) (cons (car tail) lexical) dynamic)
262 (iterate (cdr tail) lexical (cons (car tail) dynamic))))))
264 ;;; Compile let and let* expressions. The code here is used both for
265 ;;; let/let* and flet/flet*, just with a different bindings module.
267 ;;; A special module value 'lexical means that we're doing a lexical-let
268 ;;; instead and the bindings should not be saved to globals at all but
269 ;;; be done with the lexical framework instead.
271 ;;; Let is done with a single call to let-dynamic binding them locally
272 ;;; to new values all "at once". If there is at least one variable to
273 ;;; bind lexically among the bindings, we first do a let for all of them
274 ;;; to evaluate all values before any bindings take place, and then call
275 ;;; let-dynamic for the variables to bind dynamically.
277 (define (generate-let loc module bindings body)
278 (let ((bind (process-let-bindings loc bindings)))
280 (lambda () (split-let-bindings bind module))
281 (lambda (lexical dynamic)
282 (for-each (lambda (sym)
283 (mark-global-needed! (fluid-ref bindings-data)
287 (let ((make-values (lambda (for)
288 (map (lambda (el) (compile-expr (cdr el)))
290 (make-body (lambda ()
291 (make-sequence loc (map compile-expr body)))))
293 (let-dynamic loc (map car dynamic) module
294 (make-values dynamic) (make-body))
295 (let* ((lexical-syms (map (lambda (el) (gensym)) lexical))
296 (dynamic-syms (map (lambda (el) (gensym)) dynamic))
297 (all-syms (append lexical-syms dynamic-syms))
298 (vals (append (make-values lexical)
299 (make-values dynamic))))
304 (with-lexical-bindings
305 (fluid-ref bindings-data)
306 (map car lexical) lexical-syms
315 (make-lexical-ref loc
319 (make-body)))))))))))))
321 ;;; Let* is compiled to a cascaded set of "small lets" for each binding
322 ;;; in turn so that each one already sees the preceding bindings.
324 (define (generate-let* loc module bindings body)
325 (let ((bind (process-let-bindings loc bindings)))
327 (for-each (lambda (sym)
328 (if (not (bind-lexically? sym module))
329 (mark-global-needed! (fluid-ref bindings-data)
333 (let iterate ((tail bind))
335 (make-sequence loc (map compile-expr body))
336 (let ((sym (caar tail))
337 (value (compile-expr (cdar tail))))
338 (if (bind-lexically? sym module)
339 (let ((target (gensym)))
344 (with-lexical-bindings
345 (fluid-ref bindings-data)
348 (lambda () (iterate (cdr tail))))))
353 (iterate (cdr tail))))))))))
355 ;;; Split the argument list of a lambda expression into required,
356 ;;; optional and rest arguments and also check it is actually valid.
357 ;;; Additionally, we create a list of all "local variables" (that is,
358 ;;; required, optional and rest arguments together) and also this one
359 ;;; split into those to be bound lexically and dynamically. Returned is
360 ;;; as multiple values: required optional rest lexical dynamic
362 (define (bind-arg-lexical? arg)
363 (let ((always (fluid-ref always-lexical)))
364 (or (eq? always 'all)
367 (define (split-lambda-arguments loc args)
368 (let iterate ((tail args)
376 (let ((final-required (reverse required))
377 (final-optional (reverse optional))
378 (final-lexical (reverse lexical))
379 (final-dynamic (reverse dynamic)))
380 (values final-required
385 ((and (eq? mode 'required)
386 (eq? (car tail) '&optional))
387 (iterate (cdr tail) 'optional required optional lexical dynamic))
388 ((eq? (car tail) '&rest)
389 (if (or (null? (cdr tail))
390 (not (null? (cddr tail))))
391 (report-error loc "expected exactly one symbol after &rest")
392 (let* ((rest (cadr tail))
393 (rest-lexical (bind-arg-lexical? rest))
394 (final-required (reverse required))
395 (final-optional (reverse optional))
396 (final-lexical (reverse (if rest-lexical
399 (final-dynamic (reverse (if rest-lexical
401 (cons rest dynamic)))))
402 (values final-required
408 (if (not (symbol? (car tail)))
410 "expected symbol in argument list, got"
412 (let* ((arg (car tail))
413 (bind-lexical (bind-arg-lexical? arg))
414 (new-lexical (if bind-lexical
417 (new-dynamic (if bind-lexical
419 (cons arg dynamic))))
421 ((required) (iterate (cdr tail) mode
422 (cons arg required) optional
423 new-lexical new-dynamic))
424 ((optional) (iterate (cdr tail) mode
425 required (cons arg optional)
426 new-lexical new-dynamic))
428 (error "invalid mode in split-lambda-arguments"
431 ;;; Compile a lambda expression. One thing we have to be aware of is
432 ;;; that lambda arguments are usually dynamically bound, even when a
433 ;;; lexical binding is intact for a symbol. For symbols that are marked
434 ;;; as 'always lexical,' however, we lexically bind here as well, and
435 ;;; thus we get them out of the let-dynamic call and register a lexical
436 ;;; binding for them (the lexical target variable is already there,
437 ;;; namely the real lambda argument from TreeIL).
439 (define (compile-lambda loc args body)
440 (if (not (list? args))
441 (report-error loc "expected list for argument-list" args))
443 (report-error loc "function body must not be empty"))
444 (receive (required optional rest lexical dynamic)
445 (split-lambda-arguments loc args)
446 (define (process-args args)
447 (define (find-pairs pairs filter)
448 (lset-intersection (lambda (name+sym x)
449 (eq? (car name+sym) x))
452 (let* ((syms (map (lambda (x) (gensym)) args))
453 (pairs (map cons args syms))
454 (lexical-pairs (find-pairs pairs lexical))
455 (dynamic-pairs (find-pairs pairs dynamic)))
456 (values syms pairs lexical-pairs dynamic-pairs)))
457 (let*-values (((required-syms
461 (process-args required))
466 (process-args optional))
467 ((rest-syms rest-pairs rest-lex-pairs rest-dyn-pairs)
468 (process-args (if rest (list rest) '())))
469 ((the-rest-sym) (if rest (car rest-syms) #f))
470 ((all-syms) (append required-syms
473 ((all-lex-pairs) (append required-lex-pairs
476 ((all-dyn-pairs) (append required-dyn-pairs
479 (for-each (lambda (sym)
480 (mark-global-needed! (fluid-ref bindings-data)
484 (with-dynamic-bindings
485 (fluid-ref bindings-data)
488 (with-lexical-bindings
489 (fluid-ref bindings-data)
490 (map car all-lex-pairs)
491 (map cdr all-lex-pairs)
502 (map (lambda (x) (nil-value loc)) optional)
505 (make-sequence loc (map compile-expr body))))
514 (make-lexical-ref loc
517 (make-lexical-set loc
528 (map (lambda (name-sym)
537 ;;; Handle the common part of defconst and defvar, that is, checking for
538 ;;; a correct doc string and arguments as well as maybe in the future
539 ;;; handling the docstring somehow.
541 (define (handle-var-def loc sym doc)
543 ((not (symbol? sym)) (report-error loc "expected symbol, got" sym))
544 ((> (length doc) 1) (report-error loc "too many arguments to defvar"))
545 ((and (not (null? doc)) (not (string? (car doc))))
546 (report-error loc "expected string as third argument of defvar, got"
548 ;; TODO: Handle doc string if present.
551 ;;; Handle macro and special operator bindings.
553 (define (find-operator sym type)
556 (module-defined? (resolve-interface function-slot) sym)
557 (let* ((op (module-ref (resolve-module function-slot) sym))
558 (op (if (fluid? op) (fluid-ref op) op)))
559 (if (and (pair? op) (eq? (car op) type))
563 ;;; See if a (backquoted) expression contains any unquotes.
565 (define (contains-unquotes? expr)
567 (if (or (unquote? (car expr)) (unquote-splicing? (car expr)))
569 (or (contains-unquotes? (car expr))
570 (contains-unquotes? (cdr expr))))
573 ;;; Process a backquoted expression by building up the needed
574 ;;; cons/append calls. For splicing, it is assumed that the expression
575 ;;; spliced in evaluates to a list. The emacs manual does not really
576 ;;; state either it has to or what to do if it does not, but Scheme
577 ;;; explicitly forbids it and this seems reasonable also for elisp.
579 (define (unquote-cell? expr)
580 (and (list? expr) (= (length expr) 2) (unquote? (car expr))))
582 (define (unquote-splicing-cell? expr)
583 (and (list? expr) (= (length expr) 2) (unquote-splicing? (car expr))))
585 (define (process-backquote loc expr)
586 (if (contains-unquotes? expr)
588 (if (or (unquote-cell? expr) (unquote-splicing-cell? expr))
589 (compile-expr (cadr expr))
590 (let* ((head (car expr))
591 (processed-tail (process-backquote loc (cdr expr)))
592 (head-is-list-2 (and (list? head)
593 (= (length head) 2)))
594 (head-unquote (and head-is-list-2
595 (unquote? (car head))))
596 (head-unquote-splicing (and head-is-list-2
599 (if head-unquote-splicing
602 (compile-expr (cadr head))
604 (call-primitive loc 'cons
606 (compile-expr (cadr head))
607 (process-backquote loc head))
610 "non-pair expression contains unquotes"
612 (make-const loc expr)))
614 ;;; Temporarily update a list of symbols that are handled specially
615 ;;; (disabled void check or always lexical) for compiling body. We need
616 ;;; to handle special cases for already all / set to all and the like.
618 (define (with-added-symbols loc fluid syms body)
620 (report-error loc "symbol-list construct has empty body"))
621 (if (not (or (eq? syms 'all)
622 (and (list? syms) (and-map symbol? syms))))
623 (report-error loc "invalid symbol list" syms))
624 (let ((old (fluid-ref fluid))
625 (make-body (lambda ()
626 (make-sequence loc (map compile-expr body)))))
629 (let ((new (if (eq? syms 'all)
632 (with-fluids ((fluid new))
635 ;;; Special operators
637 (defspecial progn (loc args)
638 (make-sequence loc (map compile-expr args)))
640 (defspecial if (loc args)
642 ((,cond ,then . ,else)
643 (make-conditional loc
649 (map compile-expr else)))))))
651 (defspecial defconst (loc args)
653 ((,sym ,value . ,doc)
654 (if (handle-var-def loc sym doc)
656 (list (set-variable! loc
659 (compile-expr value))
660 (make-const loc sym)))))))
662 (defspecial defvar (loc args)
664 ((,sym) (make-const loc sym))
665 ((,sym ,value . ,doc)
666 (if (handle-var-def loc sym doc)
679 (make-const loc value-slot))
680 (make-const loc sym))
683 (make-module-ref loc value-slot sym #t))
686 (set-variable! loc sym value-slot (compile-expr value)))
687 (make-const loc sym)))))))
689 (defspecial setq (loc args)
690 (define (car* x) (if (null? x) '() (car x)))
691 (define (cdr* x) (if (null? x) '() (cdr x)))
692 (define (cadr* x) (car* (cdr* x)))
693 (define (cddr* x) (cdr* (cdr* x)))
696 (let loop ((args args) (last (nil-value loc)))
699 (let ((sym (car args))
700 (val (compile-expr (cadr* args))))
701 (if (not (symbol? sym))
702 (report-error loc "expected symbol in setq")
704 (set-variable! loc sym value-slot val)
706 (reference-variable loc sym value-slot)))))))))
708 (defspecial let (loc args)
711 (generate-let loc value-slot bindings body))))
713 (defspecial lexical-let (loc args)
716 (generate-let loc 'lexical bindings body))))
718 (defspecial flet (loc args)
721 (generate-let loc function-slot bindings body))))
723 (defspecial let* (loc args)
726 (generate-let* loc value-slot bindings body))))
728 (defspecial lexical-let* (loc args)
731 (generate-let* loc 'lexical bindings body))))
733 (defspecial flet* (loc args)
736 (generate-let* loc function-slot bindings body))))
738 ;;; Temporarily set symbols as always lexical only for the lexical scope
741 (defspecial with-always-lexical (loc args)
744 (with-added-symbols loc always-lexical syms body))))
746 ;;; guile-ref allows building TreeIL's module references from within
747 ;;; elisp as a way to access data within the Guile universe. The module
748 ;;; and symbol referenced are static values, just like (@ module symbol)
751 (defspecial guile-ref (loc args)
753 ((,module ,sym) (guard (and (list? module) (symbol? sym)))
754 (make-module-ref loc module sym #t))))
756 ;;; guile-primitive allows to create primitive references, which are
757 ;;; still a little faster.
759 (defspecial guile-primitive (loc args)
762 (make-primitive-ref loc sym))))
764 ;;; A while construct is transformed into a tail-recursive loop like
767 ;;; (letrec ((iterate (lambda ()
774 ;;; As letrec is not directly accessible from elisp, while is
775 ;;; implemented here instead of with a macro.
777 (defspecial while (loc args)
779 ((,condition . ,body)
780 (let* ((itersym (gensym))
781 (compiled-body (map compile-expr body))
782 (iter-call (make-application loc
783 (make-lexical-ref loc
787 (full-body (make-sequence loc
788 `(,@compiled-body ,iter-call)))
789 (lambda-body (make-conditional loc
790 (compile-expr condition)
793 (iter-thunk (make-lambda loc
811 (defspecial function (loc args)
813 (((lambda ,args . ,body))
814 (compile-lambda loc args body))
815 ((,sym) (guard (symbol? sym))
816 (reference-variable loc sym function-slot))))
818 (defspecial defmacro (loc args)
820 ((,name ,args . ,body)
821 (if (not (symbol? name))
822 (report-error loc "expected symbol as macro name" name)
833 (make-module-ref loc '(guile) 'cons #t)
834 (list (make-const loc 'macro)
835 (compile-lambda loc args body))))
836 (make-const loc name)))))
837 (compile (ensuring-globals loc bindings-data tree-il)
842 (defspecial defun (loc args)
844 ((,name ,args . ,body)
845 (if (not (symbol? name))
846 (report-error loc "expected symbol as function name" name)
848 (list (set-variable! loc
854 (make-const loc name)))))))
856 (defspecial #{`}# (loc args)
859 (process-backquote loc val))))
861 (defspecial quote (loc args)
864 (make-const loc val))))
866 ;;; Compile a compound expression to Tree-IL.
868 (define (compile-pair loc expr)
869 (let ((operator (car expr))
870 (arguments (cdr expr)))
872 ((find-operator operator 'special-operator)
873 => (lambda (special-operator-function)
874 (special-operator-function loc arguments)))
875 ((find-operator operator 'macro)
876 => (lambda (macro-function)
877 (compile-expr (apply macro-function arguments))))
879 (make-application loc
880 (if (symbol? operator)
881 (reference-variable loc
884 (compile-expr operator))
885 (map compile-expr arguments))))))
887 ;;; Compile a symbol expression. This is a variable reference or maybe
888 ;;; some special value like nil.
890 (define (compile-symbol loc sym)
892 ((nil) (nil-value loc))
894 (else (reference-variable loc sym value-slot))))
896 ;;; Compile a single expression to TreeIL.
898 (define (compile-expr expr)
899 (let ((loc (location expr)))
902 (compile-symbol loc expr))
904 (compile-pair loc expr))
905 (else (make-const loc expr)))))
907 ;;; Process the compiler options.
908 ;;; FIXME: Why is '(()) passed as options by the REPL?
910 (define (valid-symbol-list-arg? value)
912 (and (list? value) (and-map symbol? value))))
914 (define (process-options! opt)
915 (if (and (not (null? opt))
916 (not (equal? opt '(()))))
917 (if (null? (cdr opt))
918 (report-error #f "Invalid compiler options" opt)
919 (let ((key (car opt))
922 ((#:warnings) ; ignore
925 (if (valid-symbol-list-arg? value)
926 (fluid-set! always-lexical value)
928 "Invalid value for #:always-lexical"
930 (else (report-error #f
931 "Invalid compiler option"
934 ;;; Entry point for compilation to TreeIL. This creates the bindings
935 ;;; data structure, and after compiling the main expression we need to
936 ;;; make sure all globals for symbols used during the compilation are
937 ;;; created using the generate-ensure-global function.
939 (define (compile-tree-il expr env opts)
941 (with-fluids ((bindings-data (make-bindings))
942 (disable-void-check '())
943 (always-lexical '()))
944 (process-options! opts)
945 (let ((compiled (compile-expr expr)))
946 (ensuring-globals (location expr) bindings-data compiled)))