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