1 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; This software is derived from the CMU CL system, which was
5 ;;;; written at Carnegie Mellon University and released into the
6 ;;;; public domain. The software is in the public domain and is
7 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
8 ;;;; files for more information.
10 (in-package "SB-INTERPRETER")
12 ;;;; This interpreter is a hybrid of a traditional recursive descent EVAL,
13 ;;;; augmented with some semantic preprocessing. But unlike most other
14 ;;;; preprocessing implementations which produce code which emulates compiled
15 ;;;; code as closely as possible by performing macroexpansion once only,
16 ;;;; we attempt to emulate a non-preprocessing interpreter.
17 ;;;; The motivation for this is parenthetically revealed in the X3J13 issue
18 ;;;; discussing the removal of COMPILER-LET, saying:
19 ;;;; "Some users have indicated they dislike interpreters which do a semantic
20 ;;;; prepass, because they like to be able to dynamically redefine macros
21 ;;;; while debugging."
22 ;;;; In addition, preprocessing of a form is done as late as possible -
23 ;;;; only when a form is reached - and only enough to descend one level.
25 ;;; SEXPRs are the basic building blocks of interpreted code.
26 ;;; We store a function to call (in the HANDLER) as well as the original
27 ;;; list representation of the form in case it has to be re-preprocessed.
28 (defstruct (sexpr (:conc-name sexpr-
)
29 (:constructor %make-sexpr
(handler %form
))
31 (handler nil
:type cons
)
32 (%form nil
:read-only t
)) ; the original source form
34 ;;; A SEXPR's HANDLER is a cons. This constructs a HANDLER.
35 (defun %handler
(function payload
) (cons function payload
))
37 ;;; Not all "sexprs" are actually instances of SEXPR,
38 ;;; because self-evaluating constants are stored as-is.
39 ;;; Get the original source out of a SEXPR-equivalent object.
40 (defun sexpr-form (object)
41 (if (%instancep object
)
42 (progn (aver (sexpr-p object
)) (sexpr-%form object
))
45 ;;; Wrapper to hide the argument passing convention.
46 ;;; This is preferable to using a closure over the handler-function and args
47 ;;; because it makes for better inspectability and is often more compact.
48 ;;; The smallest closure requires 4 words; this has a lower bound of 2 words.
50 ;;; FIXME: the preceding claim about inspectability is no longer true now that
51 ;;; we have STORE-CLOSURE-DEBUG-POINTER which can preserve both the lexical
52 ;;; and dynamic state. And the efficiency claim might never have been true.
53 (defmacro handler
(function &rest args
)
55 `(%handler
,function
0))
57 `(%handler
,function
,(first args
)))
59 `(%handler
,function
(cons ,(first args
) ,(second args
))))
61 `(%handler
,function
(vector ,@args
)))))
63 (defmacro with-form-data
(names input
&body body
)
64 (declare (list names
))
67 (1 `((,(first names
) ,input
)))
68 (2 `((,(car names
) (car ,input
)) (,(cadr names
) (cdr ,input
))))
69 (t (loop for name in names for i from
0
70 collect
`(,name
(svref ,input
,i
)))))
73 ;;; Wrapper on LAMBDA for writing handlers.
74 ;;; (HLAMBDA NAME (DATA ENV SEXPR) ...)
75 ;;; -> (NAMED-LAMBDA (EVAL NAME) (#:DATA ENV SEXPR)
76 ;;; (WITH-FORM-DATA DATA #:DATA ...)
78 ;;; If DATA is a symbol, it is used literally as the first formal parameter
79 ;;; in the lambda; if it is a list, the lambda receives one thing which is
80 ;;; destructured according to the argument passing convention.
82 ;;; If SEXPR is omitted it becomes a gensym which gets declared ignore.
83 ;;; All handlers have identical signature but most don't use the third argument.
84 ;;; Only a handler which can restart by discarding internal state needs it.
86 (defmacro hlambda
(name captured-vars vars
&body body
)
87 (declare (list captured-vars
))
88 (let ((data (gensym "DATA")) (ignore))
89 ;; Allow &AUX vars: (HLAMBDA NAME (DATA ENV &AUX FOO) ...) has no 3rd arg.
90 (let ((aux-vars (member '&aux vars
)))
91 (when (= (length (ldiff vars aux-vars
)) 1)
92 (let ((sexpr (make-symbol "SEXPR")))
93 (setq vars
(nconc (subseq vars
0 1) (list sexpr
) aux-vars
)
94 ignore
`((ignore ,sexpr
))))))
95 (setq name
(cons '.eval.
(if (listp name
) name
(list name
))))
96 `(handler ,(if captured-vars
97 (if (= (length captured-vars
) 1)
98 `(named-lambda ,name
(,(first captured-vars
) ,@vars
)
99 (declare ,+handler-optimize
+ ,@ignore
)
101 `(named-lambda ,name
(,data
,@vars
)
102 (declare ,+handler-optimize
+ ,@ignore
)
103 (with-form-data ,captured-vars
,data
,@body
)))
104 `(named-lambda ,name
(,data
,@vars
)
105 (declare ,+handler-optimize
+ (ignore ,data
) ,@ignore
)
109 ;;; DISPATCH receives a SEXPR object or sexpr-equivalent object.
110 ;;; If a SEXPR, its handler is called to produce a value. All other Lisp
111 ;;; objects are treated as constants.
112 ;;; N.B.: herein SEXPR sometimes mean either a SEXPR object, or a constant
113 ;;; object that needs no wrapping. The union of those could be envisioned
114 ;;; as a deftype DISPATCHABLE - which is nowise helpful in practice.
116 ;;; It is always ok to call DISPATCH on a SEXPR-equivalent object, but in two
117 ;;; places %DISPATCH is used to avoid a spurious code-deletion note where
118 ;;; SEXPR is known to be a dispatchable instance. This is one of the
119 ;;; "spurious but not wrong" notes that the compiler can emit.
120 (declaim (inline dispatch %dispatch
))
121 (defun %dispatch
(sexpr env
)
122 (declare #.
+handler-optimize
+)
123 (let ((handler (sexpr-handler sexpr
)))
124 (funcall (the function
(car handler
)) (cdr handler
) env sexpr
)))
125 (defun dispatch (sexpr env
)
126 (declare #.
+handler-optimize
+)
127 (if (%instancep sexpr
) (%dispatch sexpr env
) sexpr
))
129 ;;; Helper for RETURN-CONSTANT.
130 ;;; Would be a local function except for some uses of LOAD-TIME-VALUE.
131 (defun %const
(x env sexpr
)
132 (declare (ignore env sexpr
) #.
+handler-optimize
+)
135 ;;; Return a handler that returns a constant.
136 ;;; The %SEXPR constructor elides a handler for constants,
137 ;;; but there are cases where NIL sneaks through and demands a callable handler.
138 ;;; We avoid generating N copies of such handler. Same goes for 0 and T.
139 (defun return-constant (object)
140 (cond ((null object
) (load-time-value (handler #'%const nil
)))
141 ((eq object t
) (load-time-value (handler #'%const t
)))
142 ((eql object
0) (load-time-value (handler #'%const
0)))
143 (t (handler #'%const object
))))
145 (defun return-constant-values (values env sexpr
)
146 (declare (ignore env sexpr
) #.
+handler-optimize
+)
147 (values-list values
))
150 (declaim (ftype function digest-form %eval eval-setq
))
152 ;;; Return a SEXPR object that will, when dispatched, evaluate FORM.
153 ;;; Unlike optimizations on SEXPRS whereby they install the most-specific
154 ;;; applicable handler for a given source form when first dispatched, constants
155 ;;; can't do that because by the time DIGEST-FORM is dispatched, it's too late
156 ;;; to install a function other than #'RETURN-CONSTANT. The goal here is to
157 ;;; avoid funcalling anything on constant forms. Therefore:
158 ;;; 1. For most self-evaluating objects, FORM itself is returned.
159 ;;; Structures are self-evaluating, but have to be wrapped because DISPATCH
160 ;;; assumes that anything satisfying %INSTANCEP is a dispatchable object.
161 ;;; 2. If FORM is a syntactically correct QUOTE form, return the quoted thing
162 ;;; subject to the constraint in condition 1. SEXPRs are prepared in advance
163 ;;; of being EVALed, so no errors may be signaled on malformed QUOTE forms.
165 ;;; Todo: there is still more opportunity for lifting singleton PROGN forms.
166 ;;; Lots of standard macros expand into (PROGN (ONE-THING)), which, if it appears
167 ;;; as the argument to %SEXPR should just become a SEXPR for (ONE-THING).
171 (cond ((and (atom form
)
172 (not (%instancep form
))
173 (or (not (symbolp form
))
174 ;; Only builtin constants and keywords are assumed
175 ;; to be written in stone. Others generate a continuable
176 ;; error on redefinition, and this eval will always
177 ;; get the current definition.
178 (and (eq (info :variable
:kind form
) :constant
)
179 (memq (symbol-package form
)
180 (load-time-value (list (find-package "KEYWORD")
181 (find-package "CL"))))
182 (progn (setq form
(symbol-value form
)) t
))))
185 (eq (car form
) 'quote
)
186 (consp (setq cdr
(cdr form
)))
188 (not (%instancep
(setq obj
(car cdr
)))))
190 (t ; DIGEST-FORM is the generic handler
191 (%make-sexpr
(handler #'digest-form form
) form
)))))
193 ;;; Return a SEXPR that will evaluate zero or more FORMS.
194 (defun %progn
(forms)
195 ;; The handler for PROGN contains code to handle 0, 1, 2, 3+ subforms
196 ;; but it's worthwhile to special-case 0 and 1 here too
197 ;; because it avoids some consing and preprocessing.
198 (%sexpr
(if (and (listp forms
) (not (cdr forms
)))
199 (first forms
) ; ok if (PROGN) or (PROGN form)
202 ;;; Return the binding of a lexical function indexed by INDEX in ENV.
203 ;;; INDEX must be the frame-ptr returned from a call to FIND-LEXICAL-FUN.
204 ;;; This is for runtime use by a SEXPR handler.
205 ;;; The name is borrowed from the standard function "FDEFINITION" -
206 ;;; this does not have anything to do with #<fdefn> objects.
207 (defun local-fdefinition (index env
)
208 (declare (fixnum index
) #.
+handler-optimize
+)
209 (%cell-ref env index
))
211 ;;; Return a frame-pointer for FNAME if it is a local function,
212 ;;; or return :MACRO if it names either a local *or* global macro.
213 ;;; Return NIL if none of the above.
214 (defun local-fn-frame-ptr (fname env
)
215 (multiple-value-bind (kind definition frame-ptr
) (find-lexical-fun env fname
)
217 (if (eq kind
:macro
) :macro frame-ptr
)
218 (if (and (symbolp fname
) (macro-function fname
)) :macro nil
))))
220 ;;; Fast handlers for lexical var access. This is a speed and space win,
221 ;;; the latter since it avoids some consing for small fixed frame-ptr.
223 ;;; (REF 0 N) -> (SVREF (ENV-VARS ENV) N)
224 ;;; (REF 1 N) -> (SVREF (ENV-VARS (ENV-PARENT ENV)) N)
225 ;;; (REF 2 N) -> (SVREF (ENV-VARS (ENV-PARENT (ENV-PARENT ENV))) N)
227 ;;; A fast assembler could generate accessors on demand - and then I'd cache
228 ;;; them - as the code is entirely boilerplate. Lacking that, hardwire some.
229 (declaim ((simple-array t
(4 10)) *fast-lexvar-reffers
* *fast-lexvar-setters
*))
230 (macrolet ((ref (up across
)
231 (declare (optimize (speed 0))) ; silence the generic math note
232 `(svref (env-payload ,(let ((e 'env
))
234 (setq e
`(env-parent ,e
)))))
236 (array-of (n-depths n-cells
)
237 (declare (optimize (speed 0))) ; silence the generic math note
239 ',(list n-depths n-cells
)
241 (list ,@(loop for depth below n-depths
243 `(list ,@(loop for j below n-cells
244 collect
`(access ,depth
,j
))))))))
245 (defparameter *fast-lexvar-reffers
*
246 (macrolet ((access (up across
)
247 `(hlambda GET-VAR
() (env) (ref ,up
,across
))))
248 (array-of 4 10))) ; for 4 scopes and 10 names per scope you can go fast
249 (defparameter *fast-lexvar-setters
* ; These are lambdas, not handlers
250 (macrolet ((access (up across
)
251 `(named-lambda (eval SET-VAR
) (form env sexpr
)
252 (declare #.
+handler-optimize
+ (ignore sexpr
))
253 (setf (ref ,up
,across
) (dispatch form env
)))))
256 (declaim (ftype (function (symbol t t
) nil
) typecheck-fail typecheck-fail
/ref
)
257 (ftype (function (t &rest t
) nil
) values-typecheck-fail
)
258 (ftype (function (function fixnum
) nil
)
259 err-too-few-args err-too-many-args
))
261 (macrolet ((with-indices (special-case general-case
)
262 `(let ((depth (frame-ptr-depth frame-ptr
))
263 (slot (frame-ptr-cell-index frame-ptr
)))
264 (if (and (< depth
(array-dimension *fast-lexvar-reffers
* 0))
265 (< slot
(array-dimension *fast-lexvar-reffers
* 1)))
269 ;; Return a handler that accesses a lexical var given its FRAME-PTR.
270 ;; Anything not in *FAST-LEXVAR-REFFERS* gets a general-purpose routine.
271 (defun lexvar-reffer (frame-ptr type
)
273 ;; There are no specialized reffers with type-checking
274 ;; because the check is slow anyway.
275 (hlambda GET-VAR
(frame-ptr type
) (env)
276 (let ((val (%cell-ref env frame-ptr
)))
277 (if (itypep val type
)
279 (typecheck-fail/ref
(frame-symbol env frame-ptr
) val type
))))
280 (with-indices (aref *fast-lexvar-reffers
* depth slot
)
281 (hlambda GET-VAR
(frame-ptr) (env) (%cell-ref env frame-ptr
)))))
283 (defun lexvar-setter (frame-ptr newval type
)
285 ;; There are no no specialized setters with type-checking
286 ;; because the check is slow anyway.
287 (hlambda SET-VAR
(newval frame-ptr type
) (env)
288 (let ((newval (dispatch newval env
)))
289 (if (itypep newval type
)
290 (setf (%cell-ref env frame-ptr
) newval
)
291 (typecheck-fail (frame-symbol env frame-ptr
) newval type
))))
292 (with-indices (handler (aref *fast-lexvar-setters
* depth slot
) newval
)
293 (hlambda SET-VAR
(newval frame-ptr
) (env)
294 (setf (%cell-ref env frame-ptr
) (dispatch newval env
)))))))
296 ;;; Access SYMBOL's dynamic value, enforcing TYPE if non-nil,
297 ;;; and checking for change of its :VARIABLE :KIND if PARANOID,
298 ;;; allowing the possibility that a DEFVAR changed to DEFINE-SYMBOL-MACRO
300 (defun specvar-reffer (symbol type
&optional paranoid
)
301 (macrolet ((safe-symbol-value ()
302 ;; HLAMBDA lowers safety because the interpreter itself
303 ;; is (hopefully) free of bugs, but we always want to check
304 ;; that a special variable is BOUNDP.
305 '(locally (declare (optimize (safety 3)))
306 (symbol-value symbol
))))
307 (cond ((and type paranoid
)
308 (hlambda SYMEVAL
(symbol) (env sexpr
)
309 (if (member (info :variable
:kind symbol
) '(:alien
:macro
))
310 (digest-form symbol env sexpr
)
311 (let ((val (safe-symbol-value)))
312 (if (itypep val type
)
314 (typecheck-fail/ref symbol val type
))))))
315 ((and type
(not paranoid
))
316 (hlambda SYMEVAL
(symbol type
) (env)
318 (let ((val (safe-symbol-value)))
319 (if (itypep val type
)
321 (typecheck-fail/ref symbol val type
)))))
323 (hlambda SYMEVAL
(symbol type
) (env sexpr
)
324 (if (member (info :variable
:kind symbol
) '(:alien
:macro
))
325 (digest-form symbol env sexpr
)
326 (safe-symbol-value))))
328 (hlambda SYMEVAL
(symbol) (env)
330 (safe-symbol-value))))))
332 (defconstant +mode-mask-shift
+ 4)
333 (defconstant +arg-mode-mask
+ #b1110
)
334 (defconstant +optional-arg
+ #b0000
)
335 (defconstant +keyword-arg
+ #b0010
)
336 (defconstant +supplied-p-var
+ #b0100
)
337 (defconstant +aux-var
+ #b0110
)
338 (defconstant +rest-arg
+ #b1000
)
339 ;;; This constant MUST be 1 so it can be used as an index skip count
340 ;;; versus having to write (if (logtest foo +has-default+) (incf thing))
341 (defconstant +has-default
+ #b0001
)
343 (defmacro argument-keyword-index
(encoding)
344 `(ash ,encoding
(- +mode-mask-shift
+)))
346 ;;; Compress LAMBDA-LIST into a representation that encodes which arguments
347 ;;; have defaulting forms and/or supplied-p variables.
348 (defun encode-lambda-bindings (lambda-list form-converter
)
349 (let ((n-optional 0) (n-keys -
1) fancy-vars decoder keys
)
350 (multiple-value-bind (llks required optional rest keyword-args aux
)
351 (parse-lambda-list lambda-list
352 :accept
(lambda-list-keyword-mask
353 '(&optional
&rest
&key
&allow-other-keys
&aux
))
355 ;; Do I need more syntax checking? PARSE-LAMBDA-LIST enforces syntactic
356 ;; validity but not that the vars are bindable; however the specialness
357 ;; check enforces the latter, so we're probably ok.
358 (flet ((add-var (x mode
)
359 (multiple-value-bind (key var
)
360 (if (eq mode
+keyword-arg
+)
361 (cond ((atom x
) (values (keywordicate x
) x
))
362 ((consp (car x
)) (values (caar x
) (cadar x
)))
363 (t (values (keywordicate (car x
))
365 (values nil
(if (symbolp x
) x
(car x
))))
366 (let* ((default (if (listp x
) (funcall form-converter x
)))
367 (encoding (logior mode
(if default
+has-default
+ 0))))
368 (push var fancy-vars
)
369 (when (eq mode
+keyword-arg
+)
370 (setq encoding
(logior (ash (incf n-keys
) +mode-mask-shift
+)
373 (push encoding decoder
)
375 (push default decoder
)))
376 (when (and (/= mode
+aux-var
+) (proper-list-of-length-p x
3))
377 (push (third x
) fancy-vars
)
378 (push +supplied-p-var
+ decoder
)))))
381 (add-var x
+optional-arg
+))
383 (push (car rest
) fancy-vars
)
384 (push +rest-arg
+ decoder
))
385 (dolist (x keyword-args
)
386 (add-var x
+keyword-arg
+))
388 (add-var x
+aux-var
+)))
389 (values required
(nreverse fancy-vars
)
390 ;; compute (length keys) before nreconc smashes them
391 (make-keyword-bits (length keys
) rest
392 (ll-kwds-keyp llks
) (ll-kwds-allowp llks
))
393 (if (or (ll-kwds-keyp llks
) decoder
)
394 (coerce (nreconc keys
(nreverse decoder
)) 'vector
))
397 ;;; If BODY matches (BLOCK <name> . <forms>) then return the block name, the
398 ;;; forms, and an indicator of whether it is OK to create the block and bind
399 ;;; variables at the same time. If not a block, return 0 and forms unaltered.
401 ;;; The optimization of eliding an extra ENV is performed only if no nontrival
402 ;;; defaulting SEXPRs exist. A "trivial" one is either a constant or a reference
403 ;;; to a variable (not a macro). Absent such a thing, nobody could notice that
404 ;;; there aren't actually two different lexical contours, so creating variable
405 ;;; bindings and a block may occur simultaneously.
406 ;;; [In fact, in such case, there is no difference between parallel and serial
407 ;;; binding, so the LET* part of the binding loop could be absorbed by the LET
408 ;;; part, which would have to be informed how to decode a fancy-arg vector]
409 ;;; The test is extremely paranoid by "wrongly" saying that a form can be a
410 ;;; macro despite the lambda list itself creating a binding for the symbol
411 ;;; in question. LAMBDA can cause fewer symbols to be macros, but never more.
412 ;;; Therefore it is fine to disregard the lambda decoder. For example in:
413 ;;; (symbol-macrolet ((foo (hair))) (lambda (x &optional (foo x)) ...)
414 ;;; FOO is thought to be a macro.
416 (defun extract-lambda-block (body decoder env
)
417 (labels ((definitely-var-ref-p (x)
419 (case (nth-value 1 (find-lexical-var env x
))
420 ((:normal
:special
) t
)
422 (t (neq (info :variable
:kind x
) :macro
)))))
423 (trivial-expression-p (x)
424 (or (not (sexpr-p x
)) ; a constant
425 (definitely-var-ref-p (sexpr-form x
)))))
426 (if (and (listp body
)
428 (eq (caar body
) 'block
)
430 (symbolp (cadar body
)))
431 (let ((body (cdar body
))) ; = (BLOCK-NAME . FORMS)
432 (values (car body
) (cdr body
)
433 (every #'trivial-expression-p decoder
)))
434 (values 0 body nil
))))
436 ;;; Produce a LAMBDA-FRAME for a PROTO-FN.
437 ;;; ENV is for package-lock checks and also to make a quick guess about
438 ;;; whether a block can be created at the same time as the variable bindings.
439 (defun digest-lambda (env proto-fn
)
441 (((required-args other-args keyword-bits decoder n-opt
)
442 (encode-lambda-bindings (proto-fn-lambda-list proto-fn
)
443 (lambda (x) (%sexpr
(second x
)))))
444 (decls (proto-fn-decls proto-fn
))
445 (n-required (length required-args
))
446 (n-other (length other-args
))
447 (n-lambda-vars (+ n-required n-other
))
448 (declared-specials (declared-specials decls
))
450 (remove-if (lambda (sym)
451 (or (memq sym required-args
) (memq sym other-args
)))
454 (let ((a (make-array (+ n-lambda-vars
(length free-specials
)))))
455 (replace a required-args
)
456 (replace a other-args
:start1 n-required
)
457 (replace a free-specials
:start1 n-lambda-vars
)
458 (dotimes (i n-lambda-vars a
) ; Unique-ify the binding cells.
459 (setf (svref a i
) (list (svref a i
))))))
461 (mark-bound-specials env declared-specials symbols n-lambda-vars
))
462 (required-mask (lognot (ash -
1 n-required
)))
463 (required-specials ; one list for PROGV in WITH-LET-BINDINGS
464 (nreverse ; WITH-LET-BINDINGS uses PUSH to accumulate values
465 (collect-progv-symbols
466 symbols n-lambda-vars
(logand special-b required-mask
))))
467 (other-specials ; one list per PROGV in WITH-LET*-BINDINGS.
469 (collect-progv-symbols
470 symbols n-lambda-vars
(logandc2 special-b required-mask
))))
471 ((block-name forms block-share-env-p
)
472 (extract-lambda-block (proto-fn-forms proto-fn
) decoder env
))
473 (frame (make-lambda-frame
474 :min-args n-required
:n-optional n-opt
475 :n-bound-vars n-lambda-vars
:keyword-bits keyword-bits
476 :symbols symbols
:values
(or decoder
#())
478 :specials
(if required-specials
479 (cons required-specials other-specials
)
481 :declarations decls
:%policy
(new-policy env decls
)
482 :block-name block-name
:share-block-p block-share-env-p
483 :sexpr
(%progn forms
))))
484 (process-typedecls frame env n-lambda-vars symbols
)
485 (setf (proto-fn-%frame proto-fn
) frame
486 (proto-fn-cookie proto-fn
) *globaldb-cookie
*)
487 (values frame
*globaldb-cookie
*)))
489 (defun make-local-fn-scope (decls funs forms env
)
490 (let ((specials (free-specials env decls
)))
491 (process-typedecls (%make-local-fn-scope
492 decls
(new-policy env decls
)
493 funs
(%progn forms
) specials
)
496 (defun err-too-few-args (fun n-args
)
497 (let ((frame (interpreted-function-frame fun
)))
498 (ip-error "~S received ~D argument~:P but expects~:[ at least~;~] ~D."
499 (name-for-fun fun
) n-args
500 (eql (lambda-frame-min-args frame
) (lambda-frame-max-args frame
))
501 (lambda-frame-min-args frame
))))
503 (defun err-too-many-args (fun n-args
)
504 (let ((frame (interpreted-function-frame fun
)))
505 (ip-error "~S received ~D argument~:P but expects~:[ at most~;~] ~D"
506 (name-for-fun fun
) n-args
507 (eql (lambda-frame-min-args frame
) (lambda-frame-max-args frame
))
508 (lambda-frame-max-args frame
))))
510 (defmacro with-lambda-frame
((frame-var fun if-invalid
) &body if-valid
)
511 ;; If any global variable has changed its :KIND, this function's lambda
512 ;; variables will be re-checked for specialness, constness, etc.;
513 ;; also, internally cached cached macroexpansions get discarded.
514 `(if (eq (interpreted-function-cookie ,fun
) *globaldb-cookie
*)
515 (let ((,frame-var
(interpreted-function-frame ,fun
)))
517 (progn (atomic-incf *invalidation-count
*) ; for testing cache eviction
520 ;;; LIST is a list of supplied keywords arguments to a function.
521 ;;; CONTROL-BITS are from the lambda frame, and ALLOWED is a vector
522 ;;; of the permitted keywords but has extra stuff after them, so scanning
523 ;;; must restrict itself to strictly the N-KEYS specified by CONTROL-BITS.
524 (defun validate-keywords (list control-bits allowed
)
525 (declare (list list
) (fixnum control-bits
) (simple-vector allowed
))
526 (labels ((check-odd-length (tail n-seen
)
527 (let ((n-more (length tail
))) ; just check for oddness
529 (fail-odd-length (+ n-seen n-more
)))))
531 (ip-error "odd number of &KEY arguments: ~D" n
))
533 (let ((n-allowed (keyword-bits-n-keys control-bits
)) bad
)
534 (loop for
(key val
) on list by
#'cddr
; rescan to collect them
535 unless
(or (eq key
:allow-other-keys
)
536 (find key allowed
:end n-allowed
))
537 do
(pushnew key bad
))
538 (let ((plural (cdr bad
)))
539 (ip-error "Keyword~*~:[~;s~]~2:* ~{~S~^,~} ~:[is~;are~] not ~
540 ~:[allowed.~;in the allowed set ~:*~S~]"
541 (nreverse bad
) plural
542 (replace (make-list n-allowed
) allowed
))))))
543 (if (keyword-bits-allowp control-bits
)
544 (check-odd-length list
0)
545 (let ((n-allowed (keyword-bits-n-keys control-bits
))
546 (allow-other +none
+) ; the observed value of :ALLOW-OTHER-KEYS
549 (seen-unknown-key-p nil
))
552 (if seen-unknown-key-p
555 (let ((key (pop tail
))
556 (value (if tail
(pop tail
) (fail-odd-length (1+ n-visited
)))))
558 ;; The code below is designed to pass or fail as soon as possible
559 (cond ((neq key
:allow-other-keys
)
560 (unless (find key allowed
:end n-allowed
)
561 (when (eq allow-other nil
)
563 (setq seen-unknown-key-p t
)))
564 ((eq allow-other
+none
+) ; = unseen so far
566 (return (check-odd-length tail n-visited
))
568 (when seen-unknown-key-p
570 (setq allow-other nil
)))))))))))
572 (declaim (ftype (sfunction (function) function
) interpreted-applicator
))
573 ;;; Get a parsed LAMBDA-FRAME from the proto-fn, reinstall a more specific
574 ;;; lambda applicator, and jump to it.
575 (defun interpreter-trampoline (fun &rest args
)
576 (setf (values (interpreted-function-frame fun
)
577 (interpreted-function-cookie fun
))
578 (proto-fn-frame (interpreted-function-proto-fn fun
)
579 (interpreted-function-env fun
)))
580 (apply (setf (funcallable-instance-fun fun
) (interpreted-applicator fun
))
583 ;;; The most general case of interpreted function application.
584 ;;; All combinations of fancy args are allowed and any var can be special.
585 ;;; This is a ton of code to inline, but it uses the same concept as for
586 ;;; inlined %%EVAL - there is fast version and a slow version with extra
587 ;;; extra wrapping for when hooks are in use.
589 (define-symbol-macro *absent
* (%make-lisp-obj sb-vm
:unbound-marker-widetag
))
590 (declaim (maybe-inline apply-lambda
))
591 (defun apply-lambda (frame fun n-args args
)
592 (declare (list args
))
593 (let* ((values (make-array (lambda-frame-n-bound-vars frame
)))
594 (symbol-cells (cons (lambda-frame-min-args frame
)
595 (frame-symbols frame
)))
597 (if (lambda-frame-share-block-p frame
)
599 (interpreted-function-env fun
) values symbol-cells frame
600 (list (lambda-frame-block-name frame
)))
602 (interpreted-function-env fun
) values symbol-cells frame
)))
603 (special-b (frame-special-b frame
))
604 (specials (frame-specials frame
))
606 (1- (keyword-bits-n-keys (lambda-frame-keyword-bits frame
))))
609 (declare (index-or-minus-1 decoder-index
) (list tail
))
610 (labels ((instruction ()
611 (svref (frame-values frame
) (incf decoder-index
)))
612 (fancy-arg (&aux
(mode (the fixnum
(instruction))))
613 (case (logand mode
+arg-mode-mask
+)
615 (cond ((setq arg-supplied-p tail
)
616 (incf decoder-index
(logand mode
+has-default
+))
618 ((logtest mode
+has-default
+)
619 (dispatch (instruction) new-env
))))
621 (let* ((key (svref (frame-values frame
)
622 (argument-keyword-index mode
)))
623 (val (getf tail key
*absent
*)))
624 (cond ((setq arg-supplied-p
(neq val
*absent
*))
625 (incf decoder-index
(logand mode
+has-default
+))
627 ((logtest mode
+has-default
+)
628 (dispatch (instruction) new-env
)))))
629 (#.
+supplied-p-var
+ (if arg-supplied-p t nil
))
631 (if (logtest mode
+has-default
+)
632 (dispatch (instruction) new-env
)))
633 (#.
+rest-arg
+ tail
))))
634 (declare (inline instruction
))
636 (values (car symbol-cells
)
638 (fancy-arg) (binding-typechecks frame
)
639 frame-index
(frame-symbols frame
))
640 :specials
(pop specials
))
641 ;; form to start with
643 (values (lambda-frame-min-args frame
)
645 (if tail
(pop tail
) (err-too-few-args fun n-args
))
646 (binding-typechecks frame
)
647 frame-index
(frame-symbols frame
))
648 :specials
(pop specials
))
649 ;; maxargs/keywords should be checked now because it would be
650 ;; strange to eval some defaulting forms and then croak.
651 (cond ((logtest (lambda-frame-keyword-bits frame
) +keyp-bit
+)
653 (nthcdr (lambda-frame-n-optional frame
) tail
)
654 (lambda-frame-keyword-bits frame
)
655 (frame-values frame
)))
656 ((and (not (logtest (lambda-frame-keyword-bits frame
)
658 ;; FIXME: this call to LENGTH can be avoided by counting
659 ;; bindings made and subtracting from N-ARGS.
660 (> (length (truly-the list tail
))
661 (lambda-frame-n-optional frame
)))
662 (err-too-many-args fun n-args
)))
663 (let*-bind frame-index
(lambda-frame-n-bound-vars frame
)))
664 ;; done with sequential bindings
665 (setf (env-symbols new-env
) (frame-symbols frame
))
666 (enforce-types frame
(interpreted-function-env fun
))
667 ;; Three cases: no block, block and vars share the ENV,
668 ;; or allocate another ENV
669 (cond ((eql (lambda-frame-block-name frame
) 0)
670 (dispatch (lambda-frame-sexpr frame
) new-env
))
671 ((lambda-frame-share-block-p frame
)
672 (catch (lambda-env-block new-env
)
673 (dispatch (lambda-frame-sexpr frame
) new-env
)))
675 (let ((exit (list (lambda-frame-block-name frame
))))
677 (dispatch (lambda-frame-sexpr frame
)
678 (make-block-env new-env exit nil
679 *vacuous-decls
*))))))))))
681 (defun applicator/general
(fun)
683 (declare #.
+handler-optimize
+)
684 (declare (inline apply-lambda
))
685 (with-lambda-frame (frame fun
(apply #'interpreter-trampoline fun args
))
686 (apply-lambda frame fun
(length args
) args
))))
688 ;;; Positional applicator is for lambda-lists that do not use any
689 ;;; lambda-list keyword, perform no type checks and make no special bindings.
690 ;;; Choose the most efficient applicator based on N-args and
691 ;;; whether or not there is a block name. Since there are no defaulting forms,
692 ;;; the block name (if any) and variables can have the same extent.
693 ;;; User-supplied code could not detect the difference.
694 (defun applicator/positional
(fun)
696 ((new-env (constructor storage-cells
&rest more-args
)
697 `(,constructor
(interpreted-function-env fun
) ,storage-cells
698 (frame-symbols frame
) frame
,@more-args
))
699 (invoke (n &aux
(args (subseq '(arg1 arg2 arg3 arg4 arg5
) 0 n
)))
700 `(if (eql (lambda-frame-block-name (interpreted-function-frame fun
))
702 (named-lambda (.apply.
,n
) ,args
703 (declare #.
+handler-optimize
+ (optimize sb-c
:verify-arg-count
))
705 (frame fun
(interpreter-trampoline fun
,@args
))
706 (dispatch (lambda-frame-sexpr frame
)
707 (new-env make-var-env
708 ,(if (zerop n
) nil
`(vector ,@args
))))))
709 (named-lambda (.apply.
,n
) ,args
710 (declare #.
+handler-optimize
+ (optimize sb-c
:verify-arg-count
))
712 (frame fun
(interpreter-trampoline fun
,@args
))
713 (let ((exit (list (lambda-frame-block-name frame
))))
715 (dispatch (lambda-frame-sexpr frame
)
716 (new-env make-lambda-env
717 ,(if (zerop n
) nil
`(vector ,@args
))
719 (case (lambda-frame-min-args (interpreted-function-frame fun
))
730 (let* ((n-actual (length args
))
731 (excess (- n-actual
(lambda-frame-min-args frame
))))
732 (cond ((minusp excess
) (err-too-few-args fun n-actual
))
733 ((plusp excess
) (err-too-many-args fun n-actual
))))
734 (let ((cells (make-array (lambda-frame-n-bound-vars frame
))))
735 (dotimes (i (lambda-frame-min-args frame
) ,form
)
736 (setf (svref cells i
) (nth i args
)))))))
737 (if (eql (lambda-frame-block-name (interpreted-function-frame fun
)) 0)
738 (named-lambda (.apply.
) (&rest args
)
739 (declare #.
+handler-optimize
+)
741 (frame fun
(apply #'interpreter-trampoline fun args
))
742 (with-args (dispatch (lambda-frame-sexpr frame
)
743 (new-env make-var-env cells
)))))
744 (named-lambda (.apply.
) (&rest args
)
745 (declare #.
+handler-optimize
+)
747 (frame fun
(apply #'interpreter-trampoline fun args
))
749 (let ((exit (list (lambda-frame-block-name frame
))))
751 (dispatch (lambda-frame-sexpr frame
)
752 (new-env make-lambda-env cells
755 ;;; /&Optional applicator disallows &REST and &KEY because the stack arguments
756 ;;; are never listified (it's an implicit &MORE arg). I don't want to write code
757 ;;; to manually listify, nor hand-roll GETF. Also no special bindings permitted.
758 (defun applicator/&optional
(fun)
759 (named-lambda (.apply.
&optional
) (&rest args
)
760 (declare #.
+handler-optimize
+)
761 (with-lambda-frame (frame fun
(apply #'interpreter-trampoline fun args
))
762 (let* ((n-actual (let* ((min (lambda-frame-min-args frame
))
763 (max (+ min
(lambda-frame-n-optional frame
)))
766 (err-too-few-args fun n
))
768 (err-too-many-args fun n
))
770 (values (make-array (lambda-frame-n-bound-vars frame
)))
771 (symbol-cells (cons (lambda-frame-min-args frame
)
772 (frame-symbols frame
)))
774 (if (lambda-frame-share-block-p frame
)
776 (interpreted-function-env fun
) values symbol-cells frame
777 (list (lambda-frame-block-name frame
)))
779 (interpreted-function-env fun
) values symbol-cells frame
)))
783 (declare (index-or-minus-1 decoder-index arg-index
))
784 (with-let-bindings (values (lambda-frame-min-args frame
)
785 :value
(nth (incf arg-index
) args
) :specialp nil
)
786 nil
) ; empty body. no special bindings, so no PROGV
787 (flet ((instruction ()
788 (svref (frame-values frame
) (incf decoder-index
))))
789 (declare (inline instruction
))
791 (values (car symbol-cells
)
793 (let ((mode (the fixnum
(instruction))))
794 (case (logand mode
+arg-mode-mask
+)
796 (cond ((setq arg-supplied-p
(< (incf arg-index
) n-actual
))
797 (incf decoder-index
(logand mode
+has-default
+))
798 (nth arg-index args
))
799 ((logtest mode
+has-default
+)
800 (dispatch (instruction) new-env
))))
801 (#.
+supplied-p-var
+ (if arg-supplied-p t nil
))
802 (#.
+aux-var
+ (if (logtest mode
+has-default
+)
803 (dispatch (instruction) new-env
)))))
805 ;; form to start with
806 (let*-bind
(lambda-frame-min-args frame
)
807 (lambda-frame-n-bound-vars frame
))
808 ;; done with sequential bindings
809 (setf (env-symbols new-env
) (frame-symbols frame
))
810 ;; Three cases: no block, block and vars share the ENV,
811 ;; or allocate another ENV
812 (cond ((eql (lambda-frame-block-name frame
) 0)
813 (dispatch (lambda-frame-sexpr frame
) new-env
))
814 ((lambda-frame-share-block-p frame
)
815 (catch (lambda-env-block new-env
)
816 (dispatch (lambda-frame-sexpr frame
) new-env
)))
818 (let ((exit (list (lambda-frame-block-name frame
))))
820 (dispatch (lambda-frame-sexpr frame
)
821 (make-block-env new-env exit
822 nil
*vacuous-decls
*))))))))))))
824 (declaim (type (or null compiled-function
) *self-applyhook
*))
825 (defvar *self-applyhook
* nil
) ; not quite *applyhook* as outlined in CLtL
827 ;;; A trampoline which never installs a more-specific trampoline,
828 ;;; and checks for a binding of *SELF-APPLYHOOK* on each call.
829 (defun interpreter-hooked-trampoline (fun &rest args
)
830 (multiple-value-bind (frame cookie
)
831 (proto-fn-frame (interpreted-function-proto-fn fun
)
832 (interpreted-function-env fun
))
833 (setf (values (interpreted-function-frame fun
)
834 (interpreted-function-cookie fun
)) (values frame cookie
))
835 ;; *SELF-APPLYHOOK* isn't the *APPLYHOOK* as described by CLtL.
836 ;; When bound, each hooked function will funcall the hook with
837 ;; itself and the arguments and a continuation of two arguments
838 ;; that the hook should call to actually perform the application.
839 ;; APPLY-LAMBDA is not inlined here. If you're using the hook,
840 ;; things are running at less than top speed anyway.
841 (if *self-applyhook
* ; must be a compiled-function
842 (funcall *self-applyhook
* fun args
844 (apply-lambda (interpreted-function-frame self
)
845 self
(length args
) args
)))
846 ;; Assuming the compiler is doing the right thing,
847 ;; this LENGTH is gotten from the passing location.
848 (apply-lambda frame fun
(length args
) args
))))
850 ;;; Compute the function that should be used when applying FUN.
851 ;;; This uses highly specialized code for small fixed N args <= 5,
852 ;;; and slightly more general code for any fixed number of args > 5.
853 ;;; Bound special arguments require the most general entry.
855 (defun interpreted-applicator (fun)
856 (let ((frame (interpreted-function-frame fun
)))
857 (if (and (zerop (frame-special-b frame
)) ; no bound specials
858 (not (logtest (lambda-frame-keyword-bits frame
) ; fixed upper bound
859 (logior +restp-bit
+ +keyp-bit
+))) ; on arg count
860 (eql (binding-typechecks frame
) +none
+)
861 (eql (extra-typechecks frame
) +none
+))
862 (if (zerop (length (frame-values frame
)))
863 (applicator/positional fun
)
864 (applicator/&optional fun
))
865 (applicator/general fun
))))
870 (defun get-thinginator () #'thing
)
871 (defun thing () 'the-thing
)
872 (funcall (get-thinginator)) ; returns THE-THING
873 (defmacro thing
() ''thing-macro
) ; system warns about this
874 (get-thinginator) ; errs - it correctly perceives the redefinition
877 ;;; Figure out if F is supposed to be funcallable, versus is the error trampoline
878 ;;; that is installed in a global macro's fdefn object. It is very efficient
879 ;;; to do this by seeing if F is a closure over the error trampoline, versus asking
880 ;;; the horribly slow globaldb if F's name's macro-function is NIL.
881 ;;; In the common case where F isn't a closure, it definitely isn't a macro.
882 (declaim (inline %looks-like-macro-p
))
883 (defun %looks-like-macro-p
(f) ; f is a FUNCTION
884 (and (= (fun-subtype (truly-the function f
)) sb-vm
:closure-header-widetag
)
885 ;; compare to a known global macro
886 (eq (load-time-value (%closure-fun
(symbol-function 'or
)) t
)
889 ;;; Return T if SYMBOL might get redefined as a macro when it was previously
890 ;;; a function and vice versa. Extra checks are done on every use of the symbol
891 ;;; as a function name.
892 (defun fluid-def-p (symbol)
893 ;; Todo: add most system-internal packages here probably
894 (not (memq (symbol-package symbol
)
896 (mapcar #'find-package
'("CL" "SB-KERNEL" "SB-INT" "SB-IMPL"
897 "SB-C" "SB-VM" "SB-ALIEN" "SB-ALIEN-INTERNALS"
899 "SB-SYS" #+sb-thread
"SB-THREAD"))))))
902 * (defmacro baz
(n) `(nth ,n
*mumble
*))
903 * (define-symbol-macro mumble
(cdr (feep)))
904 * (defmacro feep
() '(aref *x
* 1))
905 * (nth-value 1 (tracing-macroexpand-1 '(shiftf (baz 3) (feep)) nil
))
906 => ((#<INTERPRETED-FUNCTION
(DEFMACRO BAZ
)> . BAZ
) (MUMBLE CDR
(FEEP))
907 (#<INTERPRETED-FUNCTION
(DEFMACRO FEEP
)> . FEEP
))
910 (defvar *show-macroexpansion
* nil
)
911 ;;; Expand FORM once, capturing all "interesting" expansions occuring within.
912 ;;; For example (INCF FOO) is a builtin macro whose functional definition is
913 ;;; not considered per se interesting, however if FOO is a symbol macro,
914 ;;; then the meaning of that form changes if FOO gets redefined.
916 ;;; This is still not powerful enough to work all the time - we really need to
917 ;;; know if anything so much as inquired whether a symbol is a macro.
918 ;;; Continuing the above example, if FOO is not a macro, then making it into
919 ;;; a macro later will have no effect, because there is no object that
920 ;;; stands for the identity of FOO as a macro. This is not a problem with
922 ;;; Also note that CLtL2 says
923 ;;; "macro definitions should not depend on the time at which they are expanded"
925 (defun tracing-macroexpand-1 (form env
&optional
(predicate #'fluid-def-p
)
926 &aux
(original-hook (valid-macroexpand-hook))
928 (unless (allow-macro-redefinition env
)
929 (return-from tracing-macroexpand-1
930 (values (macroexpand-1 form env
) nil
)))
931 (flet ((macroexpand-hook (function form env
)
932 (let ((expansion (funcall original-hook function form env
)))
934 ;; All global symbol-macros are recorded - there are no builtins
935 ;; which are candidates for removal. symbol-macrolet expansions
936 ;; aren't recorded, unless they happen to be EQ to a global
937 ;; expansion, which is unlikely and nothing to worry about.
938 (if (neq expansion
(info :variable
:macro-expansion form
))
939 nil
; I have no idea what to record
940 (push (cons form expansion
) expanders
))
941 ;; If the expander is EQ to the global one and the symbol
942 ;; satisfies the interestingness test.
943 (let* ((head (car form
)) (global-fn (macro-function head nil
)))
944 (if (and (eq function global-fn
) (funcall predicate head
))
945 (push (cons global-fn head
) expanders
))))
947 (let ((expansion (let ((*macroexpand-hook
* #'macroexpand-hook
))
948 (macroexpand-1 form env
))))
949 (setq expanders
(nreverse expanders
))
950 ;; condition on #+eval-show also?
951 (when *show-macroexpansion
*
952 (format t
"~&Expanded ~S~% into ~S~%~@[ using ~S~]~%"
953 form expansion expanders
))
954 (values expansion expanders
))))
956 ;;; Return T if the evaluator should always consider that macros
957 ;;; might be redefined. If NIL then cached expansions are permanent.
958 (defun allow-macro-redefinition (env)
959 (if (policy env
(and (= speed
3) (= debug
0) (= safety
0)))
963 (defun arglist-to-sexprs (args)
964 (let ((argc (or (list-length args
)
965 (ip-error "Malformed function call"))))
966 (values (mapcar #'%sexpr args
) argc
)))
968 ;;; Return a handler which decides whether its supplied SEXPR needs
969 ;;; to have macroexpansion performed again due to changes to global macros.
970 ;;; If not, just dispatch the previously computed expansion.
972 ;;; Just knowing the macroexpander isn't enough. We need to collect _all_
973 ;;; expanders that ran during 1 round of expansion, which can't be determined
974 ;;; without running the outermost and tracing what happens.
976 ;;; EXPANSION is a SEXPR for the overall result of expansion.
977 ;;; FNAME is the symbol at the head of the original form.
978 ;;; Each element of KEYS is (#<FUNCTION> . SYM) or (SYM . SYMBOL-EXPANSION)
979 ;;; The representation is unambiguous because a symbol is not a function,
980 ;;; whereas (SYM . EXPANSION|FUNCTION) is ambigious because through contortions
981 ;;; it is possible to have a symbol's expansion be a function object.
982 ;;; If any key is changed, restart using the original sexpr form.
984 (defun digest-macro-form (expansion fname keys
)
985 (if (and (endp (cdr keys
)) ; if one key
986 (let ((k (car keys
))) ; which is the function that we expect
987 (and (functionp (car k
)) (eq (cdr k
) fname
))))
988 ;; Assume that if MACRO-FUNCTION for the form's head is EQ to what it was
989 ;; previously, that it will produce the same expansion (in this ENV).
990 ;; This can of course is easily violated by nondeterministic macros.
991 (let ((macro-fn (caar keys
)))
992 (hlambda MACRO
/1 (fname macro-fn expansion
) (env old-sexpr
)
993 (if (eq (macro-function fname
) macro-fn
)
994 (dispatch expansion env
)
997 (format t
"~&Changed expander: ~S~%" fname
)
998 (digest-form (sexpr-form old-sexpr
) env old-sexpr
)))))
999 ;; Same as above but generalized to N keys.
1000 (hlambda MACRO
+ (keys expansion
) (env old-sexpr
)
1001 (if (every (lambda (k)
1002 (if (functionp (car k
))
1003 (eq (car k
) (macro-function (cdr k
)))
1004 (eq (info :variable
:macro-expansion
(car k
))
1007 (dispatch expansion env
)
1010 (format t
"~&Changed expanders: ~S~%" keys
)
1011 (digest-form (sexpr-form old-sexpr
) env old-sexpr
))))))
1014 (CASES n
(1 5 frob
) (4 (exceptional-case)) (t (fallback-case)))
1019 (4 (EXCEPTIONAL-CASE))
1021 (T (FALLBACK-CASE)))
1024 (defmacro cases
(test-var (min max template
) &rest specified-cases
)
1026 ,@(loop for i from min to max
1028 (or (assoc i specified-cases
)
1029 `(,i
(,template
,i
))))
1031 ,@(cdr (assoc t specified-cases
)))))
1033 ;;; A local-call uses LOCAL-FDEFINITION to obtain the function, which
1034 ;;; is *always* a function, never a macro and never undefined.
1035 ;;; Some clever macrology might share the handler-generator
1036 ;;; with DIGEST-GLOBAL-CALL
1037 (defun digest-local-call (frame-ptr args
&aux
(n-args 0))
1038 (multiple-value-setq (args n-args
) (arglist-to-sexprs args
))
1039 (macrolet ((funcall-n (n)
1040 `(hlambda (LOCAL-CALL ,N
) (data) (env)
1041 (funcall (local-fdefinition (svref data
0) env
)
1042 ,@(loop for i from
1 repeat n
1043 collect
`(dispatch (svref data
,i
) env
))))))
1044 (let ((data (if (> n-args
1) (coerce (cons frame-ptr args
) 'vector
))))
1045 (cases n-args
(0 5 funcall-n
)
1046 (0 (hlambda (LOCAL-CALL 0) (frame-ptr) (env)
1047 (funcall (local-fdefinition frame-ptr env
))))
1048 (1 (let ((arg (first args
)))
1049 (hlambda (LOCAL-CALL 1) (frame-ptr arg
) (env)
1050 (funcall (local-fdefinition frame-ptr env
) (dispatch arg env
)))))
1051 (t (hlambda LOCAL-CALL
(data) (env)
1052 (declare (simple-vector data
))
1053 (let* ((arglist (make-list (1- (length data
))))
1055 (dotimes (i (1- (length data
))
1056 (apply (local-fdefinition (svref data
0) env
)
1058 (rplaca tail
(dispatch (svref data
(1+ i
)) env
))
1061 ;;; Apply what is probably a function - it was when the form was digested.
1062 ;;; This carefully mimics the compiler's behavior of referencing the
1063 ;;; function only after evaluation of its args. In particular, supposing that
1064 ;;; BAZ is not defined, this works in compiled code:
1065 ;;; (DEFUN FOO () (BAZ (SETF (SYMBOL-FUNCTION 'BAZ) (LAMBDA (X) `(HI ,X)))))
1067 ;;; Interpreted code needs an explicit check for NIL in an fdefn-fun.
1068 ;;; Compiled code doesn't because the 'raw-addr' slot is always
1069 ;;; something valid to jump to.
1070 (defun apply-probably-fun (fdefinition args env
&aux
(n-args 0))
1071 (multiple-value-setq (args n-args
) (arglist-to-sexprs args
))
1074 (let* ((arg-names (subseq '(a b c d e
) 0 n
))
1076 (loop for arg in arg-names for i from
1 repeat n
1079 ,(if (= n
1) '(cdr data
) `(svref data
,i
))
1081 `(hlambda (GLOBAL-CALL ,n
) (data) (env sexpr
)
1082 (symbol-macrolet ((fdefn ,(case n
1085 (t '(svref data
0)))))
1087 (digest-form (sexpr-form sexpr
) env sexpr
)
1089 (funcall (sb-c:safe-fdefn-fun fdefn
) ,@arg-names
)))))))
1092 (0 (let ((data fdefinition
)) (funcall-n 0)))
1093 (1 (let ((data (cons fdefinition
(first args
)))) (funcall-n 1)))
1095 (let ((data (coerce (cons fdefinition args
) 'vector
)))
1096 (cases n-args
(2 5 funcall-n
)
1097 (t (hlambda GLOBAL-CALL
(data) (env sexpr
)
1098 (declare (simple-vector data
))
1099 (symbol-macrolet ((fdefn (svref data
0)))
1101 (digest-form (sexpr-form sexpr
) env sexpr
)
1102 (let* ((arglist (make-list (1- (length data
))))
1104 (dotimes (i (1- (length data
))
1105 (apply (sb-c:safe-fdefn-fun fdefn
) arglist
))
1106 (rplaca tail
(dispatch (svref data
(1+ i
)) env
))
1107 (pop tail
)))))))))))))
1108 (if (allow-macro-redefinition env
)
1109 (macrolet ((re-expand-p ()
1110 '(let ((f (fdefn-fun fdefn
)))
1111 (and f
(%looks-like-macro-p f
)))))
1113 (macrolet ((re-expand-p () nil
)) (generate-switch)))))
1115 ;;; Evaluate the arguments to a function that can't be called,
1116 ;;; then call it. Very weird, yes! But this is reached in two situations:
1117 ;;; 1. the user wrote (funcall 'IF ...)
1118 ;;; 2. the user defined a new special operator that the interpreter
1119 ;;; does not know about.
1120 ;;; In either case %LOOKS-LIKE-MACRO-P will return T,
1121 ;;; because it knows that the fdefn-fun is an error-invoking trampoline.
1122 ;;; But there is no macroexpander, so we have to do "something".
1123 ;;; This handler is the way we punt, because the careful handler (above)
1124 ;;; would see that the thing to be applied is a guard trampoline,
1125 ;;; and would start over again at digest-global-call.
1127 ;;; Note also the subtle difference between these two:
1128 ;;; (funcall 'IF 'FOO 'BAR)
1129 ;;; (funcall #'IF ...)
1130 ;;; In the former, the FUNCALL is reached, because every argument
1131 ;;; to the funcall was legally evaluable. But in the latter, it is not
1132 ;;; reached since the first argument to funcall signals an error.
1133 (defun apply-definitely-not-fun (fname args
)
1134 (let ((data (coerce (cons fname
(arglist-to-sexprs args
)) 'vector
)))
1135 (hlambda GLOBAL-CALL
(data) (env)
1136 (declare (simple-vector data
))
1137 (let* ((arglist (make-list (1- (length data
))))
1139 ;; "unable to optimize apply"
1140 (declare (muffle-conditions compiler-note
))
1141 (dotimes (i (1- (length data
))
1142 (apply (the symbol
(svref data
0)) arglist
))
1143 (rplaca tail
(dispatch (svref data
(1+ i
)) env
))
1146 ;;; Handler is specialized only if the function is builtin.
1147 ;;; In such cases, reference the function directly, eliding the deref
1148 ;;; through an FDEFINITION.
1149 ;;; It can always be kicked out of the cache by touching the globaldb cookie.
1150 (defun digest-global-call (fname args env
)
1151 ;; For user-defined functions declared inline, don't bother
1152 ;; checking for being redefined as a macro.
1153 ;; The globaldb cookie will take care of redefinition.
1154 ;; (neq (info :function :inlinep fname) :inline))
1156 (when (symbolp fname
)
1157 (when (eq (info :function
:kind fname
) :special-form
)
1158 (return-from digest-global-call
1159 (apply-definitely-not-fun fname args
)))
1161 ;; Structure-accessor: interpreted accessors are *terrible*.
1162 ;; We could use a handler (need to respect NOTINLINE though),
1163 ;; or just COMPILE the accessor, since who's to say
1164 ;; that DEFSTRUCT doesn't somehow magically produce
1165 ;; compiled accessors via closures or a LAP assembler.
1166 ;; Frankly the latter ought to be possible.
1167 ;; And this doesn't fix the problem with SETF.
1168 (when (fboundp fname
)
1169 (let ((f (symbol-function fname
)))
1170 (when (and (interpreted-function-p f
)
1171 (structure-instance-accessor-p fname
))
1172 ;: Compile the accessor. If it was defined in a non-null environment,
1173 ;; conversion to a lexenv could say "too complex", so we want to
1174 ;; force it. Passing two arguments to COMPILE achieves this.
1175 ;; We can be confident that the expression doesn't need a lexenv,
1176 ;; because if the function were incompatible with the source-transform,
1177 ;; %DEFUN would have cleared the :source-transform, and fname would not
1178 ;; satisfy STRUCTURE-INSTANCE-ACCESSOR-P.
1179 #+nil
(format t
"~&; Interpreter: Compiling ~S~%" fname
)
1180 ;; FIXME: ensure that the compiled function is safe.
1181 (compile fname
(function-lambda-expression f
)))))
1183 (when (fluid-def-p fname
)
1184 ;; Return a handler that calls FNAME very carefully
1185 (return-from digest-global-call
1186 (apply-probably-fun (find-or-create-fdefn fname
) args env
))))
1188 ;; Try to recognize (FUNCALL constant-fun ...)
1189 ;; This syntax is required when using SETF functions, and it should
1190 ;; be no less efficient than (F args).
1191 ;; But, [FIXME?] can FUNCALL be rebound lexically?
1192 (when (and (eq fname
'funcall
)
1193 (not (endp args
)) ; FUNCALL demands at least one arg
1195 '(or (cons (eql function
)
1196 (cons (satisfies legal-fun-name-p
) null
))
1197 (cons (eql quote
) (cons symbol null
)))))
1198 (let* ((function-form (first args
))
1199 (fname (second function-form
)))
1200 ;; (FUNCALL 'SYMBOL args...) => (SYMBOL args...) without the lexenv.
1201 (when (eq (car function-form
) 'quote
)
1202 (return-from digest-global-call
1203 (digest-global-call fname
(cdr args
) env
)))
1205 ;; It's (FUNCALL #'FUNCTION ...)
1206 (let ((frame-ptr (local-fn-frame-ptr fname env
)))
1208 ((nil) ; global function (or special operator, which will barf on you)
1209 (when (symbolp fname
) (coerce fname
'function
)) ; for effect
1210 (return-from digest-global-call
1211 (digest-global-call fname
(cdr args
) env
)))
1212 (:macro
) ; do not process - let the FUNCTION operator complain
1213 (t (return-from digest-global-call
1214 (digest-local-call frame-ptr
(cdr args
))))))))
1217 (fun (fdefinition fname
)))
1218 (multiple-value-setq (args n-args
) (arglist-to-sexprs args
))
1220 ;; Fold if every arg when trivially constant and the function is foldable.
1221 ;; "trivially" means without needing propagation to decide that.
1222 (when (notany #'sexpr-p args
)
1223 (let ((info (info :function
:info fname
)))
1224 (when (and info
(sb-c::ir1-attributep
(sb-c::fun-info-attributes info
)
1226 (let ((values (multiple-value-list (apply fname args
))))
1227 (return-from digest-global-call
1228 (if (or (cdr values
) (null values
))
1229 (handler #'return-constant-values values
)
1230 (return-constant (first values
))))))))
1232 ;; Todo: redefining any function in one of the builtin packages should
1233 ;; increment the globaldb cookie to unmemoized stored #<FUNCTION> objects.
1234 ;; Btw, it's weird that FDEFINITION strips out tracing wrappers
1235 ;; since FDEFINITION is the canonical way to get the function given a
1236 ;; general name, and seems like it's supposed to be just the
1237 ;; straightforward generalization of SYMBOL-FUNCTION.
1238 (macrolet ((funcall-n (n)
1239 `(hlambda (FAST-GLOBAL-CALL ,n
) (data) (env)
1240 (funcall (the function
(svref data
0))
1241 ,@(loop for i from
1 repeat n collect
1242 `(dispatch (svref data
,i
) env
))))))
1243 (let ((data (if (> n-args
1) (coerce (cons fun args
) 'vector
))))
1244 (cases n-args
(0 5 funcall-n
)
1245 (0 (hlambda (FAST-GLOBAL-CALL 0) (fun) (env)
1246 (declare (ignore env
))
1247 (funcall (the function fun
))))
1248 (1 (let ((arg (first args
))
1249 (handler-fn (gethash fname
*unary-functions
*)))
1251 (handler handler-fn arg
)
1252 (hlambda (FAST-GLOBAL-CALL 1) (fun arg
) (env)
1253 (funcall (the function fun
) (dispatch arg env
))))))
1254 (2 (let ((handler-fn (gethash fname
*binary-functions
*)))
1256 (handler handler-fn
(cons (first args
) (second args
)))
1259 (hlambda GLOBAL-CALL
(data) (env)
1260 (declare (simple-vector data
))
1261 (let* ((arglist (make-list (1- (length data
)))) (tail arglist
))
1262 (dotimes (i (1- (length data
))
1263 (apply (the function
(svref data
0)) arglist
))
1264 (rplaca tail
(dispatch (svref data
(1+ i
)) env
))
1267 (defmethod print-object ((obj basic-env
) stream
)
1268 (print-unreadable-object (obj stream
:identity t
:type t
)
1270 (write-string (env-to-string obj
) stream
)))
1272 (defmethod print-object ((obj sexpr
) stream
)
1273 ;; It's very confusing to debug this code if sexprs are visibly indistinct
1274 ;; from their lists, but it looks more pleasing in backtraces.
1275 ;; Maybe need a toggle switch that is not one of the standard ones?
1277 (let ((string (write-to-string (sexpr-form obj
))))
1278 (format stream
"#<sexpr ~A... #x~X>"
1279 (subseq string
0 (min (length string
) 30))
1280 (get-lisp-obj-address obj
)))
1281 (write (sexpr-form obj
) :stream stream
)))