1 ;;;; This file contains the IR1 interpreter. We first convert to the
2 ;;;; compiler's IR1, then interpret that.
4 ;;;; This software is part of the SBCL system. See the README file for
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
13 (in-package "SB!EVAL")
15 ;;;; interpreter stack
17 (defvar *interpreted-function-cache-minimum-size* 25
19 "If the interpreted function cache has more functions than this come GC time,
20 then attempt to prune it according to
21 *INTERPRETED-FUNCTION-CACHE-THRESHOLD*.")
23 (defvar *interpreted-function-cache-threshold* 3
25 "If an interpreted function goes uncalled for more than this many GCs, then
26 it is eligible for flushing from the cache.")
28 (declaim (type (and fixnum unsigned-byte)
29 *interpreted-function-cache-minimum-size*
30 *interpreted-function-cache-threshold*))
32 ;;; The list of INTERPRETED-FUNCTIONS that have translated definitions.
33 (defvar *interpreted-function-cache* nil)
34 (declaim (type list *interpreted-function-cache*))
36 ;;; Setting this causes the stack operations to dump a trace.
38 ;;; FIXME: perhaps should be #!+SB-SHOW
39 (defvar *eval-stack-trace* nil)
41 ;;; Push value on *eval-stack*, growing the stack if necessary. This returns
42 ;;; value. We save *eval-stack-top* in a local and increment the global before
43 ;;; storing value on the stack to prevent a GC timing problem. If we stored
44 ;;; value on the stack using *eval-stack-top* as an index, and we GC'ed before
45 ;;; incrementing *eval-stack-top*, then INTERPRETER-GC-HOOK would clear the
47 (defun eval-stack-push (value)
48 (let ((len (length (the simple-vector *eval-stack*))))
49 (when (= len *eval-stack-top*)
50 (when *eval-stack-trace* (format t "[PUSH: growing stack.]~%"))
51 (let ((new-stack (make-array (ash len 1))))
52 (replace new-stack *eval-stack* :end1 len :end2 len)
53 (setf *eval-stack* new-stack))))
54 (let ((top *eval-stack-top*))
55 (when *eval-stack-trace* (format t "pushing ~D.~%" top))
56 (incf *eval-stack-top*)
57 (setf (svref *eval-stack* top) value)))
59 ;;; This returns the last value pushed on *eval-stack* and decrements the top
60 ;;; pointer. We forego setting elements off the end of the stack to nil for GC
61 ;;; purposes because there is a *before-gc-hook* to take care of this for us.
62 ;;; However, because of the GC hook, we must be careful to grab the value
63 ;;; before decrementing *eval-stack-top* since we could GC between the
64 ;;; decrement and the reference, and the hook would clear the stack slot.
65 (defun eval-stack-pop ()
66 (when (zerop *eval-stack-top*)
67 (error "attempt to pop empty eval stack"))
68 (let* ((new-top (1- *eval-stack-top*))
69 (value (svref *eval-stack* new-top)))
70 (when *eval-stack-trace* (format t "popping ~D --> ~S.~%" new-top value))
71 (setf *eval-stack-top* new-top)
74 ;;; This allocates n locations on the stack, bumping the top pointer and
75 ;;; growing the stack if necessary. We set new slots to nil in case we GC
76 ;;; before having set them; we don't want to hold on to potential garbage
77 ;;; from old stack fluctuations.
78 (defun eval-stack-extend (n)
79 (let ((len (length (the simple-vector *eval-stack*))))
80 (when (> (+ n *eval-stack-top*) len)
81 (when *eval-stack-trace* (format t "[EXTEND: growing stack.]~%"))
82 (let ((new-stack (make-array (+ n (ash len 1)))))
83 (replace new-stack *eval-stack* :end1 len :end2 len)
84 (setf *eval-stack* new-stack))))
85 (let ((new-top (+ *eval-stack-top* n)))
86 (when *eval-stack-trace* (format t "extending to ~D.~%" new-top))
87 (do ((i *eval-stack-top* (1+ i)))
89 (setf (svref *eval-stack* i) nil))
90 (setf *eval-stack-top* new-top)))
92 ;;; The anthesis of EVAL-STACK-EXTEND.
93 (defun eval-stack-shrink (n)
94 (when *eval-stack-trace*
95 (format t "shrinking to ~D.~%" (- *eval-stack-top* n)))
96 (decf *eval-stack-top* n))
98 ;;; This is used to shrink the stack back to a previous frame pointer.
99 (defun eval-stack-set-top (ptr)
100 (when *eval-stack-trace* (format t "setting top to ~D.~%" ptr))
101 (setf *eval-stack-top* ptr))
103 ;;; This returns a local variable from the current stack frame. This is used
104 ;;; for references the compiler represents as a lambda-var leaf. This is a
105 ;;; macro for SETF purposes.
107 ;;; FIXME: used only in this file, needn't be in runtime
108 (defmacro eval-stack-local (fp offset)
109 `(svref *eval-stack* (+ ,fp ,offset)))
111 ;;;; interpreted functions
113 ;;; The list of INTERPRETED-FUNCTIONS that have translated definitions.
114 (defvar *interpreted-function-cache* nil)
115 (declaim (type list *interpreted-function-cache*))
117 ;;; Return a function that will lazily convert Lambda when called, and will
118 ;;; cache translations.
119 (defun make-interpreted-function (lambda)
120 (let ((res (%make-interpreted-function :lambda lambda
121 :arglist (second lambda))))
122 (setf (funcallable-instance-function res)
123 #'(instance-lambda (&rest args)
124 (let ((fun (interpreted-function-definition res))
125 (args (cons (length args) args)))
126 (setf (interpreted-function-gcs res) 0)
127 (internal-apply (or fun (convert-interpreted-fun res))
131 ;;; Eval a FUNCTION form, grab the definition and stick it in.
132 (defun convert-interpreted-fun (fun)
133 (declare (type interpreted-function fun))
134 (let* ((new (interpreted-function-definition
135 (internal-eval `#',(interpreted-function-lambda fun)))))
136 (setf (interpreted-function-definition fun) new)
137 (setf (interpreted-function-converted-once fun) t)
138 (let ((name (interpreted-function-%name fun)))
139 (setf (sb!c::leaf-name new) name)
140 (setf (sb!c::leaf-name (sb!c::main-entry
141 (sb!c::functional-entry-function new)))
143 (push fun *interpreted-function-cache*)
146 ;;; Get the CLAMBDA for the XEP, then look at the inline expansion info in
147 ;;; the real function.
148 (defun interpreted-function-lambda-expression (x)
149 (let ((lambda (interpreted-function-lambda x)))
151 (values lambda nil (interpreted-function-%name x))
152 (let ((fun (sb!c::functional-entry-function
153 (interpreted-function-definition x))))
154 (values (sb!c::functional-inline-expansion fun)
155 (if (let ((env (sb!c::functional-lexenv fun)))
156 (or (sb!c::lexenv-functions env)
157 (sb!c::lexenv-variables env)
158 (sb!c::lexenv-blocks env)
159 (sb!c::lexenv-tags env)))
161 (or (interpreted-function-%name x)
162 (sb!c::component-name
163 (sb!c::block-component
165 (sb!c::lambda-bind (sb!c::main-entry fun)))))))))))
167 ;;; Return a FUNCTION-TYPE describing an eval function. We just grab the
168 ;;; LEAF-TYPE of the definition, converting the definition if not currently
170 (defvar *already-looking-for-type-of* nil)
171 (defun interpreted-function-type (fun)
172 (if (member fun *already-looking-for-type-of*)
173 (specifier-type 'function)
174 (let* ((*already-looking-for-type-of*
175 (cons fun *already-looking-for-type-of*))
176 (def (or (interpreted-function-definition fun)
177 (sb!sys:without-gcing
178 (convert-interpreted-fun fun)
179 (interpreted-function-definition fun)))))
180 (sb!c::leaf-type (sb!c::functional-entry-function def)))))
182 (defun interpreted-function-name (x)
183 (multiple-value-bind (ig1 ig2 res) (interpreted-function-lambda-expression x)
184 (declare (ignore ig1 ig2))
186 (defun (setf interpreted-function-name) (val x)
187 (let ((def (interpreted-function-definition x)))
189 (setf (sb!c::leaf-name def) val)
190 (setf (sb!c::leaf-name (sb!c::main-entry (sb!c::functional-entry-function
193 (setf (interpreted-function-%name x) val)))
195 (defun interpreter-gc-hook ()
196 ;; Clear the unused portion of the eval stack.
197 (let ((len (length (the simple-vector *eval-stack*))))
198 (do ((i *eval-stack-top* (1+ i)))
200 (setf (svref *eval-stack* i) nil)))
202 ;; KLUDGE: I'd like to get rid of this, since it adds complexity and causes
203 ;; confusion. (It's not just academic that it causes confusion. When working
204 ;; on the original cross-compiler, I ran across what looked
205 ;; as though it might be a subtle writing-to-the-host-SBCL-compiler-data bug
206 ;; in my cross-compiler code, which turned out to be just a case of compiler
207 ;; warnings coming from recompilation of a flushed-from-the-cache interpreted
208 ;; function. Since it took me a long while to realize how many things the
209 ;; problem depended on (since it was tied up with magic numbers of GC cycles,
210 ;; egads!) I blew over a day trying to isolate the problem in a small test
213 ;; The cache-flushing seems to be motivated by efficiency concerns, which
214 ;; seem misplaced when the user chooses to use the interpreter. However, it
215 ;; also interacts with SAVE, and I veered off from deleting it wholesale when
216 ;; I noticed that. After the whole system is working, though, I'd like to
217 ;; revisit this decision. -- WHN 19990713
218 (let ((num (- (length *interpreted-function-cache*)
219 *interpreted-function-cache-minimum-size*)))
221 (setq *interpreted-function-cache*
222 (delete-if #'(lambda (x)
223 (when (>= (interpreted-function-gcs x)
224 *interpreted-function-cache-threshold*)
225 (setf (interpreted-function-definition x) nil)
227 *interpreted-function-cache*
229 (dolist (fun *interpreted-function-cache*)
230 (incf (interpreted-function-gcs fun))))
231 (pushnew 'interpreter-gc-hook sb!ext:*before-gc-hooks*)
233 (defun flush-interpreted-function-cache ()
235 "Clear all entries in the eval function cache. This allows the internal
236 representation of the functions to be reclaimed, and also lazily forces
237 macroexpansions to be recomputed."
238 (dolist (fun *interpreted-function-cache*)
239 (setf (interpreted-function-definition fun) nil))
240 (setq *interpreted-function-cache* ()))
242 ;;;; INTERNAL-APPLY-LOOP macros
244 ;;;; These macros are intimately related to INTERNAL-APPLY-LOOP. They assume
245 ;;;; variables established by this function, and they assume they can return
246 ;;;; from a block by that name. This is sleazy, but we justify it as follows:
247 ;;;; They are so specialized in use, and their invocation became lengthy, that
248 ;;;; we allowed them to slime some access to things in their expanding
249 ;;;; environment. These macros don't really extend our Lisp syntax, but they do
250 ;;;; provide some template expansion service; it is these cleaner circumstance
251 ;;;; that require a more rigid programming style.
253 ;;;; Since these are macros expanded almost solely for COMBINATION nodes,
254 ;;;; they cascade from the end of this logical page to the beginning here.
255 ;;;; Therefore, it is best you start looking at them from the end of this
256 ;;;; section, backwards from normal scanning mode for Lisp code.
258 ;;; This runs a function on some arguments from the stack. If the combination
259 ;;; occurs in a tail recursive position, then we do the call such that we
260 ;;; return from tail-p-function with whatever values the call produces. With a
261 ;;; :local call, we have to restore the stack to its previous frame before
262 ;;; doing the call. The :full call mechanism does this for us. If it is NOT a
263 ;;; tail recursive call, and we're in a multiple value context, then then push
264 ;;; a list of the returned values. Do the same thing if we're in a :return
265 ;;; context. Push a single value, without listifying it, for a :single value
266 ;;; context. Otherwise, just call for side effect.
268 ;;; Node is the combination node, and cont is its continuation. Frame-ptr
269 ;;; is the current frame pointer, and closure is the current environment for
270 ;;; closure variables. Call-type is either :full or :local, and when it is
271 ;;; local, lambda is the IR1 lambda to apply.
273 ;;; This assumes the following variables are present: node, cont, frame-ptr,
274 ;;; and closure. It also assumes a block named internal-apply-loop.
276 ;;; FIXME: used only in this file, needn't be in runtime
277 ;;; FIXME: down with DO-FOO names for non-iteration constructs!
278 (defmacro do-combination (call-type lambda mv-or-normal)
279 (let* ((args (gensym))
280 (calling-closure (gensym))
281 (invoke-fun (ecase mv-or-normal
282 (:mv-call 'mv-internal-invoke)
283 (:normal 'internal-invoke)))
284 (args-form (ecase mv-or-normal
287 (length (sb!c::mv-combination-args node))))
289 `(eval-stack-args (sb!c:lambda-eval-info-args-passed
290 (sb!c::lambda-info ,lambda))))))
291 (call-form (ecase call-type
293 (length (sb!c::basic-combination-args node))))
294 (:local `(internal-apply
296 (compute-closure node ,lambda frame-ptr
303 ;; INVOKE-FUN takes care of the stack itself.
304 (,invoke-fun (length (sb!c::basic-combination-args node))
306 (:local `(let ((,args ,args-form)
308 (compute-closure node ,lambda frame-ptr closure)))
309 ;; No need to clean up stack slots for GC due to
310 ;; SB!EXT:*BEFORE-GC-HOOK*.
311 (eval-stack-set-top frame-ptr)
314 (internal-apply ,lambda ,args ,calling-closure
316 `(cond ((sb!c::node-tail-p node)
319 (ecase (sb!c::continuation-info cont)
321 (eval-stack-push (multiple-value-list ,call-form)))
323 (eval-stack-push ,call-form))
324 (:unused ,call-form))))))
326 ;;; This sets the variable block in INTERNAL-APPLY-LOOP, and it announces this
327 ;;; by setting set-block-p for later loop iteration maintenance.
329 ;;; FIXME: used only in this file, needn't be in runtime
330 (defmacro set-block (exp)
333 (setf set-block-p t)))
335 ;;; This sets all the iteration variables in INTERNAL-APPLY-LOOP to iterate
336 ;;; over a new block's nodes. Block-exp is optional because sometimes we have
337 ;;; already set block, and we only need to bring the others into agreement.
338 ;;; If we already set block, then clear the variable that announces this,
341 ;;; FIXME: used only in this file, needn't be in runtime
342 (defmacro change-blocks (&optional block-exp)
345 `(setf block ,block-exp)
346 `(setf set-block-p nil))
347 (setf node (sb!c::continuation-next (sb!c::block-start block)))
348 (setf last-cont (sb!c::node-cont (sb!c::block-last block)))))
350 ;;; This controls printing visited nodes in INTERNAL-APPLY-LOOP. We use it
351 ;;; here, and INTERNAL-INVOKE uses it to print function call looking output
352 ;;; to further describe sb!c::combination nodes.
353 (defvar *internal-apply-node-trace* nil)
354 (defun maybe-trace-funny-fun (node name &rest args)
355 (when *internal-apply-node-trace*
356 (format t "(~S ~{ ~S~}) c~S~%"
357 name args (sb!c::cont-num (sb!c::node-cont node)))))
359 ;;; This implements the intention of the virtual function name. This is a
360 ;;; macro because some of these actions must occur without a function call.
361 ;;; For example, calling a dispatch function to implement special binding would
362 ;;; be a no-op because returning from that function would cause the system to
363 ;;; undo any special bindings it established.
365 ;;; NOTE: update SB!C:ANNOTATE-COMPONENT-FOR-EVAL and/or
366 ;;; sb!c::undefined-funny-funs if you add or remove branches in this routine.
368 ;;; This assumes the following variables are present: node, cont, frame-ptr,
369 ;;; args, closure, block, and last-cont. It also assumes a block named
370 ;;; internal-apply-loop.
372 ;;; FIXME: used only in this file, needn't be in runtime
373 ;;; FIXME: down with DO-FOO names for non-iteration constructs!
374 (defmacro do-funny-function (funny-fun-name)
375 (let ((name (gensym)))
376 `(let ((,name ,funny-fun-name))
379 (let ((value (eval-stack-pop))
380 (global-var (eval-stack-pop)))
381 (maybe-trace-funny-fun node ,name global-var value)
382 (sb!sys:%primitive sb!c:bind
384 (sb!c::global-var-name global-var))))
385 (sb!c::%special-unbind
386 ;; Throw away arg telling me which special, and tell the dynamic
387 ;; binding mechanism to unbind one variable.
389 (maybe-trace-funny-fun node ,name)
390 (sb!sys:%primitive sb!c:unbind))
392 (let* ((tag (eval-stack-pop))
393 (nlx-info (eval-stack-pop))
395 ;; Ultimately THROW and CATCH will fix the interpreter's stack
396 ;; since this is necessary for compiled CATCH's and those in
397 ;; the initial top level function.
398 (stack-top *eval-stack-top*)
402 (maybe-trace-funny-fun node ,name tag)
403 (multiple-value-setq (block node cont last-cont)
404 (internal-apply-loop (sb!c::continuation-next cont)
405 frame-ptr lambda args closure))
406 (setf fell-through-p t)))))
407 (cond (fell-through-p
408 ;; We got here because we just saw the SB!C::%CATCH-BREAKUP
409 ;; funny function inside the above recursive call to
410 ;; INTERNAL-APPLY-LOOP. Therefore, we just received and
411 ;; stored the current state of evaluation for falling
415 ;; Fix up the interpreter's stack after having thrown here.
416 ;; We won't need to do this in the final implementation.
417 (eval-stack-set-top stack-top)
418 ;; Take the values received in the list bound above, and
419 ;; massage them into the form expected by the continuation
420 ;; of the non-local-exit info.
421 (ecase (sb!c::continuation-info
422 (sb!c::nlx-info-continuation nlx-info))
424 (eval-stack-push (car values)))
426 (eval-stack-push values))
428 ;; We want to continue with the code after the CATCH body.
429 ;; The non-local-exit info tells us where this is, but we
430 ;; know that block only contains a call to the funny
431 ;; function SB!C::%NLX-ENTRY, which simply is a place holder
432 ;; for the compiler IR1. We want to skip the target block
433 ;; entirely, so we say it is the block we're in now and say
434 ;; the current cont is the last-cont. This makes the COND
435 ;; at the end of INTERNAL-APPLY-LOOP do the right thing.
436 (setf block (sb!c::nlx-info-target nlx-info))
437 (setf cont last-cont)))))
438 (sb!c::%unwind-protect
439 ;; Cleanup function not pushed due to special-case :UNUSED
440 ;; annotation in ANNOTATE-COMPONENT-FOR-EVAL.
441 (let* ((nlx-info (eval-stack-pop))
443 (stack-top *eval-stack-top*))
446 (maybe-trace-funny-fun node ,name)
447 (multiple-value-setq (block node cont last-cont)
448 (internal-apply-loop (sb!c::continuation-next cont)
449 frame-ptr lambda args closure))
450 (setf fell-through-p t))
451 (cond (fell-through-p
452 ;; We got here because we just saw the
453 ;; SB!C::%UNWIND-PROTECT-BREAKUP funny function inside the
454 ;; above recursive call to INTERNAL-APPLY-LOOP.
455 ;; Therefore, we just received and stored the current
456 ;; state of evaluation for falling through.
459 ;; Fix up the interpreter's stack after having thrown
460 ;; here. We won't need to do this in the final
462 (eval-stack-set-top stack-top)
463 ;; Push some bogus values for exit context to keep the
464 ;; MV-BIND in the UNWIND-PROTECT translation happy.
465 (eval-stack-push '(nil nil 0))
466 (let ((node (sb!c::continuation-next
468 (car (sb!c::block-succ
469 (sb!c::nlx-info-target nlx-info)))))))
470 (internal-apply-loop node frame-ptr lambda args
472 ((sb!c::%catch-breakup
473 sb!c::%unwind-protect-breakup
474 sb!c::%continue-unwind)
475 ;; This shows up when we locally exit a CATCH body -- fell through.
476 ;; Return the current state of evaluation to the previous invocation
477 ;; of INTERNAL-APPLY-LOOP which happens to be running in the
478 ;; SB!C::%CATCH branch of this code.
479 (maybe-trace-funny-fun node ,name)
480 (return-from internal-apply-loop
481 (values block node cont last-cont)))
483 (maybe-trace-funny-fun node ,name)
484 ;; This just marks a spot in the code for CATCH, UNWIND-PROTECT, and
485 ;; non-local lexical exits (GO or RETURN-FROM).
486 ;; Do nothing since sb!c::%catch does it all when it catches a THROW.
487 ;; Do nothing since sb!c::%unwind-protect does it all when
488 ;; it catches a THROW.
490 (sb!c::%more-arg-context
491 (let* ((fixed-arg-count (1+ (eval-stack-pop)))
492 ;; Add 1 to actual fixed count for extra arg expected by
493 ;; external entry points (XEP) which some IR1 lambdas have.
494 ;; The extra arg is the number of arguments for arg count
495 ;; consistency checking. SB!C::%MORE-ARG-CONTEXT always runs
496 ;; within an XEP, so the lambda has an extra arg.
497 (more-args (nthcdr fixed-arg-count args)))
498 (maybe-trace-funny-fun node ,name fixed-arg-count)
499 (aver (eq (sb!c::continuation-info cont) :multiple))
500 (eval-stack-push (list more-args (length more-args)))))
501 (sb!c::%unknown-values
502 (error "SB!C::%UNKNOWN-VALUES should never be in interpreter's IR1."))
503 (sb!c::%lexical-exit-breakup
504 ;; We see this whenever we locally exit the extent of a lexical
505 ;; target. That is, we are truly locally exiting an extent we could
506 ;; have non-locally lexically exited. Return the :fell-through flag
507 ;; and the current state of evaluation to the previous invocation
508 ;; of INTERNAL-APPLY-LOOP which happens to be running in the
509 ;; sb!c::entry branch of INTERNAL-APPLY-LOOP.
510 (maybe-trace-funny-fun node ,name)
511 ;; Discard the NLX-INFO arg...
513 (return-from internal-apply-loop
514 (values :fell-through block node cont last-cont)))))))
516 ;;; This expands for the two types of combination nodes INTERNAL-APPLY-LOOP
517 ;;; sees. Type is either :mv-call or :normal. Node is the combination node,
518 ;;; and cont is its continuation. Frame-ptr is the current frame pointer, and
519 ;;; closure is the current environment for closure variables.
521 ;;; Most of the real work is done by DO-COMBINATION. This first determines if
522 ;;; the combination node describes a :full call which DO-COMBINATION directly
523 ;;; handles. If the call is :local, then we either invoke an IR1 lambda, or we
524 ;;; just bind some LET variables. If the call is :local, and type is :mv-call,
525 ;;; then we can only be binding multiple values. Otherwise, the combination
526 ;;; node describes a function known to the compiler, but this may be a funny
527 ;;; function that actually isn't ever defined. We either take some action for
528 ;;; the funny function or do a :full call on the known true function, but the
529 ;;; interpreter doesn't do optimizing stuff for functions known to the
532 ;;; This assumes the following variables are present: node, cont, frame-ptr,
533 ;;; and closure. It also assumes a block named internal-apply-loop.
535 ;;; FIXME: used only in this file, needn't be in runtime
536 (defmacro combination-node (type)
537 (let* ((kind (gensym))
541 (letp-bind (ecase type
544 `((,letp (eq (sb!c::functional-kind ,lambda) :let))))))
548 `(store-mv-let-vars ,lambda frame-ptr
549 (length (sb!c::mv-combination-args node))))
552 (store-let-vars ,lambda frame-ptr)
553 (do-combination :local ,lambda ,type))))))
554 `(let ((,kind (sb!c::basic-combination-kind node))
555 (,fun (sb!c::basic-combination-fun node)))
556 (cond ((member ,kind '(:full :error))
557 (do-combination :full nil ,type))
559 (let* ((,lambda (sb!c::ref-leaf (sb!c::continuation-use ,fun)))
562 ((eq (sb!c::continuation-info ,fun) :unused)
563 (aver (typep ,kind 'sb!c::function-info))
564 (do-funny-function (sb!c::continuation-function-name ,fun)))
566 (aver (typep ,kind 'sb!c::function-info))
567 (do-combination :full nil ,type))))))
569 (defun trace-eval (on)
570 (setf *eval-stack-trace* on)
571 (setf *internal-apply-node-trace* on))
575 ;;; Evaluate an arbitary form. We convert the form, then call internal
576 ;;; APPLY on it. If *ALREADY-EVALED-THIS* is true, then we bind it to
577 ;;; NIL around the apply to limit the inhibition to the lexical scope
578 ;;; of the EVAL-WHEN.
579 (defun internal-eval (form)
580 (let ((res (sb!c:compile-for-eval form)))
581 (if *already-evaled-this*
582 (let ((*already-evaled-this* nil))
583 (internal-apply res nil '#()))
584 (internal-apply res nil '#()))))
586 ;;; Later this will probably be the same weird internal thing the compiler
587 ;;; makes to represent these things.
588 (defun make-indirect-value-cell (value)
590 ;;; FIXME: used only in this file, needn't be in runtime
591 (defmacro indirect-value (value-cell)
594 ;;; This passes on a node's value appropriately, possibly returning from
595 ;;; function to do so. When we are tail-p, don't push the value, return it on
596 ;;; the system's actual call stack; when we blow out of function this way, we
597 ;;; must return the interpreter's stack to the its state before this call to
598 ;;; function. When we're in a multiple value context or heading for a return
599 ;;; node, we push a list of the value for easier handling later. Otherwise,
600 ;;; just push the value on the interpreter's stack.
602 ;;; FIXME: maybe used only in this file, if so, needn't be in runtime
603 (defmacro value (node info value frame-ptr function)
604 `(cond ((sb!c::node-tail-p ,node)
605 (eval-stack-set-top ,frame-ptr)
606 (return-from ,function ,value))
607 ((member ,info '(:multiple :return) :test #'eq)
608 (eval-stack-push (list ,value)))
609 (t (aver (eq ,info :single))
610 (eval-stack-push ,value))))
612 (defun maybe-trace-nodes (node)
613 (when *internal-apply-node-trace*
614 (format t "<~A-node> c~S~%"
616 (sb!c::cont-num (sb!c::node-cont node)))))
618 ;;; This interprets lambda, a compiler IR1 data structure representing a
619 ;;; function, applying it to args. Closure is the environment in which to run
620 ;;; lambda, the variables and such closed over to form lambda. The call occurs
621 ;;; on the interpreter's stack, so save the current top and extend the stack
622 ;;; for this lambda's call frame. Then store the args into locals on the
625 ;;; Args is the list of arguments to apply to. If IGNORE-UNUSED is true, then
626 ;;; values for un-read variables are present in the argument list, and must be
627 ;;; discarded (always true except in a local call.) Args may run out of values
628 ;;; before vars runs out of variables (in the case of an XEP with optionals);
629 ;;; we just do CAR of nil and store nil. This is not the proper defaulting
630 ;;; (which is done by explicit code in the XEP.)
631 (defun internal-apply (lambda args closure &optional (ignore-unused t))
632 (let ((frame-ptr *eval-stack-top*))
633 (eval-stack-extend (sb!c:lambda-eval-info-frame-size (sb!c::lambda-info lambda)))
634 (do ((vars (sb!c::lambda-vars lambda) (cdr vars))
637 (let ((var (car vars)))
638 (cond ((sb!c::leaf-refs var)
639 (setf (eval-stack-local frame-ptr (sb!c::lambda-var-info var))
640 (if (sb!c::lambda-var-indirect var)
641 (make-indirect-value-cell (pop args))
643 (ignore-unused (pop args)))))
644 (internal-apply-loop (sb!c::lambda-bind lambda) frame-ptr lambda args
647 ;;; This does the work of INTERNAL-APPLY. This also calls itself
648 ;;; recursively for certain language features, such as CATCH. First is
649 ;;; the node at which to start interpreting. FRAME-PTR is the current
650 ;;; frame pointer for accessing local variables. LAMBDA is the IR1
651 ;;; lambda from which comes the nodes a given call to this function
652 ;;; processes, and CLOSURE is the environment for interpreting LAMBDA.
653 ;;; ARGS is the argument list for the lambda given to INTERNAL-APPLY,
654 ;;; and we have to carry it around with us in case of &more-arg or
655 ;;; &rest-arg processing which is represented explicitly in the
658 ;;; KLUDGE: Due to having a truly tail recursive interpreter, some of
659 ;;; the branches handling a given node need to RETURN-FROM this
660 ;;; routine. Also, some calls this makes to do work for it must occur
661 ;;; in tail recursive positions. Because of this required access to
662 ;;; this function lexical environment and calling positions, we often
663 ;;; are unable to break off logical chunks of code into functions. We
664 ;;; have written macros intended solely for use in this routine, and
665 ;;; due to all the local stuff they need to access and length complex
666 ;;; calls, we have written them to sleazily access locals from this
667 ;;; routine. In addition to assuming a block named internal-apply-loop
668 ;;; exists, they set and reference the following variables: NODE,
669 ;;; CONT, FRAME-PTR, CLOSURE, BLOCK, LAST-CONT, and SET-BLOCK-P.
670 ;;; FIXME: Perhaps this kludge could go away if we convert to a
671 ;;; compiler-only implementation?
672 (defun internal-apply-loop (first frame-ptr lambda args closure)
673 ;; FIXME: This will cause source code location information to be compiled
674 ;; into the executable, which will probably cause problems for users running
675 ;; without the sources and/or without the build-the-system readtable.
676 (declare (optimize (debug 2)))
677 (let* ((block (sb!c::node-block first))
678 (last-cont (sb!c::node-cont (sb!c::block-last block)))
682 (let ((cont (sb!c::node-cont node)))
685 (maybe-trace-nodes node)
686 (let ((info (sb!c::continuation-info cont)))
687 (unless (eq info :unused)
688 (value node info (leaf-value node frame-ptr closure)
689 frame-ptr internal-apply-loop))))
691 (maybe-trace-nodes node)
692 (combination-node :normal))
694 (maybe-trace-nodes node)
695 ;; IF nodes always occur at the end of a block, so pick another.
696 (set-block (if (eval-stack-pop)
697 (sb!c::if-consequent node)
698 (sb!c::if-alternative node))))
700 (maybe-trace-nodes node)
701 ;; Ignore bind nodes since INTERNAL-APPLY extends the stack for
702 ;; all of a lambda's locals, and the sb!c::combination branch
703 ;; handles LET binds (moving values off stack top into locals).
706 (maybe-trace-nodes node)
707 (let ((info (sb!c::continuation-info cont))
708 (res (set-leaf-value node frame-ptr closure
710 (unless (eq info :unused)
711 (value node info res frame-ptr internal-apply-loop))))
713 (maybe-trace-nodes node)
714 (let ((info (cdr (assoc node (sb!c:lambda-eval-info-entries
715 (sb!c::lambda-info lambda))))))
716 ;; No info means no-op entry for CATCH or UNWIND-PROTECT.
718 ;; Store stack top for restoration in local exit situation
719 ;; in sb!c::exit branch.
720 (setf (eval-stack-local frame-ptr
721 (sb!c:entry-node-info-st-top info))
723 (let ((tag (sb!c:entry-node-info-nlx-tag info)))
725 ;; Non-local lexical exit (someone closed over a
726 ;; GO tag or BLOCK name).
727 (let ((unique-tag (cons nil nil))
729 (setf (eval-stack-local frame-ptr tag) unique-tag)
730 (if (eq cont last-cont)
731 (change-blocks (car (sb!c::block-succ block)))
732 (setf node (sb!c::continuation-next cont)))
734 (multiple-value-setq (values block node cont last-cont)
736 (internal-apply-loop node frame-ptr
737 lambda args closure)))
739 (when (eq values :fell-through)
740 ;; We hit a %LEXICAL-EXIT-BREAKUP.
741 ;; Interpreting state is set with MV-SETQ above.
742 ;; Just get out of this branch and go on.
745 (unless (eq values :non-local-go)
746 ;; We know we're non-locally exiting from a
747 ;; BLOCK with values (saw a RETURN-FROM).
748 (ecase (sb!c::continuation-info cont)
750 (eval-stack-push (car values)))
752 (eval-stack-push values))
754 ;; Start interpreting again at the target, skipping
755 ;; the %NLX-ENTRY block.
757 (sb!c::continuation-next
759 (car (sb!c::block-succ block))))))))))))
761 (maybe-trace-nodes node)
762 (let* ((incoming-values (sb!c::exit-value node))
763 (values (if incoming-values (eval-stack-pop))))
765 ((eq (sb!c::lambda-environment lambda)
766 (sb!c::block-environment
767 (sb!c::node-block (sb!c::exit-entry node))))
769 ;; Fixup stack top and massage values for destination.
771 (eval-stack-local frame-ptr
772 (sb!c:entry-node-info-st-top
773 (cdr (assoc (sb!c::exit-entry node)
774 (sb!c:lambda-eval-info-entries
775 (sb!c::lambda-info lambda)))))))
776 (ecase (sb!c::continuation-info cont)
778 (aver incoming-values)
779 (eval-stack-push (car values)))
781 (aver incoming-values)
782 (eval-stack-push values))
785 (let ((info (sb!c::find-nlx-info (sb!c::exit-entry node)
790 (sb!c::environment-closure
791 (sb!c::node-environment node))
794 (values values (sb!c::nlx-info-target info) nil cont)
795 (values :non-local-go (sb!c::nlx-info-target info)))))))))
797 (maybe-trace-nodes node)
798 (let ((values (eval-stack-pop)))
799 (eval-stack-set-top frame-ptr)
800 (return-from internal-apply-loop (values-list values))))
801 (sb!c::mv-combination
802 (maybe-trace-nodes node)
803 (combination-node :mv-call)))
804 ;; See function doc below.
805 (reference-this-var-to-keep-it-alive node)
806 (reference-this-var-to-keep-it-alive frame-ptr)
807 (reference-this-var-to-keep-it-alive closure)
808 (cond ((not (eq cont last-cont))
809 (setf node (sb!c::continuation-next cont)))
810 ;; Currently only the last node in a block causes this loop to
811 ;; change blocks, so we never just go to the next node when
812 ;; the current node's branch tried to change blocks.
816 ;; CIF nodes set the block for us, but other last
818 (change-blocks (car (sb!c::block-succ block)))))))))
820 ;;; This function allows a reference to a variable that the compiler cannot
821 ;;; easily eliminate as unnecessary. We use this at the end of the node
822 ;;; dispatch in INTERNAL-APPLY-LOOP to make sure the node variable has a
823 ;;; valid value. Each node branch tends to reference it at the beginning,
824 ;;; and then there is no reference but a set at the end; the compiler then
825 ;;; kills the variable between the reference in the dispatch branch and when
826 ;;; we set it at the end. The problem is that most error will occur in the
827 ;;; interpreter within one of these node dispatch branches.
828 (defun reference-this-var-to-keep-it-alive (node)
831 ;;; This sets a SB!C::CSET node's var to value, returning value. When
832 ;;; var is local, we have to compare its home environment to the
833 ;;; current one, node's environment. If they're the same, we check to
834 ;;; see whether the var is indirect, and store the value on the stack
835 ;;; or in the value cell as appropriate. Otherwise, var is a closure
836 ;;; variable, and since we're setting it, we know its location
837 ;;; contains an indirect value object.
838 (defun set-leaf-value (node frame-ptr closure value)
839 (let ((var (sb!c::set-var node)))
842 (set-leaf-value-lambda-var node var frame-ptr closure value))
844 (setf (symbol-value (sb!c::global-var-name var)) value)))))
846 ;;; This does SET-LEAF-VALUE for a LAMBDA-VAR leaf. The debugger tools'
847 ;;; internals use this also to set interpreted local variables.
848 (defun set-leaf-value-lambda-var (node var frame-ptr closure value)
849 ;; Note: We avoid trying to set a lexical variable with no refs
850 ;; because the compiler deletes such variables.
851 (when (sb!c::leaf-refs var)
852 (let ((env (sb!c::node-environment node)))
853 (cond ((not (eq (sb!c::lambda-environment (sb!c::lambda-var-home var))
855 (setf (indirect-value
857 (position var (sb!c::environment-closure env)
860 ((sb!c::lambda-var-indirect var)
861 (setf (indirect-value
862 (eval-stack-local frame-ptr (sb!c::lambda-var-info var)))
865 (setf (eval-stack-local frame-ptr (sb!c::lambda-var-info var))
868 ;;; This figures out how to return a value for a ref node. LEAF is the
869 ;;; ref's structure that tells us about the value, and it is one of
870 ;;; the following types:
871 ;;; constant -- It knows its own value.
872 ;;; global-var -- It's either a value or function reference. Get it right.
873 ;;; local-var -- This may on the stack or in the current closure, the
874 ;;; environment for the lambda INTERNAL-APPLY is currently
875 ;;; executing. If the leaf's home environment is the same
876 ;;; as the node's home environment, then the value is on the
877 ;;; stack, else it's in the closure since it came from another
878 ;;; environment. Whether the var comes from the stack or the
879 ;;; closure, it could have come from a closure, and it could
880 ;;; have been closed over for setting. When this happens, the
881 ;;; actual value is stored in an indirection object, so
882 ;;; indirect. See COMPUTE-CLOSURE for the description of
883 ;;; the structure of the closure argument to this function.
884 ;;; functional -- This is a reference to an interpreted function that may
885 ;;; be passed or called anywhere. We return a real function
886 ;;; that calls INTERNAL-APPLY, closing over the leaf. We also
887 ;;; have to compute a closure, running environment, for the
888 ;;; lambda in case it references stuff in the current
889 ;;; environment. If the closure is empty and there is no
890 ;;; functional environment, then we use
891 ;;; MAKE-INTERPRETED-FUNCTION to make a cached translation.
892 ;;; Since it is too late to lazily convert, we set up the
893 ;;; INTERPRETED-FUNCTION to be already converted.
894 (defun leaf-value (node frame-ptr closure)
895 (let ((leaf (sb!c::ref-leaf node)))
898 (sb!c::constant-value leaf))
900 (locally (declare (optimize (safety 1)))
901 (if (eq (sb!c::global-var-kind leaf) :global-function)
902 (let ((name (sb!c::global-var-name leaf)))
904 (symbol-function name)
906 (symbol-value (sb!c::global-var-name leaf)))))
908 (leaf-value-lambda-var node leaf frame-ptr closure))
910 (let* ((calling-closure (compute-closure node leaf frame-ptr closure))
911 (real-fun (sb!c::functional-entry-function leaf))
912 (arg-doc (sb!c::functional-arg-documentation real-fun)))
913 (cond ((sb!c:lambda-eval-info-function (sb!c::leaf-info leaf)))
914 ((and (zerop (length calling-closure))
915 (null (sb!c::lexenv-functions
916 (sb!c::functional-lexenv real-fun))))
917 (let ((res (make-interpreted-function
918 (sb!c::functional-inline-expansion real-fun))))
919 (push res *interpreted-function-cache*)
920 (setf (interpreted-function-definition res) leaf)
921 (setf (interpreted-function-converted-once res) t)
922 (setf (interpreted-function-arglist res) arg-doc)
923 (setf (interpreted-function-%name res)
924 (sb!c::leaf-name real-fun))
925 (setf (sb!c:lambda-eval-info-function
926 (sb!c::leaf-info leaf)) res)
929 (let ((res (%make-interpreted-function
931 :%name (sb!c::leaf-name real-fun)
933 :closure calling-closure)))
934 (setf (funcallable-instance-function res)
935 #'(instance-lambda (&rest args)
936 (declare (list args))
938 (interpreted-function-definition res)
939 (cons (length args) args)
940 (interpreted-function-closure res))))
943 ;;; This does LEAF-VALUE for a lambda-var leaf. The debugger tools' internals
944 ;;; uses this also to reference interpreted local variables.
945 (defun leaf-value-lambda-var (node leaf frame-ptr closure)
946 (let* ((env (sb!c::node-environment node))
948 (if (eq (sb!c::lambda-environment (sb!c::lambda-var-home leaf))
950 (eval-stack-local frame-ptr (sb!c::lambda-var-info leaf))
952 (position leaf (sb!c::environment-closure env)
954 (if (sb!c::lambda-var-indirect leaf)
955 (indirect-value temp)
958 ;;; Compute a closure for a local call and for returned call'able
959 ;;; closure objects. Sometimes the closure is a SIMPLE-VECTOR of no
960 ;;; elements. NODE is either a reference node or a combination node.
961 ;;; LEAF is either the leaf of the reference node or the lambda to
962 ;;; internally apply for the combination node. FRAME-PTR is the
963 ;;; current frame pointer for fetching current values to store in the
964 ;;; closure. CLOSURE is the current closure, the closed-over
965 ;;; environment of the currently interpreting LAMBDA.
967 ;;; A computed closure is a vector corresponding to the list of
968 ;;; closure variables described in an environment. The position of a
969 ;;; lambda-var in this closure list is the index into the closure
970 ;;; vector of values.
971 (defun compute-closure (node leaf frame-ptr closure)
972 (let* ((current-env (sb!c::node-environment node))
973 (current-closure-vars (sb!c::environment-closure current-env))
974 ;; FUNCTIONAL-ENV is the environment description for leaf,
975 ;; the lambda for which we're computing a closure. This
976 ;; environment describes which of lambda's vars we find in
977 ;; lambda's closure when it's running, versus finding them on
979 (functional-env (sb!c::lambda-environment leaf))
980 (functional-closure-vars (sb!c::environment-closure functional-env))
981 (functional-closure (make-array (length functional-closure-vars))))
982 ;; For each lambda-var VAR in the functional environment's closure
983 ;; list, if the VAR's home environment is the current environment,
984 ;; then get a value off the stack and store it in the closure
985 ;; we're computing. Otherwise VAR's value comes from somewhere
986 ;; else, but we have it in our current closure, the environment
987 ;; we're running in as we compute this new closure. Find this
988 ;; value the same way we do in LEAF-VALUE, by finding VAR's
989 ;; position in the current environment's description of the
991 (do ((vars functional-closure-vars (cdr vars))
994 (let ((ele (car vars)))
995 (setf (svref functional-closure i)
998 (if (eq (sb!c::lambda-environment (sb!c::lambda-var-home ele))
1000 (eval-stack-local frame-ptr (sb!c::lambda-var-info ele))
1002 (position ele current-closure-vars
1005 (if (eq (sb!c::block-environment (sb!c::nlx-info-target ele))
1009 (sb!c:entry-node-info-nlx-tag
1010 (cdr (assoc ;; entry node for non-local extent
1011 (sb!c::cleanup-mess-up
1012 (sb!c::nlx-info-cleanup ele))
1013 (sb!c::lambda-eval-info-entries
1015 ;; the lambda INTERNAL-APPLY-LOOP tosses around
1016 (sb!c::environment-function
1017 (sb!c::node-environment node))))))))
1019 (position ele current-closure-vars
1021 functional-closure))
1023 ;;; INTERNAL-APPLY uses this to invoke a function from the
1024 ;;; interpreter's stack on some arguments also taken from the stack.
1025 ;;; When tail-p is non-nil, control does not return to INTERNAL-APPLY
1026 ;;; to further interpret the current IR1 lambda, so INTERNAL-INVOKE
1027 ;;; must clean up the current interpreter's stack frame.
1028 (defun internal-invoke (arg-count &optional tailp)
1029 (let ((args (eval-stack-args arg-count)) ;LET says this init form runs first.
1030 (fun (eval-stack-pop)))
1031 (when tailp (eval-stack-set-top tailp))
1032 (when *internal-apply-node-trace*
1033 (format t "(~S~{ ~S~})~%" fun args))
1036 ;;; This is almost just like INTERNAL-INVOKE. We call
1037 ;;; MV-EVAL-STACK-ARGS, and our function is in a list on the stack
1038 ;;; instead of simply on the stack.
1039 (defun mv-internal-invoke (arg-count &optional tailp)
1040 (let ((args (mv-eval-stack-args arg-count)) ;LET runs this init form first.
1041 (fun (car (eval-stack-pop))))
1042 (when tailp (eval-stack-set-top tailp))
1043 (when *internal-apply-node-trace*
1044 (format t "(~S~{ ~S~})~%" fun args))
1047 ;;; Return a list of the top arg-count elements on the interpreter's
1048 ;;; stack. This removes them from the stack.
1049 (defun eval-stack-args (arg-count)
1051 (dotimes (i arg-count args)
1052 (push (eval-stack-pop) args))))
1054 ;;; This assumes the top count elements on interpreter's stack are
1055 ;;; lists. This returns a single list with all the elements from these
1057 (defun mv-eval-stack-args (count)
1060 (let ((last (eval-stack-pop)))
1061 (dotimes (i (1- count))
1062 (let ((next (eval-stack-pop)))
1064 (if next (nconc next last) last))))
1067 ;;; This stores lambda's vars, stack locals, from values popped off the stack.
1068 ;;; When a var has no references, the compiler computes IR1 such that the
1069 ;;; continuation delivering the value for the unreference var appears unused.
1070 ;;; Because of this, the interpreter drops the value on the floor instead of
1071 ;;; saving it on the stack for binding, so we only pop a value when the var has
1072 ;;; some reference. INTERNAL-APPLY uses this for sb!c::combination nodes
1073 ;;; representing LET's.
1075 ;;; When storing the local, if it is indirect, then someone closes over it for
1076 ;;; setting instead of just for referencing. We then store an indirection cell
1077 ;;; with the value, and the referencing code for locals knows how to get the
1079 (defun store-let-vars (lambda frame-ptr)
1080 (let* ((vars (sb!c::lambda-vars lambda))
1081 (args (eval-stack-args (count-if #'sb!c::leaf-refs vars))))
1082 (declare (list vars args))
1084 (when (sb!c::leaf-refs v)
1085 (setf (eval-stack-local frame-ptr (sb!c::lambda-var-info v))
1086 (if (sb!c::lambda-var-indirect v)
1087 (make-indirect-value-cell (pop args))
1090 ;;; This is similar to STORE-LET-VARS, but the values for the locals
1091 ;;; appear on the stack in a list due to forms that delivered multiple
1092 ;;; values to this lambda/let. Unlike STORE-LET-VARS, there is no
1093 ;;; control over the delivery of a value for an unreferenced var, so
1094 ;;; we drop the corresponding value on the floor when no one
1095 ;;; references it. INTERNAL-APPLY uses this for sb!c::mv-combination
1096 ;;; nodes representing LET's.
1097 (defun store-mv-let-vars (lambda frame-ptr count)
1099 (let ((args (eval-stack-pop)))
1100 (dolist (v (sb!c::lambda-vars lambda))
1101 (if (sb!c::leaf-refs v)
1102 (setf (eval-stack-local frame-ptr (sb!c::lambda-var-info v))
1103 (if (sb!c::lambda-var-indirect v)
1104 (make-indirect-value-cell (pop args))
1109 ;;; This stores lambda's vars, stack locals, from multiple values stored on the
1110 ;;; top of the stack in a list. Since these values arrived multiply, there is
1111 ;;; no control over the delivery of each value for an unreferenced var, so
1112 ;;; unlike STORE-LET-VARS, we have values for variables never used. We drop
1113 ;;; the value corresponding to an unreferenced var on the floor.
1114 ;;; INTERNAL-APPLY uses this for sb!c::mv-combination nodes representing LET's.
1116 ;;; IR1 represents variables bound from multiple values in a list in the
1117 ;;; opposite order of the values list. We use STORE-MV-LET-VARS-AUX to recurse
1118 ;;; down the vars list until we bottom out, storing values on the way back up
1119 ;;; the recursion. You must do this instead of NREVERSE'ing the args list, so
1120 ;;; when we run out of values, we store nil's in the correct lambda-vars.
1121 (defun store-mv-let-vars (lambda frame-ptr count)
1123 (print (sb!c::lambda-vars lambda))
1124 (store-mv-let-vars-aux frame-ptr (sb!c::lambda-vars lambda) (eval-stack-pop)))
1125 (defun store-mv-let-vars-aux (frame-ptr vars args)
1127 (let ((remaining-args (store-mv-let-vars-aux frame-ptr (cdr vars) args))
1129 (when (sb!c::leaf-refs v)
1130 (setf (eval-stack-local frame-ptr (sb!c::lambda-var-info v))
1131 (if (sb!c::lambda-var-indirect v)
1132 (make-indirect-value-cell (car remaining-args))
1133 (car remaining-args))))
1134 (cdr remaining-args))