d764e827a8499f0387b9fcdeca7045c6293dccb6
[sbcl.git] / src / compiler / ir2tran.lisp
1 ;;;; This file contains the virtual-machine-independent parts of the
2 ;;;; code which does the actual translation of nodes to VOPs.
3
4 ;;;; This software is part of the SBCL system. See the README file for
5 ;;;; more information.
6 ;;;;
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
12
13 (in-package "SB!C")
14 \f
15 ;;;; moves and type checks
16
17 ;;; Move X to Y unless they are EQ.
18 (defun emit-move (node block x y)
19   (declare (type node node) (type ir2-block block) (type tn x y))
20   (unless (eq x y)
21     (vop move node block x y))
22   (values))
23
24 ;;; Determine whether we should emit a single-stepper breakpoint
25 ;;; around a call / before a vop.
26 (defun emit-step-p (node)
27   (if (and (policy node (> insert-step-conditions 1))
28            (typep node 'combination))
29       (combination-step-info node)
30       nil))
31
32 ;;; If there is any CHECK-xxx template for TYPE, then return it,
33 ;;; otherwise return NIL.
34 (defun type-check-template (type)
35   (declare (type ctype type))
36   (multiple-value-bind (check-ptype exact) (primitive-type type)
37     (if exact
38         (primitive-type-check check-ptype)
39         (let ((name (hairy-type-check-template-name type)))
40           (if name
41               (template-or-lose name)
42               nil)))))
43
44 ;;; Emit code in BLOCK to check that VALUE is of the specified TYPE,
45 ;;; yielding the checked result in RESULT. VALUE and result may be of
46 ;;; any primitive type. There must be CHECK-xxx VOP for TYPE. Any
47 ;;; other type checks should have been converted to an explicit type
48 ;;; test.
49 (defun emit-type-check (node block value result type)
50   (declare (type tn value result) (type node node) (type ir2-block block)
51            (type ctype type))
52   (emit-move-template node block (type-check-template type) value result)
53   (values))
54
55 ;;; Allocate an indirect value cell.
56 (defevent make-value-cell-event "Allocate heap value cell for lexical var.")
57 (defun emit-make-value-cell (node block value res)
58   (event make-value-cell-event node)
59   (vop make-value-cell node block value nil res))
60 \f
61 ;;;; leaf reference
62
63 ;;; Return the TN that holds the value of THING in the environment ENV.
64 (declaim (ftype (function ((or nlx-info lambda-var clambda) physenv) tn)
65                 find-in-physenv))
66 (defun find-in-physenv (thing physenv)
67   (or (cdr (assoc thing (ir2-physenv-closure (physenv-info physenv))))
68       (etypecase thing
69         (lambda-var
70          ;; I think that a failure of this assertion means that we're
71          ;; trying to access a variable which was improperly closed
72          ;; over. The PHYSENV describes a physical environment. Every
73          ;; variable that a form refers to should either be in its
74          ;; physical environment directly, or grabbed from a
75          ;; surrounding physical environment when it was closed over.
76          ;; The ASSOC expression above finds closed-over variables, so
77          ;; if we fell through the ASSOC expression, it wasn't closed
78          ;; over. Therefore, it must be in our physical environment
79          ;; directly. If instead it is in some other physical
80          ;; environment, then it's bogus for us to reference it here
81          ;; without it being closed over. -- WHN 2001-09-29
82          (aver (eq physenv (lambda-physenv (lambda-var-home thing))))
83          (leaf-info thing))
84         (nlx-info
85          (aver (eq physenv (block-physenv (nlx-info-target thing))))
86          (ir2-nlx-info-home (nlx-info-info thing)))
87         (clambda
88          (aver (xep-p thing))
89          (entry-info-closure-tn (lambda-info thing))))
90       (bug "~@<~2I~_~S ~_not found in ~_~S~:>" thing physenv)))
91
92 ;;; If LEAF already has a constant TN, return that, otherwise make a
93 ;;; TN for it.
94 (defun constant-tn (leaf)
95   (declare (type constant leaf))
96   (or (leaf-info leaf)
97       (setf (leaf-info leaf)
98             (make-constant-tn leaf))))
99
100 ;;; Return a TN that represents the value of LEAF, or NIL if LEAF
101 ;;; isn't directly represented by a TN. ENV is the environment that
102 ;;; the reference is done in.
103 (defun leaf-tn (leaf env)
104   (declare (type leaf leaf) (type physenv env))
105   (typecase leaf
106     (lambda-var
107      (unless (lambda-var-indirect leaf)
108        (find-in-physenv leaf env)))
109     (constant (constant-tn leaf))
110     (t nil)))
111
112 ;;; This is used to conveniently get a handle on a constant TN during
113 ;;; IR2 conversion. It returns a constant TN representing the Lisp
114 ;;; object VALUE.
115 (defun emit-constant (value)
116   (constant-tn (find-constant value)))
117
118 ;;; Convert a REF node. The reference must not be delayed.
119 (defun ir2-convert-ref (node block)
120   (declare (type ref node) (type ir2-block block))
121   (let* ((lvar (node-lvar node))
122          (leaf (ref-leaf node))
123          (locs (lvar-result-tns
124                 lvar (list (primitive-type (leaf-type leaf)))))
125          (res (first locs)))
126     (etypecase leaf
127       (lambda-var
128        (let ((tn (find-in-physenv leaf (node-physenv node)))
129              (indirect (lambda-var-indirect leaf))
130              (explicit (lambda-var-explicit-value-cell leaf)))
131          (cond
132           ((and indirect explicit)
133            (vop value-cell-ref node block tn res))
134           ((and indirect
135                 (not (eq (node-physenv node)
136                          (lambda-physenv (lambda-var-home leaf)))))
137            (vop ancestor-frame-ref node block tn (leaf-info leaf) res))
138           (t (emit-move node block tn res)))))
139       (constant
140        (emit-move node block (constant-tn leaf) res))
141       (functional
142        (ir2-convert-closure node block leaf res))
143       (global-var
144        (let ((unsafe (policy node (zerop safety)))
145              (name (leaf-source-name leaf)))
146          (ecase (global-var-kind leaf)
147            ((:special :unknown)
148             (aver (symbolp name))
149             (let ((name-tn (emit-constant name)))
150               (if (or unsafe (info :variable :always-bound name))
151                   (vop fast-symbol-value node block name-tn res)
152                   (vop symbol-value node block name-tn res))))
153            (:global
154             (aver (symbolp name))
155             (let ((name-tn (emit-constant name)))
156               (if (or unsafe (info :variable :always-bound name))
157                   (vop fast-symbol-global-value node block name-tn res)
158                   (vop symbol-global-value node block name-tn res))))
159            (:global-function
160             (let ((fdefn-tn (make-load-time-constant-tn :fdefinition name)))
161               (if unsafe
162                   (vop fdefn-fun node block fdefn-tn res)
163                   (vop safe-fdefn-fun node block fdefn-tn res))))))))
164     (move-lvar-result node block locs lvar))
165   (values))
166
167 ;;; some sanity checks for a CLAMBDA passed to IR2-CONVERT-CLOSURE
168 (defun assertions-on-ir2-converted-clambda (clambda)
169   ;; This assertion was sort of an experiment. It would be nice and
170   ;; sane and easier to understand things if it were *always* true,
171   ;; but experimentally I observe that it's only *almost* always
172   ;; true. -- WHN 2001-01-02
173   #+nil
174   (aver (eql (lambda-component clambda)
175              (block-component (ir2-block-block ir2-block))))
176   ;; Check for some weirdness which came up in bug
177   ;; 138, 2002-01-02.
178   ;;
179   ;; The MAKE-LOAD-TIME-CONSTANT-TN call above puts an :ENTRY record
180   ;; into the IR2-COMPONENT-CONSTANTS table. The dump-a-COMPONENT
181   ;; code
182   ;;   * treats every HANDLEless :ENTRY record into a
183   ;;     patch, and
184   ;;   * expects every patch to correspond to an
185   ;;     IR2-COMPONENT-ENTRIES record.
186   ;; The IR2-COMPONENT-ENTRIES records are set by ENTRY-ANALYZE
187   ;; walking over COMPONENT-LAMBDAS. Bug 138b arose because there
188   ;; was a HANDLEless :ENTRY record which didn't correspond to an
189   ;; IR2-COMPONENT-ENTRIES record. That problem is hard to debug
190   ;; when it's caught at dump time, so this assertion tries to catch
191   ;; it here.
192   (aver (member clambda
193                 (component-lambdas (lambda-component clambda))))
194   ;; another bug-138-related issue: COMPONENT-NEW-FUNCTIONALS is
195   ;; used as a queue for stuff pending to do in IR1, and now that
196   ;; we're doing IR2 it should've been completely flushed (but
197   ;; wasn't).
198   (aver (null (component-new-functionals (lambda-component clambda))))
199   (values))
200
201 ;;; Emit code to load a function object implementing FUNCTIONAL into
202 ;;; RES. This gets interesting when the referenced function is a
203 ;;; closure: we must make the closure and move the closed-over values
204 ;;; into it.
205 ;;;
206 ;;; FUNCTIONAL is either a :TOPLEVEL-XEP functional or the XEP lambda
207 ;;; for the called function, since local call analysis converts all
208 ;;; closure references. If a :TOPLEVEL-XEP, we know it is not a
209 ;;; closure.
210 ;;;
211 ;;; If a closed-over LAMBDA-VAR has no refs (is deleted), then we
212 ;;; don't initialize that slot. This can happen with closures over
213 ;;; top level variables, where optimization of the closure deleted the
214 ;;; variable. Since we committed to the closure format when we
215 ;;; pre-analyzed the top level code, we just leave an empty slot.
216 (defun ir2-convert-closure (ref ir2-block functional res)
217   (declare (type ref ref)
218            (type ir2-block ir2-block)
219            (type functional functional)
220            (type tn res))
221   (aver (not (eql (functional-kind functional) :deleted)))
222   (unless (leaf-info functional)
223     (setf (leaf-info functional)
224           (make-entry-info :name (functional-debug-name functional))))
225   (let ((closure (etypecase functional
226                    (clambda
227                     (assertions-on-ir2-converted-clambda functional)
228                     (physenv-closure (get-lambda-physenv functional)))
229                    (functional
230                     (aver (eq (functional-kind functional) :toplevel-xep))
231                     nil))))
232
233     (cond (closure
234            (let* ((physenv (node-physenv ref))
235                   (tn (find-in-physenv functional physenv)))
236              (emit-move ref ir2-block tn res)))
237           (t
238            (let ((entry (make-load-time-constant-tn :entry functional)))
239              (emit-move ref ir2-block entry res)))))
240   (values))
241
242 (defun closure-initial-value (what this-env current-fp)
243   (declare (type (or nlx-info lambda-var clambda) what)
244            (type physenv this-env)
245            (type (or tn null) current-fp))
246   ;; If we have an indirect LAMBDA-VAR that does not require an
247   ;; EXPLICIT-VALUE-CELL, and is from this environment (not from being
248   ;; closed over), we need to store the current frame pointer.
249   (if (and (lambda-var-p what)
250            (lambda-var-indirect what)
251            (not (lambda-var-explicit-value-cell what))
252            (eq (lambda-physenv (lambda-var-home what))
253                this-env))
254     current-fp
255     (find-in-physenv what this-env)))
256
257 (defoptimizer (%allocate-closures ltn-annotate) ((leaves) node ltn-policy)
258   ltn-policy ; a hack to effectively (DECLARE (IGNORE LTN-POLICY))
259   (when (lvar-dynamic-extent leaves)
260     (let ((info (make-ir2-lvar *backend-t-primitive-type*)))
261       (setf (ir2-lvar-kind info) :delayed)
262       (setf (lvar-info leaves) info)
263       (setf (ir2-lvar-stack-pointer info)
264             (make-stack-pointer-tn)))))
265
266 (defoptimizer (%allocate-closures ir2-convert) ((leaves) call 2block)
267   (let ((dx-p (lvar-dynamic-extent leaves)))
268     (collect ((delayed))
269       (when dx-p
270         (vop current-stack-pointer call 2block
271              (ir2-lvar-stack-pointer (lvar-info leaves))))
272       (dolist (leaf (lvar-value leaves))
273         (binding* ((xep (awhen (functional-entry-fun leaf)
274                           ;; if the xep's been deleted then we can skip it
275                           (if (eq (functional-kind it) :deleted)
276                               nil it))
277                         :exit-if-null)
278                    (nil (aver (xep-p xep)))
279                    (entry-info (lambda-info xep) :exit-if-null)
280                    (tn (entry-info-closure-tn entry-info) :exit-if-null)
281                    (closure (physenv-closure (get-lambda-physenv xep)))
282                    (entry (make-load-time-constant-tn :entry xep)))
283           (let ((this-env (node-physenv call))
284                 (leaf-dx-p (and dx-p (leaf-dynamic-extent leaf))))
285             (vop make-closure call 2block entry (length closure)
286                  leaf-dx-p tn)
287             (loop for what in closure and n from 0 do
288                   (unless (and (lambda-var-p what)
289                                (null (leaf-refs what)))
290                     ;; In LABELS a closure may refer to another closure
291                     ;; in the same group, so we must be sure that we
292                     ;; store a closure only after its creation.
293                     ;;
294                     ;; TODO: Here is a simple solution: we postpone
295                     ;; putting of all closures after all creations
296                     ;; (though it may require more registers).
297                     (if (lambda-p what)
298                       (delayed (list tn (find-in-physenv what this-env) n))
299                       (let ((initial-value (closure-initial-value
300                                             what this-env nil)))
301                         (if initial-value
302                           (vop closure-init call 2block
303                                tn initial-value n)
304                           ;; An initial-value of NIL means to stash
305                           ;; the frame pointer... which requires a
306                           ;; different VOP.
307                           (vop closure-init-from-fp call 2block tn n)))))))))
308       (loop for (tn what n) in (delayed)
309             do (vop closure-init call 2block
310                     tn what n))))
311   (values))
312
313 ;;; Convert a SET node. If the NODE's LVAR is annotated, then we also
314 ;;; deliver the value to that lvar. If the var is a lexical variable
315 ;;; with no refs, then we don't actually set anything, since the
316 ;;; variable has been deleted.
317 (defun ir2-convert-set (node block)
318   (declare (type cset node) (type ir2-block block))
319   (let* ((lvar (node-lvar node))
320          (leaf (set-var node))
321          (val (lvar-tn node block (set-value node)))
322          (locs (if lvar
323                    (lvar-result-tns
324                     lvar (list (primitive-type (leaf-type leaf))))
325                    nil)))
326     (etypecase leaf
327       (lambda-var
328        (when (leaf-refs leaf)
329          (let ((tn (find-in-physenv leaf (node-physenv node)))
330                (indirect (lambda-var-indirect leaf))
331                (explicit (lambda-var-explicit-value-cell leaf)))
332            (cond
333             ((and indirect explicit)
334              (vop value-cell-set node block tn val))
335             ((and indirect
336                   (not (eq (node-physenv node)
337                            (lambda-physenv (lambda-var-home leaf)))))
338              (vop ancestor-frame-set node block tn val (leaf-info leaf)))
339             (t (emit-move node block val tn))))))
340       (global-var
341        (aver (symbolp (leaf-source-name leaf)))
342        (ecase (global-var-kind leaf)
343          ((:special)
344           (vop set node block (emit-constant (leaf-source-name leaf)) val))
345          ((:global)
346           (vop %set-symbol-global-value node
347                block (emit-constant (leaf-source-name leaf)) val)))))
348     (when locs
349       (emit-move node block val (first locs))
350       (move-lvar-result node block locs lvar)))
351   (values))
352 \f
353 ;;;; utilities for receiving fixed values
354
355 ;;; Return a TN that can be referenced to get the value of LVAR. LVAR
356 ;;; must be LTN-ANNOTATED either as a delayed leaf ref or as a fixed,
357 ;;; single-value lvar.
358 ;;;
359 ;;; The primitive-type of the result will always be the same as the
360 ;;; IR2-LVAR-PRIMITIVE-TYPE, ensuring that VOPs are always called with
361 ;;; TNs that satisfy the operand primitive-type restriction. We may
362 ;;; have to make a temporary of the desired type and move the actual
363 ;;; lvar TN into it. This happens when we delete a type check in
364 ;;; unsafe code or when we locally know something about the type of an
365 ;;; argument variable.
366 (defun lvar-tn (node block lvar)
367   (declare (type node node) (type ir2-block block) (type lvar lvar))
368   (let* ((2lvar (lvar-info lvar))
369          (lvar-tn
370           (ecase (ir2-lvar-kind 2lvar)
371             (:delayed
372              (let ((ref (lvar-uses lvar)))
373                (leaf-tn (ref-leaf ref) (node-physenv ref))))
374             (:fixed
375              (aver (= (length (ir2-lvar-locs 2lvar)) 1))
376              (first (ir2-lvar-locs 2lvar)))))
377          (ptype (ir2-lvar-primitive-type 2lvar)))
378
379     (cond ((eq (tn-primitive-type lvar-tn) ptype) lvar-tn)
380           (t
381            (let ((temp (make-normal-tn ptype)))
382              (emit-move node block lvar-tn temp)
383              temp)))))
384
385 ;;; This is similar to LVAR-TN, but hacks multiple values. We return
386 ;;; TNs holding the values of LVAR with PTYPES as their primitive
387 ;;; types. LVAR must be annotated for the same number of fixed values
388 ;;; are there are PTYPES.
389 ;;;
390 ;;; If the lvar has a type check, check the values into temps and
391 ;;; return the temps. When we have more values than assertions, we
392 ;;; move the extra values with no check.
393 (defun lvar-tns (node block lvar ptypes)
394   (declare (type node node) (type ir2-block block)
395            (type lvar lvar) (list ptypes))
396   (let* ((locs (ir2-lvar-locs (lvar-info lvar)))
397          (nlocs (length locs)))
398     (aver (= nlocs (length ptypes)))
399
400     (mapcar (lambda (from to-type)
401               (if (eq (tn-primitive-type from) to-type)
402                   from
403                   (let ((temp (make-normal-tn to-type)))
404                     (emit-move node block from temp)
405                     temp)))
406             locs
407             ptypes)))
408 \f
409 ;;;; utilities for delivering values to lvars
410
411 ;;; Return a list of TNs with the specifier TYPES that can be used as
412 ;;; result TNs to evaluate an expression into LVAR. This is used
413 ;;; together with MOVE-LVAR-RESULT to deliver fixed values to
414 ;;; an lvar.
415 ;;;
416 ;;; If the lvar isn't annotated (meaning the values are discarded) or
417 ;;; is unknown-values, the then we make temporaries for each supplied
418 ;;; value, providing a place to compute the result in until we decide
419 ;;; what to do with it (if anything.)
420 ;;;
421 ;;; If the lvar is fixed-values, and wants the same number of values
422 ;;; as the user wants to deliver, then we just return the
423 ;;; IR2-LVAR-LOCS. Otherwise we make a new list padded as necessary by
424 ;;; discarded TNs. We always return a TN of the specified type, using
425 ;;; the lvar locs only when they are of the correct type.
426 (defun lvar-result-tns (lvar types)
427   (declare (type (or lvar null) lvar) (type list types))
428   (if (not lvar)
429       (mapcar #'make-normal-tn types)
430       (let ((2lvar (lvar-info lvar)))
431         (ecase (ir2-lvar-kind 2lvar)
432           (:fixed
433            (let* ((locs (ir2-lvar-locs 2lvar))
434                   (nlocs (length locs))
435                   (ntypes (length types)))
436              (if (and (= nlocs ntypes)
437                       (do ((loc locs (cdr loc))
438                            (type types (cdr type)))
439                           ((null loc) t)
440                         (unless (eq (tn-primitive-type (car loc)) (car type))
441                           (return nil))))
442                  locs
443                  (mapcar (lambda (loc type)
444                            (if (eq (tn-primitive-type loc) type)
445                                loc
446                                (make-normal-tn type)))
447                          (if (< nlocs ntypes)
448                              (append locs
449                                      (mapcar #'make-normal-tn
450                                              (subseq types nlocs)))
451                              locs)
452                          types))))
453           (:unknown
454            (mapcar #'make-normal-tn types))))))
455
456 ;;; Make the first N standard value TNs, returning them in a list.
457 (defun make-standard-value-tns (n)
458   (declare (type unsigned-byte n))
459   (collect ((res))
460     (dotimes (i n)
461       (res (standard-arg-location i)))
462     (res)))
463
464 ;;; Return a list of TNs wired to the standard value passing
465 ;;; conventions that can be used to receive values according to the
466 ;;; unknown-values convention. This is used with together
467 ;;; MOVE-LVAR-RESULT for delivering unknown values to a fixed values
468 ;;; lvar.
469 ;;;
470 ;;; If the lvar isn't annotated, then we treat as 0-values, returning
471 ;;; an empty list of temporaries.
472 ;;;
473 ;;; If the lvar is annotated, then it must be :FIXED.
474 (defun standard-result-tns (lvar)
475   (declare (type (or lvar null) lvar))
476   (if lvar
477       (let ((2lvar (lvar-info lvar)))
478         (ecase (ir2-lvar-kind 2lvar)
479           (:fixed
480            (make-standard-value-tns (length (ir2-lvar-locs 2lvar))))))
481       nil))
482
483 ;;; Just move each SRC TN into the corresponding DEST TN, defaulting
484 ;;; any unsupplied source values to NIL. We let EMIT-MOVE worry about
485 ;;; doing the appropriate coercions.
486 (defun move-results-coerced (node block src dest)
487   (declare (type node node) (type ir2-block block) (list src dest))
488   (let ((nsrc (length src))
489         (ndest (length dest)))
490     (mapc (lambda (from to)
491             (unless (eq from to)
492               (emit-move node block from to)))
493           (if (> ndest nsrc)
494               (append src (make-list (- ndest nsrc)
495                                      :initial-element (emit-constant nil)))
496               src)
497           dest))
498   (values))
499
500 ;;; Move each SRC TN into the corresponding DEST TN, checking types
501 ;;; and defaulting any unsupplied source values to NIL
502 (defun move-results-checked (node block src dest types)
503   (declare (type node node) (type ir2-block block) (list src dest types))
504   (let ((nsrc (length src))
505         (ndest (length dest))
506         (ntypes (length types)))
507     (mapc (lambda (from to type)
508             (if type
509                 (emit-type-check node block from to type)
510                 (emit-move node block from to)))
511           (if (> ndest nsrc)
512               (append src (make-list (- ndest nsrc)
513                                      :initial-element (emit-constant nil)))
514               src)
515           dest
516           (if (> ndest ntypes)
517               (append types (make-list (- ndest ntypes)))
518               types)))
519   (values))
520
521 ;;; If necessary, emit coercion code needed to deliver the RESULTS to
522 ;;; the specified lvar. NODE and BLOCK provide context for emitting
523 ;;; code. Although usually obtained from STANDARD-RESULT-TNs or
524 ;;; LVAR-RESULT-TNs, RESULTS my be a list of any type or
525 ;;; number of TNs.
526 ;;;
527 ;;; If the lvar is fixed values, then move the results into the lvar
528 ;;; locations. If the lvar is unknown values, then do the moves into
529 ;;; the standard value locations, and use PUSH-VALUES to put the
530 ;;; values on the stack.
531 (defun move-lvar-result (node block results lvar)
532   (declare (type node node) (type ir2-block block)
533            (list results) (type (or lvar null) lvar))
534   (when lvar
535     (let ((2lvar (lvar-info lvar)))
536       (ecase (ir2-lvar-kind 2lvar)
537         (:fixed
538          (let ((locs (ir2-lvar-locs 2lvar)))
539            (unless (eq locs results)
540              (move-results-coerced node block results locs))))
541         (:unknown
542          (let* ((nvals (length results))
543                 (locs (make-standard-value-tns nvals)))
544            (move-results-coerced node block results locs)
545            (vop* push-values node block
546                  ((reference-tn-list locs nil))
547                  ((reference-tn-list (ir2-lvar-locs 2lvar) t))
548                  nvals))))))
549   (values))
550
551 ;;; CAST
552 (defun ir2-convert-cast (node block)
553   (declare (type cast node)
554            (type ir2-block block))
555   (binding* ((lvar (node-lvar node) :exit-if-null)
556              (2lvar (lvar-info lvar))
557              (value (cast-value node))
558              (2value (lvar-info value)))
559     (cond ((eq (ir2-lvar-kind 2lvar) :unused))
560           ((eq (ir2-lvar-kind 2lvar) :unknown)
561            (aver (eq (ir2-lvar-kind 2value) :unknown))
562            (aver (not (cast-type-check node)))
563            (move-results-coerced node block
564                                  (ir2-lvar-locs 2value)
565                                  (ir2-lvar-locs 2lvar)))
566           ((eq (ir2-lvar-kind 2lvar) :fixed)
567            (aver (eq (ir2-lvar-kind 2value) :fixed))
568            (if (cast-type-check node)
569                (move-results-checked node block
570                                      (ir2-lvar-locs 2value)
571                                      (ir2-lvar-locs 2lvar)
572                                      (multiple-value-bind (check types)
573                                          (cast-check-types node nil)
574                                        (aver (eq check :simple))
575                                        types))
576                (move-results-coerced node block
577                                      (ir2-lvar-locs 2value)
578                                      (ir2-lvar-locs 2lvar))))
579           (t (bug "CAST cannot be :DELAYED.")))))
580 \f
581 ;;;; template conversion
582
583 ;;; Build a TN-REFS list that represents access to the values of the
584 ;;; specified list of lvars ARGS for TEMPLATE. Any :CONSTANT arguments
585 ;;; are returned in the second value as a list rather than being
586 ;;; accessed as a normal argument. NODE and BLOCK provide the context
587 ;;; for emitting any necessary type-checking code.
588 (defun reference-args (node block args template)
589   (declare (type node node) (type ir2-block block) (list args)
590            (type template template))
591   (collect ((info-args))
592     (let ((last nil)
593           (first nil))
594       (do ((args args (cdr args))
595            (types (template-arg-types template) (cdr types)))
596           ((null args))
597         (let ((type (first types))
598               (arg (first args)))
599           (if (and (consp type) (eq (car type) ':constant))
600               (info-args (lvar-value arg))
601               (let ((ref (reference-tn (lvar-tn node block arg) nil)))
602                 (if last
603                     (setf (tn-ref-across last) ref)
604                     (setf first ref))
605                 (setq last ref)))))
606
607       (values (the (or tn-ref null) first) (info-args)))))
608
609 ;;; Convert a conditional template. We try to exploit any
610 ;;; drop-through, but emit an unconditional branch afterward if we
611 ;;; fail. NOT-P is true if the sense of the TEMPLATE's test should be
612 ;;; negated.
613 (defun ir2-convert-conditional (node block template args info-args if not-p)
614   (declare (type node node) (type ir2-block block)
615            (type template template) (type (or tn-ref null) args)
616            (list info-args) (type cif if) (type boolean not-p))
617   (let ((consequent (if-consequent if))
618         (alternative (if-alternative if))
619         (flags       (and (consp (template-result-types template))
620                           (rest (template-result-types template)))))
621     (aver (= (template-info-arg-count template)
622              (+ (length info-args)
623                 (if flags 0 2))))
624     (when not-p
625       (rotatef consequent alternative)
626       (setf not-p nil))
627     (when (drop-thru-p if consequent)
628       (rotatef consequent alternative)
629       (setf not-p t))
630     (cond ((not flags)
631            (emit-template node block template args nil
632                           (list* (block-label consequent) not-p
633                                  info-args))
634            (if (drop-thru-p if alternative)
635                (register-drop-thru alternative)
636                (vop branch node block (block-label alternative))))
637           (t
638            (emit-template node block template args nil info-args)
639            (vop branch-if node block (block-label consequent) flags not-p)
640            (if (drop-thru-p if alternative)
641                (register-drop-thru alternative)
642                (vop branch node block (block-label alternative)))))))
643
644 ;;; Convert an IF that isn't the DEST of a conditional template.
645 (defun ir2-convert-if (node block)
646   (declare (type ir2-block block) (type cif node))
647   (let* ((test (if-test node))
648          (test-ref (reference-tn (lvar-tn node block test) nil))
649          (nil-ref (reference-tn (emit-constant nil) nil)))
650     (setf (tn-ref-across test-ref) nil-ref)
651     (ir2-convert-conditional node block (template-or-lose 'if-eq)
652                              test-ref () node t)))
653
654 ;;; Return a list of primitive-types that we can pass to LVAR-RESULT-TNS
655 ;;; describing the result types we want for a template call. We are really
656 ;;; only interested in the number of results required: in normal case
657 ;;; TEMPLATE-RESULTS-OK has already checked them.
658 (defun find-template-result-types (call rtypes)
659   (let* ((type (node-derived-type call))
660          (types
661           (mapcar #'primitive-type
662                   (if (values-type-p type)
663                       (append (args-type-required type)
664                               (args-type-optional type))
665                       (list type))))
666          (primitive-t *backend-t-primitive-type*))
667     (loop for rtype in rtypes
668           for type = (or (pop types) primitive-t)
669           collect type)))
670
671 ;;; Return a list of TNs usable in a CALL to TEMPLATE delivering values to
672 ;;; LVAR. As an efficiency hack, we pick off the common case where the LVAR is
673 ;;; fixed values and has locations that satisfy the result restrictions. This
674 ;;; can fail when there is a type check or a values count mismatch.
675 (defun make-template-result-tns (call lvar rtypes)
676   (declare (type combination call) (type (or lvar null) lvar)
677            (list rtypes))
678   (let ((2lvar (when lvar (lvar-info lvar))))
679     (if (and 2lvar (eq (ir2-lvar-kind 2lvar) :fixed))
680         (let ((locs (ir2-lvar-locs 2lvar)))
681           (if (and (= (length rtypes) (length locs))
682                    (do ((loc locs (cdr loc))
683                         (rtypes rtypes (cdr rtypes)))
684                        ((null loc) t)
685                      (unless (operand-restriction-ok
686                               (car rtypes)
687                               (tn-primitive-type (car loc))
688                               :t-ok nil)
689                        (return nil))))
690               locs
691               (lvar-result-tns
692                lvar
693                (find-template-result-types call rtypes))))
694         (lvar-result-tns
695          lvar
696          (find-template-result-types call rtypes)))))
697
698 ;;; Get the operands into TNs, make TN-REFs for them, and then call
699 ;;; the template emit function.
700 (defun ir2-convert-template (call block)
701   (declare (type combination call) (type ir2-block block))
702   (let* ((template (combination-info call))
703          (lvar (node-lvar call))
704          (rtypes (template-result-types template)))
705     (multiple-value-bind (args info-args)
706         (reference-args call block (combination-args call) template)
707       (aver (not (template-more-results-type template)))
708       (if (template-conditional-p template)
709           (ir2-convert-conditional call block template args info-args
710                                    (lvar-dest lvar) nil)
711           (let* ((results (make-template-result-tns call lvar rtypes))
712                  (r-refs (reference-tn-list results t)))
713             (aver (= (length info-args)
714                      (template-info-arg-count template)))
715             (when (and lvar (lvar-dynamic-extent lvar))
716               (vop current-stack-pointer call block
717                    (ir2-lvar-stack-pointer (lvar-info lvar))))
718             (when (emit-step-p call)
719               (vop sb!vm::step-instrument-before-vop call block))
720             (if info-args
721                 (emit-template call block template args r-refs info-args)
722                 (emit-template call block template args r-refs))
723             (move-lvar-result call block results lvar)))))
724   (values))
725
726 ;;; We don't have to do much because operand count checking is done by
727 ;;; IR1 conversion. The only difference between this and the function
728 ;;; case of IR2-CONVERT-TEMPLATE is that there can be codegen-info
729 ;;; arguments.
730 (defoptimizer (%%primitive ir2-convert) ((template info &rest args) call block)
731   (let* ((template (lvar-value template))
732          (info (lvar-value info))
733          (lvar (node-lvar call))
734          (rtypes (template-result-types template))
735          (results (make-template-result-tns call lvar rtypes))
736          (r-refs (reference-tn-list results t)))
737     (multiple-value-bind (args info-args)
738         (reference-args call block (cddr (combination-args call)) template)
739       (aver (not (template-more-results-type template)))
740       (aver (not (template-conditional-p template)))
741       (aver (null info-args))
742
743       (if info
744           (emit-template call block template args r-refs info)
745           (emit-template call block template args r-refs))
746
747       (move-lvar-result call block results lvar)))
748   (values))
749
750 (defoptimizer (%%primitive derive-type) ((template info &rest args))
751   (let ((type (template-type (lvar-value template))))
752     (if (fun-type-p type)
753         (fun-type-returns type)
754         *wild-type*)))
755 \f
756 ;;;; local call
757
758 ;;; Convert a LET by moving the argument values into the variables.
759 ;;; Since a LET doesn't have any passing locations, we move the
760 ;;; arguments directly into the variables. We must also allocate any
761 ;;; indirect value cells, since there is no function prologue to do
762 ;;; this.
763 (defun ir2-convert-let (node block fun)
764   (declare (type combination node) (type ir2-block block) (type clambda fun))
765   (mapc (lambda (var arg)
766           (when arg
767             (let ((src (lvar-tn node block arg))
768                   (dest (leaf-info var)))
769               (if (and (lambda-var-indirect var)
770                        (lambda-var-explicit-value-cell var))
771                   (emit-make-value-cell node block src dest)
772                   (emit-move node block src dest)))))
773         (lambda-vars fun) (basic-combination-args node))
774   (values))
775
776 ;;; Emit any necessary moves into assignment temps for a local call to
777 ;;; FUN. We return two lists of TNs: TNs holding the actual argument
778 ;;; values, and (possibly EQ) TNs that are the actual destination of
779 ;;; the arguments. When necessary, we allocate temporaries for
780 ;;; arguments to preserve parallel assignment semantics. These lists
781 ;;; exclude unused arguments and include implicit environment
782 ;;; arguments, i.e. they exactly correspond to the arguments passed.
783 ;;;
784 ;;; OLD-FP is the TN currently holding the value we want to pass as
785 ;;; OLD-FP. If null, then the call is to the same environment (an
786 ;;; :ASSIGNMENT), so we only move the arguments, and leave the
787 ;;; environment alone.
788 ;;;
789 ;;; CLOSURE-FP is for calling a closure that has "implicit" value
790 ;;; cells (stored in the allocating stack frame), and is the frame
791 ;;; pointer TN to use for values allocated in the outbound stack
792 ;;; frame.  This is distinct from OLD-FP for the specific case of a
793 ;;; tail-local-call.
794 (defun emit-psetq-moves (node block fun old-fp &optional (closure-fp old-fp))
795   (declare (type combination node) (type ir2-block block) (type clambda fun)
796            (type (or tn null) old-fp closure-fp))
797   (let ((actuals (mapcar (lambda (x)
798                            (when x
799                              (lvar-tn node block x)))
800                          (combination-args node))))
801     (collect ((temps)
802               (locs))
803       (dolist (var (lambda-vars fun))
804         (let ((actual (pop actuals))
805               (loc (leaf-info var)))
806           (when actual
807             (cond
808              ((and (lambda-var-indirect var)
809                    (lambda-var-explicit-value-cell var))
810               (let ((temp
811                      (make-normal-tn *backend-t-primitive-type*)))
812                 (emit-make-value-cell node block actual temp)
813                 (temps temp)))
814              ((member actual (locs))
815               (let ((temp (make-normal-tn (tn-primitive-type loc))))
816                 (emit-move node block actual temp)
817                 (temps temp)))
818              (t
819               (temps actual)))
820             (locs loc))))
821
822       (when old-fp
823         (let ((this-1env (node-physenv node))
824               (called-env (physenv-info (lambda-physenv fun))))
825           (dolist (thing (ir2-physenv-closure called-env))
826             (temps (closure-initial-value (car thing) this-1env closure-fp))
827             (locs (cdr thing)))
828           (temps old-fp)
829           (locs (ir2-physenv-old-fp called-env))))
830
831       (values (temps) (locs)))))
832
833 ;;; A tail-recursive local call is done by emitting moves of stuff
834 ;;; into the appropriate passing locations. After setting up the args
835 ;;; and environment, we just move our return-pc into the called
836 ;;; function's passing location.
837 (defun ir2-convert-tail-local-call (node block fun)
838   (declare (type combination node) (type ir2-block block) (type clambda fun))
839   (let ((this-env (physenv-info (node-physenv node)))
840         (current-fp (make-stack-pointer-tn)))
841     (multiple-value-bind (temps locs)
842         (emit-psetq-moves node block fun
843                           (ir2-physenv-old-fp this-env) current-fp)
844
845       ;; If we're about to emit a move from CURRENT-FP then we need to
846       ;; initialize it.
847       (when (find current-fp temps)
848         (vop current-fp node block current-fp))
849
850       (mapc (lambda (temp loc)
851               (emit-move node block temp loc))
852             temps locs))
853
854     (emit-move node block
855                (ir2-physenv-return-pc this-env)
856                (ir2-physenv-return-pc-pass
857                 (physenv-info
858                  (lambda-physenv fun)))))
859
860   (values))
861
862 ;;; Convert an :ASSIGNMENT call. This is just like a tail local call,
863 ;;; except that the caller and callee environment are the same, so we
864 ;;; don't need to mess with the environment locations, return PC, etc.
865 (defun ir2-convert-assignment (node block fun)
866   (declare (type combination node) (type ir2-block block) (type clambda fun))
867     (multiple-value-bind (temps locs) (emit-psetq-moves node block fun nil)
868
869       (mapc (lambda (temp loc)
870               (emit-move node block temp loc))
871             temps locs))
872   (values))
873
874 ;;; Do stuff to set up the arguments to a non-tail local call
875 ;;; (including implicit environment args.) We allocate a frame
876 ;;; (returning the FP and NFP), and also compute the TN-REFS list for
877 ;;; the values to pass and the list of passing location TNs.
878 (defun ir2-convert-local-call-args (node block fun)
879   (declare (type combination node) (type ir2-block block) (type clambda fun))
880   (let ((fp (make-stack-pointer-tn))
881         (nfp (make-number-stack-pointer-tn))
882         (old-fp (make-stack-pointer-tn)))
883     (multiple-value-bind (temps locs)
884         (emit-psetq-moves node block fun old-fp)
885       (vop current-fp node block old-fp)
886       (vop allocate-frame node block
887            (physenv-info (lambda-physenv fun))
888            fp nfp)
889       (values fp nfp temps (mapcar #'make-alias-tn locs)))))
890
891 ;;; Handle a non-TR known-values local call. We emit the call, then
892 ;;; move the results to the lvar's destination.
893 (defun ir2-convert-local-known-call (node block fun returns lvar start)
894   (declare (type node node) (type ir2-block block) (type clambda fun)
895            (type return-info returns) (type (or lvar null) lvar)
896            (type label start))
897   (multiple-value-bind (fp nfp temps arg-locs)
898       (ir2-convert-local-call-args node block fun)
899     (let ((locs (return-info-locations returns)))
900       (vop* known-call-local node block
901             (fp nfp (reference-tn-list temps nil))
902             ((reference-tn-list locs t))
903             arg-locs (physenv-info (lambda-physenv fun)) start)
904       (move-lvar-result node block locs lvar)))
905   (values))
906
907 ;;; Handle a non-TR unknown-values local call. We do different things
908 ;;; depending on what kind of values the lvar wants.
909 ;;;
910 ;;; If LVAR is :UNKNOWN, then we use the "multiple-" variant, directly
911 ;;; specifying the lvar's LOCS as the VOP results so that we don't
912 ;;; have to do anything after the call.
913 ;;;
914 ;;; Otherwise, we use STANDARD-RESULT-TNS to get wired result TNs, and
915 ;;; then call MOVE-LVAR-RESULT to do any necessary type checks or
916 ;;; coercions.
917 (defun ir2-convert-local-unknown-call (node block fun lvar start)
918   (declare (type node node) (type ir2-block block) (type clambda fun)
919            (type (or lvar null) lvar) (type label start))
920   (multiple-value-bind (fp nfp temps arg-locs)
921       (ir2-convert-local-call-args node block fun)
922     (let ((2lvar (and lvar (lvar-info lvar)))
923           (env (physenv-info (lambda-physenv fun)))
924           (temp-refs (reference-tn-list temps nil)))
925       (if (and 2lvar (eq (ir2-lvar-kind 2lvar) :unknown))
926           (vop* multiple-call-local node block (fp nfp temp-refs)
927                 ((reference-tn-list (ir2-lvar-locs 2lvar) t))
928                 arg-locs env start)
929           (let ((locs (standard-result-tns lvar)))
930             (vop* call-local node block
931                   (fp nfp temp-refs)
932                   ((reference-tn-list locs t))
933                   arg-locs env start (length locs))
934             (move-lvar-result node block locs lvar)))))
935   (values))
936
937 ;;; Dispatch to the appropriate function, depending on whether we have
938 ;;; a let, tail or normal call. If the function doesn't return, call
939 ;;; it using the unknown-value convention. We could compile it as a
940 ;;; tail call, but that might seem confusing in the debugger.
941 (defun ir2-convert-local-call (node block)
942   (declare (type combination node) (type ir2-block block))
943   (let* ((fun (ref-leaf (lvar-uses (basic-combination-fun node))))
944          (kind (functional-kind fun)))
945     (cond ((eq kind :let)
946            (ir2-convert-let node block fun))
947           ((eq kind :assignment)
948            (ir2-convert-assignment node block fun))
949           ((node-tail-p node)
950            (ir2-convert-tail-local-call node block fun))
951           (t
952            (let ((start (block-trampoline (lambda-block fun)))
953                  (returns (tail-set-info (lambda-tail-set fun)))
954                  (lvar (node-lvar node)))
955              (ecase (if returns
956                         (return-info-kind returns)
957                         :unknown)
958                (:unknown
959                 (ir2-convert-local-unknown-call node block fun lvar start))
960                (:fixed
961                 (ir2-convert-local-known-call node block fun returns
962                                               lvar start)))))))
963   (values))
964 \f
965 ;;;; full call
966
967 ;;; Given a function lvar FUN, return (VALUES TN-TO-CALL NAMED-P),
968 ;;; where TN-TO-CALL is a TN holding the thing that we call NAMED-P is
969 ;;; true if the thing is named (false if it is a function).
970 ;;;
971 ;;; There are two interesting non-named cases:
972 ;;;   -- We know it's a function. No check needed: return the
973 ;;;      lvar LOC.
974 ;;;   -- We don't know what it is.
975 (defun fun-lvar-tn (node block lvar)
976   (declare (ignore node block))
977   (declare (type lvar lvar))
978   (let ((2lvar (lvar-info lvar)))
979     (if (eq (ir2-lvar-kind 2lvar) :delayed)
980         (let ((name (lvar-fun-name lvar t)))
981           (aver name)
982           (values (make-load-time-constant-tn :fdefinition name) t))
983         (let* ((locs (ir2-lvar-locs 2lvar))
984                (loc (first locs))
985                (function-ptype (primitive-type-or-lose 'function)))
986           (aver (and (eq (ir2-lvar-kind 2lvar) :fixed)
987                      (= (length locs) 1)))
988           (aver (eq (tn-primitive-type loc) function-ptype))
989           (values loc nil)))))
990
991 ;;; Set up the args to NODE in the current frame, and return a TN-REF
992 ;;; list for the passing locations.
993 (defun move-tail-full-call-args (node block)
994   (declare (type combination node) (type ir2-block block))
995   (let ((args (basic-combination-args node))
996         (last nil)
997         (first nil))
998     (dotimes (num (length args))
999       (let ((loc (standard-arg-location num)))
1000         (emit-move node block (lvar-tn node block (elt args num)) loc)
1001         (let ((ref (reference-tn loc nil)))
1002           (if last
1003               (setf (tn-ref-across last) ref)
1004               (setf first ref))
1005           (setq last ref))))
1006       first))
1007
1008 ;;; Move the arguments into the passing locations and do a (possibly
1009 ;;; named) tail call.
1010 (defun ir2-convert-tail-full-call (node block)
1011   (declare (type combination node) (type ir2-block block))
1012   (let* ((env (physenv-info (node-physenv node)))
1013          (args (basic-combination-args node))
1014          (nargs (length args))
1015          (pass-refs (move-tail-full-call-args node block))
1016          (old-fp (ir2-physenv-old-fp env))
1017          (return-pc (ir2-physenv-return-pc env)))
1018
1019     (multiple-value-bind (fun-tn named)
1020         (fun-lvar-tn node block (basic-combination-fun node))
1021       (if named
1022           (vop* tail-call-named node block
1023                 (fun-tn old-fp return-pc pass-refs)
1024                 (nil)
1025                 nargs
1026                 (emit-step-p node))
1027           (vop* tail-call node block
1028                 (fun-tn old-fp return-pc pass-refs)
1029                 (nil)
1030                 nargs
1031                 (emit-step-p node)))))
1032
1033   (values))
1034
1035 ;;; like IR2-CONVERT-LOCAL-CALL-ARGS, only different
1036 (defun ir2-convert-full-call-args (node block)
1037   (declare (type combination node) (type ir2-block block))
1038   (let* ((args (basic-combination-args node))
1039          (fp (make-stack-pointer-tn))
1040          (nargs (length args)))
1041     (vop allocate-full-call-frame node block nargs fp)
1042     (collect ((locs))
1043       (let ((last nil)
1044             (first nil))
1045         (dotimes (num nargs)
1046           (locs (standard-arg-location num))
1047           (let ((ref (reference-tn (lvar-tn node block (elt args num))
1048                                    nil)))
1049             (if last
1050                 (setf (tn-ref-across last) ref)
1051                 (setf first ref))
1052             (setq last ref)))
1053
1054         (values fp first (locs) nargs)))))
1055
1056 ;;; Do full call when a fixed number of values are desired. We make
1057 ;;; STANDARD-RESULT-TNS for our lvar, then deliver the result using
1058 ;;; MOVE-LVAR-RESULT. We do named or normal call, as appropriate.
1059 (defun ir2-convert-fixed-full-call (node block)
1060   (declare (type combination node) (type ir2-block block))
1061   (multiple-value-bind (fp args arg-locs nargs)
1062       (ir2-convert-full-call-args node block)
1063     (let* ((lvar (node-lvar node))
1064            (locs (standard-result-tns lvar))
1065            (loc-refs (reference-tn-list locs t))
1066            (nvals (length locs)))
1067       (multiple-value-bind (fun-tn named)
1068           (fun-lvar-tn node block (basic-combination-fun node))
1069         (if named
1070             (vop* call-named node block (fp fun-tn args) (loc-refs)
1071                   arg-locs nargs nvals (emit-step-p node))
1072             (vop* call node block (fp fun-tn args) (loc-refs)
1073                   arg-locs nargs nvals (emit-step-p node)))
1074         (move-lvar-result node block locs lvar))))
1075   (values))
1076
1077 ;;; Do full call when unknown values are desired.
1078 (defun ir2-convert-multiple-full-call (node block)
1079   (declare (type combination node) (type ir2-block block))
1080   (multiple-value-bind (fp args arg-locs nargs)
1081       (ir2-convert-full-call-args node block)
1082     (let* ((lvar (node-lvar node))
1083            (locs (ir2-lvar-locs (lvar-info lvar)))
1084            (loc-refs (reference-tn-list locs t)))
1085       (multiple-value-bind (fun-tn named)
1086           (fun-lvar-tn node block (basic-combination-fun node))
1087         (if named
1088             (vop* multiple-call-named node block (fp fun-tn args) (loc-refs)
1089                   arg-locs nargs (emit-step-p node))
1090             (vop* multiple-call node block (fp fun-tn args) (loc-refs)
1091                   arg-locs nargs (emit-step-p node))))))
1092   (values))
1093
1094 ;;; stuff to check in PONDER-FULL-CALL
1095 ;;;
1096 ;;; These came in handy when troubleshooting cold boot after making
1097 ;;; major changes in the package structure: various transforms and
1098 ;;; VOPs and stuff got attached to the wrong symbol, so that
1099 ;;; references to the right symbol were bogusly translated as full
1100 ;;; calls instead of primitives, sending the system off into infinite
1101 ;;; space. Having a report on all full calls generated makes it easier
1102 ;;; to figure out what form caused the problem this time.
1103 #!+sb-show (defvar *show-full-called-fnames-p* nil)
1104 #!+sb-show (defvar *full-called-fnames* (make-hash-table :test 'equal))
1105
1106 ;;; Do some checks (and store some notes relevant for future checks)
1107 ;;; on a full call:
1108 ;;;   * Is this a full call to something we have reason to know should
1109 ;;;     never be full called? (Except as of sbcl-0.7.18 or so, we no
1110 ;;;     longer try to ensure this behavior when *FAILURE-P* has already
1111 ;;;     been detected.)
1112 ;;;   * Is this a full call to (SETF FOO) which might conflict with
1113 ;;;     a DEFSETF or some such thing elsewhere in the program?
1114 (defun ponder-full-call (node)
1115   (let* ((lvar (basic-combination-fun node))
1116          (fname (lvar-fun-name lvar t)))
1117     (declare (type (or symbol cons) fname))
1118
1119     #!+sb-show (unless (gethash fname *full-called-fnames*)
1120                  (setf (gethash fname *full-called-fnames*) t))
1121     #!+sb-show (when *show-full-called-fnames-p*
1122                  (/show "converting full call to named function" fname)
1123                  (/show (basic-combination-args node))
1124                  (/show (policy node speed) (policy node safety))
1125                  (/show (policy node compilation-speed))
1126                  (let ((arg-types (mapcar (lambda (lvar)
1127                                             (when lvar
1128                                               (type-specifier
1129                                                (lvar-type lvar))))
1130                                           (basic-combination-args node))))
1131                    (/show arg-types)))
1132
1133     ;; When illegal code is compiled, all sorts of perverse paths
1134     ;; through the compiler can be taken, and it's much harder -- and
1135     ;; probably pointless -- to guarantee that always-optimized-away
1136     ;; functions are actually optimized away. Thus, we skip the check
1137     ;; in that case.
1138     (unless *failure-p*
1139       ;; check to see if we know anything about the function
1140       (let ((info (info :function :info fname)))
1141         ;; if we know something, check to see if the full call was valid
1142         (when (and info (ir1-attributep (fun-info-attributes info)
1143                                         always-translatable))
1144           (/show (policy node speed) (policy node safety))
1145           (/show (policy node compilation-speed))
1146           (bug "full call to ~S" fname))))
1147
1148     (when (consp fname)
1149       (aver (legal-fun-name-p fname))
1150       (destructuring-bind (setfoid &rest stem) fname
1151         (when (eq setfoid 'setf)
1152           (setf (gethash (car stem) *setf-assumed-fboundp*) t))))))
1153
1154 ;;; If the call is in a tail recursive position and the return
1155 ;;; convention is standard, then do a tail full call. If one or fewer
1156 ;;; values are desired, then use a single-value call, otherwise use a
1157 ;;; multiple-values call.
1158 (defun ir2-convert-full-call (node block)
1159   (declare (type combination node) (type ir2-block block))
1160   (ponder-full-call node)
1161   (cond ((node-tail-p node)
1162          (ir2-convert-tail-full-call node block))
1163         ((let ((lvar (node-lvar node)))
1164            (and lvar
1165                 (eq (ir2-lvar-kind (lvar-info lvar)) :unknown)))
1166          (ir2-convert-multiple-full-call node block))
1167         (t
1168          (ir2-convert-fixed-full-call node block)))
1169   (values))
1170 \f
1171 ;;;; entering functions
1172
1173 ;;; Do all the stuff that needs to be done on XEP entry:
1174 ;;; -- Create frame.
1175 ;;; -- Copy any more arg.
1176 ;;; -- Set up the environment, accessing any closure variables.
1177 ;;; -- Move args from the standard passing locations to their internal
1178 ;;;    locations.
1179 (defun init-xep-environment (node block fun)
1180   (declare (type bind node) (type ir2-block block) (type clambda fun))
1181   (let ((start-label (entry-info-offset (leaf-info fun)))
1182         (env (physenv-info (node-physenv node))))
1183     (let ((ef (functional-entry-fun fun)))
1184       (cond ((and (optional-dispatch-p ef) (optional-dispatch-more-entry ef))
1185              ;; Special case the xep-allocate-frame + copy-more-arg case.
1186              (vop xep-allocate-frame node block start-label t)
1187              (vop copy-more-arg node block (optional-dispatch-max-args ef)))
1188             (t
1189              ;; No more args, so normal entry.
1190              (vop xep-allocate-frame node block start-label nil)))
1191       (if (ir2-physenv-closure env)
1192           (let ((closure (make-normal-tn *backend-t-primitive-type*)))
1193             (vop setup-closure-environment node block start-label closure)
1194             (let ((n -1))
1195               (dolist (loc (ir2-physenv-closure env))
1196                 (vop closure-ref node block closure (incf n) (cdr loc)))))
1197           (vop setup-environment node block start-label)))
1198
1199     (unless (eq (functional-kind fun) :toplevel)
1200       (let ((vars (lambda-vars fun))
1201             (n 0))
1202         (when (leaf-refs (first vars))
1203           (emit-move node block (make-arg-count-location)
1204                      (leaf-info (first vars))))
1205         (dolist (arg (rest vars))
1206           (when (leaf-refs arg)
1207             (let ((pass (standard-arg-location n))
1208                   (home (leaf-info arg)))
1209               (if (and (lambda-var-indirect arg)
1210                        (lambda-var-explicit-value-cell arg))
1211                   (emit-make-value-cell node block pass home)
1212                   (emit-move node block pass home))))
1213           (incf n))))
1214
1215     (emit-move node block (make-old-fp-passing-location t)
1216                (ir2-physenv-old-fp env)))
1217
1218   (values))
1219
1220 ;;; Emit function prolog code. This is only called on bind nodes for
1221 ;;; functions that allocate environments. All semantics of let calls
1222 ;;; are handled by IR2-CONVERT-LET.
1223 ;;;
1224 ;;; If not an XEP, all we do is move the return PC from its passing
1225 ;;; location, since in a local call, the caller allocates the frame
1226 ;;; and sets up the arguments.
1227 (defun ir2-convert-bind (node block)
1228   (declare (type bind node) (type ir2-block block))
1229   (let* ((fun (bind-lambda node))
1230          (env (physenv-info (lambda-physenv fun))))
1231     (aver (member (functional-kind fun)
1232                   '(nil :external :optional :toplevel :cleanup)))
1233
1234     (when (xep-p fun)
1235       (init-xep-environment node block fun)
1236       #!+sb-dyncount
1237       (when *collect-dynamic-statistics*
1238         (vop count-me node block *dynamic-counts-tn*
1239              (block-number (ir2-block-block block)))))
1240
1241     (emit-move node
1242                block
1243                (ir2-physenv-return-pc-pass env)
1244                (ir2-physenv-return-pc env))
1245
1246     #!+unwind-to-frame-and-call-vop
1247     (when (and (lambda-allow-instrumenting fun)
1248                (not (lambda-inline-expanded fun))
1249                (lambda-return fun)
1250                (policy fun (>= insert-debug-catch 2)))
1251       (vop sb!vm::bind-sentinel node block))
1252
1253     (let ((lab (gen-label)))
1254       (setf (ir2-physenv-environment-start env) lab)
1255       (vop note-environment-start node block lab)))
1256
1257   (values))
1258 \f
1259 ;;;; function return
1260
1261 ;;; Do stuff to return from a function with the specified values and
1262 ;;; convention. If the return convention is :FIXED and we aren't
1263 ;;; returning from an XEP, then we do a known return (letting
1264 ;;; representation selection insert the correct move-arg VOPs.)
1265 ;;; Otherwise, we use the unknown-values convention. If there is a
1266 ;;; fixed number of return values, then use RETURN, otherwise use
1267 ;;; RETURN-MULTIPLE.
1268 (defun ir2-convert-return (node block)
1269   (declare (type creturn node) (type ir2-block block))
1270   (let* ((lvar (return-result node))
1271          (2lvar (lvar-info lvar))
1272          (lvar-kind (ir2-lvar-kind 2lvar))
1273          (fun (return-lambda node))
1274          (env (physenv-info (lambda-physenv fun)))
1275          (old-fp (ir2-physenv-old-fp env))
1276          (return-pc (ir2-physenv-return-pc env))
1277          (returns (tail-set-info (lambda-tail-set fun))))
1278     #!+unwind-to-frame-and-call-vop
1279     (when (and (lambda-allow-instrumenting fun)
1280                (not (lambda-inline-expanded fun))
1281                (policy fun (>= insert-debug-catch 2)))
1282       (vop sb!vm::unbind-sentinel node block))
1283     (cond
1284      ((and (eq (return-info-kind returns) :fixed)
1285            (not (xep-p fun)))
1286       (let ((locs (lvar-tns node block lvar
1287                                     (return-info-types returns))))
1288         (vop* known-return node block
1289               (old-fp return-pc (reference-tn-list locs nil))
1290               (nil)
1291               (return-info-locations returns))))
1292      ((eq lvar-kind :fixed)
1293       (let* ((types (mapcar #'tn-primitive-type (ir2-lvar-locs 2lvar)))
1294              (lvar-locs (lvar-tns node block lvar types))
1295              (nvals (length lvar-locs))
1296              (locs (make-standard-value-tns nvals)))
1297         (mapc (lambda (val loc)
1298                 (emit-move node block val loc))
1299               lvar-locs
1300               locs)
1301         (if (= nvals 1)
1302             (vop return-single node block old-fp return-pc (car locs))
1303             (vop* return node block
1304                   (old-fp return-pc (reference-tn-list locs nil))
1305                   (nil)
1306                   nvals))))
1307      (t
1308       (aver (eq lvar-kind :unknown))
1309       (vop* return-multiple node block
1310             (old-fp return-pc
1311                     (reference-tn-list (ir2-lvar-locs 2lvar) nil))
1312             (nil)))))
1313
1314   (values))
1315 \f
1316 ;;;; debugger hooks
1317 ;;;;
1318 ;;;; These are used by the debugger to find the top function on the
1319 ;;;; stack. They return the OLD-FP and RETURN-PC for the current
1320 ;;;; function as multiple values.
1321
1322 (defoptimizer (%caller-frame ir2-convert) (() node block)
1323   (let ((ir2-physenv (physenv-info (node-physenv node))))
1324     (move-lvar-result node block
1325                       (list (ir2-physenv-old-fp ir2-physenv))
1326                       (node-lvar node))))
1327
1328 (defoptimizer (%caller-pc ir2-convert) (() node block)
1329   (let ((ir2-physenv (physenv-info (node-physenv node))))
1330     (move-lvar-result node block
1331                       (list (ir2-physenv-return-pc ir2-physenv))
1332                       (node-lvar node))))
1333 \f
1334 ;;;; multiple values
1335
1336 ;;; This is almost identical to IR2-CONVERT-LET. Since LTN annotates
1337 ;;; the lvar for the correct number of values (with the lvar user
1338 ;;; responsible for defaulting), we can just pick them up from the
1339 ;;; lvar.
1340 (defun ir2-convert-mv-bind (node block)
1341   (declare (type mv-combination node) (type ir2-block block))
1342   (let* ((lvar (first (basic-combination-args node)))
1343          (fun (ref-leaf (lvar-uses (basic-combination-fun node))))
1344          (vars (lambda-vars fun)))
1345     (aver (eq (functional-kind fun) :mv-let))
1346     (mapc (lambda (src var)
1347             (when (leaf-refs var)
1348               (let ((dest (leaf-info var)))
1349                 (if (and (lambda-var-indirect var)
1350                          (lambda-var-explicit-value-cell var))
1351                     (emit-make-value-cell node block src dest)
1352                     (emit-move node block src dest)))))
1353           (lvar-tns node block lvar
1354                             (mapcar (lambda (x)
1355                                       (primitive-type (leaf-type x)))
1356                                     vars))
1357           vars))
1358   (values))
1359
1360 ;;; Emit the appropriate fixed value, unknown value or tail variant of
1361 ;;; CALL-VARIABLE. Note that we only need to pass the values start for
1362 ;;; the first argument: all the other argument lvar TNs are
1363 ;;; ignored. This is because we require all of the values globs to be
1364 ;;; contiguous and on stack top.
1365 (defun ir2-convert-mv-call (node block)
1366   (declare (type mv-combination node) (type ir2-block block))
1367   (aver (basic-combination-args node))
1368   (let* ((start-lvar (lvar-info (first (basic-combination-args node))))
1369          (start (first (ir2-lvar-locs start-lvar)))
1370          (tails (and (node-tail-p node)
1371                      (lambda-tail-set (node-home-lambda node))))
1372          (lvar (node-lvar node))
1373          (2lvar (and lvar (lvar-info lvar))))
1374     (multiple-value-bind (fun named)
1375         (fun-lvar-tn node block (basic-combination-fun node))
1376       (aver (and (not named)
1377                  (eq (ir2-lvar-kind start-lvar) :unknown)))
1378       (cond
1379        (tails
1380         (let ((env (physenv-info (node-physenv node))))
1381           (vop tail-call-variable node block start fun
1382                (ir2-physenv-old-fp env)
1383                (ir2-physenv-return-pc env))))
1384        ((and 2lvar
1385              (eq (ir2-lvar-kind 2lvar) :unknown))
1386         (vop* multiple-call-variable node block (start fun nil)
1387               ((reference-tn-list (ir2-lvar-locs 2lvar) t))
1388               (emit-step-p node)))
1389        (t
1390         (let ((locs (standard-result-tns lvar)))
1391           (vop* call-variable node block (start fun nil)
1392                 ((reference-tn-list locs t)) (length locs)
1393                 (emit-step-p node))
1394           (move-lvar-result node block locs lvar)))))))
1395
1396 ;;; Reset the stack pointer to the start of the specified
1397 ;;; unknown-values lvar (discarding it and all values globs on top of
1398 ;;; it.)
1399 (defoptimizer (%pop-values ir2-convert) ((%lvar) node block)
1400   (let* ((lvar (lvar-value %lvar))
1401          (2lvar (lvar-info lvar)))
1402     (cond ((eq (ir2-lvar-kind 2lvar) :unknown)
1403            (vop reset-stack-pointer node block
1404                 (first (ir2-lvar-locs 2lvar))))
1405           ((lvar-dynamic-extent lvar)
1406            (vop reset-stack-pointer node block
1407                 (ir2-lvar-stack-pointer 2lvar)))
1408           (t (bug "Trying to pop a not stack-allocated LVAR ~S."
1409                   lvar)))))
1410
1411 (defoptimizer (%nip-values ir2-convert) ((last-nipped last-preserved
1412                                                       &rest moved)
1413                                          node block)
1414   (let* ( ;; pointer immediately after the nipped block
1415          (after (lvar-value last-nipped))
1416          (2after (lvar-info after))
1417          ;; pointer to the first nipped word
1418          (first (lvar-value last-preserved))
1419          (2first (lvar-info first))
1420
1421          (moved-tns (loop for lvar-ref in moved
1422                           for lvar = (lvar-value lvar-ref)
1423                           for 2lvar = (lvar-info lvar)
1424                                         ;when 2lvar
1425                           collect (first (ir2-lvar-locs 2lvar)))))
1426     (aver (or (eq (ir2-lvar-kind 2after) :unknown)
1427               (lvar-dynamic-extent after)))
1428     (aver (eq (ir2-lvar-kind 2first) :unknown))
1429     (when *check-consistency*
1430       ;; we cannot move stack-allocated DX objects
1431       (dolist (moved-lvar moved)
1432         (aver (eq (ir2-lvar-kind (lvar-info (lvar-value moved-lvar)))
1433                   :unknown))))
1434     (flet ((nip-aligned (nipped)
1435              (vop* %%nip-values node block
1436                    (nipped
1437                     (first (ir2-lvar-locs 2first))
1438                     (reference-tn-list moved-tns nil))
1439                    ((reference-tn-list moved-tns t)))))
1440       (cond ((eq (ir2-lvar-kind 2after) :unknown)
1441              (nip-aligned (first (ir2-lvar-locs 2after))))
1442             ((lvar-dynamic-extent after)
1443              (nip-aligned (ir2-lvar-stack-pointer 2after)))
1444             (t
1445              (bug "Trying to nip a not stack-allocated LVAR ~S." after))))))
1446
1447 ;;; Deliver the values TNs to LVAR using MOVE-LVAR-RESULT.
1448 (defoptimizer (values ir2-convert) ((&rest values) node block)
1449   (let ((tns (mapcar (lambda (x)
1450                        (lvar-tn node block x))
1451                      values)))
1452     (move-lvar-result node block tns (node-lvar node))))
1453
1454 ;;; In the normal case where unknown values are desired, we use the
1455 ;;; VALUES-LIST VOP. In the relatively unimportant case of VALUES-LIST
1456 ;;; for a fixed number of values, we punt by doing a full call to the
1457 ;;; VALUES-LIST function. This gets the full call VOP to deal with
1458 ;;; defaulting any unsupplied values. It seems unworthwhile to
1459 ;;; optimize this case.
1460 (defoptimizer (values-list ir2-convert) ((list) node block)
1461   (let* ((lvar (node-lvar node))
1462          (2lvar (and lvar (lvar-info lvar))))
1463     (cond ((and 2lvar
1464                 (eq (ir2-lvar-kind 2lvar) :unknown))
1465            (let ((locs (ir2-lvar-locs 2lvar)))
1466              (vop* values-list node block
1467                    ((lvar-tn node block list) nil)
1468                    ((reference-tn-list locs t)))))
1469           (t (aver (or (not 2lvar) ; i.e. we want to check the argument
1470                        (eq (ir2-lvar-kind 2lvar) :fixed)))
1471              (ir2-convert-full-call node block)))))
1472
1473 (defoptimizer (%more-arg-values ir2-convert) ((context start count) node block)
1474   (binding* ((lvar (node-lvar node) :exit-if-null)
1475              (2lvar (lvar-info lvar)))
1476     (ecase (ir2-lvar-kind 2lvar)
1477       (:fixed (ir2-convert-full-call node block))
1478       (:unknown
1479        (let ((locs (ir2-lvar-locs 2lvar)))
1480          (vop* %more-arg-values node block
1481                ((lvar-tn node block context)
1482                 (lvar-tn node block start)
1483                 (lvar-tn node block count)
1484                 nil)
1485                ((reference-tn-list locs t))))))))
1486 \f
1487 ;;;; special binding
1488
1489 ;;; This is trivial, given our assumption of a shallow-binding
1490 ;;; implementation.
1491 (defoptimizer (%special-bind ir2-convert) ((var value) node block)
1492   (let ((name (leaf-source-name (lvar-value var))))
1493     (vop bind node block (lvar-tn node block value)
1494          (emit-constant name))))
1495 (defoptimizer (%special-unbind ir2-convert) ((var) node block)
1496   (vop unbind node block))
1497
1498 ;;; ### It's not clear that this really belongs in this file, or
1499 ;;; should really be done this way, but this is the least violation of
1500 ;;; abstraction in the current setup. We don't want to wire
1501 ;;; shallow-binding assumptions into IR1tran.
1502 (def-ir1-translator progv
1503     ((vars vals &body body) start next result)
1504   (ir1-convert
1505    start next result
1506    (with-unique-names (bind unbind)
1507      (once-only ((n-save-bs '(%primitive current-binding-pointer)))
1508        `(unwind-protect
1509              (progn
1510                (labels ((,unbind (vars)
1511                           (declare (optimize (speed 2) (debug 0)))
1512                           (let ((unbound-marker (%primitive make-other-immediate-type
1513                                                             0 sb!vm:unbound-marker-widetag)))
1514                             (dolist (var vars)
1515                               ;; CLHS says "bound and then made to have no value" -- user
1516                               ;; should not be able to tell the difference between that and this.
1517                               (about-to-modify-symbol-value var 'progv)
1518                               (%primitive bind unbound-marker var))))
1519                         (,bind (vars vals)
1520                           (declare (optimize (speed 2) (debug 0)
1521                                              (insert-debug-catch 0)))
1522                           (cond ((null vars))
1523                                 ((null vals) (,unbind vars))
1524                                 (t
1525                                  (let ((val (car vals))
1526                                        (var (car vars)))
1527                                    (about-to-modify-symbol-value var 'progv val t)
1528                                    (%primitive bind val var))
1529                                  (,bind (cdr vars) (cdr vals))))))
1530                  (,bind ,vars ,vals))
1531                nil
1532                ,@body)
1533           ;; Technically ANSI CL doesn't allow declarations at the
1534           ;; start of the cleanup form. SBCL happens to allow for
1535           ;; them, due to the way the UNWIND-PROTECT ir1 translation
1536           ;; is implemented; the cleanup forms are directly spliced
1537           ;; into an FLET definition body. And a declaration here
1538           ;; actually has exactly the right scope for what we need
1539           ;; (ensure that debug instrumentation is not emitted for the
1540           ;; cleanup function). -- JES, 2007-06-16
1541           (declare (optimize (insert-debug-catch 0)))
1542           (%primitive unbind-to-here ,n-save-bs))))))
1543 \f
1544 ;;;; non-local exit
1545
1546 ;;; Convert a non-local lexical exit. First find the NLX-INFO in our
1547 ;;; environment. Note that this is never called on the escape exits
1548 ;;; for CATCH and UNWIND-PROTECT, since the escape functions aren't
1549 ;;; IR2 converted.
1550 (defun ir2-convert-exit (node block)
1551   (declare (type exit node) (type ir2-block block))
1552   (let* ((nlx (exit-nlx-info node))
1553          (loc (find-in-physenv nlx (node-physenv node)))
1554          (temp (make-stack-pointer-tn))
1555          (value (exit-value node)))
1556     (if (nlx-info-safe-p nlx)
1557         (vop value-cell-ref node block loc temp)
1558         (emit-move node block loc temp))
1559     (if value
1560         (let ((locs (ir2-lvar-locs (lvar-info value))))
1561           (vop unwind node block temp (first locs) (second locs)))
1562         (let ((0-tn (emit-constant 0)))
1563           (vop unwind node block temp 0-tn 0-tn))))
1564
1565   (values))
1566
1567 ;;; %CLEANUP-POINT doesn't do anything except prevent the body from
1568 ;;; being entirely deleted.
1569 (defoptimizer (%cleanup-point ir2-convert) (() node block) node block)
1570
1571 ;;; This function invalidates a lexical exit on exiting from the
1572 ;;; dynamic extent. This is done by storing 0 into the indirect value
1573 ;;; cell that holds the closed unwind block.
1574 (defoptimizer (%lexical-exit-breakup ir2-convert) ((info) node block)
1575   (let ((nlx (lvar-value info)))
1576     (when (nlx-info-safe-p nlx)
1577       (vop value-cell-set node block
1578            (find-in-physenv nlx (node-physenv node))
1579            (emit-constant 0)))))
1580
1581 ;;; We have to do a spurious move of no values to the result lvar so
1582 ;;; that lifetime analysis won't get confused.
1583 (defun ir2-convert-throw (node block)
1584   (declare (type mv-combination node) (type ir2-block block))
1585   (let ((args (basic-combination-args node)))
1586     (check-catch-tag-type (first args))
1587     (vop* throw node block
1588           ((lvar-tn node block (first args))
1589            (reference-tn-list
1590             (ir2-lvar-locs (lvar-info (second args)))
1591             nil))
1592           (nil)))
1593   (move-lvar-result node block () (node-lvar node))
1594   (values))
1595
1596 ;;; Emit code to set up a non-local exit. INFO is the NLX-INFO for the
1597 ;;; exit, and TAG is the lvar for the catch tag (if any.) We get at
1598 ;;; the target PC by passing in the label to the vop. The vop is
1599 ;;; responsible for building a return-PC object.
1600 (defun emit-nlx-start (node block info tag)
1601   (declare (type node node) (type ir2-block block) (type nlx-info info)
1602            (type (or lvar null) tag))
1603   (let* ((2info (nlx-info-info info))
1604          (kind (cleanup-kind (nlx-info-cleanup info)))
1605          (block-tn (physenv-live-tn
1606                     (make-normal-tn (primitive-type-or-lose 'catch-block))
1607                     (node-physenv node)))
1608          (res (make-stack-pointer-tn))
1609          (target-label (ir2-nlx-info-target 2info)))
1610
1611     (vop current-binding-pointer node block
1612          (car (ir2-nlx-info-dynamic-state 2info)))
1613     (vop* save-dynamic-state node block
1614           (nil)
1615           ((reference-tn-list (cdr (ir2-nlx-info-dynamic-state 2info)) t)))
1616     (vop current-stack-pointer node block (ir2-nlx-info-save-sp 2info))
1617
1618     (ecase kind
1619       (:catch
1620        (vop make-catch-block node block block-tn
1621             (lvar-tn node block tag) target-label res))
1622       ((:unwind-protect :block :tagbody)
1623        (vop make-unwind-block node block block-tn target-label res)))
1624
1625     (ecase kind
1626       ((:block :tagbody)
1627        (if (nlx-info-safe-p info)
1628            (emit-make-value-cell node block res (ir2-nlx-info-home 2info))
1629            (emit-move node block res (ir2-nlx-info-home 2info))))
1630       (:unwind-protect
1631        (vop set-unwind-protect node block block-tn))
1632       (:catch)))
1633
1634   (values))
1635
1636 ;;; Scan each of ENTRY's exits, setting up the exit for each lexical exit.
1637 (defun ir2-convert-entry (node block)
1638   (declare (type entry node) (type ir2-block block))
1639   (let ((nlxes '()))
1640     (dolist (exit (entry-exits node))
1641       (let ((info (exit-nlx-info exit)))
1642         (when (and info
1643                    (not (memq info nlxes))
1644                    (member (cleanup-kind (nlx-info-cleanup info))
1645                            '(:block :tagbody)))
1646           (push info nlxes)
1647           (emit-nlx-start node block info nil)))))
1648   (values))
1649
1650 ;;; Set up the unwind block for these guys.
1651 (defoptimizer (%catch ir2-convert) ((info-lvar tag) node block)
1652   (check-catch-tag-type tag)
1653   (emit-nlx-start node block (lvar-value info-lvar) tag))
1654 (defoptimizer (%unwind-protect ir2-convert) ((info-lvar cleanup) node block)
1655   (emit-nlx-start node block (lvar-value info-lvar) nil))
1656
1657 ;;; Emit the entry code for a non-local exit. We receive values and
1658 ;;; restore dynamic state.
1659 ;;;
1660 ;;; In the case of a lexical exit or CATCH, we look at the exit lvar's
1661 ;;; kind to determine which flavor of entry VOP to emit. If unknown
1662 ;;; values, emit the xxx-MULTIPLE variant to the lvar locs. If fixed
1663 ;;; values, make the appropriate number of temps in the standard
1664 ;;; values locations and use the other variant, delivering the temps
1665 ;;; to the lvar using MOVE-LVAR-RESULT.
1666 ;;;
1667 ;;; In the UNWIND-PROTECT case, we deliver the first register
1668 ;;; argument, the argument count and the argument pointer to our lvar
1669 ;;; as multiple values. These values are the block exited to and the
1670 ;;; values start and count.
1671 ;;;
1672 ;;; After receiving values, we restore dynamic state. Except in the
1673 ;;; UNWIND-PROTECT case, the values receiving restores the stack
1674 ;;; pointer. In an UNWIND-PROTECT cleanup, we want to leave the stack
1675 ;;; pointer alone, since the thrown values are still out there.
1676 (defoptimizer (%nlx-entry ir2-convert) ((info-lvar) node block)
1677   (let* ((info (lvar-value info-lvar))
1678          (lvar (node-lvar node))
1679          (2info (nlx-info-info info))
1680          (top-loc (ir2-nlx-info-save-sp 2info))
1681          (start-loc (make-nlx-entry-arg-start-location))
1682          (count-loc (make-arg-count-location))
1683          (target (ir2-nlx-info-target 2info)))
1684
1685     (ecase (cleanup-kind (nlx-info-cleanup info))
1686       ((:catch :block :tagbody)
1687        (let ((2lvar (and lvar (lvar-info lvar))))
1688          (if (and 2lvar (eq (ir2-lvar-kind 2lvar) :unknown))
1689              (vop* nlx-entry-multiple node block
1690                    (top-loc start-loc count-loc nil)
1691                    ((reference-tn-list (ir2-lvar-locs 2lvar) t))
1692                    target)
1693              (let ((locs (standard-result-tns lvar)))
1694                (vop* nlx-entry node block
1695                      (top-loc start-loc count-loc nil)
1696                      ((reference-tn-list locs t))
1697                      target
1698                      (length locs))
1699                (move-lvar-result node block locs lvar)))))
1700       (:unwind-protect
1701        (let ((block-loc (standard-arg-location 0)))
1702          (vop uwp-entry node block target block-loc start-loc count-loc)
1703          (move-lvar-result
1704           node block
1705           (list block-loc start-loc count-loc)
1706           lvar))))
1707
1708     #!+sb-dyncount
1709     (when *collect-dynamic-statistics*
1710       (vop count-me node block *dynamic-counts-tn*
1711            (block-number (ir2-block-block block))))
1712
1713     (vop* restore-dynamic-state node block
1714           ((reference-tn-list (cdr (ir2-nlx-info-dynamic-state 2info)) nil))
1715           (nil))
1716     (vop unbind-to-here node block
1717          (car (ir2-nlx-info-dynamic-state 2info)))))
1718 \f
1719 ;;;; n-argument functions
1720
1721 (macrolet ((def (name)
1722              `(defoptimizer (,name ir2-convert) ((&rest args) node block)
1723                 (let* ((refs (move-tail-full-call-args node block))
1724                        (lvar (node-lvar node))
1725                        (res (lvar-result-tns
1726                              lvar
1727                              (list (primitive-type (specifier-type 'list))))))
1728                   (when (and lvar (lvar-dynamic-extent lvar))
1729                     (vop current-stack-pointer node block
1730                          (ir2-lvar-stack-pointer (lvar-info lvar))))
1731                   (vop* ,name node block (refs) ((first res) nil)
1732                         (length args))
1733                   (move-lvar-result node block res lvar)))))
1734   (def list)
1735   (def list*))
1736
1737 \f
1738 ;;; Convert the code in a component into VOPs.
1739 (defun ir2-convert (component)
1740   (declare (type component component))
1741   (let (#!+sb-dyncount
1742         (*dynamic-counts-tn*
1743          (when *collect-dynamic-statistics*
1744            (let* ((blocks
1745                    (block-number (block-next (component-head component))))
1746                   (counts (make-array blocks
1747                                       :element-type '(unsigned-byte 32)
1748                                       :initial-element 0))
1749                   (info (make-dyncount-info
1750                          :for (component-name component)
1751                          :costs (make-array blocks
1752                                             :element-type '(unsigned-byte 32)
1753                                             :initial-element 0)
1754                          :counts counts)))
1755              (setf (ir2-component-dyncount-info (component-info component))
1756                    info)
1757              (emit-constant info)
1758              (emit-constant counts)))))
1759     (let ((num 0))
1760       (declare (type index num))
1761       (do-ir2-blocks (2block component)
1762         (let ((block (ir2-block-block 2block)))
1763           (when (block-start block)
1764             (setf (block-number block) num)
1765             #!+sb-dyncount
1766             (when *collect-dynamic-statistics*
1767               (let ((first-node (block-start-node block)))
1768                 (unless (or (and (bind-p first-node)
1769                                  (xep-p (bind-lambda first-node)))
1770                             (eq (lvar-fun-name
1771                                  (node-lvar first-node))
1772                                 '%nlx-entry))
1773                   (vop count-me
1774                        first-node
1775                        2block
1776                        #!+sb-dyncount *dynamic-counts-tn* #!-sb-dyncount nil
1777                        num))))
1778             (ir2-convert-block block)
1779             (incf num))))))
1780   (values))
1781
1782 ;;; If necessary, emit a terminal unconditional branch to go to the
1783 ;;; successor block. If the successor is the component tail, then
1784 ;;; there isn't really any successor, but if the end is an unknown,
1785 ;;; non-tail call, then we emit an error trap just in case the
1786 ;;; function really does return.
1787 (defun finish-ir2-block (block)
1788   (declare (type cblock block))
1789   (let* ((2block (block-info block))
1790          (last (block-last block))
1791          (succ (block-succ block)))
1792     (unless (if-p last)
1793       (aver (singleton-p succ))
1794       (let ((target (first succ)))
1795         (cond ((eq target (component-tail (block-component block)))
1796                (when (and (basic-combination-p last)
1797                           (eq (basic-combination-kind last) :full))
1798                  (let* ((fun (basic-combination-fun last))
1799                         (use (lvar-uses fun))
1800                         (name (and (ref-p use)
1801                                    (leaf-has-source-name-p (ref-leaf use))
1802                                    (leaf-source-name (ref-leaf use)))))
1803                    (unless (or (node-tail-p last)
1804                                (info :function :info name)
1805                                (policy last (zerop safety)))
1806                      (vop nil-fun-returned-error last 2block
1807                           (if name
1808                               (emit-constant name)
1809                               (multiple-value-bind (tn named)
1810                                   (fun-lvar-tn last 2block fun)
1811                                 (aver (not named))
1812                                 tn)))))))
1813               ((not (eq (ir2-block-next 2block) (block-info target)))
1814                (vop branch last 2block (block-label target)))
1815               (t
1816                (register-drop-thru target))))))
1817
1818   (values))
1819
1820 ;;; Convert the code in a block into VOPs.
1821 (defun ir2-convert-block (block)
1822   (declare (type cblock block))
1823   (let ((2block (block-info block)))
1824     (do-nodes (node lvar block)
1825       (etypecase node
1826         (ref
1827          (when lvar
1828            (let ((2lvar (lvar-info lvar)))
1829              ;; function REF in a local call is not annotated
1830              (when (and 2lvar (not (eq (ir2-lvar-kind 2lvar) :delayed)))
1831                (ir2-convert-ref node 2block)))))
1832         (combination
1833          (let ((kind (basic-combination-kind node)))
1834            (ecase kind
1835              (:local
1836               (ir2-convert-local-call node 2block))
1837              (:full
1838               (ir2-convert-full-call node 2block))
1839              (:known
1840               (let* ((info (basic-combination-fun-info node))
1841                      (fun (fun-info-ir2-convert info)))
1842                 (cond (fun
1843                        (funcall fun node 2block))
1844                       ((eq (basic-combination-info node) :full)
1845                        (ir2-convert-full-call node 2block))
1846                       (t
1847                        (ir2-convert-template node 2block))))))))
1848         (cif
1849          (when (lvar-info (if-test node))
1850            (ir2-convert-if node 2block)))
1851         (bind
1852          (let ((fun (bind-lambda node)))
1853            (when (eq (lambda-home fun) fun)
1854              (ir2-convert-bind node 2block))))
1855         (creturn
1856          (ir2-convert-return node 2block))
1857         (cset
1858          (ir2-convert-set node 2block))
1859         (cast
1860          (ir2-convert-cast node 2block))
1861         (mv-combination
1862          (cond
1863            ((eq (basic-combination-kind node) :local)
1864             (ir2-convert-mv-bind node 2block))
1865            ((eq (lvar-fun-name (basic-combination-fun node))
1866                 '%throw)
1867             (ir2-convert-throw node 2block))
1868            (t
1869             (ir2-convert-mv-call node 2block))))
1870         (exit
1871          (when (exit-entry node)
1872            (ir2-convert-exit node 2block)))
1873         (entry
1874          (ir2-convert-entry node 2block)))))
1875
1876   (finish-ir2-block block)
1877
1878   (values))