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 (vop make-value-cell node block value nil res))
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)
66 (defun find-in-physenv (thing physenv)
67 (or (cdr (assoc thing (ir2-physenv-closure (physenv-info physenv))))
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))))
85 (aver (eq physenv (block-physenv (nlx-info-target thing))))
86 (ir2-nlx-info-home (nlx-info-info thing)))
89 (entry-info-closure-tn (lambda-info thing))))
90 (bug "~@<~2I~_~S ~_not found in ~_~S~:>" thing physenv)))
92 ;;; If LEAF already has a constant TN, return that, otherwise make a
94 (defun constant-tn (leaf boxedp)
95 (declare (type constant leaf))
96 ;; When convenient we can have both a boxed and unboxed TN for
99 (or (constant-boxed-tn leaf)
100 (setf (constant-boxed-tn leaf) (make-constant-tn leaf t)))
102 (setf (leaf-info leaf) (make-constant-tn leaf nil)))))
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))
111 (unless (lambda-var-indirect leaf)
112 (find-in-physenv leaf env)))
113 (constant (constant-tn leaf boxedp))
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
119 (defun emit-constant (value)
120 (constant-tn (find-constant value) t))
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)))
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)))))
140 (let ((tn (find-in-physenv leaf (node-physenv node)))
141 (indirect (lambda-var-indirect leaf))
142 (explicit (lambda-var-explicit-value-cell leaf)))
144 ((and indirect explicit)
145 (vop value-cell-ref node block tn res))
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))))))
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)))))
156 (emit-move node block (constant-tn leaf (boxed-ref-p node)) res))
158 (ir2-convert-closure node block leaf res))
160 (let ((unsafe (policy node (zerop safety)))
161 (name (leaf-source-name leaf)))
162 (ecase (global-var-kind leaf)
164 (aver (symbolp name))
165 (let ((name-tn (emit-constant name)))
166 (if (or unsafe (info :variable :always-bound name))
167 (vop fast-symbol-value node block name-tn res)
168 (vop symbol-value node block name-tn res))))
170 (aver (symbolp name))
171 (let ((name-tn (emit-constant name)))
172 (if (or unsafe (info :variable :always-bound name))
173 (vop fast-symbol-global-value node block name-tn res)
174 (vop symbol-global-value node block name-tn res))))
176 (let ((fdefn-tn (make-load-time-constant-tn :fdefinition name)))
178 (vop fdefn-fun node block fdefn-tn res)
179 (vop safe-fdefn-fun node block fdefn-tn res))))))))
180 (move-lvar-result node block locs lvar))
183 ;;; some sanity checks for a CLAMBDA passed to IR2-CONVERT-CLOSURE
184 (defun assertions-on-ir2-converted-clambda (clambda)
185 ;; This assertion was sort of an experiment. It would be nice and
186 ;; sane and easier to understand things if it were *always* true,
187 ;; but experimentally I observe that it's only *almost* always
188 ;; true. -- WHN 2001-01-02
190 (aver (eql (lambda-component clambda)
191 (block-component (ir2-block-block ir2-block))))
192 ;; Check for some weirdness which came up in bug
195 ;; The MAKE-LOAD-TIME-CONSTANT-TN call above puts an :ENTRY record
196 ;; into the IR2-COMPONENT-CONSTANTS table. The dump-a-COMPONENT
198 ;; * treats every HANDLEless :ENTRY record into a
200 ;; * expects every patch to correspond to an
201 ;; IR2-COMPONENT-ENTRIES record.
202 ;; The IR2-COMPONENT-ENTRIES records are set by ENTRY-ANALYZE
203 ;; walking over COMPONENT-LAMBDAS. Bug 138b arose because there
204 ;; was a HANDLEless :ENTRY record which didn't correspond to an
205 ;; IR2-COMPONENT-ENTRIES record. That problem is hard to debug
206 ;; when it's caught at dump time, so this assertion tries to catch
208 (aver (member clambda
209 (component-lambdas (lambda-component clambda))))
210 ;; another bug-138-related issue: COMPONENT-NEW-FUNCTIONALS is
211 ;; used as a queue for stuff pending to do in IR1, and now that
212 ;; we're doing IR2 it should've been completely flushed (but
214 (aver (null (component-new-functionals (lambda-component clambda))))
217 ;;; Emit code to load a function object implementing FUNCTIONAL into
218 ;;; RES. This gets interesting when the referenced function is a
219 ;;; closure: we must make the closure and move the closed-over values
222 ;;; FUNCTIONAL is either a :TOPLEVEL-XEP functional or the XEP lambda
223 ;;; for the called function, since local call analysis converts all
224 ;;; closure references. If a :TOPLEVEL-XEP, we know it is not a
227 ;;; If a closed-over LAMBDA-VAR has no refs (is deleted), then we
228 ;;; don't initialize that slot. This can happen with closures over
229 ;;; top level variables, where optimization of the closure deleted the
230 ;;; variable. Since we committed to the closure format when we
231 ;;; pre-analyzed the top level code, we just leave an empty slot.
232 (defun ir2-convert-closure (ref ir2-block functional res)
233 (declare (type ref ref)
234 (type ir2-block ir2-block)
235 (type functional functional)
237 (aver (not (eql (functional-kind functional) :deleted)))
238 (unless (leaf-info functional)
239 (setf (leaf-info functional)
240 (make-entry-info :name (functional-debug-name functional))))
241 (let ((closure (etypecase functional
243 (assertions-on-ir2-converted-clambda functional)
244 (physenv-closure (get-lambda-physenv functional)))
246 (aver (eq (functional-kind functional) :toplevel-xep))
250 (let* ((physenv (node-physenv ref))
251 (tn (find-in-physenv functional physenv)))
252 (emit-move ref ir2-block tn res)))
254 (let ((entry (make-load-time-constant-tn :entry functional)))
255 (emit-move ref ir2-block entry res)))))
258 (defun closure-initial-value (what this-env current-fp)
259 (declare (type (or nlx-info lambda-var clambda) what)
260 (type physenv this-env)
261 (type (or tn null) current-fp))
262 ;; If we have an indirect LAMBDA-VAR that does not require an
263 ;; EXPLICIT-VALUE-CELL, and is from this environment (not from being
264 ;; closed over), we need to store the current frame pointer.
265 (if (and (lambda-var-p what)
266 (lambda-var-indirect what)
267 (not (lambda-var-explicit-value-cell what))
268 (eq (lambda-physenv (lambda-var-home what))
271 (find-in-physenv what this-env)))
273 (defoptimizer (%allocate-closures ltn-annotate) ((leaves) node ltn-policy)
274 ltn-policy ; a hack to effectively (DECLARE (IGNORE LTN-POLICY))
275 (when (lvar-dynamic-extent leaves)
276 (let ((info (make-ir2-lvar *backend-t-primitive-type*)))
277 (setf (ir2-lvar-kind info) :delayed)
278 (setf (lvar-info leaves) info)
279 (setf (ir2-lvar-stack-pointer info)
280 (make-stack-pointer-tn)))))
282 (defoptimizer (%allocate-closures ir2-convert) ((leaves) call 2block)
283 (let ((dx-p (lvar-dynamic-extent leaves)))
286 (vop current-stack-pointer call 2block
287 (ir2-lvar-stack-pointer (lvar-info leaves))))
288 (dolist (leaf (lvar-value leaves))
289 (binding* ((xep (awhen (functional-entry-fun leaf)
290 ;; if the xep's been deleted then we can skip it
291 (if (eq (functional-kind it) :deleted)
294 (nil (aver (xep-p xep)))
295 (entry-info (lambda-info xep) :exit-if-null)
296 (tn (entry-info-closure-tn entry-info) :exit-if-null)
297 (closure (physenv-closure (get-lambda-physenv xep)))
298 (entry (make-load-time-constant-tn :entry xep)))
299 (let ((this-env (node-physenv call))
300 (leaf-dx-p (and dx-p (leaf-dynamic-extent leaf))))
301 (vop make-closure call 2block entry (length closure)
303 (loop for what in closure and n from 0 do
304 (unless (and (lambda-var-p what)
305 (null (leaf-refs what)))
306 ;; In LABELS a closure may refer to another closure
307 ;; in the same group, so we must be sure that we
308 ;; store a closure only after its creation.
310 ;; TODO: Here is a simple solution: we postpone
311 ;; putting of all closures after all creations
312 ;; (though it may require more registers).
314 (delayed (list tn (find-in-physenv what this-env) n))
315 (let ((initial-value (closure-initial-value
318 (vop closure-init call 2block
320 ;; An initial-value of NIL means to stash
321 ;; the frame pointer... which requires a
323 (vop closure-init-from-fp call 2block tn n)))))))))
324 (loop for (tn what n) in (delayed)
325 do (vop closure-init call 2block
329 ;;; Convert a SET node. If the NODE's LVAR is annotated, then we also
330 ;;; deliver the value to that lvar. If the var is a lexical variable
331 ;;; with no refs, then we don't actually set anything, since the
332 ;;; variable has been deleted.
333 (defun ir2-convert-set (node block)
334 (declare (type cset node) (type ir2-block block))
335 (let* ((lvar (node-lvar node))
336 (leaf (set-var node))
337 (val (lvar-tn node block (set-value node)))
340 lvar (list (primitive-type (leaf-type leaf))))
344 (when (leaf-refs leaf)
345 (let ((tn (find-in-physenv leaf (node-physenv node)))
346 (indirect (lambda-var-indirect leaf))
347 (explicit (lambda-var-explicit-value-cell leaf)))
349 ((and indirect explicit)
350 (vop value-cell-set node block tn val))
352 (not (eq (node-physenv node)
353 (lambda-physenv (lambda-var-home leaf)))))
354 (let ((setter (fourth (primitive-type-indirect-cell-type
355 (primitive-type (leaf-type leaf))))))
357 (funcall setter node block tn val (leaf-info leaf))
358 (vop ancestor-frame-set node block tn val (leaf-info leaf)))))
359 (t (emit-move node block val tn))))))
361 (aver (symbolp (leaf-source-name leaf)))
362 (ecase (global-var-kind leaf)
364 (vop set node block (emit-constant (leaf-source-name leaf)) val))
366 (vop %set-symbol-global-value node
367 block (emit-constant (leaf-source-name leaf)) val)))))
369 (emit-move node block val (first locs))
370 (move-lvar-result node block locs lvar)))
373 ;;;; utilities for receiving fixed values
375 ;;; Return a TN that can be referenced to get the value of LVAR. LVAR
376 ;;; must be LTN-ANNOTATED either as a delayed leaf ref or as a fixed,
377 ;;; single-value lvar.
379 ;;; The primitive-type of the result will always be the same as the
380 ;;; IR2-LVAR-PRIMITIVE-TYPE, ensuring that VOPs are always called with
381 ;;; TNs that satisfy the operand primitive-type restriction. We may
382 ;;; have to make a temporary of the desired type and move the actual
383 ;;; lvar TN into it. This happens when we delete a type check in
384 ;;; unsafe code or when we locally know something about the type of an
385 ;;; argument variable.
386 (defun lvar-tn (node block lvar)
387 (declare (type node node) (type ir2-block block) (type lvar lvar))
388 (let* ((2lvar (lvar-info lvar))
390 (ecase (ir2-lvar-kind 2lvar)
392 (let ((ref (lvar-uses lvar)))
393 (leaf-tn (ref-leaf ref) (node-physenv ref) (boxed-ref-p ref))))
395 (aver (= (length (ir2-lvar-locs 2lvar)) 1))
396 (first (ir2-lvar-locs 2lvar)))))
397 (ptype (ir2-lvar-primitive-type 2lvar)))
399 (cond ((eq (tn-primitive-type lvar-tn) ptype) lvar-tn)
401 (let ((temp (make-normal-tn ptype)))
402 (emit-move node block lvar-tn temp)
405 ;;; This is similar to LVAR-TN, but hacks multiple values. We return
406 ;;; TNs holding the values of LVAR with PTYPES as their primitive
407 ;;; types. LVAR must be annotated for the same number of fixed values
408 ;;; are there are PTYPES.
410 ;;; If the lvar has a type check, check the values into temps and
411 ;;; return the temps. When we have more values than assertions, we
412 ;;; move the extra values with no check.
413 (defun lvar-tns (node block lvar ptypes)
414 (declare (type node node) (type ir2-block block)
415 (type lvar lvar) (list ptypes))
416 (let* ((locs (ir2-lvar-locs (lvar-info lvar)))
417 (nlocs (length locs)))
418 (aver (= nlocs (length ptypes)))
420 (mapcar (lambda (from to-type)
421 (if (eq (tn-primitive-type from) to-type)
423 (let ((temp (make-normal-tn to-type)))
424 (emit-move node block from temp)
429 ;;;; utilities for delivering values to lvars
431 ;;; Return a list of TNs with the specifier TYPES that can be used as
432 ;;; result TNs to evaluate an expression into LVAR. This is used
433 ;;; together with MOVE-LVAR-RESULT to deliver fixed values to
436 ;;; If the lvar isn't annotated (meaning the values are discarded) or
437 ;;; is unknown-values, the then we make temporaries for each supplied
438 ;;; value, providing a place to compute the result in until we decide
439 ;;; what to do with it (if anything.)
441 ;;; If the lvar is fixed-values, and wants the same number of values
442 ;;; as the user wants to deliver, then we just return the
443 ;;; IR2-LVAR-LOCS. Otherwise we make a new list padded as necessary by
444 ;;; discarded TNs. We always return a TN of the specified type, using
445 ;;; the lvar locs only when they are of the correct type.
446 (defun lvar-result-tns (lvar types)
447 (declare (type (or lvar null) lvar) (type list types))
449 (mapcar #'make-normal-tn types)
450 (let ((2lvar (lvar-info lvar)))
451 (ecase (ir2-lvar-kind 2lvar)
453 (let* ((locs (ir2-lvar-locs 2lvar))
454 (nlocs (length locs))
455 (ntypes (length types)))
456 (if (and (= nlocs ntypes)
457 (do ((loc locs (cdr loc))
458 (type types (cdr type)))
460 (unless (eq (tn-primitive-type (car loc)) (car type))
463 (mapcar (lambda (loc type)
464 (if (eq (tn-primitive-type loc) type)
466 (make-normal-tn type)))
469 (mapcar #'make-normal-tn
470 (subseq types nlocs)))
474 (mapcar #'make-normal-tn types))))))
476 ;;; Make the first N standard value TNs, returning them in a list.
477 (defun make-standard-value-tns (n)
478 (declare (type unsigned-byte n))
481 (res (standard-arg-location i)))
484 ;;; Return a list of TNs wired to the standard value passing
485 ;;; conventions that can be used to receive values according to the
486 ;;; unknown-values convention. This is used with together
487 ;;; MOVE-LVAR-RESULT for delivering unknown values to a fixed values
490 ;;; If the lvar isn't annotated, then we treat as 0-values, returning
491 ;;; an empty list of temporaries.
493 ;;; If the lvar is annotated, then it must be :FIXED.
494 (defun standard-result-tns (lvar)
495 (declare (type (or lvar null) lvar))
497 (let ((2lvar (lvar-info lvar)))
498 (ecase (ir2-lvar-kind 2lvar)
500 (make-standard-value-tns (length (ir2-lvar-locs 2lvar))))))
503 ;;; Just move each SRC TN into the corresponding DEST TN, defaulting
504 ;;; any unsupplied source values to NIL. We let EMIT-MOVE worry about
505 ;;; doing the appropriate coercions.
506 (defun move-results-coerced (node block src dest)
507 (declare (type node node) (type ir2-block block) (list src dest))
508 (let ((nsrc (length src))
509 (ndest (length dest)))
510 (mapc (lambda (from to)
512 (emit-move node block from to)))
514 (append src (make-list (- ndest nsrc)
515 :initial-element (emit-constant nil)))
520 ;;; Move each SRC TN into the corresponding DEST TN, checking types
521 ;;; and defaulting any unsupplied source values to NIL
522 (defun move-results-checked (node block src dest types)
523 (declare (type node node) (type ir2-block block) (list src dest types))
524 (let ((nsrc (length src))
525 (ndest (length dest))
526 (ntypes (length types)))
527 (mapc (lambda (from to type)
529 (emit-type-check node block from to type)
530 (emit-move node block from to)))
532 (append src (make-list (- ndest nsrc)
533 :initial-element (emit-constant nil)))
537 (append types (make-list (- ndest ntypes)))
541 ;;; If necessary, emit coercion code needed to deliver the RESULTS to
542 ;;; the specified lvar. NODE and BLOCK provide context for emitting
543 ;;; code. Although usually obtained from STANDARD-RESULT-TNs or
544 ;;; LVAR-RESULT-TNs, RESULTS my be a list of any type or
547 ;;; If the lvar is fixed values, then move the results into the lvar
548 ;;; locations. If the lvar is unknown values, then do the moves into
549 ;;; the standard value locations, and use PUSH-VALUES to put the
550 ;;; values on the stack.
551 (defun move-lvar-result (node block results lvar)
552 (declare (type node node) (type ir2-block block)
553 (list results) (type (or lvar null) lvar))
555 (let ((2lvar (lvar-info lvar)))
556 (ecase (ir2-lvar-kind 2lvar)
558 (let ((locs (ir2-lvar-locs 2lvar)))
559 (unless (eq locs results)
560 (move-results-coerced node block results locs))))
562 (let* ((nvals (length results))
563 (locs (make-standard-value-tns nvals)))
564 (move-results-coerced node block results locs)
565 (vop* push-values node block
566 ((reference-tn-list locs nil))
567 ((reference-tn-list (ir2-lvar-locs 2lvar) t))
572 (defun ir2-convert-cast (node block)
573 (declare (type cast node)
574 (type ir2-block block))
575 (binding* ((lvar (node-lvar node) :exit-if-null)
576 (2lvar (lvar-info lvar))
577 (value (cast-value node))
578 (2value (lvar-info value)))
579 (cond ((eq (ir2-lvar-kind 2lvar) :unused))
580 ((eq (ir2-lvar-kind 2lvar) :unknown)
581 (aver (eq (ir2-lvar-kind 2value) :unknown))
582 (aver (not (cast-type-check node)))
583 (move-results-coerced node block
584 (ir2-lvar-locs 2value)
585 (ir2-lvar-locs 2lvar)))
586 ((eq (ir2-lvar-kind 2lvar) :fixed)
587 (aver (eq (ir2-lvar-kind 2value) :fixed))
588 (if (cast-type-check node)
589 (move-results-checked node block
590 (ir2-lvar-locs 2value)
591 (ir2-lvar-locs 2lvar)
592 (multiple-value-bind (check types)
593 (cast-check-types node nil)
594 (aver (eq check :simple))
596 (move-results-coerced node block
597 (ir2-lvar-locs 2value)
598 (ir2-lvar-locs 2lvar))))
599 (t (bug "CAST cannot be :DELAYED.")))))
601 ;;;; template conversion
603 ;;; Build a TN-REFS list that represents access to the values of the
604 ;;; specified list of lvars ARGS for TEMPLATE. Any :CONSTANT arguments
605 ;;; are returned in the second value as a list rather than being
606 ;;; accessed as a normal argument. NODE and BLOCK provide the context
607 ;;; for emitting any necessary type-checking code.
608 (defun reference-args (node block args template)
609 (declare (type node node) (type ir2-block block) (list args)
610 (type template template))
611 (collect ((info-args))
614 (do ((args args (cdr args))
615 (types (template-arg-types template) (cdr types)))
617 (let ((type (first types))
619 (if (and (consp type) (eq (car type) ':constant))
620 (info-args (lvar-value arg))
621 (let ((ref (reference-tn (lvar-tn node block arg) nil)))
623 (setf (tn-ref-across last) ref)
627 (values (the (or tn-ref null) first) (info-args)))))
629 ;;; Convert a conditional template. We try to exploit any
630 ;;; drop-through, but emit an unconditional branch afterward if we
631 ;;; fail. NOT-P is true if the sense of the TEMPLATE's test should be
633 (defun ir2-convert-conditional (node block template args info-args if not-p)
634 (declare (type node node) (type ir2-block block)
635 (type template template) (type (or tn-ref null) args)
636 (list info-args) (type cif if) (type boolean not-p))
637 (let ((consequent (if-consequent if))
638 (alternative (if-alternative if))
639 (flags (and (consp (template-result-types template))
640 (rest (template-result-types template)))))
641 (aver (= (template-info-arg-count template)
642 (+ (length info-args)
645 (rotatef consequent alternative)
647 (when (drop-thru-p if consequent)
648 (rotatef consequent alternative)
651 (emit-template node block template args nil
652 (list* (block-label consequent) not-p
654 (if (drop-thru-p if alternative)
655 (register-drop-thru alternative)
656 (vop branch node block (block-label alternative))))
658 (emit-template node block template args nil info-args)
659 (vop branch-if node block (block-label consequent) flags not-p)
660 (if (drop-thru-p if alternative)
661 (register-drop-thru alternative)
662 (vop branch node block (block-label alternative)))))))
664 ;;; Convert an IF that isn't the DEST of a conditional template.
665 (defun ir2-convert-if (node block)
666 (declare (type ir2-block block) (type cif node))
667 (let* ((test (if-test node))
668 (test-ref (reference-tn (lvar-tn node block test) nil))
669 (nil-ref (reference-tn (emit-constant nil) nil)))
670 (setf (tn-ref-across test-ref) nil-ref)
671 (ir2-convert-conditional node block (template-or-lose 'if-eq)
672 test-ref () node t)))
674 ;;; Return a list of primitive-types that we can pass to LVAR-RESULT-TNS
675 ;;; describing the result types we want for a template call. We are really
676 ;;; only interested in the number of results required: in normal case
677 ;;; TEMPLATE-RESULTS-OK has already checked them.
678 (defun find-template-result-types (call rtypes)
679 (let* ((type (node-derived-type call))
681 (mapcar #'primitive-type
682 (if (args-type-p type)
683 (append (args-type-required type)
684 (args-type-optional type))
686 (primitive-t *backend-t-primitive-type*))
687 (loop for rtype in rtypes
688 for type = (or (pop types) primitive-t)
691 ;;; Return a list of TNs usable in a CALL to TEMPLATE delivering values to
692 ;;; LVAR. As an efficiency hack, we pick off the common case where the LVAR is
693 ;;; fixed values and has locations that satisfy the result restrictions. This
694 ;;; can fail when there is a type check or a values count mismatch.
695 (defun make-template-result-tns (call lvar rtypes)
696 (declare (type combination call) (type (or lvar null) lvar)
698 (let ((2lvar (when lvar (lvar-info lvar))))
699 (if (and 2lvar (eq (ir2-lvar-kind 2lvar) :fixed))
700 (let ((locs (ir2-lvar-locs 2lvar)))
701 (if (and (= (length rtypes) (length locs))
702 (do ((loc locs (cdr loc))
703 (rtypes rtypes (cdr rtypes)))
705 (unless (operand-restriction-ok
707 (tn-primitive-type (car loc))
713 (find-template-result-types call rtypes))))
716 (find-template-result-types call rtypes)))))
718 ;;; Get the operands into TNs, make TN-REFs for them, and then call
719 ;;; the template emit function.
720 (defun ir2-convert-template (call block)
721 (declare (type combination call) (type ir2-block block))
722 (let* ((template (combination-info call))
723 (lvar (node-lvar call))
724 (rtypes (template-result-types template)))
725 (multiple-value-bind (args info-args)
726 (reference-args call block (combination-args call) template)
727 (aver (not (template-more-results-type template)))
728 (if (template-conditional-p template)
729 (ir2-convert-conditional call block template args info-args
730 (lvar-dest lvar) nil)
731 (let* ((results (make-template-result-tns call lvar rtypes))
732 (r-refs (reference-tn-list results t)))
733 (aver (= (length info-args)
734 (template-info-arg-count template)))
735 (when (and lvar (lvar-dynamic-extent lvar))
736 (vop current-stack-pointer call block
737 (ir2-lvar-stack-pointer (lvar-info lvar))))
738 (when (emit-step-p call)
739 (vop sb!vm::step-instrument-before-vop call block))
741 (emit-template call block template args r-refs info-args)
742 (emit-template call block template args r-refs))
743 (move-lvar-result call block results lvar)))))
746 ;;; We don't have to do much because operand count checking is done by
747 ;;; IR1 conversion. The only difference between this and the function
748 ;;; case of IR2-CONVERT-TEMPLATE is that there can be codegen-info
750 (defoptimizer (%%primitive ir2-convert) ((template info &rest args) call block)
751 (let* ((template (lvar-value template))
752 (info (lvar-value info))
753 (lvar (node-lvar call))
754 (rtypes (template-result-types template))
755 (results (make-template-result-tns call lvar rtypes))
756 (r-refs (reference-tn-list results t)))
757 (multiple-value-bind (args info-args)
758 (reference-args call block (cddr (combination-args call)) template)
759 (aver (not (template-more-results-type template)))
760 (aver (not (template-conditional-p template)))
761 (aver (null info-args))
764 (emit-template call block template args r-refs info)
765 (emit-template call block template args r-refs))
767 (move-lvar-result call block results lvar)))
770 (defoptimizer (%%primitive derive-type) ((template info &rest args))
771 (let ((type (template-type (lvar-value template))))
772 (if (fun-type-p type)
773 (fun-type-returns type)
778 ;;; Convert a LET by moving the argument values into the variables.
779 ;;; Since a LET doesn't have any passing locations, we move the
780 ;;; arguments directly into the variables. We must also allocate any
781 ;;; indirect value cells, since there is no function prologue to do
783 (defun ir2-convert-let (node block fun)
784 (declare (type combination node) (type ir2-block block) (type clambda fun))
785 (mapc (lambda (var arg)
787 (let ((src (lvar-tn node block arg))
788 (dest (leaf-info var)))
789 (if (and (lambda-var-indirect var)
790 (lambda-var-explicit-value-cell var))
791 (emit-make-value-cell node block src dest)
792 (emit-move node block src dest)))))
793 (lambda-vars fun) (basic-combination-args node))
796 ;;; Emit any necessary moves into assignment temps for a local call to
797 ;;; FUN. We return two lists of TNs: TNs holding the actual argument
798 ;;; values, and (possibly EQ) TNs that are the actual destination of
799 ;;; the arguments. When necessary, we allocate temporaries for
800 ;;; arguments to preserve parallel assignment semantics. These lists
801 ;;; exclude unused arguments and include implicit environment
802 ;;; arguments, i.e. they exactly correspond to the arguments passed.
804 ;;; OLD-FP is the TN currently holding the value we want to pass as
805 ;;; OLD-FP. If null, then the call is to the same environment (an
806 ;;; :ASSIGNMENT), so we only move the arguments, and leave the
807 ;;; environment alone.
809 ;;; CLOSURE-FP is for calling a closure that has "implicit" value
810 ;;; cells (stored in the allocating stack frame), and is the frame
811 ;;; pointer TN to use for values allocated in the outbound stack
812 ;;; frame. This is distinct from OLD-FP for the specific case of a
814 (defun emit-psetq-moves (node block fun old-fp &optional (closure-fp old-fp))
815 (declare (type combination node) (type ir2-block block) (type clambda fun)
816 (type (or tn null) old-fp closure-fp))
817 (let ((actuals (mapcar (lambda (x)
819 (lvar-tn node block x)))
820 (combination-args node))))
823 (dolist (var (lambda-vars fun))
824 (let ((actual (pop actuals))
825 (loc (leaf-info var)))
828 ((and (lambda-var-indirect var)
829 (lambda-var-explicit-value-cell var))
831 (make-normal-tn *backend-t-primitive-type*)))
832 (emit-make-value-cell node block actual temp)
834 ((member actual (locs))
835 (let ((temp (make-normal-tn (tn-primitive-type loc))))
836 (emit-move node block actual temp)
843 (let ((this-1env (node-physenv node))
844 (called-env (physenv-info (lambda-physenv fun))))
845 (dolist (thing (ir2-physenv-closure called-env))
846 (temps (closure-initial-value (car thing) this-1env closure-fp))
849 (locs (ir2-physenv-old-fp called-env))))
851 (values (temps) (locs)))))
853 ;;; A tail-recursive local call is done by emitting moves of stuff
854 ;;; into the appropriate passing locations. After setting up the args
855 ;;; and environment, we just move our return-pc into the called
856 ;;; function's passing location.
857 (defun ir2-convert-tail-local-call (node block fun)
858 (declare (type combination node) (type ir2-block block) (type clambda fun))
859 (let ((this-env (physenv-info (node-physenv node)))
860 (current-fp (make-stack-pointer-tn)))
861 (multiple-value-bind (temps locs)
862 (emit-psetq-moves node block fun
863 (ir2-physenv-old-fp this-env) current-fp)
865 ;; If we're about to emit a move from CURRENT-FP then we need to
867 (when (find current-fp temps)
868 (vop current-fp node block current-fp))
870 (mapc (lambda (temp loc)
871 (emit-move node block temp loc))
874 (emit-move node block
875 (ir2-physenv-return-pc this-env)
876 (ir2-physenv-return-pc-pass
878 (lambda-physenv fun)))))
882 ;;; Convert an :ASSIGNMENT call. This is just like a tail local call,
883 ;;; except that the caller and callee environment are the same, so we
884 ;;; don't need to mess with the environment locations, return PC, etc.
885 (defun ir2-convert-assignment (node block fun)
886 (declare (type combination node) (type ir2-block block) (type clambda fun))
887 (multiple-value-bind (temps locs) (emit-psetq-moves node block fun nil)
889 (mapc (lambda (temp loc)
890 (emit-move node block temp loc))
894 ;;; Do stuff to set up the arguments to a non-tail local call
895 ;;; (including implicit environment args.) We allocate a frame
896 ;;; (returning the FP and NFP), and also compute the TN-REFS list for
897 ;;; the values to pass and the list of passing location TNs.
898 (defun ir2-convert-local-call-args (node block fun)
899 (declare (type combination node) (type ir2-block block) (type clambda fun))
900 (let ((fp (make-stack-pointer-tn))
901 (nfp (make-number-stack-pointer-tn))
902 (old-fp (make-stack-pointer-tn)))
903 (multiple-value-bind (temps locs)
904 (emit-psetq-moves node block fun old-fp)
905 (vop current-fp node block old-fp)
906 (vop allocate-frame node block
907 (physenv-info (lambda-physenv fun))
909 (values fp nfp temps (mapcar #'make-alias-tn locs)))))
911 ;;; Handle a non-TR known-values local call. We emit the call, then
912 ;;; move the results to the lvar's destination.
913 (defun ir2-convert-local-known-call (node block fun returns lvar start)
914 (declare (type node node) (type ir2-block block) (type clambda fun)
915 (type return-info returns) (type (or lvar null) lvar)
917 (multiple-value-bind (fp nfp temps arg-locs)
918 (ir2-convert-local-call-args node block fun)
919 (let ((locs (return-info-locations returns)))
920 (vop* known-call-local node block
921 (fp nfp (reference-tn-list temps nil))
922 ((reference-tn-list locs t))
923 arg-locs (physenv-info (lambda-physenv fun)) start)
924 (move-lvar-result node block locs lvar)))
927 ;;; Handle a non-TR unknown-values local call. We do different things
928 ;;; depending on what kind of values the lvar wants.
930 ;;; If LVAR is :UNKNOWN, then we use the "multiple-" variant, directly
931 ;;; specifying the lvar's LOCS as the VOP results so that we don't
932 ;;; have to do anything after the call.
934 ;;; Otherwise, we use STANDARD-RESULT-TNS to get wired result TNs, and
935 ;;; then call MOVE-LVAR-RESULT to do any necessary type checks or
937 (defun ir2-convert-local-unknown-call (node block fun lvar start)
938 (declare (type node node) (type ir2-block block) (type clambda fun)
939 (type (or lvar null) lvar) (type label start))
940 (multiple-value-bind (fp nfp temps arg-locs)
941 (ir2-convert-local-call-args node block fun)
942 (let ((2lvar (and lvar (lvar-info lvar)))
943 (env (physenv-info (lambda-physenv fun)))
944 (temp-refs (reference-tn-list temps nil)))
945 (if (and 2lvar (eq (ir2-lvar-kind 2lvar) :unknown))
946 (vop* multiple-call-local node block (fp nfp temp-refs)
947 ((reference-tn-list (ir2-lvar-locs 2lvar) t))
949 (let ((locs (standard-result-tns lvar)))
950 (vop* call-local node block
952 ((reference-tn-list locs t))
953 arg-locs env start (length locs))
954 (move-lvar-result node block locs lvar)))))
957 ;;; Dispatch to the appropriate function, depending on whether we have
958 ;;; a let, tail or normal call. If the function doesn't return, call
959 ;;; it using the unknown-value convention. We could compile it as a
960 ;;; tail call, but that might seem confusing in the debugger.
961 (defun ir2-convert-local-call (node block)
962 (declare (type combination node) (type ir2-block block))
963 (let* ((fun (ref-leaf (lvar-uses (basic-combination-fun node))))
964 (kind (functional-kind fun)))
965 (cond ((eq kind :let)
966 (ir2-convert-let node block fun))
967 ((eq kind :assignment)
968 (ir2-convert-assignment node block fun))
970 (ir2-convert-tail-local-call node block fun))
972 (let ((start (block-trampoline (lambda-block fun)))
973 (returns (tail-set-info (lambda-tail-set fun)))
974 (lvar (node-lvar node)))
976 (return-info-kind returns)
979 (ir2-convert-local-unknown-call node block fun lvar start))
981 (ir2-convert-local-known-call node block fun returns
987 ;;; Given a function lvar FUN, return (VALUES TN-TO-CALL NAMED-P),
988 ;;; where TN-TO-CALL is a TN holding the thing that we call NAMED-P is
989 ;;; true if the thing is named (false if it is a function).
991 ;;; There are two interesting non-named cases:
992 ;;; -- We know it's a function. No check needed: return the
994 ;;; -- We don't know what it is.
995 (defun fun-lvar-tn (node block lvar)
996 (declare (ignore node block))
997 (declare (type lvar lvar))
998 (let ((2lvar (lvar-info lvar)))
999 (if (eq (ir2-lvar-kind 2lvar) :delayed)
1000 (let ((name (lvar-fun-name lvar t)))
1002 (values (make-load-time-constant-tn :fdefinition name) t))
1003 (let* ((locs (ir2-lvar-locs 2lvar))
1005 (function-ptype (primitive-type-or-lose 'function)))
1006 (aver (and (eq (ir2-lvar-kind 2lvar) :fixed)
1007 (= (length locs) 1)))
1008 (aver (eq (tn-primitive-type loc) function-ptype))
1009 (values loc nil)))))
1011 ;;; Set up the args to NODE in the current frame, and return a TN-REF
1012 ;;; list for the passing locations.
1013 (defun move-tail-full-call-args (node block)
1014 (declare (type combination node) (type ir2-block block))
1015 (let ((args (basic-combination-args node))
1018 (dotimes (num (length args))
1019 (let ((loc (standard-arg-location num)))
1020 (emit-move node block (lvar-tn node block (elt args num)) loc)
1021 (let ((ref (reference-tn loc nil)))
1023 (setf (tn-ref-across last) ref)
1028 ;;; Move the arguments into the passing locations and do a (possibly
1029 ;;; named) tail call.
1030 (defun ir2-convert-tail-full-call (node block)
1031 (declare (type combination node) (type ir2-block block))
1032 (let* ((env (physenv-info (node-physenv node)))
1033 (args (basic-combination-args node))
1034 (nargs (length args))
1035 (pass-refs (move-tail-full-call-args node block))
1036 (old-fp (ir2-physenv-old-fp env))
1037 (return-pc (ir2-physenv-return-pc env)))
1039 (multiple-value-bind (fun-tn named)
1040 (fun-lvar-tn node block (basic-combination-fun node))
1042 (vop* tail-call-named node block
1043 (fun-tn old-fp return-pc pass-refs)
1047 (vop* tail-call node block
1048 (fun-tn old-fp return-pc pass-refs)
1051 (emit-step-p node)))))
1055 ;;; like IR2-CONVERT-LOCAL-CALL-ARGS, only different
1056 (defun ir2-convert-full-call-args (node block)
1057 (declare (type combination node) (type ir2-block block))
1058 (let* ((args (basic-combination-args node))
1059 (fp (make-stack-pointer-tn))
1060 (nargs (length args)))
1061 (vop allocate-full-call-frame node block nargs fp)
1065 (dotimes (num nargs)
1066 (locs (standard-arg-location num))
1067 (let ((ref (reference-tn (lvar-tn node block (elt args num))
1070 (setf (tn-ref-across last) ref)
1074 (values fp first (locs) nargs)))))
1076 ;;; Do full call when a fixed number of values are desired. We make
1077 ;;; STANDARD-RESULT-TNS for our lvar, then deliver the result using
1078 ;;; MOVE-LVAR-RESULT. We do named or normal call, as appropriate.
1079 (defun ir2-convert-fixed-full-call (node block)
1080 (declare (type combination node) (type ir2-block block))
1081 (multiple-value-bind (fp args arg-locs nargs)
1082 (ir2-convert-full-call-args node block)
1083 (let* ((lvar (node-lvar node))
1084 (locs (standard-result-tns lvar))
1085 (loc-refs (reference-tn-list locs t))
1086 (nvals (length locs)))
1087 (multiple-value-bind (fun-tn named)
1088 (fun-lvar-tn node block (basic-combination-fun node))
1090 (vop* call-named node block (fp fun-tn args) (loc-refs)
1091 arg-locs nargs nvals (emit-step-p node))
1092 (vop* call node block (fp fun-tn args) (loc-refs)
1093 arg-locs nargs nvals (emit-step-p node)))
1094 (move-lvar-result node block locs lvar))))
1097 ;;; Do full call when unknown values are desired.
1098 (defun ir2-convert-multiple-full-call (node block)
1099 (declare (type combination node) (type ir2-block block))
1100 (multiple-value-bind (fp args arg-locs nargs)
1101 (ir2-convert-full-call-args node block)
1102 (let* ((lvar (node-lvar node))
1103 (locs (ir2-lvar-locs (lvar-info lvar)))
1104 (loc-refs (reference-tn-list locs t)))
1105 (multiple-value-bind (fun-tn named)
1106 (fun-lvar-tn node block (basic-combination-fun node))
1108 (vop* multiple-call-named node block (fp fun-tn args) (loc-refs)
1109 arg-locs nargs (emit-step-p node))
1110 (vop* multiple-call node block (fp fun-tn args) (loc-refs)
1111 arg-locs nargs (emit-step-p node))))))
1114 ;;; stuff to check in PONDER-FULL-CALL
1116 ;;; These came in handy when troubleshooting cold boot after making
1117 ;;; major changes in the package structure: various transforms and
1118 ;;; VOPs and stuff got attached to the wrong symbol, so that
1119 ;;; references to the right symbol were bogusly translated as full
1120 ;;; calls instead of primitives, sending the system off into infinite
1121 ;;; space. Having a report on all full calls generated makes it easier
1122 ;;; to figure out what form caused the problem this time.
1123 #!+sb-show (defvar *show-full-called-fnames-p* nil)
1124 #!+sb-show (defvar *full-called-fnames* (make-hash-table :test 'equal))
1126 ;;; Do some checks (and store some notes relevant for future checks)
1128 ;;; * Is this a full call to something we have reason to know should
1129 ;;; never be full called? (Except as of sbcl-0.7.18 or so, we no
1130 ;;; longer try to ensure this behavior when *FAILURE-P* has already
1132 ;;; * Is this a full call to (SETF FOO) which might conflict with
1133 ;;; a DEFSETF or some such thing elsewhere in the program?
1134 (defun ponder-full-call (node)
1135 (let* ((lvar (basic-combination-fun node))
1136 (fname (lvar-fun-name lvar t)))
1137 (declare (type (or symbol cons) fname))
1139 #!+sb-show (unless (gethash fname *full-called-fnames*)
1140 (setf (gethash fname *full-called-fnames*) t))
1141 #!+sb-show (when *show-full-called-fnames-p*
1142 (/show "converting full call to named function" fname)
1143 (/show (basic-combination-args node))
1144 (/show (policy node speed) (policy node safety))
1145 (/show (policy node compilation-speed))
1146 (let ((arg-types (mapcar (lambda (lvar)
1150 (basic-combination-args node))))
1153 ;; When illegal code is compiled, all sorts of perverse paths
1154 ;; through the compiler can be taken, and it's much harder -- and
1155 ;; probably pointless -- to guarantee that always-optimized-away
1156 ;; functions are actually optimized away. Thus, we skip the check
1159 ;; check to see if we know anything about the function
1160 (let ((info (info :function :info fname)))
1161 ;; if we know something, check to see if the full call was valid
1162 (when (and info (ir1-attributep (fun-info-attributes info)
1163 always-translatable))
1164 (/show (policy node speed) (policy node safety))
1165 (/show (policy node compilation-speed))
1166 (bug "full call to ~S" fname))))
1169 (aver (legal-fun-name-p fname))
1170 (destructuring-bind (setfoid &rest stem) fname
1171 (when (eq setfoid 'setf)
1172 (setf (gethash (car stem) *setf-assumed-fboundp*) t))))))
1174 ;;; If the call is in a tail recursive position and the return
1175 ;;; convention is standard, then do a tail full call. If one or fewer
1176 ;;; values are desired, then use a single-value call, otherwise use a
1177 ;;; multiple-values call.
1178 (defun ir2-convert-full-call (node block)
1179 (declare (type combination node) (type ir2-block block))
1180 (ponder-full-call node)
1181 (cond ((node-tail-p node)
1182 (ir2-convert-tail-full-call node block))
1183 ((let ((lvar (node-lvar node)))
1185 (eq (ir2-lvar-kind (lvar-info lvar)) :unknown)))
1186 (ir2-convert-multiple-full-call node block))
1188 (ir2-convert-fixed-full-call node block)))
1191 ;;;; entering functions
1193 ;;; Do all the stuff that needs to be done on XEP entry:
1194 ;;; -- Create frame.
1195 ;;; -- Copy any more arg.
1196 ;;; -- Set up the environment, accessing any closure variables.
1197 ;;; -- Move args from the standard passing locations to their internal
1199 (defun init-xep-environment (node block fun)
1200 (declare (type bind node) (type ir2-block block) (type clambda fun))
1201 (let ((start-label (entry-info-offset (leaf-info fun)))
1202 (env (physenv-info (node-physenv node))))
1203 (let ((ef (functional-entry-fun fun)))
1204 (cond ((and (optional-dispatch-p ef) (optional-dispatch-more-entry ef))
1205 ;; Special case the xep-allocate-frame + copy-more-arg case.
1206 (vop xep-allocate-frame node block start-label t)
1207 (vop copy-more-arg node block (optional-dispatch-max-args ef)))
1209 ;; No more args, so normal entry.
1210 (vop xep-allocate-frame node block start-label nil)))
1211 (if (ir2-physenv-closure env)
1212 (let ((closure (make-normal-tn *backend-t-primitive-type*)))
1213 (vop setup-closure-environment node block start-label closure)
1215 (dolist (loc (ir2-physenv-closure env))
1216 (vop closure-ref node block closure (incf n) (cdr loc)))))
1217 (vop setup-environment node block start-label)))
1219 (unless (eq (functional-kind fun) :toplevel)
1220 (let ((vars (lambda-vars fun))
1222 (when (leaf-refs (first vars))
1223 (emit-move node block (make-arg-count-location)
1224 (leaf-info (first vars))))
1225 (dolist (arg (rest vars))
1226 (when (leaf-refs arg)
1227 (let ((pass (standard-arg-location n))
1228 (home (leaf-info arg)))
1229 (if (and (lambda-var-indirect arg)
1230 (lambda-var-explicit-value-cell arg))
1231 (emit-make-value-cell node block pass home)
1232 (emit-move node block pass home))))
1235 (emit-move node block (make-old-fp-passing-location t)
1236 (ir2-physenv-old-fp env)))
1240 ;;; Emit function prolog code. This is only called on bind nodes for
1241 ;;; functions that allocate environments. All semantics of let calls
1242 ;;; are handled by IR2-CONVERT-LET.
1244 ;;; If not an XEP, all we do is move the return PC from its passing
1245 ;;; location, since in a local call, the caller allocates the frame
1246 ;;; and sets up the arguments.
1247 (defun ir2-convert-bind (node block)
1248 (declare (type bind node) (type ir2-block block))
1249 (let* ((fun (bind-lambda node))
1250 (env (physenv-info (lambda-physenv fun))))
1251 (aver (member (functional-kind fun)
1252 '(nil :external :optional :toplevel :cleanup)))
1255 (init-xep-environment node block fun)
1257 (when *collect-dynamic-statistics*
1258 (vop count-me node block *dynamic-counts-tn*
1259 (block-number (ir2-block-block block)))))
1263 (ir2-physenv-return-pc-pass env)
1264 (ir2-physenv-return-pc env))
1266 #!+unwind-to-frame-and-call-vop
1267 (when (and (lambda-allow-instrumenting fun)
1268 (not (lambda-inline-expanded fun))
1270 (policy fun (>= insert-debug-catch 2)))
1271 (vop sb!vm::bind-sentinel node block))
1273 (let ((lab (gen-label)))
1274 (setf (ir2-physenv-environment-start env) lab)
1275 (vop note-environment-start node block lab)))
1279 ;;;; function return
1281 ;;; Do stuff to return from a function with the specified values and
1282 ;;; convention. If the return convention is :FIXED and we aren't
1283 ;;; returning from an XEP, then we do a known return (letting
1284 ;;; representation selection insert the correct move-arg VOPs.)
1285 ;;; Otherwise, we use the unknown-values convention. If there is a
1286 ;;; fixed number of return values, then use RETURN, otherwise use
1287 ;;; RETURN-MULTIPLE.
1288 (defun ir2-convert-return (node block)
1289 (declare (type creturn node) (type ir2-block block))
1290 (let* ((lvar (return-result node))
1291 (2lvar (lvar-info lvar))
1292 (lvar-kind (ir2-lvar-kind 2lvar))
1293 (fun (return-lambda node))
1294 (env (physenv-info (lambda-physenv fun)))
1295 (old-fp (ir2-physenv-old-fp env))
1296 (return-pc (ir2-physenv-return-pc env))
1297 (returns (tail-set-info (lambda-tail-set fun))))
1298 #!+unwind-to-frame-and-call-vop
1299 (when (and (lambda-allow-instrumenting fun)
1300 (not (lambda-inline-expanded fun))
1301 (policy fun (>= insert-debug-catch 2)))
1302 (vop sb!vm::unbind-sentinel node block))
1304 ((and (eq (return-info-kind returns) :fixed)
1306 (let ((locs (lvar-tns node block lvar
1307 (return-info-types returns))))
1308 (vop* known-return node block
1309 (old-fp return-pc (reference-tn-list locs nil))
1311 (return-info-locations returns))))
1312 ((eq lvar-kind :fixed)
1313 (let* ((types (mapcar #'tn-primitive-type (ir2-lvar-locs 2lvar)))
1314 (lvar-locs (lvar-tns node block lvar types))
1315 (nvals (length lvar-locs))
1316 (locs (make-standard-value-tns nvals)))
1317 (mapc (lambda (val loc)
1318 (emit-move node block val loc))
1322 (vop return-single node block old-fp return-pc (car locs))
1323 (vop* return node block
1324 (old-fp return-pc (reference-tn-list locs nil))
1328 (aver (eq lvar-kind :unknown))
1329 (vop* return-multiple node block
1331 (reference-tn-list (ir2-lvar-locs 2lvar) nil))
1338 ;;;; These are used by the debugger to find the top function on the
1339 ;;;; stack. They return the OLD-FP and RETURN-PC for the current
1340 ;;;; function as multiple values.
1342 (defoptimizer (%caller-frame ir2-convert) (() node block)
1343 (let ((ir2-physenv (physenv-info (node-physenv node))))
1344 (move-lvar-result node block
1345 (list (ir2-physenv-old-fp ir2-physenv))
1348 (defoptimizer (%caller-pc ir2-convert) (() node block)
1349 (let ((ir2-physenv (physenv-info (node-physenv node))))
1350 (move-lvar-result node block
1351 (list (ir2-physenv-return-pc ir2-physenv))
1354 ;;;; multiple values
1356 ;;; This is almost identical to IR2-CONVERT-LET. Since LTN annotates
1357 ;;; the lvar for the correct number of values (with the lvar user
1358 ;;; responsible for defaulting), we can just pick them up from the
1360 (defun ir2-convert-mv-bind (node block)
1361 (declare (type mv-combination node) (type ir2-block block))
1362 (let* ((lvar (first (basic-combination-args node)))
1363 (fun (ref-leaf (lvar-uses (basic-combination-fun node))))
1364 (vars (lambda-vars fun)))
1365 (aver (eq (functional-kind fun) :mv-let))
1366 (mapc (lambda (src var)
1367 (when (leaf-refs var)
1368 (let ((dest (leaf-info var)))
1369 (if (and (lambda-var-indirect var)
1370 (lambda-var-explicit-value-cell var))
1371 (emit-make-value-cell node block src dest)
1372 (emit-move node block src dest)))))
1373 (lvar-tns node block lvar
1375 (primitive-type (leaf-type x)))
1380 ;;; Emit the appropriate fixed value, unknown value or tail variant of
1381 ;;; CALL-VARIABLE. Note that we only need to pass the values start for
1382 ;;; the first argument: all the other argument lvar TNs are
1383 ;;; ignored. This is because we require all of the values globs to be
1384 ;;; contiguous and on stack top.
1385 (defun ir2-convert-mv-call (node block)
1386 (declare (type mv-combination node) (type ir2-block block))
1387 (aver (basic-combination-args node))
1388 (let* ((start-lvar (lvar-info (first (basic-combination-args node))))
1389 (start (first (ir2-lvar-locs start-lvar)))
1390 (tails (and (node-tail-p node)
1391 (lambda-tail-set (node-home-lambda node))))
1392 (lvar (node-lvar node))
1393 (2lvar (and lvar (lvar-info lvar))))
1394 (multiple-value-bind (fun named)
1395 (fun-lvar-tn node block (basic-combination-fun node))
1396 (aver (and (not named)
1397 (eq (ir2-lvar-kind start-lvar) :unknown)))
1400 (let ((env (physenv-info (node-physenv node))))
1401 (vop tail-call-variable node block start fun
1402 (ir2-physenv-old-fp env)
1403 (ir2-physenv-return-pc env))))
1405 (eq (ir2-lvar-kind 2lvar) :unknown))
1406 (vop* multiple-call-variable node block (start fun nil)
1407 ((reference-tn-list (ir2-lvar-locs 2lvar) t))
1408 (emit-step-p node)))
1410 (let ((locs (standard-result-tns lvar)))
1411 (vop* call-variable node block (start fun nil)
1412 ((reference-tn-list locs t)) (length locs)
1414 (move-lvar-result node block locs lvar)))))))
1416 ;;; Reset the stack pointer to the start of the specified
1417 ;;; unknown-values lvar (discarding it and all values globs on top of
1419 (defoptimizer (%pop-values ir2-convert) ((%lvar) node block)
1420 (let* ((lvar (lvar-value %lvar))
1421 (2lvar (lvar-info lvar)))
1422 (cond ((eq (ir2-lvar-kind 2lvar) :unknown)
1423 (vop reset-stack-pointer node block
1424 (first (ir2-lvar-locs 2lvar))))
1425 ((lvar-dynamic-extent lvar)
1426 (vop reset-stack-pointer node block
1427 (ir2-lvar-stack-pointer 2lvar)))
1428 (t (bug "Trying to pop a not stack-allocated LVAR ~S."
1431 (defoptimizer (%nip-values ir2-convert) ((last-nipped last-preserved
1434 (let* ( ;; pointer immediately after the nipped block
1435 (after (lvar-value last-nipped))
1436 (2after (lvar-info after))
1437 ;; pointer to the first nipped word
1438 (first (lvar-value last-preserved))
1439 (2first (lvar-info first))
1441 (moved-tns (loop for lvar-ref in moved
1442 for lvar = (lvar-value lvar-ref)
1443 for 2lvar = (lvar-info lvar)
1445 collect (first (ir2-lvar-locs 2lvar)))))
1446 (aver (or (eq (ir2-lvar-kind 2after) :unknown)
1447 (lvar-dynamic-extent after)))
1448 (aver (eq (ir2-lvar-kind 2first) :unknown))
1449 (when *check-consistency*
1450 ;; we cannot move stack-allocated DX objects
1451 (dolist (moved-lvar moved)
1452 (aver (eq (ir2-lvar-kind (lvar-info (lvar-value moved-lvar)))
1454 (flet ((nip-aligned (nipped)
1455 (vop* %%nip-values node block
1457 (first (ir2-lvar-locs 2first))
1458 (reference-tn-list moved-tns nil))
1459 ((reference-tn-list moved-tns t)))))
1460 (cond ((eq (ir2-lvar-kind 2after) :unknown)
1461 (nip-aligned (first (ir2-lvar-locs 2after))))
1462 ((lvar-dynamic-extent after)
1463 (nip-aligned (ir2-lvar-stack-pointer 2after)))
1465 (bug "Trying to nip a not stack-allocated LVAR ~S." after))))))
1467 ;;; Deliver the values TNs to LVAR using MOVE-LVAR-RESULT.
1468 (defoptimizer (values ir2-convert) ((&rest values) node block)
1469 (let ((tns (mapcar (lambda (x)
1470 (lvar-tn node block x))
1472 (move-lvar-result node block tns (node-lvar node))))
1474 ;;; In the normal case where unknown values are desired, we use the
1475 ;;; VALUES-LIST VOP. In the relatively unimportant case of VALUES-LIST
1476 ;;; for a fixed number of values, we punt by doing a full call to the
1477 ;;; VALUES-LIST function. This gets the full call VOP to deal with
1478 ;;; defaulting any unsupplied values. It seems unworthwhile to
1479 ;;; optimize this case.
1480 (defoptimizer (values-list ir2-convert) ((list) node block)
1481 (let* ((lvar (node-lvar node))
1482 (2lvar (and lvar (lvar-info lvar))))
1484 (eq (ir2-lvar-kind 2lvar) :unknown))
1485 (let ((locs (ir2-lvar-locs 2lvar)))
1486 (vop* values-list node block
1487 ((lvar-tn node block list) nil)
1488 ((reference-tn-list locs t)))))
1489 (t (aver (or (not 2lvar) ; i.e. we want to check the argument
1490 (eq (ir2-lvar-kind 2lvar) :fixed)))
1491 (ir2-convert-full-call node block)))))
1493 (defoptimizer (%more-arg-values ir2-convert) ((context start count) node block)
1494 (binding* ((lvar (node-lvar node) :exit-if-null)
1495 (2lvar (lvar-info lvar)))
1496 (ecase (ir2-lvar-kind 2lvar)
1498 ;; KLUDGE: this is very much unsafe, and can leak random stack values.
1499 ;; OTOH, I think the :FIXED case can only happen with (safety 0) in the
1502 (loop for loc in (ir2-lvar-locs 2lvar)
1504 do (vop sb!vm::more-arg node block
1505 (lvar-tn node block context)
1509 (let ((locs (ir2-lvar-locs 2lvar)))
1510 (vop* %more-arg-values node block
1511 ((lvar-tn node block context)
1512 (lvar-tn node block start)
1513 (lvar-tn node block count)
1515 ((reference-tn-list locs t))))))))
1517 ;;;; special binding
1519 ;;; This is trivial, given our assumption of a shallow-binding
1521 (defoptimizer (%special-bind ir2-convert) ((var value) node block)
1522 (let ((name (leaf-source-name (lvar-value var))))
1523 (vop bind node block (lvar-tn node block value)
1524 (emit-constant name))))
1525 (defoptimizer (%special-unbind ir2-convert) ((var) node block)
1526 (vop unbind node block))
1528 ;;; ### It's not clear that this really belongs in this file, or
1529 ;;; should really be done this way, but this is the least violation of
1530 ;;; abstraction in the current setup. We don't want to wire
1531 ;;; shallow-binding assumptions into IR1tran.
1532 (def-ir1-translator progv
1533 ((vars vals &body body) start next result)
1536 (with-unique-names (bind unbind)
1537 (once-only ((n-save-bs '(%primitive current-binding-pointer)))
1540 (labels ((,unbind (vars)
1541 (declare (optimize (speed 2) (debug 0)))
1542 (let ((unbound-marker (%primitive make-other-immediate-type
1543 0 sb!vm:unbound-marker-widetag)))
1545 ;; CLHS says "bound and then made to have no value" -- user
1546 ;; should not be able to tell the difference between that and this.
1547 (about-to-modify-symbol-value var 'progv)
1548 (%primitive bind unbound-marker var))))
1550 (declare (optimize (speed 2) (debug 0)
1551 (insert-debug-catch 0)))
1553 ((null vals) (,unbind vars))
1555 (let ((val (car vals))
1557 (about-to-modify-symbol-value var 'progv val t)
1558 (%primitive bind val var))
1559 (,bind (cdr vars) (cdr vals))))))
1560 (,bind ,vars ,vals))
1563 ;; Technically ANSI CL doesn't allow declarations at the
1564 ;; start of the cleanup form. SBCL happens to allow for
1565 ;; them, due to the way the UNWIND-PROTECT ir1 translation
1566 ;; is implemented; the cleanup forms are directly spliced
1567 ;; into an FLET definition body. And a declaration here
1568 ;; actually has exactly the right scope for what we need
1569 ;; (ensure that debug instrumentation is not emitted for the
1570 ;; cleanup function). -- JES, 2007-06-16
1571 (declare (optimize (insert-debug-catch 0)))
1572 (%primitive unbind-to-here ,n-save-bs))))))
1576 ;;; Convert a non-local lexical exit. First find the NLX-INFO in our
1577 ;;; environment. Note that this is never called on the escape exits
1578 ;;; for CATCH and UNWIND-PROTECT, since the escape functions aren't
1580 (defun ir2-convert-exit (node block)
1581 (declare (type exit node) (type ir2-block block))
1582 (let* ((nlx (exit-nlx-info node))
1583 (loc (find-in-physenv nlx (node-physenv node)))
1584 (temp (make-stack-pointer-tn))
1585 (value (exit-value node)))
1586 (if (nlx-info-safe-p nlx)
1587 (vop value-cell-ref node block loc temp)
1588 (emit-move node block loc temp))
1590 (let ((locs (ir2-lvar-locs (lvar-info value))))
1591 (vop unwind node block temp (first locs) (second locs)))
1592 (let ((0-tn (emit-constant 0)))
1593 (vop unwind node block temp 0-tn 0-tn))))
1597 ;;; %CLEANUP-POINT doesn't do anything except prevent the body from
1598 ;;; being entirely deleted.
1599 (defoptimizer (%cleanup-point ir2-convert) (() node block) node block)
1601 ;;; This function invalidates a lexical exit on exiting from the
1602 ;;; dynamic extent. This is done by storing 0 into the indirect value
1603 ;;; cell that holds the closed unwind block.
1604 (defoptimizer (%lexical-exit-breakup ir2-convert) ((info) node block)
1605 (let ((nlx (lvar-value info)))
1606 (when (nlx-info-safe-p nlx)
1607 (vop value-cell-set node block
1608 (find-in-physenv nlx (node-physenv node))
1609 (emit-constant 0)))))
1611 ;;; We have to do a spurious move of no values to the result lvar so
1612 ;;; that lifetime analysis won't get confused.
1613 (defun ir2-convert-throw (node block)
1614 (declare (type mv-combination node) (type ir2-block block))
1615 (let ((args (basic-combination-args node)))
1616 (check-catch-tag-type (first args))
1617 (vop* throw node block
1618 ((lvar-tn node block (first args))
1620 (ir2-lvar-locs (lvar-info (second args)))
1623 (move-lvar-result node block () (node-lvar node))
1626 ;;; Emit code to set up a non-local exit. INFO is the NLX-INFO for the
1627 ;;; exit, and TAG is the lvar for the catch tag (if any.) We get at
1628 ;;; the target PC by passing in the label to the vop. The vop is
1629 ;;; responsible for building a return-PC object.
1630 (defun emit-nlx-start (node block info tag)
1631 (declare (type node node) (type ir2-block block) (type nlx-info info)
1632 (type (or lvar null) tag))
1633 (let* ((2info (nlx-info-info info))
1634 (kind (cleanup-kind (nlx-info-cleanup info)))
1635 (block-tn (physenv-live-tn
1636 (make-normal-tn (primitive-type-or-lose 'catch-block))
1637 (node-physenv node)))
1638 (res (make-stack-pointer-tn))
1639 (target-label (ir2-nlx-info-target 2info)))
1641 (vop current-binding-pointer node block
1642 (car (ir2-nlx-info-dynamic-state 2info)))
1643 (vop* save-dynamic-state node block
1645 ((reference-tn-list (cdr (ir2-nlx-info-dynamic-state 2info)) t)))
1646 (vop current-stack-pointer node block (ir2-nlx-info-save-sp 2info))
1650 (vop make-catch-block node block block-tn
1651 (lvar-tn node block tag) target-label res))
1652 ((:unwind-protect :block :tagbody)
1653 (vop make-unwind-block node block block-tn target-label res)))
1657 (if (nlx-info-safe-p info)
1658 (emit-make-value-cell node block res (ir2-nlx-info-home 2info))
1659 (emit-move node block res (ir2-nlx-info-home 2info))))
1661 (vop set-unwind-protect node block block-tn))
1666 ;;; Scan each of ENTRY's exits, setting up the exit for each lexical exit.
1667 (defun ir2-convert-entry (node block)
1668 (declare (type entry node) (type ir2-block block))
1670 (dolist (exit (entry-exits node))
1671 (let ((info (exit-nlx-info exit)))
1673 (not (memq info nlxes))
1674 (member (cleanup-kind (nlx-info-cleanup info))
1675 '(:block :tagbody)))
1677 (emit-nlx-start node block info nil)))))
1680 ;;; Set up the unwind block for these guys.
1681 (defoptimizer (%catch ir2-convert) ((info-lvar tag) node block)
1682 (check-catch-tag-type tag)
1683 (emit-nlx-start node block (lvar-value info-lvar) tag))
1684 (defoptimizer (%unwind-protect ir2-convert) ((info-lvar cleanup) node block)
1685 (emit-nlx-start node block (lvar-value info-lvar) nil))
1687 ;;; Emit the entry code for a non-local exit. We receive values and
1688 ;;; restore dynamic state.
1690 ;;; In the case of a lexical exit or CATCH, we look at the exit lvar's
1691 ;;; kind to determine which flavor of entry VOP to emit. If unknown
1692 ;;; values, emit the xxx-MULTIPLE variant to the lvar locs. If fixed
1693 ;;; values, make the appropriate number of temps in the standard
1694 ;;; values locations and use the other variant, delivering the temps
1695 ;;; to the lvar using MOVE-LVAR-RESULT.
1697 ;;; In the UNWIND-PROTECT case, we deliver the first register
1698 ;;; argument, the argument count and the argument pointer to our lvar
1699 ;;; as multiple values. These values are the block exited to and the
1700 ;;; values start and count.
1702 ;;; After receiving values, we restore dynamic state. Except in the
1703 ;;; UNWIND-PROTECT case, the values receiving restores the stack
1704 ;;; pointer. In an UNWIND-PROTECT cleanup, we want to leave the stack
1705 ;;; pointer alone, since the thrown values are still out there.
1706 (defoptimizer (%nlx-entry ir2-convert) ((info-lvar) node block)
1707 (let* ((info (lvar-value info-lvar))
1708 (lvar (node-lvar node))
1709 (2info (nlx-info-info info))
1710 (top-loc (ir2-nlx-info-save-sp 2info))
1711 (start-loc (make-nlx-entry-arg-start-location))
1712 (count-loc (make-arg-count-location))
1713 (target (ir2-nlx-info-target 2info)))
1715 (ecase (cleanup-kind (nlx-info-cleanup info))
1716 ((:catch :block :tagbody)
1717 (let ((2lvar (and lvar (lvar-info lvar))))
1718 (if (and 2lvar (eq (ir2-lvar-kind 2lvar) :unknown))
1719 (vop* nlx-entry-multiple node block
1720 (top-loc start-loc count-loc nil)
1721 ((reference-tn-list (ir2-lvar-locs 2lvar) t))
1723 (let ((locs (standard-result-tns lvar)))
1724 (vop* nlx-entry node block
1725 (top-loc start-loc count-loc nil)
1726 ((reference-tn-list locs t))
1729 (move-lvar-result node block locs lvar)))))
1731 (let ((block-loc (standard-arg-location 0)))
1732 (vop uwp-entry node block target block-loc start-loc count-loc)
1735 (list block-loc start-loc count-loc)
1739 (when *collect-dynamic-statistics*
1740 (vop count-me node block *dynamic-counts-tn*
1741 (block-number (ir2-block-block block))))
1743 (vop* restore-dynamic-state node block
1744 ((reference-tn-list (cdr (ir2-nlx-info-dynamic-state 2info)) nil))
1746 (vop unbind-to-here node block
1747 (car (ir2-nlx-info-dynamic-state 2info)))))
1749 ;;;; n-argument functions
1751 (macrolet ((def (name)
1752 `(defoptimizer (,name ir2-convert) ((&rest args) node block)
1753 (let* ((refs (move-tail-full-call-args node block))
1754 (lvar (node-lvar node))
1755 (res (lvar-result-tns
1757 (list (primitive-type (specifier-type 'list))))))
1758 (when (and lvar (lvar-dynamic-extent lvar))
1759 (vop current-stack-pointer node block
1760 (ir2-lvar-stack-pointer (lvar-info lvar))))
1761 (vop* ,name node block (refs) ((first res) nil)
1763 (move-lvar-result node block res lvar)))))
1768 ;;; Convert the code in a component into VOPs.
1769 (defun ir2-convert (component)
1770 (declare (type component component))
1771 (let (#!+sb-dyncount
1772 (*dynamic-counts-tn*
1773 (when *collect-dynamic-statistics*
1775 (block-number (block-next (component-head component))))
1776 (counts (make-array blocks
1777 :element-type '(unsigned-byte 32)
1778 :initial-element 0))
1779 (info (make-dyncount-info
1780 :for (component-name component)
1781 :costs (make-array blocks
1782 :element-type '(unsigned-byte 32)
1785 (setf (ir2-component-dyncount-info (component-info component))
1787 (emit-constant info)
1788 (emit-constant counts)))))
1790 (declare (type index num))
1791 (do-ir2-blocks (2block component)
1792 (let ((block (ir2-block-block 2block)))
1793 (when (block-start block)
1794 (setf (block-number block) num)
1796 (when *collect-dynamic-statistics*
1797 (let ((first-node (block-start-node block)))
1798 (unless (or (and (bind-p first-node)
1799 (xep-p (bind-lambda first-node)))
1801 (node-lvar first-node))
1806 #!+sb-dyncount *dynamic-counts-tn* #!-sb-dyncount nil
1808 (ir2-convert-block block)
1812 ;;; If necessary, emit a terminal unconditional branch to go to the
1813 ;;; successor block. If the successor is the component tail, then
1814 ;;; there isn't really any successor, but if the end is an unknown,
1815 ;;; non-tail call, then we emit an error trap just in case the
1816 ;;; function really does return.
1817 (defun finish-ir2-block (block)
1818 (declare (type cblock block))
1819 (let* ((2block (block-info block))
1820 (last (block-last block))
1821 (succ (block-succ block)))
1823 (aver (singleton-p succ))
1824 (let ((target (first succ)))
1825 (cond ((eq target (component-tail (block-component block)))
1826 (when (and (basic-combination-p last)
1827 (eq (basic-combination-kind last) :full))
1828 (let* ((fun (basic-combination-fun last))
1829 (use (lvar-uses fun))
1830 (name (and (ref-p use)
1831 (leaf-has-source-name-p (ref-leaf use))
1832 (leaf-source-name (ref-leaf use)))))
1833 (unless (or (node-tail-p last)
1834 (info :function :info name)
1835 (policy last (zerop safety)))
1836 (vop nil-fun-returned-error last 2block
1838 (emit-constant name)
1839 (multiple-value-bind (tn named)
1840 (fun-lvar-tn last 2block fun)
1843 ((not (eq (ir2-block-next 2block) (block-info target)))
1844 (vop branch last 2block (block-label target)))
1846 (register-drop-thru target))))))
1850 ;;; Convert the code in a block into VOPs.
1851 (defun ir2-convert-block (block)
1852 (declare (type cblock block))
1853 (let ((2block (block-info block)))
1854 (do-nodes (node lvar block)
1858 (let ((2lvar (lvar-info lvar)))
1859 ;; function REF in a local call is not annotated
1860 (when (and 2lvar (not (eq (ir2-lvar-kind 2lvar) :delayed)))
1861 (ir2-convert-ref node 2block)))))
1863 (let ((kind (basic-combination-kind node)))
1866 (ir2-convert-local-call node 2block))
1868 (ir2-convert-full-call node 2block))
1870 (let* ((info (basic-combination-fun-info node))
1871 (fun (fun-info-ir2-convert info)))
1873 (funcall fun node 2block))
1874 ((eq (basic-combination-info node) :full)
1875 (ir2-convert-full-call node 2block))
1877 (ir2-convert-template node 2block))))))))
1879 (when (lvar-info (if-test node))
1880 (ir2-convert-if node 2block)))
1882 (let ((fun (bind-lambda node)))
1883 (when (eq (lambda-home fun) fun)
1884 (ir2-convert-bind node 2block))))
1886 (ir2-convert-return node 2block))
1888 (ir2-convert-set node 2block))
1890 (ir2-convert-cast node 2block))
1893 ((eq (basic-combination-kind node) :local)
1894 (ir2-convert-mv-bind node 2block))
1895 ((eq (lvar-fun-name (basic-combination-fun node))
1897 (ir2-convert-throw node 2block))
1899 (ir2-convert-mv-call node 2block))))
1901 (when (exit-entry node)
1902 (ir2-convert-exit node 2block)))
1904 (ir2-convert-entry node 2block)))))
1906 (finish-ir2-block block)