3 ;; Copyright (C) 2009, 2010, 2011 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-primcall 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 (define (access-variable loc symbol handle-lexical handle-dynamic)
113 ((get-lexical-binding (fluid-ref bindings-data) symbol)
118 (define (reference-variable loc symbol)
123 (make-lexical-ref loc lexical lexical))
127 (make-module-ref loc value-slot symbol #t)))))
129 (define (global? module symbol)
130 (module-variable module symbol))
132 (define (ensure-globals! loc names body)
133 (if (and (every (cut global? (resolve-module value-slot) <>) names)
134 (every symbol-interned? names))
140 (ensure-fluid! value-slot name)
142 (make-module-ref loc runtime 'ensure-fluid! #t)
143 (list (make-const loc value-slot)
144 (make-const loc name))))
148 (define (set-variable! loc symbol value)
153 (make-lexical-set loc lexical lexical value))
160 (make-module-ref loc value-slot symbol #t)
163 (define (access-function loc symbol handle-lexical handle-global)
165 ((get-function-binding (fluid-ref bindings-data) symbol)
170 (define (reference-function loc symbol)
174 (lambda (gensym) (make-lexical-ref loc symbol gensym))
175 (lambda () (make-module-ref loc function-slot symbol #t))))
177 (define (set-function! loc symbol value)
181 (lambda (gensym) (make-lexical-set loc symbol gensym value))
185 (make-module-ref loc runtime 'set-symbol-function! #t)
186 (list (make-const loc symbol) value)))))
188 (define (bind-lexically? sym module decls)
189 (or (eq? module function-slot)
190 (let ((decl (assq-ref decls sym)))
191 (and (equal? module value-slot)
195 (fluid-ref lexical-binding)
196 (not (global? (resolve-module module) sym))))))))
198 (define (parse-let-binding loc binding)
201 (guard (symbol? var))
204 (guard (symbol? var))
207 (guard (symbol? var))
210 (report-error loc "malformed variable binding" binding))))
212 (define (parse-flet-binding loc binding)
214 ((,var ,args . ,body)
215 (guard (symbol? var))
216 (cons var `(function (lambda ,args ,@body))))
218 (report-error loc "malformed function binding" binding))))
220 (define (parse-declaration expr)
223 (map (cut cons <> 'lexical) vars))
227 (define (parse-body-1 body lambda?)
228 (let loop ((lst body)
233 (((declare . ,x) . ,tail)
234 (loop tail (append-reverse x decls) intspec doc))
235 (((interactive . ,x) . ,tail)
236 (guard lambda? (not intspec))
237 (loop tail decls x doc))
239 (guard lambda? (string? x) (not doc) (not (null? tail)))
240 (loop tail decls intspec x))
242 (values (append-map parse-declaration decls)
247 (define (parse-lambda-body body)
248 (parse-body-1 body #t))
250 (define (parse-body body)
251 (receive (decls intspec doc body) (parse-body-1 body #f)
252 (values decls body)))
254 ;;; Partition the argument list of a lambda expression into required,
255 ;;; optional and rest arguments.
257 (define (parse-lambda-list lst)
258 (define (%match lst null optional rest symbol)
261 ((&optional . ,tail) (optional tail))
262 ((&rest . ,tail) (rest tail))
263 ((,arg . ,tail) (guard (symbol? arg)) (symbol arg tail))
265 (define (return rreq ropt rest)
266 (values #t (reverse rreq) (reverse ropt) rest))
268 (values #f #f #f #f))
269 (define (parse-req lst rreq)
271 (lambda () (return rreq '() #f))
272 (lambda (tail) (parse-opt tail rreq '()))
273 (lambda (tail) (parse-rest tail rreq '()))
274 (lambda (arg tail) (parse-req tail (cons arg rreq)))))
275 (define (parse-opt lst rreq ropt)
277 (lambda () (return rreq ropt #f))
278 (lambda (tail) (fail))
279 (lambda (tail) (parse-rest tail rreq ropt))
280 (lambda (arg tail) (parse-opt tail rreq (cons arg ropt)))))
281 (define (parse-rest lst rreq ropt)
284 (lambda (tail) (fail))
285 (lambda (tail) (fail))
286 (lambda (arg tail) (parse-post-rest tail rreq ropt arg))))
287 (define (parse-post-rest lst rreq ropt rest)
289 (lambda () (return rreq ropt rest))
292 (lambda (arg tail) (fail))))
295 (define (make-simple-lambda loc meta req opt init rest vars body)
298 (make-lambda-case #f req opt rest #f init vars body #f)))
300 (define (compile-lambda loc meta args body)
301 (receive (valid? req-ids opt-ids rest-id)
302 (parse-lambda-list args)
304 (let* ((all-ids (append req-ids
306 (or (and=> rest-id list) '())))
307 (all-vars (map (lambda (ignore) (gensym)) all-ids)))
308 (let*-values (((decls intspec doc forms)
309 (parse-lambda-body body))
312 (compose (cut bind-lexically? <> value-slot decls)
314 (map list all-ids all-vars)))
315 ((lexical-ids lexical-vars) (unzip2 lexical))
316 ((dynamic-ids dynamic-vars) (unzip2 dynamic)))
317 (with-dynamic-bindings
318 (fluid-ref bindings-data)
321 (with-lexical-bindings
322 (fluid-ref bindings-data)
332 `(let ((,rest-id (if ,rest-id
342 (map (cut make-module-ref loc value-slot <> #t)
344 (map (cut make-lexical-ref loc <> <>)
348 (make-simple-lambda loc
352 (map (const (nil-value loc))
357 (report-error "invalid function" `(lambda ,args ,@body)))))
359 ;;; Handle the common part of defconst and defvar, that is, checking for
360 ;;; a correct doc string and arguments as well as maybe in the future
361 ;;; handling the docstring somehow.
363 (define (handle-var-def loc sym doc)
365 ((not (symbol? sym)) (report-error loc "expected symbol, got" sym))
366 ((> (length doc) 1) (report-error loc "too many arguments to defvar"))
367 ((and (not (null? doc)) (not (string? (car doc))))
368 (report-error loc "expected string as third argument of defvar, got"
370 ;; TODO: Handle doc string if present.
373 ;;; Handle macro and special operator bindings.
375 (define (find-operator name type)
378 (module-defined? (resolve-interface function-slot) name)
379 (let ((op (module-ref (resolve-module function-slot) name)))
380 (if (and (pair? op) (eq? (car op) type))
384 ;;; See if a (backquoted) expression contains any unquotes.
386 (define (contains-unquotes? expr)
388 (if (or (unquote? (car expr)) (unquote-splicing? (car expr)))
390 (or (contains-unquotes? (car expr))
391 (contains-unquotes? (cdr expr))))
394 ;;; Process a backquoted expression by building up the needed
395 ;;; cons/append calls. For splicing, it is assumed that the expression
396 ;;; spliced in evaluates to a list. The emacs manual does not really
397 ;;; state either it has to or what to do if it does not, but Scheme
398 ;;; explicitly forbids it and this seems reasonable also for elisp.
400 (define (unquote-cell? expr)
401 (and (list? expr) (= (length expr) 2) (unquote? (car expr))))
403 (define (unquote-splicing-cell? expr)
404 (and (list? expr) (= (length expr) 2) (unquote-splicing? (car expr))))
406 (define (process-backquote loc expr)
407 (if (contains-unquotes? expr)
409 (if (or (unquote-cell? expr) (unquote-splicing-cell? expr))
410 (compile-expr (cadr expr))
411 (let* ((head (car expr))
412 (processed-tail (process-backquote loc (cdr expr)))
413 (head-is-list-2 (and (list? head)
414 (= (length head) 2)))
415 (head-unquote (and head-is-list-2
416 (unquote? (car head))))
417 (head-unquote-splicing (and head-is-list-2
420 (if head-unquote-splicing
423 (compile-expr (cadr head))
425 (call-primitive loc 'cons
427 (compile-expr (cadr head))
428 (process-backquote loc head))
431 "non-pair expression contains unquotes"
433 (make-const loc expr)))
435 ;;; Special operators
437 (defspecial progn (loc args)
440 (list (nil-value loc))
441 (map compile-expr args))))
443 (defspecial eval-when-compile (loc args)
444 (make-const loc (compile `(progn ,@args) #:from 'elisp #:to 'value)))
446 (defspecial if (loc args)
448 ((,cond ,then . ,else)
451 (call-primitive loc 'not
452 (call-primitive loc 'nil? (compile-expr cond)))
454 (compile-expr `(progn ,@else))))))
456 (defspecial defconst (loc args)
458 ((,sym ,value . ,doc)
459 (if (handle-var-def loc sym doc)
461 (set-variable! loc sym (compile-expr value))
462 (make-const loc sym))))))
464 (defspecial defvar (loc args)
466 ((,sym) (make-const loc sym))
467 ((,sym ,value . ,doc)
468 (if (handle-var-def loc sym doc)
480 (make-const loc value-slot))
481 (make-const loc sym))
484 (make-module-ref loc value-slot sym #t))
487 (set-variable! loc sym (compile-expr value)))
488 (make-const loc sym))))))
490 (defspecial setq (loc args)
491 (define (car* x) (if (null? x) '() (car x)))
492 (define (cdr* x) (if (null? x) '() (cdr x)))
493 (define (cadr* x) (car* (cdr* x)))
494 (define (cddr* x) (cdr* (cdr* x)))
497 (let loop ((args args) (last (nil-value loc)))
500 (let ((sym (car args))
501 (val (compile-expr (cadr* args))))
502 (if (not (symbol? sym))
503 (report-error loc "expected symbol in setq")
505 (set-variable! loc sym val)
507 (reference-variable loc sym)))))))))
509 (defspecial let (loc args)
512 (let ((bindings (map (cut parse-let-binding loc <>) varlist)))
513 (receive (decls forms) (parse-body body)
514 (receive (lexical dynamic)
516 (compose (cut bind-lexically? <> value-slot decls)
519 (let ((make-values (lambda (for)
520 (map (lambda (el) (compile-expr (cdr el)))
522 (make-body (lambda () (compile-expr `(progn ,@forms)))))
528 (map (compose (cut make-module-ref
535 (map (compose compile-expr cdr)
538 (let* ((lexical-syms (map (lambda (el) (gensym)) lexical))
539 (dynamic-syms (map (lambda (el) (gensym)) dynamic))
540 (all-syms (append lexical-syms dynamic-syms))
541 (vals (append (make-values lexical)
542 (make-values dynamic))))
547 (with-lexical-bindings
548 (fluid-ref bindings-data)
571 (make-body))))))))))))))))
573 (defspecial let* (loc args)
576 (let ((bindings (map (cut parse-let-binding loc <>) varlist)))
577 (receive (decls forms) (parse-body body)
578 (let iterate ((tail bindings))
580 (compile-expr `(progn ,@forms))
581 (let ((sym (caar tail))
582 (value (compile-expr (cdar tail))))
583 (if (bind-lexically? sym value-slot decls)
584 (let ((target (gensym)))
589 (with-lexical-bindings
590 (fluid-ref bindings-data)
593 (lambda () (iterate (cdr tail))))))
598 (list (make-module-ref loc value-slot sym #t))
600 (iterate (cdr tail)))))))))))))
602 (defspecial flet (loc args)
605 (let ((names+vals (map (cut parse-flet-binding loc <>) bindings)))
606 (receive (decls forms) (parse-body body)
607 (let ((names (map car names+vals))
608 (vals (map cdr names+vals))
609 (gensyms (map (lambda (x) (gensym)) names+vals)))
610 (with-function-bindings
611 (fluid-ref bindings-data)
618 (map compile-expr vals)
619 (compile-expr `(progn ,@forms)))))))))))
621 (defspecial labels (loc args)
624 (let ((names+vals (map (cut parse-flet-binding loc <>) bindings)))
625 (receive (decls forms) (parse-body body)
626 (let ((names (map car names+vals))
627 (vals (map cdr names+vals))
628 (gensyms (map (lambda (x) (gensym)) names+vals)))
629 (with-function-bindings
630 (fluid-ref bindings-data)
638 (map compile-expr vals)
639 (compile-expr `(progn ,@forms)))))))))))
641 ;;; guile-ref allows building TreeIL's module references from within
642 ;;; elisp as a way to access data within the Guile universe. The module
643 ;;; and symbol referenced are static values, just like (@ module symbol)
646 (defspecial guile-ref (loc args)
648 ((,module ,sym) (guard (and (list? module) (symbol? sym)))
649 (make-module-ref loc module sym #t))))
651 ;;; guile-primitive allows to create primitive references, which are
652 ;;; still a little faster.
654 (defspecial guile-primitive (loc args)
657 (make-primitive-ref loc sym))))
659 (defspecial function (loc args)
661 (((lambda ,args . ,body))
662 (compile-lambda loc '() args body))
663 ((,sym) (guard (symbol? sym))
664 (reference-function loc sym))))
666 (defspecial defmacro (loc args)
668 ((,name ,args . ,body)
669 (if (not (symbol? name))
670 (report-error loc "expected symbol as macro name" name)
679 (make-module-ref loc '(guile) 'cons #t)
680 (list (make-const loc 'macro)
685 (make-const loc name))))
686 (compile tree-il #:from 'tree-il #:to 'value)
689 (defspecial defun (loc args)
691 ((,name ,args . ,body)
692 (if (not (symbol? name))
693 (report-error loc "expected symbol as function name" name)
701 (make-const loc name))))))
703 (defspecial #{`}# (loc args)
706 (process-backquote loc val))))
708 (defspecial quote (loc args)
711 (make-const loc val))))
713 (defspecial %funcall (loc args)
715 ((,function . ,arguments)
717 (compile-expr function)
718 (map compile-expr arguments)))))
720 (defspecial %set-lexical-binding-mode (loc args)
723 (fluid-set! lexical-binding val)
726 ;;; Compile a compound expression to Tree-IL.
728 (define (compile-pair loc expr)
729 (let ((operator (car expr))
730 (arguments (cdr expr)))
732 ((find-operator operator 'special-operator)
733 => (lambda (special-operator-function)
734 (special-operator-function loc arguments)))
735 ((find-operator operator 'macro)
736 => (lambda (macro-function)
737 (compile-expr (apply macro-function arguments))))
739 (compile-expr `(%funcall (function ,operator) ,@arguments))))))
741 ;;; Compile a symbol expression. This is a variable reference or maybe
742 ;;; some special value like nil.
744 (define (compile-symbol loc sym)
746 ((nil) (nil-value loc))
748 (else (reference-variable loc sym))))
750 ;;; Compile a single expression to TreeIL.
752 (define (compile-expr expr)
753 (let ((loc (location expr)))
756 (compile-symbol loc expr))
758 (compile-pair loc expr))
759 (else (make-const loc expr)))))
761 ;;; Process the compiler options.
762 ;;; FIXME: Why is '(()) passed as options by the REPL?
764 (define (valid-symbol-list-arg? value)
766 (and (list? value) (and-map symbol? value))))
768 (define (process-options! opt)
769 (if (and (not (null? opt))
770 (not (equal? opt '(()))))
771 (if (null? (cdr opt))
772 (report-error #f "Invalid compiler options" opt)
773 (let ((key (car opt))
776 ((#:warnings) ; ignore
778 (else (report-error #f
779 "Invalid compiler option"
782 (define (compile-tree-il expr env opts)
784 (with-fluids ((bindings-data (make-bindings)))
785 (process-options! opts)