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
34 compile-eval-when-compile
45 compile-guile-primitive
53 compile-%set-lexical-binding-mode))
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 (define lexical-binding (make-fluid))
66 ;;; Find the source properties of some parsed expression if there are
67 ;;; any associated with it.
71 (let ((props (source-properties x)))
72 (and (not (null? props))
75 ;;; Values to use for Elisp's nil and t.
77 (define (nil-value loc)
78 (make-const loc (@ (language elisp runtime) nil-value)))
81 (make-const loc (@ (language elisp runtime) t-value)))
83 ;;; Modules that contain the value and function slot bindings.
85 (define runtime '(language elisp runtime))
87 (define value-slot (@ (language elisp runtime) value-slot-module))
89 (define function-slot (@ (language elisp runtime) function-slot-module))
91 ;;; The backquoting works the same as quasiquotes in Scheme, but the
92 ;;; forms are named differently; to make easy adaptions, we define these
93 ;;; predicates checking for a symbol being the car of an
94 ;;; unquote/unquote-splicing/backquote form.
96 (define (unquote? sym)
97 (and (symbol? sym) (eq? sym '#{,}#)))
99 (define (unquote-splicing? sym)
100 (and (symbol? sym) (eq? sym '#{,@}#)))
102 ;;; Build a call to a primitive procedure nicely.
104 (define (call-primitive loc sym . args)
105 (make-application loc (make-primitive-ref loc sym) args))
107 ;;; Error reporting routine for syntax/compilation problems or build
108 ;;; code for a runtime-error output.
110 (define (report-error loc . args)
113 ;;; Generate code to ensure a global symbol is there for further use of
114 ;;; a given symbol. In general during the compilation, those needed are
115 ;;; only tracked with the bindings data structure. Afterwards, however,
116 ;;; for all those needed symbols the globals are really generated with
119 (define (generate-ensure-global loc sym module)
120 (make-application loc
121 (make-module-ref loc runtime 'ensure-fluid! #t)
122 (list (make-const loc module)
123 (make-const loc sym))))
125 (define (ensuring-globals loc bindings body)
128 `(,@(map-globals (fluid-ref bindings)
130 (generate-ensure-global loc sym mod)))
133 ;;; Build a construct that establishes dynamic bindings for certain
134 ;;; variables. We may want to choose between binding with fluids and
135 ;;; with-fluids* and using just ordinary module symbols and
136 ;;; setting/reverting their values with a dynamic-wind.
138 (define (let-dynamic loc syms module vals body)
142 (make-application loc
143 (make-primitive-ref loc 'list)
145 (make-module-ref loc module sym #t))
147 (make-application loc (make-primitive-ref loc 'list) vals)
150 (make-lambda-case #f '() #f #f #f '() '() body #f))))
152 ;;; Handle access to a variable (reference/setting) correctly depending
153 ;;; on whether it is currently lexically or dynamically bound. lexical
154 ;;; access is done only for references to the value-slot module!
156 (define (access-variable loc
162 (let ((lexical (get-lexical-binding (fluid-ref bindings-data) sym)))
164 (lexical (handle-lexical lexical))
165 ((equal? module function-slot) (handle-global))
166 (else (handle-dynamic)))))
168 ;;; Generate code to reference a variable. For references in the
169 ;;; value-slot module, we may want to generate a lexical reference
170 ;;; instead if the variable has a lexical binding.
172 (define (reference-variable loc sym module)
177 (lambda () (make-module-ref loc module sym #t))
178 (lambda (lexical) (make-lexical-ref loc lexical lexical))
180 (mark-global! (fluid-ref bindings-data) sym module)
183 (make-module-ref loc module sym #t)))))
185 ;;; Generate code to set a variable. Just as with reference-variable, in
186 ;;; case of a reference to value-slot, we want to generate a lexical set
187 ;;; when the variable has a lexical binding.
189 (define (set-variable! loc sym module value)
197 (make-module-ref loc runtime 'set-symbol-function! #t) ;++ fix
198 (list (make-const loc sym) value)))
199 (lambda (lexical) (make-lexical-set loc lexical lexical value))
201 (mark-global! (fluid-ref bindings-data) sym module)
204 (make-module-ref loc module sym #t)
207 ;;; Process the bindings part of a let or let* expression; that is,
208 ;;; check for correctness and bring it to the form ((sym1 . val1) (sym2
211 (define (process-let-bindings loc bindings)
216 (if (or (not (list? b))
217 (not (= (length b) 2)))
220 "expected symbol or list of 2 elements in let")
221 (if (not (symbol? (car b)))
222 (report-error loc "expected symbol in let")
223 (cons (car b) (cadr b))))))
226 ;;; Split the let bindings into a list to be done lexically and one
227 ;;; dynamically. A symbol will be bound lexically if and only if: We're
228 ;;; processing a lexical-let (i.e. module is 'lexical), OR we're
229 ;;; processing a value-slot binding AND the symbol is already lexically
230 ;;; bound or is always lexical, OR we're processing a function-slot
233 (define (bind-lexically? sym module)
234 (or (eq? module 'lexical)
235 (eq? module function-slot)
236 (and (equal? module value-slot)
237 (or (get-lexical-binding (fluid-ref bindings-data) sym)
239 (fluid-ref lexical-binding)
240 (not (global? (fluid-ref bindings-data) sym module)))))))
242 (define (split-let-bindings bindings module)
243 (let iterate ((tail bindings)
247 (values (reverse lexical) (reverse dynamic))
248 (if (bind-lexically? (caar tail) module)
249 (iterate (cdr tail) (cons (car tail) lexical) dynamic)
250 (iterate (cdr tail) lexical (cons (car tail) dynamic))))))
252 ;;; Compile let and let* expressions. The code here is used both for
253 ;;; let/let* and flet, just with a different bindings module.
255 ;;; A special module value 'lexical means that we're doing a lexical-let
256 ;;; instead and the bindings should not be saved to globals at all but
257 ;;; be done with the lexical framework instead.
259 ;;; Let is done with a single call to let-dynamic binding them locally
260 ;;; to new values all "at once". If there is at least one variable to
261 ;;; bind lexically among the bindings, we first do a let for all of them
262 ;;; to evaluate all values before any bindings take place, and then call
263 ;;; let-dynamic for the variables to bind dynamically.
265 (define (generate-let loc module bindings body)
266 (let ((bind (process-let-bindings loc bindings)))
268 (lambda () (split-let-bindings bind module))
269 (lambda (lexical dynamic)
270 (for-each (lambda (sym)
271 (mark-global! (fluid-ref bindings-data)
275 (let ((make-values (lambda (for)
276 (map (lambda (el) (compile-expr (cdr el)))
278 (make-body (lambda () (compile-expr `(progn ,@body)))))
280 (let-dynamic loc (map car dynamic) module
281 (make-values dynamic) (make-body))
282 (let* ((lexical-syms (map (lambda (el) (gensym)) lexical))
283 (dynamic-syms (map (lambda (el) (gensym)) dynamic))
284 (all-syms (append lexical-syms dynamic-syms))
285 (vals (append (make-values lexical)
286 (make-values dynamic))))
291 (with-lexical-bindings
292 (fluid-ref bindings-data)
293 (map car lexical) lexical-syms
302 (make-lexical-ref loc
306 (make-body)))))))))))))
308 ;;; Let* is compiled to a cascaded set of "small lets" for each binding
309 ;;; in turn so that each one already sees the preceding bindings.
311 (define (generate-let* loc module bindings body)
312 (let ((bind (process-let-bindings loc bindings)))
314 (for-each (lambda (sym)
315 (if (not (bind-lexically? sym module))
316 (mark-global! (fluid-ref bindings-data)
320 (let iterate ((tail bind))
322 (compile-expr `(progn ,@body))
323 (let ((sym (caar tail))
324 (value (compile-expr (cdar tail))))
325 (if (bind-lexically? sym module)
326 (let ((target (gensym)))
331 (with-lexical-bindings
332 (fluid-ref bindings-data)
335 (lambda () (iterate (cdr tail))))))
340 (iterate (cdr tail))))))))))
342 ;;; Split the argument list of a lambda expression into required,
343 ;;; optional and rest arguments and also check it is actually valid.
344 ;;; Additionally, we create a list of all "local variables" (that is,
345 ;;; required, optional and rest arguments together) and also this one
346 ;;; split into those to be bound lexically and dynamically. Returned is
347 ;;; as multiple values: required optional rest lexical dynamic
349 (define (split-lambda-arguments loc args)
350 (let iterate ((tail args)
358 (let ((final-required (reverse required))
359 (final-optional (reverse optional))
360 (final-lexical (reverse lexical))
361 (final-dynamic (reverse dynamic)))
362 (values final-required
367 ((and (eq? mode 'required)
368 (eq? (car tail) '&optional))
369 (iterate (cdr tail) 'optional required optional lexical dynamic))
370 ((eq? (car tail) '&rest)
371 (if (or (null? (cdr tail))
372 (not (null? (cddr tail))))
373 (report-error loc "expected exactly one symbol after &rest")
374 (let* ((rest (cadr tail))
375 (rest-lexical (bind-lexically? rest value-slot))
376 (final-required (reverse required))
377 (final-optional (reverse optional))
378 (final-lexical (reverse (if rest-lexical
381 (final-dynamic (reverse (if rest-lexical
383 (cons rest dynamic)))))
384 (values final-required
390 (if (not (symbol? (car tail)))
392 "expected symbol in argument list, got"
394 (let* ((arg (car tail))
395 (bind-lexical (bind-lexically? arg value-slot))
396 (new-lexical (if bind-lexical
399 (new-dynamic (if bind-lexical
401 (cons arg dynamic))))
403 ((required) (iterate (cdr tail) mode
404 (cons arg required) optional
405 new-lexical new-dynamic))
406 ((optional) (iterate (cdr tail) mode
407 required (cons arg optional)
408 new-lexical new-dynamic))
410 (error "invalid mode in split-lambda-arguments"
413 ;;; Compile a lambda expression. One thing we have to be aware of is
414 ;;; that lambda arguments are usually dynamically bound, even when a
415 ;;; lexical binding is intact for a symbol. For symbols that are marked
416 ;;; as 'always lexical,' however, we lexically bind here as well, and
417 ;;; thus we get them out of the let-dynamic call and register a lexical
418 ;;; binding for them (the lexical target variable is already there,
419 ;;; namely the real lambda argument from TreeIL).
421 (define (compile-lambda loc meta args body)
422 (if (not (list? args))
423 (report-error loc "expected list for argument-list" args))
424 (receive (required optional rest lexical dynamic)
425 (split-lambda-arguments loc args)
426 (define (process-args args)
427 (define (find-pairs pairs filter)
428 (lset-intersection (lambda (name+sym x)
429 (eq? (car name+sym) x))
432 (let* ((syms (map (lambda (x) (gensym)) args))
433 (pairs (map cons args syms))
434 (lexical-pairs (find-pairs pairs lexical))
435 (dynamic-pairs (find-pairs pairs dynamic)))
436 (values syms pairs lexical-pairs dynamic-pairs)))
437 (let*-values (((required-syms
441 (process-args required))
446 (process-args optional))
447 ((rest-syms rest-pairs rest-lex-pairs rest-dyn-pairs)
448 (process-args (if rest (list rest) '())))
449 ((the-rest-sym) (if rest (car rest-syms) #f))
450 ((all-syms) (append required-syms
453 ((all-lex-pairs) (append required-lex-pairs
456 ((all-dyn-pairs) (append required-dyn-pairs
459 (for-each (lambda (sym)
460 (mark-global! (fluid-ref bindings-data)
464 (with-dynamic-bindings
465 (fluid-ref bindings-data)
468 (with-lexical-bindings
469 (fluid-ref bindings-data)
470 (map car all-lex-pairs)
471 (map cdr all-lex-pairs)
482 (map (lambda (x) (nil-value loc)) optional)
484 (let ((compiled-body (compile-expr `(progn ,@body))))
493 (make-lexical-ref loc
496 (make-lexical-set loc
507 (map (lambda (name-sym)
516 ;;; Handle the common part of defconst and defvar, that is, checking for
517 ;;; a correct doc string and arguments as well as maybe in the future
518 ;;; handling the docstring somehow.
520 (define (handle-var-def loc sym doc)
522 ((not (symbol? sym)) (report-error loc "expected symbol, got" sym))
523 ((> (length doc) 1) (report-error loc "too many arguments to defvar"))
524 ((and (not (null? doc)) (not (string? (car doc))))
525 (report-error loc "expected string as third argument of defvar, got"
527 ;; TODO: Handle doc string if present.
530 ;;; Handle macro and special operator bindings.
532 (define (find-operator sym type)
535 (module-defined? (resolve-interface function-slot) sym)
536 (let* ((op (module-ref (resolve-module function-slot) sym))
537 (op (if (fluid? op) (fluid-ref op) op)))
538 (if (and (pair? op) (eq? (car op) type))
542 ;;; See if a (backquoted) expression contains any unquotes.
544 (define (contains-unquotes? expr)
546 (if (or (unquote? (car expr)) (unquote-splicing? (car expr)))
548 (or (contains-unquotes? (car expr))
549 (contains-unquotes? (cdr expr))))
552 ;;; Process a backquoted expression by building up the needed
553 ;;; cons/append calls. For splicing, it is assumed that the expression
554 ;;; spliced in evaluates to a list. The emacs manual does not really
555 ;;; state either it has to or what to do if it does not, but Scheme
556 ;;; explicitly forbids it and this seems reasonable also for elisp.
558 (define (unquote-cell? expr)
559 (and (list? expr) (= (length expr) 2) (unquote? (car expr))))
561 (define (unquote-splicing-cell? expr)
562 (and (list? expr) (= (length expr) 2) (unquote-splicing? (car expr))))
564 (define (process-backquote loc expr)
565 (if (contains-unquotes? expr)
567 (if (or (unquote-cell? expr) (unquote-splicing-cell? expr))
568 (compile-expr (cadr expr))
569 (let* ((head (car expr))
570 (processed-tail (process-backquote loc (cdr expr)))
571 (head-is-list-2 (and (list? head)
572 (= (length head) 2)))
573 (head-unquote (and head-is-list-2
574 (unquote? (car head))))
575 (head-unquote-splicing (and head-is-list-2
578 (if head-unquote-splicing
581 (compile-expr (cadr head))
583 (call-primitive loc 'cons
585 (compile-expr (cadr head))
586 (process-backquote loc head))
589 "non-pair expression contains unquotes"
591 (make-const loc expr)))
593 ;;; Special operators
595 (defspecial progn (loc args)
598 (list (nil-value loc))
599 (map compile-expr args))))
601 (defspecial eval-when-compile (loc args)
602 (make-const loc (compile `(progn ,@args) #:from 'elisp #:to 'value)))
604 (defspecial if (loc args)
606 ((,cond ,then . ,else)
609 (call-primitive loc 'not
610 (call-primitive loc 'nil? (compile-expr cond)))
612 (compile-expr `(progn ,@else))))))
614 (defspecial defconst (loc args)
616 ((,sym ,value . ,doc)
617 (if (handle-var-def loc sym doc)
619 (list (set-variable! loc
622 (compile-expr value))
623 (make-const loc sym)))))))
625 (defspecial defvar (loc args)
627 ((,sym) (make-const loc sym))
628 ((,sym ,value . ,doc)
629 (if (handle-var-def loc sym doc)
642 (make-const loc value-slot))
643 (make-const loc sym))
646 (make-module-ref loc value-slot sym #t))
649 (set-variable! loc sym value-slot (compile-expr value)))
650 (make-const loc sym)))))))
652 (defspecial setq (loc args)
653 (define (car* x) (if (null? x) '() (car x)))
654 (define (cdr* x) (if (null? x) '() (cdr x)))
655 (define (cadr* x) (car* (cdr* x)))
656 (define (cddr* x) (cdr* (cdr* x)))
659 (let loop ((args args) (last (nil-value loc)))
662 (let ((sym (car args))
663 (val (compile-expr (cadr* args))))
664 (if (not (symbol? sym))
665 (report-error loc "expected symbol in setq")
667 (set-variable! loc sym value-slot val)
669 (reference-variable loc sym value-slot)))))))))
671 (defspecial let (loc args)
674 (generate-let loc value-slot bindings body))))
676 (defspecial lexical-let (loc args)
679 (generate-let loc 'lexical bindings body))))
681 (defspecial flet (loc args)
684 (generate-let loc function-slot bindings body))))
686 (defspecial let* (loc args)
689 (generate-let* loc value-slot bindings body))))
691 (defspecial lexical-let* (loc args)
694 (generate-let* loc 'lexical bindings body))))
696 ;;; guile-ref allows building TreeIL's module references from within
697 ;;; elisp as a way to access data within the Guile universe. The module
698 ;;; and symbol referenced are static values, just like (@ module symbol)
701 (defspecial guile-ref (loc args)
703 ((,module ,sym) (guard (and (list? module) (symbol? sym)))
704 (make-module-ref loc module sym #t))))
706 ;;; guile-primitive allows to create primitive references, which are
707 ;;; still a little faster.
709 (defspecial guile-primitive (loc args)
712 (make-primitive-ref loc sym))))
714 ;;; A while construct is transformed into a tail-recursive loop like
717 ;;; (letrec ((iterate (lambda ()
724 ;;; As letrec is not directly accessible from elisp, while is
725 ;;; implemented here instead of with a macro.
727 (defspecial while (loc args)
729 ((,condition . ,body)
730 (let* ((itersym (gensym))
731 (compiled-body (map compile-expr body))
732 (iter-call (make-application loc
733 (make-lexical-ref loc
737 (full-body (make-sequence loc
738 `(,@compiled-body ,iter-call)))
739 (lambda-body (make-conditional loc
740 (compile-expr condition)
743 (iter-thunk (make-lambda loc
761 (defspecial function (loc args)
763 (((lambda ,args . ,body))
764 (compile-lambda loc '() args body))
765 ((,sym) (guard (symbol? sym))
766 (reference-variable loc sym function-slot))))
768 (defspecial defmacro (loc args)
770 ((,name ,args . ,body)
771 (if (not (symbol? name))
772 (report-error loc "expected symbol as macro name" name)
783 (make-module-ref loc '(guile) 'cons #t)
784 (list (make-const loc 'macro)
789 (make-const loc name)))))
790 (compile (ensuring-globals loc bindings-data tree-il)
795 (defspecial defun (loc args)
797 ((,name ,args . ,body)
798 (if (not (symbol? name))
799 (report-error loc "expected symbol as function name" name)
801 (list (set-variable! loc
808 (make-const loc name)))))))
810 (defspecial #{`}# (loc args)
813 (process-backquote loc val))))
815 (defspecial quote (loc args)
818 (make-const loc val))))
820 (defspecial %funcall (loc args)
822 ((,function . ,arguments)
823 (make-application loc
824 (compile-expr function)
825 (map compile-expr arguments)))))
827 (defspecial %set-lexical-binding-mode (loc args)
830 (fluid-set! lexical-binding val)
833 ;;; Compile a compound expression to Tree-IL.
835 (define (compile-pair loc expr)
836 (let ((operator (car expr))
837 (arguments (cdr expr)))
839 ((find-operator operator 'special-operator)
840 => (lambda (special-operator-function)
841 (special-operator-function loc arguments)))
842 ((find-operator operator 'macro)
843 => (lambda (macro-function)
844 (compile-expr (apply macro-function arguments))))
846 (make-application loc
847 (compile-expr `(function ,operator))
848 (map compile-expr arguments))))))
850 ;;; Compile a symbol expression. This is a variable reference or maybe
851 ;;; some special value like nil.
853 (define (compile-symbol loc sym)
855 ((nil) (nil-value loc))
857 (else (reference-variable loc sym value-slot))))
859 ;;; Compile a single expression to TreeIL.
861 (define (compile-expr expr)
862 (let ((loc (location expr)))
865 (compile-symbol loc expr))
867 (compile-pair loc expr))
868 (else (make-const loc expr)))))
870 ;;; Process the compiler options.
871 ;;; FIXME: Why is '(()) passed as options by the REPL?
873 (define (valid-symbol-list-arg? value)
875 (and (list? value) (and-map symbol? value))))
877 (define (process-options! opt)
878 (if (and (not (null? opt))
879 (not (equal? opt '(()))))
880 (if (null? (cdr opt))
881 (report-error #f "Invalid compiler options" opt)
882 (let ((key (car opt))
885 ((#:warnings) ; ignore
887 (else (report-error #f
888 "Invalid compiler option"
891 ;;; Entry point for compilation to TreeIL. This creates the bindings
892 ;;; data structure, and after compiling the main expression we need to
893 ;;; make sure all globals for symbols used during the compilation are
894 ;;; created using the generate-ensure-global function.
896 (define (compile-tree-il expr env opts)
898 (with-fluids ((bindings-data (make-bindings)))
899 (process-options! opts)
900 (let ((compiled (compile-expr expr)))
901 (ensuring-globals (location expr) bindings-data compiled)))