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
44 compile-guile-primitive
51 compile-%set-lexical-binding-mode))
53 ;;; Certain common parameters (like the bindings data structure or
54 ;;; compiler options) are not always passed around but accessed using
55 ;;; fluids to simulate dynamic binding (hey, this is about elisp).
57 ;;; The bindings data structure to keep track of symbol binding related
60 (define bindings-data (make-fluid))
62 (define lexical-binding (make-fluid))
64 ;;; Find the source properties of some parsed expression if there are
65 ;;; any associated with it.
69 (let ((props (source-properties x)))
70 (and (not (null? props))
73 ;;; Values to use for Elisp's nil and t.
75 (define (nil-value loc)
76 (make-const loc (@ (language elisp runtime) nil-value)))
79 (make-const loc (@ (language elisp runtime) t-value)))
81 ;;; Modules that contain the value and function slot bindings.
83 (define runtime '(language elisp runtime))
85 (define value-slot (@ (language elisp runtime) value-slot-module))
87 (define function-slot (@ (language elisp runtime) function-slot-module))
89 ;;; The backquoting works the same as quasiquotes in Scheme, but the
90 ;;; forms are named differently; to make easy adaptions, we define these
91 ;;; predicates checking for a symbol being the car of an
92 ;;; unquote/unquote-splicing/backquote form.
94 (define (unquote? sym)
95 (and (symbol? sym) (eq? sym '#{,}#)))
97 (define (unquote-splicing? sym)
98 (and (symbol? sym) (eq? sym '#{,@}#)))
100 ;;; Build a call to a primitive procedure nicely.
102 (define (call-primitive loc sym . args)
103 (make-application loc (make-primitive-ref loc sym) args))
105 ;;; Error reporting routine for syntax/compilation problems or build
106 ;;; code for a runtime-error output.
108 (define (report-error loc . args)
111 ;;; Generate code to ensure a global symbol is there for further use of
112 ;;; a given symbol. In general during the compilation, those needed are
113 ;;; only tracked with the bindings data structure. Afterwards, however,
114 ;;; for all those needed symbols the globals are really generated with
117 (define (generate-ensure-global loc sym module)
118 (make-application loc
119 (make-module-ref loc runtime 'ensure-fluid! #t)
120 (list (make-const loc module)
121 (make-const loc sym))))
123 (define (ensuring-globals loc bindings body)
126 `(,@(map-globals (fluid-ref bindings)
128 (generate-ensure-global loc sym mod)))
131 ;;; Build a construct that establishes dynamic bindings for certain
132 ;;; variables. We may want to choose between binding with fluids and
133 ;;; with-fluids* and using just ordinary module symbols and
134 ;;; setting/reverting their values with a dynamic-wind.
136 (define (let-dynamic loc syms module vals body)
140 (make-application loc
141 (make-primitive-ref loc 'list)
143 (make-module-ref loc module sym #t))
145 (make-application loc (make-primitive-ref loc 'list) vals)
148 (make-lambda-case #f '() #f #f #f '() '() body #f))))
150 ;;; Handle access to a variable (reference/setting) correctly depending
151 ;;; on whether it is currently lexically or dynamically bound. lexical
152 ;;; access is done only for references to the value-slot module!
154 (define (access-variable loc
160 (let ((lexical (get-lexical-binding (fluid-ref bindings-data) sym)))
162 (lexical (handle-lexical lexical))
163 ((equal? module function-slot) (handle-global))
164 (else (handle-dynamic)))))
166 ;;; Generate code to reference a variable. For references in the
167 ;;; value-slot module, we may want to generate a lexical reference
168 ;;; instead if the variable has a lexical binding.
170 (define (reference-variable loc sym module)
175 (lambda () (make-module-ref loc module sym #t))
176 (lambda (lexical) (make-lexical-ref loc lexical lexical))
178 (mark-global! (fluid-ref bindings-data) sym module)
181 (make-module-ref loc module sym #t)))))
183 ;;; Generate code to set a variable. Just as with reference-variable, in
184 ;;; case of a reference to value-slot, we want to generate a lexical set
185 ;;; when the variable has a lexical binding.
187 (define (set-variable! loc sym module value)
195 (make-module-ref loc runtime 'set-symbol-function! #t) ;++ fix
196 (list (make-const loc sym) value)))
197 (lambda (lexical) (make-lexical-set loc lexical lexical value))
199 (mark-global! (fluid-ref bindings-data) sym module)
202 (make-module-ref loc module sym #t)
205 (define (bind-lexically? sym module decls)
206 (or (eq? module function-slot)
207 (let ((decl (assq-ref decls sym)))
208 (and (equal? module value-slot)
212 (fluid-ref lexical-binding)
213 (not (global? (fluid-ref bindings-data) sym module))))))))
215 (define (parse-let-binding loc binding)
218 (guard (symbol? var))
221 (guard (symbol? var))
224 (guard (symbol? var))
227 (report-error loc "malformed variable binding" binding))))
229 (define (parse-flet-binding loc binding)
231 ((,var ,args . ,body)
232 (guard (symbol? var))
233 (cons var `(function (lambda ,args ,@body))))
235 (report-error loc "malformed function binding" binding))))
237 (define (parse-declaration expr)
240 (map (cut cons <> 'lexical) vars))
244 (define (parse-body-1 body lambda?)
245 (let loop ((lst body)
250 (((declare . ,x) . ,tail)
251 (loop tail (append-reverse x decls) intspec doc))
252 (((interactive . ,x) . ,tail)
253 (guard lambda? (not intspec))
254 (loop tail decls x doc))
256 (guard lambda? (string? x) (not doc) (not (null? tail)))
257 (loop tail decls intspec x))
259 (values (append-map parse-declaration decls)
264 (define (parse-lambda-body body)
265 (parse-body-1 body #t))
267 (define (parse-body body)
268 (receive (decls intspec doc body) (parse-body-1 body #f)
269 (values decls body)))
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 (receive (decls forms) (parse-body body)
279 (receive (lexical dynamic)
280 (partition (compose (cut bind-lexically? <> module decls)
283 (for-each (lambda (sym)
284 (mark-global! (fluid-ref bindings-data)
288 (let ((make-values (lambda (for)
289 (map (lambda (el) (compile-expr (cdr el)))
291 (make-body (lambda () (compile-expr `(progn ,@forms)))))
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 (receive (decls forms) (parse-body body)
327 (for-each (lambda (sym)
328 (if (not (bind-lexically? sym module decls))
329 (mark-global! (fluid-ref bindings-data)
333 (let iterate ((tail bindings))
335 (compile-expr `(progn ,@forms))
336 (let ((sym (caar tail))
337 (value (compile-expr (cdar tail))))
338 (if (bind-lexically? sym module decls)
339 (let ((target (gensym)))
344 (with-lexical-bindings
345 (fluid-ref bindings-data)
348 (lambda () (iterate (cdr tail))))))
353 (iterate (cdr tail))))))))))
355 ;;; Partition the argument list of a lambda expression into required,
356 ;;; optional and rest arguments.
358 (define (parse-lambda-list lst)
359 (define (%match lst null optional rest symbol)
362 ((&optional . ,tail) (optional tail))
363 ((&rest . ,tail) (rest tail))
364 ((,arg . ,tail) (guard (symbol? arg)) (symbol arg tail))
366 (define (return rreq ropt rest)
367 (values #t (reverse rreq) (reverse ropt) rest))
369 (values #f #f #f #f))
370 (define (parse-req lst rreq)
372 (lambda () (return rreq '() #f))
373 (lambda (tail) (parse-opt tail rreq '()))
374 (lambda (tail) (parse-rest tail rreq '()))
375 (lambda (arg tail) (parse-req tail (cons arg rreq)))))
376 (define (parse-opt lst rreq ropt)
378 (lambda () (return rreq ropt #f))
379 (lambda (tail) (fail))
380 (lambda (tail) (parse-rest tail rreq ropt))
381 (lambda (arg tail) (parse-opt tail rreq (cons arg ropt)))))
382 (define (parse-rest lst rreq ropt)
385 (lambda (tail) (fail))
386 (lambda (tail) (fail))
387 (lambda (arg tail) (parse-post-rest tail rreq ropt arg))))
388 (define (parse-post-rest lst rreq ropt rest)
390 (lambda () (return rreq ropt rest))
393 (lambda (arg tail) (fail))))
396 (define (make-simple-lambda loc meta req opt init rest vars body)
399 (make-lambda-case #f req opt rest #f init vars body #f)))
401 (define (compile-lambda loc meta args body)
402 (receive (valid? req-ids opt-ids rest-id)
403 (parse-lambda-list args)
405 (let* ((all-ids (append req-ids
407 (or (and=> rest-id list) '())))
408 (all-vars (map (lambda (ignore) (gensym)) all-ids)))
409 (let*-values (((decls intspec doc forms)
410 (parse-lambda-body body))
413 (compose (cut bind-lexically? <> value-slot decls)
415 (map list all-ids all-vars)))
416 ((lexical-ids lexical-vars) (unzip2 lexical))
417 ((dynamic-ids dynamic-vars) (unzip2 dynamic)))
418 (with-dynamic-bindings
419 (fluid-ref bindings-data)
422 (with-lexical-bindings
423 (fluid-ref bindings-data)
427 (let* ((tree-il (compile-expr `(progn ,@forms)))
434 (map (cut make-lexical-ref
441 (make-simple-lambda loc
445 (map (const (nil-value loc))
450 (report-error "invalid function" `(lambda ,args ,@body)))))
452 ;;; Handle the common part of defconst and defvar, that is, checking for
453 ;;; a correct doc string and arguments as well as maybe in the future
454 ;;; handling the docstring somehow.
456 (define (handle-var-def loc sym doc)
458 ((not (symbol? sym)) (report-error loc "expected symbol, got" sym))
459 ((> (length doc) 1) (report-error loc "too many arguments to defvar"))
460 ((and (not (null? doc)) (not (string? (car doc))))
461 (report-error loc "expected string as third argument of defvar, got"
463 ;; TODO: Handle doc string if present.
466 ;;; Handle macro and special operator bindings.
468 (define (find-operator name type)
471 (module-defined? (resolve-interface function-slot) name)
472 (let ((op (module-ref (resolve-module function-slot) name)))
473 (if (and (pair? op) (eq? (car op) type))
477 ;;; See if a (backquoted) expression contains any unquotes.
479 (define (contains-unquotes? expr)
481 (if (or (unquote? (car expr)) (unquote-splicing? (car expr)))
483 (or (contains-unquotes? (car expr))
484 (contains-unquotes? (cdr expr))))
487 ;;; Process a backquoted expression by building up the needed
488 ;;; cons/append calls. For splicing, it is assumed that the expression
489 ;;; spliced in evaluates to a list. The emacs manual does not really
490 ;;; state either it has to or what to do if it does not, but Scheme
491 ;;; explicitly forbids it and this seems reasonable also for elisp.
493 (define (unquote-cell? expr)
494 (and (list? expr) (= (length expr) 2) (unquote? (car expr))))
496 (define (unquote-splicing-cell? expr)
497 (and (list? expr) (= (length expr) 2) (unquote-splicing? (car expr))))
499 (define (process-backquote loc expr)
500 (if (contains-unquotes? expr)
502 (if (or (unquote-cell? expr) (unquote-splicing-cell? expr))
503 (compile-expr (cadr expr))
504 (let* ((head (car expr))
505 (processed-tail (process-backquote loc (cdr expr)))
506 (head-is-list-2 (and (list? head)
507 (= (length head) 2)))
508 (head-unquote (and head-is-list-2
509 (unquote? (car head))))
510 (head-unquote-splicing (and head-is-list-2
513 (if head-unquote-splicing
516 (compile-expr (cadr head))
518 (call-primitive loc 'cons
520 (compile-expr (cadr head))
521 (process-backquote loc head))
524 "non-pair expression contains unquotes"
526 (make-const loc expr)))
528 ;;; Special operators
530 (defspecial progn (loc args)
533 (list (nil-value loc))
534 (map compile-expr args))))
536 (defspecial eval-when-compile (loc args)
537 (make-const loc (compile `(progn ,@args) #:from 'elisp #:to 'value)))
539 (defspecial if (loc args)
541 ((,cond ,then . ,else)
544 (call-primitive loc 'not
545 (call-primitive loc 'nil? (compile-expr cond)))
547 (compile-expr `(progn ,@else))))))
549 (defspecial defconst (loc args)
551 ((,sym ,value . ,doc)
552 (if (handle-var-def loc sym doc)
554 (list (set-variable! loc
557 (compile-expr value))
558 (make-const loc sym)))))))
560 (defspecial defvar (loc args)
562 ((,sym) (make-const loc sym))
563 ((,sym ,value . ,doc)
564 (if (handle-var-def loc sym doc)
577 (make-const loc value-slot))
578 (make-const loc sym))
581 (make-module-ref loc value-slot sym #t))
584 (set-variable! loc sym value-slot (compile-expr value)))
585 (make-const loc sym)))))))
587 (defspecial setq (loc args)
588 (define (car* x) (if (null? x) '() (car x)))
589 (define (cdr* x) (if (null? x) '() (cdr x)))
590 (define (cadr* x) (car* (cdr* x)))
591 (define (cddr* x) (cdr* (cdr* x)))
594 (let loop ((args args) (last (nil-value loc)))
597 (let ((sym (car args))
598 (val (compile-expr (cadr* args))))
599 (if (not (symbol? sym))
600 (report-error loc "expected symbol in setq")
602 (set-variable! loc sym value-slot val)
604 (reference-variable loc sym value-slot)))))))))
606 (defspecial let (loc args)
611 (map (cut parse-let-binding loc <>) bindings)
614 (defspecial flet (loc args)
617 (let ((names+vals (map (cut parse-flet-binding loc <>) bindings)))
618 (receive (decls forms) (parse-body body)
619 (let ((names (map car names+vals))
620 (vals (map cdr names+vals))
621 (gensyms (map (lambda (x) (gensym)) names+vals)))
622 (with-lexical-bindings
623 (fluid-ref bindings-data)
630 (map compile-expr vals)
631 (compile-expr `(progn ,@forms)))))))))))
633 (defspecial labels (loc args)
636 (let ((names+vals (map (cut parse-flet-binding loc <>) bindings)))
637 (receive (decls forms) (parse-body body)
638 (let ((names (map car names+vals))
639 (vals (map cdr names+vals))
640 (gensyms (map (lambda (x) (gensym)) names+vals)))
641 (with-lexical-bindings
642 (fluid-ref bindings-data)
650 (map compile-expr vals)
651 (compile-expr `(progn ,@forms)))))))))))
653 (defspecial let* (loc args)
658 (map (cut parse-let-binding loc <>) bindings)
661 ;;; guile-ref allows building TreeIL's module references from within
662 ;;; elisp as a way to access data within the Guile universe. The module
663 ;;; and symbol referenced are static values, just like (@ module symbol)
666 (defspecial guile-ref (loc args)
668 ((,module ,sym) (guard (and (list? module) (symbol? sym)))
669 (make-module-ref loc module sym #t))))
671 ;;; guile-primitive allows to create primitive references, which are
672 ;;; still a little faster.
674 (defspecial guile-primitive (loc args)
677 (make-primitive-ref loc sym))))
679 (defspecial function (loc args)
681 (((lambda ,args . ,body))
682 (compile-lambda loc '() args body))
683 ((,sym) (guard (symbol? sym))
684 (reference-variable loc sym function-slot))))
686 (defspecial defmacro (loc args)
688 ((,name ,args . ,body)
689 (if (not (symbol? name))
690 (report-error loc "expected symbol as macro name" name)
701 (make-module-ref loc '(guile) 'cons #t)
702 (list (make-const loc 'macro)
707 (make-const loc name)))))
708 (compile (ensuring-globals loc bindings-data tree-il)
713 (defspecial defun (loc args)
715 ((,name ,args . ,body)
716 (if (not (symbol? name))
717 (report-error loc "expected symbol as function name" name)
719 (list (set-variable! loc
726 (make-const loc name)))))))
728 (defspecial #{`}# (loc args)
731 (process-backquote loc val))))
733 (defspecial quote (loc args)
736 (make-const loc val))))
738 (defspecial %funcall (loc args)
740 ((,function . ,arguments)
741 (make-application loc
742 (compile-expr function)
743 (map compile-expr arguments)))))
745 (defspecial %set-lexical-binding-mode (loc args)
748 (fluid-set! lexical-binding val)
751 ;;; Compile a compound expression to Tree-IL.
753 (define (compile-pair loc expr)
754 (let ((operator (car expr))
755 (arguments (cdr expr)))
757 ((find-operator operator 'special-operator)
758 => (lambda (special-operator-function)
759 (special-operator-function loc arguments)))
760 ((find-operator operator 'macro)
761 => (lambda (macro-function)
762 (compile-expr (apply macro-function arguments))))
764 (compile-expr `(%funcall (function ,operator) ,@arguments))))))
766 ;;; Compile a symbol expression. This is a variable reference or maybe
767 ;;; some special value like nil.
769 (define (compile-symbol loc sym)
771 ((nil) (nil-value loc))
773 (else (reference-variable loc sym value-slot))))
775 ;;; Compile a single expression to TreeIL.
777 (define (compile-expr expr)
778 (let ((loc (location expr)))
781 (compile-symbol loc expr))
783 (compile-pair loc expr))
784 (else (make-const loc expr)))))
786 ;;; Process the compiler options.
787 ;;; FIXME: Why is '(()) passed as options by the REPL?
789 (define (valid-symbol-list-arg? value)
791 (and (list? value) (and-map symbol? value))))
793 (define (process-options! opt)
794 (if (and (not (null? opt))
795 (not (equal? opt '(()))))
796 (if (null? (cdr opt))
797 (report-error #f "Invalid compiler options" opt)
798 (let ((key (car opt))
801 ((#:warnings) ; ignore
803 (else (report-error #f
804 "Invalid compiler option"
807 ;;; Entry point for compilation to TreeIL. This creates the bindings
808 ;;; data structure, and after compiling the main expression we need to
809 ;;; make sure all globals for symbols used during the compilation are
810 ;;; created using the generate-ensure-global function.
812 (define (compile-tree-il expr env opts)
814 (with-fluids ((bindings-data (make-bindings)))
815 (process-options! opts)
816 (let ((compiled (compile-expr expr)))
817 (ensuring-globals (location expr) bindings-data compiled)))