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