1 ;;;; This file contains the virtual-machine-independent parts of the
2 ;;;; code which does the actual translation of nodes to VOPs.
4 ;;;; This software is part of the SBCL system. See the README file for
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.
15 ;;;; moves and type checks
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))
21 (vop move node block x y))
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)
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)
38 (primitive-type-check check-ptype)
39 (let ((name (hairy-type-check-template-name type)))
41 (template-or-lose name)
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
49 (defun emit-type-check (node block value result type)
50 (declare (type tn value result) (type node node) (type ir2-block block)
52 (emit-move-template node block (type-check-template type) value result)
55 ;;; Allocate an indirect value cell.
56 (defevent make-value-cell-event "Allocate heap value cell for lexical var.")
57 (defun emit-make-value-cell (node block value res)
58 (event make-value-cell-event node)
59 (let* ((leaf (tn-leaf res))
60 (dx (when leaf (leaf-dynamic-extent leaf))))
61 (when (and dx (neq :truly dx) (leaf-has-source-name-p leaf))
62 (compiler-notify "cannot stack allocate value cell for ~S" (leaf-source-name leaf)))
63 (vop make-value-cell node block value
70 ;;; Return the TN that holds the value of THING in the environment ENV.
71 (declaim (ftype (function ((or nlx-info lambda-var clambda) physenv) tn)
73 (defun find-in-physenv (thing physenv)
74 (or (cdr (assoc thing (ir2-physenv-closure (physenv-info physenv))))
77 ;; I think that a failure of this assertion means that we're
78 ;; trying to access a variable which was improperly closed
79 ;; over. The PHYSENV describes a physical environment. Every
80 ;; variable that a form refers to should either be in its
81 ;; physical environment directly, or grabbed from a
82 ;; surrounding physical environment when it was closed over.
83 ;; The ASSOC expression above finds closed-over variables, so
84 ;; if we fell through the ASSOC expression, it wasn't closed
85 ;; over. Therefore, it must be in our physical environment
86 ;; directly. If instead it is in some other physical
87 ;; environment, then it's bogus for us to reference it here
88 ;; without it being closed over. -- WHN 2001-09-29
89 (aver (eq physenv (lambda-physenv (lambda-var-home thing))))
92 (aver (eq physenv (block-physenv (nlx-info-target thing))))
93 (ir2-nlx-info-home (nlx-info-info thing)))
96 (entry-info-closure-tn (lambda-info thing))))
97 (bug "~@<~2I~_~S ~_not found in ~_~S~:>" thing physenv)))
99 ;;; If LEAF already has a constant TN, return that, otherwise make a
101 (defun constant-tn (leaf)
102 (declare (type constant leaf))
104 (setf (leaf-info leaf)
105 (make-constant-tn leaf))))
107 ;;; Return a TN that represents the value of LEAF, or NIL if LEAF
108 ;;; isn't directly represented by a TN. ENV is the environment that
109 ;;; the reference is done in.
110 (defun leaf-tn (leaf env)
111 (declare (type leaf leaf) (type physenv env))
114 (unless (lambda-var-indirect leaf)
115 (find-in-physenv leaf env)))
116 (constant (constant-tn leaf))
119 ;;; This is used to conveniently get a handle on a constant TN during
120 ;;; IR2 conversion. It returns a constant TN representing the Lisp
122 (defun emit-constant (value)
123 (constant-tn (find-constant value)))
125 ;;; Convert a REF node. The reference must not be delayed.
126 (defun ir2-convert-ref (node block)
127 (declare (type ref node) (type ir2-block block))
128 (let* ((lvar (node-lvar node))
129 (leaf (ref-leaf node))
130 (locs (lvar-result-tns
131 lvar (list (primitive-type (leaf-type leaf)))))
135 (let ((tn (find-in-physenv leaf (node-physenv node)))
136 (indirect (lambda-var-indirect leaf))
137 (explicit (lambda-var-explicit-value-cell leaf)))
139 ((and indirect explicit)
140 (vop value-cell-ref node block tn res))
142 (not (eq (node-physenv node)
143 (lambda-physenv (lambda-var-home leaf)))))
144 (vop ancestor-frame-ref node block tn (leaf-info leaf) res))
145 (t (emit-move node block tn res)))))
147 (emit-move node block (constant-tn leaf) res))
149 (ir2-convert-closure node block leaf res))
151 (let ((unsafe (policy node (zerop safety)))
152 (name (leaf-source-name leaf)))
153 (ecase (global-var-kind leaf)
155 (aver (symbolp name))
156 (let ((name-tn (emit-constant name)))
157 (if (or unsafe (info :variable :always-bound name))
158 (vop fast-symbol-value node block name-tn res)
159 (vop symbol-value node block name-tn res))))
161 (aver (symbolp name))
162 (let ((name-tn (emit-constant name)))
163 (if (or unsafe (info :variable :always-bound name))
164 (vop fast-symbol-global-value node block name-tn res)
165 (vop symbol-global-value node block name-tn res))))
167 (let ((fdefn-tn (make-load-time-constant-tn :fdefinition name)))
169 (vop fdefn-fun node block fdefn-tn res)
170 (vop safe-fdefn-fun node block fdefn-tn res))))))))
171 (move-lvar-result node block locs lvar))
174 ;;; some sanity checks for a CLAMBDA passed to IR2-CONVERT-CLOSURE
175 (defun assertions-on-ir2-converted-clambda (clambda)
176 ;; This assertion was sort of an experiment. It would be nice and
177 ;; sane and easier to understand things if it were *always* true,
178 ;; but experimentally I observe that it's only *almost* always
179 ;; true. -- WHN 2001-01-02
181 (aver (eql (lambda-component clambda)
182 (block-component (ir2-block-block ir2-block))))
183 ;; Check for some weirdness which came up in bug
186 ;; The MAKE-LOAD-TIME-CONSTANT-TN call above puts an :ENTRY record
187 ;; into the IR2-COMPONENT-CONSTANTS table. The dump-a-COMPONENT
189 ;; * treats every HANDLEless :ENTRY record into a
191 ;; * expects every patch to correspond to an
192 ;; IR2-COMPONENT-ENTRIES record.
193 ;; The IR2-COMPONENT-ENTRIES records are set by ENTRY-ANALYZE
194 ;; walking over COMPONENT-LAMBDAS. Bug 138b arose because there
195 ;; was a HANDLEless :ENTRY record which didn't correspond to an
196 ;; IR2-COMPONENT-ENTRIES record. That problem is hard to debug
197 ;; when it's caught at dump time, so this assertion tries to catch
199 (aver (member clambda
200 (component-lambdas (lambda-component clambda))))
201 ;; another bug-138-related issue: COMPONENT-NEW-FUNCTIONALS is
202 ;; used as a queue for stuff pending to do in IR1, and now that
203 ;; we're doing IR2 it should've been completely flushed (but
205 (aver (null (component-new-functionals (lambda-component clambda))))
208 ;;; Emit code to load a function object implementing FUNCTIONAL into
209 ;;; RES. This gets interesting when the referenced function is a
210 ;;; closure: we must make the closure and move the closed-over values
213 ;;; FUNCTIONAL is either a :TOPLEVEL-XEP functional or the XEP lambda
214 ;;; for the called function, since local call analysis converts all
215 ;;; closure references. If a :TOPLEVEL-XEP, we know it is not a
218 ;;; If a closed-over LAMBDA-VAR has no refs (is deleted), then we
219 ;;; don't initialize that slot. This can happen with closures over
220 ;;; top level variables, where optimization of the closure deleted the
221 ;;; variable. Since we committed to the closure format when we
222 ;;; pre-analyzed the top level code, we just leave an empty slot.
223 (defun ir2-convert-closure (ref ir2-block functional res)
224 (declare (type ref ref)
225 (type ir2-block ir2-block)
226 (type functional functional)
228 (aver (not (eql (functional-kind functional) :deleted)))
229 (unless (leaf-info functional)
230 (setf (leaf-info functional)
231 (make-entry-info :name (functional-debug-name functional))))
232 (let ((closure (etypecase functional
234 (assertions-on-ir2-converted-clambda functional)
235 (physenv-closure (get-lambda-physenv functional)))
237 (aver (eq (functional-kind functional) :toplevel-xep))
241 (let* ((physenv (node-physenv ref))
242 (tn (find-in-physenv functional physenv)))
243 (emit-move ref ir2-block tn res)))
245 (let ((entry (make-load-time-constant-tn :entry functional)))
246 (emit-move ref ir2-block entry res)))))
249 (defun closure-initial-value (what this-env current-fp)
250 (declare (type (or nlx-info lambda-var clambda) what)
251 (type physenv this-env)
252 (type (or tn null) current-fp))
253 ;; If we have an indirect LAMBDA-VAR that does not require an
254 ;; EXPLICIT-VALUE-CELL, and is from this environment (not from being
255 ;; closed over), we need to store the current frame pointer.
256 (if (and (lambda-var-p what)
257 (lambda-var-indirect what)
258 (not (lambda-var-explicit-value-cell what))
259 (eq (lambda-physenv (lambda-var-home what))
262 (find-in-physenv what this-env)))
264 (defoptimizer (%allocate-closures ltn-annotate) ((leaves) node ltn-policy)
265 ltn-policy ; a hack to effectively (DECLARE (IGNORE LTN-POLICY))
266 (when (lvar-dynamic-extent leaves)
267 (let ((info (make-ir2-lvar *backend-t-primitive-type*)))
268 (setf (ir2-lvar-kind info) :delayed)
269 (setf (lvar-info leaves) info)
270 (setf (ir2-lvar-stack-pointer info)
271 (make-stack-pointer-tn)))))
273 (defoptimizer (%allocate-closures ir2-convert) ((leaves) call 2block)
274 (let ((dx-p (lvar-dynamic-extent leaves)))
277 (vop current-stack-pointer call 2block
278 (ir2-lvar-stack-pointer (lvar-info leaves))))
279 (dolist (leaf (lvar-value leaves))
280 (binding* ((xep (awhen (functional-entry-fun leaf)
281 ;; if the xep's been deleted then we can skip it
282 (if (eq (functional-kind it) :deleted)
285 (nil (aver (xep-p xep)))
286 (entry-info (lambda-info xep) :exit-if-null)
287 (tn (entry-info-closure-tn entry-info) :exit-if-null)
288 (closure (physenv-closure (get-lambda-physenv xep)))
289 (entry (make-load-time-constant-tn :entry xep)))
290 (let ((this-env (node-physenv call))
291 (leaf-dx-p (and dx-p (leaf-dynamic-extent leaf))))
292 (vop make-closure call 2block entry (length closure)
294 (loop for what in closure and n from 0 do
295 (unless (and (lambda-var-p what)
296 (null (leaf-refs what)))
297 ;; In LABELS a closure may refer to another closure
298 ;; in the same group, so we must be sure that we
299 ;; store a closure only after its creation.
301 ;; TODO: Here is a simple solution: we postpone
302 ;; putting of all closures after all creations
303 ;; (though it may require more registers).
305 (delayed (list tn (find-in-physenv what this-env) n))
306 (let ((initial-value (closure-initial-value
309 (vop closure-init call 2block
311 ;; An initial-value of NIL means to stash
312 ;; the frame pointer... which requires a
314 (vop closure-init-from-fp call 2block tn n)))))))))
315 (loop for (tn what n) in (delayed)
316 do (vop closure-init call 2block
320 ;;; Convert a SET node. If the NODE's LVAR is annotated, then we also
321 ;;; deliver the value to that lvar. If the var is a lexical variable
322 ;;; with no refs, then we don't actually set anything, since the
323 ;;; variable has been deleted.
324 (defun ir2-convert-set (node block)
325 (declare (type cset node) (type ir2-block block))
326 (let* ((lvar (node-lvar node))
327 (leaf (set-var node))
328 (val (lvar-tn node block (set-value node)))
331 lvar (list (primitive-type (leaf-type leaf))))
335 (when (leaf-refs leaf)
336 (let ((tn (find-in-physenv leaf (node-physenv node)))
337 (indirect (lambda-var-indirect leaf))
338 (explicit (lambda-var-explicit-value-cell leaf)))
340 ((and indirect explicit)
341 (vop value-cell-set node block tn val))
343 (not (eq (node-physenv node)
344 (lambda-physenv (lambda-var-home leaf)))))
345 (vop ancestor-frame-set node block tn val (leaf-info leaf)))
346 (t (emit-move node block val tn))))))
348 (aver (symbolp (leaf-source-name leaf)))
349 (ecase (global-var-kind leaf)
351 (vop set node block (emit-constant (leaf-source-name leaf)) val))
353 (vop %set-symbol-global-value node
354 block (emit-constant (leaf-source-name leaf)) val)))))
356 (emit-move node block val (first locs))
357 (move-lvar-result node block locs lvar)))
360 ;;;; utilities for receiving fixed values
362 ;;; Return a TN that can be referenced to get the value of LVAR. LVAR
363 ;;; must be LTN-ANNOTATED either as a delayed leaf ref or as a fixed,
364 ;;; single-value lvar.
366 ;;; The primitive-type of the result will always be the same as the
367 ;;; IR2-LVAR-PRIMITIVE-TYPE, ensuring that VOPs are always called with
368 ;;; TNs that satisfy the operand primitive-type restriction. We may
369 ;;; have to make a temporary of the desired type and move the actual
370 ;;; lvar TN into it. This happens when we delete a type check in
371 ;;; unsafe code or when we locally know something about the type of an
372 ;;; argument variable.
373 (defun lvar-tn (node block lvar)
374 (declare (type node node) (type ir2-block block) (type lvar lvar))
375 (let* ((2lvar (lvar-info lvar))
377 (ecase (ir2-lvar-kind 2lvar)
379 (let ((ref (lvar-uses lvar)))
380 (leaf-tn (ref-leaf ref) (node-physenv ref))))
382 (aver (= (length (ir2-lvar-locs 2lvar)) 1))
383 (first (ir2-lvar-locs 2lvar)))))
384 (ptype (ir2-lvar-primitive-type 2lvar)))
386 (cond ((eq (tn-primitive-type lvar-tn) ptype) lvar-tn)
388 (let ((temp (make-normal-tn ptype)))
389 (emit-move node block lvar-tn temp)
392 ;;; This is similar to LVAR-TN, but hacks multiple values. We return
393 ;;; TNs holding the values of LVAR with PTYPES as their primitive
394 ;;; types. LVAR must be annotated for the same number of fixed values
395 ;;; are there are PTYPES.
397 ;;; If the lvar has a type check, check the values into temps and
398 ;;; return the temps. When we have more values than assertions, we
399 ;;; move the extra values with no check.
400 (defun lvar-tns (node block lvar ptypes)
401 (declare (type node node) (type ir2-block block)
402 (type lvar lvar) (list ptypes))
403 (let* ((locs (ir2-lvar-locs (lvar-info lvar)))
404 (nlocs (length locs)))
405 (aver (= nlocs (length ptypes)))
407 (mapcar (lambda (from to-type)
408 (if (eq (tn-primitive-type from) to-type)
410 (let ((temp (make-normal-tn to-type)))
411 (emit-move node block from temp)
416 ;;;; utilities for delivering values to lvars
418 ;;; Return a list of TNs with the specifier TYPES that can be used as
419 ;;; result TNs to evaluate an expression into LVAR. This is used
420 ;;; together with MOVE-LVAR-RESULT to deliver fixed values to
423 ;;; If the lvar isn't annotated (meaning the values are discarded) or
424 ;;; is unknown-values, the then we make temporaries for each supplied
425 ;;; value, providing a place to compute the result in until we decide
426 ;;; what to do with it (if anything.)
428 ;;; If the lvar is fixed-values, and wants the same number of values
429 ;;; as the user wants to deliver, then we just return the
430 ;;; IR2-LVAR-LOCS. Otherwise we make a new list padded as necessary by
431 ;;; discarded TNs. We always return a TN of the specified type, using
432 ;;; the lvar locs only when they are of the correct type.
433 (defun lvar-result-tns (lvar types)
434 (declare (type (or lvar null) lvar) (type list types))
436 (mapcar #'make-normal-tn types)
437 (let ((2lvar (lvar-info lvar)))
438 (ecase (ir2-lvar-kind 2lvar)
440 (let* ((locs (ir2-lvar-locs 2lvar))
441 (nlocs (length locs))
442 (ntypes (length types)))
443 (if (and (= nlocs ntypes)
444 (do ((loc locs (cdr loc))
445 (type types (cdr type)))
447 (unless (eq (tn-primitive-type (car loc)) (car type))
450 (mapcar (lambda (loc type)
451 (if (eq (tn-primitive-type loc) type)
453 (make-normal-tn type)))
456 (mapcar #'make-normal-tn
457 (subseq types nlocs)))
461 (mapcar #'make-normal-tn types))))))
463 ;;; Make the first N standard value TNs, returning them in a list.
464 (defun make-standard-value-tns (n)
465 (declare (type unsigned-byte n))
468 (res (standard-arg-location i)))
471 ;;; Return a list of TNs wired to the standard value passing
472 ;;; conventions that can be used to receive values according to the
473 ;;; unknown-values convention. This is used with together
474 ;;; MOVE-LVAR-RESULT for delivering unknown values to a fixed values
477 ;;; If the lvar isn't annotated, then we treat as 0-values, returning
478 ;;; an empty list of temporaries.
480 ;;; If the lvar is annotated, then it must be :FIXED.
481 (defun standard-result-tns (lvar)
482 (declare (type (or lvar null) lvar))
484 (let ((2lvar (lvar-info lvar)))
485 (ecase (ir2-lvar-kind 2lvar)
487 (make-standard-value-tns (length (ir2-lvar-locs 2lvar))))))
490 ;;; Just move each SRC TN into the corresponding DEST TN, defaulting
491 ;;; any unsupplied source values to NIL. We let EMIT-MOVE worry about
492 ;;; doing the appropriate coercions.
493 (defun move-results-coerced (node block src dest)
494 (declare (type node node) (type ir2-block block) (list src dest))
495 (let ((nsrc (length src))
496 (ndest (length dest)))
497 (mapc (lambda (from to)
499 (emit-move node block from to)))
501 (append src (make-list (- ndest nsrc)
502 :initial-element (emit-constant nil)))
507 ;;; Move each SRC TN into the corresponding DEST TN, checking types
508 ;;; and defaulting any unsupplied source values to NIL
509 (defun move-results-checked (node block src dest types)
510 (declare (type node node) (type ir2-block block) (list src dest types))
511 (let ((nsrc (length src))
512 (ndest (length dest))
513 (ntypes (length types)))
514 (mapc (lambda (from to type)
516 (emit-type-check node block from to type)
517 (emit-move node block from to)))
519 (append src (make-list (- ndest nsrc)
520 :initial-element (emit-constant nil)))
524 (append types (make-list (- ndest ntypes)))
528 ;;; If necessary, emit coercion code needed to deliver the RESULTS to
529 ;;; the specified lvar. NODE and BLOCK provide context for emitting
530 ;;; code. Although usually obtained from STANDARD-RESULT-TNs or
531 ;;; LVAR-RESULT-TNs, RESULTS my be a list of any type or
534 ;;; If the lvar is fixed values, then move the results into the lvar
535 ;;; locations. If the lvar is unknown values, then do the moves into
536 ;;; the standard value locations, and use PUSH-VALUES to put the
537 ;;; values on the stack.
538 (defun move-lvar-result (node block results lvar)
539 (declare (type node node) (type ir2-block block)
540 (list results) (type (or lvar null) lvar))
542 (let ((2lvar (lvar-info lvar)))
543 (ecase (ir2-lvar-kind 2lvar)
545 (let ((locs (ir2-lvar-locs 2lvar)))
546 (unless (eq locs results)
547 (move-results-coerced node block results locs))))
549 (let* ((nvals (length results))
550 (locs (make-standard-value-tns nvals)))
551 (move-results-coerced node block results locs)
552 (vop* push-values node block
553 ((reference-tn-list locs nil))
554 ((reference-tn-list (ir2-lvar-locs 2lvar) t))
559 (defun ir2-convert-cast (node block)
560 (declare (type cast node)
561 (type ir2-block block))
562 (binding* ((lvar (node-lvar node) :exit-if-null)
563 (2lvar (lvar-info lvar))
564 (value (cast-value node))
565 (2value (lvar-info value)))
566 (cond ((eq (ir2-lvar-kind 2lvar) :unused))
567 ((eq (ir2-lvar-kind 2lvar) :unknown)
568 (aver (eq (ir2-lvar-kind 2value) :unknown))
569 (aver (not (cast-type-check node)))
570 (move-results-coerced node block
571 (ir2-lvar-locs 2value)
572 (ir2-lvar-locs 2lvar)))
573 ((eq (ir2-lvar-kind 2lvar) :fixed)
574 (aver (eq (ir2-lvar-kind 2value) :fixed))
575 (if (cast-type-check node)
576 (move-results-checked node block
577 (ir2-lvar-locs 2value)
578 (ir2-lvar-locs 2lvar)
579 (multiple-value-bind (check types)
580 (cast-check-types node nil)
581 (aver (eq check :simple))
583 (move-results-coerced node block
584 (ir2-lvar-locs 2value)
585 (ir2-lvar-locs 2lvar))))
586 (t (bug "CAST cannot be :DELAYED.")))))
588 ;;;; template conversion
590 ;;; Build a TN-REFS list that represents access to the values of the
591 ;;; specified list of lvars ARGS for TEMPLATE. Any :CONSTANT arguments
592 ;;; are returned in the second value as a list rather than being
593 ;;; accessed as a normal argument. NODE and BLOCK provide the context
594 ;;; for emitting any necessary type-checking code.
595 (defun reference-args (node block args template)
596 (declare (type node node) (type ir2-block block) (list args)
597 (type template template))
598 (collect ((info-args))
601 (do ((args args (cdr args))
602 (types (template-arg-types template) (cdr types)))
604 (let ((type (first types))
606 (if (and (consp type) (eq (car type) ':constant))
607 (info-args (lvar-value arg))
608 (let ((ref (reference-tn (lvar-tn node block arg) nil)))
610 (setf (tn-ref-across last) ref)
614 (values (the (or tn-ref null) first) (info-args)))))
616 ;;; Convert a conditional template. We try to exploit any
617 ;;; drop-through, but emit an unconditional branch afterward if we
618 ;;; fail. NOT-P is true if the sense of the TEMPLATE's test should be
620 (defun ir2-convert-conditional (node block template args info-args if not-p)
621 (declare (type node node) (type ir2-block block)
622 (type template template) (type (or tn-ref null) args)
623 (list info-args) (type cif if) (type boolean not-p))
624 (let ((consequent (if-consequent if))
625 (alternative (if-alternative if))
626 (flags (and (consp (template-result-types template))
627 (rest (template-result-types template)))))
628 (aver (= (template-info-arg-count template)
629 (+ (length info-args)
632 (rotatef consequent alternative)
634 (when (drop-thru-p if consequent)
635 (rotatef consequent alternative)
638 (emit-template node block template args nil
639 (list* (block-label consequent) not-p
641 (unless (drop-thru-p if alternative)
642 (vop branch node block (block-label alternative))))
644 (emit-template node block template args nil info-args)
645 (vop branch-if node block (block-label consequent) flags not-p)
646 (unless (drop-thru-p if alternative)
647 (vop branch node block (block-label alternative)))))))
649 ;;; Convert an IF that isn't the DEST of a conditional template.
650 (defun ir2-convert-if (node block)
651 (declare (type ir2-block block) (type cif node))
652 (let* ((test (if-test node))
653 (test-ref (reference-tn (lvar-tn node block test) nil))
654 (nil-ref (reference-tn (emit-constant nil) nil)))
655 (setf (tn-ref-across test-ref) nil-ref)
656 (ir2-convert-conditional node block (template-or-lose 'if-eq)
657 test-ref () node t)))
659 ;;; Return a list of primitive-types that we can pass to LVAR-RESULT-TNS
660 ;;; describing the result types we want for a template call. We are really
661 ;;; only interested in the number of results required: in normal case
662 ;;; TEMPLATE-RESULTS-OK has already checked them.
663 (defun find-template-result-types (call rtypes)
664 (let* ((type (node-derived-type call))
666 (mapcar #'primitive-type
667 (if (values-type-p type)
668 (append (args-type-required type)
669 (args-type-optional type))
671 (primitive-t *backend-t-primitive-type*))
672 (loop for rtype in rtypes
673 for type = (or (pop types) primitive-t)
676 ;;; Return a list of TNs usable in a CALL to TEMPLATE delivering values to
677 ;;; LVAR. As an efficiency hack, we pick off the common case where the LVAR is
678 ;;; fixed values and has locations that satisfy the result restrictions. This
679 ;;; can fail when there is a type check or a values count mismatch.
680 (defun make-template-result-tns (call lvar rtypes)
681 (declare (type combination call) (type (or lvar null) lvar)
683 (let ((2lvar (when lvar (lvar-info lvar))))
684 (if (and 2lvar (eq (ir2-lvar-kind 2lvar) :fixed))
685 (let ((locs (ir2-lvar-locs 2lvar)))
686 (if (and (= (length rtypes) (length locs))
687 (do ((loc locs (cdr loc))
688 (rtypes rtypes (cdr rtypes)))
690 (unless (operand-restriction-ok
692 (tn-primitive-type (car loc))
698 (find-template-result-types call rtypes))))
701 (find-template-result-types call rtypes)))))
703 ;;; Get the operands into TNs, make TN-REFs for them, and then call
704 ;;; the template emit function.
705 (defun ir2-convert-template (call block)
706 (declare (type combination call) (type ir2-block block))
707 (let* ((template (combination-info call))
708 (lvar (node-lvar call))
709 (rtypes (template-result-types template)))
710 (multiple-value-bind (args info-args)
711 (reference-args call block (combination-args call) template)
712 (aver (not (template-more-results-type template)))
713 (if (template-conditional-p template)
714 (ir2-convert-conditional call block template args info-args
715 (lvar-dest lvar) nil)
716 (let* ((results (make-template-result-tns call lvar rtypes))
717 (r-refs (reference-tn-list results t)))
718 (aver (= (length info-args)
719 (template-info-arg-count template)))
720 (when (and lvar (lvar-dynamic-extent lvar))
721 (vop current-stack-pointer call block
722 (ir2-lvar-stack-pointer (lvar-info lvar))))
723 (when (emit-step-p call)
724 (vop sb!vm::step-instrument-before-vop call block))
726 (emit-template call block template args r-refs info-args)
727 (emit-template call block template args r-refs))
728 (move-lvar-result call block results lvar)))))
731 ;;; We don't have to do much because operand count checking is done by
732 ;;; IR1 conversion. The only difference between this and the function
733 ;;; case of IR2-CONVERT-TEMPLATE is that there can be codegen-info
735 (defoptimizer (%%primitive ir2-convert) ((template info &rest args) call block)
736 (let* ((template (lvar-value template))
737 (info (lvar-value info))
738 (lvar (node-lvar call))
739 (rtypes (template-result-types template))
740 (results (make-template-result-tns call lvar rtypes))
741 (r-refs (reference-tn-list results t)))
742 (multiple-value-bind (args info-args)
743 (reference-args call block (cddr (combination-args call)) template)
744 (aver (not (template-more-results-type template)))
745 (aver (not (template-conditional-p template)))
746 (aver (null info-args))
749 (emit-template call block template args r-refs info)
750 (emit-template call block template args r-refs))
752 (move-lvar-result call block results lvar)))
755 (defoptimizer (%%primitive derive-type) ((template info &rest args))
756 (let ((type (template-type (lvar-value template))))
757 (if (fun-type-p type)
758 (fun-type-returns type)
763 ;;; Convert a LET by moving the argument values into the variables.
764 ;;; Since a LET doesn't have any passing locations, we move the
765 ;;; arguments directly into the variables. We must also allocate any
766 ;;; indirect value cells, since there is no function prologue to do
768 (defun ir2-convert-let (node block fun)
769 (declare (type combination node) (type ir2-block block) (type clambda fun))
770 (mapc (lambda (var arg)
772 (let ((src (lvar-tn node block arg))
773 (dest (leaf-info var)))
774 (if (and (lambda-var-indirect var)
775 (lambda-var-explicit-value-cell var))
776 (emit-make-value-cell node block src dest)
777 (emit-move node block src dest)))))
778 (lambda-vars fun) (basic-combination-args node))
781 ;;; Emit any necessary moves into assignment temps for a local call to
782 ;;; FUN. We return two lists of TNs: TNs holding the actual argument
783 ;;; values, and (possibly EQ) TNs that are the actual destination of
784 ;;; the arguments. When necessary, we allocate temporaries for
785 ;;; arguments to preserve parallel assignment semantics. These lists
786 ;;; exclude unused arguments and include implicit environment
787 ;;; arguments, i.e. they exactly correspond to the arguments passed.
789 ;;; OLD-FP is the TN currently holding the value we want to pass as
790 ;;; OLD-FP. If null, then the call is to the same environment (an
791 ;;; :ASSIGNMENT), so we only move the arguments, and leave the
792 ;;; environment alone.
793 (defun emit-psetq-moves (node block fun old-fp)
794 (declare (type combination node) (type ir2-block block) (type clambda fun)
795 (type (or tn null) old-fp))
796 (let ((actuals (mapcar (lambda (x)
798 (lvar-tn node block x)))
799 (combination-args node))))
802 (dolist (var (lambda-vars fun))
803 (let ((actual (pop actuals))
804 (loc (leaf-info var)))
807 ((and (lambda-var-indirect var)
808 (lambda-var-explicit-value-cell var))
810 (make-normal-tn *backend-t-primitive-type*)))
811 (emit-make-value-cell node block actual temp)
813 ((member actual (locs))
814 (let ((temp (make-normal-tn (tn-primitive-type loc))))
815 (emit-move node block actual temp)
822 (let ((this-1env (node-physenv node))
823 (called-env (physenv-info (lambda-physenv fun))))
824 (dolist (thing (ir2-physenv-closure called-env))
825 (temps (closure-initial-value (car thing) this-1env old-fp))
828 (locs (ir2-physenv-old-fp called-env))))
830 (values (temps) (locs)))))
832 ;;; A tail-recursive local call is done by emitting moves of stuff
833 ;;; into the appropriate passing locations. After setting up the args
834 ;;; and environment, we just move our return-pc into the called
835 ;;; function's passing location.
836 (defun ir2-convert-tail-local-call (node block fun)
837 (declare (type combination node) (type ir2-block block) (type clambda fun))
838 (let ((this-env (physenv-info (node-physenv node))))
839 (multiple-value-bind (temps locs)
840 (emit-psetq-moves node block fun (ir2-physenv-old-fp this-env))
842 (mapc (lambda (temp loc)
843 (emit-move node block temp loc))
846 (emit-move node block
847 (ir2-physenv-return-pc this-env)
848 (ir2-physenv-return-pc-pass
850 (lambda-physenv fun)))))
854 ;;; Convert an :ASSIGNMENT call. This is just like a tail local call,
855 ;;; except that the caller and callee environment are the same, so we
856 ;;; don't need to mess with the environment locations, return PC, etc.
857 (defun ir2-convert-assignment (node block fun)
858 (declare (type combination node) (type ir2-block block) (type clambda fun))
859 (multiple-value-bind (temps locs) (emit-psetq-moves node block fun nil)
861 (mapc (lambda (temp loc)
862 (emit-move node block temp loc))
866 ;;; Do stuff to set up the arguments to a non-tail local call
867 ;;; (including implicit environment args.) We allocate a frame
868 ;;; (returning the FP and NFP), and also compute the TN-REFS list for
869 ;;; the values to pass and the list of passing location TNs.
870 (defun ir2-convert-local-call-args (node block fun)
871 (declare (type combination node) (type ir2-block block) (type clambda fun))
872 (let ((fp (make-stack-pointer-tn))
873 (nfp (make-number-stack-pointer-tn))
874 (old-fp (make-stack-pointer-tn)))
875 (multiple-value-bind (temps locs)
876 (emit-psetq-moves node block fun old-fp)
877 (vop current-fp node block old-fp)
878 (vop allocate-frame node block
879 (physenv-info (lambda-physenv fun))
881 (values fp nfp temps (mapcar #'make-alias-tn locs)))))
883 ;;; Handle a non-TR known-values local call. We emit the call, then
884 ;;; move the results to the lvar's destination.
885 (defun ir2-convert-local-known-call (node block fun returns lvar start)
886 (declare (type node node) (type ir2-block block) (type clambda fun)
887 (type return-info returns) (type (or lvar null) lvar)
889 (multiple-value-bind (fp nfp temps arg-locs)
890 (ir2-convert-local-call-args node block fun)
891 (let ((locs (return-info-locations returns)))
892 (vop* known-call-local node block
893 (fp nfp (reference-tn-list temps nil))
894 ((reference-tn-list locs t))
895 arg-locs (physenv-info (lambda-physenv fun)) start)
896 (move-lvar-result node block locs lvar)))
899 ;;; Handle a non-TR unknown-values local call. We do different things
900 ;;; depending on what kind of values the lvar wants.
902 ;;; If LVAR is :UNKNOWN, then we use the "multiple-" variant, directly
903 ;;; specifying the lvar's LOCS as the VOP results so that we don't
904 ;;; have to do anything after the call.
906 ;;; Otherwise, we use STANDARD-RESULT-TNS to get wired result TNs, and
907 ;;; then call MOVE-LVAR-RESULT to do any necessary type checks or
909 (defun ir2-convert-local-unknown-call (node block fun lvar start)
910 (declare (type node node) (type ir2-block block) (type clambda fun)
911 (type (or lvar null) lvar) (type label start))
912 (multiple-value-bind (fp nfp temps arg-locs)
913 (ir2-convert-local-call-args node block fun)
914 (let ((2lvar (and lvar (lvar-info lvar)))
915 (env (physenv-info (lambda-physenv fun)))
916 (temp-refs (reference-tn-list temps nil)))
917 (if (and 2lvar (eq (ir2-lvar-kind 2lvar) :unknown))
918 (vop* multiple-call-local node block (fp nfp temp-refs)
919 ((reference-tn-list (ir2-lvar-locs 2lvar) t))
921 (let ((locs (standard-result-tns lvar)))
922 (vop* call-local node block
924 ((reference-tn-list locs t))
925 arg-locs env start (length locs))
926 (move-lvar-result node block locs lvar)))))
929 ;;; Dispatch to the appropriate function, depending on whether we have
930 ;;; a let, tail or normal call. If the function doesn't return, call
931 ;;; it using the unknown-value convention. We could compile it as a
932 ;;; tail call, but that might seem confusing in the debugger.
933 (defun ir2-convert-local-call (node block)
934 (declare (type combination node) (type ir2-block block))
935 (let* ((fun (ref-leaf (lvar-uses (basic-combination-fun node))))
936 (kind (functional-kind fun)))
937 (cond ((eq kind :let)
938 (ir2-convert-let node block fun))
939 ((eq kind :assignment)
940 (ir2-convert-assignment node block fun))
942 (ir2-convert-tail-local-call node block fun))
944 (let ((start (block-label (lambda-block fun)))
945 (returns (tail-set-info (lambda-tail-set fun)))
946 (lvar (node-lvar node)))
948 (return-info-kind returns)
951 (ir2-convert-local-unknown-call node block fun lvar start))
953 (ir2-convert-local-known-call node block fun returns
959 ;;; Given a function lvar FUN, return (VALUES TN-TO-CALL NAMED-P),
960 ;;; where TN-TO-CALL is a TN holding the thing that we call NAMED-P is
961 ;;; true if the thing is named (false if it is a function).
963 ;;; There are two interesting non-named cases:
964 ;;; -- We know it's a function. No check needed: return the
966 ;;; -- We don't know what it is.
967 (defun fun-lvar-tn (node block lvar)
968 (declare (ignore node block))
969 (declare (type lvar lvar))
970 (let ((2lvar (lvar-info lvar)))
971 (if (eq (ir2-lvar-kind 2lvar) :delayed)
972 (let ((name (lvar-fun-name lvar t)))
974 (values (make-load-time-constant-tn :fdefinition name) t))
975 (let* ((locs (ir2-lvar-locs 2lvar))
977 (function-ptype (primitive-type-or-lose 'function)))
978 (aver (and (eq (ir2-lvar-kind 2lvar) :fixed)
979 (= (length locs) 1)))
980 (aver (eq (tn-primitive-type loc) function-ptype))
983 ;;; Set up the args to NODE in the current frame, and return a TN-REF
984 ;;; list for the passing locations.
985 (defun move-tail-full-call-args (node block)
986 (declare (type combination node) (type ir2-block block))
987 (let ((args (basic-combination-args node))
990 (dotimes (num (length args))
991 (let ((loc (standard-arg-location num)))
992 (emit-move node block (lvar-tn node block (elt args num)) loc)
993 (let ((ref (reference-tn loc nil)))
995 (setf (tn-ref-across last) ref)
1000 ;;; Move the arguments into the passing locations and do a (possibly
1001 ;;; named) tail call.
1002 (defun ir2-convert-tail-full-call (node block)
1003 (declare (type combination node) (type ir2-block block))
1004 (let* ((env (physenv-info (node-physenv node)))
1005 (args (basic-combination-args node))
1006 (nargs (length args))
1007 (pass-refs (move-tail-full-call-args node block))
1008 (old-fp (ir2-physenv-old-fp env))
1009 (return-pc (ir2-physenv-return-pc env)))
1011 (multiple-value-bind (fun-tn named)
1012 (fun-lvar-tn node block (basic-combination-fun node))
1014 (vop* tail-call-named node block
1015 (fun-tn old-fp return-pc pass-refs)
1019 (vop* tail-call node block
1020 (fun-tn old-fp return-pc pass-refs)
1023 (emit-step-p node)))))
1027 ;;; like IR2-CONVERT-LOCAL-CALL-ARGS, only different
1028 (defun ir2-convert-full-call-args (node block)
1029 (declare (type combination node) (type ir2-block block))
1030 (let* ((args (basic-combination-args node))
1031 (fp (make-stack-pointer-tn))
1032 (nargs (length args)))
1033 (vop allocate-full-call-frame node block nargs fp)
1037 (dotimes (num nargs)
1038 (locs (standard-arg-location num))
1039 (let ((ref (reference-tn (lvar-tn node block (elt args num))
1042 (setf (tn-ref-across last) ref)
1046 (values fp first (locs) nargs)))))
1048 ;;; Do full call when a fixed number of values are desired. We make
1049 ;;; STANDARD-RESULT-TNS for our lvar, then deliver the result using
1050 ;;; MOVE-LVAR-RESULT. We do named or normal call, as appropriate.
1051 (defun ir2-convert-fixed-full-call (node block)
1052 (declare (type combination node) (type ir2-block block))
1053 (multiple-value-bind (fp args arg-locs nargs)
1054 (ir2-convert-full-call-args node block)
1055 (let* ((lvar (node-lvar node))
1056 (locs (standard-result-tns lvar))
1057 (loc-refs (reference-tn-list locs t))
1058 (nvals (length locs)))
1059 (multiple-value-bind (fun-tn named)
1060 (fun-lvar-tn node block (basic-combination-fun node))
1062 (vop* call-named node block (fp fun-tn args) (loc-refs)
1063 arg-locs nargs nvals (emit-step-p node))
1064 (vop* call node block (fp fun-tn args) (loc-refs)
1065 arg-locs nargs nvals (emit-step-p node)))
1066 (move-lvar-result node block locs lvar))))
1069 ;;; Do full call when unknown values are desired.
1070 (defun ir2-convert-multiple-full-call (node block)
1071 (declare (type combination node) (type ir2-block block))
1072 (multiple-value-bind (fp args arg-locs nargs)
1073 (ir2-convert-full-call-args node block)
1074 (let* ((lvar (node-lvar node))
1075 (locs (ir2-lvar-locs (lvar-info lvar)))
1076 (loc-refs (reference-tn-list locs t)))
1077 (multiple-value-bind (fun-tn named)
1078 (fun-lvar-tn node block (basic-combination-fun node))
1080 (vop* multiple-call-named node block (fp fun-tn args) (loc-refs)
1081 arg-locs nargs (emit-step-p node))
1082 (vop* multiple-call node block (fp fun-tn args) (loc-refs)
1083 arg-locs nargs (emit-step-p node))))))
1086 ;;; stuff to check in PONDER-FULL-CALL
1088 ;;; These came in handy when troubleshooting cold boot after making
1089 ;;; major changes in the package structure: various transforms and
1090 ;;; VOPs and stuff got attached to the wrong symbol, so that
1091 ;;; references to the right symbol were bogusly translated as full
1092 ;;; calls instead of primitives, sending the system off into infinite
1093 ;;; space. Having a report on all full calls generated makes it easier
1094 ;;; to figure out what form caused the problem this time.
1095 #!+sb-show (defvar *show-full-called-fnames-p* nil)
1096 #!+sb-show (defvar *full-called-fnames* (make-hash-table :test 'equal))
1098 ;;; Do some checks (and store some notes relevant for future checks)
1100 ;;; * Is this a full call to something we have reason to know should
1101 ;;; never be full called? (Except as of sbcl-0.7.18 or so, we no
1102 ;;; longer try to ensure this behavior when *FAILURE-P* has already
1104 ;;; * Is this a full call to (SETF FOO) which might conflict with
1105 ;;; a DEFSETF or some such thing elsewhere in the program?
1106 (defun ponder-full-call (node)
1107 (let* ((lvar (basic-combination-fun node))
1108 (fname (lvar-fun-name lvar t)))
1109 (declare (type (or symbol cons) fname))
1111 #!+sb-show (unless (gethash fname *full-called-fnames*)
1112 (setf (gethash fname *full-called-fnames*) t))
1113 #!+sb-show (when *show-full-called-fnames-p*
1114 (/show "converting full call to named function" fname)
1115 (/show (basic-combination-args node))
1116 (/show (policy node speed) (policy node safety))
1117 (/show (policy node compilation-speed))
1118 (let ((arg-types (mapcar (lambda (lvar)
1122 (basic-combination-args node))))
1125 ;; When illegal code is compiled, all sorts of perverse paths
1126 ;; through the compiler can be taken, and it's much harder -- and
1127 ;; probably pointless -- to guarantee that always-optimized-away
1128 ;; functions are actually optimized away. Thus, we skip the check
1131 ;; check to see if we know anything about the function
1132 (let ((info (info :function :info fname)))
1133 ;; if we know something, check to see if the full call was valid
1134 (when (and info (ir1-attributep (fun-info-attributes info)
1135 always-translatable))
1136 (/show (policy node speed) (policy node safety))
1137 (/show (policy node compilation-speed))
1138 (bug "full call to ~S" fname))))
1141 (aver (legal-fun-name-p fname))
1142 (destructuring-bind (setfoid &rest stem) fname
1143 (when (eq setfoid 'setf)
1144 (setf (gethash (car stem) *setf-assumed-fboundp*) t))))))
1146 ;;; If the call is in a tail recursive position and the return
1147 ;;; convention is standard, then do a tail full call. If one or fewer
1148 ;;; values are desired, then use a single-value call, otherwise use a
1149 ;;; multiple-values call.
1150 (defun ir2-convert-full-call (node block)
1151 (declare (type combination node) (type ir2-block block))
1152 (ponder-full-call node)
1153 (cond ((node-tail-p node)
1154 (ir2-convert-tail-full-call node block))
1155 ((let ((lvar (node-lvar node)))
1157 (eq (ir2-lvar-kind (lvar-info lvar)) :unknown)))
1158 (ir2-convert-multiple-full-call node block))
1160 (ir2-convert-fixed-full-call node block)))
1163 ;;;; entering functions
1165 ;;; Do all the stuff that needs to be done on XEP entry:
1166 ;;; -- Create frame.
1167 ;;; -- Copy any more arg.
1168 ;;; -- Set up the environment, accessing any closure variables.
1169 ;;; -- Move args from the standard passing locations to their internal
1171 (defun init-xep-environment (node block fun)
1172 (declare (type bind node) (type ir2-block block) (type clambda fun))
1173 (let ((start-label (entry-info-offset (leaf-info fun)))
1174 (env (physenv-info (node-physenv node))))
1175 (let ((ef (functional-entry-fun fun)))
1176 (cond ((and (optional-dispatch-p ef) (optional-dispatch-more-entry ef))
1177 ;; Special case the xep-allocate-frame + copy-more-arg case.
1178 (vop xep-allocate-frame node block start-label t)
1179 (vop copy-more-arg node block (optional-dispatch-max-args ef)))
1181 ;; No more args, so normal entry.
1182 (vop xep-allocate-frame node block start-label nil)))
1183 (if (ir2-physenv-closure env)
1184 (let ((closure (make-normal-tn *backend-t-primitive-type*)))
1185 (vop setup-closure-environment node block start-label closure)
1187 (dolist (loc (ir2-physenv-closure env))
1188 (vop closure-ref node block closure (incf n) (cdr loc)))))
1189 (vop setup-environment node block start-label)))
1191 (unless (eq (functional-kind fun) :toplevel)
1192 (let ((vars (lambda-vars fun))
1194 (when (leaf-refs (first vars))
1195 (emit-move node block (make-arg-count-location)
1196 (leaf-info (first vars))))
1197 (dolist (arg (rest vars))
1198 (when (leaf-refs arg)
1199 (let ((pass (standard-arg-location n))
1200 (home (leaf-info arg)))
1201 (if (and (lambda-var-indirect arg)
1202 (lambda-var-explicit-value-cell arg))
1203 (emit-make-value-cell node block pass home)
1204 (emit-move node block pass home))))
1207 (emit-move node block (make-old-fp-passing-location t)
1208 (ir2-physenv-old-fp env)))
1212 ;;; Emit function prolog code. This is only called on bind nodes for
1213 ;;; functions that allocate environments. All semantics of let calls
1214 ;;; are handled by IR2-CONVERT-LET.
1216 ;;; If not an XEP, all we do is move the return PC from its passing
1217 ;;; location, since in a local call, the caller allocates the frame
1218 ;;; and sets up the arguments.
1219 (defun ir2-convert-bind (node block)
1220 (declare (type bind node) (type ir2-block block))
1221 (let* ((fun (bind-lambda node))
1222 (env (physenv-info (lambda-physenv fun))))
1223 (aver (member (functional-kind fun)
1224 '(nil :external :optional :toplevel :cleanup)))
1227 (init-xep-environment node block fun)
1229 (when *collect-dynamic-statistics*
1230 (vop count-me node block *dynamic-counts-tn*
1231 (block-number (ir2-block-block block)))))
1235 (ir2-physenv-return-pc-pass env)
1236 (ir2-physenv-return-pc env))
1238 #!+unwind-to-frame-and-call-vop
1239 (when (and (lambda-allow-instrumenting fun)
1240 (not (lambda-inline-expanded fun))
1242 (policy fun (>= insert-debug-catch 2)))
1243 (vop sb!vm::bind-sentinel node block))
1245 (let ((lab (gen-label)))
1246 (setf (ir2-physenv-environment-start env) lab)
1247 (vop note-environment-start node block lab)))
1251 ;;;; function return
1253 ;;; Do stuff to return from a function with the specified values and
1254 ;;; convention. If the return convention is :FIXED and we aren't
1255 ;;; returning from an XEP, then we do a known return (letting
1256 ;;; representation selection insert the correct move-arg VOPs.)
1257 ;;; Otherwise, we use the unknown-values convention. If there is a
1258 ;;; fixed number of return values, then use RETURN, otherwise use
1259 ;;; RETURN-MULTIPLE.
1260 (defun ir2-convert-return (node block)
1261 (declare (type creturn node) (type ir2-block block))
1262 (let* ((lvar (return-result node))
1263 (2lvar (lvar-info lvar))
1264 (lvar-kind (ir2-lvar-kind 2lvar))
1265 (fun (return-lambda node))
1266 (env (physenv-info (lambda-physenv fun)))
1267 (old-fp (ir2-physenv-old-fp env))
1268 (return-pc (ir2-physenv-return-pc env))
1269 (returns (tail-set-info (lambda-tail-set fun))))
1270 #!+unwind-to-frame-and-call-vop
1271 (when (and (lambda-allow-instrumenting fun)
1272 (not (lambda-inline-expanded fun))
1273 (policy fun (>= insert-debug-catch 2)))
1274 (vop sb!vm::unbind-sentinel node block))
1276 ((and (eq (return-info-kind returns) :fixed)
1278 (let ((locs (lvar-tns node block lvar
1279 (return-info-types returns))))
1280 (vop* known-return node block
1281 (old-fp return-pc (reference-tn-list locs nil))
1283 (return-info-locations returns))))
1284 ((eq lvar-kind :fixed)
1285 (let* ((types (mapcar #'tn-primitive-type (ir2-lvar-locs 2lvar)))
1286 (lvar-locs (lvar-tns node block lvar types))
1287 (nvals (length lvar-locs))
1288 (locs (make-standard-value-tns nvals)))
1289 (mapc (lambda (val loc)
1290 (emit-move node block val loc))
1294 (vop return-single node block old-fp return-pc (car locs))
1295 (vop* return node block
1296 (old-fp return-pc (reference-tn-list locs nil))
1300 (aver (eq lvar-kind :unknown))
1301 (vop* return-multiple node block
1303 (reference-tn-list (ir2-lvar-locs 2lvar) nil))
1310 ;;;; These are used by the debugger to find the top function on the
1311 ;;;; stack. They return the OLD-FP and RETURN-PC for the current
1312 ;;;; function as multiple values.
1314 (defoptimizer (%caller-frame ir2-convert) (() node block)
1315 (let ((ir2-physenv (physenv-info (node-physenv node))))
1316 (move-lvar-result node block
1317 (list (ir2-physenv-old-fp ir2-physenv))
1320 (defoptimizer (%caller-pc ir2-convert) (() node block)
1321 (let ((ir2-physenv (physenv-info (node-physenv node))))
1322 (move-lvar-result node block
1323 (list (ir2-physenv-return-pc ir2-physenv))
1326 ;;;; multiple values
1328 ;;; This is almost identical to IR2-CONVERT-LET. Since LTN annotates
1329 ;;; the lvar for the correct number of values (with the lvar user
1330 ;;; responsible for defaulting), we can just pick them up from the
1332 (defun ir2-convert-mv-bind (node block)
1333 (declare (type mv-combination node) (type ir2-block block))
1334 (let* ((lvar (first (basic-combination-args node)))
1335 (fun (ref-leaf (lvar-uses (basic-combination-fun node))))
1336 (vars (lambda-vars fun)))
1337 (aver (eq (functional-kind fun) :mv-let))
1338 (mapc (lambda (src var)
1339 (when (leaf-refs var)
1340 (let ((dest (leaf-info var)))
1341 (if (and (lambda-var-indirect var)
1342 (lambda-var-explicit-value-cell var))
1343 (emit-make-value-cell node block src dest)
1344 (emit-move node block src dest)))))
1345 (lvar-tns node block lvar
1347 (primitive-type (leaf-type x)))
1352 ;;; Emit the appropriate fixed value, unknown value or tail variant of
1353 ;;; CALL-VARIABLE. Note that we only need to pass the values start for
1354 ;;; the first argument: all the other argument lvar TNs are
1355 ;;; ignored. This is because we require all of the values globs to be
1356 ;;; contiguous and on stack top.
1357 (defun ir2-convert-mv-call (node block)
1358 (declare (type mv-combination node) (type ir2-block block))
1359 (aver (basic-combination-args node))
1360 (let* ((start-lvar (lvar-info (first (basic-combination-args node))))
1361 (start (first (ir2-lvar-locs start-lvar)))
1362 (tails (and (node-tail-p node)
1363 (lambda-tail-set (node-home-lambda node))))
1364 (lvar (node-lvar node))
1365 (2lvar (and lvar (lvar-info lvar))))
1366 (multiple-value-bind (fun named)
1367 (fun-lvar-tn node block (basic-combination-fun node))
1368 (aver (and (not named)
1369 (eq (ir2-lvar-kind start-lvar) :unknown)))
1372 (let ((env (physenv-info (node-physenv node))))
1373 (vop tail-call-variable node block start fun
1374 (ir2-physenv-old-fp env)
1375 (ir2-physenv-return-pc env))))
1377 (eq (ir2-lvar-kind 2lvar) :unknown))
1378 (vop* multiple-call-variable node block (start fun nil)
1379 ((reference-tn-list (ir2-lvar-locs 2lvar) t))
1380 (emit-step-p node)))
1382 (let ((locs (standard-result-tns lvar)))
1383 (vop* call-variable node block (start fun nil)
1384 ((reference-tn-list locs t)) (length locs)
1386 (move-lvar-result node block locs lvar)))))))
1388 ;;; Reset the stack pointer to the start of the specified
1389 ;;; unknown-values lvar (discarding it and all values globs on top of
1391 (defoptimizer (%pop-values ir2-convert) ((%lvar) node block)
1392 (let* ((lvar (lvar-value %lvar))
1393 (2lvar (lvar-info lvar)))
1394 (cond ((eq (ir2-lvar-kind 2lvar) :unknown)
1395 (vop reset-stack-pointer node block
1396 (first (ir2-lvar-locs 2lvar))))
1397 ((lvar-dynamic-extent lvar)
1398 (vop reset-stack-pointer node block
1399 (ir2-lvar-stack-pointer 2lvar)))
1400 (t (bug "Trying to pop a not stack-allocated LVAR ~S."
1403 (defoptimizer (%nip-values ir2-convert) ((last-nipped last-preserved
1406 (let* ( ;; pointer immediately after the nipped block
1407 (after (lvar-value last-nipped))
1408 (2after (lvar-info after))
1409 ;; pointer to the first nipped word
1410 (first (lvar-value last-preserved))
1411 (2first (lvar-info first))
1413 (moved-tns (loop for lvar-ref in moved
1414 for lvar = (lvar-value lvar-ref)
1415 for 2lvar = (lvar-info lvar)
1417 collect (first (ir2-lvar-locs 2lvar)))))
1418 (aver (or (eq (ir2-lvar-kind 2after) :unknown)
1419 (lvar-dynamic-extent after)))
1420 (aver (eq (ir2-lvar-kind 2first) :unknown))
1421 (when *check-consistency*
1422 ;; we cannot move stack-allocated DX objects
1423 (dolist (moved-lvar moved)
1424 (aver (eq (ir2-lvar-kind (lvar-info (lvar-value moved-lvar)))
1426 (flet ((nip-aligned (nipped)
1427 (vop* %%nip-values node block
1429 (first (ir2-lvar-locs 2first))
1430 (reference-tn-list moved-tns nil))
1431 ((reference-tn-list moved-tns t)))))
1432 (cond ((eq (ir2-lvar-kind 2after) :unknown)
1433 (nip-aligned (first (ir2-lvar-locs 2after))))
1434 ((lvar-dynamic-extent after)
1435 (nip-aligned (ir2-lvar-stack-pointer 2after)))
1437 (bug "Trying to nip a not stack-allocated LVAR ~S." after))))))
1439 ;;; Deliver the values TNs to LVAR using MOVE-LVAR-RESULT.
1440 (defoptimizer (values ir2-convert) ((&rest values) node block)
1441 (let ((tns (mapcar (lambda (x)
1442 (lvar-tn node block x))
1444 (move-lvar-result node block tns (node-lvar node))))
1446 ;;; In the normal case where unknown values are desired, we use the
1447 ;;; VALUES-LIST VOP. In the relatively unimportant case of VALUES-LIST
1448 ;;; for a fixed number of values, we punt by doing a full call to the
1449 ;;; VALUES-LIST function. This gets the full call VOP to deal with
1450 ;;; defaulting any unsupplied values. It seems unworthwhile to
1451 ;;; optimize this case.
1452 (defoptimizer (values-list ir2-convert) ((list) node block)
1453 (let* ((lvar (node-lvar node))
1454 (2lvar (and lvar (lvar-info lvar))))
1456 (eq (ir2-lvar-kind 2lvar) :unknown))
1457 (let ((locs (ir2-lvar-locs 2lvar)))
1458 (vop* values-list node block
1459 ((lvar-tn node block list) nil)
1460 ((reference-tn-list locs t)))))
1461 (t (aver (or (not 2lvar) ; i.e. we want to check the argument
1462 (eq (ir2-lvar-kind 2lvar) :fixed)))
1463 (ir2-convert-full-call node block)))))
1465 (defoptimizer (%more-arg-values ir2-convert) ((context start count) node block)
1466 (binding* ((lvar (node-lvar node) :exit-if-null)
1467 (2lvar (lvar-info lvar)))
1468 (ecase (ir2-lvar-kind 2lvar)
1469 (:fixed (ir2-convert-full-call node block))
1471 (let ((locs (ir2-lvar-locs 2lvar)))
1472 (vop* %more-arg-values node block
1473 ((lvar-tn node block context)
1474 (lvar-tn node block start)
1475 (lvar-tn node block count)
1477 ((reference-tn-list locs t))))))))
1479 ;;;; special binding
1481 ;;; This is trivial, given our assumption of a shallow-binding
1483 (defoptimizer (%special-bind ir2-convert) ((var value) node block)
1484 (let ((name (leaf-source-name (lvar-value var))))
1485 (vop bind node block (lvar-tn node block value)
1486 (emit-constant name))))
1487 (defoptimizer (%special-unbind ir2-convert) ((var) node block)
1488 (vop unbind node block))
1490 ;;; ### It's not clear that this really belongs in this file, or
1491 ;;; should really be done this way, but this is the least violation of
1492 ;;; abstraction in the current setup. We don't want to wire
1493 ;;; shallow-binding assumptions into IR1tran.
1494 (def-ir1-translator progv
1495 ((vars vals &body body) start next result)
1498 (with-unique-names (bind unbind)
1499 (once-only ((n-save-bs '(%primitive current-binding-pointer)))
1502 (labels ((,unbind (vars)
1503 (declare (optimize (speed 2) (debug 0)))
1504 (let ((unbound-marker (%primitive make-other-immediate-type
1505 0 sb!vm:unbound-marker-widetag)))
1507 ;; CLHS says "bound and then made to have no value" -- user
1508 ;; should not be able to tell the difference between that and this.
1509 (about-to-modify-symbol-value var 'progv)
1510 (%primitive bind unbound-marker var))))
1512 (declare (optimize (speed 2) (debug 0)
1513 (insert-debug-catch 0)))
1515 ((null vals) (,unbind vars))
1517 (let ((val (car vals))
1519 (about-to-modify-symbol-value var 'progv val t)
1520 (%primitive bind val var))
1521 (,bind (cdr vars) (cdr vals))))))
1522 (,bind ,vars ,vals))
1525 ;; Technically ANSI CL doesn't allow declarations at the
1526 ;; start of the cleanup form. SBCL happens to allow for
1527 ;; them, due to the way the UNWIND-PROTECT ir1 translation
1528 ;; is implemented; the cleanup forms are directly spliced
1529 ;; into an FLET definition body. And a declaration here
1530 ;; actually has exactly the right scope for what we need
1531 ;; (ensure that debug instrumentation is not emitted for the
1532 ;; cleanup function). -- JES, 2007-06-16
1533 (declare (optimize (insert-debug-catch 0)))
1534 (%primitive unbind-to-here ,n-save-bs))))))
1538 ;;; Convert a non-local lexical exit. First find the NLX-INFO in our
1539 ;;; environment. Note that this is never called on the escape exits
1540 ;;; for CATCH and UNWIND-PROTECT, since the escape functions aren't
1542 (defun ir2-convert-exit (node block)
1543 (declare (type exit node) (type ir2-block block))
1544 (let* ((nlx (exit-nlx-info node))
1545 (loc (find-in-physenv nlx (node-physenv node)))
1546 (temp (make-stack-pointer-tn))
1547 (value (exit-value node)))
1548 (if (nlx-info-safe-p nlx)
1549 (vop value-cell-ref node block loc temp)
1550 (emit-move node block loc temp))
1552 (let ((locs (ir2-lvar-locs (lvar-info value))))
1553 (vop unwind node block temp (first locs) (second locs)))
1554 (let ((0-tn (emit-constant 0)))
1555 (vop unwind node block temp 0-tn 0-tn))))
1559 ;;; %CLEANUP-POINT doesn't do anything except prevent the body from
1560 ;;; being entirely deleted.
1561 (defoptimizer (%cleanup-point ir2-convert) (() node block) node block)
1563 ;;; This function invalidates a lexical exit on exiting from the
1564 ;;; dynamic extent. This is done by storing 0 into the indirect value
1565 ;;; cell that holds the closed unwind block.
1566 (defoptimizer (%lexical-exit-breakup ir2-convert) ((info) node block)
1567 (let ((nlx (lvar-value info)))
1568 (when (nlx-info-safe-p nlx)
1569 (vop value-cell-set node block
1570 (find-in-physenv nlx (node-physenv node))
1571 (emit-constant 0)))))
1573 ;;; We have to do a spurious move of no values to the result lvar so
1574 ;;; that lifetime analysis won't get confused.
1575 (defun ir2-convert-throw (node block)
1576 (declare (type mv-combination node) (type ir2-block block))
1577 (let ((args (basic-combination-args node)))
1578 (check-catch-tag-type (first args))
1579 (vop* throw node block
1580 ((lvar-tn node block (first args))
1582 (ir2-lvar-locs (lvar-info (second args)))
1585 (move-lvar-result node block () (node-lvar node))
1588 ;;; Emit code to set up a non-local exit. INFO is the NLX-INFO for the
1589 ;;; exit, and TAG is the lvar for the catch tag (if any.) We get at
1590 ;;; the target PC by passing in the label to the vop. The vop is
1591 ;;; responsible for building a return-PC object.
1592 (defun emit-nlx-start (node block info tag)
1593 (declare (type node node) (type ir2-block block) (type nlx-info info)
1594 (type (or lvar null) tag))
1595 (let* ((2info (nlx-info-info info))
1596 (kind (cleanup-kind (nlx-info-cleanup info)))
1597 (block-tn (physenv-live-tn
1598 (make-normal-tn (primitive-type-or-lose 'catch-block))
1599 (node-physenv node)))
1600 (res (make-stack-pointer-tn))
1601 (target-label (ir2-nlx-info-target 2info)))
1603 (vop current-binding-pointer node block
1604 (car (ir2-nlx-info-dynamic-state 2info)))
1605 (vop* save-dynamic-state node block
1607 ((reference-tn-list (cdr (ir2-nlx-info-dynamic-state 2info)) t)))
1608 (vop current-stack-pointer node block (ir2-nlx-info-save-sp 2info))
1612 (vop make-catch-block node block block-tn
1613 (lvar-tn node block tag) target-label res))
1614 ((:unwind-protect :block :tagbody)
1615 (vop make-unwind-block node block block-tn target-label res)))
1619 (if (nlx-info-safe-p info)
1620 (emit-make-value-cell node block res (ir2-nlx-info-home 2info))
1621 (emit-move node block res (ir2-nlx-info-home 2info))))
1623 (vop set-unwind-protect node block block-tn))
1628 ;;; Scan each of ENTRY's exits, setting up the exit for each lexical exit.
1629 (defun ir2-convert-entry (node block)
1630 (declare (type entry node) (type ir2-block block))
1632 (dolist (exit (entry-exits node))
1633 (let ((info (exit-nlx-info exit)))
1635 (not (memq info nlxes))
1636 (member (cleanup-kind (nlx-info-cleanup info))
1637 '(:block :tagbody)))
1639 (emit-nlx-start node block info nil)))))
1642 ;;; Set up the unwind block for these guys.
1643 (defoptimizer (%catch ir2-convert) ((info-lvar tag) node block)
1644 (check-catch-tag-type tag)
1645 (emit-nlx-start node block (lvar-value info-lvar) tag))
1646 (defoptimizer (%unwind-protect ir2-convert) ((info-lvar cleanup) node block)
1647 (emit-nlx-start node block (lvar-value info-lvar) nil))
1649 ;;; Emit the entry code for a non-local exit. We receive values and
1650 ;;; restore dynamic state.
1652 ;;; In the case of a lexical exit or CATCH, we look at the exit lvar's
1653 ;;; kind to determine which flavor of entry VOP to emit. If unknown
1654 ;;; values, emit the xxx-MULTIPLE variant to the lvar locs. If fixed
1655 ;;; values, make the appropriate number of temps in the standard
1656 ;;; values locations and use the other variant, delivering the temps
1657 ;;; to the lvar using MOVE-LVAR-RESULT.
1659 ;;; In the UNWIND-PROTECT case, we deliver the first register
1660 ;;; argument, the argument count and the argument pointer to our lvar
1661 ;;; as multiple values. These values are the block exited to and the
1662 ;;; values start and count.
1664 ;;; After receiving values, we restore dynamic state. Except in the
1665 ;;; UNWIND-PROTECT case, the values receiving restores the stack
1666 ;;; pointer. In an UNWIND-PROTECT cleanup, we want to leave the stack
1667 ;;; pointer alone, since the thrown values are still out there.
1668 (defoptimizer (%nlx-entry ir2-convert) ((info-lvar) node block)
1669 (let* ((info (lvar-value info-lvar))
1670 (lvar (node-lvar node))
1671 (2info (nlx-info-info info))
1672 (top-loc (ir2-nlx-info-save-sp 2info))
1673 (start-loc (make-nlx-entry-arg-start-location))
1674 (count-loc (make-arg-count-location))
1675 (target (ir2-nlx-info-target 2info)))
1677 (ecase (cleanup-kind (nlx-info-cleanup info))
1678 ((:catch :block :tagbody)
1679 (let ((2lvar (and lvar (lvar-info lvar))))
1680 (if (and 2lvar (eq (ir2-lvar-kind 2lvar) :unknown))
1681 (vop* nlx-entry-multiple node block
1682 (top-loc start-loc count-loc nil)
1683 ((reference-tn-list (ir2-lvar-locs 2lvar) t))
1685 (let ((locs (standard-result-tns lvar)))
1686 (vop* nlx-entry node block
1687 (top-loc start-loc count-loc nil)
1688 ((reference-tn-list locs t))
1691 (move-lvar-result node block locs lvar)))))
1693 (let ((block-loc (standard-arg-location 0)))
1694 (vop uwp-entry node block target block-loc start-loc count-loc)
1697 (list block-loc start-loc count-loc)
1701 (when *collect-dynamic-statistics*
1702 (vop count-me node block *dynamic-counts-tn*
1703 (block-number (ir2-block-block block))))
1705 (vop* restore-dynamic-state node block
1706 ((reference-tn-list (cdr (ir2-nlx-info-dynamic-state 2info)) nil))
1708 (vop unbind-to-here node block
1709 (car (ir2-nlx-info-dynamic-state 2info)))))
1711 ;;;; n-argument functions
1713 (macrolet ((def (name)
1714 `(defoptimizer (,name ir2-convert) ((&rest args) node block)
1715 (let* ((refs (move-tail-full-call-args node block))
1716 (lvar (node-lvar node))
1717 (res (lvar-result-tns
1719 (list (primitive-type (specifier-type 'list))))))
1720 (when (and lvar (lvar-dynamic-extent lvar))
1721 (vop current-stack-pointer node block
1722 (ir2-lvar-stack-pointer (lvar-info lvar))))
1723 (vop* ,name node block (refs) ((first res) nil)
1725 (move-lvar-result node block res lvar)))))
1730 ;;; Convert the code in a component into VOPs.
1731 (defun ir2-convert (component)
1732 (declare (type component component))
1733 (let (#!+sb-dyncount
1734 (*dynamic-counts-tn*
1735 (when *collect-dynamic-statistics*
1737 (block-number (block-next (component-head component))))
1738 (counts (make-array blocks
1739 :element-type '(unsigned-byte 32)
1740 :initial-element 0))
1741 (info (make-dyncount-info
1742 :for (component-name component)
1743 :costs (make-array blocks
1744 :element-type '(unsigned-byte 32)
1747 (setf (ir2-component-dyncount-info (component-info component))
1749 (emit-constant info)
1750 (emit-constant counts)))))
1752 (declare (type index num))
1753 (do-ir2-blocks (2block component)
1754 (let ((block (ir2-block-block 2block)))
1755 (when (block-start block)
1756 (setf (block-number block) num)
1758 (when *collect-dynamic-statistics*
1759 (let ((first-node (block-start-node block)))
1760 (unless (or (and (bind-p first-node)
1761 (xep-p (bind-lambda first-node)))
1763 (node-lvar first-node))
1768 #!+sb-dyncount *dynamic-counts-tn* #!-sb-dyncount nil
1770 (ir2-convert-block block)
1774 ;;; If necessary, emit a terminal unconditional branch to go to the
1775 ;;; successor block. If the successor is the component tail, then
1776 ;;; there isn't really any successor, but if the end is an unknown,
1777 ;;; non-tail call, then we emit an error trap just in case the
1778 ;;; function really does return.
1779 (defun finish-ir2-block (block)
1780 (declare (type cblock block))
1781 (let* ((2block (block-info block))
1782 (last (block-last block))
1783 (succ (block-succ block)))
1785 (aver (singleton-p succ))
1786 (let ((target (first succ)))
1787 (cond ((eq target (component-tail (block-component block)))
1788 (when (and (basic-combination-p last)
1789 (eq (basic-combination-kind last) :full))
1790 (let* ((fun (basic-combination-fun last))
1791 (use (lvar-uses fun))
1792 (name (and (ref-p use)
1793 (leaf-has-source-name-p (ref-leaf use))
1794 (leaf-source-name (ref-leaf use)))))
1795 (unless (or (node-tail-p last)
1796 (info :function :info name)
1797 (policy last (zerop safety)))
1798 (vop nil-fun-returned-error last 2block
1800 (emit-constant name)
1801 (multiple-value-bind (tn named)
1802 (fun-lvar-tn last 2block fun)
1805 ((not (eq (ir2-block-next 2block) (block-info target)))
1806 (vop branch last 2block (block-label target)))))))
1810 ;;; Convert the code in a block into VOPs.
1811 (defun ir2-convert-block (block)
1812 (declare (type cblock block))
1813 (let ((2block (block-info block)))
1814 (do-nodes (node lvar block)
1818 (let ((2lvar (lvar-info lvar)))
1819 ;; function REF in a local call is not annotated
1820 (when (and 2lvar (not (eq (ir2-lvar-kind 2lvar) :delayed)))
1821 (ir2-convert-ref node 2block)))))
1823 (let ((kind (basic-combination-kind node)))
1826 (ir2-convert-local-call node 2block))
1828 (ir2-convert-full-call node 2block))
1830 (let* ((info (basic-combination-fun-info node))
1831 (fun (fun-info-ir2-convert info)))
1833 (funcall fun node 2block))
1834 ((eq (basic-combination-info node) :full)
1835 (ir2-convert-full-call node 2block))
1837 (ir2-convert-template node 2block))))))))
1839 (when (lvar-info (if-test node))
1840 (ir2-convert-if node 2block)))
1842 (let ((fun (bind-lambda node)))
1843 (when (eq (lambda-home fun) fun)
1844 (ir2-convert-bind node 2block))))
1846 (ir2-convert-return node 2block))
1848 (ir2-convert-set node 2block))
1850 (ir2-convert-cast node 2block))
1853 ((eq (basic-combination-kind node) :local)
1854 (ir2-convert-mv-bind node 2block))
1855 ((eq (lvar-fun-name (basic-combination-fun node))
1857 (ir2-convert-throw node 2block))
1859 (ir2-convert-mv-call node 2block))))
1861 (when (exit-entry node)
1862 (ir2-convert-exit node 2block)))
1864 (ir2-convert-entry node 2block)))))
1866 (finish-ir2-block block)