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