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 ;;; Allocate an indirect value cell.
33 (defevent make-value-cell-event
"Allocate heap value cell for lexical var.")
34 (defun emit-make-value-cell (node block value res
)
35 (event make-value-cell-event node
)
36 (vop make-value-cell node block value nil res
))
40 ;;; Return the TN that holds the value of THING in the environment ENV.
41 (declaim (ftype (sfunction ((or nlx-info lambda-var clambda
) physenv
) tn
)
43 (defun find-in-physenv (thing physenv
)
44 (or (cdr (assoc thing
(ir2-physenv-closure (physenv-info physenv
))))
47 ;; I think that a failure of this assertion means that we're
48 ;; trying to access a variable which was improperly closed
49 ;; over. The PHYSENV describes a physical environment. Every
50 ;; variable that a form refers to should either be in its
51 ;; physical environment directly, or grabbed from a
52 ;; surrounding physical environment when it was closed over.
53 ;; The ASSOC expression above finds closed-over variables, so
54 ;; if we fell through the ASSOC expression, it wasn't closed
55 ;; over. Therefore, it must be in our physical environment
56 ;; directly. If instead it is in some other physical
57 ;; environment, then it's bogus for us to reference it here
58 ;; without it being closed over. -- WHN 2001-09-29
59 (aver (eq physenv
(lambda-physenv (lambda-var-home thing
))))
62 (aver (eq physenv
(block-physenv (nlx-info-target thing
))))
63 (ir2-nlx-info-home (nlx-info-info thing
)))
66 (entry-info-closure-tn (lambda-info thing
))))
67 (bug "~@<~2I~_~S ~_not found in ~_~S~:>" thing physenv
)))
69 ;;; If LEAF already has a constant TN, return that, otherwise make a
71 (defun constant-tn (leaf boxedp
)
72 (declare (type constant leaf
))
73 ;; When convenient we can have both a boxed and unboxed TN for
76 (or (constant-boxed-tn leaf
)
77 (setf (constant-boxed-tn leaf
) (make-constant-tn leaf t
)))
79 (setf (leaf-info leaf
) (make-constant-tn leaf nil
)))))
81 ;;; Return a TN that represents the value of LEAF, or NIL if LEAF
82 ;;; isn't directly represented by a TN. ENV is the environment that
83 ;;; the reference is done in.
84 (defun leaf-tn (leaf env boxedp
)
85 (declare (type leaf leaf
) (type physenv env
))
88 (unless (lambda-var-indirect leaf
)
89 (find-in-physenv leaf env
)))
90 (constant (constant-tn leaf boxedp
))
93 ;;; This is used to conveniently get a handle on a constant TN during
94 ;;; IR2 conversion. It returns a constant TN representing the Lisp
96 (defun emit-constant (value)
97 (constant-tn (find-constant value
) t
))
99 (defun boxed-combination-ref-p (combination lvar
)
100 (let ((args (combination-args combination
)))
101 (flet ((struct-slot-tagged-p (dd index
)
103 (find index
(dd-slots dd
) :key
#'dsd-index
))))
105 (eq (dsd-raw-type slot
) t
))))
107 (and (constant-lvar-p lvar
)
109 (case (combination-fun-source-name combination nil
)
110 (data-vector-set-with-offset
111 (and (eq lvar
(car (last args
)))
112 (csubtypep (lvar-type (car args
))
113 (specifier-type '(array t
)))))
115 (csubtypep (lvar-type (car args
))
116 (specifier-type '(array t
))))
117 (%make-structure-instance
118 (let* ((dd (constant (car args
)))
119 (slot-specs (constant (cadr args
)))
120 (pos (position lvar
(cddr args
)))
122 (nth pos slot-specs
))))
123 (struct-slot-tagged-p dd
(cddr slot
))))
125 (let* ((instance (lvar-type (car args
)))
126 (layout (and (structure-classoid-p instance
)
127 (classoid-layout instance
))))
128 (and (eq lvar
(car (last args
)))
129 (struct-slot-tagged-p (and layout
130 (layout-info layout
))
131 (constant (cadr args
))))))
132 ((%special-bind %set-sap-ref-lispobj
133 %rplaca %rplacd cons list list
*
137 (call-full-like-p combination
))))))
139 (defun boxed-ref-p (ref)
140 (let* ((lvar (ref-lvar ref
))
141 (dest (lvar-dest lvar
)))
142 (cond ((basic-combination-p dest
)
143 (if (combination-p dest
)
144 (boxed-combination-ref-p dest lvar
)
145 (call-full-like-p dest
)))
147 (global-var-p (set-var dest
)))
149 (let* ((fun (return-lambda dest
))
150 (returns (tail-set-info (lambda-tail-set fun
))))
152 (eq (return-info-kind returns
) :unknown
)))))))
154 ;;; Convert a REF node. The reference must not be delayed.
155 (defun ir2-convert-ref (node block
)
156 (declare (type ref node
) (type ir2-block block
))
157 (let* ((lvar (node-lvar node
))
158 (leaf (ref-leaf node
))
159 (locs (lvar-result-tns
160 lvar
(list (primitive-type (leaf-type leaf
)))))
164 (let ((tn (find-in-physenv leaf
(node-physenv node
)))
165 (indirect (lambda-var-indirect leaf
))
166 (explicit (lambda-var-explicit-value-cell leaf
)))
168 ((and indirect explicit
)
169 (vop value-cell-ref node block tn res
))
171 (not (eq (node-physenv node
)
172 (lambda-physenv (lambda-var-home leaf
)))))
173 (let ((reffer (third (primitive-type-indirect-cell-type
174 (primitive-type (leaf-type leaf
))))))
176 (funcall reffer node block tn
(leaf-info leaf
) res
)
177 (vop ancestor-frame-ref node block tn
(leaf-info leaf
) res
))))
178 (t (emit-move node block tn res
)))))
180 (emit-move node block
(constant-tn leaf
(boxed-ref-p node
)) res
))
182 (ir2-convert-closure node block leaf res
))
184 (ir2-convert-global-var node block leaf res
)))
185 (move-lvar-result node block locs lvar
))
188 (defun ir2-convert-global-var (node block leaf res
)
189 (let ((unsafe (policy node
(zerop safety
)))
190 (name (leaf-source-name leaf
)))
191 (ecase (global-var-kind leaf
)
193 (aver (symbolp name
))
194 (let ((name-tn (emit-constant name
)))
195 (if (or unsafe
(always-boundp name
))
196 (vop fast-symbol-value node block name-tn res
)
197 (vop symbol-value node block name-tn res
))))
199 (aver (symbolp name
))
200 (let ((name-tn (emit-constant name
)))
201 (if (or unsafe
(always-boundp name
))
202 (vop fast-symbol-global-value node block name-tn res
)
203 (vop symbol-global-value node block name-tn res
))))
205 ;; In cross-compilation, testing (INFO :function :definition) is not
206 ;; sensible (or possible) but we can assume that things with fun-info
207 ;; will eventually be defined. If that's untrue, e.g. if we referred
208 ;; to #'DESCRIBE during cold-load, we'd just fix it locally by declaring
209 ;; DESCRIBE notinline.
210 ;; But in the target, more caution is warranted because users might
211 ;; DEFKNOWN a function but fail to define it. And they shouldn't be
212 ;; expected to understand the failure mode and the remedy.
213 (cond ((and #-sb-xc-host
(info :function
:definition name
)
214 (info :function
:info name
)
215 (let ((*lexenv
* (node-lexenv node
)))
216 (not (fun-lexically-notinline-p name
))))
217 ;; Known functions can be dumped without going through fdefns.
218 ;; But if NOTINLINEd, don't early-bind to the functional value
219 ;; because that disallows redefinition, including but not limited
220 ;; to encapsulations, which in turn makes TRACE not work, which
221 ;; leads to extreme frustration when debugging.
222 (emit-move node block
(make-load-time-constant-tn :known-fun name
)
225 (let ((fdefn-tn (make-load-time-constant-tn :fdefinition name
)))
227 (vop fdefn-fun node block fdefn-tn res
)
228 (vop safe-fdefn-fun node block fdefn-tn res
)))))))))
230 ;;; some sanity checks for a CLAMBDA passed to IR2-CONVERT-CLOSURE
231 (defun assertions-on-ir2-converted-clambda (clambda)
232 ;; This assertion was sort of an experiment. It would be nice and
233 ;; sane and easier to understand things if it were *always* true,
234 ;; but experimentally I observe that it's only *almost* always
235 ;; true. -- WHN 2001-01-02
237 (aver (eql (lambda-component clambda
)
238 (block-component (ir2-block-block ir2-block
))))
239 ;; Check for some weirdness which came up in bug
242 ;; The MAKE-LOAD-TIME-CONSTANT-TN call above puts an :ENTRY record
243 ;; into the IR2-COMPONENT-CONSTANTS table. The dump-a-COMPONENT
245 ;; * treats every HANDLEless :ENTRY record into a
247 ;; * expects every patch to correspond to an
248 ;; IR2-COMPONENT-ENTRIES record.
249 ;; The IR2-COMPONENT-ENTRIES records are set by ENTRY-ANALYZE
250 ;; walking over COMPONENT-LAMBDAS. Bug 138b arose because there
251 ;; was a HANDLEless :ENTRY record which didn't correspond to an
252 ;; IR2-COMPONENT-ENTRIES record. That problem is hard to debug
253 ;; when it's caught at dump time, so this assertion tries to catch
255 (aver (member clambda
256 (component-lambdas (lambda-component clambda
))))
257 ;; another bug-138-related issue: COMPONENT-NEW-FUNCTIONALS is
258 ;; used as a queue for stuff pending to do in IR1, and now that
259 ;; we're doing IR2 it should've been completely flushed (but
261 (aver (null (component-new-functionals (lambda-component clambda
))))
264 ;;; Emit code to load a function object implementing FUNCTIONAL into
265 ;;; RES. This gets interesting when the referenced function is a
266 ;;; closure: we must make the closure and move the closed-over values
269 ;;; FUNCTIONAL is either a :TOPLEVEL-XEP functional or the XEP lambda
270 ;;; for the called function, since local call analysis converts all
271 ;;; closure references. If a :TOPLEVEL-XEP, we know it is not a
274 ;;; If a closed-over LAMBDA-VAR has no refs (is deleted), then we
275 ;;; don't initialize that slot. This can happen with closures over
276 ;;; top level variables, where optimization of the closure deleted the
277 ;;; variable. Since we committed to the closure format when we
278 ;;; pre-analyzed the top level code, we just leave an empty slot.
279 (defun ir2-convert-closure (ref ir2-block functional res
)
280 (declare (type ref ref
)
281 (type ir2-block ir2-block
)
282 (type functional functional
)
285 (aver (not (eql (functional-kind functional
) :deleted
)))
286 (unless (leaf-info functional
)
287 (setf (leaf-info functional
)
288 (make-entry-info :name
289 (functional-debug-name functional
))))))
290 (let ((closure (etypecase functional
292 (assertions-on-ir2-converted-clambda functional
)
293 (physenv-closure (get-lambda-physenv functional
)))
295 (aver (eq (functional-kind functional
) :toplevel-xep
))
300 (let* ((physenv (node-physenv ref
))
301 (tn (find-in-physenv functional physenv
)))
302 (emit-move ref ir2-block tn res
)))
303 ;; we're about to emit a reference to a "closure" that's actually
304 ;; an inlinable global function.
305 ((and (global-var-p (setf global-var
306 (functional-inline-expanded functional
)))
307 (eq :global-function
(global-var-kind global-var
)))
308 (ir2-convert-global-var ref ir2-block global-var res
))
310 ;; if we're here, we should have either a toplevel-xep (some
311 ;; global scope function in a different component) or an external
312 ;; reference to the "closure"'s body.
314 (aver (memq (functional-kind functional
) '(:external
:toplevel-xep
)))
315 (let ((entry (make-load-time-constant-tn :entry functional
)))
316 (emit-move ref ir2-block entry res
))))))
319 (defun closure-initial-value (what this-env current-fp
)
320 (declare (type (or nlx-info lambda-var clambda
) what
)
321 (type physenv this-env
)
322 (type (or tn null
) current-fp
))
323 ;; If we have an indirect LAMBDA-VAR that does not require an
324 ;; EXPLICIT-VALUE-CELL, and is from this environment (not from being
325 ;; closed over), we need to store the current frame pointer.
326 (if (and (lambda-var-p what
)
327 (lambda-var-indirect what
)
328 (not (lambda-var-explicit-value-cell what
))
329 (eq (lambda-physenv (lambda-var-home what
))
332 (find-in-physenv what this-env
)))
334 (defoptimizer (%allocate-closures ltn-annotate
) ((leaves) node ltn-policy
)
335 (declare (ignore ltn-policy
))
336 (when (lvar-dynamic-extent leaves
)
337 (let ((info (make-ir2-lvar *backend-t-primitive-type
*)))
338 (setf (ir2-lvar-kind info
) :delayed
)
339 (setf (lvar-info leaves
) info
)
340 (setf (ir2-lvar-stack-pointer info
)
341 (make-stack-pointer-tn)))))
343 (defoptimizer (%allocate-closures ir2-convert
) ((leaves) call
2block
)
344 (let ((dx-p (lvar-dynamic-extent leaves
)))
347 (vop current-stack-pointer call
2block
348 (ir2-lvar-stack-pointer (lvar-info leaves
))))
349 (dolist (leaf (lvar-value leaves
))
350 (binding* ((xep (awhen (functional-entry-fun leaf
)
351 ;; if the xep's been deleted then we can skip it
352 (if (eq (functional-kind it
) :deleted
)
355 (nil (aver (xep-p xep
)))
356 (entry-info (lambda-info xep
) :exit-if-null
)
357 (tn (entry-info-closure-tn entry-info
) :exit-if-null
)
358 (closure (physenv-closure (get-lambda-physenv xep
)))
360 (entry (make-load-time-constant-tn :entry xep
)))
361 (let ((this-env (node-physenv call
))
362 (leaf-dx-p (and dx-p
(leaf-dynamic-extent leaf
))))
363 (aver (entry-info-offset entry-info
))
364 (vop make-closure call
2block
#!-x86-64 entry
365 (entry-info-offset entry-info
) (length closure
)
367 (loop for what in closure and n from
0 do
368 (unless (and (lambda-var-p what
)
369 (null (leaf-refs what
)))
370 ;; In LABELS a closure may refer to another closure
371 ;; in the same group, so we must be sure that we
372 ;; store a closure only after its creation.
374 ;; TODO: Here is a simple solution: we postpone
375 ;; putting of all closures after all creations
376 ;; (though it may require more registers).
378 (delayed (list tn
(find-in-physenv what this-env
) n
))
379 (let ((initial-value (closure-initial-value
382 (vop closure-init call
2block
384 ;; An initial-value of NIL means to stash
385 ;; the frame pointer... which requires a
387 (vop closure-init-from-fp call
2block tn n
)))))))))
388 (loop for
(tn what n
) in
(delayed)
389 do
(vop closure-init call
2block
393 ;;; Convert a SET node. If the NODE's LVAR is annotated, then we also
394 ;;; deliver the value to that lvar. If the var is a lexical variable
395 ;;; with no refs, then we don't actually set anything, since the
396 ;;; variable has been deleted.
397 (defun ir2-convert-set (node block
)
398 (declare (type cset node
) (type ir2-block block
))
399 (let* ((lvar (node-lvar node
))
400 (leaf (set-var node
))
401 (val (lvar-tn node block
(set-value node
)))
404 lvar
(list (primitive-type (leaf-type leaf
))))
408 (when (leaf-refs leaf
)
409 (let ((tn (find-in-physenv leaf
(node-physenv node
)))
410 (indirect (lambda-var-indirect leaf
))
411 (explicit (lambda-var-explicit-value-cell leaf
)))
413 ((and indirect explicit
)
414 (vop value-cell-set node block tn val
))
416 (not (eq (node-physenv node
)
417 (lambda-physenv (lambda-var-home leaf
)))))
418 (let ((setter (fourth (primitive-type-indirect-cell-type
419 (primitive-type (leaf-type leaf
))))))
421 (funcall setter node block tn val
(leaf-info leaf
))
422 (vop ancestor-frame-set node block tn val
(leaf-info leaf
)))))
423 (t (emit-move node block val tn
))))))
425 (aver (symbolp (leaf-source-name leaf
)))
426 (ecase (global-var-kind leaf
)
428 (vop set node block
(emit-constant (leaf-source-name leaf
)) val
))
430 (vop %set-symbol-global-value node
431 block
(emit-constant (leaf-source-name leaf
)) val
)))))
433 (emit-move node block val
(first locs
))
434 (move-lvar-result node block locs lvar
)))
437 ;;;; utilities for receiving fixed values
439 ;;; Return a TN that can be referenced to get the value of LVAR. LVAR
440 ;;; must be LTN-ANNOTATED either as a delayed leaf ref or as a fixed,
441 ;;; single-value lvar.
443 ;;; The primitive-type of the result will always be the same as the
444 ;;; IR2-LVAR-PRIMITIVE-TYPE, ensuring that VOPs are always called with
445 ;;; TNs that satisfy the operand primitive-type restriction. We may
446 ;;; have to make a temporary of the desired type and move the actual
447 ;;; lvar TN into it. This happens when we delete a type check in
448 ;;; unsafe code or when we locally know something about the type of an
449 ;;; argument variable.
450 (defun lvar-tn (node block lvar
)
451 (declare (type node node
) (type ir2-block block
) (type lvar lvar
))
452 (let* ((2lvar (lvar-info lvar
))
454 (ecase (ir2-lvar-kind 2lvar
)
456 (let ((ref (lvar-uses lvar
)))
457 (leaf-tn (ref-leaf ref
) (node-physenv ref
) (boxed-ref-p ref
))))
459 (aver (= (length (ir2-lvar-locs 2lvar
)) 1))
460 (first (ir2-lvar-locs 2lvar
)))))
461 (ptype (ir2-lvar-primitive-type 2lvar
)))
463 (cond ((eq (tn-primitive-type lvar-tn
) ptype
) lvar-tn
)
465 (let ((temp (make-normal-tn ptype
)))
466 (emit-move node block lvar-tn temp
)
469 ;;; This is similar to LVAR-TN, but hacks multiple values. We return
470 ;;; TNs holding the values of LVAR with PTYPES as their primitive
471 ;;; types. LVAR must be annotated for the same number of fixed values
472 ;;; are there are PTYPES.
474 ;;; If the lvar has a type check, check the values into temps and
475 ;;; return the temps. When we have more values than assertions, we
476 ;;; move the extra values with no check.
477 (defun lvar-tns (node block lvar ptypes
)
478 (declare (type node node
) (type ir2-block block
)
479 (type lvar lvar
) (list ptypes
))
480 (let* ((locs (ir2-lvar-locs (lvar-info lvar
)))
481 (nlocs (length locs
)))
482 (aver (= nlocs
(length ptypes
)))
484 (mapcar (lambda (from to-type
)
485 (if (eq (tn-primitive-type from
) to-type
)
487 (let ((temp (make-normal-tn to-type
)))
488 (emit-move node block from temp
)
493 ;;;; utilities for delivering values to lvars
495 ;;; Return a list of TNs with the specifier TYPES that can be used as
496 ;;; result TNs to evaluate an expression into LVAR. This is used
497 ;;; together with MOVE-LVAR-RESULT to deliver fixed values to
500 ;;; If the lvar isn't annotated (meaning the values are discarded) or
501 ;;; is unknown-values, then we make temporaries for each supplied
502 ;;; value, providing a place to compute the result in until we decide
503 ;;; what to do with it (if anything.)
505 ;;; If the lvar is fixed-values, and wants the same number of values
506 ;;; as the user wants to deliver, then we just return the
507 ;;; IR2-LVAR-LOCS. Otherwise we make a new list padded as necessary by
508 ;;; discarded TNs. We always return a TN of the specified type, using
509 ;;; the lvar locs only when they are of the correct type.
510 (defun lvar-result-tns (lvar types
)
511 (declare (type (or lvar null
) lvar
) (type list types
))
513 (mapcar #'make-normal-tn types
)
514 (let ((2lvar (lvar-info lvar
)))
515 (ecase (ir2-lvar-kind 2lvar
)
517 (let* ((locs (ir2-lvar-locs 2lvar
))
518 (nlocs (length locs
))
519 (ntypes (length types
)))
520 (if (and (= nlocs ntypes
)
521 (do ((loc locs
(cdr loc
))
522 (type types
(cdr type
)))
524 (unless (eq (tn-primitive-type (car loc
)) (car type
))
527 (mapcar (lambda (loc type
)
528 (if (eq (tn-primitive-type loc
) type
)
530 (make-normal-tn type
)))
533 (mapcar #'make-normal-tn
534 (subseq types nlocs
)))
538 (mapcar #'make-normal-tn types
))))))
540 ;;; Make the first N standard value TNs, returning them in a list.
541 (defun make-standard-value-tns (n)
542 (declare (type unsigned-byte n
))
545 (res (standard-arg-location i
)))
548 ;;; Return a list of TNs wired to the standard value passing
549 ;;; conventions that can be used to receive values according to the
550 ;;; unknown-values convention. This is used together with
551 ;;; MOVE-LVAR-RESULT for delivering unknown values to a fixed values
554 ;;; If the lvar isn't annotated, then we treat as 0-values, returning
555 ;;; an empty list of temporaries.
557 ;;; If the lvar is annotated, then it must be :FIXED.
558 (defun standard-result-tns (lvar)
559 (declare (type (or lvar null
) lvar
))
561 (let ((2lvar (lvar-info lvar
)))
562 (ecase (ir2-lvar-kind 2lvar
)
564 (make-standard-value-tns (length (ir2-lvar-locs 2lvar
))))))
567 ;;; Just move each SRC TN into the corresponding DEST TN, defaulting
568 ;;; any unsupplied source values to NIL. We let EMIT-MOVE worry about
569 ;;; doing the appropriate coercions.
570 (defun move-results-coerced (node block src dest
)
571 (declare (type node node
) (type ir2-block block
) (list src dest
))
572 (let ((nsrc (length src
))
573 (ndest (length dest
)))
574 (mapc (lambda (from to
)
576 (emit-move node block from to
)))
578 (append src
(make-list (- ndest nsrc
)
579 :initial-element
(emit-constant nil
)))
584 ;;; If necessary, emit coercion code needed to deliver the RESULTS to
585 ;;; the specified lvar. NODE and BLOCK provide context for emitting
586 ;;; code. Although usually obtained from STANDARD-RESULT-TNs or
587 ;;; LVAR-RESULT-TNs, RESULTS may be a list of any type or
590 ;;; If the lvar is fixed values, then move the results into the lvar
591 ;;; locations. If the lvar is unknown values, then do the moves into
592 ;;; the standard value locations, and use PUSH-VALUES to put the
593 ;;; values on the stack.
594 (defun move-lvar-result (node block results lvar
)
595 (declare (type node node
) (type ir2-block block
)
596 (list results
) (type (or lvar null
) lvar
))
598 (let ((2lvar (lvar-info lvar
)))
599 (ecase (ir2-lvar-kind 2lvar
)
601 (let ((locs (ir2-lvar-locs 2lvar
)))
602 (unless (eq locs results
)
603 (move-results-coerced node block results locs
))))
605 (let* ((nvals (length results
))
606 (locs (make-standard-value-tns nvals
)))
607 (move-results-coerced node block results locs
)
608 (vop* push-values node block
609 ((reference-tn-list locs nil
))
610 ((reference-tn-list (ir2-lvar-locs 2lvar
) t
))
615 (defun ir2-convert-cast (node block
)
616 (declare (type cast node
)
617 (type ir2-block block
))
618 (binding* ((lvar (node-lvar node
) :exit-if-null
)
619 (2lvar (lvar-info lvar
))
620 (value (cast-value node
))
621 (2value (lvar-info value
)))
622 (ecase (ir2-lvar-kind 2lvar
)
625 (aver (not (cast-type-check node
)))
626 (move-results-coerced node block
627 (ir2-lvar-locs 2value
)
628 (ir2-lvar-locs 2lvar
))))))
630 (defoptimizer (%check-bound ir2-hook
) ((array bound index
) node block
)
631 (declare (ignore block
))
632 (when (constant-lvar-p bound
)
633 (let* ((bound-type (specifier-type `(integer 0 (,(lvar-value bound
)))))
634 (index-type (lvar-type index
)))
635 (when (eq (type-intersection bound-type index-type
)
637 (let ((*compiler-error-context
* node
))
638 (compiler-warn "Derived type ~s is not a suitable index for ~s."
639 (type-specifier index-type
)
640 (type-specifier (lvar-type array
))))))))
642 ;;;; template conversion
644 ;;; Build a TN-REFS list that represents access to the values of the
645 ;;; specified list of lvars ARGS for TEMPLATE. Any :CONSTANT arguments
646 ;;; are returned in the second value as a list rather than being
647 ;;; accessed as a normal argument. NODE and BLOCK provide the context
648 ;;; for emitting any necessary type-checking code.
649 (defun reference-args (node block args template
)
650 (declare (type node node
) (type ir2-block block
) (list args
)
651 (type template template
))
652 (collect ((info-args))
655 (do ((args args
(cdr args
))
656 (types (template-arg-types template
) (cdr types
)))
658 (let ((type (first types
))
660 (if (and (consp type
) (eq (car type
) ':constant
))
661 (info-args (lvar-value arg
))
662 (let ((ref (reference-tn (lvar-tn node block arg
) nil
)))
664 (setf (tn-ref-across last
) ref
)
668 (values (the (or tn-ref null
) first
) (info-args)))))
670 ;;; Convert a conditional template. We try to exploit any
671 ;;; drop-through, but emit an unconditional branch afterward if we
672 ;;; fail. NOT-P is true if the sense of the TEMPLATE's test should be
674 (defun ir2-convert-conditional (node block template args info-args if not-p
)
675 (declare (type node node
) (type ir2-block block
)
676 (type template template
) (type (or tn-ref null
) args
)
677 (list info-args
) (type cif if
) (type boolean not-p
))
678 (let ((consequent (if-consequent if
))
679 (alternative (if-alternative if
))
680 (flags (and (consp (template-result-types template
))
681 (rest (template-result-types template
)))))
682 (aver (= (template-info-arg-count template
)
683 (+ (length info-args
)
686 (rotatef consequent alternative
)
688 (when (drop-thru-p if consequent
)
689 (rotatef consequent alternative
)
692 (emit-template node block template args nil
693 (list* (block-label consequent
) not-p
695 (if (drop-thru-p if alternative
)
696 (register-drop-thru alternative
)
697 (vop branch node block
(block-label alternative
))))
699 (emit-template node block template args nil info-args
)
700 (vop branch-if node block
(block-label consequent
) flags not-p
)
701 (if (drop-thru-p if alternative
)
702 (register-drop-thru alternative
)
703 (vop branch node block
(block-label alternative
)))))))
705 ;;; Convert an IF that isn't the DEST of a conditional template.
706 (defun ir2-convert-if (node block
)
707 (declare (type ir2-block block
) (type cif node
))
708 (let* ((test (if-test node
))
709 (test-ref (reference-tn (lvar-tn node block test
) nil
))
710 (nil-ref (reference-tn (emit-constant nil
) nil
)))
711 (setf (tn-ref-across test-ref
) nil-ref
)
712 (ir2-convert-conditional node block
(template-or-lose 'if-eq
)
713 test-ref
() node t
)))
715 ;;; Return a list of primitive-types that we can pass to LVAR-RESULT-TNS
716 ;;; describing the result types we want for a template call. We are really
717 ;;; only interested in the number of results required: in normal case
718 ;;; TEMPLATE-RESULTS-OK has already checked them.
719 (defun find-template-result-types (call rtypes
)
720 (let* ((type (node-derived-type call
))
722 (mapcar #'primitive-type
723 (if (args-type-p type
)
724 (append (args-type-required type
)
725 (args-type-optional type
))
727 (primitive-t *backend-t-primitive-type
*))
728 (mapcar (lambda (rtype)
729 (declare (ignore rtype
))
730 (or (pop types
) primitive-t
)) rtypes
)))
732 ;;; Return a list of TNs usable in a CALL to TEMPLATE delivering values to
733 ;;; LVAR. As an efficiency hack, we pick off the common case where the LVAR is
734 ;;; fixed values and has locations that satisfy the result restrictions. This
735 ;;; can fail when there is a type check or a values count mismatch.
736 (defun make-template-result-tns (call lvar rtypes
)
737 (declare (type combination call
) (type (or lvar null
) lvar
)
739 (let ((2lvar (when lvar
(lvar-info lvar
))))
740 (if (and 2lvar
(eq (ir2-lvar-kind 2lvar
) :fixed
))
741 (let ((locs (ir2-lvar-locs 2lvar
)))
742 (if (and (= (length rtypes
) (length locs
))
743 (do ((loc locs
(cdr loc
))
744 (rtypes rtypes
(cdr rtypes
)))
746 (unless (operand-restriction-ok
748 (tn-primitive-type (car loc
))
754 (find-template-result-types call rtypes
))))
757 (find-template-result-types call rtypes
)))))
759 ;;; Get the operands into TNs, make TN-REFs for them, and then call
760 ;;; the template emit function.
761 (defun ir2-convert-template (call block
)
762 (declare (type combination call
) (type ir2-block block
))
763 (let* ((template (combination-info call
))
764 (lvar (node-lvar call
))
765 (rtypes (template-result-types template
)))
766 (multiple-value-bind (args info-args
)
767 (reference-args call block
(combination-args call
) template
)
768 (aver (not (template-more-results-type template
)))
769 (if (template-conditional-p template
)
770 (ir2-convert-conditional call block template args info-args
771 (lvar-dest lvar
) nil
)
772 (let* ((results (make-template-result-tns call lvar rtypes
))
773 (r-refs (reference-tn-list results t
)))
774 (aver (= (length info-args
)
775 (template-info-arg-count template
)))
776 (when (and lvar
(lvar-dynamic-extent lvar
))
777 (vop current-stack-pointer call block
778 (ir2-lvar-stack-pointer (lvar-info lvar
))))
779 (when (emit-step-p call
)
780 (vop sb
!vm
::step-instrument-before-vop call block
))
782 (emit-template call block template args r-refs info-args
)
783 (emit-template call block template args r-refs
))
784 (move-lvar-result call block results lvar
)))))
787 ;;; We don't have to do much because operand count checking is done by
788 ;;; IR1 conversion. The only difference between this and the function
789 ;;; case of IR2-CONVERT-TEMPLATE is that there can be codegen-info
791 (defoptimizer (%%primitive ir2-convert
) ((template info
&rest args
) call block
)
792 (declare (ignore args
))
793 (let* ((template (lvar-value template
))
794 (info (lvar-value info
))
795 (lvar (node-lvar call
))
796 (rtypes (template-result-types template
))
797 (results (make-template-result-tns call lvar rtypes
))
798 (r-refs (reference-tn-list results t
)))
799 (multiple-value-bind (args info-args
)
800 (reference-args call block
(cddr (combination-args call
)) template
)
801 (aver (not (template-more-results-type template
)))
802 (aver (not (template-conditional-p template
)))
803 (aver (null info-args
))
806 (emit-template call block template args r-refs info
)
807 (emit-template call block template args r-refs
))
809 (move-lvar-result call block results lvar
)))
812 (defoptimizer (%%primitive derive-type
) ((template info
&rest args
))
813 (declare (ignore info args
))
814 (let ((type (template-type (lvar-value template
))))
815 (if (fun-type-p type
)
816 (fun-type-returns type
)
821 ;;; Convert a LET by moving the argument values into the variables.
822 ;;; Since a LET doesn't have any passing locations, we move the
823 ;;; arguments directly into the variables. We must also allocate any
824 ;;; indirect value cells, since there is no function prologue to do
826 (defun ir2-convert-let (node block fun
)
827 (declare (type combination node
) (type ir2-block block
) (type clambda fun
))
828 (mapc (lambda (var arg
)
830 (let ((src (lvar-tn node block arg
))
831 (dest (leaf-info var
)))
832 (if (and (lambda-var-indirect var
)
833 (lambda-var-explicit-value-cell var
))
834 (emit-make-value-cell node block src dest
)
835 (emit-move node block src dest
)))))
836 (lambda-vars fun
) (basic-combination-args node
))
839 ;;; Emit any necessary moves into assignment temps for a local call to
840 ;;; FUN. We return two lists of TNs: TNs holding the actual argument
841 ;;; values, and (possibly EQ) TNs that are the actual destination of
842 ;;; the arguments. When necessary, we allocate temporaries for
843 ;;; arguments to preserve parallel assignment semantics. These lists
844 ;;; exclude unused arguments and include implicit environment
845 ;;; arguments, i.e. they exactly correspond to the arguments passed.
847 ;;; OLD-FP is the TN currently holding the value we want to pass as
848 ;;; OLD-FP. If null, then the call is to the same environment (an
849 ;;; :ASSIGNMENT), so we only move the arguments, and leave the
850 ;;; environment alone.
852 ;;; CLOSURE-FP is for calling a closure that has "implicit" value
853 ;;; cells (stored in the allocating stack frame), and is the frame
854 ;;; pointer TN to use for values allocated in the outbound stack
855 ;;; frame. This is distinct from OLD-FP for the specific case of a
857 (defun emit-psetq-moves (node block fun old-fp
&optional
(closure-fp old-fp
))
858 (declare (type combination node
) (type ir2-block block
) (type clambda fun
)
859 (type (or tn null
) old-fp closure-fp
))
860 (let ((actuals (mapcar (lambda (x)
862 (lvar-tn node block x
)))
863 (combination-args node
))))
866 (dolist (var (lambda-vars fun
))
867 (let ((actual (pop actuals
))
868 (loc (leaf-info var
)))
871 ((and (lambda-var-indirect var
)
872 (lambda-var-explicit-value-cell var
))
874 (make-normal-tn *backend-t-primitive-type
*)))
875 (emit-make-value-cell node block actual temp
)
877 ((member actual
(locs))
878 (let ((temp (make-normal-tn (tn-primitive-type loc
))))
879 (emit-move node block actual temp
)
886 (let ((this-1env (node-physenv node
))
887 (called-env (physenv-info (lambda-physenv fun
))))
888 (dolist (thing (ir2-physenv-closure called-env
))
889 (temps (closure-initial-value (car thing
) this-1env closure-fp
))
892 (locs (ir2-physenv-old-fp called-env
))))
894 (values (temps) (locs)))))
896 ;;; A tail-recursive local call is done by emitting moves of stuff
897 ;;; into the appropriate passing locations. After setting up the args
898 ;;; and environment, we just move our return-pc into the called
899 ;;; function's passing location.
900 (defun ir2-convert-tail-local-call (node block fun
)
901 (declare (type combination node
) (type ir2-block block
) (type clambda fun
))
902 (let ((this-env (physenv-info (node-physenv node
)))
903 (current-fp (make-stack-pointer-tn)))
904 (multiple-value-bind (temps locs
)
905 (emit-psetq-moves node block fun
906 (ir2-physenv-old-fp this-env
) current-fp
)
908 ;; If we're about to emit a move from CURRENT-FP then we need to
910 (when (find current-fp temps
)
911 (vop current-fp node block current-fp
))
913 (mapc (lambda (temp loc
)
914 (emit-move node block temp loc
))
917 (emit-move node block
918 (ir2-physenv-return-pc this-env
)
919 (ir2-physenv-return-pc-pass
921 (lambda-physenv fun
)))))
925 ;;; Convert an :ASSIGNMENT call. This is just like a tail local call,
926 ;;; except that the caller and callee environment are the same, so we
927 ;;; don't need to mess with the environment locations, return PC, etc.
928 (defun ir2-convert-assignment (node block fun
)
929 (declare (type combination node
) (type ir2-block block
) (type clambda fun
))
930 (multiple-value-bind (temps locs
) (emit-psetq-moves node block fun nil
)
932 (mapc (lambda (temp loc
)
933 (emit-move node block temp loc
))
937 ;;; Do stuff to set up the arguments to a non-tail local call
938 ;;; (including implicit environment args.) We allocate a frame
939 ;;; (returning the FP and NFP), and also compute the TN-REFS list for
940 ;;; the values to pass and the list of passing location TNs.
941 (defun ir2-convert-local-call-args (node block fun
)
942 (declare (type combination node
) (type ir2-block block
) (type clambda fun
))
943 (let ((fp (make-stack-pointer-tn))
944 (nfp (make-number-stack-pointer-tn))
945 (old-fp (make-stack-pointer-tn)))
946 (multiple-value-bind (temps locs
)
947 (emit-psetq-moves node block fun old-fp
)
948 (vop current-fp node block old-fp
)
949 (vop allocate-frame node block
950 (physenv-info (lambda-physenv fun
))
952 (values fp nfp temps
(mapcar #'make-alias-tn locs
)))))
954 ;;; Handle a non-TR known-values local call. We emit the call, then
955 ;;; move the results to the lvar's destination.
956 (defun ir2-convert-local-known-call (node block fun returns lvar start
)
957 (declare (type node node
) (type ir2-block block
) (type clambda fun
)
958 (type return-info returns
) (type (or lvar null
) lvar
)
960 (multiple-value-bind (fp nfp temps arg-locs
)
961 (ir2-convert-local-call-args node block fun
)
962 (let ((locs (return-info-locations returns
)))
963 (vop* known-call-local node block
964 (fp nfp
(reference-tn-list temps nil
))
965 ((reference-tn-list locs t
))
966 arg-locs
(physenv-info (lambda-physenv fun
)) start
)
967 (move-lvar-result node block locs lvar
)))
970 ;;; Handle a non-TR unknown-values local call. We do different things
971 ;;; depending on what kind of values the lvar wants.
973 ;;; If LVAR is :UNKNOWN, then we use the "multiple-" variant, directly
974 ;;; specifying the lvar's LOCS as the VOP results so that we don't
975 ;;; have to do anything after the call.
977 ;;; Otherwise, we use STANDARD-RESULT-TNS to get wired result TNs, and
978 ;;; then call MOVE-LVAR-RESULT to do any necessary type checks or
980 (defun ir2-convert-local-unknown-call (node block fun lvar start
)
981 (declare (type node node
) (type ir2-block block
) (type clambda fun
)
982 (type (or lvar null
) lvar
) (type label start
))
983 (multiple-value-bind (fp nfp temps arg-locs
)
984 (ir2-convert-local-call-args node block fun
)
985 (let ((2lvar (and lvar
(lvar-info lvar
)))
986 (env (physenv-info (lambda-physenv fun
)))
987 (temp-refs (reference-tn-list temps nil
)))
988 (if (and 2lvar
(eq (ir2-lvar-kind 2lvar
) :unknown
))
989 (vop* multiple-call-local node block
(fp nfp temp-refs
)
990 ((reference-tn-list (ir2-lvar-locs 2lvar
) t
))
992 (let ((locs (standard-result-tns lvar
)))
993 (vop* call-local node block
995 ((reference-tn-list locs t
))
996 arg-locs env start
(length locs
))
997 (move-lvar-result node block locs lvar
)))))
1000 ;;; Dispatch to the appropriate function, depending on whether we have
1001 ;;; a let, tail or normal call. If the function doesn't return, call
1002 ;;; it using the unknown-value convention. We could compile it as a
1003 ;;; tail call, but that might seem confusing in the debugger.
1004 (defun ir2-convert-local-call (node block
)
1005 (declare (type combination node
) (type ir2-block block
))
1006 (let* ((fun (ref-leaf (lvar-uses (basic-combination-fun node
))))
1007 (kind (functional-kind fun
)))
1008 (cond ((eq kind
:let
)
1009 (ir2-convert-let node block fun
))
1010 ((eq kind
:assignment
)
1011 (ir2-convert-assignment node block fun
))
1013 (ir2-convert-tail-local-call node block fun
))
1015 (let ((start (block-trampoline (lambda-block fun
)))
1016 (returns (tail-set-info (lambda-tail-set fun
)))
1017 (lvar (node-lvar node
)))
1019 (return-info-kind returns
)
1022 (ir2-convert-local-unknown-call node block fun lvar start
))
1024 (ir2-convert-local-known-call node block fun returns
1030 ;;; Given a function lvar FUN, return (VALUES TN-TO-CALL NAMED-P),
1031 ;;; where TN-TO-CALL is a TN holding the thing that we call NAMED-P is
1032 ;;; true if the thing is named (false if it is a function).
1034 ;;; There are two interesting non-named cases:
1035 ;;; -- We know it's a function. No check needed: return the
1037 ;;; -- We don't know what it is.
1038 (defun fun-lvar-tn (node block lvar
)
1039 (declare (ignore node block
))
1040 (declare (type lvar lvar
))
1041 (let ((2lvar (lvar-info lvar
)))
1042 (if (eq (ir2-lvar-kind 2lvar
) :delayed
)
1043 (let ((name (lvar-fun-name lvar t
)))
1045 (values (cond ((sb!vm
::static-fdefn-offset name
)
1048 ;; Named call to an immobile fdefn from an immobile component
1049 ;; uses the FUN-TN only to preserve liveness of the fdefn.
1050 ;; The name becomes an info arg.
1051 (make-load-time-constant-tn :fdefinition name
)))
1053 (let* ((locs (ir2-lvar-locs 2lvar
))
1055 (function-ptype (primitive-type-or-lose 'function
)))
1056 (aver (and (eq (ir2-lvar-kind 2lvar
) :fixed
)
1057 (= (length locs
) 1)))
1058 (aver (eq (tn-primitive-type loc
) function-ptype
))
1059 (values loc nil
)))))
1061 ;;; Set up the args to NODE in the current frame, and return a TN-REF
1062 ;;; list for the passing locations.
1063 (defun move-tail-full-call-args (node block
)
1064 (declare (type combination node
) (type ir2-block block
))
1065 (let ((args (basic-combination-args node
))
1068 (dotimes (num (length args
))
1069 (let ((loc (standard-arg-location num
)))
1070 (emit-move node block
(lvar-tn node block
(elt args num
)) loc
)
1071 (let ((ref (reference-tn loc nil
)))
1073 (setf (tn-ref-across last
) ref
)
1078 ;;; Move the arguments into the passing locations and do a (possibly
1079 ;;; named) tail call.
1080 (defun ir2-convert-tail-full-call (node block
)
1081 (declare (type combination node
) (type ir2-block block
))
1082 (let* ((env (physenv-info (node-physenv node
)))
1083 (args (basic-combination-args node
))
1084 (nargs (length args
))
1085 (pass-refs (move-tail-full-call-args node block
))
1086 (old-fp (ir2-physenv-old-fp env
))
1087 (return-pc (ir2-physenv-return-pc env
)))
1089 (multiple-value-bind (fun-tn named
)
1090 (fun-lvar-tn node block
(basic-combination-fun node
))
1092 (vop* tail-call node block
1093 (fun-tn old-fp return-pc pass-refs
)
1095 nargs
(emit-step-p node
)))
1098 (vop* static-tail-call-named node block
1099 (old-fp return-pc pass-refs
) ; args
1101 nargs named
(emit-step-p node
)))
1103 (vop* tail-call-named node block
1104 (#!-immobile-code fun-tn old-fp return-pc pass-refs
) ; args
1106 nargs
#!+immobile-code named
(emit-step-p node
)))))) ; info
1109 ;;; like IR2-CONVERT-LOCAL-CALL-ARGS, only different
1110 (defun ir2-convert-full-call-args (node block
)
1111 (declare (type combination node
) (type ir2-block block
))
1112 (let* ((args (basic-combination-args node
))
1113 (fp (make-stack-pointer-tn))
1114 (nargs (length args
)))
1115 (vop allocate-full-call-frame node block nargs fp
)
1119 (dotimes (num nargs
)
1120 (locs (standard-arg-location num
))
1121 (let ((ref (reference-tn (lvar-tn node block
(elt args num
))
1124 (setf (tn-ref-across last
) ref
)
1128 (values fp first
(locs) nargs
)))))
1130 ;;; Do full call when a fixed number of values are desired. We make
1131 ;;; STANDARD-RESULT-TNS for our lvar, then deliver the result using
1132 ;;; MOVE-LVAR-RESULT. We do named or normal call, as appropriate.
1133 (defun ir2-convert-fixed-full-call (node block
)
1134 (declare (type combination node
) (type ir2-block block
))
1135 (multiple-value-bind (fp args arg-locs nargs
)
1136 (ir2-convert-full-call-args node block
)
1137 (let* ((lvar (node-lvar node
))
1138 (locs (standard-result-tns lvar
))
1139 (loc-refs (reference-tn-list locs t
))
1140 (nvals (length locs
)))
1141 (multiple-value-bind (fun-tn named
)
1142 (fun-lvar-tn node block
(basic-combination-fun node
))
1144 (vop* call node block
(fp fun-tn args
) (loc-refs)
1145 arg-locs nargs nvals
(emit-step-p node
)))
1148 (vop* static-call-named node block
1151 arg-locs nargs named nvals
1152 (emit-step-p node
)))
1154 (vop* call-named node block
1155 (fp #!-immobile-code fun-tn args
) ; args
1156 (loc-refs) ; results
1157 arg-locs nargs
#!+immobile-code named nvals
; info
1158 (emit-step-p node
))))
1159 (move-lvar-result node block locs lvar
))))
1162 ;;; Do full call when unknown values are desired.
1163 (defun ir2-convert-multiple-full-call (node block
)
1164 (declare (type combination node
) (type ir2-block block
))
1165 (multiple-value-bind (fp args arg-locs nargs
)
1166 (ir2-convert-full-call-args node block
)
1167 (let* ((lvar (node-lvar node
))
1168 (locs (ir2-lvar-locs (lvar-info lvar
)))
1169 (loc-refs (reference-tn-list locs t
)))
1170 (multiple-value-bind (fun-tn named
)
1171 (fun-lvar-tn node block
(basic-combination-fun node
))
1173 (vop* multiple-call node block
(fp fun-tn args
) (loc-refs)
1174 arg-locs nargs
(emit-step-p node
)))
1177 (vop* static-multiple-call-named node block
1180 arg-locs nargs named
1181 (emit-step-p node
)))
1183 (vop* multiple-call-named node block
1184 (fp #!-immobile-code fun-tn args
) ; args
1185 (loc-refs) ; results
1186 arg-locs nargs
#!+immobile-code named
; info
1187 (emit-step-p node
)))))))
1190 ;;; stuff to check in PONDER-FULL-CALL
1192 ;;; These came in handy when troubleshooting cold boot after making
1193 ;;; major changes in the package structure: various transforms and
1194 ;;; VOPs and stuff got attached to the wrong symbol, so that
1195 ;;; references to the right symbol were bogusly translated as full
1196 ;;; calls instead of primitives, sending the system off into infinite
1197 ;;; space. Having a report on all full calls generated makes it easier
1198 ;;; to figure out what form caused the problem this time.
1199 (declaim (type (member :minimal
:detailed
:very-detailed
:maximal
)
1200 *track-full-called-fnames
*))
1201 (defvar *track-full-called-fnames
* :minimal
)
1203 ;;; Do some checks (and store some notes relevant for future checks)
1205 ;;; * Is this a full call to something we have reason to know should
1206 ;;; never be full called? (Except as of sbcl-0.7.18 or so, we no
1207 ;;; longer try to ensure this behavior when *FAILURE-P* has already
1209 (defun ponder-full-call (node)
1210 (let* ((lvar (basic-combination-fun node
))
1211 (fname (lvar-fun-name lvar t
)))
1212 (declare (type (or symbol cons
) fname
))
1214 (when (and (symbolp fname
)
1215 (eq (symbol-package fname
) *cl-package
*))
1216 ;; Never produce a warning from (DECLARE (INLINE LENGTH)) etc
1217 (return-from ponder-full-call
))
1219 ;; Warn about cross-compiling certain full-calls,
1220 ;; as it is indicative of dependency order problems.
1222 (let ((compname (component-name (node-component node
))))
1223 ;; Don't care too much about macro performance.
1224 (unless (and (stringp compname
) (string/= compname
"DEFMACRO"))
1225 ;; Catch FOO and (SETF FOO) both.
1226 (let ((stem (if (atom fname
) fname
(second fname
))))
1228 sb-cold
::*full-calls-to-warn-about
*
1230 (warn "Full call to ~S" fname
)))))
1232 (let* ((inlineable-p (not (let ((*lexenv
* (node-lexenv node
)))
1233 (fun-lexically-notinline-p fname
))))
1234 (inlineable-bit (if inlineable-p
1 0))
1235 (cell (info :function
:emitted-full-calls fname
)))
1237 ;; The low bit indicates whether any not-NOTINLINE call was seen.
1238 ;; The next-lowest bit is magic. Refer to %COMPILER-DEFMACRO
1239 ;; and WARN-IF-INLINE-FAILED/CALL for the pertinent logic.
1240 (setf cell
(list (logior 4 inlineable-bit
))
1241 (info :function
:emitted-full-calls fname
) cell
)
1242 (incf (car cell
) (+ 4 (if (oddp (car cell
)) 0 inlineable-bit
))))
1243 ;; If the full call was wanted, don't record anything.
1244 ;; (This was originally for debugging SBCL self-compilation)
1247 (warn-if-inline-failed/call fname
(node-lexenv node
) cell
))
1248 (case *track-full-called-fnames
*
1250 (when (boundp 'sb
!xc
:*compile-file-pathname
*)
1251 (pushnew sb
!xc
:*compile-file-pathname
* (cdr cell
)
1254 (pushnew (component-name *component-being-compiled
*)
1255 (cdr cell
) :test
#'equalp
)))))
1257 ;; Special mode, usually only for the cross-compiler
1258 ;; and only with the feature enabled.
1259 #!+sb-show
(when (eq *track-full-called-fnames
* :maximal
)
1260 (/show
"converting full call to named function" fname
)
1261 (/show
(basic-combination-args node
))
1262 (/show
(policy node speed
) (policy node safety
))
1263 (/show
(policy node compilation-speed
))
1264 (let ((arg-types (mapcar (lambda (lvar)
1268 (basic-combination-args node
))))
1271 ;; When illegal code is compiled, all sorts of perverse paths
1272 ;; through the compiler can be taken, and it's much harder -- and
1273 ;; probably pointless -- to guarantee that always-optimized-away
1274 ;; functions are actually optimized away. Thus, we skip the check
1277 ;; check to see if we know anything about the function
1278 (let ((info (info :function
:info fname
)))
1279 ;; if we know something, check to see if the full call was valid
1280 (when (and info
(ir1-attributep (fun-info-attributes info
)
1281 always-translatable
))
1282 (/show
(policy node speed
) (policy node safety
))
1283 (/show
(policy node compilation-speed
))
1284 (bug "full call to ~S" fname
))))
1287 (aver (legal-fun-name-p fname
))))) ;; FIXME: needless check?
1289 ;;; If the call is in a tail recursive position and the return
1290 ;;; convention is standard, then do a tail full call. If one or fewer
1291 ;;; values are desired, then use a single-value call, otherwise use a
1292 ;;; multiple-values call.
1293 (defun ir2-convert-full-call (node block
)
1294 (declare (type combination node
) (type ir2-block block
))
1295 (ponder-full-call node
)
1296 (cond ((node-tail-p node
)
1297 (ir2-convert-tail-full-call node block
))
1298 ((let ((lvar (node-lvar node
)))
1300 (eq (ir2-lvar-kind (lvar-info lvar
)) :unknown
)))
1301 (ir2-convert-multiple-full-call node block
))
1303 (ir2-convert-fixed-full-call node block
)))
1306 ;;;; entering functions
1308 #!+precise-arg-count-error
1309 (defun xep-verify-arg-count (node block fun arg-count-location
)
1310 (when (policy fun
(plusp verify-arg-count
))
1311 (let* ((ef (functional-entry-fun fun
))
1312 (optional (optional-dispatch-p ef
))
1314 (optional-dispatch-min-args ef
)))
1315 (max (cond ((not optional
)
1316 (1- (length (lambda-vars fun
))))
1318 (not (optional-dispatch-more-entry ef
)))
1319 (optional-dispatch-max-args ef
)))))
1320 (unless (and (eql min
0) (not max
))
1321 (vop verify-arg-count node block
1327 ;;; Do all the stuff that needs to be done on XEP entry:
1328 ;;; -- Create frame.
1329 ;;; -- Copy any more arg.
1330 ;;; -- Set up the environment, accessing any closure variables.
1331 ;;; -- Move args from the standard passing locations to their internal
1333 (defun init-xep-environment (node block fun
)
1334 (declare (type bind node
) (type ir2-block block
) (type clambda fun
))
1335 (let ((start-label (entry-info-offset (leaf-info fun
)))
1336 (env (physenv-info (node-physenv node
)))
1338 (let ((ef (functional-entry-fun fun
)))
1339 (vop xep-allocate-frame node block start-label
)
1340 ;; Arg verification needs to be done before the stack pointer is adjusted
1341 ;; so that the extra arguments are still present when the error is signalled
1342 (let ((verified (unless (eq (functional-kind fun
) :toplevel
)
1343 (setf arg-count-tn
(make-arg-count-location))
1344 #!+precise-arg-count-error
1345 (xep-verify-arg-count node block fun arg-count-tn
))))
1347 (declare (ignore verified
))
1348 (cond ((and (optional-dispatch-p ef
) (optional-dispatch-more-entry ef
))
1349 ;; COPY-MORE-ARG should handle SP adjustemnt, but it
1350 ;; isn't done on all targets.
1351 #!-precise-arg-count-error
1352 (vop xep-setup-sp node block
)
1353 (vop copy-more-arg node block
(optional-dispatch-max-args ef
)
1354 #!+x86-64 verified
))
1356 (vop xep-setup-sp node block
))))
1357 (when (ir2-physenv-closure env
)
1358 (let ((closure (make-normal-tn *backend-t-primitive-type
*)))
1359 (when (policy fun
(> store-closure-debug-pointer
1))
1360 ;; Save the closure pointer on the stack.
1361 (let ((closure-save (make-representation-tn
1362 *backend-t-primitive-type
*
1363 (sc-number-or-lose 'sb
!vm
::control-stack
))))
1364 (vop setup-closure-environment node block start-label
1366 (setf (ir2-physenv-closure-save-tn env
) closure-save
)
1367 (component-live-tn closure-save
)))
1368 (vop setup-closure-environment node block start-label closure
)
1370 (dolist (loc (ir2-physenv-closure env
))
1371 (vop closure-ref node block closure
(incf n
) (cdr loc
)))))))
1372 (unless (eq (functional-kind fun
) :toplevel
)
1373 (let ((vars (lambda-vars fun
))
1375 (when (leaf-refs (first vars
))
1376 (emit-move node block arg-count-tn
(leaf-info (first vars
))))
1377 (dolist (arg (rest vars
))
1378 (when (leaf-refs arg
)
1379 (let ((pass (standard-arg-location n
))
1380 (home (leaf-info arg
)))
1381 (if (and (lambda-var-indirect arg
)
1382 (lambda-var-explicit-value-cell arg
))
1383 (emit-make-value-cell node block pass home
)
1384 (emit-move node block pass home
))))
1387 (emit-move node block
(make-old-fp-passing-location t
)
1388 (ir2-physenv-old-fp env
)))
1392 ;;; Emit function prolog code. This is only called on bind nodes for
1393 ;;; functions that allocate environments. All semantics of let calls
1394 ;;; are handled by IR2-CONVERT-LET.
1396 ;;; If not an XEP, all we do is move the return PC from its passing
1397 ;;; location, since in a local call, the caller allocates the frame
1398 ;;; and sets up the arguments.
1400 #!+unwind-to-frame-and-call-vop
1401 (defun save-bsp (node block env
)
1402 ;; Save BSP on stack so that the binding environment can be restored
1403 ;; when restarting frames.
1404 ;; This is done inside functions, which leaves XEPs without saved
1405 ;; BSP, though the code in XEPs doesn't bind any variables, it can
1406 ;; call arbitrary code through the SATISFIES declaration.
1407 ;; And functions called by SATISFIES are not inlined, except for
1408 ;; source transforms, but these usually do not bind anything.
1409 ;; Thus when restarting it needs to check that the interrupt was in
1412 ;; It could be saved from the XEP, but some functions have both
1413 ;; external and internal entry points, so it will be saved twice.
1414 (let ((temp (make-normal-tn *backend-t-primitive-type
*))
1415 (bsp-save-tn (make-representation-tn
1416 *backend-t-primitive-type
*
1417 (sc-number-or-lose 'sb
!vm
::control-stack
))))
1418 (vop current-binding-pointer node block temp
)
1419 (emit-move node block temp bsp-save-tn
)
1420 (setf (ir2-physenv-bsp-save-tn env
) bsp-save-tn
)
1421 (component-live-tn bsp-save-tn
)))
1423 (defun ir2-convert-bind (node block
)
1424 (declare (type bind node
) (type ir2-block block
))
1425 (let* ((fun (bind-lambda node
))
1426 (env (physenv-info (lambda-physenv fun
))))
1427 (aver (member (functional-kind fun
)
1428 '(nil :external
:optional
:toplevel
:cleanup
)))
1431 (init-xep-environment node block fun
)
1433 (when *collect-dynamic-statistics
*
1434 (vop count-me node block
*dynamic-counts-tn
*
1435 (block-number (ir2-block-block block
)))))
1436 ((policy fun
(> store-closure-debug-pointer
1))
1437 ;; Propagate the location of the closure pointer from the
1438 ;; enclosing functions. (FIXME: Should make sure that this
1439 ;; handles closures inside closures correctly). [remark by JES]
1440 (let* ((entry-fun (lambda-entry-fun fun
)))
1442 (let ((2env (physenv-info (lambda-physenv fun
)))
1443 (entry-2env (physenv-info (lambda-physenv entry-fun
))))
1444 (setf (ir2-physenv-closure-save-tn 2env
)
1445 (ir2-physenv-closure-save-tn entry-2env
)))))))
1449 (ir2-physenv-return-pc-pass env
)
1450 (ir2-physenv-return-pc env
))
1451 #!+unwind-to-frame-and-call-vop
1452 (when (and (lambda-allow-instrumenting fun
)
1453 (not (lambda-inline-expanded fun
))
1454 (policy fun
(>= insert-debug-catch
1)))
1455 (save-bsp node block env
))
1457 (let ((lab (gen-label)))
1458 (setf (ir2-physenv-environment-start env
) lab
)
1459 (vop note-environment-start node block lab
)
1461 (unless (policy fun
(>= inhibit-safepoints
2))
1462 (vop sb
!vm
::insert-safepoint node block
))))
1466 ;;;; function return
1468 ;;; Do stuff to return from a function with the specified values and
1469 ;;; convention. If the return convention is :FIXED and we aren't
1470 ;;; returning from an XEP, then we do a known return (letting
1471 ;;; representation selection insert the correct move-arg VOPs.)
1472 ;;; Otherwise, we use the unknown-values convention. If there is a
1473 ;;; fixed number of return values, then use RETURN, otherwise use
1474 ;;; RETURN-MULTIPLE.
1475 (defun ir2-convert-return (node block
)
1476 (declare (type creturn node
) (type ir2-block block
))
1477 (let* ((lvar (return-result node
))
1478 (2lvar (lvar-info lvar
))
1479 (lvar-kind (ir2-lvar-kind 2lvar
))
1480 (fun (return-lambda node
))
1481 (env (physenv-info (lambda-physenv fun
)))
1482 (old-fp (ir2-physenv-old-fp env
))
1483 (return-pc (ir2-physenv-return-pc env
))
1484 (returns (tail-set-info (lambda-tail-set fun
))))
1486 ((and (eq (return-info-kind returns
) :fixed
)
1488 (let ((locs (lvar-tns node block lvar
1489 (return-info-types returns
))))
1490 (vop* known-return node block
1491 (old-fp return-pc
(reference-tn-list locs nil
))
1493 (return-info-locations returns
))))
1494 ((eq lvar-kind
:fixed
)
1495 (let* ((types (mapcar #'tn-primitive-type
(ir2-lvar-locs 2lvar
)))
1496 (lvar-locs (lvar-tns node block lvar types
))
1497 (nvals (length lvar-locs
))
1498 (locs (make-standard-value-tns nvals
)))
1499 (mapc (lambda (val loc
)
1500 (emit-move node block val loc
))
1504 (vop return-single node block old-fp return-pc
(car locs
))
1505 (vop* return node block
1506 (old-fp return-pc
(reference-tn-list locs nil
))
1510 (aver (eq lvar-kind
:unknown
))
1511 (vop* return-multiple node block
1513 (reference-tn-list (ir2-lvar-locs 2lvar
) nil
))
1520 ;;;; These are used by the debugger to find the top function on the
1521 ;;;; stack. They return the OLD-FP and RETURN-PC for the current
1522 ;;;; function as multiple values.
1524 (defoptimizer (%caller-frame ir2-convert
) (() node block
)
1525 (let ((ir2-physenv (physenv-info (node-physenv node
))))
1526 (move-lvar-result node block
1527 (list (ir2-physenv-old-fp ir2-physenv
))
1530 (defoptimizer (%caller-pc ir2-convert
) (() node block
)
1531 (let ((ir2-physenv (physenv-info (node-physenv node
))))
1532 (move-lvar-result node block
1533 (list (ir2-physenv-return-pc ir2-physenv
))
1536 ;;;; multiple values
1538 ;;; This is almost identical to IR2-CONVERT-LET. Since LTN annotates
1539 ;;; the lvar for the correct number of values (with the lvar user
1540 ;;; responsible for defaulting), we can just pick them up from the
1542 (defun ir2-convert-mv-bind (node block
)
1543 (declare (type mv-combination node
) (type ir2-block block
))
1544 (let* ((fun (ref-leaf (lvar-uses (basic-combination-fun node
))))
1545 (args (basic-combination-args node
))
1546 (vars (lambda-vars fun
)))
1547 (aver (eq (functional-kind fun
) :mv-let
))
1548 (mapc (lambda (src var
)
1549 (when (leaf-refs var
)
1550 (let ((dest (leaf-info var
)))
1551 (if (and (lambda-var-indirect var
)
1552 (lambda-var-explicit-value-cell var
))
1553 (emit-make-value-cell node block src dest
)
1554 (emit-move node block src dest
)))))
1555 (if (singleton-p args
)
1556 (lvar-tns node block
(first args
)
1558 (primitive-type (leaf-type x
)))
1561 (loop for lvar in args
1562 for values
= (nth-value 1 (values-types
1563 (lvar-derived-type lvar
)))
1566 (lvar-tns node block lvar
(loop repeat values
1567 collect
(primitive-type (leaf-type (pop vars
))))))))
1571 ;;; Emit the appropriate fixed value, unknown value or tail variant of
1572 ;;; CALL-VARIABLE. Note that we only need to pass the values start for
1573 ;;; the first argument: all the other argument lvar TNs are
1574 ;;; ignored. This is because we require all of the values globs to be
1575 ;;; contiguous and on stack top.
1576 (defun ir2-convert-mv-call (node block
)
1577 (declare (type mv-combination node
) (type ir2-block block
))
1578 (aver (basic-combination-args node
))
1579 (let* ((start-lvar (lvar-info (first (basic-combination-args node
))))
1580 (start (first (ir2-lvar-locs start-lvar
)))
1581 (tails (and (node-tail-p node
)
1582 (lambda-tail-set (node-home-lambda node
))))
1583 (lvar (node-lvar node
))
1584 (2lvar (and lvar
(lvar-info lvar
))))
1585 (multiple-value-bind (fun named
)
1586 (fun-lvar-tn node block
(basic-combination-fun node
))
1587 (aver (and (not named
)
1588 (eq (ir2-lvar-kind start-lvar
) :unknown
)))
1591 (let ((env (physenv-info (node-physenv node
))))
1592 (vop tail-call-variable node block start fun
1593 (ir2-physenv-old-fp env
)
1594 (ir2-physenv-return-pc env
))))
1596 (eq (ir2-lvar-kind 2lvar
) :unknown
))
1597 (vop* multiple-call-variable node block
(start fun nil
)
1598 ((reference-tn-list (ir2-lvar-locs 2lvar
) t
))
1599 (emit-step-p node
)))
1601 (let ((locs (standard-result-tns lvar
)))
1602 (vop* call-variable node block
(start fun nil
)
1603 ((reference-tn-list locs t
)) (length locs
)
1605 (move-lvar-result node block locs lvar
)))))))
1607 ;;; Reset the stack pointer to the start of the specified
1608 ;;; unknown-values lvar (discarding it and all values globs on top of
1610 (defoptimizer (%pop-values ir2-convert
) ((%lvar
) node block
)
1611 (let* ((lvar (lvar-value %lvar
))
1612 (2lvar (lvar-info lvar
)))
1613 (cond ((eq (ir2-lvar-kind 2lvar
) :unknown
)
1614 (vop reset-stack-pointer node block
1615 (first (ir2-lvar-locs 2lvar
))))
1616 ((lvar-dynamic-extent lvar
)
1617 (vop reset-stack-pointer node block
1618 (ir2-lvar-stack-pointer 2lvar
)))
1619 (t (bug "Trying to pop a not stack-allocated LVAR ~S."
1622 (defoptimizer (%nip-values ir2-convert
) ((last-nipped last-preserved
1625 (let* ( ;; pointer immediately after the nipped block
1626 (after (lvar-value last-nipped
))
1627 (2after (lvar-info after
))
1628 ;; pointer to the first nipped word
1629 (first (lvar-value last-preserved
))
1630 (2first (lvar-info first
))
1632 (moved-tns (loop for lvar-ref in moved
1633 for lvar
= (lvar-value lvar-ref
)
1634 for
2lvar
= (lvar-info lvar
)
1636 collect
(first (ir2-lvar-locs 2lvar
)))))
1637 (aver (or (eq (ir2-lvar-kind 2after
) :unknown
)
1638 (lvar-dynamic-extent after
)))
1639 (aver (eq (ir2-lvar-kind 2first
) :unknown
))
1640 (when *check-consistency
*
1641 ;; we cannot move stack-allocated DX objects
1642 (dolist (moved-lvar moved
)
1643 (aver (eq (ir2-lvar-kind (lvar-info (lvar-value moved-lvar
)))
1645 (flet ((nip-aligned (nipped)
1646 (vop* %%nip-values node block
1648 (first (ir2-lvar-locs 2first
))
1649 (reference-tn-list moved-tns nil
))
1650 ((reference-tn-list moved-tns t
)))))
1651 (cond ((eq (ir2-lvar-kind 2after
) :unknown
)
1652 (nip-aligned (first (ir2-lvar-locs 2after
))))
1653 ((lvar-dynamic-extent after
)
1654 (nip-aligned (ir2-lvar-stack-pointer 2after
)))
1656 (bug "Trying to nip a not stack-allocated LVAR ~S." after
))))))
1658 (defoptimizer (%dummy-dx-alloc ir2-convert
) ((target source
) node block
)
1659 (let* ((target-lvar (lvar-value target
))
1660 (source-lvar (lvar-value source
))
1661 (target-2lvar (lvar-info target-lvar
))
1662 (source-2lvar (and source-lvar
(lvar-info source-lvar
))))
1663 (aver (lvar-dynamic-extent target-lvar
))
1664 (cond ((not source-lvar
)
1665 (vop current-stack-pointer node block
1666 (ir2-lvar-stack-pointer target-2lvar
)))
1667 ((lvar-dynamic-extent source-lvar
)
1668 (emit-move node block
1669 (ir2-lvar-stack-pointer source-2lvar
)
1670 (ir2-lvar-stack-pointer target-2lvar
)))
1671 ((eq (ir2-lvar-kind source-2lvar
) :unknown
)
1672 (emit-move node block
1673 (first (ir2-lvar-locs source-2lvar
))
1674 (ir2-lvar-stack-pointer target-2lvar
)))
1675 (t (bug "Trying to dummy up DX allocation from a ~
1676 not stack-allocated LVAR ~S." source-lvar
)))))
1678 ;;; Deliver the values TNs to LVAR using MOVE-LVAR-RESULT.
1679 (defoptimizer (values ir2-convert
) ((&rest values
) node block
)
1680 (let ((tns (mapcar (lambda (x)
1681 (lvar-tn node block x
))
1684 (move-lvar-result node block tns
(node-lvar node
))))
1686 ;;; In the normal case where unknown values are desired, we use the
1687 ;;; VALUES-LIST VOP. In the relatively unimportant case of VALUES-LIST
1688 ;;; for a fixed number of values, we punt by doing a full call to the
1689 ;;; VALUES-LIST function. This gets the full call VOP to deal with
1690 ;;; defaulting any unsupplied values. It seems unworthwhile to
1691 ;;; optimize this case.
1692 (defoptimizer (values-list ir2-convert
) ((list) node block
)
1693 (let* ((lvar (node-lvar node
))
1694 (2lvar (and lvar
(lvar-info lvar
))))
1696 (eq (ir2-lvar-kind 2lvar
) :unknown
))
1697 (let ((locs (ir2-lvar-locs 2lvar
)))
1698 (vop* values-list node block
1699 ((lvar-tn node block list
) nil
)
1700 ((reference-tn-list locs t
)))))
1701 (t (aver (or (not 2lvar
) ; i.e. we want to check the argument
1702 (eq (ir2-lvar-kind 2lvar
) :fixed
)))
1703 (ir2-convert-full-call node block
)))))
1705 (defoptimizer (%more-arg-values ir2-convert
) ((context start count
) node block
)
1706 (binding* ((lvar (node-lvar node
) :exit-if-null
)
1707 (2lvar (lvar-info lvar
)))
1708 (ecase (ir2-lvar-kind 2lvar
)
1710 ;; KLUDGE: this is very much unsafe, and can leak random stack values.
1711 ;; OTOH, I think the :FIXED case can only happen with (safety 0) in the
1714 (loop for loc in
(ir2-lvar-locs 2lvar
)
1716 do
(vop sb
!vm
::more-arg node block
1717 (lvar-tn node block context
)
1721 (let ((locs (ir2-lvar-locs 2lvar
)))
1722 (vop* %more-arg-values node block
1723 ((lvar-tn node block context
)
1724 (lvar-tn node block start
)
1725 (lvar-tn node block count
)
1727 ((reference-tn-list locs t
))))))))
1729 ;;;; special binding
1731 ;;; This is trivial, given our assumption of a shallow-binding
1733 (defoptimizer (%special-bind ir2-convert
) ((var value
) node block
)
1734 (let ((name (leaf-source-name (lvar-value var
))))
1735 ;; Emit either BIND or DYNBIND, preferring BIND if both exist.
1736 ;; If only one exists, it's DYNBIND.
1737 ;; Even if the backend supports load-time TLS index assignment,
1738 ;; there might be only one vop (as with arm64).
1739 (macrolet ((doit (bind dynbind
)
1740 (if (gethash 'bind
*backend-parsed-vops
*) bind dynbind
)))
1743 ;; Inform later SYMBOL-VALUE calls that they can
1744 ;; assume a nonzero tls-index.
1745 ;; FIXME: setting INFO is inefficient when not actually
1746 ;; changing anything
1747 (unless (info :variable
:wired-tls name
)
1748 (setf (info :variable
:wired-tls name
) :always-has-tls
))
1749 ;; We force the symbol into the code constants in case BIND
1750 ;; does not actually reference it, as with x86.
1751 (emit-constant name
)
1752 (vop bind node block
(lvar-tn node block value
) name
))
1753 (vop dynbind node block
(lvar-tn node block value
)
1754 (emit-constant name
))))))
1756 (defoptimizer (%special-unbind ir2-convert
) ((n) node block
)
1757 (declare (ignorable n
))
1758 (vop unbind node block
#!+(and sb-thread unbind-n-vop
) (lvar-value n
)))
1760 ;;; ### It's not clear that this really belongs in this file, or
1761 ;;; should really be done this way, but this is the least violation of
1762 ;;; abstraction in the current setup. We don't want to wire
1763 ;;; shallow-binding assumptions into IR1tran.
1764 (def-ir1-translator progv
1765 ((vars vals
&body body
) start next result
)
1768 (with-unique-names (bind unbind
)
1769 (once-only ((n-save-bs '(%primitive current-binding-pointer
)))
1772 (labels ((,unbind
(vars)
1773 (declare (optimize (speed 2) (debug 0)))
1774 (let ((unbound-marker (%primitive make-unbound-marker
)))
1776 ;; CLHS says "bound and then made to have no value" -- user
1777 ;; should not be able to tell the difference between that and this.
1778 (about-to-modify-symbol-value var
'progv
)
1779 (%primitive dynbind unbound-marker var
))))
1781 (declare (optimize (speed 2) (debug 0)
1782 (insert-debug-catch 0)))
1784 ((null vals
) (,unbind vars
))
1786 (let ((val (car vals
))
1788 (about-to-modify-symbol-value var
'progv val t
)
1789 (%primitive dynbind val var
))
1790 (,bind
(cdr vars
) (cdr vals
))))))
1791 (,bind
,vars
,vals
))
1794 ;; Technically ANSI CL doesn't allow declarations at the
1795 ;; start of the cleanup form. SBCL happens to allow for
1796 ;; them, due to the way the UNWIND-PROTECT ir1 translation
1797 ;; is implemented; the cleanup forms are directly spliced
1798 ;; into an FLET definition body. And a declaration here
1799 ;; actually has exactly the right scope for what we need
1800 ;; (ensure that debug instrumentation is not emitted for the
1801 ;; cleanup function). -- JES, 2007-06-16
1802 (declare (optimize (insert-debug-catch 0)))
1803 (%primitive unbind-to-here
,n-save-bs
))))))
1807 ;;; Convert a non-local lexical exit. First find the NLX-INFO in our
1808 ;;; environment. Note that this is never called on the escape exits
1809 ;;; for CATCH and UNWIND-PROTECT, since the escape functions aren't
1811 (defun ir2-convert-exit (node block
)
1812 (declare (type exit node
) (type ir2-block block
))
1813 (let* ((nlx (exit-nlx-info node
))
1814 (loc (find-in-physenv nlx
(node-physenv node
)))
1815 (temp (make-stack-pointer-tn))
1816 (value (exit-value node
)))
1817 (if (nlx-info-safe-p nlx
)
1818 (vop value-cell-ref node block loc temp
)
1819 (emit-move node block loc temp
))
1821 (let ((locs (ir2-lvar-locs (lvar-info value
))))
1822 (vop unwind node block temp
(first locs
) (second locs
)))
1823 (let ((0-tn (emit-constant 0)))
1824 (vop unwind node block temp
0-tn
0-tn
))))
1828 ;;; %CLEANUP-POINT doesn't do anything except prevent the body from
1829 ;;; being entirely deleted.
1830 (defoptimizer (%cleanup-point ir2-convert
) (() node block
) node block
)
1832 ;;; This function invalidates a lexical exit on exiting from the
1833 ;;; dynamic extent. This is done by storing 0 into the indirect value
1834 ;;; cell that holds the closed unwind block.
1835 (defoptimizer (%lexical-exit-breakup ir2-convert
) ((info) node block
)
1836 (let ((nlx (lvar-value info
)))
1837 (when (nlx-info-safe-p nlx
)
1838 (vop value-cell-set node block
1839 (find-in-physenv nlx
(node-physenv node
))
1840 (emit-constant 0)))))
1842 ;;; We have to do a spurious move of no values to the result lvar so
1843 ;;; that lifetime analysis won't get confused.
1844 (defun ir2-convert-throw (node block
)
1845 (declare (type mv-combination node
) (type ir2-block block
))
1846 (let ((args (basic-combination-args node
)))
1847 (check-catch-tag-type (first args
))
1848 (vop* throw node block
1849 ((lvar-tn node block
(first args
))
1851 (ir2-lvar-locs (lvar-info (second args
)))
1854 (move-lvar-result node block
() (node-lvar node
))
1857 ;;; Emit code to set up a non-local exit. INFO is the NLX-INFO for the
1858 ;;; exit, and TAG is the lvar for the catch tag (if any.) We get at
1859 ;;; the target PC by passing in the label to the vop. The vop is
1860 ;;; responsible for building a return-PC object.
1861 (defun emit-nlx-start (node block info tag
)
1862 (declare (type node node
) (type ir2-block block
) (type nlx-info info
)
1863 (type (or lvar null
) tag
))
1864 (let* ((2info (nlx-info-info info
))
1865 (kind (cleanup-kind (nlx-info-cleanup info
)))
1866 (block-tn (physenv-live-tn
1868 (primitive-type-or-lose
1872 ((:unwind-protect
:block
:tagbody
)
1874 (node-physenv node
)))
1875 (res (make-stack-pointer-tn))
1876 (target-label (ir2-nlx-info-target 2info
)))
1878 (vop current-binding-pointer node block
1879 (car (ir2-nlx-info-dynamic-state 2info
)))
1880 (vop* save-dynamic-state node block
1882 ((reference-tn-list (cdr (ir2-nlx-info-dynamic-state 2info
)) t
)))
1883 (vop current-stack-pointer node block
(ir2-nlx-info-save-sp 2info
))
1887 (vop make-catch-block node block block-tn
1888 (lvar-tn node block tag
) target-label res
))
1889 ((:unwind-protect
:block
:tagbody
)
1890 (vop make-unwind-block node block block-tn target-label res
)))
1894 (if (nlx-info-safe-p info
)
1895 (emit-make-value-cell node block res
(ir2-nlx-info-home 2info
))
1896 (emit-move node block res
(ir2-nlx-info-home 2info
))))
1898 (vop set-unwind-protect node block block-tn
))
1903 ;;; Scan each of ENTRY's exits, setting up the exit for each lexical exit.
1904 (defun ir2-convert-entry (node block
)
1905 (declare (type entry node
) (type ir2-block block
))
1907 (dolist (exit (entry-exits node
))
1908 (let ((info (exit-nlx-info exit
)))
1910 (not (memq info nlxes
))
1911 (member (cleanup-kind (nlx-info-cleanup info
))
1912 '(:block
:tagbody
)))
1914 (emit-nlx-start node block info nil
)))))
1917 ;;; Set up the unwind block for these guys.
1918 (defoptimizer (%catch ir2-convert
) ((info-lvar tag
) node block
)
1919 (check-catch-tag-type tag
)
1920 (emit-nlx-start node block
(lvar-value info-lvar
) tag
))
1921 (defoptimizer (%unwind-protect ir2-convert
) ((info-lvar cleanup
) node block
)
1922 (declare (ignore cleanup
))
1923 (emit-nlx-start node block
(lvar-value info-lvar
) nil
))
1925 ;;; Emit the entry code for a non-local exit. We receive values and
1926 ;;; restore dynamic state.
1928 ;;; In the case of a lexical exit or CATCH, we look at the exit lvar's
1929 ;;; kind to determine which flavor of entry VOP to emit. If unknown
1930 ;;; values, emit the xxx-MULTIPLE variant to the lvar locs. If fixed
1931 ;;; values, make the appropriate number of temps in the standard
1932 ;;; values locations and use the other variant, delivering the temps
1933 ;;; to the lvar using MOVE-LVAR-RESULT.
1935 ;;; In the UNWIND-PROTECT case, we deliver the first register
1936 ;;; argument, the argument count and the argument pointer to our lvar
1937 ;;; as multiple values. These values are the block exited to and the
1938 ;;; values start and count.
1940 ;;; After receiving values, we restore dynamic state. Except in the
1941 ;;; UNWIND-PROTECT case, the values receiving restores the stack
1942 ;;; pointer. In an UNWIND-PROTECT cleanup, we want to leave the stack
1943 ;;; pointer alone, since the thrown values are still out there.
1944 (defoptimizer (%nlx-entry ir2-convert
) ((info-lvar) node block
)
1945 (let* ((info (lvar-value info-lvar
))
1946 (lvar (node-lvar node
))
1947 (2info (nlx-info-info info
))
1948 (top-loc (ir2-nlx-info-save-sp 2info
))
1949 (start-loc (make-nlx-entry-arg-start-location))
1950 (count-loc (make-arg-count-location))
1951 (target (ir2-nlx-info-target 2info
)))
1953 (ecase (cleanup-kind (nlx-info-cleanup info
))
1954 ((:catch
:block
:tagbody
)
1955 (let ((2lvar (and lvar
(lvar-info lvar
))))
1956 (if (and 2lvar
(eq (ir2-lvar-kind 2lvar
) :unknown
))
1957 (vop* nlx-entry-multiple node block
1958 (top-loc start-loc count-loc nil
)
1959 ((reference-tn-list (ir2-lvar-locs 2lvar
) t
))
1961 (let ((locs (standard-result-tns lvar
)))
1962 (vop* nlx-entry node block
1963 (top-loc start-loc count-loc nil
)
1964 ((reference-tn-list locs t
))
1967 (move-lvar-result node block locs lvar
)))))
1969 (let ((block-loc (standard-arg-location 0)))
1970 (vop uwp-entry node block target block-loc start-loc count-loc
)
1973 (list block-loc start-loc count-loc
)
1977 (when *collect-dynamic-statistics
*
1978 (vop count-me node block
*dynamic-counts-tn
*
1979 (block-number (ir2-block-block block
))))
1981 (vop* restore-dynamic-state node block
1982 ((reference-tn-list (cdr (ir2-nlx-info-dynamic-state 2info
)) nil
))
1984 (vop unbind-to-here node block
1985 (car (ir2-nlx-info-dynamic-state 2info
)))))
1987 ;;;; n-argument functions
1989 (macrolet ((def (name)
1990 `(defoptimizer (,name ir2-convert
) ((&rest args
) node block
)
1993 (/ sb
!vm
:large-object-size
1994 (* sb
!vm
:n-word-bytes
2)))
1995 ;; The VOPs will try to allocate all space at once
1996 ;; And it'll end up in large objects, and no conses
1997 ;; are welcome there.
1998 (ir2-convert-full-call node block
))
2000 (let* ((refs (reference-tn-list
2001 (loop for arg in args
2002 for tn
= (make-normal-tn *backend-t-primitive-type
*)
2004 (emit-move node block
(lvar-tn node block arg
) tn
)
2007 (lvar (node-lvar node
))
2008 (res (lvar-result-tns
2010 (list (primitive-type (specifier-type 'list
))))))
2011 (when (and lvar
(lvar-dynamic-extent lvar
))
2012 (vop current-stack-pointer node block
2013 (ir2-lvar-stack-pointer (lvar-info lvar
))))
2014 (vop* ,name node block
(refs) ((first res
) nil
)
2016 (move-lvar-result node block res lvar
)))))))
2021 (defoptimizer (mask-signed-field ir2-convert
) ((width x
) node block
)
2023 (when (constant-lvar-p width
)
2024 (case (lvar-value width
)
2025 (#.
(- sb
!vm
:n-word-bits sb
!vm
:n-fixnum-tag-bits
)
2026 (when (or (csubtypep (lvar-type x
)
2027 (specifier-type 'word
))
2028 (csubtypep (lvar-type x
)
2029 (specifier-type 'sb
!vm
:signed-word
)))
2030 (let* ((lvar (node-lvar node
))
2031 (temp (make-normal-tn
2032 (if (csubtypep (lvar-type x
)
2033 (specifier-type 'word
))
2034 (primitive-type-of most-positive-word
)
2036 (- (ash most-positive-word -
1))))))
2037 (results (lvar-result-tns
2039 (list (primitive-type-or-lose 'fixnum
)))))
2040 (emit-move node block
(lvar-tn node block x
) temp
)
2041 (vop sb
!vm
::move-from-word
/fixnum node block
2042 temp
(first results
))
2043 (move-lvar-result node block results lvar
)
2045 (#.sb
!vm
:n-word-bits
2046 (when (csubtypep (lvar-type x
) (specifier-type 'word
))
2047 (let* ((lvar (node-lvar node
))
2048 (temp (make-normal-tn
2049 (primitive-type-of most-positive-word
)))
2050 (results (lvar-result-tns
2052 (list (primitive-type
2053 (specifier-type 'sb
!vm
:signed-word
))))))
2054 (emit-move node block
(lvar-tn node block x
) temp
)
2055 (vop sb
!vm
::word-move node block
2056 temp
(first results
))
2057 (move-lvar-result node block results lvar
)
2059 (if (template-p (basic-combination-info node
))
2060 (ir2-convert-template node block
)
2061 (ir2-convert-full-call node block
))))
2063 ;; just a fancy identity
2064 (defoptimizer (%typep-wrapper ir2-convert
) ((value variable type
) node block
)
2065 (declare (ignore variable type
))
2066 (let* ((lvar (node-lvar node
))
2067 (results (lvar-result-tns lvar
(list (primitive-type-or-lose t
)))))
2068 (emit-move node block
(lvar-tn node block value
) (first results
))
2069 (move-lvar-result node block results lvar
)))
2071 ;;; An identity to avoid complaints about constant modification
2072 (defoptimizer (ltv-wrapper ir2-convert
) ((x) node block
)
2073 (let* ((lvar (node-lvar node
))
2074 (results (lvar-result-tns lvar
(list (primitive-type-or-lose t
)))))
2075 (emit-move node block
(lvar-tn node block x
) (first results
))
2076 (move-lvar-result node block results lvar
)))
2078 #-sb-xc-host
;; package-lock-violation-p is not present yet
2079 (defoptimizer (set ir2-hook
) ((symbol value
) node block
)
2080 (declare (ignore value block
))
2081 (when (constant-lvar-p symbol
)
2082 (let* ((symbol (lvar-value symbol
))
2083 (kind (info :variable
:kind symbol
)))
2084 (when (and (eq kind
:unknown
)
2085 (sb!impl
::package-lock-violation-p
(symbol-package symbol
) symbol
))
2086 (let ((*compiler-error-context
* node
))
2087 (compiler-warn "violating package lock on ~/sb-ext:print-symbol-with-prefix/"
2090 (defoptimizer (restart-point ir2-convert
) ((location) node block
)
2091 (setf (restart-location-label (lvar-value location
))
2092 (block-label (ir2-block-block block
))))
2094 ;;; Convert the code in a component into VOPs.
2095 (defun ir2-convert (component)
2096 (declare (type component component
))
2097 (let (#!+sb-dyncount
2098 (*dynamic-counts-tn
*
2099 (when *collect-dynamic-statistics
*
2101 (block-number (block-next (component-head component
))))
2102 (counts (make-array blocks
2103 :element-type
'(unsigned-byte 32)
2104 :initial-element
0))
2105 (info (make-dyncount-info
2106 :for
(component-name component
)
2107 :costs
(make-array blocks
2108 :element-type
'(unsigned-byte 32)
2111 (setf (ir2-component-dyncount-info (component-info component
))
2113 (emit-constant info
)
2114 (emit-constant counts
)))))
2116 (declare (type index num
))
2117 (do-ir2-blocks (2block component
)
2118 (let ((block (ir2-block-block 2block
)))
2119 (when (block-start block
)
2120 (setf (block-number block
) num
)
2122 (when *collect-dynamic-statistics
*
2123 (let ((first-node (block-start-node block
)))
2124 (unless (or (and (bind-p first-node
)
2125 (xep-p (bind-lambda first-node
)))
2127 (node-lvar first-node
))
2132 #!+sb-dyncount
*dynamic-counts-tn
* #!-sb-dyncount nil
2135 (let ((first-node (block-start-node block
)))
2136 (unless (or (and (bind-p first-node
)
2137 ;; Bind-nodes already have safepoints
2138 (eq (bind-lambda first-node
)
2139 (lambda-home (bind-lambda first-node
))))
2140 (and (valued-node-p first-node
)
2141 (node-lvar first-node
)
2143 (node-lvar first-node
))
2145 (when (and (rest (block-pred block
))
2147 (member (loop-kind (block-loop block
))
2148 '(:natural
:strange
))
2149 (eq block
(loop-head (block-loop block
)))
2150 (policy first-node
(< inhibit-safepoints
2)))
2151 (vop sb
!vm
::insert-safepoint first-node
2block
))))
2152 (ir2-convert-block block
)
2156 ;;; If necessary, emit a terminal unconditional branch to go to the
2157 ;;; successor block. If the successor is the component tail, then
2158 ;;; there isn't really any successor, but if the end is a non-tail
2159 ;;; call to a function that's not *known* to never return, then we
2160 ;;; emit an error trap just in case the function really does return.
2162 ;;; Trapping after known calls makes it easier to understand type
2163 ;;; derivation bugs at runtime: they show up as nil-fun-returned-error,
2164 ;;; rather than the execution of arbitrary code or error traps.
2165 (defun finish-ir2-block (block)
2166 (declare (type cblock block
))
2167 (let* ((2block (block-info block
))
2168 (last (block-last block
))
2169 (succ (block-succ block
)))
2171 (aver (singleton-p succ
))
2172 (let ((target (first succ
)))
2173 (cond ((eq target
(component-tail (block-component block
)))
2174 (when (and (basic-combination-p last
)
2175 (or (eq (basic-combination-kind last
) :full
)
2176 (and (eq (basic-combination-kind last
) :known
)
2177 (eq (basic-combination-info last
) :full
))))
2178 (let* ((fun (basic-combination-fun last
))
2179 (use (lvar-uses fun
))
2180 (name (and (ref-p use
)
2181 (leaf-has-source-name-p (ref-leaf use
))
2182 (leaf-source-name (ref-leaf use
))))
2183 (ftype (and (info :function
:info name
) ; only use the FTYPE if
2184 (proclaimed-ftype name
)))) ; NAME was DEFKNOWN
2185 (unless (or (node-tail-p last
)
2186 (policy last
(zerop safety
))
2187 (and (fun-type-p ftype
)
2188 (eq *empty-type
* (fun-type-returns ftype
))))
2189 (vop nil-fun-returned-error last
2block
2191 (emit-constant name
)
2192 (multiple-value-bind (tn named
)
2193 (fun-lvar-tn last
2block fun
)
2196 ((not (eq (ir2-block-next 2block
) (block-info target
)))
2197 (vop branch last
2block
(block-label target
)))
2199 (register-drop-thru target
))))))
2203 ;;; Convert the code in a block into VOPs.
2204 (defun ir2-convert-block (block)
2205 (declare (type cblock block
))
2206 (let ((2block (block-info block
)))
2207 (do-nodes (node lvar block
)
2211 (let ((2lvar (lvar-info lvar
)))
2212 ;; function REF in a local call is not annotated
2213 (when (and 2lvar
(not (eq (ir2-lvar-kind 2lvar
) :delayed
)))
2214 (ir2-convert-ref node
2block
)))))
2216 (let ((kind (basic-combination-kind node
)))
2219 (ir2-convert-local-call node
2block
))
2221 (ir2-convert-full-call node
2block
))
2223 (let* ((info (basic-combination-fun-info node
))
2224 (fun (fun-info-ir2-convert info
))
2225 (hook (fun-info-ir2-hook info
)))
2227 (funcall hook node
2block
))
2229 (funcall fun node
2block
))
2230 ((eq (basic-combination-info node
) :full
)
2231 (ir2-convert-full-call node
2block
))
2233 (ir2-convert-template node
2block
))))))))
2235 (when (lvar-info (if-test node
))
2236 (ir2-convert-if node
2block
)))
2238 (let ((fun (bind-lambda node
)))
2239 (when (eq (lambda-home fun
) fun
)
2240 (ir2-convert-bind node
2block
))))
2242 (ir2-convert-return node
2block
))
2244 (ir2-convert-set node
2block
))
2246 (ir2-convert-cast node
2block
))
2249 ((eq (basic-combination-kind node
) :local
)
2250 (ir2-convert-mv-bind node
2block
))
2251 ((eq (lvar-fun-name (basic-combination-fun node
))
2253 (ir2-convert-throw node
2block
))
2255 (ir2-convert-mv-call node
2block
))))
2257 (when (exit-entry node
)
2258 (ir2-convert-exit node
2block
)))
2260 (ir2-convert-entry node
2block
)))))
2262 (finish-ir2-block block
)