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 %return-is-boxed
(node)
100 (declare (type creturn node
))
101 (let* ((fun (return-lambda node
))
102 (returns (tail-set-info (lambda-tail-set fun
))))
104 (eq (return-info-kind returns
) :unknown
))))
106 (defun boxed-ref-p (ref)
107 (let ((dest (lvar-dest (ref-lvar ref
))))
108 (cond ((and (basic-combination-p dest
)
109 (call-full-like-p dest
))
111 ((and (return-p dest
) (%return-is-boxed dest
)))
115 ;;; Convert a REF node. The reference must not be delayed.
116 (defun ir2-convert-ref (node block
)
117 (declare (type ref node
) (type ir2-block block
))
118 (let* ((lvar (node-lvar node
))
119 (leaf (ref-leaf node
))
120 (locs (lvar-result-tns
121 lvar
(list (primitive-type (leaf-type leaf
)))))
125 (let ((tn (find-in-physenv leaf
(node-physenv node
)))
126 (indirect (lambda-var-indirect leaf
))
127 (explicit (lambda-var-explicit-value-cell leaf
)))
129 ((and indirect explicit
)
130 (vop value-cell-ref node block tn res
))
132 (not (eq (node-physenv node
)
133 (lambda-physenv (lambda-var-home leaf
)))))
134 (let ((reffer (third (primitive-type-indirect-cell-type
135 (primitive-type (leaf-type leaf
))))))
137 (funcall reffer node block tn
(leaf-info leaf
) res
)
138 (vop ancestor-frame-ref node block tn
(leaf-info leaf
) res
))))
139 (t (emit-move node block tn res
)))))
141 (emit-move node block
(constant-tn leaf
(boxed-ref-p node
)) res
))
143 (ir2-convert-closure node block leaf res
))
145 (ir2-convert-global-var node block leaf res
)))
146 (move-lvar-result node block locs lvar
))
149 (defun ir2-convert-global-var (node block leaf res
)
150 (let ((unsafe (policy node
(zerop safety
)))
151 (name (leaf-source-name leaf
)))
152 (ecase (global-var-kind leaf
)
154 (aver (symbolp name
))
155 (let ((name-tn (emit-constant name
)))
156 (if (or unsafe
(always-boundp name
))
157 (vop fast-symbol-value node block name-tn res
)
158 (vop symbol-value node block name-tn res
))))
160 (aver (symbolp name
))
161 (let ((name-tn (emit-constant name
)))
162 (if (or unsafe
(always-boundp name
))
163 (vop fast-symbol-global-value node block name-tn res
)
164 (vop symbol-global-value node block name-tn res
))))
166 ;; In cross-compilation, testing (INFO :function :definition) is not
167 ;; sensible (or possible) but we can assume that things with fun-info
168 ;; will eventually be defined. If that's untrue, e.g. if we referred
169 ;; to #'DESCRIBE during cold-load, we'd just fix it locally by declaring
170 ;; DESCRIBE notinline.
171 ;; But in the target, more caution is warranted because users might
172 ;; DEFKNOWN a function but fail to define it. And they shouldn't be
173 ;; expected to understand the failure mode and the remedy.
174 (cond ((and #-sb-xc-host
(info :function
:definition name
)
175 (info :function
:info name
)
176 (let ((*lexenv
* (node-lexenv node
)))
177 (not (fun-lexically-notinline-p name
))))
178 ;; Known functions can be dumped without going through fdefns.
179 ;; But if NOTINLINEd, don't early-bind to the functional value
180 ;; because that disallows redefinition, including but not limited
181 ;; to encapsulations, which in turn makes TRACE not work, which
182 ;; leads to extreme frustration when debugging.
183 (emit-move node block
(make-load-time-constant-tn :known-fun name
)
186 (let ((fdefn-tn (make-load-time-constant-tn :fdefinition name
)))
188 (vop fdefn-fun node block fdefn-tn res
)
189 (vop safe-fdefn-fun node block fdefn-tn res
)))))))))
191 ;;; some sanity checks for a CLAMBDA passed to IR2-CONVERT-CLOSURE
192 (defun assertions-on-ir2-converted-clambda (clambda)
193 ;; This assertion was sort of an experiment. It would be nice and
194 ;; sane and easier to understand things if it were *always* true,
195 ;; but experimentally I observe that it's only *almost* always
196 ;; true. -- WHN 2001-01-02
198 (aver (eql (lambda-component clambda
)
199 (block-component (ir2-block-block ir2-block
))))
200 ;; Check for some weirdness which came up in bug
203 ;; The MAKE-LOAD-TIME-CONSTANT-TN call above puts an :ENTRY record
204 ;; into the IR2-COMPONENT-CONSTANTS table. The dump-a-COMPONENT
206 ;; * treats every HANDLEless :ENTRY record into a
208 ;; * expects every patch to correspond to an
209 ;; IR2-COMPONENT-ENTRIES record.
210 ;; The IR2-COMPONENT-ENTRIES records are set by ENTRY-ANALYZE
211 ;; walking over COMPONENT-LAMBDAS. Bug 138b arose because there
212 ;; was a HANDLEless :ENTRY record which didn't correspond to an
213 ;; IR2-COMPONENT-ENTRIES record. That problem is hard to debug
214 ;; when it's caught at dump time, so this assertion tries to catch
216 (aver (member clambda
217 (component-lambdas (lambda-component clambda
))))
218 ;; another bug-138-related issue: COMPONENT-NEW-FUNCTIONALS is
219 ;; used as a queue for stuff pending to do in IR1, and now that
220 ;; we're doing IR2 it should've been completely flushed (but
222 (aver (null (component-new-functionals (lambda-component clambda
))))
225 ;;; Emit code to load a function object implementing FUNCTIONAL into
226 ;;; RES. This gets interesting when the referenced function is a
227 ;;; closure: we must make the closure and move the closed-over values
230 ;;; FUNCTIONAL is either a :TOPLEVEL-XEP functional or the XEP lambda
231 ;;; for the called function, since local call analysis converts all
232 ;;; closure references. If a :TOPLEVEL-XEP, we know it is not a
235 ;;; If a closed-over LAMBDA-VAR has no refs (is deleted), then we
236 ;;; don't initialize that slot. This can happen with closures over
237 ;;; top level variables, where optimization of the closure deleted the
238 ;;; variable. Since we committed to the closure format when we
239 ;;; pre-analyzed the top level code, we just leave an empty slot.
240 (defun ir2-convert-closure (ref ir2-block functional res
)
241 (declare (type ref ref
)
242 (type ir2-block ir2-block
)
243 (type functional functional
)
246 (aver (not (eql (functional-kind functional
) :deleted
)))
247 (unless (leaf-info functional
)
248 (setf (leaf-info functional
)
249 (make-entry-info :name
250 (functional-debug-name functional
))))))
251 (let ((closure (etypecase functional
253 (assertions-on-ir2-converted-clambda functional
)
254 (physenv-closure (get-lambda-physenv functional
)))
256 (aver (eq (functional-kind functional
) :toplevel-xep
))
261 (let* ((physenv (node-physenv ref
))
262 (tn (find-in-physenv functional physenv
)))
263 (emit-move ref ir2-block tn res
)))
264 ;; we're about to emit a reference to a "closure" that's actually
265 ;; an inlinable global function.
266 ((and (global-var-p (setf global-var
267 (functional-inline-expanded functional
)))
268 (eq :global-function
(global-var-kind global-var
)))
269 (ir2-convert-global-var ref ir2-block global-var res
))
271 ;; if we're here, we should have either a toplevel-xep (some
272 ;; global scope function in a different component) or an external
273 ;; reference to the "closure"'s body.
275 (aver (memq (functional-kind functional
) '(:external
:toplevel-xep
)))
276 (let ((entry (make-load-time-constant-tn :entry functional
)))
277 (emit-move ref ir2-block entry res
))))))
280 (defun closure-initial-value (what this-env current-fp
)
281 (declare (type (or nlx-info lambda-var clambda
) what
)
282 (type physenv this-env
)
283 (type (or tn null
) current-fp
))
284 ;; If we have an indirect LAMBDA-VAR that does not require an
285 ;; EXPLICIT-VALUE-CELL, and is from this environment (not from being
286 ;; closed over), we need to store the current frame pointer.
287 (if (and (lambda-var-p what
)
288 (lambda-var-indirect what
)
289 (not (lambda-var-explicit-value-cell what
))
290 (eq (lambda-physenv (lambda-var-home what
))
293 (find-in-physenv what this-env
)))
295 (defoptimizer (%allocate-closures ltn-annotate
) ((leaves) node ltn-policy
)
296 (declare (ignore ltn-policy
))
297 (when (lvar-dynamic-extent leaves
)
298 (let ((info (make-ir2-lvar *backend-t-primitive-type
*)))
299 (setf (ir2-lvar-kind info
) :delayed
)
300 (setf (lvar-info leaves
) info
)
301 (setf (ir2-lvar-stack-pointer info
)
302 (make-stack-pointer-tn)))))
304 (defoptimizer (%allocate-closures ir2-convert
) ((leaves) call
2block
)
305 (let ((dx-p (lvar-dynamic-extent leaves
)))
308 (vop current-stack-pointer call
2block
309 (ir2-lvar-stack-pointer (lvar-info leaves
))))
310 (dolist (leaf (lvar-value leaves
))
311 (binding* ((xep (awhen (functional-entry-fun leaf
)
312 ;; if the xep's been deleted then we can skip it
313 (if (eq (functional-kind it
) :deleted
)
316 (nil (aver (xep-p xep
)))
317 (entry-info (lambda-info xep
) :exit-if-null
)
318 (tn (entry-info-closure-tn entry-info
) :exit-if-null
)
319 (closure (physenv-closure (get-lambda-physenv xep
)))
320 (entry (make-load-time-constant-tn :entry xep
)))
321 (let ((this-env (node-physenv call
))
322 (leaf-dx-p (and dx-p
(leaf-dynamic-extent leaf
))))
323 (vop make-closure call
2block entry
(length closure
)
325 (loop for what in closure and n from
0 do
326 (unless (and (lambda-var-p what
)
327 (null (leaf-refs what
)))
328 ;; In LABELS a closure may refer to another closure
329 ;; in the same group, so we must be sure that we
330 ;; store a closure only after its creation.
332 ;; TODO: Here is a simple solution: we postpone
333 ;; putting of all closures after all creations
334 ;; (though it may require more registers).
336 (delayed (list tn
(find-in-physenv what this-env
) n
))
337 (let ((initial-value (closure-initial-value
340 (vop closure-init call
2block
342 ;; An initial-value of NIL means to stash
343 ;; the frame pointer... which requires a
345 (vop closure-init-from-fp call
2block tn n
)))))))))
346 (loop for
(tn what n
) in
(delayed)
347 do
(vop closure-init call
2block
351 ;;; Convert a SET node. If the NODE's LVAR is annotated, then we also
352 ;;; deliver the value to that lvar. If the var is a lexical variable
353 ;;; with no refs, then we don't actually set anything, since the
354 ;;; variable has been deleted.
355 (defun ir2-convert-set (node block
)
356 (declare (type cset node
) (type ir2-block block
))
357 (let* ((lvar (node-lvar node
))
358 (leaf (set-var node
))
359 (val (lvar-tn node block
(set-value node
)))
362 lvar
(list (primitive-type (leaf-type leaf
))))
366 (when (leaf-refs leaf
)
367 (let ((tn (find-in-physenv leaf
(node-physenv node
)))
368 (indirect (lambda-var-indirect leaf
))
369 (explicit (lambda-var-explicit-value-cell leaf
)))
371 ((and indirect explicit
)
372 (vop value-cell-set node block tn val
))
374 (not (eq (node-physenv node
)
375 (lambda-physenv (lambda-var-home leaf
)))))
376 (let ((setter (fourth (primitive-type-indirect-cell-type
377 (primitive-type (leaf-type leaf
))))))
379 (funcall setter node block tn val
(leaf-info leaf
))
380 (vop ancestor-frame-set node block tn val
(leaf-info leaf
)))))
381 (t (emit-move node block val tn
))))))
383 (aver (symbolp (leaf-source-name leaf
)))
384 (ecase (global-var-kind leaf
)
386 (vop set node block
(emit-constant (leaf-source-name leaf
)) val
))
388 (vop %set-symbol-global-value node
389 block
(emit-constant (leaf-source-name leaf
)) val
)))))
391 (emit-move node block val
(first locs
))
392 (move-lvar-result node block locs lvar
)))
395 ;;;; utilities for receiving fixed values
397 ;;; Return a TN that can be referenced to get the value of LVAR. LVAR
398 ;;; must be LTN-ANNOTATED either as a delayed leaf ref or as a fixed,
399 ;;; single-value lvar.
401 ;;; The primitive-type of the result will always be the same as the
402 ;;; IR2-LVAR-PRIMITIVE-TYPE, ensuring that VOPs are always called with
403 ;;; TNs that satisfy the operand primitive-type restriction. We may
404 ;;; have to make a temporary of the desired type and move the actual
405 ;;; lvar TN into it. This happens when we delete a type check in
406 ;;; unsafe code or when we locally know something about the type of an
407 ;;; argument variable.
408 (defun lvar-tn (node block lvar
)
409 (declare (type node node
) (type ir2-block block
) (type lvar lvar
))
410 (let* ((2lvar (lvar-info lvar
))
412 (ecase (ir2-lvar-kind 2lvar
)
414 (let ((ref (lvar-uses lvar
)))
415 (leaf-tn (ref-leaf ref
) (node-physenv ref
) (boxed-ref-p ref
))))
417 (aver (= (length (ir2-lvar-locs 2lvar
)) 1))
418 (first (ir2-lvar-locs 2lvar
)))))
419 (ptype (ir2-lvar-primitive-type 2lvar
)))
421 (cond ((eq (tn-primitive-type lvar-tn
) ptype
) lvar-tn
)
423 (let ((temp (make-normal-tn ptype
)))
424 (emit-move node block lvar-tn temp
)
427 ;;; This is similar to LVAR-TN, but hacks multiple values. We return
428 ;;; TNs holding the values of LVAR with PTYPES as their primitive
429 ;;; types. LVAR must be annotated for the same number of fixed values
430 ;;; are there are PTYPES.
432 ;;; If the lvar has a type check, check the values into temps and
433 ;;; return the temps. When we have more values than assertions, we
434 ;;; move the extra values with no check.
435 (defun lvar-tns (node block lvar ptypes
)
436 (declare (type node node
) (type ir2-block block
)
437 (type lvar lvar
) (list ptypes
))
438 (let* ((locs (ir2-lvar-locs (lvar-info lvar
)))
439 (nlocs (length locs
)))
440 (aver (= nlocs
(length ptypes
)))
442 (mapcar (lambda (from to-type
)
443 (if (eq (tn-primitive-type from
) to-type
)
445 (let ((temp (make-normal-tn to-type
)))
446 (emit-move node block from temp
)
451 ;;;; utilities for delivering values to lvars
453 ;;; Return a list of TNs with the specifier TYPES that can be used as
454 ;;; result TNs to evaluate an expression into LVAR. This is used
455 ;;; together with MOVE-LVAR-RESULT to deliver fixed values to
458 ;;; If the lvar isn't annotated (meaning the values are discarded) or
459 ;;; is unknown-values, then we make temporaries for each supplied
460 ;;; value, providing a place to compute the result in until we decide
461 ;;; what to do with it (if anything.)
463 ;;; If the lvar is fixed-values, and wants the same number of values
464 ;;; as the user wants to deliver, then we just return the
465 ;;; IR2-LVAR-LOCS. Otherwise we make a new list padded as necessary by
466 ;;; discarded TNs. We always return a TN of the specified type, using
467 ;;; the lvar locs only when they are of the correct type.
468 (defun lvar-result-tns (lvar types
)
469 (declare (type (or lvar null
) lvar
) (type list types
))
471 (mapcar #'make-normal-tn types
)
472 (let ((2lvar (lvar-info lvar
)))
473 (ecase (ir2-lvar-kind 2lvar
)
475 (let* ((locs (ir2-lvar-locs 2lvar
))
476 (nlocs (length locs
))
477 (ntypes (length types
)))
478 (if (and (= nlocs ntypes
)
479 (do ((loc locs
(cdr loc
))
480 (type types
(cdr type
)))
482 (unless (eq (tn-primitive-type (car loc
)) (car type
))
485 (mapcar (lambda (loc type
)
486 (if (eq (tn-primitive-type loc
) type
)
488 (make-normal-tn type
)))
491 (mapcar #'make-normal-tn
492 (subseq types nlocs
)))
496 (mapcar #'make-normal-tn types
))))))
498 ;;; Make the first N standard value TNs, returning them in a list.
499 (defun make-standard-value-tns (n)
500 (declare (type unsigned-byte n
))
503 (res (standard-arg-location i
)))
506 ;;; Return a list of TNs wired to the standard value passing
507 ;;; conventions that can be used to receive values according to the
508 ;;; unknown-values convention. This is used together with
509 ;;; MOVE-LVAR-RESULT for delivering unknown values to a fixed values
512 ;;; If the lvar isn't annotated, then we treat as 0-values, returning
513 ;;; an empty list of temporaries.
515 ;;; If the lvar is annotated, then it must be :FIXED.
516 (defun standard-result-tns (lvar)
517 (declare (type (or lvar null
) lvar
))
519 (let ((2lvar (lvar-info lvar
)))
520 (ecase (ir2-lvar-kind 2lvar
)
522 (make-standard-value-tns (length (ir2-lvar-locs 2lvar
))))))
525 ;;; Just move each SRC TN into the corresponding DEST TN, defaulting
526 ;;; any unsupplied source values to NIL. We let EMIT-MOVE worry about
527 ;;; doing the appropriate coercions.
528 (defun move-results-coerced (node block src dest
)
529 (declare (type node node
) (type ir2-block block
) (list src dest
))
530 (let ((nsrc (length src
))
531 (ndest (length dest
)))
532 (mapc (lambda (from to
)
534 (emit-move node block from to
)))
536 (append src
(make-list (- ndest nsrc
)
537 :initial-element
(emit-constant nil
)))
542 ;;; If necessary, emit coercion code needed to deliver the RESULTS to
543 ;;; the specified lvar. NODE and BLOCK provide context for emitting
544 ;;; code. Although usually obtained from STANDARD-RESULT-TNs or
545 ;;; LVAR-RESULT-TNs, RESULTS may be a list of any type or
548 ;;; If the lvar is fixed values, then move the results into the lvar
549 ;;; locations. If the lvar is unknown values, then do the moves into
550 ;;; the standard value locations, and use PUSH-VALUES to put the
551 ;;; values on the stack.
552 (defun move-lvar-result (node block results lvar
)
553 (declare (type node node
) (type ir2-block block
)
554 (list results
) (type (or lvar null
) lvar
))
556 (let ((2lvar (lvar-info lvar
)))
557 (ecase (ir2-lvar-kind 2lvar
)
559 (let ((locs (ir2-lvar-locs 2lvar
)))
560 (unless (eq locs results
)
561 (move-results-coerced node block results locs
))))
563 (let* ((nvals (length results
))
564 (locs (make-standard-value-tns nvals
)))
565 (move-results-coerced node block results locs
)
566 (vop* push-values node block
567 ((reference-tn-list locs nil
))
568 ((reference-tn-list (ir2-lvar-locs 2lvar
) t
))
573 (defun ir2-convert-cast (node block
)
574 (declare (type cast node
)
575 (type ir2-block block
))
576 (binding* ((lvar (node-lvar node
) :exit-if-null
)
577 (2lvar (lvar-info lvar
))
578 (value (cast-value node
))
579 (2value (lvar-info value
)))
580 (ecase (ir2-lvar-kind 2lvar
)
583 (aver (not (cast-type-check node
)))
584 (move-results-coerced node block
585 (ir2-lvar-locs 2value
)
586 (ir2-lvar-locs 2lvar
))))))
588 (defoptimizer (%check-bound ir2-hook
) ((array bound index
) node block
)
589 (declare (ignore block
))
590 (when (constant-lvar-p bound
)
591 (let* ((bound-type (specifier-type `(integer 0 (,(lvar-value bound
)))))
592 (index-type (lvar-type index
)))
593 (when (eq (type-intersection bound-type index-type
)
595 (let ((*compiler-error-context
* node
))
596 (compiler-warn "Derived type ~s is not a suitable index for ~s."
597 (type-specifier index-type
)
598 (type-specifier (lvar-type array
))))))))
600 ;;;; template conversion
602 ;;; Build a TN-REFS list that represents access to the values of the
603 ;;; specified list of lvars ARGS for TEMPLATE. Any :CONSTANT arguments
604 ;;; are returned in the second value as a list rather than being
605 ;;; accessed as a normal argument. NODE and BLOCK provide the context
606 ;;; for emitting any necessary type-checking code.
607 (defun reference-args (node block args template
)
608 (declare (type node node
) (type ir2-block block
) (list args
)
609 (type template template
))
610 (collect ((info-args))
613 (do ((args args
(cdr args
))
614 (types (template-arg-types template
) (cdr types
)))
616 (let ((type (first types
))
618 (if (and (consp type
) (eq (car type
) ':constant
))
619 (info-args (lvar-value arg
))
620 (let ((ref (reference-tn (lvar-tn node block arg
) nil
)))
622 (setf (tn-ref-across last
) ref
)
626 (values (the (or tn-ref null
) first
) (info-args)))))
628 ;;; Convert a conditional template. We try to exploit any
629 ;;; drop-through, but emit an unconditional branch afterward if we
630 ;;; fail. NOT-P is true if the sense of the TEMPLATE's test should be
632 (defun ir2-convert-conditional (node block template args info-args if not-p
)
633 (declare (type node node
) (type ir2-block block
)
634 (type template template
) (type (or tn-ref null
) args
)
635 (list info-args
) (type cif if
) (type boolean not-p
))
636 (let ((consequent (if-consequent if
))
637 (alternative (if-alternative if
))
638 (flags (and (consp (template-result-types template
))
639 (rest (template-result-types template
)))))
640 (aver (= (template-info-arg-count template
)
641 (+ (length info-args
)
644 (rotatef consequent alternative
)
646 (when (drop-thru-p if consequent
)
647 (rotatef consequent alternative
)
650 (emit-template node block template args nil
651 (list* (block-label consequent
) not-p
653 (if (drop-thru-p if alternative
)
654 (register-drop-thru alternative
)
655 (vop branch node block
(block-label alternative
))))
657 (emit-template node block template args nil info-args
)
658 (vop branch-if node block
(block-label consequent
) flags not-p
)
659 (if (drop-thru-p if alternative
)
660 (register-drop-thru alternative
)
661 (vop branch node block
(block-label alternative
)))))))
663 ;;; Convert an IF that isn't the DEST of a conditional template.
664 (defun ir2-convert-if (node block
)
665 (declare (type ir2-block block
) (type cif node
))
666 (let* ((test (if-test node
))
667 (test-ref (reference-tn (lvar-tn node block test
) nil
))
668 (nil-ref (reference-tn (emit-constant nil
) nil
)))
669 (setf (tn-ref-across test-ref
) nil-ref
)
670 (ir2-convert-conditional node block
(template-or-lose 'if-eq
)
671 test-ref
() node t
)))
673 ;;; Return a list of primitive-types that we can pass to LVAR-RESULT-TNS
674 ;;; describing the result types we want for a template call. We are really
675 ;;; only interested in the number of results required: in normal case
676 ;;; TEMPLATE-RESULTS-OK has already checked them.
677 (defun find-template-result-types (call rtypes
)
678 (let* ((type (node-derived-type call
))
680 (mapcar #'primitive-type
681 (if (args-type-p type
)
682 (append (args-type-required type
)
683 (args-type-optional type
))
685 (primitive-t *backend-t-primitive-type
*))
686 (mapcar (lambda (rtype)
687 (declare (ignore rtype
))
688 (or (pop types
) primitive-t
)) rtypes
)))
690 ;;; Return a list of TNs usable in a CALL to TEMPLATE delivering values to
691 ;;; LVAR. As an efficiency hack, we pick off the common case where the LVAR is
692 ;;; fixed values and has locations that satisfy the result restrictions. This
693 ;;; can fail when there is a type check or a values count mismatch.
694 (defun make-template-result-tns (call lvar rtypes
)
695 (declare (type combination call
) (type (or lvar null
) lvar
)
697 (let ((2lvar (when lvar
(lvar-info lvar
))))
698 (if (and 2lvar
(eq (ir2-lvar-kind 2lvar
) :fixed
))
699 (let ((locs (ir2-lvar-locs 2lvar
)))
700 (if (and (= (length rtypes
) (length locs
))
701 (do ((loc locs
(cdr loc
))
702 (rtypes rtypes
(cdr rtypes
)))
704 (unless (operand-restriction-ok
706 (tn-primitive-type (car loc
))
712 (find-template-result-types call rtypes
))))
715 (find-template-result-types call rtypes
)))))
717 ;;; Get the operands into TNs, make TN-REFs for them, and then call
718 ;;; the template emit function.
719 (defun ir2-convert-template (call block
)
720 (declare (type combination call
) (type ir2-block block
))
721 (let* ((template (combination-info call
))
722 (lvar (node-lvar call
))
723 (rtypes (template-result-types template
)))
724 (multiple-value-bind (args info-args
)
725 (reference-args call block
(combination-args call
) template
)
726 (aver (not (template-more-results-type template
)))
727 (if (template-conditional-p template
)
728 (ir2-convert-conditional call block template args info-args
729 (lvar-dest lvar
) nil
)
730 (let* ((results (make-template-result-tns call lvar rtypes
))
731 (r-refs (reference-tn-list results t
)))
732 (aver (= (length info-args
)
733 (template-info-arg-count template
)))
734 (when (and lvar
(lvar-dynamic-extent lvar
))
735 (vop current-stack-pointer call block
736 (ir2-lvar-stack-pointer (lvar-info lvar
))))
737 (when (emit-step-p call
)
738 (vop sb
!vm
::step-instrument-before-vop call block
))
740 (emit-template call block template args r-refs info-args
)
741 (emit-template call block template args r-refs
))
742 (move-lvar-result call block results lvar
)))))
745 ;;; We don't have to do much because operand count checking is done by
746 ;;; IR1 conversion. The only difference between this and the function
747 ;;; case of IR2-CONVERT-TEMPLATE is that there can be codegen-info
749 (defoptimizer (%%primitive ir2-convert
) ((template info
&rest args
) call block
)
750 (declare (ignore args
))
751 (let* ((template (lvar-value template
))
752 (info (lvar-value info
))
753 (lvar (node-lvar call
))
754 (rtypes (template-result-types template
))
755 (results (make-template-result-tns call lvar rtypes
))
756 (r-refs (reference-tn-list results t
)))
757 (multiple-value-bind (args info-args
)
758 (reference-args call block
(cddr (combination-args call
)) template
)
759 (aver (not (template-more-results-type template
)))
760 (aver (not (template-conditional-p template
)))
761 (aver (null info-args
))
764 (emit-template call block template args r-refs info
)
765 (emit-template call block template args r-refs
))
767 (move-lvar-result call block results lvar
)))
770 (defoptimizer (%%primitive derive-type
) ((template info
&rest args
))
771 (declare (ignore info args
))
772 (let ((type (template-type (lvar-value template
))))
773 (if (fun-type-p type
)
774 (fun-type-returns type
)
779 ;;; Convert a LET by moving the argument values into the variables.
780 ;;; Since a LET doesn't have any passing locations, we move the
781 ;;; arguments directly into the variables. We must also allocate any
782 ;;; indirect value cells, since there is no function prologue to do
784 (defun ir2-convert-let (node block fun
)
785 (declare (type combination node
) (type ir2-block block
) (type clambda fun
))
786 (mapc (lambda (var arg
)
788 (let ((src (lvar-tn node block arg
))
789 (dest (leaf-info var
)))
790 (if (and (lambda-var-indirect var
)
791 (lambda-var-explicit-value-cell var
))
792 (emit-make-value-cell node block src dest
)
793 (emit-move node block src dest
)))))
794 (lambda-vars fun
) (basic-combination-args node
))
797 ;;; Emit any necessary moves into assignment temps for a local call to
798 ;;; FUN. We return two lists of TNs: TNs holding the actual argument
799 ;;; values, and (possibly EQ) TNs that are the actual destination of
800 ;;; the arguments. When necessary, we allocate temporaries for
801 ;;; arguments to preserve parallel assignment semantics. These lists
802 ;;; exclude unused arguments and include implicit environment
803 ;;; arguments, i.e. they exactly correspond to the arguments passed.
805 ;;; OLD-FP is the TN currently holding the value we want to pass as
806 ;;; OLD-FP. If null, then the call is to the same environment (an
807 ;;; :ASSIGNMENT), so we only move the arguments, and leave the
808 ;;; environment alone.
810 ;;; CLOSURE-FP is for calling a closure that has "implicit" value
811 ;;; cells (stored in the allocating stack frame), and is the frame
812 ;;; pointer TN to use for values allocated in the outbound stack
813 ;;; frame. This is distinct from OLD-FP for the specific case of a
815 (defun emit-psetq-moves (node block fun old-fp
&optional
(closure-fp old-fp
))
816 (declare (type combination node
) (type ir2-block block
) (type clambda fun
)
817 (type (or tn null
) old-fp closure-fp
))
818 (let ((actuals (mapcar (lambda (x)
820 (lvar-tn node block x
)))
821 (combination-args node
))))
824 (dolist (var (lambda-vars fun
))
825 (let ((actual (pop actuals
))
826 (loc (leaf-info var
)))
829 ((and (lambda-var-indirect var
)
830 (lambda-var-explicit-value-cell var
))
832 (make-normal-tn *backend-t-primitive-type
*)))
833 (emit-make-value-cell node block actual temp
)
835 ((member actual
(locs))
836 (let ((temp (make-normal-tn (tn-primitive-type loc
))))
837 (emit-move node block actual temp
)
844 (let ((this-1env (node-physenv node
))
845 (called-env (physenv-info (lambda-physenv fun
))))
846 (dolist (thing (ir2-physenv-closure called-env
))
847 (temps (closure-initial-value (car thing
) this-1env closure-fp
))
850 (locs (ir2-physenv-old-fp called-env
))))
852 (values (temps) (locs)))))
854 ;;; A tail-recursive local call is done by emitting moves of stuff
855 ;;; into the appropriate passing locations. After setting up the args
856 ;;; and environment, we just move our return-pc into the called
857 ;;; function's passing location.
858 (defun ir2-convert-tail-local-call (node block fun
)
859 (declare (type combination node
) (type ir2-block block
) (type clambda fun
))
860 (let ((this-env (physenv-info (node-physenv node
)))
861 (current-fp (make-stack-pointer-tn)))
862 (multiple-value-bind (temps locs
)
863 (emit-psetq-moves node block fun
864 (ir2-physenv-old-fp this-env
) current-fp
)
866 ;; If we're about to emit a move from CURRENT-FP then we need to
868 (when (find current-fp temps
)
869 (vop current-fp node block current-fp
))
871 (mapc (lambda (temp loc
)
872 (emit-move node block temp loc
))
875 (emit-move node block
876 (ir2-physenv-return-pc this-env
)
877 (ir2-physenv-return-pc-pass
879 (lambda-physenv fun
)))))
883 ;;; Convert an :ASSIGNMENT call. This is just like a tail local call,
884 ;;; except that the caller and callee environment are the same, so we
885 ;;; don't need to mess with the environment locations, return PC, etc.
886 (defun ir2-convert-assignment (node block fun
)
887 (declare (type combination node
) (type ir2-block block
) (type clambda fun
))
888 (multiple-value-bind (temps locs
) (emit-psetq-moves node block fun nil
)
890 (mapc (lambda (temp loc
)
891 (emit-move node block temp loc
))
895 ;;; Do stuff to set up the arguments to a non-tail local call
896 ;;; (including implicit environment args.) We allocate a frame
897 ;;; (returning the FP and NFP), and also compute the TN-REFS list for
898 ;;; the values to pass and the list of passing location TNs.
899 (defun ir2-convert-local-call-args (node block fun
)
900 (declare (type combination node
) (type ir2-block block
) (type clambda fun
))
901 (let ((fp (make-stack-pointer-tn))
902 (nfp (make-number-stack-pointer-tn))
903 (old-fp (make-stack-pointer-tn)))
904 (multiple-value-bind (temps locs
)
905 (emit-psetq-moves node block fun old-fp
)
906 (vop current-fp node block old-fp
)
907 (vop allocate-frame node block
908 (physenv-info (lambda-physenv fun
))
910 (values fp nfp temps
(mapcar #'make-alias-tn locs
)))))
912 ;;; Handle a non-TR known-values local call. We emit the call, then
913 ;;; move the results to the lvar's destination.
914 (defun ir2-convert-local-known-call (node block fun returns lvar start
)
915 (declare (type node node
) (type ir2-block block
) (type clambda fun
)
916 (type return-info returns
) (type (or lvar null
) lvar
)
918 (multiple-value-bind (fp nfp temps arg-locs
)
919 (ir2-convert-local-call-args node block fun
)
920 (let ((locs (return-info-locations returns
)))
921 (vop* known-call-local node block
922 (fp nfp
(reference-tn-list temps nil
))
923 ((reference-tn-list locs t
))
924 arg-locs
(physenv-info (lambda-physenv fun
)) start
)
925 (move-lvar-result node block locs lvar
)))
928 ;;; Handle a non-TR unknown-values local call. We do different things
929 ;;; depending on what kind of values the lvar wants.
931 ;;; If LVAR is :UNKNOWN, then we use the "multiple-" variant, directly
932 ;;; specifying the lvar's LOCS as the VOP results so that we don't
933 ;;; have to do anything after the call.
935 ;;; Otherwise, we use STANDARD-RESULT-TNS to get wired result TNs, and
936 ;;; then call MOVE-LVAR-RESULT to do any necessary type checks or
938 (defun ir2-convert-local-unknown-call (node block fun lvar start
)
939 (declare (type node node
) (type ir2-block block
) (type clambda fun
)
940 (type (or lvar null
) lvar
) (type label start
))
941 (multiple-value-bind (fp nfp temps arg-locs
)
942 (ir2-convert-local-call-args node block fun
)
943 (let ((2lvar (and lvar
(lvar-info lvar
)))
944 (env (physenv-info (lambda-physenv fun
)))
945 (temp-refs (reference-tn-list temps nil
)))
946 (if (and 2lvar
(eq (ir2-lvar-kind 2lvar
) :unknown
))
947 (vop* multiple-call-local node block
(fp nfp temp-refs
)
948 ((reference-tn-list (ir2-lvar-locs 2lvar
) t
))
950 (let ((locs (standard-result-tns lvar
)))
951 (vop* call-local node block
953 ((reference-tn-list locs t
))
954 arg-locs env start
(length locs
))
955 (move-lvar-result node block locs lvar
)))))
958 ;;; Dispatch to the appropriate function, depending on whether we have
959 ;;; a let, tail or normal call. If the function doesn't return, call
960 ;;; it using the unknown-value convention. We could compile it as a
961 ;;; tail call, but that might seem confusing in the debugger.
962 (defun ir2-convert-local-call (node block
)
963 (declare (type combination node
) (type ir2-block block
))
964 (let* ((fun (ref-leaf (lvar-uses (basic-combination-fun node
))))
965 (kind (functional-kind fun
)))
966 (cond ((eq kind
:let
)
967 (ir2-convert-let node block fun
))
968 ((eq kind
:assignment
)
969 (ir2-convert-assignment node block fun
))
971 (ir2-convert-tail-local-call node block fun
))
973 (let ((start (block-trampoline (lambda-block fun
)))
974 (returns (tail-set-info (lambda-tail-set fun
)))
975 (lvar (node-lvar node
)))
977 (return-info-kind returns
)
980 (ir2-convert-local-unknown-call node block fun lvar start
))
982 (ir2-convert-local-known-call node block fun returns
988 ;;; Given a function lvar FUN, return (VALUES TN-TO-CALL NAMED-P),
989 ;;; where TN-TO-CALL is a TN holding the thing that we call NAMED-P is
990 ;;; true if the thing is named (false if it is a function).
992 ;;; There are two interesting non-named cases:
993 ;;; -- We know it's a function. No check needed: return the
995 ;;; -- We don't know what it is.
996 (defun fun-lvar-tn (node block lvar
)
997 (declare (ignore node block
))
998 (declare (type lvar lvar
))
999 (let ((2lvar (lvar-info lvar
)))
1000 (if (eq (ir2-lvar-kind 2lvar
) :delayed
)
1001 (let ((name (lvar-fun-name lvar t
)))
1003 (values (make-load-time-constant-tn :fdefinition name
) name
))
1004 (let* ((locs (ir2-lvar-locs 2lvar
))
1006 (function-ptype (primitive-type-or-lose 'function
)))
1007 (aver (and (eq (ir2-lvar-kind 2lvar
) :fixed
)
1008 (= (length locs
) 1)))
1009 (aver (eq (tn-primitive-type loc
) function-ptype
))
1010 (values loc nil
)))))
1012 ;;; Set up the args to NODE in the current frame, and return a TN-REF
1013 ;;; list for the passing locations.
1014 (defun move-tail-full-call-args (node block
)
1015 (declare (type combination node
) (type ir2-block block
))
1016 (let ((args (basic-combination-args node
))
1019 (dotimes (num (length args
))
1020 (let ((loc (standard-arg-location num
)))
1021 (emit-move node block
(lvar-tn node block
(elt args num
)) loc
)
1022 (let ((ref (reference-tn loc nil
)))
1024 (setf (tn-ref-across last
) ref
)
1029 ;;; Move the arguments into the passing locations and do a (possibly
1030 ;;; named) tail call.
1031 (defun ir2-convert-tail-full-call (node block
)
1032 (declare (type combination node
) (type ir2-block block
))
1033 (let* ((env (physenv-info (node-physenv node
)))
1034 (args (basic-combination-args node
))
1035 (nargs (length args
))
1036 (pass-refs (move-tail-full-call-args node block
))
1037 (old-fp (ir2-physenv-old-fp env
))
1038 (return-pc (ir2-physenv-return-pc env
)))
1040 (multiple-value-bind (fun-tn named
)
1041 (fun-lvar-tn node block
(basic-combination-fun node
))
1043 (vop* tail-call-named node block
1044 (fun-tn old-fp return-pc pass-refs
)
1048 (vop* tail-call node block
1049 (fun-tn old-fp return-pc pass-refs
)
1052 (emit-step-p node
)))))
1056 ;;; like IR2-CONVERT-LOCAL-CALL-ARGS, only different
1057 (defun ir2-convert-full-call-args (node block
)
1058 (declare (type combination node
) (type ir2-block block
))
1059 (let* ((args (basic-combination-args node
))
1060 (fp (make-stack-pointer-tn))
1061 (nargs (length args
)))
1062 (vop allocate-full-call-frame node block nargs fp
)
1066 (dotimes (num nargs
)
1067 (locs (standard-arg-location num
))
1068 (let ((ref (reference-tn (lvar-tn node block
(elt args num
))
1071 (setf (tn-ref-across last
) ref
)
1075 (values fp first
(locs) nargs
)))))
1077 ;;; Do full call when a fixed number of values are desired. We make
1078 ;;; STANDARD-RESULT-TNS for our lvar, then deliver the result using
1079 ;;; MOVE-LVAR-RESULT. We do named or normal call, as appropriate.
1080 (defun ir2-convert-fixed-full-call (node block
)
1081 (declare (type combination node
) (type ir2-block block
))
1082 (multiple-value-bind (fp args arg-locs nargs
)
1083 (ir2-convert-full-call-args node block
)
1084 (let* ((lvar (node-lvar node
))
1085 (locs (standard-result-tns lvar
))
1086 (loc-refs (reference-tn-list locs t
))
1087 (nvals (length locs
)))
1088 (multiple-value-bind (fun-tn named
)
1089 (fun-lvar-tn node block
(basic-combination-fun node
))
1091 (vop* call-named node block
(fp fun-tn args
) (loc-refs)
1092 arg-locs nargs nvals
(emit-step-p node
))
1093 (vop* call node block
(fp fun-tn args
) (loc-refs)
1094 arg-locs nargs nvals
(emit-step-p node
)))
1095 (move-lvar-result node block locs lvar
))))
1098 ;;; Do full call when unknown values are desired.
1099 (defun ir2-convert-multiple-full-call (node block
)
1100 (declare (type combination node
) (type ir2-block block
))
1101 (multiple-value-bind (fp args arg-locs nargs
)
1102 (ir2-convert-full-call-args node block
)
1103 (let* ((lvar (node-lvar node
))
1104 (locs (ir2-lvar-locs (lvar-info lvar
)))
1105 (loc-refs (reference-tn-list locs t
)))
1106 (multiple-value-bind (fun-tn named
)
1107 (fun-lvar-tn node block
(basic-combination-fun node
))
1109 (vop* multiple-call-named node block
(fp fun-tn args
) (loc-refs)
1110 arg-locs nargs
(emit-step-p node
))
1111 (vop* multiple-call node block
(fp fun-tn args
) (loc-refs)
1112 arg-locs nargs
(emit-step-p node
))))))
1115 ;;; stuff to check in PONDER-FULL-CALL
1117 ;;; These came in handy when troubleshooting cold boot after making
1118 ;;; major changes in the package structure: various transforms and
1119 ;;; VOPs and stuff got attached to the wrong symbol, so that
1120 ;;; references to the right symbol were bogusly translated as full
1121 ;;; calls instead of primitives, sending the system off into infinite
1122 ;;; space. Having a report on all full calls generated makes it easier
1123 ;;; to figure out what form caused the problem this time.
1124 (declaim (type (member :minimal
:detailed
:very-detailed
:maximal
)
1125 *track-full-called-fnames
*))
1126 (defvar *track-full-called-fnames
* :minimal
)
1128 ;;; Do some checks (and store some notes relevant for future checks)
1130 ;;; * Is this a full call to something we have reason to know should
1131 ;;; never be full called? (Except as of sbcl-0.7.18 or so, we no
1132 ;;; longer try to ensure this behavior when *FAILURE-P* has already
1134 (defun ponder-full-call (node)
1135 (let* ((lvar (basic-combination-fun node
))
1136 (fname (lvar-fun-name lvar t
)))
1137 (declare (type (or symbol cons
) fname
))
1139 ;; Warn about cross-compiling certain full-calls,
1140 ;; as it is indicative of dependency order problems.
1142 (let ((compname (component-name (node-component node
))))
1143 ;; Don't care too much about macro performance.
1144 (unless (and (stringp compname
) (string/= compname
"DEFMACRO"))
1145 ;; Catch FOO and (SETF FOO) both.
1146 (let ((stem (if (atom fname
) fname
(second fname
))))
1148 sb-cold
::*full-calls-to-warn-about
*
1150 (warn "Full call to ~S" fname
)))))
1152 (let* ((inlineable-p (not (let ((*lexenv
* (node-lexenv node
)))
1153 (fun-lexically-notinline-p fname
))))
1154 (inlineable-bit (if inlineable-p
1 0))
1155 (cell (info :function
:emitted-full-calls fname
)))
1157 ;; The low bit indicates whether any not-NOTINLINE call was seen.
1158 ;; The next-lowest bit is magic. Refer to %COMPILER-DEFMACRO
1159 ;; and WARN-IF-INLINE-FAILED/CALL for the pertinent logic.
1160 (setf cell
(list (logior 4 inlineable-bit
))
1161 (info :function
:emitted-full-calls fname
) cell
)
1162 (incf (car cell
) (+ 4 (if (oddp (car cell
)) 0 inlineable-bit
))))
1163 ;; If the full call was wanted, don't record anything.
1164 ;; (This was originally for debugging SBCL self-compilation)
1167 (warn-if-inline-failed/call fname
(node-lexenv node
) cell
))
1168 (case *track-full-called-fnames
*
1170 (when (boundp 'sb
!xc
:*compile-file-pathname
*)
1171 (pushnew sb
!xc
:*compile-file-pathname
* (cdr cell
)
1174 (pushnew (component-name *component-being-compiled
*)
1175 (cdr cell
) :test
#'equalp
)))))
1177 ;; Special mode, usually only for the cross-compiler
1178 ;; and only with the feature enabled.
1179 #!+sb-show
(when (eq *track-full-called-fnames
* :maximal
)
1180 (/show
"converting full call to named function" fname
)
1181 (/show
(basic-combination-args node
))
1182 (/show
(policy node speed
) (policy node safety
))
1183 (/show
(policy node compilation-speed
))
1184 (let ((arg-types (mapcar (lambda (lvar)
1188 (basic-combination-args node
))))
1191 ;; When illegal code is compiled, all sorts of perverse paths
1192 ;; through the compiler can be taken, and it's much harder -- and
1193 ;; probably pointless -- to guarantee that always-optimized-away
1194 ;; functions are actually optimized away. Thus, we skip the check
1197 ;; check to see if we know anything about the function
1198 (let ((info (info :function
:info fname
)))
1199 ;; if we know something, check to see if the full call was valid
1200 (when (and info
(ir1-attributep (fun-info-attributes info
)
1201 always-translatable
))
1202 (/show
(policy node speed
) (policy node safety
))
1203 (/show
(policy node compilation-speed
))
1204 (bug "full call to ~S" fname
))))
1207 (aver (legal-fun-name-p fname
))))) ;; FIXME: needless check?
1209 ;;; If the call is in a tail recursive position and the return
1210 ;;; convention is standard, then do a tail full call. If one or fewer
1211 ;;; values are desired, then use a single-value call, otherwise use a
1212 ;;; multiple-values call.
1213 (defun ir2-convert-full-call (node block
)
1214 (declare (type combination node
) (type ir2-block block
))
1215 (ponder-full-call node
)
1216 (cond ((node-tail-p node
)
1217 (ir2-convert-tail-full-call node block
))
1218 ((let ((lvar (node-lvar node
)))
1220 (eq (ir2-lvar-kind (lvar-info lvar
)) :unknown
)))
1221 (ir2-convert-multiple-full-call node block
))
1223 (ir2-convert-fixed-full-call node block
)))
1226 ;;;; entering functions
1228 #!+precise-arg-count-error
1229 (defun xep-verify-arg-count (node block fun arg-count-location
)
1230 (when (policy fun
(plusp verify-arg-count
))
1231 (let* ((ef (functional-entry-fun fun
))
1232 (optional (optional-dispatch-p ef
))
1234 (optional-dispatch-min-args ef
)))
1235 (max (cond ((not optional
)
1236 (1- (length (lambda-vars fun
))))
1238 (not (optional-dispatch-more-entry ef
)))
1239 (optional-dispatch-max-args ef
)))))
1240 (unless (and (eql min
0) (not max
))
1241 (vop verify-arg-count node block
1247 ;;; Do all the stuff that needs to be done on XEP entry:
1248 ;;; -- Create frame.
1249 ;;; -- Copy any more arg.
1250 ;;; -- Set up the environment, accessing any closure variables.
1251 ;;; -- Move args from the standard passing locations to their internal
1253 (defun init-xep-environment (node block fun
)
1254 (declare (type bind node
) (type ir2-block block
) (type clambda fun
))
1255 (let ((start-label (entry-info-offset (leaf-info fun
)))
1256 (env (physenv-info (node-physenv node
)))
1258 (let ((ef (functional-entry-fun fun
)))
1259 (vop xep-allocate-frame node block start-label
)
1260 ;; Arg verification needs to be done before the stack pointer is adjusted
1261 ;; so that the extra arguments are still present when the error is signalled
1262 (let ((verified (unless (eq (functional-kind fun
) :toplevel
)
1263 (setf arg-count-tn
(make-arg-count-location))
1264 #!+precise-arg-count-error
1265 (xep-verify-arg-count node block fun arg-count-tn
))))
1267 (declare (ignore verified
))
1268 (cond ((and (optional-dispatch-p ef
) (optional-dispatch-more-entry ef
))
1269 ;; COPY-MORE-ARG should handle SP adjustemnt, but it
1270 ;; isn't done on all targets.
1271 #!-precise-arg-count-error
1272 (vop xep-setup-sp node block
)
1273 (vop copy-more-arg node block
(optional-dispatch-max-args ef
)
1274 #!+x86-64 verified
))
1276 (vop xep-setup-sp node block
))))
1277 (when (ir2-physenv-closure env
)
1278 (let ((closure (make-normal-tn *backend-t-primitive-type
*)))
1279 (when (policy fun
(> store-closure-debug-pointer
1))
1280 ;; Save the closure pointer on the stack.
1281 (let ((closure-save (make-representation-tn
1282 *backend-t-primitive-type
*
1283 (sc-number-or-lose 'sb
!vm
::control-stack
))))
1284 (vop setup-closure-environment node block start-label
1286 (setf (ir2-physenv-closure-save-tn env
) closure-save
)
1287 (component-live-tn closure-save
)))
1288 (vop setup-closure-environment node block start-label closure
)
1290 (dolist (loc (ir2-physenv-closure env
))
1291 (vop closure-ref node block closure
(incf n
) (cdr loc
)))))))
1292 (unless (eq (functional-kind fun
) :toplevel
)
1293 (let ((vars (lambda-vars fun
))
1295 (when (leaf-refs (first vars
))
1296 (emit-move node block arg-count-tn
(leaf-info (first vars
))))
1297 (dolist (arg (rest vars
))
1298 (when (leaf-refs arg
)
1299 (let ((pass (standard-arg-location n
))
1300 (home (leaf-info arg
)))
1301 (if (and (lambda-var-indirect arg
)
1302 (lambda-var-explicit-value-cell arg
))
1303 (emit-make-value-cell node block pass home
)
1304 (emit-move node block pass home
))))
1307 (emit-move node block
(make-old-fp-passing-location t
)
1308 (ir2-physenv-old-fp env
)))
1312 ;;; Emit function prolog code. This is only called on bind nodes for
1313 ;;; functions that allocate environments. All semantics of let calls
1314 ;;; are handled by IR2-CONVERT-LET.
1316 ;;; If not an XEP, all we do is move the return PC from its passing
1317 ;;; location, since in a local call, the caller allocates the frame
1318 ;;; and sets up the arguments.
1320 #!+unwind-to-frame-and-call-vop
1321 (defun save-bsp (node block env
)
1322 ;; Save BSP on stack so that the binding environment can be restored
1323 ;; when restarting frames.
1324 ;; This is done inside functions, which leaves XEPs without saved
1325 ;; BSP, though the code in XEPs doesn't bind any variables, it can
1326 ;; call arbitrary code through the SATISFIES declaration.
1327 ;; And functions called by SATISFIES are not inlined, except for
1328 ;; source transforms, but these usually do not bind anything.
1329 ;; Thus when restarting it needs to check that the interrupt was in
1332 ;; It could be saved from the XEP, but some functions have both
1333 ;; external and internal entry points, so it will be saved twice.
1334 (let ((temp (make-normal-tn *backend-t-primitive-type
*))
1335 (bsp-save-tn (make-representation-tn
1336 *backend-t-primitive-type
*
1337 (sc-number-or-lose 'sb
!vm
::control-stack
))))
1338 (vop current-binding-pointer node block temp
)
1339 (emit-move node block temp bsp-save-tn
)
1340 (setf (ir2-physenv-bsp-save-tn env
) bsp-save-tn
)
1341 (component-live-tn bsp-save-tn
)))
1343 (defun ir2-convert-bind (node block
)
1344 (declare (type bind node
) (type ir2-block block
))
1345 (let* ((fun (bind-lambda node
))
1346 (env (physenv-info (lambda-physenv fun
))))
1347 (aver (member (functional-kind fun
)
1348 '(nil :external
:optional
:toplevel
:cleanup
)))
1351 (init-xep-environment node block fun
)
1353 (when *collect-dynamic-statistics
*
1354 (vop count-me node block
*dynamic-counts-tn
*
1355 (block-number (ir2-block-block block
)))))
1356 ((policy fun
(> store-closure-debug-pointer
1))
1357 ;; Propagate the location of the closure pointer from the
1358 ;; enclosing functions. (FIXME: Should make sure that this
1359 ;; handles closures inside closures correctly). [remark by JES]
1360 (let* ((entry-fun (lambda-entry-fun fun
)))
1362 (let ((2env (physenv-info (lambda-physenv fun
)))
1363 (entry-2env (physenv-info (lambda-physenv entry-fun
))))
1364 (setf (ir2-physenv-closure-save-tn 2env
)
1365 (ir2-physenv-closure-save-tn entry-2env
)))))))
1369 (ir2-physenv-return-pc-pass env
)
1370 (ir2-physenv-return-pc env
))
1371 #!+unwind-to-frame-and-call-vop
1372 (when (and (lambda-allow-instrumenting fun
)
1373 (not (lambda-inline-expanded fun
))
1374 (policy fun
(>= insert-debug-catch
1)))
1375 (save-bsp node block env
))
1377 (let ((lab (gen-label)))
1378 (setf (ir2-physenv-environment-start env
) lab
)
1379 (vop note-environment-start node block lab
)
1381 (unless (policy fun
(>= inhibit-safepoints
2))
1382 (vop sb
!vm
::insert-safepoint node block
))))
1386 ;;;; function return
1388 ;;; Do stuff to return from a function with the specified values and
1389 ;;; convention. If the return convention is :FIXED and we aren't
1390 ;;; returning from an XEP, then we do a known return (letting
1391 ;;; representation selection insert the correct move-arg VOPs.)
1392 ;;; Otherwise, we use the unknown-values convention. If there is a
1393 ;;; fixed number of return values, then use RETURN, otherwise use
1394 ;;; RETURN-MULTIPLE.
1395 (defun ir2-convert-return (node block
)
1396 (declare (type creturn node
) (type ir2-block block
))
1397 (let* ((lvar (return-result node
))
1398 (2lvar (lvar-info lvar
))
1399 (lvar-kind (ir2-lvar-kind 2lvar
))
1400 (fun (return-lambda node
))
1401 (env (physenv-info (lambda-physenv fun
)))
1402 (old-fp (ir2-physenv-old-fp env
))
1403 (return-pc (ir2-physenv-return-pc env
))
1404 (returns (tail-set-info (lambda-tail-set fun
))))
1406 ((and (eq (return-info-kind returns
) :fixed
)
1408 (let ((locs (lvar-tns node block lvar
1409 (return-info-types returns
))))
1410 (vop* known-return node block
1411 (old-fp return-pc
(reference-tn-list locs nil
))
1413 (return-info-locations returns
))))
1414 ((eq lvar-kind
:fixed
)
1415 (let* ((types (mapcar #'tn-primitive-type
(ir2-lvar-locs 2lvar
)))
1416 (lvar-locs (lvar-tns node block lvar types
))
1417 (nvals (length lvar-locs
))
1418 (locs (make-standard-value-tns nvals
)))
1419 (mapc (lambda (val loc
)
1420 (emit-move node block val loc
))
1424 (vop return-single node block old-fp return-pc
(car locs
))
1425 (vop* return node block
1426 (old-fp return-pc
(reference-tn-list locs nil
))
1430 (aver (eq lvar-kind
:unknown
))
1431 (vop* return-multiple node block
1433 (reference-tn-list (ir2-lvar-locs 2lvar
) nil
))
1440 ;;;; These are used by the debugger to find the top function on the
1441 ;;;; stack. They return the OLD-FP and RETURN-PC for the current
1442 ;;;; function as multiple values.
1444 (defoptimizer (%caller-frame ir2-convert
) (() node block
)
1445 (let ((ir2-physenv (physenv-info (node-physenv node
))))
1446 (move-lvar-result node block
1447 (list (ir2-physenv-old-fp ir2-physenv
))
1450 (defoptimizer (%caller-pc ir2-convert
) (() node block
)
1451 (let ((ir2-physenv (physenv-info (node-physenv node
))))
1452 (move-lvar-result node block
1453 (list (ir2-physenv-return-pc ir2-physenv
))
1456 ;;;; multiple values
1458 ;;; This is almost identical to IR2-CONVERT-LET. Since LTN annotates
1459 ;;; the lvar for the correct number of values (with the lvar user
1460 ;;; responsible for defaulting), we can just pick them up from the
1462 (defun ir2-convert-mv-bind (node block
)
1463 (declare (type mv-combination node
) (type ir2-block block
))
1464 (let* ((fun (ref-leaf (lvar-uses (basic-combination-fun node
))))
1465 (args (basic-combination-args node
))
1466 (vars (lambda-vars fun
)))
1467 (aver (eq (functional-kind fun
) :mv-let
))
1468 (mapc (lambda (src var
)
1469 (when (leaf-refs var
)
1470 (let ((dest (leaf-info var
)))
1471 (if (and (lambda-var-indirect var
)
1472 (lambda-var-explicit-value-cell var
))
1473 (emit-make-value-cell node block src dest
)
1474 (emit-move node block src dest
)))))
1475 (if (singleton-p args
)
1476 (lvar-tns node block
(first args
)
1478 (primitive-type (leaf-type x
)))
1481 (loop for lvar in args
1482 for values
= (nth-value 1 (values-types
1483 (lvar-derived-type lvar
)))
1486 (lvar-tns node block lvar
(loop repeat values
1487 collect
(primitive-type (leaf-type (pop vars
))))))))
1491 ;;; Emit the appropriate fixed value, unknown value or tail variant of
1492 ;;; CALL-VARIABLE. Note that we only need to pass the values start for
1493 ;;; the first argument: all the other argument lvar TNs are
1494 ;;; ignored. This is because we require all of the values globs to be
1495 ;;; contiguous and on stack top.
1496 (defun ir2-convert-mv-call (node block
)
1497 (declare (type mv-combination node
) (type ir2-block block
))
1498 (aver (basic-combination-args node
))
1499 (let* ((start-lvar (lvar-info (first (basic-combination-args node
))))
1500 (start (first (ir2-lvar-locs start-lvar
)))
1501 (tails (and (node-tail-p node
)
1502 (lambda-tail-set (node-home-lambda node
))))
1503 (lvar (node-lvar node
))
1504 (2lvar (and lvar
(lvar-info lvar
))))
1505 (multiple-value-bind (fun named
)
1506 (fun-lvar-tn node block
(basic-combination-fun node
))
1507 (aver (and (not named
)
1508 (eq (ir2-lvar-kind start-lvar
) :unknown
)))
1511 (let ((env (physenv-info (node-physenv node
))))
1512 (vop tail-call-variable node block start fun
1513 (ir2-physenv-old-fp env
)
1514 (ir2-physenv-return-pc env
))))
1516 (eq (ir2-lvar-kind 2lvar
) :unknown
))
1517 (vop* multiple-call-variable node block
(start fun nil
)
1518 ((reference-tn-list (ir2-lvar-locs 2lvar
) t
))
1519 (emit-step-p node
)))
1521 (let ((locs (standard-result-tns lvar
)))
1522 (vop* call-variable node block
(start fun nil
)
1523 ((reference-tn-list locs t
)) (length locs
)
1525 (move-lvar-result node block locs lvar
)))))))
1527 ;;; Reset the stack pointer to the start of the specified
1528 ;;; unknown-values lvar (discarding it and all values globs on top of
1530 (defoptimizer (%pop-values ir2-convert
) ((%lvar
) node block
)
1531 (let* ((lvar (lvar-value %lvar
))
1532 (2lvar (lvar-info lvar
)))
1533 (cond ((eq (ir2-lvar-kind 2lvar
) :unknown
)
1534 (vop reset-stack-pointer node block
1535 (first (ir2-lvar-locs 2lvar
))))
1536 ((lvar-dynamic-extent lvar
)
1537 (vop reset-stack-pointer node block
1538 (ir2-lvar-stack-pointer 2lvar
)))
1539 (t (bug "Trying to pop a not stack-allocated LVAR ~S."
1542 (defoptimizer (%nip-values ir2-convert
) ((last-nipped last-preserved
1545 (let* ( ;; pointer immediately after the nipped block
1546 (after (lvar-value last-nipped
))
1547 (2after (lvar-info after
))
1548 ;; pointer to the first nipped word
1549 (first (lvar-value last-preserved
))
1550 (2first (lvar-info first
))
1552 (moved-tns (loop for lvar-ref in moved
1553 for lvar
= (lvar-value lvar-ref
)
1554 for
2lvar
= (lvar-info lvar
)
1556 collect
(first (ir2-lvar-locs 2lvar
)))))
1557 (aver (or (eq (ir2-lvar-kind 2after
) :unknown
)
1558 (lvar-dynamic-extent after
)))
1559 (aver (eq (ir2-lvar-kind 2first
) :unknown
))
1560 (when *check-consistency
*
1561 ;; we cannot move stack-allocated DX objects
1562 (dolist (moved-lvar moved
)
1563 (aver (eq (ir2-lvar-kind (lvar-info (lvar-value moved-lvar
)))
1565 (flet ((nip-aligned (nipped)
1566 (vop* %%nip-values node block
1568 (first (ir2-lvar-locs 2first
))
1569 (reference-tn-list moved-tns nil
))
1570 ((reference-tn-list moved-tns t
)))))
1571 (cond ((eq (ir2-lvar-kind 2after
) :unknown
)
1572 (nip-aligned (first (ir2-lvar-locs 2after
))))
1573 ((lvar-dynamic-extent after
)
1574 (nip-aligned (ir2-lvar-stack-pointer 2after
)))
1576 (bug "Trying to nip a not stack-allocated LVAR ~S." after
))))))
1578 (defoptimizer (%dummy-dx-alloc ir2-convert
) ((target source
) node block
)
1579 (let* ((target-lvar (lvar-value target
))
1580 (source-lvar (lvar-value source
))
1581 (target-2lvar (lvar-info target-lvar
))
1582 (source-2lvar (and source-lvar
(lvar-info source-lvar
))))
1583 (aver (lvar-dynamic-extent target-lvar
))
1584 (cond ((not source-lvar
)
1585 (vop current-stack-pointer node block
1586 (ir2-lvar-stack-pointer target-2lvar
)))
1587 ((lvar-dynamic-extent source-lvar
)
1588 (emit-move node block
1589 (ir2-lvar-stack-pointer source-2lvar
)
1590 (ir2-lvar-stack-pointer target-2lvar
)))
1591 ((eq (ir2-lvar-kind source-2lvar
) :unknown
)
1592 (emit-move node block
1593 (first (ir2-lvar-locs source-2lvar
))
1594 (ir2-lvar-stack-pointer target-2lvar
)))
1595 (t (bug "Trying to dummy up DX allocation from a ~
1596 not stack-allocated LVAR ~S." source-lvar
)))))
1598 ;;; Deliver the values TNs to LVAR using MOVE-LVAR-RESULT.
1599 (defoptimizer (values ir2-convert
) ((&rest values
) node block
)
1600 (let ((tns (mapcar (lambda (x)
1601 (lvar-tn node block x
))
1604 (move-lvar-result node block tns
(node-lvar node
))))
1606 ;;; In the normal case where unknown values are desired, we use the
1607 ;;; VALUES-LIST VOP. In the relatively unimportant case of VALUES-LIST
1608 ;;; for a fixed number of values, we punt by doing a full call to the
1609 ;;; VALUES-LIST function. This gets the full call VOP to deal with
1610 ;;; defaulting any unsupplied values. It seems unworthwhile to
1611 ;;; optimize this case.
1612 (defoptimizer (values-list ir2-convert
) ((list) node block
)
1613 (let* ((lvar (node-lvar node
))
1614 (2lvar (and lvar
(lvar-info lvar
))))
1616 (eq (ir2-lvar-kind 2lvar
) :unknown
))
1617 (let ((locs (ir2-lvar-locs 2lvar
)))
1618 (vop* values-list node block
1619 ((lvar-tn node block list
) nil
)
1620 ((reference-tn-list locs t
)))))
1621 (t (aver (or (not 2lvar
) ; i.e. we want to check the argument
1622 (eq (ir2-lvar-kind 2lvar
) :fixed
)))
1623 (ir2-convert-full-call node block
)))))
1625 (defoptimizer (%more-arg-values ir2-convert
) ((context start count
) node block
)
1626 (binding* ((lvar (node-lvar node
) :exit-if-null
)
1627 (2lvar (lvar-info lvar
)))
1628 (ecase (ir2-lvar-kind 2lvar
)
1630 ;; KLUDGE: this is very much unsafe, and can leak random stack values.
1631 ;; OTOH, I think the :FIXED case can only happen with (safety 0) in the
1634 (loop for loc in
(ir2-lvar-locs 2lvar
)
1636 do
(vop sb
!vm
::more-arg node block
1637 (lvar-tn node block context
)
1641 (let ((locs (ir2-lvar-locs 2lvar
)))
1642 (vop* %more-arg-values node block
1643 ((lvar-tn node block context
)
1644 (lvar-tn node block start
)
1645 (lvar-tn node block count
)
1647 ((reference-tn-list locs t
))))))))
1649 ;;;; special binding
1651 ;;; This is trivial, given our assumption of a shallow-binding
1653 (defoptimizer (%special-bind ir2-convert
) ((var value
) node block
)
1654 (let ((name (leaf-source-name (lvar-value var
))))
1655 ;; Emit either BIND or DYNBIND, preferring BIND if both exist.
1656 ;; If only one exists, it's DYNBIND.
1657 ;; Even if the backend supports load-time TLS index assignment,
1658 ;; there might be only one vop (as with arm64).
1659 (macrolet ((doit (bind dynbind
)
1660 (if (gethash 'bind
*backend-parsed-vops
*) bind dynbind
)))
1663 ;; Inform later SYMBOL-VALUE calls that they can
1664 ;; assume a nonzero tls-index.
1665 ;; FIXME: setting INFO is inefficient when not actually
1666 ;; changing anything
1667 (unless (info :variable
:wired-tls name
)
1668 (setf (info :variable
:wired-tls name
) :always-has-tls
))
1669 ;; We force the symbol into the code constants in case BIND
1670 ;; does not actually reference it, as with x86.
1671 (emit-constant name
)
1672 (vop bind node block
(lvar-tn node block value
) name
))
1673 (vop dynbind node block
(lvar-tn node block value
)
1674 (emit-constant name
))))))
1676 (defoptimizer (%special-unbind ir2-convert
) ((n) node block
)
1677 (declare (ignorable n
))
1678 (vop unbind node block
#!+(and sb-thread unbind-n-vop
) (lvar-value n
)))
1680 ;;; ### It's not clear that this really belongs in this file, or
1681 ;;; should really be done this way, but this is the least violation of
1682 ;;; abstraction in the current setup. We don't want to wire
1683 ;;; shallow-binding assumptions into IR1tran.
1684 (def-ir1-translator progv
1685 ((vars vals
&body body
) start next result
)
1688 (with-unique-names (bind unbind
)
1689 (once-only ((n-save-bs '(%primitive current-binding-pointer
)))
1692 (labels ((,unbind
(vars)
1693 (declare (optimize (speed 2) (debug 0)))
1694 (let ((unbound-marker (%primitive make-unbound-marker
)))
1696 ;; CLHS says "bound and then made to have no value" -- user
1697 ;; should not be able to tell the difference between that and this.
1698 (about-to-modify-symbol-value var
'progv
)
1699 (%primitive dynbind unbound-marker var
))))
1701 (declare (optimize (speed 2) (debug 0)
1702 (insert-debug-catch 0)))
1704 ((null vals
) (,unbind vars
))
1706 (let ((val (car vals
))
1708 (about-to-modify-symbol-value var
'progv val t
)
1709 (%primitive dynbind val var
))
1710 (,bind
(cdr vars
) (cdr vals
))))))
1711 (,bind
,vars
,vals
))
1714 ;; Technically ANSI CL doesn't allow declarations at the
1715 ;; start of the cleanup form. SBCL happens to allow for
1716 ;; them, due to the way the UNWIND-PROTECT ir1 translation
1717 ;; is implemented; the cleanup forms are directly spliced
1718 ;; into an FLET definition body. And a declaration here
1719 ;; actually has exactly the right scope for what we need
1720 ;; (ensure that debug instrumentation is not emitted for the
1721 ;; cleanup function). -- JES, 2007-06-16
1722 (declare (optimize (insert-debug-catch 0)))
1723 (%primitive unbind-to-here
,n-save-bs
))))))
1727 ;;; Convert a non-local lexical exit. First find the NLX-INFO in our
1728 ;;; environment. Note that this is never called on the escape exits
1729 ;;; for CATCH and UNWIND-PROTECT, since the escape functions aren't
1731 (defun ir2-convert-exit (node block
)
1732 (declare (type exit node
) (type ir2-block block
))
1733 (let* ((nlx (exit-nlx-info node
))
1734 (loc (find-in-physenv nlx
(node-physenv node
)))
1735 (temp (make-stack-pointer-tn))
1736 (value (exit-value node
)))
1737 (if (nlx-info-safe-p nlx
)
1738 (vop value-cell-ref node block loc temp
)
1739 (emit-move node block loc temp
))
1741 (let ((locs (ir2-lvar-locs (lvar-info value
))))
1742 (vop unwind node block temp
(first locs
) (second locs
)))
1743 (let ((0-tn (emit-constant 0)))
1744 (vop unwind node block temp
0-tn
0-tn
))))
1748 ;;; %CLEANUP-POINT doesn't do anything except prevent the body from
1749 ;;; being entirely deleted.
1750 (defoptimizer (%cleanup-point ir2-convert
) (() node block
) node block
)
1752 ;;; This function invalidates a lexical exit on exiting from the
1753 ;;; dynamic extent. This is done by storing 0 into the indirect value
1754 ;;; cell that holds the closed unwind block.
1755 (defoptimizer (%lexical-exit-breakup ir2-convert
) ((info) node block
)
1756 (let ((nlx (lvar-value info
)))
1757 (when (nlx-info-safe-p nlx
)
1758 (vop value-cell-set node block
1759 (find-in-physenv nlx
(node-physenv node
))
1760 (emit-constant 0)))))
1762 ;;; We have to do a spurious move of no values to the result lvar so
1763 ;;; that lifetime analysis won't get confused.
1764 (defun ir2-convert-throw (node block
)
1765 (declare (type mv-combination node
) (type ir2-block block
))
1766 (let ((args (basic-combination-args node
)))
1767 (check-catch-tag-type (first args
))
1768 (vop* throw node block
1769 ((lvar-tn node block
(first args
))
1771 (ir2-lvar-locs (lvar-info (second args
)))
1774 (move-lvar-result node block
() (node-lvar node
))
1777 ;;; Emit code to set up a non-local exit. INFO is the NLX-INFO for the
1778 ;;; exit, and TAG is the lvar for the catch tag (if any.) We get at
1779 ;;; the target PC by passing in the label to the vop. The vop is
1780 ;;; responsible for building a return-PC object.
1781 (defun emit-nlx-start (node block info tag
)
1782 (declare (type node node
) (type ir2-block block
) (type nlx-info info
)
1783 (type (or lvar null
) tag
))
1784 (let* ((2info (nlx-info-info info
))
1785 (kind (cleanup-kind (nlx-info-cleanup info
)))
1786 (block-tn (physenv-live-tn
1788 (primitive-type-or-lose
1792 ((:unwind-protect
:block
:tagbody
)
1794 (node-physenv node
)))
1795 (res (make-stack-pointer-tn))
1796 (target-label (ir2-nlx-info-target 2info
)))
1798 (vop current-binding-pointer node block
1799 (car (ir2-nlx-info-dynamic-state 2info
)))
1800 (vop* save-dynamic-state node block
1802 ((reference-tn-list (cdr (ir2-nlx-info-dynamic-state 2info
)) t
)))
1803 (vop current-stack-pointer node block
(ir2-nlx-info-save-sp 2info
))
1807 (vop make-catch-block node block block-tn
1808 (lvar-tn node block tag
) target-label res
))
1809 ((:unwind-protect
:block
:tagbody
)
1810 (vop make-unwind-block node block block-tn target-label res
)))
1814 (if (nlx-info-safe-p info
)
1815 (emit-make-value-cell node block res
(ir2-nlx-info-home 2info
))
1816 (emit-move node block res
(ir2-nlx-info-home 2info
))))
1818 (vop set-unwind-protect node block block-tn
))
1823 ;;; Scan each of ENTRY's exits, setting up the exit for each lexical exit.
1824 (defun ir2-convert-entry (node block
)
1825 (declare (type entry node
) (type ir2-block block
))
1827 (dolist (exit (entry-exits node
))
1828 (let ((info (exit-nlx-info exit
)))
1830 (not (memq info nlxes
))
1831 (member (cleanup-kind (nlx-info-cleanup info
))
1832 '(:block
:tagbody
)))
1834 (emit-nlx-start node block info nil
)))))
1837 ;;; Set up the unwind block for these guys.
1838 (defoptimizer (%catch ir2-convert
) ((info-lvar tag
) node block
)
1839 (check-catch-tag-type tag
)
1840 (emit-nlx-start node block
(lvar-value info-lvar
) tag
))
1841 (defoptimizer (%unwind-protect ir2-convert
) ((info-lvar cleanup
) node block
)
1842 (declare (ignore cleanup
))
1843 (emit-nlx-start node block
(lvar-value info-lvar
) nil
))
1845 ;;; Emit the entry code for a non-local exit. We receive values and
1846 ;;; restore dynamic state.
1848 ;;; In the case of a lexical exit or CATCH, we look at the exit lvar's
1849 ;;; kind to determine which flavor of entry VOP to emit. If unknown
1850 ;;; values, emit the xxx-MULTIPLE variant to the lvar locs. If fixed
1851 ;;; values, make the appropriate number of temps in the standard
1852 ;;; values locations and use the other variant, delivering the temps
1853 ;;; to the lvar using MOVE-LVAR-RESULT.
1855 ;;; In the UNWIND-PROTECT case, we deliver the first register
1856 ;;; argument, the argument count and the argument pointer to our lvar
1857 ;;; as multiple values. These values are the block exited to and the
1858 ;;; values start and count.
1860 ;;; After receiving values, we restore dynamic state. Except in the
1861 ;;; UNWIND-PROTECT case, the values receiving restores the stack
1862 ;;; pointer. In an UNWIND-PROTECT cleanup, we want to leave the stack
1863 ;;; pointer alone, since the thrown values are still out there.
1864 (defoptimizer (%nlx-entry ir2-convert
) ((info-lvar) node block
)
1865 (let* ((info (lvar-value info-lvar
))
1866 (lvar (node-lvar node
))
1867 (2info (nlx-info-info info
))
1868 (top-loc (ir2-nlx-info-save-sp 2info
))
1869 (start-loc (make-nlx-entry-arg-start-location))
1870 (count-loc (make-arg-count-location))
1871 (target (ir2-nlx-info-target 2info
)))
1873 (ecase (cleanup-kind (nlx-info-cleanup info
))
1874 ((:catch
:block
:tagbody
)
1875 (let ((2lvar (and lvar
(lvar-info lvar
))))
1876 (if (and 2lvar
(eq (ir2-lvar-kind 2lvar
) :unknown
))
1877 (vop* nlx-entry-multiple node block
1878 (top-loc start-loc count-loc nil
)
1879 ((reference-tn-list (ir2-lvar-locs 2lvar
) t
))
1881 (let ((locs (standard-result-tns lvar
)))
1882 (vop* nlx-entry node block
1883 (top-loc start-loc count-loc nil
)
1884 ((reference-tn-list locs t
))
1887 (move-lvar-result node block locs lvar
)))))
1889 (let ((block-loc (standard-arg-location 0)))
1890 (vop uwp-entry node block target block-loc start-loc count-loc
)
1893 (list block-loc start-loc count-loc
)
1897 (when *collect-dynamic-statistics
*
1898 (vop count-me node block
*dynamic-counts-tn
*
1899 (block-number (ir2-block-block block
))))
1901 (vop* restore-dynamic-state node block
1902 ((reference-tn-list (cdr (ir2-nlx-info-dynamic-state 2info
)) nil
))
1904 (vop unbind-to-here node block
1905 (car (ir2-nlx-info-dynamic-state 2info
)))))
1907 ;;;; n-argument functions
1909 (macrolet ((def (name)
1910 `(defoptimizer (,name ir2-convert
) ((&rest args
) node block
)
1913 (/ sb
!vm
:large-object-size
1914 (* sb
!vm
:n-word-bytes
2)))
1915 ;; The VOPs will try to allocate all space at once
1916 ;; And it'll end up in large objects, and no conses
1917 ;; are welcome there.
1918 (ir2-convert-full-call node block
))
1920 (let* ((refs (reference-tn-list
1921 (loop for arg in args
1922 for tn
= (make-normal-tn *backend-t-primitive-type
*)
1924 (emit-move node block
(lvar-tn node block arg
) tn
)
1927 (lvar (node-lvar node
))
1928 (res (lvar-result-tns
1930 (list (primitive-type (specifier-type 'list
))))))
1931 (when (and lvar
(lvar-dynamic-extent lvar
))
1932 (vop current-stack-pointer node block
1933 (ir2-lvar-stack-pointer (lvar-info lvar
))))
1934 (vop* ,name node block
(refs) ((first res
) nil
)
1936 (move-lvar-result node block res lvar
)))))))
1941 (defoptimizer (mask-signed-field ir2-convert
) ((width x
) node block
)
1943 (when (constant-lvar-p width
)
1944 (case (lvar-value width
)
1945 (#.
(- sb
!vm
:n-word-bits sb
!vm
:n-fixnum-tag-bits
)
1946 (when (or (csubtypep (lvar-type x
)
1947 (specifier-type 'word
))
1948 (csubtypep (lvar-type x
)
1949 (specifier-type 'sb
!vm
:signed-word
)))
1950 (let* ((lvar (node-lvar node
))
1951 (temp (make-normal-tn
1952 (if (csubtypep (lvar-type x
)
1953 (specifier-type 'word
))
1954 (primitive-type-of most-positive-word
)
1956 (- (ash most-positive-word -
1))))))
1957 (results (lvar-result-tns
1959 (list (primitive-type-or-lose 'fixnum
)))))
1960 (emit-move node block
(lvar-tn node block x
) temp
)
1961 (vop sb
!vm
::move-from-word
/fixnum node block
1962 temp
(first results
))
1963 (move-lvar-result node block results lvar
)
1965 (#.sb
!vm
:n-word-bits
1966 (when (csubtypep (lvar-type x
) (specifier-type 'word
))
1967 (let* ((lvar (node-lvar node
))
1968 (temp (make-normal-tn
1969 (primitive-type-of most-positive-word
)))
1970 (results (lvar-result-tns
1972 (list (primitive-type
1973 (specifier-type 'sb
!vm
:signed-word
))))))
1974 (emit-move node block
(lvar-tn node block x
) temp
)
1975 (vop sb
!vm
::word-move node block
1976 temp
(first results
))
1977 (move-lvar-result node block results lvar
)
1979 (if (template-p (basic-combination-info node
))
1980 (ir2-convert-template node block
)
1981 (ir2-convert-full-call node block
))))
1983 ;; just a fancy identity
1984 (defoptimizer (%typep-wrapper ir2-convert
) ((value variable type
) node block
)
1985 (declare (ignore variable type
))
1986 (let* ((lvar (node-lvar node
))
1987 (results (lvar-result-tns lvar
(list (primitive-type-or-lose t
)))))
1988 (emit-move node block
(lvar-tn node block value
) (first results
))
1989 (move-lvar-result node block results lvar
)))
1991 ;;; An identity to avoid complaints about constant modification
1992 (defoptimizer (ltv-wrapper ir2-convert
) ((x) node block
)
1993 (let* ((lvar (node-lvar node
))
1994 (results (lvar-result-tns lvar
(list (primitive-type-or-lose t
)))))
1995 (emit-move node block
(lvar-tn node block x
) (first results
))
1996 (move-lvar-result node block results lvar
)))
1998 #-sb-xc-host
;; package-lock-violation-p is not present yet
1999 (defoptimizer (set ir2-hook
) ((symbol value
) node block
)
2000 (declare (ignore value block
))
2001 (when (constant-lvar-p symbol
)
2002 (let* ((symbol (lvar-value symbol
))
2003 (kind (info :variable
:kind symbol
)))
2004 (when (and (eq kind
:unknown
)
2005 (sb!impl
::package-lock-violation-p
(symbol-package symbol
) symbol
))
2006 (let ((*compiler-error-context
* node
))
2007 (compiler-warn "violating package lock on ~/sb-impl:print-symbol-with-prefix/"
2010 ;;; Convert the code in a component into VOPs.
2011 (defun ir2-convert (component)
2012 (declare (type component component
))
2013 (let (#!+sb-dyncount
2014 (*dynamic-counts-tn
*
2015 (when *collect-dynamic-statistics
*
2017 (block-number (block-next (component-head component
))))
2018 (counts (make-array blocks
2019 :element-type
'(unsigned-byte 32)
2020 :initial-element
0))
2021 (info (make-dyncount-info
2022 :for
(component-name component
)
2023 :costs
(make-array blocks
2024 :element-type
'(unsigned-byte 32)
2027 (setf (ir2-component-dyncount-info (component-info component
))
2029 (emit-constant info
)
2030 (emit-constant counts
)))))
2032 (declare (type index num
))
2033 (do-ir2-blocks (2block component
)
2034 (let ((block (ir2-block-block 2block
)))
2035 (when (block-start block
)
2036 (setf (block-number block
) num
)
2038 (when *collect-dynamic-statistics
*
2039 (let ((first-node (block-start-node block
)))
2040 (unless (or (and (bind-p first-node
)
2041 (xep-p (bind-lambda first-node
)))
2043 (node-lvar first-node
))
2048 #!+sb-dyncount
*dynamic-counts-tn
* #!-sb-dyncount nil
2051 (let ((first-node (block-start-node block
)))
2052 (unless (or (and (bind-p first-node
)
2053 ;; Bind-nodes already have safepoints
2054 (eq (bind-lambda first-node
)
2055 (lambda-home (bind-lambda first-node
))))
2056 (and (valued-node-p first-node
)
2057 (node-lvar first-node
)
2059 (node-lvar first-node
))
2061 (when (and (rest (block-pred block
))
2063 (member (loop-kind (block-loop block
))
2064 '(:natural
:strange
))
2065 (eq block
(loop-head (block-loop block
)))
2066 (policy first-node
(< inhibit-safepoints
2)))
2067 (vop sb
!vm
::insert-safepoint first-node
2block
))))
2068 (ir2-convert-block block
)
2072 ;;; If necessary, emit a terminal unconditional branch to go to the
2073 ;;; successor block. If the successor is the component tail, then
2074 ;;; there isn't really any successor, but if the end is a non-tail
2075 ;;; call to a function that's not *known* to never return, then we
2076 ;;; emit an error trap just in case the function really does return.
2078 ;;; Trapping after known calls makes it easier to understand type
2079 ;;; derivation bugs at runtime: they show up as nil-fun-returned-error,
2080 ;;; rather than the execution of arbitrary code or error traps.
2081 (defun finish-ir2-block (block)
2082 (declare (type cblock block
))
2083 (let* ((2block (block-info block
))
2084 (last (block-last block
))
2085 (succ (block-succ block
)))
2087 (aver (singleton-p succ
))
2088 (let ((target (first succ
)))
2089 (cond ((eq target
(component-tail (block-component block
)))
2090 (when (and (basic-combination-p last
)
2091 (or (eq (basic-combination-kind last
) :full
)
2092 (and (eq (basic-combination-kind last
) :known
)
2093 (eq (basic-combination-info last
) :full
))))
2094 (let* ((fun (basic-combination-fun last
))
2095 (use (lvar-uses fun
))
2096 (name (and (ref-p use
)
2097 (leaf-has-source-name-p (ref-leaf use
))
2098 (leaf-source-name (ref-leaf use
))))
2099 (ftype (and (info :function
:info name
) ; only use the FTYPE if
2100 (proclaimed-ftype name
)))) ; NAME was DEFKNOWN
2101 (unless (or (node-tail-p last
)
2102 (policy last
(zerop safety
))
2103 (and (fun-type-p ftype
)
2104 (eq *empty-type
* (fun-type-returns ftype
))))
2105 (vop nil-fun-returned-error last
2block
2107 (emit-constant name
)
2108 (multiple-value-bind (tn named
)
2109 (fun-lvar-tn last
2block fun
)
2112 ((not (eq (ir2-block-next 2block
) (block-info target
)))
2113 (vop branch last
2block
(block-label target
)))
2115 (register-drop-thru target
))))))
2119 ;;; Convert the code in a block into VOPs.
2120 (defun ir2-convert-block (block)
2121 (declare (type cblock block
))
2122 (let ((2block (block-info block
)))
2123 (do-nodes (node lvar block
)
2127 (let ((2lvar (lvar-info lvar
)))
2128 ;; function REF in a local call is not annotated
2129 (when (and 2lvar
(not (eq (ir2-lvar-kind 2lvar
) :delayed
)))
2130 (ir2-convert-ref node
2block
)))))
2132 (let ((kind (basic-combination-kind node
)))
2135 (ir2-convert-local-call node
2block
))
2137 (ir2-convert-full-call node
2block
))
2139 (let* ((info (basic-combination-fun-info node
))
2140 (fun (fun-info-ir2-convert info
))
2141 (hook (fun-info-ir2-hook info
)))
2143 (funcall hook node
2block
))
2145 (funcall fun node
2block
))
2146 ((eq (basic-combination-info node
) :full
)
2147 (ir2-convert-full-call node
2block
))
2149 (ir2-convert-template node
2block
))))))))
2151 (when (lvar-info (if-test node
))
2152 (ir2-convert-if node
2block
)))
2154 (let ((fun (bind-lambda node
)))
2155 (when (eq (lambda-home fun
) fun
)
2156 (ir2-convert-bind node
2block
))))
2158 (ir2-convert-return node
2block
))
2160 (ir2-convert-set node
2block
))
2162 (ir2-convert-cast node
2block
))
2165 ((eq (basic-combination-kind node
) :local
)
2166 (ir2-convert-mv-bind node
2block
))
2167 ((eq (lvar-fun-name (basic-combination-fun node
))
2169 (ir2-convert-throw node
2block
))
2171 (ir2-convert-mv-call node
2block
))))
2173 (when (exit-entry node
)
2174 (ir2-convert-exit node
2block
)))
2176 (ir2-convert-entry node
2block
)))))
2178 (finish-ir2-block block
)