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 ;;; If there is any CHECK-xxx template for TYPE, then return it,
25 ;;; otherwise return NIL.
26 (defun type-check-template (type)
27 (declare (type ctype type))
28 (multiple-value-bind (check-ptype exact) (primitive-type type)
30 (primitive-type-check check-ptype)
31 (let ((name (hairy-type-check-template-name type)))
33 (template-or-lose name)
36 ;;; Emit code in BLOCK to check that VALUE is of the specified TYPE,
37 ;;; yielding the checked result in RESULT. VALUE and result may be of
38 ;;; any primitive type. There must be CHECK-xxx VOP for TYPE. Any
39 ;;; other type checks should have been converted to an explicit type
41 (defun emit-type-check (node block value result type)
42 (declare (type tn value result) (type node node) (type ir2-block block)
44 (emit-move-template node block (type-check-template type) value result)
47 ;;; Allocate an indirect value cell. Maybe do some clever stack
48 ;;; allocation someday.
50 ;;; FIXME: DO-MAKE-VALUE-CELL is a bad name, since it doesn't make
51 ;;; clear what's the distinction between it and the MAKE-VALUE-CELL
52 ;;; VOP, and since the DO- further connotes iteration, which has
53 ;;; nothing to do with this. Clearer, more systematic names, anyone?
54 (defevent make-value-cell-event "Allocate heap value cell for lexical var.")
55 (defun do-make-value-cell (node block value res)
56 (event make-value-cell-event node)
57 (vop make-value-cell node block value res))
61 ;;; Return the TN that holds the value of THING in the environment ENV.
62 (declaim (ftype (function ((or nlx-info lambda-var clambda) physenv) tn)
64 (defun find-in-physenv (thing physenv)
65 (or (cdr (assoc thing (ir2-physenv-closure (physenv-info physenv))))
68 ;; I think that a failure of this assertion means that we're
69 ;; trying to access a variable which was improperly closed
70 ;; over. The PHYSENV describes a physical environment. Every
71 ;; variable that a form refers to should either be in its
72 ;; physical environment directly, or grabbed from a
73 ;; surrounding physical environment when it was closed over.
74 ;; The ASSOC expression above finds closed-over variables, so
75 ;; if we fell through the ASSOC expression, it wasn't closed
76 ;; over. Therefore, it must be in our physical environment
77 ;; directly. If instead it is in some other physical
78 ;; environment, then it's bogus for us to reference it here
79 ;; without it being closed over. -- WHN 2001-09-29
80 (aver (eq physenv (lambda-physenv (lambda-var-home thing))))
83 (aver (eq physenv (block-physenv (nlx-info-target thing))))
84 (ir2-nlx-info-home (nlx-info-info thing)))
87 (entry-info-closure-tn (lambda-info thing))))
88 (bug "~@<~2I~_~S ~_not found in ~_~S~:>" thing physenv)))
90 ;;; If LEAF already has a constant TN, return that, otherwise make a
92 (defun constant-tn (leaf)
93 (declare (type constant leaf))
95 (setf (leaf-info leaf)
96 (make-constant-tn leaf))))
98 ;;; Return a TN that represents the value of LEAF, or NIL if LEAF
99 ;;; isn't directly represented by a TN. ENV is the environment that
100 ;;; the reference is done in.
101 (defun leaf-tn (leaf env)
102 (declare (type leaf leaf) (type physenv env))
105 (unless (lambda-var-indirect leaf)
106 (find-in-physenv leaf env)))
107 (constant (constant-tn leaf))
110 ;;; This is used to conveniently get a handle on a constant TN during
111 ;;; IR2 conversion. It returns a constant TN representing the Lisp
113 (defun emit-constant (value)
114 (constant-tn (find-constant value)))
116 ;;; Convert a REF node. The reference must not be delayed.
117 (defun ir2-convert-ref (node block)
118 (declare (type ref node) (type ir2-block block))
119 (let* ((lvar (node-lvar node))
120 (leaf (ref-leaf node))
121 (locs (lvar-result-tns
122 lvar (list (primitive-type (leaf-type leaf)))))
126 (let ((tn (find-in-physenv leaf (node-physenv node))))
127 (if (lambda-var-indirect leaf)
128 (vop value-cell-ref node block tn res)
129 (emit-move node block tn res))))
131 (if (legal-immediate-constant-p leaf)
132 (emit-move node block (constant-tn leaf) res)
133 (let* ((name (leaf-source-name leaf))
134 (name-tn (emit-constant name)))
135 (if (policy node (zerop safety))
136 (vop fast-symbol-value node block name-tn res)
137 (vop symbol-value node block name-tn res)))))
139 (ir2-convert-closure node block leaf res))
141 (let ((unsafe (policy node (zerop safety)))
142 (name (leaf-source-name leaf)))
143 (ecase (global-var-kind leaf)
145 (aver (symbolp name))
146 (let ((name-tn (emit-constant name)))
148 (vop fast-symbol-value node block name-tn res)
149 (vop symbol-value node block name-tn res))))
151 (let ((fdefn-tn (make-load-time-constant-tn :fdefinition name)))
153 (vop fdefn-fun node block fdefn-tn res)
154 (vop safe-fdefn-fun node block fdefn-tn res))))))))
155 (move-lvar-result node block locs lvar))
158 ;;; some sanity checks for a CLAMBDA passed to IR2-CONVERT-CLOSURE
159 (defun assertions-on-ir2-converted-clambda (clambda)
160 ;; This assertion was sort of an experiment. It would be nice and
161 ;; sane and easier to understand things if it were *always* true,
162 ;; but experimentally I observe that it's only *almost* always
163 ;; true. -- WHN 2001-01-02
165 (aver (eql (lambda-component clambda)
166 (block-component (ir2-block-block ir2-block))))
167 ;; Check for some weirdness which came up in bug
170 ;; The MAKE-LOAD-TIME-CONSTANT-TN call above puts an :ENTRY record
171 ;; into the IR2-COMPONENT-CONSTANTS table. The dump-a-COMPONENT
173 ;; * treats every HANDLEless :ENTRY record into a
175 ;; * expects every patch to correspond to an
176 ;; IR2-COMPONENT-ENTRIES record.
177 ;; The IR2-COMPONENT-ENTRIES records are set by ENTRY-ANALYZE
178 ;; walking over COMPONENT-LAMBDAS. Bug 138b arose because there
179 ;; was a HANDLEless :ENTRY record which didn't correspond to an
180 ;; IR2-COMPONENT-ENTRIES record. That problem is hard to debug
181 ;; when it's caught at dump time, so this assertion tries to catch
183 (aver (member clambda
184 (component-lambdas (lambda-component clambda))))
185 ;; another bug-138-related issue: COMPONENT-NEW-FUNCTIONALS is
186 ;; used as a queue for stuff pending to do in IR1, and now that
187 ;; we're doing IR2 it should've been completely flushed (but
189 (aver (null (component-new-functionals (lambda-component clambda))))
192 ;;; Emit code to load a function object implementing FUNCTIONAL into
193 ;;; RES. This gets interesting when the referenced function is a
194 ;;; closure: we must make the closure and move the closed-over values
197 ;;; FUNCTIONAL is either a :TOPLEVEL-XEP functional or the XEP lambda
198 ;;; for the called function, since local call analysis converts all
199 ;;; closure references. If a :TOPLEVEL-XEP, we know it is not a
202 ;;; If a closed-over LAMBDA-VAR has no refs (is deleted), then we
203 ;;; don't initialize that slot. This can happen with closures over
204 ;;; top level variables, where optimization of the closure deleted the
205 ;;; variable. Since we committed to the closure format when we
206 ;;; pre-analyzed the top level code, we just leave an empty slot.
207 (defun ir2-convert-closure (ref ir2-block functional res)
208 (declare (type ref ref)
209 (type ir2-block ir2-block)
210 (type functional functional)
212 (aver (not (eql (functional-kind functional) :deleted)))
213 (unless (leaf-info functional)
214 (setf (leaf-info functional)
215 (make-entry-info :name (functional-debug-name functional))))
216 (let ((closure (etypecase functional
218 (assertions-on-ir2-converted-clambda functional)
219 (physenv-closure (get-lambda-physenv functional)))
221 (aver (eq (functional-kind functional) :toplevel-xep))
225 (let* ((physenv (node-physenv ref))
226 (tn (find-in-physenv functional physenv)))
227 (emit-move ref ir2-block tn res)))
229 (let ((entry (make-load-time-constant-tn :entry functional)))
230 (emit-move ref ir2-block entry res)))))
233 (defoptimizer (%allocate-closures ltn-annotate) ((leaves) node ltn-policy)
234 ltn-policy ; a hack to effectively (DECLARE (IGNORE LTN-POLICY))
235 (when (lvar-dynamic-extent leaves)
236 (let ((info (make-ir2-lvar *backend-t-primitive-type*)))
237 (setf (ir2-lvar-kind info) :delayed)
238 (setf (lvar-info leaves) info)
239 #!+stack-grows-upward-not-downward
240 (let ((tn (make-normal-tn *backend-t-primitive-type*)))
241 (setf (ir2-lvar-locs info) (list tn)))
242 #!+stack-grows-downward-not-upward
243 (setf (ir2-lvar-stack-pointer info)
244 (make-stack-pointer-tn)))))
246 (defoptimizer (%allocate-closures ir2-convert) ((leaves) call 2block)
247 (let ((dx-p (lvar-dynamic-extent leaves))
248 #!+stack-grows-upward-not-downward
251 #!+stack-grows-downward-not-upward
253 (vop current-stack-pointer call 2block
254 (ir2-lvar-stack-pointer (lvar-info leaves))))
255 (dolist (leaf (lvar-value leaves))
256 (binding* ((xep (functional-entry-fun leaf) :exit-if-null)
257 (nil (aver (xep-p xep)))
258 (entry-info (lambda-info xep) :exit-if-null)
259 (tn (entry-info-closure-tn entry-info) :exit-if-null)
260 (closure (physenv-closure (get-lambda-physenv xep)))
261 (entry (make-load-time-constant-tn :entry xep)))
262 (let ((this-env (node-physenv call))
263 (leaf-dx-p (and dx-p (leaf-dynamic-extent leaf))))
264 (vop make-closure call 2block entry (length closure)
266 #!+stack-grows-upward-not-downward
267 (when (and (not first-closure) leaf-dx-p)
268 (setq first-closure tn))
269 (loop for what in closure and n from 0 do
270 (unless (and (lambda-var-p what)
271 (null (leaf-refs what)))
272 ;; In LABELS a closure may refer to another closure
273 ;; in the same group, so we must be sure that we
274 ;; store a closure only after its creation.
276 ;; TODO: Here is a simple solution: we postpone
277 ;; putting of all closures after all creations
278 ;; (though it may require more registers).
280 (delayed (list tn (find-in-physenv what this-env) n))
281 (vop closure-init call 2block
283 (find-in-physenv what this-env)
285 #!+stack-grows-upward-not-downward
287 (emit-move call 2block first-closure
288 (first (ir2-lvar-locs (lvar-info leaves)))))
289 (loop for (tn what n) in (delayed)
290 do (vop closure-init call 2block
294 ;;; Convert a SET node. If the NODE's LVAR is annotated, then we also
295 ;;; deliver the value to that lvar. If the var is a lexical variable
296 ;;; with no refs, then we don't actually set anything, since the
297 ;;; variable has been deleted.
298 (defun ir2-convert-set (node block)
299 (declare (type cset node) (type ir2-block block))
300 (let* ((lvar (node-lvar node))
301 (leaf (set-var node))
302 (val (lvar-tn node block (set-value node)))
305 lvar (list (primitive-type (leaf-type leaf))))
309 (when (leaf-refs leaf)
310 (let ((tn (find-in-physenv leaf (node-physenv node))))
311 (if (lambda-var-indirect leaf)
312 (vop value-cell-set node block tn val)
313 (emit-move node block val tn)))))
315 (ecase (global-var-kind leaf)
317 (aver (symbolp (leaf-source-name leaf)))
318 (vop set node block (emit-constant (leaf-source-name leaf)) val)))))
320 (emit-move node block val (first locs))
321 (move-lvar-result node block locs lvar)))
324 ;;;; utilities for receiving fixed values
326 ;;; Return a TN that can be referenced to get the value of LVAR. LVAR
327 ;;; must be LTN-ANNOTATED either as a delayed leaf ref or as a fixed,
328 ;;; single-value lvar.
330 ;;; The primitive-type of the result will always be the same as the
331 ;;; IR2-LVAR-PRIMITIVE-TYPE, ensuring that VOPs are always called with
332 ;;; TNs that satisfy the operand primitive-type restriction. We may
333 ;;; have to make a temporary of the desired type and move the actual
334 ;;; lvar TN into it. This happens when we delete a type check in
335 ;;; unsafe code or when we locally know something about the type of an
336 ;;; argument variable.
337 (defun lvar-tn (node block lvar)
338 (declare (type node node) (type ir2-block block) (type lvar lvar))
339 (let* ((2lvar (lvar-info lvar))
341 (ecase (ir2-lvar-kind 2lvar)
343 (let ((ref (lvar-uses lvar)))
344 (leaf-tn (ref-leaf ref) (node-physenv ref))))
346 (aver (= (length (ir2-lvar-locs 2lvar)) 1))
347 (first (ir2-lvar-locs 2lvar)))))
348 (ptype (ir2-lvar-primitive-type 2lvar)))
350 (cond ((eq (tn-primitive-type lvar-tn) ptype) lvar-tn)
352 (let ((temp (make-normal-tn ptype)))
353 (emit-move node block lvar-tn temp)
356 ;;; This is similar to LVAR-TN, but hacks multiple values. We return
357 ;;; TNs holding the values of LVAR with PTYPES as their primitive
358 ;;; types. LVAR must be annotated for the same number of fixed values
359 ;;; are there are PTYPES.
361 ;;; If the lvar has a type check, check the values into temps and
362 ;;; return the temps. When we have more values than assertions, we
363 ;;; move the extra values with no check.
364 (defun lvar-tns (node block lvar ptypes)
365 (declare (type node node) (type ir2-block block)
366 (type lvar lvar) (list ptypes))
367 (let* ((locs (ir2-lvar-locs (lvar-info lvar)))
368 (nlocs (length locs)))
369 (aver (= nlocs (length ptypes)))
371 (mapcar (lambda (from to-type)
372 (if (eq (tn-primitive-type from) to-type)
374 (let ((temp (make-normal-tn to-type)))
375 (emit-move node block from temp)
380 ;;;; utilities for delivering values to lvars
382 ;;; Return a list of TNs with the specifier TYPES that can be used as
383 ;;; result TNs to evaluate an expression into LVAR. This is used
384 ;;; together with MOVE-LVAR-RESULT to deliver fixed values to
387 ;;; If the lvar isn't annotated (meaning the values are discarded) or
388 ;;; is unknown-values, the then we make temporaries for each supplied
389 ;;; value, providing a place to compute the result in until we decide
390 ;;; what to do with it (if anything.)
392 ;;; If the lvar is fixed-values, and wants the same number of values
393 ;;; as the user wants to deliver, then we just return the
394 ;;; IR2-LVAR-LOCS. Otherwise we make a new list padded as necessary by
395 ;;; discarded TNs. We always return a TN of the specified type, using
396 ;;; the lvar locs only when they are of the correct type.
397 (defun lvar-result-tns (lvar types)
398 (declare (type (or lvar null) lvar) (type list types))
400 (mapcar #'make-normal-tn types)
401 (let ((2lvar (lvar-info lvar)))
402 (ecase (ir2-lvar-kind 2lvar)
404 (let* ((locs (ir2-lvar-locs 2lvar))
405 (nlocs (length locs))
406 (ntypes (length types)))
407 (if (and (= nlocs ntypes)
408 (do ((loc locs (cdr loc))
409 (type types (cdr type)))
411 (unless (eq (tn-primitive-type (car loc)) (car type))
414 (mapcar (lambda (loc type)
415 (if (eq (tn-primitive-type loc) type)
417 (make-normal-tn type)))
420 (mapcar #'make-normal-tn
421 (subseq types nlocs)))
425 (mapcar #'make-normal-tn types))))))
427 ;;; Make the first N standard value TNs, returning them in a list.
428 (defun make-standard-value-tns (n)
429 (declare (type unsigned-byte n))
432 (res (standard-arg-location i)))
435 ;;; Return a list of TNs wired to the standard value passing
436 ;;; conventions that can be used to receive values according to the
437 ;;; unknown-values convention. This is used with together
438 ;;; MOVE-LVAR-RESULT for delivering unknown values to a fixed values
441 ;;; If the lvar isn't annotated, then we treat as 0-values, returning
442 ;;; an empty list of temporaries.
444 ;;; If the lvar is annotated, then it must be :FIXED.
445 (defun standard-result-tns (lvar)
446 (declare (type (or lvar null) lvar))
448 (let ((2lvar (lvar-info lvar)))
449 (ecase (ir2-lvar-kind 2lvar)
451 (make-standard-value-tns (length (ir2-lvar-locs 2lvar))))))
454 ;;; Just move each SRC TN into the corresponding DEST TN, defaulting
455 ;;; any unsupplied source values to NIL. We let EMIT-MOVE worry about
456 ;;; doing the appropriate coercions.
457 (defun move-results-coerced (node block src dest)
458 (declare (type node node) (type ir2-block block) (list src dest))
459 (let ((nsrc (length src))
460 (ndest (length dest)))
461 (mapc (lambda (from to)
463 (emit-move node block from to)))
465 (append src (make-list (- ndest nsrc)
466 :initial-element (emit-constant nil)))
471 ;;; Move each SRC TN into the corresponding DEST TN, checking types
472 ;;; and defaulting any unsupplied source values to NIL
473 (defun move-results-checked (node block src dest types)
474 (declare (type node node) (type ir2-block block) (list src dest types))
475 (let ((nsrc (length src))
476 (ndest (length dest))
477 (ntypes (length types)))
478 (mapc (lambda (from to type)
480 (emit-type-check node block from to type)
481 (emit-move node block from to)))
483 (append src (make-list (- ndest nsrc)
484 :initial-element (emit-constant nil)))
488 (append types (make-list (- ndest ntypes)))
492 ;;; If necessary, emit coercion code needed to deliver the RESULTS to
493 ;;; the specified lvar. NODE and BLOCK provide context for emitting
494 ;;; code. Although usually obtained from STANDARD-RESULT-TNs or
495 ;;; LVAR-RESULT-TNs, RESULTS my be a list of any type or
498 ;;; If the lvar is fixed values, then move the results into the lvar
499 ;;; locations. If the lvar is unknown values, then do the moves into
500 ;;; the standard value locations, and use PUSH-VALUES to put the
501 ;;; values on the stack.
502 (defun move-lvar-result (node block results lvar)
503 (declare (type node node) (type ir2-block block)
504 (list results) (type (or lvar null) lvar))
506 (let ((2lvar (lvar-info lvar)))
507 (ecase (ir2-lvar-kind 2lvar)
509 (let ((locs (ir2-lvar-locs 2lvar)))
510 (unless (eq locs results)
511 (move-results-coerced node block results locs))))
513 (let* ((nvals (length results))
514 (locs (make-standard-value-tns nvals)))
515 (move-results-coerced node block results locs)
516 (vop* push-values node block
517 ((reference-tn-list locs nil))
518 ((reference-tn-list (ir2-lvar-locs 2lvar) t))
523 (defun ir2-convert-cast (node block)
524 (declare (type cast node)
525 (type ir2-block block))
526 (binding* ((lvar (node-lvar node) :exit-if-null)
527 (2lvar (lvar-info lvar))
528 (value (cast-value node))
529 (2value (lvar-info value)))
530 (cond ((eq (ir2-lvar-kind 2lvar) :unused))
531 ((eq (ir2-lvar-kind 2lvar) :unknown)
532 (aver (eq (ir2-lvar-kind 2value) :unknown))
533 (aver (not (cast-type-check node)))
534 (move-results-coerced node block
535 (ir2-lvar-locs 2value)
536 (ir2-lvar-locs 2lvar)))
537 ((eq (ir2-lvar-kind 2lvar) :fixed)
538 (aver (eq (ir2-lvar-kind 2value) :fixed))
539 (if (cast-type-check node)
540 (move-results-checked node block
541 (ir2-lvar-locs 2value)
542 (ir2-lvar-locs 2lvar)
543 (multiple-value-bind (check types)
544 (cast-check-types node nil)
545 (aver (eq check :simple))
547 (move-results-coerced node block
548 (ir2-lvar-locs 2value)
549 (ir2-lvar-locs 2lvar))))
550 (t (bug "CAST cannot be :DELAYED.")))))
552 ;;;; template conversion
554 ;;; Build a TN-REFS list that represents access to the values of the
555 ;;; specified list of lvars ARGS for TEMPLATE. Any :CONSTANT arguments
556 ;;; are returned in the second value as a list rather than being
557 ;;; accessed as a normal argument. NODE and BLOCK provide the context
558 ;;; for emitting any necessary type-checking code.
559 (defun reference-args (node block args template)
560 (declare (type node node) (type ir2-block block) (list args)
561 (type template template))
562 (collect ((info-args))
565 (do ((args args (cdr args))
566 (types (template-arg-types template) (cdr types)))
568 (let ((type (first types))
570 (if (and (consp type) (eq (car type) ':constant))
571 (info-args (lvar-value arg))
572 (let ((ref (reference-tn (lvar-tn node block arg) nil)))
574 (setf (tn-ref-across last) ref)
578 (values (the (or tn-ref null) first) (info-args)))))
580 ;;; Convert a conditional template. We try to exploit any
581 ;;; drop-through, but emit an unconditional branch afterward if we
582 ;;; fail. NOT-P is true if the sense of the TEMPLATE's test should be
584 (defun ir2-convert-conditional (node block template args info-args if not-p)
585 (declare (type node node) (type ir2-block block)
586 (type template template) (type (or tn-ref null) args)
587 (list info-args) (type cif if) (type boolean not-p))
588 (aver (= (template-info-arg-count template) (+ (length info-args) 2)))
589 (let ((consequent (if-consequent if))
590 (alternative (if-alternative if)))
591 (cond ((drop-thru-p if consequent)
592 (emit-template node block template args nil
593 (list* (block-label alternative) (not not-p)
596 (emit-template node block template args nil
597 (list* (block-label consequent) not-p info-args))
598 (unless (drop-thru-p if alternative)
599 (vop branch node block (block-label alternative)))))))
601 ;;; Convert an IF that isn't the DEST of a conditional template.
602 (defun ir2-convert-if (node block)
603 (declare (type ir2-block block) (type cif node))
604 (let* ((test (if-test node))
605 (test-ref (reference-tn (lvar-tn node block test) nil))
606 (nil-ref (reference-tn (emit-constant nil) nil)))
607 (setf (tn-ref-across test-ref) nil-ref)
608 (ir2-convert-conditional node block (template-or-lose 'if-eq)
609 test-ref () node t)))
611 ;;; Return a list of primitive-types that we can pass to
612 ;;; LVAR-RESULT-TNS describing the result types we want for a
613 ;;; template call. We duplicate here the determination of output type
614 ;;; that was done in initially selecting the template, so we know that
615 ;;; the types we find are allowed by the template output type
617 (defun find-template-result-types (call template rtypes)
618 (declare (type combination call)
619 (type template template) (list rtypes))
620 (declare (ignore template))
621 (let* ((dtype (node-derived-type call))
623 (types (mapcar #'primitive-type
624 (if (values-type-p type)
625 (append (values-type-required type)
626 (values-type-optional type))
628 (let ((nvals (length rtypes))
629 (ntypes (length types)))
630 (cond ((< ntypes nvals)
632 (make-list (- nvals ntypes)
633 :initial-element *backend-t-primitive-type*)))
635 (subseq types 0 nvals))
639 ;;; Return a list of TNs usable in a CALL to TEMPLATE delivering
640 ;;; values to LVAR. As an efficiency hack, we pick off the common case
641 ;;; where the LVAR is fixed values and has locations that satisfy the
642 ;;; result restrictions. This can fail when there is a type check or a
643 ;;; values count mismatch.
644 (defun make-template-result-tns (call lvar template rtypes)
645 (declare (type combination call) (type (or lvar null) lvar)
646 (type template template) (list rtypes))
647 (let ((2lvar (when lvar (lvar-info lvar))))
648 (if (and 2lvar (eq (ir2-lvar-kind 2lvar) :fixed))
649 (let ((locs (ir2-lvar-locs 2lvar)))
650 (if (and (= (length rtypes) (length locs))
651 (do ((loc locs (cdr loc))
652 (rtype rtypes (cdr rtype)))
654 (unless (operand-restriction-ok
656 (tn-primitive-type (car loc))
662 (find-template-result-types call template rtypes))))
665 (find-template-result-types call template rtypes)))))
667 ;;; Get the operands into TNs, make TN-REFs for them, and then call
668 ;;; the template emit function.
669 (defun ir2-convert-template (call block)
670 (declare (type combination call) (type ir2-block block))
671 (let* ((template (combination-info call))
672 (lvar (node-lvar call))
673 (rtypes (template-result-types template)))
674 (multiple-value-bind (args info-args)
675 (reference-args call block (combination-args call) template)
676 (aver (not (template-more-results-type template)))
677 (if (eq rtypes :conditional)
678 (ir2-convert-conditional call block template args info-args
679 (lvar-dest lvar) nil)
680 (let* ((results (make-template-result-tns call lvar template rtypes))
681 (r-refs (reference-tn-list results t)))
682 (aver (= (length info-args)
683 (template-info-arg-count template)))
684 #!+stack-grows-downward-not-upward
685 (when (and lvar (lvar-dynamic-extent lvar))
686 (vop current-stack-pointer call block
687 (ir2-lvar-stack-pointer (lvar-info lvar))))
689 (emit-template call block template args r-refs info-args)
690 (emit-template call block template args r-refs))
691 (move-lvar-result call block results lvar)))))
694 ;;; We don't have to do much because operand count checking is done by
695 ;;; IR1 conversion. The only difference between this and the function
696 ;;; case of IR2-CONVERT-TEMPLATE is that there can be codegen-info
698 (defoptimizer (%%primitive ir2-convert) ((template info &rest args) call block)
699 (let* ((template (lvar-value template))
700 (info (lvar-value info))
701 (lvar (node-lvar call))
702 (rtypes (template-result-types template))
703 (results (make-template-result-tns call lvar template rtypes))
704 (r-refs (reference-tn-list results t)))
705 (multiple-value-bind (args info-args)
706 (reference-args call block (cddr (combination-args call)) template)
707 (aver (not (template-more-results-type template)))
708 (aver (not (eq rtypes :conditional)))
709 (aver (null info-args))
712 (emit-template call block template args r-refs info)
713 (emit-template call block template args r-refs))
715 (move-lvar-result call block results lvar)))
720 ;;; Convert a LET by moving the argument values into the variables.
721 ;;; Since a LET doesn't have any passing locations, we move the
722 ;;; arguments directly into the variables. We must also allocate any
723 ;;; indirect value cells, since there is no function prologue to do
725 (defun ir2-convert-let (node block fun)
726 (declare (type combination node) (type ir2-block block) (type clambda fun))
727 (mapc (lambda (var arg)
729 (let ((src (lvar-tn node block arg))
730 (dest (leaf-info var)))
731 (if (lambda-var-indirect var)
732 (do-make-value-cell node block src dest)
733 (emit-move node block src dest)))))
734 (lambda-vars fun) (basic-combination-args node))
737 ;;; Emit any necessary moves into assignment temps for a local call to
738 ;;; FUN. We return two lists of TNs: TNs holding the actual argument
739 ;;; values, and (possibly EQ) TNs that are the actual destination of
740 ;;; the arguments. When necessary, we allocate temporaries for
741 ;;; arguments to preserve parallel assignment semantics. These lists
742 ;;; exclude unused arguments and include implicit environment
743 ;;; arguments, i.e. they exactly correspond to the arguments passed.
745 ;;; OLD-FP is the TN currently holding the value we want to pass as
746 ;;; OLD-FP. If null, then the call is to the same environment (an
747 ;;; :ASSIGNMENT), so we only move the arguments, and leave the
748 ;;; environment alone.
749 (defun emit-psetq-moves (node block fun old-fp)
750 (declare (type combination node) (type ir2-block block) (type clambda fun)
751 (type (or tn null) old-fp))
752 (let ((actuals (mapcar (lambda (x)
754 (lvar-tn node block x)))
755 (combination-args node))))
758 (dolist (var (lambda-vars fun))
759 (let ((actual (pop actuals))
760 (loc (leaf-info var)))
763 ((lambda-var-indirect var)
765 (make-normal-tn *backend-t-primitive-type*)))
766 (do-make-value-cell node block actual temp)
768 ((member actual (locs))
769 (let ((temp (make-normal-tn (tn-primitive-type loc))))
770 (emit-move node block actual temp)
777 (let ((this-1env (node-physenv node))
778 (called-env (physenv-info (lambda-physenv fun))))
779 (dolist (thing (ir2-physenv-closure called-env))
780 (temps (find-in-physenv (car thing) this-1env))
783 (locs (ir2-physenv-old-fp called-env))))
785 (values (temps) (locs)))))
787 ;;; A tail-recursive local call is done by emitting moves of stuff
788 ;;; into the appropriate passing locations. After setting up the args
789 ;;; and environment, we just move our return-pc into the called
790 ;;; function's passing location.
791 (defun ir2-convert-tail-local-call (node block fun)
792 (declare (type combination node) (type ir2-block block) (type clambda fun))
793 (let ((this-env (physenv-info (node-physenv node))))
794 (multiple-value-bind (temps locs)
795 (emit-psetq-moves node block fun (ir2-physenv-old-fp this-env))
797 (mapc (lambda (temp loc)
798 (emit-move node block temp loc))
801 (emit-move node block
802 (ir2-physenv-return-pc this-env)
803 (ir2-physenv-return-pc-pass
805 (lambda-physenv fun)))))
809 ;;; Convert an :ASSIGNMENT call. This is just like a tail local call,
810 ;;; except that the caller and callee environment are the same, so we
811 ;;; don't need to mess with the environment locations, return PC, etc.
812 (defun ir2-convert-assignment (node block fun)
813 (declare (type combination node) (type ir2-block block) (type clambda fun))
814 (multiple-value-bind (temps locs) (emit-psetq-moves node block fun nil)
816 (mapc (lambda (temp loc)
817 (emit-move node block temp loc))
821 ;;; Do stuff to set up the arguments to a non-tail local call
822 ;;; (including implicit environment args.) We allocate a frame
823 ;;; (returning the FP and NFP), and also compute the TN-REFS list for
824 ;;; the values to pass and the list of passing location TNs.
825 (defun ir2-convert-local-call-args (node block fun)
826 (declare (type combination node) (type ir2-block block) (type clambda fun))
827 (let ((fp (make-stack-pointer-tn))
828 (nfp (make-number-stack-pointer-tn))
829 (old-fp (make-stack-pointer-tn)))
830 (multiple-value-bind (temps locs)
831 (emit-psetq-moves node block fun old-fp)
832 (vop current-fp node block old-fp)
833 (vop allocate-frame node block
834 (physenv-info (lambda-physenv fun))
836 (values fp nfp temps (mapcar #'make-alias-tn locs)))))
838 ;;; Handle a non-TR known-values local call. We emit the call, then
839 ;;; move the results to the lvar's destination.
840 (defun ir2-convert-local-known-call (node block fun returns lvar start)
841 (declare (type node node) (type ir2-block block) (type clambda fun)
842 (type return-info returns) (type (or lvar null) lvar)
844 (multiple-value-bind (fp nfp temps arg-locs)
845 (ir2-convert-local-call-args node block fun)
846 (let ((locs (return-info-locations returns)))
847 (vop* known-call-local node block
848 (fp nfp (reference-tn-list temps nil))
849 ((reference-tn-list locs t))
850 arg-locs (physenv-info (lambda-physenv fun)) start)
851 (move-lvar-result node block locs lvar)))
854 ;;; Handle a non-TR unknown-values local call. We do different things
855 ;;; depending on what kind of values the lvar wants.
857 ;;; If LVAR is :UNKNOWN, then we use the "multiple-" variant, directly
858 ;;; specifying the lvar's LOCS as the VOP results so that we don't
859 ;;; have to do anything after the call.
861 ;;; Otherwise, we use STANDARD-RESULT-TNS to get wired result TNs, and
862 ;;; then call MOVE-LVAR-RESULT to do any necessary type checks or
864 (defun ir2-convert-local-unknown-call (node block fun lvar start)
865 (declare (type node node) (type ir2-block block) (type clambda fun)
866 (type (or lvar null) lvar) (type label start))
867 (multiple-value-bind (fp nfp temps arg-locs)
868 (ir2-convert-local-call-args node block fun)
869 (let ((2lvar (and lvar (lvar-info lvar)))
870 (env (physenv-info (lambda-physenv fun)))
871 (temp-refs (reference-tn-list temps nil)))
872 (if (and 2lvar (eq (ir2-lvar-kind 2lvar) :unknown))
873 (vop* multiple-call-local node block (fp nfp temp-refs)
874 ((reference-tn-list (ir2-lvar-locs 2lvar) t))
876 (let ((locs (standard-result-tns lvar)))
877 (vop* call-local node block
879 ((reference-tn-list locs t))
880 arg-locs env start (length locs))
881 (move-lvar-result node block locs lvar)))))
884 ;;; Dispatch to the appropriate function, depending on whether we have
885 ;;; a let, tail or normal call. If the function doesn't return, call
886 ;;; it using the unknown-value convention. We could compile it as a
887 ;;; tail call, but that might seem confusing in the debugger.
888 (defun ir2-convert-local-call (node block)
889 (declare (type combination node) (type ir2-block block))
890 (let* ((fun (ref-leaf (lvar-uses (basic-combination-fun node))))
891 (kind (functional-kind fun)))
892 (cond ((eq kind :let)
893 (ir2-convert-let node block fun))
894 ((eq kind :assignment)
895 (ir2-convert-assignment node block fun))
897 (ir2-convert-tail-local-call node block fun))
899 (let ((start (block-label (lambda-block fun)))
900 (returns (tail-set-info (lambda-tail-set fun)))
901 (lvar (node-lvar node)))
903 (return-info-kind returns)
906 (ir2-convert-local-unknown-call node block fun lvar start))
908 (ir2-convert-local-known-call node block fun returns
914 ;;; Given a function lvar FUN, return (VALUES TN-TO-CALL NAMED-P),
915 ;;; where TN-TO-CALL is a TN holding the thing that we call NAMED-P is
916 ;;; true if the thing is named (false if it is a function).
918 ;;; There are two interesting non-named cases:
919 ;;; -- We know it's a function. No check needed: return the
921 ;;; -- We don't know what it is.
922 (defun fun-lvar-tn (node block lvar)
923 (declare (ignore node block))
924 (declare (type lvar lvar))
925 (let ((2lvar (lvar-info lvar)))
926 (if (eq (ir2-lvar-kind 2lvar) :delayed)
927 (let ((name (lvar-fun-name lvar t)))
929 (values (make-load-time-constant-tn :fdefinition name) t))
930 (let* ((locs (ir2-lvar-locs 2lvar))
932 (function-ptype (primitive-type-or-lose 'function)))
933 (aver (and (eq (ir2-lvar-kind 2lvar) :fixed)
934 (= (length locs) 1)))
935 (aver (eq (tn-primitive-type loc) function-ptype))
938 ;;; Set up the args to NODE in the current frame, and return a TN-REF
939 ;;; list for the passing locations.
940 (defun move-tail-full-call-args (node block)
941 (declare (type combination node) (type ir2-block block))
942 (let ((args (basic-combination-args node))
945 (dotimes (num (length args))
946 (let ((loc (standard-arg-location num)))
947 (emit-move node block (lvar-tn node block (elt args num)) loc)
948 (let ((ref (reference-tn loc nil)))
950 (setf (tn-ref-across last) ref)
955 ;;; Move the arguments into the passing locations and do a (possibly
956 ;;; named) tail call.
957 (defun ir2-convert-tail-full-call (node block)
958 (declare (type combination node) (type ir2-block block))
959 (let* ((env (physenv-info (node-physenv node)))
960 (args (basic-combination-args node))
961 (nargs (length args))
962 (pass-refs (move-tail-full-call-args node block))
963 (old-fp (ir2-physenv-old-fp env))
964 (return-pc (ir2-physenv-return-pc env)))
966 (multiple-value-bind (fun-tn named)
967 (fun-lvar-tn node block (basic-combination-fun node))
969 (vop* tail-call-named node block
970 (fun-tn old-fp return-pc pass-refs)
973 (vop* tail-call node block
974 (fun-tn old-fp return-pc pass-refs)
980 ;;; like IR2-CONVERT-LOCAL-CALL-ARGS, only different
981 (defun ir2-convert-full-call-args (node block)
982 (declare (type combination node) (type ir2-block block))
983 (let* ((args (basic-combination-args node))
984 (fp (make-stack-pointer-tn))
985 (nargs (length args)))
986 (vop allocate-full-call-frame node block nargs fp)
991 (locs (standard-arg-location num))
992 (let ((ref (reference-tn (lvar-tn node block (elt args num))
995 (setf (tn-ref-across last) ref)
999 (values fp first (locs) nargs)))))
1001 ;;; Do full call when a fixed number of values are desired. We make
1002 ;;; STANDARD-RESULT-TNS for our lvar, then deliver the result using
1003 ;;; MOVE-LVAR-RESULT. We do named or normal call, as appropriate.
1004 (defun ir2-convert-fixed-full-call (node block)
1005 (declare (type combination node) (type ir2-block block))
1006 (multiple-value-bind (fp args arg-locs nargs)
1007 (ir2-convert-full-call-args node block)
1008 (let* ((lvar (node-lvar node))
1009 (locs (standard-result-tns lvar))
1010 (loc-refs (reference-tn-list locs t))
1011 (nvals (length locs)))
1012 (multiple-value-bind (fun-tn named)
1013 (fun-lvar-tn node block (basic-combination-fun node))
1015 (vop* call-named node block (fp fun-tn args) (loc-refs)
1016 arg-locs nargs nvals)
1017 (vop* call node block (fp fun-tn args) (loc-refs)
1018 arg-locs nargs nvals))
1019 (move-lvar-result node block locs lvar))))
1022 ;;; Do full call when unknown values are desired.
1023 (defun ir2-convert-multiple-full-call (node block)
1024 (declare (type combination node) (type ir2-block block))
1025 (multiple-value-bind (fp args arg-locs nargs)
1026 (ir2-convert-full-call-args node block)
1027 (let* ((lvar (node-lvar node))
1028 (locs (ir2-lvar-locs (lvar-info lvar)))
1029 (loc-refs (reference-tn-list locs t)))
1030 (multiple-value-bind (fun-tn named)
1031 (fun-lvar-tn node block (basic-combination-fun node))
1033 (vop* multiple-call-named node block (fp fun-tn args) (loc-refs)
1035 (vop* multiple-call node block (fp fun-tn args) (loc-refs)
1039 ;;; stuff to check in PONDER-FULL-CALL
1041 ;;; There are some things which are intended always to be optimized
1042 ;;; away by DEFTRANSFORMs and such, and so never compiled into full
1043 ;;; calls. This has been a source of bugs so many times that it seems
1044 ;;; worth listing some of them here so that we can check the list
1045 ;;; whenever we compile a full call.
1047 ;;; FIXME: It might be better to represent this property by setting a
1048 ;;; flag in DEFKNOWN, instead of representing it by membership in this
1050 (defvar *always-optimized-away*
1051 '(;; This should always be DEFTRANSFORMed away, but wasn't in a bug
1052 ;; reported to cmucl-imp 2000-06-20.
1054 ;; These should always turn into VOPs, but wasn't in a bug which
1055 ;; appeared when LTN-POLICY stuff was being tweaked in
1056 ;; sbcl-0.6.9.16. in sbcl-0.6.0
1060 ;;; more stuff to check in PONDER-FULL-CALL
1062 ;;; These came in handy when troubleshooting cold boot after making
1063 ;;; major changes in the package structure: various transforms and
1064 ;;; VOPs and stuff got attached to the wrong symbol, so that
1065 ;;; references to the right symbol were bogusly translated as full
1066 ;;; calls instead of primitives, sending the system off into infinite
1067 ;;; space. Having a report on all full calls generated makes it easier
1068 ;;; to figure out what form caused the problem this time.
1069 #!+sb-show (defvar *show-full-called-fnames-p* nil)
1070 #!+sb-show (defvar *full-called-fnames* (make-hash-table :test 'equal))
1072 ;;; Do some checks (and store some notes relevant for future checks)
1074 ;;; * Is this a full call to something we have reason to know should
1075 ;;; never be full called? (Except as of sbcl-0.7.18 or so, we no
1076 ;;; longer try to ensure this behavior when *FAILURE-P* has already
1078 ;;; * Is this a full call to (SETF FOO) which might conflict with
1079 ;;; a DEFSETF or some such thing elsewhere in the program?
1080 (defun ponder-full-call (node)
1081 (let* ((lvar (basic-combination-fun node))
1082 (fname (lvar-fun-name lvar t)))
1083 (declare (type (or symbol cons) fname))
1085 #!+sb-show (unless (gethash fname *full-called-fnames*)
1086 (setf (gethash fname *full-called-fnames*) t))
1087 #!+sb-show (when *show-full-called-fnames-p*
1088 (/show "converting full call to named function" fname)
1089 (/show (basic-combination-args node))
1090 (/show (policy node speed) (policy node safety))
1091 (/show (policy node compilation-speed))
1092 (let ((arg-types (mapcar (lambda (lvar)
1096 (basic-combination-args node))))
1099 ;; When illegal code is compiled, all sorts of perverse paths
1100 ;; through the compiler can be taken, and it's much harder -- and
1101 ;; probably pointless -- to guarantee that always-optimized-away
1102 ;; functions are actually optimized away. Thus, we skip the check
1105 (when (memq fname *always-optimized-away*)
1106 (/show (policy node speed) (policy node safety))
1107 (/show (policy node compilation-speed))
1108 (bug "full call to ~S" fname)))
1111 (aver (legal-fun-name-p fname))
1112 (destructuring-bind (setfoid &rest stem) fname
1113 (when (eq setfoid 'setf)
1114 (setf (gethash (car stem) *setf-assumed-fboundp*) t))))))
1116 ;;; If the call is in a tail recursive position and the return
1117 ;;; convention is standard, then do a tail full call. If one or fewer
1118 ;;; values are desired, then use a single-value call, otherwise use a
1119 ;;; multiple-values call.
1120 (defun ir2-convert-full-call (node block)
1121 (declare (type combination node) (type ir2-block block))
1122 (ponder-full-call node)
1123 (cond ((node-tail-p node)
1124 (ir2-convert-tail-full-call node block))
1125 ((let ((lvar (node-lvar node)))
1127 (eq (ir2-lvar-kind (lvar-info lvar)) :unknown)))
1128 (ir2-convert-multiple-full-call node block))
1130 (ir2-convert-fixed-full-call node block)))
1133 ;;;; entering functions
1135 ;;; Do all the stuff that needs to be done on XEP entry:
1136 ;;; -- Create frame.
1137 ;;; -- Copy any more arg.
1138 ;;; -- Set up the environment, accessing any closure variables.
1139 ;;; -- Move args from the standard passing locations to their internal
1141 (defun init-xep-environment (node block fun)
1142 (declare (type bind node) (type ir2-block block) (type clambda fun))
1143 (let ((start-label (entry-info-offset (leaf-info fun)))
1144 (env (physenv-info (node-physenv node))))
1145 (let ((ef (functional-entry-fun fun)))
1146 (cond ((and (optional-dispatch-p ef) (optional-dispatch-more-entry ef))
1147 ;; Special case the xep-allocate-frame + copy-more-arg case.
1148 (vop xep-allocate-frame node block start-label t)
1149 (vop copy-more-arg node block (optional-dispatch-max-args ef)))
1151 ;; No more args, so normal entry.
1152 (vop xep-allocate-frame node block start-label nil)))
1153 (if (ir2-physenv-closure env)
1154 (let ((closure (make-normal-tn *backend-t-primitive-type*)))
1155 (vop setup-closure-environment node block start-label closure)
1156 (when (getf (functional-plist ef) :fin-function)
1157 (vop funcallable-instance-lexenv node block closure closure))
1159 (dolist (loc (ir2-physenv-closure env))
1160 (vop closure-ref node block closure (incf n) (cdr loc)))))
1161 (vop setup-environment node block start-label)))
1163 (unless (eq (functional-kind fun) :toplevel)
1164 (let ((vars (lambda-vars fun))
1166 (when (leaf-refs (first vars))
1167 (emit-move node block (make-arg-count-location)
1168 (leaf-info (first vars))))
1169 (dolist (arg (rest vars))
1170 (when (leaf-refs arg)
1171 (let ((pass (standard-arg-location n))
1172 (home (leaf-info arg)))
1173 (if (lambda-var-indirect arg)
1174 (do-make-value-cell node block pass home)
1175 (emit-move node block pass home))))
1178 (emit-move node block (make-old-fp-passing-location t)
1179 (ir2-physenv-old-fp env)))
1183 ;;; Emit function prolog code. This is only called on bind nodes for
1184 ;;; functions that allocate environments. All semantics of let calls
1185 ;;; are handled by IR2-CONVERT-LET.
1187 ;;; If not an XEP, all we do is move the return PC from its passing
1188 ;;; location, since in a local call, the caller allocates the frame
1189 ;;; and sets up the arguments.
1190 (defun ir2-convert-bind (node block)
1191 (declare (type bind node) (type ir2-block block))
1192 (let* ((fun (bind-lambda node))
1193 (env (physenv-info (lambda-physenv fun))))
1194 (aver (member (functional-kind fun)
1195 '(nil :external :optional :toplevel :cleanup)))
1198 (init-xep-environment node block fun)
1200 (when *collect-dynamic-statistics*
1201 (vop count-me node block *dynamic-counts-tn*
1202 (block-number (ir2-block-block block)))))
1206 (ir2-physenv-return-pc-pass env)
1207 (ir2-physenv-return-pc env))
1209 (let ((lab (gen-label)))
1210 (setf (ir2-physenv-environment-start env) lab)
1211 (vop note-environment-start node block lab)))
1215 ;;;; function return
1217 ;;; Do stuff to return from a function with the specified values and
1218 ;;; convention. If the return convention is :FIXED and we aren't
1219 ;;; returning from an XEP, then we do a known return (letting
1220 ;;; representation selection insert the correct move-arg VOPs.)
1221 ;;; Otherwise, we use the unknown-values convention. If there is a
1222 ;;; fixed number of return values, then use RETURN, otherwise use
1223 ;;; RETURN-MULTIPLE.
1224 (defun ir2-convert-return (node block)
1225 (declare (type creturn node) (type ir2-block block))
1226 (let* ((lvar (return-result node))
1227 (2lvar (lvar-info lvar))
1228 (lvar-kind (ir2-lvar-kind 2lvar))
1229 (fun (return-lambda node))
1230 (env (physenv-info (lambda-physenv fun)))
1231 (old-fp (ir2-physenv-old-fp env))
1232 (return-pc (ir2-physenv-return-pc env))
1233 (returns (tail-set-info (lambda-tail-set fun))))
1235 ((and (eq (return-info-kind returns) :fixed)
1237 (let ((locs (lvar-tns node block lvar
1238 (return-info-types returns))))
1239 (vop* known-return node block
1240 (old-fp return-pc (reference-tn-list locs nil))
1242 (return-info-locations returns))))
1243 ((eq lvar-kind :fixed)
1244 (let* ((types (mapcar #'tn-primitive-type (ir2-lvar-locs 2lvar)))
1245 (lvar-locs (lvar-tns node block lvar types))
1246 (nvals (length lvar-locs))
1247 (locs (make-standard-value-tns nvals)))
1248 (mapc (lambda (val loc)
1249 (emit-move node block val loc))
1253 (vop return-single node block old-fp return-pc (car locs))
1254 (vop* return node block
1255 (old-fp return-pc (reference-tn-list locs nil))
1259 (aver (eq lvar-kind :unknown))
1260 (vop* return-multiple node block
1262 (reference-tn-list (ir2-lvar-locs 2lvar) nil))
1269 ;;; This is used by the debugger to find the top function on the
1270 ;;; stack. It returns the OLD-FP and RETURN-PC for the current
1271 ;;; function as multiple values.
1272 (defoptimizer (sb!kernel:%caller-frame-and-pc ir2-convert) (() node block)
1273 (let ((ir2-physenv (physenv-info (node-physenv node))))
1274 (move-lvar-result node block
1275 (list (ir2-physenv-old-fp ir2-physenv)
1276 (ir2-physenv-return-pc ir2-physenv))
1279 ;;;; multiple values
1281 ;;; This is almost identical to IR2-CONVERT-LET. Since LTN annotates
1282 ;;; the lvar for the correct number of values (with the lvar user
1283 ;;; responsible for defaulting), we can just pick them up from the
1285 (defun ir2-convert-mv-bind (node block)
1286 (declare (type mv-combination node) (type ir2-block block))
1287 (let* ((lvar (first (basic-combination-args node)))
1288 (fun (ref-leaf (lvar-uses (basic-combination-fun node))))
1289 (vars (lambda-vars fun)))
1290 (aver (eq (functional-kind fun) :mv-let))
1291 (mapc (lambda (src var)
1292 (when (leaf-refs var)
1293 (let ((dest (leaf-info var)))
1294 (if (lambda-var-indirect var)
1295 (do-make-value-cell node block src dest)
1296 (emit-move node block src dest)))))
1297 (lvar-tns node block lvar
1299 (primitive-type (leaf-type x)))
1304 ;;; Emit the appropriate fixed value, unknown value or tail variant of
1305 ;;; CALL-VARIABLE. Note that we only need to pass the values start for
1306 ;;; the first argument: all the other argument lvar TNs are
1307 ;;; ignored. This is because we require all of the values globs to be
1308 ;;; contiguous and on stack top.
1309 (defun ir2-convert-mv-call (node block)
1310 (declare (type mv-combination node) (type ir2-block block))
1311 (aver (basic-combination-args node))
1312 (let* ((start-lvar (lvar-info (first (basic-combination-args node))))
1313 (start (first (ir2-lvar-locs start-lvar)))
1314 (tails (and (node-tail-p node)
1315 (lambda-tail-set (node-home-lambda node))))
1316 (lvar (node-lvar node))
1317 (2lvar (and lvar (lvar-info lvar))))
1318 (multiple-value-bind (fun named)
1319 (fun-lvar-tn node block (basic-combination-fun node))
1320 (aver (and (not named)
1321 (eq (ir2-lvar-kind start-lvar) :unknown)))
1324 (let ((env (physenv-info (node-physenv node))))
1325 (vop tail-call-variable node block start fun
1326 (ir2-physenv-old-fp env)
1327 (ir2-physenv-return-pc env))))
1329 (eq (ir2-lvar-kind 2lvar) :unknown))
1330 (vop* multiple-call-variable node block (start fun nil)
1331 ((reference-tn-list (ir2-lvar-locs 2lvar) t))))
1333 (let ((locs (standard-result-tns lvar)))
1334 (vop* call-variable node block (start fun nil)
1335 ((reference-tn-list locs t)) (length locs))
1336 (move-lvar-result node block locs lvar)))))))
1338 ;;; Reset the stack pointer to the start of the specified
1339 ;;; unknown-values lvar (discarding it and all values globs on top of
1341 (defoptimizer (%pop-values ir2-convert) ((%lvar) node block)
1342 (let* ((lvar (lvar-value %lvar))
1343 (2lvar (lvar-info lvar)))
1344 (cond ((eq (ir2-lvar-kind 2lvar) :unknown)
1345 (vop reset-stack-pointer node block
1346 (first (ir2-lvar-locs 2lvar))))
1347 ((lvar-dynamic-extent lvar)
1348 #!+stack-grows-downward-not-upward
1349 (vop reset-stack-pointer node block
1350 (ir2-lvar-stack-pointer 2lvar))
1351 #!-stack-grows-downward-not-upward
1352 (vop %%pop-dx node block
1353 (first (ir2-lvar-locs 2lvar))))
1354 (t (bug "Trying to pop a not stack-allocated LVAR ~S."
1357 (defoptimizer (%nip-values ir2-convert) ((last-nipped last-preserved
1360 (let* ( ;; pointer immediately after the nipped block
1361 (after (lvar-value last-nipped))
1362 (2after (lvar-info after))
1363 ;; pointer to the first nipped word
1364 (first (lvar-value last-preserved))
1365 (2first (lvar-info first))
1367 (moved-tns (loop for lvar-ref in moved
1368 for lvar = (lvar-value lvar-ref)
1369 for 2lvar = (lvar-info lvar)
1371 collect (first (ir2-lvar-locs 2lvar)))))
1372 (aver (or (eq (ir2-lvar-kind 2after) :unknown)
1373 (lvar-dynamic-extent after)))
1374 (aver (eq (ir2-lvar-kind 2first) :unknown))
1375 (when *check-consistency*
1376 ;; we cannot move stack-allocated DX objects
1377 (dolist (moved-lvar moved)
1378 (aver (eq (ir2-lvar-kind (lvar-info (lvar-value moved-lvar)))
1380 (flet ((nip-aligned (nipped)
1381 (vop* %%nip-values node block
1383 (first (ir2-lvar-locs 2first))
1384 (reference-tn-list moved-tns nil))
1385 ((reference-tn-list moved-tns t))))
1386 #!-stack-grows-downward-not-upward
1387 (nip-unaligned (nipped)
1388 (vop* %%nip-dx node block
1390 (first (ir2-lvar-locs 2first))
1391 (reference-tn-list moved-tns nil))
1392 ((reference-tn-list moved-tns t)))))
1393 (cond ((eq (ir2-lvar-kind 2after) :unknown)
1394 (nip-aligned (first (ir2-lvar-locs 2after))))
1395 ((lvar-dynamic-extent after)
1396 #!+stack-grows-downward-not-upward
1397 (nip-aligned (ir2-lvar-stack-pointer 2after))
1398 #!-stack-grows-downward-not-upward
1399 (nip-unaligned (ir2-lvar-stack-pointer 2after)))
1401 (bug "Trying to nip a not stack-allocated LVAR ~S." after))))))
1403 ;;; Deliver the values TNs to LVAR using MOVE-LVAR-RESULT.
1404 (defoptimizer (values ir2-convert) ((&rest values) node block)
1405 (let ((tns (mapcar (lambda (x)
1406 (lvar-tn node block x))
1408 (move-lvar-result node block tns (node-lvar node))))
1410 ;;; In the normal case where unknown values are desired, we use the
1411 ;;; VALUES-LIST VOP. In the relatively unimportant case of VALUES-LIST
1412 ;;; for a fixed number of values, we punt by doing a full call to the
1413 ;;; VALUES-LIST function. This gets the full call VOP to deal with
1414 ;;; defaulting any unsupplied values. It seems unworthwhile to
1415 ;;; optimize this case.
1416 (defoptimizer (values-list ir2-convert) ((list) node block)
1417 (let* ((lvar (node-lvar node))
1418 (2lvar (and lvar (lvar-info lvar))))
1420 (eq (ir2-lvar-kind 2lvar) :unknown))
1421 (let ((locs (ir2-lvar-locs 2lvar)))
1422 (vop* values-list node block
1423 ((lvar-tn node block list) nil)
1424 ((reference-tn-list locs t)))))
1425 (t (aver (or (not 2lvar) ; i.e. we want to check the argument
1426 (eq (ir2-lvar-kind 2lvar) :fixed)))
1427 (ir2-convert-full-call node block)))))
1429 (defoptimizer (%more-arg-values ir2-convert) ((context start count) node block)
1430 (binding* ((lvar (node-lvar node) :exit-if-null)
1431 (2lvar (lvar-info lvar)))
1432 (ecase (ir2-lvar-kind 2lvar)
1433 (:fixed (ir2-convert-full-call node block))
1435 (let ((locs (ir2-lvar-locs 2lvar)))
1436 (vop* %more-arg-values node block
1437 ((lvar-tn node block context)
1438 (lvar-tn node block start)
1439 (lvar-tn node block count)
1441 ((reference-tn-list locs t))))))))
1443 ;;;; special binding
1445 ;;; This is trivial, given our assumption of a shallow-binding
1447 (defoptimizer (%special-bind ir2-convert) ((var value) node block)
1448 (let ((name (leaf-source-name (lvar-value var))))
1449 (vop bind node block (lvar-tn node block value)
1450 (emit-constant name))))
1451 (defoptimizer (%special-unbind ir2-convert) ((var) node block)
1452 (vop unbind node block))
1454 ;;; ### It's not clear that this really belongs in this file, or
1455 ;;; should really be done this way, but this is the least violation of
1456 ;;; abstraction in the current setup. We don't want to wire
1457 ;;; shallow-binding assumptions into IR1tran.
1458 (def-ir1-translator progv
1459 ((vars vals &body body) start next result)
1462 (with-unique-names (bind unbind)
1463 (once-only ((n-save-bs '(%primitive current-binding-pointer)))
1466 (labels ((,unbind (vars)
1467 (declare (optimize (speed 2) (debug 0)))
1469 (%primitive bind nil var)
1472 (declare (optimize (speed 2) (debug 0)))
1474 ((null vals) (,unbind vars))
1478 (,bind (cdr vars) (cdr vals))))))
1479 (,bind ,vars ,vals))
1482 (%primitive unbind-to-here ,n-save-bs))))))
1486 ;;; Convert a non-local lexical exit. First find the NLX-INFO in our
1487 ;;; environment. Note that this is never called on the escape exits
1488 ;;; for CATCH and UNWIND-PROTECT, since the escape functions aren't
1490 (defun ir2-convert-exit (node block)
1491 (declare (type exit node) (type ir2-block block))
1492 (let* ((nlx (exit-nlx-info node))
1493 (loc (find-in-physenv nlx (node-physenv node)))
1494 (temp (make-stack-pointer-tn))
1495 (value (exit-value node)))
1496 (if (nlx-info-safe-p nlx)
1497 (vop value-cell-ref node block loc temp)
1498 (emit-move node block loc temp))
1500 (let ((locs (ir2-lvar-locs (lvar-info value))))
1501 (vop unwind node block temp (first locs) (second locs)))
1502 (let ((0-tn (emit-constant 0)))
1503 (vop unwind node block temp 0-tn 0-tn))))
1507 ;;; %CLEANUP-POINT doesn't do anything except prevent the body from
1508 ;;; being entirely deleted.
1509 (defoptimizer (%cleanup-point ir2-convert) (() node block) node block)
1511 ;;; This function invalidates a lexical exit on exiting from the
1512 ;;; dynamic extent. This is done by storing 0 into the indirect value
1513 ;;; cell that holds the closed unwind block.
1514 (defoptimizer (%lexical-exit-breakup ir2-convert) ((info) node block)
1515 (let ((nlx (lvar-value info)))
1516 (when (nlx-info-safe-p nlx)
1517 (vop value-cell-set node block
1518 (find-in-physenv nlx (node-physenv node))
1519 (emit-constant 0)))))
1521 ;;; We have to do a spurious move of no values to the result lvar so
1522 ;;; that lifetime analysis won't get confused.
1523 (defun ir2-convert-throw (node block)
1524 (declare (type mv-combination node) (type ir2-block block))
1525 (let ((args (basic-combination-args node)))
1526 (check-catch-tag-type (first args))
1527 (vop* throw node block
1528 ((lvar-tn node block (first args))
1530 (ir2-lvar-locs (lvar-info (second args)))
1533 (move-lvar-result node block () (node-lvar node))
1536 ;;; Emit code to set up a non-local exit. INFO is the NLX-INFO for the
1537 ;;; exit, and TAG is the lvar for the catch tag (if any.) We get at
1538 ;;; the target PC by passing in the label to the vop. The vop is
1539 ;;; responsible for building a return-PC object.
1540 (defun emit-nlx-start (node block info tag)
1541 (declare (type node node) (type ir2-block block) (type nlx-info info)
1542 (type (or lvar null) tag))
1543 (let* ((2info (nlx-info-info info))
1544 (kind (cleanup-kind (nlx-info-cleanup info)))
1545 (block-tn (physenv-live-tn
1546 (make-normal-tn (primitive-type-or-lose 'catch-block))
1547 (node-physenv node)))
1548 (res (make-stack-pointer-tn))
1549 (target-label (ir2-nlx-info-target 2info)))
1551 (vop current-binding-pointer node block
1552 (car (ir2-nlx-info-dynamic-state 2info)))
1553 (vop* save-dynamic-state node block
1555 ((reference-tn-list (cdr (ir2-nlx-info-dynamic-state 2info)) t)))
1556 (vop current-stack-pointer node block (ir2-nlx-info-save-sp 2info))
1560 (vop make-catch-block node block block-tn
1561 (lvar-tn node block tag) target-label res))
1562 ((:unwind-protect :block :tagbody)
1563 (vop make-unwind-block node block block-tn target-label res)))
1567 (if (nlx-info-safe-p info)
1568 (do-make-value-cell node block res (ir2-nlx-info-home 2info))
1569 (emit-move node block res (ir2-nlx-info-home 2info))))
1571 (vop set-unwind-protect node block block-tn))
1576 ;;; Scan each of ENTRY's exits, setting up the exit for each lexical exit.
1577 (defun ir2-convert-entry (node block)
1578 (declare (type entry node) (type ir2-block block))
1580 (dolist (exit (entry-exits node))
1581 (let ((info (exit-nlx-info exit)))
1583 (not (memq info nlxes))
1584 (member (cleanup-kind (nlx-info-cleanup info))
1585 '(:block :tagbody)))
1587 (emit-nlx-start node block info nil)))))
1590 ;;; Set up the unwind block for these guys.
1591 (defoptimizer (%catch ir2-convert) ((info-lvar tag) node block)
1592 (check-catch-tag-type tag)
1593 (emit-nlx-start node block (lvar-value info-lvar) tag))
1594 (defoptimizer (%unwind-protect ir2-convert) ((info-lvar cleanup) node block)
1595 (emit-nlx-start node block (lvar-value info-lvar) nil))
1597 ;;; Emit the entry code for a non-local exit. We receive values and
1598 ;;; restore dynamic state.
1600 ;;; In the case of a lexical exit or CATCH, we look at the exit lvar's
1601 ;;; kind to determine which flavor of entry VOP to emit. If unknown
1602 ;;; values, emit the xxx-MULTIPLE variant to the lvar locs. If fixed
1603 ;;; values, make the appropriate number of temps in the standard
1604 ;;; values locations and use the other variant, delivering the temps
1605 ;;; to the lvar using MOVE-LVAR-RESULT.
1607 ;;; In the UNWIND-PROTECT case, we deliver the first register
1608 ;;; argument, the argument count and the argument pointer to our lvar
1609 ;;; as multiple values. These values are the block exited to and the
1610 ;;; values start and count.
1612 ;;; After receiving values, we restore dynamic state. Except in the
1613 ;;; UNWIND-PROTECT case, the values receiving restores the stack
1614 ;;; pointer. In an UNWIND-PROTECT cleanup, we want to leave the stack
1615 ;;; pointer alone, since the thrown values are still out there.
1616 (defoptimizer (%nlx-entry ir2-convert) ((info-lvar) node block)
1617 (let* ((info (lvar-value info-lvar))
1618 (lvar (node-lvar node))
1619 (2info (nlx-info-info info))
1620 (top-loc (ir2-nlx-info-save-sp 2info))
1621 (start-loc (make-nlx-entry-arg-start-location))
1622 (count-loc (make-arg-count-location))
1623 (target (ir2-nlx-info-target 2info)))
1625 (ecase (cleanup-kind (nlx-info-cleanup info))
1626 ((:catch :block :tagbody)
1627 (let ((2lvar (and lvar (lvar-info lvar))))
1628 (if (and 2lvar (eq (ir2-lvar-kind 2lvar) :unknown))
1629 (vop* nlx-entry-multiple node block
1630 (top-loc start-loc count-loc nil)
1631 ((reference-tn-list (ir2-lvar-locs 2lvar) t))
1633 (let ((locs (standard-result-tns lvar)))
1634 (vop* nlx-entry node block
1635 (top-loc start-loc count-loc nil)
1636 ((reference-tn-list locs t))
1639 (move-lvar-result node block locs lvar)))))
1641 (let ((block-loc (standard-arg-location 0)))
1642 (vop uwp-entry node block target block-loc start-loc count-loc)
1645 (list block-loc start-loc count-loc)
1649 (when *collect-dynamic-statistics*
1650 (vop count-me node block *dynamic-counts-tn*
1651 (block-number (ir2-block-block block))))
1653 (vop* restore-dynamic-state node block
1654 ((reference-tn-list (cdr (ir2-nlx-info-dynamic-state 2info)) nil))
1656 (vop unbind-to-here node block
1657 (car (ir2-nlx-info-dynamic-state 2info)))))
1659 ;;;; n-argument functions
1661 (macrolet ((def (name)
1662 `(defoptimizer (,name ir2-convert) ((&rest args) node block)
1663 (let* ((refs (move-tail-full-call-args node block))
1664 (lvar (node-lvar node))
1665 (res (lvar-result-tns
1667 (list (primitive-type (specifier-type 'list))))))
1668 #!+stack-grows-downward-not-upward
1669 (when (and lvar (lvar-dynamic-extent lvar))
1670 (vop current-stack-pointer node block
1671 (ir2-lvar-stack-pointer (lvar-info lvar))))
1672 (vop* ,name node block (refs) ((first res) nil)
1674 (move-lvar-result node block res lvar)))))
1679 ;;; Convert the code in a component into VOPs.
1680 (defun ir2-convert (component)
1681 (declare (type component component))
1682 (let (#!+sb-dyncount
1683 (*dynamic-counts-tn*
1684 (when *collect-dynamic-statistics*
1686 (block-number (block-next (component-head component))))
1687 (counts (make-array blocks
1688 :element-type '(unsigned-byte 32)
1689 :initial-element 0))
1690 (info (make-dyncount-info
1691 :for (component-name component)
1692 :costs (make-array blocks
1693 :element-type '(unsigned-byte 32)
1696 (setf (ir2-component-dyncount-info (component-info component))
1698 (emit-constant info)
1699 (emit-constant counts)))))
1701 (declare (type index num))
1702 (do-ir2-blocks (2block component)
1703 (let ((block (ir2-block-block 2block)))
1704 (when (block-start block)
1705 (setf (block-number block) num)
1707 (when *collect-dynamic-statistics*
1708 (let ((first-node (block-start-node block)))
1709 (unless (or (and (bind-p first-node)
1710 (xep-p (bind-lambda first-node)))
1712 (node-lvar first-node))
1717 #!+sb-dyncount *dynamic-counts-tn* #!-sb-dyncount nil
1719 (ir2-convert-block block)
1723 ;;; If necessary, emit a terminal unconditional branch to go to the
1724 ;;; successor block. If the successor is the component tail, then
1725 ;;; there isn't really any successor, but if the end is an unknown,
1726 ;;; non-tail call, then we emit an error trap just in case the
1727 ;;; function really does return.
1728 (defun finish-ir2-block (block)
1729 (declare (type cblock block))
1730 (let* ((2block (block-info block))
1731 (last (block-last block))
1732 (succ (block-succ block)))
1734 (aver (singleton-p succ))
1735 (let ((target (first succ)))
1736 (cond ((eq target (component-tail (block-component block)))
1737 (when (and (basic-combination-p last)
1738 (eq (basic-combination-kind last) :full))
1739 (let* ((fun (basic-combination-fun last))
1740 (use (lvar-uses fun))
1741 (name (and (ref-p use)
1742 (leaf-has-source-name-p (ref-leaf use))
1743 (leaf-source-name (ref-leaf use)))))
1744 (unless (or (node-tail-p last)
1745 (info :function :info name)
1746 (policy last (zerop safety)))
1747 (vop nil-fun-returned-error last 2block
1749 (emit-constant name)
1750 (multiple-value-bind (tn named)
1751 (fun-lvar-tn last 2block fun)
1754 ((not (eq (ir2-block-next 2block) (block-info target)))
1755 (vop branch last 2block (block-label target)))))))
1759 ;;; Convert the code in a block into VOPs.
1760 (defun ir2-convert-block (block)
1761 (declare (type cblock block))
1762 (let ((2block (block-info block)))
1763 (do-nodes (node lvar block)
1767 (let ((2lvar (lvar-info lvar)))
1768 ;; function REF in a local call is not annotated
1769 (when (and 2lvar (not (eq (ir2-lvar-kind 2lvar) :delayed)))
1770 (ir2-convert-ref node 2block)))))
1772 (let ((kind (basic-combination-kind node)))
1775 (ir2-convert-local-call node 2block))
1777 (ir2-convert-full-call node 2block))
1779 (let* ((info (basic-combination-fun-info node))
1780 (fun (fun-info-ir2-convert info)))
1782 (funcall fun node 2block))
1783 ((eq (basic-combination-info node) :full)
1784 (ir2-convert-full-call node 2block))
1786 (ir2-convert-template node 2block))))))))
1788 (when (lvar-info (if-test node))
1789 (ir2-convert-if node 2block)))
1791 (let ((fun (bind-lambda node)))
1792 (when (eq (lambda-home fun) fun)
1793 (ir2-convert-bind node 2block))))
1795 (ir2-convert-return node 2block))
1797 (ir2-convert-set node 2block))
1799 (ir2-convert-cast node 2block))
1802 ((eq (basic-combination-kind node) :local)
1803 (ir2-convert-mv-bind node 2block))
1804 ((eq (lvar-fun-name (basic-combination-fun node))
1806 (ir2-convert-throw node 2block))
1808 (ir2-convert-mv-call node 2block))))
1810 (when (exit-entry node)
1811 (ir2-convert-exit node 2block)))
1813 (ir2-convert-entry node 2block)))))
1815 (finish-ir2-block block)