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 (let ((lab (gen-label)))
1183 (setf (ir2-physenv-environment-start env) lab)
1184 (vop note-environment-start node block lab)))
1188 ;;;; function return
1190 ;;; Do stuff to return from a function with the specified values and
1191 ;;; convention. If the return convention is :FIXED and we aren't
1192 ;;; returning from an XEP, then we do a known return (letting
1193 ;;; representation selection insert the correct move-arg VOPs.)
1194 ;;; Otherwise, we use the unknown-values convention. If there is a
1195 ;;; fixed number of return values, then use RETURN, otherwise use
1196 ;;; RETURN-MULTIPLE.
1197 (defun ir2-convert-return (node block)
1198 (declare (type creturn node) (type ir2-block block))
1199 (let* ((lvar (return-result node))
1200 (2lvar (lvar-info lvar))
1201 (lvar-kind (ir2-lvar-kind 2lvar))
1202 (fun (return-lambda node))
1203 (env (physenv-info (lambda-physenv fun)))
1204 (old-fp (ir2-physenv-old-fp env))
1205 (return-pc (ir2-physenv-return-pc env))
1206 (returns (tail-set-info (lambda-tail-set fun))))
1208 ((and (eq (return-info-kind returns) :fixed)
1210 (let ((locs (lvar-tns node block lvar
1211 (return-info-types returns))))
1212 (vop* known-return node block
1213 (old-fp return-pc (reference-tn-list locs nil))
1215 (return-info-locations returns))))
1216 ((eq lvar-kind :fixed)
1217 (let* ((types (mapcar #'tn-primitive-type (ir2-lvar-locs 2lvar)))
1218 (lvar-locs (lvar-tns node block lvar types))
1219 (nvals (length lvar-locs))
1220 (locs (make-standard-value-tns nvals)))
1221 (mapc (lambda (val loc)
1222 (emit-move node block val loc))
1226 (vop return-single node block old-fp return-pc (car locs))
1227 (vop* return node block
1228 (old-fp return-pc (reference-tn-list locs nil))
1232 (aver (eq lvar-kind :unknown))
1233 (vop* return-multiple node block
1235 (reference-tn-list (ir2-lvar-locs 2lvar) nil))
1242 ;;; This is used by the debugger to find the top function on the
1243 ;;; stack. It returns the OLD-FP and RETURN-PC for the current
1244 ;;; function as multiple values.
1245 (defoptimizer (sb!kernel:%caller-frame-and-pc ir2-convert) (() node block)
1246 (let ((ir2-physenv (physenv-info (node-physenv node))))
1247 (move-lvar-result node block
1248 (list (ir2-physenv-old-fp ir2-physenv)
1249 (ir2-physenv-return-pc ir2-physenv))
1252 ;;;; multiple values
1254 ;;; This is almost identical to IR2-CONVERT-LET. Since LTN annotates
1255 ;;; the lvar for the correct number of values (with the lvar user
1256 ;;; responsible for defaulting), we can just pick them up from the
1258 (defun ir2-convert-mv-bind (node block)
1259 (declare (type mv-combination node) (type ir2-block block))
1260 (let* ((lvar (first (basic-combination-args node)))
1261 (fun (ref-leaf (lvar-uses (basic-combination-fun node))))
1262 (vars (lambda-vars fun)))
1263 (aver (eq (functional-kind fun) :mv-let))
1264 (mapc (lambda (src var)
1265 (when (leaf-refs var)
1266 (let ((dest (leaf-info var)))
1267 (if (lambda-var-indirect var)
1268 (emit-make-value-cell node block src dest)
1269 (emit-move node block src dest)))))
1270 (lvar-tns node block lvar
1272 (primitive-type (leaf-type x)))
1277 ;;; Emit the appropriate fixed value, unknown value or tail variant of
1278 ;;; CALL-VARIABLE. Note that we only need to pass the values start for
1279 ;;; the first argument: all the other argument lvar TNs are
1280 ;;; ignored. This is because we require all of the values globs to be
1281 ;;; contiguous and on stack top.
1282 (defun ir2-convert-mv-call (node block)
1283 (declare (type mv-combination node) (type ir2-block block))
1284 (aver (basic-combination-args node))
1285 (let* ((start-lvar (lvar-info (first (basic-combination-args node))))
1286 (start (first (ir2-lvar-locs start-lvar)))
1287 (tails (and (node-tail-p node)
1288 (lambda-tail-set (node-home-lambda node))))
1289 (lvar (node-lvar node))
1290 (2lvar (and lvar (lvar-info lvar))))
1291 (multiple-value-bind (fun named)
1292 (fun-lvar-tn node block (basic-combination-fun node))
1293 (aver (and (not named)
1294 (eq (ir2-lvar-kind start-lvar) :unknown)))
1297 (let ((env (physenv-info (node-physenv node))))
1298 (vop tail-call-variable node block start fun
1299 (ir2-physenv-old-fp env)
1300 (ir2-physenv-return-pc env))))
1302 (eq (ir2-lvar-kind 2lvar) :unknown))
1303 (vop* multiple-call-variable node block (start fun nil)
1304 ((reference-tn-list (ir2-lvar-locs 2lvar) t))
1305 (emit-step-p node)))
1307 (let ((locs (standard-result-tns lvar)))
1308 (vop* call-variable node block (start fun nil)
1309 ((reference-tn-list locs t)) (length locs)
1311 (move-lvar-result node block locs lvar)))))))
1313 ;;; Reset the stack pointer to the start of the specified
1314 ;;; unknown-values lvar (discarding it and all values globs on top of
1316 (defoptimizer (%pop-values ir2-convert) ((%lvar) node block)
1317 (let* ((lvar (lvar-value %lvar))
1318 (2lvar (lvar-info lvar)))
1319 (cond ((eq (ir2-lvar-kind 2lvar) :unknown)
1320 (vop reset-stack-pointer node block
1321 (first (ir2-lvar-locs 2lvar))))
1322 ((lvar-dynamic-extent lvar)
1323 (vop reset-stack-pointer node block
1324 (ir2-lvar-stack-pointer 2lvar)))
1325 (t (bug "Trying to pop a not stack-allocated LVAR ~S."
1328 (defoptimizer (%nip-values ir2-convert) ((last-nipped last-preserved
1331 (let* ( ;; pointer immediately after the nipped block
1332 (after (lvar-value last-nipped))
1333 (2after (lvar-info after))
1334 ;; pointer to the first nipped word
1335 (first (lvar-value last-preserved))
1336 (2first (lvar-info first))
1338 (moved-tns (loop for lvar-ref in moved
1339 for lvar = (lvar-value lvar-ref)
1340 for 2lvar = (lvar-info lvar)
1342 collect (first (ir2-lvar-locs 2lvar)))))
1343 (aver (or (eq (ir2-lvar-kind 2after) :unknown)
1344 (lvar-dynamic-extent after)))
1345 (aver (eq (ir2-lvar-kind 2first) :unknown))
1346 (when *check-consistency*
1347 ;; we cannot move stack-allocated DX objects
1348 (dolist (moved-lvar moved)
1349 (aver (eq (ir2-lvar-kind (lvar-info (lvar-value moved-lvar)))
1351 (flet ((nip-aligned (nipped)
1352 (vop* %%nip-values node block
1354 (first (ir2-lvar-locs 2first))
1355 (reference-tn-list moved-tns nil))
1356 ((reference-tn-list moved-tns t)))))
1357 (cond ((eq (ir2-lvar-kind 2after) :unknown)
1358 (nip-aligned (first (ir2-lvar-locs 2after))))
1359 ((lvar-dynamic-extent after)
1360 (nip-aligned (ir2-lvar-stack-pointer 2after)))
1362 (bug "Trying to nip a not stack-allocated LVAR ~S." after))))))
1364 ;;; Deliver the values TNs to LVAR using MOVE-LVAR-RESULT.
1365 (defoptimizer (values ir2-convert) ((&rest values) node block)
1366 (let ((tns (mapcar (lambda (x)
1367 (lvar-tn node block x))
1369 (move-lvar-result node block tns (node-lvar node))))
1371 ;;; In the normal case where unknown values are desired, we use the
1372 ;;; VALUES-LIST VOP. In the relatively unimportant case of VALUES-LIST
1373 ;;; for a fixed number of values, we punt by doing a full call to the
1374 ;;; VALUES-LIST function. This gets the full call VOP to deal with
1375 ;;; defaulting any unsupplied values. It seems unworthwhile to
1376 ;;; optimize this case.
1377 (defoptimizer (values-list ir2-convert) ((list) node block)
1378 (let* ((lvar (node-lvar node))
1379 (2lvar (and lvar (lvar-info lvar))))
1381 (eq (ir2-lvar-kind 2lvar) :unknown))
1382 (let ((locs (ir2-lvar-locs 2lvar)))
1383 (vop* values-list node block
1384 ((lvar-tn node block list) nil)
1385 ((reference-tn-list locs t)))))
1386 (t (aver (or (not 2lvar) ; i.e. we want to check the argument
1387 (eq (ir2-lvar-kind 2lvar) :fixed)))
1388 (ir2-convert-full-call node block)))))
1390 (defoptimizer (%more-arg-values ir2-convert) ((context start count) node block)
1391 (binding* ((lvar (node-lvar node) :exit-if-null)
1392 (2lvar (lvar-info lvar)))
1393 (ecase (ir2-lvar-kind 2lvar)
1394 (:fixed (ir2-convert-full-call node block))
1396 (let ((locs (ir2-lvar-locs 2lvar)))
1397 (vop* %more-arg-values node block
1398 ((lvar-tn node block context)
1399 (lvar-tn node block start)
1400 (lvar-tn node block count)
1402 ((reference-tn-list locs t))))))))
1404 ;;;; special binding
1406 ;;; This is trivial, given our assumption of a shallow-binding
1408 (defoptimizer (%special-bind ir2-convert) ((var value) node block)
1409 (let ((name (leaf-source-name (lvar-value var))))
1410 (vop bind node block (lvar-tn node block value)
1411 (emit-constant name))))
1412 (defoptimizer (%special-unbind ir2-convert) ((var) node block)
1413 (vop unbind node block))
1415 ;;; ### It's not clear that this really belongs in this file, or
1416 ;;; should really be done this way, but this is the least violation of
1417 ;;; abstraction in the current setup. We don't want to wire
1418 ;;; shallow-binding assumptions into IR1tran.
1419 (def-ir1-translator progv
1420 ((vars vals &body body) start next result)
1423 (with-unique-names (bind unbind)
1424 (once-only ((n-save-bs '(%primitive current-binding-pointer)))
1427 (labels ((,unbind (vars)
1428 (declare (optimize (speed 2) (debug 0)))
1430 (%primitive bind nil var)
1433 (declare (optimize (speed 2) (debug 0)))
1435 ((null vals) (,unbind vars))
1439 (,bind (cdr vars) (cdr vals))))))
1440 (,bind ,vars ,vals))
1443 (%primitive unbind-to-here ,n-save-bs))))))
1447 ;;; Convert a non-local lexical exit. First find the NLX-INFO in our
1448 ;;; environment. Note that this is never called on the escape exits
1449 ;;; for CATCH and UNWIND-PROTECT, since the escape functions aren't
1451 (defun ir2-convert-exit (node block)
1452 (declare (type exit node) (type ir2-block block))
1453 (let* ((nlx (exit-nlx-info node))
1454 (loc (find-in-physenv nlx (node-physenv node)))
1455 (temp (make-stack-pointer-tn))
1456 (value (exit-value node)))
1457 (if (nlx-info-safe-p nlx)
1458 (vop value-cell-ref node block loc temp)
1459 (emit-move node block loc temp))
1461 (let ((locs (ir2-lvar-locs (lvar-info value))))
1462 (vop unwind node block temp (first locs) (second locs)))
1463 (let ((0-tn (emit-constant 0)))
1464 (vop unwind node block temp 0-tn 0-tn))))
1468 ;;; %CLEANUP-POINT doesn't do anything except prevent the body from
1469 ;;; being entirely deleted.
1470 (defoptimizer (%cleanup-point ir2-convert) (() node block) node block)
1472 ;;; This function invalidates a lexical exit on exiting from the
1473 ;;; dynamic extent. This is done by storing 0 into the indirect value
1474 ;;; cell that holds the closed unwind block.
1475 (defoptimizer (%lexical-exit-breakup ir2-convert) ((info) node block)
1476 (let ((nlx (lvar-value info)))
1477 (when (nlx-info-safe-p nlx)
1478 (vop value-cell-set node block
1479 (find-in-physenv nlx (node-physenv node))
1480 (emit-constant 0)))))
1482 ;;; We have to do a spurious move of no values to the result lvar so
1483 ;;; that lifetime analysis won't get confused.
1484 (defun ir2-convert-throw (node block)
1485 (declare (type mv-combination node) (type ir2-block block))
1486 (let ((args (basic-combination-args node)))
1487 (check-catch-tag-type (first args))
1488 (vop* throw node block
1489 ((lvar-tn node block (first args))
1491 (ir2-lvar-locs (lvar-info (second args)))
1494 (move-lvar-result node block () (node-lvar node))
1497 ;;; Emit code to set up a non-local exit. INFO is the NLX-INFO for the
1498 ;;; exit, and TAG is the lvar for the catch tag (if any.) We get at
1499 ;;; the target PC by passing in the label to the vop. The vop is
1500 ;;; responsible for building a return-PC object.
1501 (defun emit-nlx-start (node block info tag)
1502 (declare (type node node) (type ir2-block block) (type nlx-info info)
1503 (type (or lvar null) tag))
1504 (let* ((2info (nlx-info-info info))
1505 (kind (cleanup-kind (nlx-info-cleanup info)))
1506 (block-tn (physenv-live-tn
1507 (make-normal-tn (primitive-type-or-lose 'catch-block))
1508 (node-physenv node)))
1509 (res (make-stack-pointer-tn))
1510 (target-label (ir2-nlx-info-target 2info)))
1512 (vop current-binding-pointer node block
1513 (car (ir2-nlx-info-dynamic-state 2info)))
1514 (vop* save-dynamic-state node block
1516 ((reference-tn-list (cdr (ir2-nlx-info-dynamic-state 2info)) t)))
1517 (vop current-stack-pointer node block (ir2-nlx-info-save-sp 2info))
1521 (vop make-catch-block node block block-tn
1522 (lvar-tn node block tag) target-label res))
1523 ((:unwind-protect :block :tagbody)
1524 (vop make-unwind-block node block block-tn target-label res)))
1528 (if (nlx-info-safe-p info)
1529 (emit-make-value-cell node block res (ir2-nlx-info-home 2info))
1530 (emit-move node block res (ir2-nlx-info-home 2info))))
1532 (vop set-unwind-protect node block block-tn))
1537 ;;; Scan each of ENTRY's exits, setting up the exit for each lexical exit.
1538 (defun ir2-convert-entry (node block)
1539 (declare (type entry node) (type ir2-block block))
1541 (dolist (exit (entry-exits node))
1542 (let ((info (exit-nlx-info exit)))
1544 (not (memq info nlxes))
1545 (member (cleanup-kind (nlx-info-cleanup info))
1546 '(:block :tagbody)))
1548 (emit-nlx-start node block info nil)))))
1551 ;;; Set up the unwind block for these guys.
1552 (defoptimizer (%catch ir2-convert) ((info-lvar tag) node block)
1553 (check-catch-tag-type tag)
1554 (emit-nlx-start node block (lvar-value info-lvar) tag))
1555 (defoptimizer (%unwind-protect ir2-convert) ((info-lvar cleanup) node block)
1556 (emit-nlx-start node block (lvar-value info-lvar) nil))
1558 ;;; Emit the entry code for a non-local exit. We receive values and
1559 ;;; restore dynamic state.
1561 ;;; In the case of a lexical exit or CATCH, we look at the exit lvar's
1562 ;;; kind to determine which flavor of entry VOP to emit. If unknown
1563 ;;; values, emit the xxx-MULTIPLE variant to the lvar locs. If fixed
1564 ;;; values, make the appropriate number of temps in the standard
1565 ;;; values locations and use the other variant, delivering the temps
1566 ;;; to the lvar using MOVE-LVAR-RESULT.
1568 ;;; In the UNWIND-PROTECT case, we deliver the first register
1569 ;;; argument, the argument count and the argument pointer to our lvar
1570 ;;; as multiple values. These values are the block exited to and the
1571 ;;; values start and count.
1573 ;;; After receiving values, we restore dynamic state. Except in the
1574 ;;; UNWIND-PROTECT case, the values receiving restores the stack
1575 ;;; pointer. In an UNWIND-PROTECT cleanup, we want to leave the stack
1576 ;;; pointer alone, since the thrown values are still out there.
1577 (defoptimizer (%nlx-entry ir2-convert) ((info-lvar) node block)
1578 (let* ((info (lvar-value info-lvar))
1579 (lvar (node-lvar node))
1580 (2info (nlx-info-info info))
1581 (top-loc (ir2-nlx-info-save-sp 2info))
1582 (start-loc (make-nlx-entry-arg-start-location))
1583 (count-loc (make-arg-count-location))
1584 (target (ir2-nlx-info-target 2info)))
1586 (ecase (cleanup-kind (nlx-info-cleanup info))
1587 ((:catch :block :tagbody)
1588 (let ((2lvar (and lvar (lvar-info lvar))))
1589 (if (and 2lvar (eq (ir2-lvar-kind 2lvar) :unknown))
1590 (vop* nlx-entry-multiple node block
1591 (top-loc start-loc count-loc nil)
1592 ((reference-tn-list (ir2-lvar-locs 2lvar) t))
1594 (let ((locs (standard-result-tns lvar)))
1595 (vop* nlx-entry node block
1596 (top-loc start-loc count-loc nil)
1597 ((reference-tn-list locs t))
1600 (move-lvar-result node block locs lvar)))))
1602 (let ((block-loc (standard-arg-location 0)))
1603 (vop uwp-entry node block target block-loc start-loc count-loc)
1606 (list block-loc start-loc count-loc)
1610 (when *collect-dynamic-statistics*
1611 (vop count-me node block *dynamic-counts-tn*
1612 (block-number (ir2-block-block block))))
1614 (vop* restore-dynamic-state node block
1615 ((reference-tn-list (cdr (ir2-nlx-info-dynamic-state 2info)) nil))
1617 (vop unbind-to-here node block
1618 (car (ir2-nlx-info-dynamic-state 2info)))))
1620 ;;;; n-argument functions
1622 (macrolet ((def (name)
1623 `(defoptimizer (,name ir2-convert) ((&rest args) node block)
1624 (let* ((refs (move-tail-full-call-args node block))
1625 (lvar (node-lvar node))
1626 (res (lvar-result-tns
1628 (list (primitive-type (specifier-type 'list))))))
1629 (when (and lvar (lvar-dynamic-extent lvar))
1630 (vop current-stack-pointer node block
1631 (ir2-lvar-stack-pointer (lvar-info lvar))))
1632 (vop* ,name node block (refs) ((first res) nil)
1634 (move-lvar-result node block res lvar)))))
1639 ;;; Convert the code in a component into VOPs.
1640 (defun ir2-convert (component)
1641 (declare (type component component))
1642 (let (#!+sb-dyncount
1643 (*dynamic-counts-tn*
1644 (when *collect-dynamic-statistics*
1646 (block-number (block-next (component-head component))))
1647 (counts (make-array blocks
1648 :element-type '(unsigned-byte 32)
1649 :initial-element 0))
1650 (info (make-dyncount-info
1651 :for (component-name component)
1652 :costs (make-array blocks
1653 :element-type '(unsigned-byte 32)
1656 (setf (ir2-component-dyncount-info (component-info component))
1658 (emit-constant info)
1659 (emit-constant counts)))))
1661 (declare (type index num))
1662 (do-ir2-blocks (2block component)
1663 (let ((block (ir2-block-block 2block)))
1664 (when (block-start block)
1665 (setf (block-number block) num)
1667 (when *collect-dynamic-statistics*
1668 (let ((first-node (block-start-node block)))
1669 (unless (or (and (bind-p first-node)
1670 (xep-p (bind-lambda first-node)))
1672 (node-lvar first-node))
1677 #!+sb-dyncount *dynamic-counts-tn* #!-sb-dyncount nil
1679 (ir2-convert-block block)
1683 ;;; If necessary, emit a terminal unconditional branch to go to the
1684 ;;; successor block. If the successor is the component tail, then
1685 ;;; there isn't really any successor, but if the end is an unknown,
1686 ;;; non-tail call, then we emit an error trap just in case the
1687 ;;; function really does return.
1688 (defun finish-ir2-block (block)
1689 (declare (type cblock block))
1690 (let* ((2block (block-info block))
1691 (last (block-last block))
1692 (succ (block-succ block)))
1694 (aver (singleton-p succ))
1695 (let ((target (first succ)))
1696 (cond ((eq target (component-tail (block-component block)))
1697 (when (and (basic-combination-p last)
1698 (eq (basic-combination-kind last) :full))
1699 (let* ((fun (basic-combination-fun last))
1700 (use (lvar-uses fun))
1701 (name (and (ref-p use)
1702 (leaf-has-source-name-p (ref-leaf use))
1703 (leaf-source-name (ref-leaf use)))))
1704 (unless (or (node-tail-p last)
1705 (info :function :info name)
1706 (policy last (zerop safety)))
1707 (vop nil-fun-returned-error last 2block
1709 (emit-constant name)
1710 (multiple-value-bind (tn named)
1711 (fun-lvar-tn last 2block fun)
1714 ((not (eq (ir2-block-next 2block) (block-info target)))
1715 (vop branch last 2block (block-label target)))))))
1719 ;;; Convert the code in a block into VOPs.
1720 (defun ir2-convert-block (block)
1721 (declare (type cblock block))
1722 (let ((2block (block-info block)))
1723 (do-nodes (node lvar block)
1727 (let ((2lvar (lvar-info lvar)))
1728 ;; function REF in a local call is not annotated
1729 (when (and 2lvar (not (eq (ir2-lvar-kind 2lvar) :delayed)))
1730 (ir2-convert-ref node 2block)))))
1732 (let ((kind (basic-combination-kind node)))
1735 (ir2-convert-local-call node 2block))
1737 (ir2-convert-full-call node 2block))
1739 (let* ((info (basic-combination-fun-info node))
1740 (fun (fun-info-ir2-convert info)))
1742 (funcall fun node 2block))
1743 ((eq (basic-combination-info node) :full)
1744 (ir2-convert-full-call node 2block))
1746 (ir2-convert-template node 2block))))))))
1748 (when (lvar-info (if-test node))
1749 (ir2-convert-if node 2block)))
1751 (let ((fun (bind-lambda node)))
1752 (when (eq (lambda-home fun) fun)
1753 (ir2-convert-bind node 2block))))
1755 (ir2-convert-return node 2block))
1757 (ir2-convert-set node 2block))
1759 (ir2-convert-cast node 2block))
1762 ((eq (basic-combination-kind node) :local)
1763 (ir2-convert-mv-bind node 2block))
1764 ((eq (lvar-fun-name (basic-combination-fun node))
1766 (ir2-convert-throw node 2block))
1768 (ir2-convert-mv-call node 2block))))
1770 (when (exit-entry node)
1771 (ir2-convert-exit node 2block)))
1773 (ir2-convert-entry node 2block)))))
1775 (finish-ir2-block block)