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 %return-is-boxed
(node)
123 (declare (type creturn node
))
124 (let* ((fun (return-lambda node
))
125 (returns (tail-set-info (lambda-tail-set fun
))))
127 (eq (return-info-kind returns
) :unknown
))))
129 (defun boxed-ref-p (ref)
130 (let ((dest (lvar-dest (ref-lvar ref
))))
131 (cond ((and (basic-combination-p dest
)
132 (call-full-like-p dest
))
134 ((and (return-p dest
) (%return-is-boxed dest
)))
138 ;;; Convert a REF node. The reference must not be delayed.
139 (defun ir2-convert-ref (node block
)
140 (declare (type ref node
) (type ir2-block block
))
141 (let* ((lvar (node-lvar node
))
142 (leaf (ref-leaf node
))
143 (locs (lvar-result-tns
144 lvar
(list (primitive-type (leaf-type leaf
)))))
148 (let ((tn (find-in-physenv leaf
(node-physenv node
)))
149 (indirect (lambda-var-indirect leaf
))
150 (explicit (lambda-var-explicit-value-cell leaf
)))
152 ((and indirect explicit
)
153 (vop value-cell-ref node block tn res
))
155 (not (eq (node-physenv node
)
156 (lambda-physenv (lambda-var-home leaf
)))))
157 (let ((reffer (third (primitive-type-indirect-cell-type
158 (primitive-type (leaf-type leaf
))))))
160 (funcall reffer node block tn
(leaf-info leaf
) res
)
161 (vop ancestor-frame-ref node block tn
(leaf-info leaf
) res
))))
162 (t (emit-move node block tn res
)))))
164 (emit-move node block
(constant-tn leaf
(boxed-ref-p node
)) res
))
166 (ir2-convert-closure node block leaf res
))
168 (ir2-convert-global-var node block leaf res
)))
169 (move-lvar-result node block locs lvar
))
172 (defun ir2-convert-global-var (node block leaf res
)
173 (let ((unsafe (policy node
(zerop safety
)))
174 (name (leaf-source-name leaf
)))
175 (ecase (global-var-kind leaf
)
177 (aver (symbolp name
))
178 (let ((name-tn (emit-constant name
)))
179 (if (or unsafe
(always-boundp name
))
180 (vop fast-symbol-value node block name-tn res
)
181 (vop symbol-value node block name-tn res
))))
183 (aver (symbolp name
))
184 (let ((name-tn (emit-constant name
)))
185 (if (or unsafe
(always-boundp name
))
186 (vop fast-symbol-global-value node block name-tn res
)
187 (vop symbol-global-value node block name-tn res
))))
189 ;; In cross-compilation, testing (INFO :function :definition) is not
190 ;; sensible (or possible) but we can assume that things with fun-info
191 ;; will eventually be defined. If that's untrue, e.g. if we referred
192 ;; to #'DESCRIBE during cold-load, we'd just fix it locally by declaring
193 ;; DESCRIBE notinline.
194 ;; But in the target, more caution is warranted because users might
195 ;; DEFKNOWN a function but fail to define it. And they shouldn't be
196 ;; expected to understand the failure mode and the remedy.
197 (cond ((and #-sb-xc-host
(info :function
:definition name
)
198 (info :function
:info name
)
199 (let ((*lexenv
* (node-lexenv node
)))
200 (not (fun-lexically-notinline-p name
))))
201 ;; Known functions can be dumped without going through fdefns.
202 ;; But if NOTINLINEd, don't early-bind to the functional value
203 ;; because that disallows redefinition, including but not limited
204 ;; to encapsulations, which in turn makes TRACE not work, which
205 ;; leads to extreme frustration when debugging.
206 (emit-move node block
(make-load-time-constant-tn :known-fun name
)
209 (let ((fdefn-tn (make-load-time-constant-tn :fdefinition name
)))
211 (vop fdefn-fun node block fdefn-tn res
)
212 (vop safe-fdefn-fun node block fdefn-tn res
)))))))))
214 ;;; some sanity checks for a CLAMBDA passed to IR2-CONVERT-CLOSURE
215 (defun assertions-on-ir2-converted-clambda (clambda)
216 ;; This assertion was sort of an experiment. It would be nice and
217 ;; sane and easier to understand things if it were *always* true,
218 ;; but experimentally I observe that it's only *almost* always
219 ;; true. -- WHN 2001-01-02
221 (aver (eql (lambda-component clambda
)
222 (block-component (ir2-block-block ir2-block
))))
223 ;; Check for some weirdness which came up in bug
226 ;; The MAKE-LOAD-TIME-CONSTANT-TN call above puts an :ENTRY record
227 ;; into the IR2-COMPONENT-CONSTANTS table. The dump-a-COMPONENT
229 ;; * treats every HANDLEless :ENTRY record into a
231 ;; * expects every patch to correspond to an
232 ;; IR2-COMPONENT-ENTRIES record.
233 ;; The IR2-COMPONENT-ENTRIES records are set by ENTRY-ANALYZE
234 ;; walking over COMPONENT-LAMBDAS. Bug 138b arose because there
235 ;; was a HANDLEless :ENTRY record which didn't correspond to an
236 ;; IR2-COMPONENT-ENTRIES record. That problem is hard to debug
237 ;; when it's caught at dump time, so this assertion tries to catch
239 (aver (member clambda
240 (component-lambdas (lambda-component clambda
))))
241 ;; another bug-138-related issue: COMPONENT-NEW-FUNCTIONALS is
242 ;; used as a queue for stuff pending to do in IR1, and now that
243 ;; we're doing IR2 it should've been completely flushed (but
245 (aver (null (component-new-functionals (lambda-component clambda
))))
248 ;;; Emit code to load a function object implementing FUNCTIONAL into
249 ;;; RES. This gets interesting when the referenced function is a
250 ;;; closure: we must make the closure and move the closed-over values
253 ;;; FUNCTIONAL is either a :TOPLEVEL-XEP functional or the XEP lambda
254 ;;; for the called function, since local call analysis converts all
255 ;;; closure references. If a :TOPLEVEL-XEP, we know it is not a
258 ;;; If a closed-over LAMBDA-VAR has no refs (is deleted), then we
259 ;;; don't initialize that slot. This can happen with closures over
260 ;;; top level variables, where optimization of the closure deleted the
261 ;;; variable. Since we committed to the closure format when we
262 ;;; pre-analyzed the top level code, we just leave an empty slot.
263 (defun ir2-convert-closure (ref ir2-block functional res
)
264 (declare (type ref ref
)
265 (type ir2-block ir2-block
)
266 (type functional functional
)
269 (aver (not (eql (functional-kind functional
) :deleted
)))
270 (unless (leaf-info functional
)
271 (setf (leaf-info functional
)
272 (make-entry-info :name
273 (functional-debug-name functional
))))))
274 (let ((closure (etypecase functional
276 (assertions-on-ir2-converted-clambda functional
)
277 (physenv-closure (get-lambda-physenv functional
)))
279 (aver (eq (functional-kind functional
) :toplevel-xep
))
284 (let* ((physenv (node-physenv ref
))
285 (tn (find-in-physenv functional physenv
)))
286 (emit-move ref ir2-block tn res
)))
287 ;; we're about to emit a reference to a "closure" that's actually
288 ;; an inlinable global function.
289 ((and (global-var-p (setf global-var
290 (functional-inline-expanded functional
)))
291 (eq :global-function
(global-var-kind global-var
)))
292 (ir2-convert-global-var ref ir2-block global-var res
))
294 ;; if we're here, we should have either a toplevel-xep (some
295 ;; global scope function in a different component) or an external
296 ;; reference to the "closure"'s body.
298 (aver (memq (functional-kind functional
) '(:external
:toplevel-xep
)))
299 (let ((entry (make-load-time-constant-tn :entry functional
)))
300 (emit-move ref ir2-block entry res
))))))
303 (defun closure-initial-value (what this-env current-fp
)
304 (declare (type (or nlx-info lambda-var clambda
) what
)
305 (type physenv this-env
)
306 (type (or tn null
) current-fp
))
307 ;; If we have an indirect LAMBDA-VAR that does not require an
308 ;; EXPLICIT-VALUE-CELL, and is from this environment (not from being
309 ;; closed over), we need to store the current frame pointer.
310 (if (and (lambda-var-p what
)
311 (lambda-var-indirect what
)
312 (not (lambda-var-explicit-value-cell what
))
313 (eq (lambda-physenv (lambda-var-home what
))
316 (find-in-physenv what this-env
)))
318 (defoptimizer (%allocate-closures ltn-annotate
) ((leaves) node ltn-policy
)
319 (declare (ignore ltn-policy
))
320 (when (lvar-dynamic-extent leaves
)
321 (let ((info (make-ir2-lvar *backend-t-primitive-type
*)))
322 (setf (ir2-lvar-kind info
) :delayed
)
323 (setf (lvar-info leaves
) info
)
324 (setf (ir2-lvar-stack-pointer info
)
325 (make-stack-pointer-tn)))))
327 (defoptimizer (%allocate-closures ir2-convert
) ((leaves) call
2block
)
328 (let ((dx-p (lvar-dynamic-extent leaves
)))
331 (vop current-stack-pointer call
2block
332 (ir2-lvar-stack-pointer (lvar-info leaves
))))
333 (dolist (leaf (lvar-value leaves
))
334 (binding* ((xep (awhen (functional-entry-fun leaf
)
335 ;; if the xep's been deleted then we can skip it
336 (if (eq (functional-kind it
) :deleted
)
339 (nil (aver (xep-p xep
)))
340 (entry-info (lambda-info xep
) :exit-if-null
)
341 (tn (entry-info-closure-tn entry-info
) :exit-if-null
)
342 (closure (physenv-closure (get-lambda-physenv xep
)))
343 (entry (make-load-time-constant-tn :entry xep
)))
344 (let ((this-env (node-physenv call
))
345 (leaf-dx-p (and dx-p
(leaf-dynamic-extent leaf
))))
346 (vop make-closure call
2block entry
(length closure
)
348 (loop for what in closure and n from
0 do
349 (unless (and (lambda-var-p what
)
350 (null (leaf-refs what
)))
351 ;; In LABELS a closure may refer to another closure
352 ;; in the same group, so we must be sure that we
353 ;; store a closure only after its creation.
355 ;; TODO: Here is a simple solution: we postpone
356 ;; putting of all closures after all creations
357 ;; (though it may require more registers).
359 (delayed (list tn
(find-in-physenv what this-env
) n
))
360 (let ((initial-value (closure-initial-value
363 (vop closure-init call
2block
365 ;; An initial-value of NIL means to stash
366 ;; the frame pointer... which requires a
368 (vop closure-init-from-fp call
2block tn n
)))))))))
369 (loop for
(tn what n
) in
(delayed)
370 do
(vop closure-init call
2block
374 ;;; Convert a SET node. If the NODE's LVAR is annotated, then we also
375 ;;; deliver the value to that lvar. If the var is a lexical variable
376 ;;; with no refs, then we don't actually set anything, since the
377 ;;; variable has been deleted.
378 (defun ir2-convert-set (node block
)
379 (declare (type cset node
) (type ir2-block block
))
380 (let* ((lvar (node-lvar node
))
381 (leaf (set-var node
))
382 (val (lvar-tn node block
(set-value node
)))
385 lvar
(list (primitive-type (leaf-type leaf
))))
389 (when (leaf-refs leaf
)
390 (let ((tn (find-in-physenv leaf
(node-physenv node
)))
391 (indirect (lambda-var-indirect leaf
))
392 (explicit (lambda-var-explicit-value-cell leaf
)))
394 ((and indirect explicit
)
395 (vop value-cell-set node block tn val
))
397 (not (eq (node-physenv node
)
398 (lambda-physenv (lambda-var-home leaf
)))))
399 (let ((setter (fourth (primitive-type-indirect-cell-type
400 (primitive-type (leaf-type leaf
))))))
402 (funcall setter node block tn val
(leaf-info leaf
))
403 (vop ancestor-frame-set node block tn val
(leaf-info leaf
)))))
404 (t (emit-move node block val tn
))))))
406 (aver (symbolp (leaf-source-name leaf
)))
407 (ecase (global-var-kind leaf
)
409 (vop set node block
(emit-constant (leaf-source-name leaf
)) val
))
411 (vop %set-symbol-global-value node
412 block
(emit-constant (leaf-source-name leaf
)) val
)))))
414 (emit-move node block val
(first locs
))
415 (move-lvar-result node block locs lvar
)))
418 ;;;; utilities for receiving fixed values
420 ;;; Return a TN that can be referenced to get the value of LVAR. LVAR
421 ;;; must be LTN-ANNOTATED either as a delayed leaf ref or as a fixed,
422 ;;; single-value lvar.
424 ;;; The primitive-type of the result will always be the same as the
425 ;;; IR2-LVAR-PRIMITIVE-TYPE, ensuring that VOPs are always called with
426 ;;; TNs that satisfy the operand primitive-type restriction. We may
427 ;;; have to make a temporary of the desired type and move the actual
428 ;;; lvar TN into it. This happens when we delete a type check in
429 ;;; unsafe code or when we locally know something about the type of an
430 ;;; argument variable.
431 (defun lvar-tn (node block lvar
)
432 (declare (type node node
) (type ir2-block block
) (type lvar lvar
))
433 (let* ((2lvar (lvar-info lvar
))
435 (ecase (ir2-lvar-kind 2lvar
)
437 (let ((ref (lvar-uses lvar
)))
438 (leaf-tn (ref-leaf ref
) (node-physenv ref
) (boxed-ref-p ref
))))
440 (aver (= (length (ir2-lvar-locs 2lvar
)) 1))
441 (first (ir2-lvar-locs 2lvar
)))))
442 (ptype (ir2-lvar-primitive-type 2lvar
)))
444 (cond ((eq (tn-primitive-type lvar-tn
) ptype
) lvar-tn
)
446 (let ((temp (make-normal-tn ptype
)))
447 (emit-move node block lvar-tn temp
)
450 ;;; This is similar to LVAR-TN, but hacks multiple values. We return
451 ;;; TNs holding the values of LVAR with PTYPES as their primitive
452 ;;; types. LVAR must be annotated for the same number of fixed values
453 ;;; are there are PTYPES.
455 ;;; If the lvar has a type check, check the values into temps and
456 ;;; return the temps. When we have more values than assertions, we
457 ;;; move the extra values with no check.
458 (defun lvar-tns (node block lvar ptypes
)
459 (declare (type node node
) (type ir2-block block
)
460 (type lvar lvar
) (list ptypes
))
461 (let* ((locs (ir2-lvar-locs (lvar-info lvar
)))
462 (nlocs (length locs
)))
463 (aver (= nlocs
(length ptypes
)))
465 (mapcar (lambda (from to-type
)
466 (if (eq (tn-primitive-type from
) to-type
)
468 (let ((temp (make-normal-tn to-type
)))
469 (emit-move node block from temp
)
474 ;;;; utilities for delivering values to lvars
476 ;;; Return a list of TNs with the specifier TYPES that can be used as
477 ;;; result TNs to evaluate an expression into LVAR. This is used
478 ;;; together with MOVE-LVAR-RESULT to deliver fixed values to
481 ;;; If the lvar isn't annotated (meaning the values are discarded) or
482 ;;; is unknown-values, then we make temporaries for each supplied
483 ;;; value, providing a place to compute the result in until we decide
484 ;;; what to do with it (if anything.)
486 ;;; If the lvar is fixed-values, and wants the same number of values
487 ;;; as the user wants to deliver, then we just return the
488 ;;; IR2-LVAR-LOCS. Otherwise we make a new list padded as necessary by
489 ;;; discarded TNs. We always return a TN of the specified type, using
490 ;;; the lvar locs only when they are of the correct type.
491 (defun lvar-result-tns (lvar types
)
492 (declare (type (or lvar null
) lvar
) (type list types
))
494 (mapcar #'make-normal-tn types
)
495 (let ((2lvar (lvar-info lvar
)))
496 (ecase (ir2-lvar-kind 2lvar
)
498 (let* ((locs (ir2-lvar-locs 2lvar
))
499 (nlocs (length locs
))
500 (ntypes (length types
)))
501 (if (and (= nlocs ntypes
)
502 (do ((loc locs
(cdr loc
))
503 (type types
(cdr type
)))
505 (unless (eq (tn-primitive-type (car loc
)) (car type
))
508 (mapcar (lambda (loc type
)
509 (if (eq (tn-primitive-type loc
) type
)
511 (make-normal-tn type
)))
514 (mapcar #'make-normal-tn
515 (subseq types nlocs
)))
519 (mapcar #'make-normal-tn types
))))))
521 ;;; Make the first N standard value TNs, returning them in a list.
522 (defun make-standard-value-tns (n)
523 (declare (type unsigned-byte n
))
526 (res (standard-arg-location i
)))
529 ;;; Return a list of TNs wired to the standard value passing
530 ;;; conventions that can be used to receive values according to the
531 ;;; unknown-values convention. This is used together with
532 ;;; MOVE-LVAR-RESULT for delivering unknown values to a fixed values
535 ;;; If the lvar isn't annotated, then we treat as 0-values, returning
536 ;;; an empty list of temporaries.
538 ;;; If the lvar is annotated, then it must be :FIXED.
539 (defun standard-result-tns (lvar)
540 (declare (type (or lvar null
) lvar
))
542 (let ((2lvar (lvar-info lvar
)))
543 (ecase (ir2-lvar-kind 2lvar
)
545 (make-standard-value-tns (length (ir2-lvar-locs 2lvar
))))))
548 ;;; Just move each SRC TN into the corresponding DEST TN, defaulting
549 ;;; any unsupplied source values to NIL. We let EMIT-MOVE worry about
550 ;;; doing the appropriate coercions.
551 (defun move-results-coerced (node block src dest
)
552 (declare (type node node
) (type ir2-block block
) (list src dest
))
553 (let ((nsrc (length src
))
554 (ndest (length dest
)))
555 (mapc (lambda (from to
)
557 (emit-move node block from to
)))
559 (append src
(make-list (- ndest nsrc
)
560 :initial-element
(emit-constant nil
)))
565 ;;; Move each SRC TN into the corresponding DEST TN, checking types
566 ;;; and defaulting any unsupplied source values to NIL
567 (defun move-results-checked (node block src dest types
)
568 (declare (type node node
) (type ir2-block block
) (list src dest types
))
569 (let ((nsrc (length src
))
570 (ndest (length dest
))
571 (ntypes (length types
)))
572 (mapc (lambda (from to type
)
574 (emit-type-check node block from to type
)
575 (emit-move node block from to
)))
577 (append src
(make-list (- ndest nsrc
)
578 :initial-element
(emit-constant nil
)))
582 (append types
(make-list (- ndest ntypes
)))
586 ;;; If necessary, emit coercion code needed to deliver the RESULTS to
587 ;;; the specified lvar. NODE and BLOCK provide context for emitting
588 ;;; code. Although usually obtained from STANDARD-RESULT-TNs or
589 ;;; LVAR-RESULT-TNs, RESULTS may be a list of any type or
592 ;;; If the lvar is fixed values, then move the results into the lvar
593 ;;; locations. If the lvar is unknown values, then do the moves into
594 ;;; the standard value locations, and use PUSH-VALUES to put the
595 ;;; values on the stack.
596 (defun move-lvar-result (node block results lvar
)
597 (declare (type node node
) (type ir2-block block
)
598 (list results
) (type (or lvar null
) lvar
))
600 (let ((2lvar (lvar-info lvar
)))
601 (ecase (ir2-lvar-kind 2lvar
)
603 (let ((locs (ir2-lvar-locs 2lvar
)))
604 (unless (eq locs results
)
605 (move-results-coerced node block results locs
))))
607 (let* ((nvals (length results
))
608 (locs (make-standard-value-tns nvals
)))
609 (move-results-coerced node block results locs
)
610 (vop* push-values node block
611 ((reference-tn-list locs nil
))
612 ((reference-tn-list (ir2-lvar-locs 2lvar
) t
))
617 (defun ir2-convert-cast (node block
)
618 (declare (type cast node
)
619 (type ir2-block block
))
620 (binding* ((lvar (node-lvar node
) :exit-if-null
)
621 (2lvar (lvar-info lvar
))
622 (value (cast-value node
))
623 (2value (lvar-info value
)))
624 (cond ((eq (ir2-lvar-kind 2lvar
) :unused
))
625 ((eq (ir2-lvar-kind 2lvar
) :unknown
)
626 (aver (eq (ir2-lvar-kind 2value
) :unknown
))
627 (aver (not (cast-type-check node
)))
628 (move-results-coerced node block
629 (ir2-lvar-locs 2value
)
630 (ir2-lvar-locs 2lvar
)))
631 ((eq (ir2-lvar-kind 2lvar
) :fixed
)
632 (aver (eq (ir2-lvar-kind 2value
) :fixed
))
633 (if (cast-type-check node
)
634 (move-results-checked node block
635 (ir2-lvar-locs 2value
)
636 (ir2-lvar-locs 2lvar
)
637 (multiple-value-bind (check types
)
638 (cast-check-types node nil
)
639 (aver (eq check
:simple
))
641 (move-results-coerced node block
642 (ir2-lvar-locs 2value
)
643 (ir2-lvar-locs 2lvar
))))
644 (t (bug "CAST cannot be :DELAYED.")))))
646 ;;;; template conversion
648 ;;; Build a TN-REFS list that represents access to the values of the
649 ;;; specified list of lvars ARGS for TEMPLATE. Any :CONSTANT arguments
650 ;;; are returned in the second value as a list rather than being
651 ;;; accessed as a normal argument. NODE and BLOCK provide the context
652 ;;; for emitting any necessary type-checking code.
653 (defun reference-args (node block args template
)
654 (declare (type node node
) (type ir2-block block
) (list args
)
655 (type template template
))
656 (collect ((info-args))
659 (do ((args args
(cdr args
))
660 (types (template-arg-types template
) (cdr types
)))
662 (let ((type (first types
))
664 (if (and (consp type
) (eq (car type
) ':constant
))
665 (info-args (lvar-value arg
))
666 (let ((ref (reference-tn (lvar-tn node block arg
) nil
)))
668 (setf (tn-ref-across last
) ref
)
672 (values (the (or tn-ref null
) first
) (info-args)))))
674 ;;; Convert a conditional template. We try to exploit any
675 ;;; drop-through, but emit an unconditional branch afterward if we
676 ;;; fail. NOT-P is true if the sense of the TEMPLATE's test should be
678 (defun ir2-convert-conditional (node block template args info-args if not-p
)
679 (declare (type node node
) (type ir2-block block
)
680 (type template template
) (type (or tn-ref null
) args
)
681 (list info-args
) (type cif if
) (type boolean not-p
))
682 (let ((consequent (if-consequent if
))
683 (alternative (if-alternative if
))
684 (flags (and (consp (template-result-types template
))
685 (rest (template-result-types template
)))))
686 (aver (= (template-info-arg-count template
)
687 (+ (length info-args
)
690 (rotatef consequent alternative
)
692 (when (drop-thru-p if consequent
)
693 (rotatef consequent alternative
)
696 (emit-template node block template args nil
697 (list* (block-label consequent
) not-p
699 (if (drop-thru-p if alternative
)
700 (register-drop-thru alternative
)
701 (vop branch node block
(block-label alternative
))))
703 (emit-template node block template args nil info-args
)
704 (vop branch-if node block
(block-label consequent
) flags not-p
)
705 (if (drop-thru-p if alternative
)
706 (register-drop-thru alternative
)
707 (vop branch node block
(block-label alternative
)))))))
709 ;;; Convert an IF that isn't the DEST of a conditional template.
710 (defun ir2-convert-if (node block
)
711 (declare (type ir2-block block
) (type cif node
))
712 (let* ((test (if-test node
))
713 (test-ref (reference-tn (lvar-tn node block test
) nil
))
714 (nil-ref (reference-tn (emit-constant nil
) nil
)))
715 (setf (tn-ref-across test-ref
) nil-ref
)
716 (ir2-convert-conditional node block
(template-or-lose 'if-eq
)
717 test-ref
() node t
)))
719 ;;; Return a list of primitive-types that we can pass to LVAR-RESULT-TNS
720 ;;; describing the result types we want for a template call. We are really
721 ;;; only interested in the number of results required: in normal case
722 ;;; TEMPLATE-RESULTS-OK has already checked them.
723 (defun find-template-result-types (call rtypes
)
724 (let* ((type (node-derived-type call
))
726 (mapcar #'primitive-type
727 (if (args-type-p type
)
728 (append (args-type-required type
)
729 (args-type-optional type
))
731 (primitive-t *backend-t-primitive-type
*))
732 (loop for rtype in rtypes
733 for type
= (or (pop types
) primitive-t
)
736 ;;; Return a list of TNs usable in a CALL to TEMPLATE delivering values to
737 ;;; LVAR. As an efficiency hack, we pick off the common case where the LVAR is
738 ;;; fixed values and has locations that satisfy the result restrictions. This
739 ;;; can fail when there is a type check or a values count mismatch.
740 (defun make-template-result-tns (call lvar rtypes
)
741 (declare (type combination call
) (type (or lvar null
) lvar
)
743 (let ((2lvar (when lvar
(lvar-info lvar
))))
744 (if (and 2lvar
(eq (ir2-lvar-kind 2lvar
) :fixed
))
745 (let ((locs (ir2-lvar-locs 2lvar
)))
746 (if (and (= (length rtypes
) (length locs
))
747 (do ((loc locs
(cdr loc
))
748 (rtypes rtypes
(cdr rtypes
)))
750 (unless (operand-restriction-ok
752 (tn-primitive-type (car loc
))
758 (find-template-result-types call rtypes
))))
761 (find-template-result-types call rtypes
)))))
763 ;;; Get the operands into TNs, make TN-REFs for them, and then call
764 ;;; the template emit function.
765 (defun ir2-convert-template (call block
)
766 (declare (type combination call
) (type ir2-block block
))
767 (let* ((template (combination-info call
))
768 (lvar (node-lvar call
))
769 (rtypes (template-result-types template
)))
770 (multiple-value-bind (args info-args
)
771 (reference-args call block
(combination-args call
) template
)
772 (aver (not (template-more-results-type template
)))
773 (if (template-conditional-p template
)
774 (ir2-convert-conditional call block template args info-args
775 (lvar-dest lvar
) nil
)
776 (let* ((results (make-template-result-tns call lvar rtypes
))
777 (r-refs (reference-tn-list results t
)))
778 (aver (= (length info-args
)
779 (template-info-arg-count template
)))
780 (when (and lvar
(lvar-dynamic-extent lvar
))
781 (vop current-stack-pointer call block
782 (ir2-lvar-stack-pointer (lvar-info lvar
))))
783 (when (emit-step-p call
)
784 (vop sb
!vm
::step-instrument-before-vop call block
))
786 (emit-template call block template args r-refs info-args
)
787 (emit-template call block template args r-refs
))
788 (move-lvar-result call block results lvar
)))))
791 ;;; We don't have to do much because operand count checking is done by
792 ;;; IR1 conversion. The only difference between this and the function
793 ;;; case of IR2-CONVERT-TEMPLATE is that there can be codegen-info
795 (defoptimizer (%%primitive ir2-convert
) ((template info
&rest args
) call block
)
796 (declare (ignore args
))
797 (let* ((template (lvar-value template
))
798 (info (lvar-value info
))
799 (lvar (node-lvar call
))
800 (rtypes (template-result-types template
))
801 (results (make-template-result-tns call lvar rtypes
))
802 (r-refs (reference-tn-list results t
)))
803 (multiple-value-bind (args info-args
)
804 (reference-args call block
(cddr (combination-args call
)) template
)
805 (aver (not (template-more-results-type template
)))
806 (aver (not (template-conditional-p template
)))
807 (aver (null info-args
))
810 (emit-template call block template args r-refs info
)
811 (emit-template call block template args r-refs
))
813 (move-lvar-result call block results lvar
)))
816 (defoptimizer (%%primitive derive-type
) ((template info
&rest args
))
817 (declare (ignore info args
))
818 (let ((type (template-type (lvar-value template
))))
819 (if (fun-type-p type
)
820 (fun-type-returns type
)
825 ;;; Convert a LET by moving the argument values into the variables.
826 ;;; Since a LET doesn't have any passing locations, we move the
827 ;;; arguments directly into the variables. We must also allocate any
828 ;;; indirect value cells, since there is no function prologue to do
830 (defun ir2-convert-let (node block fun
)
831 (declare (type combination node
) (type ir2-block block
) (type clambda fun
))
832 (mapc (lambda (var arg
)
834 (let ((src (lvar-tn node block arg
))
835 (dest (leaf-info var
)))
836 (if (and (lambda-var-indirect var
)
837 (lambda-var-explicit-value-cell var
))
838 (emit-make-value-cell node block src dest
)
839 (emit-move node block src dest
)))))
840 (lambda-vars fun
) (basic-combination-args node
))
843 ;;; Emit any necessary moves into assignment temps for a local call to
844 ;;; FUN. We return two lists of TNs: TNs holding the actual argument
845 ;;; values, and (possibly EQ) TNs that are the actual destination of
846 ;;; the arguments. When necessary, we allocate temporaries for
847 ;;; arguments to preserve parallel assignment semantics. These lists
848 ;;; exclude unused arguments and include implicit environment
849 ;;; arguments, i.e. they exactly correspond to the arguments passed.
851 ;;; OLD-FP is the TN currently holding the value we want to pass as
852 ;;; OLD-FP. If null, then the call is to the same environment (an
853 ;;; :ASSIGNMENT), so we only move the arguments, and leave the
854 ;;; environment alone.
856 ;;; CLOSURE-FP is for calling a closure that has "implicit" value
857 ;;; cells (stored in the allocating stack frame), and is the frame
858 ;;; pointer TN to use for values allocated in the outbound stack
859 ;;; frame. This is distinct from OLD-FP for the specific case of a
861 (defun emit-psetq-moves (node block fun old-fp
&optional
(closure-fp old-fp
))
862 (declare (type combination node
) (type ir2-block block
) (type clambda fun
)
863 (type (or tn null
) old-fp closure-fp
))
864 (let ((actuals (mapcar (lambda (x)
866 (lvar-tn node block x
)))
867 (combination-args node
))))
870 (dolist (var (lambda-vars fun
))
871 (let ((actual (pop actuals
))
872 (loc (leaf-info var
)))
875 ((and (lambda-var-indirect var
)
876 (lambda-var-explicit-value-cell var
))
878 (make-normal-tn *backend-t-primitive-type
*)))
879 (emit-make-value-cell node block actual temp
)
881 ((member actual
(locs))
882 (let ((temp (make-normal-tn (tn-primitive-type loc
))))
883 (emit-move node block actual temp
)
890 (let ((this-1env (node-physenv node
))
891 (called-env (physenv-info (lambda-physenv fun
))))
892 (dolist (thing (ir2-physenv-closure called-env
))
893 (temps (closure-initial-value (car thing
) this-1env closure-fp
))
896 (locs (ir2-physenv-old-fp called-env
))))
898 (values (temps) (locs)))))
900 ;;; A tail-recursive local call is done by emitting moves of stuff
901 ;;; into the appropriate passing locations. After setting up the args
902 ;;; and environment, we just move our return-pc into the called
903 ;;; function's passing location.
904 (defun ir2-convert-tail-local-call (node block fun
)
905 (declare (type combination node
) (type ir2-block block
) (type clambda fun
))
906 (let ((this-env (physenv-info (node-physenv node
)))
907 (current-fp (make-stack-pointer-tn)))
908 (multiple-value-bind (temps locs
)
909 (emit-psetq-moves node block fun
910 (ir2-physenv-old-fp this-env
) current-fp
)
912 ;; If we're about to emit a move from CURRENT-FP then we need to
914 (when (find current-fp temps
)
915 (vop current-fp node block current-fp
))
917 (mapc (lambda (temp loc
)
918 (emit-move node block temp loc
))
921 (emit-move node block
922 (ir2-physenv-return-pc this-env
)
923 (ir2-physenv-return-pc-pass
925 (lambda-physenv fun
)))))
929 ;;; Convert an :ASSIGNMENT call. This is just like a tail local call,
930 ;;; except that the caller and callee environment are the same, so we
931 ;;; don't need to mess with the environment locations, return PC, etc.
932 (defun ir2-convert-assignment (node block fun
)
933 (declare (type combination node
) (type ir2-block block
) (type clambda fun
))
934 (multiple-value-bind (temps locs
) (emit-psetq-moves node block fun nil
)
936 (mapc (lambda (temp loc
)
937 (emit-move node block temp loc
))
941 ;;; Do stuff to set up the arguments to a non-tail local call
942 ;;; (including implicit environment args.) We allocate a frame
943 ;;; (returning the FP and NFP), and also compute the TN-REFS list for
944 ;;; the values to pass and the list of passing location TNs.
945 (defun ir2-convert-local-call-args (node block fun
)
946 (declare (type combination node
) (type ir2-block block
) (type clambda fun
))
947 (let ((fp (make-stack-pointer-tn))
948 (nfp (make-number-stack-pointer-tn))
949 (old-fp (make-stack-pointer-tn)))
950 (multiple-value-bind (temps locs
)
951 (emit-psetq-moves node block fun old-fp
)
952 (vop current-fp node block old-fp
)
953 (vop allocate-frame node block
954 (physenv-info (lambda-physenv fun
))
956 (values fp nfp temps
(mapcar #'make-alias-tn locs
)))))
958 ;;; Handle a non-TR known-values local call. We emit the call, then
959 ;;; move the results to the lvar's destination.
960 (defun ir2-convert-local-known-call (node block fun returns lvar start
)
961 (declare (type node node
) (type ir2-block block
) (type clambda fun
)
962 (type return-info returns
) (type (or lvar null
) lvar
)
964 (multiple-value-bind (fp nfp temps arg-locs
)
965 (ir2-convert-local-call-args node block fun
)
966 (let ((locs (return-info-locations returns
)))
967 (vop* known-call-local node block
968 (fp nfp
(reference-tn-list temps nil
))
969 ((reference-tn-list locs t
))
970 arg-locs
(physenv-info (lambda-physenv fun
)) start
)
971 (move-lvar-result node block locs lvar
)))
974 ;;; Handle a non-TR unknown-values local call. We do different things
975 ;;; depending on what kind of values the lvar wants.
977 ;;; If LVAR is :UNKNOWN, then we use the "multiple-" variant, directly
978 ;;; specifying the lvar's LOCS as the VOP results so that we don't
979 ;;; have to do anything after the call.
981 ;;; Otherwise, we use STANDARD-RESULT-TNS to get wired result TNs, and
982 ;;; then call MOVE-LVAR-RESULT to do any necessary type checks or
984 (defun ir2-convert-local-unknown-call (node block fun lvar start
)
985 (declare (type node node
) (type ir2-block block
) (type clambda fun
)
986 (type (or lvar null
) lvar
) (type label start
))
987 (multiple-value-bind (fp nfp temps arg-locs
)
988 (ir2-convert-local-call-args node block fun
)
989 (let ((2lvar (and lvar
(lvar-info lvar
)))
990 (env (physenv-info (lambda-physenv fun
)))
991 (temp-refs (reference-tn-list temps nil
)))
992 (if (and 2lvar
(eq (ir2-lvar-kind 2lvar
) :unknown
))
993 (vop* multiple-call-local node block
(fp nfp temp-refs
)
994 ((reference-tn-list (ir2-lvar-locs 2lvar
) t
))
996 (let ((locs (standard-result-tns lvar
)))
997 (vop* call-local node block
999 ((reference-tn-list locs t
))
1000 arg-locs env start
(length locs
))
1001 (move-lvar-result node block locs lvar
)))))
1004 ;;; Dispatch to the appropriate function, depending on whether we have
1005 ;;; a let, tail or normal call. If the function doesn't return, call
1006 ;;; it using the unknown-value convention. We could compile it as a
1007 ;;; tail call, but that might seem confusing in the debugger.
1008 (defun ir2-convert-local-call (node block
)
1009 (declare (type combination node
) (type ir2-block block
))
1010 (let* ((fun (ref-leaf (lvar-uses (basic-combination-fun node
))))
1011 (kind (functional-kind fun
)))
1012 (cond ((eq kind
:let
)
1013 (ir2-convert-let node block fun
))
1014 ((eq kind
:assignment
)
1015 (ir2-convert-assignment node block fun
))
1017 (ir2-convert-tail-local-call node block fun
))
1019 (let ((start (block-trampoline (lambda-block fun
)))
1020 (returns (tail-set-info (lambda-tail-set fun
)))
1021 (lvar (node-lvar node
)))
1023 (return-info-kind returns
)
1026 (ir2-convert-local-unknown-call node block fun lvar start
))
1028 (ir2-convert-local-known-call node block fun returns
1034 ;;; Given a function lvar FUN, return (VALUES TN-TO-CALL NAMED-P),
1035 ;;; where TN-TO-CALL is a TN holding the thing that we call NAMED-P is
1036 ;;; true if the thing is named (false if it is a function).
1038 ;;; There are two interesting non-named cases:
1039 ;;; -- We know it's a function. No check needed: return the
1041 ;;; -- We don't know what it is.
1042 (defun fun-lvar-tn (node block lvar
)
1043 (declare (ignore node block
))
1044 (declare (type lvar lvar
))
1045 (let ((2lvar (lvar-info lvar
)))
1046 (if (eq (ir2-lvar-kind 2lvar
) :delayed
)
1047 (let ((name (lvar-fun-name lvar t
)))
1049 (values (make-load-time-constant-tn :fdefinition name
) t
))
1050 (let* ((locs (ir2-lvar-locs 2lvar
))
1052 (function-ptype (primitive-type-or-lose 'function
)))
1053 (aver (and (eq (ir2-lvar-kind 2lvar
) :fixed
)
1054 (= (length locs
) 1)))
1055 (aver (eq (tn-primitive-type loc
) function-ptype
))
1056 (values loc nil
)))))
1058 ;;; Set up the args to NODE in the current frame, and return a TN-REF
1059 ;;; list for the passing locations.
1060 (defun move-tail-full-call-args (node block
)
1061 (declare (type combination node
) (type ir2-block block
))
1062 (let ((args (basic-combination-args node
))
1065 (dotimes (num (length args
))
1066 (let ((loc (standard-arg-location num
)))
1067 (emit-move node block
(lvar-tn node block
(elt args num
)) loc
)
1068 (let ((ref (reference-tn loc nil
)))
1070 (setf (tn-ref-across last
) ref
)
1075 ;;; Move the arguments into the passing locations and do a (possibly
1076 ;;; named) tail call.
1077 (defun ir2-convert-tail-full-call (node block
)
1078 (declare (type combination node
) (type ir2-block block
))
1079 (let* ((env (physenv-info (node-physenv node
)))
1080 (args (basic-combination-args node
))
1081 (nargs (length args
))
1082 (pass-refs (move-tail-full-call-args node block
))
1083 (old-fp (ir2-physenv-old-fp env
))
1084 (return-pc (ir2-physenv-return-pc env
)))
1086 (multiple-value-bind (fun-tn named
)
1087 (fun-lvar-tn node block
(basic-combination-fun node
))
1089 (vop* tail-call-named node block
1090 (fun-tn old-fp return-pc pass-refs
)
1094 (vop* tail-call node block
1095 (fun-tn old-fp return-pc pass-refs
)
1098 (emit-step-p node
)))))
1102 ;;; like IR2-CONVERT-LOCAL-CALL-ARGS, only different
1103 (defun ir2-convert-full-call-args (node block
)
1104 (declare (type combination node
) (type ir2-block block
))
1105 (let* ((args (basic-combination-args node
))
1106 (fp (make-stack-pointer-tn))
1107 (nargs (length args
)))
1108 (vop allocate-full-call-frame node block nargs fp
)
1112 (dotimes (num nargs
)
1113 (locs (standard-arg-location num
))
1114 (let ((ref (reference-tn (lvar-tn node block
(elt args num
))
1117 (setf (tn-ref-across last
) ref
)
1121 (values fp first
(locs) nargs
)))))
1123 ;;; Do full call when a fixed number of values are desired. We make
1124 ;;; STANDARD-RESULT-TNS for our lvar, then deliver the result using
1125 ;;; MOVE-LVAR-RESULT. We do named or normal call, as appropriate.
1126 (defun ir2-convert-fixed-full-call (node block
)
1127 (declare (type combination node
) (type ir2-block block
))
1128 (multiple-value-bind (fp args arg-locs nargs
)
1129 (ir2-convert-full-call-args node block
)
1130 (let* ((lvar (node-lvar node
))
1131 (locs (standard-result-tns lvar
))
1132 (loc-refs (reference-tn-list locs t
))
1133 (nvals (length locs
)))
1134 (multiple-value-bind (fun-tn named
)
1135 (fun-lvar-tn node block
(basic-combination-fun node
))
1137 (vop* call-named node block
(fp fun-tn args
) (loc-refs)
1138 arg-locs nargs nvals
(emit-step-p node
))
1139 (vop* call node block
(fp fun-tn args
) (loc-refs)
1140 arg-locs nargs nvals
(emit-step-p node
)))
1141 (move-lvar-result node block locs lvar
))))
1144 ;;; Do full call when unknown values are desired.
1145 (defun ir2-convert-multiple-full-call (node block
)
1146 (declare (type combination node
) (type ir2-block block
))
1147 (multiple-value-bind (fp args arg-locs nargs
)
1148 (ir2-convert-full-call-args node block
)
1149 (let* ((lvar (node-lvar node
))
1150 (locs (ir2-lvar-locs (lvar-info lvar
)))
1151 (loc-refs (reference-tn-list locs t
)))
1152 (multiple-value-bind (fun-tn named
)
1153 (fun-lvar-tn node block
(basic-combination-fun node
))
1155 (vop* multiple-call-named node block
(fp fun-tn args
) (loc-refs)
1156 arg-locs nargs
(emit-step-p node
))
1157 (vop* multiple-call node block
(fp fun-tn args
) (loc-refs)
1158 arg-locs nargs
(emit-step-p node
))))))
1161 ;;; stuff to check in PONDER-FULL-CALL
1163 ;;; These came in handy when troubleshooting cold boot after making
1164 ;;; major changes in the package structure: various transforms and
1165 ;;; VOPs and stuff got attached to the wrong symbol, so that
1166 ;;; references to the right symbol were bogusly translated as full
1167 ;;; calls instead of primitives, sending the system off into infinite
1168 ;;; space. Having a report on all full calls generated makes it easier
1169 ;;; to figure out what form caused the problem this time.
1170 (declaim (type (member :minimal
:detailed
:very-detailed
:maximal
)
1171 *track-full-called-fnames
*))
1172 (defvar *track-full-called-fnames
* :minimal
)
1174 ;;; Do some checks (and store some notes relevant for future checks)
1176 ;;; * Is this a full call to something we have reason to know should
1177 ;;; never be full called? (Except as of sbcl-0.7.18 or so, we no
1178 ;;; longer try to ensure this behavior when *FAILURE-P* has already
1180 (defun ponder-full-call (node)
1181 (let* ((lvar (basic-combination-fun node
))
1182 (fname (lvar-fun-name lvar t
)))
1183 (declare (type (or symbol cons
) fname
))
1185 (let* ((inlineable-p (not (let ((*lexenv
* (node-lexenv node
)))
1186 (fun-lexically-notinline-p fname
))))
1187 (inlineable-bit (if inlineable-p
1 0))
1188 (cell (info :function
:emitted-full-calls fname
)))
1190 ;; The low bit indicates whether any not-NOTINLINE call was seen.
1191 ;; The next-lowest bit is magic. Refer to %COMPILER-DEFMACRO
1192 ;; and WARN-IF-INLINE-FAILED/CALL for the pertinent logic.
1193 (setf cell
(list (logior 4 inlineable-bit
))
1194 (info :function
:emitted-full-calls fname
) cell
)
1195 (incf (car cell
) (+ 4 (if (oddp (car cell
)) 0 inlineable-bit
))))
1196 ;; If the full call was wanted, don't record anything.
1197 ;; (This was originally for debugging SBCL self-compilation)
1200 (warn-if-inline-failed/call fname
(node-lexenv node
) cell
))
1201 (case *track-full-called-fnames
*
1203 (when (boundp 'sb
!xc
:*compile-file-pathname
*)
1204 (pushnew sb
!xc
:*compile-file-pathname
* (cdr cell
)
1207 (pushnew (component-name *component-being-compiled
*)
1208 (cdr cell
) :test
#'equalp
)))))
1210 ;; Special mode, usually only for the cross-compiler
1211 ;; and only with the feature enabled.
1212 #!+sb-show
(when (eq *track-full-called-fnames
* :maximal
)
1213 (/show
"converting full call to named function" fname
)
1214 (/show
(basic-combination-args node
))
1215 (/show
(policy node speed
) (policy node safety
))
1216 (/show
(policy node compilation-speed
))
1217 (let ((arg-types (mapcar (lambda (lvar)
1221 (basic-combination-args node
))))
1224 ;; When illegal code is compiled, all sorts of perverse paths
1225 ;; through the compiler can be taken, and it's much harder -- and
1226 ;; probably pointless -- to guarantee that always-optimized-away
1227 ;; functions are actually optimized away. Thus, we skip the check
1230 ;; check to see if we know anything about the function
1231 (let ((info (info :function
:info fname
)))
1232 ;; if we know something, check to see if the full call was valid
1233 (when (and info
(ir1-attributep (fun-info-attributes info
)
1234 always-translatable
))
1235 (/show
(policy node speed
) (policy node safety
))
1236 (/show
(policy node compilation-speed
))
1237 (bug "full call to ~S" fname
))))
1240 (aver (legal-fun-name-p fname
))))) ;; FIXME: needless check?
1242 ;;; If the call is in a tail recursive position and the return
1243 ;;; convention is standard, then do a tail full call. If one or fewer
1244 ;;; values are desired, then use a single-value call, otherwise use a
1245 ;;; multiple-values call.
1246 (defun ir2-convert-full-call (node block
)
1247 (declare (type combination node
) (type ir2-block block
))
1248 (ponder-full-call node
)
1249 (cond ((node-tail-p node
)
1250 (ir2-convert-tail-full-call node block
))
1251 ((let ((lvar (node-lvar node
)))
1253 (eq (ir2-lvar-kind (lvar-info lvar
)) :unknown
)))
1254 (ir2-convert-multiple-full-call node block
))
1256 (ir2-convert-fixed-full-call node block
)))
1259 ;;;; entering functions
1261 #!+precise-arg-count-error
1262 (defun xep-verify-arg-count (node block fun arg-count-location
)
1263 (when (policy fun
(plusp verify-arg-count
))
1264 (let* ((ef (functional-entry-fun fun
))
1265 (optional (optional-dispatch-p ef
))
1267 (optional-dispatch-min-args ef
)))
1268 (max (cond ((not optional
)
1269 (1- (length (lambda-vars fun
))))
1271 (not (optional-dispatch-more-entry ef
)))
1272 (optional-dispatch-max-args ef
)))))
1273 (vop verify-arg-count node block
1278 ;;; Do all the stuff that needs to be done on XEP entry:
1279 ;;; -- Create frame.
1280 ;;; -- Copy any more arg.
1281 ;;; -- Set up the environment, accessing any closure variables.
1282 ;;; -- Move args from the standard passing locations to their internal
1284 (defun init-xep-environment (node block fun
)
1285 (declare (type bind node
) (type ir2-block block
) (type clambda fun
))
1286 (let ((start-label (entry-info-offset (leaf-info fun
)))
1287 (env (physenv-info (node-physenv node
)))
1289 (let ((ef (functional-entry-fun fun
)))
1290 (vop xep-allocate-frame node block start-label
)
1291 ;; Arg verification needs to be done before the stack pointer is adjusted
1292 ;; so that the extra arguments are still present when the error is signalled
1293 (unless (eq (functional-kind fun
) :toplevel
)
1294 (setf arg-count-tn
(make-arg-count-location))
1295 #!+precise-arg-count-error
1296 (xep-verify-arg-count node block fun arg-count-tn
))
1297 (cond ((and (optional-dispatch-p ef
) (optional-dispatch-more-entry ef
))
1298 ;; COPY-MORE-ARG should handle SP adjustemnt, but it
1299 ;; isn't done on all targets.
1300 #!-precise-arg-count-error
1301 (vop xep-setup-sp node block
)
1302 (vop copy-more-arg node block
(optional-dispatch-max-args ef
)))
1304 (vop xep-setup-sp node block
)))
1305 (when (ir2-physenv-closure env
)
1306 (let ((closure (make-normal-tn *backend-t-primitive-type
*)))
1307 (when (policy fun
(> store-closure-debug-pointer
1))
1308 ;; Save the closure pointer on the stack.
1309 (let ((closure-save (make-representation-tn
1310 *backend-t-primitive-type
*
1311 (sc-number-or-lose 'sb
!vm
::control-stack
))))
1312 (vop setup-closure-environment node block start-label
1314 (setf (ir2-physenv-closure-save-tn env
) closure-save
)
1315 (component-live-tn closure-save
)))
1316 (vop setup-closure-environment node block start-label closure
)
1318 (dolist (loc (ir2-physenv-closure env
))
1319 (vop closure-ref node block closure
(incf n
) (cdr loc
)))))))
1320 (unless (eq (functional-kind fun
) :toplevel
)
1321 (let ((vars (lambda-vars fun
))
1323 (when (leaf-refs (first vars
))
1324 (emit-move node block arg-count-tn
(leaf-info (first vars
))))
1325 (dolist (arg (rest vars
))
1326 (when (leaf-refs arg
)
1327 (let ((pass (standard-arg-location n
))
1328 (home (leaf-info arg
)))
1329 (if (and (lambda-var-indirect arg
)
1330 (lambda-var-explicit-value-cell arg
))
1331 (emit-make-value-cell node block pass home
)
1332 (emit-move node block pass home
))))
1335 (emit-move node block
(make-old-fp-passing-location t
)
1336 (ir2-physenv-old-fp env
)))
1340 ;;; Emit function prolog code. This is only called on bind nodes for
1341 ;;; functions that allocate environments. All semantics of let calls
1342 ;;; are handled by IR2-CONVERT-LET.
1344 ;;; If not an XEP, all we do is move the return PC from its passing
1345 ;;; location, since in a local call, the caller allocates the frame
1346 ;;; and sets up the arguments.
1348 #!+unwind-to-frame-and-call-vop
1349 (defun save-bsp (node block env
)
1350 ;; Save BSP on stack so that the binding environment can be restored
1351 ;; when restarting frames.
1352 ;; This is done inside functions, which leaves XEPs without saved
1353 ;; BSP, though the code in XEPs doesn't bind any variables, it can
1354 ;; call arbitrary code through the SATISFIES declaration.
1355 ;; And functions called by SATISFIES are not inlined, except for
1356 ;; source transforms, but these usually do not bind anything.
1357 ;; Thus when restarting it needs to check that the interrupt was in
1360 ;; It could be saved from the XEP, but some functions have both
1361 ;; external and internal entry points, so it will be saved twice.
1362 (let ((temp (make-normal-tn *backend-t-primitive-type
*))
1363 (bsp-save-tn (make-representation-tn
1364 *backend-t-primitive-type
*
1365 (sc-number-or-lose 'sb
!vm
::control-stack
))))
1366 (vop current-binding-pointer node block temp
)
1367 (emit-move node block temp bsp-save-tn
)
1368 (setf (ir2-physenv-bsp-save-tn env
) bsp-save-tn
)
1369 (component-live-tn bsp-save-tn
)))
1371 (defun ir2-convert-bind (node block
)
1372 (declare (type bind node
) (type ir2-block block
))
1373 (let* ((fun (bind-lambda node
))
1374 (env (physenv-info (lambda-physenv fun
))))
1375 (aver (member (functional-kind fun
)
1376 '(nil :external
:optional
:toplevel
:cleanup
)))
1379 (init-xep-environment node block fun
)
1381 (when *collect-dynamic-statistics
*
1382 (vop count-me node block
*dynamic-counts-tn
*
1383 (block-number (ir2-block-block block
)))))
1384 ((policy fun
(> store-closure-debug-pointer
1))
1385 ;; Propagate the location of the closure pointer from the
1386 ;; enclosing functions. (FIXME: Should make sure that this
1387 ;; handles closures inside closures correctly). [remark by JES]
1388 (let* ((entry-fun (lambda-entry-fun fun
)))
1390 (let ((2env (physenv-info (lambda-physenv fun
)))
1391 (entry-2env (physenv-info (lambda-physenv entry-fun
))))
1392 (setf (ir2-physenv-closure-save-tn 2env
)
1393 (ir2-physenv-closure-save-tn entry-2env
)))))))
1397 (ir2-physenv-return-pc-pass env
)
1398 (ir2-physenv-return-pc env
))
1399 #!+unwind-to-frame-and-call-vop
1400 (when (and (lambda-allow-instrumenting fun
)
1401 (not (lambda-inline-expanded fun
))
1402 (policy fun
(>= insert-debug-catch
1)))
1403 (save-bsp node block env
))
1405 (let ((lab (gen-label)))
1406 (setf (ir2-physenv-environment-start env
) lab
)
1407 (vop note-environment-start node block lab
)
1409 (unless (policy fun
(>= inhibit-safepoints
2))
1410 (vop sb
!vm
::insert-safepoint node block
))))
1414 ;;;; function return
1416 ;;; Do stuff to return from a function with the specified values and
1417 ;;; convention. If the return convention is :FIXED and we aren't
1418 ;;; returning from an XEP, then we do a known return (letting
1419 ;;; representation selection insert the correct move-arg VOPs.)
1420 ;;; Otherwise, we use the unknown-values convention. If there is a
1421 ;;; fixed number of return values, then use RETURN, otherwise use
1422 ;;; RETURN-MULTIPLE.
1423 (defun ir2-convert-return (node block
)
1424 (declare (type creturn node
) (type ir2-block block
))
1425 (let* ((lvar (return-result node
))
1426 (2lvar (lvar-info lvar
))
1427 (lvar-kind (ir2-lvar-kind 2lvar
))
1428 (fun (return-lambda node
))
1429 (env (physenv-info (lambda-physenv fun
)))
1430 (old-fp (ir2-physenv-old-fp env
))
1431 (return-pc (ir2-physenv-return-pc env
))
1432 (returns (tail-set-info (lambda-tail-set fun
))))
1434 ((and (eq (return-info-kind returns
) :fixed
)
1436 (let ((locs (lvar-tns node block lvar
1437 (return-info-types returns
))))
1438 (vop* known-return node block
1439 (old-fp return-pc
(reference-tn-list locs nil
))
1441 (return-info-locations returns
))))
1442 ((eq lvar-kind
:fixed
)
1443 (let* ((types (mapcar #'tn-primitive-type
(ir2-lvar-locs 2lvar
)))
1444 (lvar-locs (lvar-tns node block lvar types
))
1445 (nvals (length lvar-locs
))
1446 (locs (make-standard-value-tns nvals
)))
1447 (mapc (lambda (val loc
)
1448 (emit-move node block val loc
))
1452 (vop return-single node block old-fp return-pc
(car locs
))
1453 (vop* return node block
1454 (old-fp return-pc
(reference-tn-list locs nil
))
1458 (aver (eq lvar-kind
:unknown
))
1459 (vop* return-multiple node block
1461 (reference-tn-list (ir2-lvar-locs 2lvar
) nil
))
1468 ;;;; These are used by the debugger to find the top function on the
1469 ;;;; stack. They return the OLD-FP and RETURN-PC for the current
1470 ;;;; function as multiple values.
1472 (defoptimizer (%caller-frame ir2-convert
) (() node block
)
1473 (let ((ir2-physenv (physenv-info (node-physenv node
))))
1474 (move-lvar-result node block
1475 (list (ir2-physenv-old-fp ir2-physenv
))
1478 (defoptimizer (%caller-pc ir2-convert
) (() node block
)
1479 (let ((ir2-physenv (physenv-info (node-physenv node
))))
1480 (move-lvar-result node block
1481 (list (ir2-physenv-return-pc ir2-physenv
))
1484 ;;;; multiple values
1486 ;;; This is almost identical to IR2-CONVERT-LET. Since LTN annotates
1487 ;;; the lvar for the correct number of values (with the lvar user
1488 ;;; responsible for defaulting), we can just pick them up from the
1490 (defun ir2-convert-mv-bind (node block
)
1491 (declare (type mv-combination node
) (type ir2-block block
))
1492 (let* ((lvar (first (basic-combination-args node
)))
1493 (fun (ref-leaf (lvar-uses (basic-combination-fun node
))))
1494 (vars (lambda-vars fun
)))
1495 (aver (eq (functional-kind fun
) :mv-let
))
1496 (mapc (lambda (src var
)
1497 (when (leaf-refs var
)
1498 (let ((dest (leaf-info var
)))
1499 (if (and (lambda-var-indirect var
)
1500 (lambda-var-explicit-value-cell var
))
1501 (emit-make-value-cell node block src dest
)
1502 (emit-move node block src dest
)))))
1503 (lvar-tns node block lvar
1505 (primitive-type (leaf-type x
)))
1510 ;;; Emit the appropriate fixed value, unknown value or tail variant of
1511 ;;; CALL-VARIABLE. Note that we only need to pass the values start for
1512 ;;; the first argument: all the other argument lvar TNs are
1513 ;;; ignored. This is because we require all of the values globs to be
1514 ;;; contiguous and on stack top.
1515 (defun ir2-convert-mv-call (node block
)
1516 (declare (type mv-combination node
) (type ir2-block block
))
1517 (aver (basic-combination-args node
))
1518 (let* ((start-lvar (lvar-info (first (basic-combination-args node
))))
1519 (start (first (ir2-lvar-locs start-lvar
)))
1520 (tails (and (node-tail-p node
)
1521 (lambda-tail-set (node-home-lambda node
))))
1522 (lvar (node-lvar node
))
1523 (2lvar (and lvar
(lvar-info lvar
))))
1524 (multiple-value-bind (fun named
)
1525 (fun-lvar-tn node block
(basic-combination-fun node
))
1526 (aver (and (not named
)
1527 (eq (ir2-lvar-kind start-lvar
) :unknown
)))
1530 (let ((env (physenv-info (node-physenv node
))))
1531 (vop tail-call-variable node block start fun
1532 (ir2-physenv-old-fp env
)
1533 (ir2-physenv-return-pc env
))))
1535 (eq (ir2-lvar-kind 2lvar
) :unknown
))
1536 (vop* multiple-call-variable node block
(start fun nil
)
1537 ((reference-tn-list (ir2-lvar-locs 2lvar
) t
))
1538 (emit-step-p node
)))
1540 (let ((locs (standard-result-tns lvar
)))
1541 (vop* call-variable node block
(start fun nil
)
1542 ((reference-tn-list locs t
)) (length locs
)
1544 (move-lvar-result node block locs lvar
)))))))
1546 ;;; Reset the stack pointer to the start of the specified
1547 ;;; unknown-values lvar (discarding it and all values globs on top of
1549 (defoptimizer (%pop-values ir2-convert
) ((%lvar
) node block
)
1550 (let* ((lvar (lvar-value %lvar
))
1551 (2lvar (lvar-info lvar
)))
1552 (cond ((eq (ir2-lvar-kind 2lvar
) :unknown
)
1553 (vop reset-stack-pointer node block
1554 (first (ir2-lvar-locs 2lvar
))))
1555 ((lvar-dynamic-extent lvar
)
1556 (vop reset-stack-pointer node block
1557 (ir2-lvar-stack-pointer 2lvar
)))
1558 (t (bug "Trying to pop a not stack-allocated LVAR ~S."
1561 (defoptimizer (%nip-values ir2-convert
) ((last-nipped last-preserved
1564 (let* ( ;; pointer immediately after the nipped block
1565 (after (lvar-value last-nipped
))
1566 (2after (lvar-info after
))
1567 ;; pointer to the first nipped word
1568 (first (lvar-value last-preserved
))
1569 (2first (lvar-info first
))
1571 (moved-tns (loop for lvar-ref in moved
1572 for lvar
= (lvar-value lvar-ref
)
1573 for
2lvar
= (lvar-info lvar
)
1575 collect
(first (ir2-lvar-locs 2lvar
)))))
1576 (aver (or (eq (ir2-lvar-kind 2after
) :unknown
)
1577 (lvar-dynamic-extent after
)))
1578 (aver (eq (ir2-lvar-kind 2first
) :unknown
))
1579 (when *check-consistency
*
1580 ;; we cannot move stack-allocated DX objects
1581 (dolist (moved-lvar moved
)
1582 (aver (eq (ir2-lvar-kind (lvar-info (lvar-value moved-lvar
)))
1584 (flet ((nip-aligned (nipped)
1585 (vop* %%nip-values node block
1587 (first (ir2-lvar-locs 2first
))
1588 (reference-tn-list moved-tns nil
))
1589 ((reference-tn-list moved-tns t
)))))
1590 (cond ((eq (ir2-lvar-kind 2after
) :unknown
)
1591 (nip-aligned (first (ir2-lvar-locs 2after
))))
1592 ((lvar-dynamic-extent after
)
1593 (nip-aligned (ir2-lvar-stack-pointer 2after
)))
1595 (bug "Trying to nip a not stack-allocated LVAR ~S." after
))))))
1597 (defoptimizer (%dummy-dx-alloc ir2-convert
) ((target source
) node block
)
1598 (let* ((target-lvar (lvar-value target
))
1599 (source-lvar (lvar-value source
))
1600 (target-2lvar (lvar-info target-lvar
))
1601 (source-2lvar (and source-lvar
(lvar-info source-lvar
))))
1602 (aver (lvar-dynamic-extent target-lvar
))
1603 (cond ((not source-lvar
)
1604 (vop current-stack-pointer node block
1605 (ir2-lvar-stack-pointer target-2lvar
)))
1606 ((lvar-dynamic-extent source-lvar
)
1607 (emit-move node block
1608 (ir2-lvar-stack-pointer source-2lvar
)
1609 (ir2-lvar-stack-pointer target-2lvar
)))
1610 ((eq (ir2-lvar-kind source-2lvar
) :unknown
)
1611 (emit-move node block
1612 (first (ir2-lvar-locs source-2lvar
))
1613 (ir2-lvar-stack-pointer target-2lvar
)))
1614 (t (bug "Trying to dummy up DX allocation from a ~
1615 not stack-allocated LVAR ~S." source-lvar
)))))
1617 ;;; Deliver the values TNs to LVAR using MOVE-LVAR-RESULT.
1618 (defoptimizer (values ir2-convert
) ((&rest values
) node block
)
1619 (let ((tns (mapcar (lambda (x)
1620 (lvar-tn node block x
))
1622 (move-lvar-result node block tns
(node-lvar node
))))
1624 ;;; In the normal case where unknown values are desired, we use the
1625 ;;; VALUES-LIST VOP. In the relatively unimportant case of VALUES-LIST
1626 ;;; for a fixed number of values, we punt by doing a full call to the
1627 ;;; VALUES-LIST function. This gets the full call VOP to deal with
1628 ;;; defaulting any unsupplied values. It seems unworthwhile to
1629 ;;; optimize this case.
1630 (defoptimizer (values-list ir2-convert
) ((list) node block
)
1631 (let* ((lvar (node-lvar node
))
1632 (2lvar (and lvar
(lvar-info lvar
))))
1634 (eq (ir2-lvar-kind 2lvar
) :unknown
))
1635 (let ((locs (ir2-lvar-locs 2lvar
)))
1636 (vop* values-list node block
1637 ((lvar-tn node block list
) nil
)
1638 ((reference-tn-list locs t
)))))
1639 (t (aver (or (not 2lvar
) ; i.e. we want to check the argument
1640 (eq (ir2-lvar-kind 2lvar
) :fixed
)))
1641 (ir2-convert-full-call node block
)))))
1643 (defoptimizer (%more-arg-values ir2-convert
) ((context start count
) node block
)
1644 (binding* ((lvar (node-lvar node
) :exit-if-null
)
1645 (2lvar (lvar-info lvar
)))
1646 (ecase (ir2-lvar-kind 2lvar
)
1648 ;; KLUDGE: this is very much unsafe, and can leak random stack values.
1649 ;; OTOH, I think the :FIXED case can only happen with (safety 0) in the
1652 (loop for loc in
(ir2-lvar-locs 2lvar
)
1654 do
(vop sb
!vm
::more-arg node block
1655 (lvar-tn node block context
)
1659 (let ((locs (ir2-lvar-locs 2lvar
)))
1660 (vop* %more-arg-values node block
1661 ((lvar-tn node block context
)
1662 (lvar-tn node block start
)
1663 (lvar-tn node block count
)
1665 ((reference-tn-list locs t
))))))))
1667 ;;;; special binding
1669 ;;; This is trivial, given our assumption of a shallow-binding
1671 (defoptimizer (%special-bind ir2-convert
) ((var value
) node block
)
1672 (let ((name (leaf-source-name (lvar-value var
))))
1673 #!-
(and sb-thread x86-64
)
1674 (vop bind node block
(lvar-tn node block value
) (emit-constant name
))
1675 #!+(and sb-thread x86-64
)
1677 ;; GC must understand that the symbol is implicitly live even though
1678 ;; binding makes no references to the object.
1679 (emit-constant name
)
1680 (vop sb
!vm
::bind
/let node block
(lvar-tn node block value
) name
))))
1682 (defoptimizer (%special-unbind ir2-convert
) ((var) node block
)
1683 (declare (ignore var
))
1684 (vop unbind node block
))
1686 ;;; ### It's not clear that this really belongs in this file, or
1687 ;;; should really be done this way, but this is the least violation of
1688 ;;; abstraction in the current setup. We don't want to wire
1689 ;;; shallow-binding assumptions into IR1tran.
1690 (def-ir1-translator progv
1691 ((vars vals
&body body
) start next result
)
1694 (with-unique-names (bind unbind
)
1695 (once-only ((n-save-bs '(%primitive current-binding-pointer
)))
1698 (labels ((,unbind
(vars)
1699 (declare (optimize (speed 2) (debug 0)))
1700 (let ((unbound-marker (%primitive make-unbound-marker
)))
1702 ;; CLHS says "bound and then made to have no value" -- user
1703 ;; should not be able to tell the difference between that and this.
1704 (about-to-modify-symbol-value var
'progv
)
1705 (%primitive bind unbound-marker var
))))
1707 (declare (optimize (speed 2) (debug 0)
1708 (insert-debug-catch 0)))
1710 ((null vals
) (,unbind vars
))
1712 (let ((val (car vals
))
1714 (about-to-modify-symbol-value var
'progv val t
)
1715 (%primitive bind val var
))
1716 (,bind
(cdr vars
) (cdr vals
))))))
1717 (,bind
,vars
,vals
))
1720 ;; Technically ANSI CL doesn't allow declarations at the
1721 ;; start of the cleanup form. SBCL happens to allow for
1722 ;; them, due to the way the UNWIND-PROTECT ir1 translation
1723 ;; is implemented; the cleanup forms are directly spliced
1724 ;; into an FLET definition body. And a declaration here
1725 ;; actually has exactly the right scope for what we need
1726 ;; (ensure that debug instrumentation is not emitted for the
1727 ;; cleanup function). -- JES, 2007-06-16
1728 (declare (optimize (insert-debug-catch 0)))
1729 (%primitive unbind-to-here
,n-save-bs
))))))
1733 ;;; Convert a non-local lexical exit. First find the NLX-INFO in our
1734 ;;; environment. Note that this is never called on the escape exits
1735 ;;; for CATCH and UNWIND-PROTECT, since the escape functions aren't
1737 (defun ir2-convert-exit (node block
)
1738 (declare (type exit node
) (type ir2-block block
))
1739 (let* ((nlx (exit-nlx-info node
))
1740 (loc (find-in-physenv nlx
(node-physenv node
)))
1741 (temp (make-stack-pointer-tn))
1742 (value (exit-value node
)))
1743 (if (nlx-info-safe-p nlx
)
1744 (vop value-cell-ref node block loc temp
)
1745 (emit-move node block loc temp
))
1747 (let ((locs (ir2-lvar-locs (lvar-info value
))))
1748 (vop unwind node block temp
(first locs
) (second locs
)))
1749 (let ((0-tn (emit-constant 0)))
1750 (vop unwind node block temp
0-tn
0-tn
))))
1754 ;;; %CLEANUP-POINT doesn't do anything except prevent the body from
1755 ;;; being entirely deleted.
1756 (defoptimizer (%cleanup-point ir2-convert
) (() node block
) node block
)
1758 ;;; This function invalidates a lexical exit on exiting from the
1759 ;;; dynamic extent. This is done by storing 0 into the indirect value
1760 ;;; cell that holds the closed unwind block.
1761 (defoptimizer (%lexical-exit-breakup ir2-convert
) ((info) node block
)
1762 (let ((nlx (lvar-value info
)))
1763 (when (nlx-info-safe-p nlx
)
1764 (vop value-cell-set node block
1765 (find-in-physenv nlx
(node-physenv node
))
1766 (emit-constant 0)))))
1768 ;;; We have to do a spurious move of no values to the result lvar so
1769 ;;; that lifetime analysis won't get confused.
1770 (defun ir2-convert-throw (node block
)
1771 (declare (type mv-combination node
) (type ir2-block block
))
1772 (let ((args (basic-combination-args node
)))
1773 (check-catch-tag-type (first args
))
1774 (vop* throw node block
1775 ((lvar-tn node block
(first args
))
1777 (ir2-lvar-locs (lvar-info (second args
)))
1780 (move-lvar-result node block
() (node-lvar node
))
1783 ;;; Emit code to set up a non-local exit. INFO is the NLX-INFO for the
1784 ;;; exit, and TAG is the lvar for the catch tag (if any.) We get at
1785 ;;; the target PC by passing in the label to the vop. The vop is
1786 ;;; responsible for building a return-PC object.
1787 (defun emit-nlx-start (node block info tag
)
1788 (declare (type node node
) (type ir2-block block
) (type nlx-info info
)
1789 (type (or lvar null
) tag
))
1790 (let* ((2info (nlx-info-info info
))
1791 (kind (cleanup-kind (nlx-info-cleanup info
)))
1792 (block-tn (physenv-live-tn
1793 (make-normal-tn (primitive-type-or-lose 'catch-block
))
1794 (node-physenv node
)))
1795 (res (make-stack-pointer-tn))
1796 (target-label (ir2-nlx-info-target 2info
)))
1798 (vop current-binding-pointer node block
1799 (car (ir2-nlx-info-dynamic-state 2info
)))
1800 (vop* save-dynamic-state node block
1802 ((reference-tn-list (cdr (ir2-nlx-info-dynamic-state 2info
)) t
)))
1803 (vop current-stack-pointer node block
(ir2-nlx-info-save-sp 2info
))
1807 (vop make-catch-block node block block-tn
1808 (lvar-tn node block tag
) target-label res
))
1809 ((:unwind-protect
:block
:tagbody
)
1810 (vop make-unwind-block node block block-tn target-label res
)))
1814 (if (nlx-info-safe-p info
)
1815 (emit-make-value-cell node block res
(ir2-nlx-info-home 2info
))
1816 (emit-move node block res
(ir2-nlx-info-home 2info
))))
1818 (vop set-unwind-protect node block block-tn
))
1823 ;;; Scan each of ENTRY's exits, setting up the exit for each lexical exit.
1824 (defun ir2-convert-entry (node block
)
1825 (declare (type entry node
) (type ir2-block block
))
1827 (dolist (exit (entry-exits node
))
1828 (let ((info (exit-nlx-info exit
)))
1830 (not (memq info nlxes
))
1831 (member (cleanup-kind (nlx-info-cleanup info
))
1832 '(:block
:tagbody
)))
1834 (emit-nlx-start node block info nil
)))))
1837 ;;; Set up the unwind block for these guys.
1838 (defoptimizer (%catch ir2-convert
) ((info-lvar tag
) node block
)
1839 (check-catch-tag-type tag
)
1840 (emit-nlx-start node block
(lvar-value info-lvar
) tag
))
1841 (defoptimizer (%unwind-protect ir2-convert
) ((info-lvar cleanup
) node block
)
1842 (declare (ignore cleanup
))
1843 (emit-nlx-start node block
(lvar-value info-lvar
) nil
))
1845 ;;; Emit the entry code for a non-local exit. We receive values and
1846 ;;; restore dynamic state.
1848 ;;; In the case of a lexical exit or CATCH, we look at the exit lvar's
1849 ;;; kind to determine which flavor of entry VOP to emit. If unknown
1850 ;;; values, emit the xxx-MULTIPLE variant to the lvar locs. If fixed
1851 ;;; values, make the appropriate number of temps in the standard
1852 ;;; values locations and use the other variant, delivering the temps
1853 ;;; to the lvar using MOVE-LVAR-RESULT.
1855 ;;; In the UNWIND-PROTECT case, we deliver the first register
1856 ;;; argument, the argument count and the argument pointer to our lvar
1857 ;;; as multiple values. These values are the block exited to and the
1858 ;;; values start and count.
1860 ;;; After receiving values, we restore dynamic state. Except in the
1861 ;;; UNWIND-PROTECT case, the values receiving restores the stack
1862 ;;; pointer. In an UNWIND-PROTECT cleanup, we want to leave the stack
1863 ;;; pointer alone, since the thrown values are still out there.
1864 (defoptimizer (%nlx-entry ir2-convert
) ((info-lvar) node block
)
1865 (let* ((info (lvar-value info-lvar
))
1866 (lvar (node-lvar node
))
1867 (2info (nlx-info-info info
))
1868 (top-loc (ir2-nlx-info-save-sp 2info
))
1869 (start-loc (make-nlx-entry-arg-start-location))
1870 (count-loc (make-arg-count-location))
1871 (target (ir2-nlx-info-target 2info
)))
1873 (ecase (cleanup-kind (nlx-info-cleanup info
))
1874 ((:catch
:block
:tagbody
)
1875 (let ((2lvar (and lvar
(lvar-info lvar
))))
1876 (if (and 2lvar
(eq (ir2-lvar-kind 2lvar
) :unknown
))
1877 (vop* nlx-entry-multiple node block
1878 (top-loc start-loc count-loc nil
)
1879 ((reference-tn-list (ir2-lvar-locs 2lvar
) t
))
1881 (let ((locs (standard-result-tns lvar
)))
1882 (vop* nlx-entry node block
1883 (top-loc start-loc count-loc nil
)
1884 ((reference-tn-list locs t
))
1887 (move-lvar-result node block locs lvar
)))))
1889 (let ((block-loc (standard-arg-location 0)))
1890 (vop uwp-entry node block target block-loc start-loc count-loc
)
1893 (list block-loc start-loc count-loc
)
1897 (when *collect-dynamic-statistics
*
1898 (vop count-me node block
*dynamic-counts-tn
*
1899 (block-number (ir2-block-block block
))))
1901 (vop* restore-dynamic-state node block
1902 ((reference-tn-list (cdr (ir2-nlx-info-dynamic-state 2info
)) nil
))
1904 (vop unbind-to-here node block
1905 (car (ir2-nlx-info-dynamic-state 2info
)))))
1907 ;;;; n-argument functions
1909 (macrolet ((def (name)
1910 `(defoptimizer (,name ir2-convert
) ((&rest args
) node block
)
1911 (let* ((refs (reference-tn-list
1912 (loop for arg in args
1913 for tn
= (make-normal-tn *backend-t-primitive-type
*)
1915 (emit-move node block
(lvar-tn node block arg
) tn
)
1918 (lvar (node-lvar node
))
1919 (res (lvar-result-tns
1921 (list (primitive-type (specifier-type 'list
))))))
1922 (when (and lvar
(lvar-dynamic-extent lvar
))
1923 (vop current-stack-pointer node block
1924 (ir2-lvar-stack-pointer (lvar-info lvar
))))
1925 (vop* ,name node block
(refs) ((first res
) nil
)
1927 (move-lvar-result node block res lvar
)))))
1932 (defoptimizer (mask-signed-field ir2-convert
) ((width x
) node block
)
1934 (when (constant-lvar-p width
)
1935 (case (lvar-value width
)
1936 (#.
(- sb
!vm
:n-word-bits sb
!vm
:n-fixnum-tag-bits
)
1937 (when (or (csubtypep (lvar-type x
)
1938 (specifier-type 'word
))
1939 (csubtypep (lvar-type x
)
1940 (specifier-type 'sb
!vm
:signed-word
)))
1941 (let* ((lvar (node-lvar node
))
1942 (temp (make-normal-tn
1943 (if (csubtypep (lvar-type x
)
1944 (specifier-type 'word
))
1945 (primitive-type-of most-positive-word
)
1947 (- (ash most-positive-word -
1))))))
1948 (results (lvar-result-tns
1950 (list (primitive-type-or-lose 'fixnum
)))))
1951 (emit-move node block
(lvar-tn node block x
) temp
)
1952 (vop sb
!vm
::move-from-word
/fixnum node block
1953 temp
(first results
))
1954 (move-lvar-result node block results lvar
)
1956 (#.sb
!vm
:n-word-bits
1957 (when (csubtypep (lvar-type x
) (specifier-type 'word
))
1958 (let* ((lvar (node-lvar node
))
1959 (temp (make-normal-tn
1960 (primitive-type-of most-positive-word
)))
1961 (results (lvar-result-tns
1963 (list (primitive-type
1964 (specifier-type 'sb
!vm
:signed-word
))))))
1965 (emit-move node block
(lvar-tn node block x
) temp
)
1966 (vop sb
!vm
::word-move node block
1967 temp
(first results
))
1968 (move-lvar-result node block results lvar
)
1970 (if (template-p (basic-combination-info node
))
1971 (ir2-convert-template node block
)
1972 (ir2-convert-full-call node block
))))
1974 ;; just a fancy identity
1975 (defoptimizer (%typep-wrapper ir2-convert
) ((value variable type
) node block
)
1976 (declare (ignore variable type
))
1977 (let* ((lvar (node-lvar node
))
1978 (results (lvar-result-tns lvar
(list (primitive-type-or-lose t
)))))
1979 (emit-move node block
(lvar-tn node block value
) (first results
))
1980 (move-lvar-result node block results lvar
)))
1982 ;;; Convert the code in a component into VOPs.
1983 (defun ir2-convert (component)
1984 (declare (type component component
))
1985 (let (#!+sb-dyncount
1986 (*dynamic-counts-tn
*
1987 (when *collect-dynamic-statistics
*
1989 (block-number (block-next (component-head component
))))
1990 (counts (make-array blocks
1991 :element-type
'(unsigned-byte 32)
1992 :initial-element
0))
1993 (info (make-dyncount-info
1994 :for
(component-name component
)
1995 :costs
(make-array blocks
1996 :element-type
'(unsigned-byte 32)
1999 (setf (ir2-component-dyncount-info (component-info component
))
2001 (emit-constant info
)
2002 (emit-constant counts
)))))
2004 (declare (type index num
))
2005 (do-ir2-blocks (2block component
)
2006 (let ((block (ir2-block-block 2block
)))
2007 (when (block-start block
)
2008 (setf (block-number block
) num
)
2010 (when *collect-dynamic-statistics
*
2011 (let ((first-node (block-start-node block
)))
2012 (unless (or (and (bind-p first-node
)
2013 (xep-p (bind-lambda first-node
)))
2015 (node-lvar first-node
))
2020 #!+sb-dyncount
*dynamic-counts-tn
* #!-sb-dyncount nil
2023 (let ((first-node (block-start-node block
)))
2024 (unless (or (and (bind-p first-node
)
2025 ;; Bind-nodes already have safepoints
2026 (eq (bind-lambda first-node
)
2027 (lambda-home (bind-lambda first-node
))))
2028 (and (valued-node-p first-node
)
2029 (node-lvar first-node
)
2031 (node-lvar first-node
))
2033 (when (and (rest (block-pred block
))
2035 (member (loop-kind (block-loop block
))
2036 '(:natural
:strange
))
2037 (eq block
(loop-head (block-loop block
)))
2038 (policy first-node
(< inhibit-safepoints
2)))
2039 (vop sb
!vm
::insert-safepoint first-node
2block
))))
2040 (ir2-convert-block block
)
2044 ;;; If necessary, emit a terminal unconditional branch to go to the
2045 ;;; successor block. If the successor is the component tail, then
2046 ;;; there isn't really any successor, but if the end is a non-tail
2047 ;;; call to a function that's not *known* to never return, then we
2048 ;;; emit an error trap just in case the function really does return.
2050 ;;; Trapping after known calls makes it easier to understand type
2051 ;;; derivation bugs at runtime: they show up as nil-fun-returned-error,
2052 ;;; rather than the execution of arbitrary code or error traps.
2053 (defun finish-ir2-block (block)
2054 (declare (type cblock block
))
2055 (let* ((2block (block-info block
))
2056 (last (block-last block
))
2057 (succ (block-succ block
)))
2059 (aver (singleton-p succ
))
2060 (let ((target (first succ
)))
2061 (cond ((eq target
(component-tail (block-component block
)))
2062 (when (and (basic-combination-p last
)
2063 (or (eq (basic-combination-kind last
) :full
)
2064 (and (eq (basic-combination-kind last
) :known
)
2065 (eq (basic-combination-info last
) :full
))))
2066 (let* ((fun (basic-combination-fun last
))
2067 (use (lvar-uses fun
))
2068 (name (and (ref-p use
)
2069 (leaf-has-source-name-p (ref-leaf use
))
2070 (leaf-source-name (ref-leaf use
))))
2071 (ftype (and (info :function
:info name
) ; only use the FTYPE if
2072 (info :function
:type name
)))) ; NAME was DEFKNOWN
2073 (unless (or (node-tail-p last
)
2074 (policy last
(zerop safety
))
2075 (and (fun-type-p ftype
)
2076 (eq *empty-type
* (fun-type-returns ftype
))))
2077 (vop nil-fun-returned-error last
2block
2079 (emit-constant name
)
2080 (multiple-value-bind (tn named
)
2081 (fun-lvar-tn last
2block fun
)
2084 ((not (eq (ir2-block-next 2block
) (block-info target
)))
2085 (vop branch last
2block
(block-label target
)))
2087 (register-drop-thru target
))))))
2091 ;;; Convert the code in a block into VOPs.
2092 (defun ir2-convert-block (block)
2093 (declare (type cblock block
))
2094 (let ((2block (block-info block
)))
2095 (do-nodes (node lvar block
)
2099 (let ((2lvar (lvar-info lvar
)))
2100 ;; function REF in a local call is not annotated
2101 (when (and 2lvar
(not (eq (ir2-lvar-kind 2lvar
) :delayed
)))
2102 (ir2-convert-ref node
2block
)))))
2104 (let ((kind (basic-combination-kind node
)))
2107 (ir2-convert-local-call node
2block
))
2109 (ir2-convert-full-call node
2block
))
2111 (let* ((info (basic-combination-fun-info node
))
2112 (fun (fun-info-ir2-convert info
)))
2114 (funcall fun node
2block
))
2115 ((eq (basic-combination-info node
) :full
)
2116 (ir2-convert-full-call node
2block
))
2118 (ir2-convert-template node
2block
))))))))
2120 (when (lvar-info (if-test node
))
2121 (ir2-convert-if node
2block
)))
2123 (let ((fun (bind-lambda node
)))
2124 (when (eq (lambda-home fun
) fun
)
2125 (ir2-convert-bind node
2block
))))
2127 (ir2-convert-return node
2block
))
2129 (ir2-convert-set node
2block
))
2131 (ir2-convert-cast node
2block
))
2134 ((eq (basic-combination-kind node
) :local
)
2135 (ir2-convert-mv-bind node
2block
))
2136 ((eq (lvar-fun-name (basic-combination-fun node
))
2138 (ir2-convert-throw node
2block
))
2140 (ir2-convert-mv-call node
2block
))))
2142 (when (exit-entry node
)
2143 (ir2-convert-exit node
2block
)))
2145 (ir2-convert-entry node
2block
)))))
2147 (finish-ir2-block block
)