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