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
))
20 (aver (neq (tn-kind x
) :unused
))
21 (aver (neq (tn-kind y
) :unused
))
23 (vop move node block x y
))
26 ;;; Determine whether we should emit a single-stepper breakpoint
27 ;;; around a call / before a vop.
28 (defun emit-step-p (node)
29 (if (and (policy node
(> insert-step-conditions
1))
30 (typep node
'combination
))
31 (combination-step-info node
)
34 ;;; Allocate an indirect value cell.
35 (defevent make-value-cell-event
"Allocate heap value cell for lexical var.")
36 (defun emit-make-value-cell (node block value res
)
37 (event make-value-cell-event node
)
38 (vop make-value-cell node block value nil res
))
42 ;;; Return the TN that holds the value of THING in the environment ENV.
43 (declaim (ftype (sfunction ((or nlx-info lambda-var clambda
) physenv
) tn
)
45 (defun find-in-physenv (thing physenv
)
46 (or (cdr (assoc thing
(ir2-physenv-closure (physenv-info physenv
))))
49 ;; I think that a failure of this assertion means that we're
50 ;; trying to access a variable which was improperly closed
51 ;; over. The PHYSENV describes a physical environment. Every
52 ;; variable that a form refers to should either be in its
53 ;; physical environment directly, or grabbed from a
54 ;; surrounding physical environment when it was closed over.
55 ;; The ASSOC expression above finds closed-over variables, so
56 ;; if we fell through the ASSOC expression, it wasn't closed
57 ;; over. Therefore, it must be in our physical environment
58 ;; directly. If instead it is in some other physical
59 ;; environment, then it's bogus for us to reference it here
60 ;; without it being closed over. -- WHN 2001-09-29
61 (aver (eq physenv
(lambda-physenv (lambda-var-home thing
))))
64 (aver (eq physenv
(block-physenv (nlx-info-target thing
))))
65 (ir2-nlx-info-home (nlx-info-info thing
)))
68 (entry-info-closure-tn (lambda-info thing
))))
69 (bug "~@<~2I~_~S ~_not found in ~_~S~:>" thing physenv
)))
71 ;;; If LEAF already has a constant TN, return that, otherwise make a
73 (defun constant-tn (leaf boxedp
)
74 (declare (type constant leaf
))
75 ;; When convenient we can have both a boxed and unboxed TN for
78 (or (constant-boxed-tn leaf
)
79 (setf (constant-boxed-tn leaf
) (make-constant-tn leaf t
)))
81 (setf (leaf-info leaf
) (make-constant-tn leaf nil
)))))
83 ;;; Return a TN that represents the value of LEAF, or NIL if LEAF
84 ;;; isn't directly represented by a TN. ENV is the environment that
85 ;;; the reference is done in.
86 (defun leaf-tn (leaf env boxedp
)
87 (declare (type leaf leaf
) (type physenv env
))
90 (unless (lambda-var-indirect leaf
)
91 (find-in-physenv leaf env
)))
92 (constant (constant-tn leaf boxedp
))
95 ;;; This is used to conveniently get a handle on a constant TN during
96 ;;; IR2 conversion. It returns a constant TN representing the Lisp
98 (defun emit-constant (value)
99 (constant-tn (find-constant value
) t
))
101 (defun boxed-combination-ref-p (combination lvar
)
102 (let ((args (combination-args combination
)))
103 (flet ((struct-slot-tagged-p (dd index
)
105 (find index
(dd-slots dd
) :key
#'dsd-index
))))
107 (eq (dsd-raw-type slot
) t
))))
109 (and (constant-lvar-p lvar
)
111 (case (combination-fun-source-name combination nil
)
112 (data-vector-set-with-offset
113 (and (eq lvar
(car (last args
)))
114 (csubtypep (lvar-type (car args
))
115 (specifier-type '(array t
)))))
117 (csubtypep (lvar-type (car args
))
118 (specifier-type '(array t
))))
119 (%make-structure-instance
120 (let* ((dd (constant (car args
)))
121 (slot-specs (constant (cadr args
)))
122 (pos (position lvar
(cddr args
)))
124 (nth pos slot-specs
))))
125 (struct-slot-tagged-p dd
(cddr slot
))))
127 (let* ((instance (lvar-type (car args
)))
128 (layout (and (structure-classoid-p instance
)
129 (classoid-layout instance
))))
130 (and (eq lvar
(car (last args
)))
131 (struct-slot-tagged-p (and layout
132 (layout-info layout
))
133 (constant (cadr args
))))))
134 ((%special-bind %set-sap-ref-lispobj
135 %rplaca %rplacd cons list list
*
139 (call-full-like-p combination
))))))
141 (defun boxed-ref-p (ref)
142 (let* ((lvar (ref-lvar ref
))
143 (dest (lvar-dest lvar
)))
144 (cond ((basic-combination-p dest
)
145 (if (combination-p dest
)
146 (boxed-combination-ref-p dest lvar
)
147 (call-full-like-p dest
)))
149 (global-var-p (set-var dest
)))
151 (let* ((fun (return-lambda dest
))
152 (returns (tail-set-info (lambda-tail-set fun
))))
154 (eq (return-info-kind returns
) :unknown
)))))))
156 ;;; Convert a REF node. The reference must not be delayed.
157 (defun ir2-convert-ref (node block
)
158 (declare (type ref node
) (type ir2-block block
))
159 (let* ((lvar (node-lvar node
))
160 (leaf (ref-leaf node
))
161 (locs (lvar-result-tns
162 lvar
(list (primitive-type (leaf-type leaf
)))))
166 (let ((tn (find-in-physenv leaf
(node-physenv node
)))
167 (indirect (lambda-var-indirect leaf
))
168 (explicit (lambda-var-explicit-value-cell leaf
)))
170 ((and indirect explicit
)
171 (vop value-cell-ref node block tn res
))
173 (not (eq (node-physenv node
)
174 (lambda-physenv (lambda-var-home leaf
)))))
175 (let ((reffer (third (primitive-type-indirect-cell-type
176 (primitive-type (leaf-type leaf
))))))
178 (funcall reffer node block tn
(leaf-info leaf
) res
)
179 (vop ancestor-frame-ref node block tn
(leaf-info leaf
) res
))))
180 (t (emit-move node block tn res
)))))
182 (emit-move node block
(constant-tn leaf
(boxed-ref-p node
)) res
))
184 (ir2-convert-closure node block leaf res
))
186 (ir2-convert-global-var node block leaf res
)))
187 (move-lvar-result node block locs lvar
))
190 (defun ir2-convert-global-var (node block leaf res
)
191 (let ((unsafe (policy node
(zerop safety
)))
192 (name (leaf-source-name leaf
)))
193 (ecase (global-var-kind leaf
)
195 (aver (symbolp name
))
196 (let ((name-tn (emit-constant name
)))
197 (if (or unsafe
(always-boundp name
))
198 (vop fast-symbol-value node block name-tn res
)
199 (vop symbol-value node block name-tn res
))))
201 (aver (symbolp name
))
202 (let ((name-tn (emit-constant name
)))
203 (if (or unsafe
(always-boundp name
))
204 (vop fast-symbol-global-value node block name-tn res
)
205 (vop symbol-global-value node block name-tn res
))))
207 ;; In cross-compilation, testing (INFO :function :definition) is not
208 ;; sensible (or possible) but we can assume that things with fun-info
209 ;; will eventually be defined. If that's untrue, e.g. if we referred
210 ;; to #'DESCRIBE during cold-load, we'd just fix it locally by declaring
211 ;; DESCRIBE notinline.
212 ;; But in the target, more caution is warranted because users might
213 ;; DEFKNOWN a function but fail to define it. And they shouldn't be
214 ;; expected to understand the failure mode and the remedy.
215 (cond ((and #-sb-xc-host
(info :function
:definition name
)
216 (info :function
:info name
)
217 (let ((*lexenv
* (node-lexenv node
)))
218 (not (fun-lexically-notinline-p name
))))
219 ;; Known functions can be dumped without going through fdefns.
220 ;; But if NOTINLINEd, don't early-bind to the functional value
221 ;; because that disallows redefinition, including but not limited
222 ;; to encapsulations, which in turn makes TRACE not work, which
223 ;; leads to extreme frustration when debugging.
224 (emit-move node block
(make-load-time-constant-tn :known-fun name
)
227 (let ((fdefn-tn (make-load-time-constant-tn :fdefinition name
)))
229 (vop fdefn-fun node block fdefn-tn res
)
230 (vop safe-fdefn-fun node block fdefn-tn res
)))))))))
232 ;;; some sanity checks for a CLAMBDA passed to IR2-CONVERT-CLOSURE
233 (defun assertions-on-ir2-converted-clambda (clambda)
234 ;; This assertion was sort of an experiment. It would be nice and
235 ;; sane and easier to understand things if it were *always* true,
236 ;; but experimentally I observe that it's only *almost* always
237 ;; true. -- WHN 2001-01-02
239 (aver (eql (lambda-component clambda
)
240 (block-component (ir2-block-block ir2-block
))))
241 ;; Check for some weirdness which came up in bug
244 ;; The MAKE-LOAD-TIME-CONSTANT-TN call above puts an :ENTRY record
245 ;; into the IR2-COMPONENT-CONSTANTS table. The dump-a-COMPONENT
247 ;; * treats every HANDLEless :ENTRY record into a
249 ;; * expects every patch to correspond to an
250 ;; IR2-COMPONENT-ENTRIES record.
251 ;; The IR2-COMPONENT-ENTRIES records are set by ENTRY-ANALYZE
252 ;; walking over COMPONENT-LAMBDAS. Bug 138b arose because there
253 ;; was a HANDLEless :ENTRY record which didn't correspond to an
254 ;; IR2-COMPONENT-ENTRIES record. That problem is hard to debug
255 ;; when it's caught at dump time, so this assertion tries to catch
257 (aver (member clambda
258 (component-lambdas (lambda-component clambda
))))
259 ;; another bug-138-related issue: COMPONENT-NEW-FUNCTIONALS is
260 ;; used as a queue for stuff pending to do in IR1, and now that
261 ;; we're doing IR2 it should've been completely flushed (but
263 (aver (null (component-new-functionals (lambda-component clambda
))))
266 ;;; Emit code to load a function object implementing FUNCTIONAL into
267 ;;; RES. This gets interesting when the referenced function is a
268 ;;; closure: we must make the closure and move the closed-over values
271 ;;; FUNCTIONAL is either a :TOPLEVEL-XEP functional or the XEP lambda
272 ;;; for the called function, since local call analysis converts all
273 ;;; closure references. If a :TOPLEVEL-XEP, we know it is not a
276 ;;; If a closed-over LAMBDA-VAR has no refs (is deleted), then we
277 ;;; don't initialize that slot. This can happen with closures over
278 ;;; top level variables, where optimization of the closure deleted the
279 ;;; variable. Since we committed to the closure format when we
280 ;;; pre-analyzed the top level code, we just leave an empty slot.
281 (defun ir2-convert-closure (ref ir2-block functional res
)
282 (declare (type ref ref
)
283 (type ir2-block ir2-block
)
284 (type functional functional
)
287 (aver (not (eql (functional-kind functional
) :deleted
)))
288 (unless (leaf-info functional
)
289 (setf (leaf-info functional
)
290 (make-entry-info :name
291 (functional-debug-name functional
))))))
292 (let ((closure (etypecase functional
294 (assertions-on-ir2-converted-clambda functional
)
295 (physenv-closure (get-lambda-physenv functional
)))
297 (aver (eq (functional-kind functional
) :toplevel-xep
))
302 (let* ((physenv (node-physenv ref
))
303 (tn (find-in-physenv functional physenv
)))
304 (emit-move ref ir2-block tn res
)))
305 ;; we're about to emit a reference to a "closure" that's actually
306 ;; an inlinable global function.
307 ((and (global-var-p (setf global-var
308 (functional-inline-expanded functional
)))
309 (eq :global-function
(global-var-kind global-var
)))
310 (ir2-convert-global-var ref ir2-block global-var res
))
312 ;; if we're here, we should have either a toplevel-xep (some
313 ;; global scope function in a different component) or an external
314 ;; reference to the "closure"'s body.
316 (aver (memq (functional-kind functional
) '(:external
:toplevel-xep
)))
317 (let ((entry (make-load-time-constant-tn :entry functional
)))
318 (emit-move ref ir2-block entry res
))))))
321 (defun closure-initial-value (what this-env current-fp
)
322 (declare (type (or nlx-info lambda-var clambda
) what
)
323 (type physenv this-env
)
324 (type (or tn null
) current-fp
))
325 ;; If we have an indirect LAMBDA-VAR that does not require an
326 ;; EXPLICIT-VALUE-CELL, and is from this environment (not from being
327 ;; closed over), we need to store the current frame pointer.
328 (if (and (lambda-var-p what
)
329 (lambda-var-indirect what
)
330 (not (lambda-var-explicit-value-cell what
))
331 (eq (lambda-physenv (lambda-var-home what
))
334 (find-in-physenv what this-env
)))
336 (defoptimizer (%allocate-closures ltn-annotate
) ((leaves) node ltn-policy
)
337 (declare (ignore ltn-policy
))
338 (when (lvar-dynamic-extent leaves
)
339 (let ((info (make-ir2-lvar *backend-t-primitive-type
*)))
340 (setf (ir2-lvar-kind info
) :delayed
)
341 (setf (lvar-info leaves
) info
)
342 (setf (ir2-lvar-stack-pointer info
)
343 (make-stack-pointer-tn)))))
345 (defoptimizer (%allocate-closures ir2-convert
) ((leaves) call
2block
)
346 (let ((dx-p (lvar-dynamic-extent leaves
)))
349 (vop current-stack-pointer call
2block
350 (ir2-lvar-stack-pointer (lvar-info leaves
))))
351 (dolist (leaf (lvar-value leaves
))
352 (binding* ((xep (awhen (functional-entry-fun leaf
)
353 ;; if the xep's been deleted then we can skip it
354 (if (eq (functional-kind it
) :deleted
)
357 (nil (aver (xep-p xep
)))
358 (entry-info (lambda-info xep
) :exit-if-null
)
359 (tn (entry-info-closure-tn entry-info
) :exit-if-null
)
360 (closure (physenv-closure (get-lambda-physenv xep
)))
362 (entry (make-load-time-constant-tn :entry xep
)))
363 (let ((this-env (node-physenv call
))
364 (leaf-dx-p (and dx-p
(leaf-dynamic-extent leaf
))))
365 (aver (entry-info-offset entry-info
))
366 (vop make-closure call
2block
#!-x86-64 entry
367 (entry-info-offset entry-info
) (length closure
)
369 (loop for what in closure and n from
0 do
370 (unless (and (lambda-var-p what
)
371 (null (leaf-refs what
)))
372 ;; In LABELS a closure may refer to another closure
373 ;; in the same group, so we must be sure that we
374 ;; store a closure only after its creation.
376 ;; TODO: Here is a simple solution: we postpone
377 ;; putting of all closures after all creations
378 ;; (though it may require more registers).
380 (delayed (list tn
(find-in-physenv what this-env
) n
))
381 (let ((initial-value (closure-initial-value
384 (vop closure-init call
2block
386 ;; An initial-value of NIL means to stash
387 ;; the frame pointer... which requires a
389 (vop closure-init-from-fp call
2block tn n
)))))))))
390 (loop for
(tn what n
) in
(delayed)
391 do
(vop closure-init call
2block
395 ;;; Convert a SET node. If the NODE's LVAR is annotated, then we also
396 ;;; deliver the value to that lvar. If the var is a lexical variable
397 ;;; with no refs, then we don't actually set anything, since the
398 ;;; variable has been deleted.
399 (defun ir2-convert-set (node block
)
400 (declare (type cset node
) (type ir2-block block
))
401 (let* ((lvar (node-lvar node
))
402 (leaf (set-var node
))
403 (val (lvar-tn node block
(set-value node
)))
406 lvar
(list (primitive-type (leaf-type leaf
))))
410 (when (leaf-refs leaf
)
411 (let ((tn (find-in-physenv leaf
(node-physenv node
)))
412 (indirect (lambda-var-indirect leaf
))
413 (explicit (lambda-var-explicit-value-cell leaf
)))
415 ((and indirect explicit
)
416 (vop value-cell-set node block tn val
))
418 (not (eq (node-physenv node
)
419 (lambda-physenv (lambda-var-home leaf
)))))
420 (let ((setter (fourth (primitive-type-indirect-cell-type
421 (primitive-type (leaf-type leaf
))))))
423 (funcall setter node block tn val
(leaf-info leaf
))
424 (vop ancestor-frame-set node block tn val
(leaf-info leaf
)))))
425 (t (emit-move node block val tn
))))))
427 (aver (symbolp (leaf-source-name leaf
)))
428 (ecase (global-var-kind leaf
)
430 (vop set node block
(emit-constant (leaf-source-name leaf
)) val
))
432 (vop %set-symbol-global-value node
433 block
(emit-constant (leaf-source-name leaf
)) val
)))))
435 (emit-move node block val
(first locs
))
436 (move-lvar-result node block locs lvar
)))
439 ;;;; utilities for receiving fixed values
441 ;;; Return a TN that can be referenced to get the value of LVAR. LVAR
442 ;;; must be LTN-ANNOTATED either as a delayed leaf ref or as a fixed,
443 ;;; single-value lvar.
445 ;;; The primitive-type of the result will always be the same as the
446 ;;; IR2-LVAR-PRIMITIVE-TYPE, ensuring that VOPs are always called with
447 ;;; TNs that satisfy the operand primitive-type restriction. We may
448 ;;; have to make a temporary of the desired type and move the actual
449 ;;; lvar TN into it. This happens when we delete a type check in
450 ;;; unsafe code or when we locally know something about the type of an
451 ;;; argument variable.
452 (defun lvar-tn (node block lvar
)
453 (declare (type node node
) (type ir2-block block
) (type lvar lvar
))
454 (let* ((2lvar (lvar-info lvar
))
456 (ecase (ir2-lvar-kind 2lvar
)
458 (let ((ref (lvar-uses lvar
)))
459 (leaf-tn (ref-leaf ref
) (node-physenv ref
) (boxed-ref-p ref
))))
461 (aver (= (length (ir2-lvar-locs 2lvar
)) 1))
462 (first (ir2-lvar-locs 2lvar
)))))
463 (ptype (ir2-lvar-primitive-type 2lvar
)))
465 (cond ((eq (tn-primitive-type lvar-tn
) ptype
) lvar-tn
)
467 (let ((temp (make-normal-tn ptype
)))
468 (emit-move node block lvar-tn temp
)
471 ;;; This is similar to LVAR-TN, but hacks multiple values. We return
472 ;;; TNs holding the values of LVAR with PTYPES as their primitive
473 ;;; types. LVAR must be annotated for the same number of fixed values
474 ;;; are there are PTYPES.
476 ;;; If the lvar has a type check, check the values into temps and
477 ;;; return the temps. When we have more values than assertions, we
478 ;;; move the extra values with no check.
479 (defun lvar-tns (node block lvar ptypes
)
480 (declare (type node node
) (type ir2-block block
)
481 (type lvar lvar
) (list ptypes
))
482 (let* ((locs (ir2-lvar-locs (lvar-info lvar
)))
483 (nlocs (length locs
)))
484 (aver (= nlocs
(length ptypes
)))
486 (mapcar (lambda (from to-type
)
487 (if (or (eq (tn-kind from
) :unused
)
488 (eq (tn-primitive-type from
) to-type
))
490 (let ((temp (make-normal-tn to-type
)))
491 (emit-move node block from temp
)
496 ;;;; utilities for delivering values to lvars
498 ;;; Return a list of TNs with the specifier TYPES that can be used as
499 ;;; result TNs to evaluate an expression into LVAR. This is used
500 ;;; together with MOVE-LVAR-RESULT to deliver fixed values to
503 ;;; If the lvar isn't annotated (meaning the values are discarded) or
504 ;;; is unknown-values, then we make temporaries for each supplied
505 ;;; value, providing a place to compute the result in until we decide
506 ;;; what to do with it (if anything.)
508 ;;; If the lvar is fixed-values, and wants the same number of values
509 ;;; as the user wants to deliver, then we just return the
510 ;;; IR2-LVAR-LOCS. Otherwise we make a new list padded as necessary by
511 ;;; discarded TNs. We always return a TN of the specified type, using
512 ;;; the lvar locs only when they are of the correct type.
513 (defun lvar-result-tns (lvar types
)
514 (declare (type (or lvar null
) lvar
) (type list types
))
516 (mapcar #'make-normal-tn types
)
517 (let ((2lvar (lvar-info lvar
)))
518 (ecase (ir2-lvar-kind 2lvar
)
520 (let* ((locs (ir2-lvar-locs 2lvar
))
521 (nlocs (length locs
))
522 (ntypes (length types
)))
523 (if (and (= nlocs ntypes
)
524 (do ((loc locs
(cdr loc
))
525 (type types
(cdr type
)))
527 (unless (eq (tn-primitive-type (car loc
)) (car type
))
530 (mapcar (lambda (loc type
)
531 (if (eq (tn-primitive-type loc
) type
)
533 (make-normal-tn type
)))
536 (mapcar #'make-normal-tn
537 (subseq types nlocs
)))
541 (mapcar #'make-normal-tn types
))))))
543 ;;; Make the first N standard value TNs, returning them in a list.
544 (defun make-standard-value-tns (n)
545 (declare (type unsigned-byte n
))
548 (res (standard-arg-location i
)))
551 ;;; Return a list of TNs wired to the standard value passing
552 ;;; conventions that can be used to receive values according to the
553 ;;; unknown-values convention. This is used together with
554 ;;; MOVE-LVAR-RESULT for delivering unknown values to a fixed values
557 ;;; If the lvar isn't annotated, then we treat as 0-values, returning
558 ;;; an empty list of temporaries.
560 ;;; If the lvar is annotated, then it must be :FIXED.
561 (defun standard-result-tns (lvar)
562 (declare (type (or lvar null
) lvar
))
564 (let ((2lvar (lvar-info lvar
)))
565 (ecase (ir2-lvar-kind 2lvar
)
567 (make-standard-value-tns (length (ir2-lvar-locs 2lvar
))))))
570 ;;; Just move each SRC TN into the corresponding DEST TN, defaulting
571 ;;; any unsupplied source values to NIL. We let EMIT-MOVE worry about
572 ;;; doing the appropriate coercions.
573 (defun move-results-coerced (node block src dest
)
574 (declare (type node node
) (type ir2-block block
) (list src dest
))
575 (let ((nsrc (length src
))
576 (ndest (length dest
)))
577 (mapc (lambda (from to
)
578 (unless (or (eq from to
)
579 (eq (tn-kind to
) :unused
))
580 (emit-move node block from to
)))
582 (append src
(make-list (- ndest nsrc
)
583 :initial-element
(emit-constant nil
)))
588 ;;; If necessary, emit coercion code needed to deliver the RESULTS to
589 ;;; the specified lvar. NODE and BLOCK provide context for emitting
590 ;;; code. Although usually obtained from STANDARD-RESULT-TNs or
591 ;;; LVAR-RESULT-TNs, RESULTS may be a list of any type or
594 ;;; If the lvar is fixed values, then move the results into the lvar
595 ;;; locations. If the lvar is unknown values, then do the moves into
596 ;;; the standard value locations, and use PUSH-VALUES to put the
597 ;;; values on the stack.
598 (defun move-lvar-result (node block results lvar
)
599 (declare (type node node
) (type ir2-block block
)
600 (list results
) (type (or lvar null
) lvar
))
602 (let ((2lvar (lvar-info lvar
)))
603 ;; If LVAR flows through a CAST which is unused it won't get
604 ;; deleted and won't be annotated
606 (ecase (ir2-lvar-kind 2lvar
)
608 (let ((locs (ir2-lvar-locs 2lvar
)))
609 (unless (eq locs results
)
610 (move-results-coerced node block results locs
))))
612 (let* ((nvals (length results
))
613 (locs (make-standard-value-tns nvals
)))
614 (move-results-coerced node block results locs
)
615 (vop* push-values node block
616 ((reference-tn-list locs nil
))
617 ((reference-tn-list (ir2-lvar-locs 2lvar
) t
))
621 ;;; Doing this during IR2 conversion and not in ir1-optimize-cast
622 ;;; because of possible function redefinition and the need to signal a
623 ;;; style-warning and not a full warning.
624 ;;; If the cast is not deleted after a warning is signalled then it
625 ;;; might get signalled multiple times, and IR2 conversion happens
627 (defun check-functional-cast (cast)
628 (let ((value (cast-value cast
))
629 (atype (cast-asserted-type cast
)))
630 (cond ((function-designator-cast-p cast
)
631 (multiple-value-bind (type name leaf
) (lvar-fun-type value
)
632 (when (and (fun-type-p type
)
634 (let ((*valid-fun-use-name
* (function-designator-cast-caller cast
))
635 (*lossage-fun
* (callable-argument-lossage-kind name
637 #'compiler-style-warn
639 (*compiler-error-context
* cast
)
640 (dest (lvar-dest (cast-lvar cast
))))
641 (if (and (combination-p dest
)
642 (eq (lvar-fun-name (combination-fun dest
) t
)
643 *valid-fun-use-name
*))
644 (map-callable-arguments (lambda (lvar &rest args
)
645 (when (eq lvar
(cast-lvar cast
))
646 (apply #'valid-callable-argument value args
)))
648 ;; Coming from CALLABLE-CAST
649 (valid-callable-argument value
651 (function-designator-cast-arg-count cast
)))))))
653 (multiple-value-bind (type name leaf
) (lvar-fun-type value
)
654 (when (and (fun-type-p type
)
656 (let ((int (type-intersection type atype
)))
657 (when (or (memq *empty-type
* (fun-type-required int
))
658 (and (eq (fun-type-returns int
) *empty-type
*)
659 (neq (fun-type-returns type
) *empty-type
*)
660 (not (and (eq (fun-type-returns atype
) *empty-type
*)
661 (eq (fun-type-returns type
) *wild-type
*)))))
662 (%compile-time-type-error-warn cast
663 (type-specifier atype
)
664 (type-specifier type
)
667 (callable-argument-lossage-kind name
670 'type-warning
))))))))))
673 (defun ir2-convert-cast (node block
)
674 (declare (type cast node
)
675 (type ir2-block block
))
676 (binding* ((lvar (node-lvar node
) :exit-if-null
)
677 (2lvar (lvar-info lvar
))
678 (value (cast-value node
))
679 (2value (lvar-info value
)))
680 (check-functional-cast node
)
681 (when 2lvar
;; the cast can be unused but not deleted to due vestigial exits
682 (ecase (ir2-lvar-kind 2lvar
)
685 (aver (not (cast-type-check node
)))
686 (move-results-coerced node block
687 (ir2-lvar-locs 2value
)
688 (ir2-lvar-locs 2lvar
)))))))
690 (defoptimizer (%check-bound ir2-hook
) ((array bound index
) node block
)
691 (declare (ignore block
))
692 (when (constant-lvar-p bound
)
693 (let* ((bound-type (specifier-type `(integer 0 (,(lvar-value bound
)))))
694 (index-type (lvar-type index
)))
695 (when (eq (type-intersection bound-type index-type
)
697 (let ((*compiler-error-context
* node
))
698 (compiler-warn "Derived type ~s is not a suitable index for ~s."
699 (type-specifier index-type
)
700 (type-specifier (lvar-type array
))))))))
702 ;;;; template conversion
704 ;;; Build a TN-REFS list that represents access to the values of the
705 ;;; specified list of lvars ARGS for TEMPLATE. Any :CONSTANT arguments
706 ;;; are returned in the second value as a list rather than being
707 ;;; accessed as a normal argument. NODE and BLOCK provide the context
708 ;;; for emitting any necessary type-checking code.
709 (defun reference-args (node block args template
)
710 (declare (type node node
) (type ir2-block block
) (list args
)
711 (type template template
))
712 (collect ((info-args))
715 (do ((args args
(cdr args
))
716 (types (template-arg-types template
) (cdr types
)))
718 (let ((type (first types
))
720 (if (and (consp type
) (eq (car type
) ':constant
))
721 (info-args (lvar-value arg
))
722 (let ((ref (reference-tn (lvar-tn node block arg
) nil
)))
724 (setf (tn-ref-across last
) ref
)
728 (values (the (or tn-ref null
) first
) (info-args)))))
730 ;;; Convert a conditional template. We try to exploit any
731 ;;; drop-through, but emit an unconditional branch afterward if we
732 ;;; fail. NOT-P is true if the sense of the TEMPLATE's test should be
734 (defun ir2-convert-conditional (node block template args info-args if not-p
)
735 (declare (type node node
) (type ir2-block block
)
736 (type template template
) (type (or tn-ref null
) args
)
737 (list info-args
) (type cif if
) (type boolean not-p
))
738 (let ((consequent (if-consequent if
))
739 (alternative (if-alternative if
))
740 (flags (and (consp (template-result-types template
))
741 (rest (template-result-types template
)))))
742 (aver (= (template-info-arg-count template
)
743 (+ (length info-args
)
746 (rotatef consequent alternative
)
748 (when (drop-thru-p if consequent
)
749 (rotatef consequent alternative
)
752 (emit-template node block template args nil
753 (list* (block-label consequent
) not-p
755 (if (drop-thru-p if alternative
)
756 (register-drop-thru alternative
)
757 (vop branch node block
(block-label alternative
))))
759 (emit-template node block template args nil info-args
)
760 (vop branch-if node block
(block-label consequent
) flags not-p
)
761 (if (drop-thru-p if alternative
)
762 (register-drop-thru alternative
)
763 (vop branch node block
(block-label alternative
)))))))
765 ;;; Convert an IF that isn't the DEST of a conditional template.
766 (defun ir2-convert-if (node block
)
767 (declare (type ir2-block block
) (type cif node
))
768 (let* ((test (if-test node
))
769 (test-ref (reference-tn (lvar-tn node block test
) nil
))
770 (nil-ref (reference-tn (emit-constant nil
) nil
)))
771 (setf (tn-ref-across test-ref
) nil-ref
)
772 (ir2-convert-conditional node block
(template-or-lose 'if-eq
)
773 test-ref
() node t
)))
775 ;;; Return a list of primitive-types that we can pass to LVAR-RESULT-TNS
776 ;;; describing the result types we want for a template call. We are really
777 ;;; only interested in the number of results required: in normal case
778 ;;; TEMPLATE-RESULTS-OK has already checked them.
779 (defun find-template-result-types (call rtypes
)
780 (let* ((type (node-derived-type call
))
782 (mapcar #'primitive-type
783 (if (args-type-p type
)
784 (append (args-type-required type
)
785 (args-type-optional type
))
787 (primitive-t *backend-t-primitive-type
*))
788 (mapcar (lambda (rtype)
789 (declare (ignore rtype
))
790 (or (pop types
) primitive-t
)) rtypes
)))
792 ;;; Return a list of TNs usable in a CALL to TEMPLATE delivering values to
793 ;;; LVAR. As an efficiency hack, we pick off the common case where the LVAR is
794 ;;; fixed values and has locations that satisfy the result restrictions. This
795 ;;; can fail when there is a type check or a values count mismatch.
796 (defun make-template-result-tns (call lvar rtypes
)
797 (declare (type combination call
) (type (or lvar null
) lvar
)
799 (let ((2lvar (when lvar
(lvar-info lvar
))))
800 (if (and 2lvar
(eq (ir2-lvar-kind 2lvar
) :fixed
))
801 (let ((locs (ir2-lvar-locs 2lvar
)))
802 (if (and (= (length rtypes
) (length locs
))
803 (do ((loc locs
(cdr loc
))
804 (rtypes rtypes
(cdr rtypes
)))
806 (unless (and (neq (tn-kind (car loc
)) :unused
)
807 (operand-restriction-ok
809 (tn-primitive-type (car loc
))
815 (find-template-result-types call rtypes
))))
818 (find-template-result-types call rtypes
)))))
820 ;;; Get the operands into TNs, make TN-REFs for them, and then call
821 ;;; the template emit function.
822 (defun ir2-convert-template (call block
)
823 (declare (type combination call
) (type ir2-block block
))
824 (let* ((template (combination-info call
))
825 (lvar (node-lvar call
))
826 (rtypes (template-result-types template
)))
827 (multiple-value-bind (args info-args
)
828 (reference-args call block
(combination-args call
) template
)
829 (aver (not (template-more-results-type template
)))
830 (if (template-conditional-p template
)
831 (ir2-convert-conditional call block template args info-args
832 (lvar-dest lvar
) nil
)
833 (let* ((results (make-template-result-tns call lvar rtypes
))
834 (r-refs (reference-tn-list results t
)))
835 (aver (= (length info-args
)
836 (template-info-arg-count template
)))
837 (when (and lvar
(lvar-dynamic-extent lvar
))
838 (vop current-stack-pointer call block
839 (ir2-lvar-stack-pointer (lvar-info lvar
))))
840 (when (emit-step-p call
)
841 (vop sb
!vm
::step-instrument-before-vop call block
))
843 (emit-template call block template args r-refs info-args
)
844 (emit-template call block template args r-refs
))
845 (move-lvar-result call block results lvar
)))))
848 ;;; We don't have to do much because operand count checking is done by
849 ;;; IR1 conversion. The only difference between this and the function
850 ;;; case of IR2-CONVERT-TEMPLATE is that there can be codegen-info
852 (defoptimizer (%%primitive ir2-convert
) ((template info
&rest args
) call block
)
853 (declare (ignore args
))
854 (let* ((template (lvar-value template
))
855 (info (lvar-value info
))
856 (lvar (node-lvar call
))
857 (rtypes (template-result-types template
))
858 (results (make-template-result-tns call lvar rtypes
))
859 (r-refs (reference-tn-list results t
)))
860 (multiple-value-bind (args info-args
)
861 (reference-args call block
(cddr (combination-args call
)) template
)
862 (aver (not (template-more-results-type template
)))
863 (aver (not (template-conditional-p template
)))
864 (aver (null info-args
))
867 (emit-template call block template args r-refs info
)
868 (emit-template call block template args r-refs
))
870 (move-lvar-result call block results lvar
)))
873 (defoptimizer (%%primitive derive-type
) ((template info
&rest args
))
874 (declare (ignore info args
))
875 (let ((type (template-type (lvar-value template
))))
876 (if (fun-type-p type
)
877 (fun-type-returns type
)
882 ;;; Convert a LET by moving the argument values into the variables.
883 ;;; Since a LET doesn't have any passing locations, we move the
884 ;;; arguments directly into the variables. We must also allocate any
885 ;;; indirect value cells, since there is no function prologue to do
887 (defun ir2-convert-let (node block fun
)
888 (declare (type combination node
) (type ir2-block block
) (type clambda fun
))
889 (mapc (lambda (var arg
)
891 (let ((src (lvar-tn node block arg
))
892 (dest (leaf-info var
)))
893 (if (and (lambda-var-indirect var
)
894 (lambda-var-explicit-value-cell var
))
895 (emit-make-value-cell node block src dest
)
896 (emit-move node block src dest
)))))
897 (lambda-vars fun
) (basic-combination-args node
))
900 ;;; Emit any necessary moves into assignment temps for a local call to
901 ;;; FUN. We return two lists of TNs: TNs holding the actual argument
902 ;;; values, and (possibly EQ) TNs that are the actual destination of
903 ;;; the arguments. When necessary, we allocate temporaries for
904 ;;; arguments to preserve parallel assignment semantics. These lists
905 ;;; exclude unused arguments and include implicit environment
906 ;;; arguments, i.e. they exactly correspond to the arguments passed.
908 ;;; OLD-FP is the TN currently holding the value we want to pass as
909 ;;; OLD-FP. If null, then the call is to the same environment (an
910 ;;; :ASSIGNMENT), so we only move the arguments, and leave the
911 ;;; environment alone.
913 ;;; CLOSURE-FP is for calling a closure that has "implicit" value
914 ;;; cells (stored in the allocating stack frame), and is the frame
915 ;;; pointer TN to use for values allocated in the outbound stack
916 ;;; frame. This is distinct from OLD-FP for the specific case of a
918 (defun emit-psetq-moves (node block fun old-fp
&optional
(closure-fp old-fp
))
919 (declare (type combination node
) (type ir2-block block
) (type clambda fun
)
920 (type (or tn null
) old-fp closure-fp
))
921 (let ((actuals (mapcar (lambda (x)
923 (lvar-tn node block x
)))
924 (combination-args node
))))
927 (dolist (var (lambda-vars fun
))
928 (let ((actual (pop actuals
))
929 (loc (leaf-info var
)))
932 ((and (lambda-var-indirect var
)
933 (lambda-var-explicit-value-cell var
))
935 (make-normal-tn *backend-t-primitive-type
*)))
936 (emit-make-value-cell node block actual temp
)
938 ((member actual
(locs))
939 (let ((temp (make-normal-tn (tn-primitive-type loc
))))
940 (emit-move node block actual temp
)
947 (let ((this-1env (node-physenv node
))
948 (called-env (physenv-info (lambda-physenv fun
))))
949 (dolist (thing (ir2-physenv-closure called-env
))
950 (temps (closure-initial-value (car thing
) this-1env closure-fp
))
953 (locs (ir2-physenv-old-fp called-env
))))
955 (values (temps) (locs)))))
957 ;;; A tail-recursive local call is done by emitting moves of stuff
958 ;;; into the appropriate passing locations. After setting up the args
959 ;;; and environment, we just move our return-pc into the called
960 ;;; function's passing location.
961 (defun ir2-convert-tail-local-call (node block fun
)
962 (declare (type combination node
) (type ir2-block block
) (type clambda fun
))
963 (let ((this-env (physenv-info (node-physenv node
)))
964 (current-fp (make-stack-pointer-tn)))
965 (multiple-value-bind (temps locs
)
966 (emit-psetq-moves node block fun
967 (ir2-physenv-old-fp this-env
) current-fp
)
969 ;; If we're about to emit a move from CURRENT-FP then we need to
971 (when (find current-fp temps
)
972 (vop current-fp node block current-fp
))
974 (mapc (lambda (temp loc
)
975 (emit-move node block temp loc
))
978 (emit-move node block
979 (ir2-physenv-return-pc this-env
)
980 (ir2-physenv-return-pc-pass
982 (lambda-physenv fun
)))))
986 ;;; Convert an :ASSIGNMENT call. This is just like a tail local call,
987 ;;; except that the caller and callee environment are the same, so we
988 ;;; don't need to mess with the environment locations, return PC, etc.
989 (defun ir2-convert-assignment (node block fun
)
990 (declare (type combination node
) (type ir2-block block
) (type clambda fun
))
991 (multiple-value-bind (temps locs
) (emit-psetq-moves node block fun nil
)
993 (mapc (lambda (temp loc
)
994 (emit-move node block temp loc
))
998 ;;; Do stuff to set up the arguments to a non-tail local call
999 ;;; (including implicit environment args.) We allocate a frame
1000 ;;; (returning the FP and NFP), and also compute the TN-REFS list for
1001 ;;; the values to pass and the list of passing location TNs.
1002 (defun ir2-convert-local-call-args (node block fun
)
1003 (declare (type combination node
) (type ir2-block block
) (type clambda fun
))
1004 (let ((fp (make-stack-pointer-tn))
1005 (nfp (make-number-stack-pointer-tn))
1006 (old-fp (make-stack-pointer-tn)))
1007 (multiple-value-bind (temps locs
)
1008 (emit-psetq-moves node block fun old-fp
)
1009 (vop current-fp node block old-fp
)
1010 (vop allocate-frame node block
1011 (physenv-info (lambda-physenv fun
))
1013 (values fp nfp temps
(mapcar #'make-alias-tn locs
)))))
1015 ;;; Handle a non-TR known-values local call. We emit the call, then
1016 ;;; move the results to the lvar's destination.
1017 (defun ir2-convert-local-known-call (node block fun returns lvar start
)
1018 (declare (type node node
) (type ir2-block block
) (type clambda fun
)
1019 (type return-info returns
) (type (or lvar null
) lvar
)
1021 (multiple-value-bind (fp nfp temps arg-locs
)
1022 (ir2-convert-local-call-args node block fun
)
1023 (let ((locs (return-info-locations returns
)))
1024 (vop* known-call-local node block
1025 (fp nfp
(reference-tn-list temps nil
))
1026 ((reference-tn-list locs t
))
1027 arg-locs
(physenv-info (lambda-physenv fun
)) start
)
1028 (move-lvar-result node block locs lvar
)))
1031 ;;; Handle a non-TR unknown-values local call. We do different things
1032 ;;; depending on what kind of values the lvar wants.
1034 ;;; If LVAR is :UNKNOWN, then we use the "multiple-" variant, directly
1035 ;;; specifying the lvar's LOCS as the VOP results so that we don't
1036 ;;; have to do anything after the call.
1038 ;;; Otherwise, we use STANDARD-RESULT-TNS to get wired result TNs, and
1039 ;;; then call MOVE-LVAR-RESULT to do any necessary type checks or
1041 (defun ir2-convert-local-unknown-call (node block fun lvar start
)
1042 (declare (type node node
) (type ir2-block block
) (type clambda fun
)
1043 (type (or lvar null
) lvar
) (type label start
))
1044 (multiple-value-bind (fp nfp temps arg-locs
)
1045 (ir2-convert-local-call-args node block fun
)
1046 (let ((2lvar (and lvar
(lvar-info lvar
)))
1047 (env (physenv-info (lambda-physenv fun
)))
1048 (temp-refs (reference-tn-list temps nil
)))
1049 (if (and 2lvar
(eq (ir2-lvar-kind 2lvar
) :unknown
))
1050 (vop* multiple-call-local node block
(fp nfp temp-refs
)
1051 ((reference-tn-list (ir2-lvar-locs 2lvar
) t
))
1053 (let ((locs (standard-result-tns lvar
)))
1054 (vop* call-local node block
1056 ((reference-tn-list locs t
))
1057 arg-locs env start
(length locs
))
1058 (move-lvar-result node block locs lvar
)))))
1061 ;;; Dispatch to the appropriate function, depending on whether we have
1062 ;;; a let, tail or normal call. If the function doesn't return, call
1063 ;;; it using the unknown-value convention. We could compile it as a
1064 ;;; tail call, but that might seem confusing in the debugger.
1065 (defun ir2-convert-local-call (node block
)
1066 (declare (type combination node
) (type ir2-block block
))
1067 (let* ((fun (ref-leaf (lvar-uses (basic-combination-fun node
))))
1068 (kind (functional-kind fun
)))
1069 (cond ((eq kind
:let
)
1070 (ir2-convert-let node block fun
))
1071 ((eq kind
:assignment
)
1072 (ir2-convert-assignment node block fun
))
1074 (ir2-convert-tail-local-call node block fun
))
1076 (let ((start (block-trampoline (lambda-block fun
)))
1077 (returns (tail-set-info (lambda-tail-set fun
)))
1078 (lvar (node-lvar node
)))
1080 (return-info-kind returns
)
1083 (ir2-convert-local-unknown-call node block fun lvar start
))
1085 (ir2-convert-local-known-call node block fun returns
1091 ;;; Given a function lvar FUN, return (VALUES TN-TO-CALL NAMED-P),
1092 ;;; where TN-TO-CALL is a TN holding the thing that we call NAMED-P is
1093 ;;; true if the thing is named (false if it is a function).
1095 ;;; There are two interesting non-named cases:
1096 ;;; -- We know it's a function. No check needed: return the
1098 ;;; -- We don't know what it is.
1099 (defun fun-lvar-tn (node block lvar
)
1100 (declare (ignore node block
))
1101 (declare (type lvar lvar
))
1102 (let ((2lvar (lvar-info lvar
)))
1103 (if (eq (ir2-lvar-kind 2lvar
) :delayed
)
1104 (let ((name (lvar-fun-name lvar t
)))
1106 (values (cond ((sb!vm
::static-fdefn-offset name
)
1109 ;; Named call to an immobile fdefn from an immobile component
1110 ;; uses the FUN-TN only to preserve liveness of the fdefn.
1111 ;; The name becomes an info arg.
1112 (make-load-time-constant-tn :fdefinition name
)))
1114 (let* ((locs (ir2-lvar-locs 2lvar
))
1116 (function-ptype (primitive-type-or-lose 'function
)))
1117 (aver (and (eq (ir2-lvar-kind 2lvar
) :fixed
)
1118 (= (length locs
) 1)))
1119 (aver (eq (tn-primitive-type loc
) function-ptype
))
1120 (values loc nil
)))))
1122 ;;; Set up the args to NODE in the current frame, and return a TN-REF
1123 ;;; list for the passing locations.
1124 (defun move-tail-full-call-args (node block
)
1125 (declare (type combination node
) (type ir2-block block
))
1126 (let ((args (basic-combination-args node
))
1129 (dotimes (num (length args
))
1130 (let ((loc (standard-arg-location num
)))
1131 (emit-move node block
(lvar-tn node block
(elt args num
)) loc
)
1132 (let ((ref (reference-tn loc nil
)))
1134 (setf (tn-ref-across last
) ref
)
1139 ;;; Move the arguments into the passing locations and do a (possibly
1140 ;;; named) tail call.
1141 (defun ir2-convert-tail-full-call (node block
)
1142 (declare (type combination node
) (type ir2-block block
))
1143 (let* ((env (physenv-info (node-physenv node
)))
1144 (args (basic-combination-args node
))
1145 (nargs (length args
))
1146 (pass-refs (move-tail-full-call-args node block
))
1147 (old-fp (ir2-physenv-old-fp env
))
1148 (return-pc (ir2-physenv-return-pc env
)))
1150 (multiple-value-bind (fun-tn named
)
1151 (fun-lvar-tn node block
(basic-combination-fun node
))
1153 (vop* tail-call node block
1154 (fun-tn old-fp return-pc pass-refs
)
1156 nargs
(emit-step-p node
)))
1159 (vop* static-tail-call-named node block
1160 (old-fp return-pc pass-refs
) ; args
1162 nargs named
(emit-step-p node
)))
1164 (vop* tail-call-named node block
1165 (#!-immobile-code fun-tn old-fp return-pc pass-refs
) ; args
1167 nargs
#!+immobile-code named
(emit-step-p node
)))))) ; info
1170 ;;; like IR2-CONVERT-LOCAL-CALL-ARGS, only different
1171 (defun ir2-convert-full-call-args (node block
)
1172 (declare (type combination node
) (type ir2-block block
))
1173 (let* ((args (basic-combination-args node
))
1174 (nargs (length args
))
1175 (fp (make-stack-pointer-tn nargs
)))
1176 (vop allocate-full-call-frame node block nargs fp
)
1180 (dotimes (num nargs
)
1181 (locs (standard-arg-location num
))
1182 (let ((ref (reference-tn (lvar-tn node block
(elt args num
))
1185 (setf (tn-ref-across last
) ref
)
1189 (values fp first
(locs) nargs
)))))
1191 ;;; Do full call when a fixed number of values are desired. We make
1192 ;;; STANDARD-RESULT-TNS for our lvar, then deliver the result using
1193 ;;; MOVE-LVAR-RESULT. We do named or normal call, as appropriate.
1194 (defun ir2-convert-fixed-full-call (node block
)
1195 (declare (type combination node
) (type ir2-block block
))
1196 (multiple-value-bind (fp args arg-locs nargs
)
1197 (ir2-convert-full-call-args node block
)
1198 (let* ((lvar (node-lvar node
))
1200 (loop for loc in
(ir2-lvar-locs (lvar-info lvar
))
1202 collect
(cond ((eql (tn-kind loc
) :unused
)
1204 #!+(or x86-64 arm64
) ;; needs default-unknown-values support
1205 ((>= i sb
!vm
::register-arg-count
)
1206 (make-normal-tn *backend-t-primitive-type
*))
1208 (standard-arg-location i
))))))
1209 (loc-refs (reference-tn-list locs t
))
1210 (nvals (length locs
)))
1211 (multiple-value-bind (fun-tn named
)
1212 (fun-lvar-tn node block
(basic-combination-fun node
))
1214 (vop* call node block
(fp fun-tn args
) (loc-refs)
1215 arg-locs nargs nvals
(emit-step-p node
)))
1218 (vop* static-call-named node block
1221 arg-locs nargs named nvals
1222 (emit-step-p node
)))
1224 (vop* call-named node block
1225 (fp #!-immobile-code fun-tn args
) ; args
1226 (loc-refs) ; results
1227 arg-locs nargs
#!+immobile-code named nvals
; info
1228 (emit-step-p node
))))
1229 (move-lvar-result node block locs lvar
))))
1232 ;;; Do full call when unknown values are desired.
1233 (defun ir2-convert-multiple-full-call (node block
)
1234 (declare (type combination node
) (type ir2-block block
))
1235 (multiple-value-bind (fp args arg-locs nargs
)
1236 (ir2-convert-full-call-args node block
)
1237 (let* ((lvar (node-lvar node
))
1238 (locs (ir2-lvar-locs (lvar-info lvar
)))
1239 (loc-refs (reference-tn-list locs t
)))
1240 (multiple-value-bind (fun-tn named
)
1241 (fun-lvar-tn node block
(basic-combination-fun node
))
1243 (vop* multiple-call node block
(fp fun-tn args
) (loc-refs)
1244 arg-locs nargs
(emit-step-p node
)))
1247 (vop* static-multiple-call-named node block
1250 arg-locs nargs named
1251 (emit-step-p node
)))
1253 (vop* multiple-call-named node block
1254 (fp #!-immobile-code fun-tn args
) ; args
1255 (loc-refs) ; results
1256 arg-locs nargs
#!+immobile-code named
; info
1257 (emit-step-p node
)))))))
1260 ;;; stuff to check in PONDER-FULL-CALL
1262 ;;; These came in handy when troubleshooting cold boot after making
1263 ;;; major changes in the package structure: various transforms and
1264 ;;; VOPs and stuff got attached to the wrong symbol, so that
1265 ;;; references to the right symbol were bogusly translated as full
1266 ;;; calls instead of primitives, sending the system off into infinite
1267 ;;; space. Having a report on all full calls generated makes it easier
1268 ;;; to figure out what form caused the problem this time.
1269 (declaim (type (member :minimal
:detailed
:very-detailed
:maximal
)
1270 *track-full-called-fnames
*))
1271 (defvar *track-full-called-fnames
* :minimal
)
1273 ;;; Do some checks (and store some notes relevant for future checks)
1275 ;;; * Is this a full call to something we have reason to know should
1276 ;;; never be full called? (Except as of sbcl-0.7.18 or so, we no
1277 ;;; longer try to ensure this behavior when *FAILURE-P* has already
1279 (defun ponder-full-call (node)
1280 (let* ((lvar (basic-combination-fun node
))
1281 (fname (lvar-fun-name lvar t
)))
1282 (declare (type (or symbol cons
) fname
))
1284 (when (and (symbolp fname
)
1285 (eq (symbol-package fname
) *cl-package
*))
1286 ;; Never produce a warning from (DECLARE (INLINE LENGTH)) etc
1287 (return-from ponder-full-call
))
1289 ;; Warn about cross-compiling certain full-calls,
1290 ;; as it is indicative of dependency order problems.
1292 (let ((compname (component-name (node-component node
))))
1293 ;; Don't care too much about macro performance.
1294 (unless (and (stringp compname
) (string/= compname
"DEFMACRO"))
1295 ;; Catch FOO and (SETF FOO) both.
1296 (let ((stem (if (atom fname
) fname
(second fname
))))
1298 sb-cold
::*full-calls-to-warn-about
*
1300 (warn "Full call to ~S" fname
)))))
1302 (let* ((inlineable-p (not (let ((*lexenv
* (node-lexenv node
)))
1303 (fun-lexically-notinline-p fname
))))
1304 (inlineable-bit (if inlineable-p
1 0))
1305 (cell (info :function
:emitted-full-calls fname
)))
1307 ;; The low bit indicates whether any not-NOTINLINE call was seen.
1308 ;; The next-lowest bit is magic. Refer to %COMPILER-DEFMACRO
1309 ;; and WARN-IF-INLINE-FAILED/CALL for the pertinent logic.
1310 (setf cell
(list (logior 4 inlineable-bit
))
1311 (info :function
:emitted-full-calls fname
) cell
)
1312 (incf (car cell
) (+ 4 (if (oddp (car cell
)) 0 inlineable-bit
))))
1313 ;; If the full call was wanted, don't record anything.
1314 ;; (This was originally for debugging SBCL self-compilation)
1317 (warn-if-inline-failed/call fname
(node-lexenv node
) cell
))
1318 (case *track-full-called-fnames
*
1320 (when (boundp 'sb
!xc
:*compile-file-pathname
*)
1321 (pushnew sb
!xc
:*compile-file-pathname
* (cdr cell
)
1324 (pushnew (component-name *component-being-compiled
*)
1325 (cdr cell
) :test
#'equalp
)))))
1327 ;; Special mode, usually only for the cross-compiler
1328 ;; and only with the feature enabled.
1329 #!+sb-show
(when (eq *track-full-called-fnames
* :maximal
)
1330 (/show
"converting full call to named function" fname
)
1331 (/show
(basic-combination-args node
))
1332 (/show
(policy node speed
) (policy node safety
))
1333 (/show
(policy node compilation-speed
))
1334 (let ((arg-types (mapcar (lambda (lvar)
1338 (basic-combination-args node
))))
1341 ;; When illegal code is compiled, all sorts of perverse paths
1342 ;; through the compiler can be taken, and it's much harder -- and
1343 ;; probably pointless -- to guarantee that always-optimized-away
1344 ;; functions are actually optimized away. Thus, we skip the check
1347 ;; check to see if we know anything about the function
1348 (let ((info (info :function
:info fname
)))
1349 ;; if we know something, check to see if the full call was valid
1350 (when (and info
(ir1-attributep (fun-info-attributes info
)
1351 always-translatable
))
1352 (/show
(policy node speed
) (policy node safety
))
1353 (/show
(policy node compilation-speed
))
1354 (bug "full call to ~S" fname
))))
1357 (aver (legal-fun-name-p fname
))))) ;; FIXME: needless check?
1359 ;;; If the call is in a tail recursive position and the return
1360 ;;; convention is standard, then do a tail full call. If one or fewer
1361 ;;; values are desired, then use a single-value call, otherwise use a
1362 ;;; multiple-values call.
1363 (defun ir2-convert-full-call (node block
)
1364 (declare (type combination node
) (type ir2-block block
))
1365 (ponder-full-call node
)
1366 (cond ((node-tail-p node
)
1367 (ir2-convert-tail-full-call node block
))
1368 ((let ((lvar (node-lvar node
)))
1370 (eq (ir2-lvar-kind (lvar-info lvar
)) :unknown
)))
1371 (ir2-convert-multiple-full-call node block
))
1373 (ir2-convert-fixed-full-call node block
)))
1376 ;;;; entering functions
1377 (defun xep-verify-arg-count (node block fun arg-count-location
)
1378 (when (policy fun
(plusp verify-arg-count
))
1379 (let* ((ef (functional-entry-fun fun
))
1380 (optional (optional-dispatch-p ef
))
1382 (optional-dispatch-min-args ef
)))
1383 (max (cond ((not optional
)
1384 (1- (length (lambda-vars fun
))))
1386 (not (optional-dispatch-more-entry ef
)))
1387 (optional-dispatch-max-args ef
)))))
1388 (unless (and (eql min
0) (not max
))
1389 (vop verify-arg-count node block
1395 ;;; Do all the stuff that needs to be done on XEP entry:
1396 ;;; -- Create frame.
1397 ;;; -- Copy any more arg.
1398 ;;; -- Set up the environment, accessing any closure variables.
1399 ;;; -- Move args from the standard passing locations to their internal
1401 (defun init-xep-environment (node block fun
)
1402 (declare (type bind node
) (type ir2-block block
) (type clambda fun
))
1403 (let ((start-label (entry-info-offset (leaf-info fun
)))
1404 (env (physenv-info (node-physenv node
)))
1406 (let ((ef (functional-entry-fun fun
)))
1407 (vop xep-allocate-frame node block start-label
)
1408 ;; Arg verification needs to be done before the stack pointer is adjusted
1409 ;; so that the extra arguments are still present when the error is signalled
1410 #!-precise-arg-count-error
1411 (vop xep-setup-sp node block
)
1412 (let ((verified (unless (eq (functional-kind fun
) :toplevel
)
1413 (setf arg-count-tn
(make-arg-count-location))
1414 (xep-verify-arg-count node block fun arg-count-tn
))))
1416 (declare (ignore verified
))
1417 (cond ((and (optional-dispatch-p ef
)
1418 (optional-dispatch-more-entry ef
)
1419 (neq (functional-kind (optional-dispatch-more-entry ef
)) :deleted
))
1420 ;; COPY-MORE-ARG does the job of XEP-SETUP-SP on +precise-arg-count-error
1421 (vop copy-more-arg node block
(optional-dispatch-max-args ef
)
1422 #!+x86-64 verified
))
1423 #!+precise-arg-count-error
1425 (vop xep-setup-sp node block
))))
1426 (when (ir2-physenv-closure env
)
1427 (let ((closure (make-normal-tn *backend-t-primitive-type
*)))
1428 (when (policy fun
(> store-closure-debug-pointer
1))
1429 ;; Save the closure pointer on the stack.
1430 (let ((closure-save (make-representation-tn
1431 *backend-t-primitive-type
*
1432 (sc-number-or-lose 'sb
!vm
::control-stack
))))
1433 (vop setup-closure-environment node block start-label
1435 (setf (ir2-physenv-closure-save-tn env
) closure-save
)
1436 (component-live-tn closure-save
)))
1437 (vop setup-closure-environment node block start-label closure
)
1439 (dolist (loc (ir2-physenv-closure env
))
1440 (vop closure-ref node block closure
(incf n
) (cdr loc
)))))))
1441 (unless (eq (functional-kind fun
) :toplevel
)
1442 (let ((vars (lambda-vars fun
))
1444 (when (leaf-refs (first vars
))
1445 (emit-move node block arg-count-tn
(leaf-info (first vars
))))
1446 (dolist (arg (rest vars
))
1447 (when (leaf-refs arg
)
1448 (let ((pass (standard-arg-location n
))
1449 (home (leaf-info arg
)))
1450 (if (and (lambda-var-indirect arg
)
1451 (lambda-var-explicit-value-cell arg
))
1452 (emit-make-value-cell node block pass home
)
1453 (emit-move node block pass home
))))
1456 (emit-move node block
(make-old-fp-passing-location t
)
1457 (ir2-physenv-old-fp env
)))
1461 ;;; Emit function prolog code. This is only called on bind nodes for
1462 ;;; functions that allocate environments. All semantics of let calls
1463 ;;; are handled by IR2-CONVERT-LET.
1465 ;;; If not an XEP, all we do is move the return PC from its passing
1466 ;;; location, since in a local call, the caller allocates the frame
1467 ;;; and sets up the arguments.
1469 #!+unwind-to-frame-and-call-vop
1470 (defun save-bsp (node block env
)
1471 ;; Save BSP on stack so that the binding environment can be restored
1472 ;; when restarting frames.
1473 ;; This is done inside functions, which leaves XEPs without saved
1474 ;; BSP, though the code in XEPs doesn't bind any variables, it can
1475 ;; call arbitrary code through the SATISFIES declaration.
1476 ;; And functions called by SATISFIES are not inlined, except for
1477 ;; source transforms, but these usually do not bind anything.
1478 ;; Thus when restarting it needs to check that the interrupt was in
1481 ;; It could be saved from the XEP, but some functions have both
1482 ;; external and internal entry points, so it will be saved twice.
1483 (let ((temp (make-normal-tn *backend-t-primitive-type
*))
1484 (bsp-save-tn (make-representation-tn
1485 *backend-t-primitive-type
*
1486 (sc-number-or-lose 'sb
!vm
::control-stack
))))
1487 (vop current-binding-pointer node block temp
)
1488 (emit-move node block temp bsp-save-tn
)
1489 (setf (ir2-physenv-bsp-save-tn env
) bsp-save-tn
)
1490 (component-live-tn bsp-save-tn
)))
1492 (defun ir2-convert-bind (node block
)
1493 (declare (type bind node
) (type ir2-block block
))
1494 (let* ((fun (bind-lambda node
))
1495 (env (physenv-info (lambda-physenv fun
))))
1496 (aver (member (functional-kind fun
)
1497 '(nil :external
:optional
:toplevel
:cleanup
)))
1500 (init-xep-environment node block fun
)
1502 (when *collect-dynamic-statistics
*
1503 (vop count-me node block
*dynamic-counts-tn
*
1504 (block-number (ir2-block-block block
)))))
1505 ((policy fun
(> store-closure-debug-pointer
1))
1506 ;; Propagate the location of the closure pointer from the
1507 ;; enclosing functions. (FIXME: Should make sure that this
1508 ;; handles closures inside closures correctly). [remark by JES]
1509 (let* ((entry-fun (lambda-entry-fun fun
)))
1511 (let ((2env (physenv-info (lambda-physenv fun
)))
1512 (entry-2env (physenv-info (lambda-physenv entry-fun
))))
1513 (setf (ir2-physenv-closure-save-tn 2env
)
1514 (ir2-physenv-closure-save-tn entry-2env
)))))))
1518 (ir2-physenv-return-pc-pass env
)
1519 (ir2-physenv-return-pc env
))
1520 #!+unwind-to-frame-and-call-vop
1521 (when (and (lambda-allow-instrumenting fun
)
1522 (not (lambda-inline-expanded fun
))
1523 (policy fun
(>= insert-debug-catch
1)))
1524 (save-bsp node block env
))
1526 (let ((lab (gen-label)))
1527 (setf (ir2-physenv-environment-start env
) lab
)
1528 (vop note-environment-start node block lab
)
1530 (unless (policy fun
(>= inhibit-safepoints
2))
1531 (vop sb
!vm
::insert-safepoint node block
))))
1535 ;;;; function return
1537 ;;; Do stuff to return from a function with the specified values and
1538 ;;; convention. If the return convention is :FIXED and we aren't
1539 ;;; returning from an XEP, then we do a known return (letting
1540 ;;; representation selection insert the correct move-arg VOPs.)
1541 ;;; Otherwise, we use the unknown-values convention. If there is a
1542 ;;; fixed number of return values, then use RETURN, otherwise use
1543 ;;; RETURN-MULTIPLE.
1544 (defun ir2-convert-return (node block
)
1545 (declare (type creturn node
) (type ir2-block block
))
1546 (let* ((lvar (return-result node
))
1547 (2lvar (lvar-info lvar
))
1548 (lvar-kind (ir2-lvar-kind 2lvar
))
1549 (fun (return-lambda node
))
1550 (env (physenv-info (lambda-physenv fun
)))
1551 (old-fp (ir2-physenv-old-fp env
))
1552 (return-pc (ir2-physenv-return-pc env
))
1553 (returns (tail-set-info (lambda-tail-set fun
))))
1555 ((and (eq (return-info-kind returns
) :fixed
)
1557 (let ((locs (lvar-tns node block lvar
1558 (return-info-types returns
))))
1559 (vop* known-return node block
1560 (old-fp return-pc
(reference-tn-list locs nil
))
1562 (return-info-locations returns
))))
1563 ((eq lvar-kind
:fixed
)
1564 (let* ((types (mapcar #'tn-primitive-type
(ir2-lvar-locs 2lvar
)))
1565 (lvar-locs (lvar-tns node block lvar types
))
1566 (nvals (length lvar-locs
))
1567 (locs (make-standard-value-tns nvals
)))
1568 (mapc (lambda (val loc
)
1569 (emit-move node block val loc
))
1573 (vop return-single node block old-fp return-pc
(car locs
))
1574 (vop* return node block
1575 (old-fp return-pc
(reference-tn-list locs nil
))
1579 (aver (eq lvar-kind
:unknown
))
1580 (vop* return-multiple node block
1582 (reference-tn-list (ir2-lvar-locs 2lvar
) nil
))
1589 ;;;; These are used by the debugger to find the top function on the
1590 ;;;; stack. They return the OLD-FP and RETURN-PC for the current
1591 ;;;; function as multiple values.
1593 (defoptimizer (%caller-frame ir2-convert
) (() node block
)
1594 (let ((ir2-physenv (physenv-info (node-physenv node
))))
1595 (move-lvar-result node block
1596 (list (ir2-physenv-old-fp ir2-physenv
))
1599 (defoptimizer (%caller-pc ir2-convert
) (() node block
)
1600 (let ((ir2-physenv (physenv-info (node-physenv node
))))
1601 (move-lvar-result node block
1602 (list (ir2-physenv-return-pc ir2-physenv
))
1605 ;;;; multiple values
1607 ;;; This is almost identical to IR2-CONVERT-LET. Since LTN annotates
1608 ;;; the lvar for the correct number of values (with the lvar user
1609 ;;; responsible for defaulting), we can just pick them up from the
1611 (defun ir2-convert-mv-bind (node block
)
1612 (declare (type mv-combination node
) (type ir2-block block
))
1613 (let* ((fun (ref-leaf (lvar-uses (basic-combination-fun node
))))
1614 (args (basic-combination-args node
))
1615 (vars (lambda-vars fun
)))
1616 (aver (eq (functional-kind fun
) :mv-let
))
1617 (mapc (lambda (src var
)
1618 (when (leaf-refs var
)
1619 (let ((dest (leaf-info var
)))
1620 (if (and (lambda-var-indirect var
)
1621 (lambda-var-explicit-value-cell var
))
1622 (emit-make-value-cell node block src dest
)
1623 (emit-move node block src dest
)))))
1624 (if (singleton-p args
)
1625 (lvar-tns node block
(first args
)
1627 (primitive-type (leaf-type x
)))
1630 (loop for lvar in args
1631 for values
= (nth-value 1 (values-types
1632 (lvar-derived-type lvar
)))
1635 (lvar-tns node block lvar
(loop repeat values
1637 (primitive-type (leaf-type (pop vars
)))
1638 *backend-t-primitive-type
*))))))
1642 ;;; Emit the appropriate fixed value, unknown value or tail variant of
1643 ;;; CALL-VARIABLE. Note that we only need to pass the values start for
1644 ;;; the first argument: all the other argument lvar TNs are
1645 ;;; ignored. This is because we require all of the values globs to be
1646 ;;; contiguous and on stack top.
1647 (defun ir2-convert-mv-call (node block
)
1648 (declare (type mv-combination node
) (type ir2-block block
))
1649 (aver (basic-combination-args node
))
1650 (let* ((start-lvar (lvar-info (first (basic-combination-args node
))))
1651 (start (first (ir2-lvar-locs start-lvar
)))
1652 (tails (and (node-tail-p node
)
1653 (lambda-tail-set (node-home-lambda node
))))
1654 (lvar (node-lvar node
))
1655 (2lvar (and lvar
(lvar-info lvar
))))
1656 (multiple-value-bind (fun named
)
1657 (fun-lvar-tn node block
(basic-combination-fun node
))
1658 (aver (and (not named
)
1659 (eq (ir2-lvar-kind start-lvar
) :unknown
)))
1662 (let ((env (physenv-info (node-physenv node
))))
1663 (vop tail-call-variable node block start fun
1664 (ir2-physenv-old-fp env
)
1665 (ir2-physenv-return-pc env
))))
1667 (eq (ir2-lvar-kind 2lvar
) :unknown
))
1668 (vop* multiple-call-variable node block
(start fun nil
)
1669 ((reference-tn-list (ir2-lvar-locs 2lvar
) t
))
1670 (emit-step-p node
)))
1672 (let ((locs (standard-result-tns lvar
)))
1673 (vop* call-variable node block
(start fun nil
)
1674 ((reference-tn-list locs t
)) (length locs
)
1676 (move-lvar-result node block locs lvar
)))))))
1678 ;;; Reset the stack pointer to the start of the specified
1679 ;;; unknown-values lvar (discarding it and all values globs on top of
1681 (defoptimizer (%pop-values ir2-convert
) ((%lvar
) node block
)
1682 (let* ((lvar (lvar-value %lvar
))
1683 (2lvar (lvar-info lvar
)))
1684 (cond ((eq (ir2-lvar-kind 2lvar
) :unknown
)
1685 (vop reset-stack-pointer node block
1686 (first (ir2-lvar-locs 2lvar
))))
1687 ((lvar-dynamic-extent lvar
)
1688 (vop reset-stack-pointer node block
1689 (ir2-lvar-stack-pointer 2lvar
)))
1690 (t (bug "Trying to pop a not stack-allocated LVAR ~S."
1693 (defoptimizer (%nip-values ir2-convert
) ((last-nipped last-preserved
1696 (let* ( ;; pointer immediately after the nipped block
1697 (after (lvar-value last-nipped
))
1698 (2after (lvar-info after
))
1699 ;; pointer to the first nipped word
1700 (first (lvar-value last-preserved
))
1701 (2first (lvar-info first
))
1703 (moved-tns (loop for lvar-ref in moved
1704 for lvar
= (lvar-value lvar-ref
)
1705 for
2lvar
= (lvar-info lvar
)
1707 collect
(first (ir2-lvar-locs 2lvar
)))))
1708 (aver (or (eq (ir2-lvar-kind 2after
) :unknown
)
1709 (lvar-dynamic-extent after
)))
1710 (aver (eq (ir2-lvar-kind 2first
) :unknown
))
1711 (when *check-consistency
*
1712 ;; we cannot move stack-allocated DX objects
1713 (dolist (moved-lvar moved
)
1714 (aver (eq (ir2-lvar-kind (lvar-info (lvar-value moved-lvar
)))
1716 (flet ((nip-aligned (nipped)
1717 (vop* %%nip-values node block
1719 (first (ir2-lvar-locs 2first
))
1720 (reference-tn-list moved-tns nil
))
1721 ((reference-tn-list moved-tns t
)))))
1722 (cond ((eq (ir2-lvar-kind 2after
) :unknown
)
1723 (nip-aligned (first (ir2-lvar-locs 2after
))))
1724 ((lvar-dynamic-extent after
)
1725 (nip-aligned (ir2-lvar-stack-pointer 2after
)))
1727 (bug "Trying to nip a not stack-allocated LVAR ~S." after
))))))
1729 (defoptimizer (%dummy-dx-alloc ir2-convert
) ((target source
) node block
)
1730 (let* ((target-lvar (lvar-value target
))
1731 (source-lvar (lvar-value source
))
1732 (target-2lvar (lvar-info target-lvar
))
1733 (source-2lvar (and source-lvar
(lvar-info source-lvar
))))
1734 (aver (lvar-dynamic-extent target-lvar
))
1735 (cond ((not source-lvar
)
1736 (vop current-stack-pointer node block
1737 (ir2-lvar-stack-pointer target-2lvar
)))
1738 ((lvar-dynamic-extent source-lvar
)
1739 (emit-move node block
1740 (ir2-lvar-stack-pointer source-2lvar
)
1741 (ir2-lvar-stack-pointer target-2lvar
)))
1742 ((eq (ir2-lvar-kind source-2lvar
) :unknown
)
1743 (emit-move node block
1744 (first (ir2-lvar-locs source-2lvar
))
1745 (ir2-lvar-stack-pointer target-2lvar
)))
1746 (t (bug "Trying to dummy up DX allocation from a ~
1747 not stack-allocated LVAR ~S." source-lvar
)))))
1749 ;;; Deliver the values TNs to LVAR using MOVE-LVAR-RESULT.
1750 (defoptimizer (values ir2-convert
) ((&rest values
) node block
)
1751 (let ((tns (mapcar (lambda (x)
1752 (lvar-tn node block x
))
1755 (move-lvar-result node block tns
(node-lvar node
))))
1757 ;;; In the normal case where unknown values are desired, we use the
1758 ;;; VALUES-LIST VOP. In the relatively unimportant case of VALUES-LIST
1759 ;;; for a fixed number of values, we punt by doing a full call to the
1760 ;;; VALUES-LIST function. This gets the full call VOP to deal with
1761 ;;; defaulting any unsupplied values. It seems unworthwhile to
1762 ;;; optimize this case.
1763 (defoptimizer (values-list ir2-convert
) ((list) node block
)
1764 (let* ((lvar (node-lvar node
))
1765 (2lvar (and lvar
(lvar-info lvar
))))
1767 (eq (ir2-lvar-kind 2lvar
) :unknown
))
1768 (let ((locs (ir2-lvar-locs 2lvar
)))
1769 (vop* values-list node block
1770 ((lvar-tn node block list
) nil
)
1771 ((reference-tn-list locs t
)))))
1772 (t (aver (or (not 2lvar
) ; i.e. we want to check the argument
1773 (eq (ir2-lvar-kind 2lvar
) :fixed
)))
1774 (ir2-convert-full-call node block
)))))
1776 (defoptimizer (%more-arg-values ir2-convert
) ((context start count
) node block
)
1777 (binding* ((lvar (node-lvar node
) :exit-if-null
)
1778 (2lvar (lvar-info lvar
)))
1779 (ecase (ir2-lvar-kind 2lvar
)
1781 ;; KLUDGE: this is very much unsafe, and can leak random stack values.
1782 ;; OTOH, I think the :FIXED case can only happen with (safety 0) in the
1785 (loop for loc in
(ir2-lvar-locs 2lvar
)
1787 do
(vop sb
!vm
::more-arg node block
1788 (lvar-tn node block context
)
1792 (let ((locs (ir2-lvar-locs 2lvar
)))
1793 (vop* %more-arg-values node block
1794 ((lvar-tn node block context
)
1795 (lvar-tn node block start
)
1796 (lvar-tn node block count
)
1798 ((reference-tn-list locs t
))))))))
1800 ;;;; special binding
1802 ;;; This is trivial, given our assumption of a shallow-binding
1804 (defoptimizer (%special-bind ir2-convert
) ((var value
) node block
)
1805 (let ((name (leaf-source-name (lvar-value var
))))
1806 ;; Emit either BIND or DYNBIND, preferring BIND if both exist.
1807 ;; If only one exists, it's DYNBIND.
1808 ;; Even if the backend supports load-time TLS index assignment,
1809 ;; there might be only one vop (as with arm64).
1810 (macrolet ((doit (bind dynbind
)
1811 (if (gethash 'bind
*backend-parsed-vops
*) bind dynbind
)))
1814 ;; Inform later SYMBOL-VALUE calls that they can
1815 ;; assume a nonzero tls-index.
1816 ;; FIXME: setting INFO is inefficient when not actually
1817 ;; changing anything
1818 (unless (info :variable
:wired-tls name
)
1819 (setf (info :variable
:wired-tls name
) t
))
1820 ;; We force the symbol into the code constants in case BIND
1821 ;; does not actually reference it, as with immobile symbols.
1822 (emit-constant name
)
1823 (vop bind node block
(lvar-tn node block value
) name
))
1824 (vop dynbind node block
(lvar-tn node block value
)
1825 (emit-constant name
))))))
1827 (defoptimizer (%special-unbind ir2-convert
) ((n) node block
)
1828 (declare (ignorable n
))
1829 (vop unbind node block
#!+(and sb-thread unbind-n-vop
) (lvar-value n
)))
1831 ;;; ### It's not clear that this really belongs in this file, or
1832 ;;; should really be done this way, but this is the least violation of
1833 ;;; abstraction in the current setup. We don't want to wire
1834 ;;; shallow-binding assumptions into IR1tran.
1835 (def-ir1-translator progv
1836 ((vars vals
&body body
) start next result
)
1839 (with-unique-names (bind unbind
)
1840 (once-only ((n-save-bs '(%primitive current-binding-pointer
)))
1843 (labels ((,unbind
(vars)
1844 (declare (optimize (speed 2) (debug 0)))
1845 (let ((unbound-marker (%primitive make-unbound-marker
)))
1847 ;; CLHS says "bound and then made to have no value" -- user
1848 ;; should not be able to tell the difference between that and this.
1849 (about-to-modify-symbol-value var
'progv
)
1850 (%primitive dynbind unbound-marker var
))))
1852 (declare (optimize (speed 2) (debug 0)
1853 (insert-debug-catch 0)))
1855 ((null vals
) (,unbind vars
))
1857 (let ((val (car vals
))
1859 (about-to-modify-symbol-value var
'progv val t
)
1860 (%primitive dynbind val var
))
1861 (,bind
(cdr vars
) (cdr vals
))))))
1862 (,bind
,vars
,vals
))
1865 ;; Technically ANSI CL doesn't allow declarations at the
1866 ;; start of the cleanup form. SBCL happens to allow for
1867 ;; them, due to the way the UNWIND-PROTECT ir1 translation
1868 ;; is implemented; the cleanup forms are directly spliced
1869 ;; into an FLET definition body. And a declaration here
1870 ;; actually has exactly the right scope for what we need
1871 ;; (ensure that debug instrumentation is not emitted for the
1872 ;; cleanup function). -- JES, 2007-06-16
1873 (declare (optimize (insert-debug-catch 0)))
1874 (%primitive unbind-to-here
,n-save-bs
))))))
1878 ;;; Convert a non-local lexical exit. First find the NLX-INFO in our
1879 ;;; environment. Note that this is never called on the escape exits
1880 ;;; for CATCH and UNWIND-PROTECT, since the escape functions aren't
1882 (defun ir2-convert-exit (node block
)
1883 (declare (type exit node
) (type ir2-block block
))
1884 (let* ((nlx (exit-nlx-info node
))
1885 (loc (find-in-physenv nlx
(node-physenv node
)))
1886 (temp (make-stack-pointer-tn))
1887 (value (exit-value node
)))
1888 (if (nlx-info-safe-p nlx
)
1889 (vop value-cell-ref node block loc temp
)
1890 (emit-move node block loc temp
))
1892 (let ((locs (ir2-lvar-locs (lvar-info value
))))
1893 (vop unwind node block temp
(first locs
) (second locs
)))
1894 (let ((0-tn (emit-constant 0)))
1895 (vop unwind node block temp
0-tn
0-tn
))))
1899 ;;; %CLEANUP-POINT doesn't do anything except prevent the body from
1900 ;;; being entirely deleted.
1901 (defoptimizer (%cleanup-point ir2-convert
) (() node block
) node block
)
1903 ;;; This function invalidates a lexical exit on exiting from the
1904 ;;; dynamic extent. This is done by storing 0 into the indirect value
1905 ;;; cell that holds the closed unwind block.
1906 (defoptimizer (%lexical-exit-breakup ir2-convert
) ((info) node block
)
1907 (let ((nlx (lvar-value info
)))
1908 (when (nlx-info-safe-p nlx
)
1909 (vop value-cell-set node block
1910 (find-in-physenv nlx
(node-physenv node
))
1911 (emit-constant 0)))))
1913 ;;; We have to do a spurious move of no values to the result lvar so
1914 ;;; that lifetime analysis won't get confused.
1915 (defun ir2-convert-throw (node block
)
1916 (declare (type mv-combination node
) (type ir2-block block
))
1917 (let ((args (basic-combination-args node
)))
1918 (check-catch-tag-type (first args
))
1919 (vop* throw node block
1920 ((lvar-tn node block
(first args
))
1922 (ir2-lvar-locs (lvar-info (second args
)))
1925 (move-lvar-result node block
() (node-lvar node
))
1928 ;;; Emit code to set up a non-local exit. INFO is the NLX-INFO for the
1929 ;;; exit, and TAG is the lvar for the catch tag (if any.) We get at
1930 ;;; the target PC by passing in the label to the vop. The vop is
1931 ;;; responsible for building a return-PC object.
1932 (defun emit-nlx-start (node block info tag
)
1933 (declare (type node node
) (type ir2-block block
) (type nlx-info info
)
1934 (type (or lvar null
) tag
))
1935 (let* ((2info (nlx-info-info info
))
1936 (kind (cleanup-kind (nlx-info-cleanup info
)))
1937 (block-tn (physenv-live-tn
1939 (primitive-type-or-lose
1943 ((:unwind-protect
:block
:tagbody
)
1945 (node-physenv node
)))
1946 (res (make-stack-pointer-tn))
1947 (target-label (ir2-nlx-info-target 2info
)))
1949 (vop current-binding-pointer node block
1950 (car (ir2-nlx-info-dynamic-state 2info
)))
1951 (vop* save-dynamic-state node block
1953 ((reference-tn-list (cdr (ir2-nlx-info-dynamic-state 2info
)) t
)))
1954 (vop current-stack-pointer node block
(ir2-nlx-info-save-sp 2info
))
1958 (vop make-catch-block node block block-tn
1959 (lvar-tn node block tag
) target-label res
))
1960 ((:unwind-protect
:block
:tagbody
)
1961 (vop make-unwind-block node block block-tn target-label res
)))
1965 (if (nlx-info-safe-p info
)
1966 (emit-make-value-cell node block res
(ir2-nlx-info-home 2info
))
1967 (emit-move node block res
(ir2-nlx-info-home 2info
))))
1969 (vop set-unwind-protect node block block-tn
))
1974 ;;; Scan each of ENTRY's exits, setting up the exit for each lexical exit.
1975 (defun ir2-convert-entry (node block
)
1976 (declare (type entry node
) (type ir2-block block
))
1978 (dolist (exit (entry-exits node
))
1979 (let ((info (exit-nlx-info exit
)))
1981 (not (memq info nlxes
))
1982 (member (cleanup-kind (nlx-info-cleanup info
))
1983 '(:block
:tagbody
)))
1985 (emit-nlx-start node block info nil
)))))
1988 ;;; Set up the unwind block for these guys.
1989 (defoptimizer (%catch ir2-convert
) ((info-lvar tag
) node block
)
1990 (check-catch-tag-type tag
)
1991 (emit-nlx-start node block
(lvar-value info-lvar
) tag
))
1992 (defoptimizer (%unwind-protect ir2-convert
) ((info-lvar cleanup
) node block
)
1993 (declare (ignore cleanup
))
1994 (emit-nlx-start node block
(lvar-value info-lvar
) nil
))
1996 ;;; Emit the entry code for a non-local exit. We receive values and
1997 ;;; restore dynamic state.
1999 ;;; In the case of a lexical exit or CATCH, we look at the exit lvar's
2000 ;;; kind to determine which flavor of entry VOP to emit. If unknown
2001 ;;; values, emit the xxx-MULTIPLE variant to the lvar locs. If fixed
2002 ;;; values, make the appropriate number of temps in the standard
2003 ;;; values locations and use the other variant, delivering the temps
2004 ;;; to the lvar using MOVE-LVAR-RESULT.
2006 ;;; In the UNWIND-PROTECT case, we deliver the first register
2007 ;;; argument, the argument count and the argument pointer to our lvar
2008 ;;; as multiple values. These values are the block exited to and the
2009 ;;; values start and count.
2011 ;;; After receiving values, we restore dynamic state. Except in the
2012 ;;; UNWIND-PROTECT case, the values receiving restores the stack
2013 ;;; pointer. In an UNWIND-PROTECT cleanup, we want to leave the stack
2014 ;;; pointer alone, since the thrown values are still out there.
2015 (defoptimizer (%nlx-entry ir2-convert
) ((info-lvar) node block
)
2016 (let* ((info (lvar-value info-lvar
))
2017 (lvar (node-lvar node
))
2018 (2info (nlx-info-info info
))
2019 (top-loc (ir2-nlx-info-save-sp 2info
))
2020 (start-loc (make-nlx-entry-arg-start-location))
2021 (count-loc (make-arg-count-location))
2022 (target (ir2-nlx-info-target 2info
)))
2024 (ecase (cleanup-kind (nlx-info-cleanup info
))
2025 ((:catch
:block
:tagbody
)
2026 (let ((2lvar (and lvar
(lvar-info lvar
))))
2027 (if (and 2lvar
(eq (ir2-lvar-kind 2lvar
) :unknown
))
2028 (vop* nlx-entry-multiple node block
2029 (top-loc start-loc count-loc nil
)
2030 ((reference-tn-list (ir2-lvar-locs 2lvar
) t
))
2032 (let ((locs (standard-result-tns lvar
)))
2033 (vop* nlx-entry node block
2034 (top-loc start-loc count-loc nil
)
2035 ((reference-tn-list locs t
))
2038 (move-lvar-result node block locs lvar
)))))
2040 (let ((block-loc (standard-arg-location 0)))
2041 (vop uwp-entry node block target block-loc start-loc count-loc
)
2044 (list block-loc start-loc count-loc
)
2048 (when *collect-dynamic-statistics
*
2049 (vop count-me node block
*dynamic-counts-tn
*
2050 (block-number (ir2-block-block block
))))
2052 (vop* restore-dynamic-state node block
2053 ((reference-tn-list (cdr (ir2-nlx-info-dynamic-state 2info
)) nil
))
2055 (vop unbind-to-here node block
2056 (car (ir2-nlx-info-dynamic-state 2info
)))))
2058 ;;;; n-argument functions
2060 (macrolet ((def (name)
2061 `(defoptimizer (,name ir2-convert
) ((&rest args
) node block
)
2064 (/ sb
!vm
:large-object-size
2065 (* sb
!vm
:n-word-bytes
2)))
2066 ;; The VOPs will try to allocate all space at once
2067 ;; And it'll end up in large objects, and no conses
2068 ;; are welcome there.
2069 (ir2-convert-full-call node block
))
2071 (let* ((refs (reference-tn-list
2072 (loop for arg in args
2073 for tn
= (make-normal-tn *backend-t-primitive-type
*)
2075 (emit-move node block
(lvar-tn node block arg
) tn
)
2078 (lvar (node-lvar node
))
2079 (res (lvar-result-tns
2081 (list (primitive-type (specifier-type 'list
))))))
2082 (when (and lvar
(lvar-dynamic-extent lvar
))
2083 (vop current-stack-pointer node block
2084 (ir2-lvar-stack-pointer (lvar-info lvar
))))
2085 (vop* ,name node block
(refs) ((first res
) nil
)
2087 (move-lvar-result node block res lvar
)))))))
2092 (defoptimizer (mask-signed-field ir2-convert
) ((width x
) node block
)
2094 (when (constant-lvar-p width
)
2095 (case (lvar-value width
)
2096 (#.
(- sb
!vm
:n-word-bits sb
!vm
:n-fixnum-tag-bits
)
2097 (when (or (csubtypep (lvar-type x
)
2098 (specifier-type 'word
))
2099 (csubtypep (lvar-type x
)
2100 (specifier-type 'sb
!vm
:signed-word
)))
2101 (let* ((lvar (node-lvar node
))
2102 (temp (make-normal-tn
2103 (if (csubtypep (lvar-type x
)
2104 (specifier-type 'word
))
2105 (primitive-type-of most-positive-word
)
2107 (- (ash most-positive-word -
1))))))
2108 (results (lvar-result-tns
2110 (list (primitive-type-or-lose 'fixnum
)))))
2111 (emit-move node block
(lvar-tn node block x
) temp
)
2112 (vop sb
!vm
::move-from-word
/fixnum node block
2113 temp
(first results
))
2114 (move-lvar-result node block results lvar
)
2116 (#.sb
!vm
:n-word-bits
2117 (when (csubtypep (lvar-type x
) (specifier-type 'word
))
2118 (let* ((lvar (node-lvar node
))
2119 (temp (make-normal-tn
2120 (primitive-type-of most-positive-word
)))
2121 (results (lvar-result-tns
2123 (list (primitive-type
2124 (specifier-type 'sb
!vm
:signed-word
))))))
2125 (emit-move node block
(lvar-tn node block x
) temp
)
2126 (vop sb
!vm
::word-move node block
2127 temp
(first results
))
2128 (move-lvar-result node block results lvar
)
2130 (if (template-p (basic-combination-info node
))
2131 (ir2-convert-template node block
)
2132 (ir2-convert-full-call node block
))))
2134 ;; just a fancy identity
2135 (defoptimizer (%typep-wrapper ir2-convert
) ((value variable type
) node block
)
2136 (declare (ignore variable type
))
2137 (let* ((lvar (node-lvar node
))
2138 (results (lvar-result-tns lvar
(list (primitive-type-or-lose t
)))))
2139 (emit-move node block
(lvar-tn node block value
) (first results
))
2140 (move-lvar-result node block results lvar
)))
2142 ;;; An identity to avoid complaints about constant modification
2143 (defoptimizer (ltv-wrapper ir2-convert
) ((x) node block
)
2144 (let* ((lvar (node-lvar node
))
2145 (results (lvar-result-tns lvar
(list (primitive-type-or-lose t
)))))
2146 (emit-move node block
(lvar-tn node block x
) (first results
))
2147 (move-lvar-result node block results lvar
)))
2149 #-sb-xc-host
;; package-lock-violation-p is not present yet
2150 (defoptimizer (set ir2-hook
) ((symbol value
) node block
)
2151 (declare (ignore value block
))
2152 (when (constant-lvar-p symbol
)
2153 (let* ((symbol (lvar-value symbol
))
2154 (kind (info :variable
:kind symbol
)))
2155 (when (and (eq kind
:unknown
)
2156 (sb!impl
::package-lock-violation-p
(symbol-package symbol
) symbol
))
2157 (let ((*compiler-error-context
* node
))
2158 (compiler-warn "violating package lock on ~/sb-ext:print-symbol-with-prefix/"
2161 (defoptimizer (restart-point ir2-convert
) ((location) node block
)
2162 (setf (restart-location-label (lvar-value location
))
2163 (block-label (ir2-block-block block
))))
2165 ;;; Convert the code in a component into VOPs.
2166 (defun ir2-convert (component)
2167 (declare (type component component
))
2168 (let (#!+sb-dyncount
2169 (*dynamic-counts-tn
*
2170 (when *collect-dynamic-statistics
*
2172 (block-number (block-next (component-head component
))))
2173 (counts (make-array blocks
2174 :element-type
'(unsigned-byte 32)
2175 :initial-element
0))
2176 (info (make-dyncount-info
2177 :for
(component-name component
)
2178 :costs
(make-array blocks
2179 :element-type
'(unsigned-byte 32)
2182 (setf (ir2-component-dyncount-info (component-info component
))
2184 (emit-constant info
)
2185 (emit-constant counts
)))))
2187 (declare (type index num
))
2188 (do-ir2-blocks (2block component
)
2189 (let ((block (ir2-block-block 2block
)))
2190 (when (block-start block
)
2191 (setf (block-number block
) num
)
2193 (when *collect-dynamic-statistics
*
2194 (let ((first-node (block-start-node block
)))
2195 (unless (or (and (bind-p first-node
)
2196 (xep-p (bind-lambda first-node
)))
2198 (node-lvar first-node
))
2203 #!+sb-dyncount
*dynamic-counts-tn
* #!-sb-dyncount nil
2206 (let ((first-node (block-start-node block
)))
2207 (unless (or (and (bind-p first-node
)
2208 ;; Bind-nodes already have safepoints
2209 (eq (bind-lambda first-node
)
2210 (lambda-home (bind-lambda first-node
))))
2211 (and (valued-node-p first-node
)
2212 (node-lvar first-node
)
2214 (node-lvar first-node
))
2216 (when (and (rest (block-pred block
))
2218 (member (loop-kind (block-loop block
))
2219 '(:natural
:strange
))
2220 (eq block
(loop-head (block-loop block
)))
2221 (policy first-node
(< inhibit-safepoints
2)))
2222 (vop sb
!vm
::insert-safepoint first-node
2block
))))
2223 (ir2-convert-block block
)
2227 ;;; If necessary, emit a terminal unconditional branch to go to the
2228 ;;; successor block. If the successor is the component tail, then
2229 ;;; there isn't really any successor, but if the end is a non-tail
2230 ;;; call to a function that's not *known* to never return, then we
2231 ;;; emit an error trap just in case the function really does return.
2233 ;;; Trapping after known calls makes it easier to understand type
2234 ;;; derivation bugs at runtime: they show up as nil-fun-returned-error,
2235 ;;; rather than the execution of arbitrary code or error traps.
2236 (defun finish-ir2-block (block)
2237 (declare (type cblock block
))
2238 (let* ((2block (block-info block
))
2239 (last (block-last block
))
2240 (succ (block-succ block
)))
2242 (aver (singleton-p succ
))
2243 (let ((target (first succ
)))
2244 (cond ((eq target
(component-tail (block-component block
)))
2245 (when (and (basic-combination-p last
)
2246 (or (eq (basic-combination-kind last
) :full
)
2247 (and (eq (basic-combination-kind last
) :known
)
2248 (eq (basic-combination-info last
) :full
))))
2249 (let* ((fun (basic-combination-fun last
))
2250 (use (lvar-uses fun
))
2251 (name (and (ref-p use
)
2252 (leaf-has-source-name-p (ref-leaf use
))
2253 (leaf-source-name (ref-leaf use
))))
2254 (ftype (and (info :function
:info name
) ; only use the FTYPE if
2255 (proclaimed-ftype name
)))) ; NAME was DEFKNOWN
2256 (unless (or (node-tail-p last
)
2257 (policy last
(zerop safety
))
2258 (and (fun-type-p ftype
)
2259 (eq *empty-type
* (fun-type-returns ftype
))))
2260 (vop nil-fun-returned-error last
2block
2262 (emit-constant name
)
2263 (multiple-value-bind (tn named
)
2264 (fun-lvar-tn last
2block fun
)
2267 ((not (eq (ir2-block-next 2block
) (block-info target
)))
2268 (vop branch last
2block
(block-label target
)))
2270 (register-drop-thru target
))))))
2274 ;;; Convert the code in a block into VOPs.
2275 (defun ir2-convert-block (block)
2276 (declare (type cblock block
))
2277 (let ((2block (block-info block
)))
2278 (do-nodes (node lvar block
)
2282 (let ((2lvar (lvar-info lvar
)))
2283 ;; function REF in a local call is not annotated
2284 (when (and 2lvar
(not (eq (ir2-lvar-kind 2lvar
) :delayed
)))
2285 (ir2-convert-ref node
2block
)))))
2287 (let ((kind (basic-combination-kind node
)))
2290 (ir2-convert-local-call node
2block
))
2292 (ir2-convert-full-call node
2block
))
2294 (let* ((info (basic-combination-fun-info node
))
2295 (fun (fun-info-ir2-convert info
))
2296 (hook (fun-info-ir2-hook info
)))
2298 (funcall hook node
2block
))
2300 (funcall fun node
2block
))
2301 ((eq (basic-combination-info node
) :full
)
2302 (ir2-convert-full-call node
2block
))
2304 (ir2-convert-template node
2block
))))))))
2306 (when (lvar-info (if-test node
))
2307 (ir2-convert-if node
2block
)))
2309 (let ((fun (bind-lambda node
)))
2310 (when (eq (lambda-home fun
) fun
)
2311 (ir2-convert-bind node
2block
))))
2313 (ir2-convert-return node
2block
))
2315 (ir2-convert-set node
2block
))
2317 (ir2-convert-cast node
2block
))
2320 ((eq (basic-combination-kind node
) :local
)
2321 (ir2-convert-mv-bind node
2block
))
2322 ((eq (lvar-fun-name (basic-combination-fun node
))
2324 (ir2-convert-throw node
2block
))
2326 (ir2-convert-mv-call node
2block
))))
2328 (when (exit-entry node
)
2329 (ir2-convert-exit node
2block
)))
2331 (ir2-convert-entry node
2block
)))))
2333 (finish-ir2-block block
)