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