1 ;;;; This file contains the virtual-machine-independent parts of the
2 ;;;; code which does the actual translation of nodes to VOPs.
4 ;;;; This software is part of the SBCL system. See the README file for
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
15 ;;;; moves and type checks
17 ;;; Move X to Y unless they are EQ.
18 (defun emit-move (node block x y
)
19 (declare (type node node
) (type ir2-block block
) (type tn x y
))
20 (aver (neq (tn-kind x
) :unused
))
21 (aver (neq (tn-kind y
) :unused
))
23 (vop move node block x y
))
26 ;;; Determine whether we should emit a single-stepper breakpoint
27 ;;; around a call / before a vop.
28 (defun emit-step-p (node)
29 (if (and (policy node
(> insert-step-conditions
1))
30 (typep node
'combination
))
31 (combination-step-info node
)
34 ;;; Allocate an indirect value cell.
35 (defevent make-value-cell-event
"Allocate heap value cell for lexical var.")
36 (defun emit-make-value-cell (node block value res
)
37 (event make-value-cell-event node
)
38 (vop make-value-cell node block value nil res
))
42 ;;; Return the TN that holds the value of THING in the environment ENV.
43 (declaim (ftype (sfunction ((or nlx-info lambda-var clambda
) physenv
) tn
)
45 (defun find-in-physenv (thing physenv
)
46 (or (cdr (assoc thing
(ir2-physenv-closure (physenv-info physenv
))))
49 ;; I think that a failure of this assertion means that we're
50 ;; trying to access a variable which was improperly closed
51 ;; over. The PHYSENV describes a physical environment. Every
52 ;; variable that a form refers to should either be in its
53 ;; physical environment directly, or grabbed from a
54 ;; surrounding physical environment when it was closed over.
55 ;; The ASSOC expression above finds closed-over variables, so
56 ;; if we fell through the ASSOC expression, it wasn't closed
57 ;; over. Therefore, it must be in our physical environment
58 ;; directly. If instead it is in some other physical
59 ;; environment, then it's bogus for us to reference it here
60 ;; without it being closed over. -- WHN 2001-09-29
61 (aver (eq physenv
(lambda-physenv (lambda-var-home thing
))))
64 (aver (eq physenv
(block-physenv (nlx-info-target thing
))))
65 (ir2-nlx-info-home (nlx-info-info thing
)))
68 (entry-info-closure-tn (lambda-info thing
))))
69 (bug "~@<~2I~_~S ~_not found in ~_~S~:>" thing physenv
)))
71 ;;; Return a TN that represents the value of LEAF, or NIL if LEAF
72 ;;; isn't directly represented by a TN. ENV is the environment that
73 ;;; the reference is done in.
74 (defun leaf-tn (leaf env
)
75 (declare (type leaf leaf
) (type physenv env
))
78 (unless (lambda-var-indirect leaf
)
79 (find-in-physenv leaf env
)))
80 (constant (make-constant-tn leaf
))
83 ;;; This is used to conveniently get a handle on a constant TN during
84 ;;; IR2 conversion. It returns a constant TN representing the Lisp
86 (defun emit-constant (value)
87 (make-constant-tn (find-constant value
)))
89 ;;; Convert a REF node. The reference must not be delayed.
90 (defun ir2-convert-ref (node block
)
91 (declare (type ref node
) (type ir2-block block
))
92 (let* ((lvar (node-lvar node
))
93 (leaf (ref-leaf node
))
94 (locs (lvar-result-tns
95 lvar
(list (primitive-type (leaf-type leaf
)))))
99 (let ((tn (find-in-physenv leaf
(node-physenv node
)))
100 (indirect (lambda-var-indirect leaf
))
101 (explicit (lambda-var-explicit-value-cell leaf
)))
103 ((and indirect explicit
)
104 (vop value-cell-ref node block tn res
))
106 (not (eq (node-physenv node
)
107 (lambda-physenv (lambda-var-home leaf
)))))
108 (let ((reffer (third (primitive-type-indirect-cell-type
109 (primitive-type (leaf-type leaf
))))))
111 (funcall reffer node block tn
(leaf-info leaf
) res
)
112 (vop ancestor-frame-ref node block tn
(leaf-info leaf
) res
))))
113 (t (emit-move node block tn res
)))))
115 (emit-move node block
(make-constant-tn leaf
) res
))
117 (ir2-convert-closure node block leaf res
))
119 (ir2-convert-global-var node block leaf res
)))
120 (move-lvar-result node block locs lvar
))
123 (defun ir2-convert-global-var (node block leaf res
)
124 (let ((unsafe (policy node
(zerop safety
)))
125 (name (leaf-source-name leaf
)))
126 (ecase (global-var-kind leaf
)
128 (aver (symbolp name
))
129 (let ((name-tn (emit-constant name
)))
130 (if (or unsafe
(always-boundp name
))
131 (vop fast-symbol-value node block name-tn res
)
132 (vop symbol-value node block name-tn res
))))
134 (aver (symbolp name
))
135 (let ((name-tn (emit-constant name
)))
136 (if (or unsafe
(always-boundp name
))
137 (vop fast-symbol-global-value node block name-tn res
)
138 (vop symbol-global-value node block name-tn res
))))
140 ;; In cross-compilation, testing (INFO :function :definition) is not
141 ;; sensible (or possible) but we can assume that things with fun-info
142 ;; will eventually be defined. If that's untrue, e.g. if we referred
143 ;; to #'DESCRIBE during cold-load, we'd just fix it locally by declaring
144 ;; DESCRIBE notinline.
145 ;; But in the target, more caution is warranted because users might
146 ;; DEFKNOWN a function but fail to define it. And they shouldn't be
147 ;; expected to understand the failure mode and the remedy.
148 (cond ((and #-sb-xc-host
(info :function
:definition name
)
149 (info :function
:info name
)
150 (let ((*lexenv
* (node-lexenv node
)))
151 (not (fun-lexically-notinline-p name
))))
152 ;; Known functions can be dumped without going through fdefns.
153 ;; But if NOTINLINEd, don't early-bind to the functional value
154 ;; because that disallows redefinition, including but not limited
155 ;; to encapsulations, which in turn makes TRACE not work, which
156 ;; leads to extreme frustration when debugging.
157 (emit-move node block
(make-load-time-constant-tn :known-fun name
)
160 (let ((fdefn-tn (make-load-time-constant-tn :fdefinition name
)))
162 (vop fdefn-fun node block fdefn-tn res
)
163 (vop safe-fdefn-fun node block fdefn-tn res
)))))))))
165 ;;; some sanity checks for a CLAMBDA passed to IR2-CONVERT-CLOSURE
166 (defun assertions-on-ir2-converted-clambda (clambda)
167 ;; This assertion was sort of an experiment. It would be nice and
168 ;; sane and easier to understand things if it were *always* true,
169 ;; but experimentally I observe that it's only *almost* always
170 ;; true. -- WHN 2001-01-02
172 (aver (eql (lambda-component clambda
)
173 (block-component (ir2-block-block ir2-block
))))
174 ;; Check for some weirdness which came up in bug
177 ;; The MAKE-LOAD-TIME-CONSTANT-TN call above puts an :ENTRY record
178 ;; into the IR2-COMPONENT-CONSTANTS table. The dump-a-COMPONENT
180 ;; * treats every HANDLEless :ENTRY record into a
182 ;; * expects every patch to correspond to an
183 ;; IR2-COMPONENT-ENTRIES record.
184 ;; The IR2-COMPONENT-ENTRIES records are set by ENTRY-ANALYZE
185 ;; walking over COMPONENT-LAMBDAS. Bug 138b arose because there
186 ;; was a HANDLEless :ENTRY record which didn't correspond to an
187 ;; IR2-COMPONENT-ENTRIES record. That problem is hard to debug
188 ;; when it's caught at dump time, so this assertion tries to catch
190 (aver (member clambda
191 (component-lambdas (lambda-component clambda
))))
192 ;; another bug-138-related issue: COMPONENT-NEW-FUNCTIONALS is
193 ;; used as a queue for stuff pending to do in IR1, and now that
194 ;; we're doing IR2 it should've been completely flushed (but
196 (aver (null (component-new-functionals (lambda-component clambda
))))
199 ;;; Emit code to load a function object implementing FUNCTIONAL into
200 ;;; RES. This gets interesting when the referenced function is a
201 ;;; closure: we must make the closure and move the closed-over values
204 ;;; FUNCTIONAL is either a :TOPLEVEL-XEP functional or the XEP lambda
205 ;;; for the called function, since local call analysis converts all
206 ;;; closure references. If a :TOPLEVEL-XEP, we know it is not a
209 ;;; If a closed-over LAMBDA-VAR has no refs (is deleted), then we
210 ;;; don't initialize that slot. This can happen with closures over
211 ;;; top level variables, where optimization of the closure deleted the
212 ;;; variable. Since we committed to the closure format when we
213 ;;; pre-analyzed the top level code, we just leave an empty slot.
214 (defun ir2-convert-closure (ref ir2-block functional res
)
215 (declare (type ref ref
)
216 (type ir2-block ir2-block
)
217 (type functional functional
)
220 (aver (not (eql (functional-kind functional
) :deleted
)))
221 (unless (leaf-info functional
)
222 (setf (leaf-info functional
)
223 (make-entry-info :name
224 (functional-debug-name functional
))))))
225 (let ((closure (etypecase functional
227 (assertions-on-ir2-converted-clambda functional
)
228 (physenv-closure (get-lambda-physenv functional
)))
230 (aver (eq (functional-kind functional
) :toplevel-xep
))
235 (let* ((physenv (node-physenv ref
))
236 (tn (find-in-physenv functional physenv
)))
237 (emit-move ref ir2-block tn res
)))
238 ;; we're about to emit a reference to a "closure" that's actually
239 ;; an inlinable global function.
240 ((and (global-var-p (setf global-var
241 (functional-inline-expanded functional
)))
242 (eq :global-function
(global-var-kind global-var
)))
243 (ir2-convert-global-var ref ir2-block global-var res
))
245 ;; if we're here, we should have either a toplevel-xep (some
246 ;; global scope function in a different component) or an external
247 ;; reference to the "closure"'s body.
249 (aver (memq (functional-kind functional
) '(:external
:toplevel-xep
)))
250 (let ((entry (make-load-time-constant-tn :entry functional
)))
251 (emit-move ref ir2-block entry res
))))))
254 (defun closure-initial-value (what this-env current-fp
)
255 (declare (type (or nlx-info lambda-var clambda
) what
)
256 (type physenv this-env
)
257 (type (or tn null
) current-fp
))
258 ;; If we have an indirect LAMBDA-VAR that does not require an
259 ;; EXPLICIT-VALUE-CELL, and is from this environment (not from being
260 ;; closed over), we need to store the current frame pointer.
261 (if (and (lambda-var-p what
)
262 (lambda-var-indirect what
)
263 (not (lambda-var-explicit-value-cell what
))
264 (eq (lambda-physenv (lambda-var-home what
))
267 (find-in-physenv what this-env
)))
269 (defoptimizer (%allocate-closures ltn-annotate
) ((leaves) node ltn-policy
)
270 (declare (ignore ltn-policy
))
271 (when (lvar-dynamic-extent leaves
)
272 (let ((info (make-ir2-lvar *backend-t-primitive-type
*)))
273 (setf (ir2-lvar-kind info
) :delayed
)
274 (setf (lvar-info leaves
) info
)
275 (setf (ir2-lvar-stack-pointer info
)
276 (make-stack-pointer-tn)))))
278 (defoptimizer (%allocate-closures ir2-convert
) ((leaves) call
2block
)
279 (let ((dx-p (lvar-dynamic-extent leaves
)))
282 (vop current-stack-pointer call
2block
283 (ir2-lvar-stack-pointer (lvar-info leaves
))))
284 (dolist (leaf (lvar-value leaves
))
285 (binding* ((xep (awhen (functional-entry-fun leaf
)
286 ;; if the xep's been deleted then we can skip it
287 (if (eq (functional-kind it
) :deleted
)
290 (nil (aver (xep-p xep
)))
291 (entry-info (lambda-info xep
) :exit-if-null
)
292 (tn (entry-info-closure-tn entry-info
) :exit-if-null
)
293 (closure (physenv-closure (get-lambda-physenv xep
)))
295 (entry (make-load-time-constant-tn :entry xep
)))
296 (let ((this-env (node-physenv call
))
297 (leaf-dx-p (and dx-p
(leaf-dynamic-extent leaf
))))
298 (aver (entry-info-offset entry-info
))
299 (vop make-closure call
2block
#!-x86-64 entry
300 (entry-info-offset entry-info
) (length closure
)
302 (loop for what in closure and n from
0 do
303 (unless (and (lambda-var-p what
)
304 (null (leaf-refs what
)))
305 ;; In LABELS a closure may refer to another closure
306 ;; in the same group, so we must be sure that we
307 ;; store a closure only after its creation.
309 ;; TODO: Here is a simple solution: we postpone
310 ;; putting of all closures after all creations
311 ;; (though it may require more registers).
313 (delayed (list tn
(find-in-physenv what this-env
) n
))
314 (let ((initial-value (closure-initial-value
317 (vop closure-init call
2block
319 ;; An initial-value of NIL means to stash
320 ;; the frame pointer... which requires a
322 (vop closure-init-from-fp call
2block tn n
)))))))))
323 (loop for
(tn what n
) in
(delayed)
324 do
(vop closure-init call
2block
328 ;;; Convert a SET node. If the NODE's LVAR is annotated, then we also
329 ;;; deliver the value to that lvar. If the var is a lexical variable
330 ;;; with no refs, then we don't actually set anything, since the
331 ;;; variable has been deleted.
332 (defun ir2-convert-set (node block
)
333 (declare (type cset node
) (type ir2-block block
))
334 (let* ((lvar (node-lvar node
))
335 (leaf (set-var node
))
336 (val (lvar-tn node block
(set-value node
)))
339 lvar
(list (primitive-type (leaf-type leaf
))))
343 (when (leaf-refs leaf
)
344 (let ((tn (find-in-physenv leaf
(node-physenv node
)))
345 (indirect (lambda-var-indirect leaf
))
346 (explicit (lambda-var-explicit-value-cell leaf
)))
348 ((and indirect explicit
)
349 (vop value-cell-set node block tn val
))
351 (not (eq (node-physenv node
)
352 (lambda-physenv (lambda-var-home leaf
)))))
353 (let ((setter (fourth (primitive-type-indirect-cell-type
354 (primitive-type (leaf-type leaf
))))))
356 (funcall setter node block tn val
(leaf-info leaf
))
357 (vop ancestor-frame-set node block tn val
(leaf-info leaf
)))))
358 (t (emit-move node block val tn
))))))
360 (aver (symbolp (leaf-source-name leaf
)))
361 (ecase (global-var-kind leaf
)
363 (vop set node block
(emit-constant (leaf-source-name leaf
)) val
))
365 (vop %set-symbol-global-value node
366 block
(emit-constant (leaf-source-name leaf
)) val
)))))
368 (emit-move node block val
(first locs
))
369 (move-lvar-result node block locs lvar
)))
372 ;;;; utilities for receiving fixed values
374 ;;; Return a TN that can be referenced to get the value of LVAR. LVAR
375 ;;; must be LTN-ANNOTATED either as a delayed leaf ref or as a fixed,
376 ;;; single-value lvar.
378 ;;; The primitive-type of the result will always be the same as the
379 ;;; IR2-LVAR-PRIMITIVE-TYPE, ensuring that VOPs are always called with
380 ;;; TNs that satisfy the operand primitive-type restriction. We may
381 ;;; have to make a temporary of the desired type and move the actual
382 ;;; lvar TN into it. This happens when we delete a type check in
383 ;;; unsafe code or when we locally know something about the type of an
384 ;;; argument variable.
385 (defun lvar-tn (node block lvar
)
386 (declare (type node node
) (type ir2-block block
) (type lvar lvar
))
387 (let* ((2lvar (lvar-info lvar
))
389 (ecase (ir2-lvar-kind 2lvar
)
391 (let ((ref (lvar-uses lvar
)))
392 (leaf-tn (ref-leaf ref
) (node-physenv ref
))))
394 (aver (= (length (ir2-lvar-locs 2lvar
)) 1))
395 (first (ir2-lvar-locs 2lvar
)))))
396 (ptype (ir2-lvar-primitive-type 2lvar
)))
398 (cond ((eq (tn-primitive-type lvar-tn
) ptype
) lvar-tn
)
400 (let ((temp (make-normal-tn ptype
)))
401 (emit-move node block lvar-tn temp
)
404 ;;; This is similar to LVAR-TN, but hacks multiple values. We return
405 ;;; TNs holding the values of LVAR with PTYPES as their primitive
406 ;;; types. LVAR must be annotated for the same number of fixed values
407 ;;; are there are PTYPES.
409 ;;; If the lvar has a type check, check the values into temps and
410 ;;; return the temps. When we have more values than assertions, we
411 ;;; move the extra values with no check.
412 (defun lvar-tns (node block lvar ptypes
)
413 (declare (type node node
) (type ir2-block block
)
414 (type lvar lvar
) (list ptypes
))
415 (let* ((locs (ir2-lvar-locs (lvar-info lvar
)))
416 (nlocs (length locs
)))
417 (aver (= nlocs
(length ptypes
)))
419 (mapcar (lambda (from to-type
)
420 (if (or (eq (tn-kind from
) :unused
)
421 (eq (tn-primitive-type from
) to-type
))
423 (let ((temp (make-normal-tn to-type
)))
424 (emit-move node block from temp
)
429 ;;;; utilities for delivering values to lvars
431 ;;; Return a list of TNs with the specifier TYPES that can be used as
432 ;;; result TNs to evaluate an expression into LVAR. This is used
433 ;;; together with MOVE-LVAR-RESULT to deliver fixed values to
436 ;;; If the lvar isn't annotated (meaning the values are discarded) or
437 ;;; is unknown-values, then we make temporaries for each supplied
438 ;;; value, providing a place to compute the result in until we decide
439 ;;; what to do with it (if anything.)
441 ;;; If the lvar is fixed-values, and wants the same number of values
442 ;;; as the user wants to deliver, then we just return the
443 ;;; IR2-LVAR-LOCS. Otherwise we make a new list padded as necessary by
444 ;;; discarded TNs. We always return a TN of the specified type, using
445 ;;; the lvar locs only when they are of the correct type.
446 (defun lvar-result-tns (lvar types
)
447 (declare (type (or lvar null
) lvar
) (type list types
))
449 (mapcar #'make-normal-tn types
)
450 (let ((2lvar (lvar-info lvar
)))
451 (ecase (ir2-lvar-kind 2lvar
)
453 (let* ((locs (ir2-lvar-locs 2lvar
))
454 (nlocs (length locs
))
455 (ntypes (length types
)))
456 (if (and (= nlocs ntypes
)
457 (do ((loc locs
(cdr loc
))
458 (type types
(cdr type
)))
460 (unless (eq (tn-primitive-type (car loc
)) (car type
))
463 (mapcar (lambda (loc type
)
464 (if (eq (tn-primitive-type loc
) type
)
466 (make-normal-tn type
)))
469 (mapcar #'make-normal-tn
470 (subseq types nlocs
)))
474 (mapcar #'make-normal-tn types
))))))
476 ;;; Make the first N standard value TNs, returning them in a list.
477 (defun make-standard-value-tns (n)
478 (declare (type unsigned-byte n
))
481 (res (standard-arg-location i
)))
484 ;;; Return a list of TNs wired to the standard value passing
485 ;;; conventions that can be used to receive values according to the
486 ;;; unknown-values convention. This is used together with
487 ;;; MOVE-LVAR-RESULT for delivering unknown values to a fixed values
490 ;;; If the lvar isn't annotated, then we treat as 0-values, returning
491 ;;; an empty list of temporaries.
493 ;;; If the lvar is annotated, then it must be :FIXED.
494 (defun standard-result-tns (lvar)
495 (declare (type (or lvar null
) lvar
))
497 (let ((2lvar (lvar-info lvar
)))
498 (ecase (ir2-lvar-kind 2lvar
)
500 (make-standard-value-tns (length (ir2-lvar-locs 2lvar
))))))
503 ;;; Just move each SRC TN into the corresponding DEST TN, defaulting
504 ;;; any unsupplied source values to NIL. We let EMIT-MOVE worry about
505 ;;; doing the appropriate coercions.
506 (defun move-results-coerced (node block src dest
)
507 (declare (type node node
) (type ir2-block block
) (list src dest
))
508 (let ((nsrc (length src
))
509 (ndest (length dest
)))
510 (mapc (lambda (from to
)
511 (unless (or (eq from to
)
512 (eq (tn-kind to
) :unused
))
513 (emit-move node block from to
)))
515 (append src
(make-list (- ndest nsrc
)
516 :initial-element
(emit-constant nil
)))
521 ;;; If necessary, emit coercion code needed to deliver the RESULTS to
522 ;;; the specified lvar. NODE and BLOCK provide context for emitting
523 ;;; code. Although usually obtained from STANDARD-RESULT-TNs or
524 ;;; LVAR-RESULT-TNs, RESULTS may be a list of any type or
527 ;;; If the lvar is fixed values, then move the results into the lvar
528 ;;; locations. If the lvar is unknown values, then do the moves into
529 ;;; the standard value locations, and use PUSH-VALUES to put the
530 ;;; values on the stack.
531 (defun move-lvar-result (node block results lvar
)
532 (declare (type node node
) (type ir2-block block
)
533 (list results
) (type (or lvar null
) lvar
))
535 (let ((2lvar (lvar-info lvar
)))
536 ;; If LVAR flows through a CAST which is unused it won't get
537 ;; deleted and won't be annotated
539 (ecase (ir2-lvar-kind 2lvar
)
541 (let ((locs (ir2-lvar-locs 2lvar
)))
542 (unless (eq locs results
)
543 (move-results-coerced node block results locs
))))
545 (let* ((nvals (length results
))
546 (locs (make-standard-value-tns nvals
)))
547 (move-results-coerced node block results locs
)
548 (vop* push-values node block
549 ((reference-tn-list locs nil
))
550 ((reference-tn-list (ir2-lvar-locs 2lvar
) t
))
554 ;;; Doing this during IR2 conversion and not in ir1-optimize-cast
555 ;;; because of possible function redefinition and the need to signal a
556 ;;; style-warning and not a full warning.
557 ;;; If the cast is not deleted after a warning is signalled then it
558 ;;; might get signalled multiple times, and IR2 conversion happens
560 (defun check-functional-cast (cast)
561 (let ((value (cast-value cast
))
562 (atype (cast-asserted-type cast
)))
563 (cond ((function-designator-cast-p cast
)
564 (multiple-value-bind (type name leaf
) (lvar-fun-type value
)
565 (when (and (fun-type-p type
)
567 (let ((*valid-fun-use-name
* (function-designator-cast-caller cast
))
568 (*lossage-fun
* (callable-argument-lossage-kind name
570 #'compiler-style-warn
572 (*compiler-error-context
* cast
)
573 (dest (lvar-dest (cast-lvar cast
))))
574 (if (and (combination-p dest
)
575 (eq (lvar-fun-name (combination-fun dest
) t
)
576 *valid-fun-use-name
*))
577 (map-callable-arguments (lambda (lvar &rest args
)
578 (when (eq lvar
(cast-lvar cast
))
579 (apply #'valid-callable-argument value args
)))
581 ;; Coming from CALLABLE-CAST
582 (valid-callable-argument value
584 (function-designator-cast-arg-count cast
)))))))
586 (multiple-value-bind (type name leaf
) (lvar-fun-type value
)
587 (when (and (fun-type-p type
)
589 (let ((int (type-intersection type atype
)))
590 (when (or (memq *empty-type
* (fun-type-required int
))
591 (and (eq (fun-type-returns int
) *empty-type
*)
592 (neq (fun-type-returns type
) *empty-type
*)
593 (not (and (eq (fun-type-returns atype
) *empty-type
*)
594 (eq (fun-type-returns type
) *wild-type
*)))))
595 (%compile-time-type-error-warn cast
596 (type-specifier atype
)
597 (type-specifier type
)
600 (callable-argument-lossage-kind name
603 'type-warning
))))))))))
606 (defun ir2-convert-cast (node block
)
607 (declare (type cast node
)
608 (type ir2-block block
))
609 (binding* ((lvar (node-lvar node
) :exit-if-null
)
610 (2lvar (lvar-info lvar
))
611 (value (cast-value node
))
612 (2value (lvar-info value
)))
613 (check-functional-cast node
)
614 (when 2lvar
;; the cast can be unused but not deleted to due vestigial exits
615 (ecase (ir2-lvar-kind 2lvar
)
618 (aver (not (cast-type-check node
)))
619 (move-results-coerced node block
620 (ir2-lvar-locs 2value
)
621 (ir2-lvar-locs 2lvar
)))))))
623 (defoptimizer (%check-bound ir2-hook
) ((array bound index
) node block
)
624 (declare (ignore block
))
625 (when (constant-lvar-p bound
)
626 (let* ((bound-type (specifier-type `(integer 0 (,(lvar-value bound
)))))
627 (index-type (lvar-type index
)))
628 (when (eq (type-intersection bound-type index-type
)
630 (let ((*compiler-error-context
* node
))
631 (compiler-warn "Derived type ~s is not a suitable index for ~s."
632 (type-specifier index-type
)
633 (type-specifier (lvar-type array
))))))))
635 ;;;; template conversion
637 ;;; Build a TN-REFS list that represents access to the values of the
638 ;;; specified list of lvars ARGS for TEMPLATE. Any :CONSTANT arguments
639 ;;; are returned in the second value as a list rather than being
640 ;;; accessed as a normal argument. NODE and BLOCK provide the context
641 ;;; for emitting any necessary type-checking code.
642 (defun reference-args (node block args template
)
643 (declare (type node node
) (type ir2-block block
) (list args
)
644 (type template template
))
645 (collect ((info-args))
648 (do ((args args
(cdr args
))
649 (types (template-arg-types template
) (cdr types
)))
651 (let ((type (first types
))
653 (if (and (consp type
) (eq (car type
) ':constant
))
654 (info-args (lvar-value arg
))
655 (let ((ref (reference-tn (lvar-tn node block arg
) nil
)))
657 (setf (tn-ref-across last
) ref
)
661 (values (the (or tn-ref null
) first
) (info-args)))))
663 ;;; Convert a conditional template. We try to exploit any
664 ;;; drop-through, but emit an unconditional branch afterward if we
665 ;;; fail. NOT-P is true if the sense of the TEMPLATE's test should be
667 (defun ir2-convert-conditional (node block template args info-args if not-p
)
668 (declare (type node node
) (type ir2-block block
)
669 (type template template
) (type (or tn-ref null
) args
)
670 (list info-args
) (type cif if
) (type boolean not-p
))
671 (let ((consequent (if-consequent if
))
672 (alternative (if-alternative if
))
673 (flags (and (consp (template-result-types template
))
674 (rest (template-result-types template
)))))
675 (aver (= (template-info-arg-count template
)
676 (+ (length info-args
)
679 (rotatef consequent alternative
)
681 (when (drop-thru-p if consequent
)
682 (rotatef consequent alternative
)
685 (emit-template node block template args nil
686 (list* (block-label consequent
) not-p
688 (if (drop-thru-p if alternative
)
689 (register-drop-thru alternative
)
690 (vop branch node block
(block-label alternative
))))
692 (emit-template node block template args nil info-args
)
693 (vop branch-if node block
(block-label consequent
) flags not-p
)
694 (if (drop-thru-p if alternative
)
695 (register-drop-thru alternative
)
696 (vop branch node block
(block-label alternative
)))))))
698 ;;; Convert an IF that isn't the DEST of a conditional template.
699 (defun ir2-convert-if (node block
)
700 (declare (type ir2-block block
) (type cif node
))
701 (let* ((test (if-test node
))
702 (test-ref (reference-tn (lvar-tn node block test
) nil
))
703 (nil-ref (reference-tn (emit-constant nil
) nil
)))
704 (setf (tn-ref-across test-ref
) nil-ref
)
705 (ir2-convert-conditional node block
(template-or-lose 'if-eq
)
706 test-ref
() node t
)))
708 ;;; Return a list of primitive-types that we can pass to LVAR-RESULT-TNS
709 ;;; describing the result types we want for a template call. We are really
710 ;;; only interested in the number of results required: in normal case
711 ;;; TEMPLATE-RESULTS-OK has already checked them.
712 (defun find-template-result-types (call rtypes
)
713 (let* ((type (node-derived-type call
))
715 (mapcar #'primitive-type
716 (if (args-type-p type
)
717 (append (args-type-required type
)
718 (args-type-optional type
))
720 (primitive-t *backend-t-primitive-type
*))
721 (mapcar (lambda (rtype)
722 (declare (ignore rtype
))
723 (or (pop types
) primitive-t
)) rtypes
)))
725 ;;; Return a list of TNs usable in a CALL to TEMPLATE delivering values to
726 ;;; LVAR. As an efficiency hack, we pick off the common case where the LVAR is
727 ;;; fixed values and has locations that satisfy the result restrictions. This
728 ;;; can fail when there is a type check or a values count mismatch.
729 (defun make-template-result-tns (call lvar rtypes
)
730 (declare (type combination call
) (type (or lvar null
) lvar
)
732 (let ((2lvar (when lvar
(lvar-info lvar
))))
733 (if (and 2lvar
(eq (ir2-lvar-kind 2lvar
) :fixed
))
734 (let ((locs (ir2-lvar-locs 2lvar
)))
735 (if (and (= (length rtypes
) (length locs
))
736 (do ((loc locs
(cdr loc
))
737 (rtypes rtypes
(cdr rtypes
)))
739 (unless (and (neq (tn-kind (car loc
)) :unused
)
740 (operand-restriction-ok
742 (tn-primitive-type (car loc
))
748 (find-template-result-types call rtypes
))))
751 (find-template-result-types call rtypes
)))))
753 ;;; Get the operands into TNs, make TN-REFs for them, and then call
754 ;;; the template emit function.
755 (defun ir2-convert-template (call block
)
756 (declare (type combination call
) (type ir2-block block
))
757 (let* ((template (combination-info call
))
758 (lvar (node-lvar call
))
759 (rtypes (template-result-types template
)))
760 (multiple-value-bind (args info-args
)
761 (reference-args call block
(combination-args call
) template
)
762 (aver (not (template-more-results-type template
)))
763 (if (template-conditional-p template
)
764 (ir2-convert-conditional call block template args info-args
765 (lvar-dest lvar
) nil
)
766 (let* ((results (make-template-result-tns call lvar rtypes
))
767 (r-refs (reference-tn-list results t
)))
768 (aver (= (length info-args
)
769 (template-info-arg-count template
)))
770 (when (and lvar
(lvar-dynamic-extent lvar
))
771 (vop current-stack-pointer call block
772 (ir2-lvar-stack-pointer (lvar-info lvar
))))
773 (when (emit-step-p call
)
774 (vop sb
!vm
::step-instrument-before-vop call block
))
776 (emit-template call block template args r-refs info-args
)
777 (emit-template call block template args r-refs
))
778 (move-lvar-result call block results lvar
)))))
781 ;;; We don't have to do much because operand count checking is done by
782 ;;; IR1 conversion. The only difference between this and the function
783 ;;; case of IR2-CONVERT-TEMPLATE is that there can be codegen-info
785 (defoptimizer (%%primitive ir2-convert
) ((template info
&rest args
) call block
)
786 (declare (ignore args
))
787 (let* ((template (lvar-value template
))
788 (info (lvar-value info
))
789 (lvar (node-lvar call
))
790 (rtypes (template-result-types template
))
791 (results (make-template-result-tns call lvar rtypes
))
792 (r-refs (reference-tn-list results t
)))
793 (multiple-value-bind (args info-args
)
794 (reference-args call block
(cddr (combination-args call
)) template
)
795 (aver (not (template-more-results-type template
)))
796 (aver (not (template-conditional-p template
)))
797 (aver (null info-args
))
800 (emit-template call block template args r-refs info
)
801 (emit-template call block template args r-refs
))
803 (move-lvar-result call block results lvar
)))
806 (defoptimizer (%%primitive derive-type
) ((template info
&rest args
))
807 (declare (ignore info args
))
808 (let ((type (template-type (lvar-value template
))))
809 (if (fun-type-p type
)
810 (fun-type-returns type
)
815 ;;; Convert a LET by moving the argument values into the variables.
816 ;;; Since a LET doesn't have any passing locations, we move the
817 ;;; arguments directly into the variables. We must also allocate any
818 ;;; indirect value cells, since there is no function prologue to do
820 (defun ir2-convert-let (node block fun
)
821 (declare (type combination node
) (type ir2-block block
) (type clambda fun
))
822 (mapc (lambda (var arg
)
824 (let ((src (lvar-tn node block arg
))
825 (dest (leaf-info var
)))
826 (if (and (lambda-var-indirect var
)
827 (lambda-var-explicit-value-cell var
))
828 (emit-make-value-cell node block src dest
)
829 (emit-move node block src dest
)))))
830 (lambda-vars fun
) (basic-combination-args node
))
833 ;;; Emit any necessary moves into assignment temps for a local call to
834 ;;; FUN. We return two lists of TNs: TNs holding the actual argument
835 ;;; values, and (possibly EQ) TNs that are the actual destination of
836 ;;; the arguments. When necessary, we allocate temporaries for
837 ;;; arguments to preserve parallel assignment semantics. These lists
838 ;;; exclude unused arguments and include implicit environment
839 ;;; arguments, i.e. they exactly correspond to the arguments passed.
841 ;;; OLD-FP is the TN currently holding the value we want to pass as
842 ;;; OLD-FP. If null, then the call is to the same environment (an
843 ;;; :ASSIGNMENT), so we only move the arguments, and leave the
844 ;;; environment alone.
846 ;;; CLOSURE-FP is for calling a closure that has "implicit" value
847 ;;; cells (stored in the allocating stack frame), and is the frame
848 ;;; pointer TN to use for values allocated in the outbound stack
849 ;;; frame. This is distinct from OLD-FP for the specific case of a
851 (defun emit-psetq-moves (node block fun old-fp
&optional
(closure-fp old-fp
))
852 (declare (type combination node
) (type ir2-block block
) (type clambda fun
)
853 (type (or tn null
) old-fp closure-fp
))
854 (let ((actuals (mapcar (lambda (x)
856 (lvar-tn node block x
)))
857 (combination-args node
))))
860 (dolist (var (lambda-vars fun
))
861 (let ((actual (pop actuals
))
862 (loc (leaf-info var
)))
865 ((and (lambda-var-indirect var
)
866 (lambda-var-explicit-value-cell var
))
868 (make-normal-tn *backend-t-primitive-type
*)))
869 (emit-make-value-cell node block actual temp
)
871 ((member actual
(locs))
872 (let ((temp (make-normal-tn (tn-primitive-type loc
))))
873 (emit-move node block actual temp
)
880 (let ((this-1env (node-physenv node
))
881 (called-env (physenv-info (lambda-physenv fun
))))
882 (dolist (thing (ir2-physenv-closure called-env
))
883 (temps (closure-initial-value (car thing
) this-1env closure-fp
))
886 (locs (ir2-physenv-old-fp called-env
))))
888 (values (temps) (locs)))))
890 ;;; A tail-recursive local call is done by emitting moves of stuff
891 ;;; into the appropriate passing locations. After setting up the args
892 ;;; and environment, we just move our return-pc into the called
893 ;;; function's passing location.
894 (defun ir2-convert-tail-local-call (node block fun
)
895 (declare (type combination node
) (type ir2-block block
) (type clambda fun
))
896 (let ((this-env (physenv-info (node-physenv node
)))
897 (current-fp (make-stack-pointer-tn)))
898 (multiple-value-bind (temps locs
)
899 (emit-psetq-moves node block fun
900 (ir2-physenv-old-fp this-env
) current-fp
)
902 ;; If we're about to emit a move from CURRENT-FP then we need to
904 (when (find current-fp temps
)
905 (vop current-fp node block current-fp
))
907 (mapc (lambda (temp loc
)
908 (emit-move node block temp loc
))
911 (emit-move node block
912 (ir2-physenv-return-pc this-env
)
913 (ir2-physenv-return-pc-pass
915 (lambda-physenv fun
)))))
919 ;;; Convert an :ASSIGNMENT call. This is just like a tail local call,
920 ;;; except that the caller and callee environment are the same, so we
921 ;;; don't need to mess with the environment locations, return PC, etc.
922 (defun ir2-convert-assignment (node block fun
)
923 (declare (type combination node
) (type ir2-block block
) (type clambda fun
))
924 (multiple-value-bind (temps locs
) (emit-psetq-moves node block fun nil
)
926 (mapc (lambda (temp loc
)
927 (emit-move node block temp loc
))
931 ;;; Do stuff to set up the arguments to a non-tail local call
932 ;;; (including implicit environment args.) We allocate a frame
933 ;;; (returning the FP and NFP), and also compute the TN-REFS list for
934 ;;; the values to pass and the list of passing location TNs.
935 (defun ir2-convert-local-call-args (node block fun
)
936 (declare (type combination node
) (type ir2-block block
) (type clambda fun
))
937 (let ((fp (make-stack-pointer-tn))
938 (nfp (make-number-stack-pointer-tn))
939 (old-fp (make-stack-pointer-tn)))
940 (multiple-value-bind (temps locs
)
941 (emit-psetq-moves node block fun old-fp
)
942 (vop current-fp node block old-fp
)
943 (vop allocate-frame node block
944 (physenv-info (lambda-physenv fun
))
946 (values fp nfp temps
(mapcar #'make-alias-tn locs
)))))
948 ;;; Handle a non-TR known-values local call. We emit the call, then
949 ;;; move the results to the lvar's destination.
950 (defun ir2-convert-local-known-call (node block fun returns lvar start
)
951 (declare (type node node
) (type ir2-block block
) (type clambda fun
)
952 (type return-info returns
) (type (or lvar null
) lvar
)
954 (multiple-value-bind (fp nfp temps arg-locs
)
955 (ir2-convert-local-call-args node block fun
)
956 (let ((locs (return-info-locations returns
)))
957 (vop* known-call-local node block
958 (fp nfp
(reference-tn-list temps nil
))
959 ((reference-tn-list locs t
))
960 arg-locs
(physenv-info (lambda-physenv fun
)) start
)
961 (move-lvar-result node block locs lvar
)))
964 ;;; Handle a non-TR unknown-values local call. We do different things
965 ;;; depending on what kind of values the lvar wants.
967 ;;; If LVAR is :UNKNOWN, then we use the "multiple-" variant, directly
968 ;;; specifying the lvar's LOCS as the VOP results so that we don't
969 ;;; have to do anything after the call.
971 ;;; Otherwise, we use STANDARD-RESULT-TNS to get wired result TNs, and
972 ;;; then call MOVE-LVAR-RESULT to do any necessary type checks or
974 (defun ir2-convert-local-unknown-call (node block fun lvar start
)
975 (declare (type node node
) (type ir2-block block
) (type clambda fun
)
976 (type (or lvar null
) lvar
) (type label start
))
977 (multiple-value-bind (fp nfp temps arg-locs
)
978 (ir2-convert-local-call-args node block fun
)
979 (let ((2lvar (and lvar
(lvar-info lvar
)))
980 (env (physenv-info (lambda-physenv fun
)))
981 (temp-refs (reference-tn-list temps nil
)))
982 (if (and 2lvar
(eq (ir2-lvar-kind 2lvar
) :unknown
))
983 (vop* multiple-call-local node block
(fp nfp temp-refs
)
984 ((reference-tn-list (ir2-lvar-locs 2lvar
) t
))
986 (let ((locs (standard-result-tns lvar
)))
987 (vop* call-local node block
989 ((reference-tn-list locs t
))
990 arg-locs env start
(length locs
))
991 (move-lvar-result node block locs lvar
)))))
994 ;;; Dispatch to the appropriate function, depending on whether we have
995 ;;; a let, tail or normal call. If the function doesn't return, call
996 ;;; it using the unknown-value convention. We could compile it as a
997 ;;; tail call, but that might seem confusing in the debugger.
998 (defun ir2-convert-local-call (node block
)
999 (declare (type combination node
) (type ir2-block block
))
1000 (let* ((fun (ref-leaf (lvar-uses (basic-combination-fun node
))))
1001 (kind (functional-kind fun
)))
1002 (cond ((eq kind
:let
)
1003 (ir2-convert-let node block fun
))
1004 ((eq kind
:assignment
)
1005 (ir2-convert-assignment node block fun
))
1007 (ir2-convert-tail-local-call node block fun
))
1009 (let ((start (block-trampoline (lambda-block fun
)))
1010 (returns (tail-set-info (lambda-tail-set fun
)))
1011 (lvar (node-lvar node
)))
1013 (return-info-kind returns
)
1016 (ir2-convert-local-unknown-call node block fun lvar start
))
1018 (ir2-convert-local-known-call node block fun returns
1024 ;;; Given a function lvar FUN, return (VALUES TN-TO-CALL NAMED-P),
1025 ;;; where TN-TO-CALL is a TN holding the thing that we call NAMED-P is
1026 ;;; true if the thing is named (false if it is a function).
1028 ;;; There are two interesting non-named cases:
1029 ;;; -- We know it's a function. No check needed: return the
1031 ;;; -- We don't know what it is.
1032 (defun fun-lvar-tn (node block lvar
)
1033 (declare (ignore node block
))
1034 (declare (type lvar lvar
))
1035 (let ((2lvar (lvar-info lvar
)))
1036 (if (eq (ir2-lvar-kind 2lvar
) :delayed
)
1037 (let ((name (lvar-fun-name lvar t
)))
1039 (values (cond ((sb!vm
::static-fdefn-offset name
)
1042 ;; Named call to an immobile fdefn from an immobile component
1043 ;; uses the FUN-TN only to preserve liveness of the fdefn.
1044 ;; The name becomes an info arg.
1045 (make-load-time-constant-tn :fdefinition name
)))
1047 (let* ((locs (ir2-lvar-locs 2lvar
))
1049 (function-ptype (primitive-type-or-lose 'function
)))
1050 (aver (and (eq (ir2-lvar-kind 2lvar
) :fixed
)
1051 (= (length locs
) 1)))
1052 (aver (eq (tn-primitive-type loc
) function-ptype
))
1053 (values loc nil
)))))
1055 ;;; Set up the args to NODE in the current frame, and return a TN-REF
1056 ;;; list for the passing locations.
1057 (defun move-tail-full-call-args (node block
)
1058 (declare (type combination node
) (type ir2-block block
))
1059 (let ((args (basic-combination-args node
))
1062 (dotimes (num (length args
))
1063 (let ((loc (standard-arg-location num
)))
1064 (emit-move node block
(lvar-tn node block
(elt args num
)) loc
)
1065 (let ((ref (reference-tn loc nil
)))
1067 (setf (tn-ref-across last
) ref
)
1072 ;;; Move the arguments into the passing locations and do a (possibly
1073 ;;; named) tail call.
1074 (defun ir2-convert-tail-full-call (node block
)
1075 (declare (type combination node
) (type ir2-block block
))
1076 (let* ((env (physenv-info (node-physenv node
)))
1077 (args (basic-combination-args node
))
1078 (nargs (length args
))
1079 (pass-refs (move-tail-full-call-args node block
))
1080 (old-fp (ir2-physenv-old-fp env
))
1081 (return-pc (ir2-physenv-return-pc env
)))
1083 (multiple-value-bind (fun-tn named
)
1084 (fun-lvar-tn node block
(basic-combination-fun node
))
1086 (vop* tail-call node block
1087 (fun-tn old-fp return-pc pass-refs
)
1089 nargs
(emit-step-p node
)))
1092 (vop* static-tail-call-named node block
1093 (old-fp return-pc pass-refs
) ; args
1095 nargs named
(emit-step-p node
)))
1097 (vop* tail-call-named node block
1098 (#!-immobile-code fun-tn old-fp return-pc pass-refs
) ; args
1100 nargs
#!+immobile-code named
(emit-step-p node
)))))) ; info
1103 ;;; like IR2-CONVERT-LOCAL-CALL-ARGS, only different
1104 (defun ir2-convert-full-call-args (node block
)
1105 (declare (type combination node
) (type ir2-block block
))
1106 (let* ((args (basic-combination-args node
))
1107 (nargs (length args
))
1108 (fp (make-stack-pointer-tn nargs
)))
1109 (vop allocate-full-call-frame node block nargs fp
)
1113 (dotimes (num nargs
)
1114 (locs (standard-arg-location num
))
1115 (let ((ref (reference-tn (lvar-tn node block
(elt args num
))
1118 (setf (tn-ref-across last
) ref
)
1122 (values fp first
(locs) nargs
)))))
1124 ;;; Do full call when a fixed number of values are desired. We make
1125 ;;; STANDARD-RESULT-TNS for our lvar, then deliver the result using
1126 ;;; MOVE-LVAR-RESULT. We do named or normal call, as appropriate.
1127 (defun ir2-convert-fixed-full-call (node block
)
1128 (declare (type combination node
) (type ir2-block block
))
1129 (multiple-value-bind (fp args arg-locs nargs
)
1130 (ir2-convert-full-call-args node block
)
1131 (let* ((lvar (node-lvar node
))
1133 (loop for loc in
(ir2-lvar-locs (lvar-info lvar
))
1135 collect
(cond ((eql (tn-kind loc
) :unused
)
1137 #!+(or x86-64 arm64
) ;; needs default-unknown-values support
1138 ((>= i sb
!vm
::register-arg-count
)
1139 (make-normal-tn *backend-t-primitive-type
*))
1141 (standard-arg-location i
))))))
1142 (loc-refs (reference-tn-list locs t
))
1143 (nvals (length locs
)))
1144 (multiple-value-bind (fun-tn named
)
1145 (fun-lvar-tn node block
(basic-combination-fun node
))
1147 (vop* call node block
(fp fun-tn args
) (loc-refs)
1148 arg-locs nargs nvals
(emit-step-p node
)))
1151 (vop* static-call-named node block
1154 arg-locs nargs named nvals
1155 (emit-step-p node
)))
1157 (vop* call-named node block
1158 (fp #!-immobile-code fun-tn args
) ; args
1159 (loc-refs) ; results
1160 arg-locs nargs
#!+immobile-code named nvals
; info
1161 (emit-step-p node
))))
1162 (move-lvar-result node block locs lvar
))))
1165 ;;; Do full call when unknown values are desired.
1166 (defun ir2-convert-multiple-full-call (node block
)
1167 (declare (type combination node
) (type ir2-block block
))
1168 (multiple-value-bind (fp args arg-locs nargs
)
1169 (ir2-convert-full-call-args node block
)
1170 (let* ((lvar (node-lvar node
))
1171 (locs (ir2-lvar-locs (lvar-info lvar
)))
1172 (loc-refs (reference-tn-list locs t
)))
1173 (multiple-value-bind (fun-tn named
)
1174 (fun-lvar-tn node block
(basic-combination-fun node
))
1176 (vop* multiple-call node block
(fp fun-tn args
) (loc-refs)
1177 arg-locs nargs
(emit-step-p node
)))
1180 (vop* static-multiple-call-named node block
1183 arg-locs nargs named
1184 (emit-step-p node
)))
1186 (vop* multiple-call-named node block
1187 (fp #!-immobile-code fun-tn args
) ; args
1188 (loc-refs) ; results
1189 arg-locs nargs
#!+immobile-code named
; info
1190 (emit-step-p node
)))))))
1193 ;;; stuff to check in PONDER-FULL-CALL
1195 ;;; These came in handy when troubleshooting cold boot after making
1196 ;;; major changes in the package structure: various transforms and
1197 ;;; VOPs and stuff got attached to the wrong symbol, so that
1198 ;;; references to the right symbol were bogusly translated as full
1199 ;;; calls instead of primitives, sending the system off into infinite
1200 ;;; space. Having a report on all full calls generated makes it easier
1201 ;;; to figure out what form caused the problem this time.
1202 (declaim (type (member :minimal
:detailed
:very-detailed
:maximal
)
1203 *track-full-called-fnames
*))
1204 (defvar *track-full-called-fnames
* :minimal
)
1206 ;;; Do some checks (and store some notes relevant for future checks)
1208 ;;; * Is this a full call to something we have reason to know should
1209 ;;; never be full called? (Except as of sbcl-0.7.18 or so, we no
1210 ;;; longer try to ensure this behavior when *FAILURE-P* has already
1212 (defun ponder-full-call (node)
1213 (let* ((lvar (basic-combination-fun node
))
1214 (fname (lvar-fun-name lvar t
)))
1215 (declare (type (or symbol cons
) fname
))
1217 (when (and (symbolp fname
)
1218 (eq (symbol-package fname
) *cl-package
*))
1219 ;; Never produce a warning from (DECLARE (INLINE LENGTH)) etc
1220 (return-from ponder-full-call
))
1222 ;; Warn about cross-compiling certain full-calls,
1223 ;; as it is indicative of dependency order problems.
1225 (let ((compname (component-name (node-component node
))))
1226 ;; Don't care too much about macro performance.
1227 (unless (and (stringp compname
) (string/= compname
"DEFMACRO"))
1228 ;; Catch FOO and (SETF FOO) both.
1229 (let ((stem (if (atom fname
) fname
(second fname
))))
1230 (when (member stem
*full-calls-to-warn-about
* :test
#'string
=)
1231 (warn "Full call to ~S" fname
)))))
1233 (let* ((inlineable-p (not (let ((*lexenv
* (node-lexenv node
)))
1234 (fun-lexically-notinline-p fname
))))
1235 (inlineable-bit (if inlineable-p
1 0))
1236 (cell (info :function
:emitted-full-calls fname
)))
1238 ;; The low bit indicates whether any not-NOTINLINE call was seen.
1239 ;; The next-lowest bit is magic. Refer to %COMPILER-DEFMACRO
1240 ;; and WARN-IF-INLINE-FAILED/CALL for the pertinent logic.
1241 (setf cell
(list (logior 4 inlineable-bit
))
1242 (info :function
:emitted-full-calls fname
) cell
)
1243 (incf (car cell
) (+ 4 (if (oddp (car cell
)) 0 inlineable-bit
))))
1244 ;; If the full call was wanted, don't record anything.
1245 ;; (This was originally for debugging SBCL self-compilation)
1248 (warn-if-inline-failed/call fname
(node-lexenv node
) cell
))
1249 (case *track-full-called-fnames
*
1251 (when (boundp 'sb
!xc
:*compile-file-pathname
*)
1252 (pushnew sb
!xc
:*compile-file-pathname
* (cdr cell
)
1255 (pushnew (component-name *component-being-compiled
*)
1256 (cdr cell
) :test
#'equalp
)))))
1258 ;; Special mode, usually only for the cross-compiler
1259 ;; and only with the feature enabled.
1260 #!+sb-show
(when (eq *track-full-called-fnames
* :maximal
)
1261 (/show
"converting full call to named function" fname
)
1262 (/show
(basic-combination-args node
))
1263 (/show
(policy node speed
) (policy node safety
))
1264 (/show
(policy node compilation-speed
))
1265 (let ((arg-types (mapcar (lambda (lvar)
1269 (basic-combination-args node
))))
1272 ;; When illegal code is compiled, all sorts of perverse paths
1273 ;; through the compiler can be taken, and it's much harder -- and
1274 ;; probably pointless -- to guarantee that always-optimized-away
1275 ;; functions are actually optimized away. Thus, we skip the check
1278 ;; check to see if we know anything about the function
1279 (let ((info (info :function
:info fname
)))
1280 ;; if we know something, check to see if the full call was valid
1281 (when (and info
(ir1-attributep (fun-info-attributes info
)
1282 always-translatable
))
1283 (/show
(policy node speed
) (policy node safety
))
1284 (/show
(policy node compilation-speed
))
1285 (bug "full call to ~S" fname
))))
1288 (aver (legal-fun-name-p fname
))))) ;; FIXME: needless check?
1290 ;;; If the call is in a tail recursive position and the return
1291 ;;; convention is standard, then do a tail full call. If one or fewer
1292 ;;; values are desired, then use a single-value call, otherwise use a
1293 ;;; multiple-values call.
1294 (defun ir2-convert-full-call (node block
)
1295 (declare (type combination node
) (type ir2-block block
))
1296 (ponder-full-call node
)
1297 (cond ((node-tail-p node
)
1298 (ir2-convert-tail-full-call node block
))
1299 ((let ((lvar (node-lvar node
)))
1301 (eq (ir2-lvar-kind (lvar-info lvar
)) :unknown
)))
1302 (ir2-convert-multiple-full-call node block
))
1304 (ir2-convert-fixed-full-call node block
)))
1307 ;;;; entering functions
1308 (defun xep-verify-arg-count (node block fun arg-count-location
)
1309 (when (policy fun
(plusp verify-arg-count
))
1310 (let* ((ef (functional-entry-fun fun
))
1311 (optional (optional-dispatch-p ef
))
1313 (optional-dispatch-min-args ef
)))
1314 (max (cond ((not optional
)
1315 (1- (length (lambda-vars fun
))))
1317 (not (optional-dispatch-more-entry ef
)))
1318 (optional-dispatch-max-args ef
)))))
1319 (unless (and (eql min
0) (not max
))
1320 (vop verify-arg-count node block
1326 ;;; Do all the stuff that needs to be done on XEP entry:
1327 ;;; -- Create frame.
1328 ;;; -- Copy any more arg.
1329 ;;; -- Set up the environment, accessing any closure variables.
1330 ;;; -- Move args from the standard passing locations to their internal
1332 (defun init-xep-environment (node block fun
)
1333 (declare (type bind node
) (type ir2-block block
) (type clambda fun
))
1334 (let ((start-label (entry-info-offset (leaf-info fun
)))
1335 (env (physenv-info (node-physenv node
)))
1337 (let ((ef (functional-entry-fun fun
)))
1338 (vop xep-allocate-frame node block start-label
)
1339 ;; Arg verification needs to be done before the stack pointer is adjusted
1340 ;; so that the extra arguments are still present when the error is signalled
1341 #!-precise-arg-count-error
1342 (vop xep-setup-sp node block
)
1343 (let ((verified (unless (eq (functional-kind fun
) :toplevel
)
1344 (setf arg-count-tn
(make-arg-count-location))
1345 (xep-verify-arg-count node block fun arg-count-tn
))))
1347 (declare (ignore verified
))
1348 (cond ((and (optional-dispatch-p ef
)
1349 (optional-dispatch-more-entry ef
)
1350 (neq (functional-kind (optional-dispatch-more-entry ef
)) :deleted
))
1351 ;; COPY-MORE-ARG does the job of XEP-SETUP-SP on +precise-arg-count-error
1352 (vop copy-more-arg node block
(optional-dispatch-max-args ef
)
1353 #!+x86-64 verified
))
1354 #!+precise-arg-count-error
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
1568 (primitive-type (leaf-type (pop vars
)))
1569 *backend-t-primitive-type
*))))))
1573 ;;; Emit the appropriate fixed value, unknown value or tail variant of
1574 ;;; CALL-VARIABLE. Note that we only need to pass the values start for
1575 ;;; the first argument: all the other argument lvar TNs are
1576 ;;; ignored. This is because we require all of the values globs to be
1577 ;;; contiguous and on stack top.
1578 (defun ir2-convert-mv-call (node block
)
1579 (declare (type mv-combination node
) (type ir2-block block
))
1580 (aver (basic-combination-args node
))
1581 (let* ((start-lvar (lvar-info (first (basic-combination-args node
))))
1582 (start (first (ir2-lvar-locs start-lvar
)))
1583 (tails (and (node-tail-p node
)
1584 (lambda-tail-set (node-home-lambda node
))))
1585 (lvar (node-lvar node
))
1586 (2lvar (and lvar
(lvar-info lvar
))))
1587 (multiple-value-bind (fun named
)
1588 (fun-lvar-tn node block
(basic-combination-fun node
))
1589 (aver (and (not named
)
1590 (eq (ir2-lvar-kind start-lvar
) :unknown
)))
1593 (let ((env (physenv-info (node-physenv node
))))
1594 (vop tail-call-variable node block start fun
1595 (ir2-physenv-old-fp env
)
1596 (ir2-physenv-return-pc env
))))
1598 (eq (ir2-lvar-kind 2lvar
) :unknown
))
1599 (vop* multiple-call-variable node block
(start fun nil
)
1600 ((reference-tn-list (ir2-lvar-locs 2lvar
) t
))
1601 (emit-step-p node
)))
1603 (let ((locs (standard-result-tns lvar
)))
1604 (vop* call-variable node block
(start fun nil
)
1605 ((reference-tn-list locs t
)) (length locs
)
1607 (move-lvar-result node block locs lvar
)))))))
1609 ;;; Reset the stack pointer to the start of the specified
1610 ;;; unknown-values lvar (discarding it and all values globs on top of
1612 (defoptimizer (%pop-values ir2-convert
) ((%lvar
) node block
)
1613 (let* ((lvar (lvar-value %lvar
))
1614 (2lvar (lvar-info lvar
)))
1615 (cond ((eq (ir2-lvar-kind 2lvar
) :unknown
)
1616 (vop reset-stack-pointer node block
1617 (first (ir2-lvar-locs 2lvar
))))
1618 ((lvar-dynamic-extent lvar
)
1619 (vop reset-stack-pointer node block
1620 (ir2-lvar-stack-pointer 2lvar
)))
1621 (t (bug "Trying to pop a not stack-allocated LVAR ~S."
1624 (defoptimizer (%nip-values ir2-convert
) ((last-nipped last-preserved
1627 (let* ( ;; pointer immediately after the nipped block
1628 (after (lvar-value last-nipped
))
1629 (2after (lvar-info after
))
1630 ;; pointer to the first nipped word
1631 (first (lvar-value last-preserved
))
1632 (2first (lvar-info first
))
1634 (moved-tns (loop for lvar-ref in moved
1635 for lvar
= (lvar-value lvar-ref
)
1636 for
2lvar
= (lvar-info lvar
)
1638 collect
(first (ir2-lvar-locs 2lvar
)))))
1639 (aver (or (eq (ir2-lvar-kind 2after
) :unknown
)
1640 (lvar-dynamic-extent after
)))
1641 (aver (eq (ir2-lvar-kind 2first
) :unknown
))
1642 (when *check-consistency
*
1643 ;; we cannot move stack-allocated DX objects
1644 (dolist (moved-lvar moved
)
1645 (aver (eq (ir2-lvar-kind (lvar-info (lvar-value moved-lvar
)))
1647 (flet ((nip-aligned (nipped)
1648 (vop* %%nip-values node block
1650 (first (ir2-lvar-locs 2first
))
1651 (reference-tn-list moved-tns nil
))
1652 ((reference-tn-list moved-tns t
)))))
1653 (cond ((eq (ir2-lvar-kind 2after
) :unknown
)
1654 (nip-aligned (first (ir2-lvar-locs 2after
))))
1655 ((lvar-dynamic-extent after
)
1656 (nip-aligned (ir2-lvar-stack-pointer 2after
)))
1658 (bug "Trying to nip a not stack-allocated LVAR ~S." after
))))))
1660 (defoptimizer (%dummy-dx-alloc ir2-convert
) ((target source
) node block
)
1661 (let* ((target-lvar (lvar-value target
))
1662 (source-lvar (lvar-value source
))
1663 (target-2lvar (lvar-info target-lvar
))
1664 (source-2lvar (and source-lvar
(lvar-info source-lvar
))))
1665 (aver (lvar-dynamic-extent target-lvar
))
1666 (cond ((not source-lvar
)
1667 (vop current-stack-pointer node block
1668 (ir2-lvar-stack-pointer target-2lvar
)))
1669 ((lvar-dynamic-extent source-lvar
)
1670 (emit-move node block
1671 (ir2-lvar-stack-pointer source-2lvar
)
1672 (ir2-lvar-stack-pointer target-2lvar
)))
1673 ((eq (ir2-lvar-kind source-2lvar
) :unknown
)
1674 (emit-move node block
1675 (first (ir2-lvar-locs source-2lvar
))
1676 (ir2-lvar-stack-pointer target-2lvar
)))
1677 (t (bug "Trying to dummy up DX allocation from a ~
1678 not stack-allocated LVAR ~S." source-lvar
)))))
1680 ;;; Deliver the values TNs to LVAR using MOVE-LVAR-RESULT.
1681 (defoptimizer (values ir2-convert
) ((&rest values
) node block
)
1682 (let ((tns (mapcar (lambda (x)
1683 (lvar-tn node block x
))
1686 (move-lvar-result node block tns
(node-lvar node
))))
1688 ;;; In the normal case where unknown values are desired, we use the
1689 ;;; VALUES-LIST VOP. In the relatively unimportant case of VALUES-LIST
1690 ;;; for a fixed number of values, we punt by doing a full call to the
1691 ;;; VALUES-LIST function. This gets the full call VOP to deal with
1692 ;;; defaulting any unsupplied values. It seems unworthwhile to
1693 ;;; optimize this case.
1694 (defoptimizer (values-list ir2-convert
) ((list) node block
)
1695 (let* ((lvar (node-lvar node
))
1696 (2lvar (and lvar
(lvar-info lvar
))))
1698 (eq (ir2-lvar-kind 2lvar
) :unknown
))
1699 (let ((locs (ir2-lvar-locs 2lvar
)))
1700 (vop* values-list node block
1701 ((lvar-tn node block list
) nil
)
1702 ((reference-tn-list locs t
)))))
1703 (t (aver (or (not 2lvar
) ; i.e. we want to check the argument
1704 (eq (ir2-lvar-kind 2lvar
) :fixed
)))
1705 (ir2-convert-full-call node block
)))))
1707 (defoptimizer (%more-arg-values ir2-convert
) ((context start count
) node block
)
1708 (binding* ((lvar (node-lvar node
) :exit-if-null
)
1709 (2lvar (lvar-info lvar
)))
1710 (ecase (ir2-lvar-kind 2lvar
)
1712 ;; KLUDGE: this is very much unsafe, and can leak random stack values.
1713 ;; OTOH, I think the :FIXED case can only happen with (safety 0) in the
1716 (loop for loc in
(ir2-lvar-locs 2lvar
)
1718 do
(vop sb
!vm
::more-arg node block
1719 (lvar-tn node block context
)
1723 (let ((locs (ir2-lvar-locs 2lvar
)))
1724 (vop* %more-arg-values node block
1725 ((lvar-tn node block context
)
1726 (lvar-tn node block start
)
1727 (lvar-tn node block count
)
1729 ((reference-tn-list locs t
))))))))
1731 ;;;; special binding
1733 ;;; This is trivial, given our assumption of a shallow-binding
1735 (defoptimizer (%special-bind ir2-convert
) ((var value
) node block
)
1736 (let ((name (leaf-source-name (lvar-value var
))))
1737 ;; Emit either BIND or DYNBIND, preferring BIND if both exist.
1738 ;; If only one exists, it's DYNBIND.
1739 ;; Even if the backend supports load-time TLS index assignment,
1740 ;; there might be only one vop (as with arm64).
1741 (macrolet ((doit (bind dynbind
)
1742 (if (gethash 'bind
*backend-parsed-vops
*) bind dynbind
)))
1745 ;; Inform later SYMBOL-VALUE calls that they can
1746 ;; assume a nonzero tls-index.
1747 ;; FIXME: setting INFO is inefficient when not actually
1748 ;; changing anything
1749 (unless (info :variable
:wired-tls name
)
1750 (setf (info :variable
:wired-tls name
) t
))
1751 ;; We force the symbol into the code constants in case BIND
1752 ;; does not actually reference it, as with immobile symbols.
1753 (emit-constant name
)
1754 (vop bind node block
(lvar-tn node block value
) name
))
1755 (vop dynbind node block
(lvar-tn node block value
)
1756 (emit-constant name
))))))
1758 (defoptimizer (%special-unbind ir2-convert
) ((&rest symbols
) node block
)
1759 (declare (ignorable symbols
))
1760 (vop unbind node block
#!+(and sb-thread unbind-n-vop
)
1761 (mapcar #'lvar-value symbols
)))
1763 ;;; ### It's not clear that this really belongs in this file, or
1764 ;;; should really be done this way, but this is the least violation of
1765 ;;; abstraction in the current setup. We don't want to wire
1766 ;;; shallow-binding assumptions into IR1tran.
1767 (def-ir1-translator progv
1768 ((vars vals
&body body
) start next result
)
1771 (with-unique-names (bind unbind
)
1772 (once-only ((n-save-bs '(%primitive current-binding-pointer
)))
1775 (labels ((,unbind
(vars)
1776 (declare (optimize (speed 2) (debug 0)))
1777 (let ((unbound-marker (%primitive make-unbound-marker
)))
1779 ;; CLHS says "bound and then made to have no value" -- user
1780 ;; should not be able to tell the difference between that and this.
1781 (about-to-modify-symbol-value var
'progv
)
1782 (%primitive dynbind unbound-marker var
))))
1784 (declare (optimize (speed 2) (debug 0)
1785 (insert-debug-catch 0)))
1787 ((null vals
) (,unbind vars
))
1789 (let ((val (car vals
))
1791 (about-to-modify-symbol-value var
'progv val t
)
1792 (%primitive dynbind val var
))
1793 (,bind
(cdr vars
) (cdr vals
))))))
1794 (,bind
,vars
,vals
))
1797 ;; Technically ANSI CL doesn't allow declarations at the
1798 ;; start of the cleanup form. SBCL happens to allow for
1799 ;; them, due to the way the UNWIND-PROTECT ir1 translation
1800 ;; is implemented; the cleanup forms are directly spliced
1801 ;; into an FLET definition body. And a declaration here
1802 ;; actually has exactly the right scope for what we need
1803 ;; (ensure that debug instrumentation is not emitted for the
1804 ;; cleanup function). -- JES, 2007-06-16
1805 (declare (optimize (insert-debug-catch 0)))
1806 (%primitive unbind-to-here
,n-save-bs
))))))
1810 ;;; Convert a non-local lexical exit. First find the NLX-INFO in our
1811 ;;; environment. Note that this is never called on the escape exits
1812 ;;; for CATCH and UNWIND-PROTECT, since the escape functions aren't
1814 (defun ir2-convert-exit (node block
)
1815 (declare (type exit node
) (type ir2-block block
))
1816 (let* ((nlx (exit-nlx-info node
))
1817 (loc (find-in-physenv nlx
(node-physenv node
)))
1818 (temp (make-stack-pointer-tn))
1819 (value (exit-value node
)))
1820 (if (nlx-info-safe-p nlx
)
1821 (vop value-cell-ref node block loc temp
)
1822 (emit-move node block loc temp
))
1824 (let ((locs (ir2-lvar-locs (lvar-info value
))))
1825 (vop unwind node block temp
(first locs
) (second locs
)))
1826 (let ((0-tn (emit-constant 0)))
1827 (vop unwind node block temp
0-tn
0-tn
))))
1831 ;;; %CLEANUP-POINT doesn't do anything except prevent the body from
1832 ;;; being entirely deleted.
1833 (defoptimizer (%cleanup-point ir2-convert
) ((&rest args
) node block
) args node block
)
1835 ;;; This function invalidates a lexical exit on exiting from the
1836 ;;; dynamic extent. This is done by storing 0 into the indirect value
1837 ;;; cell that holds the closed unwind block.
1838 (defoptimizer (%lexical-exit-breakup ir2-convert
) ((info) node block
)
1839 (let ((nlx (lvar-value info
)))
1840 (when (nlx-info-safe-p nlx
)
1841 (vop value-cell-set node block
1842 (find-in-physenv nlx
(node-physenv node
))
1843 (emit-constant 0)))))
1845 ;;; We have to do a spurious move of no values to the result lvar so
1846 ;;; that lifetime analysis won't get confused.
1847 (defun ir2-convert-throw (node block
)
1848 (declare (type mv-combination node
) (type ir2-block block
))
1849 (let ((args (basic-combination-args node
)))
1850 (check-catch-tag-type (first args
))
1851 (vop* throw node block
1852 ((lvar-tn node block
(first args
))
1854 (ir2-lvar-locs (lvar-info (second args
)))
1857 (move-lvar-result node block
() (node-lvar node
))
1860 ;;; Emit code to set up a non-local exit. INFO is the NLX-INFO for the
1861 ;;; exit, and TAG is the lvar for the catch tag (if any.) We get at
1862 ;;; the target PC by passing in the label to the vop. The vop is
1863 ;;; responsible for building a return-PC object.
1864 (defun emit-nlx-start (node block info tag
)
1865 (declare (type node node
) (type ir2-block block
) (type nlx-info info
)
1866 (type (or lvar null
) tag
))
1867 (let* ((2info (nlx-info-info info
))
1868 (kind (cleanup-kind (nlx-info-cleanup info
)))
1869 (block-tn (physenv-live-tn
1871 (primitive-type-or-lose
1875 ((:unwind-protect
:block
:tagbody
)
1877 (node-physenv node
)))
1878 (res (make-stack-pointer-tn))
1879 (target-label (ir2-nlx-info-target 2info
)))
1881 (vop current-binding-pointer node block
1882 (car (ir2-nlx-info-dynamic-state 2info
)))
1883 (vop* save-dynamic-state node block
1885 ((reference-tn-list (cdr (ir2-nlx-info-dynamic-state 2info
)) t
)))
1886 (vop current-stack-pointer node block
(ir2-nlx-info-save-sp 2info
))
1890 (vop make-catch-block node block block-tn
1891 (lvar-tn node block tag
) target-label res
))
1892 ((:unwind-protect
:block
:tagbody
)
1893 (vop make-unwind-block node block block-tn target-label res
)))
1897 (if (nlx-info-safe-p info
)
1898 (emit-make-value-cell node block res
(ir2-nlx-info-home 2info
))
1899 (emit-move node block res
(ir2-nlx-info-home 2info
))))
1901 (vop set-unwind-protect node block block-tn
))
1906 ;;; Scan each of ENTRY's exits, setting up the exit for each lexical exit.
1907 (defun ir2-convert-entry (node block
)
1908 (declare (type entry node
) (type ir2-block block
))
1910 (dolist (exit (entry-exits node
))
1911 (let ((info (exit-nlx-info exit
)))
1913 (not (memq info nlxes
))
1914 (member (cleanup-kind (nlx-info-cleanup info
))
1915 '(:block
:tagbody
)))
1917 (emit-nlx-start node block info nil
)))))
1920 ;;; Set up the unwind block for these guys.
1921 (defoptimizer (%catch ir2-convert
) ((info-lvar tag
) node block
)
1922 (check-catch-tag-type tag
)
1923 (emit-nlx-start node block
(lvar-value info-lvar
) tag
))
1924 (defoptimizer (%unwind-protect ir2-convert
) ((info-lvar cleanup
) node block
)
1925 (declare (ignore cleanup
))
1926 (emit-nlx-start node block
(lvar-value info-lvar
) nil
))
1928 ;;; Emit the entry code for a non-local exit. We receive values and
1929 ;;; restore dynamic state.
1931 ;;; In the case of a lexical exit or CATCH, we look at the exit lvar's
1932 ;;; kind to determine which flavor of entry VOP to emit. If unknown
1933 ;;; values, emit the xxx-MULTIPLE variant to the lvar locs. If fixed
1934 ;;; values, make the appropriate number of temps in the standard
1935 ;;; values locations and use the other variant, delivering the temps
1936 ;;; to the lvar using MOVE-LVAR-RESULT.
1938 ;;; In the UNWIND-PROTECT case, we deliver the first register
1939 ;;; argument, the argument count and the argument pointer to our lvar
1940 ;;; as multiple values. These values are the block exited to and the
1941 ;;; values start and count.
1943 ;;; After receiving values, we restore dynamic state. Except in the
1944 ;;; UNWIND-PROTECT case, the values receiving restores the stack
1945 ;;; pointer. In an UNWIND-PROTECT cleanup, we want to leave the stack
1946 ;;; pointer alone, since the thrown values are still out there.
1947 (defoptimizer (%nlx-entry ir2-convert
) ((info-lvar) node block
)
1948 (let* ((info (lvar-value info-lvar
))
1949 (lvar (node-lvar node
))
1950 (2info (nlx-info-info info
))
1951 (top-loc (ir2-nlx-info-save-sp 2info
))
1952 (start-loc (make-nlx-entry-arg-start-location))
1953 (count-loc (make-arg-count-location))
1954 (target (ir2-nlx-info-target 2info
)))
1956 (ecase (cleanup-kind (nlx-info-cleanup info
))
1957 ((:catch
:block
:tagbody
)
1958 (let ((2lvar (and lvar
(lvar-info lvar
))))
1959 (if (and 2lvar
(eq (ir2-lvar-kind 2lvar
) :unknown
))
1960 (vop* nlx-entry-multiple node block
1961 (top-loc start-loc count-loc nil
)
1962 ((reference-tn-list (ir2-lvar-locs 2lvar
) t
))
1964 (let ((locs (standard-result-tns lvar
)))
1965 (vop* nlx-entry node block
1966 (top-loc start-loc count-loc nil
)
1967 ((reference-tn-list locs t
))
1970 (move-lvar-result node block locs lvar
)))))
1972 (let ((block-loc (standard-arg-location 0)))
1973 (vop uwp-entry node block target block-loc start-loc count-loc
)
1976 (list block-loc start-loc count-loc
)
1980 (when *collect-dynamic-statistics
*
1981 (vop count-me node block
*dynamic-counts-tn
*
1982 (block-number (ir2-block-block block
))))
1984 (vop* restore-dynamic-state node block
1985 ((reference-tn-list (cdr (ir2-nlx-info-dynamic-state 2info
)) nil
))
1987 (vop unbind-to-here node block
1988 (car (ir2-nlx-info-dynamic-state 2info
)))))
1990 ;;;; n-argument functions
1992 (macrolet ((def (name)
1993 `(defoptimizer (,name ir2-convert
) ((&rest args
) node block
)
1996 (/ sb
!vm
:large-object-size
1997 (* sb
!vm
:n-word-bytes
2)))
1998 ;; The VOPs will try to allocate all space at once
1999 ;; And it'll end up in large objects, and no conses
2000 ;; are welcome there.
2001 (ir2-convert-full-call node block
))
2003 (let* ((refs (reference-tn-list
2004 (loop for arg in args
2005 for tn
= (make-normal-tn *backend-t-primitive-type
*)
2007 (emit-move node block
(lvar-tn node block arg
) tn
)
2010 (lvar (node-lvar node
))
2011 (res (lvar-result-tns
2013 (list (primitive-type (specifier-type 'list
))))))
2014 (when (and lvar
(lvar-dynamic-extent lvar
))
2015 (vop current-stack-pointer node block
2016 (ir2-lvar-stack-pointer (lvar-info lvar
))))
2017 (vop* ,name node block
(refs) ((first res
) nil
)
2019 (move-lvar-result node block res lvar
)))))))
2024 (defoptimizer (mask-signed-field ir2-convert
) ((width x
) node block
)
2026 (when (constant-lvar-p width
)
2027 (case (lvar-value width
)
2028 (#.
(- sb
!vm
:n-word-bits sb
!vm
:n-fixnum-tag-bits
)
2029 (when (or (csubtypep (lvar-type x
)
2030 (specifier-type 'word
))
2031 (csubtypep (lvar-type x
)
2032 (specifier-type 'sb
!vm
:signed-word
)))
2033 (let* ((lvar (node-lvar node
))
2034 (temp (make-normal-tn
2035 (if (csubtypep (lvar-type x
)
2036 (specifier-type 'word
))
2037 (primitive-type-of most-positive-word
)
2039 (- (ash most-positive-word -
1))))))
2040 (results (lvar-result-tns
2042 (list (primitive-type-or-lose 'fixnum
)))))
2043 (emit-move node block
(lvar-tn node block x
) temp
)
2044 (vop sb
!vm
::move-from-word
/fixnum node block
2045 temp
(first results
))
2046 (move-lvar-result node block results lvar
)
2048 (#.sb
!vm
:n-word-bits
2049 (when (csubtypep (lvar-type x
) (specifier-type 'word
))
2050 (let* ((lvar (node-lvar node
))
2051 (temp (make-normal-tn
2052 (primitive-type-of most-positive-word
)))
2053 (results (lvar-result-tns
2055 (list (primitive-type
2056 (specifier-type 'sb
!vm
:signed-word
))))))
2057 (emit-move node block
(lvar-tn node block x
) temp
)
2058 (vop sb
!vm
::word-move node block
2059 temp
(first results
))
2060 (move-lvar-result node block results lvar
)
2062 (if (template-p (basic-combination-info node
))
2063 (ir2-convert-template node block
)
2064 (ir2-convert-full-call node block
))))
2066 ;; just a fancy identity
2067 (defoptimizer (%typep-wrapper ir2-convert
) ((value variable type
) node block
)
2068 (declare (ignore variable type
))
2069 (let* ((lvar (node-lvar node
))
2070 (results (lvar-result-tns lvar
(list (primitive-type-or-lose t
)))))
2071 (emit-move node block
(lvar-tn node block value
) (first results
))
2072 (move-lvar-result node block results lvar
)))
2074 ;;; An identity to avoid complaints about constant modification
2075 (defoptimizer (ltv-wrapper ir2-convert
) ((x) node block
)
2076 (let* ((lvar (node-lvar node
))
2077 (results (lvar-result-tns lvar
(list (primitive-type-or-lose t
)))))
2078 (emit-move node block
(lvar-tn node block x
) (first results
))
2079 (move-lvar-result node block results lvar
)))
2081 #-sb-xc-host
;; package-lock-violation-p is not present yet
2082 (defoptimizer (set ir2-hook
) ((symbol value
) node block
)
2083 (declare (ignore value block
))
2084 (when (constant-lvar-p symbol
)
2085 (let* ((symbol (lvar-value symbol
))
2086 (kind (info :variable
:kind symbol
)))
2087 (when (and (eq kind
:unknown
)
2088 (sb!impl
::package-lock-violation-p
(symbol-package symbol
) symbol
))
2089 (let ((*compiler-error-context
* node
))
2090 (compiler-warn "violating package lock on ~/sb-ext:print-symbol-with-prefix/"
2093 (defoptimizer (restart-point ir2-convert
) ((location) node block
)
2094 (setf (restart-location-label (lvar-value location
))
2095 (block-label (ir2-block-block block
))))
2097 ;;; Convert the code in a component into VOPs.
2098 (defun ir2-convert (component)
2099 (declare (type component component
))
2100 (let (#!+sb-dyncount
2101 (*dynamic-counts-tn
*
2102 (when *collect-dynamic-statistics
*
2104 (block-number (block-next (component-head component
))))
2105 (counts (make-array blocks
2106 :element-type
'(unsigned-byte 32)
2107 :initial-element
0))
2108 (info (make-dyncount-info
2109 :for
(component-name component
)
2110 :costs
(make-array blocks
2111 :element-type
'(unsigned-byte 32)
2114 (setf (ir2-component-dyncount-info (component-info component
))
2116 (emit-constant info
)
2117 (emit-constant counts
)))))
2119 (declare (type index num
))
2120 (do-ir2-blocks (2block component
)
2121 (let ((block (ir2-block-block 2block
)))
2122 (when (block-start block
)
2123 (setf (block-number block
) num
)
2125 (when *collect-dynamic-statistics
*
2126 (let ((first-node (block-start-node block
)))
2127 (unless (or (and (bind-p first-node
)
2128 (xep-p (bind-lambda first-node
)))
2130 (node-lvar first-node
))
2135 #!+sb-dyncount
*dynamic-counts-tn
* #!-sb-dyncount nil
2138 (let ((first-node (block-start-node block
)))
2139 (unless (or (and (bind-p first-node
)
2140 ;; Bind-nodes already have safepoints
2141 (eq (bind-lambda first-node
)
2142 (lambda-home (bind-lambda first-node
))))
2143 (and (valued-node-p first-node
)
2144 (node-lvar first-node
)
2146 (node-lvar first-node
))
2148 (when (and (rest (block-pred block
))
2150 (member (loop-kind (block-loop block
))
2151 '(:natural
:strange
))
2152 (eq block
(loop-head (block-loop block
)))
2153 (policy first-node
(< inhibit-safepoints
2)))
2154 (vop sb
!vm
::insert-safepoint first-node
2block
))))
2155 (ir2-convert-block block
)
2159 ;;; If necessary, emit a terminal unconditional branch to go to the
2160 ;;; successor block. If the successor is the component tail, then
2161 ;;; there isn't really any successor, but if the end is a non-tail
2162 ;;; call to a function that's not *known* to never return, then we
2163 ;;; emit an error trap just in case the function really does return.
2165 ;;; Trapping after known calls makes it easier to understand type
2166 ;;; derivation bugs at runtime: they show up as nil-fun-returned-error,
2167 ;;; rather than the execution of arbitrary code or error traps.
2168 (defun finish-ir2-block (block)
2169 (declare (type cblock block
))
2170 (let* ((2block (block-info block
))
2171 (last (block-last block
))
2172 (succ (block-succ block
)))
2174 (aver (singleton-p succ
))
2175 (let ((target (first succ
)))
2176 (cond ((eq target
(component-tail (block-component block
)))
2177 (when (and (basic-combination-p last
)
2178 (or (eq (basic-combination-kind last
) :full
)
2179 (and (eq (basic-combination-kind last
) :known
)
2180 (eq (basic-combination-info last
) :full
))))
2181 (let* ((fun (basic-combination-fun last
))
2182 (use (lvar-uses fun
))
2183 (name (and (ref-p use
)
2184 (leaf-has-source-name-p (ref-leaf use
))
2185 (leaf-source-name (ref-leaf use
))))
2186 (ftype (and (info :function
:info name
) ; only use the FTYPE if
2187 (proclaimed-ftype name
)))) ; NAME was DEFKNOWN
2188 (unless (or (node-tail-p last
)
2189 (policy last
(zerop safety
))
2190 (and (fun-type-p ftype
)
2191 (eq *empty-type
* (fun-type-returns ftype
))))
2192 (vop nil-fun-returned-error last
2block
2194 (emit-constant name
)
2195 (multiple-value-bind (tn named
)
2196 (fun-lvar-tn last
2block fun
)
2199 ((not (eq (ir2-block-next 2block
) (block-info target
)))
2200 (vop branch last
2block
(block-label target
)))
2202 (register-drop-thru target
))))))
2206 ;;; Convert the code in a block into VOPs.
2207 (defun ir2-convert-block (block)
2208 (declare (type cblock block
))
2209 (let ((2block (block-info block
)))
2210 (do-nodes (node lvar block
)
2214 (let ((2lvar (lvar-info lvar
)))
2215 ;; function REF in a local call is not annotated
2216 (when (and 2lvar
(not (eq (ir2-lvar-kind 2lvar
) :delayed
)))
2217 (ir2-convert-ref node
2block
)))))
2219 (let ((kind (basic-combination-kind node
)))
2222 (ir2-convert-local-call node
2block
))
2224 (ir2-convert-full-call node
2block
))
2226 (let* ((info (basic-combination-fun-info node
))
2227 (fun (fun-info-ir2-convert info
))
2228 (hook (fun-info-ir2-hook info
)))
2230 (funcall hook node
2block
))
2232 (funcall fun node
2block
))
2233 ((eq (basic-combination-info node
) :full
)
2234 (ir2-convert-full-call node
2block
))
2236 (ir2-convert-template node
2block
))))))))
2238 (when (lvar-info (if-test node
))
2239 (ir2-convert-if node
2block
)))
2241 (let ((fun (bind-lambda node
)))
2242 (when (eq (lambda-home fun
) fun
)
2243 (ir2-convert-bind node
2block
))))
2245 (ir2-convert-return node
2block
))
2247 (ir2-convert-set node
2block
))
2249 (ir2-convert-cast node
2block
))
2252 ((eq (basic-combination-kind node
) :local
)
2253 (ir2-convert-mv-bind node
2block
))
2254 ((eq (lvar-fun-name (basic-combination-fun node
))
2256 (ir2-convert-throw node
2block
))
2258 (ir2-convert-mv-call node
2block
))))
2260 (when (exit-entry node
)
2261 (ir2-convert-exit node
2block
)))
2263 (ir2-convert-entry node
2block
)))))
2265 (finish-ir2-block block
)