/[cmucl]/src/compiler/eval.lisp
ViewVC logotype

Contents of /src/compiler/eval.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.19.1.1 - (show annotations) (vendor branch)
Tue Oct 15 14:40:20 1991 UTC (22 years, 6 months ago) by wlott
Branch: gengc
Changes since 1.19: +46 -41 lines
Just mutator slots for eval-stack and eval-stack-top instead of specials.
This is really only needed for threads support, but why not change it now?
1 ;;; -*- Package: eval; Log: C.Log -*-
2 ;;;
3 ;;; **********************************************************************
4 ;;; This code was written as part of the CMU Common Lisp project at
5 ;;; Carnegie Mellon University, and has been placed in the public domain.
6 ;;; If you want to use this code or any part of CMU Common Lisp, please contact
7 ;;; Scott Fahlman or slisp-group@cs.cmu.edu.
8 ;;;
9 (ext:file-comment
10 "$Header: /tiger/var/lib/cvsroots/cmucl/src/compiler/eval.lisp,v 1.19.1.1 1991/10/15 14:40:20 wlott Exp $")
11 ;;;
12 ;;; **********************************************************************
13 ;;;
14 ;;; This file contains the interpreter. We first convert to the compiler's
15 ;;; IR1 and interpret that.
16 ;;;
17 ;;; Written by Rob MacLachlan and Bill Chiles.
18 ;;;
19
20 (in-package "EVAL")
21
22 (export '(internal-eval *eval-stack-trace* *internal-apply-node-trace*
23 *interpreted-function-cache-minimum-size*
24 *interpreted-function-cache-threshold*
25 flush-interpreted-function-cache
26 trace-eval interpreted-function-p
27 interpreted-function-lambda-expression
28 interpreted-function-closure
29 interpreted-function-name
30 interpreted-function-arglist
31 interpreted-function-type
32 make-interpreted-function))
33
34
35 ;;;; Interpreter stack.
36
37 (defmacro eval-stack ()
38 "This is the interpreter's evaluation stack."
39 '(di::stack-ref (kernel:mutator-self) vm:mutator-eval-stack-slot))
40 (defmacro eval-stack-top ()
41 "This is the next free element of the interpreter's evaluation stack."
42 '(di::stack-ref (kernel:mutator-self) vm:mutator-eval-stack-top-slot))
43
44 (unless (eval-stack)
45 (setf (eval-stack) (make-array 100)))
46
47 ;;; Setting this causes the stack operations to dump a trace.
48 ;;;
49 (defvar *eval-stack-trace* nil)
50
51
52 ;;; EVAL-STACK-PUSH -- Internal.
53 ;;;
54 ;;; Push value on (eval-stack), growing the stack if necessary. This returns
55 ;;; value. We save (eval-stack-top) in a local and increment the global before
56 ;;; storing value on the stack to prevent a GC timing problem. If we stored
57 ;;; value on the stack using (eval-stack-top) as an index, and we GC'ed before
58 ;;; incrementing (eval-stack-top), then INTERPRETER-GC-HOOK would clear the
59 ;;; location.
60 ;;;
61 (defun eval-stack-push (value)
62 (let ((len (length (the simple-vector (eval-stack)))))
63 (when (= len (eval-stack-top))
64 (when *eval-stack-trace* (format t "[PUSH: growing stack.]~%"))
65 (let ((new-stack (make-array (ash len 1))))
66 (replace new-stack (eval-stack) :end1 len :end2 len)
67 (setf (eval-stack) new-stack))))
68 (let ((top (eval-stack-top)))
69 (when *eval-stack-trace* (format t "pushing ~D.~%" top))
70 (incf (eval-stack-top))
71 (setf (svref (eval-stack) top) value)))
72
73 ;;; EVAL-STACK-POP -- Internal.
74 ;;;
75 ;;; This returns the last value pushed on (eval-stack) and decrements the top
76 ;;; pointer. We forego setting elements off the end of the stack to nil for GC
77 ;;; purposes because there is a *before-gc-hook* to take care of this for us.
78 ;;; However, because of the GC hook, we must be careful to grab the value
79 ;;; before decrementing (eval-stack-top) since we could GC between the
80 ;;; decrement and the reference, and the hook would clear the stack slot.
81 ;;;
82 (defun eval-stack-pop ()
83 (when (zerop (eval-stack-top))
84 (error "Attempt to pop empty eval stack."))
85 (let* ((new-top (1- (eval-stack-top)))
86 (value (svref (eval-stack) new-top)))
87 (when *eval-stack-trace* (format t "popping ~D --> ~S.~%" new-top value))
88 (setf (eval-stack-top) new-top)
89 value))
90
91 ;;; EVAL-STACK-EXTEND -- Internal.
92 ;;;
93 ;;; This allocates n locations on the stack, bumping the top pointer and
94 ;;; growing the stack if necessary. We set new slots to nil in case we GC
95 ;;; before having set them; we don't want to hold on to potential garbage
96 ;;; from old stack fluctuations.
97 ;;;
98 (defun eval-stack-extend (n)
99 (let ((len (length (the simple-vector (eval-stack)))))
100 (when (> (+ n (eval-stack-top)) len)
101 (when *eval-stack-trace* (format t "[EXTEND: growing stack.]~%"))
102 (let ((new-stack (make-array (+ n (ash len 1)))))
103 (replace new-stack (eval-stack) :end1 len :end2 len)
104 (setf (eval-stack) new-stack))))
105 (let ((new-top (+ (eval-stack-top) n)))
106 (when *eval-stack-trace* (format t "extending to ~D.~%" new-top))
107 (do ((i (eval-stack-top) (1+ i)))
108 ((= i new-top))
109 (setf (svref (eval-stack) i) nil))
110 (setf (eval-stack-top) new-top)))
111
112 ;;; EVAL-STACK-SHRINK -- Internal.
113 ;;;
114 ;;; The anthesis of EVAL-STACK-EXTEND.
115 ;;;
116 (defun eval-stack-shrink (n)
117 (when *eval-stack-trace*
118 (format t "shrinking to ~D.~%" (- (eval-stack-top) n)))
119 (decf (eval-stack-top) n))
120
121 ;;; EVAL-STACK-SET-TOP -- Internal.
122 ;;;
123 ;;; This is used to shrink the stack back to a previous frame pointer.
124 ;;;
125 (defun eval-stack-set-top (ptr)
126 (when *eval-stack-trace* (format t "setting top to ~D.~%" ptr))
127 (setf (eval-stack-top) ptr))
128
129
130 ;;; EVAL-STACK-LOCAL -- Internal.
131 ;;;
132 ;;; This returns a local variable from the current stack frame. This is used
133 ;;; for references the compiler represents as a lambda-var leaf. This is a
134 ;;; macro for SETF purposes.
135 ;;;
136 (defmacro eval-stack-local (fp offset)
137 `(svref (eval-stack) (+ ,fp ,offset)))
138
139
140 ;;;; Interpreted functions:
141
142 (defstruct (eval-function
143 (:print-function
144 (lambda (s stream d)
145 (declare (ignore d))
146 (format stream "#<EVAL-FUNCTION ~S>"
147 (eval-function-name s)))))
148 ;;
149 ;; The name of this interpreted function, or NIL if none specified.
150 (name nil)
151 ;;
152 ;; This function's debug arglist.
153 (arglist nil)
154 ;;
155 ;; A lambda that can be converted to get the definition.
156 (lambda nil)
157 ;;
158 ;; If this function has been converted, then this is the XEP. If this is
159 ;; false, then the function is not in the cache (or is in the process of
160 ;; being removed.)
161 (definition nil :type (or c::clambda null))
162 ;;
163 ;; The number of consequtive GCs that this function has been unused. This is
164 ;; used to control cache replacement.
165 (gcs 0 :type c::index)
166 ;;
167 ;; True if Lambda has been converted at least once, and thus warnings should
168 ;; be suppressed on additional conversions.
169 (converted-once nil))
170
171
172 (defvar *interpreted-function-cache-minimum-size* 25
173 "If the interpreted function cache has more functions than this come GC time,
174 then attempt to prune it according to
175 *INTERPRETED-FUNCTION-CACHE-THRESHOLD*.")
176
177 (defvar *interpreted-function-cache-threshold* 3
178 "If an interpreted function goes uncalled for more than this many GCs, then
179 it is eligible for flushing from the cache.")
180
181 (proclaim '(type c::index
182 *interpreted-function-cache-minimum-size*
183 *interpreted-function-cache-threshold*))
184
185
186 ;;; The list of EVAL-FUNCTIONS that have translated definitions.
187 ;;;
188 (defvar *interpreted-function-cache* nil)
189 (proclaim '(type list *interpreted-function-cache*))
190
191
192 ;;; MAKE-INTERPRETED-FUNCTION -- Interface
193 ;;;
194 ;;; Return a function that will lazily convert Lambda when called, and will
195 ;;; cache translations.
196 ;;;
197 (defun make-interpreted-function (lambda)
198 (let ((eval-fun (make-eval-function :lambda lambda)))
199 #'(lambda (&rest args)
200 (let ((fun (eval-function-definition eval-fun))
201 (args (cons (length args) args)))
202 (setf (eval-function-gcs eval-fun) 0)
203 (internal-apply (or fun (convert-eval-fun eval-fun))
204 args '#())))))
205
206
207 ;;; GET-EVAL-FUNCTION -- Internal
208 ;;;
209 (defun get-eval-function (x)
210 (let ((res (system:find-if-in-closure #'eval-function-p x)))
211 (assert res)
212 res))
213
214
215 ;;; CONVERT-EVAL-FUN -- Internal
216 ;;;
217 ;;; Eval a FUNCTION form, grab the definition and stick it in.
218 ;;;
219 (defun convert-eval-fun (eval-fun)
220 (declare (type eval-function eval-fun))
221 (let* ((new (eval-function-definition
222 (get-eval-function
223 (internal-eval `#',(eval-function-lambda eval-fun)
224 (eval-function-converted-once eval-fun))))))
225 (setf (eval-function-definition eval-fun) new)
226 (setf (eval-function-converted-once eval-fun) t)
227 (let ((name (eval-function-name eval-fun)))
228 (setf (c::leaf-name new) name)
229 (setf (c::leaf-name (c::main-entry (c::functional-entry-function new)))
230 name))
231 (push eval-fun *interpreted-function-cache*)
232 new))
233
234
235 ;;; INTERPRETED-FUNCTION-LAMDBA-EXPRESSION -- Interface
236 ;;;
237 ;;; Get the CLAMBDA for the XEP, then look at the inline expansion info in
238 ;;; the real function.
239 ;;;
240 (defun interpreted-function-lambda-expression (x)
241 (let* ((eval-fun (get-eval-function x))
242 (lambda (eval-function-lambda eval-fun)))
243 (if lambda
244 (values lambda nil (eval-function-name eval-fun))
245 (let ((fun (c::functional-entry-function
246 (eval-function-definition eval-fun))))
247 (values (c::functional-inline-expansion fun)
248 (if (let ((env (c::functional-lexenv fun)))
249 (or (c::lexenv-functions env)
250 (c::lexenv-variables env)
251 (c::lexenv-blocks env)
252 (c::lexenv-tags env)))
253 t nil)
254 (or (eval-function-name eval-fun)
255 (c::component-name
256 (c::block-component
257 (c::node-block (c::lambda-bind fun))))))))))
258
259
260 ;;; INTERPRETED-FUNCTION-TYPE -- Interface
261 ;;;
262 ;;; Return a FUNCTION-TYPE describing an eval function. We just grab the
263 ;;; LEAF-TYPE of the definition, converting the definition if not currently
264 ;;; cached.
265 ;;;
266 (defvar *already-looking-for-type-of* nil)
267 ;;;
268 (defun interpreted-function-type (fun)
269 (if (member fun *already-looking-for-type-of*)
270 (specifier-type 'function)
271 (let* ((*already-looking-for-type-of*
272 (cons fun *already-looking-for-type-of*))
273 (eval-fun (get-eval-function fun))
274 (def (or (eval-function-definition eval-fun)
275 (system:without-gcing
276 (convert-eval-fun eval-fun)
277 (eval-function-definition eval-fun)))))
278 (c::leaf-type (c::functional-entry-function def)))))
279
280
281 ;;;
282 ;;; INTERPRETED-FUNCTION-{NAME,ARGLIST} -- Interface
283 ;;;
284 (defun interpreted-function-name (x)
285 (multiple-value-bind (ig1 ig2 res)
286 (interpreted-function-lambda-expression x)
287 (declare (ignore ig1 ig2))
288 res))
289 ;;;
290 (defun (setf interpreted-function-name) (val x)
291 (let* ((eval-fun (get-eval-function x))
292 (def (eval-function-definition eval-fun)))
293 (when def
294 (setf (c::leaf-name def) val)
295 (setf (c::leaf-name (c::main-entry (c::functional-entry-function def)))
296 val))
297 (setf (eval-function-name eval-fun) val)))
298 ;;;
299 (defun interpreted-function-arglist (x)
300 (eval-function-arglist (get-eval-function x)))
301 ;;;
302 (defun (setf interpreted-function-arglist) (val x)
303 (setf (eval-function-arglist (get-eval-function x)) val))
304
305
306 ;;; INTERPRETED-FUNCTION-ENVIRONMENT -- Interface
307 ;;;
308 ;;; The environment should be the only SIMPLE-VECTOR in the closure. We
309 ;;; have to throw in the EVAL-FUNCTION-P test, since structure are currently
310 ;;; also SIMPLE-VECTORs.
311 ;;;
312 (defun interpreted-function-closure (x)
313 (system:find-if-in-closure #'(lambda (x)
314 (and (simple-vector-p x)
315 (not (eval-function-p x))))
316 x))
317
318
319 ;;; INTERPRETER-GC-HOOK -- Internal
320 ;;;
321 ;;; Clear the unused portion of the eval stack, and flush the definitions of
322 ;;; all functions in the cache that haven't been used enough.
323 ;;;
324 (defun interpreter-gc-hook ()
325 (let ((len (length (the simple-vector (eval-stack)))))
326 (do ((i (eval-stack-top) (1+ i)))
327 ((= i len))
328 (setf (svref (eval-stack) i) nil)))
329
330 (let ((num (- (length *interpreted-function-cache*)
331 *interpreted-function-cache-minimum-size*)))
332 (when (plusp num)
333 (setq *interpreted-function-cache*
334 (delete-if #'(lambda (x)
335 (when (>= (eval-function-gcs x)
336 *interpreted-function-cache-threshold*)
337 (setf (eval-function-definition x) nil)
338 t))
339 *interpreted-function-cache*
340 :count num))))
341
342 (dolist (fun *interpreted-function-cache*)
343 (incf (eval-function-gcs fun))))
344 ;;;
345 (pushnew 'interpreter-gc-hook ext:*before-gc-hooks*)
346
347
348 ;;; FLUSH-INTERPRETED-FUNCTION-CACHE -- Interface
349 ;;;
350 (defun flush-interpreted-function-cache ()
351 "Clear all entries in the eval function cache. This allows the internal
352 representation of the functions to be reclaimed, and also lazily forces
353 macroexpansions to be recomputed."
354 (dolist (fun *interpreted-function-cache*)
355 (setf (eval-function-definition fun) nil))
356 (setq *interpreted-function-cache* ()))
357
358
359 ;;;; INTERNAL-APPLY-LOOP macros.
360
361 ;;; These macros are intimately related to INTERNAL-APPLY-LOOP. They assume
362 ;;; variables established by this function, and they assume they can return
363 ;;; from a block by that name. This is sleazy, but we justify it as follows:
364 ;;; They are so specialized in use, and their invocation became lengthy, that
365 ;;; we allowed them to slime some access to things in their expanding
366 ;;; environment. These macros don't really extend our Lisp syntax, but they do
367 ;;; provide some template expansion service; it is these cleaner circumstance
368 ;;; that require a more rigid programming style.
369 ;;;
370 ;;; Since these are macros expanded almost solely for c::combination nodes,
371 ;;; they cascade from the end of this logical page to the beginning here.
372 ;;; Therefore, it is best you start looking at them from the end of this
373 ;;; section, backwards from normal scanning mode for Lisp code.
374 ;;;
375
376 ;;; DO-COMBINATION -- Internal.
377 ;;;
378 ;;; This runs a function on some arguments from the stack. If the combination
379 ;;; occurs in a tail recursive position, then we do the call such that we
380 ;;; return from tail-p-function with whatever values the call produces. With a
381 ;;; :local call, we have to restore the stack to its previous frame before
382 ;;; doing the call. The :full call mechanism does this for us. If it is NOT a
383 ;;; tail recursive call, and we're in a multiple value context, then then push
384 ;;; a list of the returned values. Do the same thing if we're in a :return
385 ;;; context. Push a single value, without listifying it, for a :single value
386 ;;; context. Otherwise, just call for side effect.
387 ;;;
388 ;;; Node is the combination node, and cont is its continuation. Frame-ptr
389 ;;; is the current frame pointer, and closure is the current environment for
390 ;;; closure variables. Call-type is either :full or :local, and when it is
391 ;;; local, lambda is the IR1 lambda to apply.
392 ;;;
393 ;;; This assumes the following variables are present: node, cont, frame-ptr,
394 ;;; and closure. It also assumes a block named internal-apply-loop.
395 ;;;
396 (defmacro do-combination (call-type lambda mv-or-normal)
397 (let* ((args (gensym))
398 (calling-closure (gensym))
399 (invoke-fun (ecase mv-or-normal
400 (:mv-call 'mv-internal-invoke)
401 (:normal 'internal-invoke)))
402 (args-form (ecase mv-or-normal
403 (:mv-call
404 `(mv-eval-stack-args
405 (length (c::mv-combination-args node))))
406 (:normal
407 `(eval-stack-args (c:lambda-eval-info-args-passed
408 (c::lambda-info ,lambda))))))
409 (call-form (ecase call-type
410 (:full `(,invoke-fun
411 (length (c::basic-combination-args node))))
412 (:local `(internal-apply
413 ,lambda ,args-form
414 (compute-closure node ,lambda frame-ptr
415 closure)
416 nil))))
417 (tailp-call-form
418 (ecase call-type
419 (:full `(return-from
420 internal-apply-loop
421 ;; INVOKE-FUN takes care of the stack itself.
422 (,invoke-fun (length (c::basic-combination-args node))
423 frame-ptr)))
424 (:local `(let ((,args ,args-form)
425 (,calling-closure
426 (compute-closure node ,lambda frame-ptr closure)))
427 ;; No need to clean up stack slots for GC due to
428 ;; ext:*before-gc-hook*.
429 (eval-stack-set-top frame-ptr)
430 (return-from
431 internal-apply-loop
432 (internal-apply ,lambda ,args ,calling-closure
433 nil)))))))
434 `(cond ((c::node-tail-p node)
435 ,tailp-call-form)
436 (t
437 (ecase (c::continuation-info cont)
438 ((:multiple :return)
439 (eval-stack-push (multiple-value-list ,call-form)))
440 (:single
441 (eval-stack-push ,call-form))
442 (:unused ,call-form))))))
443
444 ;;; SET-BLOCK -- Internal.
445 ;;;
446 ;;; This sets the variable block in INTERNAL-APPLY-LOOP, and it announces this
447 ;;; by setting set-block-p for later loop iteration maintenance.
448 ;;;
449 (defmacro set-block (exp)
450 `(progn
451 (setf block ,exp)
452 (setf set-block-p t)))
453
454 ;;; CHANGE-BLOCKS -- Internal.
455 ;;;
456 ;;; This sets all the iteration variables in INTERNAL-APPLY-LOOP to iterate
457 ;;; over a new block's nodes. Block-exp is optional because sometimes we have
458 ;;; already set block, and we only need to bring the others into agreement.
459 ;;; If we already set block, then clear the variable that announces this,
460 ;;; set-block-p.
461 ;;;
462 (defmacro change-blocks (&optional block-exp)
463 `(progn
464 ,(if block-exp
465 `(setf block ,block-exp)
466 `(setf set-block-p nil))
467 (setf node (c::continuation-next (c::block-start block)))
468 (setf last-cont (c::node-cont (c::block-last block)))))
469
470
471 ;;; This controls printing visited nodes in INTERNAL-APPLY-LOOP. We use it
472 ;;; here, and INTERNAL-INVOKE uses it to print function call looking output
473 ;;; to further describe c::combination nodes.
474 ;;;
475 (defvar *internal-apply-node-trace* nil)
476 ;;;
477 (defun maybe-trace-funny-fun (node name &rest args)
478 (when *internal-apply-node-trace*
479 (format t "(~S ~{ ~S~}) c~S~%"
480 name args (c::cont-num (c::node-cont node)))))
481
482
483 ;;; DO-FUNNY-FUNCTION -- Internal.
484 ;;;
485 ;;; This implements the intention of the virtual function name. This is a
486 ;;; macro because some of these actions must occur without a function call.
487 ;;; For example, calling a dispatch function to implement special binding would
488 ;;; be a no-op because returning from that function would cause the system to
489 ;;; undo any special bindings it established.
490 ;;;
491 ;;; NOTE: update C:ANNOTATE-COMPONENT-FOR-EVAL and/or c::undefined-funny-funs
492 ;;; if you add or remove branches in this routine.
493 ;;;
494 ;;; This assumes the following variables are present: node, cont, frame-ptr,
495 ;;; args, closure, block, and last-cont. It also assumes a block named
496 ;;; internal-apply-loop.
497 ;;;
498 (defmacro do-funny-function (funny-fun-name)
499 (let ((name (gensym)))
500 `(let ((,name ,funny-fun-name))
501 (ecase ,name
502 (c::%special-bind
503 (let ((value (eval-stack-pop))
504 (global-var (eval-stack-pop)))
505 (maybe-trace-funny-fun node ,name global-var value)
506 (system:%primitive bind value (c::global-var-name global-var))))
507 (c::%special-unbind
508 ;; Throw away arg telling me which special, and tell the dynamic
509 ;; binding mechanism to unbind one variable.
510 (eval-stack-pop)
511 (maybe-trace-funny-fun node ,name)
512 (system:%primitive unbind))
513 (c::%catch
514 (let* ((tag (eval-stack-pop))
515 (nlx-info (eval-stack-pop))
516 (fell-through-p nil)
517 ;; Ultimately THROW and CATCH will fix the interpreter's stack
518 ;; since this is necessary for compiled CATCH's and those in
519 ;; the initial top level function.
520 (stack-top (eval-stack-top))
521 (values
522 (multiple-value-list
523 (catch tag
524 (maybe-trace-funny-fun node ,name tag)
525 (multiple-value-setq (block node cont last-cont)
526 (internal-apply-loop (c::continuation-next cont)
527 frame-ptr lambda args closure))
528 (setf fell-through-p t)))))
529 (cond (fell-through-p
530 ;; We got here because we just saw the C::%CATCH-BREAKUP
531 ;; funny function inside the above recursive call to
532 ;; INTERNAL-APPLY-LOOP. Therefore, we just received and
533 ;; stored the current state of evaluation for falling
534 ;; through.
535 )
536 (t
537 ;; Fix up the interpreter's stack after having thrown here.
538 ;; We won't need to do this in the final implementation.
539 (eval-stack-set-top stack-top)
540 ;; Take the values received in the list bound above, and
541 ;; massage them into the form expected by the continuation
542 ;; of the non-local-exit info.
543 (ecase (c::continuation-info
544 (c::nlx-info-continuation nlx-info))
545 (:single
546 (eval-stack-push (car values)))
547 ((:multiple :return)
548 (eval-stack-push values))
549 (:unused))
550 ;; We want to continue with the code after the CATCH body.
551 ;; The non-local-exit info tells us where this is, but we
552 ;; know that block only contains a call to the funny
553 ;; function C::%NLX-ENTRY, which simply is a place holder
554 ;; for the compiler IR1. We want to skip the target block
555 ;; entirely, so we say it is the block we're in now and say
556 ;; the current cont is the last-cont. This makes the COND
557 ;; at the end of INTERNAL-APPLY-LOOP do the right thing.
558 (setf block (c::nlx-info-target nlx-info))
559 (setf cont last-cont)))))
560 (c::%unwind-protect
561 ;; Cleanup function not pushed due to special-case :UNUSED
562 ;; annotation in ANNOTATE-COMPONENT-FOR-EVAL.
563 (let* ((nlx-info (eval-stack-pop))
564 (fell-through-p nil)
565 (stack-top (eval-stack-top)))
566 (unwind-protect
567 (progn
568 (maybe-trace-funny-fun node ,name)
569 (multiple-value-setq (block node cont last-cont)
570 (internal-apply-loop (c::continuation-next cont)
571 frame-ptr lambda args closure))
572 (setf fell-through-p t))
573 (cond (fell-through-p
574 ;; We got here because we just saw the
575 ;; C::%UNWIND-PROTECT-BREAKUP funny function inside the
576 ;; above recursive call to INTERNAL-APPLY-LOOP.
577 ;; Therefore, we just received and stored the current
578 ;; state of evaluation for falling through.
579 )
580 (t
581 ;; Fix up the interpreter's stack after having thrown here.
582 ;; We won't need to do this in the final implementation.
583 (eval-stack-set-top stack-top)
584 ;;
585 ;; Push some bogus values for exit context to keep the
586 ;; MV-BIND in the UNWIND-PROTECT translation happy.
587 (eval-stack-push '(nil nil 0))
588 (let ((node (c::continuation-next
589 (c::block-start
590 (car (c::block-succ
591 (c::nlx-info-target nlx-info)))))))
592 (internal-apply-loop node frame-ptr lambda args
593 closure)))))))
594 ((c::%catch-breakup c::%unwind-protect-breakup c::%continue-unwind)
595 ;; This shows up when we locally exit a CATCH body -- fell through.
596 ;; Return the current state of evaluation to the previous invocation
597 ;; of INTERNAL-APPLY-LOOP which happens to be running in the
598 ;; c::%catch branch of this code.
599 (maybe-trace-funny-fun node ,name)
600 (return-from internal-apply-loop
601 (values block node cont last-cont)))
602 (c::%nlx-entry
603 (maybe-trace-funny-fun node ,name)
604 ;; This just marks a spot in the code for CATCH, UNWIND-PROTECT, and
605 ;; non-local lexical exits (GO or RETURN-FROM).
606 ;; Do nothing since c::%catch does it all when it catches a THROW.
607 ;; Do nothing since c::%unwind-protect does it all when
608 ;; it catches a THROW.
609 )
610 (c::%more-arg-context
611 (let* ((fixed-arg-count (1+ (eval-stack-pop)))
612 ;; Add 1 to actual fixed count for extra arg expected by
613 ;; external entry points (XEP) which some IR1 lambdas have.
614 ;; The extra arg is the number of arguments for arg count
615 ;; consistency checking. C::%MORE-ARG-CONTEXT always runs
616 ;; within an XEP, so the lambda has an extra arg.
617 (more-args (nthcdr fixed-arg-count args)))
618 (maybe-trace-funny-fun node ,name fixed-arg-count)
619 (assert (eq (c::continuation-info cont) :multiple))
620 (eval-stack-push (list more-args (length more-args)))))
621 (c::%unknown-values
622 (error "C::%UNKNOWN-VALUES should never be in interpreter's IR1."))
623 (c::%lexical-exit-breakup
624 ;; We see this whenever we locally exit the extent of a lexical
625 ;; target. That is, we are truly locally exiting an extent we could
626 ;; have non-locally lexically exited. Return the :fell-through flag
627 ;; and the current state of evaluation to the previous invocation
628 ;; of INTERNAL-APPLY-LOOP which happens to be running in the
629 ;; c::entry branch of INTERNAL-APPLY-LOOP.
630 (maybe-trace-funny-fun node ,name)
631 ;;
632 ;; Discard the NLX-INFO arg...
633 (eval-stack-pop)
634 (return-from internal-apply-loop
635 (values :fell-through block node cont last-cont)))))))
636
637
638 ;;; COMBINATION-NODE -- Internal.
639 ;;;
640 ;;; This expands for the two types of combination nodes INTERNAL-APPLY-LOOP
641 ;;; sees. Type is either :mv-call or :normal. Node is the combination node,
642 ;;; and cont is its continuation. Frame-ptr is the current frame pointer, and
643 ;;; closure is the current environment for closure variables.
644 ;;;
645 ;;; Most of the real work is done by DO-COMBINATION. This first determines if
646 ;;; the combination node describes a :full call which DO-COMBINATION directly
647 ;;; handles. If the call is :local, then we either invoke an IR1 lambda, or we
648 ;;; just bind some LET variables. If the call is :local, and type is :mv-call,
649 ;;; then we can only be binding multiple values. Otherwise, the combination
650 ;;; node describes a function known to the compiler, but this may be a funny
651 ;;; function that actually isn't ever defined. We either take some action for
652 ;;; the funny function or do a :full call on the known true function, but the
653 ;;; interpreter doesn't do optimizing stuff for functions known to the
654 ;;; compiler.
655 ;;;
656 ;;; This assumes the following variables are present: node, cont, frame-ptr,
657 ;;; and closure. It also assumes a block named internal-apply-loop.
658 ;;;
659 (defmacro combination-node (type)
660 (let* ((kind (gensym))
661 (fun (gensym))
662 (lambda (gensym))
663 (letp (gensym))
664 (letp-bind (ecase type
665 (:mv-call nil)
666 (:normal
667 `((,letp (eq (c::functional-kind ,lambda) :let))))))
668 (local-branch
669 (ecase type
670 (:mv-call
671 `(store-mv-let-vars ,lambda frame-ptr
672 (length (c::mv-combination-args node))))
673 (:normal
674 `(if ,letp
675 (store-let-vars ,lambda frame-ptr)
676 (do-combination :local ,lambda ,type))))))
677 `(let ((,kind (c::basic-combination-kind node))
678 (,fun (c::basic-combination-fun node)))
679 (cond ((eq ,kind :full)
680 (do-combination :full nil ,type))
681 ((eq ,kind :local)
682 (let* ((,lambda (c::ref-leaf (c::continuation-use ,fun)))
683 ,@letp-bind)
684 ,local-branch))
685 ((eq (c::continuation-info ,fun) :unused)
686 (assert (typep ,kind 'c::function-info))
687 (do-funny-function (c::continuation-function-name ,fun)))
688 (t
689 (assert (typep ,kind 'c::function-info))
690 (do-combination :full nil ,type))))))
691
692
693 (defun trace-eval (on)
694 (setf *eval-stack-trace* on)
695 (setf *internal-apply-node-trace* on))
696
697
698 ;;;; INTERNAL-EVAL:
699
700 (proclaim '(special lisp::*already-evaled-this*))
701
702 ;;; INTERNAL-EVAL -- Interface
703 ;;;
704 ;;; Evaluate an arbitary form. We convert the form, then call internal
705 ;;; apply on it. If *ALREADY-EVALED-THIS* is true, then we bind it to NIL
706 ;;; around the apply to limit the inhibition to the lexical scope of the
707 ;;; EVAL-WHEN.
708 ;;;
709 (defun internal-eval (form &optional quietly)
710 (let ((res (c:compile-for-eval form quietly)))
711 (if lisp::*already-evaled-this*
712 (let ((lisp::*already-evaled-this* nil))
713 (internal-apply res nil '#()))
714 (internal-apply res nil '#()))))
715
716
717 ;;; MAKE-INDIRECT-VALUE-CELL -- Internal.
718 ;;;
719 ;;; Later this will probably be the same weird internal thing the compiler
720 ;;; makes to represent these things.
721 ;;;
722 (defun make-indirect-value-cell (value)
723 (list value))
724 ;;;
725 (defmacro indirect-value (value-cell)
726 `(car ,value-cell))
727
728
729 ;;; VALUE -- Internal.
730 ;;;
731 ;;; This passes on a node's value appropriately, possibly returning from
732 ;;; function to do so. When we are tail-p, don't push the value, return it on
733 ;;; the system's actual call stack; when we blow out of function this way, we
734 ;;; must return the interpreter's stack to the its state before this call to
735 ;;; function. When we're in a multiple value context or heading for a return
736 ;;; node, we push a list of the value for easier handling later. Otherwise,
737 ;;; just push the value on the interpreter's stack.
738 ;;;
739 (defmacro value (node info value frame-ptr function)
740 `(cond ((c::node-tail-p ,node)
741 (eval-stack-set-top ,frame-ptr)
742 (return-from ,function ,value))
743 ((member ,info '(:multiple :return) :test #'eq)
744 (eval-stack-push (list ,value)))
745 (t (assert (eq ,info :single))
746 (eval-stack-push ,value))))))
747
748
749 (defun maybe-trace-nodes (node)
750 (when *internal-apply-node-trace*
751 (format t "<~A-node> c~S~%"
752 (type-of node)
753 (c::cont-num (c::node-cont node)))))
754
755 ;;; INTERNAL-APPLY -- Internal.
756 ;;;
757 ;;; This interprets lambda, a compiler IR1 data structure representing a
758 ;;; function, applying it to args. Closure is the environment in which to run
759 ;;; lambda, the variables and such closed over to form lambda. The call occurs
760 ;;; on the interpreter's stack, so save the current top and extend the stack
761 ;;; for this lambda's call frame. Then store the args into locals on the
762 ;;; stack.
763 ;;;
764 ;;; Args is the list of arguments to apply to. If IGNORE-UNUSED is true, then
765 ;;; values for un-read variables are present in the argument list, and must be
766 ;;; discarded (always true except in a local call.) Args may run out of values
767 ;;; before vars runs out of variables (in the case of an XEP with optionals);
768 ;;; we just do CAR of nil and store nil. This is not the proper defaulting
769 ;;; (which is done by explicit code in the XEP.)
770 ;;;
771 (defun internal-apply (lambda args closure &optional (ignore-unused t))
772 (let ((frame-ptr (eval-stack-top)))
773 (eval-stack-extend (c:lambda-eval-info-frame-size (c::lambda-info lambda)))
774 (do ((vars (c::lambda-vars lambda) (cdr vars))
775 (args args))
776 ((null vars))
777 (let ((var (car vars)))
778 (cond ((c::leaf-refs var)
779 (setf (eval-stack-local frame-ptr (c::lambda-var-info var))
780 (if (c::lambda-var-indirect var)
781 (make-indirect-value-cell (pop args))
782 (pop args))))
783 (ignore-unused (pop args)))))
784 (internal-apply-loop (c::lambda-bind lambda) frame-ptr lambda args
785 closure)))
786
787 ;;; INTERNAL-APPLY-LOOP -- Internal.
788 ;;;
789 ;;; This does the work of INTERNAL-APPLY. This also calls itself recursively
790 ;;; for certain language features, such as CATCH. First is the node at which
791 ;;; to start interpreting. Frame-ptr is the current frame pointer for
792 ;;; accessing local variables. Lambda is the IR1 lambda from which comes the
793 ;;; nodes a given call to this function processes, and closure is the
794 ;;; environment for interpreting lambda. Args is the argument list for the
795 ;;; lambda given to INTERNAL-APPLY, and we have to carry it around with us
796 ;;; in case of more-arg or rest-arg processing which is represented explicitly
797 ;;; in the compiler's IR1.
798 ;;;
799 ;;; Due to having a truly tail recursive interpreter, some of the branches
800 ;;; handling a given node need to RETURN-FROM this routine. Also, some calls
801 ;;; this makes to do work for it must occur in tail recursive positions.
802 ;;; Because of this required access to this function lexical environment and
803 ;;; calling positions, we often are unable to break off logical chunks of code
804 ;;; into functions. We have written macros intended solely for use in this
805 ;;; routine, and due to all the local stuff they need to access and length
806 ;;; complex calls, we have written them to sleazily access locals from this
807 ;;; routine. In addition to assuming a block named internal-apply-loop exists,
808 ;;; they set and reference the following variables: node, cont, frame-ptr,
809 ;;; closure, block, last-cont, and set-block-p.
810 ;;;
811 (defun internal-apply-loop (first frame-ptr lambda args closure)
812 (declare (optimize (debug-info 2)))
813 (let* ((block (c::node-block first))
814 (last-cont (c::node-cont (c::block-last block)))
815 (node first)
816 (set-block-p nil))
817 (loop
818 (let ((cont (c::node-cont node)))
819 (etypecase node
820 (c::ref
821 (maybe-trace-nodes node)
822 (let ((info (c::continuation-info cont)))
823 (unless (eq info :unused)
824 (value node info (leaf-value node frame-ptr closure)
825 frame-ptr internal-apply-loop))))
826 (c::combination
827 (maybe-trace-nodes node)
828 (combination-node :normal))
829 (c::cif
830 (maybe-trace-nodes node)
831 ;; IF nodes always occur at the end of a block, so pick another.
832 (set-block (if (eval-stack-pop)
833 (c::if-consequent node)
834 (c::if-alternative node))))
835 (c::bind
836 (maybe-trace-nodes node)
837 ;; Ignore bind nodes since INTERNAL-APPLY extends the stack for
838 ;; all of a lambda's locals, and the c::combination branch
839 ;; handles LET binds (moving values off stack top into locals).
840 )
841 (c::cset
842 (maybe-trace-nodes node)
843 (let ((info (c::continuation-info cont))
844 (res (set-leaf-value node frame-ptr closure
845 (eval-stack-pop))))
846 (unless (eq info :unused)
847 (value node info res frame-ptr internal-apply-loop))))
848 (c::entry
849 (maybe-trace-nodes node)
850 (let ((info (cdr (assoc node (c:lambda-eval-info-entries
851 (c::lambda-info lambda))))))
852 ;; No info means no-op entry for CATCH or UNWIND-PROTECT.
853 (when info
854 ;; Store stack top for restoration in local exit situation
855 ;; in c::exit branch.
856 (setf (eval-stack-local frame-ptr
857 (c:entry-node-info-st-top info))
858 (eval-stack-top))
859 (let ((tag (c:entry-node-info-nlx-tag info)))
860 (when tag
861 ;; Non-local lexical exit (someone closed over a
862 ;; GO tag or BLOCK name).
863 (let ((unique-tag (cons nil nil))
864 values)
865 (setf (eval-stack-local frame-ptr tag) unique-tag)
866 (if (eq cont last-cont)
867 (change-blocks (car (c::block-succ block)))
868 (setf node (c::continuation-next cont)))
869 (loop
870 (multiple-value-setq (values block node cont last-cont)
871 (catch unique-tag
872 (internal-apply-loop node frame-ptr
873 lambda args closure)))
874
875 (when (eq values :fell-through)
876 ;; We hit a %LEXICAL-EXIT-BREAKUP.
877 ;; Interpreting state is set with MV-SETQ above.
878 ;; Just get out of this branch and go on.
879 (return))
880
881 (unless (eq values :non-local-go)
882 ;; We know we're non-locally exiting from a
883 ;; BLOCK with values (saw a RETURN-FROM).
884 (ecase (c::continuation-info cont)
885 (:single
886 (eval-stack-push (car values)))
887 ((:multiple :return)
888 (eval-stack-push values))
889 (:unused)))
890 ;;
891 ;; Start interpreting again at the target, skipping
892 ;; the %NLX-ENTRY block.
893 (setf node
894 (c::continuation-next
895 (c::block-start
896 (car (c::block-succ block))))))))))))
897 (c::exit
898 (maybe-trace-nodes node)
899 (let* ((incoming-values (c::exit-value node))
900 (values (if incoming-values (eval-stack-pop))))
901 (cond
902 ((eq (c::lambda-environment lambda)
903 (c::block-environment (c::continuation-block cont)))
904 ;; Local exit.
905 ;; Fixup stack top and massage values for destination.
906 (eval-stack-set-top
907 (eval-stack-local frame-ptr
908 (c:entry-node-info-st-top
909 (cdr (assoc (c::exit-entry node)
910 (c:lambda-eval-info-entries
911 (c::lambda-info lambda)))))))
912 (ecase (c::continuation-info cont)
913 (:single
914 (assert incoming-values)
915 (eval-stack-push (car values)))
916 ((:multiple :return)
917 (assert incoming-values)
918 (eval-stack-push values))
919 (:unused)))
920 (t
921 (let ((info (c::find-nlx-info (c::exit-entry node) cont)))
922 (throw
923 (svref closure
924 (position info
925 (c::environment-closure
926 (c::node-environment node))
927 :test #'eq))
928 (if incoming-values
929 (values values (c::nlx-info-target info) nil cont)
930 (values :non-local-go (c::nlx-info-target info)))))))))
931 (c::creturn
932 (maybe-trace-nodes node)
933 (let ((values (eval-stack-pop)))
934 (eval-stack-set-top frame-ptr)
935 (return-from internal-apply-loop (values-list values))))
936 (c::mv-combination
937 (maybe-trace-nodes node)
938 (combination-node :mv-call)))
939 ;; See function doc below.
940 (reference-this-var-to-keep-it-alive node)
941 (reference-this-var-to-keep-it-alive frame-ptr)
942 (reference-this-var-to-keep-it-alive closure)
943 (cond ((not (eq cont last-cont))
944 (setf node (c::continuation-next cont)))
945 ;; Currently only the last node in a block causes this loop to
946 ;; change blocks, so we never just go to the next node when
947 ;; the current node's branch tried to change blocks.
948 (set-block-p
949 (change-blocks))
950 (t
951 ;; Cif nodes set the block for us, but other last nodes do not.
952 (change-blocks (car (c::block-succ block)))))))))
953
954 ;;; REFERENCE-THIS-VAR-TO-KEEP-IT-ALIVE -- Internal.
955 ;;;
956 ;;; This function allows a reference to a variable that the compiler cannot
957 ;;; easily eliminate as unnecessary. We use this at the end of the node
958 ;;; dispatch in INTERNAL-APPLY-LOOP to make sure the node variable has a
959 ;;; valid value. Each node branch tends to reference it at the beginning,
960 ;;; and then there is no reference but a set at the end; the compiler then
961 ;;; kills the variable between the reference in the dispatch branch and when
962 ;;; we set it at the end. The problem is that most error will occur in the
963 ;;; interpreter within one of these node dispatch branches.
964 ;;;
965 (defun reference-this-var-to-keep-it-alive (node)
966 node)
967
968
969 ;;; SET-LEAF-VALUE -- Internal.
970 ;;;
971 ;;; This sets a c::cset node's var to value, returning value. When var is
972 ;;; local, we have to compare its home environment to the current one, node's
973 ;;; environment. If they're the same, we check to see if the var is indirect,
974 ;;; and store the value on the stack or in the value cell as appropriate.
975 ;;; Otherwise, var is a closure variable, and since we're setting it, we know
976 ;;; it's location contains an indirect value object.
977 ;;;
978 (defun set-leaf-value (node frame-ptr closure value)
979 (let ((var (c::set-var node)))
980 (typecase var
981 (c::global-var
982 (setf (symbol-value (c::global-var-name var)) value))
983 (c::lambda-var
984 (set-leaf-value-lambda-var node var frame-ptr closure value)))))
985
986 ;;; SET-LEAF-VALUE-LAMBDA-VAR -- Internal Interface.
987 ;;;
988 ;;; This does SET-LEAF-VALUE for a lambda-var leaf. The debugger tools'
989 ;;; internals uses this also to set interpreted local variables.
990 ;;;
991 (defun set-leaf-value-lambda-var (node var frame-ptr closure value)
992 (let ((env (c::node-environment node)))
993 (cond ((not (eq (c::lambda-environment (c::lambda-var-home var))
994 env))
995 (setf (indirect-value
996 (svref closure
997 (position var (c::environment-closure env)
998 :test #'eq)))
999 value))
1000 ((c::lambda-var-indirect var)
1001 (setf (indirect-value
1002 (eval-stack-local frame-ptr (c::lambda-var-info var)))
1003 value))
1004 (t
1005 (setf (eval-stack-local frame-ptr (c::lambda-var-info var))
1006 value)))))
1007
1008 ;;; LEAF-VALUE -- Internal.
1009 ;;;
1010 ;;; This figures out how to return a value for a ref node. Leaf is the ref's
1011 ;;; structure that tells us about the value, and it is one of the following
1012 ;;; types:
1013 ;;; constant -- It knows its own value.
1014 ;;; global-var -- It's either a value or function reference. Get it right.
1015 ;;; local-var -- This may on the stack or in the current closure, the
1016 ;;; environment for the lambda INTERNAL-APPLY is currently
1017 ;;; executing. If the leaf's home environment is the same
1018 ;;; as the node's home environment, then the value is on the
1019 ;;; stack, else it's in the closure since it came from another
1020 ;;; environment. Whether the var comes from the stack or the
1021 ;;; closure, it could have come from a closure, and it could
1022 ;;; have been closed over for setting. When this happens, the
1023 ;;; actual value is stored in an indirection object, so
1024 ;;; indirect. See COMPUTE-CLOSURE for the description of
1025 ;;; the structure of the closure argument to this function.
1026 ;;; functional -- This is a reference to an interpreted function that may
1027 ;;; be passed or called anywhere. We return a real function
1028 ;;; that calls INTERNAL-APPLY, closing over the leaf. We also
1029 ;;; have to compute a closure, running environment, for the
1030 ;;; lambda in case it references stuff in the current
1031 ;;; environment. If the closure is empty and there is no
1032 ;;; functional environment, then we use
1033 ;;; MAKE-INTERPRETED-FUNCTION to make a cached translation.
1034 ;;; Since it is too late to lazily convert, we set up the
1035 ;;; EVAL-FUNCTION to be already converted.
1036 ;;;
1037 (defun leaf-value (node frame-ptr closure)
1038 (let ((leaf (c::ref-leaf node)))
1039 (typecase leaf
1040 (c::constant
1041 (c::constant-value leaf))
1042 (c::global-var
1043 (locally (declare (optimize (safety 1)))
1044 (if (eq (c::global-var-kind leaf) :global-function)
1045 (let ((name (c::global-var-name leaf)))
1046 (if (symbolp name)
1047 (symbol-function name)
1048 (fdefinition name)))
1049 (symbol-value (c::global-var-name leaf)))))
1050 (c::lambda-var
1051 (leaf-value-lambda-var node leaf frame-ptr closure))
1052 (c::functional
1053 (let* ((calling-closure (compute-closure node leaf frame-ptr closure))
1054 (real-fun (c::functional-entry-function leaf))
1055 (arg-doc (c::functional-arg-documentation real-fun)))
1056 (cond ((c:lambda-eval-info-function (c::leaf-info leaf)))
1057 ((and (zerop (length calling-closure))
1058 (null (c::lexenv-functions
1059 (c::functional-lexenv real-fun))))
1060 (let* ((res (make-interpreted-function
1061 (c::functional-inline-expansion real-fun)))
1062 (eval-fun (get-eval-function res)))
1063 (push eval-fun *interpreted-function-cache*)
1064 (setf (eval-function-definition eval-fun) leaf)
1065 (setf (eval-function-converted-once eval-fun) t)
1066 (setf (eval-function-arglist eval-fun) arg-doc)
1067 (setf (eval-function-name eval-fun) (c::leaf-name real-fun))
1068 (setf (c:lambda-eval-info-function (c::leaf-info leaf)) res)
1069 res))
1070 (t
1071 (let ((eval-fun (make-eval-function
1072 :definition leaf
1073 :name (c::leaf-name real-fun)
1074 :arglist arg-doc)))
1075 #'(lambda (&rest args)
1076 (declare (list args))
1077 (internal-apply (eval-function-definition eval-fun)
1078 (cons (length args) args)
1079 calling-closure))))))))))
1080
1081 ;;; LEAF-VALUE-LAMBDA-VAR -- Internal Interface.
1082 ;;;
1083 ;;; This does LEAF-VALUE for a lambda-var leaf. The debugger tools' internals
1084 ;;; uses this also to reference interpreted local variables.
1085 ;;;
1086 (defun leaf-value-lambda-var (node leaf frame-ptr closure)
1087 (let* ((env (c::node-environment node))
1088 (temp
1089 (if (eq (c::lambda-environment (c::lambda-var-home leaf))
1090 env)
1091 (eval-stack-local frame-ptr (c::lambda-var-info leaf))
1092 (svref closure
1093 (position leaf (c::environment-closure env)
1094 :test #'eq)))))
1095 (if (c::lambda-var-indirect leaf)
1096 (indirect-value temp)
1097 temp)))
1098
1099 ;;; COMPUTE-CLOSURE -- Internal.
1100 ;;;
1101 ;;; This computes a closure for a local call and for returned call'able closure
1102 ;;; objects. Sometimes the closure is a simple-vector of no elements. Node
1103 ;;; is either a reference node or a combination node. Leaf is either the leaf
1104 ;;; of the reference node or the lambda to internally apply for the combination
1105 ;;; node. Frame-ptr is the current frame pointer for fetching current values
1106 ;;; to store in the closure. Closure is the current closure, the currently
1107 ;;; interpreting lambda's closed over environment.
1108 ;;;
1109 ;;; A computed closure is a vector corresponding to the list of closure
1110 ;;; variables described in an environment. The position of a lambda-var in
1111 ;;; this closure list is the index into the closure vector of values.
1112 ;;;
1113 ;;; Functional-env is the environment description for leaf, the lambda for which
1114 ;;; we're computing a closure. This environment describes which of lambda's
1115 ;;; vars we find in lambda's closure when it's running, versus finding them
1116 ;;; on the stack. For each lambda-var in the functional environment's closure
1117 ;;; list, if the lambda-var's home environment is the current environment, then
1118 ;;; get a value off the stack and store it in the closure we're computing.
1119 ;;; Otherwise that lambda-var's value comes from somewhere else, but we have it
1120 ;;; in our current closure, the environment we're running in as we compute this
1121 ;;; new closure. Find this value the same way we do in LEAF-VALUE, by finding
1122 ;;; the lambda-var's position in the current environment's description of the
1123 ;;; current closure.
1124 ;;;
1125 (defun compute-closure (node leaf frame-ptr closure)
1126 (let* ((current-env (c::node-environment node))
1127 (current-closure-vars (c::environment-closure current-env))
1128 (functional-env (c::lambda-environment leaf))
1129 (functional-closure-vars (c::environment-closure functional-env))
1130 (functional-closure (make-array (length functional-closure-vars))))
1131 (do ((vars functional-closure-vars (cdr vars))
1132 (i 0 (1+ i)))
1133 ((null vars))
1134 (let ((ele (car vars)))
1135 (setf (svref functional-closure i)
1136 (etypecase ele
1137 (c::lambda-var
1138 (if (eq (c::lambda-environment (c::lambda-var-home ele))
1139 current-env)
1140 (eval-stack-local frame-ptr (c::lambda-var-info ele))
1141 (svref closure
1142 (position ele current-closure-vars
1143 :test #'eq))))
1144 (c::nlx-info
1145 (if (eq (c::block-environment (c::nlx-info-target ele))
1146 current-env)
1147 (eval-stack-local
1148 frame-ptr
1149 (c:entry-node-info-nlx-tag
1150 (cdr (assoc ;; entry node for non-local extent
1151 (c::cleanup-mess-up (c::nlx-info-cleanup ele))
1152 (c::lambda-eval-info-entries
1153 (c::lambda-info
1154 ;; lambda INTERNAL-APPLY-LOOP tosses around.
1155 (c::environment-function
1156 (c::node-environment node))))))))
1157 (svref closure
1158 (position ele current-closure-vars
1159 :test #'eq))))))))
1160 functional-closure))
1161
1162 ;;; INTERNAL-INVOKE -- Internal.
1163 ;;;
1164 ;;; INTERNAL-APPLY uses this to invoke a function from the interpreter's stack
1165 ;;; on some arguments also taken from the stack. When tail-p is non-nil,
1166 ;;; control does not return to INTERNAL-APPLY to further interpret the current
1167 ;;; IR1 lambda, so INTERNAL-INVOKE must clean up the current interpreter's
1168 ;;; stack frame.
1169 ;;;
1170 (defun internal-invoke (arg-count &optional tailp)
1171 (let ((args (eval-stack-args arg-count)) ;LET says this init form runs first.
1172 (fun (eval-stack-pop)))
1173 (when tailp (eval-stack-set-top tailp))
1174 (when *internal-apply-node-trace*
1175 (format t "(~S~{ ~S~})~%" fun args))
1176 (apply fun args)))
1177
1178 ;;; MV-INTERNAL-INVOKE -- Internal.
1179 ;;;
1180 ;;; Almost just like INTERNAL-INVOKE. We call MV-EVAL-STACK-ARGS, and our
1181 ;;; function is in a list on the stack instead of simply on the stack.
1182 ;;;
1183 (defun mv-internal-invoke (arg-count &optional tailp)
1184 (let ((args (mv-eval-stack-args arg-count)) ;LET runs this init form first.
1185 (fun (car (eval-stack-pop))))
1186 (when tailp (eval-stack-set-top tailp))
1187 (when *internal-apply-node-trace*
1188 (format t "(~S~{ ~S~})~%" fun args))
1189 (apply fun args)))
1190
1191
1192 ;;; EVAL-STACK-ARGS -- Internal.
1193 ;;;
1194 ;;; This returns a list of the top arg-count elements on the interpreter's
1195 ;;; stack. This removes them from the stack.
1196 ;;;
1197 (defun eval-stack-args (arg-count)
1198 (let ((args nil))
1199 (dotimes (i arg-count args)
1200 (push (eval-stack-pop) args))))
1201
1202 ;;; MV-EVAL-STACK-ARGS -- Internal.
1203 ;;;
1204 ;;; This assumes the top count elements on interpreter's stack are lists. This
1205 ;;; returns a single list with all the elements from these lists.
1206 ;;;
1207 (defun mv-eval-stack-args (count)
1208 (if (= count 1)
1209 (eval-stack-pop)
1210 (let ((last (eval-stack-pop)))
1211 (dotimes (i (1- count))
1212 (let ((next (eval-stack-pop)))
1213 (setf last
1214 (if next (nconc next last) last))))
1215 last)))
1216
1217 ;;; STORE-LET-VARS -- Internal.
1218 ;;;
1219 ;;; This stores lambda's vars, stack locals, from values popped off the stack.
1220 ;;; When a var has no references, the compiler computes IR1 such that the
1221 ;;; continuation delivering the value for the unreference var appears unused.
1222 ;;; Because of this, the interpreter drops the value on the floor instead of
1223 ;;; saving it on the stack for binding, so we only pop a value when the var has
1224 ;;; some reference. INTERNAL-APPLY uses this for c::combination nodes
1225 ;;; representing LET's.
1226 ;;;
1227 ;;; When storing the local, if it is indirect, then someone closes over it for
1228 ;;; setting instead of just for referencing. We then store an indirection cell
1229 ;;; with the value, and the referencing code for locals knows how to get the
1230 ;;; actual value.
1231 ;;;
1232 (defun store-let-vars (lambda frame-ptr)
1233 (let* ((vars (c::lambda-vars lambda))
1234 (args (eval-stack-args (count-if #'c::leaf-refs vars))))
1235 (declare (list vars args))
1236 (dolist (v vars)
1237 (when (c::leaf-refs v)
1238 (setf (eval-stack-local frame-ptr (c::lambda-var-info v))
1239 (if (c::lambda-var-indirect v)
1240 (make-indirect-value-cell (pop args))
1241 (pop args)))))))
1242
1243 ;;; STORE-MV-LET-VARS -- Internal.
1244 ;;;
1245 ;;; This is similar to STORE-LET-VARS, but the values for the locals appear on
1246 ;;; the stack in a list due to forms that delivered multiple values to this
1247 ;;; lambda/let. Unlike STORE-LET-VARS, there is no control over the delivery
1248 ;;; of a value for an unreferenced var, so we drop the corresponding value on
1249 ;;; the floor when no one references it. INTERNAL-APPLY uses this for
1250 ;;; c::mv-combination nodes representing LET's.
1251 ;;;
1252 (defun store-mv-let-vars (lambda frame-ptr count)
1253 (assert (= count 1))
1254 (let ((args (eval-stack-pop)))
1255 (dolist (v (c::lambda-vars lambda))
1256 (if (c::leaf-refs v)
1257 (setf (eval-stack-local frame-ptr (c::lambda-var-info v))
1258 (if (c::lambda-var-indirect v)
1259 (make-indirect-value-cell (pop args))
1260 (pop args)))
1261 (pop args)))))
1262
1263 #|
1264 ;;; STORE-MV-LET-VARS -- Internal.
1265 ;;;
1266 ;;; This stores lambda's vars, stack locals, from multiple values stored on the
1267 ;;; top of the stack in a list. Since these values arrived multiply, there is
1268 ;;; no control over the delivery of each value for an unreferenced var, so
1269 ;;; unlike STORE-LET-VARS, we have values for variables never used. We drop
1270 ;;; the value corresponding to an unreferenced var on the floor.
1271 ;;; INTERNAL-APPLY uses this for c::mv-combination nodes representing LET's.
1272 ;;;
1273 ;;; IR1 represents variables bound from multiple values in a list in the
1274 ;;; opposite order of the values list. We use STORE-MV-LET-VARS-AUX to recurse
1275 ;;; down the vars list until we bottom out, storing values on the way back up
1276 ;;; the recursion. You must do this instead of NREVERSE'ing the args list, so
1277 ;;; when we run out of values, we store nil's in the correct lambda-vars.
1278 ;;;
1279 (defun store-mv-let-vars (lambda frame-ptr count)
1280 (assert (= count 1))
1281 (print (c::lambda-vars lambda))
1282 (store-mv-let-vars-aux frame-ptr (c::lambda-vars lambda) (eval-stack-pop)))
1283 ;;;
1284 (defun store-mv-let-vars-aux (frame-ptr vars args)
1285 (if vars
1286 (let ((remaining-args (store-mv-let-vars-aux frame-ptr (cdr vars) args))
1287 (v (car vars)))
1288 (when (c::leaf-refs v)
1289 (setf (eval-stack-local frame-ptr (c::lambda-var-info v))
1290 (if (c::lambda-var-indirect v)
1291 (make-indirect-value-cell (car remaining-args))
1292 (car remaining-args))))
1293 (cdr remaining-args))
1294 args))
1295 |#

  ViewVC Help
Powered by ViewVC 1.1.5