primitive-eval takes expanded, not memoized, source
[bpt/guile.git] / module / ice-9 / eval.scm
1 ;;; -*- mode: scheme; coding: utf-8; -*-
2
3 ;;;; Copyright (C) 2009, 2010
4 ;;;; Free Software Foundation, Inc.
5 ;;;;
6 ;;;; This library is free software; you can redistribute it and/or
7 ;;;; modify it under the terms of the GNU Lesser General Public
8 ;;;; License as published by the Free Software Foundation; either
9 ;;;; version 3 of the License, or (at your option) any later version.
10 ;;;;
11 ;;;; This library is distributed in the hope that it will be useful,
12 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ;;;; Lesser General Public License for more details.
15 ;;;;
16 ;;;; You should have received a copy of the GNU Lesser General Public
17 ;;;; License along with this library; if not, write to the Free Software
18 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 ;;;;
20
21 \f
22
23 ;;; Commentary:
24
25 ;;; Scheme eval, written in Scheme.
26 ;;;
27 ;;; Expressions are first expanded, by the syntax expander (i.e.
28 ;;; psyntax), then memoized into internal forms. The evaluator itself
29 ;;; only operates on the internal forms ("memoized expressions").
30 ;;;
31 ;;; Environments are represented as linked lists of the form (VAL ... .
32 ;;; MOD). If MOD is #f, it means the environment was captured before
33 ;;; modules were booted. If MOD is the literal value '(), we are
34 ;;; evaluating at the top level, and so should track changes to the
35 ;;; current module.
36 ;;;
37 ;;; Evaluate this in Emacs to make code indentation work right:
38 ;;;
39 ;;; (put 'memoized-expression-case 'scheme-indent-function 1)
40 ;;;
41
42 ;;; Code:
43
44 \f
45
46 (eval-when (compile)
47 (define-syntax capture-env
48 (syntax-rules ()
49 ((_ env)
50 (if (null? env)
51 (current-module)
52 (if (not env)
53 ;; the and current-module checks that modules are booted,
54 ;; and thus the-root-module is defined
55 (and (current-module) the-root-module)
56 env)))))
57
58 ;; Fast case for procedures with fixed arities.
59 (define-syntax make-fixed-closure
60 (lambda (x)
61 (define *max-static-argument-count* 8)
62 (define (make-formals n)
63 (map (lambda (i)
64 (datum->syntax
65 x
66 (string->symbol
67 (string (integer->char (+ (char->integer #\a) i))))))
68 (iota n)))
69 (syntax-case x ()
70 ((_ eval nreq body env) (not (identifier? #'env))
71 #'(let ((e env))
72 (make-fixed-closure eval nreq body e)))
73 ((_ eval nreq body env)
74 #`(case nreq
75 #,@(map (lambda (nreq)
76 (let ((formals (make-formals nreq)))
77 #`((#,nreq)
78 (lambda (#,@formals)
79 (eval body
80 (cons* #,@(reverse formals) env))))))
81 (iota *max-static-argument-count*))
82 (else
83 #,(let ((formals (make-formals *max-static-argument-count*)))
84 #`(lambda (#,@formals . more)
85 (let lp ((new-env (cons* #,@(reverse formals) env))
86 (nreq (- nreq #,*max-static-argument-count*))
87 (args more))
88 (if (zero? nreq)
89 (eval body
90 (if (null? args)
91 new-env
92 (scm-error 'wrong-number-of-args
93 "eval" "Wrong number of arguments"
94 '() #f)))
95 (if (null? args)
96 (scm-error 'wrong-number-of-args
97 "eval" "Wrong number of arguments"
98 '() #f)
99 (lp (cons (car args) new-env)
100 (1- nreq)
101 (cdr args)))))))))))))
102
103 (define-syntax call
104 (lambda (x)
105 (define *max-static-call-count* 4)
106 (syntax-case x ()
107 ((_ eval proc nargs args env) (identifier? #'env)
108 #`(case nargs
109 #,@(map (lambda (nargs)
110 #`((#,nargs)
111 (proc
112 #,@(map
113 (lambda (n)
114 (let lp ((n n) (args #'args))
115 (if (zero? n)
116 #`(eval (car #,args) env)
117 (lp (1- n) #`(cdr #,args)))))
118 (iota nargs)))))
119 (iota *max-static-call-count*))
120 (else
121 (apply proc
122 #,@(map
123 (lambda (n)
124 (let lp ((n n) (args #'args))
125 (if (zero? n)
126 #`(eval (car #,args) env)
127 (lp (1- n) #`(cdr #,args)))))
128 (iota *max-static-call-count*))
129 (let lp ((exps #,(let lp ((n *max-static-call-count*)
130 (args #'args))
131 (if (zero? n)
132 args
133 (lp (1- n) #`(cdr #,args)))))
134 (args '()))
135 (if (null? exps)
136 (reverse args)
137 (lp (cdr exps)
138 (cons (eval (car exps) env) args)))))))))))
139
140 ;; This macro could be more straightforward if the compiler had better
141 ;; copy propagation. As it is we do some copy propagation by hand.
142 (define-syntax mx-bind
143 (lambda (x)
144 (syntax-case x ()
145 ((_ data () body)
146 #'body)
147 ((_ data (a . b) body) (and (identifier? #'a) (identifier? #'b))
148 #'(let ((a (car data))
149 (b (cdr data)))
150 body))
151 ((_ data (a . b) body) (identifier? #'a)
152 #'(let ((a (car data))
153 (xb (cdr data)))
154 (mx-bind xb b body)))
155 ((_ data (a . b) body)
156 #'(let ((xa (car data))
157 (xb (cdr data)))
158 (mx-bind xa a (mx-bind xb b body))))
159 ((_ data v body) (identifier? #'v)
160 #'(let ((v data))
161 body)))))
162
163 ;; The resulting nested if statements will be an O(n) dispatch. Once
164 ;; we compile `case' effectively, this situation will improve.
165 (define-syntax mx-match
166 (lambda (x)
167 (syntax-case x (quote)
168 ((_ mx data tag)
169 #'(error "what" mx))
170 ((_ mx data tag (('type pat) body) c* ...)
171 #`(if (eqv? tag #,(or (memoized-typecode (syntax->datum #'type))
172 (error "not a typecode" #'type)))
173 (mx-bind data pat body)
174 (mx-match mx data tag c* ...))))))
175
176 (define-syntax memoized-expression-case
177 (lambda (x)
178 (syntax-case x ()
179 ((_ mx c ...)
180 #'(let ((tag (memoized-expression-typecode mx))
181 (data (memoized-expression-data mx)))
182 (mx-match mx data tag c ...)))))))
183
184
185 ;;;
186 ;;; On 18 Feb 2010, I did a profile of how often the various memoized expression
187 ;;; types occur when getting to a prompt on a fresh build. Here are the numbers
188 ;;; I got:
189 ;;;
190 ;;; lexical-ref: 32933054
191 ;;; call: 20281547
192 ;;; toplevel-ref: 13228724
193 ;;; if: 9156156
194 ;;; quote: 6610137
195 ;;; let: 2619707
196 ;;; lambda: 1010921
197 ;;; begin: 948945
198 ;;; lexical-set: 509862
199 ;;; call-with-values: 139668
200 ;;; apply: 49402
201 ;;; module-ref: 14468
202 ;;; define: 1259
203 ;;; toplevel-set: 328
204 ;;; dynwind: 162
205 ;;; with-fluids: 0
206 ;;; call/cc: 0
207 ;;; module-set: 0
208 ;;;
209 ;;; So until we compile `case' into a computed goto, we'll order the clauses in
210 ;;; `eval' in this order, to put the most frequent cases first.
211 ;;;
212
213 (define primitive-eval
214 (let ()
215 ;; We pre-generate procedures with fixed arities, up to some number of
216 ;; arguments; see make-fixed-closure above.
217
218 ;; A unique marker for unbound keywords.
219 (define unbound-arg (list 'unbound-arg))
220
221 ;; Procedures with rest, optional, or keyword arguments, potentially with
222 ;; multiple arities, as with case-lambda.
223 (define (make-general-closure env body nreq rest? nopt kw inits alt)
224 (define alt-proc
225 (and alt
226 (let* ((body (car alt))
227 (nreq (cadr alt))
228 (rest (if (null? (cddr alt)) #f (caddr alt)))
229 (tail (and (pair? (cddr alt)) (pair? (cdddr alt)) (cdddr alt)))
230 (nopt (if tail (car tail) 0))
231 (kw (and tail (cadr tail)))
232 (inits (if tail (caddr tail) '()))
233 (alt (and tail (cadddr tail))))
234 (make-general-closure env body nreq rest nopt kw inits alt))))
235 (lambda %args
236 (let lp ((env env)
237 (nreq* nreq)
238 (args %args))
239 (if (> nreq* 0)
240 ;; First, bind required arguments.
241 (if (null? args)
242 (if alt
243 (apply alt-proc %args)
244 (scm-error 'wrong-number-of-args
245 "eval" "Wrong number of arguments"
246 '() #f))
247 (lp (cons (car args) env)
248 (1- nreq*)
249 (cdr args)))
250 ;; Move on to optional arguments.
251 (if (not kw)
252 ;; Without keywords, bind optionals from arguments.
253 (let lp ((env env)
254 (nopt nopt)
255 (args args)
256 (inits inits))
257 (if (zero? nopt)
258 (if rest?
259 (eval body (cons args env))
260 (if (null? args)
261 (eval body env)
262 (if alt
263 (apply alt-proc %args)
264 (scm-error 'wrong-number-of-args
265 "eval" "Wrong number of arguments"
266 '() #f))))
267 (if (null? args)
268 (lp (cons (eval (car inits) env) env)
269 (1- nopt) args (cdr inits))
270 (lp (cons (car args) env)
271 (1- nopt) (cdr args) (cdr inits)))))
272 ;; With keywords, we stop binding optionals at the first
273 ;; keyword.
274 (let lp ((env env)
275 (nopt* nopt)
276 (args args)
277 (inits inits))
278 (if (> nopt* 0)
279 (if (or (null? args) (keyword? (car args)))
280 (lp (cons (eval (car inits) env) env)
281 (1- nopt*) args (cdr inits))
282 (lp (cons (car args) env)
283 (1- nopt*) (cdr args) (cdr inits)))
284 ;; Finished with optionals.
285 (let* ((aok (car kw))
286 (kw (cdr kw))
287 (kw-base (+ nopt nreq (if rest? 1 0)))
288 (imax (let lp ((imax (1- kw-base)) (kw kw))
289 (if (null? kw)
290 imax
291 (lp (max (cdar kw) imax)
292 (cdr kw)))))
293 ;; Fill in kwargs with "undefined" vals.
294 (env (let lp ((i kw-base)
295 ;; Also, here we bind the rest
296 ;; arg, if any.
297 (env (if rest? (cons args env) env)))
298 (if (<= i imax)
299 (lp (1+ i) (cons unbound-arg env))
300 env))))
301 ;; Now scan args for keywords.
302 (let lp ((args args))
303 (if (and (pair? args) (pair? (cdr args))
304 (keyword? (car args)))
305 (let ((kw-pair (assq (car args) kw))
306 (v (cadr args)))
307 (if kw-pair
308 ;; Found a known keyword; set its value.
309 (list-set! env (- imax (cdr kw-pair)) v)
310 ;; Unknown keyword.
311 (if (not aok)
312 (scm-error 'keyword-argument-error
313 "eval" "Unrecognized keyword"
314 '() #f)))
315 (lp (cddr args)))
316 (if (pair? args)
317 (if rest?
318 ;; Be lenient parsing rest args.
319 (lp (cdr args))
320 (scm-error 'keyword-argument-error
321 "eval" "Invalid keyword"
322 '() #f))
323 ;; Finished parsing keywords. Fill in
324 ;; uninitialized kwargs by evalling init
325 ;; expressions in their appropriate
326 ;; environment.
327 (let lp ((i (- imax kw-base))
328 (inits inits))
329 (if (pair? inits)
330 (let ((tail (list-tail env i)))
331 (if (eq? (car tail) unbound-arg)
332 (set-car! tail
333 (eval (car inits)
334 (cdr tail))))
335 (lp (1- i) (cdr inits)))
336 ;; Finally, eval the body.
337 (eval body env))))))))))))))
338
339 ;; The "engine". EXP is a memoized expression.
340 (define (eval exp env)
341 (memoized-expression-case exp
342 (('lexical-ref n)
343 (let lp ((n n) (env env))
344 (if (zero? n)
345 (car env)
346 (lp (1- n) (cdr env)))))
347
348 (('call (f nargs . args))
349 (let ((proc (eval f env)))
350 (call eval proc nargs args env)))
351
352 (('toplevel-ref var-or-sym)
353 (variable-ref
354 (if (variable? var-or-sym)
355 var-or-sym
356 (let lp ((env env))
357 (if (pair? env)
358 (lp (cdr env))
359 (memoize-variable-access! exp (capture-env env)))))))
360
361 (('if (test consequent . alternate))
362 (if (eval test env)
363 (eval consequent env)
364 (eval alternate env)))
365
366 (('quote x)
367 x)
368
369 (('let (inits . body))
370 (let lp ((inits inits) (new-env (capture-env env)))
371 (if (null? inits)
372 (eval body new-env)
373 (lp (cdr inits)
374 (cons (eval (car inits) env) new-env)))))
375
376 (('lambda (body nreq . tail))
377 (if (null? tail)
378 (make-fixed-closure eval nreq body (capture-env env))
379 (if (null? (cdr tail))
380 (make-general-closure (capture-env env) body nreq (car tail)
381 0 #f '() #f)
382 (apply make-general-closure (capture-env env) body nreq tail))))
383
384 (('begin (first . rest))
385 (let lp ((first first) (rest rest))
386 (if (null? rest)
387 (eval first env)
388 (begin
389 (eval first env)
390 (lp (car rest) (cdr rest))))))
391
392 (('lexical-set! (n . x))
393 (let ((val (eval x env)))
394 (let lp ((n n) (env env))
395 (if (zero? n)
396 (set-car! env val)
397 (lp (1- n) (cdr env))))))
398
399 (('call-with-values (producer . consumer))
400 (call-with-values (eval producer env)
401 (eval consumer env)))
402
403 (('apply (f args))
404 (apply (eval f env) (eval args env)))
405
406 (('module-ref var-or-spec)
407 (variable-ref
408 (if (variable? var-or-spec)
409 var-or-spec
410 (memoize-variable-access! exp #f))))
411
412 (('define (name . x))
413 (define! name (eval x env)))
414
415 (('toplevel-set! (var-or-sym . x))
416 (variable-set!
417 (if (variable? var-or-sym)
418 var-or-sym
419 (let lp ((env env))
420 (if (pair? env)
421 (lp (cdr env))
422 (memoize-variable-access! exp (capture-env env)))))
423 (eval x env)))
424
425 (('dynwind (in exp . out))
426 (dynamic-wind (eval in env)
427 (lambda () (eval exp env))
428 (eval out env)))
429
430 (('with-fluids (fluids vals . exp))
431 (let* ((fluids (map (lambda (x) (eval x env)) fluids))
432 (vals (map (lambda (x) (eval x env)) vals)))
433 (let lp ((fluids fluids) (vals vals))
434 (if (null? fluids)
435 (eval exp env)
436 (with-fluids (((car fluids) (car vals)))
437 (lp (cdr fluids) (cdr vals)))))))
438
439 (('prompt (tag exp . handler))
440 (@prompt (eval tag env)
441 (eval exp env)
442 (eval handler env)))
443
444 (('call/cc proc)
445 (call/cc (eval proc env)))
446
447 (('module-set! (x . var-or-spec))
448 (variable-set!
449 (if (variable? var-or-spec)
450 var-or-spec
451 (memoize-variable-access! exp #f))
452 (eval x env)))))
453
454 ;; primitive-eval
455 (lambda (exp)
456 "Evaluate @var{exp} in the current module."
457 (eval
458 (memoize-expression
459 (if (macroexpanded? exp)
460 exp
461 ((module-transformer (current-module)) exp)))
462 '()))))