add <primcall> to tree-il
[bpt/guile.git] / module / ice-9 / eval.scm
CommitLineData
5161a3c0
AW
1;;; -*- mode: scheme; coding: utf-8; -*-
2
d69531e2 3;;;; Copyright (C) 2009, 2010
5161a3c0
AW
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
b2b554ef
AW
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)
5161a3c0
AW
40;;;
41
42;;; Code:
43
44\f
45
5161a3c0
AW
46(eval-when (compile)
47 (define-syntax capture-env
48 (syntax-rules ()
bb0c8157
AW
49 ((_ (exp ...))
50 (let ((env (exp ...)))
51 (capture-env env)))
5161a3c0
AW
52 ((_ env)
53 (if (null? env)
54 (current-module)
55 (if (not env)
b2b554ef
AW
56 ;; the and current-module checks that modules are booted,
57 ;; and thus the-root-module is defined
5f161164 58 (and (current-module) the-root-module)
5161a3c0
AW
59 env)))))
60
d8a071fc
AW
61 ;; Fast case for procedures with fixed arities.
62 (define-syntax make-fixed-closure
4abb824c 63 (lambda (x)
9331f91c 64 (define *max-static-argument-count* 8)
4abb824c
AW
65 (define (make-formals n)
66 (map (lambda (i)
67 (datum->syntax
68 x
69 (string->symbol
70 (string (integer->char (+ (char->integer #\a) i))))))
71 (iota n)))
72 (syntax-case x ()
d8a071fc 73 ((_ eval nreq body env) (not (identifier? #'env))
4abb824c 74 #'(let ((e env))
d8a071fc
AW
75 (make-fixed-closure eval nreq body e)))
76 ((_ eval nreq body env)
4abb824c
AW
77 #`(case nreq
78 #,@(map (lambda (nreq)
79 (let ((formals (make-formals nreq)))
80 #`((#,nreq)
d8a071fc
AW
81 (lambda (#,@formals)
82 (eval body
83 (cons* #,@(reverse formals) env))))))
4abb824c
AW
84 (iota *max-static-argument-count*))
85 (else
86 #,(let ((formals (make-formals *max-static-argument-count*)))
87 #`(lambda (#,@formals . more)
88 (let lp ((new-env (cons* #,@(reverse formals) env))
89 (nreq (- nreq #,*max-static-argument-count*))
90 (args more))
91 (if (zero? nreq)
92 (eval body
d8a071fc
AW
93 (if (null? args)
94 new-env
95 (scm-error 'wrong-number-of-args
96 "eval" "Wrong number of arguments"
97 '() #f)))
4abb824c
AW
98 (if (null? args)
99 (scm-error 'wrong-number-of-args
100 "eval" "Wrong number of arguments"
101 '() #f)
102 (lp (cons (car args) new-env)
103 (1- nreq)
104 (cdr args)))))))))))))
105
9331f91c
AW
106 (define-syntax call
107 (lambda (x)
108 (define *max-static-call-count* 4)
109 (syntax-case x ()
110 ((_ eval proc nargs args env) (identifier? #'env)
111 #`(case nargs
112 #,@(map (lambda (nargs)
113 #`((#,nargs)
114 (proc
115 #,@(map
116 (lambda (n)
117 (let lp ((n n) (args #'args))
118 (if (zero? n)
119 #`(eval (car #,args) env)
120 (lp (1- n) #`(cdr #,args)))))
121 (iota nargs)))))
122 (iota *max-static-call-count*))
123 (else
124 (apply proc
125 #,@(map
126 (lambda (n)
127 (let lp ((n n) (args #'args))
128 (if (zero? n)
129 #`(eval (car #,args) env)
130 (lp (1- n) #`(cdr #,args)))))
131 (iota *max-static-call-count*))
132 (let lp ((exps #,(let lp ((n *max-static-call-count*)
133 (args #'args))
134 (if (zero? n)
135 args
136 (lp (1- n) #`(cdr #,args)))))
137 (args '()))
138 (if (null? exps)
139 (reverse args)
140 (lp (cdr exps)
141 (cons (eval (car exps) env) args)))))))))))
142
b2b554ef
AW
143 ;; This macro could be more straightforward if the compiler had better
144 ;; copy propagation. As it is we do some copy propagation by hand.
5161a3c0
AW
145 (define-syntax mx-bind
146 (lambda (x)
147 (syntax-case x ()
148 ((_ data () body)
149 #'body)
150 ((_ data (a . b) body) (and (identifier? #'a) (identifier? #'b))
151 #'(let ((a (car data))
152 (b (cdr data)))
153 body))
154 ((_ data (a . b) body) (identifier? #'a)
155 #'(let ((a (car data))
156 (xb (cdr data)))
157 (mx-bind xb b body)))
158 ((_ data (a . b) body)
159 #'(let ((xa (car data))
160 (xb (cdr data)))
161 (mx-bind xa a (mx-bind xb b body))))
162 ((_ data v body) (identifier? #'v)
163 #'(let ((v data))
164 body)))))
165
b2b554ef
AW
166 ;; The resulting nested if statements will be an O(n) dispatch. Once
167 ;; we compile `case' effectively, this situation will improve.
5161a3c0
AW
168 (define-syntax mx-match
169 (lambda (x)
170 (syntax-case x (quote)
171 ((_ mx data tag)
172 #'(error "what" mx))
173 ((_ mx data tag (('type pat) body) c* ...)
174 #`(if (eqv? tag #,(or (memoized-typecode (syntax->datum #'type))
175 (error "not a typecode" #'type)))
176 (mx-bind data pat body)
177 (mx-match mx data tag c* ...))))))
178
179 (define-syntax memoized-expression-case
180 (lambda (x)
181 (syntax-case x ()
182 ((_ mx c ...)
183 #'(let ((tag (memoized-expression-typecode mx))
184 (data (memoized-expression-data mx)))
185 (mx-match mx data tag c ...)))))))
186
187
21ec0bd9
AW
188;;;
189;;; On 18 Feb 2010, I did a profile of how often the various memoized expression
190;;; types occur when getting to a prompt on a fresh build. Here are the numbers
191;;; I got:
192;;;
193;;; lexical-ref: 32933054
194;;; call: 20281547
195;;; toplevel-ref: 13228724
196;;; if: 9156156
197;;; quote: 6610137
198;;; let: 2619707
199;;; lambda: 1010921
200;;; begin: 948945
201;;; lexical-set: 509862
202;;; call-with-values: 139668
203;;; apply: 49402
204;;; module-ref: 14468
205;;; define: 1259
206;;; toplevel-set: 328
207;;; dynwind: 162
208;;; with-fluids: 0
209;;; call/cc: 0
210;;; module-set: 0
211;;;
212;;; So until we compile `case' into a computed goto, we'll order the clauses in
213;;; `eval' in this order, to put the most frequent cases first.
214;;;
215
5161a3c0
AW
216(define primitive-eval
217 (let ()
d8a071fc
AW
218 ;; We pre-generate procedures with fixed arities, up to some number of
219 ;; arguments; see make-fixed-closure above.
220
221 ;; A unique marker for unbound keywords.
222 (define unbound-arg (list 'unbound-arg))
223
7572ee52
AW
224 ;; Procedures with rest, optional, or keyword arguments, potentially with
225 ;; multiple arities, as with case-lambda.
d8a071fc 226 (define (make-general-closure env body nreq rest? nopt kw inits alt)
7572ee52
AW
227 (define alt-proc
228 (and alt
dc3e203e
AW
229 (let* ((body (car alt))
230 (nreq (cadr alt))
231 (rest (if (null? (cddr alt)) #f (caddr alt)))
232 (tail (and (pair? (cddr alt)) (pair? (cdddr alt)) (cdddr alt)))
233 (nopt (if tail (car tail) 0))
234 (kw (and tail (cadr tail)))
235 (inits (if tail (caddr tail) '()))
236 (alt (and tail (cadddr tail))))
237 (make-general-closure env body nreq rest nopt kw inits alt))))
7572ee52 238 (lambda %args
d8a071fc 239 (let lp ((env env)
7572ee52
AW
240 (nreq* nreq)
241 (args %args))
242 (if (> nreq* 0)
d8a071fc
AW
243 ;; First, bind required arguments.
244 (if (null? args)
7572ee52
AW
245 (if alt
246 (apply alt-proc %args)
247 (scm-error 'wrong-number-of-args
248 "eval" "Wrong number of arguments"
249 '() #f))
d8a071fc 250 (lp (cons (car args) env)
7572ee52 251 (1- nreq*)
d8a071fc
AW
252 (cdr args)))
253 ;; Move on to optional arguments.
254 (if (not kw)
255 ;; Without keywords, bind optionals from arguments.
256 (let lp ((env env)
257 (nopt nopt)
258 (args args)
259 (inits inits))
260 (if (zero? nopt)
261 (if rest?
262 (eval body (cons args env))
263 (if (null? args)
264 (eval body env)
7572ee52
AW
265 (if alt
266 (apply alt-proc %args)
267 (scm-error 'wrong-number-of-args
268 "eval" "Wrong number of arguments"
269 '() #f))))
d8a071fc
AW
270 (if (null? args)
271 (lp (cons (eval (car inits) env) env)
272 (1- nopt) args (cdr inits))
273 (lp (cons (car args) env)
274 (1- nopt) (cdr args) (cdr inits)))))
275 ;; With keywords, we stop binding optionals at the first
276 ;; keyword.
277 (let lp ((env env)
278 (nopt* nopt)
279 (args args)
280 (inits inits))
281 (if (> nopt* 0)
282 (if (or (null? args) (keyword? (car args)))
283 (lp (cons (eval (car inits) env) env)
284 (1- nopt*) args (cdr inits))
285 (lp (cons (car args) env)
286 (1- nopt*) (cdr args) (cdr inits)))
287 ;; Finished with optionals.
288 (let* ((aok (car kw))
289 (kw (cdr kw))
290 (kw-base (+ nopt nreq (if rest? 1 0)))
291 (imax (let lp ((imax (1- kw-base)) (kw kw))
292 (if (null? kw)
293 imax
294 (lp (max (cdar kw) imax)
295 (cdr kw)))))
296 ;; Fill in kwargs with "undefined" vals.
297 (env (let lp ((i kw-base)
298 ;; Also, here we bind the rest
299 ;; arg, if any.
300 (env (if rest? (cons args env) env)))
301 (if (<= i imax)
302 (lp (1+ i) (cons unbound-arg env))
303 env))))
304 ;; Now scan args for keywords.
305 (let lp ((args args))
306 (if (and (pair? args) (pair? (cdr args))
307 (keyword? (car args)))
308 (let ((kw-pair (assq (car args) kw))
309 (v (cadr args)))
310 (if kw-pair
311 ;; Found a known keyword; set its value.
312 (list-set! env (- imax (cdr kw-pair)) v)
313 ;; Unknown keyword.
314 (if (not aok)
315 (scm-error 'keyword-argument-error
316 "eval" "Unrecognized keyword"
317 '() #f)))
318 (lp (cddr args)))
319 (if (pair? args)
320 (if rest?
321 ;; Be lenient parsing rest args.
322 (lp (cdr args))
323 (scm-error 'keyword-argument-error
324 "eval" "Invalid keyword"
325 '() #f))
326 ;; Finished parsing keywords. Fill in
327 ;; uninitialized kwargs by evalling init
328 ;; expressions in their appropriate
329 ;; environment.
330 (let lp ((i (- imax kw-base))
331 (inits inits))
332 (if (pair? inits)
333 (let ((tail (list-tail env i)))
334 (if (eq? (car tail) unbound-arg)
335 (set-car! tail
336 (eval (car inits)
337 (cdr tail))))
338 (lp (1- i) (cdr inits)))
339 ;; Finally, eval the body.
340 (eval body env))))))))))))))
341
b2b554ef 342 ;; The "engine". EXP is a memoized expression.
5161a3c0
AW
343 (define (eval exp env)
344 (memoized-expression-case exp
21ec0bd9 345 (('lexical-ref n)
bb0c8157
AW
346 (list-ref env n))
347
21ec0bd9
AW
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
bb0c8157
AW
356 (memoize-variable-access! exp
357 (capture-env (if (pair? env)
358 (cdr (last-pair env))
359 env))))))
21ec0bd9 360
5161a3c0
AW
361 (('if (test consequent . alternate))
362 (if (eval test env)
363 (eval consequent env)
364 (eval alternate env)))
365
21ec0bd9
AW
366 (('quote x)
367 x)
368
5161a3c0
AW
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
8f9c5b58 376 (('lambda (body nreq . tail))
d8a071fc
AW
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
21ec0bd9
AW
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))))))
5161a3c0
AW
391
392 (('lexical-set! (n . x))
393 (let ((val (eval x env)))
bb0c8157 394 (list-set! env n val)))
5161a3c0 395
21ec0bd9
AW
396 (('call-with-values (producer . consumer))
397 (call-with-values (eval producer env)
398 (eval consumer env)))
399
400 (('apply (f args))
401 (apply (eval f env) (eval args env)))
402
403 (('module-ref var-or-spec)
5161a3c0 404 (variable-ref
21ec0bd9
AW
405 (if (variable? var-or-spec)
406 var-or-spec
407 (memoize-variable-access! exp #f))))
5161a3c0 408
21ec0bd9
AW
409 (('define (name . x))
410 (define! name (eval x env)))
411
5161a3c0
AW
412 (('toplevel-set! (var-or-sym . x))
413 (variable-set!
414 (if (variable? var-or-sym)
415 var-or-sym
bb0c8157
AW
416 (memoize-variable-access! exp
417 (capture-env (if (pair? env)
418 (cdr (last-pair env))
419 env))))
5161a3c0
AW
420 (eval x env)))
421
21ec0bd9
AW
422 (('dynwind (in exp . out))
423 (dynamic-wind (eval in env)
424 (lambda () (eval exp env))
425 (eval out env)))
426
bb0229b5
AW
427 (('with-fluids (fluids vals . exp))
428 (let* ((fluids (map (lambda (x) (eval x env)) fluids))
429 (vals (map (lambda (x) (eval x env)) vals)))
1371fe9b
AW
430 (let lp ((fluids fluids) (vals vals))
431 (if (null? fluids)
432 (eval exp env)
433 (with-fluids (((car fluids) (car vals)))
434 (lp (cdr fluids) (cdr vals)))))))
bb0229b5 435
747022e4
AW
436 (('prompt (tag exp . handler))
437 (@prompt (eval tag env)
438 (eval exp env)
439 (eval handler env)))
440
21ec0bd9
AW
441 (('call/cc proc)
442 (call/cc (eval proc env)))
5161a3c0
AW
443
444 (('module-set! (x . var-or-spec))
445 (variable-set!
446 (if (variable? var-or-spec)
447 var-or-spec
448 (memoize-variable-access! exp #f))
449 (eval x env)))))
450
b2b554ef 451 ;; primitive-eval
5161a3c0 452 (lambda (exp)
b2b554ef 453 "Evaluate @var{exp} in the current module."
5161a3c0 454 (eval
a310a1d1
AW
455 (memoize-expression
456 (if (macroexpanded? exp)
457 exp
458 ((module-transformer (current-module)) exp)))
5161a3c0 459 '()))))