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