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