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 #:export (compile-tree-il
35 compile-eval-when-compile
45 compile-guile-private-ref
46 compile-guile-primitive
54 compile-%set-lexical-binding-mode))
56 ;;; Certain common parameters (like the bindings data structure or
57 ;;; compiler options) are not always passed around but accessed using
58 ;;; fluids to simulate dynamic binding (hey, this is about elisp).
60 ;;; The bindings data structure to keep track of symbol binding related
63 (define bindings-data (make-fluid))
65 ;;; Find the source properties of some parsed expression if there are
66 ;;; any associated with it.
70 (let ((props (source-properties x)))
71 (and (not (null? props))
74 ;;; Values to use for Elisp's nil and t.
76 (define (nil-value loc)
77 (make-const loc (@ (language elisp runtime) nil-value)))
80 (make-const loc (@ (language elisp runtime) t-value)))
82 ;;; Modules that contain the value and function slot bindings.
84 (define runtime '(language elisp runtime))
86 (define value-slot (@ (language elisp runtime) value-slot-module))
88 (define function-slot (@ (language elisp runtime) function-slot-module))
90 ;;; The backquoting works the same as quasiquotes in Scheme, but the
91 ;;; forms are named differently; to make easy adaptions, we define these
92 ;;; predicates checking for a symbol being the car of an
93 ;;; unquote/unquote-splicing/backquote form.
95 (define (unquote? sym)
96 (and (symbol? sym) (eq? sym '#{,}#)))
98 (define (unquote-splicing? sym)
99 (and (symbol? sym) (eq? sym '#{,@}#)))
101 ;;; Build a call to a primitive procedure nicely.
103 (define (call-primitive loc sym . args)
104 (make-primcall loc sym args))
106 ;;; Error reporting routine for syntax/compilation problems or build
107 ;;; code for a runtime-error output.
109 (define (report-error loc . args)
112 (define (access-variable loc symbol handle-lexical handle-dynamic)
114 ((get-lexical-binding (fluid-ref bindings-data) symbol)
119 (define (reference-variable loc symbol)
124 (if (symbol? lexical)
125 (make-lexical-ref loc symbol lexical)
126 (make-call loc lexical '())))
129 (make-module-ref loc runtime 'symbol-value #t)
130 (list (make-const loc symbol))))))
132 (define (global? symbol)
133 (module-variable value-slot symbol))
135 (define (ensure-globals! loc names body)
136 (if (and (every global? names)
137 (every symbol-interned? names))
145 (make-module-ref loc runtime 'symbol-desc #t)
146 (list (make-const loc name))))
150 (define (set-variable! loc symbol value)
155 (if (symbol? lexical)
156 (make-lexical-set loc symbol lexical value)
157 (make-call loc lexical (list value))))
163 (make-module-ref loc runtime 'set-symbol-value! #t)
164 (list (make-const loc symbol)
167 (define (access-function loc symbol handle-lexical handle-global)
169 ((get-function-binding (fluid-ref bindings-data) symbol)
174 (define (reference-function loc symbol)
178 (lambda (gensym) (make-lexical-ref loc symbol gensym))
180 (make-module-ref loc '(elisp-functions) symbol #t))))
182 (define (set-function! loc symbol value)
186 (lambda (gensym) (make-lexical-set loc symbol gensym value))
190 (make-module-ref loc runtime 'set-symbol-function! #t)
191 (list (make-const loc symbol) value)))))
193 (define (bind-lexically? sym decls)
194 (let ((decl (assq-ref decls sym)))
195 (or (eq? decl 'lexical)
198 (not (special? sym))))))
200 (define (parse-let-binding loc binding)
203 (guard (symbol? var))
206 (guard (symbol? var))
209 (guard (symbol? var))
212 (report-error loc "malformed variable binding" binding))))
214 (define (parse-flet-binding loc binding)
216 ((,var ,args . ,body)
217 (guard (symbol? var))
218 (cons var `(function (lambda ,args ,@body))))
220 (report-error loc "malformed function binding" binding))))
222 (define (parse-declaration expr)
225 (map (cut cons <> 'lexical) vars))
229 (define (parse-body-1 body lambda?)
230 (let loop ((lst body)
235 (((declare . ,x) . ,tail)
236 (loop tail (append-reverse x decls) intspec doc))
237 (((interactive) . ,tail)
238 (guard lambda? (not intspec))
239 (loop tail decls (cons 'interactive-form #nil) doc))
240 (((interactive ,x) . ,tail)
241 (guard lambda? (not intspec))
242 (loop tail decls (cons 'interactive-form x) doc))
244 (guard lambda? (or (string? x) (lisp-string? x)) (not doc) (not (null? tail)))
245 (loop tail decls intspec x))
247 (values (append-map parse-declaration decls)
252 (define (parse-lambda-body body)
253 (parse-body-1 body #t))
255 (define (parse-body body)
256 (receive (decls intspec doc body) (parse-body-1 body #f)
257 (values decls body)))
259 ;;; Partition the argument list of a lambda expression into required,
260 ;;; optional and rest arguments.
262 (define (parse-lambda-list lst)
263 (define (%match lst null optional rest symbol list*)
266 ((&optional . ,tail) (optional tail))
267 ((&rest . ,tail) (rest tail))
268 ((,arg . ,tail) (guard (symbol? arg)) (symbol arg tail))
269 ((,arg . ,tail) (guard (list? arg)) (list* arg tail))
271 (define (return rreq ropt rest)
272 (values #t (reverse rreq) (reverse ropt) rest))
274 (values #f #f #f #f))
275 (define (parse-req lst rreq)
277 (lambda () (return rreq '() #f))
278 (lambda (tail) (parse-opt tail rreq '()))
279 (lambda (tail) (parse-rest tail rreq '()))
280 (lambda (arg tail) (parse-req tail (cons arg rreq)))
281 (lambda (arg tail) (fail))))
282 (define (parse-opt lst rreq ropt)
284 (lambda () (return rreq ropt #f))
285 (lambda (tail) (fail))
286 (lambda (tail) (parse-rest tail rreq ropt))
287 (lambda (arg tail) (parse-opt tail rreq (cons (list arg) ropt)))
288 (lambda (arg tail) (parse-opt tail rreq (cons arg ropt)))))
289 (define (parse-rest lst rreq ropt)
292 (lambda (tail) (fail))
293 (lambda (tail) (fail))
294 (lambda (arg tail) (parse-post-rest tail rreq ropt arg))
295 (lambda (arg tail) (fail))))
296 (define (parse-post-rest lst rreq ropt rest)
298 (lambda () (return rreq ropt rest))
301 (lambda (arg tail) (fail))
302 (lambda (arg tail) (fail))))
305 (define (make-simple-lambda loc meta req opt init rest vars body)
308 (make-lambda-case #f req opt rest #f init vars body #f)))
310 (define (make-dynlet src fluids vals body)
311 (let ((f (map (lambda (x) (gensym "fluid ")) fluids))
312 (v (map (lambda (x) (gensym "valud ")) vals)))
313 (make-let src (map (lambda (_) 'fluid) fluids) f fluids
314 (make-let src (map (lambda (_) 'val) vals) v vals
315 (let lp ((f f) (v v))
319 (make-module-ref src runtime 'bind-symbol #t)
320 (list (make-lexical-ref #f 'fluid (car f))
321 (make-lexical-ref #f 'val (car v))
325 src '() #f #f #f '() '()
329 (define (compile-lambda loc meta args body)
330 (receive (valid? req-ids opts rest-id)
331 (parse-lambda-list args)
333 (let* ((all-ids (append req-ids
334 (and opts (map car opts))
335 (or (and=> rest-id list) '())))
336 (all-vars (map (lambda (ignore) (gensym)) all-ids)))
337 (let*-values (((decls intspec doc forms)
338 (parse-lambda-body body))
341 (compose (cut bind-lexically? <> decls)
343 (map list all-ids all-vars)))
344 ((lexical-ids lexical-vars) (unzip2 lexical))
345 ((dynamic-ids dynamic-vars) (unzip2 dynamic)))
346 (with-dynamic-bindings
347 (fluid-ref bindings-data)
350 (with-lexical-bindings
351 (fluid-ref bindings-data)
361 `(let ((,rest-id (if ,rest-id
371 (map (cut make-const loc <>) dynamic-ids)
372 (map (cut make-lexical-ref loc <> <>)
376 (make-simple-lambda loc
381 (list (cons 'emacs-documentation doc))
388 (compile-expr (car (cdr x)))
389 (make-const loc #nil)))
394 (report-error "invalid function" `(lambda ,args ,@body)))))
396 ;;; Handle macro and special operator bindings.
398 (define (find-operator name type)
401 (module-defined? function-slot name)
402 (let ((op (module-ref function-slot name)))
403 (if (and (pair? op) (eq? (car op) type))
407 (define (contains-unquotes? expr)
409 (if (or (unquote? (car expr)) (unquote-splicing? (car expr)))
411 (or (contains-unquotes? (car expr))
412 (contains-unquotes? (cdr expr))))
415 ;;; Process a backquoted expression by building up the needed
416 ;;; cons/append calls. For splicing, it is assumed that the expression
417 ;;; spliced in evaluates to a list. The emacs manual does not really
418 ;;; state either it has to or what to do if it does not, but Scheme
419 ;;; explicitly forbids it and this seems reasonable also for elisp.
421 (define (unquote-cell? expr)
422 (and (list? expr) (= (length expr) 2) (unquote? (car expr))))
424 (define (unquote-splicing-cell? expr)
425 (and (list? expr) (= (length expr) 2) (unquote-splicing? (car expr))))
427 (define (process-backquote loc expr)
428 (if (contains-unquotes? expr)
430 (if (or (unquote-cell? expr) (unquote-splicing-cell? expr))
431 (compile-expr (cadr expr))
432 (let* ((head (car expr))
433 (processed-tail (process-backquote loc (cdr expr)))
434 (head-is-list-2 (and (list? head)
435 (= (length head) 2)))
436 (head-unquote (and head-is-list-2
437 (unquote? (car head))))
438 (head-unquote-splicing (and head-is-list-2
441 (if head-unquote-splicing
444 (compile-expr (cadr head))
446 (call-primitive loc 'cons
448 (compile-expr (cadr head))
449 (process-backquote loc head))
452 "non-pair expression contains unquotes"
454 (make-const loc expr)))
456 ;;; Special operators
458 (defspecial progn (loc args)
461 (list (nil-value loc))
462 (map compile-expr-1 args))))
464 (defspecial eval-when-compile (loc args)
465 (make-const loc (compile `(progn ,@args) #:from 'elisp #:to 'value)))
467 (define toplevel? (make-fluid))
469 (define compile-time-too? (make-fluid))
471 (defspecial eval-when (loc args)
473 ((,situations . ,forms)
474 (let ((compile? (memq ':compile-toplevel situations))
475 (load? (memq ':load-toplevel situations))
476 (execute? (memq ':execute situations)))
478 ((not (fluid-ref toplevel?))
480 (compile-expr `(progn ,@forms))
481 (make-const loc #nil)))
483 (with-fluids ((compile-time-too?
485 (execute? (fluid-ref compile-time-too?))
487 (when (fluid-ref compile-time-too?)
488 (eval-elisp `(progn ,@forms)))
489 (compile-expr-1 `(progn ,@forms))))
490 ((or compile? (and execute? (fluid-ref compile-time-too?)))
491 (eval-elisp `(progn ,@forms))
492 (make-const loc #nil))
494 (make-const loc #nil)))))))
496 (defspecial if (loc args)
498 ((,cond ,then . ,else)
501 (call-primitive loc 'not
502 (call-primitive loc 'nil? (compile-expr cond)))
504 (compile-expr `(progn ,@else))))
505 (else (report-error loc "Bad if" args))))
507 (defspecial defconst (loc args)
509 ((,sym ,value . ,doc)
510 (proclaim-special! sym)
514 (make-module-ref loc runtime 'proclaim-special! #t)
515 (list (make-const loc sym)))
517 (set-variable! loc sym (compile-expr value))
518 (make-const loc sym))))
519 (else (report-error loc "Bad defconst" args))))
521 (defspecial defvar (loc args)
524 (proclaim-special! sym)
527 (make-module-ref loc runtime 'proclaim-special! #t)
528 (list (make-const loc sym)))
529 (make-const loc sym)))
530 ((,sym ,value . ,doc)
531 (proclaim-special! sym)
535 (make-module-ref loc runtime 'proclaim-special! #t)
536 (list (make-const loc sym)))
542 (make-module-ref loc runtime 'symbol-default-bound? #t)
543 (list (make-const loc sym)))
546 (make-module-ref loc runtime 'set-symbol-default-value! #t)
547 (list (make-const loc sym)
548 (compile-expr value))))
549 (make-const loc sym))))
550 (else (report-error loc "Bad defvar" args))))
552 (defspecial setq (loc args)
553 (define (car* x) (if (null? x) '() (car x)))
554 (define (cdr* x) (if (null? x) '() (cdr x)))
555 (define (cadr* x) (car* (cdr* x)))
556 (define (cddr* x) (cdr* (cdr* x)))
559 (let loop ((args args) (last (nil-value loc)))
562 (let ((sym (car args))
563 (val (compile-expr (cadr* args))))
564 (if (not (symbol? sym))
565 (report-error loc "expected symbol in setq" args)
567 (set-variable! loc sym val)
569 (reference-variable loc sym)))))))))
571 (defspecial let (loc args)
574 (let ((bindings (map (cut parse-let-binding loc <>) varlist)))
575 (receive (decls forms) (parse-body body)
576 (receive (lexical dynamic)
578 (compose (cut bind-lexically? <> decls)
581 (let ((make-values (lambda (for)
582 (map (lambda (el) (compile-expr (cdr el)))
584 (make-body (lambda () (compile-expr `(progn ,@forms)))))
590 (map (compose (cut make-const loc <>) car)
592 (map (compose compile-expr cdr)
595 (let* ((lexical-syms (map (lambda (el) (gensym)) lexical))
596 (dynamic-syms (map (lambda (el) (gensym)) dynamic))
597 (all-syms (append lexical-syms dynamic-syms))
598 (vals (append (make-values lexical)
599 (make-values dynamic))))
604 (with-lexical-bindings
605 (fluid-ref bindings-data)
613 (compose (cut make-const
625 (make-body))))))))))))))
626 (else (report-error loc "bad let args"))))
628 (defspecial let* (loc args)
631 (let ((bindings (map (cut parse-let-binding loc <>) varlist)))
632 (receive (decls forms) (parse-body body)
633 (let iterate ((tail bindings))
635 (compile-expr `(progn ,@forms))
636 (let ((sym (caar tail))
637 (value (compile-expr (cdar tail))))
638 (if (bind-lexically? sym decls)
639 (let ((target (gensym)))
644 (with-lexical-bindings
645 (fluid-ref bindings-data)
648 (lambda () (iterate (cdr tail))))))
653 (list (make-const loc sym))
655 (iterate (cdr tail)))))))))))
656 (else (report-error loc "Bad let*" args))))
658 (defspecial flet (loc args)
661 (let ((names+vals (map (cut parse-flet-binding loc <>) bindings)))
662 (receive (decls forms) (parse-body body)
663 (let ((names (map car names+vals))
664 (vals (map cdr names+vals))
665 (gensyms (map (lambda (x) (gensym)) names+vals)))
666 (with-function-bindings
667 (fluid-ref bindings-data)
674 (map compile-expr vals)
675 (compile-expr `(progn ,@forms)))))))))
676 (else (report-error loc "bad flet" args))))
678 (defspecial labels (loc args)
681 (let ((names+vals (map (cut parse-flet-binding loc <>) bindings)))
682 (receive (decls forms) (parse-body body)
683 (let ((names (map car names+vals))
684 (vals (map cdr names+vals))
685 (gensyms (map (lambda (x) (gensym)) names+vals)))
686 (with-function-bindings
687 (fluid-ref bindings-data)
695 (map compile-expr vals)
696 (compile-expr `(progn ,@forms)))))))))
697 (else (report-error loc "bad labels" args))))
699 ;;; guile-ref allows building TreeIL's module references from within
700 ;;; elisp as a way to access data within the Guile universe. The module
701 ;;; and symbol referenced are static values, just like (@ module symbol)
704 (defspecial guile-ref (loc args)
706 ((,module ,sym) (guard (and (list? module) (symbol? sym)))
707 (make-module-ref loc module sym #t))
708 (else (report-error loc "bad guile-ref" args))))
710 (defspecial guile-private-ref (loc args)
712 ((,module ,sym) (guard (and (list? module) (symbol? sym)))
713 (make-module-ref loc module sym #f))
714 (else (report-error loc "bad guile-private-ref" args))))
716 ;;; guile-primitive allows to create primitive references, which are
717 ;;; still a little faster.
719 (defspecial guile-primitive (loc args)
722 (make-primitive-ref loc sym))
723 (else (report-error loc "bad guile-primitive" args))))
725 (defspecial %function (loc args)
727 (((lambda ,args . ,body))
728 (compile-lambda loc '() args body))
729 (((closure ,env ,args . ,body))
730 (let ((bindings (map (lambda (x) (list (car x) (cdr x)))
731 (filter pair? env))))
733 (let ((form `(let ,bindings
734 (declare ,@(map (lambda (x) (list 'lexical x))
736 (function (lambda ,args
742 ((memq x '(&optional &rest))
751 ((,sym) (guard (symbol? sym))
752 (reference-function loc sym))
755 (else (report-error loc "bad function" args))))
757 (defspecial function (loc args)
759 ((,sym) (guard (symbol? sym))
760 (make-const loc sym))
761 (else ((cdr compile-%function) loc args))))
763 (defspecial defmacro (loc args)
765 ((,name ,args . ,body)
766 (if (not (symbol? name))
767 (report-error loc "expected symbol as macro name" name)
776 (make-module-ref loc '(guile) 'cons #t)
777 (list (make-const loc 'macro)
782 (make-const loc name))))
783 (compile tree-il #:from 'tree-il #:to 'value)
785 (else (report-error loc "bad defmacro" args))))
787 (defspecial #{`}# (loc args)
790 (process-backquote loc val))
791 (else (report-error loc "bad backquote" args))))
793 (defspecial quote (loc args)
796 (make-const loc val))
797 (else (report-error loc "bad quote" args))))
799 (defspecial %funcall (loc args)
801 ((,function . ,arguments)
803 (compile-expr function)
804 (map compile-expr arguments)))
805 (else (report-error loc "bad %funcall" args))))
807 (defspecial %set-lexical-binding-mode (loc args)
810 (set-lexical-binding-mode val)
812 (else (report-error loc "bad %set-lexical-binding-mode" args))))
815 (if (symbol-fbound? 'get)
816 ((symbol-function 'get) s p)
819 ;;; Compile a compound expression to Tree-IL.
821 (define (compile-pair loc expr)
822 (let ((operator (car expr))
823 (arguments (cdr expr)))
825 ((find-operator operator 'special-operator)
826 => (lambda (special-operator-function)
827 (special-operator-function loc arguments)))
828 ((find-operator operator 'macro)
829 => (lambda (macro-function)
830 (compile-expr (apply macro-function arguments))))
831 ((and (symbol? operator)
832 (eget operator '%compiler-macro))
833 => (lambda (compiler-macro-function)
834 (let ((new (compiler-macro-function expr)))
836 (compile-expr `(%funcall (%function ,operator) ,@arguments))
837 (compile-expr new)))))
839 (compile-expr `(%funcall (%function ,operator) ,@arguments))))))
841 ;;; Compile a symbol expression. This is a variable reference or maybe
842 ;;; some special value like nil.
844 (define (compile-symbol loc sym)
846 ((nil) (nil-value loc))
848 (else (reference-variable loc sym))))
850 ;;; Compile a single expression to TreeIL.
852 (define (compile-expr-1 expr)
853 (let ((loc (location expr)))
856 (compile-symbol loc expr))
858 (compile-pair loc expr))
859 (else (make-const loc expr)))))
861 (define (compile-expr expr)
862 (if (fluid-ref toplevel?)
863 (with-fluids ((toplevel? #f))
864 (compile-expr-1 expr))
865 (compile-expr-1 expr)))
867 (define (compile-tree-il expr env opts)
869 (with-fluids ((bindings-data (make-bindings))
871 (compile-time-too? #f))
872 (compile-expr-1 expr))