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