1 ;;; -*- mode: scheme; coding: utf-8; -*-
3 ;;;; Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 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 env-toplevel
54 (define-syntax make-env
57 (let ((v (make-vector (1+ n) init)))
58 (vector-set! v 0 next)
61 (define-syntax make-env*
64 (vector next init ...))))
66 (define-syntax env-ref
69 (let lp ((e env) (d depth))
71 (vector-ref e (1+ width))
72 (lp (vector-ref e 0) (1- d)))))))
74 (define-syntax env-set!
76 ((_ env depth width val)
77 (let lp ((e env) (d depth))
79 (vector-set! e (1+ width) val)
80 (lp (vector-ref e 0) (1- d)))))))
82 ;; For evaluating the initializers in a "let" expression. We have to
83 ;; evaluate the initializers before creating the environment rib, to
84 ;; prevent continuation-related shenanigans; see
85 ;; http://wingolog.org/archives/2013/11/02/scheme-quiz-time for a
88 ;; This macro will inline evaluation of the first N initializers.
89 ;; That number N is indicated by the number of template arguments
90 ;; passed to the macro. It's a bit nasty but it's flexible and
92 (define-syntax let-env-evaluator
94 ((eval-and-make-env eval env (template ...))
96 (define-syntax eval-and-make-env
98 ((eval-and-make-env inits width (template ...) k)
99 (let lp ((n (length '(template ...))) (vals '()))
101 (let ((env (make-env n #f env)))
102 (let lp ((n (1- n)) (vals vals))
106 (env-set! env 0 n (car vals))
107 (lp (1- n) (cdr vals))))))
109 (cons (eval (vector-ref inits n) env) vals)))))
110 ((eval-and-make-env inits width (var (... ...)) k)
111 (let ((n (length '(var (... ...)))))
113 (k (make-env n #f env))
114 (let* ((x (eval (vector-ref inits n) env))
118 (eval-and-make-env inits width (x var (... ...)) k)))))))
120 (let ((width (vector-length inits))
121 (k (lambda (env) env)))
122 (eval-and-make-env inits width () k)))))))
124 ;; Fast case for procedures with fixed arities.
125 (define-syntax make-fixed-closure
127 (define *max-static-argument-count* 8)
128 (define (make-formals n)
133 (string (integer->char (+ (char->integer #\a) i))))))
136 ((_ eval nreq body env) (not (identifier? #'env))
138 (make-fixed-closure eval nreq body e)))
139 ((_ eval nreq body env)
141 #,@(map (lambda (nreq)
142 (let ((formals (make-formals nreq)))
146 (make-env* env #,@formals))))))
147 (iota *max-static-argument-count*))
149 #,(let ((formals (make-formals *max-static-argument-count*)))
150 #`(lambda (#,@formals . more)
151 (let ((env (make-env nreq #f env)))
152 #,@(map (lambda (formal n)
153 #`(env-set! env 0 #,n #,formal))
154 formals (iota (length formals)))
155 (let lp ((i #,*max-static-argument-count*)
162 (scm-error 'wrong-number-of-args
163 "eval" "Wrong number of arguments"
166 (scm-error 'wrong-number-of-args
167 "eval" "Wrong number of arguments"
170 (env-set! env 0 i (car args))
171 (lp (1+ i) (cdr args))))))))))))))
173 ;; Fast case for procedures with fixed arities and a rest argument.
174 (define-syntax make-rest-closure
176 (define *max-static-argument-count* 3)
177 (define (make-formals n)
182 (string (integer->char (+ (char->integer #\a) i))))))
185 ((_ eval nreq body env) (not (identifier? #'env))
187 (make-rest-closure eval nreq body e)))
188 ((_ eval nreq body env)
190 #,@(map (lambda (nreq)
191 (let ((formals (make-formals nreq)))
193 (lambda (#,@formals . rest)
195 (make-env* env #,@formals rest))))))
196 (iota *max-static-argument-count*))
198 #,(let ((formals (make-formals *max-static-argument-count*)))
199 #`(lambda (#,@formals . more)
200 (let ((env (make-env (1+ nreq) #f env)))
201 #,@(map (lambda (formal n)
202 #`(env-set! env 0 #,n #,formal))
203 formals (iota (length formals)))
204 (let lp ((i #,*max-static-argument-count*)
208 (env-set! env 0 nreq args)
211 (scm-error 'wrong-number-of-args
212 "eval" "Wrong number of arguments"
215 (env-set! env 0 i (car args))
216 (lp (1+ i) (cdr args))))))))))))))
220 (define *max-static-call-count* 4)
222 ((_ eval proc nargs args env) (identifier? #'env)
224 #,@(map (lambda (nargs)
229 (let lp ((n n) (args #'args))
231 #`(eval (car #,args) env)
232 (lp (1- n) #`(cdr #,args)))))
234 (iota *max-static-call-count*))
239 (let lp ((n n) (args #'args))
241 #`(eval (car #,args) env)
242 (lp (1- n) #`(cdr #,args)))))
243 (iota *max-static-call-count*))
244 (let lp ((exps #,(let lp ((n *max-static-call-count*)
248 (lp (1- n) #`(cdr #,args)))))
253 (cons (eval (car exps) env) args)))))))))))
255 ;; This macro could be more straightforward if the compiler had better
256 ;; copy propagation. As it is we do some copy propagation by hand.
257 (define-syntax mx-bind
262 ((_ data (a . b) body) (and (identifier? #'a) (identifier? #'b))
263 #'(let ((a (car data))
266 ((_ data (a . b) body) (identifier? #'a)
267 #'(let ((a (car data))
269 (mx-bind xb b body)))
270 ((_ data (a . b) body)
271 #'(let ((xa (car data))
273 (mx-bind xa a (mx-bind xb b body))))
274 ((_ data v body) (identifier? #'v)
278 ;; The resulting nested if statements will be an O(n) dispatch. Once
279 ;; we compile `case' effectively, this situation will improve.
280 (define-syntax mx-match
282 (syntax-case x (quote)
285 ((_ mx data tag (('type pat) body) c* ...)
286 #`(if (eqv? tag #,(or (memoized-typecode (syntax->datum #'type))
287 (error "not a typecode" #'type)))
288 (mx-bind data pat body)
289 (mx-match mx data tag c* ...))))))
291 (define-syntax memoized-expression-case
295 #'(let ((tag (car mx))
297 (mx-match mx data tag c ...)))))))
301 ;;; On 18 Feb 2010, I did a profile of how often the various memoized expression
302 ;;; types occur when getting to a prompt on a fresh build. Here are the numbers
305 ;;; lexical-ref: 32933054
307 ;;; toplevel-ref: 13228724
313 ;;; lexical-set: 509862
314 ;;; call-with-values: 139668
316 ;;; module-ref: 14468
318 ;;; toplevel-set: 328
322 ;;; So until we compile `case' into a computed goto, we'll order the clauses in
323 ;;; `eval' in this order, to put the most frequent cases first.
326 (define primitive-eval
328 ;; We pre-generate procedures with fixed arities, up to some number
329 ;; of arguments, and some rest arities; see make-fixed-closure and
330 ;; make-rest-closure above.
332 ;; A unique marker for unbound keywords. NB: There should be no
333 ;; other instance of '(unbound-arg) in this compilation unit, so
334 ;; that this marker is indeed unique. It's a hack, but it allows
335 ;; the constant to propagate to inner closures, reducing free
336 ;; variable counts all around, so it is important for perf.
337 (define unbound-arg '(unbound-arg))
339 ;; Procedures with rest, optional, or keyword arguments, potentially with
340 ;; multiple arities, as with case-lambda.
341 (define (make-general-closure env body nreq rest? nopt kw inits alt)
343 (and alt ; (body meta nreq ...)
344 (let* ((body (car alt))
347 (rest (if (null? (cdr spec)) #f (cadr spec)))
348 (tail (and (pair? (cdr spec)) (pair? (cddr spec)) (cddr spec)))
349 (nopt (if tail (car tail) 0))
350 (kw (and tail (cadr tail)))
351 (inits (if tail (caddr tail) '()))
352 (alt (and tail (cadddr tail))))
353 (make-general-closure env body nreq rest nopt kw inits alt))))
354 (define (set-procedure-arity! proc)
355 (let lp ((alt alt) (nreq nreq) (nopt nopt) (rest? rest?))
358 (set-procedure-property! proc 'arglist
364 (set-procedure-minimum-arity! proc nreq nopt rest?))
365 (let* ((spec (cddr alt))
367 (rest?* (if (null? (cdr spec)) #f (cadr spec)))
368 (tail (and (pair? (cdr spec)) (pair? (cddr spec)) (cddr spec)))
369 (nopt* (if tail (car tail) 0))
370 (alt* (and tail (cadddr tail))))
371 (if (or (< nreq* nreq)
374 (and rest?* (> nopt* nopt))
375 (or rest?* (> nopt* nopt)))))
376 (lp alt* nreq* nopt* rest?*)
377 (lp alt* nreq nopt rest?)))))
379 (set-procedure-arity!
381 (define (npositional args)
382 (let lp ((n 0) (args args))
384 (and (>= n nreq) (keyword? (car args))))
386 (lp (1+ n) (cdr args)))))
387 (let ((nargs (length %args)))
390 (and (not kw) (not rest?) (> nargs (+ nreq nopt)))
391 (and alt kw (not rest?) (> (npositional %args) (+ nreq nopt))))
393 (apply alt-proc %args)
394 ((scm-error 'wrong-number-of-args
395 "eval" "Wrong number of arguments"
398 (let* ((nvals (+ nreq (if rest? 1 0) (length inits)))
399 (env (make-env nvals unbound-arg env)))
400 (let lp ((i 0) (args %args))
403 ;; Bind required arguments.
404 (env-set! env 0 i (car args))
405 (lp (1+ i) (cdr args)))
407 ;; Optional args (possibly), but no keyword args.
408 (let lp ((i i) (args args) (inits inits))
413 (env-set! env 0 i (car args))
414 (lp (1+ i) (cdr args) (cdr inits)))
416 (env-set! env 0 i (eval (car inits) env))
417 (lp (1+ i) args (cdr inits)))))
420 (env-set! env 0 i args))
423 ;; Optional args. As before, but stop at the first
425 (let lp ((i i) (args args) (inits inits))
429 ((and (< i nargs) (not (keyword? (car args))))
430 (env-set! env 0 i (car args))
431 (lp (1+ i) (cdr args) (cdr inits)))
433 (env-set! env 0 i (eval (car inits) env))
434 (lp (1+ i) args (cdr inits)))))
437 (env-set! env 0 i args))
440 (kw-base (if rest? (1+ i) i)))
441 ;; Now scan args for keywords.
442 (let lp ((args args))
444 ((and (pair? args) (pair? (cdr args))
445 (keyword? (car args)))
446 (let ((kw-pair (assq (car args) kw))
449 ;; Found a known keyword; set its value.
450 (env-set! env 0 (cdr kw-pair) v)
454 'keyword-argument-error
455 "eval" "Unrecognized keyword"
456 '() (list (car args))))))
460 ;; Be lenient parsing rest args.
462 ((scm-error 'keyword-argument-error
463 "eval" "Invalid keyword"
464 '() (list (car args))))))
466 ;; Finished parsing keywords. Fill in
467 ;; uninitialized kwargs by evalling init
468 ;; expressions in their appropriate
470 (let lp ((i kw-base) (inits inits))
473 (when (eq? (env-ref env 0 i) unbound-arg)
474 (env-set! env 0 i (eval (car inits) env)))
475 (lp (1+ i) (cdr inits)))
477 ;; Finally, eval the body.
478 (eval body env)))))))))))))))))))))
480 ;; The "engine". EXP is a memoized expression.
481 (define (eval exp env)
482 (memoized-expression-case exp
483 (('lexical-ref (depth . width))
484 (env-ref env depth width))
486 (('call (f nargs . args))
487 (let ((proc (eval f env)))
488 (call eval proc nargs args env)))
490 (('toplevel-ref var-or-sym)
492 (if (variable? var-or-sym)
494 (memoize-variable-access! exp (env-toplevel env)))))
496 (('if (test consequent . alternate))
498 (eval consequent env)
499 (eval alternate env)))
504 (('let (inits . body))
505 (eval body ((let-env-evaluator eval env (_ _ _ _)) inits)))
507 (('lambda (body meta nreq . tail))
510 (make-fixed-closure eval nreq body env)
514 (make-rest-closure eval nreq body env)
516 tail (nopt kw inits alt)
517 (make-general-closure env body nreq rest?
518 nopt kw inits alt)))))))
519 (let lp ((meta meta))
521 (set-procedure-property! proc (caar meta) (cdar meta))
525 (('seq (head . tail))
530 (('lexical-set! ((depth . width) . x))
531 (env-set! env depth width (eval x env)))
533 (('call-with-values (producer . consumer))
534 (call-with-values (eval producer env)
535 (eval consumer env)))
538 (apply (eval f env) (eval args env)))
540 (('module-ref var-or-spec)
542 (if (variable? var-or-spec)
544 (memoize-variable-access! exp #f))))
546 (('define (name . x))
548 (define! name (eval x env))
552 (eval x (current-module)))
554 (('toplevel-set! (var-or-sym . x))
556 (if (variable? var-or-sym)
558 (memoize-variable-access! exp (env-toplevel env)))
561 (('call-with-prompt (tag thunk . handler))
568 (call/cc (eval proc env)))
570 (('module-set! (x . var-or-spec))
572 (if (variable? var-or-spec)
574 (memoize-variable-access! exp #f))
579 "Evaluate @var{exp} in the current module."
582 (if (macroexpanded? exp)
584 ((module-transformer (current-module)) exp)))