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.
786 (defun emit-psetq-moves (node block fun old-fp)
787 (declare (type combination node) (type ir2-block block) (type clambda fun)
788 (type (or tn null) old-fp))
789 (let ((actuals (mapcar (lambda (x)
791 (lvar-tn node block x)))
792 (combination-args node))))
795 (dolist (var (lambda-vars fun))
796 (let ((actual (pop actuals))
797 (loc (leaf-info var)))
800 ((and (lambda-var-indirect var)
801 (lambda-var-explicit-value-cell var))
803 (make-normal-tn *backend-t-primitive-type*)))
804 (emit-make-value-cell node block actual temp)
806 ((member actual (locs))
807 (let ((temp (make-normal-tn (tn-primitive-type loc))))
808 (emit-move node block actual temp)
815 (let ((this-1env (node-physenv node))
816 (called-env (physenv-info (lambda-physenv fun))))
817 (dolist (thing (ir2-physenv-closure called-env))
818 (temps (closure-initial-value (car thing) this-1env old-fp))
821 (locs (ir2-physenv-old-fp called-env))))
823 (values (temps) (locs)))))
825 ;;; A tail-recursive local call is done by emitting moves of stuff
826 ;;; into the appropriate passing locations. After setting up the args
827 ;;; and environment, we just move our return-pc into the called
828 ;;; function's passing location.
829 (defun ir2-convert-tail-local-call (node block fun)
830 (declare (type combination node) (type ir2-block block) (type clambda fun))
831 (let ((this-env (physenv-info (node-physenv node))))
832 (multiple-value-bind (temps locs)
833 (emit-psetq-moves node block fun (ir2-physenv-old-fp this-env))
835 (mapc (lambda (temp loc)
836 (emit-move node block temp loc))
839 (emit-move node block
840 (ir2-physenv-return-pc this-env)
841 (ir2-physenv-return-pc-pass
843 (lambda-physenv fun)))))
847 ;;; Convert an :ASSIGNMENT call. This is just like a tail local call,
848 ;;; except that the caller and callee environment are the same, so we
849 ;;; don't need to mess with the environment locations, return PC, etc.
850 (defun ir2-convert-assignment (node block fun)
851 (declare (type combination node) (type ir2-block block) (type clambda fun))
852 (multiple-value-bind (temps locs) (emit-psetq-moves node block fun nil)
854 (mapc (lambda (temp loc)
855 (emit-move node block temp loc))
859 ;;; Do stuff to set up the arguments to a non-tail local call
860 ;;; (including implicit environment args.) We allocate a frame
861 ;;; (returning the FP and NFP), and also compute the TN-REFS list for
862 ;;; the values to pass and the list of passing location TNs.
863 (defun ir2-convert-local-call-args (node block fun)
864 (declare (type combination node) (type ir2-block block) (type clambda fun))
865 (let ((fp (make-stack-pointer-tn))
866 (nfp (make-number-stack-pointer-tn))
867 (old-fp (make-stack-pointer-tn)))
868 (multiple-value-bind (temps locs)
869 (emit-psetq-moves node block fun old-fp)
870 (vop current-fp node block old-fp)
871 (vop allocate-frame node block
872 (physenv-info (lambda-physenv fun))
874 (values fp nfp temps (mapcar #'make-alias-tn locs)))))
876 ;;; Handle a non-TR known-values local call. We emit the call, then
877 ;;; move the results to the lvar's destination.
878 (defun ir2-convert-local-known-call (node block fun returns lvar start)
879 (declare (type node node) (type ir2-block block) (type clambda fun)
880 (type return-info returns) (type (or lvar null) lvar)
882 (multiple-value-bind (fp nfp temps arg-locs)
883 (ir2-convert-local-call-args node block fun)
884 (let ((locs (return-info-locations returns)))
885 (vop* known-call-local node block
886 (fp nfp (reference-tn-list temps nil))
887 ((reference-tn-list locs t))
888 arg-locs (physenv-info (lambda-physenv fun)) start)
889 (move-lvar-result node block locs lvar)))
892 ;;; Handle a non-TR unknown-values local call. We do different things
893 ;;; depending on what kind of values the lvar wants.
895 ;;; If LVAR is :UNKNOWN, then we use the "multiple-" variant, directly
896 ;;; specifying the lvar's LOCS as the VOP results so that we don't
897 ;;; have to do anything after the call.
899 ;;; Otherwise, we use STANDARD-RESULT-TNS to get wired result TNs, and
900 ;;; then call MOVE-LVAR-RESULT to do any necessary type checks or
902 (defun ir2-convert-local-unknown-call (node block fun lvar start)
903 (declare (type node node) (type ir2-block block) (type clambda fun)
904 (type (or lvar null) lvar) (type label start))
905 (multiple-value-bind (fp nfp temps arg-locs)
906 (ir2-convert-local-call-args node block fun)
907 (let ((2lvar (and lvar (lvar-info lvar)))
908 (env (physenv-info (lambda-physenv fun)))
909 (temp-refs (reference-tn-list temps nil)))
910 (if (and 2lvar (eq (ir2-lvar-kind 2lvar) :unknown))
911 (vop* multiple-call-local node block (fp nfp temp-refs)
912 ((reference-tn-list (ir2-lvar-locs 2lvar) t))
914 (let ((locs (standard-result-tns lvar)))
915 (vop* call-local node block
917 ((reference-tn-list locs t))
918 arg-locs env start (length locs))
919 (move-lvar-result node block locs lvar)))))
922 ;;; Dispatch to the appropriate function, depending on whether we have
923 ;;; a let, tail or normal call. If the function doesn't return, call
924 ;;; it using the unknown-value convention. We could compile it as a
925 ;;; tail call, but that might seem confusing in the debugger.
926 (defun ir2-convert-local-call (node block)
927 (declare (type combination node) (type ir2-block block))
928 (let* ((fun (ref-leaf (lvar-uses (basic-combination-fun node))))
929 (kind (functional-kind fun)))
930 (cond ((eq kind :let)
931 (ir2-convert-let node block fun))
932 ((eq kind :assignment)
933 (ir2-convert-assignment node block fun))
935 (ir2-convert-tail-local-call node block fun))
937 (let ((start (block-label (lambda-block fun)))
938 (returns (tail-set-info (lambda-tail-set fun)))
939 (lvar (node-lvar node)))
941 (return-info-kind returns)
944 (ir2-convert-local-unknown-call node block fun lvar start))
946 (ir2-convert-local-known-call node block fun returns
952 ;;; Given a function lvar FUN, return (VALUES TN-TO-CALL NAMED-P),
953 ;;; where TN-TO-CALL is a TN holding the thing that we call NAMED-P is
954 ;;; true if the thing is named (false if it is a function).
956 ;;; There are two interesting non-named cases:
957 ;;; -- We know it's a function. No check needed: return the
959 ;;; -- We don't know what it is.
960 (defun fun-lvar-tn (node block lvar)
961 (declare (ignore node block))
962 (declare (type lvar lvar))
963 (let ((2lvar (lvar-info lvar)))
964 (if (eq (ir2-lvar-kind 2lvar) :delayed)
965 (let ((name (lvar-fun-name lvar t)))
967 (values (make-load-time-constant-tn :fdefinition name) t))
968 (let* ((locs (ir2-lvar-locs 2lvar))
970 (function-ptype (primitive-type-or-lose 'function)))
971 (aver (and (eq (ir2-lvar-kind 2lvar) :fixed)
972 (= (length locs) 1)))
973 (aver (eq (tn-primitive-type loc) function-ptype))
976 ;;; Set up the args to NODE in the current frame, and return a TN-REF
977 ;;; list for the passing locations.
978 (defun move-tail-full-call-args (node block)
979 (declare (type combination node) (type ir2-block block))
980 (let ((args (basic-combination-args node))
983 (dotimes (num (length args))
984 (let ((loc (standard-arg-location num)))
985 (emit-move node block (lvar-tn node block (elt args num)) loc)
986 (let ((ref (reference-tn loc nil)))
988 (setf (tn-ref-across last) ref)
993 ;;; Move the arguments into the passing locations and do a (possibly
994 ;;; named) tail call.
995 (defun ir2-convert-tail-full-call (node block)
996 (declare (type combination node) (type ir2-block block))
997 (let* ((env (physenv-info (node-physenv node)))
998 (args (basic-combination-args node))
999 (nargs (length args))
1000 (pass-refs (move-tail-full-call-args node block))
1001 (old-fp (ir2-physenv-old-fp env))
1002 (return-pc (ir2-physenv-return-pc env)))
1004 (multiple-value-bind (fun-tn named)
1005 (fun-lvar-tn node block (basic-combination-fun node))
1007 (vop* tail-call-named node block
1008 (fun-tn old-fp return-pc pass-refs)
1012 (vop* tail-call node block
1013 (fun-tn old-fp return-pc pass-refs)
1016 (emit-step-p node)))))
1020 ;;; like IR2-CONVERT-LOCAL-CALL-ARGS, only different
1021 (defun ir2-convert-full-call-args (node block)
1022 (declare (type combination node) (type ir2-block block))
1023 (let* ((args (basic-combination-args node))
1024 (fp (make-stack-pointer-tn))
1025 (nargs (length args)))
1026 (vop allocate-full-call-frame node block nargs fp)
1030 (dotimes (num nargs)
1031 (locs (standard-arg-location num))
1032 (let ((ref (reference-tn (lvar-tn node block (elt args num))
1035 (setf (tn-ref-across last) ref)
1039 (values fp first (locs) nargs)))))
1041 ;;; Do full call when a fixed number of values are desired. We make
1042 ;;; STANDARD-RESULT-TNS for our lvar, then deliver the result using
1043 ;;; MOVE-LVAR-RESULT. We do named or normal call, as appropriate.
1044 (defun ir2-convert-fixed-full-call (node block)
1045 (declare (type combination node) (type ir2-block block))
1046 (multiple-value-bind (fp args arg-locs nargs)
1047 (ir2-convert-full-call-args node block)
1048 (let* ((lvar (node-lvar node))
1049 (locs (standard-result-tns lvar))
1050 (loc-refs (reference-tn-list locs t))
1051 (nvals (length locs)))
1052 (multiple-value-bind (fun-tn named)
1053 (fun-lvar-tn node block (basic-combination-fun node))
1055 (vop* call-named node block (fp fun-tn args) (loc-refs)
1056 arg-locs nargs nvals (emit-step-p node))
1057 (vop* call node block (fp fun-tn args) (loc-refs)
1058 arg-locs nargs nvals (emit-step-p node)))
1059 (move-lvar-result node block locs lvar))))
1062 ;;; Do full call when unknown values are desired.
1063 (defun ir2-convert-multiple-full-call (node block)
1064 (declare (type combination node) (type ir2-block block))
1065 (multiple-value-bind (fp args arg-locs nargs)
1066 (ir2-convert-full-call-args node block)
1067 (let* ((lvar (node-lvar node))
1068 (locs (ir2-lvar-locs (lvar-info lvar)))
1069 (loc-refs (reference-tn-list locs t)))
1070 (multiple-value-bind (fun-tn named)
1071 (fun-lvar-tn node block (basic-combination-fun node))
1073 (vop* multiple-call-named node block (fp fun-tn args) (loc-refs)
1074 arg-locs nargs (emit-step-p node))
1075 (vop* multiple-call node block (fp fun-tn args) (loc-refs)
1076 arg-locs nargs (emit-step-p node))))))
1079 ;;; stuff to check in PONDER-FULL-CALL
1081 ;;; These came in handy when troubleshooting cold boot after making
1082 ;;; major changes in the package structure: various transforms and
1083 ;;; VOPs and stuff got attached to the wrong symbol, so that
1084 ;;; references to the right symbol were bogusly translated as full
1085 ;;; calls instead of primitives, sending the system off into infinite
1086 ;;; space. Having a report on all full calls generated makes it easier
1087 ;;; to figure out what form caused the problem this time.
1088 #!+sb-show (defvar *show-full-called-fnames-p* nil)
1089 #!+sb-show (defvar *full-called-fnames* (make-hash-table :test 'equal))
1091 ;;; Do some checks (and store some notes relevant for future checks)
1093 ;;; * Is this a full call to something we have reason to know should
1094 ;;; never be full called? (Except as of sbcl-0.7.18 or so, we no
1095 ;;; longer try to ensure this behavior when *FAILURE-P* has already
1097 ;;; * Is this a full call to (SETF FOO) which might conflict with
1098 ;;; a DEFSETF or some such thing elsewhere in the program?
1099 (defun ponder-full-call (node)
1100 (let* ((lvar (basic-combination-fun node))
1101 (fname (lvar-fun-name lvar t)))
1102 (declare (type (or symbol cons) fname))
1104 #!+sb-show (unless (gethash fname *full-called-fnames*)
1105 (setf (gethash fname *full-called-fnames*) t))
1106 #!+sb-show (when *show-full-called-fnames-p*
1107 (/show "converting full call to named function" fname)
1108 (/show (basic-combination-args node))
1109 (/show (policy node speed) (policy node safety))
1110 (/show (policy node compilation-speed))
1111 (let ((arg-types (mapcar (lambda (lvar)
1115 (basic-combination-args node))))
1118 ;; When illegal code is compiled, all sorts of perverse paths
1119 ;; through the compiler can be taken, and it's much harder -- and
1120 ;; probably pointless -- to guarantee that always-optimized-away
1121 ;; functions are actually optimized away. Thus, we skip the check
1124 ;; check to see if we know anything about the function
1125 (let ((info (info :function :info fname)))
1126 ;; if we know something, check to see if the full call was valid
1127 (when (and info (ir1-attributep (fun-info-attributes info)
1128 always-translatable))
1129 (/show (policy node speed) (policy node safety))
1130 (/show (policy node compilation-speed))
1131 (bug "full call to ~S" fname))))
1134 (aver (legal-fun-name-p fname))
1135 (destructuring-bind (setfoid &rest stem) fname
1136 (when (eq setfoid 'setf)
1137 (setf (gethash (car stem) *setf-assumed-fboundp*) t))))))
1139 ;;; If the call is in a tail recursive position and the return
1140 ;;; convention is standard, then do a tail full call. If one or fewer
1141 ;;; values are desired, then use a single-value call, otherwise use a
1142 ;;; multiple-values call.
1143 (defun ir2-convert-full-call (node block)
1144 (declare (type combination node) (type ir2-block block))
1145 (ponder-full-call node)
1146 (cond ((node-tail-p node)
1147 (ir2-convert-tail-full-call node block))
1148 ((let ((lvar (node-lvar node)))
1150 (eq (ir2-lvar-kind (lvar-info lvar)) :unknown)))
1151 (ir2-convert-multiple-full-call node block))
1153 (ir2-convert-fixed-full-call node block)))
1156 ;;;; entering functions
1158 ;;; Do all the stuff that needs to be done on XEP entry:
1159 ;;; -- Create frame.
1160 ;;; -- Copy any more arg.
1161 ;;; -- Set up the environment, accessing any closure variables.
1162 ;;; -- Move args from the standard passing locations to their internal
1164 (defun init-xep-environment (node block fun)
1165 (declare (type bind node) (type ir2-block block) (type clambda fun))
1166 (let ((start-label (entry-info-offset (leaf-info fun)))
1167 (env (physenv-info (node-physenv node))))
1168 (let ((ef (functional-entry-fun fun)))
1169 (cond ((and (optional-dispatch-p ef) (optional-dispatch-more-entry ef))
1170 ;; Special case the xep-allocate-frame + copy-more-arg case.
1171 (vop xep-allocate-frame node block start-label t)
1172 (vop copy-more-arg node block (optional-dispatch-max-args ef)))
1174 ;; No more args, so normal entry.
1175 (vop xep-allocate-frame node block start-label nil)))
1176 (if (ir2-physenv-closure env)
1177 (let ((closure (make-normal-tn *backend-t-primitive-type*)))
1178 (vop setup-closure-environment node block start-label closure)
1180 (dolist (loc (ir2-physenv-closure env))
1181 (vop closure-ref node block closure (incf n) (cdr loc)))))
1182 (vop setup-environment node block start-label)))
1184 (unless (eq (functional-kind fun) :toplevel)
1185 (let ((vars (lambda-vars fun))
1187 (when (leaf-refs (first vars))
1188 (emit-move node block (make-arg-count-location)
1189 (leaf-info (first vars))))
1190 (dolist (arg (rest vars))
1191 (when (leaf-refs arg)
1192 (let ((pass (standard-arg-location n))
1193 (home (leaf-info arg)))
1194 (if (and (lambda-var-indirect arg)
1195 (lambda-var-explicit-value-cell arg))
1196 (emit-make-value-cell node block pass home)
1197 (emit-move node block pass home))))
1200 (emit-move node block (make-old-fp-passing-location t)
1201 (ir2-physenv-old-fp env)))
1205 ;;; Emit function prolog code. This is only called on bind nodes for
1206 ;;; functions that allocate environments. All semantics of let calls
1207 ;;; are handled by IR2-CONVERT-LET.
1209 ;;; If not an XEP, all we do is move the return PC from its passing
1210 ;;; location, since in a local call, the caller allocates the frame
1211 ;;; and sets up the arguments.
1212 (defun ir2-convert-bind (node block)
1213 (declare (type bind node) (type ir2-block block))
1214 (let* ((fun (bind-lambda node))
1215 (env (physenv-info (lambda-physenv fun))))
1216 (aver (member (functional-kind fun)
1217 '(nil :external :optional :toplevel :cleanup)))
1220 (init-xep-environment node block fun)
1222 (when *collect-dynamic-statistics*
1223 (vop count-me node block *dynamic-counts-tn*
1224 (block-number (ir2-block-block block)))))
1228 (ir2-physenv-return-pc-pass env)
1229 (ir2-physenv-return-pc env))
1231 #!+unwind-to-frame-and-call-vop
1232 (when (and (lambda-allow-instrumenting fun)
1233 (not (lambda-inline-expanded fun))
1235 (policy fun (>= insert-debug-catch 2)))
1236 (vop sb!vm::bind-sentinel node block))
1238 (let ((lab (gen-label)))
1239 (setf (ir2-physenv-environment-start env) lab)
1240 (vop note-environment-start node block lab)))
1244 ;;;; function return
1246 ;;; Do stuff to return from a function with the specified values and
1247 ;;; convention. If the return convention is :FIXED and we aren't
1248 ;;; returning from an XEP, then we do a known return (letting
1249 ;;; representation selection insert the correct move-arg VOPs.)
1250 ;;; Otherwise, we use the unknown-values convention. If there is a
1251 ;;; fixed number of return values, then use RETURN, otherwise use
1252 ;;; RETURN-MULTIPLE.
1253 (defun ir2-convert-return (node block)
1254 (declare (type creturn node) (type ir2-block block))
1255 (let* ((lvar (return-result node))
1256 (2lvar (lvar-info lvar))
1257 (lvar-kind (ir2-lvar-kind 2lvar))
1258 (fun (return-lambda node))
1259 (env (physenv-info (lambda-physenv fun)))
1260 (old-fp (ir2-physenv-old-fp env))
1261 (return-pc (ir2-physenv-return-pc env))
1262 (returns (tail-set-info (lambda-tail-set fun))))
1263 #!+unwind-to-frame-and-call-vop
1264 (when (and (lambda-allow-instrumenting fun)
1265 (not (lambda-inline-expanded fun))
1266 (policy fun (>= insert-debug-catch 2)))
1267 (vop sb!vm::unbind-sentinel node block))
1269 ((and (eq (return-info-kind returns) :fixed)
1271 (let ((locs (lvar-tns node block lvar
1272 (return-info-types returns))))
1273 (vop* known-return node block
1274 (old-fp return-pc (reference-tn-list locs nil))
1276 (return-info-locations returns))))
1277 ((eq lvar-kind :fixed)
1278 (let* ((types (mapcar #'tn-primitive-type (ir2-lvar-locs 2lvar)))
1279 (lvar-locs (lvar-tns node block lvar types))
1280 (nvals (length lvar-locs))
1281 (locs (make-standard-value-tns nvals)))
1282 (mapc (lambda (val loc)
1283 (emit-move node block val loc))
1287 (vop return-single node block old-fp return-pc (car locs))
1288 (vop* return node block
1289 (old-fp return-pc (reference-tn-list locs nil))
1293 (aver (eq lvar-kind :unknown))
1294 (vop* return-multiple node block
1296 (reference-tn-list (ir2-lvar-locs 2lvar) nil))
1303 ;;;; These are used by the debugger to find the top function on the
1304 ;;;; stack. They return the OLD-FP and RETURN-PC for the current
1305 ;;;; function as multiple values.
1307 (defoptimizer (%caller-frame ir2-convert) (() node block)
1308 (let ((ir2-physenv (physenv-info (node-physenv node))))
1309 (move-lvar-result node block
1310 (list (ir2-physenv-old-fp ir2-physenv))
1313 (defoptimizer (%caller-pc ir2-convert) (() node block)
1314 (let ((ir2-physenv (physenv-info (node-physenv node))))
1315 (move-lvar-result node block
1316 (list (ir2-physenv-return-pc ir2-physenv))
1319 ;;;; multiple values
1321 ;;; This is almost identical to IR2-CONVERT-LET. Since LTN annotates
1322 ;;; the lvar for the correct number of values (with the lvar user
1323 ;;; responsible for defaulting), we can just pick them up from the
1325 (defun ir2-convert-mv-bind (node block)
1326 (declare (type mv-combination node) (type ir2-block block))
1327 (let* ((lvar (first (basic-combination-args node)))
1328 (fun (ref-leaf (lvar-uses (basic-combination-fun node))))
1329 (vars (lambda-vars fun)))
1330 (aver (eq (functional-kind fun) :mv-let))
1331 (mapc (lambda (src var)
1332 (when (leaf-refs var)
1333 (let ((dest (leaf-info var)))
1334 (if (and (lambda-var-indirect var)
1335 (lambda-var-explicit-value-cell var))
1336 (emit-make-value-cell node block src dest)
1337 (emit-move node block src dest)))))
1338 (lvar-tns node block lvar
1340 (primitive-type (leaf-type x)))
1345 ;;; Emit the appropriate fixed value, unknown value or tail variant of
1346 ;;; CALL-VARIABLE. Note that we only need to pass the values start for
1347 ;;; the first argument: all the other argument lvar TNs are
1348 ;;; ignored. This is because we require all of the values globs to be
1349 ;;; contiguous and on stack top.
1350 (defun ir2-convert-mv-call (node block)
1351 (declare (type mv-combination node) (type ir2-block block))
1352 (aver (basic-combination-args node))
1353 (let* ((start-lvar (lvar-info (first (basic-combination-args node))))
1354 (start (first (ir2-lvar-locs start-lvar)))
1355 (tails (and (node-tail-p node)
1356 (lambda-tail-set (node-home-lambda node))))
1357 (lvar (node-lvar node))
1358 (2lvar (and lvar (lvar-info lvar))))
1359 (multiple-value-bind (fun named)
1360 (fun-lvar-tn node block (basic-combination-fun node))
1361 (aver (and (not named)
1362 (eq (ir2-lvar-kind start-lvar) :unknown)))
1365 (let ((env (physenv-info (node-physenv node))))
1366 (vop tail-call-variable node block start fun
1367 (ir2-physenv-old-fp env)
1368 (ir2-physenv-return-pc env))))
1370 (eq (ir2-lvar-kind 2lvar) :unknown))
1371 (vop* multiple-call-variable node block (start fun nil)
1372 ((reference-tn-list (ir2-lvar-locs 2lvar) t))
1373 (emit-step-p node)))
1375 (let ((locs (standard-result-tns lvar)))
1376 (vop* call-variable node block (start fun nil)
1377 ((reference-tn-list locs t)) (length locs)
1379 (move-lvar-result node block locs lvar)))))))
1381 ;;; Reset the stack pointer to the start of the specified
1382 ;;; unknown-values lvar (discarding it and all values globs on top of
1384 (defoptimizer (%pop-values ir2-convert) ((%lvar) node block)
1385 (let* ((lvar (lvar-value %lvar))
1386 (2lvar (lvar-info lvar)))
1387 (cond ((eq (ir2-lvar-kind 2lvar) :unknown)
1388 (vop reset-stack-pointer node block
1389 (first (ir2-lvar-locs 2lvar))))
1390 ((lvar-dynamic-extent lvar)
1391 (vop reset-stack-pointer node block
1392 (ir2-lvar-stack-pointer 2lvar)))
1393 (t (bug "Trying to pop a not stack-allocated LVAR ~S."
1396 (defoptimizer (%nip-values ir2-convert) ((last-nipped last-preserved
1399 (let* ( ;; pointer immediately after the nipped block
1400 (after (lvar-value last-nipped))
1401 (2after (lvar-info after))
1402 ;; pointer to the first nipped word
1403 (first (lvar-value last-preserved))
1404 (2first (lvar-info first))
1406 (moved-tns (loop for lvar-ref in moved
1407 for lvar = (lvar-value lvar-ref)
1408 for 2lvar = (lvar-info lvar)
1410 collect (first (ir2-lvar-locs 2lvar)))))
1411 (aver (or (eq (ir2-lvar-kind 2after) :unknown)
1412 (lvar-dynamic-extent after)))
1413 (aver (eq (ir2-lvar-kind 2first) :unknown))
1414 (when *check-consistency*
1415 ;; we cannot move stack-allocated DX objects
1416 (dolist (moved-lvar moved)
1417 (aver (eq (ir2-lvar-kind (lvar-info (lvar-value moved-lvar)))
1419 (flet ((nip-aligned (nipped)
1420 (vop* %%nip-values node block
1422 (first (ir2-lvar-locs 2first))
1423 (reference-tn-list moved-tns nil))
1424 ((reference-tn-list moved-tns t)))))
1425 (cond ((eq (ir2-lvar-kind 2after) :unknown)
1426 (nip-aligned (first (ir2-lvar-locs 2after))))
1427 ((lvar-dynamic-extent after)
1428 (nip-aligned (ir2-lvar-stack-pointer 2after)))
1430 (bug "Trying to nip a not stack-allocated LVAR ~S." after))))))
1432 ;;; Deliver the values TNs to LVAR using MOVE-LVAR-RESULT.
1433 (defoptimizer (values ir2-convert) ((&rest values) node block)
1434 (let ((tns (mapcar (lambda (x)
1435 (lvar-tn node block x))
1437 (move-lvar-result node block tns (node-lvar node))))
1439 ;;; In the normal case where unknown values are desired, we use the
1440 ;;; VALUES-LIST VOP. In the relatively unimportant case of VALUES-LIST
1441 ;;; for a fixed number of values, we punt by doing a full call to the
1442 ;;; VALUES-LIST function. This gets the full call VOP to deal with
1443 ;;; defaulting any unsupplied values. It seems unworthwhile to
1444 ;;; optimize this case.
1445 (defoptimizer (values-list ir2-convert) ((list) node block)
1446 (let* ((lvar (node-lvar node))
1447 (2lvar (and lvar (lvar-info lvar))))
1449 (eq (ir2-lvar-kind 2lvar) :unknown))
1450 (let ((locs (ir2-lvar-locs 2lvar)))
1451 (vop* values-list node block
1452 ((lvar-tn node block list) nil)
1453 ((reference-tn-list locs t)))))
1454 (t (aver (or (not 2lvar) ; i.e. we want to check the argument
1455 (eq (ir2-lvar-kind 2lvar) :fixed)))
1456 (ir2-convert-full-call node block)))))
1458 (defoptimizer (%more-arg-values ir2-convert) ((context start count) node block)
1459 (binding* ((lvar (node-lvar node) :exit-if-null)
1460 (2lvar (lvar-info lvar)))
1461 (ecase (ir2-lvar-kind 2lvar)
1462 (:fixed (ir2-convert-full-call node block))
1464 (let ((locs (ir2-lvar-locs 2lvar)))
1465 (vop* %more-arg-values node block
1466 ((lvar-tn node block context)
1467 (lvar-tn node block start)
1468 (lvar-tn node block count)
1470 ((reference-tn-list locs t))))))))
1472 ;;;; special binding
1474 ;;; This is trivial, given our assumption of a shallow-binding
1476 (defoptimizer (%special-bind ir2-convert) ((var value) node block)
1477 (let ((name (leaf-source-name (lvar-value var))))
1478 (vop bind node block (lvar-tn node block value)
1479 (emit-constant name))))
1480 (defoptimizer (%special-unbind ir2-convert) ((var) node block)
1481 (vop unbind node block))
1483 ;;; ### It's not clear that this really belongs in this file, or
1484 ;;; should really be done this way, but this is the least violation of
1485 ;;; abstraction in the current setup. We don't want to wire
1486 ;;; shallow-binding assumptions into IR1tran.
1487 (def-ir1-translator progv
1488 ((vars vals &body body) start next result)
1491 (with-unique-names (bind unbind)
1492 (once-only ((n-save-bs '(%primitive current-binding-pointer)))
1495 (labels ((,unbind (vars)
1496 (declare (optimize (speed 2) (debug 0)))
1497 (let ((unbound-marker (%primitive make-other-immediate-type
1498 0 sb!vm:unbound-marker-widetag)))
1500 ;; CLHS says "bound and then made to have no value" -- user
1501 ;; should not be able to tell the difference between that and this.
1502 (about-to-modify-symbol-value var 'progv)
1503 (%primitive bind unbound-marker var))))
1505 (declare (optimize (speed 2) (debug 0)
1506 (insert-debug-catch 0)))
1508 ((null vals) (,unbind vars))
1510 (let ((val (car vals))
1512 (about-to-modify-symbol-value var 'progv val t)
1513 (%primitive bind val var))
1514 (,bind (cdr vars) (cdr vals))))))
1515 (,bind ,vars ,vals))
1518 ;; Technically ANSI CL doesn't allow declarations at the
1519 ;; start of the cleanup form. SBCL happens to allow for
1520 ;; them, due to the way the UNWIND-PROTECT ir1 translation
1521 ;; is implemented; the cleanup forms are directly spliced
1522 ;; into an FLET definition body. And a declaration here
1523 ;; actually has exactly the right scope for what we need
1524 ;; (ensure that debug instrumentation is not emitted for the
1525 ;; cleanup function). -- JES, 2007-06-16
1526 (declare (optimize (insert-debug-catch 0)))
1527 (%primitive unbind-to-here ,n-save-bs))))))
1531 ;;; Convert a non-local lexical exit. First find the NLX-INFO in our
1532 ;;; environment. Note that this is never called on the escape exits
1533 ;;; for CATCH and UNWIND-PROTECT, since the escape functions aren't
1535 (defun ir2-convert-exit (node block)
1536 (declare (type exit node) (type ir2-block block))
1537 (let* ((nlx (exit-nlx-info node))
1538 (loc (find-in-physenv nlx (node-physenv node)))
1539 (temp (make-stack-pointer-tn))
1540 (value (exit-value node)))
1541 (if (nlx-info-safe-p nlx)
1542 (vop value-cell-ref node block loc temp)
1543 (emit-move node block loc temp))
1545 (let ((locs (ir2-lvar-locs (lvar-info value))))
1546 (vop unwind node block temp (first locs) (second locs)))
1547 (let ((0-tn (emit-constant 0)))
1548 (vop unwind node block temp 0-tn 0-tn))))
1552 ;;; %CLEANUP-POINT doesn't do anything except prevent the body from
1553 ;;; being entirely deleted.
1554 (defoptimizer (%cleanup-point ir2-convert) (() node block) node block)
1556 ;;; This function invalidates a lexical exit on exiting from the
1557 ;;; dynamic extent. This is done by storing 0 into the indirect value
1558 ;;; cell that holds the closed unwind block.
1559 (defoptimizer (%lexical-exit-breakup ir2-convert) ((info) node block)
1560 (let ((nlx (lvar-value info)))
1561 (when (nlx-info-safe-p nlx)
1562 (vop value-cell-set node block
1563 (find-in-physenv nlx (node-physenv node))
1564 (emit-constant 0)))))
1566 ;;; We have to do a spurious move of no values to the result lvar so
1567 ;;; that lifetime analysis won't get confused.
1568 (defun ir2-convert-throw (node block)
1569 (declare (type mv-combination node) (type ir2-block block))
1570 (let ((args (basic-combination-args node)))
1571 (check-catch-tag-type (first args))
1572 (vop* throw node block
1573 ((lvar-tn node block (first args))
1575 (ir2-lvar-locs (lvar-info (second args)))
1578 (move-lvar-result node block () (node-lvar node))
1581 ;;; Emit code to set up a non-local exit. INFO is the NLX-INFO for the
1582 ;;; exit, and TAG is the lvar for the catch tag (if any.) We get at
1583 ;;; the target PC by passing in the label to the vop. The vop is
1584 ;;; responsible for building a return-PC object.
1585 (defun emit-nlx-start (node block info tag)
1586 (declare (type node node) (type ir2-block block) (type nlx-info info)
1587 (type (or lvar null) tag))
1588 (let* ((2info (nlx-info-info info))
1589 (kind (cleanup-kind (nlx-info-cleanup info)))
1590 (block-tn (physenv-live-tn
1591 (make-normal-tn (primitive-type-or-lose 'catch-block))
1592 (node-physenv node)))
1593 (res (make-stack-pointer-tn))
1594 (target-label (ir2-nlx-info-target 2info)))
1596 (vop current-binding-pointer node block
1597 (car (ir2-nlx-info-dynamic-state 2info)))
1598 (vop* save-dynamic-state node block
1600 ((reference-tn-list (cdr (ir2-nlx-info-dynamic-state 2info)) t)))
1601 (vop current-stack-pointer node block (ir2-nlx-info-save-sp 2info))
1605 (vop make-catch-block node block block-tn
1606 (lvar-tn node block tag) target-label res))
1607 ((:unwind-protect :block :tagbody)
1608 (vop make-unwind-block node block block-tn target-label res)))
1612 (if (nlx-info-safe-p info)
1613 (emit-make-value-cell node block res (ir2-nlx-info-home 2info))
1614 (emit-move node block res (ir2-nlx-info-home 2info))))
1616 (vop set-unwind-protect node block block-tn))
1621 ;;; Scan each of ENTRY's exits, setting up the exit for each lexical exit.
1622 (defun ir2-convert-entry (node block)
1623 (declare (type entry node) (type ir2-block block))
1625 (dolist (exit (entry-exits node))
1626 (let ((info (exit-nlx-info exit)))
1628 (not (memq info nlxes))
1629 (member (cleanup-kind (nlx-info-cleanup info))
1630 '(:block :tagbody)))
1632 (emit-nlx-start node block info nil)))))
1635 ;;; Set up the unwind block for these guys.
1636 (defoptimizer (%catch ir2-convert) ((info-lvar tag) node block)
1637 (check-catch-tag-type tag)
1638 (emit-nlx-start node block (lvar-value info-lvar) tag))
1639 (defoptimizer (%unwind-protect ir2-convert) ((info-lvar cleanup) node block)
1640 (emit-nlx-start node block (lvar-value info-lvar) nil))
1642 ;;; Emit the entry code for a non-local exit. We receive values and
1643 ;;; restore dynamic state.
1645 ;;; In the case of a lexical exit or CATCH, we look at the exit lvar's
1646 ;;; kind to determine which flavor of entry VOP to emit. If unknown
1647 ;;; values, emit the xxx-MULTIPLE variant to the lvar locs. If fixed
1648 ;;; values, make the appropriate number of temps in the standard
1649 ;;; values locations and use the other variant, delivering the temps
1650 ;;; to the lvar using MOVE-LVAR-RESULT.
1652 ;;; In the UNWIND-PROTECT case, we deliver the first register
1653 ;;; argument, the argument count and the argument pointer to our lvar
1654 ;;; as multiple values. These values are the block exited to and the
1655 ;;; values start and count.
1657 ;;; After receiving values, we restore dynamic state. Except in the
1658 ;;; UNWIND-PROTECT case, the values receiving restores the stack
1659 ;;; pointer. In an UNWIND-PROTECT cleanup, we want to leave the stack
1660 ;;; pointer alone, since the thrown values are still out there.
1661 (defoptimizer (%nlx-entry ir2-convert) ((info-lvar) node block)
1662 (let* ((info (lvar-value info-lvar))
1663 (lvar (node-lvar node))
1664 (2info (nlx-info-info info))
1665 (top-loc (ir2-nlx-info-save-sp 2info))
1666 (start-loc (make-nlx-entry-arg-start-location))
1667 (count-loc (make-arg-count-location))
1668 (target (ir2-nlx-info-target 2info)))
1670 (ecase (cleanup-kind (nlx-info-cleanup info))
1671 ((:catch :block :tagbody)
1672 (let ((2lvar (and lvar (lvar-info lvar))))
1673 (if (and 2lvar (eq (ir2-lvar-kind 2lvar) :unknown))
1674 (vop* nlx-entry-multiple node block
1675 (top-loc start-loc count-loc nil)
1676 ((reference-tn-list (ir2-lvar-locs 2lvar) t))
1678 (let ((locs (standard-result-tns lvar)))
1679 (vop* nlx-entry node block
1680 (top-loc start-loc count-loc nil)
1681 ((reference-tn-list locs t))
1684 (move-lvar-result node block locs lvar)))))
1686 (let ((block-loc (standard-arg-location 0)))
1687 (vop uwp-entry node block target block-loc start-loc count-loc)
1690 (list block-loc start-loc count-loc)
1694 (when *collect-dynamic-statistics*
1695 (vop count-me node block *dynamic-counts-tn*
1696 (block-number (ir2-block-block block))))
1698 (vop* restore-dynamic-state node block
1699 ((reference-tn-list (cdr (ir2-nlx-info-dynamic-state 2info)) nil))
1701 (vop unbind-to-here node block
1702 (car (ir2-nlx-info-dynamic-state 2info)))))
1704 ;;;; n-argument functions
1706 (macrolet ((def (name)
1707 `(defoptimizer (,name ir2-convert) ((&rest args) node block)
1708 (let* ((refs (move-tail-full-call-args node block))
1709 (lvar (node-lvar node))
1710 (res (lvar-result-tns
1712 (list (primitive-type (specifier-type 'list))))))
1713 (when (and lvar (lvar-dynamic-extent lvar))
1714 (vop current-stack-pointer node block
1715 (ir2-lvar-stack-pointer (lvar-info lvar))))
1716 (vop* ,name node block (refs) ((first res) nil)
1718 (move-lvar-result node block res lvar)))))
1723 ;;; Convert the code in a component into VOPs.
1724 (defun ir2-convert (component)
1725 (declare (type component component))
1726 (let (#!+sb-dyncount
1727 (*dynamic-counts-tn*
1728 (when *collect-dynamic-statistics*
1730 (block-number (block-next (component-head component))))
1731 (counts (make-array blocks
1732 :element-type '(unsigned-byte 32)
1733 :initial-element 0))
1734 (info (make-dyncount-info
1735 :for (component-name component)
1736 :costs (make-array blocks
1737 :element-type '(unsigned-byte 32)
1740 (setf (ir2-component-dyncount-info (component-info component))
1742 (emit-constant info)
1743 (emit-constant counts)))))
1745 (declare (type index num))
1746 (do-ir2-blocks (2block component)
1747 (let ((block (ir2-block-block 2block)))
1748 (when (block-start block)
1749 (setf (block-number block) num)
1751 (when *collect-dynamic-statistics*
1752 (let ((first-node (block-start-node block)))
1753 (unless (or (and (bind-p first-node)
1754 (xep-p (bind-lambda first-node)))
1756 (node-lvar first-node))
1761 #!+sb-dyncount *dynamic-counts-tn* #!-sb-dyncount nil
1763 (ir2-convert-block block)
1767 ;;; If necessary, emit a terminal unconditional branch to go to the
1768 ;;; successor block. If the successor is the component tail, then
1769 ;;; there isn't really any successor, but if the end is an unknown,
1770 ;;; non-tail call, then we emit an error trap just in case the
1771 ;;; function really does return.
1772 (defun finish-ir2-block (block)
1773 (declare (type cblock block))
1774 (let* ((2block (block-info block))
1775 (last (block-last block))
1776 (succ (block-succ block)))
1778 (aver (singleton-p succ))
1779 (let ((target (first succ)))
1780 (cond ((eq target (component-tail (block-component block)))
1781 (when (and (basic-combination-p last)
1782 (eq (basic-combination-kind last) :full))
1783 (let* ((fun (basic-combination-fun last))
1784 (use (lvar-uses fun))
1785 (name (and (ref-p use)
1786 (leaf-has-source-name-p (ref-leaf use))
1787 (leaf-source-name (ref-leaf use)))))
1788 (unless (or (node-tail-p last)
1789 (info :function :info name)
1790 (policy last (zerop safety)))
1791 (vop nil-fun-returned-error last 2block
1793 (emit-constant name)
1794 (multiple-value-bind (tn named)
1795 (fun-lvar-tn last 2block fun)
1798 ((not (eq (ir2-block-next 2block) (block-info target)))
1799 (vop branch last 2block (block-label target)))))))
1803 ;;; Convert the code in a block into VOPs.
1804 (defun ir2-convert-block (block)
1805 (declare (type cblock block))
1806 (let ((2block (block-info block)))
1807 (do-nodes (node lvar block)
1811 (let ((2lvar (lvar-info lvar)))
1812 ;; function REF in a local call is not annotated
1813 (when (and 2lvar (not (eq (ir2-lvar-kind 2lvar) :delayed)))
1814 (ir2-convert-ref node 2block)))))
1816 (let ((kind (basic-combination-kind node)))
1819 (ir2-convert-local-call node 2block))
1821 (ir2-convert-full-call node 2block))
1823 (let* ((info (basic-combination-fun-info node))
1824 (fun (fun-info-ir2-convert info)))
1826 (funcall fun node 2block))
1827 ((eq (basic-combination-info node) :full)
1828 (ir2-convert-full-call node 2block))
1830 (ir2-convert-template node 2block))))))))
1832 (when (lvar-info (if-test node))
1833 (ir2-convert-if node 2block)))
1835 (let ((fun (bind-lambda node)))
1836 (when (eq (lambda-home fun) fun)
1837 (ir2-convert-bind node 2block))))
1839 (ir2-convert-return node 2block))
1841 (ir2-convert-set node 2block))
1843 (ir2-convert-cast node 2block))
1846 ((eq (basic-combination-kind node) :local)
1847 (ir2-convert-mv-bind node 2block))
1848 ((eq (lvar-fun-name (basic-combination-fun node))
1850 (ir2-convert-throw node 2block))
1852 (ir2-convert-mv-call node 2block))))
1854 (when (exit-entry node)
1855 (ir2-convert-exit node 2block)))
1857 (ir2-convert-entry node 2block)))))
1859 (finish-ir2-block block)