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. Maybe do some clever stack
56 ;;; allocation someday.
57 (defevent make-value-cell-event "Allocate heap value cell for lexical var.")
58 (defun emit-make-value-cell (node block value res)
59 (event make-value-cell-event node)
60 (vop make-value-cell node block value res))
64 ;;; Return the TN that holds the value of THING in the environment ENV.
65 (declaim (ftype (function ((or nlx-info lambda-var clambda) physenv) tn)
67 (defun find-in-physenv (thing physenv)
68 (or (cdr (assoc thing (ir2-physenv-closure (physenv-info physenv))))
71 ;; I think that a failure of this assertion means that we're
72 ;; trying to access a variable which was improperly closed
73 ;; over. The PHYSENV describes a physical environment. Every
74 ;; variable that a form refers to should either be in its
75 ;; physical environment directly, or grabbed from a
76 ;; surrounding physical environment when it was closed over.
77 ;; The ASSOC expression above finds closed-over variables, so
78 ;; if we fell through the ASSOC expression, it wasn't closed
79 ;; over. Therefore, it must be in our physical environment
80 ;; directly. If instead it is in some other physical
81 ;; environment, then it's bogus for us to reference it here
82 ;; without it being closed over. -- WHN 2001-09-29
83 (aver (eq physenv (lambda-physenv (lambda-var-home thing))))
86 (aver (eq physenv (block-physenv (nlx-info-target thing))))
87 (ir2-nlx-info-home (nlx-info-info thing)))
90 (entry-info-closure-tn (lambda-info thing))))
91 (bug "~@<~2I~_~S ~_not found in ~_~S~:>" thing physenv)))
93 ;;; If LEAF already has a constant TN, return that, otherwise make a
95 (defun constant-tn (leaf)
96 (declare (type constant leaf))
98 (setf (leaf-info leaf)
99 (make-constant-tn leaf))))
101 ;;; Return a TN that represents the value of LEAF, or NIL if LEAF
102 ;;; isn't directly represented by a TN. ENV is the environment that
103 ;;; the reference is done in.
104 (defun leaf-tn (leaf env)
105 (declare (type leaf leaf) (type physenv env))
108 (unless (lambda-var-indirect leaf)
109 (find-in-physenv leaf env)))
110 (constant (constant-tn leaf))
113 ;;; This is used to conveniently get a handle on a constant TN during
114 ;;; IR2 conversion. It returns a constant TN representing the Lisp
116 (defun emit-constant (value)
117 (constant-tn (find-constant value)))
119 ;;; Convert a REF node. The reference must not be delayed.
120 (defun ir2-convert-ref (node block)
121 (declare (type ref node) (type ir2-block block))
122 (let* ((lvar (node-lvar node))
123 (leaf (ref-leaf node))
124 (locs (lvar-result-tns
125 lvar (list (primitive-type (leaf-type leaf)))))
129 (let ((tn (find-in-physenv leaf (node-physenv node))))
130 (if (lambda-var-indirect leaf)
131 (vop value-cell-ref node block tn res)
132 (emit-move node block tn res))))
134 (if (legal-immediate-constant-p leaf)
135 (emit-move node block (constant-tn leaf) res)
136 (let* ((name (leaf-source-name leaf))
137 (name-tn (emit-constant name)))
138 (if (policy node (zerop safety))
139 (vop fast-symbol-value node block name-tn res)
140 (vop symbol-value node block name-tn 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)))
151 (vop fast-symbol-value node block name-tn res)
152 (vop symbol-value node block name-tn res))))
154 (let ((fdefn-tn (make-load-time-constant-tn :fdefinition name)))
156 (vop fdefn-fun node block fdefn-tn res)
157 (vop safe-fdefn-fun node block fdefn-tn res))))))))
158 (move-lvar-result node block locs lvar))
161 ;;; some sanity checks for a CLAMBDA passed to IR2-CONVERT-CLOSURE
162 (defun assertions-on-ir2-converted-clambda (clambda)
163 ;; This assertion was sort of an experiment. It would be nice and
164 ;; sane and easier to understand things if it were *always* true,
165 ;; but experimentally I observe that it's only *almost* always
166 ;; true. -- WHN 2001-01-02
168 (aver (eql (lambda-component clambda)
169 (block-component (ir2-block-block ir2-block))))
170 ;; Check for some weirdness which came up in bug
173 ;; The MAKE-LOAD-TIME-CONSTANT-TN call above puts an :ENTRY record
174 ;; into the IR2-COMPONENT-CONSTANTS table. The dump-a-COMPONENT
176 ;; * treats every HANDLEless :ENTRY record into a
178 ;; * expects every patch to correspond to an
179 ;; IR2-COMPONENT-ENTRIES record.
180 ;; The IR2-COMPONENT-ENTRIES records are set by ENTRY-ANALYZE
181 ;; walking over COMPONENT-LAMBDAS. Bug 138b arose because there
182 ;; was a HANDLEless :ENTRY record which didn't correspond to an
183 ;; IR2-COMPONENT-ENTRIES record. That problem is hard to debug
184 ;; when it's caught at dump time, so this assertion tries to catch
186 (aver (member clambda
187 (component-lambdas (lambda-component clambda))))
188 ;; another bug-138-related issue: COMPONENT-NEW-FUNCTIONALS is
189 ;; used as a queue for stuff pending to do in IR1, and now that
190 ;; we're doing IR2 it should've been completely flushed (but
192 (aver (null (component-new-functionals (lambda-component clambda))))
195 ;;; Emit code to load a function object implementing FUNCTIONAL into
196 ;;; RES. This gets interesting when the referenced function is a
197 ;;; closure: we must make the closure and move the closed-over values
200 ;;; FUNCTIONAL is either a :TOPLEVEL-XEP functional or the XEP lambda
201 ;;; for the called function, since local call analysis converts all
202 ;;; closure references. If a :TOPLEVEL-XEP, we know it is not a
205 ;;; If a closed-over LAMBDA-VAR has no refs (is deleted), then we
206 ;;; don't initialize that slot. This can happen with closures over
207 ;;; top level variables, where optimization of the closure deleted the
208 ;;; variable. Since we committed to the closure format when we
209 ;;; pre-analyzed the top level code, we just leave an empty slot.
210 (defun ir2-convert-closure (ref ir2-block functional res)
211 (declare (type ref ref)
212 (type ir2-block ir2-block)
213 (type functional functional)
215 (aver (not (eql (functional-kind functional) :deleted)))
216 (unless (leaf-info functional)
217 (setf (leaf-info functional)
218 (make-entry-info :name (functional-debug-name functional))))
219 (let ((closure (etypecase functional
221 (assertions-on-ir2-converted-clambda functional)
222 (physenv-closure (get-lambda-physenv functional)))
224 (aver (eq (functional-kind functional) :toplevel-xep))
228 (let* ((physenv (node-physenv ref))
229 (tn (find-in-physenv functional physenv)))
230 (emit-move ref ir2-block tn res)))
232 (let ((entry (make-load-time-constant-tn :entry functional)))
233 (emit-move ref ir2-block entry res)))))
236 (defoptimizer (%allocate-closures ltn-annotate) ((leaves) node ltn-policy)
237 ltn-policy ; a hack to effectively (DECLARE (IGNORE LTN-POLICY))
238 (when (lvar-dynamic-extent leaves)
239 (let ((info (make-ir2-lvar *backend-t-primitive-type*)))
240 (setf (ir2-lvar-kind info) :delayed)
241 (setf (lvar-info leaves) info)
242 (setf (ir2-lvar-stack-pointer info)
243 (make-stack-pointer-tn)))))
245 (defoptimizer (%allocate-closures ir2-convert) ((leaves) call 2block)
246 (let ((dx-p (lvar-dynamic-extent leaves)))
249 (vop current-stack-pointer call 2block
250 (ir2-lvar-stack-pointer (lvar-info leaves))))
251 (dolist (leaf (lvar-value leaves))
252 (binding* ((xep (functional-entry-fun leaf) :exit-if-null)
253 (nil (aver (xep-p xep)))
254 (entry-info (lambda-info xep) :exit-if-null)
255 (tn (entry-info-closure-tn entry-info) :exit-if-null)
256 (closure (physenv-closure (get-lambda-physenv xep)))
257 (entry (make-load-time-constant-tn :entry xep)))
258 (let ((this-env (node-physenv call))
259 (leaf-dx-p (and dx-p (leaf-dynamic-extent leaf))))
260 (vop make-closure call 2block entry (length closure)
262 (loop for what in closure and n from 0 do
263 (unless (and (lambda-var-p what)
264 (null (leaf-refs what)))
265 ;; In LABELS a closure may refer to another closure
266 ;; in the same group, so we must be sure that we
267 ;; store a closure only after its creation.
269 ;; TODO: Here is a simple solution: we postpone
270 ;; putting of all closures after all creations
271 ;; (though it may require more registers).
273 (delayed (list tn (find-in-physenv what this-env) n))
274 (vop closure-init call 2block
276 (find-in-physenv what this-env)
278 (loop for (tn what n) in (delayed)
279 do (vop closure-init call 2block
283 ;;; Convert a SET node. If the NODE's LVAR is annotated, then we also
284 ;;; deliver the value to that lvar. If the var is a lexical variable
285 ;;; with no refs, then we don't actually set anything, since the
286 ;;; variable has been deleted.
287 (defun ir2-convert-set (node block)
288 (declare (type cset node) (type ir2-block block))
289 (let* ((lvar (node-lvar node))
290 (leaf (set-var node))
291 (val (lvar-tn node block (set-value node)))
294 lvar (list (primitive-type (leaf-type leaf))))
298 (when (leaf-refs leaf)
299 (let ((tn (find-in-physenv leaf (node-physenv node))))
300 (if (lambda-var-indirect leaf)
301 (vop value-cell-set node block tn val)
302 (emit-move node block val tn)))))
304 (ecase (global-var-kind leaf)
306 (aver (symbolp (leaf-source-name leaf)))
307 (vop set node block (emit-constant (leaf-source-name leaf)) val)))))
309 (emit-move node block val (first locs))
310 (move-lvar-result node block locs lvar)))
313 ;;;; utilities for receiving fixed values
315 ;;; Return a TN that can be referenced to get the value of LVAR. LVAR
316 ;;; must be LTN-ANNOTATED either as a delayed leaf ref or as a fixed,
317 ;;; single-value lvar.
319 ;;; The primitive-type of the result will always be the same as the
320 ;;; IR2-LVAR-PRIMITIVE-TYPE, ensuring that VOPs are always called with
321 ;;; TNs that satisfy the operand primitive-type restriction. We may
322 ;;; have to make a temporary of the desired type and move the actual
323 ;;; lvar TN into it. This happens when we delete a type check in
324 ;;; unsafe code or when we locally know something about the type of an
325 ;;; argument variable.
326 (defun lvar-tn (node block lvar)
327 (declare (type node node) (type ir2-block block) (type lvar lvar))
328 (let* ((2lvar (lvar-info lvar))
330 (ecase (ir2-lvar-kind 2lvar)
332 (let ((ref (lvar-uses lvar)))
333 (leaf-tn (ref-leaf ref) (node-physenv ref))))
335 (aver (= (length (ir2-lvar-locs 2lvar)) 1))
336 (first (ir2-lvar-locs 2lvar)))))
337 (ptype (ir2-lvar-primitive-type 2lvar)))
339 (cond ((eq (tn-primitive-type lvar-tn) ptype) lvar-tn)
341 (let ((temp (make-normal-tn ptype)))
342 (emit-move node block lvar-tn temp)
345 ;;; This is similar to LVAR-TN, but hacks multiple values. We return
346 ;;; TNs holding the values of LVAR with PTYPES as their primitive
347 ;;; types. LVAR must be annotated for the same number of fixed values
348 ;;; are there are PTYPES.
350 ;;; If the lvar has a type check, check the values into temps and
351 ;;; return the temps. When we have more values than assertions, we
352 ;;; move the extra values with no check.
353 (defun lvar-tns (node block lvar ptypes)
354 (declare (type node node) (type ir2-block block)
355 (type lvar lvar) (list ptypes))
356 (let* ((locs (ir2-lvar-locs (lvar-info lvar)))
357 (nlocs (length locs)))
358 (aver (= nlocs (length ptypes)))
360 (mapcar (lambda (from to-type)
361 (if (eq (tn-primitive-type from) to-type)
363 (let ((temp (make-normal-tn to-type)))
364 (emit-move node block from temp)
369 ;;;; utilities for delivering values to lvars
371 ;;; Return a list of TNs with the specifier TYPES that can be used as
372 ;;; result TNs to evaluate an expression into LVAR. This is used
373 ;;; together with MOVE-LVAR-RESULT to deliver fixed values to
376 ;;; If the lvar isn't annotated (meaning the values are discarded) or
377 ;;; is unknown-values, the then we make temporaries for each supplied
378 ;;; value, providing a place to compute the result in until we decide
379 ;;; what to do with it (if anything.)
381 ;;; If the lvar is fixed-values, and wants the same number of values
382 ;;; as the user wants to deliver, then we just return the
383 ;;; IR2-LVAR-LOCS. Otherwise we make a new list padded as necessary by
384 ;;; discarded TNs. We always return a TN of the specified type, using
385 ;;; the lvar locs only when they are of the correct type.
386 (defun lvar-result-tns (lvar types)
387 (declare (type (or lvar null) lvar) (type list types))
389 (mapcar #'make-normal-tn types)
390 (let ((2lvar (lvar-info lvar)))
391 (ecase (ir2-lvar-kind 2lvar)
393 (let* ((locs (ir2-lvar-locs 2lvar))
394 (nlocs (length locs))
395 (ntypes (length types)))
396 (if (and (= nlocs ntypes)
397 (do ((loc locs (cdr loc))
398 (type types (cdr type)))
400 (unless (eq (tn-primitive-type (car loc)) (car type))
403 (mapcar (lambda (loc type)
404 (if (eq (tn-primitive-type loc) type)
406 (make-normal-tn type)))
409 (mapcar #'make-normal-tn
410 (subseq types nlocs)))
414 (mapcar #'make-normal-tn types))))))
416 ;;; Make the first N standard value TNs, returning them in a list.
417 (defun make-standard-value-tns (n)
418 (declare (type unsigned-byte n))
421 (res (standard-arg-location i)))
424 ;;; Return a list of TNs wired to the standard value passing
425 ;;; conventions that can be used to receive values according to the
426 ;;; unknown-values convention. This is used with together
427 ;;; MOVE-LVAR-RESULT for delivering unknown values to a fixed values
430 ;;; If the lvar isn't annotated, then we treat as 0-values, returning
431 ;;; an empty list of temporaries.
433 ;;; If the lvar is annotated, then it must be :FIXED.
434 (defun standard-result-tns (lvar)
435 (declare (type (or lvar null) lvar))
437 (let ((2lvar (lvar-info lvar)))
438 (ecase (ir2-lvar-kind 2lvar)
440 (make-standard-value-tns (length (ir2-lvar-locs 2lvar))))))
443 ;;; Just move each SRC TN into the corresponding DEST TN, defaulting
444 ;;; any unsupplied source values to NIL. We let EMIT-MOVE worry about
445 ;;; doing the appropriate coercions.
446 (defun move-results-coerced (node block src dest)
447 (declare (type node node) (type ir2-block block) (list src dest))
448 (let ((nsrc (length src))
449 (ndest (length dest)))
450 (mapc (lambda (from to)
452 (emit-move node block from to)))
454 (append src (make-list (- ndest nsrc)
455 :initial-element (emit-constant nil)))
460 ;;; Move each SRC TN into the corresponding DEST TN, checking types
461 ;;; and defaulting any unsupplied source values to NIL
462 (defun move-results-checked (node block src dest types)
463 (declare (type node node) (type ir2-block block) (list src dest types))
464 (let ((nsrc (length src))
465 (ndest (length dest))
466 (ntypes (length types)))
467 (mapc (lambda (from to type)
469 (emit-type-check node block from to type)
470 (emit-move node block from to)))
472 (append src (make-list (- ndest nsrc)
473 :initial-element (emit-constant nil)))
477 (append types (make-list (- ndest ntypes)))
481 ;;; If necessary, emit coercion code needed to deliver the RESULTS to
482 ;;; the specified lvar. NODE and BLOCK provide context for emitting
483 ;;; code. Although usually obtained from STANDARD-RESULT-TNs or
484 ;;; LVAR-RESULT-TNs, RESULTS my be a list of any type or
487 ;;; If the lvar is fixed values, then move the results into the lvar
488 ;;; locations. If the lvar is unknown values, then do the moves into
489 ;;; the standard value locations, and use PUSH-VALUES to put the
490 ;;; values on the stack.
491 (defun move-lvar-result (node block results lvar)
492 (declare (type node node) (type ir2-block block)
493 (list results) (type (or lvar null) lvar))
495 (let ((2lvar (lvar-info lvar)))
496 (ecase (ir2-lvar-kind 2lvar)
498 (let ((locs (ir2-lvar-locs 2lvar)))
499 (unless (eq locs results)
500 (move-results-coerced node block results locs))))
502 (let* ((nvals (length results))
503 (locs (make-standard-value-tns nvals)))
504 (move-results-coerced node block results locs)
505 (vop* push-values node block
506 ((reference-tn-list locs nil))
507 ((reference-tn-list (ir2-lvar-locs 2lvar) t))
512 (defun ir2-convert-cast (node block)
513 (declare (type cast node)
514 (type ir2-block block))
515 (binding* ((lvar (node-lvar node) :exit-if-null)
516 (2lvar (lvar-info lvar))
517 (value (cast-value node))
518 (2value (lvar-info value)))
519 (cond ((eq (ir2-lvar-kind 2lvar) :unused))
520 ((eq (ir2-lvar-kind 2lvar) :unknown)
521 (aver (eq (ir2-lvar-kind 2value) :unknown))
522 (aver (not (cast-type-check node)))
523 (move-results-coerced node block
524 (ir2-lvar-locs 2value)
525 (ir2-lvar-locs 2lvar)))
526 ((eq (ir2-lvar-kind 2lvar) :fixed)
527 (aver (eq (ir2-lvar-kind 2value) :fixed))
528 (if (cast-type-check node)
529 (move-results-checked node block
530 (ir2-lvar-locs 2value)
531 (ir2-lvar-locs 2lvar)
532 (multiple-value-bind (check types)
533 (cast-check-types node nil)
534 (aver (eq check :simple))
536 (move-results-coerced node block
537 (ir2-lvar-locs 2value)
538 (ir2-lvar-locs 2lvar))))
539 (t (bug "CAST cannot be :DELAYED.")))))
541 ;;;; template conversion
543 ;;; Build a TN-REFS list that represents access to the values of the
544 ;;; specified list of lvars ARGS for TEMPLATE. Any :CONSTANT arguments
545 ;;; are returned in the second value as a list rather than being
546 ;;; accessed as a normal argument. NODE and BLOCK provide the context
547 ;;; for emitting any necessary type-checking code.
548 (defun reference-args (node block args template)
549 (declare (type node node) (type ir2-block block) (list args)
550 (type template template))
551 (collect ((info-args))
554 (do ((args args (cdr args))
555 (types (template-arg-types template) (cdr types)))
557 (let ((type (first types))
559 (if (and (consp type) (eq (car type) ':constant))
560 (info-args (lvar-value arg))
561 (let ((ref (reference-tn (lvar-tn node block arg) nil)))
563 (setf (tn-ref-across last) ref)
567 (values (the (or tn-ref null) first) (info-args)))))
569 ;;; Convert a conditional template. We try to exploit any
570 ;;; drop-through, but emit an unconditional branch afterward if we
571 ;;; fail. NOT-P is true if the sense of the TEMPLATE's test should be
573 (defun ir2-convert-conditional (node block template args info-args if not-p)
574 (declare (type node node) (type ir2-block block)
575 (type template template) (type (or tn-ref null) args)
576 (list info-args) (type cif if) (type boolean not-p))
577 (aver (= (template-info-arg-count template) (+ (length info-args) 2)))
578 (let ((consequent (if-consequent if))
579 (alternative (if-alternative if)))
580 (cond ((drop-thru-p if consequent)
581 (emit-template node block template args nil
582 (list* (block-label alternative) (not not-p)
585 (emit-template node block template args nil
586 (list* (block-label consequent) not-p info-args))
587 (unless (drop-thru-p if alternative)
588 (vop branch node block (block-label alternative)))))))
590 ;;; Convert an IF that isn't the DEST of a conditional template.
591 (defun ir2-convert-if (node block)
592 (declare (type ir2-block block) (type cif node))
593 (let* ((test (if-test node))
594 (test-ref (reference-tn (lvar-tn node block test) nil))
595 (nil-ref (reference-tn (emit-constant nil) nil)))
596 (setf (tn-ref-across test-ref) nil-ref)
597 (ir2-convert-conditional node block (template-or-lose 'if-eq)
598 test-ref () node t)))
600 ;;; Return a list of primitive-types that we can pass to
601 ;;; LVAR-RESULT-TNS describing the result types we want for a
602 ;;; template call. We duplicate here the determination of output type
603 ;;; that was done in initially selecting the template, so we know that
604 ;;; the types we find are allowed by the template output type
606 (defun find-template-result-types (call template rtypes)
607 (declare (type combination call)
608 (type template template) (list rtypes))
609 (declare (ignore template))
610 (let* ((dtype (node-derived-type call))
612 (types (mapcar #'primitive-type
613 (if (values-type-p type)
614 (append (values-type-required type)
615 (values-type-optional type))
617 (let ((nvals (length rtypes))
618 (ntypes (length types)))
619 (cond ((< ntypes nvals)
621 (make-list (- nvals ntypes)
622 :initial-element *backend-t-primitive-type*)))
624 (subseq types 0 nvals))
628 ;;; Return a list of TNs usable in a CALL to TEMPLATE delivering
629 ;;; values to LVAR. As an efficiency hack, we pick off the common case
630 ;;; where the LVAR is fixed values and has locations that satisfy the
631 ;;; result restrictions. This can fail when there is a type check or a
632 ;;; values count mismatch.
633 (defun make-template-result-tns (call lvar template rtypes)
634 (declare (type combination call) (type (or lvar null) lvar)
635 (type template template) (list rtypes))
636 (let ((2lvar (when lvar (lvar-info lvar))))
637 (if (and 2lvar (eq (ir2-lvar-kind 2lvar) :fixed))
638 (let ((locs (ir2-lvar-locs 2lvar)))
639 (if (and (= (length rtypes) (length locs))
640 (do ((loc locs (cdr loc))
641 (rtype rtypes (cdr rtype)))
643 (unless (operand-restriction-ok
645 (tn-primitive-type (car loc))
651 (find-template-result-types call template rtypes))))
654 (find-template-result-types call template rtypes)))))
656 ;;; Get the operands into TNs, make TN-REFs for them, and then call
657 ;;; the template emit function.
658 (defun ir2-convert-template (call block)
659 (declare (type combination call) (type ir2-block block))
660 (let* ((template (combination-info call))
661 (lvar (node-lvar call))
662 (rtypes (template-result-types template)))
663 (multiple-value-bind (args info-args)
664 (reference-args call block (combination-args call) template)
665 (aver (not (template-more-results-type template)))
666 (if (eq rtypes :conditional)
667 (ir2-convert-conditional call block template args info-args
668 (lvar-dest lvar) nil)
669 (let* ((results (make-template-result-tns call lvar template rtypes))
670 (r-refs (reference-tn-list results t)))
671 (aver (= (length info-args)
672 (template-info-arg-count template)))
673 (when (and lvar (lvar-dynamic-extent lvar))
674 (vop current-stack-pointer call block
675 (ir2-lvar-stack-pointer (lvar-info lvar))))
676 (when (emit-step-p call)
677 (vop sb!vm::step-instrument-before-vop call block))
679 (emit-template call block template args r-refs info-args)
680 (emit-template call block template args r-refs))
681 (move-lvar-result call block results lvar)))))
684 ;;; We don't have to do much because operand count checking is done by
685 ;;; IR1 conversion. The only difference between this and the function
686 ;;; case of IR2-CONVERT-TEMPLATE is that there can be codegen-info
688 (defoptimizer (%%primitive ir2-convert) ((template info &rest args) call block)
689 (let* ((template (lvar-value template))
690 (info (lvar-value info))
691 (lvar (node-lvar call))
692 (rtypes (template-result-types template))
693 (results (make-template-result-tns call lvar template rtypes))
694 (r-refs (reference-tn-list results t)))
695 (multiple-value-bind (args info-args)
696 (reference-args call block (cddr (combination-args call)) template)
697 (aver (not (template-more-results-type template)))
698 (aver (not (eq rtypes :conditional)))
699 (aver (null info-args))
702 (emit-template call block template args r-refs info)
703 (emit-template call block template args r-refs))
705 (move-lvar-result call block results lvar)))
710 ;;; Convert a LET by moving the argument values into the variables.
711 ;;; Since a LET doesn't have any passing locations, we move the
712 ;;; arguments directly into the variables. We must also allocate any
713 ;;; indirect value cells, since there is no function prologue to do
715 (defun ir2-convert-let (node block fun)
716 (declare (type combination node) (type ir2-block block) (type clambda fun))
717 (mapc (lambda (var arg)
719 (let ((src (lvar-tn node block arg))
720 (dest (leaf-info var)))
721 (if (lambda-var-indirect var)
722 (emit-make-value-cell node block src dest)
723 (emit-move node block src dest)))))
724 (lambda-vars fun) (basic-combination-args node))
727 ;;; Emit any necessary moves into assignment temps for a local call to
728 ;;; FUN. We return two lists of TNs: TNs holding the actual argument
729 ;;; values, and (possibly EQ) TNs that are the actual destination of
730 ;;; the arguments. When necessary, we allocate temporaries for
731 ;;; arguments to preserve parallel assignment semantics. These lists
732 ;;; exclude unused arguments and include implicit environment
733 ;;; arguments, i.e. they exactly correspond to the arguments passed.
735 ;;; OLD-FP is the TN currently holding the value we want to pass as
736 ;;; OLD-FP. If null, then the call is to the same environment (an
737 ;;; :ASSIGNMENT), so we only move the arguments, and leave the
738 ;;; environment alone.
739 (defun emit-psetq-moves (node block fun old-fp)
740 (declare (type combination node) (type ir2-block block) (type clambda fun)
741 (type (or tn null) old-fp))
742 (let ((actuals (mapcar (lambda (x)
744 (lvar-tn node block x)))
745 (combination-args node))))
748 (dolist (var (lambda-vars fun))
749 (let ((actual (pop actuals))
750 (loc (leaf-info var)))
753 ((lambda-var-indirect var)
755 (make-normal-tn *backend-t-primitive-type*)))
756 (emit-make-value-cell node block actual temp)
758 ((member actual (locs))
759 (let ((temp (make-normal-tn (tn-primitive-type loc))))
760 (emit-move node block actual temp)
767 (let ((this-1env (node-physenv node))
768 (called-env (physenv-info (lambda-physenv fun))))
769 (dolist (thing (ir2-physenv-closure called-env))
770 (temps (find-in-physenv (car thing) this-1env))
773 (locs (ir2-physenv-old-fp called-env))))
775 (values (temps) (locs)))))
777 ;;; A tail-recursive local call is done by emitting moves of stuff
778 ;;; into the appropriate passing locations. After setting up the args
779 ;;; and environment, we just move our return-pc into the called
780 ;;; function's passing location.
781 (defun ir2-convert-tail-local-call (node block fun)
782 (declare (type combination node) (type ir2-block block) (type clambda fun))
783 (let ((this-env (physenv-info (node-physenv node))))
784 (multiple-value-bind (temps locs)
785 (emit-psetq-moves node block fun (ir2-physenv-old-fp this-env))
787 (mapc (lambda (temp loc)
788 (emit-move node block temp loc))
791 (emit-move node block
792 (ir2-physenv-return-pc this-env)
793 (ir2-physenv-return-pc-pass
795 (lambda-physenv fun)))))
799 ;;; Convert an :ASSIGNMENT call. This is just like a tail local call,
800 ;;; except that the caller and callee environment are the same, so we
801 ;;; don't need to mess with the environment locations, return PC, etc.
802 (defun ir2-convert-assignment (node block fun)
803 (declare (type combination node) (type ir2-block block) (type clambda fun))
804 (multiple-value-bind (temps locs) (emit-psetq-moves node block fun nil)
806 (mapc (lambda (temp loc)
807 (emit-move node block temp loc))
811 ;;; Do stuff to set up the arguments to a non-tail local call
812 ;;; (including implicit environment args.) We allocate a frame
813 ;;; (returning the FP and NFP), and also compute the TN-REFS list for
814 ;;; the values to pass and the list of passing location TNs.
815 (defun ir2-convert-local-call-args (node block fun)
816 (declare (type combination node) (type ir2-block block) (type clambda fun))
817 (let ((fp (make-stack-pointer-tn))
818 (nfp (make-number-stack-pointer-tn))
819 (old-fp (make-stack-pointer-tn)))
820 (multiple-value-bind (temps locs)
821 (emit-psetq-moves node block fun old-fp)
822 (vop current-fp node block old-fp)
823 (vop allocate-frame node block
824 (physenv-info (lambda-physenv fun))
826 (values fp nfp temps (mapcar #'make-alias-tn locs)))))
828 ;;; Handle a non-TR known-values local call. We emit the call, then
829 ;;; move the results to the lvar's destination.
830 (defun ir2-convert-local-known-call (node block fun returns lvar start)
831 (declare (type node node) (type ir2-block block) (type clambda fun)
832 (type return-info returns) (type (or lvar null) lvar)
834 (multiple-value-bind (fp nfp temps arg-locs)
835 (ir2-convert-local-call-args node block fun)
836 (let ((locs (return-info-locations returns)))
837 (vop* known-call-local node block
838 (fp nfp (reference-tn-list temps nil))
839 ((reference-tn-list locs t))
840 arg-locs (physenv-info (lambda-physenv fun)) start)
841 (move-lvar-result node block locs lvar)))
844 ;;; Handle a non-TR unknown-values local call. We do different things
845 ;;; depending on what kind of values the lvar wants.
847 ;;; If LVAR is :UNKNOWN, then we use the "multiple-" variant, directly
848 ;;; specifying the lvar's LOCS as the VOP results so that we don't
849 ;;; have to do anything after the call.
851 ;;; Otherwise, we use STANDARD-RESULT-TNS to get wired result TNs, and
852 ;;; then call MOVE-LVAR-RESULT to do any necessary type checks or
854 (defun ir2-convert-local-unknown-call (node block fun lvar start)
855 (declare (type node node) (type ir2-block block) (type clambda fun)
856 (type (or lvar null) lvar) (type label start))
857 (multiple-value-bind (fp nfp temps arg-locs)
858 (ir2-convert-local-call-args node block fun)
859 (let ((2lvar (and lvar (lvar-info lvar)))
860 (env (physenv-info (lambda-physenv fun)))
861 (temp-refs (reference-tn-list temps nil)))
862 (if (and 2lvar (eq (ir2-lvar-kind 2lvar) :unknown))
863 (vop* multiple-call-local node block (fp nfp temp-refs)
864 ((reference-tn-list (ir2-lvar-locs 2lvar) t))
866 (let ((locs (standard-result-tns lvar)))
867 (vop* call-local node block
869 ((reference-tn-list locs t))
870 arg-locs env start (length locs))
871 (move-lvar-result node block locs lvar)))))
874 ;;; Dispatch to the appropriate function, depending on whether we have
875 ;;; a let, tail or normal call. If the function doesn't return, call
876 ;;; it using the unknown-value convention. We could compile it as a
877 ;;; tail call, but that might seem confusing in the debugger.
878 (defun ir2-convert-local-call (node block)
879 (declare (type combination node) (type ir2-block block))
880 (let* ((fun (ref-leaf (lvar-uses (basic-combination-fun node))))
881 (kind (functional-kind fun)))
882 (cond ((eq kind :let)
883 (ir2-convert-let node block fun))
884 ((eq kind :assignment)
885 (ir2-convert-assignment node block fun))
887 (ir2-convert-tail-local-call node block fun))
889 (let ((start (block-label (lambda-block fun)))
890 (returns (tail-set-info (lambda-tail-set fun)))
891 (lvar (node-lvar node)))
893 (return-info-kind returns)
896 (ir2-convert-local-unknown-call node block fun lvar start))
898 (ir2-convert-local-known-call node block fun returns
904 ;;; Given a function lvar FUN, return (VALUES TN-TO-CALL NAMED-P),
905 ;;; where TN-TO-CALL is a TN holding the thing that we call NAMED-P is
906 ;;; true if the thing is named (false if it is a function).
908 ;;; There are two interesting non-named cases:
909 ;;; -- We know it's a function. No check needed: return the
911 ;;; -- We don't know what it is.
912 (defun fun-lvar-tn (node block lvar)
913 (declare (ignore node block))
914 (declare (type lvar lvar))
915 (let ((2lvar (lvar-info lvar)))
916 (if (eq (ir2-lvar-kind 2lvar) :delayed)
917 (let ((name (lvar-fun-name lvar t)))
919 (values (make-load-time-constant-tn :fdefinition name) t))
920 (let* ((locs (ir2-lvar-locs 2lvar))
922 (function-ptype (primitive-type-or-lose 'function)))
923 (aver (and (eq (ir2-lvar-kind 2lvar) :fixed)
924 (= (length locs) 1)))
925 (aver (eq (tn-primitive-type loc) function-ptype))
928 ;;; Set up the args to NODE in the current frame, and return a TN-REF
929 ;;; list for the passing locations.
930 (defun move-tail-full-call-args (node block)
931 (declare (type combination node) (type ir2-block block))
932 (let ((args (basic-combination-args node))
935 (dotimes (num (length args))
936 (let ((loc (standard-arg-location num)))
937 (emit-move node block (lvar-tn node block (elt args num)) loc)
938 (let ((ref (reference-tn loc nil)))
940 (setf (tn-ref-across last) ref)
945 ;;; Move the arguments into the passing locations and do a (possibly
946 ;;; named) tail call.
947 (defun ir2-convert-tail-full-call (node block)
948 (declare (type combination node) (type ir2-block block))
949 (let* ((env (physenv-info (node-physenv node)))
950 (args (basic-combination-args node))
951 (nargs (length args))
952 (pass-refs (move-tail-full-call-args node block))
953 (old-fp (ir2-physenv-old-fp env))
954 (return-pc (ir2-physenv-return-pc env)))
956 (multiple-value-bind (fun-tn named)
957 (fun-lvar-tn node block (basic-combination-fun node))
959 (vop* tail-call-named node block
960 (fun-tn old-fp return-pc pass-refs)
964 (vop* tail-call node block
965 (fun-tn old-fp return-pc pass-refs)
968 (emit-step-p node)))))
972 ;;; like IR2-CONVERT-LOCAL-CALL-ARGS, only different
973 (defun ir2-convert-full-call-args (node block)
974 (declare (type combination node) (type ir2-block block))
975 (let* ((args (basic-combination-args node))
976 (fp (make-stack-pointer-tn))
977 (nargs (length args)))
978 (vop allocate-full-call-frame node block nargs fp)
983 (locs (standard-arg-location num))
984 (let ((ref (reference-tn (lvar-tn node block (elt args num))
987 (setf (tn-ref-across last) ref)
991 (values fp first (locs) nargs)))))
993 ;;; Do full call when a fixed number of values are desired. We make
994 ;;; STANDARD-RESULT-TNS for our lvar, then deliver the result using
995 ;;; MOVE-LVAR-RESULT. We do named or normal call, as appropriate.
996 (defun ir2-convert-fixed-full-call (node block)
997 (declare (type combination node) (type ir2-block block))
998 (multiple-value-bind (fp args arg-locs nargs)
999 (ir2-convert-full-call-args node block)
1000 (let* ((lvar (node-lvar node))
1001 (locs (standard-result-tns lvar))
1002 (loc-refs (reference-tn-list locs t))
1003 (nvals (length locs)))
1004 (multiple-value-bind (fun-tn named)
1005 (fun-lvar-tn node block (basic-combination-fun node))
1007 (vop* call-named node block (fp fun-tn args) (loc-refs)
1008 arg-locs nargs nvals (emit-step-p node))
1009 (vop* call node block (fp fun-tn args) (loc-refs)
1010 arg-locs nargs nvals (emit-step-p node)))
1011 (move-lvar-result node block locs lvar))))
1014 ;;; Do full call when unknown values are desired.
1015 (defun ir2-convert-multiple-full-call (node block)
1016 (declare (type combination node) (type ir2-block block))
1017 (multiple-value-bind (fp args arg-locs nargs)
1018 (ir2-convert-full-call-args node block)
1019 (let* ((lvar (node-lvar node))
1020 (locs (ir2-lvar-locs (lvar-info lvar)))
1021 (loc-refs (reference-tn-list locs t)))
1022 (multiple-value-bind (fun-tn named)
1023 (fun-lvar-tn node block (basic-combination-fun node))
1025 (vop* multiple-call-named node block (fp fun-tn args) (loc-refs)
1026 arg-locs nargs (emit-step-p node))
1027 (vop* multiple-call node block (fp fun-tn args) (loc-refs)
1028 arg-locs nargs (emit-step-p node))))))
1031 ;;; stuff to check in PONDER-FULL-CALL
1033 ;;; These came in handy when troubleshooting cold boot after making
1034 ;;; major changes in the package structure: various transforms and
1035 ;;; VOPs and stuff got attached to the wrong symbol, so that
1036 ;;; references to the right symbol were bogusly translated as full
1037 ;;; calls instead of primitives, sending the system off into infinite
1038 ;;; space. Having a report on all full calls generated makes it easier
1039 ;;; to figure out what form caused the problem this time.
1040 #!+sb-show (defvar *show-full-called-fnames-p* nil)
1041 #!+sb-show (defvar *full-called-fnames* (make-hash-table :test 'equal))
1043 ;;; Do some checks (and store some notes relevant for future checks)
1045 ;;; * Is this a full call to something we have reason to know should
1046 ;;; never be full called? (Except as of sbcl-0.7.18 or so, we no
1047 ;;; longer try to ensure this behavior when *FAILURE-P* has already
1049 ;;; * Is this a full call to (SETF FOO) which might conflict with
1050 ;;; a DEFSETF or some such thing elsewhere in the program?
1051 (defun ponder-full-call (node)
1052 (let* ((lvar (basic-combination-fun node))
1053 (fname (lvar-fun-name lvar t)))
1054 (declare (type (or symbol cons) fname))
1056 #!+sb-show (unless (gethash fname *full-called-fnames*)
1057 (setf (gethash fname *full-called-fnames*) t))
1058 #!+sb-show (when *show-full-called-fnames-p*
1059 (/show "converting full call to named function" fname)
1060 (/show (basic-combination-args node))
1061 (/show (policy node speed) (policy node safety))
1062 (/show (policy node compilation-speed))
1063 (let ((arg-types (mapcar (lambda (lvar)
1067 (basic-combination-args node))))
1070 ;; When illegal code is compiled, all sorts of perverse paths
1071 ;; through the compiler can be taken, and it's much harder -- and
1072 ;; probably pointless -- to guarantee that always-optimized-away
1073 ;; functions are actually optimized away. Thus, we skip the check
1076 ;; check to see if we know anything about the function
1077 (let ((info (info :function :info fname)))
1078 ;; if we know something, check to see if the full call was valid
1079 (when (and info (ir1-attributep (fun-info-attributes info)
1080 always-translatable))
1081 (/show (policy node speed) (policy node safety))
1082 (/show (policy node compilation-speed))
1083 (bug "full call to ~S" fname))))
1086 (aver (legal-fun-name-p fname))
1087 (destructuring-bind (setfoid &rest stem) fname
1088 (when (eq setfoid 'setf)
1089 (setf (gethash (car stem) *setf-assumed-fboundp*) t))))))
1091 ;;; If the call is in a tail recursive position and the return
1092 ;;; convention is standard, then do a tail full call. If one or fewer
1093 ;;; values are desired, then use a single-value call, otherwise use a
1094 ;;; multiple-values call.
1095 (defun ir2-convert-full-call (node block)
1096 (declare (type combination node) (type ir2-block block))
1097 (ponder-full-call node)
1098 (cond ((node-tail-p node)
1099 (ir2-convert-tail-full-call node block))
1100 ((let ((lvar (node-lvar node)))
1102 (eq (ir2-lvar-kind (lvar-info lvar)) :unknown)))
1103 (ir2-convert-multiple-full-call node block))
1105 (ir2-convert-fixed-full-call node block)))
1108 ;;;; entering functions
1110 ;;; Do all the stuff that needs to be done on XEP entry:
1111 ;;; -- Create frame.
1112 ;;; -- Copy any more arg.
1113 ;;; -- Set up the environment, accessing any closure variables.
1114 ;;; -- Move args from the standard passing locations to their internal
1116 (defun init-xep-environment (node block fun)
1117 (declare (type bind node) (type ir2-block block) (type clambda fun))
1118 (let ((start-label (entry-info-offset (leaf-info fun)))
1119 (env (physenv-info (node-physenv node))))
1120 (let ((ef (functional-entry-fun fun)))
1121 (cond ((and (optional-dispatch-p ef) (optional-dispatch-more-entry ef))
1122 ;; Special case the xep-allocate-frame + copy-more-arg case.
1123 (vop xep-allocate-frame node block start-label t)
1124 (vop copy-more-arg node block (optional-dispatch-max-args ef)))
1126 ;; No more args, so normal entry.
1127 (vop xep-allocate-frame node block start-label nil)))
1128 (if (ir2-physenv-closure env)
1129 (let ((closure (make-normal-tn *backend-t-primitive-type*)))
1130 (vop setup-closure-environment node block start-label closure)
1132 (dolist (loc (ir2-physenv-closure env))
1133 (vop closure-ref node block closure (incf n) (cdr loc)))))
1134 (vop setup-environment node block start-label)))
1136 (unless (eq (functional-kind fun) :toplevel)
1137 (let ((vars (lambda-vars fun))
1139 (when (leaf-refs (first vars))
1140 (emit-move node block (make-arg-count-location)
1141 (leaf-info (first vars))))
1142 (dolist (arg (rest vars))
1143 (when (leaf-refs arg)
1144 (let ((pass (standard-arg-location n))
1145 (home (leaf-info arg)))
1146 (if (lambda-var-indirect arg)
1147 (emit-make-value-cell node block pass home)
1148 (emit-move node block pass home))))
1151 (emit-move node block (make-old-fp-passing-location t)
1152 (ir2-physenv-old-fp env)))
1156 ;;; Emit function prolog code. This is only called on bind nodes for
1157 ;;; functions that allocate environments. All semantics of let calls
1158 ;;; are handled by IR2-CONVERT-LET.
1160 ;;; If not an XEP, all we do is move the return PC from its passing
1161 ;;; location, since in a local call, the caller allocates the frame
1162 ;;; and sets up the arguments.
1163 (defun ir2-convert-bind (node block)
1164 (declare (type bind node) (type ir2-block block))
1165 (let* ((fun (bind-lambda node))
1166 (env (physenv-info (lambda-physenv fun))))
1167 (aver (member (functional-kind fun)
1168 '(nil :external :optional :toplevel :cleanup)))
1171 (init-xep-environment node block fun)
1173 (when *collect-dynamic-statistics*
1174 (vop count-me node block *dynamic-counts-tn*
1175 (block-number (ir2-block-block block)))))
1179 (ir2-physenv-return-pc-pass env)
1180 (ir2-physenv-return-pc env))
1182 #!+unwind-to-frame-and-call-vop
1183 (when (and (policy fun (>= insert-debug-catch 2))
1184 (lambda-return fun))
1185 (vop sb!vm::bind-sentinel node block))
1187 (let ((lab (gen-label)))
1188 (setf (ir2-physenv-environment-start env) lab)
1189 (vop note-environment-start node block lab)))
1193 ;;;; function return
1195 ;;; Do stuff to return from a function with the specified values and
1196 ;;; convention. If the return convention is :FIXED and we aren't
1197 ;;; returning from an XEP, then we do a known return (letting
1198 ;;; representation selection insert the correct move-arg VOPs.)
1199 ;;; Otherwise, we use the unknown-values convention. If there is a
1200 ;;; fixed number of return values, then use RETURN, otherwise use
1201 ;;; RETURN-MULTIPLE.
1202 (defun ir2-convert-return (node block)
1203 (declare (type creturn node) (type ir2-block block))
1204 (let* ((lvar (return-result node))
1205 (2lvar (lvar-info lvar))
1206 (lvar-kind (ir2-lvar-kind 2lvar))
1207 (fun (return-lambda node))
1208 (env (physenv-info (lambda-physenv fun)))
1209 (old-fp (ir2-physenv-old-fp env))
1210 (return-pc (ir2-physenv-return-pc env))
1211 (returns (tail-set-info (lambda-tail-set fun))))
1212 #!+unwind-to-frame-and-call-vop
1213 (when (policy fun (>= insert-debug-catch 2))
1214 (vop sb!vm::unbind-sentinel node block))
1216 ((and (eq (return-info-kind returns) :fixed)
1218 (let ((locs (lvar-tns node block lvar
1219 (return-info-types returns))))
1220 (vop* known-return node block
1221 (old-fp return-pc (reference-tn-list locs nil))
1223 (return-info-locations returns))))
1224 ((eq lvar-kind :fixed)
1225 (let* ((types (mapcar #'tn-primitive-type (ir2-lvar-locs 2lvar)))
1226 (lvar-locs (lvar-tns node block lvar types))
1227 (nvals (length lvar-locs))
1228 (locs (make-standard-value-tns nvals)))
1229 (mapc (lambda (val loc)
1230 (emit-move node block val loc))
1234 (vop return-single node block old-fp return-pc (car locs))
1235 (vop* return node block
1236 (old-fp return-pc (reference-tn-list locs nil))
1240 (aver (eq lvar-kind :unknown))
1241 (vop* return-multiple node block
1243 (reference-tn-list (ir2-lvar-locs 2lvar) nil))
1250 ;;; This is used by the debugger to find the top function on the
1251 ;;; stack. It returns the OLD-FP and RETURN-PC for the current
1252 ;;; function as multiple values.
1253 (defoptimizer (sb!kernel:%caller-frame-and-pc ir2-convert) (() node block)
1254 (let ((ir2-physenv (physenv-info (node-physenv node))))
1255 (move-lvar-result node block
1256 (list (ir2-physenv-old-fp ir2-physenv)
1257 (ir2-physenv-return-pc ir2-physenv))
1260 ;;;; multiple values
1262 ;;; This is almost identical to IR2-CONVERT-LET. Since LTN annotates
1263 ;;; the lvar for the correct number of values (with the lvar user
1264 ;;; responsible for defaulting), we can just pick them up from the
1266 (defun ir2-convert-mv-bind (node block)
1267 (declare (type mv-combination node) (type ir2-block block))
1268 (let* ((lvar (first (basic-combination-args node)))
1269 (fun (ref-leaf (lvar-uses (basic-combination-fun node))))
1270 (vars (lambda-vars fun)))
1271 (aver (eq (functional-kind fun) :mv-let))
1272 (mapc (lambda (src var)
1273 (when (leaf-refs var)
1274 (let ((dest (leaf-info var)))
1275 (if (lambda-var-indirect var)
1276 (emit-make-value-cell node block src dest)
1277 (emit-move node block src dest)))))
1278 (lvar-tns node block lvar
1280 (primitive-type (leaf-type x)))
1285 ;;; Emit the appropriate fixed value, unknown value or tail variant of
1286 ;;; CALL-VARIABLE. Note that we only need to pass the values start for
1287 ;;; the first argument: all the other argument lvar TNs are
1288 ;;; ignored. This is because we require all of the values globs to be
1289 ;;; contiguous and on stack top.
1290 (defun ir2-convert-mv-call (node block)
1291 (declare (type mv-combination node) (type ir2-block block))
1292 (aver (basic-combination-args node))
1293 (let* ((start-lvar (lvar-info (first (basic-combination-args node))))
1294 (start (first (ir2-lvar-locs start-lvar)))
1295 (tails (and (node-tail-p node)
1296 (lambda-tail-set (node-home-lambda node))))
1297 (lvar (node-lvar node))
1298 (2lvar (and lvar (lvar-info lvar))))
1299 (multiple-value-bind (fun named)
1300 (fun-lvar-tn node block (basic-combination-fun node))
1301 (aver (and (not named)
1302 (eq (ir2-lvar-kind start-lvar) :unknown)))
1305 (let ((env (physenv-info (node-physenv node))))
1306 (vop tail-call-variable node block start fun
1307 (ir2-physenv-old-fp env)
1308 (ir2-physenv-return-pc env))))
1310 (eq (ir2-lvar-kind 2lvar) :unknown))
1311 (vop* multiple-call-variable node block (start fun nil)
1312 ((reference-tn-list (ir2-lvar-locs 2lvar) t))
1313 (emit-step-p node)))
1315 (let ((locs (standard-result-tns lvar)))
1316 (vop* call-variable node block (start fun nil)
1317 ((reference-tn-list locs t)) (length locs)
1319 (move-lvar-result node block locs lvar)))))))
1321 ;;; Reset the stack pointer to the start of the specified
1322 ;;; unknown-values lvar (discarding it and all values globs on top of
1324 (defoptimizer (%pop-values ir2-convert) ((%lvar) node block)
1325 (let* ((lvar (lvar-value %lvar))
1326 (2lvar (lvar-info lvar)))
1327 (cond ((eq (ir2-lvar-kind 2lvar) :unknown)
1328 (vop reset-stack-pointer node block
1329 (first (ir2-lvar-locs 2lvar))))
1330 ((lvar-dynamic-extent lvar)
1331 (vop reset-stack-pointer node block
1332 (ir2-lvar-stack-pointer 2lvar)))
1333 (t (bug "Trying to pop a not stack-allocated LVAR ~S."
1336 (defoptimizer (%nip-values ir2-convert) ((last-nipped last-preserved
1339 (let* ( ;; pointer immediately after the nipped block
1340 (after (lvar-value last-nipped))
1341 (2after (lvar-info after))
1342 ;; pointer to the first nipped word
1343 (first (lvar-value last-preserved))
1344 (2first (lvar-info first))
1346 (moved-tns (loop for lvar-ref in moved
1347 for lvar = (lvar-value lvar-ref)
1348 for 2lvar = (lvar-info lvar)
1350 collect (first (ir2-lvar-locs 2lvar)))))
1351 (aver (or (eq (ir2-lvar-kind 2after) :unknown)
1352 (lvar-dynamic-extent after)))
1353 (aver (eq (ir2-lvar-kind 2first) :unknown))
1354 (when *check-consistency*
1355 ;; we cannot move stack-allocated DX objects
1356 (dolist (moved-lvar moved)
1357 (aver (eq (ir2-lvar-kind (lvar-info (lvar-value moved-lvar)))
1359 (flet ((nip-aligned (nipped)
1360 (vop* %%nip-values node block
1362 (first (ir2-lvar-locs 2first))
1363 (reference-tn-list moved-tns nil))
1364 ((reference-tn-list moved-tns t)))))
1365 (cond ((eq (ir2-lvar-kind 2after) :unknown)
1366 (nip-aligned (first (ir2-lvar-locs 2after))))
1367 ((lvar-dynamic-extent after)
1368 (nip-aligned (ir2-lvar-stack-pointer 2after)))
1370 (bug "Trying to nip a not stack-allocated LVAR ~S." after))))))
1372 ;;; Deliver the values TNs to LVAR using MOVE-LVAR-RESULT.
1373 (defoptimizer (values ir2-convert) ((&rest values) node block)
1374 (let ((tns (mapcar (lambda (x)
1375 (lvar-tn node block x))
1377 (move-lvar-result node block tns (node-lvar node))))
1379 ;;; In the normal case where unknown values are desired, we use the
1380 ;;; VALUES-LIST VOP. In the relatively unimportant case of VALUES-LIST
1381 ;;; for a fixed number of values, we punt by doing a full call to the
1382 ;;; VALUES-LIST function. This gets the full call VOP to deal with
1383 ;;; defaulting any unsupplied values. It seems unworthwhile to
1384 ;;; optimize this case.
1385 (defoptimizer (values-list ir2-convert) ((list) node block)
1386 (let* ((lvar (node-lvar node))
1387 (2lvar (and lvar (lvar-info lvar))))
1389 (eq (ir2-lvar-kind 2lvar) :unknown))
1390 (let ((locs (ir2-lvar-locs 2lvar)))
1391 (vop* values-list node block
1392 ((lvar-tn node block list) nil)
1393 ((reference-tn-list locs t)))))
1394 (t (aver (or (not 2lvar) ; i.e. we want to check the argument
1395 (eq (ir2-lvar-kind 2lvar) :fixed)))
1396 (ir2-convert-full-call node block)))))
1398 (defoptimizer (%more-arg-values ir2-convert) ((context start count) node block)
1399 (binding* ((lvar (node-lvar node) :exit-if-null)
1400 (2lvar (lvar-info lvar)))
1401 (ecase (ir2-lvar-kind 2lvar)
1402 (:fixed (ir2-convert-full-call node block))
1404 (let ((locs (ir2-lvar-locs 2lvar)))
1405 (vop* %more-arg-values node block
1406 ((lvar-tn node block context)
1407 (lvar-tn node block start)
1408 (lvar-tn node block count)
1410 ((reference-tn-list locs t))))))))
1412 ;;;; special binding
1414 ;;; This is trivial, given our assumption of a shallow-binding
1416 (defoptimizer (%special-bind ir2-convert) ((var value) node block)
1417 (let ((name (leaf-source-name (lvar-value var))))
1418 (vop bind node block (lvar-tn node block value)
1419 (emit-constant name))))
1420 (defoptimizer (%special-unbind ir2-convert) ((var) node block)
1421 (vop unbind node block))
1423 ;;; ### It's not clear that this really belongs in this file, or
1424 ;;; should really be done this way, but this is the least violation of
1425 ;;; abstraction in the current setup. We don't want to wire
1426 ;;; shallow-binding assumptions into IR1tran.
1427 (def-ir1-translator progv
1428 ((vars vals &body body) start next result)
1431 (with-unique-names (bind unbind)
1432 (once-only ((n-save-bs '(%primitive current-binding-pointer)))
1435 (labels ((,unbind (vars)
1436 (declare (optimize (speed 2) (debug 0)))
1438 (%primitive bind nil var)
1441 (declare (optimize (speed 2) (debug 0)))
1443 ((null vals) (,unbind vars))
1447 (,bind (cdr vars) (cdr vals))))))
1448 (,bind ,vars ,vals))
1451 ;; Technically ANSI CL doesn't allow declarations at the
1452 ;; start of the cleanup form. SBCL happens to allow for
1453 ;; them, due to the way the UNWIND-PROTECT ir1 translation
1454 ;; is implemented; the cleanup forms are directly spliced
1455 ;; into an FLET definition body. And a declaration here
1456 ;; actually has exactly the right scope for what we need
1457 ;; (ensure that debug instrumentation is not emitted for the
1458 ;; cleanup function). -- JES, 2007-06-16
1459 (declare (optimize (insert-debug-catch 0)))
1460 (%primitive unbind-to-here ,n-save-bs))))))
1464 ;;; Convert a non-local lexical exit. First find the NLX-INFO in our
1465 ;;; environment. Note that this is never called on the escape exits
1466 ;;; for CATCH and UNWIND-PROTECT, since the escape functions aren't
1468 (defun ir2-convert-exit (node block)
1469 (declare (type exit node) (type ir2-block block))
1470 (let* ((nlx (exit-nlx-info node))
1471 (loc (find-in-physenv nlx (node-physenv node)))
1472 (temp (make-stack-pointer-tn))
1473 (value (exit-value node)))
1474 (if (nlx-info-safe-p nlx)
1475 (vop value-cell-ref node block loc temp)
1476 (emit-move node block loc temp))
1478 (let ((locs (ir2-lvar-locs (lvar-info value))))
1479 (vop unwind node block temp (first locs) (second locs)))
1480 (let ((0-tn (emit-constant 0)))
1481 (vop unwind node block temp 0-tn 0-tn))))
1485 ;;; %CLEANUP-POINT doesn't do anything except prevent the body from
1486 ;;; being entirely deleted.
1487 (defoptimizer (%cleanup-point ir2-convert) (() node block) node block)
1489 ;;; This function invalidates a lexical exit on exiting from the
1490 ;;; dynamic extent. This is done by storing 0 into the indirect value
1491 ;;; cell that holds the closed unwind block.
1492 (defoptimizer (%lexical-exit-breakup ir2-convert) ((info) node block)
1493 (let ((nlx (lvar-value info)))
1494 (when (nlx-info-safe-p nlx)
1495 (vop value-cell-set node block
1496 (find-in-physenv nlx (node-physenv node))
1497 (emit-constant 0)))))
1499 ;;; We have to do a spurious move of no values to the result lvar so
1500 ;;; that lifetime analysis won't get confused.
1501 (defun ir2-convert-throw (node block)
1502 (declare (type mv-combination node) (type ir2-block block))
1503 (let ((args (basic-combination-args node)))
1504 (check-catch-tag-type (first args))
1505 (vop* throw node block
1506 ((lvar-tn node block (first args))
1508 (ir2-lvar-locs (lvar-info (second args)))
1511 (move-lvar-result node block () (node-lvar node))
1514 ;;; Emit code to set up a non-local exit. INFO is the NLX-INFO for the
1515 ;;; exit, and TAG is the lvar for the catch tag (if any.) We get at
1516 ;;; the target PC by passing in the label to the vop. The vop is
1517 ;;; responsible for building a return-PC object.
1518 (defun emit-nlx-start (node block info tag)
1519 (declare (type node node) (type ir2-block block) (type nlx-info info)
1520 (type (or lvar null) tag))
1521 (let* ((2info (nlx-info-info info))
1522 (kind (cleanup-kind (nlx-info-cleanup info)))
1523 (block-tn (physenv-live-tn
1524 (make-normal-tn (primitive-type-or-lose 'catch-block))
1525 (node-physenv node)))
1526 (res (make-stack-pointer-tn))
1527 (target-label (ir2-nlx-info-target 2info)))
1529 (vop current-binding-pointer node block
1530 (car (ir2-nlx-info-dynamic-state 2info)))
1531 (vop* save-dynamic-state node block
1533 ((reference-tn-list (cdr (ir2-nlx-info-dynamic-state 2info)) t)))
1534 (vop current-stack-pointer node block (ir2-nlx-info-save-sp 2info))
1538 (vop make-catch-block node block block-tn
1539 (lvar-tn node block tag) target-label res))
1540 ((:unwind-protect :block :tagbody)
1541 (vop make-unwind-block node block block-tn target-label res)))
1545 (if (nlx-info-safe-p info)
1546 (emit-make-value-cell node block res (ir2-nlx-info-home 2info))
1547 (emit-move node block res (ir2-nlx-info-home 2info))))
1549 (vop set-unwind-protect node block block-tn))
1554 ;;; Scan each of ENTRY's exits, setting up the exit for each lexical exit.
1555 (defun ir2-convert-entry (node block)
1556 (declare (type entry node) (type ir2-block block))
1558 (dolist (exit (entry-exits node))
1559 (let ((info (exit-nlx-info exit)))
1561 (not (memq info nlxes))
1562 (member (cleanup-kind (nlx-info-cleanup info))
1563 '(:block :tagbody)))
1565 (emit-nlx-start node block info nil)))))
1568 ;;; Set up the unwind block for these guys.
1569 (defoptimizer (%catch ir2-convert) ((info-lvar tag) node block)
1570 (check-catch-tag-type tag)
1571 (emit-nlx-start node block (lvar-value info-lvar) tag))
1572 (defoptimizer (%unwind-protect ir2-convert) ((info-lvar cleanup) node block)
1573 (emit-nlx-start node block (lvar-value info-lvar) nil))
1575 ;;; Emit the entry code for a non-local exit. We receive values and
1576 ;;; restore dynamic state.
1578 ;;; In the case of a lexical exit or CATCH, we look at the exit lvar's
1579 ;;; kind to determine which flavor of entry VOP to emit. If unknown
1580 ;;; values, emit the xxx-MULTIPLE variant to the lvar locs. If fixed
1581 ;;; values, make the appropriate number of temps in the standard
1582 ;;; values locations and use the other variant, delivering the temps
1583 ;;; to the lvar using MOVE-LVAR-RESULT.
1585 ;;; In the UNWIND-PROTECT case, we deliver the first register
1586 ;;; argument, the argument count and the argument pointer to our lvar
1587 ;;; as multiple values. These values are the block exited to and the
1588 ;;; values start and count.
1590 ;;; After receiving values, we restore dynamic state. Except in the
1591 ;;; UNWIND-PROTECT case, the values receiving restores the stack
1592 ;;; pointer. In an UNWIND-PROTECT cleanup, we want to leave the stack
1593 ;;; pointer alone, since the thrown values are still out there.
1594 (defoptimizer (%nlx-entry ir2-convert) ((info-lvar) node block)
1595 (let* ((info (lvar-value info-lvar))
1596 (lvar (node-lvar node))
1597 (2info (nlx-info-info info))
1598 (top-loc (ir2-nlx-info-save-sp 2info))
1599 (start-loc (make-nlx-entry-arg-start-location))
1600 (count-loc (make-arg-count-location))
1601 (target (ir2-nlx-info-target 2info)))
1603 (ecase (cleanup-kind (nlx-info-cleanup info))
1604 ((:catch :block :tagbody)
1605 (let ((2lvar (and lvar (lvar-info lvar))))
1606 (if (and 2lvar (eq (ir2-lvar-kind 2lvar) :unknown))
1607 (vop* nlx-entry-multiple node block
1608 (top-loc start-loc count-loc nil)
1609 ((reference-tn-list (ir2-lvar-locs 2lvar) t))
1611 (let ((locs (standard-result-tns lvar)))
1612 (vop* nlx-entry node block
1613 (top-loc start-loc count-loc nil)
1614 ((reference-tn-list locs t))
1617 (move-lvar-result node block locs lvar)))))
1619 (let ((block-loc (standard-arg-location 0)))
1620 (vop uwp-entry node block target block-loc start-loc count-loc)
1623 (list block-loc start-loc count-loc)
1627 (when *collect-dynamic-statistics*
1628 (vop count-me node block *dynamic-counts-tn*
1629 (block-number (ir2-block-block block))))
1631 (vop* restore-dynamic-state node block
1632 ((reference-tn-list (cdr (ir2-nlx-info-dynamic-state 2info)) nil))
1634 (vop unbind-to-here node block
1635 (car (ir2-nlx-info-dynamic-state 2info)))))
1637 ;;;; n-argument functions
1639 (macrolet ((def (name)
1640 `(defoptimizer (,name ir2-convert) ((&rest args) node block)
1641 (let* ((refs (move-tail-full-call-args node block))
1642 (lvar (node-lvar node))
1643 (res (lvar-result-tns
1645 (list (primitive-type (specifier-type 'list))))))
1646 (when (and lvar (lvar-dynamic-extent lvar))
1647 (vop current-stack-pointer node block
1648 (ir2-lvar-stack-pointer (lvar-info lvar))))
1649 (vop* ,name node block (refs) ((first res) nil)
1651 (move-lvar-result node block res lvar)))))
1656 ;;; Convert the code in a component into VOPs.
1657 (defun ir2-convert (component)
1658 (declare (type component component))
1659 (let (#!+sb-dyncount
1660 (*dynamic-counts-tn*
1661 (when *collect-dynamic-statistics*
1663 (block-number (block-next (component-head component))))
1664 (counts (make-array blocks
1665 :element-type '(unsigned-byte 32)
1666 :initial-element 0))
1667 (info (make-dyncount-info
1668 :for (component-name component)
1669 :costs (make-array blocks
1670 :element-type '(unsigned-byte 32)
1673 (setf (ir2-component-dyncount-info (component-info component))
1675 (emit-constant info)
1676 (emit-constant counts)))))
1678 (declare (type index num))
1679 (do-ir2-blocks (2block component)
1680 (let ((block (ir2-block-block 2block)))
1681 (when (block-start block)
1682 (setf (block-number block) num)
1684 (when *collect-dynamic-statistics*
1685 (let ((first-node (block-start-node block)))
1686 (unless (or (and (bind-p first-node)
1687 (xep-p (bind-lambda first-node)))
1689 (node-lvar first-node))
1694 #!+sb-dyncount *dynamic-counts-tn* #!-sb-dyncount nil
1696 (ir2-convert-block block)
1700 ;;; If necessary, emit a terminal unconditional branch to go to the
1701 ;;; successor block. If the successor is the component tail, then
1702 ;;; there isn't really any successor, but if the end is an unknown,
1703 ;;; non-tail call, then we emit an error trap just in case the
1704 ;;; function really does return.
1705 (defun finish-ir2-block (block)
1706 (declare (type cblock block))
1707 (let* ((2block (block-info block))
1708 (last (block-last block))
1709 (succ (block-succ block)))
1711 (aver (singleton-p succ))
1712 (let ((target (first succ)))
1713 (cond ((eq target (component-tail (block-component block)))
1714 (when (and (basic-combination-p last)
1715 (eq (basic-combination-kind last) :full))
1716 (let* ((fun (basic-combination-fun last))
1717 (use (lvar-uses fun))
1718 (name (and (ref-p use)
1719 (leaf-has-source-name-p (ref-leaf use))
1720 (leaf-source-name (ref-leaf use)))))
1721 (unless (or (node-tail-p last)
1722 (info :function :info name)
1723 (policy last (zerop safety)))
1724 (vop nil-fun-returned-error last 2block
1726 (emit-constant name)
1727 (multiple-value-bind (tn named)
1728 (fun-lvar-tn last 2block fun)
1731 ((not (eq (ir2-block-next 2block) (block-info target)))
1732 (vop branch last 2block (block-label target)))))))
1736 ;;; Convert the code in a block into VOPs.
1737 (defun ir2-convert-block (block)
1738 (declare (type cblock block))
1739 (let ((2block (block-info block)))
1740 (do-nodes (node lvar block)
1744 (let ((2lvar (lvar-info lvar)))
1745 ;; function REF in a local call is not annotated
1746 (when (and 2lvar (not (eq (ir2-lvar-kind 2lvar) :delayed)))
1747 (ir2-convert-ref node 2block)))))
1749 (let ((kind (basic-combination-kind node)))
1752 (ir2-convert-local-call node 2block))
1754 (ir2-convert-full-call node 2block))
1756 (let* ((info (basic-combination-fun-info node))
1757 (fun (fun-info-ir2-convert info)))
1759 (funcall fun node 2block))
1760 ((eq (basic-combination-info node) :full)
1761 (ir2-convert-full-call node 2block))
1763 (ir2-convert-template node 2block))))))))
1765 (when (lvar-info (if-test node))
1766 (ir2-convert-if node 2block)))
1768 (let ((fun (bind-lambda node)))
1769 (when (eq (lambda-home fun) fun)
1770 (ir2-convert-bind node 2block))))
1772 (ir2-convert-return node 2block))
1774 (ir2-convert-set node 2block))
1776 (ir2-convert-cast node 2block))
1779 ((eq (basic-combination-kind node) :local)
1780 (ir2-convert-mv-bind node 2block))
1781 ((eq (lvar-fun-name (basic-combination-fun node))
1783 (ir2-convert-throw node 2block))
1785 (ir2-convert-mv-call node 2block))))
1787 (when (exit-entry node)
1788 (ir2-convert-exit node 2block)))
1790 (ir2-convert-entry node 2block)))))
1792 (finish-ir2-block block)