1 ;;; -*- mode: scheme; coding: utf-8; -*-
3 ;;;; Copyright (C) 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
5 ;;;; This library is free software; you can redistribute it and/or
6 ;;;; modify it under the terms of the GNU Lesser General Public
7 ;;;; License as published by the Free Software Foundation; either
8 ;;;; version 3 of the License, or (at your option) any later version.
10 ;;;; This library 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 GNU
13 ;;;; Lesser General Public License for more details.
15 ;;;; You should have received a copy of the GNU Lesser General Public
16 ;;;; License along with this library; if not, write to the Free Software
17 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 ;;; Scheme eval, written in Scheme.
26 ;;; Expressions are first expanded, by the syntax expander (i.e.
27 ;;; psyntax), then memoized into internal forms. The evaluator itself
28 ;;; only operates on the internal forms ("memoized expressions").
30 ;;; Environments are represented as linked lists of the form (VAL ... .
31 ;;; MOD). If MOD is #f, it means the environment was captured before
32 ;;; modules were booted. If MOD is the literal value '(), we are
33 ;;; evaluating at the top level, and so should track changes to the
36 ;;; Evaluate this in Emacs to make code indentation work right:
38 ;;; (put 'memoized-expression-case 'scheme-indent-function 1)
46 (define-syntax capture-env
49 (let ((env (exp ...)))
55 ;; the and current-module checks that modules are booted,
56 ;; and thus the-root-module is defined
57 (and (current-module) the-root-module)
60 (define-syntax env-toplevel
68 (define-syntax make-env
71 (let ((v (make-vector (1+ n) init)))
72 (vector-set! v 0 next)
75 (define-syntax make-env*
78 (vector next init ...))))
80 (define-syntax env-ref
83 (let lp ((e env) (d depth))
85 (vector-ref e (1+ width))
86 (lp (vector-ref e 0) (1- d)))))))
88 (define-syntax env-set!
90 ((_ env depth width val)
91 (let lp ((e env) (d depth))
93 (vector-set! e (1+ width) val)
94 (lp (vector-ref e 0) (1- d)))))))
96 ;; Fast case for procedures with fixed arities.
97 (define-syntax make-fixed-closure
99 (define *max-static-argument-count* 8)
100 (define (make-formals n)
105 (string (integer->char (+ (char->integer #\a) i))))))
108 ((_ eval nreq body env) (not (identifier? #'env))
110 (make-fixed-closure eval nreq body e)))
111 ((_ eval nreq body env)
113 #,@(map (lambda (nreq)
114 (let ((formals (make-formals nreq)))
118 (make-env* env #,@formals))))))
119 (iota *max-static-argument-count*))
121 #,(let ((formals (make-formals *max-static-argument-count*)))
122 #`(lambda (#,@formals . more)
123 (let ((env (make-env nreq #f env)))
124 #,@(map (lambda (formal n)
125 #`(env-set! env 0 #,n #,formal))
126 formals (iota (length formals)))
127 (let lp ((i #,*max-static-argument-count*)
134 (scm-error 'wrong-number-of-args
135 "eval" "Wrong number of arguments"
138 (scm-error 'wrong-number-of-args
139 "eval" "Wrong number of arguments"
142 (env-set! env 0 i (car args))
143 (lp (1+ i) (cdr args))))))))))))))
145 ;; Fast case for procedures with fixed arities and a rest argument.
146 (define-syntax make-rest-closure
148 (define *max-static-argument-count* 3)
149 (define (make-formals n)
154 (string (integer->char (+ (char->integer #\a) i))))))
157 ((_ eval nreq body env) (not (identifier? #'env))
159 (make-rest-closure eval nreq body e)))
160 ((_ eval nreq body env)
162 #,@(map (lambda (nreq)
163 (let ((formals (make-formals nreq)))
165 (lambda (#,@formals . rest)
167 (make-env* env #,@formals rest))))))
168 (iota *max-static-argument-count*))
170 #,(let ((formals (make-formals *max-static-argument-count*)))
171 #`(lambda (#,@formals . more)
172 (let ((env (make-env (1+ nreq) #f env)))
173 #,@(map (lambda (formal n)
174 #`(env-set! env 0 #,n #,formal))
175 formals (iota (length formals)))
176 (let lp ((i #,*max-static-argument-count*)
180 (env-set! env 0 nreq args)
183 (scm-error 'wrong-number-of-args
184 "eval" "Wrong number of arguments"
187 (env-set! env 0 i (car args))
188 (lp (1+ i) (cdr args))))))))))))))
192 (define *max-static-call-count* 4)
194 ((_ eval proc nargs args env) (identifier? #'env)
196 #,@(map (lambda (nargs)
201 (let lp ((n n) (args #'args))
203 #`(eval (car #,args) env)
204 (lp (1- n) #`(cdr #,args)))))
206 (iota *max-static-call-count*))
211 (let lp ((n n) (args #'args))
213 #`(eval (car #,args) env)
214 (lp (1- n) #`(cdr #,args)))))
215 (iota *max-static-call-count*))
216 (let lp ((exps #,(let lp ((n *max-static-call-count*)
220 (lp (1- n) #`(cdr #,args)))))
225 (cons (eval (car exps) env) args)))))))))))
227 ;; This macro could be more straightforward if the compiler had better
228 ;; copy propagation. As it is we do some copy propagation by hand.
229 (define-syntax mx-bind
234 ((_ data (a . b) body) (and (identifier? #'a) (identifier? #'b))
235 #'(let ((a (car data))
238 ((_ data (a . b) body) (identifier? #'a)
239 #'(let ((a (car data))
241 (mx-bind xb b body)))
242 ((_ data (a . b) body)
243 #'(let ((xa (car data))
245 (mx-bind xa a (mx-bind xb b body))))
246 ((_ data v body) (identifier? #'v)
250 ;; The resulting nested if statements will be an O(n) dispatch. Once
251 ;; we compile `case' effectively, this situation will improve.
252 (define-syntax mx-match
254 (syntax-case x (quote)
257 ((_ mx data tag (('type pat) body) c* ...)
258 #`(if (eqv? tag #,(or (memoized-typecode (syntax->datum #'type))
259 (error "not a typecode" #'type)))
260 (mx-bind data pat body)
261 (mx-match mx data tag c* ...))))))
263 (define-syntax memoized-expression-case
267 #'(let ((tag (memoized-expression-typecode mx))
268 (data (memoized-expression-data mx)))
269 (mx-match mx data tag c ...)))))))
273 ;;; On 18 Feb 2010, I did a profile of how often the various memoized expression
274 ;;; types occur when getting to a prompt on a fresh build. Here are the numbers
277 ;;; lexical-ref: 32933054
279 ;;; toplevel-ref: 13228724
285 ;;; lexical-set: 509862
286 ;;; call-with-values: 139668
288 ;;; module-ref: 14468
290 ;;; toplevel-set: 328
294 ;;; So until we compile `case' into a computed goto, we'll order the clauses in
295 ;;; `eval' in this order, to put the most frequent cases first.
298 (define primitive-eval
300 ;; We pre-generate procedures with fixed arities, up to some number
301 ;; of arguments, and some rest arities; see make-fixed-closure and
302 ;; make-rest-closure above.
304 ;; A unique marker for unbound keywords.
305 (define unbound-arg (list 'unbound-arg))
307 ;; Procedures with rest, optional, or keyword arguments, potentially with
308 ;; multiple arities, as with case-lambda.
309 (define (make-general-closure env body nreq rest? nopt kw inits alt)
311 (and alt ; (body docstring nreq ...)
312 (let* ((body (car alt))
315 (rest (if (null? (cdr spec)) #f (cadr spec)))
316 (tail (and (pair? (cdr spec)) (pair? (cddr spec)) (cddr spec)))
317 (nopt (if tail (car tail) 0))
318 (kw (and tail (cadr tail)))
319 (inits (if tail (caddr tail) '()))
320 (alt (and tail (cadddr tail))))
321 (make-general-closure env body nreq rest nopt kw inits alt))))
322 (define (set-procedure-arity! proc)
323 (let lp ((alt alt) (nreq nreq) (nopt nopt) (rest? rest?))
326 (set-procedure-property! proc 'arglist
332 (set-procedure-minimum-arity! proc nreq nopt rest?))
333 (let* ((spec (cddr alt))
335 (rest?* (if (null? (cdr spec)) #f (cadr spec)))
336 (tail (and (pair? (cdr spec)) (pair? (cddr spec)) (cddr spec)))
337 (nopt* (if tail (car tail) 0))
338 (alt* (and tail (cadddr tail))))
339 (if (or (< nreq* nreq)
342 (and rest?* (> nopt* nopt))
343 (or rest?* (> nopt* nopt)))))
344 (lp alt* nreq* nopt* rest?*)
345 (lp alt* nreq nopt rest?)))))
347 (set-procedure-arity!
349 (define (npositional args)
350 (let lp ((n 0) (args args))
352 (and (>= n nreq) (keyword? (car args))))
354 (lp (1+ n) (cdr args)))))
355 (let ((nargs (length %args)))
358 (and (not kw) (not rest?) (> nargs (+ nreq nopt)))
359 (and kw (not rest?) (> (npositional %args) (+ nreq nopt))))
361 (apply alt-proc %args)
362 ((scm-error 'wrong-number-of-args
363 "eval" "Wrong number of arguments"
366 (let* ((nvals (+ nreq (if rest? 1 0) (length inits)))
367 (env (make-env nvals unbound-arg env)))
368 (let lp ((i 0) (args %args))
371 ;; Bind required arguments.
372 (env-set! env 0 i (car args))
373 (lp (1+ i) (cdr args)))
375 ;; Optional args (possibly), but no keyword args.
376 (let lp ((i i) (args args) (inits inits))
381 (env-set! env 0 i (car args))
382 (lp (1+ i) (cdr args) (cdr inits)))
384 (env-set! env 0 i (eval (car inits) env))
385 (lp (1+ i) args (cdr inits)))))
388 (env-set! env 0 i args))
391 ;; Optional args. As before, but stop at the first
393 (let lp ((i i) (args args) (inits inits))
397 ((and (< i nargs) (not (keyword? (car args))))
398 (env-set! env 0 i (car args))
399 (lp (1+ i) (cdr args) (cdr inits)))
401 (env-set! env 0 i (eval (car inits) env))
402 (lp (1+ i) args (cdr inits)))))
405 (env-set! env 0 i args))
408 (kw-base (if rest? (1+ i) i)))
409 ;; Now scan args for keywords.
410 (let lp ((args args))
412 ((and (pair? args) (pair? (cdr args))
413 (keyword? (car args)))
414 (let ((kw-pair (assq (car args) kw))
417 ;; Found a known keyword; set its value.
418 (env-set! env 0 (cdr kw-pair) v)
422 'keyword-argument-error
423 "eval" "Unrecognized keyword"
424 '() (list (car args))))))
428 ;; Be lenient parsing rest args.
430 ((scm-error 'keyword-argument-error
431 "eval" "Invalid keyword"
432 '() (list (car args))))))
434 ;; Finished parsing keywords. Fill in
435 ;; uninitialized kwargs by evalling init
436 ;; expressions in their appropriate
438 (let lp ((i kw-base) (inits inits))
441 (when (eq? (env-ref env 0 i) unbound-arg)
442 (env-set! env 0 i (eval (car inits) env)))
443 (lp (1+ i) (cdr inits)))
445 ;; Finally, eval the body.
446 (eval body env)))))))))))))))))))))
448 ;; The "engine". EXP is a memoized expression.
449 (define (eval exp env)
450 (memoized-expression-case exp
451 (('lexical-ref (depth . width))
452 (env-ref env depth width))
454 (('call (f nargs . args))
455 (let ((proc (eval f env)))
456 (call eval proc nargs args env)))
458 (('toplevel-ref var-or-sym)
460 (if (variable? var-or-sym)
462 (memoize-variable-access! exp
463 (capture-env (env-toplevel env))))))
465 (('if (test consequent . alternate))
467 (eval consequent env)
468 (eval alternate env)))
473 (('let (inits . body))
474 (let* ((width (vector-length inits))
475 (new-env (make-env width #f (capture-env env))))
478 (env-set! new-env 0 i (eval (vector-ref inits i) env))
480 (eval body new-env)))
482 (('lambda (body docstring nreq . tail))
485 (make-fixed-closure eval nreq body (capture-env env))
486 (if (null? (cdr tail))
487 (make-rest-closure eval nreq body (capture-env env))
488 (apply make-general-closure (capture-env env)
491 (set-procedure-property! proc 'documentation docstring))
494 (('seq (head . tail))
499 (('lexical-set! ((depth . width) . x))
500 (env-set! env depth width (eval x env)))
502 (('call-with-values (producer . consumer))
503 (call-with-values (eval producer env)
504 (eval consumer env)))
507 (apply (eval f env) (eval args env)))
509 (('module-ref var-or-spec)
511 (if (variable? var-or-spec)
513 (memoize-variable-access! exp #f))))
515 (('define (name . x))
516 (let ((x (eval x env)))
517 (if (and (procedure? x) (not (procedure-property x 'name)))
518 (set-procedure-property! x 'name name))
522 (('toplevel-set! (var-or-sym . x))
524 (if (variable? var-or-sym)
526 (memoize-variable-access! exp
527 (capture-env (env-toplevel env))))
530 (('call-with-prompt (tag thunk . handler))
537 (call/cc (eval proc env)))
539 (('module-set! (x . var-or-spec))
541 (if (variable? var-or-spec)
543 (memoize-variable-access! exp #f))
548 "Evaluate @var{exp} in the current module."
551 (if (macroexpanded? exp)
553 ((module-transformer (current-module)) exp)))