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