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