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 (vop make-value-cell node block value
62 (and leaf (eq :truly (leaf-dynamic-extent leaf)))
67 ;;; Return the TN that holds the value of THING in the environment ENV.
68 (declaim (ftype (function ((or nlx-info lambda-var clambda) physenv) tn)
70 (defun find-in-physenv (thing physenv)
71 (or (cdr (assoc thing (ir2-physenv-closure (physenv-info physenv))))
74 ;; I think that a failure of this assertion means that we're
75 ;; trying to access a variable which was improperly closed
76 ;; over. The PHYSENV describes a physical environment. Every
77 ;; variable that a form refers to should either be in its
78 ;; physical environment directly, or grabbed from a
79 ;; surrounding physical environment when it was closed over.
80 ;; The ASSOC expression above finds closed-over variables, so
81 ;; if we fell through the ASSOC expression, it wasn't closed
82 ;; over. Therefore, it must be in our physical environment
83 ;; directly. If instead it is in some other physical
84 ;; environment, then it's bogus for us to reference it here
85 ;; without it being closed over. -- WHN 2001-09-29
86 (aver (eq physenv (lambda-physenv (lambda-var-home thing))))
89 (aver (eq physenv (block-physenv (nlx-info-target thing))))
90 (ir2-nlx-info-home (nlx-info-info thing)))
93 (entry-info-closure-tn (lambda-info thing))))
94 (bug "~@<~2I~_~S ~_not found in ~_~S~:>" thing physenv)))
96 ;;; If LEAF already has a constant TN, return that, otherwise make a
98 (defun constant-tn (leaf)
99 (declare (type constant leaf))
101 (setf (leaf-info leaf)
102 (make-constant-tn leaf))))
104 ;;; Return a TN that represents the value of LEAF, or NIL if LEAF
105 ;;; isn't directly represented by a TN. ENV is the environment that
106 ;;; the reference is done in.
107 (defun leaf-tn (leaf env)
108 (declare (type leaf leaf) (type physenv env))
111 (unless (lambda-var-indirect leaf)
112 (find-in-physenv leaf env)))
113 (constant (constant-tn leaf))
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)))
122 ;;; Convert a REF node. The reference must not be delayed.
123 (defun ir2-convert-ref (node block)
124 (declare (type ref node) (type ir2-block block))
125 (let* ((lvar (node-lvar node))
126 (leaf (ref-leaf node))
127 (locs (lvar-result-tns
128 lvar (list (primitive-type (leaf-type leaf)))))
132 (let ((tn (find-in-physenv leaf (node-physenv node))))
133 (if (lambda-var-indirect leaf)
134 (vop value-cell-ref node block tn res)
135 (emit-move node block tn res))))
137 (emit-move node block (constant-tn leaf) res))
139 (ir2-convert-closure node block leaf res))
141 (let ((unsafe (policy node (zerop safety)))
142 (name (leaf-source-name leaf)))
143 (ecase (global-var-kind leaf)
145 (aver (symbolp name))
146 (let ((name-tn (emit-constant name)))
147 (if (or unsafe (info :variable :always-bound name))
148 (vop fast-symbol-value node block name-tn res)
149 (vop symbol-value node block name-tn res))))
151 (aver (symbolp name))
152 (let ((name-tn (emit-constant name)))
153 (if (or unsafe (info :variable :always-bound name))
154 (vop fast-symbol-global-value node block name-tn res)
155 (vop symbol-global-value node block name-tn res))))
157 (let ((fdefn-tn (make-load-time-constant-tn :fdefinition name)))
159 (vop fdefn-fun node block fdefn-tn res)
160 (vop safe-fdefn-fun node block fdefn-tn res))))))))
161 (move-lvar-result node block locs lvar))
164 ;;; some sanity checks for a CLAMBDA passed to IR2-CONVERT-CLOSURE
165 (defun assertions-on-ir2-converted-clambda (clambda)
166 ;; This assertion was sort of an experiment. It would be nice and
167 ;; sane and easier to understand things if it were *always* true,
168 ;; but experimentally I observe that it's only *almost* always
169 ;; true. -- WHN 2001-01-02
171 (aver (eql (lambda-component clambda)
172 (block-component (ir2-block-block ir2-block))))
173 ;; Check for some weirdness which came up in bug
176 ;; The MAKE-LOAD-TIME-CONSTANT-TN call above puts an :ENTRY record
177 ;; into the IR2-COMPONENT-CONSTANTS table. The dump-a-COMPONENT
179 ;; * treats every HANDLEless :ENTRY record into a
181 ;; * expects every patch to correspond to an
182 ;; IR2-COMPONENT-ENTRIES record.
183 ;; The IR2-COMPONENT-ENTRIES records are set by ENTRY-ANALYZE
184 ;; walking over COMPONENT-LAMBDAS. Bug 138b arose because there
185 ;; was a HANDLEless :ENTRY record which didn't correspond to an
186 ;; IR2-COMPONENT-ENTRIES record. That problem is hard to debug
187 ;; when it's caught at dump time, so this assertion tries to catch
189 (aver (member clambda
190 (component-lambdas (lambda-component clambda))))
191 ;; another bug-138-related issue: COMPONENT-NEW-FUNCTIONALS is
192 ;; used as a queue for stuff pending to do in IR1, and now that
193 ;; we're doing IR2 it should've been completely flushed (but
195 (aver (null (component-new-functionals (lambda-component clambda))))
198 ;;; Emit code to load a function object implementing FUNCTIONAL into
199 ;;; RES. This gets interesting when the referenced function is a
200 ;;; closure: we must make the closure and move the closed-over values
203 ;;; FUNCTIONAL is either a :TOPLEVEL-XEP functional or the XEP lambda
204 ;;; for the called function, since local call analysis converts all
205 ;;; closure references. If a :TOPLEVEL-XEP, we know it is not a
208 ;;; If a closed-over LAMBDA-VAR has no refs (is deleted), then we
209 ;;; don't initialize that slot. This can happen with closures over
210 ;;; top level variables, where optimization of the closure deleted the
211 ;;; variable. Since we committed to the closure format when we
212 ;;; pre-analyzed the top level code, we just leave an empty slot.
213 (defun ir2-convert-closure (ref ir2-block functional res)
214 (declare (type ref ref)
215 (type ir2-block ir2-block)
216 (type functional functional)
218 (aver (not (eql (functional-kind functional) :deleted)))
219 (unless (leaf-info functional)
220 (setf (leaf-info functional)
221 (make-entry-info :name (functional-debug-name functional))))
222 (let ((closure (etypecase functional
224 (assertions-on-ir2-converted-clambda functional)
225 (physenv-closure (get-lambda-physenv functional)))
227 (aver (eq (functional-kind functional) :toplevel-xep))
231 (let* ((physenv (node-physenv ref))
232 (tn (find-in-physenv functional physenv)))
233 (emit-move ref ir2-block tn res)))
235 (let ((entry (make-load-time-constant-tn :entry functional)))
236 (emit-move ref ir2-block entry res)))))
239 (defoptimizer (%allocate-closures ltn-annotate) ((leaves) node ltn-policy)
240 ltn-policy ; a hack to effectively (DECLARE (IGNORE LTN-POLICY))
241 (when (lvar-dynamic-extent leaves)
242 (let ((info (make-ir2-lvar *backend-t-primitive-type*)))
243 (setf (ir2-lvar-kind info) :delayed)
244 (setf (lvar-info leaves) info)
245 (setf (ir2-lvar-stack-pointer info)
246 (make-stack-pointer-tn)))))
248 (defoptimizer (%allocate-closures ir2-convert) ((leaves) call 2block)
249 (let ((dx-p (lvar-dynamic-extent leaves)))
252 (vop current-stack-pointer call 2block
253 (ir2-lvar-stack-pointer (lvar-info leaves))))
254 (dolist (leaf (lvar-value leaves))
255 (binding* ((xep (functional-entry-fun leaf) :exit-if-null)
256 (nil (aver (xep-p xep)))
257 (entry-info (lambda-info xep) :exit-if-null)
258 (tn (entry-info-closure-tn entry-info) :exit-if-null)
259 (closure (physenv-closure (get-lambda-physenv xep)))
260 (entry (make-load-time-constant-tn :entry xep)))
261 (let ((this-env (node-physenv call))
262 (leaf-dx-p (and dx-p (leaf-dynamic-extent leaf))))
263 (vop make-closure call 2block entry (length closure)
265 (loop for what in closure and n from 0 do
266 (unless (and (lambda-var-p what)
267 (null (leaf-refs what)))
268 ;; In LABELS a closure may refer to another closure
269 ;; in the same group, so we must be sure that we
270 ;; store a closure only after its creation.
272 ;; TODO: Here is a simple solution: we postpone
273 ;; putting of all closures after all creations
274 ;; (though it may require more registers).
276 (delayed (list tn (find-in-physenv what this-env) n))
277 (vop closure-init call 2block
279 (find-in-physenv what this-env)
281 (loop for (tn what n) in (delayed)
282 do (vop closure-init call 2block
286 ;;; Convert a SET node. If the NODE's LVAR is annotated, then we also
287 ;;; deliver the value to that lvar. If the var is a lexical variable
288 ;;; with no refs, then we don't actually set anything, since the
289 ;;; variable has been deleted.
290 (defun ir2-convert-set (node block)
291 (declare (type cset node) (type ir2-block block))
292 (let* ((lvar (node-lvar node))
293 (leaf (set-var node))
294 (val (lvar-tn node block (set-value node)))
297 lvar (list (primitive-type (leaf-type leaf))))
301 (when (leaf-refs leaf)
302 (let ((tn (find-in-physenv leaf (node-physenv node))))
303 (if (lambda-var-indirect leaf)
304 (vop value-cell-set node block tn val)
305 (emit-move node block val tn)))))
307 (aver (symbolp (leaf-source-name leaf)))
308 (ecase (global-var-kind leaf)
310 (vop set node block (emit-constant (leaf-source-name leaf)) val))
312 (vop %set-symbol-global-value node
313 block (emit-constant (leaf-source-name leaf)) val)))))
315 (emit-move node block val (first locs))
316 (move-lvar-result node block locs lvar)))
319 ;;;; utilities for receiving fixed values
321 ;;; Return a TN that can be referenced to get the value of LVAR. LVAR
322 ;;; must be LTN-ANNOTATED either as a delayed leaf ref or as a fixed,
323 ;;; single-value lvar.
325 ;;; The primitive-type of the result will always be the same as the
326 ;;; IR2-LVAR-PRIMITIVE-TYPE, ensuring that VOPs are always called with
327 ;;; TNs that satisfy the operand primitive-type restriction. We may
328 ;;; have to make a temporary of the desired type and move the actual
329 ;;; lvar TN into it. This happens when we delete a type check in
330 ;;; unsafe code or when we locally know something about the type of an
331 ;;; argument variable.
332 (defun lvar-tn (node block lvar)
333 (declare (type node node) (type ir2-block block) (type lvar lvar))
334 (let* ((2lvar (lvar-info lvar))
336 (ecase (ir2-lvar-kind 2lvar)
338 (let ((ref (lvar-uses lvar)))
339 (leaf-tn (ref-leaf ref) (node-physenv ref))))
341 (aver (= (length (ir2-lvar-locs 2lvar)) 1))
342 (first (ir2-lvar-locs 2lvar)))))
343 (ptype (ir2-lvar-primitive-type 2lvar)))
345 (cond ((eq (tn-primitive-type lvar-tn) ptype) lvar-tn)
347 (let ((temp (make-normal-tn ptype)))
348 (emit-move node block lvar-tn temp)
351 ;;; This is similar to LVAR-TN, but hacks multiple values. We return
352 ;;; TNs holding the values of LVAR with PTYPES as their primitive
353 ;;; types. LVAR must be annotated for the same number of fixed values
354 ;;; are there are PTYPES.
356 ;;; If the lvar has a type check, check the values into temps and
357 ;;; return the temps. When we have more values than assertions, we
358 ;;; move the extra values with no check.
359 (defun lvar-tns (node block lvar ptypes)
360 (declare (type node node) (type ir2-block block)
361 (type lvar lvar) (list ptypes))
362 (let* ((locs (ir2-lvar-locs (lvar-info lvar)))
363 (nlocs (length locs)))
364 (aver (= nlocs (length ptypes)))
366 (mapcar (lambda (from to-type)
367 (if (eq (tn-primitive-type from) to-type)
369 (let ((temp (make-normal-tn to-type)))
370 (emit-move node block from temp)
375 ;;;; utilities for delivering values to lvars
377 ;;; Return a list of TNs with the specifier TYPES that can be used as
378 ;;; result TNs to evaluate an expression into LVAR. This is used
379 ;;; together with MOVE-LVAR-RESULT to deliver fixed values to
382 ;;; If the lvar isn't annotated (meaning the values are discarded) or
383 ;;; is unknown-values, the then we make temporaries for each supplied
384 ;;; value, providing a place to compute the result in until we decide
385 ;;; what to do with it (if anything.)
387 ;;; If the lvar is fixed-values, and wants the same number of values
388 ;;; as the user wants to deliver, then we just return the
389 ;;; IR2-LVAR-LOCS. Otherwise we make a new list padded as necessary by
390 ;;; discarded TNs. We always return a TN of the specified type, using
391 ;;; the lvar locs only when they are of the correct type.
392 (defun lvar-result-tns (lvar types)
393 (declare (type (or lvar null) lvar) (type list types))
395 (mapcar #'make-normal-tn types)
396 (let ((2lvar (lvar-info lvar)))
397 (ecase (ir2-lvar-kind 2lvar)
399 (let* ((locs (ir2-lvar-locs 2lvar))
400 (nlocs (length locs))
401 (ntypes (length types)))
402 (if (and (= nlocs ntypes)
403 (do ((loc locs (cdr loc))
404 (type types (cdr type)))
406 (unless (eq (tn-primitive-type (car loc)) (car type))
409 (mapcar (lambda (loc type)
410 (if (eq (tn-primitive-type loc) type)
412 (make-normal-tn type)))
415 (mapcar #'make-normal-tn
416 (subseq types nlocs)))
420 (mapcar #'make-normal-tn types))))))
422 ;;; Make the first N standard value TNs, returning them in a list.
423 (defun make-standard-value-tns (n)
424 (declare (type unsigned-byte n))
427 (res (standard-arg-location i)))
430 ;;; Return a list of TNs wired to the standard value passing
431 ;;; conventions that can be used to receive values according to the
432 ;;; unknown-values convention. This is used with together
433 ;;; MOVE-LVAR-RESULT for delivering unknown values to a fixed values
436 ;;; If the lvar isn't annotated, then we treat as 0-values, returning
437 ;;; an empty list of temporaries.
439 ;;; If the lvar is annotated, then it must be :FIXED.
440 (defun standard-result-tns (lvar)
441 (declare (type (or lvar null) lvar))
443 (let ((2lvar (lvar-info lvar)))
444 (ecase (ir2-lvar-kind 2lvar)
446 (make-standard-value-tns (length (ir2-lvar-locs 2lvar))))))
449 ;;; Just move each SRC TN into the corresponding DEST TN, defaulting
450 ;;; any unsupplied source values to NIL. We let EMIT-MOVE worry about
451 ;;; doing the appropriate coercions.
452 (defun move-results-coerced (node block src dest)
453 (declare (type node node) (type ir2-block block) (list src dest))
454 (let ((nsrc (length src))
455 (ndest (length dest)))
456 (mapc (lambda (from to)
458 (emit-move node block from to)))
460 (append src (make-list (- ndest nsrc)
461 :initial-element (emit-constant nil)))
466 ;;; Move each SRC TN into the corresponding DEST TN, checking types
467 ;;; and defaulting any unsupplied source values to NIL
468 (defun move-results-checked (node block src dest types)
469 (declare (type node node) (type ir2-block block) (list src dest types))
470 (let ((nsrc (length src))
471 (ndest (length dest))
472 (ntypes (length types)))
473 (mapc (lambda (from to type)
475 (emit-type-check node block from to type)
476 (emit-move node block from to)))
478 (append src (make-list (- ndest nsrc)
479 :initial-element (emit-constant nil)))
483 (append types (make-list (- ndest ntypes)))
487 ;;; If necessary, emit coercion code needed to deliver the RESULTS to
488 ;;; the specified lvar. NODE and BLOCK provide context for emitting
489 ;;; code. Although usually obtained from STANDARD-RESULT-TNs or
490 ;;; LVAR-RESULT-TNs, RESULTS my be a list of any type or
493 ;;; If the lvar is fixed values, then move the results into the lvar
494 ;;; locations. If the lvar is unknown values, then do the moves into
495 ;;; the standard value locations, and use PUSH-VALUES to put the
496 ;;; values on the stack.
497 (defun move-lvar-result (node block results lvar)
498 (declare (type node node) (type ir2-block block)
499 (list results) (type (or lvar null) lvar))
501 (let ((2lvar (lvar-info lvar)))
502 (ecase (ir2-lvar-kind 2lvar)
504 (let ((locs (ir2-lvar-locs 2lvar)))
505 (unless (eq locs results)
506 (move-results-coerced node block results locs))))
508 (let* ((nvals (length results))
509 (locs (make-standard-value-tns nvals)))
510 (move-results-coerced node block results locs)
511 (vop* push-values node block
512 ((reference-tn-list locs nil))
513 ((reference-tn-list (ir2-lvar-locs 2lvar) t))
518 (defun ir2-convert-cast (node block)
519 (declare (type cast node)
520 (type ir2-block block))
521 (binding* ((lvar (node-lvar node) :exit-if-null)
522 (2lvar (lvar-info lvar))
523 (value (cast-value node))
524 (2value (lvar-info value)))
525 (cond ((eq (ir2-lvar-kind 2lvar) :unused))
526 ((eq (ir2-lvar-kind 2lvar) :unknown)
527 (aver (eq (ir2-lvar-kind 2value) :unknown))
528 (aver (not (cast-type-check node)))
529 (move-results-coerced node block
530 (ir2-lvar-locs 2value)
531 (ir2-lvar-locs 2lvar)))
532 ((eq (ir2-lvar-kind 2lvar) :fixed)
533 (aver (eq (ir2-lvar-kind 2value) :fixed))
534 (if (cast-type-check node)
535 (move-results-checked node block
536 (ir2-lvar-locs 2value)
537 (ir2-lvar-locs 2lvar)
538 (multiple-value-bind (check types)
539 (cast-check-types node nil)
540 (aver (eq check :simple))
542 (move-results-coerced node block
543 (ir2-lvar-locs 2value)
544 (ir2-lvar-locs 2lvar))))
545 (t (bug "CAST cannot be :DELAYED.")))))
547 ;;;; template conversion
549 ;;; Build a TN-REFS list that represents access to the values of the
550 ;;; specified list of lvars ARGS for TEMPLATE. Any :CONSTANT arguments
551 ;;; are returned in the second value as a list rather than being
552 ;;; accessed as a normal argument. NODE and BLOCK provide the context
553 ;;; for emitting any necessary type-checking code.
554 (defun reference-args (node block args template)
555 (declare (type node node) (type ir2-block block) (list args)
556 (type template template))
557 (collect ((info-args))
560 (do ((args args (cdr args))
561 (types (template-arg-types template) (cdr types)))
563 (let ((type (first types))
565 (if (and (consp type) (eq (car type) ':constant))
566 (info-args (lvar-value arg))
567 (let ((ref (reference-tn (lvar-tn node block arg) nil)))
569 (setf (tn-ref-across last) ref)
573 (values (the (or tn-ref null) first) (info-args)))))
575 ;;; Convert a conditional template. We try to exploit any
576 ;;; drop-through, but emit an unconditional branch afterward if we
577 ;;; fail. NOT-P is true if the sense of the TEMPLATE's test should be
579 (defun ir2-convert-conditional (node block template args info-args if not-p)
580 (declare (type node node) (type ir2-block block)
581 (type template template) (type (or tn-ref null) args)
582 (list info-args) (type cif if) (type boolean not-p))
583 (let ((consequent (if-consequent if))
584 (alternative (if-alternative if))
585 (flags (and (consp (template-result-types template))
586 (rest (template-result-types template)))))
587 (aver (= (template-info-arg-count template)
588 (+ (length info-args)
591 (rotatef consequent alternative)
593 (when (drop-thru-p if consequent)
594 (rotatef consequent alternative)
597 (emit-template node block template args nil
598 (list* (block-label consequent) not-p
600 (unless (drop-thru-p if alternative)
601 (vop branch node block (block-label alternative))))
603 (emit-template node block template args nil info-args)
604 (vop branch-if node block (block-label consequent) flags not-p)
605 (unless (drop-thru-p if alternative)
606 (vop branch node block (block-label alternative)))))))
608 ;;; Convert an IF that isn't the DEST of a conditional template.
609 (defun ir2-convert-if (node block)
610 (declare (type ir2-block block) (type cif node))
611 (let* ((test (if-test node))
612 (test-ref (reference-tn (lvar-tn node block test) nil))
613 (nil-ref (reference-tn (emit-constant nil) nil)))
614 (setf (tn-ref-across test-ref) nil-ref)
615 (ir2-convert-conditional node block (template-or-lose 'if-eq)
616 test-ref () node t)))
618 ;;; Return a list of primitive-types that we can pass to LVAR-RESULT-TNS
619 ;;; describing the result types we want for a template call. We are really
620 ;;; only interested in the number of results required: in normal case
621 ;;; TEMPLATE-RESULTS-OK has already checked them.
622 (defun find-template-result-types (call rtypes)
623 (let* ((type (node-derived-type call))
625 (mapcar #'primitive-type
626 (if (values-type-p type)
627 (append (args-type-required type)
628 (args-type-optional type))
630 (primitive-t *backend-t-primitive-type*))
631 (loop for rtype in rtypes
632 for type = (or (pop types) primitive-t)
635 ;;; Return a list of TNs usable in a CALL to TEMPLATE delivering values to
636 ;;; LVAR. As an efficiency hack, we pick off the common case where the LVAR is
637 ;;; fixed values and has locations that satisfy the result restrictions. This
638 ;;; can fail when there is a type check or a values count mismatch.
639 (defun make-template-result-tns (call lvar rtypes)
640 (declare (type combination call) (type (or lvar null) lvar)
642 (let ((2lvar (when lvar (lvar-info lvar))))
643 (if (and 2lvar (eq (ir2-lvar-kind 2lvar) :fixed))
644 (let ((locs (ir2-lvar-locs 2lvar)))
645 (if (and (= (length rtypes) (length locs))
646 (do ((loc locs (cdr loc))
647 (rtypes rtypes (cdr rtypes)))
649 (unless (operand-restriction-ok
651 (tn-primitive-type (car loc))
657 (find-template-result-types call rtypes))))
660 (find-template-result-types call rtypes)))))
662 ;;; Get the operands into TNs, make TN-REFs for them, and then call
663 ;;; the template emit function.
664 (defun ir2-convert-template (call block)
665 (declare (type combination call) (type ir2-block block))
666 (let* ((template (combination-info call))
667 (lvar (node-lvar call))
668 (rtypes (template-result-types template)))
669 (multiple-value-bind (args info-args)
670 (reference-args call block (combination-args call) template)
671 (aver (not (template-more-results-type template)))
672 (if (template-conditional-p template)
673 (ir2-convert-conditional call block template args info-args
674 (lvar-dest lvar) nil)
675 (let* ((results (make-template-result-tns call lvar rtypes))
676 (r-refs (reference-tn-list results t)))
677 (aver (= (length info-args)
678 (template-info-arg-count template)))
679 (when (and lvar (lvar-dynamic-extent lvar))
680 (vop current-stack-pointer call block
681 (ir2-lvar-stack-pointer (lvar-info lvar))))
682 (when (emit-step-p call)
683 (vop sb!vm::step-instrument-before-vop call block))
685 (emit-template call block template args r-refs info-args)
686 (emit-template call block template args r-refs))
687 (move-lvar-result call block results lvar)))))
690 ;;; We don't have to do much because operand count checking is done by
691 ;;; IR1 conversion. The only difference between this and the function
692 ;;; case of IR2-CONVERT-TEMPLATE is that there can be codegen-info
694 (defoptimizer (%%primitive ir2-convert) ((template info &rest args) call block)
695 (let* ((template (lvar-value template))
696 (info (lvar-value info))
697 (lvar (node-lvar call))
698 (rtypes (template-result-types template))
699 (results (make-template-result-tns call lvar rtypes))
700 (r-refs (reference-tn-list results t)))
701 (multiple-value-bind (args info-args)
702 (reference-args call block (cddr (combination-args call)) template)
703 (aver (not (template-more-results-type template)))
704 (aver (not (template-conditional-p template)))
705 (aver (null info-args))
708 (emit-template call block template args r-refs info)
709 (emit-template call block template args r-refs))
711 (move-lvar-result call block results lvar)))
714 (defoptimizer (%%primitive derive-type) ((template info &rest args))
715 (let ((type (template-type (lvar-value template))))
716 (if (fun-type-p type)
717 (fun-type-returns type)
722 ;;; Convert a LET by moving the argument values into the variables.
723 ;;; Since a LET doesn't have any passing locations, we move the
724 ;;; arguments directly into the variables. We must also allocate any
725 ;;; indirect value cells, since there is no function prologue to do
727 (defun ir2-convert-let (node block fun)
728 (declare (type combination node) (type ir2-block block) (type clambda fun))
729 (mapc (lambda (var arg)
731 (let ((src (lvar-tn node block arg))
732 (dest (leaf-info var)))
733 (if (lambda-var-indirect var)
734 (emit-make-value-cell node block src dest)
735 (emit-move node block src dest)))))
736 (lambda-vars fun) (basic-combination-args node))
739 ;;; Emit any necessary moves into assignment temps for a local call to
740 ;;; FUN. We return two lists of TNs: TNs holding the actual argument
741 ;;; values, and (possibly EQ) TNs that are the actual destination of
742 ;;; the arguments. When necessary, we allocate temporaries for
743 ;;; arguments to preserve parallel assignment semantics. These lists
744 ;;; exclude unused arguments and include implicit environment
745 ;;; arguments, i.e. they exactly correspond to the arguments passed.
747 ;;; OLD-FP is the TN currently holding the value we want to pass as
748 ;;; OLD-FP. If null, then the call is to the same environment (an
749 ;;; :ASSIGNMENT), so we only move the arguments, and leave the
750 ;;; environment alone.
751 (defun emit-psetq-moves (node block fun old-fp)
752 (declare (type combination node) (type ir2-block block) (type clambda fun)
753 (type (or tn null) old-fp))
754 (let ((actuals (mapcar (lambda (x)
756 (lvar-tn node block x)))
757 (combination-args node))))
760 (dolist (var (lambda-vars fun))
761 (let ((actual (pop actuals))
762 (loc (leaf-info var)))
765 ((lambda-var-indirect var)
767 (make-normal-tn *backend-t-primitive-type*)))
768 (emit-make-value-cell node block actual temp)
770 ((member actual (locs))
771 (let ((temp (make-normal-tn (tn-primitive-type loc))))
772 (emit-move node block actual temp)
779 (let ((this-1env (node-physenv node))
780 (called-env (physenv-info (lambda-physenv fun))))
781 (dolist (thing (ir2-physenv-closure called-env))
782 (temps (find-in-physenv (car thing) this-1env))
785 (locs (ir2-physenv-old-fp called-env))))
787 (values (temps) (locs)))))
789 ;;; A tail-recursive local call is done by emitting moves of stuff
790 ;;; into the appropriate passing locations. After setting up the args
791 ;;; and environment, we just move our return-pc into the called
792 ;;; function's passing location.
793 (defun ir2-convert-tail-local-call (node block fun)
794 (declare (type combination node) (type ir2-block block) (type clambda fun))
795 (let ((this-env (physenv-info (node-physenv node))))
796 (multiple-value-bind (temps locs)
797 (emit-psetq-moves node block fun (ir2-physenv-old-fp this-env))
799 (mapc (lambda (temp loc)
800 (emit-move node block temp loc))
803 (emit-move node block
804 (ir2-physenv-return-pc this-env)
805 (ir2-physenv-return-pc-pass
807 (lambda-physenv fun)))))
811 ;;; Convert an :ASSIGNMENT call. This is just like a tail local call,
812 ;;; except that the caller and callee environment are the same, so we
813 ;;; don't need to mess with the environment locations, return PC, etc.
814 (defun ir2-convert-assignment (node block fun)
815 (declare (type combination node) (type ir2-block block) (type clambda fun))
816 (multiple-value-bind (temps locs) (emit-psetq-moves node block fun nil)
818 (mapc (lambda (temp loc)
819 (emit-move node block temp loc))
823 ;;; Do stuff to set up the arguments to a non-tail local call
824 ;;; (including implicit environment args.) We allocate a frame
825 ;;; (returning the FP and NFP), and also compute the TN-REFS list for
826 ;;; the values to pass and the list of passing location TNs.
827 (defun ir2-convert-local-call-args (node block fun)
828 (declare (type combination node) (type ir2-block block) (type clambda fun))
829 (let ((fp (make-stack-pointer-tn))
830 (nfp (make-number-stack-pointer-tn))
831 (old-fp (make-stack-pointer-tn)))
832 (multiple-value-bind (temps locs)
833 (emit-psetq-moves node block fun old-fp)
834 (vop current-fp node block old-fp)
835 (vop allocate-frame node block
836 (physenv-info (lambda-physenv fun))
838 (values fp nfp temps (mapcar #'make-alias-tn locs)))))
840 ;;; Handle a non-TR known-values local call. We emit the call, then
841 ;;; move the results to the lvar's destination.
842 (defun ir2-convert-local-known-call (node block fun returns lvar start)
843 (declare (type node node) (type ir2-block block) (type clambda fun)
844 (type return-info returns) (type (or lvar null) lvar)
846 (multiple-value-bind (fp nfp temps arg-locs)
847 (ir2-convert-local-call-args node block fun)
848 (let ((locs (return-info-locations returns)))
849 (vop* known-call-local node block
850 (fp nfp (reference-tn-list temps nil))
851 ((reference-tn-list locs t))
852 arg-locs (physenv-info (lambda-physenv fun)) start)
853 (move-lvar-result node block locs lvar)))
856 ;;; Handle a non-TR unknown-values local call. We do different things
857 ;;; depending on what kind of values the lvar wants.
859 ;;; If LVAR is :UNKNOWN, then we use the "multiple-" variant, directly
860 ;;; specifying the lvar's LOCS as the VOP results so that we don't
861 ;;; have to do anything after the call.
863 ;;; Otherwise, we use STANDARD-RESULT-TNS to get wired result TNs, and
864 ;;; then call MOVE-LVAR-RESULT to do any necessary type checks or
866 (defun ir2-convert-local-unknown-call (node block fun lvar start)
867 (declare (type node node) (type ir2-block block) (type clambda fun)
868 (type (or lvar null) lvar) (type label start))
869 (multiple-value-bind (fp nfp temps arg-locs)
870 (ir2-convert-local-call-args node block fun)
871 (let ((2lvar (and lvar (lvar-info lvar)))
872 (env (physenv-info (lambda-physenv fun)))
873 (temp-refs (reference-tn-list temps nil)))
874 (if (and 2lvar (eq (ir2-lvar-kind 2lvar) :unknown))
875 (vop* multiple-call-local node block (fp nfp temp-refs)
876 ((reference-tn-list (ir2-lvar-locs 2lvar) t))
878 (let ((locs (standard-result-tns lvar)))
879 (vop* call-local node block
881 ((reference-tn-list locs t))
882 arg-locs env start (length locs))
883 (move-lvar-result node block locs lvar)))))
886 ;;; Dispatch to the appropriate function, depending on whether we have
887 ;;; a let, tail or normal call. If the function doesn't return, call
888 ;;; it using the unknown-value convention. We could compile it as a
889 ;;; tail call, but that might seem confusing in the debugger.
890 (defun ir2-convert-local-call (node block)
891 (declare (type combination node) (type ir2-block block))
892 (let* ((fun (ref-leaf (lvar-uses (basic-combination-fun node))))
893 (kind (functional-kind fun)))
894 (cond ((eq kind :let)
895 (ir2-convert-let node block fun))
896 ((eq kind :assignment)
897 (ir2-convert-assignment node block fun))
899 (ir2-convert-tail-local-call node block fun))
901 (let ((start (block-label (lambda-block fun)))
902 (returns (tail-set-info (lambda-tail-set fun)))
903 (lvar (node-lvar node)))
905 (return-info-kind returns)
908 (ir2-convert-local-unknown-call node block fun lvar start))
910 (ir2-convert-local-known-call node block fun returns
916 ;;; Given a function lvar FUN, return (VALUES TN-TO-CALL NAMED-P),
917 ;;; where TN-TO-CALL is a TN holding the thing that we call NAMED-P is
918 ;;; true if the thing is named (false if it is a function).
920 ;;; There are two interesting non-named cases:
921 ;;; -- We know it's a function. No check needed: return the
923 ;;; -- We don't know what it is.
924 (defun fun-lvar-tn (node block lvar)
925 (declare (ignore node block))
926 (declare (type lvar lvar))
927 (let ((2lvar (lvar-info lvar)))
928 (if (eq (ir2-lvar-kind 2lvar) :delayed)
929 (let ((name (lvar-fun-name lvar t)))
931 (values (make-load-time-constant-tn :fdefinition name) t))
932 (let* ((locs (ir2-lvar-locs 2lvar))
934 (function-ptype (primitive-type-or-lose 'function)))
935 (aver (and (eq (ir2-lvar-kind 2lvar) :fixed)
936 (= (length locs) 1)))
937 (aver (eq (tn-primitive-type loc) function-ptype))
940 ;;; Set up the args to NODE in the current frame, and return a TN-REF
941 ;;; list for the passing locations.
942 (defun move-tail-full-call-args (node block)
943 (declare (type combination node) (type ir2-block block))
944 (let ((args (basic-combination-args node))
947 (dotimes (num (length args))
948 (let ((loc (standard-arg-location num)))
949 (emit-move node block (lvar-tn node block (elt args num)) loc)
950 (let ((ref (reference-tn loc nil)))
952 (setf (tn-ref-across last) ref)
957 ;;; Move the arguments into the passing locations and do a (possibly
958 ;;; named) tail call.
959 (defun ir2-convert-tail-full-call (node block)
960 (declare (type combination node) (type ir2-block block))
961 (let* ((env (physenv-info (node-physenv node)))
962 (args (basic-combination-args node))
963 (nargs (length args))
964 (pass-refs (move-tail-full-call-args node block))
965 (old-fp (ir2-physenv-old-fp env))
966 (return-pc (ir2-physenv-return-pc env)))
968 (multiple-value-bind (fun-tn named)
969 (fun-lvar-tn node block (basic-combination-fun node))
971 (vop* tail-call-named node block
972 (fun-tn old-fp return-pc pass-refs)
976 (vop* tail-call node block
977 (fun-tn old-fp return-pc pass-refs)
980 (emit-step-p node)))))
984 ;;; like IR2-CONVERT-LOCAL-CALL-ARGS, only different
985 (defun ir2-convert-full-call-args (node block)
986 (declare (type combination node) (type ir2-block block))
987 (let* ((args (basic-combination-args node))
988 (fp (make-stack-pointer-tn))
989 (nargs (length args)))
990 (vop allocate-full-call-frame node block nargs fp)
995 (locs (standard-arg-location num))
996 (let ((ref (reference-tn (lvar-tn node block (elt args num))
999 (setf (tn-ref-across last) ref)
1003 (values fp first (locs) nargs)))))
1005 ;;; Do full call when a fixed number of values are desired. We make
1006 ;;; STANDARD-RESULT-TNS for our lvar, then deliver the result using
1007 ;;; MOVE-LVAR-RESULT. We do named or normal call, as appropriate.
1008 (defun ir2-convert-fixed-full-call (node block)
1009 (declare (type combination node) (type ir2-block block))
1010 (multiple-value-bind (fp args arg-locs nargs)
1011 (ir2-convert-full-call-args node block)
1012 (let* ((lvar (node-lvar node))
1013 (locs (standard-result-tns lvar))
1014 (loc-refs (reference-tn-list locs t))
1015 (nvals (length locs)))
1016 (multiple-value-bind (fun-tn named)
1017 (fun-lvar-tn node block (basic-combination-fun node))
1019 (vop* call-named node block (fp fun-tn args) (loc-refs)
1020 arg-locs nargs nvals (emit-step-p node))
1021 (vop* call node block (fp fun-tn args) (loc-refs)
1022 arg-locs nargs nvals (emit-step-p node)))
1023 (move-lvar-result node block locs lvar))))
1026 ;;; Do full call when unknown values are desired.
1027 (defun ir2-convert-multiple-full-call (node block)
1028 (declare (type combination node) (type ir2-block block))
1029 (multiple-value-bind (fp args arg-locs nargs)
1030 (ir2-convert-full-call-args node block)
1031 (let* ((lvar (node-lvar node))
1032 (locs (ir2-lvar-locs (lvar-info lvar)))
1033 (loc-refs (reference-tn-list locs t)))
1034 (multiple-value-bind (fun-tn named)
1035 (fun-lvar-tn node block (basic-combination-fun node))
1037 (vop* multiple-call-named node block (fp fun-tn args) (loc-refs)
1038 arg-locs nargs (emit-step-p node))
1039 (vop* multiple-call node block (fp fun-tn args) (loc-refs)
1040 arg-locs nargs (emit-step-p node))))))
1043 ;;; stuff to check in PONDER-FULL-CALL
1045 ;;; These came in handy when troubleshooting cold boot after making
1046 ;;; major changes in the package structure: various transforms and
1047 ;;; VOPs and stuff got attached to the wrong symbol, so that
1048 ;;; references to the right symbol were bogusly translated as full
1049 ;;; calls instead of primitives, sending the system off into infinite
1050 ;;; space. Having a report on all full calls generated makes it easier
1051 ;;; to figure out what form caused the problem this time.
1052 #!+sb-show (defvar *show-full-called-fnames-p* nil)
1053 #!+sb-show (defvar *full-called-fnames* (make-hash-table :test 'equal))
1055 ;;; Do some checks (and store some notes relevant for future checks)
1057 ;;; * Is this a full call to something we have reason to know should
1058 ;;; never be full called? (Except as of sbcl-0.7.18 or so, we no
1059 ;;; longer try to ensure this behavior when *FAILURE-P* has already
1061 ;;; * Is this a full call to (SETF FOO) which might conflict with
1062 ;;; a DEFSETF or some such thing elsewhere in the program?
1063 (defun ponder-full-call (node)
1064 (let* ((lvar (basic-combination-fun node))
1065 (fname (lvar-fun-name lvar t)))
1066 (declare (type (or symbol cons) fname))
1068 #!+sb-show (unless (gethash fname *full-called-fnames*)
1069 (setf (gethash fname *full-called-fnames*) t))
1070 #!+sb-show (when *show-full-called-fnames-p*
1071 (/show "converting full call to named function" fname)
1072 (/show (basic-combination-args node))
1073 (/show (policy node speed) (policy node safety))
1074 (/show (policy node compilation-speed))
1075 (let ((arg-types (mapcar (lambda (lvar)
1079 (basic-combination-args node))))
1082 ;; When illegal code is compiled, all sorts of perverse paths
1083 ;; through the compiler can be taken, and it's much harder -- and
1084 ;; probably pointless -- to guarantee that always-optimized-away
1085 ;; functions are actually optimized away. Thus, we skip the check
1088 ;; check to see if we know anything about the function
1089 (let ((info (info :function :info fname)))
1090 ;; if we know something, check to see if the full call was valid
1091 (when (and info (ir1-attributep (fun-info-attributes info)
1092 always-translatable))
1093 (/show (policy node speed) (policy node safety))
1094 (/show (policy node compilation-speed))
1095 (bug "full call to ~S" fname))))
1098 (aver (legal-fun-name-p fname))
1099 (destructuring-bind (setfoid &rest stem) fname
1100 (when (eq setfoid 'setf)
1101 (setf (gethash (car stem) *setf-assumed-fboundp*) t))))))
1103 ;;; If the call is in a tail recursive position and the return
1104 ;;; convention is standard, then do a tail full call. If one or fewer
1105 ;;; values are desired, then use a single-value call, otherwise use a
1106 ;;; multiple-values call.
1107 (defun ir2-convert-full-call (node block)
1108 (declare (type combination node) (type ir2-block block))
1109 (ponder-full-call node)
1110 (cond ((node-tail-p node)
1111 (ir2-convert-tail-full-call node block))
1112 ((let ((lvar (node-lvar node)))
1114 (eq (ir2-lvar-kind (lvar-info lvar)) :unknown)))
1115 (ir2-convert-multiple-full-call node block))
1117 (ir2-convert-fixed-full-call node block)))
1120 ;;;; entering functions
1122 ;;; Do all the stuff that needs to be done on XEP entry:
1123 ;;; -- Create frame.
1124 ;;; -- Copy any more arg.
1125 ;;; -- Set up the environment, accessing any closure variables.
1126 ;;; -- Move args from the standard passing locations to their internal
1128 (defun init-xep-environment (node block fun)
1129 (declare (type bind node) (type ir2-block block) (type clambda fun))
1130 (let ((start-label (entry-info-offset (leaf-info fun)))
1131 (env (physenv-info (node-physenv node))))
1132 (let ((ef (functional-entry-fun fun)))
1133 (cond ((and (optional-dispatch-p ef) (optional-dispatch-more-entry ef))
1134 ;; Special case the xep-allocate-frame + copy-more-arg case.
1135 (vop xep-allocate-frame node block start-label t)
1136 (vop copy-more-arg node block (optional-dispatch-max-args ef)))
1138 ;; No more args, so normal entry.
1139 (vop xep-allocate-frame node block start-label nil)))
1140 (if (ir2-physenv-closure env)
1141 (let ((closure (make-normal-tn *backend-t-primitive-type*)))
1142 (vop setup-closure-environment node block start-label closure)
1144 (dolist (loc (ir2-physenv-closure env))
1145 (vop closure-ref node block closure (incf n) (cdr loc)))))
1146 (vop setup-environment node block start-label)))
1148 (unless (eq (functional-kind fun) :toplevel)
1149 (let ((vars (lambda-vars fun))
1151 (when (leaf-refs (first vars))
1152 (emit-move node block (make-arg-count-location)
1153 (leaf-info (first vars))))
1154 (dolist (arg (rest vars))
1155 (when (leaf-refs arg)
1156 (let ((pass (standard-arg-location n))
1157 (home (leaf-info arg)))
1158 (if (lambda-var-indirect arg)
1159 (emit-make-value-cell node block pass home)
1160 (emit-move node block pass home))))
1163 (emit-move node block (make-old-fp-passing-location t)
1164 (ir2-physenv-old-fp env)))
1168 ;;; Emit function prolog code. This is only called on bind nodes for
1169 ;;; functions that allocate environments. All semantics of let calls
1170 ;;; are handled by IR2-CONVERT-LET.
1172 ;;; If not an XEP, all we do is move the return PC from its passing
1173 ;;; location, since in a local call, the caller allocates the frame
1174 ;;; and sets up the arguments.
1175 (defun ir2-convert-bind (node block)
1176 (declare (type bind node) (type ir2-block block))
1177 (let* ((fun (bind-lambda node))
1178 (env (physenv-info (lambda-physenv fun))))
1179 (aver (member (functional-kind fun)
1180 '(nil :external :optional :toplevel :cleanup)))
1183 (init-xep-environment node block fun)
1185 (when *collect-dynamic-statistics*
1186 (vop count-me node block *dynamic-counts-tn*
1187 (block-number (ir2-block-block block)))))
1191 (ir2-physenv-return-pc-pass env)
1192 (ir2-physenv-return-pc env))
1194 #!+unwind-to-frame-and-call-vop
1195 (when (and (lambda-allow-instrumenting fun)
1196 (not (lambda-inline-expanded fun))
1198 (policy fun (>= insert-debug-catch 2)))
1199 (vop sb!vm::bind-sentinel node block))
1201 (let ((lab (gen-label)))
1202 (setf (ir2-physenv-environment-start env) lab)
1203 (vop note-environment-start node block lab)))
1207 ;;;; function return
1209 ;;; Do stuff to return from a function with the specified values and
1210 ;;; convention. If the return convention is :FIXED and we aren't
1211 ;;; returning from an XEP, then we do a known return (letting
1212 ;;; representation selection insert the correct move-arg VOPs.)
1213 ;;; Otherwise, we use the unknown-values convention. If there is a
1214 ;;; fixed number of return values, then use RETURN, otherwise use
1215 ;;; RETURN-MULTIPLE.
1216 (defun ir2-convert-return (node block)
1217 (declare (type creturn node) (type ir2-block block))
1218 (let* ((lvar (return-result node))
1219 (2lvar (lvar-info lvar))
1220 (lvar-kind (ir2-lvar-kind 2lvar))
1221 (fun (return-lambda node))
1222 (env (physenv-info (lambda-physenv fun)))
1223 (old-fp (ir2-physenv-old-fp env))
1224 (return-pc (ir2-physenv-return-pc env))
1225 (returns (tail-set-info (lambda-tail-set fun))))
1226 #!+unwind-to-frame-and-call-vop
1227 (when (and (lambda-allow-instrumenting fun)
1228 (not (lambda-inline-expanded fun))
1229 (policy fun (>= insert-debug-catch 2)))
1230 (vop sb!vm::unbind-sentinel node block))
1232 ((and (eq (return-info-kind returns) :fixed)
1234 (let ((locs (lvar-tns node block lvar
1235 (return-info-types returns))))
1236 (vop* known-return node block
1237 (old-fp return-pc (reference-tn-list locs nil))
1239 (return-info-locations returns))))
1240 ((eq lvar-kind :fixed)
1241 (let* ((types (mapcar #'tn-primitive-type (ir2-lvar-locs 2lvar)))
1242 (lvar-locs (lvar-tns node block lvar types))
1243 (nvals (length lvar-locs))
1244 (locs (make-standard-value-tns nvals)))
1245 (mapc (lambda (val loc)
1246 (emit-move node block val loc))
1250 (vop return-single node block old-fp return-pc (car locs))
1251 (vop* return node block
1252 (old-fp return-pc (reference-tn-list locs nil))
1256 (aver (eq lvar-kind :unknown))
1257 (vop* return-multiple node block
1259 (reference-tn-list (ir2-lvar-locs 2lvar) nil))
1266 ;;;; These are used by the debugger to find the top function on the
1267 ;;;; stack. They return the OLD-FP and RETURN-PC for the current
1268 ;;;; function as multiple values.
1270 (defoptimizer (%caller-frame ir2-convert) (() node block)
1271 (let ((ir2-physenv (physenv-info (node-physenv node))))
1272 (move-lvar-result node block
1273 (list (ir2-physenv-old-fp ir2-physenv))
1276 (defoptimizer (%caller-pc ir2-convert) (() node block)
1277 (let ((ir2-physenv (physenv-info (node-physenv node))))
1278 (move-lvar-result node block
1279 (list (ir2-physenv-return-pc ir2-physenv))
1282 ;;;; multiple values
1284 ;;; This is almost identical to IR2-CONVERT-LET. Since LTN annotates
1285 ;;; the lvar for the correct number of values (with the lvar user
1286 ;;; responsible for defaulting), we can just pick them up from the
1288 (defun ir2-convert-mv-bind (node block)
1289 (declare (type mv-combination node) (type ir2-block block))
1290 (let* ((lvar (first (basic-combination-args node)))
1291 (fun (ref-leaf (lvar-uses (basic-combination-fun node))))
1292 (vars (lambda-vars fun)))
1293 (aver (eq (functional-kind fun) :mv-let))
1294 (mapc (lambda (src var)
1295 (when (leaf-refs var)
1296 (let ((dest (leaf-info var)))
1297 (if (lambda-var-indirect var)
1298 (emit-make-value-cell node block src dest)
1299 (emit-move node block src dest)))))
1300 (lvar-tns node block lvar
1302 (primitive-type (leaf-type x)))
1307 ;;; Emit the appropriate fixed value, unknown value or tail variant of
1308 ;;; CALL-VARIABLE. Note that we only need to pass the values start for
1309 ;;; the first argument: all the other argument lvar TNs are
1310 ;;; ignored. This is because we require all of the values globs to be
1311 ;;; contiguous and on stack top.
1312 (defun ir2-convert-mv-call (node block)
1313 (declare (type mv-combination node) (type ir2-block block))
1314 (aver (basic-combination-args node))
1315 (let* ((start-lvar (lvar-info (first (basic-combination-args node))))
1316 (start (first (ir2-lvar-locs start-lvar)))
1317 (tails (and (node-tail-p node)
1318 (lambda-tail-set (node-home-lambda node))))
1319 (lvar (node-lvar node))
1320 (2lvar (and lvar (lvar-info lvar))))
1321 (multiple-value-bind (fun named)
1322 (fun-lvar-tn node block (basic-combination-fun node))
1323 (aver (and (not named)
1324 (eq (ir2-lvar-kind start-lvar) :unknown)))
1327 (let ((env (physenv-info (node-physenv node))))
1328 (vop tail-call-variable node block start fun
1329 (ir2-physenv-old-fp env)
1330 (ir2-physenv-return-pc env))))
1332 (eq (ir2-lvar-kind 2lvar) :unknown))
1333 (vop* multiple-call-variable node block (start fun nil)
1334 ((reference-tn-list (ir2-lvar-locs 2lvar) t))
1335 (emit-step-p node)))
1337 (let ((locs (standard-result-tns lvar)))
1338 (vop* call-variable node block (start fun nil)
1339 ((reference-tn-list locs t)) (length locs)
1341 (move-lvar-result node block locs lvar)))))))
1343 ;;; Reset the stack pointer to the start of the specified
1344 ;;; unknown-values lvar (discarding it and all values globs on top of
1346 (defoptimizer (%pop-values ir2-convert) ((%lvar) node block)
1347 (let* ((lvar (lvar-value %lvar))
1348 (2lvar (lvar-info lvar)))
1349 (cond ((eq (ir2-lvar-kind 2lvar) :unknown)
1350 (vop reset-stack-pointer node block
1351 (first (ir2-lvar-locs 2lvar))))
1352 ((lvar-dynamic-extent lvar)
1353 (vop reset-stack-pointer node block
1354 (ir2-lvar-stack-pointer 2lvar)))
1355 (t (bug "Trying to pop a not stack-allocated LVAR ~S."
1358 (defoptimizer (%nip-values ir2-convert) ((last-nipped last-preserved
1361 (let* ( ;; pointer immediately after the nipped block
1362 (after (lvar-value last-nipped))
1363 (2after (lvar-info after))
1364 ;; pointer to the first nipped word
1365 (first (lvar-value last-preserved))
1366 (2first (lvar-info first))
1368 (moved-tns (loop for lvar-ref in moved
1369 for lvar = (lvar-value lvar-ref)
1370 for 2lvar = (lvar-info lvar)
1372 collect (first (ir2-lvar-locs 2lvar)))))
1373 (aver (or (eq (ir2-lvar-kind 2after) :unknown)
1374 (lvar-dynamic-extent after)))
1375 (aver (eq (ir2-lvar-kind 2first) :unknown))
1376 (when *check-consistency*
1377 ;; we cannot move stack-allocated DX objects
1378 (dolist (moved-lvar moved)
1379 (aver (eq (ir2-lvar-kind (lvar-info (lvar-value moved-lvar)))
1381 (flet ((nip-aligned (nipped)
1382 (vop* %%nip-values node block
1384 (first (ir2-lvar-locs 2first))
1385 (reference-tn-list moved-tns nil))
1386 ((reference-tn-list moved-tns t)))))
1387 (cond ((eq (ir2-lvar-kind 2after) :unknown)
1388 (nip-aligned (first (ir2-lvar-locs 2after))))
1389 ((lvar-dynamic-extent after)
1390 (nip-aligned (ir2-lvar-stack-pointer 2after)))
1392 (bug "Trying to nip a not stack-allocated LVAR ~S." after))))))
1394 ;;; Deliver the values TNs to LVAR using MOVE-LVAR-RESULT.
1395 (defoptimizer (values ir2-convert) ((&rest values) node block)
1396 (let ((tns (mapcar (lambda (x)
1397 (lvar-tn node block x))
1399 (move-lvar-result node block tns (node-lvar node))))
1401 ;;; In the normal case where unknown values are desired, we use the
1402 ;;; VALUES-LIST VOP. In the relatively unimportant case of VALUES-LIST
1403 ;;; for a fixed number of values, we punt by doing a full call to the
1404 ;;; VALUES-LIST function. This gets the full call VOP to deal with
1405 ;;; defaulting any unsupplied values. It seems unworthwhile to
1406 ;;; optimize this case.
1407 (defoptimizer (values-list ir2-convert) ((list) node block)
1408 (let* ((lvar (node-lvar node))
1409 (2lvar (and lvar (lvar-info lvar))))
1411 (eq (ir2-lvar-kind 2lvar) :unknown))
1412 (let ((locs (ir2-lvar-locs 2lvar)))
1413 (vop* values-list node block
1414 ((lvar-tn node block list) nil)
1415 ((reference-tn-list locs t)))))
1416 (t (aver (or (not 2lvar) ; i.e. we want to check the argument
1417 (eq (ir2-lvar-kind 2lvar) :fixed)))
1418 (ir2-convert-full-call node block)))))
1420 (defoptimizer (%more-arg-values ir2-convert) ((context start count) node block)
1421 (binding* ((lvar (node-lvar node) :exit-if-null)
1422 (2lvar (lvar-info lvar)))
1423 (ecase (ir2-lvar-kind 2lvar)
1424 (:fixed (ir2-convert-full-call node block))
1426 (let ((locs (ir2-lvar-locs 2lvar)))
1427 (vop* %more-arg-values node block
1428 ((lvar-tn node block context)
1429 (lvar-tn node block start)
1430 (lvar-tn node block count)
1432 ((reference-tn-list locs t))))))))
1434 ;;;; special binding
1436 ;;; This is trivial, given our assumption of a shallow-binding
1438 (defoptimizer (%special-bind ir2-convert) ((var value) node block)
1439 (let ((name (leaf-source-name (lvar-value var))))
1440 (vop bind node block (lvar-tn node block value)
1441 (emit-constant name))))
1442 (defoptimizer (%special-unbind ir2-convert) ((var) node block)
1443 (vop unbind node block))
1445 ;;; ### It's not clear that this really belongs in this file, or
1446 ;;; should really be done this way, but this is the least violation of
1447 ;;; abstraction in the current setup. We don't want to wire
1448 ;;; shallow-binding assumptions into IR1tran.
1449 (def-ir1-translator progv
1450 ((vars vals &body body) start next result)
1453 (with-unique-names (bind unbind)
1454 (once-only ((n-save-bs '(%primitive current-binding-pointer)))
1457 (labels ((,unbind (vars)
1458 (declare (optimize (speed 2) (debug 0)))
1459 (let ((unbound-marker (%primitive make-other-immediate-type
1460 0 sb!vm:unbound-marker-widetag)))
1462 ;; CLHS says "bound and then made to have no value" -- user
1463 ;; should not be able to tell the difference between that and this.
1464 (about-to-modify-symbol-value var 'progv)
1465 (%primitive bind unbound-marker var))))
1467 (declare (optimize (speed 2) (debug 0)
1468 (insert-debug-catch 0)))
1470 ((null vals) (,unbind vars))
1472 (let ((val (car vals))
1474 (about-to-modify-symbol-value var 'progv val t)
1475 (%primitive bind val var))
1476 (,bind (cdr vars) (cdr vals))))))
1477 (,bind ,vars ,vals))
1480 ;; Technically ANSI CL doesn't allow declarations at the
1481 ;; start of the cleanup form. SBCL happens to allow for
1482 ;; them, due to the way the UNWIND-PROTECT ir1 translation
1483 ;; is implemented; the cleanup forms are directly spliced
1484 ;; into an FLET definition body. And a declaration here
1485 ;; actually has exactly the right scope for what we need
1486 ;; (ensure that debug instrumentation is not emitted for the
1487 ;; cleanup function). -- JES, 2007-06-16
1488 (declare (optimize (insert-debug-catch 0)))
1489 (%primitive unbind-to-here ,n-save-bs))))))
1493 ;;; Convert a non-local lexical exit. First find the NLX-INFO in our
1494 ;;; environment. Note that this is never called on the escape exits
1495 ;;; for CATCH and UNWIND-PROTECT, since the escape functions aren't
1497 (defun ir2-convert-exit (node block)
1498 (declare (type exit node) (type ir2-block block))
1499 (let* ((nlx (exit-nlx-info node))
1500 (loc (find-in-physenv nlx (node-physenv node)))
1501 (temp (make-stack-pointer-tn))
1502 (value (exit-value node)))
1503 (if (nlx-info-safe-p nlx)
1504 (vop value-cell-ref node block loc temp)
1505 (emit-move node block loc temp))
1507 (let ((locs (ir2-lvar-locs (lvar-info value))))
1508 (vop unwind node block temp (first locs) (second locs)))
1509 (let ((0-tn (emit-constant 0)))
1510 (vop unwind node block temp 0-tn 0-tn))))
1514 ;;; %CLEANUP-POINT doesn't do anything except prevent the body from
1515 ;;; being entirely deleted.
1516 (defoptimizer (%cleanup-point ir2-convert) (() node block) node block)
1518 ;;; This function invalidates a lexical exit on exiting from the
1519 ;;; dynamic extent. This is done by storing 0 into the indirect value
1520 ;;; cell that holds the closed unwind block.
1521 (defoptimizer (%lexical-exit-breakup ir2-convert) ((info) node block)
1522 (let ((nlx (lvar-value info)))
1523 (when (nlx-info-safe-p nlx)
1524 (vop value-cell-set node block
1525 (find-in-physenv nlx (node-physenv node))
1526 (emit-constant 0)))))
1528 ;;; We have to do a spurious move of no values to the result lvar so
1529 ;;; that lifetime analysis won't get confused.
1530 (defun ir2-convert-throw (node block)
1531 (declare (type mv-combination node) (type ir2-block block))
1532 (let ((args (basic-combination-args node)))
1533 (check-catch-tag-type (first args))
1534 (vop* throw node block
1535 ((lvar-tn node block (first args))
1537 (ir2-lvar-locs (lvar-info (second args)))
1540 (move-lvar-result node block () (node-lvar node))
1543 ;;; Emit code to set up a non-local exit. INFO is the NLX-INFO for the
1544 ;;; exit, and TAG is the lvar for the catch tag (if any.) We get at
1545 ;;; the target PC by passing in the label to the vop. The vop is
1546 ;;; responsible for building a return-PC object.
1547 (defun emit-nlx-start (node block info tag)
1548 (declare (type node node) (type ir2-block block) (type nlx-info info)
1549 (type (or lvar null) tag))
1550 (let* ((2info (nlx-info-info info))
1551 (kind (cleanup-kind (nlx-info-cleanup info)))
1552 (block-tn (physenv-live-tn
1553 (make-normal-tn (primitive-type-or-lose 'catch-block))
1554 (node-physenv node)))
1555 (res (make-stack-pointer-tn))
1556 (target-label (ir2-nlx-info-target 2info)))
1558 (vop current-binding-pointer node block
1559 (car (ir2-nlx-info-dynamic-state 2info)))
1560 (vop* save-dynamic-state node block
1562 ((reference-tn-list (cdr (ir2-nlx-info-dynamic-state 2info)) t)))
1563 (vop current-stack-pointer node block (ir2-nlx-info-save-sp 2info))
1567 (vop make-catch-block node block block-tn
1568 (lvar-tn node block tag) target-label res))
1569 ((:unwind-protect :block :tagbody)
1570 (vop make-unwind-block node block block-tn target-label res)))
1574 (if (nlx-info-safe-p info)
1575 (emit-make-value-cell node block res (ir2-nlx-info-home 2info))
1576 (emit-move node block res (ir2-nlx-info-home 2info))))
1578 (vop set-unwind-protect node block block-tn))
1583 ;;; Scan each of ENTRY's exits, setting up the exit for each lexical exit.
1584 (defun ir2-convert-entry (node block)
1585 (declare (type entry node) (type ir2-block block))
1587 (dolist (exit (entry-exits node))
1588 (let ((info (exit-nlx-info exit)))
1590 (not (memq info nlxes))
1591 (member (cleanup-kind (nlx-info-cleanup info))
1592 '(:block :tagbody)))
1594 (emit-nlx-start node block info nil)))))
1597 ;;; Set up the unwind block for these guys.
1598 (defoptimizer (%catch ir2-convert) ((info-lvar tag) node block)
1599 (check-catch-tag-type tag)
1600 (emit-nlx-start node block (lvar-value info-lvar) tag))
1601 (defoptimizer (%unwind-protect ir2-convert) ((info-lvar cleanup) node block)
1602 (emit-nlx-start node block (lvar-value info-lvar) nil))
1604 ;;; Emit the entry code for a non-local exit. We receive values and
1605 ;;; restore dynamic state.
1607 ;;; In the case of a lexical exit or CATCH, we look at the exit lvar's
1608 ;;; kind to determine which flavor of entry VOP to emit. If unknown
1609 ;;; values, emit the xxx-MULTIPLE variant to the lvar locs. If fixed
1610 ;;; values, make the appropriate number of temps in the standard
1611 ;;; values locations and use the other variant, delivering the temps
1612 ;;; to the lvar using MOVE-LVAR-RESULT.
1614 ;;; In the UNWIND-PROTECT case, we deliver the first register
1615 ;;; argument, the argument count and the argument pointer to our lvar
1616 ;;; as multiple values. These values are the block exited to and the
1617 ;;; values start and count.
1619 ;;; After receiving values, we restore dynamic state. Except in the
1620 ;;; UNWIND-PROTECT case, the values receiving restores the stack
1621 ;;; pointer. In an UNWIND-PROTECT cleanup, we want to leave the stack
1622 ;;; pointer alone, since the thrown values are still out there.
1623 (defoptimizer (%nlx-entry ir2-convert) ((info-lvar) node block)
1624 (let* ((info (lvar-value info-lvar))
1625 (lvar (node-lvar node))
1626 (2info (nlx-info-info info))
1627 (top-loc (ir2-nlx-info-save-sp 2info))
1628 (start-loc (make-nlx-entry-arg-start-location))
1629 (count-loc (make-arg-count-location))
1630 (target (ir2-nlx-info-target 2info)))
1632 (ecase (cleanup-kind (nlx-info-cleanup info))
1633 ((:catch :block :tagbody)
1634 (let ((2lvar (and lvar (lvar-info lvar))))
1635 (if (and 2lvar (eq (ir2-lvar-kind 2lvar) :unknown))
1636 (vop* nlx-entry-multiple node block
1637 (top-loc start-loc count-loc nil)
1638 ((reference-tn-list (ir2-lvar-locs 2lvar) t))
1640 (let ((locs (standard-result-tns lvar)))
1641 (vop* nlx-entry node block
1642 (top-loc start-loc count-loc nil)
1643 ((reference-tn-list locs t))
1646 (move-lvar-result node block locs lvar)))))
1648 (let ((block-loc (standard-arg-location 0)))
1649 (vop uwp-entry node block target block-loc start-loc count-loc)
1652 (list block-loc start-loc count-loc)
1656 (when *collect-dynamic-statistics*
1657 (vop count-me node block *dynamic-counts-tn*
1658 (block-number (ir2-block-block block))))
1660 (vop* restore-dynamic-state node block
1661 ((reference-tn-list (cdr (ir2-nlx-info-dynamic-state 2info)) nil))
1663 (vop unbind-to-here node block
1664 (car (ir2-nlx-info-dynamic-state 2info)))))
1666 ;;;; n-argument functions
1668 (macrolet ((def (name)
1669 `(defoptimizer (,name ir2-convert) ((&rest args) node block)
1670 (let* ((refs (move-tail-full-call-args node block))
1671 (lvar (node-lvar node))
1672 (res (lvar-result-tns
1674 (list (primitive-type (specifier-type 'list))))))
1675 (when (and lvar (lvar-dynamic-extent lvar))
1676 (vop current-stack-pointer node block
1677 (ir2-lvar-stack-pointer (lvar-info lvar))))
1678 (vop* ,name node block (refs) ((first res) nil)
1680 (move-lvar-result node block res lvar)))))
1685 ;;; Convert the code in a component into VOPs.
1686 (defun ir2-convert (component)
1687 (declare (type component component))
1688 (let (#!+sb-dyncount
1689 (*dynamic-counts-tn*
1690 (when *collect-dynamic-statistics*
1692 (block-number (block-next (component-head component))))
1693 (counts (make-array blocks
1694 :element-type '(unsigned-byte 32)
1695 :initial-element 0))
1696 (info (make-dyncount-info
1697 :for (component-name component)
1698 :costs (make-array blocks
1699 :element-type '(unsigned-byte 32)
1702 (setf (ir2-component-dyncount-info (component-info component))
1704 (emit-constant info)
1705 (emit-constant counts)))))
1707 (declare (type index num))
1708 (do-ir2-blocks (2block component)
1709 (let ((block (ir2-block-block 2block)))
1710 (when (block-start block)
1711 (setf (block-number block) num)
1713 (when *collect-dynamic-statistics*
1714 (let ((first-node (block-start-node block)))
1715 (unless (or (and (bind-p first-node)
1716 (xep-p (bind-lambda first-node)))
1718 (node-lvar first-node))
1723 #!+sb-dyncount *dynamic-counts-tn* #!-sb-dyncount nil
1725 (ir2-convert-block block)
1729 ;;; If necessary, emit a terminal unconditional branch to go to the
1730 ;;; successor block. If the successor is the component tail, then
1731 ;;; there isn't really any successor, but if the end is an unknown,
1732 ;;; non-tail call, then we emit an error trap just in case the
1733 ;;; function really does return.
1734 (defun finish-ir2-block (block)
1735 (declare (type cblock block))
1736 (let* ((2block (block-info block))
1737 (last (block-last block))
1738 (succ (block-succ block)))
1740 (aver (singleton-p succ))
1741 (let ((target (first succ)))
1742 (cond ((eq target (component-tail (block-component block)))
1743 (when (and (basic-combination-p last)
1744 (eq (basic-combination-kind last) :full))
1745 (let* ((fun (basic-combination-fun last))
1746 (use (lvar-uses fun))
1747 (name (and (ref-p use)
1748 (leaf-has-source-name-p (ref-leaf use))
1749 (leaf-source-name (ref-leaf use)))))
1750 (unless (or (node-tail-p last)
1751 (info :function :info name)
1752 (policy last (zerop safety)))
1753 (vop nil-fun-returned-error last 2block
1755 (emit-constant name)
1756 (multiple-value-bind (tn named)
1757 (fun-lvar-tn last 2block fun)
1760 ((not (eq (ir2-block-next 2block) (block-info target)))
1761 (vop branch last 2block (block-label target)))))))
1765 ;;; Convert the code in a block into VOPs.
1766 (defun ir2-convert-block (block)
1767 (declare (type cblock block))
1768 (let ((2block (block-info block)))
1769 (do-nodes (node lvar block)
1773 (let ((2lvar (lvar-info lvar)))
1774 ;; function REF in a local call is not annotated
1775 (when (and 2lvar (not (eq (ir2-lvar-kind 2lvar) :delayed)))
1776 (ir2-convert-ref node 2block)))))
1778 (let ((kind (basic-combination-kind node)))
1781 (ir2-convert-local-call node 2block))
1783 (ir2-convert-full-call node 2block))
1785 (let* ((info (basic-combination-fun-info node))
1786 (fun (fun-info-ir2-convert info)))
1788 (funcall fun node 2block))
1789 ((eq (basic-combination-info node) :full)
1790 (ir2-convert-full-call node 2block))
1792 (ir2-convert-template node 2block))))))))
1794 (when (lvar-info (if-test node))
1795 (ir2-convert-if node 2block)))
1797 (let ((fun (bind-lambda node)))
1798 (when (eq (lambda-home fun) fun)
1799 (ir2-convert-bind node 2block))))
1801 (ir2-convert-return node 2block))
1803 (ir2-convert-set node 2block))
1805 (ir2-convert-cast node 2block))
1808 ((eq (basic-combination-kind node) :local)
1809 (ir2-convert-mv-bind node 2block))
1810 ((eq (lvar-fun-name (basic-combination-fun node))
1812 (ir2-convert-throw node 2block))
1814 (ir2-convert-mv-call node 2block))))
1816 (when (exit-entry node)
1817 (ir2-convert-exit node 2block)))
1819 (ir2-convert-entry node 2block)))))
1821 (finish-ir2-block block)