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-convert
)
589 ((array bound index
) node 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
)))))))
599 (ir2-convert-template node block
))
601 ;;;; template conversion
603 ;;; Build a TN-REFS list that represents access to the values of the
604 ;;; specified list of lvars ARGS for TEMPLATE. Any :CONSTANT arguments
605 ;;; are returned in the second value as a list rather than being
606 ;;; accessed as a normal argument. NODE and BLOCK provide the context
607 ;;; for emitting any necessary type-checking code.
608 (defun reference-args (node block args template
)
609 (declare (type node node
) (type ir2-block block
) (list args
)
610 (type template template
))
611 (collect ((info-args))
614 (do ((args args
(cdr args
))
615 (types (template-arg-types template
) (cdr types
)))
617 (let ((type (first types
))
619 (if (and (consp type
) (eq (car type
) ':constant
))
620 (info-args (lvar-value arg
))
621 (let ((ref (reference-tn (lvar-tn node block arg
) nil
)))
623 (setf (tn-ref-across last
) ref
)
627 (values (the (or tn-ref null
) first
) (info-args)))))
629 ;;; Convert a conditional template. We try to exploit any
630 ;;; drop-through, but emit an unconditional branch afterward if we
631 ;;; fail. NOT-P is true if the sense of the TEMPLATE's test should be
633 (defun ir2-convert-conditional (node block template args info-args if not-p
)
634 (declare (type node node
) (type ir2-block block
)
635 (type template template
) (type (or tn-ref null
) args
)
636 (list info-args
) (type cif if
) (type boolean not-p
))
637 (let ((consequent (if-consequent if
))
638 (alternative (if-alternative if
))
639 (flags (and (consp (template-result-types template
))
640 (rest (template-result-types template
)))))
641 (aver (= (template-info-arg-count template
)
642 (+ (length info-args
)
645 (rotatef consequent alternative
)
647 (when (drop-thru-p if consequent
)
648 (rotatef consequent alternative
)
651 (emit-template node block template args nil
652 (list* (block-label consequent
) not-p
654 (if (drop-thru-p if alternative
)
655 (register-drop-thru alternative
)
656 (vop branch node block
(block-label alternative
))))
658 (emit-template node block template args nil info-args
)
659 (vop branch-if node block
(block-label consequent
) flags not-p
)
660 (if (drop-thru-p if alternative
)
661 (register-drop-thru alternative
)
662 (vop branch node block
(block-label alternative
)))))))
664 ;;; Convert an IF that isn't the DEST of a conditional template.
665 (defun ir2-convert-if (node block
)
666 (declare (type ir2-block block
) (type cif node
))
667 (let* ((test (if-test node
))
668 (test-ref (reference-tn (lvar-tn node block test
) nil
))
669 (nil-ref (reference-tn (emit-constant nil
) nil
)))
670 (setf (tn-ref-across test-ref
) nil-ref
)
671 (ir2-convert-conditional node block
(template-or-lose 'if-eq
)
672 test-ref
() node t
)))
674 ;;; Return a list of primitive-types that we can pass to LVAR-RESULT-TNS
675 ;;; describing the result types we want for a template call. We are really
676 ;;; only interested in the number of results required: in normal case
677 ;;; TEMPLATE-RESULTS-OK has already checked them.
678 (defun find-template-result-types (call rtypes
)
679 (let* ((type (node-derived-type call
))
681 (mapcar #'primitive-type
682 (if (args-type-p type
)
683 (append (args-type-required type
)
684 (args-type-optional type
))
686 (primitive-t *backend-t-primitive-type
*))
687 (loop for rtype in rtypes
688 for type
= (or (pop types
) primitive-t
)
691 ;;; Return a list of TNs usable in a CALL to TEMPLATE delivering values to
692 ;;; LVAR. As an efficiency hack, we pick off the common case where the LVAR is
693 ;;; fixed values and has locations that satisfy the result restrictions. This
694 ;;; can fail when there is a type check or a values count mismatch.
695 (defun make-template-result-tns (call lvar rtypes
)
696 (declare (type combination call
) (type (or lvar null
) lvar
)
698 (let ((2lvar (when lvar
(lvar-info lvar
))))
699 (if (and 2lvar
(eq (ir2-lvar-kind 2lvar
) :fixed
))
700 (let ((locs (ir2-lvar-locs 2lvar
)))
701 (if (and (= (length rtypes
) (length locs
))
702 (do ((loc locs
(cdr loc
))
703 (rtypes rtypes
(cdr rtypes
)))
705 (unless (operand-restriction-ok
707 (tn-primitive-type (car loc
))
713 (find-template-result-types call rtypes
))))
716 (find-template-result-types call rtypes
)))))
718 ;;; Get the operands into TNs, make TN-REFs for them, and then call
719 ;;; the template emit function.
720 (defun ir2-convert-template (call block
)
721 (declare (type combination call
) (type ir2-block block
))
722 (let* ((template (combination-info call
))
723 (lvar (node-lvar call
))
724 (rtypes (template-result-types template
)))
725 (multiple-value-bind (args info-args
)
726 (reference-args call block
(combination-args call
) template
)
727 (aver (not (template-more-results-type template
)))
728 (if (template-conditional-p template
)
729 (ir2-convert-conditional call block template args info-args
730 (lvar-dest lvar
) nil
)
731 (let* ((results (make-template-result-tns call lvar rtypes
))
732 (r-refs (reference-tn-list results t
)))
733 (aver (= (length info-args
)
734 (template-info-arg-count template
)))
735 (when (and lvar
(lvar-dynamic-extent lvar
))
736 (vop current-stack-pointer call block
737 (ir2-lvar-stack-pointer (lvar-info lvar
))))
738 (when (emit-step-p call
)
739 (vop sb
!vm
::step-instrument-before-vop call block
))
741 (emit-template call block template args r-refs info-args
)
742 (emit-template call block template args r-refs
))
743 (move-lvar-result call block results lvar
)))))
746 ;;; We don't have to do much because operand count checking is done by
747 ;;; IR1 conversion. The only difference between this and the function
748 ;;; case of IR2-CONVERT-TEMPLATE is that there can be codegen-info
750 (defoptimizer (%%primitive ir2-convert
) ((template info
&rest args
) call block
)
751 (declare (ignore args
))
752 (let* ((template (lvar-value template
))
753 (info (lvar-value info
))
754 (lvar (node-lvar call
))
755 (rtypes (template-result-types template
))
756 (results (make-template-result-tns call lvar rtypes
))
757 (r-refs (reference-tn-list results t
)))
758 (multiple-value-bind (args info-args
)
759 (reference-args call block
(cddr (combination-args call
)) template
)
760 (aver (not (template-more-results-type template
)))
761 (aver (not (template-conditional-p template
)))
762 (aver (null info-args
))
765 (emit-template call block template args r-refs info
)
766 (emit-template call block template args r-refs
))
768 (move-lvar-result call block results lvar
)))
771 (defoptimizer (%%primitive derive-type
) ((template info
&rest args
))
772 (declare (ignore info args
))
773 (let ((type (template-type (lvar-value template
))))
774 (if (fun-type-p type
)
775 (fun-type-returns type
)
780 ;;; Convert a LET by moving the argument values into the variables.
781 ;;; Since a LET doesn't have any passing locations, we move the
782 ;;; arguments directly into the variables. We must also allocate any
783 ;;; indirect value cells, since there is no function prologue to do
785 (defun ir2-convert-let (node block fun
)
786 (declare (type combination node
) (type ir2-block block
) (type clambda fun
))
787 (mapc (lambda (var arg
)
789 (let ((src (lvar-tn node block arg
))
790 (dest (leaf-info var
)))
791 (if (and (lambda-var-indirect var
)
792 (lambda-var-explicit-value-cell var
))
793 (emit-make-value-cell node block src dest
)
794 (emit-move node block src dest
)))))
795 (lambda-vars fun
) (basic-combination-args node
))
798 ;;; Emit any necessary moves into assignment temps for a local call to
799 ;;; FUN. We return two lists of TNs: TNs holding the actual argument
800 ;;; values, and (possibly EQ) TNs that are the actual destination of
801 ;;; the arguments. When necessary, we allocate temporaries for
802 ;;; arguments to preserve parallel assignment semantics. These lists
803 ;;; exclude unused arguments and include implicit environment
804 ;;; arguments, i.e. they exactly correspond to the arguments passed.
806 ;;; OLD-FP is the TN currently holding the value we want to pass as
807 ;;; OLD-FP. If null, then the call is to the same environment (an
808 ;;; :ASSIGNMENT), so we only move the arguments, and leave the
809 ;;; environment alone.
811 ;;; CLOSURE-FP is for calling a closure that has "implicit" value
812 ;;; cells (stored in the allocating stack frame), and is the frame
813 ;;; pointer TN to use for values allocated in the outbound stack
814 ;;; frame. This is distinct from OLD-FP for the specific case of a
816 (defun emit-psetq-moves (node block fun old-fp
&optional
(closure-fp old-fp
))
817 (declare (type combination node
) (type ir2-block block
) (type clambda fun
)
818 (type (or tn null
) old-fp closure-fp
))
819 (let ((actuals (mapcar (lambda (x)
821 (lvar-tn node block x
)))
822 (combination-args node
))))
825 (dolist (var (lambda-vars fun
))
826 (let ((actual (pop actuals
))
827 (loc (leaf-info var
)))
830 ((and (lambda-var-indirect var
)
831 (lambda-var-explicit-value-cell var
))
833 (make-normal-tn *backend-t-primitive-type
*)))
834 (emit-make-value-cell node block actual temp
)
836 ((member actual
(locs))
837 (let ((temp (make-normal-tn (tn-primitive-type loc
))))
838 (emit-move node block actual temp
)
845 (let ((this-1env (node-physenv node
))
846 (called-env (physenv-info (lambda-physenv fun
))))
847 (dolist (thing (ir2-physenv-closure called-env
))
848 (temps (closure-initial-value (car thing
) this-1env closure-fp
))
851 (locs (ir2-physenv-old-fp called-env
))))
853 (values (temps) (locs)))))
855 ;;; A tail-recursive local call is done by emitting moves of stuff
856 ;;; into the appropriate passing locations. After setting up the args
857 ;;; and environment, we just move our return-pc into the called
858 ;;; function's passing location.
859 (defun ir2-convert-tail-local-call (node block fun
)
860 (declare (type combination node
) (type ir2-block block
) (type clambda fun
))
861 (let ((this-env (physenv-info (node-physenv node
)))
862 (current-fp (make-stack-pointer-tn)))
863 (multiple-value-bind (temps locs
)
864 (emit-psetq-moves node block fun
865 (ir2-physenv-old-fp this-env
) current-fp
)
867 ;; If we're about to emit a move from CURRENT-FP then we need to
869 (when (find current-fp temps
)
870 (vop current-fp node block current-fp
))
872 (mapc (lambda (temp loc
)
873 (emit-move node block temp loc
))
876 (emit-move node block
877 (ir2-physenv-return-pc this-env
)
878 (ir2-physenv-return-pc-pass
880 (lambda-physenv fun
)))))
884 ;;; Convert an :ASSIGNMENT call. This is just like a tail local call,
885 ;;; except that the caller and callee environment are the same, so we
886 ;;; don't need to mess with the environment locations, return PC, etc.
887 (defun ir2-convert-assignment (node block fun
)
888 (declare (type combination node
) (type ir2-block block
) (type clambda fun
))
889 (multiple-value-bind (temps locs
) (emit-psetq-moves node block fun nil
)
891 (mapc (lambda (temp loc
)
892 (emit-move node block temp loc
))
896 ;;; Do stuff to set up the arguments to a non-tail local call
897 ;;; (including implicit environment args.) We allocate a frame
898 ;;; (returning the FP and NFP), and also compute the TN-REFS list for
899 ;;; the values to pass and the list of passing location TNs.
900 (defun ir2-convert-local-call-args (node block fun
)
901 (declare (type combination node
) (type ir2-block block
) (type clambda fun
))
902 (let ((fp (make-stack-pointer-tn))
903 (nfp (make-number-stack-pointer-tn))
904 (old-fp (make-stack-pointer-tn)))
905 (multiple-value-bind (temps locs
)
906 (emit-psetq-moves node block fun old-fp
)
907 (vop current-fp node block old-fp
)
908 (vop allocate-frame node block
909 (physenv-info (lambda-physenv fun
))
911 (values fp nfp temps
(mapcar #'make-alias-tn locs
)))))
913 ;;; Handle a non-TR known-values local call. We emit the call, then
914 ;;; move the results to the lvar's destination.
915 (defun ir2-convert-local-known-call (node block fun returns lvar start
)
916 (declare (type node node
) (type ir2-block block
) (type clambda fun
)
917 (type return-info returns
) (type (or lvar null
) lvar
)
919 (multiple-value-bind (fp nfp temps arg-locs
)
920 (ir2-convert-local-call-args node block fun
)
921 (let ((locs (return-info-locations returns
)))
922 (vop* known-call-local node block
923 (fp nfp
(reference-tn-list temps nil
))
924 ((reference-tn-list locs t
))
925 arg-locs
(physenv-info (lambda-physenv fun
)) start
)
926 (move-lvar-result node block locs lvar
)))
929 ;;; Handle a non-TR unknown-values local call. We do different things
930 ;;; depending on what kind of values the lvar wants.
932 ;;; If LVAR is :UNKNOWN, then we use the "multiple-" variant, directly
933 ;;; specifying the lvar's LOCS as the VOP results so that we don't
934 ;;; have to do anything after the call.
936 ;;; Otherwise, we use STANDARD-RESULT-TNS to get wired result TNs, and
937 ;;; then call MOVE-LVAR-RESULT to do any necessary type checks or
939 (defun ir2-convert-local-unknown-call (node block fun lvar start
)
940 (declare (type node node
) (type ir2-block block
) (type clambda fun
)
941 (type (or lvar null
) lvar
) (type label start
))
942 (multiple-value-bind (fp nfp temps arg-locs
)
943 (ir2-convert-local-call-args node block fun
)
944 (let ((2lvar (and lvar
(lvar-info lvar
)))
945 (env (physenv-info (lambda-physenv fun
)))
946 (temp-refs (reference-tn-list temps nil
)))
947 (if (and 2lvar
(eq (ir2-lvar-kind 2lvar
) :unknown
))
948 (vop* multiple-call-local node block
(fp nfp temp-refs
)
949 ((reference-tn-list (ir2-lvar-locs 2lvar
) t
))
951 (let ((locs (standard-result-tns lvar
)))
952 (vop* call-local node block
954 ((reference-tn-list locs t
))
955 arg-locs env start
(length locs
))
956 (move-lvar-result node block locs lvar
)))))
959 ;;; Dispatch to the appropriate function, depending on whether we have
960 ;;; a let, tail or normal call. If the function doesn't return, call
961 ;;; it using the unknown-value convention. We could compile it as a
962 ;;; tail call, but that might seem confusing in the debugger.
963 (defun ir2-convert-local-call (node block
)
964 (declare (type combination node
) (type ir2-block block
))
965 (let* ((fun (ref-leaf (lvar-uses (basic-combination-fun node
))))
966 (kind (functional-kind fun
)))
967 (cond ((eq kind
:let
)
968 (ir2-convert-let node block fun
))
969 ((eq kind
:assignment
)
970 (ir2-convert-assignment node block fun
))
972 (ir2-convert-tail-local-call node block fun
))
974 (let ((start (block-trampoline (lambda-block fun
)))
975 (returns (tail-set-info (lambda-tail-set fun
)))
976 (lvar (node-lvar node
)))
978 (return-info-kind returns
)
981 (ir2-convert-local-unknown-call node block fun lvar start
))
983 (ir2-convert-local-known-call node block fun returns
989 ;;; Given a function lvar FUN, return (VALUES TN-TO-CALL NAMED-P),
990 ;;; where TN-TO-CALL is a TN holding the thing that we call NAMED-P is
991 ;;; true if the thing is named (false if it is a function).
993 ;;; There are two interesting non-named cases:
994 ;;; -- We know it's a function. No check needed: return the
996 ;;; -- We don't know what it is.
997 (defun fun-lvar-tn (node block lvar
)
998 (declare (ignore node block
))
999 (declare (type lvar lvar
))
1000 (let ((2lvar (lvar-info lvar
)))
1001 (if (eq (ir2-lvar-kind 2lvar
) :delayed
)
1002 (let ((name (lvar-fun-name lvar t
)))
1004 (values (make-load-time-constant-tn :fdefinition name
) t
))
1005 (let* ((locs (ir2-lvar-locs 2lvar
))
1007 (function-ptype (primitive-type-or-lose 'function
)))
1008 (aver (and (eq (ir2-lvar-kind 2lvar
) :fixed
)
1009 (= (length locs
) 1)))
1010 (aver (eq (tn-primitive-type loc
) function-ptype
))
1011 (values loc nil
)))))
1013 ;;; Set up the args to NODE in the current frame, and return a TN-REF
1014 ;;; list for the passing locations.
1015 (defun move-tail-full-call-args (node block
)
1016 (declare (type combination node
) (type ir2-block block
))
1017 (let ((args (basic-combination-args node
))
1020 (dotimes (num (length args
))
1021 (let ((loc (standard-arg-location num
)))
1022 (emit-move node block
(lvar-tn node block
(elt args num
)) loc
)
1023 (let ((ref (reference-tn loc nil
)))
1025 (setf (tn-ref-across last
) ref
)
1030 ;;; Move the arguments into the passing locations and do a (possibly
1031 ;;; named) tail call.
1032 (defun ir2-convert-tail-full-call (node block
)
1033 (declare (type combination node
) (type ir2-block block
))
1034 (let* ((env (physenv-info (node-physenv node
)))
1035 (args (basic-combination-args node
))
1036 (nargs (length args
))
1037 (pass-refs (move-tail-full-call-args node block
))
1038 (old-fp (ir2-physenv-old-fp env
))
1039 (return-pc (ir2-physenv-return-pc env
)))
1041 (multiple-value-bind (fun-tn named
)
1042 (fun-lvar-tn node block
(basic-combination-fun node
))
1044 (vop* tail-call-named node block
1045 (fun-tn old-fp return-pc pass-refs
)
1049 (vop* tail-call node block
1050 (fun-tn old-fp return-pc pass-refs
)
1053 (emit-step-p node
)))))
1057 ;;; like IR2-CONVERT-LOCAL-CALL-ARGS, only different
1058 (defun ir2-convert-full-call-args (node block
)
1059 (declare (type combination node
) (type ir2-block block
))
1060 (let* ((args (basic-combination-args node
))
1061 (fp (make-stack-pointer-tn))
1062 (nargs (length args
)))
1063 (vop allocate-full-call-frame node block nargs fp
)
1067 (dotimes (num nargs
)
1068 (locs (standard-arg-location num
))
1069 (let ((ref (reference-tn (lvar-tn node block
(elt args num
))
1072 (setf (tn-ref-across last
) ref
)
1076 (values fp first
(locs) nargs
)))))
1078 ;;; Do full call when a fixed number of values are desired. We make
1079 ;;; STANDARD-RESULT-TNS for our lvar, then deliver the result using
1080 ;;; MOVE-LVAR-RESULT. We do named or normal call, as appropriate.
1081 (defun ir2-convert-fixed-full-call (node block
)
1082 (declare (type combination node
) (type ir2-block block
))
1083 (multiple-value-bind (fp args arg-locs nargs
)
1084 (ir2-convert-full-call-args node block
)
1085 (let* ((lvar (node-lvar node
))
1086 (locs (standard-result-tns lvar
))
1087 (loc-refs (reference-tn-list locs t
))
1088 (nvals (length locs
)))
1089 (multiple-value-bind (fun-tn named
)
1090 (fun-lvar-tn node block
(basic-combination-fun node
))
1092 (vop* call-named node block
(fp fun-tn args
) (loc-refs)
1093 arg-locs nargs nvals
(emit-step-p node
))
1094 (vop* call node block
(fp fun-tn args
) (loc-refs)
1095 arg-locs nargs nvals
(emit-step-p node
)))
1096 (move-lvar-result node block locs lvar
))))
1099 ;;; Do full call when unknown values are desired.
1100 (defun ir2-convert-multiple-full-call (node block
)
1101 (declare (type combination node
) (type ir2-block block
))
1102 (multiple-value-bind (fp args arg-locs nargs
)
1103 (ir2-convert-full-call-args node block
)
1104 (let* ((lvar (node-lvar node
))
1105 (locs (ir2-lvar-locs (lvar-info lvar
)))
1106 (loc-refs (reference-tn-list locs t
)))
1107 (multiple-value-bind (fun-tn named
)
1108 (fun-lvar-tn node block
(basic-combination-fun node
))
1110 (vop* multiple-call-named node block
(fp fun-tn args
) (loc-refs)
1111 arg-locs nargs
(emit-step-p node
))
1112 (vop* multiple-call node block
(fp fun-tn args
) (loc-refs)
1113 arg-locs nargs
(emit-step-p node
))))))
1116 ;;; stuff to check in PONDER-FULL-CALL
1118 ;;; These came in handy when troubleshooting cold boot after making
1119 ;;; major changes in the package structure: various transforms and
1120 ;;; VOPs and stuff got attached to the wrong symbol, so that
1121 ;;; references to the right symbol were bogusly translated as full
1122 ;;; calls instead of primitives, sending the system off into infinite
1123 ;;; space. Having a report on all full calls generated makes it easier
1124 ;;; to figure out what form caused the problem this time.
1125 (declaim (type (member :minimal
:detailed
:very-detailed
:maximal
)
1126 *track-full-called-fnames
*))
1127 (defvar *track-full-called-fnames
* :minimal
)
1129 ;;; Do some checks (and store some notes relevant for future checks)
1131 ;;; * Is this a full call to something we have reason to know should
1132 ;;; never be full called? (Except as of sbcl-0.7.18 or so, we no
1133 ;;; longer try to ensure this behavior when *FAILURE-P* has already
1135 (defun ponder-full-call (node)
1136 (let* ((lvar (basic-combination-fun node
))
1137 (fname (lvar-fun-name lvar t
)))
1138 (declare (type (or symbol cons
) fname
))
1140 ;; Warn about cross-compiling certain full-calls,
1141 ;; as it is indicative of dependency order problems.
1143 (let ((compname (component-name (node-component node
))))
1144 ;; Don't care too much about macro performance.
1145 (unless (and (stringp compname
) (string/= compname
"DEFMACRO"))
1146 ;; Catch FOO and (SETF FOO) both.
1147 (let ((stem (if (atom fname
) fname
(second fname
))))
1149 sb-cold
::*full-calls-to-warn-about
*
1151 (warn "Full call to ~S" fname
)))))
1153 (let* ((inlineable-p (not (let ((*lexenv
* (node-lexenv node
)))
1154 (fun-lexically-notinline-p fname
))))
1155 (inlineable-bit (if inlineable-p
1 0))
1156 (cell (info :function
:emitted-full-calls fname
)))
1158 ;; The low bit indicates whether any not-NOTINLINE call was seen.
1159 ;; The next-lowest bit is magic. Refer to %COMPILER-DEFMACRO
1160 ;; and WARN-IF-INLINE-FAILED/CALL for the pertinent logic.
1161 (setf cell
(list (logior 4 inlineable-bit
))
1162 (info :function
:emitted-full-calls fname
) cell
)
1163 (incf (car cell
) (+ 4 (if (oddp (car cell
)) 0 inlineable-bit
))))
1164 ;; If the full call was wanted, don't record anything.
1165 ;; (This was originally for debugging SBCL self-compilation)
1168 (warn-if-inline-failed/call fname
(node-lexenv node
) cell
))
1169 (case *track-full-called-fnames
*
1171 (when (boundp 'sb
!xc
:*compile-file-pathname
*)
1172 (pushnew sb
!xc
:*compile-file-pathname
* (cdr cell
)
1175 (pushnew (component-name *component-being-compiled
*)
1176 (cdr cell
) :test
#'equalp
)))))
1178 ;; Special mode, usually only for the cross-compiler
1179 ;; and only with the feature enabled.
1180 #!+sb-show
(when (eq *track-full-called-fnames
* :maximal
)
1181 (/show
"converting full call to named function" fname
)
1182 (/show
(basic-combination-args node
))
1183 (/show
(policy node speed
) (policy node safety
))
1184 (/show
(policy node compilation-speed
))
1185 (let ((arg-types (mapcar (lambda (lvar)
1189 (basic-combination-args node
))))
1192 ;; When illegal code is compiled, all sorts of perverse paths
1193 ;; through the compiler can be taken, and it's much harder -- and
1194 ;; probably pointless -- to guarantee that always-optimized-away
1195 ;; functions are actually optimized away. Thus, we skip the check
1198 ;; check to see if we know anything about the function
1199 (let ((info (info :function
:info fname
)))
1200 ;; if we know something, check to see if the full call was valid
1201 (when (and info
(ir1-attributep (fun-info-attributes info
)
1202 always-translatable
))
1203 (/show
(policy node speed
) (policy node safety
))
1204 (/show
(policy node compilation-speed
))
1205 (bug "full call to ~S" fname
))))
1208 (aver (legal-fun-name-p fname
))))) ;; FIXME: needless check?
1210 ;;; If the call is in a tail recursive position and the return
1211 ;;; convention is standard, then do a tail full call. If one or fewer
1212 ;;; values are desired, then use a single-value call, otherwise use a
1213 ;;; multiple-values call.
1214 (defun ir2-convert-full-call (node block
)
1215 (declare (type combination node
) (type ir2-block block
))
1216 (ponder-full-call node
)
1217 (cond ((node-tail-p node
)
1218 (ir2-convert-tail-full-call node block
))
1219 ((let ((lvar (node-lvar node
)))
1221 (eq (ir2-lvar-kind (lvar-info lvar
)) :unknown
)))
1222 (ir2-convert-multiple-full-call node block
))
1224 (ir2-convert-fixed-full-call node block
)))
1227 ;;;; entering functions
1229 #!+precise-arg-count-error
1230 (defun xep-verify-arg-count (node block fun arg-count-location
)
1231 (when (policy fun
(plusp verify-arg-count
))
1232 (let* ((ef (functional-entry-fun fun
))
1233 (optional (optional-dispatch-p ef
))
1235 (optional-dispatch-min-args ef
)))
1236 (max (cond ((not optional
)
1237 (1- (length (lambda-vars fun
))))
1239 (not (optional-dispatch-more-entry ef
)))
1240 (optional-dispatch-max-args ef
)))))
1241 (unless (and (eql min
0) (not max
))
1242 (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 (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
))
1266 (cond ((and (optional-dispatch-p ef
) (optional-dispatch-more-entry ef
))
1267 ;; COPY-MORE-ARG should handle SP adjustemnt, but it
1268 ;; isn't done on all targets.
1269 #!-precise-arg-count-error
1270 (vop xep-setup-sp node block
)
1271 (vop copy-more-arg node block
(optional-dispatch-max-args ef
)))
1273 (vop xep-setup-sp node block
)))
1274 (when (ir2-physenv-closure env
)
1275 (let ((closure (make-normal-tn *backend-t-primitive-type
*)))
1276 (when (policy fun
(> store-closure-debug-pointer
1))
1277 ;; Save the closure pointer on the stack.
1278 (let ((closure-save (make-representation-tn
1279 *backend-t-primitive-type
*
1280 (sc-number-or-lose 'sb
!vm
::control-stack
))))
1281 (vop setup-closure-environment node block start-label
1283 (setf (ir2-physenv-closure-save-tn env
) closure-save
)
1284 (component-live-tn closure-save
)))
1285 (vop setup-closure-environment node block start-label closure
)
1287 (dolist (loc (ir2-physenv-closure env
))
1288 (vop closure-ref node block closure
(incf n
) (cdr loc
)))))))
1289 (unless (eq (functional-kind fun
) :toplevel
)
1290 (let ((vars (lambda-vars fun
))
1292 (when (leaf-refs (first vars
))
1293 (emit-move node block arg-count-tn
(leaf-info (first vars
))))
1294 (dolist (arg (rest vars
))
1295 (when (leaf-refs arg
)
1296 (let ((pass (standard-arg-location n
))
1297 (home (leaf-info arg
)))
1298 (if (and (lambda-var-indirect arg
)
1299 (lambda-var-explicit-value-cell arg
))
1300 (emit-make-value-cell node block pass home
)
1301 (emit-move node block pass home
))))
1304 (emit-move node block
(make-old-fp-passing-location t
)
1305 (ir2-physenv-old-fp env
)))
1309 ;;; Emit function prolog code. This is only called on bind nodes for
1310 ;;; functions that allocate environments. All semantics of let calls
1311 ;;; are handled by IR2-CONVERT-LET.
1313 ;;; If not an XEP, all we do is move the return PC from its passing
1314 ;;; location, since in a local call, the caller allocates the frame
1315 ;;; and sets up the arguments.
1317 #!+unwind-to-frame-and-call-vop
1318 (defun save-bsp (node block env
)
1319 ;; Save BSP on stack so that the binding environment can be restored
1320 ;; when restarting frames.
1321 ;; This is done inside functions, which leaves XEPs without saved
1322 ;; BSP, though the code in XEPs doesn't bind any variables, it can
1323 ;; call arbitrary code through the SATISFIES declaration.
1324 ;; And functions called by SATISFIES are not inlined, except for
1325 ;; source transforms, but these usually do not bind anything.
1326 ;; Thus when restarting it needs to check that the interrupt was in
1329 ;; It could be saved from the XEP, but some functions have both
1330 ;; external and internal entry points, so it will be saved twice.
1331 (let ((temp (make-normal-tn *backend-t-primitive-type
*))
1332 (bsp-save-tn (make-representation-tn
1333 *backend-t-primitive-type
*
1334 (sc-number-or-lose 'sb
!vm
::control-stack
))))
1335 (vop current-binding-pointer node block temp
)
1336 (emit-move node block temp bsp-save-tn
)
1337 (setf (ir2-physenv-bsp-save-tn env
) bsp-save-tn
)
1338 (component-live-tn bsp-save-tn
)))
1340 (defun ir2-convert-bind (node block
)
1341 (declare (type bind node
) (type ir2-block block
))
1342 (let* ((fun (bind-lambda node
))
1343 (env (physenv-info (lambda-physenv fun
))))
1344 (aver (member (functional-kind fun
)
1345 '(nil :external
:optional
:toplevel
:cleanup
)))
1348 (init-xep-environment node block fun
)
1350 (when *collect-dynamic-statistics
*
1351 (vop count-me node block
*dynamic-counts-tn
*
1352 (block-number (ir2-block-block block
)))))
1353 ((policy fun
(> store-closure-debug-pointer
1))
1354 ;; Propagate the location of the closure pointer from the
1355 ;; enclosing functions. (FIXME: Should make sure that this
1356 ;; handles closures inside closures correctly). [remark by JES]
1357 (let* ((entry-fun (lambda-entry-fun fun
)))
1359 (let ((2env (physenv-info (lambda-physenv fun
)))
1360 (entry-2env (physenv-info (lambda-physenv entry-fun
))))
1361 (setf (ir2-physenv-closure-save-tn 2env
)
1362 (ir2-physenv-closure-save-tn entry-2env
)))))))
1366 (ir2-physenv-return-pc-pass env
)
1367 (ir2-physenv-return-pc env
))
1368 #!+unwind-to-frame-and-call-vop
1369 (when (and (lambda-allow-instrumenting fun
)
1370 (not (lambda-inline-expanded fun
))
1371 (policy fun
(>= insert-debug-catch
1)))
1372 (save-bsp node block env
))
1374 (let ((lab (gen-label)))
1375 (setf (ir2-physenv-environment-start env
) lab
)
1376 (vop note-environment-start node block lab
)
1378 (unless (policy fun
(>= inhibit-safepoints
2))
1379 (vop sb
!vm
::insert-safepoint node block
))))
1383 ;;;; function return
1385 ;;; Do stuff to return from a function with the specified values and
1386 ;;; convention. If the return convention is :FIXED and we aren't
1387 ;;; returning from an XEP, then we do a known return (letting
1388 ;;; representation selection insert the correct move-arg VOPs.)
1389 ;;; Otherwise, we use the unknown-values convention. If there is a
1390 ;;; fixed number of return values, then use RETURN, otherwise use
1391 ;;; RETURN-MULTIPLE.
1392 (defun ir2-convert-return (node block
)
1393 (declare (type creturn node
) (type ir2-block block
))
1394 (let* ((lvar (return-result node
))
1395 (2lvar (lvar-info lvar
))
1396 (lvar-kind (ir2-lvar-kind 2lvar
))
1397 (fun (return-lambda node
))
1398 (env (physenv-info (lambda-physenv fun
)))
1399 (old-fp (ir2-physenv-old-fp env
))
1400 (return-pc (ir2-physenv-return-pc env
))
1401 (returns (tail-set-info (lambda-tail-set fun
))))
1403 ((and (eq (return-info-kind returns
) :fixed
)
1405 (let ((locs (lvar-tns node block lvar
1406 (return-info-types returns
))))
1407 (vop* known-return node block
1408 (old-fp return-pc
(reference-tn-list locs nil
))
1410 (return-info-locations returns
))))
1411 ((eq lvar-kind
:fixed
)
1412 (let* ((types (mapcar #'tn-primitive-type
(ir2-lvar-locs 2lvar
)))
1413 (lvar-locs (lvar-tns node block lvar types
))
1414 (nvals (length lvar-locs
))
1415 (locs (make-standard-value-tns nvals
)))
1416 (mapc (lambda (val loc
)
1417 (emit-move node block val loc
))
1421 (vop return-single node block old-fp return-pc
(car locs
))
1422 (vop* return node block
1423 (old-fp return-pc
(reference-tn-list locs nil
))
1427 (aver (eq lvar-kind
:unknown
))
1428 (vop* return-multiple node block
1430 (reference-tn-list (ir2-lvar-locs 2lvar
) nil
))
1437 ;;;; These are used by the debugger to find the top function on the
1438 ;;;; stack. They return the OLD-FP and RETURN-PC for the current
1439 ;;;; function as multiple values.
1441 (defoptimizer (%caller-frame ir2-convert
) (() node block
)
1442 (let ((ir2-physenv (physenv-info (node-physenv node
))))
1443 (move-lvar-result node block
1444 (list (ir2-physenv-old-fp ir2-physenv
))
1447 (defoptimizer (%caller-pc ir2-convert
) (() node block
)
1448 (let ((ir2-physenv (physenv-info (node-physenv node
))))
1449 (move-lvar-result node block
1450 (list (ir2-physenv-return-pc ir2-physenv
))
1453 ;;;; multiple values
1455 ;;; This is almost identical to IR2-CONVERT-LET. Since LTN annotates
1456 ;;; the lvar for the correct number of values (with the lvar user
1457 ;;; responsible for defaulting), we can just pick them up from the
1459 (defun ir2-convert-mv-bind (node block
)
1460 (declare (type mv-combination node
) (type ir2-block block
))
1461 (let* ((lvar (first (basic-combination-args node
)))
1462 (fun (ref-leaf (lvar-uses (basic-combination-fun node
))))
1463 (vars (lambda-vars fun
)))
1464 (aver (eq (functional-kind fun
) :mv-let
))
1465 (mapc (lambda (src var
)
1466 (when (leaf-refs var
)
1467 (let ((dest (leaf-info var
)))
1468 (if (and (lambda-var-indirect var
)
1469 (lambda-var-explicit-value-cell var
))
1470 (emit-make-value-cell node block src dest
)
1471 (emit-move node block src dest
)))))
1472 (lvar-tns node block lvar
1474 (primitive-type (leaf-type x
)))
1479 ;;; Emit the appropriate fixed value, unknown value or tail variant of
1480 ;;; CALL-VARIABLE. Note that we only need to pass the values start for
1481 ;;; the first argument: all the other argument lvar TNs are
1482 ;;; ignored. This is because we require all of the values globs to be
1483 ;;; contiguous and on stack top.
1484 (defun ir2-convert-mv-call (node block
)
1485 (declare (type mv-combination node
) (type ir2-block block
))
1486 (aver (basic-combination-args node
))
1487 (let* ((start-lvar (lvar-info (first (basic-combination-args node
))))
1488 (start (first (ir2-lvar-locs start-lvar
)))
1489 (tails (and (node-tail-p node
)
1490 (lambda-tail-set (node-home-lambda node
))))
1491 (lvar (node-lvar node
))
1492 (2lvar (and lvar
(lvar-info lvar
))))
1493 (multiple-value-bind (fun named
)
1494 (fun-lvar-tn node block
(basic-combination-fun node
))
1495 (aver (and (not named
)
1496 (eq (ir2-lvar-kind start-lvar
) :unknown
)))
1499 (let ((env (physenv-info (node-physenv node
))))
1500 (vop tail-call-variable node block start fun
1501 (ir2-physenv-old-fp env
)
1502 (ir2-physenv-return-pc env
))))
1504 (eq (ir2-lvar-kind 2lvar
) :unknown
))
1505 (vop* multiple-call-variable node block
(start fun nil
)
1506 ((reference-tn-list (ir2-lvar-locs 2lvar
) t
))
1507 (emit-step-p node
)))
1509 (let ((locs (standard-result-tns lvar
)))
1510 (vop* call-variable node block
(start fun nil
)
1511 ((reference-tn-list locs t
)) (length locs
)
1513 (move-lvar-result node block locs lvar
)))))))
1515 ;;; Reset the stack pointer to the start of the specified
1516 ;;; unknown-values lvar (discarding it and all values globs on top of
1518 (defoptimizer (%pop-values ir2-convert
) ((%lvar
) node block
)
1519 (let* ((lvar (lvar-value %lvar
))
1520 (2lvar (lvar-info lvar
)))
1521 (cond ((eq (ir2-lvar-kind 2lvar
) :unknown
)
1522 (vop reset-stack-pointer node block
1523 (first (ir2-lvar-locs 2lvar
))))
1524 ((lvar-dynamic-extent lvar
)
1525 (vop reset-stack-pointer node block
1526 (ir2-lvar-stack-pointer 2lvar
)))
1527 (t (bug "Trying to pop a not stack-allocated LVAR ~S."
1530 (defoptimizer (%nip-values ir2-convert
) ((last-nipped last-preserved
1533 (let* ( ;; pointer immediately after the nipped block
1534 (after (lvar-value last-nipped
))
1535 (2after (lvar-info after
))
1536 ;; pointer to the first nipped word
1537 (first (lvar-value last-preserved
))
1538 (2first (lvar-info first
))
1540 (moved-tns (loop for lvar-ref in moved
1541 for lvar
= (lvar-value lvar-ref
)
1542 for
2lvar
= (lvar-info lvar
)
1544 collect
(first (ir2-lvar-locs 2lvar
)))))
1545 (aver (or (eq (ir2-lvar-kind 2after
) :unknown
)
1546 (lvar-dynamic-extent after
)))
1547 (aver (eq (ir2-lvar-kind 2first
) :unknown
))
1548 (when *check-consistency
*
1549 ;; we cannot move stack-allocated DX objects
1550 (dolist (moved-lvar moved
)
1551 (aver (eq (ir2-lvar-kind (lvar-info (lvar-value moved-lvar
)))
1553 (flet ((nip-aligned (nipped)
1554 (vop* %%nip-values node block
1556 (first (ir2-lvar-locs 2first
))
1557 (reference-tn-list moved-tns nil
))
1558 ((reference-tn-list moved-tns t
)))))
1559 (cond ((eq (ir2-lvar-kind 2after
) :unknown
)
1560 (nip-aligned (first (ir2-lvar-locs 2after
))))
1561 ((lvar-dynamic-extent after
)
1562 (nip-aligned (ir2-lvar-stack-pointer 2after
)))
1564 (bug "Trying to nip a not stack-allocated LVAR ~S." after
))))))
1566 (defoptimizer (%dummy-dx-alloc ir2-convert
) ((target source
) node block
)
1567 (let* ((target-lvar (lvar-value target
))
1568 (source-lvar (lvar-value source
))
1569 (target-2lvar (lvar-info target-lvar
))
1570 (source-2lvar (and source-lvar
(lvar-info source-lvar
))))
1571 (aver (lvar-dynamic-extent target-lvar
))
1572 (cond ((not source-lvar
)
1573 (vop current-stack-pointer node block
1574 (ir2-lvar-stack-pointer target-2lvar
)))
1575 ((lvar-dynamic-extent source-lvar
)
1576 (emit-move node block
1577 (ir2-lvar-stack-pointer source-2lvar
)
1578 (ir2-lvar-stack-pointer target-2lvar
)))
1579 ((eq (ir2-lvar-kind source-2lvar
) :unknown
)
1580 (emit-move node block
1581 (first (ir2-lvar-locs source-2lvar
))
1582 (ir2-lvar-stack-pointer target-2lvar
)))
1583 (t (bug "Trying to dummy up DX allocation from a ~
1584 not stack-allocated LVAR ~S." source-lvar
)))))
1586 ;;; Deliver the values TNs to LVAR using MOVE-LVAR-RESULT.
1587 (defoptimizer (values ir2-convert
) ((&rest values
) node block
)
1588 (let ((tns (mapcar (lambda (x)
1589 (lvar-tn node block x
))
1591 (move-lvar-result node block tns
(node-lvar node
))))
1593 ;;; In the normal case where unknown values are desired, we use the
1594 ;;; VALUES-LIST VOP. In the relatively unimportant case of VALUES-LIST
1595 ;;; for a fixed number of values, we punt by doing a full call to the
1596 ;;; VALUES-LIST function. This gets the full call VOP to deal with
1597 ;;; defaulting any unsupplied values. It seems unworthwhile to
1598 ;;; optimize this case.
1599 (defoptimizer (values-list ir2-convert
) ((list) node block
)
1600 (let* ((lvar (node-lvar node
))
1601 (2lvar (and lvar
(lvar-info lvar
))))
1603 (eq (ir2-lvar-kind 2lvar
) :unknown
))
1604 (let ((locs (ir2-lvar-locs 2lvar
)))
1605 (vop* values-list node block
1606 ((lvar-tn node block list
) nil
)
1607 ((reference-tn-list locs t
)))))
1608 (t (aver (or (not 2lvar
) ; i.e. we want to check the argument
1609 (eq (ir2-lvar-kind 2lvar
) :fixed
)))
1610 (ir2-convert-full-call node block
)))))
1612 (defoptimizer (%more-arg-values ir2-convert
) ((context start count
) node block
)
1613 (binding* ((lvar (node-lvar node
) :exit-if-null
)
1614 (2lvar (lvar-info lvar
)))
1615 (ecase (ir2-lvar-kind 2lvar
)
1617 ;; KLUDGE: this is very much unsafe, and can leak random stack values.
1618 ;; OTOH, I think the :FIXED case can only happen with (safety 0) in the
1621 (loop for loc in
(ir2-lvar-locs 2lvar
)
1623 do
(vop sb
!vm
::more-arg node block
1624 (lvar-tn node block context
)
1628 (let ((locs (ir2-lvar-locs 2lvar
)))
1629 (vop* %more-arg-values node block
1630 ((lvar-tn node block context
)
1631 (lvar-tn node block start
)
1632 (lvar-tn node block count
)
1634 ((reference-tn-list locs t
))))))))
1636 ;;;; special binding
1638 ;;; This is trivial, given our assumption of a shallow-binding
1640 (defoptimizer (%special-bind ir2-convert
) ((var value
) node block
)
1641 (let ((name (leaf-source-name (lvar-value var
))))
1642 #!-
(and sb-thread x86-64
)
1643 (vop bind node block
(lvar-tn node block value
) (emit-constant name
))
1644 #!+(and sb-thread x86-64
)
1646 ;; GC must understand that the symbol is implicitly live even though
1647 ;; binding makes no references to the object.
1648 (emit-constant name
)
1649 (vop sb
!vm
::bind
/let node block
(lvar-tn node block value
) name
))))
1651 (defoptimizer (%special-unbind ir2-convert
) ((var) node block
)
1652 (declare (ignore var
))
1653 (vop unbind node block
))
1655 ;;; ### It's not clear that this really belongs in this file, or
1656 ;;; should really be done this way, but this is the least violation of
1657 ;;; abstraction in the current setup. We don't want to wire
1658 ;;; shallow-binding assumptions into IR1tran.
1659 (def-ir1-translator progv
1660 ((vars vals
&body body
) start next result
)
1663 (with-unique-names (bind unbind
)
1664 (once-only ((n-save-bs '(%primitive current-binding-pointer
)))
1667 (labels ((,unbind
(vars)
1668 (declare (optimize (speed 2) (debug 0)))
1669 (let ((unbound-marker (%primitive make-unbound-marker
)))
1671 ;; CLHS says "bound and then made to have no value" -- user
1672 ;; should not be able to tell the difference between that and this.
1673 (about-to-modify-symbol-value var
'progv
)
1674 (%primitive bind unbound-marker var
))))
1676 (declare (optimize (speed 2) (debug 0)
1677 (insert-debug-catch 0)))
1679 ((null vals
) (,unbind vars
))
1681 (let ((val (car vals
))
1683 (about-to-modify-symbol-value var
'progv val t
)
1684 (%primitive bind val var
))
1685 (,bind
(cdr vars
) (cdr vals
))))))
1686 (,bind
,vars
,vals
))
1689 ;; Technically ANSI CL doesn't allow declarations at the
1690 ;; start of the cleanup form. SBCL happens to allow for
1691 ;; them, due to the way the UNWIND-PROTECT ir1 translation
1692 ;; is implemented; the cleanup forms are directly spliced
1693 ;; into an FLET definition body. And a declaration here
1694 ;; actually has exactly the right scope for what we need
1695 ;; (ensure that debug instrumentation is not emitted for the
1696 ;; cleanup function). -- JES, 2007-06-16
1697 (declare (optimize (insert-debug-catch 0)))
1698 (%primitive unbind-to-here
,n-save-bs
))))))
1702 ;;; Convert a non-local lexical exit. First find the NLX-INFO in our
1703 ;;; environment. Note that this is never called on the escape exits
1704 ;;; for CATCH and UNWIND-PROTECT, since the escape functions aren't
1706 (defun ir2-convert-exit (node block
)
1707 (declare (type exit node
) (type ir2-block block
))
1708 (let* ((nlx (exit-nlx-info node
))
1709 (loc (find-in-physenv nlx
(node-physenv node
)))
1710 (temp (make-stack-pointer-tn))
1711 (value (exit-value node
)))
1712 (if (nlx-info-safe-p nlx
)
1713 (vop value-cell-ref node block loc temp
)
1714 (emit-move node block loc temp
))
1716 (let ((locs (ir2-lvar-locs (lvar-info value
))))
1717 (vop unwind node block temp
(first locs
) (second locs
)))
1718 (let ((0-tn (emit-constant 0)))
1719 (vop unwind node block temp
0-tn
0-tn
))))
1723 ;;; %CLEANUP-POINT doesn't do anything except prevent the body from
1724 ;;; being entirely deleted.
1725 (defoptimizer (%cleanup-point ir2-convert
) (() node block
) node block
)
1727 ;;; This function invalidates a lexical exit on exiting from the
1728 ;;; dynamic extent. This is done by storing 0 into the indirect value
1729 ;;; cell that holds the closed unwind block.
1730 (defoptimizer (%lexical-exit-breakup ir2-convert
) ((info) node block
)
1731 (let ((nlx (lvar-value info
)))
1732 (when (nlx-info-safe-p nlx
)
1733 (vop value-cell-set node block
1734 (find-in-physenv nlx
(node-physenv node
))
1735 (emit-constant 0)))))
1737 ;;; We have to do a spurious move of no values to the result lvar so
1738 ;;; that lifetime analysis won't get confused.
1739 (defun ir2-convert-throw (node block
)
1740 (declare (type mv-combination node
) (type ir2-block block
))
1741 (let ((args (basic-combination-args node
)))
1742 (check-catch-tag-type (first args
))
1743 (vop* throw node block
1744 ((lvar-tn node block
(first args
))
1746 (ir2-lvar-locs (lvar-info (second args
)))
1749 (move-lvar-result node block
() (node-lvar node
))
1752 ;;; Emit code to set up a non-local exit. INFO is the NLX-INFO for the
1753 ;;; exit, and TAG is the lvar for the catch tag (if any.) We get at
1754 ;;; the target PC by passing in the label to the vop. The vop is
1755 ;;; responsible for building a return-PC object.
1756 (defun emit-nlx-start (node block info tag
)
1757 (declare (type node node
) (type ir2-block block
) (type nlx-info info
)
1758 (type (or lvar null
) tag
))
1759 (let* ((2info (nlx-info-info info
))
1760 (kind (cleanup-kind (nlx-info-cleanup info
)))
1761 (block-tn (physenv-live-tn
1763 (primitive-type-or-lose
1767 ((:unwind-protect
:block
:tagbody
)
1769 (node-physenv node
)))
1770 (res (make-stack-pointer-tn))
1771 (target-label (ir2-nlx-info-target 2info
)))
1773 (vop current-binding-pointer node block
1774 (car (ir2-nlx-info-dynamic-state 2info
)))
1775 (vop* save-dynamic-state node block
1777 ((reference-tn-list (cdr (ir2-nlx-info-dynamic-state 2info
)) t
)))
1778 (vop current-stack-pointer node block
(ir2-nlx-info-save-sp 2info
))
1782 (vop make-catch-block node block block-tn
1783 (lvar-tn node block tag
) target-label res
))
1784 ((:unwind-protect
:block
:tagbody
)
1785 (vop make-unwind-block node block block-tn target-label res
)))
1789 (if (nlx-info-safe-p info
)
1790 (emit-make-value-cell node block res
(ir2-nlx-info-home 2info
))
1791 (emit-move node block res
(ir2-nlx-info-home 2info
))))
1793 (vop set-unwind-protect node block block-tn
))
1798 ;;; Scan each of ENTRY's exits, setting up the exit for each lexical exit.
1799 (defun ir2-convert-entry (node block
)
1800 (declare (type entry node
) (type ir2-block block
))
1802 (dolist (exit (entry-exits node
))
1803 (let ((info (exit-nlx-info exit
)))
1805 (not (memq info nlxes
))
1806 (member (cleanup-kind (nlx-info-cleanup info
))
1807 '(:block
:tagbody
)))
1809 (emit-nlx-start node block info nil
)))))
1812 ;;; Set up the unwind block for these guys.
1813 (defoptimizer (%catch ir2-convert
) ((info-lvar tag
) node block
)
1814 (check-catch-tag-type tag
)
1815 (emit-nlx-start node block
(lvar-value info-lvar
) tag
))
1816 (defoptimizer (%unwind-protect ir2-convert
) ((info-lvar cleanup
) node block
)
1817 (declare (ignore cleanup
))
1818 (emit-nlx-start node block
(lvar-value info-lvar
) nil
))
1820 ;;; Emit the entry code for a non-local exit. We receive values and
1821 ;;; restore dynamic state.
1823 ;;; In the case of a lexical exit or CATCH, we look at the exit lvar's
1824 ;;; kind to determine which flavor of entry VOP to emit. If unknown
1825 ;;; values, emit the xxx-MULTIPLE variant to the lvar locs. If fixed
1826 ;;; values, make the appropriate number of temps in the standard
1827 ;;; values locations and use the other variant, delivering the temps
1828 ;;; to the lvar using MOVE-LVAR-RESULT.
1830 ;;; In the UNWIND-PROTECT case, we deliver the first register
1831 ;;; argument, the argument count and the argument pointer to our lvar
1832 ;;; as multiple values. These values are the block exited to and the
1833 ;;; values start and count.
1835 ;;; After receiving values, we restore dynamic state. Except in the
1836 ;;; UNWIND-PROTECT case, the values receiving restores the stack
1837 ;;; pointer. In an UNWIND-PROTECT cleanup, we want to leave the stack
1838 ;;; pointer alone, since the thrown values are still out there.
1839 (defoptimizer (%nlx-entry ir2-convert
) ((info-lvar) node block
)
1840 (let* ((info (lvar-value info-lvar
))
1841 (lvar (node-lvar node
))
1842 (2info (nlx-info-info info
))
1843 (top-loc (ir2-nlx-info-save-sp 2info
))
1844 (start-loc (make-nlx-entry-arg-start-location))
1845 (count-loc (make-arg-count-location))
1846 (target (ir2-nlx-info-target 2info
)))
1848 (ecase (cleanup-kind (nlx-info-cleanup info
))
1849 ((:catch
:block
:tagbody
)
1850 (let ((2lvar (and lvar
(lvar-info lvar
))))
1851 (if (and 2lvar
(eq (ir2-lvar-kind 2lvar
) :unknown
))
1852 (vop* nlx-entry-multiple node block
1853 (top-loc start-loc count-loc nil
)
1854 ((reference-tn-list (ir2-lvar-locs 2lvar
) t
))
1856 (let ((locs (standard-result-tns lvar
)))
1857 (vop* nlx-entry node block
1858 (top-loc start-loc count-loc nil
)
1859 ((reference-tn-list locs t
))
1862 (move-lvar-result node block locs lvar
)))))
1864 (let ((block-loc (standard-arg-location 0)))
1865 (vop uwp-entry node block target block-loc start-loc count-loc
)
1868 (list block-loc start-loc count-loc
)
1872 (when *collect-dynamic-statistics
*
1873 (vop count-me node block
*dynamic-counts-tn
*
1874 (block-number (ir2-block-block block
))))
1876 (vop* restore-dynamic-state node block
1877 ((reference-tn-list (cdr (ir2-nlx-info-dynamic-state 2info
)) nil
))
1879 (vop unbind-to-here node block
1880 (car (ir2-nlx-info-dynamic-state 2info
)))))
1882 ;;;; n-argument functions
1884 (macrolet ((def (name)
1885 `(defoptimizer (,name ir2-convert
) ((&rest args
) node block
)
1888 (/ sb
!vm
:large-object-size
1889 (* sb
!vm
:n-word-bytes
2)))
1890 ;; The VOPs will try to allocate all space at once
1891 ;; And it'll end up in large objects, and no conses
1892 ;; are welcome there.
1893 (ir2-convert-full-call node block
))
1895 (let* ((refs (reference-tn-list
1896 (loop for arg in args
1897 for tn
= (make-normal-tn *backend-t-primitive-type
*)
1899 (emit-move node block
(lvar-tn node block arg
) tn
)
1902 (lvar (node-lvar node
))
1903 (res (lvar-result-tns
1905 (list (primitive-type (specifier-type 'list
))))))
1906 (when (and lvar
(lvar-dynamic-extent lvar
))
1907 (vop current-stack-pointer node block
1908 (ir2-lvar-stack-pointer (lvar-info lvar
))))
1909 (vop* ,name node block
(refs) ((first res
) nil
)
1911 (move-lvar-result node block res lvar
)))))))
1916 (defoptimizer (mask-signed-field ir2-convert
) ((width x
) node block
)
1918 (when (constant-lvar-p width
)
1919 (case (lvar-value width
)
1920 (#.
(- sb
!vm
:n-word-bits sb
!vm
:n-fixnum-tag-bits
)
1921 (when (or (csubtypep (lvar-type x
)
1922 (specifier-type 'word
))
1923 (csubtypep (lvar-type x
)
1924 (specifier-type 'sb
!vm
:signed-word
)))
1925 (let* ((lvar (node-lvar node
))
1926 (temp (make-normal-tn
1927 (if (csubtypep (lvar-type x
)
1928 (specifier-type 'word
))
1929 (primitive-type-of most-positive-word
)
1931 (- (ash most-positive-word -
1))))))
1932 (results (lvar-result-tns
1934 (list (primitive-type-or-lose 'fixnum
)))))
1935 (emit-move node block
(lvar-tn node block x
) temp
)
1936 (vop sb
!vm
::move-from-word
/fixnum node block
1937 temp
(first results
))
1938 (move-lvar-result node block results lvar
)
1940 (#.sb
!vm
:n-word-bits
1941 (when (csubtypep (lvar-type x
) (specifier-type 'word
))
1942 (let* ((lvar (node-lvar node
))
1943 (temp (make-normal-tn
1944 (primitive-type-of most-positive-word
)))
1945 (results (lvar-result-tns
1947 (list (primitive-type
1948 (specifier-type 'sb
!vm
:signed-word
))))))
1949 (emit-move node block
(lvar-tn node block x
) temp
)
1950 (vop sb
!vm
::word-move node block
1951 temp
(first results
))
1952 (move-lvar-result node block results lvar
)
1954 (if (template-p (basic-combination-info node
))
1955 (ir2-convert-template node block
)
1956 (ir2-convert-full-call node block
))))
1958 ;; just a fancy identity
1959 (defoptimizer (%typep-wrapper ir2-convert
) ((value variable type
) node block
)
1960 (declare (ignore variable type
))
1961 (let* ((lvar (node-lvar node
))
1962 (results (lvar-result-tns lvar
(list (primitive-type-or-lose t
)))))
1963 (emit-move node block
(lvar-tn node block value
) (first results
))
1964 (move-lvar-result node block results lvar
)))
1966 ;;; Convert the code in a component into VOPs.
1967 (defun ir2-convert (component)
1968 (declare (type component component
))
1969 (let (#!+sb-dyncount
1970 (*dynamic-counts-tn
*
1971 (when *collect-dynamic-statistics
*
1973 (block-number (block-next (component-head component
))))
1974 (counts (make-array blocks
1975 :element-type
'(unsigned-byte 32)
1976 :initial-element
0))
1977 (info (make-dyncount-info
1978 :for
(component-name component
)
1979 :costs
(make-array blocks
1980 :element-type
'(unsigned-byte 32)
1983 (setf (ir2-component-dyncount-info (component-info component
))
1985 (emit-constant info
)
1986 (emit-constant counts
)))))
1988 (declare (type index num
))
1989 (do-ir2-blocks (2block component
)
1990 (let ((block (ir2-block-block 2block
)))
1991 (when (block-start block
)
1992 (setf (block-number block
) num
)
1994 (when *collect-dynamic-statistics
*
1995 (let ((first-node (block-start-node block
)))
1996 (unless (or (and (bind-p first-node
)
1997 (xep-p (bind-lambda first-node
)))
1999 (node-lvar first-node
))
2004 #!+sb-dyncount
*dynamic-counts-tn
* #!-sb-dyncount nil
2007 (let ((first-node (block-start-node block
)))
2008 (unless (or (and (bind-p first-node
)
2009 ;; Bind-nodes already have safepoints
2010 (eq (bind-lambda first-node
)
2011 (lambda-home (bind-lambda first-node
))))
2012 (and (valued-node-p first-node
)
2013 (node-lvar first-node
)
2015 (node-lvar first-node
))
2017 (when (and (rest (block-pred block
))
2019 (member (loop-kind (block-loop block
))
2020 '(:natural
:strange
))
2021 (eq block
(loop-head (block-loop block
)))
2022 (policy first-node
(< inhibit-safepoints
2)))
2023 (vop sb
!vm
::insert-safepoint first-node
2block
))))
2024 (ir2-convert-block block
)
2028 ;;; If necessary, emit a terminal unconditional branch to go to the
2029 ;;; successor block. If the successor is the component tail, then
2030 ;;; there isn't really any successor, but if the end is a non-tail
2031 ;;; call to a function that's not *known* to never return, then we
2032 ;;; emit an error trap just in case the function really does return.
2034 ;;; Trapping after known calls makes it easier to understand type
2035 ;;; derivation bugs at runtime: they show up as nil-fun-returned-error,
2036 ;;; rather than the execution of arbitrary code or error traps.
2037 (defun finish-ir2-block (block)
2038 (declare (type cblock block
))
2039 (let* ((2block (block-info block
))
2040 (last (block-last block
))
2041 (succ (block-succ block
)))
2043 (aver (singleton-p succ
))
2044 (let ((target (first succ
)))
2045 (cond ((eq target
(component-tail (block-component block
)))
2046 (when (and (basic-combination-p last
)
2047 (or (eq (basic-combination-kind last
) :full
)
2048 (and (eq (basic-combination-kind last
) :known
)
2049 (eq (basic-combination-info last
) :full
))))
2050 (let* ((fun (basic-combination-fun last
))
2051 (use (lvar-uses fun
))
2052 (name (and (ref-p use
)
2053 (leaf-has-source-name-p (ref-leaf use
))
2054 (leaf-source-name (ref-leaf use
))))
2055 (ftype (and (info :function
:info name
) ; only use the FTYPE if
2056 (proclaimed-ftype name
)))) ; NAME was DEFKNOWN
2057 (unless (or (node-tail-p last
)
2058 (policy last
(zerop safety
))
2059 (and (fun-type-p ftype
)
2060 (eq *empty-type
* (fun-type-returns ftype
))))
2061 (vop nil-fun-returned-error last
2block
2063 (emit-constant name
)
2064 (multiple-value-bind (tn named
)
2065 (fun-lvar-tn last
2block fun
)
2068 ((not (eq (ir2-block-next 2block
) (block-info target
)))
2069 (vop branch last
2block
(block-label target
)))
2071 (register-drop-thru target
))))))
2075 ;;; Convert the code in a block into VOPs.
2076 (defun ir2-convert-block (block)
2077 (declare (type cblock block
))
2078 (let ((2block (block-info block
)))
2079 (do-nodes (node lvar block
)
2083 (let ((2lvar (lvar-info lvar
)))
2084 ;; function REF in a local call is not annotated
2085 (when (and 2lvar
(not (eq (ir2-lvar-kind 2lvar
) :delayed
)))
2086 (ir2-convert-ref node
2block
)))))
2088 (let ((kind (basic-combination-kind node
)))
2091 (ir2-convert-local-call node
2block
))
2093 (ir2-convert-full-call node
2block
))
2095 (let* ((info (basic-combination-fun-info node
))
2096 (fun (fun-info-ir2-convert info
)))
2098 (funcall fun node
2block
))
2099 ((eq (basic-combination-info node
) :full
)
2100 (ir2-convert-full-call node
2block
))
2102 (ir2-convert-template node
2block
))))))))
2104 (when (lvar-info (if-test node
))
2105 (ir2-convert-if node
2block
)))
2107 (let ((fun (bind-lambda node
)))
2108 (when (eq (lambda-home fun
) fun
)
2109 (ir2-convert-bind node
2block
))))
2111 (ir2-convert-return node
2block
))
2113 (ir2-convert-set node
2block
))
2115 (ir2-convert-cast node
2block
))
2118 ((eq (basic-combination-kind node
) :local
)
2119 (ir2-convert-mv-bind node
2block
))
2120 ((eq (lvar-fun-name (basic-combination-fun node
))
2122 (ir2-convert-throw node
2block
))
2124 (ir2-convert-mv-call node
2block
))))
2126 (when (exit-entry node
)
2127 (ir2-convert-exit node
2block
)))
2129 (ir2-convert-entry node
2block
)))))
2131 (finish-ir2-block block
)