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

Contents of /src/compiler/eval.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5