1.0.24.34: IR2: additional representation for predicates, conditional moves
[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 ;;; This is used by the debugger to find the top function on the
1258 ;;; stack. It returns the OLD-FP and RETURN-PC for the current
1259 ;;; function as multiple values.
1260 (defoptimizer (sb!kernel:%caller-frame-and-pc ir2-convert) (() node block)
1261   (let ((ir2-physenv (physenv-info (node-physenv node))))
1262     (move-lvar-result node block
1263                       (list (ir2-physenv-old-fp ir2-physenv)
1264                             (ir2-physenv-return-pc ir2-physenv))
1265                       (node-lvar node))))
1266 \f
1267 ;;;; multiple values
1268
1269 ;;; This is almost identical to IR2-CONVERT-LET. Since LTN annotates
1270 ;;; the lvar for the correct number of values (with the lvar user
1271 ;;; responsible for defaulting), we can just pick them up from the
1272 ;;; lvar.
1273 (defun ir2-convert-mv-bind (node block)
1274   (declare (type mv-combination node) (type ir2-block block))
1275   (let* ((lvar (first (basic-combination-args node)))
1276          (fun (ref-leaf (lvar-uses (basic-combination-fun node))))
1277          (vars (lambda-vars fun)))
1278     (aver (eq (functional-kind fun) :mv-let))
1279     (mapc (lambda (src var)
1280             (when (leaf-refs var)
1281               (let ((dest (leaf-info var)))
1282                 (if (lambda-var-indirect var)
1283                     (emit-make-value-cell node block src dest)
1284                     (emit-move node block src dest)))))
1285           (lvar-tns node block lvar
1286                             (mapcar (lambda (x)
1287                                       (primitive-type (leaf-type x)))
1288                                     vars))
1289           vars))
1290   (values))
1291
1292 ;;; Emit the appropriate fixed value, unknown value or tail variant of
1293 ;;; CALL-VARIABLE. Note that we only need to pass the values start for
1294 ;;; the first argument: all the other argument lvar TNs are
1295 ;;; ignored. This is because we require all of the values globs to be
1296 ;;; contiguous and on stack top.
1297 (defun ir2-convert-mv-call (node block)
1298   (declare (type mv-combination node) (type ir2-block block))
1299   (aver (basic-combination-args node))
1300   (let* ((start-lvar (lvar-info (first (basic-combination-args node))))
1301          (start (first (ir2-lvar-locs start-lvar)))
1302          (tails (and (node-tail-p node)
1303                      (lambda-tail-set (node-home-lambda node))))
1304          (lvar (node-lvar node))
1305          (2lvar (and lvar (lvar-info lvar))))
1306     (multiple-value-bind (fun named)
1307         (fun-lvar-tn node block (basic-combination-fun node))
1308       (aver (and (not named)
1309                  (eq (ir2-lvar-kind start-lvar) :unknown)))
1310       (cond
1311        (tails
1312         (let ((env (physenv-info (node-physenv node))))
1313           (vop tail-call-variable node block start fun
1314                (ir2-physenv-old-fp env)
1315                (ir2-physenv-return-pc env))))
1316        ((and 2lvar
1317              (eq (ir2-lvar-kind 2lvar) :unknown))
1318         (vop* multiple-call-variable node block (start fun nil)
1319               ((reference-tn-list (ir2-lvar-locs 2lvar) t))
1320               (emit-step-p node)))
1321        (t
1322         (let ((locs (standard-result-tns lvar)))
1323           (vop* call-variable node block (start fun nil)
1324                 ((reference-tn-list locs t)) (length locs)
1325                 (emit-step-p node))
1326           (move-lvar-result node block locs lvar)))))))
1327
1328 ;;; Reset the stack pointer to the start of the specified
1329 ;;; unknown-values lvar (discarding it and all values globs on top of
1330 ;;; it.)
1331 (defoptimizer (%pop-values ir2-convert) ((%lvar) node block)
1332   (let* ((lvar (lvar-value %lvar))
1333          (2lvar (lvar-info lvar)))
1334     (cond ((eq (ir2-lvar-kind 2lvar) :unknown)
1335            (vop reset-stack-pointer node block
1336                 (first (ir2-lvar-locs 2lvar))))
1337           ((lvar-dynamic-extent lvar)
1338            (vop reset-stack-pointer node block
1339                 (ir2-lvar-stack-pointer 2lvar)))
1340           (t (bug "Trying to pop a not stack-allocated LVAR ~S."
1341                   lvar)))))
1342
1343 (defoptimizer (%nip-values ir2-convert) ((last-nipped last-preserved
1344                                                       &rest moved)
1345                                          node block)
1346   (let* ( ;; pointer immediately after the nipped block
1347          (after (lvar-value last-nipped))
1348          (2after (lvar-info after))
1349          ;; pointer to the first nipped word
1350          (first (lvar-value last-preserved))
1351          (2first (lvar-info first))
1352
1353          (moved-tns (loop for lvar-ref in moved
1354                           for lvar = (lvar-value lvar-ref)
1355                           for 2lvar = (lvar-info lvar)
1356                                         ;when 2lvar
1357                           collect (first (ir2-lvar-locs 2lvar)))))
1358     (aver (or (eq (ir2-lvar-kind 2after) :unknown)
1359               (lvar-dynamic-extent after)))
1360     (aver (eq (ir2-lvar-kind 2first) :unknown))
1361     (when *check-consistency*
1362       ;; we cannot move stack-allocated DX objects
1363       (dolist (moved-lvar moved)
1364         (aver (eq (ir2-lvar-kind (lvar-info (lvar-value moved-lvar)))
1365                   :unknown))))
1366     (flet ((nip-aligned (nipped)
1367              (vop* %%nip-values node block
1368                    (nipped
1369                     (first (ir2-lvar-locs 2first))
1370                     (reference-tn-list moved-tns nil))
1371                    ((reference-tn-list moved-tns t)))))
1372       (cond ((eq (ir2-lvar-kind 2after) :unknown)
1373              (nip-aligned (first (ir2-lvar-locs 2after))))
1374             ((lvar-dynamic-extent after)
1375              (nip-aligned (ir2-lvar-stack-pointer 2after)))
1376             (t
1377              (bug "Trying to nip a not stack-allocated LVAR ~S." after))))))
1378
1379 ;;; Deliver the values TNs to LVAR using MOVE-LVAR-RESULT.
1380 (defoptimizer (values ir2-convert) ((&rest values) node block)
1381   (let ((tns (mapcar (lambda (x)
1382                        (lvar-tn node block x))
1383                      values)))
1384     (move-lvar-result node block tns (node-lvar node))))
1385
1386 ;;; In the normal case where unknown values are desired, we use the
1387 ;;; VALUES-LIST VOP. In the relatively unimportant case of VALUES-LIST
1388 ;;; for a fixed number of values, we punt by doing a full call to the
1389 ;;; VALUES-LIST function. This gets the full call VOP to deal with
1390 ;;; defaulting any unsupplied values. It seems unworthwhile to
1391 ;;; optimize this case.
1392 (defoptimizer (values-list ir2-convert) ((list) node block)
1393   (let* ((lvar (node-lvar node))
1394          (2lvar (and lvar (lvar-info lvar))))
1395     (cond ((and 2lvar
1396                 (eq (ir2-lvar-kind 2lvar) :unknown))
1397            (let ((locs (ir2-lvar-locs 2lvar)))
1398              (vop* values-list node block
1399                    ((lvar-tn node block list) nil)
1400                    ((reference-tn-list locs t)))))
1401           (t (aver (or (not 2lvar) ; i.e. we want to check the argument
1402                        (eq (ir2-lvar-kind 2lvar) :fixed)))
1403              (ir2-convert-full-call node block)))))
1404
1405 (defoptimizer (%more-arg-values ir2-convert) ((context start count) node block)
1406   (binding* ((lvar (node-lvar node) :exit-if-null)
1407              (2lvar (lvar-info lvar)))
1408     (ecase (ir2-lvar-kind 2lvar)
1409       (:fixed (ir2-convert-full-call node block))
1410       (:unknown
1411        (let ((locs (ir2-lvar-locs 2lvar)))
1412          (vop* %more-arg-values node block
1413                ((lvar-tn node block context)
1414                 (lvar-tn node block start)
1415                 (lvar-tn node block count)
1416                 nil)
1417                ((reference-tn-list locs t))))))))
1418 \f
1419 ;;;; special binding
1420
1421 ;;; This is trivial, given our assumption of a shallow-binding
1422 ;;; implementation.
1423 (defoptimizer (%special-bind ir2-convert) ((var value) node block)
1424   (let ((name (leaf-source-name (lvar-value var))))
1425     (vop bind node block (lvar-tn node block value)
1426          (emit-constant name))))
1427 (defoptimizer (%special-unbind ir2-convert) ((var) node block)
1428   (vop unbind node block))
1429
1430 ;;; ### It's not clear that this really belongs in this file, or
1431 ;;; should really be done this way, but this is the least violation of
1432 ;;; abstraction in the current setup. We don't want to wire
1433 ;;; shallow-binding assumptions into IR1tran.
1434 (def-ir1-translator progv
1435     ((vars vals &body body) start next result)
1436   (ir1-convert
1437    start next result
1438    (with-unique-names (bind unbind)
1439      (once-only ((n-save-bs '(%primitive current-binding-pointer)))
1440        `(unwind-protect
1441              (progn
1442                (labels ((,unbind (vars)
1443                           (declare (optimize (speed 2) (debug 0)))
1444                           (let ((unbound-marker (%primitive make-other-immediate-type
1445                                                             0 sb!vm:unbound-marker-widetag)))
1446                             (dolist (var vars)
1447                               ;; CLHS says "bound and then made to have no value" -- user
1448                               ;; should not be able to tell the difference between that and this.
1449                               (about-to-modify-symbol-value var "bind ~S")
1450                               (%primitive bind unbound-marker var))))
1451                         (,bind (vars vals)
1452                           (declare (optimize (speed 2) (debug 0)
1453                                              (insert-debug-catch 0)))
1454                           (cond ((null vars))
1455                                 ((null vals) (,unbind vars))
1456                                 (t
1457                                  (let ((val (car vals))
1458                                        (var (car vars)))
1459                                    (about-to-modify-symbol-value var "bind ~S" val)
1460                                    (%primitive bind val var))
1461                                  (,bind (cdr vars) (cdr vals))))))
1462                  (,bind ,vars ,vals))
1463                nil
1464                ,@body)
1465           ;; Technically ANSI CL doesn't allow declarations at the
1466           ;; start of the cleanup form. SBCL happens to allow for
1467           ;; them, due to the way the UNWIND-PROTECT ir1 translation
1468           ;; is implemented; the cleanup forms are directly spliced
1469           ;; into an FLET definition body. And a declaration here
1470           ;; actually has exactly the right scope for what we need
1471           ;; (ensure that debug instrumentation is not emitted for the
1472           ;; cleanup function). -- JES, 2007-06-16
1473           (declare (optimize (insert-debug-catch 0)))
1474           (%primitive unbind-to-here ,n-save-bs))))))
1475 \f
1476 ;;;; non-local exit
1477
1478 ;;; Convert a non-local lexical exit. First find the NLX-INFO in our
1479 ;;; environment. Note that this is never called on the escape exits
1480 ;;; for CATCH and UNWIND-PROTECT, since the escape functions aren't
1481 ;;; IR2 converted.
1482 (defun ir2-convert-exit (node block)
1483   (declare (type exit node) (type ir2-block block))
1484   (let* ((nlx (exit-nlx-info node))
1485          (loc (find-in-physenv nlx (node-physenv node)))
1486          (temp (make-stack-pointer-tn))
1487          (value (exit-value node)))
1488     (if (nlx-info-safe-p nlx)
1489         (vop value-cell-ref node block loc temp)
1490         (emit-move node block loc temp))
1491     (if value
1492         (let ((locs (ir2-lvar-locs (lvar-info value))))
1493           (vop unwind node block temp (first locs) (second locs)))
1494         (let ((0-tn (emit-constant 0)))
1495           (vop unwind node block temp 0-tn 0-tn))))
1496
1497   (values))
1498
1499 ;;; %CLEANUP-POINT doesn't do anything except prevent the body from
1500 ;;; being entirely deleted.
1501 (defoptimizer (%cleanup-point ir2-convert) (() node block) node block)
1502
1503 ;;; This function invalidates a lexical exit on exiting from the
1504 ;;; dynamic extent. This is done by storing 0 into the indirect value
1505 ;;; cell that holds the closed unwind block.
1506 (defoptimizer (%lexical-exit-breakup ir2-convert) ((info) node block)
1507   (let ((nlx (lvar-value info)))
1508     (when (nlx-info-safe-p nlx)
1509       (vop value-cell-set node block
1510            (find-in-physenv nlx (node-physenv node))
1511            (emit-constant 0)))))
1512
1513 ;;; We have to do a spurious move of no values to the result lvar so
1514 ;;; that lifetime analysis won't get confused.
1515 (defun ir2-convert-throw (node block)
1516   (declare (type mv-combination node) (type ir2-block block))
1517   (let ((args (basic-combination-args node)))
1518     (check-catch-tag-type (first args))
1519     (vop* throw node block
1520           ((lvar-tn node block (first args))
1521            (reference-tn-list
1522             (ir2-lvar-locs (lvar-info (second args)))
1523             nil))
1524           (nil)))
1525   (move-lvar-result node block () (node-lvar node))
1526   (values))
1527
1528 ;;; Emit code to set up a non-local exit. INFO is the NLX-INFO for the
1529 ;;; exit, and TAG is the lvar for the catch tag (if any.) We get at
1530 ;;; the target PC by passing in the label to the vop. The vop is
1531 ;;; responsible for building a return-PC object.
1532 (defun emit-nlx-start (node block info tag)
1533   (declare (type node node) (type ir2-block block) (type nlx-info info)
1534            (type (or lvar null) tag))
1535   (let* ((2info (nlx-info-info info))
1536          (kind (cleanup-kind (nlx-info-cleanup info)))
1537          (block-tn (physenv-live-tn
1538                     (make-normal-tn (primitive-type-or-lose 'catch-block))
1539                     (node-physenv node)))
1540          (res (make-stack-pointer-tn))
1541          (target-label (ir2-nlx-info-target 2info)))
1542
1543     (vop current-binding-pointer node block
1544          (car (ir2-nlx-info-dynamic-state 2info)))
1545     (vop* save-dynamic-state node block
1546           (nil)
1547           ((reference-tn-list (cdr (ir2-nlx-info-dynamic-state 2info)) t)))
1548     (vop current-stack-pointer node block (ir2-nlx-info-save-sp 2info))
1549
1550     (ecase kind
1551       (:catch
1552        (vop make-catch-block node block block-tn
1553             (lvar-tn node block tag) target-label res))
1554       ((:unwind-protect :block :tagbody)
1555        (vop make-unwind-block node block block-tn target-label res)))
1556
1557     (ecase kind
1558       ((:block :tagbody)
1559        (if (nlx-info-safe-p info)
1560            (emit-make-value-cell node block res (ir2-nlx-info-home 2info))
1561            (emit-move node block res (ir2-nlx-info-home 2info))))
1562       (:unwind-protect
1563        (vop set-unwind-protect node block block-tn))
1564       (:catch)))
1565
1566   (values))
1567
1568 ;;; Scan each of ENTRY's exits, setting up the exit for each lexical exit.
1569 (defun ir2-convert-entry (node block)
1570   (declare (type entry node) (type ir2-block block))
1571   (let ((nlxes '()))
1572     (dolist (exit (entry-exits node))
1573       (let ((info (exit-nlx-info exit)))
1574         (when (and info
1575                    (not (memq info nlxes))
1576                    (member (cleanup-kind (nlx-info-cleanup info))
1577                            '(:block :tagbody)))
1578           (push info nlxes)
1579           (emit-nlx-start node block info nil)))))
1580   (values))
1581
1582 ;;; Set up the unwind block for these guys.
1583 (defoptimizer (%catch ir2-convert) ((info-lvar tag) node block)
1584   (check-catch-tag-type tag)
1585   (emit-nlx-start node block (lvar-value info-lvar) tag))
1586 (defoptimizer (%unwind-protect ir2-convert) ((info-lvar cleanup) node block)
1587   (emit-nlx-start node block (lvar-value info-lvar) nil))
1588
1589 ;;; Emit the entry code for a non-local exit. We receive values and
1590 ;;; restore dynamic state.
1591 ;;;
1592 ;;; In the case of a lexical exit or CATCH, we look at the exit lvar's
1593 ;;; kind to determine which flavor of entry VOP to emit. If unknown
1594 ;;; values, emit the xxx-MULTIPLE variant to the lvar locs. If fixed
1595 ;;; values, make the appropriate number of temps in the standard
1596 ;;; values locations and use the other variant, delivering the temps
1597 ;;; to the lvar using MOVE-LVAR-RESULT.
1598 ;;;
1599 ;;; In the UNWIND-PROTECT case, we deliver the first register
1600 ;;; argument, the argument count and the argument pointer to our lvar
1601 ;;; as multiple values. These values are the block exited to and the
1602 ;;; values start and count.
1603 ;;;
1604 ;;; After receiving values, we restore dynamic state. Except in the
1605 ;;; UNWIND-PROTECT case, the values receiving restores the stack
1606 ;;; pointer. In an UNWIND-PROTECT cleanup, we want to leave the stack
1607 ;;; pointer alone, since the thrown values are still out there.
1608 (defoptimizer (%nlx-entry ir2-convert) ((info-lvar) node block)
1609   (let* ((info (lvar-value info-lvar))
1610          (lvar (node-lvar node))
1611          (2info (nlx-info-info info))
1612          (top-loc (ir2-nlx-info-save-sp 2info))
1613          (start-loc (make-nlx-entry-arg-start-location))
1614          (count-loc (make-arg-count-location))
1615          (target (ir2-nlx-info-target 2info)))
1616
1617     (ecase (cleanup-kind (nlx-info-cleanup info))
1618       ((:catch :block :tagbody)
1619        (let ((2lvar (and lvar (lvar-info lvar))))
1620          (if (and 2lvar (eq (ir2-lvar-kind 2lvar) :unknown))
1621              (vop* nlx-entry-multiple node block
1622                    (top-loc start-loc count-loc nil)
1623                    ((reference-tn-list (ir2-lvar-locs 2lvar) t))
1624                    target)
1625              (let ((locs (standard-result-tns lvar)))
1626                (vop* nlx-entry node block
1627                      (top-loc start-loc count-loc nil)
1628                      ((reference-tn-list locs t))
1629                      target
1630                      (length locs))
1631                (move-lvar-result node block locs lvar)))))
1632       (:unwind-protect
1633        (let ((block-loc (standard-arg-location 0)))
1634          (vop uwp-entry node block target block-loc start-loc count-loc)
1635          (move-lvar-result
1636           node block
1637           (list block-loc start-loc count-loc)
1638           lvar))))
1639
1640     #!+sb-dyncount
1641     (when *collect-dynamic-statistics*
1642       (vop count-me node block *dynamic-counts-tn*
1643            (block-number (ir2-block-block block))))
1644
1645     (vop* restore-dynamic-state node block
1646           ((reference-tn-list (cdr (ir2-nlx-info-dynamic-state 2info)) nil))
1647           (nil))
1648     (vop unbind-to-here node block
1649          (car (ir2-nlx-info-dynamic-state 2info)))))
1650 \f
1651 ;;;; n-argument functions
1652
1653 (macrolet ((def (name)
1654              `(defoptimizer (,name ir2-convert) ((&rest args) node block)
1655                 (let* ((refs (move-tail-full-call-args node block))
1656                        (lvar (node-lvar node))
1657                        (res (lvar-result-tns
1658                              lvar
1659                              (list (primitive-type (specifier-type 'list))))))
1660                   (when (and lvar (lvar-dynamic-extent lvar))
1661                     (vop current-stack-pointer node block
1662                          (ir2-lvar-stack-pointer (lvar-info lvar))))
1663                   (vop* ,name node block (refs) ((first res) nil)
1664                         (length args))
1665                   (move-lvar-result node block res lvar)))))
1666   (def list)
1667   (def list*))
1668
1669 \f
1670 ;;; Convert the code in a component into VOPs.
1671 (defun ir2-convert (component)
1672   (declare (type component component))
1673   (let (#!+sb-dyncount
1674         (*dynamic-counts-tn*
1675          (when *collect-dynamic-statistics*
1676            (let* ((blocks
1677                    (block-number (block-next (component-head component))))
1678                   (counts (make-array blocks
1679                                       :element-type '(unsigned-byte 32)
1680                                       :initial-element 0))
1681                   (info (make-dyncount-info
1682                          :for (component-name component)
1683                          :costs (make-array blocks
1684                                             :element-type '(unsigned-byte 32)
1685                                             :initial-element 0)
1686                          :counts counts)))
1687              (setf (ir2-component-dyncount-info (component-info component))
1688                    info)
1689              (emit-constant info)
1690              (emit-constant counts)))))
1691     (let ((num 0))
1692       (declare (type index num))
1693       (do-ir2-blocks (2block component)
1694         (let ((block (ir2-block-block 2block)))
1695           (when (block-start block)
1696             (setf (block-number block) num)
1697             #!+sb-dyncount
1698             (when *collect-dynamic-statistics*
1699               (let ((first-node (block-start-node block)))
1700                 (unless (or (and (bind-p first-node)
1701                                  (xep-p (bind-lambda first-node)))
1702                             (eq (lvar-fun-name
1703                                  (node-lvar first-node))
1704                                 '%nlx-entry))
1705                   (vop count-me
1706                        first-node
1707                        2block
1708                        #!+sb-dyncount *dynamic-counts-tn* #!-sb-dyncount nil
1709                        num))))
1710             (ir2-convert-block block)
1711             (incf num))))))
1712   (values))
1713
1714 ;;; If necessary, emit a terminal unconditional branch to go to the
1715 ;;; successor block. If the successor is the component tail, then
1716 ;;; there isn't really any successor, but if the end is an unknown,
1717 ;;; non-tail call, then we emit an error trap just in case the
1718 ;;; function really does return.
1719 (defun finish-ir2-block (block)
1720   (declare (type cblock block))
1721   (let* ((2block (block-info block))
1722          (last (block-last block))
1723          (succ (block-succ block)))
1724     (unless (if-p last)
1725       (aver (singleton-p succ))
1726       (let ((target (first succ)))
1727         (cond ((eq target (component-tail (block-component block)))
1728                (when (and (basic-combination-p last)
1729                           (eq (basic-combination-kind last) :full))
1730                  (let* ((fun (basic-combination-fun last))
1731                         (use (lvar-uses fun))
1732                         (name (and (ref-p use)
1733                                    (leaf-has-source-name-p (ref-leaf use))
1734                                    (leaf-source-name (ref-leaf use)))))
1735                    (unless (or (node-tail-p last)
1736                                (info :function :info name)
1737                                (policy last (zerop safety)))
1738                      (vop nil-fun-returned-error last 2block
1739                           (if name
1740                               (emit-constant name)
1741                               (multiple-value-bind (tn named)
1742                                   (fun-lvar-tn last 2block fun)
1743                                 (aver (not named))
1744                                 tn)))))))
1745               ((not (eq (ir2-block-next 2block) (block-info target)))
1746                (vop branch last 2block (block-label target)))))))
1747
1748   (values))
1749
1750 ;;; Convert the code in a block into VOPs.
1751 (defun ir2-convert-block (block)
1752   (declare (type cblock block))
1753   (let ((2block (block-info block)))
1754     (do-nodes (node lvar block)
1755       (etypecase node
1756         (ref
1757          (when lvar
1758            (let ((2lvar (lvar-info lvar)))
1759              ;; function REF in a local call is not annotated
1760              (when (and 2lvar (not (eq (ir2-lvar-kind 2lvar) :delayed)))
1761                (ir2-convert-ref node 2block)))))
1762         (combination
1763          (let ((kind (basic-combination-kind node)))
1764            (ecase kind
1765              (:local
1766               (ir2-convert-local-call node 2block))
1767              (:full
1768               (ir2-convert-full-call node 2block))
1769              (:known
1770               (let* ((info (basic-combination-fun-info node))
1771                      (fun (fun-info-ir2-convert info)))
1772                 (cond (fun
1773                        (funcall fun node 2block))
1774                       ((eq (basic-combination-info node) :full)
1775                        (ir2-convert-full-call node 2block))
1776                       (t
1777                        (ir2-convert-template node 2block))))))))
1778         (cif
1779          (when (lvar-info (if-test node))
1780            (ir2-convert-if node 2block)))
1781         (bind
1782          (let ((fun (bind-lambda node)))
1783            (when (eq (lambda-home fun) fun)
1784              (ir2-convert-bind node 2block))))
1785         (creturn
1786          (ir2-convert-return node 2block))
1787         (cset
1788          (ir2-convert-set node 2block))
1789         (cast
1790          (ir2-convert-cast node 2block))
1791         (mv-combination
1792          (cond
1793            ((eq (basic-combination-kind node) :local)
1794             (ir2-convert-mv-bind node 2block))
1795            ((eq (lvar-fun-name (basic-combination-fun node))
1796                 '%throw)
1797             (ir2-convert-throw node 2block))
1798            (t
1799             (ir2-convert-mv-call node 2block))))
1800         (exit
1801          (when (exit-entry node)
1802            (ir2-convert-exit node 2block)))
1803         (entry
1804          (ir2-convert-entry node 2block)))))
1805
1806   (finish-ir2-block block)
1807
1808   (values))