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