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.
49 (defevent make-value-cell-event "Allocate heap value cell for lexical var.")
50 (defun emit-make-value-cell (node block value res)
51 (event make-value-cell-event node)
52 (vop make-value-cell node block value res))
56 ;;; Return the TN that holds the value of THING in the environment ENV.
57 (declaim (ftype (function ((or nlx-info lambda-var clambda) physenv) tn)
59 (defun find-in-physenv (thing physenv)
60 (or (cdr (assoc thing (ir2-physenv-closure (physenv-info physenv))))
63 ;; I think that a failure of this assertion means that we're
64 ;; trying to access a variable which was improperly closed
65 ;; over. The PHYSENV describes a physical environment. Every
66 ;; variable that a form refers to should either be in its
67 ;; physical environment directly, or grabbed from a
68 ;; surrounding physical environment when it was closed over.
69 ;; The ASSOC expression above finds closed-over variables, so
70 ;; if we fell through the ASSOC expression, it wasn't closed
71 ;; over. Therefore, it must be in our physical environment
72 ;; directly. If instead it is in some other physical
73 ;; environment, then it's bogus for us to reference it here
74 ;; without it being closed over. -- WHN 2001-09-29
75 (aver (eq physenv (lambda-physenv (lambda-var-home thing))))
78 (aver (eq physenv (block-physenv (nlx-info-target thing))))
79 (ir2-nlx-info-home (nlx-info-info thing)))
82 (entry-info-closure-tn (lambda-info thing))))
83 (bug "~@<~2I~_~S ~_not found in ~_~S~:>" thing physenv)))
85 ;;; If LEAF already has a constant TN, return that, otherwise make a
87 (defun constant-tn (leaf)
88 (declare (type constant leaf))
90 (setf (leaf-info leaf)
91 (make-constant-tn leaf))))
93 ;;; Return a TN that represents the value of LEAF, or NIL if LEAF
94 ;;; isn't directly represented by a TN. ENV is the environment that
95 ;;; the reference is done in.
96 (defun leaf-tn (leaf env)
97 (declare (type leaf leaf) (type physenv env))
100 (unless (lambda-var-indirect leaf)
101 (find-in-physenv leaf env)))
102 (constant (constant-tn leaf))
105 ;;; This is used to conveniently get a handle on a constant TN during
106 ;;; IR2 conversion. It returns a constant TN representing the Lisp
108 (defun emit-constant (value)
109 (constant-tn (find-constant value)))
111 ;;; Convert a REF node. The reference must not be delayed.
112 (defun ir2-convert-ref (node block)
113 (declare (type ref node) (type ir2-block block))
114 (let* ((lvar (node-lvar node))
115 (leaf (ref-leaf node))
116 (locs (lvar-result-tns
117 lvar (list (primitive-type (leaf-type leaf)))))
121 (let ((tn (find-in-physenv leaf (node-physenv node))))
122 (if (lambda-var-indirect leaf)
123 (vop value-cell-ref node block tn res)
124 (emit-move node block tn res))))
126 (if (legal-immediate-constant-p leaf)
127 (emit-move node block (constant-tn leaf) res)
128 (let* ((name (leaf-source-name leaf))
129 (name-tn (emit-constant name)))
130 (if (policy node (zerop safety))
131 (vop fast-symbol-value node block name-tn res)
132 (vop symbol-value node block name-tn res)))))
134 (ir2-convert-closure node block leaf res))
136 (let ((unsafe (policy node (zerop safety)))
137 (name (leaf-source-name leaf)))
138 (ecase (global-var-kind leaf)
140 (aver (symbolp name))
141 (let ((name-tn (emit-constant name)))
143 (vop fast-symbol-value node block name-tn res)
144 (vop symbol-value node block name-tn res))))
146 (let ((fdefn-tn (make-load-time-constant-tn :fdefinition name)))
148 (vop fdefn-fun node block fdefn-tn res)
149 (vop safe-fdefn-fun node block fdefn-tn res))))))))
150 (move-lvar-result node block locs lvar))
153 ;;; some sanity checks for a CLAMBDA passed to IR2-CONVERT-CLOSURE
154 (defun assertions-on-ir2-converted-clambda (clambda)
155 ;; This assertion was sort of an experiment. It would be nice and
156 ;; sane and easier to understand things if it were *always* true,
157 ;; but experimentally I observe that it's only *almost* always
158 ;; true. -- WHN 2001-01-02
160 (aver (eql (lambda-component clambda)
161 (block-component (ir2-block-block ir2-block))))
162 ;; Check for some weirdness which came up in bug
165 ;; The MAKE-LOAD-TIME-CONSTANT-TN call above puts an :ENTRY record
166 ;; into the IR2-COMPONENT-CONSTANTS table. The dump-a-COMPONENT
168 ;; * treats every HANDLEless :ENTRY record into a
170 ;; * expects every patch to correspond to an
171 ;; IR2-COMPONENT-ENTRIES record.
172 ;; The IR2-COMPONENT-ENTRIES records are set by ENTRY-ANALYZE
173 ;; walking over COMPONENT-LAMBDAS. Bug 138b arose because there
174 ;; was a HANDLEless :ENTRY record which didn't correspond to an
175 ;; IR2-COMPONENT-ENTRIES record. That problem is hard to debug
176 ;; when it's caught at dump time, so this assertion tries to catch
178 (aver (member clambda
179 (component-lambdas (lambda-component clambda))))
180 ;; another bug-138-related issue: COMPONENT-NEW-FUNCTIONALS is
181 ;; used as a queue for stuff pending to do in IR1, and now that
182 ;; we're doing IR2 it should've been completely flushed (but
184 (aver (null (component-new-functionals (lambda-component clambda))))
187 ;;; Emit code to load a function object implementing FUNCTIONAL into
188 ;;; RES. This gets interesting when the referenced function is a
189 ;;; closure: we must make the closure and move the closed-over values
192 ;;; FUNCTIONAL is either a :TOPLEVEL-XEP functional or the XEP lambda
193 ;;; for the called function, since local call analysis converts all
194 ;;; closure references. If a :TOPLEVEL-XEP, we know it is not a
197 ;;; If a closed-over LAMBDA-VAR has no refs (is deleted), then we
198 ;;; don't initialize that slot. This can happen with closures over
199 ;;; top level variables, where optimization of the closure deleted the
200 ;;; variable. Since we committed to the closure format when we
201 ;;; pre-analyzed the top level code, we just leave an empty slot.
202 (defun ir2-convert-closure (ref ir2-block functional res)
203 (declare (type ref ref)
204 (type ir2-block ir2-block)
205 (type functional functional)
207 (aver (not (eql (functional-kind functional) :deleted)))
208 (unless (leaf-info functional)
209 (setf (leaf-info functional)
210 (make-entry-info :name (functional-debug-name functional))))
211 (let ((closure (etypecase functional
213 (assertions-on-ir2-converted-clambda functional)
214 (physenv-closure (get-lambda-physenv functional)))
216 (aver (eq (functional-kind functional) :toplevel-xep))
220 (let* ((physenv (node-physenv ref))
221 (tn (find-in-physenv functional physenv)))
222 (emit-move ref ir2-block tn res)))
224 (let ((entry (make-load-time-constant-tn :entry functional)))
225 (emit-move ref ir2-block entry res)))))
228 (defoptimizer (%allocate-closures ltn-annotate) ((leaves) node ltn-policy)
229 ltn-policy ; a hack to effectively (DECLARE (IGNORE LTN-POLICY))
230 (when (lvar-dynamic-extent leaves)
231 (let ((info (make-ir2-lvar *backend-t-primitive-type*)))
232 (setf (ir2-lvar-kind info) :delayed)
233 (setf (lvar-info leaves) info)
234 (setf (ir2-lvar-stack-pointer info)
235 (make-stack-pointer-tn)))))
237 (defoptimizer (%allocate-closures ir2-convert) ((leaves) call 2block)
238 (let ((dx-p (lvar-dynamic-extent leaves)))
241 (vop current-stack-pointer call 2block
242 (ir2-lvar-stack-pointer (lvar-info leaves))))
243 (dolist (leaf (lvar-value leaves))
244 (binding* ((xep (functional-entry-fun leaf) :exit-if-null)
245 (nil (aver (xep-p xep)))
246 (entry-info (lambda-info xep) :exit-if-null)
247 (tn (entry-info-closure-tn entry-info) :exit-if-null)
248 (closure (physenv-closure (get-lambda-physenv xep)))
249 (entry (make-load-time-constant-tn :entry xep)))
250 (let ((this-env (node-physenv call))
251 (leaf-dx-p (and dx-p (leaf-dynamic-extent leaf))))
252 (vop make-closure call 2block entry (length closure)
254 (loop for what in closure and n from 0 do
255 (unless (and (lambda-var-p what)
256 (null (leaf-refs what)))
257 ;; In LABELS a closure may refer to another closure
258 ;; in the same group, so we must be sure that we
259 ;; store a closure only after its creation.
261 ;; TODO: Here is a simple solution: we postpone
262 ;; putting of all closures after all creations
263 ;; (though it may require more registers).
265 (delayed (list tn (find-in-physenv what this-env) n))
266 (vop closure-init call 2block
268 (find-in-physenv what this-env)
270 (loop for (tn what n) in (delayed)
271 do (vop closure-init call 2block
275 ;;; Convert a SET node. If the NODE's LVAR is annotated, then we also
276 ;;; deliver the value to that lvar. If the var is a lexical variable
277 ;;; with no refs, then we don't actually set anything, since the
278 ;;; variable has been deleted.
279 (defun ir2-convert-set (node block)
280 (declare (type cset node) (type ir2-block block))
281 (let* ((lvar (node-lvar node))
282 (leaf (set-var node))
283 (val (lvar-tn node block (set-value node)))
286 lvar (list (primitive-type (leaf-type leaf))))
290 (when (leaf-refs leaf)
291 (let ((tn (find-in-physenv leaf (node-physenv node))))
292 (if (lambda-var-indirect leaf)
293 (vop value-cell-set node block tn val)
294 (emit-move node block val tn)))))
296 (ecase (global-var-kind leaf)
298 (aver (symbolp (leaf-source-name leaf)))
299 (vop set node block (emit-constant (leaf-source-name leaf)) val)))))
301 (emit-move node block val (first locs))
302 (move-lvar-result node block locs lvar)))
305 ;;;; utilities for receiving fixed values
307 ;;; Return a TN that can be referenced to get the value of LVAR. LVAR
308 ;;; must be LTN-ANNOTATED either as a delayed leaf ref or as a fixed,
309 ;;; single-value lvar.
311 ;;; The primitive-type of the result will always be the same as the
312 ;;; IR2-LVAR-PRIMITIVE-TYPE, ensuring that VOPs are always called with
313 ;;; TNs that satisfy the operand primitive-type restriction. We may
314 ;;; have to make a temporary of the desired type and move the actual
315 ;;; lvar TN into it. This happens when we delete a type check in
316 ;;; unsafe code or when we locally know something about the type of an
317 ;;; argument variable.
318 (defun lvar-tn (node block lvar)
319 (declare (type node node) (type ir2-block block) (type lvar lvar))
320 (let* ((2lvar (lvar-info lvar))
322 (ecase (ir2-lvar-kind 2lvar)
324 (let ((ref (lvar-uses lvar)))
325 (leaf-tn (ref-leaf ref) (node-physenv ref))))
327 (aver (= (length (ir2-lvar-locs 2lvar)) 1))
328 (first (ir2-lvar-locs 2lvar)))))
329 (ptype (ir2-lvar-primitive-type 2lvar)))
331 (cond ((eq (tn-primitive-type lvar-tn) ptype) lvar-tn)
333 (let ((temp (make-normal-tn ptype)))
334 (emit-move node block lvar-tn temp)
337 ;;; This is similar to LVAR-TN, but hacks multiple values. We return
338 ;;; TNs holding the values of LVAR with PTYPES as their primitive
339 ;;; types. LVAR must be annotated for the same number of fixed values
340 ;;; are there are PTYPES.
342 ;;; If the lvar has a type check, check the values into temps and
343 ;;; return the temps. When we have more values than assertions, we
344 ;;; move the extra values with no check.
345 (defun lvar-tns (node block lvar ptypes)
346 (declare (type node node) (type ir2-block block)
347 (type lvar lvar) (list ptypes))
348 (let* ((locs (ir2-lvar-locs (lvar-info lvar)))
349 (nlocs (length locs)))
350 (aver (= nlocs (length ptypes)))
352 (mapcar (lambda (from to-type)
353 (if (eq (tn-primitive-type from) to-type)
355 (let ((temp (make-normal-tn to-type)))
356 (emit-move node block from temp)
361 ;;;; utilities for delivering values to lvars
363 ;;; Return a list of TNs with the specifier TYPES that can be used as
364 ;;; result TNs to evaluate an expression into LVAR. This is used
365 ;;; together with MOVE-LVAR-RESULT to deliver fixed values to
368 ;;; If the lvar isn't annotated (meaning the values are discarded) or
369 ;;; is unknown-values, the then we make temporaries for each supplied
370 ;;; value, providing a place to compute the result in until we decide
371 ;;; what to do with it (if anything.)
373 ;;; If the lvar is fixed-values, and wants the same number of values
374 ;;; as the user wants to deliver, then we just return the
375 ;;; IR2-LVAR-LOCS. Otherwise we make a new list padded as necessary by
376 ;;; discarded TNs. We always return a TN of the specified type, using
377 ;;; the lvar locs only when they are of the correct type.
378 (defun lvar-result-tns (lvar types)
379 (declare (type (or lvar null) lvar) (type list types))
381 (mapcar #'make-normal-tn types)
382 (let ((2lvar (lvar-info lvar)))
383 (ecase (ir2-lvar-kind 2lvar)
385 (let* ((locs (ir2-lvar-locs 2lvar))
386 (nlocs (length locs))
387 (ntypes (length types)))
388 (if (and (= nlocs ntypes)
389 (do ((loc locs (cdr loc))
390 (type types (cdr type)))
392 (unless (eq (tn-primitive-type (car loc)) (car type))
395 (mapcar (lambda (loc type)
396 (if (eq (tn-primitive-type loc) type)
398 (make-normal-tn type)))
401 (mapcar #'make-normal-tn
402 (subseq types nlocs)))
406 (mapcar #'make-normal-tn types))))))
408 ;;; Make the first N standard value TNs, returning them in a list.
409 (defun make-standard-value-tns (n)
410 (declare (type unsigned-byte n))
413 (res (standard-arg-location i)))
416 ;;; Return a list of TNs wired to the standard value passing
417 ;;; conventions that can be used to receive values according to the
418 ;;; unknown-values convention. This is used with together
419 ;;; MOVE-LVAR-RESULT for delivering unknown values to a fixed values
422 ;;; If the lvar isn't annotated, then we treat as 0-values, returning
423 ;;; an empty list of temporaries.
425 ;;; If the lvar is annotated, then it must be :FIXED.
426 (defun standard-result-tns (lvar)
427 (declare (type (or lvar null) lvar))
429 (let ((2lvar (lvar-info lvar)))
430 (ecase (ir2-lvar-kind 2lvar)
432 (make-standard-value-tns (length (ir2-lvar-locs 2lvar))))))
435 ;;; Just move each SRC TN into the corresponding DEST TN, defaulting
436 ;;; any unsupplied source values to NIL. We let EMIT-MOVE worry about
437 ;;; doing the appropriate coercions.
438 (defun move-results-coerced (node block src dest)
439 (declare (type node node) (type ir2-block block) (list src dest))
440 (let ((nsrc (length src))
441 (ndest (length dest)))
442 (mapc (lambda (from to)
444 (emit-move node block from to)))
446 (append src (make-list (- ndest nsrc)
447 :initial-element (emit-constant nil)))
452 ;;; Move each SRC TN into the corresponding DEST TN, checking types
453 ;;; and defaulting any unsupplied source values to NIL
454 (defun move-results-checked (node block src dest types)
455 (declare (type node node) (type ir2-block block) (list src dest types))
456 (let ((nsrc (length src))
457 (ndest (length dest))
458 (ntypes (length types)))
459 (mapc (lambda (from to type)
461 (emit-type-check node block from to type)
462 (emit-move node block from to)))
464 (append src (make-list (- ndest nsrc)
465 :initial-element (emit-constant nil)))
469 (append types (make-list (- ndest ntypes)))
473 ;;; If necessary, emit coercion code needed to deliver the RESULTS to
474 ;;; the specified lvar. NODE and BLOCK provide context for emitting
475 ;;; code. Although usually obtained from STANDARD-RESULT-TNs or
476 ;;; LVAR-RESULT-TNs, RESULTS my be a list of any type or
479 ;;; If the lvar is fixed values, then move the results into the lvar
480 ;;; locations. If the lvar is unknown values, then do the moves into
481 ;;; the standard value locations, and use PUSH-VALUES to put the
482 ;;; values on the stack.
483 (defun move-lvar-result (node block results lvar)
484 (declare (type node node) (type ir2-block block)
485 (list results) (type (or lvar null) lvar))
487 (let ((2lvar (lvar-info lvar)))
488 (ecase (ir2-lvar-kind 2lvar)
490 (let ((locs (ir2-lvar-locs 2lvar)))
491 (unless (eq locs results)
492 (move-results-coerced node block results locs))))
494 (let* ((nvals (length results))
495 (locs (make-standard-value-tns nvals)))
496 (move-results-coerced node block results locs)
497 (vop* push-values node block
498 ((reference-tn-list locs nil))
499 ((reference-tn-list (ir2-lvar-locs 2lvar) t))
504 (defun ir2-convert-cast (node block)
505 (declare (type cast node)
506 (type ir2-block block))
507 (binding* ((lvar (node-lvar node) :exit-if-null)
508 (2lvar (lvar-info lvar))
509 (value (cast-value node))
510 (2value (lvar-info value)))
511 (cond ((eq (ir2-lvar-kind 2lvar) :unused))
512 ((eq (ir2-lvar-kind 2lvar) :unknown)
513 (aver (eq (ir2-lvar-kind 2value) :unknown))
514 (aver (not (cast-type-check node)))
515 (move-results-coerced node block
516 (ir2-lvar-locs 2value)
517 (ir2-lvar-locs 2lvar)))
518 ((eq (ir2-lvar-kind 2lvar) :fixed)
519 (aver (eq (ir2-lvar-kind 2value) :fixed))
520 (if (cast-type-check node)
521 (move-results-checked node block
522 (ir2-lvar-locs 2value)
523 (ir2-lvar-locs 2lvar)
524 (multiple-value-bind (check types)
525 (cast-check-types node nil)
526 (aver (eq check :simple))
528 (move-results-coerced node block
529 (ir2-lvar-locs 2value)
530 (ir2-lvar-locs 2lvar))))
531 (t (bug "CAST cannot be :DELAYED.")))))
533 ;;;; template conversion
535 ;;; Build a TN-REFS list that represents access to the values of the
536 ;;; specified list of lvars ARGS for TEMPLATE. Any :CONSTANT arguments
537 ;;; are returned in the second value as a list rather than being
538 ;;; accessed as a normal argument. NODE and BLOCK provide the context
539 ;;; for emitting any necessary type-checking code.
540 (defun reference-args (node block args template)
541 (declare (type node node) (type ir2-block block) (list args)
542 (type template template))
543 (collect ((info-args))
546 (do ((args args (cdr args))
547 (types (template-arg-types template) (cdr types)))
549 (let ((type (first types))
551 (if (and (consp type) (eq (car type) ':constant))
552 (info-args (lvar-value arg))
553 (let ((ref (reference-tn (lvar-tn node block arg) nil)))
555 (setf (tn-ref-across last) ref)
559 (values (the (or tn-ref null) first) (info-args)))))
561 ;;; Convert a conditional template. We try to exploit any
562 ;;; drop-through, but emit an unconditional branch afterward if we
563 ;;; fail. NOT-P is true if the sense of the TEMPLATE's test should be
565 (defun ir2-convert-conditional (node block template args info-args if not-p)
566 (declare (type node node) (type ir2-block block)
567 (type template template) (type (or tn-ref null) args)
568 (list info-args) (type cif if) (type boolean not-p))
569 (aver (= (template-info-arg-count template) (+ (length info-args) 2)))
570 (let ((consequent (if-consequent if))
571 (alternative (if-alternative if)))
572 (cond ((drop-thru-p if consequent)
573 (emit-template node block template args nil
574 (list* (block-label alternative) (not not-p)
577 (emit-template node block template args nil
578 (list* (block-label consequent) not-p info-args))
579 (unless (drop-thru-p if alternative)
580 (vop branch node block (block-label alternative)))))))
582 ;;; Convert an IF that isn't the DEST of a conditional template.
583 (defun ir2-convert-if (node block)
584 (declare (type ir2-block block) (type cif node))
585 (let* ((test (if-test node))
586 (test-ref (reference-tn (lvar-tn node block test) nil))
587 (nil-ref (reference-tn (emit-constant nil) nil)))
588 (setf (tn-ref-across test-ref) nil-ref)
589 (ir2-convert-conditional node block (template-or-lose 'if-eq)
590 test-ref () node t)))
592 ;;; Return a list of primitive-types that we can pass to
593 ;;; LVAR-RESULT-TNS describing the result types we want for a
594 ;;; template call. We duplicate here the determination of output type
595 ;;; that was done in initially selecting the template, so we know that
596 ;;; the types we find are allowed by the template output type
598 (defun find-template-result-types (call template rtypes)
599 (declare (type combination call)
600 (type template template) (list rtypes))
601 (declare (ignore template))
602 (let* ((dtype (node-derived-type call))
604 (types (mapcar #'primitive-type
605 (if (values-type-p type)
606 (append (values-type-required type)
607 (values-type-optional type))
609 (let ((nvals (length rtypes))
610 (ntypes (length types)))
611 (cond ((< ntypes nvals)
613 (make-list (- nvals ntypes)
614 :initial-element *backend-t-primitive-type*)))
616 (subseq types 0 nvals))
620 ;;; Return a list of TNs usable in a CALL to TEMPLATE delivering
621 ;;; values to LVAR. As an efficiency hack, we pick off the common case
622 ;;; where the LVAR is fixed values and has locations that satisfy the
623 ;;; result restrictions. This can fail when there is a type check or a
624 ;;; values count mismatch.
625 (defun make-template-result-tns (call lvar template rtypes)
626 (declare (type combination call) (type (or lvar null) lvar)
627 (type template template) (list rtypes))
628 (let ((2lvar (when lvar (lvar-info lvar))))
629 (if (and 2lvar (eq (ir2-lvar-kind 2lvar) :fixed))
630 (let ((locs (ir2-lvar-locs 2lvar)))
631 (if (and (= (length rtypes) (length locs))
632 (do ((loc locs (cdr loc))
633 (rtype rtypes (cdr rtype)))
635 (unless (operand-restriction-ok
637 (tn-primitive-type (car loc))
643 (find-template-result-types call template rtypes))))
646 (find-template-result-types call template rtypes)))))
648 ;;; Get the operands into TNs, make TN-REFs for them, and then call
649 ;;; the template emit function.
650 (defun ir2-convert-template (call block)
651 (declare (type combination call) (type ir2-block block))
652 (let* ((template (combination-info call))
653 (lvar (node-lvar call))
654 (rtypes (template-result-types template)))
655 (multiple-value-bind (args info-args)
656 (reference-args call block (combination-args call) template)
657 (aver (not (template-more-results-type template)))
658 (if (eq rtypes :conditional)
659 (ir2-convert-conditional call block template args info-args
660 (lvar-dest lvar) nil)
661 (let* ((results (make-template-result-tns call lvar template rtypes))
662 (r-refs (reference-tn-list results t)))
663 (aver (= (length info-args)
664 (template-info-arg-count template)))
665 (when (and lvar (lvar-dynamic-extent lvar))
666 (vop current-stack-pointer call block
667 (ir2-lvar-stack-pointer (lvar-info lvar))))
669 (emit-template call block template args r-refs info-args)
670 (emit-template call block template args r-refs))
671 (move-lvar-result call block results lvar)))))
674 ;;; We don't have to do much because operand count checking is done by
675 ;;; IR1 conversion. The only difference between this and the function
676 ;;; case of IR2-CONVERT-TEMPLATE is that there can be codegen-info
678 (defoptimizer (%%primitive ir2-convert) ((template info &rest args) call block)
679 (let* ((template (lvar-value template))
680 (info (lvar-value info))
681 (lvar (node-lvar call))
682 (rtypes (template-result-types template))
683 (results (make-template-result-tns call lvar template rtypes))
684 (r-refs (reference-tn-list results t)))
685 (multiple-value-bind (args info-args)
686 (reference-args call block (cddr (combination-args call)) template)
687 (aver (not (template-more-results-type template)))
688 (aver (not (eq rtypes :conditional)))
689 (aver (null info-args))
692 (emit-template call block template args r-refs info)
693 (emit-template call block template args r-refs))
695 (move-lvar-result call block results lvar)))
700 ;;; Convert a LET by moving the argument values into the variables.
701 ;;; Since a LET doesn't have any passing locations, we move the
702 ;;; arguments directly into the variables. We must also allocate any
703 ;;; indirect value cells, since there is no function prologue to do
705 (defun ir2-convert-let (node block fun)
706 (declare (type combination node) (type ir2-block block) (type clambda fun))
707 (mapc (lambda (var arg)
709 (let ((src (lvar-tn node block arg))
710 (dest (leaf-info var)))
711 (if (lambda-var-indirect var)
712 (emit-make-value-cell node block src dest)
713 (emit-move node block src dest)))))
714 (lambda-vars fun) (basic-combination-args node))
717 ;;; Emit any necessary moves into assignment temps for a local call to
718 ;;; FUN. We return two lists of TNs: TNs holding the actual argument
719 ;;; values, and (possibly EQ) TNs that are the actual destination of
720 ;;; the arguments. When necessary, we allocate temporaries for
721 ;;; arguments to preserve parallel assignment semantics. These lists
722 ;;; exclude unused arguments and include implicit environment
723 ;;; arguments, i.e. they exactly correspond to the arguments passed.
725 ;;; OLD-FP is the TN currently holding the value we want to pass as
726 ;;; OLD-FP. If null, then the call is to the same environment (an
727 ;;; :ASSIGNMENT), so we only move the arguments, and leave the
728 ;;; environment alone.
729 (defun emit-psetq-moves (node block fun old-fp)
730 (declare (type combination node) (type ir2-block block) (type clambda fun)
731 (type (or tn null) old-fp))
732 (let ((actuals (mapcar (lambda (x)
734 (lvar-tn node block x)))
735 (combination-args node))))
738 (dolist (var (lambda-vars fun))
739 (let ((actual (pop actuals))
740 (loc (leaf-info var)))
743 ((lambda-var-indirect var)
745 (make-normal-tn *backend-t-primitive-type*)))
746 (emit-make-value-cell node block actual temp)
748 ((member actual (locs))
749 (let ((temp (make-normal-tn (tn-primitive-type loc))))
750 (emit-move node block actual temp)
757 (let ((this-1env (node-physenv node))
758 (called-env (physenv-info (lambda-physenv fun))))
759 (dolist (thing (ir2-physenv-closure called-env))
760 (temps (find-in-physenv (car thing) this-1env))
763 (locs (ir2-physenv-old-fp called-env))))
765 (values (temps) (locs)))))
767 ;;; A tail-recursive local call is done by emitting moves of stuff
768 ;;; into the appropriate passing locations. After setting up the args
769 ;;; and environment, we just move our return-pc into the called
770 ;;; function's passing location.
771 (defun ir2-convert-tail-local-call (node block fun)
772 (declare (type combination node) (type ir2-block block) (type clambda fun))
773 (let ((this-env (physenv-info (node-physenv node))))
774 (multiple-value-bind (temps locs)
775 (emit-psetq-moves node block fun (ir2-physenv-old-fp this-env))
777 (mapc (lambda (temp loc)
778 (emit-move node block temp loc))
781 (emit-move node block
782 (ir2-physenv-return-pc this-env)
783 (ir2-physenv-return-pc-pass
785 (lambda-physenv fun)))))
789 ;;; Convert an :ASSIGNMENT call. This is just like a tail local call,
790 ;;; except that the caller and callee environment are the same, so we
791 ;;; don't need to mess with the environment locations, return PC, etc.
792 (defun ir2-convert-assignment (node block fun)
793 (declare (type combination node) (type ir2-block block) (type clambda fun))
794 (multiple-value-bind (temps locs) (emit-psetq-moves node block fun nil)
796 (mapc (lambda (temp loc)
797 (emit-move node block temp loc))
801 ;;; Do stuff to set up the arguments to a non-tail local call
802 ;;; (including implicit environment args.) We allocate a frame
803 ;;; (returning the FP and NFP), and also compute the TN-REFS list for
804 ;;; the values to pass and the list of passing location TNs.
805 (defun ir2-convert-local-call-args (node block fun)
806 (declare (type combination node) (type ir2-block block) (type clambda fun))
807 (let ((fp (make-stack-pointer-tn))
808 (nfp (make-number-stack-pointer-tn))
809 (old-fp (make-stack-pointer-tn)))
810 (multiple-value-bind (temps locs)
811 (emit-psetq-moves node block fun old-fp)
812 (vop current-fp node block old-fp)
813 (vop allocate-frame node block
814 (physenv-info (lambda-physenv fun))
816 (values fp nfp temps (mapcar #'make-alias-tn locs)))))
818 ;;; Handle a non-TR known-values local call. We emit the call, then
819 ;;; move the results to the lvar's destination.
820 (defun ir2-convert-local-known-call (node block fun returns lvar start)
821 (declare (type node node) (type ir2-block block) (type clambda fun)
822 (type return-info returns) (type (or lvar null) lvar)
824 (multiple-value-bind (fp nfp temps arg-locs)
825 (ir2-convert-local-call-args node block fun)
826 (let ((locs (return-info-locations returns)))
827 (vop* known-call-local node block
828 (fp nfp (reference-tn-list temps nil))
829 ((reference-tn-list locs t))
830 arg-locs (physenv-info (lambda-physenv fun)) start)
831 (move-lvar-result node block locs lvar)))
834 ;;; Handle a non-TR unknown-values local call. We do different things
835 ;;; depending on what kind of values the lvar wants.
837 ;;; If LVAR is :UNKNOWN, then we use the "multiple-" variant, directly
838 ;;; specifying the lvar's LOCS as the VOP results so that we don't
839 ;;; have to do anything after the call.
841 ;;; Otherwise, we use STANDARD-RESULT-TNS to get wired result TNs, and
842 ;;; then call MOVE-LVAR-RESULT to do any necessary type checks or
844 (defun ir2-convert-local-unknown-call (node block fun lvar start)
845 (declare (type node node) (type ir2-block block) (type clambda fun)
846 (type (or lvar null) lvar) (type label start))
847 (multiple-value-bind (fp nfp temps arg-locs)
848 (ir2-convert-local-call-args node block fun)
849 (let ((2lvar (and lvar (lvar-info lvar)))
850 (env (physenv-info (lambda-physenv fun)))
851 (temp-refs (reference-tn-list temps nil)))
852 (if (and 2lvar (eq (ir2-lvar-kind 2lvar) :unknown))
853 (vop* multiple-call-local node block (fp nfp temp-refs)
854 ((reference-tn-list (ir2-lvar-locs 2lvar) t))
856 (let ((locs (standard-result-tns lvar)))
857 (vop* call-local node block
859 ((reference-tn-list locs t))
860 arg-locs env start (length locs))
861 (move-lvar-result node block locs lvar)))))
864 ;;; Dispatch to the appropriate function, depending on whether we have
865 ;;; a let, tail or normal call. If the function doesn't return, call
866 ;;; it using the unknown-value convention. We could compile it as a
867 ;;; tail call, but that might seem confusing in the debugger.
868 (defun ir2-convert-local-call (node block)
869 (declare (type combination node) (type ir2-block block))
870 (let* ((fun (ref-leaf (lvar-uses (basic-combination-fun node))))
871 (kind (functional-kind fun)))
872 (cond ((eq kind :let)
873 (ir2-convert-let node block fun))
874 ((eq kind :assignment)
875 (ir2-convert-assignment node block fun))
877 (ir2-convert-tail-local-call node block fun))
879 (let ((start (block-label (lambda-block fun)))
880 (returns (tail-set-info (lambda-tail-set fun)))
881 (lvar (node-lvar node)))
883 (return-info-kind returns)
886 (ir2-convert-local-unknown-call node block fun lvar start))
888 (ir2-convert-local-known-call node block fun returns
894 ;;; Given a function lvar FUN, return (VALUES TN-TO-CALL NAMED-P),
895 ;;; where TN-TO-CALL is a TN holding the thing that we call NAMED-P is
896 ;;; true if the thing is named (false if it is a function).
898 ;;; There are two interesting non-named cases:
899 ;;; -- We know it's a function. No check needed: return the
901 ;;; -- We don't know what it is.
902 (defun fun-lvar-tn (node block lvar)
903 (declare (ignore node block))
904 (declare (type lvar lvar))
905 (let ((2lvar (lvar-info lvar)))
906 (if (eq (ir2-lvar-kind 2lvar) :delayed)
907 (let ((name (lvar-fun-name lvar t)))
909 (values (make-load-time-constant-tn :fdefinition name) t))
910 (let* ((locs (ir2-lvar-locs 2lvar))
912 (function-ptype (primitive-type-or-lose 'function)))
913 (aver (and (eq (ir2-lvar-kind 2lvar) :fixed)
914 (= (length locs) 1)))
915 (aver (eq (tn-primitive-type loc) function-ptype))
918 ;;; Set up the args to NODE in the current frame, and return a TN-REF
919 ;;; list for the passing locations.
920 (defun move-tail-full-call-args (node block)
921 (declare (type combination node) (type ir2-block block))
922 (let ((args (basic-combination-args node))
925 (dotimes (num (length args))
926 (let ((loc (standard-arg-location num)))
927 (emit-move node block (lvar-tn node block (elt args num)) loc)
928 (let ((ref (reference-tn loc nil)))
930 (setf (tn-ref-across last) ref)
935 ;;; Move the arguments into the passing locations and do a (possibly
936 ;;; named) tail call.
937 (defun ir2-convert-tail-full-call (node block)
938 (declare (type combination node) (type ir2-block block))
939 (let* ((env (physenv-info (node-physenv node)))
940 (args (basic-combination-args node))
941 (nargs (length args))
942 (pass-refs (move-tail-full-call-args node block))
943 (old-fp (ir2-physenv-old-fp env))
944 (return-pc (ir2-physenv-return-pc env)))
946 (multiple-value-bind (fun-tn named)
947 (fun-lvar-tn node block (basic-combination-fun node))
949 (vop* tail-call-named node block
950 (fun-tn old-fp return-pc pass-refs)
953 (vop* tail-call node block
954 (fun-tn old-fp return-pc pass-refs)
960 ;;; like IR2-CONVERT-LOCAL-CALL-ARGS, only different
961 (defun ir2-convert-full-call-args (node block)
962 (declare (type combination node) (type ir2-block block))
963 (let* ((args (basic-combination-args node))
964 (fp (make-stack-pointer-tn))
965 (nargs (length args)))
966 (vop allocate-full-call-frame node block nargs fp)
971 (locs (standard-arg-location num))
972 (let ((ref (reference-tn (lvar-tn node block (elt args num))
975 (setf (tn-ref-across last) ref)
979 (values fp first (locs) nargs)))))
981 ;;; Do full call when a fixed number of values are desired. We make
982 ;;; STANDARD-RESULT-TNS for our lvar, then deliver the result using
983 ;;; MOVE-LVAR-RESULT. We do named or normal call, as appropriate.
984 (defun ir2-convert-fixed-full-call (node block)
985 (declare (type combination node) (type ir2-block block))
986 (multiple-value-bind (fp args arg-locs nargs)
987 (ir2-convert-full-call-args node block)
988 (let* ((lvar (node-lvar node))
989 (locs (standard-result-tns lvar))
990 (loc-refs (reference-tn-list locs t))
991 (nvals (length locs)))
992 (multiple-value-bind (fun-tn named)
993 (fun-lvar-tn node block (basic-combination-fun node))
995 (vop* call-named node block (fp fun-tn args) (loc-refs)
996 arg-locs nargs nvals)
997 (vop* call node block (fp fun-tn args) (loc-refs)
998 arg-locs nargs nvals))
999 (move-lvar-result node block locs lvar))))
1002 ;;; Do full call when unknown values are desired.
1003 (defun ir2-convert-multiple-full-call (node block)
1004 (declare (type combination node) (type ir2-block block))
1005 (multiple-value-bind (fp args arg-locs nargs)
1006 (ir2-convert-full-call-args node block)
1007 (let* ((lvar (node-lvar node))
1008 (locs (ir2-lvar-locs (lvar-info lvar)))
1009 (loc-refs (reference-tn-list locs t)))
1010 (multiple-value-bind (fun-tn named)
1011 (fun-lvar-tn node block (basic-combination-fun node))
1013 (vop* multiple-call-named node block (fp fun-tn args) (loc-refs)
1015 (vop* multiple-call node block (fp fun-tn args) (loc-refs)
1019 ;;; stuff to check in PONDER-FULL-CALL
1021 ;;; These came in handy when troubleshooting cold boot after making
1022 ;;; major changes in the package structure: various transforms and
1023 ;;; VOPs and stuff got attached to the wrong symbol, so that
1024 ;;; references to the right symbol were bogusly translated as full
1025 ;;; calls instead of primitives, sending the system off into infinite
1026 ;;; space. Having a report on all full calls generated makes it easier
1027 ;;; to figure out what form caused the problem this time.
1028 #!+sb-show (defvar *show-full-called-fnames-p* nil)
1029 #!+sb-show (defvar *full-called-fnames* (make-hash-table :test 'equal))
1031 ;;; Do some checks (and store some notes relevant for future checks)
1033 ;;; * Is this a full call to something we have reason to know should
1034 ;;; never be full called? (Except as of sbcl-0.7.18 or so, we no
1035 ;;; longer try to ensure this behavior when *FAILURE-P* has already
1037 ;;; * Is this a full call to (SETF FOO) which might conflict with
1038 ;;; a DEFSETF or some such thing elsewhere in the program?
1039 (defun ponder-full-call (node)
1040 (let* ((lvar (basic-combination-fun node))
1041 (fname (lvar-fun-name lvar t)))
1042 (declare (type (or symbol cons) fname))
1044 #!+sb-show (unless (gethash fname *full-called-fnames*)
1045 (setf (gethash fname *full-called-fnames*) t))
1046 #!+sb-show (when *show-full-called-fnames-p*
1047 (/show "converting full call to named function" fname)
1048 (/show (basic-combination-args node))
1049 (/show (policy node speed) (policy node safety))
1050 (/show (policy node compilation-speed))
1051 (let ((arg-types (mapcar (lambda (lvar)
1055 (basic-combination-args node))))
1058 ;; When illegal code is compiled, all sorts of perverse paths
1059 ;; through the compiler can be taken, and it's much harder -- and
1060 ;; probably pointless -- to guarantee that always-optimized-away
1061 ;; functions are actually optimized away. Thus, we skip the check
1064 ;; check to see if we know anything about the function
1065 (let ((info (info :function :info fname)))
1066 ;; if we know something, check to see if the full call was valid
1067 (when (and info (ir1-attributep (fun-info-attributes info)
1068 always-translatable))
1069 (/show (policy node speed) (policy node safety))
1070 (/show (policy node compilation-speed))
1071 (bug "full call to ~S" fname))))
1074 (aver (legal-fun-name-p fname))
1075 (destructuring-bind (setfoid &rest stem) fname
1076 (when (eq setfoid 'setf)
1077 (setf (gethash (car stem) *setf-assumed-fboundp*) t))))))
1079 ;;; If the call is in a tail recursive position and the return
1080 ;;; convention is standard, then do a tail full call. If one or fewer
1081 ;;; values are desired, then use a single-value call, otherwise use a
1082 ;;; multiple-values call.
1083 (defun ir2-convert-full-call (node block)
1084 (declare (type combination node) (type ir2-block block))
1085 (ponder-full-call node)
1086 (cond ((node-tail-p node)
1087 (ir2-convert-tail-full-call node block))
1088 ((let ((lvar (node-lvar node)))
1090 (eq (ir2-lvar-kind (lvar-info lvar)) :unknown)))
1091 (ir2-convert-multiple-full-call node block))
1093 (ir2-convert-fixed-full-call node block)))
1096 ;;;; entering functions
1098 ;;; Do all the stuff that needs to be done on XEP entry:
1099 ;;; -- Create frame.
1100 ;;; -- Copy any more arg.
1101 ;;; -- Set up the environment, accessing any closure variables.
1102 ;;; -- Move args from the standard passing locations to their internal
1104 (defun init-xep-environment (node block fun)
1105 (declare (type bind node) (type ir2-block block) (type clambda fun))
1106 (let ((start-label (entry-info-offset (leaf-info fun)))
1107 (env (physenv-info (node-physenv node))))
1108 (let ((ef (functional-entry-fun fun)))
1109 (cond ((and (optional-dispatch-p ef) (optional-dispatch-more-entry ef))
1110 ;; Special case the xep-allocate-frame + copy-more-arg case.
1111 (vop xep-allocate-frame node block start-label t)
1112 (vop copy-more-arg node block (optional-dispatch-max-args ef)))
1114 ;; No more args, so normal entry.
1115 (vop xep-allocate-frame node block start-label nil)))
1116 (if (ir2-physenv-closure env)
1117 (let ((closure (make-normal-tn *backend-t-primitive-type*)))
1118 (vop setup-closure-environment node block start-label closure)
1119 ;; KLUDGE: see the comment around the definition of
1120 ;; CLOSURE objects in src/compiler/objdef.lisp
1121 (vop funcallable-instance-lexenv node block closure closure)
1123 (dolist (loc (ir2-physenv-closure env))
1124 (vop closure-ref node block closure (incf n) (cdr loc)))))
1125 (vop setup-environment node block start-label)))
1127 (unless (eq (functional-kind fun) :toplevel)
1128 (let ((vars (lambda-vars fun))
1130 (when (leaf-refs (first vars))
1131 (emit-move node block (make-arg-count-location)
1132 (leaf-info (first vars))))
1133 (dolist (arg (rest vars))
1134 (when (leaf-refs arg)
1135 (let ((pass (standard-arg-location n))
1136 (home (leaf-info arg)))
1137 (if (lambda-var-indirect arg)
1138 (emit-make-value-cell node block pass home)
1139 (emit-move node block pass home))))
1142 (emit-move node block (make-old-fp-passing-location t)
1143 (ir2-physenv-old-fp env)))
1147 ;;; Emit function prolog code. This is only called on bind nodes for
1148 ;;; functions that allocate environments. All semantics of let calls
1149 ;;; are handled by IR2-CONVERT-LET.
1151 ;;; If not an XEP, all we do is move the return PC from its passing
1152 ;;; location, since in a local call, the caller allocates the frame
1153 ;;; and sets up the arguments.
1154 (defun ir2-convert-bind (node block)
1155 (declare (type bind node) (type ir2-block block))
1156 (let* ((fun (bind-lambda node))
1157 (env (physenv-info (lambda-physenv fun))))
1158 (aver (member (functional-kind fun)
1159 '(nil :external :optional :toplevel :cleanup)))
1162 (init-xep-environment node block fun)
1164 (when *collect-dynamic-statistics*
1165 (vop count-me node block *dynamic-counts-tn*
1166 (block-number (ir2-block-block block)))))
1170 (ir2-physenv-return-pc-pass env)
1171 (ir2-physenv-return-pc env))
1173 (let ((lab (gen-label)))
1174 (setf (ir2-physenv-environment-start env) lab)
1175 (vop note-environment-start node block lab)))
1179 ;;;; function return
1181 ;;; Do stuff to return from a function with the specified values and
1182 ;;; convention. If the return convention is :FIXED and we aren't
1183 ;;; returning from an XEP, then we do a known return (letting
1184 ;;; representation selection insert the correct move-arg VOPs.)
1185 ;;; Otherwise, we use the unknown-values convention. If there is a
1186 ;;; fixed number of return values, then use RETURN, otherwise use
1187 ;;; RETURN-MULTIPLE.
1188 (defun ir2-convert-return (node block)
1189 (declare (type creturn node) (type ir2-block block))
1190 (let* ((lvar (return-result node))
1191 (2lvar (lvar-info lvar))
1192 (lvar-kind (ir2-lvar-kind 2lvar))
1193 (fun (return-lambda node))
1194 (env (physenv-info (lambda-physenv fun)))
1195 (old-fp (ir2-physenv-old-fp env))
1196 (return-pc (ir2-physenv-return-pc env))
1197 (returns (tail-set-info (lambda-tail-set fun))))
1199 ((and (eq (return-info-kind returns) :fixed)
1201 (let ((locs (lvar-tns node block lvar
1202 (return-info-types returns))))
1203 (vop* known-return node block
1204 (old-fp return-pc (reference-tn-list locs nil))
1206 (return-info-locations returns))))
1207 ((eq lvar-kind :fixed)
1208 (let* ((types (mapcar #'tn-primitive-type (ir2-lvar-locs 2lvar)))
1209 (lvar-locs (lvar-tns node block lvar types))
1210 (nvals (length lvar-locs))
1211 (locs (make-standard-value-tns nvals)))
1212 (mapc (lambda (val loc)
1213 (emit-move node block val loc))
1217 (vop return-single node block old-fp return-pc (car locs))
1218 (vop* return node block
1219 (old-fp return-pc (reference-tn-list locs nil))
1223 (aver (eq lvar-kind :unknown))
1224 (vop* return-multiple node block
1226 (reference-tn-list (ir2-lvar-locs 2lvar) nil))
1233 ;;; This is used by the debugger to find the top function on the
1234 ;;; stack. It returns the OLD-FP and RETURN-PC for the current
1235 ;;; function as multiple values.
1236 (defoptimizer (sb!kernel:%caller-frame-and-pc ir2-convert) (() node block)
1237 (let ((ir2-physenv (physenv-info (node-physenv node))))
1238 (move-lvar-result node block
1239 (list (ir2-physenv-old-fp ir2-physenv)
1240 (ir2-physenv-return-pc ir2-physenv))
1243 ;;;; multiple values
1245 ;;; This is almost identical to IR2-CONVERT-LET. Since LTN annotates
1246 ;;; the lvar for the correct number of values (with the lvar user
1247 ;;; responsible for defaulting), we can just pick them up from the
1249 (defun ir2-convert-mv-bind (node block)
1250 (declare (type mv-combination node) (type ir2-block block))
1251 (let* ((lvar (first (basic-combination-args node)))
1252 (fun (ref-leaf (lvar-uses (basic-combination-fun node))))
1253 (vars (lambda-vars fun)))
1254 (aver (eq (functional-kind fun) :mv-let))
1255 (mapc (lambda (src var)
1256 (when (leaf-refs var)
1257 (let ((dest (leaf-info var)))
1258 (if (lambda-var-indirect var)
1259 (emit-make-value-cell node block src dest)
1260 (emit-move node block src dest)))))
1261 (lvar-tns node block lvar
1263 (primitive-type (leaf-type x)))
1268 ;;; Emit the appropriate fixed value, unknown value or tail variant of
1269 ;;; CALL-VARIABLE. Note that we only need to pass the values start for
1270 ;;; the first argument: all the other argument lvar TNs are
1271 ;;; ignored. This is because we require all of the values globs to be
1272 ;;; contiguous and on stack top.
1273 (defun ir2-convert-mv-call (node block)
1274 (declare (type mv-combination node) (type ir2-block block))
1275 (aver (basic-combination-args node))
1276 (let* ((start-lvar (lvar-info (first (basic-combination-args node))))
1277 (start (first (ir2-lvar-locs start-lvar)))
1278 (tails (and (node-tail-p node)
1279 (lambda-tail-set (node-home-lambda node))))
1280 (lvar (node-lvar node))
1281 (2lvar (and lvar (lvar-info lvar))))
1282 (multiple-value-bind (fun named)
1283 (fun-lvar-tn node block (basic-combination-fun node))
1284 (aver (and (not named)
1285 (eq (ir2-lvar-kind start-lvar) :unknown)))
1288 (let ((env (physenv-info (node-physenv node))))
1289 (vop tail-call-variable node block start fun
1290 (ir2-physenv-old-fp env)
1291 (ir2-physenv-return-pc env))))
1293 (eq (ir2-lvar-kind 2lvar) :unknown))
1294 (vop* multiple-call-variable node block (start fun nil)
1295 ((reference-tn-list (ir2-lvar-locs 2lvar) t))))
1297 (let ((locs (standard-result-tns lvar)))
1298 (vop* call-variable node block (start fun nil)
1299 ((reference-tn-list locs t)) (length locs))
1300 (move-lvar-result node block locs lvar)))))))
1302 ;;; Reset the stack pointer to the start of the specified
1303 ;;; unknown-values lvar (discarding it and all values globs on top of
1305 (defoptimizer (%pop-values ir2-convert) ((%lvar) node block)
1306 (let* ((lvar (lvar-value %lvar))
1307 (2lvar (lvar-info lvar)))
1308 (cond ((eq (ir2-lvar-kind 2lvar) :unknown)
1309 (vop reset-stack-pointer node block
1310 (first (ir2-lvar-locs 2lvar))))
1311 ((lvar-dynamic-extent lvar)
1312 (vop reset-stack-pointer node block
1313 (ir2-lvar-stack-pointer 2lvar)))
1314 (t (bug "Trying to pop a not stack-allocated LVAR ~S."
1317 (defoptimizer (%nip-values ir2-convert) ((last-nipped last-preserved
1320 (let* ( ;; pointer immediately after the nipped block
1321 (after (lvar-value last-nipped))
1322 (2after (lvar-info after))
1323 ;; pointer to the first nipped word
1324 (first (lvar-value last-preserved))
1325 (2first (lvar-info first))
1327 (moved-tns (loop for lvar-ref in moved
1328 for lvar = (lvar-value lvar-ref)
1329 for 2lvar = (lvar-info lvar)
1331 collect (first (ir2-lvar-locs 2lvar)))))
1332 (aver (or (eq (ir2-lvar-kind 2after) :unknown)
1333 (lvar-dynamic-extent after)))
1334 (aver (eq (ir2-lvar-kind 2first) :unknown))
1335 (when *check-consistency*
1336 ;; we cannot move stack-allocated DX objects
1337 (dolist (moved-lvar moved)
1338 (aver (eq (ir2-lvar-kind (lvar-info (lvar-value moved-lvar)))
1340 (flet ((nip-aligned (nipped)
1341 (vop* %%nip-values node block
1343 (first (ir2-lvar-locs 2first))
1344 (reference-tn-list moved-tns nil))
1345 ((reference-tn-list moved-tns t)))))
1346 (cond ((eq (ir2-lvar-kind 2after) :unknown)
1347 (nip-aligned (first (ir2-lvar-locs 2after))))
1348 ((lvar-dynamic-extent after)
1349 (nip-aligned (ir2-lvar-stack-pointer 2after)))
1351 (bug "Trying to nip a not stack-allocated LVAR ~S." after))))))
1353 ;;; Deliver the values TNs to LVAR using MOVE-LVAR-RESULT.
1354 (defoptimizer (values ir2-convert) ((&rest values) node block)
1355 (let ((tns (mapcar (lambda (x)
1356 (lvar-tn node block x))
1358 (move-lvar-result node block tns (node-lvar node))))
1360 ;;; In the normal case where unknown values are desired, we use the
1361 ;;; VALUES-LIST VOP. In the relatively unimportant case of VALUES-LIST
1362 ;;; for a fixed number of values, we punt by doing a full call to the
1363 ;;; VALUES-LIST function. This gets the full call VOP to deal with
1364 ;;; defaulting any unsupplied values. It seems unworthwhile to
1365 ;;; optimize this case.
1366 (defoptimizer (values-list ir2-convert) ((list) node block)
1367 (let* ((lvar (node-lvar node))
1368 (2lvar (and lvar (lvar-info lvar))))
1370 (eq (ir2-lvar-kind 2lvar) :unknown))
1371 (let ((locs (ir2-lvar-locs 2lvar)))
1372 (vop* values-list node block
1373 ((lvar-tn node block list) nil)
1374 ((reference-tn-list locs t)))))
1375 (t (aver (or (not 2lvar) ; i.e. we want to check the argument
1376 (eq (ir2-lvar-kind 2lvar) :fixed)))
1377 (ir2-convert-full-call node block)))))
1379 (defoptimizer (%more-arg-values ir2-convert) ((context start count) node block)
1380 (binding* ((lvar (node-lvar node) :exit-if-null)
1381 (2lvar (lvar-info lvar)))
1382 (ecase (ir2-lvar-kind 2lvar)
1383 (:fixed (ir2-convert-full-call node block))
1385 (let ((locs (ir2-lvar-locs 2lvar)))
1386 (vop* %more-arg-values node block
1387 ((lvar-tn node block context)
1388 (lvar-tn node block start)
1389 (lvar-tn node block count)
1391 ((reference-tn-list locs t))))))))
1393 ;;;; special binding
1395 ;;; This is trivial, given our assumption of a shallow-binding
1397 (defoptimizer (%special-bind ir2-convert) ((var value) node block)
1398 (let ((name (leaf-source-name (lvar-value var))))
1399 (vop bind node block (lvar-tn node block value)
1400 (emit-constant name))))
1401 (defoptimizer (%special-unbind ir2-convert) ((var) node block)
1402 (vop unbind node block))
1404 ;;; ### It's not clear that this really belongs in this file, or
1405 ;;; should really be done this way, but this is the least violation of
1406 ;;; abstraction in the current setup. We don't want to wire
1407 ;;; shallow-binding assumptions into IR1tran.
1408 (def-ir1-translator progv
1409 ((vars vals &body body) start next result)
1412 (with-unique-names (bind unbind)
1413 (once-only ((n-save-bs '(%primitive current-binding-pointer)))
1416 (labels ((,unbind (vars)
1417 (declare (optimize (speed 2) (debug 0)))
1419 (%primitive bind nil var)
1422 (declare (optimize (speed 2) (debug 0)))
1424 ((null vals) (,unbind vars))
1428 (,bind (cdr vars) (cdr vals))))))
1429 (,bind ,vars ,vals))
1432 (%primitive unbind-to-here ,n-save-bs))))))
1436 ;;; Convert a non-local lexical exit. First find the NLX-INFO in our
1437 ;;; environment. Note that this is never called on the escape exits
1438 ;;; for CATCH and UNWIND-PROTECT, since the escape functions aren't
1440 (defun ir2-convert-exit (node block)
1441 (declare (type exit node) (type ir2-block block))
1442 (let* ((nlx (exit-nlx-info node))
1443 (loc (find-in-physenv nlx (node-physenv node)))
1444 (temp (make-stack-pointer-tn))
1445 (value (exit-value node)))
1446 (if (nlx-info-safe-p nlx)
1447 (vop value-cell-ref node block loc temp)
1448 (emit-move node block loc temp))
1450 (let ((locs (ir2-lvar-locs (lvar-info value))))
1451 (vop unwind node block temp (first locs) (second locs)))
1452 (let ((0-tn (emit-constant 0)))
1453 (vop unwind node block temp 0-tn 0-tn))))
1457 ;;; %CLEANUP-POINT doesn't do anything except prevent the body from
1458 ;;; being entirely deleted.
1459 (defoptimizer (%cleanup-point ir2-convert) (() node block) node block)
1461 ;;; This function invalidates a lexical exit on exiting from the
1462 ;;; dynamic extent. This is done by storing 0 into the indirect value
1463 ;;; cell that holds the closed unwind block.
1464 (defoptimizer (%lexical-exit-breakup ir2-convert) ((info) node block)
1465 (let ((nlx (lvar-value info)))
1466 (when (nlx-info-safe-p nlx)
1467 (vop value-cell-set node block
1468 (find-in-physenv nlx (node-physenv node))
1469 (emit-constant 0)))))
1471 ;;; We have to do a spurious move of no values to the result lvar so
1472 ;;; that lifetime analysis won't get confused.
1473 (defun ir2-convert-throw (node block)
1474 (declare (type mv-combination node) (type ir2-block block))
1475 (let ((args (basic-combination-args node)))
1476 (check-catch-tag-type (first args))
1477 (vop* throw node block
1478 ((lvar-tn node block (first args))
1480 (ir2-lvar-locs (lvar-info (second args)))
1483 (move-lvar-result node block () (node-lvar node))
1486 ;;; Emit code to set up a non-local exit. INFO is the NLX-INFO for the
1487 ;;; exit, and TAG is the lvar for the catch tag (if any.) We get at
1488 ;;; the target PC by passing in the label to the vop. The vop is
1489 ;;; responsible for building a return-PC object.
1490 (defun emit-nlx-start (node block info tag)
1491 (declare (type node node) (type ir2-block block) (type nlx-info info)
1492 (type (or lvar null) tag))
1493 (let* ((2info (nlx-info-info info))
1494 (kind (cleanup-kind (nlx-info-cleanup info)))
1495 (block-tn (physenv-live-tn
1496 (make-normal-tn (primitive-type-or-lose 'catch-block))
1497 (node-physenv node)))
1498 (res (make-stack-pointer-tn))
1499 (target-label (ir2-nlx-info-target 2info)))
1501 (vop current-binding-pointer node block
1502 (car (ir2-nlx-info-dynamic-state 2info)))
1503 (vop* save-dynamic-state node block
1505 ((reference-tn-list (cdr (ir2-nlx-info-dynamic-state 2info)) t)))
1506 (vop current-stack-pointer node block (ir2-nlx-info-save-sp 2info))
1510 (vop make-catch-block node block block-tn
1511 (lvar-tn node block tag) target-label res))
1512 ((:unwind-protect :block :tagbody)
1513 (vop make-unwind-block node block block-tn target-label res)))
1517 (if (nlx-info-safe-p info)
1518 (emit-make-value-cell node block res (ir2-nlx-info-home 2info))
1519 (emit-move node block res (ir2-nlx-info-home 2info))))
1521 (vop set-unwind-protect node block block-tn))
1526 ;;; Scan each of ENTRY's exits, setting up the exit for each lexical exit.
1527 (defun ir2-convert-entry (node block)
1528 (declare (type entry node) (type ir2-block block))
1530 (dolist (exit (entry-exits node))
1531 (let ((info (exit-nlx-info exit)))
1533 (not (memq info nlxes))
1534 (member (cleanup-kind (nlx-info-cleanup info))
1535 '(:block :tagbody)))
1537 (emit-nlx-start node block info nil)))))
1540 ;;; Set up the unwind block for these guys.
1541 (defoptimizer (%catch ir2-convert) ((info-lvar tag) node block)
1542 (check-catch-tag-type tag)
1543 (emit-nlx-start node block (lvar-value info-lvar) tag))
1544 (defoptimizer (%unwind-protect ir2-convert) ((info-lvar cleanup) node block)
1545 (emit-nlx-start node block (lvar-value info-lvar) nil))
1547 ;;; Emit the entry code for a non-local exit. We receive values and
1548 ;;; restore dynamic state.
1550 ;;; In the case of a lexical exit or CATCH, we look at the exit lvar's
1551 ;;; kind to determine which flavor of entry VOP to emit. If unknown
1552 ;;; values, emit the xxx-MULTIPLE variant to the lvar locs. If fixed
1553 ;;; values, make the appropriate number of temps in the standard
1554 ;;; values locations and use the other variant, delivering the temps
1555 ;;; to the lvar using MOVE-LVAR-RESULT.
1557 ;;; In the UNWIND-PROTECT case, we deliver the first register
1558 ;;; argument, the argument count and the argument pointer to our lvar
1559 ;;; as multiple values. These values are the block exited to and the
1560 ;;; values start and count.
1562 ;;; After receiving values, we restore dynamic state. Except in the
1563 ;;; UNWIND-PROTECT case, the values receiving restores the stack
1564 ;;; pointer. In an UNWIND-PROTECT cleanup, we want to leave the stack
1565 ;;; pointer alone, since the thrown values are still out there.
1566 (defoptimizer (%nlx-entry ir2-convert) ((info-lvar) node block)
1567 (let* ((info (lvar-value info-lvar))
1568 (lvar (node-lvar node))
1569 (2info (nlx-info-info info))
1570 (top-loc (ir2-nlx-info-save-sp 2info))
1571 (start-loc (make-nlx-entry-arg-start-location))
1572 (count-loc (make-arg-count-location))
1573 (target (ir2-nlx-info-target 2info)))
1575 (ecase (cleanup-kind (nlx-info-cleanup info))
1576 ((:catch :block :tagbody)
1577 (let ((2lvar (and lvar (lvar-info lvar))))
1578 (if (and 2lvar (eq (ir2-lvar-kind 2lvar) :unknown))
1579 (vop* nlx-entry-multiple node block
1580 (top-loc start-loc count-loc nil)
1581 ((reference-tn-list (ir2-lvar-locs 2lvar) t))
1583 (let ((locs (standard-result-tns lvar)))
1584 (vop* nlx-entry node block
1585 (top-loc start-loc count-loc nil)
1586 ((reference-tn-list locs t))
1589 (move-lvar-result node block locs lvar)))))
1591 (let ((block-loc (standard-arg-location 0)))
1592 (vop uwp-entry node block target block-loc start-loc count-loc)
1595 (list block-loc start-loc count-loc)
1599 (when *collect-dynamic-statistics*
1600 (vop count-me node block *dynamic-counts-tn*
1601 (block-number (ir2-block-block block))))
1603 (vop* restore-dynamic-state node block
1604 ((reference-tn-list (cdr (ir2-nlx-info-dynamic-state 2info)) nil))
1606 (vop unbind-to-here node block
1607 (car (ir2-nlx-info-dynamic-state 2info)))))
1609 ;;;; n-argument functions
1611 (macrolet ((def (name)
1612 `(defoptimizer (,name ir2-convert) ((&rest args) node block)
1613 (let* ((refs (move-tail-full-call-args node block))
1614 (lvar (node-lvar node))
1615 (res (lvar-result-tns
1617 (list (primitive-type (specifier-type 'list))))))
1618 (when (and lvar (lvar-dynamic-extent lvar))
1619 (vop current-stack-pointer node block
1620 (ir2-lvar-stack-pointer (lvar-info lvar))))
1621 (vop* ,name node block (refs) ((first res) nil)
1623 (move-lvar-result node block res lvar)))))
1628 ;;; Convert the code in a component into VOPs.
1629 (defun ir2-convert (component)
1630 (declare (type component component))
1631 (let (#!+sb-dyncount
1632 (*dynamic-counts-tn*
1633 (when *collect-dynamic-statistics*
1635 (block-number (block-next (component-head component))))
1636 (counts (make-array blocks
1637 :element-type '(unsigned-byte 32)
1638 :initial-element 0))
1639 (info (make-dyncount-info
1640 :for (component-name component)
1641 :costs (make-array blocks
1642 :element-type '(unsigned-byte 32)
1645 (setf (ir2-component-dyncount-info (component-info component))
1647 (emit-constant info)
1648 (emit-constant counts)))))
1650 (declare (type index num))
1651 (do-ir2-blocks (2block component)
1652 (let ((block (ir2-block-block 2block)))
1653 (when (block-start block)
1654 (setf (block-number block) num)
1656 (when *collect-dynamic-statistics*
1657 (let ((first-node (block-start-node block)))
1658 (unless (or (and (bind-p first-node)
1659 (xep-p (bind-lambda first-node)))
1661 (node-lvar first-node))
1666 #!+sb-dyncount *dynamic-counts-tn* #!-sb-dyncount nil
1668 (ir2-convert-block block)
1672 ;;; If necessary, emit a terminal unconditional branch to go to the
1673 ;;; successor block. If the successor is the component tail, then
1674 ;;; there isn't really any successor, but if the end is an unknown,
1675 ;;; non-tail call, then we emit an error trap just in case the
1676 ;;; function really does return.
1677 (defun finish-ir2-block (block)
1678 (declare (type cblock block))
1679 (let* ((2block (block-info block))
1680 (last (block-last block))
1681 (succ (block-succ block)))
1683 (aver (singleton-p succ))
1684 (let ((target (first succ)))
1685 (cond ((eq target (component-tail (block-component block)))
1686 (when (and (basic-combination-p last)
1687 (eq (basic-combination-kind last) :full))
1688 (let* ((fun (basic-combination-fun last))
1689 (use (lvar-uses fun))
1690 (name (and (ref-p use)
1691 (leaf-has-source-name-p (ref-leaf use))
1692 (leaf-source-name (ref-leaf use)))))
1693 (unless (or (node-tail-p last)
1694 (info :function :info name)
1695 (policy last (zerop safety)))
1696 (vop nil-fun-returned-error last 2block
1698 (emit-constant name)
1699 (multiple-value-bind (tn named)
1700 (fun-lvar-tn last 2block fun)
1703 ((not (eq (ir2-block-next 2block) (block-info target)))
1704 (vop branch last 2block (block-label target)))))))
1708 ;;; Convert the code in a block into VOPs.
1709 (defun ir2-convert-block (block)
1710 (declare (type cblock block))
1711 (let ((2block (block-info block)))
1712 (do-nodes (node lvar block)
1716 (let ((2lvar (lvar-info lvar)))
1717 ;; function REF in a local call is not annotated
1718 (when (and 2lvar (not (eq (ir2-lvar-kind 2lvar) :delayed)))
1719 (ir2-convert-ref node 2block)))))
1721 (let ((kind (basic-combination-kind node)))
1724 (ir2-convert-local-call node 2block))
1726 (ir2-convert-full-call node 2block))
1728 (let* ((info (basic-combination-fun-info node))
1729 (fun (fun-info-ir2-convert info)))
1731 (funcall fun node 2block))
1732 ((eq (basic-combination-info node) :full)
1733 (ir2-convert-full-call node 2block))
1735 (ir2-convert-template node 2block))))))))
1737 (when (lvar-info (if-test node))
1738 (ir2-convert-if node 2block)))
1740 (let ((fun (bind-lambda node)))
1741 (when (eq (lambda-home fun) fun)
1742 (ir2-convert-bind node 2block))))
1744 (ir2-convert-return node 2block))
1746 (ir2-convert-set node 2block))
1748 (ir2-convert-cast node 2block))
1751 ((eq (basic-combination-kind node) :local)
1752 (ir2-convert-mv-bind node 2block))
1753 ((eq (lvar-fun-name (basic-combination-fun node))
1755 (ir2-convert-throw node 2block))
1757 (ir2-convert-mv-call node 2block))))
1759 (when (exit-entry node)
1760 (ir2-convert-exit node 2block)))
1762 (ir2-convert-entry node 2block)))))
1764 (finish-ir2-block block)