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 ;;; If there is any CHECK-xxx template for TYPE, then return it,
33 ;;; otherwise return NIL.
34 (defun type-check-template (type)
35 (declare (type ctype type))
36 (multiple-value-bind (check-ptype exact) (primitive-type type)
38 (primitive-type-check check-ptype)
39 (let ((name (hairy-type-check-template-name type)))
41 (template-or-lose name)
44 ;;; Emit code in BLOCK to check that VALUE is of the specified TYPE,
45 ;;; yielding the checked result in RESULT. VALUE and result may be of
46 ;;; any primitive type. There must be CHECK-xxx VOP for TYPE. Any
47 ;;; other type checks should have been converted to an explicit type
49 (defun emit-type-check (node block value result type)
50 (declare (type tn value result) (type node node) (type ir2-block block)
52 (emit-move-template node block (type-check-template type) value result)
55 ;;; Allocate an indirect value cell.
56 (defevent make-value-cell-event "Allocate heap value cell for lexical var.")
57 (defun emit-make-value-cell (node block value res)
58 (event make-value-cell-event node)
59 (vop make-value-cell node block value nil res))
63 ;;; Return the TN that holds the value of THING in the environment ENV.
64 (declaim (ftype (function ((or nlx-info lambda-var clambda) physenv) tn)
66 (defun find-in-physenv (thing physenv)
67 (or (cdr (assoc thing (ir2-physenv-closure (physenv-info physenv))))
70 ;; I think that a failure of this assertion means that we're
71 ;; trying to access a variable which was improperly closed
72 ;; over. The PHYSENV describes a physical environment. Every
73 ;; variable that a form refers to should either be in its
74 ;; physical environment directly, or grabbed from a
75 ;; surrounding physical environment when it was closed over.
76 ;; The ASSOC expression above finds closed-over variables, so
77 ;; if we fell through the ASSOC expression, it wasn't closed
78 ;; over. Therefore, it must be in our physical environment
79 ;; directly. If instead it is in some other physical
80 ;; environment, then it's bogus for us to reference it here
81 ;; without it being closed over. -- WHN 2001-09-29
82 (aver (eq physenv (lambda-physenv (lambda-var-home thing))))
85 (aver (eq physenv (block-physenv (nlx-info-target thing))))
86 (ir2-nlx-info-home (nlx-info-info thing)))
89 (entry-info-closure-tn (lambda-info thing))))
90 (bug "~@<~2I~_~S ~_not found in ~_~S~:>" thing physenv)))
92 ;;; If LEAF already has a constant TN, return that, otherwise make a
94 (defun constant-tn (leaf)
95 (declare (type constant leaf))
97 (setf (leaf-info leaf)
98 (make-constant-tn leaf))))
100 ;;; Return a TN that represents the value of LEAF, or NIL if LEAF
101 ;;; isn't directly represented by a TN. ENV is the environment that
102 ;;; the reference is done in.
103 (defun leaf-tn (leaf env)
104 (declare (type leaf leaf) (type physenv env))
107 (unless (lambda-var-indirect leaf)
108 (find-in-physenv leaf env)))
109 (constant (constant-tn leaf))
112 ;;; This is used to conveniently get a handle on a constant TN during
113 ;;; IR2 conversion. It returns a constant TN representing the Lisp
115 (defun emit-constant (value)
116 (constant-tn (find-constant value)))
118 ;;; Convert a REF node. The reference must not be delayed.
119 (defun ir2-convert-ref (node block)
120 (declare (type ref node) (type ir2-block block))
121 (let* ((lvar (node-lvar node))
122 (leaf (ref-leaf node))
123 (locs (lvar-result-tns
124 lvar (list (primitive-type (leaf-type leaf)))))
128 (let ((tn (find-in-physenv leaf (node-physenv node)))
129 (indirect (lambda-var-indirect leaf))
130 (explicit (lambda-var-explicit-value-cell leaf)))
132 ((and indirect explicit)
133 (vop value-cell-ref node block tn res))
135 (not (eq (node-physenv node)
136 (lambda-physenv (lambda-var-home leaf)))))
137 (vop ancestor-frame-ref node block tn (leaf-info leaf) res))
138 (t (emit-move node block tn res)))))
140 (emit-move node block (constant-tn leaf) res))
142 (ir2-convert-closure node block leaf res))
144 (let ((unsafe (policy node (zerop safety)))
145 (name (leaf-source-name leaf)))
146 (ecase (global-var-kind leaf)
148 (aver (symbolp name))
149 (let ((name-tn (emit-constant name)))
150 (if (or unsafe (info :variable :always-bound name))
151 (vop fast-symbol-value node block name-tn res)
152 (vop symbol-value node block name-tn res))))
154 (aver (symbolp name))
155 (let ((name-tn (emit-constant name)))
156 (if (or unsafe (info :variable :always-bound name))
157 (vop fast-symbol-global-value node block name-tn res)
158 (vop symbol-global-value node block name-tn res))))
160 (let ((fdefn-tn (make-load-time-constant-tn :fdefinition name)))
162 (vop fdefn-fun node block fdefn-tn res)
163 (vop safe-fdefn-fun node block fdefn-tn res))))))))
164 (move-lvar-result node block locs lvar))
167 ;;; some sanity checks for a CLAMBDA passed to IR2-CONVERT-CLOSURE
168 (defun assertions-on-ir2-converted-clambda (clambda)
169 ;; This assertion was sort of an experiment. It would be nice and
170 ;; sane and easier to understand things if it were *always* true,
171 ;; but experimentally I observe that it's only *almost* always
172 ;; true. -- WHN 2001-01-02
174 (aver (eql (lambda-component clambda)
175 (block-component (ir2-block-block ir2-block))))
176 ;; Check for some weirdness which came up in bug
179 ;; The MAKE-LOAD-TIME-CONSTANT-TN call above puts an :ENTRY record
180 ;; into the IR2-COMPONENT-CONSTANTS table. The dump-a-COMPONENT
182 ;; * treats every HANDLEless :ENTRY record into a
184 ;; * expects every patch to correspond to an
185 ;; IR2-COMPONENT-ENTRIES record.
186 ;; The IR2-COMPONENT-ENTRIES records are set by ENTRY-ANALYZE
187 ;; walking over COMPONENT-LAMBDAS. Bug 138b arose because there
188 ;; was a HANDLEless :ENTRY record which didn't correspond to an
189 ;; IR2-COMPONENT-ENTRIES record. That problem is hard to debug
190 ;; when it's caught at dump time, so this assertion tries to catch
192 (aver (member clambda
193 (component-lambdas (lambda-component clambda))))
194 ;; another bug-138-related issue: COMPONENT-NEW-FUNCTIONALS is
195 ;; used as a queue for stuff pending to do in IR1, and now that
196 ;; we're doing IR2 it should've been completely flushed (but
198 (aver (null (component-new-functionals (lambda-component clambda))))
201 ;;; Emit code to load a function object implementing FUNCTIONAL into
202 ;;; RES. This gets interesting when the referenced function is a
203 ;;; closure: we must make the closure and move the closed-over values
206 ;;; FUNCTIONAL is either a :TOPLEVEL-XEP functional or the XEP lambda
207 ;;; for the called function, since local call analysis converts all
208 ;;; closure references. If a :TOPLEVEL-XEP, we know it is not a
211 ;;; If a closed-over LAMBDA-VAR has no refs (is deleted), then we
212 ;;; don't initialize that slot. This can happen with closures over
213 ;;; top level variables, where optimization of the closure deleted the
214 ;;; variable. Since we committed to the closure format when we
215 ;;; pre-analyzed the top level code, we just leave an empty slot.
216 (defun ir2-convert-closure (ref ir2-block functional res)
217 (declare (type ref ref)
218 (type ir2-block ir2-block)
219 (type functional functional)
221 (aver (not (eql (functional-kind functional) :deleted)))
222 (unless (leaf-info functional)
223 (setf (leaf-info functional)
224 (make-entry-info :name (functional-debug-name functional))))
225 (let ((closure (etypecase functional
227 (assertions-on-ir2-converted-clambda functional)
228 (physenv-closure (get-lambda-physenv functional)))
230 (aver (eq (functional-kind functional) :toplevel-xep))
234 (let* ((physenv (node-physenv ref))
235 (tn (find-in-physenv functional physenv)))
236 (emit-move ref ir2-block tn res)))
238 (let ((entry (make-load-time-constant-tn :entry functional)))
239 (emit-move ref ir2-block entry res)))))
242 (defun closure-initial-value (what this-env current-fp)
243 (declare (type (or nlx-info lambda-var clambda) what)
244 (type physenv this-env)
245 (type (or tn null) current-fp))
246 ;; If we have an indirect LAMBDA-VAR that does not require an
247 ;; EXPLICIT-VALUE-CELL, and is from this environment (not from being
248 ;; closed over), we need to store the current frame pointer.
249 (if (and (lambda-var-p what)
250 (lambda-var-indirect what)
251 (not (lambda-var-explicit-value-cell what))
252 (eq (lambda-physenv (lambda-var-home what))
255 (find-in-physenv what this-env)))
257 (defoptimizer (%allocate-closures ltn-annotate) ((leaves) node ltn-policy)
258 ltn-policy ; a hack to effectively (DECLARE (IGNORE LTN-POLICY))
259 (when (lvar-dynamic-extent leaves)
260 (let ((info (make-ir2-lvar *backend-t-primitive-type*)))
261 (setf (ir2-lvar-kind info) :delayed)
262 (setf (lvar-info leaves) info)
263 (setf (ir2-lvar-stack-pointer info)
264 (make-stack-pointer-tn)))))
266 (defoptimizer (%allocate-closures ir2-convert) ((leaves) call 2block)
267 (let ((dx-p (lvar-dynamic-extent leaves)))
270 (vop current-stack-pointer call 2block
271 (ir2-lvar-stack-pointer (lvar-info leaves))))
272 (dolist (leaf (lvar-value leaves))
273 (binding* ((xep (awhen (functional-entry-fun leaf)
274 ;; if the xep's been deleted then we can skip it
275 (if (eq (functional-kind it) :deleted)
278 (nil (aver (xep-p xep)))
279 (entry-info (lambda-info xep) :exit-if-null)
280 (tn (entry-info-closure-tn entry-info) :exit-if-null)
281 (closure (physenv-closure (get-lambda-physenv xep)))
282 (entry (make-load-time-constant-tn :entry xep)))
283 (let ((this-env (node-physenv call))
284 (leaf-dx-p (and dx-p (leaf-dynamic-extent leaf))))
285 (vop make-closure call 2block entry (length closure)
287 (loop for what in closure and n from 0 do
288 (unless (and (lambda-var-p what)
289 (null (leaf-refs what)))
290 ;; In LABELS a closure may refer to another closure
291 ;; in the same group, so we must be sure that we
292 ;; store a closure only after its creation.
294 ;; TODO: Here is a simple solution: we postpone
295 ;; putting of all closures after all creations
296 ;; (though it may require more registers).
298 (delayed (list tn (find-in-physenv what this-env) n))
299 (let ((initial-value (closure-initial-value
302 (vop closure-init call 2block
304 ;; An initial-value of NIL means to stash
305 ;; the frame pointer... which requires a
307 (vop closure-init-from-fp call 2block tn n)))))))))
308 (loop for (tn what n) in (delayed)
309 do (vop closure-init call 2block
313 ;;; Convert a SET node. If the NODE's LVAR is annotated, then we also
314 ;;; deliver the value to that lvar. If the var is a lexical variable
315 ;;; with no refs, then we don't actually set anything, since the
316 ;;; variable has been deleted.
317 (defun ir2-convert-set (node block)
318 (declare (type cset node) (type ir2-block block))
319 (let* ((lvar (node-lvar node))
320 (leaf (set-var node))
321 (val (lvar-tn node block (set-value node)))
324 lvar (list (primitive-type (leaf-type leaf))))
328 (when (leaf-refs leaf)
329 (let ((tn (find-in-physenv leaf (node-physenv node)))
330 (indirect (lambda-var-indirect leaf))
331 (explicit (lambda-var-explicit-value-cell leaf)))
333 ((and indirect explicit)
334 (vop value-cell-set node block tn val))
336 (not (eq (node-physenv node)
337 (lambda-physenv (lambda-var-home leaf)))))
338 (vop ancestor-frame-set node block tn val (leaf-info leaf)))
339 (t (emit-move node block val tn))))))
341 (aver (symbolp (leaf-source-name leaf)))
342 (ecase (global-var-kind leaf)
344 (vop set node block (emit-constant (leaf-source-name leaf)) val))
346 (vop %set-symbol-global-value node
347 block (emit-constant (leaf-source-name leaf)) val)))))
349 (emit-move node block val (first locs))
350 (move-lvar-result node block locs lvar)))
353 ;;;; utilities for receiving fixed values
355 ;;; Return a TN that can be referenced to get the value of LVAR. LVAR
356 ;;; must be LTN-ANNOTATED either as a delayed leaf ref or as a fixed,
357 ;;; single-value lvar.
359 ;;; The primitive-type of the result will always be the same as the
360 ;;; IR2-LVAR-PRIMITIVE-TYPE, ensuring that VOPs are always called with
361 ;;; TNs that satisfy the operand primitive-type restriction. We may
362 ;;; have to make a temporary of the desired type and move the actual
363 ;;; lvar TN into it. This happens when we delete a type check in
364 ;;; unsafe code or when we locally know something about the type of an
365 ;;; argument variable.
366 (defun lvar-tn (node block lvar)
367 (declare (type node node) (type ir2-block block) (type lvar lvar))
368 (let* ((2lvar (lvar-info lvar))
370 (ecase (ir2-lvar-kind 2lvar)
372 (let ((ref (lvar-uses lvar)))
373 (leaf-tn (ref-leaf ref) (node-physenv ref))))
375 (aver (= (length (ir2-lvar-locs 2lvar)) 1))
376 (first (ir2-lvar-locs 2lvar)))))
377 (ptype (ir2-lvar-primitive-type 2lvar)))
379 (cond ((eq (tn-primitive-type lvar-tn) ptype) lvar-tn)
381 (let ((temp (make-normal-tn ptype)))
382 (emit-move node block lvar-tn temp)
385 ;;; This is similar to LVAR-TN, but hacks multiple values. We return
386 ;;; TNs holding the values of LVAR with PTYPES as their primitive
387 ;;; types. LVAR must be annotated for the same number of fixed values
388 ;;; are there are PTYPES.
390 ;;; If the lvar has a type check, check the values into temps and
391 ;;; return the temps. When we have more values than assertions, we
392 ;;; move the extra values with no check.
393 (defun lvar-tns (node block lvar ptypes)
394 (declare (type node node) (type ir2-block block)
395 (type lvar lvar) (list ptypes))
396 (let* ((locs (ir2-lvar-locs (lvar-info lvar)))
397 (nlocs (length locs)))
398 (aver (= nlocs (length ptypes)))
400 (mapcar (lambda (from to-type)
401 (if (eq (tn-primitive-type from) to-type)
403 (let ((temp (make-normal-tn to-type)))
404 (emit-move node block from temp)
409 ;;;; utilities for delivering values to lvars
411 ;;; Return a list of TNs with the specifier TYPES that can be used as
412 ;;; result TNs to evaluate an expression into LVAR. This is used
413 ;;; together with MOVE-LVAR-RESULT to deliver fixed values to
416 ;;; If the lvar isn't annotated (meaning the values are discarded) or
417 ;;; is unknown-values, the then we make temporaries for each supplied
418 ;;; value, providing a place to compute the result in until we decide
419 ;;; what to do with it (if anything.)
421 ;;; If the lvar is fixed-values, and wants the same number of values
422 ;;; as the user wants to deliver, then we just return the
423 ;;; IR2-LVAR-LOCS. Otherwise we make a new list padded as necessary by
424 ;;; discarded TNs. We always return a TN of the specified type, using
425 ;;; the lvar locs only when they are of the correct type.
426 (defun lvar-result-tns (lvar types)
427 (declare (type (or lvar null) lvar) (type list types))
429 (mapcar #'make-normal-tn types)
430 (let ((2lvar (lvar-info lvar)))
431 (ecase (ir2-lvar-kind 2lvar)
433 (let* ((locs (ir2-lvar-locs 2lvar))
434 (nlocs (length locs))
435 (ntypes (length types)))
436 (if (and (= nlocs ntypes)
437 (do ((loc locs (cdr loc))
438 (type types (cdr type)))
440 (unless (eq (tn-primitive-type (car loc)) (car type))
443 (mapcar (lambda (loc type)
444 (if (eq (tn-primitive-type loc) type)
446 (make-normal-tn type)))
449 (mapcar #'make-normal-tn
450 (subseq types nlocs)))
454 (mapcar #'make-normal-tn types))))))
456 ;;; Make the first N standard value TNs, returning them in a list.
457 (defun make-standard-value-tns (n)
458 (declare (type unsigned-byte n))
461 (res (standard-arg-location i)))
464 ;;; Return a list of TNs wired to the standard value passing
465 ;;; conventions that can be used to receive values according to the
466 ;;; unknown-values convention. This is used with together
467 ;;; MOVE-LVAR-RESULT for delivering unknown values to a fixed values
470 ;;; If the lvar isn't annotated, then we treat as 0-values, returning
471 ;;; an empty list of temporaries.
473 ;;; If the lvar is annotated, then it must be :FIXED.
474 (defun standard-result-tns (lvar)
475 (declare (type (or lvar null) lvar))
477 (let ((2lvar (lvar-info lvar)))
478 (ecase (ir2-lvar-kind 2lvar)
480 (make-standard-value-tns (length (ir2-lvar-locs 2lvar))))))
483 ;;; Just move each SRC TN into the corresponding DEST TN, defaulting
484 ;;; any unsupplied source values to NIL. We let EMIT-MOVE worry about
485 ;;; doing the appropriate coercions.
486 (defun move-results-coerced (node block src dest)
487 (declare (type node node) (type ir2-block block) (list src dest))
488 (let ((nsrc (length src))
489 (ndest (length dest)))
490 (mapc (lambda (from to)
492 (emit-move node block from to)))
494 (append src (make-list (- ndest nsrc)
495 :initial-element (emit-constant nil)))
500 ;;; Move each SRC TN into the corresponding DEST TN, checking types
501 ;;; and defaulting any unsupplied source values to NIL
502 (defun move-results-checked (node block src dest types)
503 (declare (type node node) (type ir2-block block) (list src dest types))
504 (let ((nsrc (length src))
505 (ndest (length dest))
506 (ntypes (length types)))
507 (mapc (lambda (from to type)
509 (emit-type-check node block from to type)
510 (emit-move node block from to)))
512 (append src (make-list (- ndest nsrc)
513 :initial-element (emit-constant nil)))
517 (append types (make-list (- ndest ntypes)))
521 ;;; If necessary, emit coercion code needed to deliver the RESULTS to
522 ;;; the specified lvar. NODE and BLOCK provide context for emitting
523 ;;; code. Although usually obtained from STANDARD-RESULT-TNs or
524 ;;; LVAR-RESULT-TNs, RESULTS my be a list of any type or
527 ;;; If the lvar is fixed values, then move the results into the lvar
528 ;;; locations. If the lvar is unknown values, then do the moves into
529 ;;; the standard value locations, and use PUSH-VALUES to put the
530 ;;; values on the stack.
531 (defun move-lvar-result (node block results lvar)
532 (declare (type node node) (type ir2-block block)
533 (list results) (type (or lvar null) lvar))
535 (let ((2lvar (lvar-info lvar)))
536 (ecase (ir2-lvar-kind 2lvar)
538 (let ((locs (ir2-lvar-locs 2lvar)))
539 (unless (eq locs results)
540 (move-results-coerced node block results locs))))
542 (let* ((nvals (length results))
543 (locs (make-standard-value-tns nvals)))
544 (move-results-coerced node block results locs)
545 (vop* push-values node block
546 ((reference-tn-list locs nil))
547 ((reference-tn-list (ir2-lvar-locs 2lvar) t))
552 (defun ir2-convert-cast (node block)
553 (declare (type cast node)
554 (type ir2-block block))
555 (binding* ((lvar (node-lvar node) :exit-if-null)
556 (2lvar (lvar-info lvar))
557 (value (cast-value node))
558 (2value (lvar-info value)))
559 (cond ((eq (ir2-lvar-kind 2lvar) :unused))
560 ((eq (ir2-lvar-kind 2lvar) :unknown)
561 (aver (eq (ir2-lvar-kind 2value) :unknown))
562 (aver (not (cast-type-check node)))
563 (move-results-coerced node block
564 (ir2-lvar-locs 2value)
565 (ir2-lvar-locs 2lvar)))
566 ((eq (ir2-lvar-kind 2lvar) :fixed)
567 (aver (eq (ir2-lvar-kind 2value) :fixed))
568 (if (cast-type-check node)
569 (move-results-checked node block
570 (ir2-lvar-locs 2value)
571 (ir2-lvar-locs 2lvar)
572 (multiple-value-bind (check types)
573 (cast-check-types node nil)
574 (aver (eq check :simple))
576 (move-results-coerced node block
577 (ir2-lvar-locs 2value)
578 (ir2-lvar-locs 2lvar))))
579 (t (bug "CAST cannot be :DELAYED.")))))
581 ;;;; template conversion
583 ;;; Build a TN-REFS list that represents access to the values of the
584 ;;; specified list of lvars ARGS for TEMPLATE. Any :CONSTANT arguments
585 ;;; are returned in the second value as a list rather than being
586 ;;; accessed as a normal argument. NODE and BLOCK provide the context
587 ;;; for emitting any necessary type-checking code.
588 (defun reference-args (node block args template)
589 (declare (type node node) (type ir2-block block) (list args)
590 (type template template))
591 (collect ((info-args))
594 (do ((args args (cdr args))
595 (types (template-arg-types template) (cdr types)))
597 (let ((type (first types))
599 (if (and (consp type) (eq (car type) ':constant))
600 (info-args (lvar-value arg))
601 (let ((ref (reference-tn (lvar-tn node block arg) nil)))
603 (setf (tn-ref-across last) ref)
607 (values (the (or tn-ref null) first) (info-args)))))
609 ;;; Convert a conditional template. We try to exploit any
610 ;;; drop-through, but emit an unconditional branch afterward if we
611 ;;; fail. NOT-P is true if the sense of the TEMPLATE's test should be
613 (defun ir2-convert-conditional (node block template args info-args if not-p)
614 (declare (type node node) (type ir2-block block)
615 (type template template) (type (or tn-ref null) args)
616 (list info-args) (type cif if) (type boolean not-p))
617 (let ((consequent (if-consequent if))
618 (alternative (if-alternative if))
619 (flags (and (consp (template-result-types template))
620 (rest (template-result-types template)))))
621 (aver (= (template-info-arg-count template)
622 (+ (length info-args)
625 (rotatef consequent alternative)
627 (when (drop-thru-p if consequent)
628 (rotatef consequent alternative)
631 (emit-template node block template args nil
632 (list* (block-label consequent) not-p
634 (unless (drop-thru-p if alternative)
635 (vop branch node block (block-label alternative))))
637 (emit-template node block template args nil info-args)
638 (vop branch-if node block (block-label consequent) flags not-p)
639 (unless (drop-thru-p if alternative)
640 (vop branch node block (block-label alternative)))))))
642 ;;; Convert an IF that isn't the DEST of a conditional template.
643 (defun ir2-convert-if (node block)
644 (declare (type ir2-block block) (type cif node))
645 (let* ((test (if-test node))
646 (test-ref (reference-tn (lvar-tn node block test) nil))
647 (nil-ref (reference-tn (emit-constant nil) nil)))
648 (setf (tn-ref-across test-ref) nil-ref)
649 (ir2-convert-conditional node block (template-or-lose 'if-eq)
650 test-ref () node t)))
652 ;;; Return a list of primitive-types that we can pass to LVAR-RESULT-TNS
653 ;;; describing the result types we want for a template call. We are really
654 ;;; only interested in the number of results required: in normal case
655 ;;; TEMPLATE-RESULTS-OK has already checked them.
656 (defun find-template-result-types (call rtypes)
657 (let* ((type (node-derived-type call))
659 (mapcar #'primitive-type
660 (if (values-type-p type)
661 (append (args-type-required type)
662 (args-type-optional type))
664 (primitive-t *backend-t-primitive-type*))
665 (loop for rtype in rtypes
666 for type = (or (pop types) primitive-t)
669 ;;; Return a list of TNs usable in a CALL to TEMPLATE delivering values to
670 ;;; LVAR. As an efficiency hack, we pick off the common case where the LVAR is
671 ;;; fixed values and has locations that satisfy the result restrictions. This
672 ;;; can fail when there is a type check or a values count mismatch.
673 (defun make-template-result-tns (call lvar rtypes)
674 (declare (type combination call) (type (or lvar null) lvar)
676 (let ((2lvar (when lvar (lvar-info lvar))))
677 (if (and 2lvar (eq (ir2-lvar-kind 2lvar) :fixed))
678 (let ((locs (ir2-lvar-locs 2lvar)))
679 (if (and (= (length rtypes) (length locs))
680 (do ((loc locs (cdr loc))
681 (rtypes rtypes (cdr rtypes)))
683 (unless (operand-restriction-ok
685 (tn-primitive-type (car loc))
691 (find-template-result-types call rtypes))))
694 (find-template-result-types call rtypes)))))
696 ;;; Get the operands into TNs, make TN-REFs for them, and then call
697 ;;; the template emit function.
698 (defun ir2-convert-template (call block)
699 (declare (type combination call) (type ir2-block block))
700 (let* ((template (combination-info call))
701 (lvar (node-lvar call))
702 (rtypes (template-result-types template)))
703 (multiple-value-bind (args info-args)
704 (reference-args call block (combination-args call) template)
705 (aver (not (template-more-results-type template)))
706 (if (template-conditional-p template)
707 (ir2-convert-conditional call block template args info-args
708 (lvar-dest lvar) nil)
709 (let* ((results (make-template-result-tns call lvar rtypes))
710 (r-refs (reference-tn-list results t)))
711 (aver (= (length info-args)
712 (template-info-arg-count template)))
713 (when (and lvar (lvar-dynamic-extent lvar))
714 (vop current-stack-pointer call block
715 (ir2-lvar-stack-pointer (lvar-info lvar))))
716 (when (emit-step-p call)
717 (vop sb!vm::step-instrument-before-vop call block))
719 (emit-template call block template args r-refs info-args)
720 (emit-template call block template args r-refs))
721 (move-lvar-result call block results lvar)))))
724 ;;; We don't have to do much because operand count checking is done by
725 ;;; IR1 conversion. The only difference between this and the function
726 ;;; case of IR2-CONVERT-TEMPLATE is that there can be codegen-info
728 (defoptimizer (%%primitive ir2-convert) ((template info &rest args) call block)
729 (let* ((template (lvar-value template))
730 (info (lvar-value info))
731 (lvar (node-lvar call))
732 (rtypes (template-result-types template))
733 (results (make-template-result-tns call lvar rtypes))
734 (r-refs (reference-tn-list results t)))
735 (multiple-value-bind (args info-args)
736 (reference-args call block (cddr (combination-args call)) template)
737 (aver (not (template-more-results-type template)))
738 (aver (not (template-conditional-p template)))
739 (aver (null info-args))
742 (emit-template call block template args r-refs info)
743 (emit-template call block template args r-refs))
745 (move-lvar-result call block results lvar)))
748 (defoptimizer (%%primitive derive-type) ((template info &rest args))
749 (let ((type (template-type (lvar-value template))))
750 (if (fun-type-p type)
751 (fun-type-returns type)
756 ;;; Convert a LET by moving the argument values into the variables.
757 ;;; Since a LET doesn't have any passing locations, we move the
758 ;;; arguments directly into the variables. We must also allocate any
759 ;;; indirect value cells, since there is no function prologue to do
761 (defun ir2-convert-let (node block fun)
762 (declare (type combination node) (type ir2-block block) (type clambda fun))
763 (mapc (lambda (var arg)
765 (let ((src (lvar-tn node block arg))
766 (dest (leaf-info var)))
767 (if (and (lambda-var-indirect var)
768 (lambda-var-explicit-value-cell var))
769 (emit-make-value-cell node block src dest)
770 (emit-move node block src dest)))))
771 (lambda-vars fun) (basic-combination-args node))
774 ;;; Emit any necessary moves into assignment temps for a local call to
775 ;;; FUN. We return two lists of TNs: TNs holding the actual argument
776 ;;; values, and (possibly EQ) TNs that are the actual destination of
777 ;;; the arguments. When necessary, we allocate temporaries for
778 ;;; arguments to preserve parallel assignment semantics. These lists
779 ;;; exclude unused arguments and include implicit environment
780 ;;; arguments, i.e. they exactly correspond to the arguments passed.
782 ;;; OLD-FP is the TN currently holding the value we want to pass as
783 ;;; OLD-FP. If null, then the call is to the same environment (an
784 ;;; :ASSIGNMENT), so we only move the arguments, and leave the
785 ;;; environment alone.
787 ;;; CLOSURE-FP is for calling a closure that has "implicit" value
788 ;;; cells (stored in the allocating stack frame), and is the frame
789 ;;; pointer TN to use for values allocated in the outbound stack
790 ;;; frame. This is distinct from OLD-FP for the specific case of a
792 (defun emit-psetq-moves (node block fun old-fp &optional (closure-fp old-fp))
793 (declare (type combination node) (type ir2-block block) (type clambda fun)
794 (type (or tn null) old-fp closure-fp))
795 (let ((actuals (mapcar (lambda (x)
797 (lvar-tn node block x)))
798 (combination-args node))))
801 (dolist (var (lambda-vars fun))
802 (let ((actual (pop actuals))
803 (loc (leaf-info var)))
806 ((and (lambda-var-indirect var)
807 (lambda-var-explicit-value-cell var))
809 (make-normal-tn *backend-t-primitive-type*)))
810 (emit-make-value-cell node block actual temp)
812 ((member actual (locs))
813 (let ((temp (make-normal-tn (tn-primitive-type loc))))
814 (emit-move node block actual temp)
821 (let ((this-1env (node-physenv node))
822 (called-env (physenv-info (lambda-physenv fun))))
823 (dolist (thing (ir2-physenv-closure called-env))
824 (temps (closure-initial-value (car thing) this-1env closure-fp))
827 (locs (ir2-physenv-old-fp called-env))))
829 (values (temps) (locs)))))
831 ;;; A tail-recursive local call is done by emitting moves of stuff
832 ;;; into the appropriate passing locations. After setting up the args
833 ;;; and environment, we just move our return-pc into the called
834 ;;; function's passing location.
835 (defun ir2-convert-tail-local-call (node block fun)
836 (declare (type combination node) (type ir2-block block) (type clambda fun))
837 (let ((this-env (physenv-info (node-physenv node)))
838 (current-fp (make-stack-pointer-tn)))
839 (multiple-value-bind (temps locs)
840 (emit-psetq-moves node block fun
841 (ir2-physenv-old-fp this-env) current-fp)
843 ;; If we're about to emit a move from CURRENT-FP then we need to
845 (when (find current-fp temps)
846 (vop current-fp node block current-fp))
848 (mapc (lambda (temp loc)
849 (emit-move node block temp loc))
852 (emit-move node block
853 (ir2-physenv-return-pc this-env)
854 (ir2-physenv-return-pc-pass
856 (lambda-physenv fun)))))
860 ;;; Convert an :ASSIGNMENT call. This is just like a tail local call,
861 ;;; except that the caller and callee environment are the same, so we
862 ;;; don't need to mess with the environment locations, return PC, etc.
863 (defun ir2-convert-assignment (node block fun)
864 (declare (type combination node) (type ir2-block block) (type clambda fun))
865 (multiple-value-bind (temps locs) (emit-psetq-moves node block fun nil)
867 (mapc (lambda (temp loc)
868 (emit-move node block temp loc))
872 ;;; Do stuff to set up the arguments to a non-tail local call
873 ;;; (including implicit environment args.) We allocate a frame
874 ;;; (returning the FP and NFP), and also compute the TN-REFS list for
875 ;;; the values to pass and the list of passing location TNs.
876 (defun ir2-convert-local-call-args (node block fun)
877 (declare (type combination node) (type ir2-block block) (type clambda fun))
878 (let ((fp (make-stack-pointer-tn))
879 (nfp (make-number-stack-pointer-tn))
880 (old-fp (make-stack-pointer-tn)))
881 (multiple-value-bind (temps locs)
882 (emit-psetq-moves node block fun old-fp)
883 (vop current-fp node block old-fp)
884 (vop allocate-frame node block
885 (physenv-info (lambda-physenv fun))
887 (values fp nfp temps (mapcar #'make-alias-tn locs)))))
889 ;;; Handle a non-TR known-values local call. We emit the call, then
890 ;;; move the results to the lvar's destination.
891 (defun ir2-convert-local-known-call (node block fun returns lvar start)
892 (declare (type node node) (type ir2-block block) (type clambda fun)
893 (type return-info returns) (type (or lvar null) lvar)
895 (multiple-value-bind (fp nfp temps arg-locs)
896 (ir2-convert-local-call-args node block fun)
897 (let ((locs (return-info-locations returns)))
898 (vop* known-call-local node block
899 (fp nfp (reference-tn-list temps nil))
900 ((reference-tn-list locs t))
901 arg-locs (physenv-info (lambda-physenv fun)) start)
902 (move-lvar-result node block locs lvar)))
905 ;;; Handle a non-TR unknown-values local call. We do different things
906 ;;; depending on what kind of values the lvar wants.
908 ;;; If LVAR is :UNKNOWN, then we use the "multiple-" variant, directly
909 ;;; specifying the lvar's LOCS as the VOP results so that we don't
910 ;;; have to do anything after the call.
912 ;;; Otherwise, we use STANDARD-RESULT-TNS to get wired result TNs, and
913 ;;; then call MOVE-LVAR-RESULT to do any necessary type checks or
915 (defun ir2-convert-local-unknown-call (node block fun lvar start)
916 (declare (type node node) (type ir2-block block) (type clambda fun)
917 (type (or lvar null) lvar) (type label start))
918 (multiple-value-bind (fp nfp temps arg-locs)
919 (ir2-convert-local-call-args node block fun)
920 (let ((2lvar (and lvar (lvar-info lvar)))
921 (env (physenv-info (lambda-physenv fun)))
922 (temp-refs (reference-tn-list temps nil)))
923 (if (and 2lvar (eq (ir2-lvar-kind 2lvar) :unknown))
924 (vop* multiple-call-local node block (fp nfp temp-refs)
925 ((reference-tn-list (ir2-lvar-locs 2lvar) t))
927 (let ((locs (standard-result-tns lvar)))
928 (vop* call-local node block
930 ((reference-tn-list locs t))
931 arg-locs env start (length locs))
932 (move-lvar-result node block locs lvar)))))
935 ;;; Dispatch to the appropriate function, depending on whether we have
936 ;;; a let, tail or normal call. If the function doesn't return, call
937 ;;; it using the unknown-value convention. We could compile it as a
938 ;;; tail call, but that might seem confusing in the debugger.
939 (defun ir2-convert-local-call (node block)
940 (declare (type combination node) (type ir2-block block))
941 (let* ((fun (ref-leaf (lvar-uses (basic-combination-fun node))))
942 (kind (functional-kind fun)))
943 (cond ((eq kind :let)
944 (ir2-convert-let node block fun))
945 ((eq kind :assignment)
946 (ir2-convert-assignment node block fun))
948 (ir2-convert-tail-local-call node block fun))
950 (let ((start (block-label (lambda-block fun)))
951 (returns (tail-set-info (lambda-tail-set fun)))
952 (lvar (node-lvar node)))
954 (return-info-kind returns)
957 (ir2-convert-local-unknown-call node block fun lvar start))
959 (ir2-convert-local-known-call node block fun returns
965 ;;; Given a function lvar FUN, return (VALUES TN-TO-CALL NAMED-P),
966 ;;; where TN-TO-CALL is a TN holding the thing that we call NAMED-P is
967 ;;; true if the thing is named (false if it is a function).
969 ;;; There are two interesting non-named cases:
970 ;;; -- We know it's a function. No check needed: return the
972 ;;; -- We don't know what it is.
973 (defun fun-lvar-tn (node block lvar)
974 (declare (ignore node block))
975 (declare (type lvar lvar))
976 (let ((2lvar (lvar-info lvar)))
977 (if (eq (ir2-lvar-kind 2lvar) :delayed)
978 (let ((name (lvar-fun-name lvar t)))
980 (values (make-load-time-constant-tn :fdefinition name) t))
981 (let* ((locs (ir2-lvar-locs 2lvar))
983 (function-ptype (primitive-type-or-lose 'function)))
984 (aver (and (eq (ir2-lvar-kind 2lvar) :fixed)
985 (= (length locs) 1)))
986 (aver (eq (tn-primitive-type loc) function-ptype))
989 ;;; Set up the args to NODE in the current frame, and return a TN-REF
990 ;;; list for the passing locations.
991 (defun move-tail-full-call-args (node block)
992 (declare (type combination node) (type ir2-block block))
993 (let ((args (basic-combination-args node))
996 (dotimes (num (length args))
997 (let ((loc (standard-arg-location num)))
998 (emit-move node block (lvar-tn node block (elt args num)) loc)
999 (let ((ref (reference-tn loc nil)))
1001 (setf (tn-ref-across last) ref)
1006 ;;; Move the arguments into the passing locations and do a (possibly
1007 ;;; named) tail call.
1008 (defun ir2-convert-tail-full-call (node block)
1009 (declare (type combination node) (type ir2-block block))
1010 (let* ((env (physenv-info (node-physenv node)))
1011 (args (basic-combination-args node))
1012 (nargs (length args))
1013 (pass-refs (move-tail-full-call-args node block))
1014 (old-fp (ir2-physenv-old-fp env))
1015 (return-pc (ir2-physenv-return-pc env)))
1017 (multiple-value-bind (fun-tn named)
1018 (fun-lvar-tn node block (basic-combination-fun node))
1020 (vop* tail-call-named node block
1021 (fun-tn old-fp return-pc pass-refs)
1025 (vop* tail-call node block
1026 (fun-tn old-fp return-pc pass-refs)
1029 (emit-step-p node)))))
1033 ;;; like IR2-CONVERT-LOCAL-CALL-ARGS, only different
1034 (defun ir2-convert-full-call-args (node block)
1035 (declare (type combination node) (type ir2-block block))
1036 (let* ((args (basic-combination-args node))
1037 (fp (make-stack-pointer-tn))
1038 (nargs (length args)))
1039 (vop allocate-full-call-frame node block nargs fp)
1043 (dotimes (num nargs)
1044 (locs (standard-arg-location num))
1045 (let ((ref (reference-tn (lvar-tn node block (elt args num))
1048 (setf (tn-ref-across last) ref)
1052 (values fp first (locs) nargs)))))
1054 ;;; Do full call when a fixed number of values are desired. We make
1055 ;;; STANDARD-RESULT-TNS for our lvar, then deliver the result using
1056 ;;; MOVE-LVAR-RESULT. We do named or normal call, as appropriate.
1057 (defun ir2-convert-fixed-full-call (node block)
1058 (declare (type combination node) (type ir2-block block))
1059 (multiple-value-bind (fp args arg-locs nargs)
1060 (ir2-convert-full-call-args node block)
1061 (let* ((lvar (node-lvar node))
1062 (locs (standard-result-tns lvar))
1063 (loc-refs (reference-tn-list locs t))
1064 (nvals (length locs)))
1065 (multiple-value-bind (fun-tn named)
1066 (fun-lvar-tn node block (basic-combination-fun node))
1068 (vop* call-named node block (fp fun-tn args) (loc-refs)
1069 arg-locs nargs nvals (emit-step-p node))
1070 (vop* call node block (fp fun-tn args) (loc-refs)
1071 arg-locs nargs nvals (emit-step-p node)))
1072 (move-lvar-result node block locs lvar))))
1075 ;;; Do full call when unknown values are desired.
1076 (defun ir2-convert-multiple-full-call (node block)
1077 (declare (type combination node) (type ir2-block block))
1078 (multiple-value-bind (fp args arg-locs nargs)
1079 (ir2-convert-full-call-args node block)
1080 (let* ((lvar (node-lvar node))
1081 (locs (ir2-lvar-locs (lvar-info lvar)))
1082 (loc-refs (reference-tn-list locs t)))
1083 (multiple-value-bind (fun-tn named)
1084 (fun-lvar-tn node block (basic-combination-fun node))
1086 (vop* multiple-call-named node block (fp fun-tn args) (loc-refs)
1087 arg-locs nargs (emit-step-p node))
1088 (vop* multiple-call node block (fp fun-tn args) (loc-refs)
1089 arg-locs nargs (emit-step-p node))))))
1092 ;;; stuff to check in PONDER-FULL-CALL
1094 ;;; These came in handy when troubleshooting cold boot after making
1095 ;;; major changes in the package structure: various transforms and
1096 ;;; VOPs and stuff got attached to the wrong symbol, so that
1097 ;;; references to the right symbol were bogusly translated as full
1098 ;;; calls instead of primitives, sending the system off into infinite
1099 ;;; space. Having a report on all full calls generated makes it easier
1100 ;;; to figure out what form caused the problem this time.
1101 #!+sb-show (defvar *show-full-called-fnames-p* nil)
1102 #!+sb-show (defvar *full-called-fnames* (make-hash-table :test 'equal))
1104 ;;; Do some checks (and store some notes relevant for future checks)
1106 ;;; * Is this a full call to something we have reason to know should
1107 ;;; never be full called? (Except as of sbcl-0.7.18 or so, we no
1108 ;;; longer try to ensure this behavior when *FAILURE-P* has already
1110 ;;; * Is this a full call to (SETF FOO) which might conflict with
1111 ;;; a DEFSETF or some such thing elsewhere in the program?
1112 (defun ponder-full-call (node)
1113 (let* ((lvar (basic-combination-fun node))
1114 (fname (lvar-fun-name lvar t)))
1115 (declare (type (or symbol cons) fname))
1117 #!+sb-show (unless (gethash fname *full-called-fnames*)
1118 (setf (gethash fname *full-called-fnames*) t))
1119 #!+sb-show (when *show-full-called-fnames-p*
1120 (/show "converting full call to named function" fname)
1121 (/show (basic-combination-args node))
1122 (/show (policy node speed) (policy node safety))
1123 (/show (policy node compilation-speed))
1124 (let ((arg-types (mapcar (lambda (lvar)
1128 (basic-combination-args node))))
1131 ;; When illegal code is compiled, all sorts of perverse paths
1132 ;; through the compiler can be taken, and it's much harder -- and
1133 ;; probably pointless -- to guarantee that always-optimized-away
1134 ;; functions are actually optimized away. Thus, we skip the check
1137 ;; check to see if we know anything about the function
1138 (let ((info (info :function :info fname)))
1139 ;; if we know something, check to see if the full call was valid
1140 (when (and info (ir1-attributep (fun-info-attributes info)
1141 always-translatable))
1142 (/show (policy node speed) (policy node safety))
1143 (/show (policy node compilation-speed))
1144 (bug "full call to ~S" fname))))
1147 (aver (legal-fun-name-p fname))
1148 (destructuring-bind (setfoid &rest stem) fname
1149 (when (eq setfoid 'setf)
1150 (setf (gethash (car stem) *setf-assumed-fboundp*) t))))))
1152 ;;; If the call is in a tail recursive position and the return
1153 ;;; convention is standard, then do a tail full call. If one or fewer
1154 ;;; values are desired, then use a single-value call, otherwise use a
1155 ;;; multiple-values call.
1156 (defun ir2-convert-full-call (node block)
1157 (declare (type combination node) (type ir2-block block))
1158 (ponder-full-call node)
1159 (cond ((node-tail-p node)
1160 (ir2-convert-tail-full-call node block))
1161 ((let ((lvar (node-lvar node)))
1163 (eq (ir2-lvar-kind (lvar-info lvar)) :unknown)))
1164 (ir2-convert-multiple-full-call node block))
1166 (ir2-convert-fixed-full-call node block)))
1169 ;;;; entering functions
1171 ;;; Do all the stuff that needs to be done on XEP entry:
1172 ;;; -- Create frame.
1173 ;;; -- Copy any more arg.
1174 ;;; -- Set up the environment, accessing any closure variables.
1175 ;;; -- Move args from the standard passing locations to their internal
1177 (defun init-xep-environment (node block fun)
1178 (declare (type bind node) (type ir2-block block) (type clambda fun))
1179 (let ((start-label (entry-info-offset (leaf-info fun)))
1180 (env (physenv-info (node-physenv node))))
1181 (let ((ef (functional-entry-fun fun)))
1182 (cond ((and (optional-dispatch-p ef) (optional-dispatch-more-entry ef))
1183 ;; Special case the xep-allocate-frame + copy-more-arg case.
1184 (vop xep-allocate-frame node block start-label t)
1185 (vop copy-more-arg node block (optional-dispatch-max-args ef)))
1187 ;; No more args, so normal entry.
1188 (vop xep-allocate-frame node block start-label nil)))
1189 (if (ir2-physenv-closure env)
1190 (let ((closure (make-normal-tn *backend-t-primitive-type*)))
1191 (vop setup-closure-environment node block start-label closure)
1193 (dolist (loc (ir2-physenv-closure env))
1194 (vop closure-ref node block closure (incf n) (cdr loc)))))
1195 (vop setup-environment node block start-label)))
1197 (unless (eq (functional-kind fun) :toplevel)
1198 (let ((vars (lambda-vars fun))
1200 (when (leaf-refs (first vars))
1201 (emit-move node block (make-arg-count-location)
1202 (leaf-info (first vars))))
1203 (dolist (arg (rest vars))
1204 (when (leaf-refs arg)
1205 (let ((pass (standard-arg-location n))
1206 (home (leaf-info arg)))
1207 (if (and (lambda-var-indirect arg)
1208 (lambda-var-explicit-value-cell arg))
1209 (emit-make-value-cell node block pass home)
1210 (emit-move node block pass home))))
1213 (emit-move node block (make-old-fp-passing-location t)
1214 (ir2-physenv-old-fp env)))
1218 ;;; Emit function prolog code. This is only called on bind nodes for
1219 ;;; functions that allocate environments. All semantics of let calls
1220 ;;; are handled by IR2-CONVERT-LET.
1222 ;;; If not an XEP, all we do is move the return PC from its passing
1223 ;;; location, since in a local call, the caller allocates the frame
1224 ;;; and sets up the arguments.
1225 (defun ir2-convert-bind (node block)
1226 (declare (type bind node) (type ir2-block block))
1227 (let* ((fun (bind-lambda node))
1228 (env (physenv-info (lambda-physenv fun))))
1229 (aver (member (functional-kind fun)
1230 '(nil :external :optional :toplevel :cleanup)))
1233 (init-xep-environment node block fun)
1235 (when *collect-dynamic-statistics*
1236 (vop count-me node block *dynamic-counts-tn*
1237 (block-number (ir2-block-block block)))))
1241 (ir2-physenv-return-pc-pass env)
1242 (ir2-physenv-return-pc env))
1244 #!+unwind-to-frame-and-call-vop
1245 (when (and (lambda-allow-instrumenting fun)
1246 (not (lambda-inline-expanded fun))
1248 (policy fun (>= insert-debug-catch 2)))
1249 (vop sb!vm::bind-sentinel node block))
1251 (let ((lab (gen-label)))
1252 (setf (ir2-physenv-environment-start env) lab)
1253 (vop note-environment-start node block lab)))
1257 ;;;; function return
1259 ;;; Do stuff to return from a function with the specified values and
1260 ;;; convention. If the return convention is :FIXED and we aren't
1261 ;;; returning from an XEP, then we do a known return (letting
1262 ;;; representation selection insert the correct move-arg VOPs.)
1263 ;;; Otherwise, we use the unknown-values convention. If there is a
1264 ;;; fixed number of return values, then use RETURN, otherwise use
1265 ;;; RETURN-MULTIPLE.
1266 (defun ir2-convert-return (node block)
1267 (declare (type creturn node) (type ir2-block block))
1268 (let* ((lvar (return-result node))
1269 (2lvar (lvar-info lvar))
1270 (lvar-kind (ir2-lvar-kind 2lvar))
1271 (fun (return-lambda node))
1272 (env (physenv-info (lambda-physenv fun)))
1273 (old-fp (ir2-physenv-old-fp env))
1274 (return-pc (ir2-physenv-return-pc env))
1275 (returns (tail-set-info (lambda-tail-set fun))))
1276 #!+unwind-to-frame-and-call-vop
1277 (when (and (lambda-allow-instrumenting fun)
1278 (not (lambda-inline-expanded fun))
1279 (policy fun (>= insert-debug-catch 2)))
1280 (vop sb!vm::unbind-sentinel node block))
1282 ((and (eq (return-info-kind returns) :fixed)
1284 (let ((locs (lvar-tns node block lvar
1285 (return-info-types returns))))
1286 (vop* known-return node block
1287 (old-fp return-pc (reference-tn-list locs nil))
1289 (return-info-locations returns))))
1290 ((eq lvar-kind :fixed)
1291 (let* ((types (mapcar #'tn-primitive-type (ir2-lvar-locs 2lvar)))
1292 (lvar-locs (lvar-tns node block lvar types))
1293 (nvals (length lvar-locs))
1294 (locs (make-standard-value-tns nvals)))
1295 (mapc (lambda (val loc)
1296 (emit-move node block val loc))
1300 (vop return-single node block old-fp return-pc (car locs))
1301 (vop* return node block
1302 (old-fp return-pc (reference-tn-list locs nil))
1306 (aver (eq lvar-kind :unknown))
1307 (vop* return-multiple node block
1309 (reference-tn-list (ir2-lvar-locs 2lvar) nil))
1316 ;;;; These are used by the debugger to find the top function on the
1317 ;;;; stack. They return the OLD-FP and RETURN-PC for the current
1318 ;;;; function as multiple values.
1320 (defoptimizer (%caller-frame ir2-convert) (() node block)
1321 (let ((ir2-physenv (physenv-info (node-physenv node))))
1322 (move-lvar-result node block
1323 (list (ir2-physenv-old-fp ir2-physenv))
1326 (defoptimizer (%caller-pc ir2-convert) (() node block)
1327 (let ((ir2-physenv (physenv-info (node-physenv node))))
1328 (move-lvar-result node block
1329 (list (ir2-physenv-return-pc ir2-physenv))
1332 ;;;; multiple values
1334 ;;; This is almost identical to IR2-CONVERT-LET. Since LTN annotates
1335 ;;; the lvar for the correct number of values (with the lvar user
1336 ;;; responsible for defaulting), we can just pick them up from the
1338 (defun ir2-convert-mv-bind (node block)
1339 (declare (type mv-combination node) (type ir2-block block))
1340 (let* ((lvar (first (basic-combination-args node)))
1341 (fun (ref-leaf (lvar-uses (basic-combination-fun node))))
1342 (vars (lambda-vars fun)))
1343 (aver (eq (functional-kind fun) :mv-let))
1344 (mapc (lambda (src var)
1345 (when (leaf-refs var)
1346 (let ((dest (leaf-info var)))
1347 (if (and (lambda-var-indirect var)
1348 (lambda-var-explicit-value-cell var))
1349 (emit-make-value-cell node block src dest)
1350 (emit-move node block src dest)))))
1351 (lvar-tns node block lvar
1353 (primitive-type (leaf-type x)))
1358 ;;; Emit the appropriate fixed value, unknown value or tail variant of
1359 ;;; CALL-VARIABLE. Note that we only need to pass the values start for
1360 ;;; the first argument: all the other argument lvar TNs are
1361 ;;; ignored. This is because we require all of the values globs to be
1362 ;;; contiguous and on stack top.
1363 (defun ir2-convert-mv-call (node block)
1364 (declare (type mv-combination node) (type ir2-block block))
1365 (aver (basic-combination-args node))
1366 (let* ((start-lvar (lvar-info (first (basic-combination-args node))))
1367 (start (first (ir2-lvar-locs start-lvar)))
1368 (tails (and (node-tail-p node)
1369 (lambda-tail-set (node-home-lambda node))))
1370 (lvar (node-lvar node))
1371 (2lvar (and lvar (lvar-info lvar))))
1372 (multiple-value-bind (fun named)
1373 (fun-lvar-tn node block (basic-combination-fun node))
1374 (aver (and (not named)
1375 (eq (ir2-lvar-kind start-lvar) :unknown)))
1378 (let ((env (physenv-info (node-physenv node))))
1379 (vop tail-call-variable node block start fun
1380 (ir2-physenv-old-fp env)
1381 (ir2-physenv-return-pc env))))
1383 (eq (ir2-lvar-kind 2lvar) :unknown))
1384 (vop* multiple-call-variable node block (start fun nil)
1385 ((reference-tn-list (ir2-lvar-locs 2lvar) t))
1386 (emit-step-p node)))
1388 (let ((locs (standard-result-tns lvar)))
1389 (vop* call-variable node block (start fun nil)
1390 ((reference-tn-list locs t)) (length locs)
1392 (move-lvar-result node block locs lvar)))))))
1394 ;;; Reset the stack pointer to the start of the specified
1395 ;;; unknown-values lvar (discarding it and all values globs on top of
1397 (defoptimizer (%pop-values ir2-convert) ((%lvar) node block)
1398 (let* ((lvar (lvar-value %lvar))
1399 (2lvar (lvar-info lvar)))
1400 (cond ((eq (ir2-lvar-kind 2lvar) :unknown)
1401 (vop reset-stack-pointer node block
1402 (first (ir2-lvar-locs 2lvar))))
1403 ((lvar-dynamic-extent lvar)
1404 (vop reset-stack-pointer node block
1405 (ir2-lvar-stack-pointer 2lvar)))
1406 (t (bug "Trying to pop a not stack-allocated LVAR ~S."
1409 (defoptimizer (%nip-values ir2-convert) ((last-nipped last-preserved
1412 (let* ( ;; pointer immediately after the nipped block
1413 (after (lvar-value last-nipped))
1414 (2after (lvar-info after))
1415 ;; pointer to the first nipped word
1416 (first (lvar-value last-preserved))
1417 (2first (lvar-info first))
1419 (moved-tns (loop for lvar-ref in moved
1420 for lvar = (lvar-value lvar-ref)
1421 for 2lvar = (lvar-info lvar)
1423 collect (first (ir2-lvar-locs 2lvar)))))
1424 (aver (or (eq (ir2-lvar-kind 2after) :unknown)
1425 (lvar-dynamic-extent after)))
1426 (aver (eq (ir2-lvar-kind 2first) :unknown))
1427 (when *check-consistency*
1428 ;; we cannot move stack-allocated DX objects
1429 (dolist (moved-lvar moved)
1430 (aver (eq (ir2-lvar-kind (lvar-info (lvar-value moved-lvar)))
1432 (flet ((nip-aligned (nipped)
1433 (vop* %%nip-values node block
1435 (first (ir2-lvar-locs 2first))
1436 (reference-tn-list moved-tns nil))
1437 ((reference-tn-list moved-tns t)))))
1438 (cond ((eq (ir2-lvar-kind 2after) :unknown)
1439 (nip-aligned (first (ir2-lvar-locs 2after))))
1440 ((lvar-dynamic-extent after)
1441 (nip-aligned (ir2-lvar-stack-pointer 2after)))
1443 (bug "Trying to nip a not stack-allocated LVAR ~S." after))))))
1445 ;;; Deliver the values TNs to LVAR using MOVE-LVAR-RESULT.
1446 (defoptimizer (values ir2-convert) ((&rest values) node block)
1447 (let ((tns (mapcar (lambda (x)
1448 (lvar-tn node block x))
1450 (move-lvar-result node block tns (node-lvar node))))
1452 ;;; In the normal case where unknown values are desired, we use the
1453 ;;; VALUES-LIST VOP. In the relatively unimportant case of VALUES-LIST
1454 ;;; for a fixed number of values, we punt by doing a full call to the
1455 ;;; VALUES-LIST function. This gets the full call VOP to deal with
1456 ;;; defaulting any unsupplied values. It seems unworthwhile to
1457 ;;; optimize this case.
1458 (defoptimizer (values-list ir2-convert) ((list) node block)
1459 (let* ((lvar (node-lvar node))
1460 (2lvar (and lvar (lvar-info lvar))))
1462 (eq (ir2-lvar-kind 2lvar) :unknown))
1463 (let ((locs (ir2-lvar-locs 2lvar)))
1464 (vop* values-list node block
1465 ((lvar-tn node block list) nil)
1466 ((reference-tn-list locs t)))))
1467 (t (aver (or (not 2lvar) ; i.e. we want to check the argument
1468 (eq (ir2-lvar-kind 2lvar) :fixed)))
1469 (ir2-convert-full-call node block)))))
1471 (defoptimizer (%more-arg-values ir2-convert) ((context start count) node block)
1472 (binding* ((lvar (node-lvar node) :exit-if-null)
1473 (2lvar (lvar-info lvar)))
1474 (ecase (ir2-lvar-kind 2lvar)
1475 (:fixed (ir2-convert-full-call node block))
1477 (let ((locs (ir2-lvar-locs 2lvar)))
1478 (vop* %more-arg-values node block
1479 ((lvar-tn node block context)
1480 (lvar-tn node block start)
1481 (lvar-tn node block count)
1483 ((reference-tn-list locs t))))))))
1485 ;;;; special binding
1487 ;;; This is trivial, given our assumption of a shallow-binding
1489 (defoptimizer (%special-bind ir2-convert) ((var value) node block)
1490 (let ((name (leaf-source-name (lvar-value var))))
1491 (vop bind node block (lvar-tn node block value)
1492 (emit-constant name))))
1493 (defoptimizer (%special-unbind ir2-convert) ((var) node block)
1494 (vop unbind node block))
1496 ;;; ### It's not clear that this really belongs in this file, or
1497 ;;; should really be done this way, but this is the least violation of
1498 ;;; abstraction in the current setup. We don't want to wire
1499 ;;; shallow-binding assumptions into IR1tran.
1500 (def-ir1-translator progv
1501 ((vars vals &body body) start next result)
1504 (with-unique-names (bind unbind)
1505 (once-only ((n-save-bs '(%primitive current-binding-pointer)))
1508 (labels ((,unbind (vars)
1509 (declare (optimize (speed 2) (debug 0)))
1510 (let ((unbound-marker (%primitive make-other-immediate-type
1511 0 sb!vm:unbound-marker-widetag)))
1513 ;; CLHS says "bound and then made to have no value" -- user
1514 ;; should not be able to tell the difference between that and this.
1515 (about-to-modify-symbol-value var 'progv)
1516 (%primitive bind unbound-marker var))))
1518 (declare (optimize (speed 2) (debug 0)
1519 (insert-debug-catch 0)))
1521 ((null vals) (,unbind vars))
1523 (let ((val (car vals))
1525 (about-to-modify-symbol-value var 'progv val t)
1526 (%primitive bind val var))
1527 (,bind (cdr vars) (cdr vals))))))
1528 (,bind ,vars ,vals))
1531 ;; Technically ANSI CL doesn't allow declarations at the
1532 ;; start of the cleanup form. SBCL happens to allow for
1533 ;; them, due to the way the UNWIND-PROTECT ir1 translation
1534 ;; is implemented; the cleanup forms are directly spliced
1535 ;; into an FLET definition body. And a declaration here
1536 ;; actually has exactly the right scope for what we need
1537 ;; (ensure that debug instrumentation is not emitted for the
1538 ;; cleanup function). -- JES, 2007-06-16
1539 (declare (optimize (insert-debug-catch 0)))
1540 (%primitive unbind-to-here ,n-save-bs))))))
1544 ;;; Convert a non-local lexical exit. First find the NLX-INFO in our
1545 ;;; environment. Note that this is never called on the escape exits
1546 ;;; for CATCH and UNWIND-PROTECT, since the escape functions aren't
1548 (defun ir2-convert-exit (node block)
1549 (declare (type exit node) (type ir2-block block))
1550 (let* ((nlx (exit-nlx-info node))
1551 (loc (find-in-physenv nlx (node-physenv node)))
1552 (temp (make-stack-pointer-tn))
1553 (value (exit-value node)))
1554 (if (nlx-info-safe-p nlx)
1555 (vop value-cell-ref node block loc temp)
1556 (emit-move node block loc temp))
1558 (let ((locs (ir2-lvar-locs (lvar-info value))))
1559 (vop unwind node block temp (first locs) (second locs)))
1560 (let ((0-tn (emit-constant 0)))
1561 (vop unwind node block temp 0-tn 0-tn))))
1565 ;;; %CLEANUP-POINT doesn't do anything except prevent the body from
1566 ;;; being entirely deleted.
1567 (defoptimizer (%cleanup-point ir2-convert) (() node block) node block)
1569 ;;; This function invalidates a lexical exit on exiting from the
1570 ;;; dynamic extent. This is done by storing 0 into the indirect value
1571 ;;; cell that holds the closed unwind block.
1572 (defoptimizer (%lexical-exit-breakup ir2-convert) ((info) node block)
1573 (let ((nlx (lvar-value info)))
1574 (when (nlx-info-safe-p nlx)
1575 (vop value-cell-set node block
1576 (find-in-physenv nlx (node-physenv node))
1577 (emit-constant 0)))))
1579 ;;; We have to do a spurious move of no values to the result lvar so
1580 ;;; that lifetime analysis won't get confused.
1581 (defun ir2-convert-throw (node block)
1582 (declare (type mv-combination node) (type ir2-block block))
1583 (let ((args (basic-combination-args node)))
1584 (check-catch-tag-type (first args))
1585 (vop* throw node block
1586 ((lvar-tn node block (first args))
1588 (ir2-lvar-locs (lvar-info (second args)))
1591 (move-lvar-result node block () (node-lvar node))
1594 ;;; Emit code to set up a non-local exit. INFO is the NLX-INFO for the
1595 ;;; exit, and TAG is the lvar for the catch tag (if any.) We get at
1596 ;;; the target PC by passing in the label to the vop. The vop is
1597 ;;; responsible for building a return-PC object.
1598 (defun emit-nlx-start (node block info tag)
1599 (declare (type node node) (type ir2-block block) (type nlx-info info)
1600 (type (or lvar null) tag))
1601 (let* ((2info (nlx-info-info info))
1602 (kind (cleanup-kind (nlx-info-cleanup info)))
1603 (block-tn (physenv-live-tn
1604 (make-normal-tn (primitive-type-or-lose 'catch-block))
1605 (node-physenv node)))
1606 (res (make-stack-pointer-tn))
1607 (target-label (ir2-nlx-info-target 2info)))
1609 (vop current-binding-pointer node block
1610 (car (ir2-nlx-info-dynamic-state 2info)))
1611 (vop* save-dynamic-state node block
1613 ((reference-tn-list (cdr (ir2-nlx-info-dynamic-state 2info)) t)))
1614 (vop current-stack-pointer node block (ir2-nlx-info-save-sp 2info))
1618 (vop make-catch-block node block block-tn
1619 (lvar-tn node block tag) target-label res))
1620 ((:unwind-protect :block :tagbody)
1621 (vop make-unwind-block node block block-tn target-label res)))
1625 (if (nlx-info-safe-p info)
1626 (emit-make-value-cell node block res (ir2-nlx-info-home 2info))
1627 (emit-move node block res (ir2-nlx-info-home 2info))))
1629 (vop set-unwind-protect node block block-tn))
1634 ;;; Scan each of ENTRY's exits, setting up the exit for each lexical exit.
1635 (defun ir2-convert-entry (node block)
1636 (declare (type entry node) (type ir2-block block))
1638 (dolist (exit (entry-exits node))
1639 (let ((info (exit-nlx-info exit)))
1641 (not (memq info nlxes))
1642 (member (cleanup-kind (nlx-info-cleanup info))
1643 '(:block :tagbody)))
1645 (emit-nlx-start node block info nil)))))
1648 ;;; Set up the unwind block for these guys.
1649 (defoptimizer (%catch ir2-convert) ((info-lvar tag) node block)
1650 (check-catch-tag-type tag)
1651 (emit-nlx-start node block (lvar-value info-lvar) tag))
1652 (defoptimizer (%unwind-protect ir2-convert) ((info-lvar cleanup) node block)
1653 (emit-nlx-start node block (lvar-value info-lvar) nil))
1655 ;;; Emit the entry code for a non-local exit. We receive values and
1656 ;;; restore dynamic state.
1658 ;;; In the case of a lexical exit or CATCH, we look at the exit lvar's
1659 ;;; kind to determine which flavor of entry VOP to emit. If unknown
1660 ;;; values, emit the xxx-MULTIPLE variant to the lvar locs. If fixed
1661 ;;; values, make the appropriate number of temps in the standard
1662 ;;; values locations and use the other variant, delivering the temps
1663 ;;; to the lvar using MOVE-LVAR-RESULT.
1665 ;;; In the UNWIND-PROTECT case, we deliver the first register
1666 ;;; argument, the argument count and the argument pointer to our lvar
1667 ;;; as multiple values. These values are the block exited to and the
1668 ;;; values start and count.
1670 ;;; After receiving values, we restore dynamic state. Except in the
1671 ;;; UNWIND-PROTECT case, the values receiving restores the stack
1672 ;;; pointer. In an UNWIND-PROTECT cleanup, we want to leave the stack
1673 ;;; pointer alone, since the thrown values are still out there.
1674 (defoptimizer (%nlx-entry ir2-convert) ((info-lvar) node block)
1675 (let* ((info (lvar-value info-lvar))
1676 (lvar (node-lvar node))
1677 (2info (nlx-info-info info))
1678 (top-loc (ir2-nlx-info-save-sp 2info))
1679 (start-loc (make-nlx-entry-arg-start-location))
1680 (count-loc (make-arg-count-location))
1681 (target (ir2-nlx-info-target 2info)))
1683 (ecase (cleanup-kind (nlx-info-cleanup info))
1684 ((:catch :block :tagbody)
1685 (let ((2lvar (and lvar (lvar-info lvar))))
1686 (if (and 2lvar (eq (ir2-lvar-kind 2lvar) :unknown))
1687 (vop* nlx-entry-multiple node block
1688 (top-loc start-loc count-loc nil)
1689 ((reference-tn-list (ir2-lvar-locs 2lvar) t))
1691 (let ((locs (standard-result-tns lvar)))
1692 (vop* nlx-entry node block
1693 (top-loc start-loc count-loc nil)
1694 ((reference-tn-list locs t))
1697 (move-lvar-result node block locs lvar)))))
1699 (let ((block-loc (standard-arg-location 0)))
1700 (vop uwp-entry node block target block-loc start-loc count-loc)
1703 (list block-loc start-loc count-loc)
1707 (when *collect-dynamic-statistics*
1708 (vop count-me node block *dynamic-counts-tn*
1709 (block-number (ir2-block-block block))))
1711 (vop* restore-dynamic-state node block
1712 ((reference-tn-list (cdr (ir2-nlx-info-dynamic-state 2info)) nil))
1714 (vop unbind-to-here node block
1715 (car (ir2-nlx-info-dynamic-state 2info)))))
1717 ;;;; n-argument functions
1719 (macrolet ((def (name)
1720 `(defoptimizer (,name ir2-convert) ((&rest args) node block)
1721 (let* ((refs (move-tail-full-call-args node block))
1722 (lvar (node-lvar node))
1723 (res (lvar-result-tns
1725 (list (primitive-type (specifier-type 'list))))))
1726 (when (and lvar (lvar-dynamic-extent lvar))
1727 (vop current-stack-pointer node block
1728 (ir2-lvar-stack-pointer (lvar-info lvar))))
1729 (vop* ,name node block (refs) ((first res) nil)
1731 (move-lvar-result node block res lvar)))))
1736 ;;; Convert the code in a component into VOPs.
1737 (defun ir2-convert (component)
1738 (declare (type component component))
1739 (let (#!+sb-dyncount
1740 (*dynamic-counts-tn*
1741 (when *collect-dynamic-statistics*
1743 (block-number (block-next (component-head component))))
1744 (counts (make-array blocks
1745 :element-type '(unsigned-byte 32)
1746 :initial-element 0))
1747 (info (make-dyncount-info
1748 :for (component-name component)
1749 :costs (make-array blocks
1750 :element-type '(unsigned-byte 32)
1753 (setf (ir2-component-dyncount-info (component-info component))
1755 (emit-constant info)
1756 (emit-constant counts)))))
1758 (declare (type index num))
1759 (do-ir2-blocks (2block component)
1760 (let ((block (ir2-block-block 2block)))
1761 (when (block-start block)
1762 (setf (block-number block) num)
1764 (when *collect-dynamic-statistics*
1765 (let ((first-node (block-start-node block)))
1766 (unless (or (and (bind-p first-node)
1767 (xep-p (bind-lambda first-node)))
1769 (node-lvar first-node))
1774 #!+sb-dyncount *dynamic-counts-tn* #!-sb-dyncount nil
1776 (ir2-convert-block block)
1780 ;;; If necessary, emit a terminal unconditional branch to go to the
1781 ;;; successor block. If the successor is the component tail, then
1782 ;;; there isn't really any successor, but if the end is an unknown,
1783 ;;; non-tail call, then we emit an error trap just in case the
1784 ;;; function really does return.
1785 (defun finish-ir2-block (block)
1786 (declare (type cblock block))
1787 (let* ((2block (block-info block))
1788 (last (block-last block))
1789 (succ (block-succ block)))
1791 (aver (singleton-p succ))
1792 (let ((target (first succ)))
1793 (cond ((eq target (component-tail (block-component block)))
1794 (when (and (basic-combination-p last)
1795 (eq (basic-combination-kind last) :full))
1796 (let* ((fun (basic-combination-fun last))
1797 (use (lvar-uses fun))
1798 (name (and (ref-p use)
1799 (leaf-has-source-name-p (ref-leaf use))
1800 (leaf-source-name (ref-leaf use)))))
1801 (unless (or (node-tail-p last)
1802 (info :function :info name)
1803 (policy last (zerop safety)))
1804 (vop nil-fun-returned-error last 2block
1806 (emit-constant name)
1807 (multiple-value-bind (tn named)
1808 (fun-lvar-tn last 2block fun)
1811 ((not (eq (ir2-block-next 2block) (block-info target)))
1812 (vop branch last 2block (block-label target)))))))
1816 ;;; Convert the code in a block into VOPs.
1817 (defun ir2-convert-block (block)
1818 (declare (type cblock block))
1819 (let ((2block (block-info block)))
1820 (do-nodes (node lvar block)
1824 (let ((2lvar (lvar-info lvar)))
1825 ;; function REF in a local call is not annotated
1826 (when (and 2lvar (not (eq (ir2-lvar-kind 2lvar) :delayed)))
1827 (ir2-convert-ref node 2block)))))
1829 (let ((kind (basic-combination-kind node)))
1832 (ir2-convert-local-call node 2block))
1834 (ir2-convert-full-call node 2block))
1836 (let* ((info (basic-combination-fun-info node))
1837 (fun (fun-info-ir2-convert info)))
1839 (funcall fun node 2block))
1840 ((eq (basic-combination-info node) :full)
1841 (ir2-convert-full-call node 2block))
1843 (ir2-convert-template node 2block))))))))
1845 (when (lvar-info (if-test node))
1846 (ir2-convert-if node 2block)))
1848 (let ((fun (bind-lambda node)))
1849 (when (eq (lambda-home fun) fun)
1850 (ir2-convert-bind node 2block))))
1852 (ir2-convert-return node 2block))
1854 (ir2-convert-set node 2block))
1856 (ir2-convert-cast node 2block))
1859 ((eq (basic-combination-kind node) :local)
1860 (ir2-convert-mv-bind node 2block))
1861 ((eq (lvar-fun-name (basic-combination-fun node))
1863 (ir2-convert-throw node 2block))
1865 (ir2-convert-mv-call node 2block))))
1867 (when (exit-entry node)
1868 (ir2-convert-exit node 2block)))
1870 (ir2-convert-entry node 2block)))))
1872 (finish-ir2-block block)