3 ;; Copyright (C) 2009, 2010, 2011, 2013 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 #:use-module (ice-9 format)
33 #:use-module (language tree-il eval)
34 #:export (compile-tree-il
36 compile-eval-when-compile
46 compile-guile-private-ref
47 compile-guile-primitive
55 compile-%set-lexical-binding-mode))
57 ;;; Certain common parameters (like the bindings data structure or
58 ;;; compiler options) are not always passed around but accessed using
59 ;;; fluids to simulate dynamic binding (hey, this is about elisp).
61 ;;; The bindings data structure to keep track of symbol binding related
64 (define bindings-data (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-primcall 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 (define (access-variable loc symbol handle-lexical handle-dynamic)
115 ((get-lexical-binding (fluid-ref bindings-data) symbol)
120 (define (reference-variable loc symbol)
125 (if (symbol? lexical)
126 (make-lexical-ref loc symbol lexical)
127 (make-call loc lexical '())))
130 (make-module-ref loc runtime 'symbol-value #t)
131 (list (make-const loc symbol))))))
133 (define (global? symbol)
134 (module-variable value-slot symbol))
136 (define (ensure-globals! loc names body)
137 (if (and (every global? names)
138 (every symbol-interned? names))
146 (make-module-ref loc runtime 'symbol-desc #t)
147 (list (make-const loc name))))
151 (define (set-variable! loc symbol value)
156 (if (symbol? lexical)
157 (make-lexical-set loc symbol lexical value)
158 (make-call loc lexical (list value))))
164 (make-module-ref loc runtime 'set-symbol-value! #t)
165 (list (make-const loc symbol)
168 (define (access-function loc symbol handle-lexical handle-global)
170 ((get-function-binding (fluid-ref bindings-data) symbol)
175 (define (reference-function loc symbol)
179 (lambda (gensym) (make-lexical-ref loc symbol gensym))
181 (make-module-ref loc '(elisp-functions) symbol #t))))
183 (define (set-function! loc symbol value)
187 (lambda (gensym) (make-lexical-set loc symbol gensym value))
191 (make-module-ref loc runtime 'set-symbol-function! #t)
192 (list (make-const loc symbol) value)))))
194 (define (bind-lexically? sym decls)
195 (let ((decl (assq-ref decls sym)))
196 (or (eq? decl 'lexical)
199 (not (special? sym))))))
201 (define (parse-let-binding loc binding)
204 (guard (symbol? var))
207 (guard (symbol? var))
210 (guard (symbol? var))
213 (report-error loc "malformed variable binding" binding))))
215 (define (parse-flet-binding loc binding)
217 ((,var ,args . ,body)
218 (guard (symbol? var))
219 (cons var `(function (lambda ,args ,@body))))
221 (report-error loc "malformed function binding" binding))))
223 (define (parse-declaration expr)
226 (map (cut cons <> 'lexical) vars))
230 (define (parse-body-1 body lambda?)
231 (let loop ((lst body)
236 (((declare . ,x) . ,tail)
237 (loop tail (append-reverse x decls) intspec doc))
238 (((interactive) . ,tail)
239 (guard lambda? (not intspec))
240 (loop tail decls (cons 'interactive-form #nil) doc))
241 (((interactive ,x) . ,tail)
242 (guard lambda? (not intspec))
243 (loop tail decls (cons 'interactive-form x) doc))
245 (guard lambda? (or (string? x) (lisp-string? x)) (not doc) (not (null? tail)))
246 (loop tail decls intspec x))
248 (values (append-map parse-declaration decls)
253 (define (parse-lambda-body body)
254 (parse-body-1 body #t))
256 (define (parse-body body)
257 (receive (decls intspec doc body) (parse-body-1 body #f)
258 (values decls body)))
260 ;;; Partition the argument list of a lambda expression into required,
261 ;;; optional and rest arguments.
263 (define (parse-lambda-list lst)
264 (define (%match lst null optional rest symbol list*)
267 ((&optional . ,tail) (optional tail))
268 ((&rest . ,tail) (rest tail))
269 ((,arg . ,tail) (guard (symbol? arg)) (symbol arg tail))
270 ((,arg . ,tail) (guard (list? arg)) (list* arg tail))
272 (define (return rreq ropt rest)
273 (values #t (reverse rreq) (reverse ropt) rest))
275 (values #f #f #f #f))
276 (define (parse-req lst rreq)
278 (lambda () (return rreq '() #f))
279 (lambda (tail) (parse-opt tail rreq '()))
280 (lambda (tail) (parse-rest tail rreq '()))
281 (lambda (arg tail) (parse-req tail (cons arg rreq)))
282 (lambda (arg tail) (fail))))
283 (define (parse-opt lst rreq ropt)
285 (lambda () (return rreq ropt #f))
286 (lambda (tail) (fail))
287 (lambda (tail) (parse-rest tail rreq ropt))
288 (lambda (arg tail) (parse-opt tail rreq (cons (list arg) ropt)))
289 (lambda (arg tail) (parse-opt tail rreq (cons arg ropt)))))
290 (define (parse-rest lst rreq ropt)
293 (lambda (tail) (fail))
294 (lambda (tail) (fail))
295 (lambda (arg tail) (parse-post-rest tail rreq ropt arg))
296 (lambda (arg tail) (fail))))
297 (define (parse-post-rest lst rreq ropt rest)
299 (lambda () (return rreq ropt rest))
302 (lambda (arg tail) (fail))
303 (lambda (arg tail) (fail))))
306 (define (make-simple-lambda loc meta req opt init rest vars body)
309 (make-lambda-case #f req opt rest #f init vars body #f)))
311 (define (make-dynlet src fluids vals body)
312 (let ((f (map (lambda (x) (gensym "fluid ")) fluids))
313 (v (map (lambda (x) (gensym "valud ")) vals)))
314 (make-let src (map (lambda (_) 'fluid) fluids) f fluids
315 (make-let src (map (lambda (_) 'val) vals) v vals
316 (let lp ((f f) (v v))
320 (make-module-ref src runtime 'bind-symbol #t)
321 (list (make-lexical-ref #f 'fluid (car f))
322 (make-lexical-ref #f 'val (car v))
326 src '() #f #f #f '() '()
330 (define (compile-lambda loc meta args body)
331 (receive (valid? req-ids opts rest-id)
332 (parse-lambda-list args)
334 (let* ((all-ids (append req-ids
335 (and opts (map car opts))
336 (or (and=> rest-id list) '())))
337 (all-vars (map (lambda (ignore) (gensym)) all-ids)))
338 (let*-values (((decls intspec doc forms)
339 (parse-lambda-body body))
342 (compose (cut bind-lexically? <> decls)
344 (map list all-ids all-vars)))
345 ((lexical-ids lexical-vars) (unzip2 lexical))
346 ((dynamic-ids dynamic-vars) (unzip2 dynamic)))
347 (with-dynamic-bindings
348 (fluid-ref bindings-data)
351 (with-lexical-bindings
352 (fluid-ref bindings-data)
362 `(let ((,rest-id (if ,rest-id
372 (map (cut make-const loc <>) dynamic-ids)
373 (map (cut make-lexical-ref loc <> <>)
377 (make-simple-lambda loc
382 (list (cons 'emacs-documentation doc))
389 (compile-expr (car (cdr x)))
390 (make-const loc #nil)))
395 (report-error "invalid function" `(lambda ,args ,@body)))))
397 ;;; Handle macro and special operator bindings.
399 (define (find-operator name type)
402 (module-defined? function-slot name)
403 (let ((op (module-ref function-slot name)))
404 (if (and (pair? op) (eq? (car op) type))
408 (define (contains-unquotes? expr)
410 (if (or (unquote? (car expr)) (unquote-splicing? (car expr)))
412 (or (contains-unquotes? (car expr))
413 (contains-unquotes? (cdr expr))))
416 ;;; Process a backquoted expression by building up the needed
417 ;;; cons/append calls. For splicing, it is assumed that the expression
418 ;;; spliced in evaluates to a list. The emacs manual does not really
419 ;;; state either it has to or what to do if it does not, but Scheme
420 ;;; explicitly forbids it and this seems reasonable also for elisp.
422 (define (unquote-cell? expr)
423 (and (list? expr) (= (length expr) 2) (unquote? (car expr))))
425 (define (unquote-splicing-cell? expr)
426 (and (list? expr) (= (length expr) 2) (unquote-splicing? (car expr))))
428 (define (process-backquote loc expr)
429 (if (contains-unquotes? expr)
431 (if (or (unquote-cell? expr) (unquote-splicing-cell? expr))
432 (compile-expr (cadr expr))
433 (let* ((head (car expr))
434 (processed-tail (process-backquote loc (cdr expr)))
435 (head-is-list-2 (and (list? head)
436 (= (length head) 2)))
437 (head-unquote (and head-is-list-2
438 (unquote? (car head))))
439 (head-unquote-splicing (and head-is-list-2
442 (if head-unquote-splicing
445 (compile-expr (cadr head))
447 (call-primitive loc 'cons
449 (compile-expr (cadr head))
450 (process-backquote loc head))
453 "non-pair expression contains unquotes"
455 (make-const loc expr)))
457 ;;; Special operators
459 (defspecial progn (loc args)
462 (list (nil-value loc))
463 (map compile-expr-1 args))))
465 (defspecial eval-when-compile (loc args)
466 (make-const loc (eval-elisp `(progn ,@args))))
468 (define toplevel? (make-fluid))
470 (define compile-time-too? (make-fluid))
472 (defspecial eval-when (loc args)
474 ((,situations . ,forms)
475 (let ((compile? (or (memq ':compile-toplevel situations)
476 (memq 'compile situations)))
477 (load? (or (memq ':load-toplevel situations)
478 (memq 'load situations)))
479 (execute? (or (memq ':execute situations)
480 (memq 'eval situations))))
482 ((not (fluid-ref toplevel?))
484 (compile-expr `(progn ,@forms))
485 (make-const loc #nil)))
487 (with-fluids ((compile-time-too?
489 (execute? (fluid-ref compile-time-too?))
491 (when (fluid-ref compile-time-too?)
492 (eval-elisp `(progn ,@forms)))
493 (compile-expr-1 `(progn ,@forms))))
494 ((or compile? (and execute? (fluid-ref compile-time-too?)))
495 (eval-elisp `(progn ,@forms))
496 (make-const loc #nil))
498 (make-const loc #nil)))))))
500 (defspecial if (loc args)
502 ((,cond ,then . ,else)
505 (call-primitive loc 'not
506 (call-primitive loc 'nil? (compile-expr cond)))
508 (compile-expr `(progn ,@else))))
509 (else (report-error loc "Bad if" args))))
511 (defspecial defconst (loc args)
513 ((,sym ,value . ,doc)
514 (proclaim-special! sym)
518 (make-module-ref loc runtime 'proclaim-special! #t)
519 (list (make-const loc sym)))
521 (set-variable! loc sym (compile-expr value))
522 (make-const loc sym))))
523 (else (report-error loc "Bad defconst" args))))
525 (defspecial defvar (loc args)
528 (proclaim-special! sym)
531 (make-module-ref loc runtime 'proclaim-special! #t)
532 (list (make-const loc sym)))
533 (make-const loc sym)))
534 ((,sym ,value . ,doc)
535 (proclaim-special! sym)
539 (make-module-ref loc runtime 'proclaim-special! #t)
540 (list (make-const loc sym)))
546 (make-module-ref loc runtime 'symbol-default-bound? #t)
547 (list (make-const loc sym)))
550 (make-module-ref loc runtime 'set-symbol-default-value! #t)
551 (list (make-const loc sym)
552 (compile-expr value))))
553 (make-const loc sym))))
554 (else (report-error loc "Bad defvar" args))))
556 (defspecial setq (loc args)
557 (define (car* x) (if (null? x) '() (car x)))
558 (define (cdr* x) (if (null? x) '() (cdr x)))
559 (define (cadr* x) (car* (cdr* x)))
560 (define (cddr* x) (cdr* (cdr* x)))
563 (let loop ((args args) (last (nil-value loc)))
566 (let ((sym (car args))
567 (val (compile-expr (cadr* args))))
568 (if (not (symbol? sym))
569 (report-error loc "expected symbol in setq" args)
571 (set-variable! loc sym val)
573 (reference-variable loc sym)))))))))
575 (defspecial let (loc args)
578 (let ((bindings (map (cut parse-let-binding loc <>) varlist)))
579 (receive (decls forms) (parse-body body)
580 (receive (lexical dynamic)
582 (compose (cut bind-lexically? <> decls)
585 (let ((make-values (lambda (for)
586 (map (lambda (el) (compile-expr (cdr el)))
588 (make-body (lambda () (compile-expr `(progn ,@forms)))))
596 (map (compose (cut make-const loc <>) car)
598 (map (compose compile-expr cdr)
601 (let* ((lexical-syms (map (lambda (el) (gensym)) lexical))
602 (dynamic-syms (map (lambda (el) (gensym)) dynamic))
603 (all-syms (append lexical-syms dynamic-syms))
604 (vals (append (make-values lexical)
605 (make-values dynamic))))
610 (with-lexical-bindings
611 (fluid-ref bindings-data)
619 (compose (cut make-const
631 (make-body))))))))))))))
632 (else (report-error loc "bad let args"))))
634 (defspecial let* (loc args)
637 (let ((bindings (map (cut parse-let-binding loc <>) varlist)))
638 (receive (decls forms) (parse-body body)
639 (let iterate ((tail bindings))
641 (compile-expr `(progn ,@forms))
642 (let ((sym (caar tail))
643 (value (compile-expr (cdar tail))))
644 (if (bind-lexically? sym decls)
645 (let ((target (gensym)))
650 (with-lexical-bindings
651 (fluid-ref bindings-data)
654 (lambda () (iterate (cdr tail))))))
659 (list (make-const loc sym))
661 (iterate (cdr tail)))))))))))
662 (else (report-error loc "Bad let*" args))))
664 (defspecial flet (loc args)
667 (let ((names+vals (map (cut parse-flet-binding loc <>) bindings)))
668 (receive (decls forms) (parse-body body)
669 (let ((names (map car names+vals))
670 (vals (map cdr names+vals))
671 (gensyms (map (lambda (x) (gensym)) names+vals)))
672 (with-function-bindings
673 (fluid-ref bindings-data)
680 (map compile-expr vals)
681 (compile-expr `(progn ,@forms)))))))))
682 (else (report-error loc "bad flet" args))))
684 (defspecial labels (loc args)
687 (let ((names+vals (map (cut parse-flet-binding loc <>) bindings)))
688 (receive (decls forms) (parse-body body)
689 (let ((names (map car names+vals))
690 (vals (map cdr names+vals))
691 (gensyms (map (lambda (x) (gensym)) names+vals)))
692 (with-function-bindings
693 (fluid-ref bindings-data)
701 (map compile-expr vals)
702 (compile-expr `(progn ,@forms)))))))))
703 (else (report-error loc "bad labels" args))))
705 ;;; guile-ref allows building TreeIL's module references from within
706 ;;; elisp as a way to access data within the Guile universe. The module
707 ;;; and symbol referenced are static values, just like (@ module symbol)
710 (defspecial guile-ref (loc args)
712 ((,module ,sym) (guard (and (list? module) (symbol? sym)))
713 (make-module-ref loc module sym #t))
714 (else (report-error loc "bad guile-ref" args))))
716 (defspecial guile-private-ref (loc args)
718 ((,module ,sym) (guard (and (list? module) (symbol? sym)))
719 (make-module-ref loc module sym #f))
720 (else (report-error loc "bad guile-private-ref" args))))
722 ;;; guile-primitive allows to create primitive references, which are
723 ;;; still a little faster.
725 (defspecial guile-primitive (loc args)
728 (make-primitive-ref loc sym))
729 (else (report-error loc "bad guile-primitive" args))))
731 (defspecial %function (loc args)
733 (((lambda ,args . ,body))
734 (compile-lambda loc '() args body))
735 (((closure ,env ,args . ,body))
736 (let ((bindings (map (lambda (x) (list (car x) (cdr x)))
737 (filter pair? env))))
739 (let ((form `(let ,bindings
740 (declare ,@(map (lambda (x) (list 'lexical x))
742 (function (lambda ,args
748 ((memq x '(&optional &rest))
757 ((,sym) (guard (symbol? sym))
758 (reference-function loc sym))
761 (else (report-error loc "bad function" args))))
763 (defspecial function (loc args)
765 ((,sym) (guard (symbol? sym))
766 (make-const loc sym))
767 (else ((cdr compile-%function) loc args))))
769 (defspecial defmacro (loc args)
771 ((,name ,args . ,body)
772 (if (not (symbol? name))
773 (report-error loc "expected symbol as macro name" name)
782 (make-module-ref loc '(guile) 'cons #t)
783 (list (make-const loc 'macro)
788 (make-const loc name))))
789 (when (fluid-ref toplevel?)
790 (eval-tree-il tree-il))
792 (else (report-error loc "bad defmacro" args))))
794 (defspecial #{`}# (loc args)
797 (process-backquote loc val))
798 (else (report-error loc "bad backquote" args))))
800 (defspecial quote (loc args)
803 (make-const loc val))
804 (else (report-error loc "bad quote" args))))
806 (defspecial %funcall (loc args)
808 ((,function . ,arguments)
810 (compile-expr function)
811 (map compile-expr arguments)))
812 (else (report-error loc "bad %funcall" args))))
814 (defspecial %set-lexical-binding-mode (loc args)
817 (set-lexical-binding-mode val)
819 (else (report-error loc "bad %set-lexical-binding-mode" args))))
822 (if (symbol-fbound? 'get)
823 ((symbol-function 'get) s p)
826 ;;; Compile a compound expression to Tree-IL.
828 (define (compile-pair loc expr)
829 (let ((operator (car expr))
830 (arguments (cdr expr)))
832 ((find-operator operator 'special-operator)
833 => (lambda (special-operator-function)
834 (special-operator-function loc arguments)))
835 ((find-operator operator 'macro)
836 => (lambda (macro-function)
837 (compile-expr-1 (apply macro-function arguments))))
838 ((and (symbol? operator)
839 (eget operator '%compiler-macro))
840 => (lambda (compiler-macro-function)
841 (let ((new (compiler-macro-function expr)))
843 (compile-expr `(%funcall (%function ,operator) ,@arguments))
844 (compile-expr-1 new)))))
846 (compile-expr `(%funcall (%function ,operator) ,@arguments))))))
848 ;;; Compile a symbol expression. This is a variable reference or maybe
849 ;;; some special value like nil.
851 (define (compile-symbol loc sym)
853 ((nil) (nil-value loc))
855 (else (reference-variable loc sym))))
857 ;;; Compile a single expression to TreeIL.
859 (define (compile-expr-1 expr)
860 (let ((loc (location expr)))
863 (compile-symbol loc expr))
865 (compile-pair loc expr))
866 (else (make-const loc expr)))))
868 (define (compile-expr expr)
869 (if (fluid-ref toplevel?)
870 (with-fluids ((toplevel? #f))
871 (compile-expr-1 expr))
872 (compile-expr-1 expr)))
874 (define (compile-tree-il expr env opts)
876 (with-fluids ((bindings-data (make-bindings))
878 (compile-time-too? #f))
879 (compile-expr-1 expr))