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-raw-p (dd index
)
103 (find index
(dd-slots dd
) :key
#'dsd-index
))))
105 (= (sb!kernel
::dsd-%raw-type slot
) -
1))))
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 (slot (position lvar
(cddr args
)))
122 (nth slot slot-specs
))))
123 (struct-slot-raw-p dd
126 (let* ((instance (lvar-type (car args
)))
127 (layout (and (structure-classoid-p instance
)
128 (classoid-layout instance
))))
129 (and (eq lvar
(car (last args
)))
130 (struct-slot-raw-p (and layout
131 (layout-info layout
))
132 (constant (cadr args
))))))
133 ((%special-bind %set-sap-ref-lispobj
134 %rplaca %rplacd cons list list
*
138 (call-full-like-p combination
))))))
140 (defun boxed-ref-p (ref)
141 (let* ((lvar (ref-lvar ref
))
142 (dest (lvar-dest lvar
)))
143 (cond ((basic-combination-p dest
)
144 (if (combination-p dest
)
145 (boxed-combination-ref-p dest lvar
)
146 (call-full-like-p dest
)))
148 (global-var-p (set-var dest
)))
150 (let* ((fun (return-lambda dest
))
151 (returns (tail-set-info (lambda-tail-set fun
))))
153 (eq (return-info-kind returns
) :unknown
)))))))
155 ;;; Convert a REF node. The reference must not be delayed.
156 (defun ir2-convert-ref (node block
)
157 (declare (type ref node
) (type ir2-block block
))
158 (let* ((lvar (node-lvar node
))
159 (leaf (ref-leaf node
))
160 (locs (lvar-result-tns
161 lvar
(list (primitive-type (leaf-type leaf
)))))
165 (let ((tn (find-in-physenv leaf
(node-physenv node
)))
166 (indirect (lambda-var-indirect leaf
))
167 (explicit (lambda-var-explicit-value-cell leaf
)))
169 ((and indirect explicit
)
170 (vop value-cell-ref node block tn res
))
172 (not (eq (node-physenv node
)
173 (lambda-physenv (lambda-var-home leaf
)))))
174 (let ((reffer (third (primitive-type-indirect-cell-type
175 (primitive-type (leaf-type leaf
))))))
177 (funcall reffer node block tn
(leaf-info leaf
) res
)
178 (vop ancestor-frame-ref node block tn
(leaf-info leaf
) res
))))
179 (t (emit-move node block tn res
)))))
181 (emit-move node block
(constant-tn leaf
(boxed-ref-p node
)) res
))
183 (ir2-convert-closure node block leaf res
))
185 (ir2-convert-global-var node block leaf res
)))
186 (move-lvar-result node block locs lvar
))
189 (defun ir2-convert-global-var (node block leaf res
)
190 (let ((unsafe (policy node
(zerop safety
)))
191 (name (leaf-source-name leaf
)))
192 (ecase (global-var-kind leaf
)
194 (aver (symbolp name
))
195 (let ((name-tn (emit-constant name
)))
196 (if (or unsafe
(always-boundp name
))
197 (vop fast-symbol-value node block name-tn res
)
198 (vop symbol-value node block name-tn res
))))
200 (aver (symbolp name
))
201 (let ((name-tn (emit-constant name
)))
202 (if (or unsafe
(always-boundp name
))
203 (vop fast-symbol-global-value node block name-tn res
)
204 (vop symbol-global-value node block name-tn res
))))
206 ;; In cross-compilation, testing (INFO :function :definition) is not
207 ;; sensible (or possible) but we can assume that things with fun-info
208 ;; will eventually be defined. If that's untrue, e.g. if we referred
209 ;; to #'DESCRIBE during cold-load, we'd just fix it locally by declaring
210 ;; DESCRIBE notinline.
211 ;; But in the target, more caution is warranted because users might
212 ;; DEFKNOWN a function but fail to define it. And they shouldn't be
213 ;; expected to understand the failure mode and the remedy.
214 (cond ((and #-sb-xc-host
(info :function
:definition name
)
215 (info :function
:info name
)
216 (let ((*lexenv
* (node-lexenv node
)))
217 (not (fun-lexically-notinline-p name
))))
218 ;; Known functions can be dumped without going through fdefns.
219 ;; But if NOTINLINEd, don't early-bind to the functional value
220 ;; because that disallows redefinition, including but not limited
221 ;; to encapsulations, which in turn makes TRACE not work, which
222 ;; leads to extreme frustration when debugging.
223 (emit-move node block
(make-load-time-constant-tn :known-fun name
)
226 (let ((fdefn-tn (make-load-time-constant-tn :fdefinition name
)))
228 (vop fdefn-fun node block fdefn-tn res
)
229 (vop safe-fdefn-fun node block fdefn-tn res
)))))))))
231 ;;; some sanity checks for a CLAMBDA passed to IR2-CONVERT-CLOSURE
232 (defun assertions-on-ir2-converted-clambda (clambda)
233 ;; This assertion was sort of an experiment. It would be nice and
234 ;; sane and easier to understand things if it were *always* true,
235 ;; but experimentally I observe that it's only *almost* always
236 ;; true. -- WHN 2001-01-02
238 (aver (eql (lambda-component clambda
)
239 (block-component (ir2-block-block ir2-block
))))
240 ;; Check for some weirdness which came up in bug
243 ;; The MAKE-LOAD-TIME-CONSTANT-TN call above puts an :ENTRY record
244 ;; into the IR2-COMPONENT-CONSTANTS table. The dump-a-COMPONENT
246 ;; * treats every HANDLEless :ENTRY record into a
248 ;; * expects every patch to correspond to an
249 ;; IR2-COMPONENT-ENTRIES record.
250 ;; The IR2-COMPONENT-ENTRIES records are set by ENTRY-ANALYZE
251 ;; walking over COMPONENT-LAMBDAS. Bug 138b arose because there
252 ;; was a HANDLEless :ENTRY record which didn't correspond to an
253 ;; IR2-COMPONENT-ENTRIES record. That problem is hard to debug
254 ;; when it's caught at dump time, so this assertion tries to catch
256 (aver (member clambda
257 (component-lambdas (lambda-component clambda
))))
258 ;; another bug-138-related issue: COMPONENT-NEW-FUNCTIONALS is
259 ;; used as a queue for stuff pending to do in IR1, and now that
260 ;; we're doing IR2 it should've been completely flushed (but
262 (aver (null (component-new-functionals (lambda-component clambda
))))
265 ;;; Emit code to load a function object implementing FUNCTIONAL into
266 ;;; RES. This gets interesting when the referenced function is a
267 ;;; closure: we must make the closure and move the closed-over values
270 ;;; FUNCTIONAL is either a :TOPLEVEL-XEP functional or the XEP lambda
271 ;;; for the called function, since local call analysis converts all
272 ;;; closure references. If a :TOPLEVEL-XEP, we know it is not a
275 ;;; If a closed-over LAMBDA-VAR has no refs (is deleted), then we
276 ;;; don't initialize that slot. This can happen with closures over
277 ;;; top level variables, where optimization of the closure deleted the
278 ;;; variable. Since we committed to the closure format when we
279 ;;; pre-analyzed the top level code, we just leave an empty slot.
280 (defun ir2-convert-closure (ref ir2-block functional res
)
281 (declare (type ref ref
)
282 (type ir2-block ir2-block
)
283 (type functional functional
)
286 (aver (not (eql (functional-kind functional
) :deleted
)))
287 (unless (leaf-info functional
)
288 (setf (leaf-info functional
)
289 (make-entry-info :name
290 (functional-debug-name functional
))))))
291 (let ((closure (etypecase functional
293 (assertions-on-ir2-converted-clambda functional
)
294 (physenv-closure (get-lambda-physenv functional
)))
296 (aver (eq (functional-kind functional
) :toplevel-xep
))
301 (let* ((physenv (node-physenv ref
))
302 (tn (find-in-physenv functional physenv
)))
303 (emit-move ref ir2-block tn res
)))
304 ;; we're about to emit a reference to a "closure" that's actually
305 ;; an inlinable global function.
306 ((and (global-var-p (setf global-var
307 (functional-inline-expanded functional
)))
308 (eq :global-function
(global-var-kind global-var
)))
309 (ir2-convert-global-var ref ir2-block global-var res
))
311 ;; if we're here, we should have either a toplevel-xep (some
312 ;; global scope function in a different component) or an external
313 ;; reference to the "closure"'s body.
315 (aver (memq (functional-kind functional
) '(:external
:toplevel-xep
)))
316 (let ((entry (make-load-time-constant-tn :entry functional
)))
317 (emit-move ref ir2-block entry res
))))))
320 (defun closure-initial-value (what this-env current-fp
)
321 (declare (type (or nlx-info lambda-var clambda
) what
)
322 (type physenv this-env
)
323 (type (or tn null
) current-fp
))
324 ;; If we have an indirect LAMBDA-VAR that does not require an
325 ;; EXPLICIT-VALUE-CELL, and is from this environment (not from being
326 ;; closed over), we need to store the current frame pointer.
327 (if (and (lambda-var-p what
)
328 (lambda-var-indirect what
)
329 (not (lambda-var-explicit-value-cell what
))
330 (eq (lambda-physenv (lambda-var-home what
))
333 (find-in-physenv what this-env
)))
335 (defoptimizer (%allocate-closures ltn-annotate
) ((leaves) node ltn-policy
)
336 (declare (ignore ltn-policy
))
337 (when (lvar-dynamic-extent leaves
)
338 (let ((info (make-ir2-lvar *backend-t-primitive-type
*)))
339 (setf (ir2-lvar-kind info
) :delayed
)
340 (setf (lvar-info leaves
) info
)
341 (setf (ir2-lvar-stack-pointer info
)
342 (make-stack-pointer-tn)))))
344 (defoptimizer (%allocate-closures ir2-convert
) ((leaves) call
2block
)
345 (let ((dx-p (lvar-dynamic-extent leaves
)))
348 (vop current-stack-pointer call
2block
349 (ir2-lvar-stack-pointer (lvar-info leaves
))))
350 (dolist (leaf (lvar-value leaves
))
351 (binding* ((xep (awhen (functional-entry-fun leaf
)
352 ;; if the xep's been deleted then we can skip it
353 (if (eq (functional-kind it
) :deleted
)
356 (nil (aver (xep-p xep
)))
357 (entry-info (lambda-info xep
) :exit-if-null
)
358 (tn (entry-info-closure-tn entry-info
) :exit-if-null
)
359 (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 (vop make-closure call
2block entry
(length closure
)
365 (loop for what in closure and n from
0 do
366 (unless (and (lambda-var-p what
)
367 (null (leaf-refs what
)))
368 ;; In LABELS a closure may refer to another closure
369 ;; in the same group, so we must be sure that we
370 ;; store a closure only after its creation.
372 ;; TODO: Here is a simple solution: we postpone
373 ;; putting of all closures after all creations
374 ;; (though it may require more registers).
376 (delayed (list tn
(find-in-physenv what this-env
) n
))
377 (let ((initial-value (closure-initial-value
380 (vop closure-init call
2block
382 ;; An initial-value of NIL means to stash
383 ;; the frame pointer... which requires a
385 (vop closure-init-from-fp call
2block tn n
)))))))))
386 (loop for
(tn what n
) in
(delayed)
387 do
(vop closure-init call
2block
391 ;;; Convert a SET node. If the NODE's LVAR is annotated, then we also
392 ;;; deliver the value to that lvar. If the var is a lexical variable
393 ;;; with no refs, then we don't actually set anything, since the
394 ;;; variable has been deleted.
395 (defun ir2-convert-set (node block
)
396 (declare (type cset node
) (type ir2-block block
))
397 (let* ((lvar (node-lvar node
))
398 (leaf (set-var node
))
399 (val (lvar-tn node block
(set-value node
)))
402 lvar
(list (primitive-type (leaf-type leaf
))))
406 (when (leaf-refs leaf
)
407 (let ((tn (find-in-physenv leaf
(node-physenv node
)))
408 (indirect (lambda-var-indirect leaf
))
409 (explicit (lambda-var-explicit-value-cell leaf
)))
411 ((and indirect explicit
)
412 (vop value-cell-set node block tn val
))
414 (not (eq (node-physenv node
)
415 (lambda-physenv (lambda-var-home leaf
)))))
416 (let ((setter (fourth (primitive-type-indirect-cell-type
417 (primitive-type (leaf-type leaf
))))))
419 (funcall setter node block tn val
(leaf-info leaf
))
420 (vop ancestor-frame-set node block tn val
(leaf-info leaf
)))))
421 (t (emit-move node block val tn
))))))
423 (aver (symbolp (leaf-source-name leaf
)))
424 (ecase (global-var-kind leaf
)
426 (vop set node block
(emit-constant (leaf-source-name leaf
)) val
))
428 (vop %set-symbol-global-value node
429 block
(emit-constant (leaf-source-name leaf
)) val
)))))
431 (emit-move node block val
(first locs
))
432 (move-lvar-result node block locs lvar
)))
435 ;;;; utilities for receiving fixed values
437 ;;; Return a TN that can be referenced to get the value of LVAR. LVAR
438 ;;; must be LTN-ANNOTATED either as a delayed leaf ref or as a fixed,
439 ;;; single-value lvar.
441 ;;; The primitive-type of the result will always be the same as the
442 ;;; IR2-LVAR-PRIMITIVE-TYPE, ensuring that VOPs are always called with
443 ;;; TNs that satisfy the operand primitive-type restriction. We may
444 ;;; have to make a temporary of the desired type and move the actual
445 ;;; lvar TN into it. This happens when we delete a type check in
446 ;;; unsafe code or when we locally know something about the type of an
447 ;;; argument variable.
448 (defun lvar-tn (node block lvar
)
449 (declare (type node node
) (type ir2-block block
) (type lvar lvar
))
450 (let* ((2lvar (lvar-info lvar
))
452 (ecase (ir2-lvar-kind 2lvar
)
454 (let ((ref (lvar-uses lvar
)))
455 (leaf-tn (ref-leaf ref
) (node-physenv ref
) (boxed-ref-p ref
))))
457 (aver (= (length (ir2-lvar-locs 2lvar
)) 1))
458 (first (ir2-lvar-locs 2lvar
)))))
459 (ptype (ir2-lvar-primitive-type 2lvar
)))
461 (cond ((eq (tn-primitive-type lvar-tn
) ptype
) lvar-tn
)
463 (let ((temp (make-normal-tn ptype
)))
464 (emit-move node block lvar-tn temp
)
467 ;;; This is similar to LVAR-TN, but hacks multiple values. We return
468 ;;; TNs holding the values of LVAR with PTYPES as their primitive
469 ;;; types. LVAR must be annotated for the same number of fixed values
470 ;;; are there are PTYPES.
472 ;;; If the lvar has a type check, check the values into temps and
473 ;;; return the temps. When we have more values than assertions, we
474 ;;; move the extra values with no check.
475 (defun lvar-tns (node block lvar ptypes
)
476 (declare (type node node
) (type ir2-block block
)
477 (type lvar lvar
) (list ptypes
))
478 (let* ((locs (ir2-lvar-locs (lvar-info lvar
)))
479 (nlocs (length locs
)))
480 (aver (= nlocs
(length ptypes
)))
482 (mapcar (lambda (from to-type
)
483 (if (eq (tn-primitive-type from
) to-type
)
485 (let ((temp (make-normal-tn to-type
)))
486 (emit-move node block from temp
)
491 ;;;; utilities for delivering values to lvars
493 ;;; Return a list of TNs with the specifier TYPES that can be used as
494 ;;; result TNs to evaluate an expression into LVAR. This is used
495 ;;; together with MOVE-LVAR-RESULT to deliver fixed values to
498 ;;; If the lvar isn't annotated (meaning the values are discarded) or
499 ;;; is unknown-values, then we make temporaries for each supplied
500 ;;; value, providing a place to compute the result in until we decide
501 ;;; what to do with it (if anything.)
503 ;;; If the lvar is fixed-values, and wants the same number of values
504 ;;; as the user wants to deliver, then we just return the
505 ;;; IR2-LVAR-LOCS. Otherwise we make a new list padded as necessary by
506 ;;; discarded TNs. We always return a TN of the specified type, using
507 ;;; the lvar locs only when they are of the correct type.
508 (defun lvar-result-tns (lvar types
)
509 (declare (type (or lvar null
) lvar
) (type list types
))
511 (mapcar #'make-normal-tn types
)
512 (let ((2lvar (lvar-info lvar
)))
513 (ecase (ir2-lvar-kind 2lvar
)
515 (let* ((locs (ir2-lvar-locs 2lvar
))
516 (nlocs (length locs
))
517 (ntypes (length types
)))
518 (if (and (= nlocs ntypes
)
519 (do ((loc locs
(cdr loc
))
520 (type types
(cdr type
)))
522 (unless (eq (tn-primitive-type (car loc
)) (car type
))
525 (mapcar (lambda (loc type
)
526 (if (eq (tn-primitive-type loc
) type
)
528 (make-normal-tn type
)))
531 (mapcar #'make-normal-tn
532 (subseq types nlocs
)))
536 (mapcar #'make-normal-tn types
))))))
538 ;;; Make the first N standard value TNs, returning them in a list.
539 (defun make-standard-value-tns (n)
540 (declare (type unsigned-byte n
))
543 (res (standard-arg-location i
)))
546 ;;; Return a list of TNs wired to the standard value passing
547 ;;; conventions that can be used to receive values according to the
548 ;;; unknown-values convention. This is used together with
549 ;;; MOVE-LVAR-RESULT for delivering unknown values to a fixed values
552 ;;; If the lvar isn't annotated, then we treat as 0-values, returning
553 ;;; an empty list of temporaries.
555 ;;; If the lvar is annotated, then it must be :FIXED.
556 (defun standard-result-tns (lvar)
557 (declare (type (or lvar null
) lvar
))
559 (let ((2lvar (lvar-info lvar
)))
560 (ecase (ir2-lvar-kind 2lvar
)
562 (make-standard-value-tns (length (ir2-lvar-locs 2lvar
))))))
565 ;;; Just move each SRC TN into the corresponding DEST TN, defaulting
566 ;;; any unsupplied source values to NIL. We let EMIT-MOVE worry about
567 ;;; doing the appropriate coercions.
568 (defun move-results-coerced (node block src dest
)
569 (declare (type node node
) (type ir2-block block
) (list src dest
))
570 (let ((nsrc (length src
))
571 (ndest (length dest
)))
572 (mapc (lambda (from to
)
574 (emit-move node block from to
)))
576 (append src
(make-list (- ndest nsrc
)
577 :initial-element
(emit-constant nil
)))
582 ;;; If necessary, emit coercion code needed to deliver the RESULTS to
583 ;;; the specified lvar. NODE and BLOCK provide context for emitting
584 ;;; code. Although usually obtained from STANDARD-RESULT-TNs or
585 ;;; LVAR-RESULT-TNs, RESULTS may be a list of any type or
588 ;;; If the lvar is fixed values, then move the results into the lvar
589 ;;; locations. If the lvar is unknown values, then do the moves into
590 ;;; the standard value locations, and use PUSH-VALUES to put the
591 ;;; values on the stack.
592 (defun move-lvar-result (node block results lvar
)
593 (declare (type node node
) (type ir2-block block
)
594 (list results
) (type (or lvar null
) lvar
))
596 (let ((2lvar (lvar-info lvar
)))
597 (ecase (ir2-lvar-kind 2lvar
)
599 (let ((locs (ir2-lvar-locs 2lvar
)))
600 (unless (eq locs results
)
601 (move-results-coerced node block results locs
))))
603 (let* ((nvals (length results
))
604 (locs (make-standard-value-tns nvals
)))
605 (move-results-coerced node block results locs
)
606 (vop* push-values node block
607 ((reference-tn-list locs nil
))
608 ((reference-tn-list (ir2-lvar-locs 2lvar
) t
))
613 (defun ir2-convert-cast (node block
)
614 (declare (type cast node
)
615 (type ir2-block block
))
616 (binding* ((lvar (node-lvar node
) :exit-if-null
)
617 (2lvar (lvar-info lvar
))
618 (value (cast-value node
))
619 (2value (lvar-info value
)))
620 (ecase (ir2-lvar-kind 2lvar
)
623 (aver (not (cast-type-check node
)))
624 (move-results-coerced node block
625 (ir2-lvar-locs 2value
)
626 (ir2-lvar-locs 2lvar
))))))
628 (defoptimizer (%check-bound ir2-hook
) ((array bound index
) node block
)
629 (declare (ignore block
))
630 (when (constant-lvar-p bound
)
631 (let* ((bound-type (specifier-type `(integer 0 (,(lvar-value bound
)))))
632 (index-type (lvar-type index
)))
633 (when (eq (type-intersection bound-type index-type
)
635 (let ((*compiler-error-context
* node
))
636 (compiler-warn "Derived type ~s is not a suitable index for ~s."
637 (type-specifier index-type
)
638 (type-specifier (lvar-type array
))))))))
640 ;;;; template conversion
642 ;;; Build a TN-REFS list that represents access to the values of the
643 ;;; specified list of lvars ARGS for TEMPLATE. Any :CONSTANT arguments
644 ;;; are returned in the second value as a list rather than being
645 ;;; accessed as a normal argument. NODE and BLOCK provide the context
646 ;;; for emitting any necessary type-checking code.
647 (defun reference-args (node block args template
)
648 (declare (type node node
) (type ir2-block block
) (list args
)
649 (type template template
))
650 (collect ((info-args))
653 (do ((args args
(cdr args
))
654 (types (template-arg-types template
) (cdr types
)))
656 (let ((type (first types
))
658 (if (and (consp type
) (eq (car type
) ':constant
))
659 (info-args (lvar-value arg
))
660 (let ((ref (reference-tn (lvar-tn node block arg
) nil
)))
662 (setf (tn-ref-across last
) ref
)
666 (values (the (or tn-ref null
) first
) (info-args)))))
668 ;;; Convert a conditional template. We try to exploit any
669 ;;; drop-through, but emit an unconditional branch afterward if we
670 ;;; fail. NOT-P is true if the sense of the TEMPLATE's test should be
672 (defun ir2-convert-conditional (node block template args info-args if not-p
)
673 (declare (type node node
) (type ir2-block block
)
674 (type template template
) (type (or tn-ref null
) args
)
675 (list info-args
) (type cif if
) (type boolean not-p
))
676 (let ((consequent (if-consequent if
))
677 (alternative (if-alternative if
))
678 (flags (and (consp (template-result-types template
))
679 (rest (template-result-types template
)))))
680 (aver (= (template-info-arg-count template
)
681 (+ (length info-args
)
684 (rotatef consequent alternative
)
686 (when (drop-thru-p if consequent
)
687 (rotatef consequent alternative
)
690 (emit-template node block template args nil
691 (list* (block-label consequent
) not-p
693 (if (drop-thru-p if alternative
)
694 (register-drop-thru alternative
)
695 (vop branch node block
(block-label alternative
))))
697 (emit-template node block template args nil info-args
)
698 (vop branch-if node block
(block-label consequent
) flags not-p
)
699 (if (drop-thru-p if alternative
)
700 (register-drop-thru alternative
)
701 (vop branch node block
(block-label alternative
)))))))
703 ;;; Convert an IF that isn't the DEST of a conditional template.
704 (defun ir2-convert-if (node block
)
705 (declare (type ir2-block block
) (type cif node
))
706 (let* ((test (if-test node
))
707 (test-ref (reference-tn (lvar-tn node block test
) nil
))
708 (nil-ref (reference-tn (emit-constant nil
) nil
)))
709 (setf (tn-ref-across test-ref
) nil-ref
)
710 (ir2-convert-conditional node block
(template-or-lose 'if-eq
)
711 test-ref
() node t
)))
713 ;;; Return a list of primitive-types that we can pass to LVAR-RESULT-TNS
714 ;;; describing the result types we want for a template call. We are really
715 ;;; only interested in the number of results required: in normal case
716 ;;; TEMPLATE-RESULTS-OK has already checked them.
717 (defun find-template-result-types (call rtypes
)
718 (let* ((type (node-derived-type call
))
720 (mapcar #'primitive-type
721 (if (args-type-p type
)
722 (append (args-type-required type
)
723 (args-type-optional type
))
725 (primitive-t *backend-t-primitive-type
*))
726 (mapcar (lambda (rtype)
727 (declare (ignore rtype
))
728 (or (pop types
) primitive-t
)) rtypes
)))
730 ;;; Return a list of TNs usable in a CALL to TEMPLATE delivering values to
731 ;;; LVAR. As an efficiency hack, we pick off the common case where the LVAR is
732 ;;; fixed values and has locations that satisfy the result restrictions. This
733 ;;; can fail when there is a type check or a values count mismatch.
734 (defun make-template-result-tns (call lvar rtypes
)
735 (declare (type combination call
) (type (or lvar null
) lvar
)
737 (let ((2lvar (when lvar
(lvar-info lvar
))))
738 (if (and 2lvar
(eq (ir2-lvar-kind 2lvar
) :fixed
))
739 (let ((locs (ir2-lvar-locs 2lvar
)))
740 (if (and (= (length rtypes
) (length locs
))
741 (do ((loc locs
(cdr loc
))
742 (rtypes rtypes
(cdr rtypes
)))
744 (unless (operand-restriction-ok
746 (tn-primitive-type (car loc
))
752 (find-template-result-types call rtypes
))))
755 (find-template-result-types call rtypes
)))))
757 ;;; Get the operands into TNs, make TN-REFs for them, and then call
758 ;;; the template emit function.
759 (defun ir2-convert-template (call block
)
760 (declare (type combination call
) (type ir2-block block
))
761 (let* ((template (combination-info call
))
762 (lvar (node-lvar call
))
763 (rtypes (template-result-types template
)))
764 (multiple-value-bind (args info-args
)
765 (reference-args call block
(combination-args call
) template
)
766 (aver (not (template-more-results-type template
)))
767 (if (template-conditional-p template
)
768 (ir2-convert-conditional call block template args info-args
769 (lvar-dest lvar
) nil
)
770 (let* ((results (make-template-result-tns call lvar rtypes
))
771 (r-refs (reference-tn-list results t
)))
772 (aver (= (length info-args
)
773 (template-info-arg-count template
)))
774 (when (and lvar
(lvar-dynamic-extent lvar
))
775 (vop current-stack-pointer call block
776 (ir2-lvar-stack-pointer (lvar-info lvar
))))
777 (when (emit-step-p call
)
778 (vop sb
!vm
::step-instrument-before-vop call block
))
780 (emit-template call block template args r-refs info-args
)
781 (emit-template call block template args r-refs
))
782 (move-lvar-result call block results lvar
)))))
785 ;;; We don't have to do much because operand count checking is done by
786 ;;; IR1 conversion. The only difference between this and the function
787 ;;; case of IR2-CONVERT-TEMPLATE is that there can be codegen-info
789 (defoptimizer (%%primitive ir2-convert
) ((template info
&rest args
) call block
)
790 (declare (ignore args
))
791 (let* ((template (lvar-value template
))
792 (info (lvar-value info
))
793 (lvar (node-lvar call
))
794 (rtypes (template-result-types template
))
795 (results (make-template-result-tns call lvar rtypes
))
796 (r-refs (reference-tn-list results t
)))
797 (multiple-value-bind (args info-args
)
798 (reference-args call block
(cddr (combination-args call
)) template
)
799 (aver (not (template-more-results-type template
)))
800 (aver (not (template-conditional-p template
)))
801 (aver (null info-args
))
804 (emit-template call block template args r-refs info
)
805 (emit-template call block template args r-refs
))
807 (move-lvar-result call block results lvar
)))
810 (defoptimizer (%%primitive derive-type
) ((template info
&rest args
))
811 (declare (ignore info args
))
812 (let ((type (template-type (lvar-value template
))))
813 (if (fun-type-p type
)
814 (fun-type-returns type
)
819 ;;; Convert a LET by moving the argument values into the variables.
820 ;;; Since a LET doesn't have any passing locations, we move the
821 ;;; arguments directly into the variables. We must also allocate any
822 ;;; indirect value cells, since there is no function prologue to do
824 (defun ir2-convert-let (node block fun
)
825 (declare (type combination node
) (type ir2-block block
) (type clambda fun
))
826 (mapc (lambda (var arg
)
828 (let ((src (lvar-tn node block arg
))
829 (dest (leaf-info var
)))
830 (if (and (lambda-var-indirect var
)
831 (lambda-var-explicit-value-cell var
))
832 (emit-make-value-cell node block src dest
)
833 (emit-move node block src dest
)))))
834 (lambda-vars fun
) (basic-combination-args node
))
837 ;;; Emit any necessary moves into assignment temps for a local call to
838 ;;; FUN. We return two lists of TNs: TNs holding the actual argument
839 ;;; values, and (possibly EQ) TNs that are the actual destination of
840 ;;; the arguments. When necessary, we allocate temporaries for
841 ;;; arguments to preserve parallel assignment semantics. These lists
842 ;;; exclude unused arguments and include implicit environment
843 ;;; arguments, i.e. they exactly correspond to the arguments passed.
845 ;;; OLD-FP is the TN currently holding the value we want to pass as
846 ;;; OLD-FP. If null, then the call is to the same environment (an
847 ;;; :ASSIGNMENT), so we only move the arguments, and leave the
848 ;;; environment alone.
850 ;;; CLOSURE-FP is for calling a closure that has "implicit" value
851 ;;; cells (stored in the allocating stack frame), and is the frame
852 ;;; pointer TN to use for values allocated in the outbound stack
853 ;;; frame. This is distinct from OLD-FP for the specific case of a
855 (defun emit-psetq-moves (node block fun old-fp
&optional
(closure-fp old-fp
))
856 (declare (type combination node
) (type ir2-block block
) (type clambda fun
)
857 (type (or tn null
) old-fp closure-fp
))
858 (let ((actuals (mapcar (lambda (x)
860 (lvar-tn node block x
)))
861 (combination-args node
))))
864 (dolist (var (lambda-vars fun
))
865 (let ((actual (pop actuals
))
866 (loc (leaf-info var
)))
869 ((and (lambda-var-indirect var
)
870 (lambda-var-explicit-value-cell var
))
872 (make-normal-tn *backend-t-primitive-type
*)))
873 (emit-make-value-cell node block actual temp
)
875 ((member actual
(locs))
876 (let ((temp (make-normal-tn (tn-primitive-type loc
))))
877 (emit-move node block actual temp
)
884 (let ((this-1env (node-physenv node
))
885 (called-env (physenv-info (lambda-physenv fun
))))
886 (dolist (thing (ir2-physenv-closure called-env
))
887 (temps (closure-initial-value (car thing
) this-1env closure-fp
))
890 (locs (ir2-physenv-old-fp called-env
))))
892 (values (temps) (locs)))))
894 ;;; A tail-recursive local call is done by emitting moves of stuff
895 ;;; into the appropriate passing locations. After setting up the args
896 ;;; and environment, we just move our return-pc into the called
897 ;;; function's passing location.
898 (defun ir2-convert-tail-local-call (node block fun
)
899 (declare (type combination node
) (type ir2-block block
) (type clambda fun
))
900 (let ((this-env (physenv-info (node-physenv node
)))
901 (current-fp (make-stack-pointer-tn)))
902 (multiple-value-bind (temps locs
)
903 (emit-psetq-moves node block fun
904 (ir2-physenv-old-fp this-env
) current-fp
)
906 ;; If we're about to emit a move from CURRENT-FP then we need to
908 (when (find current-fp temps
)
909 (vop current-fp node block current-fp
))
911 (mapc (lambda (temp loc
)
912 (emit-move node block temp loc
))
915 (emit-move node block
916 (ir2-physenv-return-pc this-env
)
917 (ir2-physenv-return-pc-pass
919 (lambda-physenv fun
)))))
923 ;;; Convert an :ASSIGNMENT call. This is just like a tail local call,
924 ;;; except that the caller and callee environment are the same, so we
925 ;;; don't need to mess with the environment locations, return PC, etc.
926 (defun ir2-convert-assignment (node block fun
)
927 (declare (type combination node
) (type ir2-block block
) (type clambda fun
))
928 (multiple-value-bind (temps locs
) (emit-psetq-moves node block fun nil
)
930 (mapc (lambda (temp loc
)
931 (emit-move node block temp loc
))
935 ;;; Do stuff to set up the arguments to a non-tail local call
936 ;;; (including implicit environment args.) We allocate a frame
937 ;;; (returning the FP and NFP), and also compute the TN-REFS list for
938 ;;; the values to pass and the list of passing location TNs.
939 (defun ir2-convert-local-call-args (node block fun
)
940 (declare (type combination node
) (type ir2-block block
) (type clambda fun
))
941 (let ((fp (make-stack-pointer-tn))
942 (nfp (make-number-stack-pointer-tn))
943 (old-fp (make-stack-pointer-tn)))
944 (multiple-value-bind (temps locs
)
945 (emit-psetq-moves node block fun old-fp
)
946 (vop current-fp node block old-fp
)
947 (vop allocate-frame node block
948 (physenv-info (lambda-physenv fun
))
950 (values fp nfp temps
(mapcar #'make-alias-tn locs
)))))
952 ;;; Handle a non-TR known-values local call. We emit the call, then
953 ;;; move the results to the lvar's destination.
954 (defun ir2-convert-local-known-call (node block fun returns lvar start
)
955 (declare (type node node
) (type ir2-block block
) (type clambda fun
)
956 (type return-info returns
) (type (or lvar null
) lvar
)
958 (multiple-value-bind (fp nfp temps arg-locs
)
959 (ir2-convert-local-call-args node block fun
)
960 (let ((locs (return-info-locations returns
)))
961 (vop* known-call-local node block
962 (fp nfp
(reference-tn-list temps nil
))
963 ((reference-tn-list locs t
))
964 arg-locs
(physenv-info (lambda-physenv fun
)) start
)
965 (move-lvar-result node block locs lvar
)))
968 ;;; Handle a non-TR unknown-values local call. We do different things
969 ;;; depending on what kind of values the lvar wants.
971 ;;; If LVAR is :UNKNOWN, then we use the "multiple-" variant, directly
972 ;;; specifying the lvar's LOCS as the VOP results so that we don't
973 ;;; have to do anything after the call.
975 ;;; Otherwise, we use STANDARD-RESULT-TNS to get wired result TNs, and
976 ;;; then call MOVE-LVAR-RESULT to do any necessary type checks or
978 (defun ir2-convert-local-unknown-call (node block fun lvar start
)
979 (declare (type node node
) (type ir2-block block
) (type clambda fun
)
980 (type (or lvar null
) lvar
) (type label start
))
981 (multiple-value-bind (fp nfp temps arg-locs
)
982 (ir2-convert-local-call-args node block fun
)
983 (let ((2lvar (and lvar
(lvar-info lvar
)))
984 (env (physenv-info (lambda-physenv fun
)))
985 (temp-refs (reference-tn-list temps nil
)))
986 (if (and 2lvar
(eq (ir2-lvar-kind 2lvar
) :unknown
))
987 (vop* multiple-call-local node block
(fp nfp temp-refs
)
988 ((reference-tn-list (ir2-lvar-locs 2lvar
) t
))
990 (let ((locs (standard-result-tns lvar
)))
991 (vop* call-local node block
993 ((reference-tn-list locs t
))
994 arg-locs env start
(length locs
))
995 (move-lvar-result node block locs lvar
)))))
998 ;;; Dispatch to the appropriate function, depending on whether we have
999 ;;; a let, tail or normal call. If the function doesn't return, call
1000 ;;; it using the unknown-value convention. We could compile it as a
1001 ;;; tail call, but that might seem confusing in the debugger.
1002 (defun ir2-convert-local-call (node block
)
1003 (declare (type combination node
) (type ir2-block block
))
1004 (let* ((fun (ref-leaf (lvar-uses (basic-combination-fun node
))))
1005 (kind (functional-kind fun
)))
1006 (cond ((eq kind
:let
)
1007 (ir2-convert-let node block fun
))
1008 ((eq kind
:assignment
)
1009 (ir2-convert-assignment node block fun
))
1011 (ir2-convert-tail-local-call node block fun
))
1013 (let ((start (block-trampoline (lambda-block fun
)))
1014 (returns (tail-set-info (lambda-tail-set fun
)))
1015 (lvar (node-lvar node
)))
1017 (return-info-kind returns
)
1020 (ir2-convert-local-unknown-call node block fun lvar start
))
1022 (ir2-convert-local-known-call node block fun returns
1028 ;;; Given a function lvar FUN, return (VALUES TN-TO-CALL NAMED-P),
1029 ;;; where TN-TO-CALL is a TN holding the thing that we call NAMED-P is
1030 ;;; true if the thing is named (false if it is a function).
1032 ;;; There are two interesting non-named cases:
1033 ;;; -- We know it's a function. No check needed: return the
1035 ;;; -- We don't know what it is.
1036 (defun fun-lvar-tn (node block lvar
)
1037 (declare (ignore node block
))
1038 (declare (type lvar lvar
))
1039 (let ((2lvar (lvar-info lvar
)))
1040 (if (eq (ir2-lvar-kind 2lvar
) :delayed
)
1041 (let ((name (lvar-fun-name lvar t
)))
1043 (values (make-load-time-constant-tn :fdefinition name
) name
))
1044 (let* ((locs (ir2-lvar-locs 2lvar
))
1046 (function-ptype (primitive-type-or-lose 'function
)))
1047 (aver (and (eq (ir2-lvar-kind 2lvar
) :fixed
)
1048 (= (length locs
) 1)))
1049 (aver (eq (tn-primitive-type loc
) function-ptype
))
1050 (values loc nil
)))))
1052 ;;; Set up the args to NODE in the current frame, and return a TN-REF
1053 ;;; list for the passing locations.
1054 (defun move-tail-full-call-args (node block
)
1055 (declare (type combination node
) (type ir2-block block
))
1056 (let ((args (basic-combination-args node
))
1059 (dotimes (num (length args
))
1060 (let ((loc (standard-arg-location num
)))
1061 (emit-move node block
(lvar-tn node block
(elt args num
)) loc
)
1062 (let ((ref (reference-tn loc nil
)))
1064 (setf (tn-ref-across last
) ref
)
1069 ;;; Move the arguments into the passing locations and do a (possibly
1070 ;;; named) tail call.
1071 (defun ir2-convert-tail-full-call (node block
)
1072 (declare (type combination node
) (type ir2-block block
))
1073 (let* ((env (physenv-info (node-physenv node
)))
1074 (args (basic-combination-args node
))
1075 (nargs (length args
))
1076 (pass-refs (move-tail-full-call-args node block
))
1077 (old-fp (ir2-physenv-old-fp env
))
1078 (return-pc (ir2-physenv-return-pc env
)))
1080 (multiple-value-bind (fun-tn named
)
1081 (fun-lvar-tn node block
(basic-combination-fun node
))
1083 (vop* tail-call-named node block
1084 (fun-tn old-fp return-pc pass-refs
)
1088 (vop* tail-call node block
1089 (fun-tn old-fp return-pc pass-refs
)
1092 (emit-step-p node
)))))
1096 ;;; like IR2-CONVERT-LOCAL-CALL-ARGS, only different
1097 (defun ir2-convert-full-call-args (node block
)
1098 (declare (type combination node
) (type ir2-block block
))
1099 (let* ((args (basic-combination-args node
))
1100 (fp (make-stack-pointer-tn))
1101 (nargs (length args
)))
1102 (vop allocate-full-call-frame node block nargs fp
)
1106 (dotimes (num nargs
)
1107 (locs (standard-arg-location num
))
1108 (let ((ref (reference-tn (lvar-tn node block
(elt args num
))
1111 (setf (tn-ref-across last
) ref
)
1115 (values fp first
(locs) nargs
)))))
1117 ;;; Do full call when a fixed number of values are desired. We make
1118 ;;; STANDARD-RESULT-TNS for our lvar, then deliver the result using
1119 ;;; MOVE-LVAR-RESULT. We do named or normal call, as appropriate.
1120 (defun ir2-convert-fixed-full-call (node block
)
1121 (declare (type combination node
) (type ir2-block block
))
1122 (multiple-value-bind (fp args arg-locs nargs
)
1123 (ir2-convert-full-call-args node block
)
1124 (let* ((lvar (node-lvar node
))
1125 (locs (standard-result-tns lvar
))
1126 (loc-refs (reference-tn-list locs t
))
1127 (nvals (length locs
)))
1128 (multiple-value-bind (fun-tn named
)
1129 (fun-lvar-tn node block
(basic-combination-fun node
))
1131 (vop* call-named node block
(fp fun-tn args
) (loc-refs)
1132 arg-locs nargs nvals
(emit-step-p node
))
1133 (vop* call node block
(fp fun-tn args
) (loc-refs)
1134 arg-locs nargs nvals
(emit-step-p node
)))
1135 (move-lvar-result node block locs lvar
))))
1138 ;;; Do full call when unknown values are desired.
1139 (defun ir2-convert-multiple-full-call (node block
)
1140 (declare (type combination node
) (type ir2-block block
))
1141 (multiple-value-bind (fp args arg-locs nargs
)
1142 (ir2-convert-full-call-args node block
)
1143 (let* ((lvar (node-lvar node
))
1144 (locs (ir2-lvar-locs (lvar-info lvar
)))
1145 (loc-refs (reference-tn-list locs t
)))
1146 (multiple-value-bind (fun-tn named
)
1147 (fun-lvar-tn node block
(basic-combination-fun node
))
1149 (vop* multiple-call-named node block
(fp fun-tn args
) (loc-refs)
1150 arg-locs nargs
(emit-step-p node
))
1151 (vop* multiple-call node block
(fp fun-tn args
) (loc-refs)
1152 arg-locs nargs
(emit-step-p node
))))))
1155 ;;; stuff to check in PONDER-FULL-CALL
1157 ;;; These came in handy when troubleshooting cold boot after making
1158 ;;; major changes in the package structure: various transforms and
1159 ;;; VOPs and stuff got attached to the wrong symbol, so that
1160 ;;; references to the right symbol were bogusly translated as full
1161 ;;; calls instead of primitives, sending the system off into infinite
1162 ;;; space. Having a report on all full calls generated makes it easier
1163 ;;; to figure out what form caused the problem this time.
1164 (declaim (type (member :minimal
:detailed
:very-detailed
:maximal
)
1165 *track-full-called-fnames
*))
1166 (defvar *track-full-called-fnames
* :minimal
)
1168 ;;; Do some checks (and store some notes relevant for future checks)
1170 ;;; * Is this a full call to something we have reason to know should
1171 ;;; never be full called? (Except as of sbcl-0.7.18 or so, we no
1172 ;;; longer try to ensure this behavior when *FAILURE-P* has already
1174 (defun ponder-full-call (node)
1175 (let* ((lvar (basic-combination-fun node
))
1176 (fname (lvar-fun-name lvar t
)))
1177 (declare (type (or symbol cons
) fname
))
1179 ;; Warn about cross-compiling certain full-calls,
1180 ;; as it is indicative of dependency order problems.
1182 (let ((compname (component-name (node-component node
))))
1183 ;; Don't care too much about macro performance.
1184 (unless (and (stringp compname
) (string/= compname
"DEFMACRO"))
1185 ;; Catch FOO and (SETF FOO) both.
1186 (let ((stem (if (atom fname
) fname
(second fname
))))
1188 sb-cold
::*full-calls-to-warn-about
*
1190 (warn "Full call to ~S" fname
)))))
1192 (let* ((inlineable-p (not (let ((*lexenv
* (node-lexenv node
)))
1193 (fun-lexically-notinline-p fname
))))
1194 (inlineable-bit (if inlineable-p
1 0))
1195 (cell (info :function
:emitted-full-calls fname
)))
1197 ;; The low bit indicates whether any not-NOTINLINE call was seen.
1198 ;; The next-lowest bit is magic. Refer to %COMPILER-DEFMACRO
1199 ;; and WARN-IF-INLINE-FAILED/CALL for the pertinent logic.
1200 (setf cell
(list (logior 4 inlineable-bit
))
1201 (info :function
:emitted-full-calls fname
) cell
)
1202 (incf (car cell
) (+ 4 (if (oddp (car cell
)) 0 inlineable-bit
))))
1203 ;; If the full call was wanted, don't record anything.
1204 ;; (This was originally for debugging SBCL self-compilation)
1207 (warn-if-inline-failed/call fname
(node-lexenv node
) cell
))
1208 (case *track-full-called-fnames
*
1210 (when (boundp 'sb
!xc
:*compile-file-pathname
*)
1211 (pushnew sb
!xc
:*compile-file-pathname
* (cdr cell
)
1214 (pushnew (component-name *component-being-compiled
*)
1215 (cdr cell
) :test
#'equalp
)))))
1217 ;; Special mode, usually only for the cross-compiler
1218 ;; and only with the feature enabled.
1219 #!+sb-show
(when (eq *track-full-called-fnames
* :maximal
)
1220 (/show
"converting full call to named function" fname
)
1221 (/show
(basic-combination-args node
))
1222 (/show
(policy node speed
) (policy node safety
))
1223 (/show
(policy node compilation-speed
))
1224 (let ((arg-types (mapcar (lambda (lvar)
1228 (basic-combination-args node
))))
1231 ;; When illegal code is compiled, all sorts of perverse paths
1232 ;; through the compiler can be taken, and it's much harder -- and
1233 ;; probably pointless -- to guarantee that always-optimized-away
1234 ;; functions are actually optimized away. Thus, we skip the check
1237 ;; check to see if we know anything about the function
1238 (let ((info (info :function
:info fname
)))
1239 ;; if we know something, check to see if the full call was valid
1240 (when (and info
(ir1-attributep (fun-info-attributes info
)
1241 always-translatable
))
1242 (/show
(policy node speed
) (policy node safety
))
1243 (/show
(policy node compilation-speed
))
1244 (bug "full call to ~S" fname
))))
1247 (aver (legal-fun-name-p fname
))))) ;; FIXME: needless check?
1249 ;;; If the call is in a tail recursive position and the return
1250 ;;; convention is standard, then do a tail full call. If one or fewer
1251 ;;; values are desired, then use a single-value call, otherwise use a
1252 ;;; multiple-values call.
1253 (defun ir2-convert-full-call (node block
)
1254 (declare (type combination node
) (type ir2-block block
))
1255 (ponder-full-call node
)
1256 (cond ((node-tail-p node
)
1257 (ir2-convert-tail-full-call node block
))
1258 ((let ((lvar (node-lvar node
)))
1260 (eq (ir2-lvar-kind (lvar-info lvar
)) :unknown
)))
1261 (ir2-convert-multiple-full-call node block
))
1263 (ir2-convert-fixed-full-call node block
)))
1266 ;;;; entering functions
1268 #!+precise-arg-count-error
1269 (defun xep-verify-arg-count (node block fun arg-count-location
)
1270 (when (policy fun
(plusp verify-arg-count
))
1271 (let* ((ef (functional-entry-fun fun
))
1272 (optional (optional-dispatch-p ef
))
1274 (optional-dispatch-min-args ef
)))
1275 (max (cond ((not optional
)
1276 (1- (length (lambda-vars fun
))))
1278 (not (optional-dispatch-more-entry ef
)))
1279 (optional-dispatch-max-args ef
)))))
1280 (unless (and (eql min
0) (not max
))
1281 (vop verify-arg-count node block
1287 ;;; Do all the stuff that needs to be done on XEP entry:
1288 ;;; -- Create frame.
1289 ;;; -- Copy any more arg.
1290 ;;; -- Set up the environment, accessing any closure variables.
1291 ;;; -- Move args from the standard passing locations to their internal
1293 (defun init-xep-environment (node block fun
)
1294 (declare (type bind node
) (type ir2-block block
) (type clambda fun
))
1295 (let ((start-label (entry-info-offset (leaf-info fun
)))
1296 (env (physenv-info (node-physenv node
)))
1298 (let ((ef (functional-entry-fun fun
)))
1299 (vop xep-allocate-frame node block start-label
)
1300 ;; Arg verification needs to be done before the stack pointer is adjusted
1301 ;; so that the extra arguments are still present when the error is signalled
1302 (let ((verified (unless (eq (functional-kind fun
) :toplevel
)
1303 (setf arg-count-tn
(make-arg-count-location))
1304 #!+precise-arg-count-error
1305 (xep-verify-arg-count node block fun arg-count-tn
))))
1307 (declare (ignore verified
))
1308 (cond ((and (optional-dispatch-p ef
) (optional-dispatch-more-entry ef
))
1309 ;; COPY-MORE-ARG should handle SP adjustemnt, but it
1310 ;; isn't done on all targets.
1311 #!-precise-arg-count-error
1312 (vop xep-setup-sp node block
)
1313 (vop copy-more-arg node block
(optional-dispatch-max-args ef
)
1314 #!+x86-64 verified
))
1316 (vop xep-setup-sp node block
))))
1317 (when (ir2-physenv-closure env
)
1318 (let ((closure (make-normal-tn *backend-t-primitive-type
*)))
1319 (when (policy fun
(> store-closure-debug-pointer
1))
1320 ;; Save the closure pointer on the stack.
1321 (let ((closure-save (make-representation-tn
1322 *backend-t-primitive-type
*
1323 (sc-number-or-lose 'sb
!vm
::control-stack
))))
1324 (vop setup-closure-environment node block start-label
1326 (setf (ir2-physenv-closure-save-tn env
) closure-save
)
1327 (component-live-tn closure-save
)))
1328 (vop setup-closure-environment node block start-label closure
)
1330 (dolist (loc (ir2-physenv-closure env
))
1331 (vop closure-ref node block closure
(incf n
) (cdr loc
)))))))
1332 (unless (eq (functional-kind fun
) :toplevel
)
1333 (let ((vars (lambda-vars fun
))
1335 (when (leaf-refs (first vars
))
1336 (emit-move node block arg-count-tn
(leaf-info (first vars
))))
1337 (dolist (arg (rest vars
))
1338 (when (leaf-refs arg
)
1339 (let ((pass (standard-arg-location n
))
1340 (home (leaf-info arg
)))
1341 (if (and (lambda-var-indirect arg
)
1342 (lambda-var-explicit-value-cell arg
))
1343 (emit-make-value-cell node block pass home
)
1344 (emit-move node block pass home
))))
1347 (emit-move node block
(make-old-fp-passing-location t
)
1348 (ir2-physenv-old-fp env
)))
1352 ;;; Emit function prolog code. This is only called on bind nodes for
1353 ;;; functions that allocate environments. All semantics of let calls
1354 ;;; are handled by IR2-CONVERT-LET.
1356 ;;; If not an XEP, all we do is move the return PC from its passing
1357 ;;; location, since in a local call, the caller allocates the frame
1358 ;;; and sets up the arguments.
1360 #!+unwind-to-frame-and-call-vop
1361 (defun save-bsp (node block env
)
1362 ;; Save BSP on stack so that the binding environment can be restored
1363 ;; when restarting frames.
1364 ;; This is done inside functions, which leaves XEPs without saved
1365 ;; BSP, though the code in XEPs doesn't bind any variables, it can
1366 ;; call arbitrary code through the SATISFIES declaration.
1367 ;; And functions called by SATISFIES are not inlined, except for
1368 ;; source transforms, but these usually do not bind anything.
1369 ;; Thus when restarting it needs to check that the interrupt was in
1372 ;; It could be saved from the XEP, but some functions have both
1373 ;; external and internal entry points, so it will be saved twice.
1374 (let ((temp (make-normal-tn *backend-t-primitive-type
*))
1375 (bsp-save-tn (make-representation-tn
1376 *backend-t-primitive-type
*
1377 (sc-number-or-lose 'sb
!vm
::control-stack
))))
1378 (vop current-binding-pointer node block temp
)
1379 (emit-move node block temp bsp-save-tn
)
1380 (setf (ir2-physenv-bsp-save-tn env
) bsp-save-tn
)
1381 (component-live-tn bsp-save-tn
)))
1383 (defun ir2-convert-bind (node block
)
1384 (declare (type bind node
) (type ir2-block block
))
1385 (let* ((fun (bind-lambda node
))
1386 (env (physenv-info (lambda-physenv fun
))))
1387 (aver (member (functional-kind fun
)
1388 '(nil :external
:optional
:toplevel
:cleanup
)))
1391 (init-xep-environment node block fun
)
1393 (when *collect-dynamic-statistics
*
1394 (vop count-me node block
*dynamic-counts-tn
*
1395 (block-number (ir2-block-block block
)))))
1396 ((policy fun
(> store-closure-debug-pointer
1))
1397 ;; Propagate the location of the closure pointer from the
1398 ;; enclosing functions. (FIXME: Should make sure that this
1399 ;; handles closures inside closures correctly). [remark by JES]
1400 (let* ((entry-fun (lambda-entry-fun fun
)))
1402 (let ((2env (physenv-info (lambda-physenv fun
)))
1403 (entry-2env (physenv-info (lambda-physenv entry-fun
))))
1404 (setf (ir2-physenv-closure-save-tn 2env
)
1405 (ir2-physenv-closure-save-tn entry-2env
)))))))
1409 (ir2-physenv-return-pc-pass env
)
1410 (ir2-physenv-return-pc env
))
1411 #!+unwind-to-frame-and-call-vop
1412 (when (and (lambda-allow-instrumenting fun
)
1413 (not (lambda-inline-expanded fun
))
1414 (policy fun
(>= insert-debug-catch
1)))
1415 (save-bsp node block env
))
1417 (let ((lab (gen-label)))
1418 (setf (ir2-physenv-environment-start env
) lab
)
1419 (vop note-environment-start node block lab
)
1421 (unless (policy fun
(>= inhibit-safepoints
2))
1422 (vop sb
!vm
::insert-safepoint node block
))))
1426 ;;;; function return
1428 ;;; Do stuff to return from a function with the specified values and
1429 ;;; convention. If the return convention is :FIXED and we aren't
1430 ;;; returning from an XEP, then we do a known return (letting
1431 ;;; representation selection insert the correct move-arg VOPs.)
1432 ;;; Otherwise, we use the unknown-values convention. If there is a
1433 ;;; fixed number of return values, then use RETURN, otherwise use
1434 ;;; RETURN-MULTIPLE.
1435 (defun ir2-convert-return (node block
)
1436 (declare (type creturn node
) (type ir2-block block
))
1437 (let* ((lvar (return-result node
))
1438 (2lvar (lvar-info lvar
))
1439 (lvar-kind (ir2-lvar-kind 2lvar
))
1440 (fun (return-lambda node
))
1441 (env (physenv-info (lambda-physenv fun
)))
1442 (old-fp (ir2-physenv-old-fp env
))
1443 (return-pc (ir2-physenv-return-pc env
))
1444 (returns (tail-set-info (lambda-tail-set fun
))))
1446 ((and (eq (return-info-kind returns
) :fixed
)
1448 (let ((locs (lvar-tns node block lvar
1449 (return-info-types returns
))))
1450 (vop* known-return node block
1451 (old-fp return-pc
(reference-tn-list locs nil
))
1453 (return-info-locations returns
))))
1454 ((eq lvar-kind
:fixed
)
1455 (let* ((types (mapcar #'tn-primitive-type
(ir2-lvar-locs 2lvar
)))
1456 (lvar-locs (lvar-tns node block lvar types
))
1457 (nvals (length lvar-locs
))
1458 (locs (make-standard-value-tns nvals
)))
1459 (mapc (lambda (val loc
)
1460 (emit-move node block val loc
))
1464 (vop return-single node block old-fp return-pc
(car locs
))
1465 (vop* return node block
1466 (old-fp return-pc
(reference-tn-list locs nil
))
1470 (aver (eq lvar-kind
:unknown
))
1471 (vop* return-multiple node block
1473 (reference-tn-list (ir2-lvar-locs 2lvar
) nil
))
1480 ;;;; These are used by the debugger to find the top function on the
1481 ;;;; stack. They return the OLD-FP and RETURN-PC for the current
1482 ;;;; function as multiple values.
1484 (defoptimizer (%caller-frame ir2-convert
) (() node block
)
1485 (let ((ir2-physenv (physenv-info (node-physenv node
))))
1486 (move-lvar-result node block
1487 (list (ir2-physenv-old-fp ir2-physenv
))
1490 (defoptimizer (%caller-pc ir2-convert
) (() node block
)
1491 (let ((ir2-physenv (physenv-info (node-physenv node
))))
1492 (move-lvar-result node block
1493 (list (ir2-physenv-return-pc ir2-physenv
))
1496 ;;;; multiple values
1498 ;;; This is almost identical to IR2-CONVERT-LET. Since LTN annotates
1499 ;;; the lvar for the correct number of values (with the lvar user
1500 ;;; responsible for defaulting), we can just pick them up from the
1502 (defun ir2-convert-mv-bind (node block
)
1503 (declare (type mv-combination node
) (type ir2-block block
))
1504 (let* ((fun (ref-leaf (lvar-uses (basic-combination-fun node
))))
1505 (args (basic-combination-args node
))
1506 (vars (lambda-vars fun
)))
1507 (aver (eq (functional-kind fun
) :mv-let
))
1508 (mapc (lambda (src var
)
1509 (when (leaf-refs var
)
1510 (let ((dest (leaf-info var
)))
1511 (if (and (lambda-var-indirect var
)
1512 (lambda-var-explicit-value-cell var
))
1513 (emit-make-value-cell node block src dest
)
1514 (emit-move node block src dest
)))))
1515 (if (singleton-p args
)
1516 (lvar-tns node block
(first args
)
1518 (primitive-type (leaf-type x
)))
1521 (loop for lvar in args
1522 for values
= (nth-value 1 (values-types
1523 (lvar-derived-type lvar
)))
1526 (lvar-tns node block lvar
(loop repeat values
1527 collect
(primitive-type (leaf-type (pop vars
))))))))
1531 ;;; Emit the appropriate fixed value, unknown value or tail variant of
1532 ;;; CALL-VARIABLE. Note that we only need to pass the values start for
1533 ;;; the first argument: all the other argument lvar TNs are
1534 ;;; ignored. This is because we require all of the values globs to be
1535 ;;; contiguous and on stack top.
1536 (defun ir2-convert-mv-call (node block
)
1537 (declare (type mv-combination node
) (type ir2-block block
))
1538 (aver (basic-combination-args node
))
1539 (let* ((start-lvar (lvar-info (first (basic-combination-args node
))))
1540 (start (first (ir2-lvar-locs start-lvar
)))
1541 (tails (and (node-tail-p node
)
1542 (lambda-tail-set (node-home-lambda node
))))
1543 (lvar (node-lvar node
))
1544 (2lvar (and lvar
(lvar-info lvar
))))
1545 (multiple-value-bind (fun named
)
1546 (fun-lvar-tn node block
(basic-combination-fun node
))
1547 (aver (and (not named
)
1548 (eq (ir2-lvar-kind start-lvar
) :unknown
)))
1551 (let ((env (physenv-info (node-physenv node
))))
1552 (vop tail-call-variable node block start fun
1553 (ir2-physenv-old-fp env
)
1554 (ir2-physenv-return-pc env
))))
1556 (eq (ir2-lvar-kind 2lvar
) :unknown
))
1557 (vop* multiple-call-variable node block
(start fun nil
)
1558 ((reference-tn-list (ir2-lvar-locs 2lvar
) t
))
1559 (emit-step-p node
)))
1561 (let ((locs (standard-result-tns lvar
)))
1562 (vop* call-variable node block
(start fun nil
)
1563 ((reference-tn-list locs t
)) (length locs
)
1565 (move-lvar-result node block locs lvar
)))))))
1567 ;;; Reset the stack pointer to the start of the specified
1568 ;;; unknown-values lvar (discarding it and all values globs on top of
1570 (defoptimizer (%pop-values ir2-convert
) ((%lvar
) node block
)
1571 (let* ((lvar (lvar-value %lvar
))
1572 (2lvar (lvar-info lvar
)))
1573 (cond ((eq (ir2-lvar-kind 2lvar
) :unknown
)
1574 (vop reset-stack-pointer node block
1575 (first (ir2-lvar-locs 2lvar
))))
1576 ((lvar-dynamic-extent lvar
)
1577 (vop reset-stack-pointer node block
1578 (ir2-lvar-stack-pointer 2lvar
)))
1579 (t (bug "Trying to pop a not stack-allocated LVAR ~S."
1582 (defoptimizer (%nip-values ir2-convert
) ((last-nipped last-preserved
1585 (let* ( ;; pointer immediately after the nipped block
1586 (after (lvar-value last-nipped
))
1587 (2after (lvar-info after
))
1588 ;; pointer to the first nipped word
1589 (first (lvar-value last-preserved
))
1590 (2first (lvar-info first
))
1592 (moved-tns (loop for lvar-ref in moved
1593 for lvar
= (lvar-value lvar-ref
)
1594 for
2lvar
= (lvar-info lvar
)
1596 collect
(first (ir2-lvar-locs 2lvar
)))))
1597 (aver (or (eq (ir2-lvar-kind 2after
) :unknown
)
1598 (lvar-dynamic-extent after
)))
1599 (aver (eq (ir2-lvar-kind 2first
) :unknown
))
1600 (when *check-consistency
*
1601 ;; we cannot move stack-allocated DX objects
1602 (dolist (moved-lvar moved
)
1603 (aver (eq (ir2-lvar-kind (lvar-info (lvar-value moved-lvar
)))
1605 (flet ((nip-aligned (nipped)
1606 (vop* %%nip-values node block
1608 (first (ir2-lvar-locs 2first
))
1609 (reference-tn-list moved-tns nil
))
1610 ((reference-tn-list moved-tns t
)))))
1611 (cond ((eq (ir2-lvar-kind 2after
) :unknown
)
1612 (nip-aligned (first (ir2-lvar-locs 2after
))))
1613 ((lvar-dynamic-extent after
)
1614 (nip-aligned (ir2-lvar-stack-pointer 2after
)))
1616 (bug "Trying to nip a not stack-allocated LVAR ~S." after
))))))
1618 (defoptimizer (%dummy-dx-alloc ir2-convert
) ((target source
) node block
)
1619 (let* ((target-lvar (lvar-value target
))
1620 (source-lvar (lvar-value source
))
1621 (target-2lvar (lvar-info target-lvar
))
1622 (source-2lvar (and source-lvar
(lvar-info source-lvar
))))
1623 (aver (lvar-dynamic-extent target-lvar
))
1624 (cond ((not source-lvar
)
1625 (vop current-stack-pointer node block
1626 (ir2-lvar-stack-pointer target-2lvar
)))
1627 ((lvar-dynamic-extent source-lvar
)
1628 (emit-move node block
1629 (ir2-lvar-stack-pointer source-2lvar
)
1630 (ir2-lvar-stack-pointer target-2lvar
)))
1631 ((eq (ir2-lvar-kind source-2lvar
) :unknown
)
1632 (emit-move node block
1633 (first (ir2-lvar-locs source-2lvar
))
1634 (ir2-lvar-stack-pointer target-2lvar
)))
1635 (t (bug "Trying to dummy up DX allocation from a ~
1636 not stack-allocated LVAR ~S." source-lvar
)))))
1638 ;;; Deliver the values TNs to LVAR using MOVE-LVAR-RESULT.
1639 (defoptimizer (values ir2-convert
) ((&rest values
) node block
)
1640 (let ((tns (mapcar (lambda (x)
1641 (lvar-tn node block x
))
1644 (move-lvar-result node block tns
(node-lvar node
))))
1646 ;;; In the normal case where unknown values are desired, we use the
1647 ;;; VALUES-LIST VOP. In the relatively unimportant case of VALUES-LIST
1648 ;;; for a fixed number of values, we punt by doing a full call to the
1649 ;;; VALUES-LIST function. This gets the full call VOP to deal with
1650 ;;; defaulting any unsupplied values. It seems unworthwhile to
1651 ;;; optimize this case.
1652 (defoptimizer (values-list ir2-convert
) ((list) node block
)
1653 (let* ((lvar (node-lvar node
))
1654 (2lvar (and lvar
(lvar-info lvar
))))
1656 (eq (ir2-lvar-kind 2lvar
) :unknown
))
1657 (let ((locs (ir2-lvar-locs 2lvar
)))
1658 (vop* values-list node block
1659 ((lvar-tn node block list
) nil
)
1660 ((reference-tn-list locs t
)))))
1661 (t (aver (or (not 2lvar
) ; i.e. we want to check the argument
1662 (eq (ir2-lvar-kind 2lvar
) :fixed
)))
1663 (ir2-convert-full-call node block
)))))
1665 (defoptimizer (%more-arg-values ir2-convert
) ((context start count
) node block
)
1666 (binding* ((lvar (node-lvar node
) :exit-if-null
)
1667 (2lvar (lvar-info lvar
)))
1668 (ecase (ir2-lvar-kind 2lvar
)
1670 ;; KLUDGE: this is very much unsafe, and can leak random stack values.
1671 ;; OTOH, I think the :FIXED case can only happen with (safety 0) in the
1674 (loop for loc in
(ir2-lvar-locs 2lvar
)
1676 do
(vop sb
!vm
::more-arg node block
1677 (lvar-tn node block context
)
1681 (let ((locs (ir2-lvar-locs 2lvar
)))
1682 (vop* %more-arg-values node block
1683 ((lvar-tn node block context
)
1684 (lvar-tn node block start
)
1685 (lvar-tn node block count
)
1687 ((reference-tn-list locs t
))))))))
1689 ;;;; special binding
1691 ;;; This is trivial, given our assumption of a shallow-binding
1693 (defoptimizer (%special-bind ir2-convert
) ((var value
) node block
)
1694 (let ((name (leaf-source-name (lvar-value var
))))
1695 ;; Emit either BIND or DYNBIND, preferring BIND if both exist.
1696 ;; If only one exists, it's DYNBIND.
1697 ;; Even if the backend supports load-time TLS index assignment,
1698 ;; there might be only one vop (as with arm64).
1699 (macrolet ((doit (bind dynbind
)
1700 (if (gethash 'bind
*backend-parsed-vops
*) bind dynbind
)))
1703 ;; Inform later SYMBOL-VALUE calls that they can
1704 ;; assume a nonzero tls-index.
1705 ;; FIXME: setting INFO is inefficient when not actually
1706 ;; changing anything
1707 (unless (info :variable
:wired-tls name
)
1708 (setf (info :variable
:wired-tls name
) :always-has-tls
))
1709 ;; We force the symbol into the code constants in case BIND
1710 ;; does not actually reference it, as with x86.
1711 (emit-constant name
)
1712 (vop bind node block
(lvar-tn node block value
) name
))
1713 (vop dynbind node block
(lvar-tn node block value
)
1714 (emit-constant name
))))))
1716 (defoptimizer (%special-unbind ir2-convert
) ((n) node block
)
1717 (declare (ignorable n
))
1718 (vop unbind node block
#!+(and sb-thread unbind-n-vop
) (lvar-value n
)))
1720 ;;; ### It's not clear that this really belongs in this file, or
1721 ;;; should really be done this way, but this is the least violation of
1722 ;;; abstraction in the current setup. We don't want to wire
1723 ;;; shallow-binding assumptions into IR1tran.
1724 (def-ir1-translator progv
1725 ((vars vals
&body body
) start next result
)
1728 (with-unique-names (bind unbind
)
1729 (once-only ((n-save-bs '(%primitive current-binding-pointer
)))
1732 (labels ((,unbind
(vars)
1733 (declare (optimize (speed 2) (debug 0)))
1734 (let ((unbound-marker (%primitive make-unbound-marker
)))
1736 ;; CLHS says "bound and then made to have no value" -- user
1737 ;; should not be able to tell the difference between that and this.
1738 (about-to-modify-symbol-value var
'progv
)
1739 (%primitive dynbind unbound-marker var
))))
1741 (declare (optimize (speed 2) (debug 0)
1742 (insert-debug-catch 0)))
1744 ((null vals
) (,unbind vars
))
1746 (let ((val (car vals
))
1748 (about-to-modify-symbol-value var
'progv val t
)
1749 (%primitive dynbind val var
))
1750 (,bind
(cdr vars
) (cdr vals
))))))
1751 (,bind
,vars
,vals
))
1754 ;; Technically ANSI CL doesn't allow declarations at the
1755 ;; start of the cleanup form. SBCL happens to allow for
1756 ;; them, due to the way the UNWIND-PROTECT ir1 translation
1757 ;; is implemented; the cleanup forms are directly spliced
1758 ;; into an FLET definition body. And a declaration here
1759 ;; actually has exactly the right scope for what we need
1760 ;; (ensure that debug instrumentation is not emitted for the
1761 ;; cleanup function). -- JES, 2007-06-16
1762 (declare (optimize (insert-debug-catch 0)))
1763 (%primitive unbind-to-here
,n-save-bs
))))))
1767 ;;; Convert a non-local lexical exit. First find the NLX-INFO in our
1768 ;;; environment. Note that this is never called on the escape exits
1769 ;;; for CATCH and UNWIND-PROTECT, since the escape functions aren't
1771 (defun ir2-convert-exit (node block
)
1772 (declare (type exit node
) (type ir2-block block
))
1773 (let* ((nlx (exit-nlx-info node
))
1774 (loc (find-in-physenv nlx
(node-physenv node
)))
1775 (temp (make-stack-pointer-tn))
1776 (value (exit-value node
)))
1777 (if (nlx-info-safe-p nlx
)
1778 (vop value-cell-ref node block loc temp
)
1779 (emit-move node block loc temp
))
1781 (let ((locs (ir2-lvar-locs (lvar-info value
))))
1782 (vop unwind node block temp
(first locs
) (second locs
)))
1783 (let ((0-tn (emit-constant 0)))
1784 (vop unwind node block temp
0-tn
0-tn
))))
1788 ;;; %CLEANUP-POINT doesn't do anything except prevent the body from
1789 ;;; being entirely deleted.
1790 (defoptimizer (%cleanup-point ir2-convert
) (() node block
) node block
)
1792 ;;; This function invalidates a lexical exit on exiting from the
1793 ;;; dynamic extent. This is done by storing 0 into the indirect value
1794 ;;; cell that holds the closed unwind block.
1795 (defoptimizer (%lexical-exit-breakup ir2-convert
) ((info) node block
)
1796 (let ((nlx (lvar-value info
)))
1797 (when (nlx-info-safe-p nlx
)
1798 (vop value-cell-set node block
1799 (find-in-physenv nlx
(node-physenv node
))
1800 (emit-constant 0)))))
1802 ;;; We have to do a spurious move of no values to the result lvar so
1803 ;;; that lifetime analysis won't get confused.
1804 (defun ir2-convert-throw (node block
)
1805 (declare (type mv-combination node
) (type ir2-block block
))
1806 (let ((args (basic-combination-args node
)))
1807 (check-catch-tag-type (first args
))
1808 (vop* throw node block
1809 ((lvar-tn node block
(first args
))
1811 (ir2-lvar-locs (lvar-info (second args
)))
1814 (move-lvar-result node block
() (node-lvar node
))
1817 ;;; Emit code to set up a non-local exit. INFO is the NLX-INFO for the
1818 ;;; exit, and TAG is the lvar for the catch tag (if any.) We get at
1819 ;;; the target PC by passing in the label to the vop. The vop is
1820 ;;; responsible for building a return-PC object.
1821 (defun emit-nlx-start (node block info tag
)
1822 (declare (type node node
) (type ir2-block block
) (type nlx-info info
)
1823 (type (or lvar null
) tag
))
1824 (let* ((2info (nlx-info-info info
))
1825 (kind (cleanup-kind (nlx-info-cleanup info
)))
1826 (block-tn (physenv-live-tn
1828 (primitive-type-or-lose
1832 ((:unwind-protect
:block
:tagbody
)
1834 (node-physenv node
)))
1835 (res (make-stack-pointer-tn))
1836 (target-label (ir2-nlx-info-target 2info
)))
1838 (vop current-binding-pointer node block
1839 (car (ir2-nlx-info-dynamic-state 2info
)))
1840 (vop* save-dynamic-state node block
1842 ((reference-tn-list (cdr (ir2-nlx-info-dynamic-state 2info
)) t
)))
1843 (vop current-stack-pointer node block
(ir2-nlx-info-save-sp 2info
))
1847 (vop make-catch-block node block block-tn
1848 (lvar-tn node block tag
) target-label res
))
1849 ((:unwind-protect
:block
:tagbody
)
1850 (vop make-unwind-block node block block-tn target-label res
)))
1854 (if (nlx-info-safe-p info
)
1855 (emit-make-value-cell node block res
(ir2-nlx-info-home 2info
))
1856 (emit-move node block res
(ir2-nlx-info-home 2info
))))
1858 (vop set-unwind-protect node block block-tn
))
1863 ;;; Scan each of ENTRY's exits, setting up the exit for each lexical exit.
1864 (defun ir2-convert-entry (node block
)
1865 (declare (type entry node
) (type ir2-block block
))
1867 (dolist (exit (entry-exits node
))
1868 (let ((info (exit-nlx-info exit
)))
1870 (not (memq info nlxes
))
1871 (member (cleanup-kind (nlx-info-cleanup info
))
1872 '(:block
:tagbody
)))
1874 (emit-nlx-start node block info nil
)))))
1877 ;;; Set up the unwind block for these guys.
1878 (defoptimizer (%catch ir2-convert
) ((info-lvar tag
) node block
)
1879 (check-catch-tag-type tag
)
1880 (emit-nlx-start node block
(lvar-value info-lvar
) tag
))
1881 (defoptimizer (%unwind-protect ir2-convert
) ((info-lvar cleanup
) node block
)
1882 (declare (ignore cleanup
))
1883 (emit-nlx-start node block
(lvar-value info-lvar
) nil
))
1885 ;;; Emit the entry code for a non-local exit. We receive values and
1886 ;;; restore dynamic state.
1888 ;;; In the case of a lexical exit or CATCH, we look at the exit lvar's
1889 ;;; kind to determine which flavor of entry VOP to emit. If unknown
1890 ;;; values, emit the xxx-MULTIPLE variant to the lvar locs. If fixed
1891 ;;; values, make the appropriate number of temps in the standard
1892 ;;; values locations and use the other variant, delivering the temps
1893 ;;; to the lvar using MOVE-LVAR-RESULT.
1895 ;;; In the UNWIND-PROTECT case, we deliver the first register
1896 ;;; argument, the argument count and the argument pointer to our lvar
1897 ;;; as multiple values. These values are the block exited to and the
1898 ;;; values start and count.
1900 ;;; After receiving values, we restore dynamic state. Except in the
1901 ;;; UNWIND-PROTECT case, the values receiving restores the stack
1902 ;;; pointer. In an UNWIND-PROTECT cleanup, we want to leave the stack
1903 ;;; pointer alone, since the thrown values are still out there.
1904 (defoptimizer (%nlx-entry ir2-convert
) ((info-lvar) node block
)
1905 (let* ((info (lvar-value info-lvar
))
1906 (lvar (node-lvar node
))
1907 (2info (nlx-info-info info
))
1908 (top-loc (ir2-nlx-info-save-sp 2info
))
1909 (start-loc (make-nlx-entry-arg-start-location))
1910 (count-loc (make-arg-count-location))
1911 (target (ir2-nlx-info-target 2info
)))
1913 (ecase (cleanup-kind (nlx-info-cleanup info
))
1914 ((:catch
:block
:tagbody
)
1915 (let ((2lvar (and lvar
(lvar-info lvar
))))
1916 (if (and 2lvar
(eq (ir2-lvar-kind 2lvar
) :unknown
))
1917 (vop* nlx-entry-multiple node block
1918 (top-loc start-loc count-loc nil
)
1919 ((reference-tn-list (ir2-lvar-locs 2lvar
) t
))
1921 (let ((locs (standard-result-tns lvar
)))
1922 (vop* nlx-entry node block
1923 (top-loc start-loc count-loc nil
)
1924 ((reference-tn-list locs t
))
1927 (move-lvar-result node block locs lvar
)))))
1929 (let ((block-loc (standard-arg-location 0)))
1930 (vop uwp-entry node block target block-loc start-loc count-loc
)
1933 (list block-loc start-loc count-loc
)
1937 (when *collect-dynamic-statistics
*
1938 (vop count-me node block
*dynamic-counts-tn
*
1939 (block-number (ir2-block-block block
))))
1941 (vop* restore-dynamic-state node block
1942 ((reference-tn-list (cdr (ir2-nlx-info-dynamic-state 2info
)) nil
))
1944 (vop unbind-to-here node block
1945 (car (ir2-nlx-info-dynamic-state 2info
)))))
1947 ;;;; n-argument functions
1949 (macrolet ((def (name)
1950 `(defoptimizer (,name ir2-convert
) ((&rest args
) node block
)
1953 (/ sb
!vm
:large-object-size
1954 (* sb
!vm
:n-word-bytes
2)))
1955 ;; The VOPs will try to allocate all space at once
1956 ;; And it'll end up in large objects, and no conses
1957 ;; are welcome there.
1958 (ir2-convert-full-call node block
))
1960 (let* ((refs (reference-tn-list
1961 (loop for arg in args
1962 for tn
= (make-normal-tn *backend-t-primitive-type
*)
1964 (emit-move node block
(lvar-tn node block arg
) tn
)
1967 (lvar (node-lvar node
))
1968 (res (lvar-result-tns
1970 (list (primitive-type (specifier-type 'list
))))))
1971 (when (and lvar
(lvar-dynamic-extent lvar
))
1972 (vop current-stack-pointer node block
1973 (ir2-lvar-stack-pointer (lvar-info lvar
))))
1974 (vop* ,name node block
(refs) ((first res
) nil
)
1976 (move-lvar-result node block res lvar
)))))))
1981 (defoptimizer (mask-signed-field ir2-convert
) ((width x
) node block
)
1983 (when (constant-lvar-p width
)
1984 (case (lvar-value width
)
1985 (#.
(- sb
!vm
:n-word-bits sb
!vm
:n-fixnum-tag-bits
)
1986 (when (or (csubtypep (lvar-type x
)
1987 (specifier-type 'word
))
1988 (csubtypep (lvar-type x
)
1989 (specifier-type 'sb
!vm
:signed-word
)))
1990 (let* ((lvar (node-lvar node
))
1991 (temp (make-normal-tn
1992 (if (csubtypep (lvar-type x
)
1993 (specifier-type 'word
))
1994 (primitive-type-of most-positive-word
)
1996 (- (ash most-positive-word -
1))))))
1997 (results (lvar-result-tns
1999 (list (primitive-type-or-lose 'fixnum
)))))
2000 (emit-move node block
(lvar-tn node block x
) temp
)
2001 (vop sb
!vm
::move-from-word
/fixnum node block
2002 temp
(first results
))
2003 (move-lvar-result node block results lvar
)
2005 (#.sb
!vm
:n-word-bits
2006 (when (csubtypep (lvar-type x
) (specifier-type 'word
))
2007 (let* ((lvar (node-lvar node
))
2008 (temp (make-normal-tn
2009 (primitive-type-of most-positive-word
)))
2010 (results (lvar-result-tns
2012 (list (primitive-type
2013 (specifier-type 'sb
!vm
:signed-word
))))))
2014 (emit-move node block
(lvar-tn node block x
) temp
)
2015 (vop sb
!vm
::word-move node block
2016 temp
(first results
))
2017 (move-lvar-result node block results lvar
)
2019 (if (template-p (basic-combination-info node
))
2020 (ir2-convert-template node block
)
2021 (ir2-convert-full-call node block
))))
2023 ;; just a fancy identity
2024 (defoptimizer (%typep-wrapper ir2-convert
) ((value variable type
) node block
)
2025 (declare (ignore variable type
))
2026 (let* ((lvar (node-lvar node
))
2027 (results (lvar-result-tns lvar
(list (primitive-type-or-lose t
)))))
2028 (emit-move node block
(lvar-tn node block value
) (first results
))
2029 (move-lvar-result node block results lvar
)))
2031 ;;; An identity to avoid complaints about constant modification
2032 (defoptimizer (ltv-wrapper ir2-convert
) ((x) node block
)
2033 (let* ((lvar (node-lvar node
))
2034 (results (lvar-result-tns lvar
(list (primitive-type-or-lose t
)))))
2035 (emit-move node block
(lvar-tn node block x
) (first results
))
2036 (move-lvar-result node block results lvar
)))
2038 #-sb-xc-host
;; package-lock-violation-p is not present yet
2039 (defoptimizer (set ir2-hook
) ((symbol value
) node block
)
2040 (declare (ignore value block
))
2041 (when (constant-lvar-p symbol
)
2042 (let* ((symbol (lvar-value symbol
))
2043 (kind (info :variable
:kind symbol
)))
2044 (when (and (eq kind
:unknown
)
2045 (sb!impl
::package-lock-violation-p
(symbol-package symbol
) symbol
))
2046 (let ((*compiler-error-context
* node
))
2047 (compiler-warn "violating package lock on ~/sb-impl:print-symbol-with-prefix/"
2050 ;;; Convert the code in a component into VOPs.
2051 (defun ir2-convert (component)
2052 (declare (type component component
))
2053 (let (#!+sb-dyncount
2054 (*dynamic-counts-tn
*
2055 (when *collect-dynamic-statistics
*
2057 (block-number (block-next (component-head component
))))
2058 (counts (make-array blocks
2059 :element-type
'(unsigned-byte 32)
2060 :initial-element
0))
2061 (info (make-dyncount-info
2062 :for
(component-name component
)
2063 :costs
(make-array blocks
2064 :element-type
'(unsigned-byte 32)
2067 (setf (ir2-component-dyncount-info (component-info component
))
2069 (emit-constant info
)
2070 (emit-constant counts
)))))
2072 (declare (type index num
))
2073 (do-ir2-blocks (2block component
)
2074 (let ((block (ir2-block-block 2block
)))
2075 (when (block-start block
)
2076 (setf (block-number block
) num
)
2078 (when *collect-dynamic-statistics
*
2079 (let ((first-node (block-start-node block
)))
2080 (unless (or (and (bind-p first-node
)
2081 (xep-p (bind-lambda first-node
)))
2083 (node-lvar first-node
))
2088 #!+sb-dyncount
*dynamic-counts-tn
* #!-sb-dyncount nil
2091 (let ((first-node (block-start-node block
)))
2092 (unless (or (and (bind-p first-node
)
2093 ;; Bind-nodes already have safepoints
2094 (eq (bind-lambda first-node
)
2095 (lambda-home (bind-lambda first-node
))))
2096 (and (valued-node-p first-node
)
2097 (node-lvar first-node
)
2099 (node-lvar first-node
))
2101 (when (and (rest (block-pred block
))
2103 (member (loop-kind (block-loop block
))
2104 '(:natural
:strange
))
2105 (eq block
(loop-head (block-loop block
)))
2106 (policy first-node
(< inhibit-safepoints
2)))
2107 (vop sb
!vm
::insert-safepoint first-node
2block
))))
2108 (ir2-convert-block block
)
2112 ;;; If necessary, emit a terminal unconditional branch to go to the
2113 ;;; successor block. If the successor is the component tail, then
2114 ;;; there isn't really any successor, but if the end is a non-tail
2115 ;;; call to a function that's not *known* to never return, then we
2116 ;;; emit an error trap just in case the function really does return.
2118 ;;; Trapping after known calls makes it easier to understand type
2119 ;;; derivation bugs at runtime: they show up as nil-fun-returned-error,
2120 ;;; rather than the execution of arbitrary code or error traps.
2121 (defun finish-ir2-block (block)
2122 (declare (type cblock block
))
2123 (let* ((2block (block-info block
))
2124 (last (block-last block
))
2125 (succ (block-succ block
)))
2127 (aver (singleton-p succ
))
2128 (let ((target (first succ
)))
2129 (cond ((eq target
(component-tail (block-component block
)))
2130 (when (and (basic-combination-p last
)
2131 (or (eq (basic-combination-kind last
) :full
)
2132 (and (eq (basic-combination-kind last
) :known
)
2133 (eq (basic-combination-info last
) :full
))))
2134 (let* ((fun (basic-combination-fun last
))
2135 (use (lvar-uses fun
))
2136 (name (and (ref-p use
)
2137 (leaf-has-source-name-p (ref-leaf use
))
2138 (leaf-source-name (ref-leaf use
))))
2139 (ftype (and (info :function
:info name
) ; only use the FTYPE if
2140 (proclaimed-ftype name
)))) ; NAME was DEFKNOWN
2141 (unless (or (node-tail-p last
)
2142 (policy last
(zerop safety
))
2143 (and (fun-type-p ftype
)
2144 (eq *empty-type
* (fun-type-returns ftype
))))
2145 (vop nil-fun-returned-error last
2block
2147 (emit-constant name
)
2148 (multiple-value-bind (tn named
)
2149 (fun-lvar-tn last
2block fun
)
2152 ((not (eq (ir2-block-next 2block
) (block-info target
)))
2153 (vop branch last
2block
(block-label target
)))
2155 (register-drop-thru target
))))))
2159 ;;; Convert the code in a block into VOPs.
2160 (defun ir2-convert-block (block)
2161 (declare (type cblock block
))
2162 (let ((2block (block-info block
)))
2163 (do-nodes (node lvar block
)
2167 (let ((2lvar (lvar-info lvar
)))
2168 ;; function REF in a local call is not annotated
2169 (when (and 2lvar
(not (eq (ir2-lvar-kind 2lvar
) :delayed
)))
2170 (ir2-convert-ref node
2block
)))))
2172 (let ((kind (basic-combination-kind node
)))
2175 (ir2-convert-local-call node
2block
))
2177 (ir2-convert-full-call node
2block
))
2179 (let* ((info (basic-combination-fun-info node
))
2180 (fun (fun-info-ir2-convert info
))
2181 (hook (fun-info-ir2-hook info
)))
2183 (funcall hook node
2block
))
2185 (funcall fun node
2block
))
2186 ((eq (basic-combination-info node
) :full
)
2187 (ir2-convert-full-call node
2block
))
2189 (ir2-convert-template node
2block
))))))))
2191 (when (lvar-info (if-test node
))
2192 (ir2-convert-if node
2block
)))
2194 (let ((fun (bind-lambda node
)))
2195 (when (eq (lambda-home fun
) fun
)
2196 (ir2-convert-bind node
2block
))))
2198 (ir2-convert-return node
2block
))
2200 (ir2-convert-set node
2block
))
2202 (ir2-convert-cast node
2block
))
2205 ((eq (basic-combination-kind node
) :local
)
2206 (ir2-convert-mv-bind node
2block
))
2207 ((eq (lvar-fun-name (basic-combination-fun node
))
2209 (ir2-convert-throw node
2block
))
2211 (ir2-convert-mv-call node
2block
))))
2213 (when (exit-entry node
)
2214 (ir2-convert-exit node
2block
)))
2216 (ir2-convert-entry node
2block
)))))
2218 (finish-ir2-block block
)