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

Contents of /src/compiler/eval.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5