0.8.5.19:
[sbcl/lichteblau.git] / src / compiler / ir2tran.lisp
blob1941881ca46e9e6888b016e9b3edea9aa4504f9f
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
5 ;;;; more information.
6 ;;;;
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.
13 (in-package "SB!C")
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))
20 (unless (eq x y)
21 (vop move node block x y))
22 (values))
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)
29 (if exact
30 (primitive-type-check check-ptype)
31 (let ((name (hairy-type-check-template-name type)))
32 (if name
33 (template-or-lose name)
34 nil)))))
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
40 ;;; test.
41 (defun emit-type-check (node block value result type)
42 (declare (type tn value result) (type node node) (type ir2-block block)
43 (type ctype type))
44 (emit-move-template node block (type-check-template type) value result)
45 (values))
47 ;;; Allocate an indirect value cell. Maybe do some clever stack
48 ;;; allocation someday.
49 ;;;
50 ;;; FIXME: DO-MAKE-VALUE-CELL is a bad name, since it doesn't make
51 ;;; clear what's the distinction between it and the MAKE-VALUE-CELL
52 ;;; VOP, and since the DO- further connotes iteration, which has
53 ;;; nothing to do with this. Clearer, more systematic names, anyone?
54 (defevent make-value-cell-event "Allocate heap value cell for lexical var.")
55 (defun do-make-value-cell (node block value res)
56 (event make-value-cell-event node)
57 (vop make-value-cell node block value res))
59 ;;;; leaf reference
61 ;;; Return the TN that holds the value of THING in the environment ENV.
62 (declaim (ftype (function ((or nlx-info lambda-var) physenv) tn)
63 find-in-physenv))
64 (defun find-in-physenv (thing physenv)
65 (or (cdr (assoc thing (ir2-physenv-closure (physenv-info physenv))))
66 (etypecase thing
67 (lambda-var
68 ;; I think that a failure of this assertion means that we're
69 ;; trying to access a variable which was improperly closed
70 ;; over. The PHYSENV describes a physical environment. Every
71 ;; variable that a form refers to should either be in its
72 ;; physical environment directly, or grabbed from a
73 ;; surrounding physical environment when it was closed over.
74 ;; The ASSOC expression above finds closed-over variables, so
75 ;; if we fell through the ASSOC expression, it wasn't closed
76 ;; over. Therefore, it must be in our physical environment
77 ;; directly. If instead it is in some other physical
78 ;; environment, then it's bogus for us to reference it here
79 ;; without it being closed over. -- WHN 2001-09-29
80 (aver (eq physenv (lambda-physenv (lambda-var-home thing))))
81 (leaf-info thing))
82 (nlx-info
83 (aver (eq physenv (block-physenv (nlx-info-target thing))))
84 (ir2-nlx-info-home (nlx-info-info thing))))
85 (bug "~@<~2I~_~S ~_not found in ~_~S~:>" thing physenv)))
87 ;;; If LEAF already has a constant TN, return that, otherwise make a
88 ;;; TN for it.
89 (defun constant-tn (leaf)
90 (declare (type constant leaf))
91 (or (leaf-info leaf)
92 (setf (leaf-info leaf)
93 (make-constant-tn leaf))))
95 ;;; Return a TN that represents the value of LEAF, or NIL if LEAF
96 ;;; isn't directly represented by a TN. ENV is the environment that
97 ;;; the reference is done in.
98 (defun leaf-tn (leaf env)
99 (declare (type leaf leaf) (type physenv env))
100 (typecase leaf
101 (lambda-var
102 (unless (lambda-var-indirect leaf)
103 (find-in-physenv leaf env)))
104 (constant (constant-tn leaf))
105 (t nil)))
107 ;;; This is used to conveniently get a handle on a constant TN during
108 ;;; IR2 conversion. It returns a constant TN representing the Lisp
109 ;;; object VALUE.
110 (defun emit-constant (value)
111 (constant-tn (find-constant value)))
113 ;;; Convert a REF node. The reference must not be delayed.
114 (defun ir2-convert-ref (node block)
115 (declare (type ref node) (type ir2-block block))
116 (let* ((lvar (node-lvar node))
117 (leaf (ref-leaf node))
118 (locs (lvar-result-tns
119 lvar (list (primitive-type (leaf-type leaf)))))
120 (res (first locs)))
121 (etypecase leaf
122 (lambda-var
123 (let ((tn (find-in-physenv leaf (node-physenv node))))
124 (if (lambda-var-indirect leaf)
125 (vop value-cell-ref node block tn res)
126 (emit-move node block tn res))))
127 (constant
128 (if (legal-immediate-constant-p leaf)
129 (emit-move node block (constant-tn leaf) res)
130 (let* ((name (leaf-source-name leaf))
131 (name-tn (emit-constant name)))
132 (if (policy node (zerop safety))
133 (vop fast-symbol-value node block name-tn res)
134 (vop symbol-value node block name-tn res)))))
135 (functional
136 (ir2-convert-closure node block leaf res))
137 (global-var
138 (let ((unsafe (policy node (zerop safety)))
139 (name (leaf-source-name leaf)))
140 (ecase (global-var-kind leaf)
141 ((:special :global)
142 (aver (symbolp name))
143 (let ((name-tn (emit-constant name)))
144 (if unsafe
145 (vop fast-symbol-value node block name-tn res)
146 (vop symbol-value node block name-tn res))))
147 (:global-function
148 (let ((fdefn-tn (make-load-time-constant-tn :fdefinition name)))
149 (if unsafe
150 (vop fdefn-fun node block fdefn-tn res)
151 (vop safe-fdefn-fun node block fdefn-tn res))))))))
152 (move-lvar-result node block locs lvar))
153 (values))
155 ;;; some sanity checks for a CLAMBDA passed to IR2-CONVERT-CLOSURE
156 (defun assertions-on-ir2-converted-clambda (clambda)
157 ;; This assertion was sort of an experiment. It would be nice and
158 ;; sane and easier to understand things if it were *always* true,
159 ;; but experimentally I observe that it's only *almost* always
160 ;; true. -- WHN 2001-01-02
161 #+nil
162 (aver (eql (lambda-component clambda)
163 (block-component (ir2-block-block ir2-block))))
164 ;; Check for some weirdness which came up in bug
165 ;; 138, 2002-01-02.
167 ;; The MAKE-LOAD-TIME-CONSTANT-TN call above puts an :ENTRY record
168 ;; into the IR2-COMPONENT-CONSTANTS table. The dump-a-COMPONENT
169 ;; code
170 ;; * treats every HANDLEless :ENTRY record into a
171 ;; patch, and
172 ;; * expects every patch to correspond to an
173 ;; IR2-COMPONENT-ENTRIES record.
174 ;; The IR2-COMPONENT-ENTRIES records are set by ENTRY-ANALYZE
175 ;; walking over COMPONENT-LAMBDAS. Bug 138b arose because there
176 ;; was a HANDLEless :ENTRY record which didn't correspond to an
177 ;; IR2-COMPONENT-ENTRIES record. That problem is hard to debug
178 ;; when it's caught at dump time, so this assertion tries to catch
179 ;; it here.
180 (aver (member clambda
181 (component-lambdas (lambda-component clambda))))
182 ;; another bug-138-related issue: COMPONENT-NEW-FUNCTIONALS is
183 ;; used as a queue for stuff pending to do in IR1, and now that
184 ;; we're doing IR2 it should've been completely flushed (but
185 ;; wasn't).
186 (aver (null (component-new-functionals (lambda-component clambda))))
187 (values))
189 ;;; Emit code to load a function object implementing FUNCTIONAL into
190 ;;; RES. This gets interesting when the referenced function is a
191 ;;; closure: we must make the closure and move the closed-over values
192 ;;; into it.
194 ;;; FUNCTIONAL is either a :TOPLEVEL-XEP functional or the XEP lambda
195 ;;; for the called function, since local call analysis converts all
196 ;;; closure references. If a :TOPLEVEL-XEP, we know it is not a
197 ;;; closure.
199 ;;; If a closed-over LAMBDA-VAR has no refs (is deleted), then we
200 ;;; don't initialize that slot. This can happen with closures over
201 ;;; top level variables, where optimization of the closure deleted the
202 ;;; variable. Since we committed to the closure format when we
203 ;;; pre-analyzed the top level code, we just leave an empty slot.
204 (defun ir2-convert-closure (ref ir2-block functional res)
205 (declare (type ref ref)
206 (type ir2-block ir2-block)
207 (type functional functional)
208 (type tn res))
209 (aver (not (eql (functional-kind functional) :deleted)))
210 (unless (leaf-info functional)
211 (setf (leaf-info functional)
212 (make-entry-info :name (functional-debug-name functional))))
213 (let ((entry (make-load-time-constant-tn :entry functional))
214 (closure (etypecase functional
215 (clambda
216 (assertions-on-ir2-converted-clambda functional)
217 (physenv-closure (get-lambda-physenv functional)))
218 (functional
219 (aver (eq (functional-kind functional) :toplevel-xep))
220 nil))))
222 (cond (closure
223 (let ((this-env (node-physenv ref)))
224 (vop make-closure ref ir2-block entry (length closure) res)
225 (loop for what in closure and n from 0 do
226 (unless (and (lambda-var-p what)
227 (null (leaf-refs what)))
228 (vop closure-init ref ir2-block
230 (find-in-physenv what this-env)
231 n)))))
233 (emit-move ref ir2-block entry res))))
234 (values))
236 ;;; Convert a SET node. If the NODE's LVAR is annotated, then we also
237 ;;; deliver the value to that lvar. If the var is a lexical variable
238 ;;; with no refs, then we don't actually set anything, since the
239 ;;; variable has been deleted.
240 (defun ir2-convert-set (node block)
241 (declare (type cset node) (type ir2-block block))
242 (let* ((lvar (node-lvar node))
243 (leaf (set-var node))
244 (val (lvar-tn node block (set-value node)))
245 (locs (if lvar
246 (lvar-result-tns
247 lvar (list (primitive-type (leaf-type leaf))))
248 nil)))
249 (etypecase leaf
250 (lambda-var
251 (when (leaf-refs leaf)
252 (let ((tn (find-in-physenv leaf (node-physenv node))))
253 (if (lambda-var-indirect leaf)
254 (vop value-cell-set node block tn val)
255 (emit-move node block val tn)))))
256 (global-var
257 (ecase (global-var-kind leaf)
258 ((:special :global)
259 (aver (symbolp (leaf-source-name leaf)))
260 (vop set node block (emit-constant (leaf-source-name leaf)) val)))))
261 (when locs
262 (emit-move node block val (first locs))
263 (move-lvar-result node block locs lvar)))
264 (values))
266 ;;;; utilities for receiving fixed values
268 ;;; Return a TN that can be referenced to get the value of LVAR. LVAR
269 ;;; must be LTN-ANNOTATED either as a delayed leaf ref or as a fixed,
270 ;;; single-value lvar.
272 ;;; The primitive-type of the result will always be the same as the
273 ;;; IR2-LVAR-PRIMITIVE-TYPE, ensuring that VOPs are always called with
274 ;;; TNs that satisfy the operand primitive-type restriction. We may
275 ;;; have to make a temporary of the desired type and move the actual
276 ;;; lvar TN into it. This happens when we delete a type check in
277 ;;; unsafe code or when we locally know something about the type of an
278 ;;; argument variable.
279 (defun lvar-tn (node block lvar)
280 (declare (type node node) (type ir2-block block) (type lvar lvar))
281 (let* ((2lvar (lvar-info lvar))
282 (lvar-tn
283 (ecase (ir2-lvar-kind 2lvar)
284 (:delayed
285 (let ((ref (lvar-uses lvar)))
286 (leaf-tn (ref-leaf ref) (node-physenv ref))))
287 (:fixed
288 (aver (= (length (ir2-lvar-locs 2lvar)) 1))
289 (first (ir2-lvar-locs 2lvar)))))
290 (ptype (ir2-lvar-primitive-type 2lvar)))
292 (cond ((eq (tn-primitive-type lvar-tn) ptype) lvar-tn)
294 (let ((temp (make-normal-tn ptype)))
295 (emit-move node block lvar-tn temp)
296 temp)))))
298 ;;; This is similar to LVAR-TN, but hacks multiple values. We return
299 ;;; TNs holding the values of LVAR with PTYPES as their primitive
300 ;;; types. LVAR must be annotated for the same number of fixed values
301 ;;; are there are PTYPES.
303 ;;; If the lvar has a type check, check the values into temps and
304 ;;; return the temps. When we have more values than assertions, we
305 ;;; move the extra values with no check.
306 (defun lvar-tns (node block lvar ptypes)
307 (declare (type node node) (type ir2-block block)
308 (type lvar lvar) (list ptypes))
309 (let* ((locs (ir2-lvar-locs (lvar-info lvar)))
310 (nlocs (length locs)))
311 (aver (= nlocs (length ptypes)))
313 (mapcar (lambda (from to-type)
314 (if (eq (tn-primitive-type from) to-type)
315 from
316 (let ((temp (make-normal-tn to-type)))
317 (emit-move node block from temp)
318 temp)))
319 locs
320 ptypes)))
322 ;;;; utilities for delivering values to lvars
324 ;;; Return a list of TNs with the specifier TYPES that can be used as
325 ;;; result TNs to evaluate an expression into LVAR. This is used
326 ;;; together with MOVE-LVAR-RESULT to deliver fixed values to
327 ;;; an lvar.
329 ;;; If the lvar isn't annotated (meaning the values are discarded) or
330 ;;; is unknown-values, the then we make temporaries for each supplied
331 ;;; value, providing a place to compute the result in until we decide
332 ;;; what to do with it (if anything.)
334 ;;; If the lvar is fixed-values, and wants the same number of values
335 ;;; as the user wants to deliver, then we just return the
336 ;;; IR2-LVAR-LOCS. Otherwise we make a new list padded as necessary by
337 ;;; discarded TNs. We always return a TN of the specified type, using
338 ;;; the lvar locs only when they are of the correct type.
339 (defun lvar-result-tns (lvar types)
340 (declare (type (or lvar null) lvar) (type list types))
341 (if (not lvar)
342 (mapcar #'make-normal-tn types)
343 (let ((2lvar (lvar-info lvar)))
344 (ecase (ir2-lvar-kind 2lvar)
345 (:fixed
346 (let* ((locs (ir2-lvar-locs 2lvar))
347 (nlocs (length locs))
348 (ntypes (length types)))
349 (if (and (= nlocs ntypes)
350 (do ((loc locs (cdr loc))
351 (type types (cdr type)))
352 ((null loc) t)
353 (unless (eq (tn-primitive-type (car loc)) (car type))
354 (return nil))))
355 locs
356 (mapcar (lambda (loc type)
357 (if (eq (tn-primitive-type loc) type)
359 (make-normal-tn type)))
360 (if (< nlocs ntypes)
361 (append locs
362 (mapcar #'make-normal-tn
363 (subseq types nlocs)))
364 locs)
365 types))))
366 (:unknown
367 (mapcar #'make-normal-tn types))))))
369 ;;; Make the first N standard value TNs, returning them in a list.
370 (defun make-standard-value-tns (n)
371 (declare (type unsigned-byte n))
372 (collect ((res))
373 (dotimes (i n)
374 (res (standard-arg-location i)))
375 (res)))
377 ;;; Return a list of TNs wired to the standard value passing
378 ;;; conventions that can be used to receive values according to the
379 ;;; unknown-values convention. This is used with together
380 ;;; MOVE-LVAR-RESULT for delivering unknown values to a fixed values
381 ;;; lvar.
383 ;;; If the lvar isn't annotated, then we treat as 0-values, returning
384 ;;; an empty list of temporaries.
386 ;;; If the lvar is annotated, then it must be :FIXED.
387 (defun standard-result-tns (lvar)
388 (declare (type (or lvar null) lvar))
389 (if lvar
390 (let ((2lvar (lvar-info lvar)))
391 (ecase (ir2-lvar-kind 2lvar)
392 (:fixed
393 (make-standard-value-tns (length (ir2-lvar-locs 2lvar))))))
394 nil))
396 ;;; Just move each SRC TN into the corresponding DEST TN, defaulting
397 ;;; any unsupplied source values to NIL. We let EMIT-MOVE worry about
398 ;;; doing the appropriate coercions.
399 (defun move-results-coerced (node block src dest)
400 (declare (type node node) (type ir2-block block) (list src dest))
401 (let ((nsrc (length src))
402 (ndest (length dest)))
403 (mapc (lambda (from to)
404 (unless (eq from to)
405 (emit-move node block from to)))
406 (if (> ndest nsrc)
407 (append src (make-list (- ndest nsrc)
408 :initial-element (emit-constant nil)))
409 src)
410 dest))
411 (values))
413 ;;; Move each SRC TN into the corresponding DEST TN, checking types
414 ;;; and defaulting any unsupplied source values to NIL
415 (defun move-results-checked (node block src dest types)
416 (declare (type node node) (type ir2-block block) (list src dest types))
417 (let ((nsrc (length src))
418 (ndest (length dest))
419 (ntypes (length types)))
420 (mapc (lambda (from to type)
421 (if type
422 (emit-type-check node block from to type)
423 (emit-move node block from to)))
424 (if (> ndest nsrc)
425 (append src (make-list (- ndest nsrc)
426 :initial-element (emit-constant nil)))
427 src)
428 dest
429 (if (> ndest ntypes)
430 (append types (make-list (- ndest ntypes)))
431 types)))
432 (values))
434 ;;; If necessary, emit coercion code needed to deliver the RESULTS to
435 ;;; the specified lvar. NODE and BLOCK provide context for emitting
436 ;;; code. Although usually obtained from STANDARD-RESULT-TNs or
437 ;;; LVAR-RESULT-TNs, RESULTS my be a list of any type or
438 ;;; number of TNs.
440 ;;; If the lvar is fixed values, then move the results into the lvar
441 ;;; locations. If the lvar is unknown values, then do the moves into
442 ;;; the standard value locations, and use PUSH-VALUES to put the
443 ;;; values on the stack.
444 (defun move-lvar-result (node block results lvar)
445 (declare (type node node) (type ir2-block block)
446 (list results) (type (or lvar null) lvar))
447 (when lvar
448 (let ((2lvar (lvar-info lvar)))
449 (ecase (ir2-lvar-kind 2lvar)
450 (:fixed
451 (let ((locs (ir2-lvar-locs 2lvar)))
452 (unless (eq locs results)
453 (move-results-coerced node block results locs))))
454 (:unknown
455 (let* ((nvals (length results))
456 (locs (make-standard-value-tns nvals)))
457 (move-results-coerced node block results locs)
458 (vop* push-values node block
459 ((reference-tn-list locs nil))
460 ((reference-tn-list (ir2-lvar-locs 2lvar) t))
461 nvals))))))
462 (values))
464 ;;; CAST
465 (defun ir2-convert-cast (node block)
466 (declare (type cast node)
467 (type ir2-block block))
468 (binding* ((lvar (node-lvar node) :exit-if-null)
469 (2lvar (lvar-info lvar))
470 (value (cast-value node))
471 (2value (lvar-info value)))
472 (cond ((eq (ir2-lvar-kind 2lvar) :unused))
473 ((eq (ir2-lvar-kind 2lvar) :unknown)
474 (aver (eq (ir2-lvar-kind 2value) :unknown))
475 (aver (not (cast-type-check node)))
476 (move-results-coerced node block
477 (ir2-lvar-locs 2value)
478 (ir2-lvar-locs 2lvar)))
479 ((eq (ir2-lvar-kind 2lvar) :fixed)
480 (aver (eq (ir2-lvar-kind 2value) :fixed))
481 (if (cast-type-check node)
482 (move-results-checked node block
483 (ir2-lvar-locs 2value)
484 (ir2-lvar-locs 2lvar)
485 (multiple-value-bind (check types)
486 (cast-check-types node nil)
487 (aver (eq check :simple))
488 types))
489 (move-results-coerced node block
490 (ir2-lvar-locs 2value)
491 (ir2-lvar-locs 2lvar))))
492 (t (bug "CAST cannot be :DELAYED.")))))
494 ;;;; template conversion
496 ;;; Build a TN-REFS list that represents access to the values of the
497 ;;; specified list of lvars ARGS for TEMPLATE. Any :CONSTANT arguments
498 ;;; are returned in the second value as a list rather than being
499 ;;; accessed as a normal argument. NODE and BLOCK provide the context
500 ;;; for emitting any necessary type-checking code.
501 (defun reference-args (node block args template)
502 (declare (type node node) (type ir2-block block) (list args)
503 (type template template))
504 (collect ((info-args))
505 (let ((last nil)
506 (first nil))
507 (do ((args args (cdr args))
508 (types (template-arg-types template) (cdr types)))
509 ((null args))
510 (let ((type (first types))
511 (arg (first args)))
512 (if (and (consp type) (eq (car type) ':constant))
513 (info-args (lvar-value arg))
514 (let ((ref (reference-tn (lvar-tn node block arg) nil)))
515 (if last
516 (setf (tn-ref-across last) ref)
517 (setf first ref))
518 (setq last ref)))))
520 (values (the (or tn-ref null) first) (info-args)))))
522 ;;; Convert a conditional template. We try to exploit any
523 ;;; drop-through, but emit an unconditional branch afterward if we
524 ;;; fail. NOT-P is true if the sense of the TEMPLATE's test should be
525 ;;; negated.
526 (defun ir2-convert-conditional (node block template args info-args if not-p)
527 (declare (type node node) (type ir2-block block)
528 (type template template) (type (or tn-ref null) args)
529 (list info-args) (type cif if) (type boolean not-p))
530 (aver (= (template-info-arg-count template) (+ (length info-args) 2)))
531 (let ((consequent (if-consequent if))
532 (alternative (if-alternative if)))
533 (cond ((drop-thru-p if consequent)
534 (emit-template node block template args nil
535 (list* (block-label alternative) (not not-p)
536 info-args)))
538 (emit-template node block template args nil
539 (list* (block-label consequent) not-p info-args))
540 (unless (drop-thru-p if alternative)
541 (vop branch node block (block-label alternative)))))))
543 ;;; Convert an IF that isn't the DEST of a conditional template.
544 (defun ir2-convert-if (node block)
545 (declare (type ir2-block block) (type cif node))
546 (let* ((test (if-test node))
547 (test-ref (reference-tn (lvar-tn node block test) nil))
548 (nil-ref (reference-tn (emit-constant nil) nil)))
549 (setf (tn-ref-across test-ref) nil-ref)
550 (ir2-convert-conditional node block (template-or-lose 'if-eq)
551 test-ref () node t)))
553 ;;; Return a list of primitive-types that we can pass to
554 ;;; LVAR-RESULT-TNS describing the result types we want for a
555 ;;; template call. We duplicate here the determination of output type
556 ;;; that was done in initially selecting the template, so we know that
557 ;;; the types we find are allowed by the template output type
558 ;;; restrictions.
559 (defun find-template-result-types (call template rtypes)
560 (declare (type combination call)
561 (type template template) (list rtypes))
562 (let* ((dtype (node-derived-type call))
563 (type dtype)
564 (types (mapcar #'primitive-type
565 (if (values-type-p type)
566 (append (values-type-required type)
567 (values-type-optional type))
568 (list type)))))
569 (let ((nvals (length rtypes))
570 (ntypes (length types)))
571 (cond ((< ntypes nvals)
572 (append types
573 (make-list (- nvals ntypes)
574 :initial-element *backend-t-primitive-type*)))
575 ((> ntypes nvals)
576 (subseq types 0 nvals))
578 types)))))
580 ;;; Return a list of TNs usable in a CALL to TEMPLATE delivering
581 ;;; values to LVAR. As an efficiency hack, we pick off the common case
582 ;;; where the LVAR is fixed values and has locations that satisfy the
583 ;;; result restrictions. This can fail when there is a type check or a
584 ;;; values count mismatch.
585 (defun make-template-result-tns (call lvar template rtypes)
586 (declare (type combination call) (type (or lvar null) lvar)
587 (type template template) (list rtypes))
588 (let ((2lvar (when lvar (lvar-info lvar))))
589 (if (and 2lvar (eq (ir2-lvar-kind 2lvar) :fixed))
590 (let ((locs (ir2-lvar-locs 2lvar)))
591 (if (and (= (length rtypes) (length locs))
592 (do ((loc locs (cdr loc))
593 (rtype rtypes (cdr rtype)))
594 ((null loc) t)
595 (unless (operand-restriction-ok
596 (car rtype)
597 (tn-primitive-type (car loc))
598 :t-ok nil)
599 (return nil))))
600 locs
601 (lvar-result-tns
602 lvar
603 (find-template-result-types call template rtypes))))
604 (lvar-result-tns
605 lvar
606 (find-template-result-types call template rtypes)))))
608 ;;; Get the operands into TNs, make TN-REFs for them, and then call
609 ;;; the template emit function.
610 (defun ir2-convert-template (call block)
611 (declare (type combination call) (type ir2-block block))
612 (let* ((template (combination-info call))
613 (lvar (node-lvar call))
614 (rtypes (template-result-types template)))
615 (multiple-value-bind (args info-args)
616 (reference-args call block (combination-args call) template)
617 (aver (not (template-more-results-type template)))
618 (if (eq rtypes :conditional)
619 (ir2-convert-conditional call block template args info-args
620 (lvar-dest lvar) nil)
621 (let* ((results (make-template-result-tns call lvar template rtypes))
622 (r-refs (reference-tn-list results t)))
623 (aver (= (length info-args)
624 (template-info-arg-count template)))
625 (if info-args
626 (emit-template call block template args r-refs info-args)
627 (emit-template call block template args r-refs))
628 (move-lvar-result call block results lvar)))))
629 (values))
631 ;;; We don't have to do much because operand count checking is done by
632 ;;; IR1 conversion. The only difference between this and the function
633 ;;; case of IR2-CONVERT-TEMPLATE is that there can be codegen-info
634 ;;; arguments.
635 (defoptimizer (%%primitive ir2-convert) ((template info &rest args) call block)
636 (let* ((template (lvar-value template))
637 (info (lvar-value info))
638 (lvar (node-lvar call))
639 (rtypes (template-result-types template))
640 (results (make-template-result-tns call lvar template rtypes))
641 (r-refs (reference-tn-list results t)))
642 (multiple-value-bind (args info-args)
643 (reference-args call block (cddr (combination-args call)) template)
644 (aver (not (template-more-results-type template)))
645 (aver (not (eq rtypes :conditional)))
646 (aver (null info-args))
648 (if info
649 (emit-template call block template args r-refs info)
650 (emit-template call block template args r-refs))
652 (move-lvar-result call block results lvar)))
653 (values))
655 ;;;; local call
657 ;;; Convert a LET by moving the argument values into the variables.
658 ;;; Since a LET doesn't have any passing locations, we move the
659 ;;; arguments directly into the variables. We must also allocate any
660 ;;; indirect value cells, since there is no function prologue to do
661 ;;; this.
662 (defun ir2-convert-let (node block fun)
663 (declare (type combination node) (type ir2-block block) (type clambda fun))
664 (mapc (lambda (var arg)
665 (when arg
666 (let ((src (lvar-tn node block arg))
667 (dest (leaf-info var)))
668 (if (lambda-var-indirect var)
669 (do-make-value-cell node block src dest)
670 (emit-move node block src dest)))))
671 (lambda-vars fun) (basic-combination-args node))
672 (values))
674 ;;; Emit any necessary moves into assignment temps for a local call to
675 ;;; FUN. We return two lists of TNs: TNs holding the actual argument
676 ;;; values, and (possibly EQ) TNs that are the actual destination of
677 ;;; the arguments. When necessary, we allocate temporaries for
678 ;;; arguments to preserve parallel assignment semantics. These lists
679 ;;; exclude unused arguments and include implicit environment
680 ;;; arguments, i.e. they exactly correspond to the arguments passed.
682 ;;; OLD-FP is the TN currently holding the value we want to pass as
683 ;;; OLD-FP. If null, then the call is to the same environment (an
684 ;;; :ASSIGNMENT), so we only move the arguments, and leave the
685 ;;; environment alone.
686 (defun emit-psetq-moves (node block fun old-fp)
687 (declare (type combination node) (type ir2-block block) (type clambda fun)
688 (type (or tn null) old-fp))
689 (let ((actuals (mapcar (lambda (x)
690 (when x
691 (lvar-tn node block x)))
692 (combination-args node))))
693 (collect ((temps)
694 (locs))
695 (dolist (var (lambda-vars fun))
696 (let ((actual (pop actuals))
697 (loc (leaf-info var)))
698 (when actual
699 (cond
700 ((lambda-var-indirect var)
701 (let ((temp
702 (make-normal-tn *backend-t-primitive-type*)))
703 (do-make-value-cell node block actual temp)
704 (temps temp)))
705 ((member actual (locs))
706 (let ((temp (make-normal-tn (tn-primitive-type loc))))
707 (emit-move node block actual temp)
708 (temps temp)))
710 (temps actual)))
711 (locs loc))))
713 (when old-fp
714 (let ((this-1env (node-physenv node))
715 (called-env (physenv-info (lambda-physenv fun))))
716 (dolist (thing (ir2-physenv-closure called-env))
717 (temps (find-in-physenv (car thing) this-1env))
718 (locs (cdr thing)))
719 (temps old-fp)
720 (locs (ir2-physenv-old-fp called-env))))
722 (values (temps) (locs)))))
724 ;;; A tail-recursive local call is done by emitting moves of stuff
725 ;;; into the appropriate passing locations. After setting up the args
726 ;;; and environment, we just move our return-pc into the called
727 ;;; function's passing location.
728 (defun ir2-convert-tail-local-call (node block fun)
729 (declare (type combination node) (type ir2-block block) (type clambda fun))
730 (let ((this-env (physenv-info (node-physenv node))))
731 (multiple-value-bind (temps locs)
732 (emit-psetq-moves node block fun (ir2-physenv-old-fp this-env))
734 (mapc (lambda (temp loc)
735 (emit-move node block temp loc))
736 temps locs))
738 (emit-move node block
739 (ir2-physenv-return-pc this-env)
740 (ir2-physenv-return-pc-pass
741 (physenv-info
742 (lambda-physenv fun)))))
744 (values))
746 ;;; Convert an :ASSIGNMENT call. This is just like a tail local call,
747 ;;; except that the caller and callee environment are the same, so we
748 ;;; don't need to mess with the environment locations, return PC, etc.
749 (defun ir2-convert-assignment (node block fun)
750 (declare (type combination node) (type ir2-block block) (type clambda fun))
751 (multiple-value-bind (temps locs) (emit-psetq-moves node block fun nil)
753 (mapc (lambda (temp loc)
754 (emit-move node block temp loc))
755 temps locs))
756 (values))
758 ;;; Do stuff to set up the arguments to a non-tail local call
759 ;;; (including implicit environment args.) We allocate a frame
760 ;;; (returning the FP and NFP), and also compute the TN-REFS list for
761 ;;; the values to pass and the list of passing location TNs.
762 (defun ir2-convert-local-call-args (node block fun)
763 (declare (type combination node) (type ir2-block block) (type clambda fun))
764 (let ((fp (make-stack-pointer-tn))
765 (nfp (make-number-stack-pointer-tn))
766 (old-fp (make-stack-pointer-tn)))
767 (multiple-value-bind (temps locs)
768 (emit-psetq-moves node block fun old-fp)
769 (vop current-fp node block old-fp)
770 (vop allocate-frame node block
771 (physenv-info (lambda-physenv fun))
772 fp nfp)
773 (values fp nfp temps (mapcar #'make-alias-tn locs)))))
775 ;;; Handle a non-TR known-values local call. We emit the call, then
776 ;;; move the results to the lvar's destination.
777 (defun ir2-convert-local-known-call (node block fun returns lvar start)
778 (declare (type node node) (type ir2-block block) (type clambda fun)
779 (type return-info returns) (type (or lvar null) lvar)
780 (type label start))
781 (multiple-value-bind (fp nfp temps arg-locs)
782 (ir2-convert-local-call-args node block fun)
783 (let ((locs (return-info-locations returns)))
784 (vop* known-call-local node block
785 (fp nfp (reference-tn-list temps nil))
786 ((reference-tn-list locs t))
787 arg-locs (physenv-info (lambda-physenv fun)) start)
788 (move-lvar-result node block locs lvar)))
789 (values))
791 ;;; Handle a non-TR unknown-values local call. We do different things
792 ;;; depending on what kind of values the lvar wants.
794 ;;; If LVAR is :UNKNOWN, then we use the "multiple-" variant, directly
795 ;;; specifying the lvar's LOCS as the VOP results so that we don't
796 ;;; have to do anything after the call.
798 ;;; Otherwise, we use STANDARD-RESULT-TNS to get wired result TNs, and
799 ;;; then call MOVE-LVAR-RESULT to do any necessary type checks or
800 ;;; coercions.
801 (defun ir2-convert-local-unknown-call (node block fun lvar start)
802 (declare (type node node) (type ir2-block block) (type clambda fun)
803 (type (or lvar null) lvar) (type label start))
804 (multiple-value-bind (fp nfp temps arg-locs)
805 (ir2-convert-local-call-args node block fun)
806 (let ((2lvar (and lvar (lvar-info lvar)))
807 (env (physenv-info (lambda-physenv fun)))
808 (temp-refs (reference-tn-list temps nil)))
809 (if (and 2lvar (eq (ir2-lvar-kind 2lvar) :unknown))
810 (vop* multiple-call-local node block (fp nfp temp-refs)
811 ((reference-tn-list (ir2-lvar-locs 2lvar) t))
812 arg-locs env start)
813 (let ((locs (standard-result-tns lvar)))
814 (vop* call-local node block
815 (fp nfp temp-refs)
816 ((reference-tn-list locs t))
817 arg-locs env start (length locs))
818 (move-lvar-result node block locs lvar)))))
819 (values))
821 ;;; Dispatch to the appropriate function, depending on whether we have
822 ;;; a let, tail or normal call. If the function doesn't return, call
823 ;;; it using the unknown-value convention. We could compile it as a
824 ;;; tail call, but that might seem confusing in the debugger.
825 (defun ir2-convert-local-call (node block)
826 (declare (type combination node) (type ir2-block block))
827 (let* ((fun (ref-leaf (lvar-uses (basic-combination-fun node))))
828 (kind (functional-kind fun)))
829 (cond ((eq kind :let)
830 (ir2-convert-let node block fun))
831 ((eq kind :assignment)
832 (ir2-convert-assignment node block fun))
833 ((node-tail-p node)
834 (ir2-convert-tail-local-call node block fun))
836 (let ((start (block-label (lambda-block fun)))
837 (returns (tail-set-info (lambda-tail-set fun)))
838 (lvar (node-lvar node)))
839 (ecase (if returns
840 (return-info-kind returns)
841 :unknown)
842 (:unknown
843 (ir2-convert-local-unknown-call node block fun lvar start))
844 (:fixed
845 (ir2-convert-local-known-call node block fun returns
846 lvar start)))))))
847 (values))
849 ;;;; full call
851 ;;; Given a function lvar FUN, return (VALUES TN-TO-CALL NAMED-P),
852 ;;; where TN-TO-CALL is a TN holding the thing that we call NAMED-P is
853 ;;; true if the thing is named (false if it is a function).
855 ;;; There are two interesting non-named cases:
856 ;;; -- We know it's a function. No check needed: return the
857 ;;; lvar LOC.
858 ;;; -- We don't know what it is.
859 (defun fun-lvar-tn (node block lvar)
860 (declare (type lvar lvar))
861 (let ((2lvar (lvar-info lvar)))
862 (if (eq (ir2-lvar-kind 2lvar) :delayed)
863 (let ((name (lvar-fun-name lvar t)))
864 (aver name)
865 (values (make-load-time-constant-tn :fdefinition name) t))
866 (let* ((locs (ir2-lvar-locs 2lvar))
867 (loc (first locs))
868 (function-ptype (primitive-type-or-lose 'function)))
869 (aver (and (eq (ir2-lvar-kind 2lvar) :fixed)
870 (= (length locs) 1)))
871 (aver (eq (tn-primitive-type loc) function-ptype))
872 (values loc nil)))))
874 ;;; Set up the args to NODE in the current frame, and return a TN-REF
875 ;;; list for the passing locations.
876 (defun move-tail-full-call-args (node block)
877 (declare (type combination node) (type ir2-block block))
878 (let ((args (basic-combination-args node))
879 (last nil)
880 (first nil))
881 (dotimes (num (length args))
882 (let ((loc (standard-arg-location num)))
883 (emit-move node block (lvar-tn node block (elt args num)) loc)
884 (let ((ref (reference-tn loc nil)))
885 (if last
886 (setf (tn-ref-across last) ref)
887 (setf first ref))
888 (setq last ref))))
889 first))
891 ;;; Move the arguments into the passing locations and do a (possibly
892 ;;; named) tail call.
893 (defun ir2-convert-tail-full-call (node block)
894 (declare (type combination node) (type ir2-block block))
895 (let* ((env (physenv-info (node-physenv node)))
896 (args (basic-combination-args node))
897 (nargs (length args))
898 (pass-refs (move-tail-full-call-args node block))
899 (old-fp (ir2-physenv-old-fp env))
900 (return-pc (ir2-physenv-return-pc env)))
902 (multiple-value-bind (fun-tn named)
903 (fun-lvar-tn node block (basic-combination-fun node))
904 (if named
905 (vop* tail-call-named node block
906 (fun-tn old-fp return-pc pass-refs)
907 (nil)
908 nargs)
909 (vop* tail-call node block
910 (fun-tn old-fp return-pc pass-refs)
911 (nil)
912 nargs))))
914 (values))
916 ;;; like IR2-CONVERT-LOCAL-CALL-ARGS, only different
917 (defun ir2-convert-full-call-args (node block)
918 (declare (type combination node) (type ir2-block block))
919 (let* ((args (basic-combination-args node))
920 (fp (make-stack-pointer-tn))
921 (nargs (length args)))
922 (vop allocate-full-call-frame node block nargs fp)
923 (collect ((locs))
924 (let ((last nil)
925 (first nil))
926 (dotimes (num nargs)
927 (locs (standard-arg-location num))
928 (let ((ref (reference-tn (lvar-tn node block (elt args num))
929 nil)))
930 (if last
931 (setf (tn-ref-across last) ref)
932 (setf first ref))
933 (setq last ref)))
935 (values fp first (locs) nargs)))))
937 ;;; Do full call when a fixed number of values are desired. We make
938 ;;; STANDARD-RESULT-TNS for our lvar, then deliver the result using
939 ;;; MOVE-LVAR-RESULT. We do named or normal call, as appropriate.
940 (defun ir2-convert-fixed-full-call (node block)
941 (declare (type combination node) (type ir2-block block))
942 (multiple-value-bind (fp args arg-locs nargs)
943 (ir2-convert-full-call-args node block)
944 (let* ((lvar (node-lvar node))
945 (locs (standard-result-tns lvar))
946 (loc-refs (reference-tn-list locs t))
947 (nvals (length locs)))
948 (multiple-value-bind (fun-tn named)
949 (fun-lvar-tn node block (basic-combination-fun node))
950 (if named
951 (vop* call-named node block (fp fun-tn args) (loc-refs)
952 arg-locs nargs nvals)
953 (vop* call node block (fp fun-tn args) (loc-refs)
954 arg-locs nargs nvals))
955 (move-lvar-result node block locs lvar))))
956 (values))
958 ;;; Do full call when unknown values are desired.
959 (defun ir2-convert-multiple-full-call (node block)
960 (declare (type combination node) (type ir2-block block))
961 (multiple-value-bind (fp args arg-locs nargs)
962 (ir2-convert-full-call-args node block)
963 (let* ((lvar (node-lvar node))
964 (locs (ir2-lvar-locs (lvar-info lvar)))
965 (loc-refs (reference-tn-list locs t)))
966 (multiple-value-bind (fun-tn named)
967 (fun-lvar-tn node block (basic-combination-fun node))
968 (if named
969 (vop* multiple-call-named node block (fp fun-tn args) (loc-refs)
970 arg-locs nargs)
971 (vop* multiple-call node block (fp fun-tn args) (loc-refs)
972 arg-locs nargs)))))
973 (values))
975 ;;; stuff to check in PONDER-FULL-CALL
977 ;;; There are some things which are intended always to be optimized
978 ;;; away by DEFTRANSFORMs and such, and so never compiled into full
979 ;;; calls. This has been a source of bugs so many times that it seems
980 ;;; worth listing some of them here so that we can check the list
981 ;;; whenever we compile a full call.
983 ;;; FIXME: It might be better to represent this property by setting a
984 ;;; flag in DEFKNOWN, instead of representing it by membership in this
985 ;;; list.
986 (defvar *always-optimized-away*
987 '(;; This should always be DEFTRANSFORMed away, but wasn't in a bug
988 ;; reported to cmucl-imp 2000-06-20.
989 %instance-ref
990 ;; These should always turn into VOPs, but wasn't in a bug which
991 ;; appeared when LTN-POLICY stuff was being tweaked in
992 ;; sbcl-0.6.9.16. in sbcl-0.6.0
993 data-vector-set
994 data-vector-ref))
996 ;;; more stuff to check in PONDER-FULL-CALL
998 ;;; These came in handy when troubleshooting cold boot after making
999 ;;; major changes in the package structure: various transforms and
1000 ;;; VOPs and stuff got attached to the wrong symbol, so that
1001 ;;; references to the right symbol were bogusly translated as full
1002 ;;; calls instead of primitives, sending the system off into infinite
1003 ;;; space. Having a report on all full calls generated makes it easier
1004 ;;; to figure out what form caused the problem this time.
1005 #!+sb-show (defvar *show-full-called-fnames-p* nil)
1006 #!+sb-show (defvar *full-called-fnames* (make-hash-table :test 'equal))
1008 ;;; Do some checks (and store some notes relevant for future checks)
1009 ;;; on a full call:
1010 ;;; * Is this a full call to something we have reason to know should
1011 ;;; never be full called? (Except as of sbcl-0.7.18 or so, we no
1012 ;;; longer try to ensure this behavior when *FAILURE-P* has already
1013 ;;; been detected.)
1014 ;;; * Is this a full call to (SETF FOO) which might conflict with
1015 ;;; a DEFSETF or some such thing elsewhere in the program?
1016 (defun ponder-full-call (node)
1017 (let* ((lvar (basic-combination-fun node))
1018 (fname (lvar-fun-name lvar t)))
1019 (declare (type (or symbol cons) fname))
1021 #!+sb-show (unless (gethash fname *full-called-fnames*)
1022 (setf (gethash fname *full-called-fnames*) t))
1023 #!+sb-show (when *show-full-called-fnames-p*
1024 (/show "converting full call to named function" fname)
1025 (/show (basic-combination-args node))
1026 (/show (policy node speed) (policy node safety))
1027 (/show (policy node compilation-speed))
1028 (let ((arg-types (mapcar (lambda (lvar)
1029 (when lvar
1030 (type-specifier
1031 (lvar-type lvar))))
1032 (basic-combination-args node))))
1033 (/show arg-types)))
1035 ;; When illegal code is compiled, all sorts of perverse paths
1036 ;; through the compiler can be taken, and it's much harder -- and
1037 ;; probably pointless -- to guarantee that always-optimized-away
1038 ;; functions are actually optimized away. Thus, we skip the check
1039 ;; in that case.
1040 (unless *failure-p*
1041 (when (memq fname *always-optimized-away*)
1042 (/show (policy node speed) (policy node safety))
1043 (/show (policy node compilation-speed))
1044 (bug "full call to ~S" fname)))
1046 (when (consp fname)
1047 (aver (legal-fun-name-p fname))
1048 (destructuring-bind (setfoid &rest stem) fname
1049 (when (eq setfoid 'setf)
1050 (setf (gethash (car stem) *setf-assumed-fboundp*) t))))))
1052 ;;; If the call is in a tail recursive position and the return
1053 ;;; convention is standard, then do a tail full call. If one or fewer
1054 ;;; values are desired, then use a single-value call, otherwise use a
1055 ;;; multiple-values call.
1056 (defun ir2-convert-full-call (node block)
1057 (declare (type combination node) (type ir2-block block))
1058 (ponder-full-call node)
1059 (cond ((node-tail-p node)
1060 (ir2-convert-tail-full-call node block))
1061 ((let ((lvar (node-lvar node)))
1062 (and lvar
1063 (eq (ir2-lvar-kind (lvar-info lvar)) :unknown)))
1064 (ir2-convert-multiple-full-call node block))
1066 (ir2-convert-fixed-full-call node block)))
1067 (values))
1069 ;;;; entering functions
1071 ;;; Do all the stuff that needs to be done on XEP entry:
1072 ;;; -- Create frame.
1073 ;;; -- Copy any more arg.
1074 ;;; -- Set up the environment, accessing any closure variables.
1075 ;;; -- Move args from the standard passing locations to their internal
1076 ;;; locations.
1077 (defun init-xep-environment (node block fun)
1078 (declare (type bind node) (type ir2-block block) (type clambda fun))
1079 (let ((start-label (entry-info-offset (leaf-info fun)))
1080 (env (physenv-info (node-physenv node))))
1081 (let ((ef (functional-entry-fun fun)))
1082 (cond ((and (optional-dispatch-p ef) (optional-dispatch-more-entry ef))
1083 ;; Special case the xep-allocate-frame + copy-more-arg case.
1084 (vop xep-allocate-frame node block start-label t)
1085 (vop copy-more-arg node block (optional-dispatch-max-args ef)))
1087 ;; No more args, so normal entry.
1088 (vop xep-allocate-frame node block start-label nil)))
1089 (if (ir2-physenv-closure env)
1090 (let ((closure (make-normal-tn *backend-t-primitive-type*)))
1091 (vop setup-closure-environment node block start-label closure)
1092 (when (getf (functional-plist ef) :fin-function)
1093 (vop funcallable-instance-lexenv node block closure closure))
1094 (let ((n -1))
1095 (dolist (loc (ir2-physenv-closure env))
1096 (vop closure-ref node block closure (incf n) (cdr loc)))))
1097 (vop setup-environment node block start-label)))
1099 (unless (eq (functional-kind fun) :toplevel)
1100 (let ((vars (lambda-vars fun))
1101 (n 0))
1102 (when (leaf-refs (first vars))
1103 (emit-move node block (make-arg-count-location)
1104 (leaf-info (first vars))))
1105 (dolist (arg (rest vars))
1106 (when (leaf-refs arg)
1107 (let ((pass (standard-arg-location n))
1108 (home (leaf-info arg)))
1109 (if (lambda-var-indirect arg)
1110 (do-make-value-cell node block pass home)
1111 (emit-move node block pass home))))
1112 (incf n))))
1114 (emit-move node block (make-old-fp-passing-location t)
1115 (ir2-physenv-old-fp env)))
1117 (values))
1119 ;;; Emit function prolog code. This is only called on bind nodes for
1120 ;;; functions that allocate environments. All semantics of let calls
1121 ;;; are handled by IR2-CONVERT-LET.
1123 ;;; If not an XEP, all we do is move the return PC from its passing
1124 ;;; location, since in a local call, the caller allocates the frame
1125 ;;; and sets up the arguments.
1126 (defun ir2-convert-bind (node block)
1127 (declare (type bind node) (type ir2-block block))
1128 (let* ((fun (bind-lambda node))
1129 (env (physenv-info (lambda-physenv fun))))
1130 (aver (member (functional-kind fun)
1131 '(nil :external :optional :toplevel :cleanup)))
1133 (when (xep-p fun)
1134 (init-xep-environment node block fun)
1135 #!+sb-dyncount
1136 (when *collect-dynamic-statistics*
1137 (vop count-me node block *dynamic-counts-tn*
1138 (block-number (ir2-block-block block)))))
1140 (emit-move node
1141 block
1142 (ir2-physenv-return-pc-pass env)
1143 (ir2-physenv-return-pc env))
1145 (let ((lab (gen-label)))
1146 (setf (ir2-physenv-environment-start env) lab)
1147 (vop note-environment-start node block lab)))
1149 (values))
1151 ;;;; function return
1153 ;;; Do stuff to return from a function with the specified values and
1154 ;;; convention. If the return convention is :FIXED and we aren't
1155 ;;; returning from an XEP, then we do a known return (letting
1156 ;;; representation selection insert the correct move-arg VOPs.)
1157 ;;; Otherwise, we use the unknown-values convention. If there is a
1158 ;;; fixed number of return values, then use RETURN, otherwise use
1159 ;;; RETURN-MULTIPLE.
1160 (defun ir2-convert-return (node block)
1161 (declare (type creturn node) (type ir2-block block))
1162 (let* ((lvar (return-result node))
1163 (2lvar (lvar-info lvar))
1164 (lvar-kind (ir2-lvar-kind 2lvar))
1165 (fun (return-lambda node))
1166 (env (physenv-info (lambda-physenv fun)))
1167 (old-fp (ir2-physenv-old-fp env))
1168 (return-pc (ir2-physenv-return-pc env))
1169 (returns (tail-set-info (lambda-tail-set fun))))
1170 (cond
1171 ((and (eq (return-info-kind returns) :fixed)
1172 (not (xep-p fun)))
1173 (let ((locs (lvar-tns node block lvar
1174 (return-info-types returns))))
1175 (vop* known-return node block
1176 (old-fp return-pc (reference-tn-list locs nil))
1177 (nil)
1178 (return-info-locations returns))))
1179 ((eq lvar-kind :fixed)
1180 (let* ((types (mapcar #'tn-primitive-type (ir2-lvar-locs 2lvar)))
1181 (lvar-locs (lvar-tns node block lvar types))
1182 (nvals (length lvar-locs))
1183 (locs (make-standard-value-tns nvals)))
1184 (mapc (lambda (val loc)
1185 (emit-move node block val loc))
1186 lvar-locs
1187 locs)
1188 (if (= nvals 1)
1189 (vop return-single node block old-fp return-pc (car locs))
1190 (vop* return node block
1191 (old-fp return-pc (reference-tn-list locs nil))
1192 (nil)
1193 nvals))))
1195 (aver (eq lvar-kind :unknown))
1196 (vop* return-multiple node block
1197 (old-fp return-pc
1198 (reference-tn-list (ir2-lvar-locs 2lvar) nil))
1199 (nil)))))
1201 (values))
1203 ;;;; debugger hooks
1205 ;;; This is used by the debugger to find the top function on the
1206 ;;; stack. It returns the OLD-FP and RETURN-PC for the current
1207 ;;; function as multiple values.
1208 (defoptimizer (sb!kernel:%caller-frame-and-pc ir2-convert) (() node block)
1209 (let ((ir2-physenv (physenv-info (node-physenv node))))
1210 (move-lvar-result node block
1211 (list (ir2-physenv-old-fp ir2-physenv)
1212 (ir2-physenv-return-pc ir2-physenv))
1213 (node-lvar node))))
1215 ;;;; multiple values
1217 ;;; This is almost identical to IR2-CONVERT-LET. Since LTN annotates
1218 ;;; the lvarinuation for the correct number of values (with the lvar
1219 ;;; user responsible for defaulting), we can just pick them up from
1220 ;;; the lvar.
1221 (defun ir2-convert-mv-bind (node block)
1222 (declare (type mv-combination node) (type ir2-block block))
1223 (let* ((lvar (first (basic-combination-args node)))
1224 (fun (ref-leaf (lvar-uses (basic-combination-fun node))))
1225 (vars (lambda-vars fun)))
1226 (aver (eq (functional-kind fun) :mv-let))
1227 (mapc (lambda (src var)
1228 (when (leaf-refs var)
1229 (let ((dest (leaf-info var)))
1230 (if (lambda-var-indirect var)
1231 (do-make-value-cell node block src dest)
1232 (emit-move node block src dest)))))
1233 (lvar-tns node block lvar
1234 (mapcar (lambda (x)
1235 (primitive-type (leaf-type x)))
1236 vars))
1237 vars))
1238 (values))
1240 ;;; Emit the appropriate fixed value, unknown value or tail variant of
1241 ;;; CALL-VARIABLE. Note that we only need to pass the values start for
1242 ;;; the first argument: all the other argument lvar TNs are
1243 ;;; ignored. This is because we require all of the values globs to be
1244 ;;; contiguous and on stack top.
1245 (defun ir2-convert-mv-call (node block)
1246 (declare (type mv-combination node) (type ir2-block block))
1247 (aver (basic-combination-args node))
1248 (let* ((start-lvar (lvar-info (first (basic-combination-args node))))
1249 (start (first (ir2-lvar-locs start-lvar)))
1250 (tails (and (node-tail-p node)
1251 (lambda-tail-set (node-home-lambda node))))
1252 (lvar (node-lvar node))
1253 (2lvar (and lvar (lvar-info lvar))))
1254 (multiple-value-bind (fun named)
1255 (fun-lvar-tn node block (basic-combination-fun node))
1256 (aver (and (not named)
1257 (eq (ir2-lvar-kind start-lvar) :unknown)))
1258 (cond
1259 (tails
1260 (let ((env (physenv-info (node-physenv node))))
1261 (vop tail-call-variable node block start fun
1262 (ir2-physenv-old-fp env)
1263 (ir2-physenv-return-pc env))))
1264 ((and 2lvar
1265 (eq (ir2-lvar-kind 2lvar) :unknown))
1266 (vop* multiple-call-variable node block (start fun nil)
1267 ((reference-tn-list (ir2-lvar-locs 2lvar) t))))
1269 (let ((locs (standard-result-tns lvar)))
1270 (vop* call-variable node block (start fun nil)
1271 ((reference-tn-list locs t)) (length locs))
1272 (move-lvar-result node block locs lvar)))))))
1274 ;;; Reset the stack pointer to the start of the specified
1275 ;;; unknown-values lvar (discarding it and all values globs on top of
1276 ;;; it.)
1277 (defoptimizer (%pop-values ir2-convert) ((lvar) node block)
1278 (let ((2lvar (lvar-info (lvar-value lvar))))
1279 (aver (eq (ir2-lvar-kind 2lvar) :unknown))
1280 (vop reset-stack-pointer node block
1281 (first (ir2-lvar-locs 2lvar)))))
1283 ;;; Deliver the values TNs to LVAR using MOVE-LVAR-RESULT.
1284 (defoptimizer (values ir2-convert) ((&rest values) node block)
1285 (let ((tns (mapcar (lambda (x)
1286 (lvar-tn node block x))
1287 values)))
1288 (move-lvar-result node block tns (node-lvar node))))
1290 ;;; In the normal case where unknown values are desired, we use the
1291 ;;; VALUES-LIST VOP. In the relatively unimportant case of VALUES-LIST
1292 ;;; for a fixed number of values, we punt by doing a full call to the
1293 ;;; VALUES-LIST function. This gets the full call VOP to deal with
1294 ;;; defaulting any unsupplied values. It seems unworthwhile to
1295 ;;; optimize this case.
1296 (defoptimizer (values-list ir2-convert) ((list) node block)
1297 (let* ((lvar (node-lvar node))
1298 (2lvar (and lvar (lvar-info lvar))))
1299 (cond ((and 2lvar
1300 (eq (ir2-lvar-kind 2lvar) :unknown))
1301 (let ((locs (ir2-lvar-locs 2lvar)))
1302 (vop* values-list node block
1303 ((lvar-tn node block list) nil)
1304 ((reference-tn-list locs t)))))
1305 (t (aver (or (not 2lvar) ; i.e. we want to check the argument
1306 (eq (ir2-lvar-kind 2lvar) :fixed)))
1307 (ir2-convert-full-call node block)))))
1309 (defoptimizer (%more-arg-values ir2-convert) ((context start count) node block)
1310 (binding* ((lvar (node-lvar node) :exit-if-null)
1311 (2lvar (lvar-info lvar)))
1312 (ecase (ir2-lvar-kind 2lvar)
1313 (:fixed (ir2-convert-full-call node block))
1314 (:unknown
1315 (let ((locs (ir2-lvar-locs 2lvar)))
1316 (vop* %more-arg-values node block
1317 ((lvar-tn node block context)
1318 (lvar-tn node block start)
1319 (lvar-tn node block count)
1320 nil)
1321 ((reference-tn-list locs t))))))))
1323 ;;;; special binding
1325 ;;; This is trivial, given our assumption of a shallow-binding
1326 ;;; implementation.
1327 (defoptimizer (%special-bind ir2-convert) ((var value) node block)
1328 (let ((name (leaf-source-name (lvar-value var))))
1329 (vop bind node block (lvar-tn node block value)
1330 (emit-constant name))))
1331 (defoptimizer (%special-unbind ir2-convert) ((var) node block)
1332 (vop unbind node block))
1334 ;;; ### It's not clear that this really belongs in this file, or
1335 ;;; should really be done this way, but this is the least violation of
1336 ;;; abstraction in the current setup. We don't want to wire
1337 ;;; shallow-binding assumptions into IR1tran.
1338 (def-ir1-translator progv
1339 ((vars vals &body body) start next result)
1340 (ir1-convert
1341 start next result
1342 (with-unique-names (bind unbind)
1343 (once-only ((n-save-bs '(%primitive current-binding-pointer)))
1344 `(unwind-protect
1345 (progn
1346 (labels ((,unbind (vars)
1347 (declare (optimize (speed 2) (debug 0)))
1348 (dolist (var vars)
1349 (%primitive bind nil var)
1350 (makunbound var)))
1351 (,bind (vars vals)
1352 (declare (optimize (speed 2) (debug 0)))
1353 (cond ((null vars))
1354 ((null vals) (,unbind vars))
1355 (t (%primitive bind
1356 (car vals)
1357 (car vars))
1358 (,bind (cdr vars) (cdr vals))))))
1359 (,bind ,vars ,vals))
1361 ,@body)
1362 (%primitive unbind-to-here ,n-save-bs))))))
1364 ;;;; non-local exit
1366 ;;; Convert a non-local lexical exit. First find the NLX-INFO in our
1367 ;;; environment. Note that this is never called on the escape exits
1368 ;;; for CATCH and UNWIND-PROTECT, since the escape functions aren't
1369 ;;; IR2 converted.
1370 (defun ir2-convert-exit (node block)
1371 (declare (type exit node) (type ir2-block block))
1372 (let ((loc (find-in-physenv (find-nlx-info node)
1373 (node-physenv node)))
1374 (temp (make-stack-pointer-tn))
1375 (value (exit-value node)))
1376 (vop value-cell-ref node block loc temp)
1377 (if value
1378 (let ((locs (ir2-lvar-locs (lvar-info value))))
1379 (vop unwind node block temp (first locs) (second locs)))
1380 (let ((0-tn (emit-constant 0)))
1381 (vop unwind node block temp 0-tn 0-tn))))
1383 (values))
1385 ;;; %CLEANUP-POINT doesn't do anything except prevent the body from
1386 ;;; being entirely deleted.
1387 (defoptimizer (%cleanup-point ir2-convert) (() node block) node block)
1389 ;;; This function invalidates a lexical exit on exiting from the
1390 ;;; dynamic extent. This is done by storing 0 into the indirect value
1391 ;;; cell that holds the closed unwind block.
1392 (defoptimizer (%lexical-exit-breakup ir2-convert) ((info) node block)
1393 (vop value-cell-set node block
1394 (find-in-physenv (lvar-value info) (node-physenv node))
1395 (emit-constant 0)))
1397 ;;; We have to do a spurious move of no values to the result lvar so
1398 ;;; that lifetime analysis won't get confused.
1399 (defun ir2-convert-throw (node block)
1400 (declare (type mv-combination node) (type ir2-block block))
1401 (let ((args (basic-combination-args node)))
1402 (check-catch-tag-type (first args))
1403 (vop* throw node block
1404 ((lvar-tn node block (first args))
1405 (reference-tn-list
1406 (ir2-lvar-locs (lvar-info (second args)))
1407 nil))
1408 (nil)))
1409 (move-lvar-result node block () (node-lvar node))
1410 (values))
1412 ;;; Emit code to set up a non-local exit. INFO is the NLX-INFO for the
1413 ;;; exit, and TAG is the lvar for the catch tag (if any.) We get at
1414 ;;; the target PC by passing in the label to the vop. The vop is
1415 ;;; responsible for building a return-PC object.
1416 (defun emit-nlx-start (node block info tag)
1417 (declare (type node node) (type ir2-block block) (type nlx-info info)
1418 (type (or lvar null) tag))
1419 (let* ((2info (nlx-info-info info))
1420 (kind (cleanup-kind (nlx-info-cleanup info)))
1421 (block-tn (physenv-live-tn
1422 (make-normal-tn (primitive-type-or-lose 'catch-block))
1423 (node-physenv node)))
1424 (res (make-stack-pointer-tn))
1425 (target-label (ir2-nlx-info-target 2info)))
1427 (vop current-binding-pointer node block
1428 (car (ir2-nlx-info-dynamic-state 2info)))
1429 (vop* save-dynamic-state node block
1430 (nil)
1431 ((reference-tn-list (cdr (ir2-nlx-info-dynamic-state 2info)) t)))
1432 (vop current-stack-pointer node block (ir2-nlx-info-save-sp 2info))
1434 (ecase kind
1435 (:catch
1436 (vop make-catch-block node block block-tn
1437 (lvar-tn node block tag) target-label res))
1438 ((:unwind-protect :block :tagbody)
1439 (vop make-unwind-block node block block-tn target-label res)))
1441 (ecase kind
1442 ((:block :tagbody)
1443 (do-make-value-cell node block res (ir2-nlx-info-home 2info)))
1444 (:unwind-protect
1445 (vop set-unwind-protect node block block-tn))
1446 (:catch)))
1448 (values))
1450 ;;; Scan each of ENTRY's exits, setting up the exit for each lexical exit.
1451 (defun ir2-convert-entry (node block)
1452 (declare (type entry node) (type ir2-block block))
1453 (dolist (exit (entry-exits node))
1454 (let ((info (find-nlx-info exit)))
1455 (when (and info
1456 (member (cleanup-kind (nlx-info-cleanup info))
1457 '(:block :tagbody)))
1458 (emit-nlx-start node block info nil))))
1459 (values))
1461 ;;; Set up the unwind block for these guys.
1462 (defoptimizer (%catch ir2-convert) ((info-lvar tag) node block)
1463 (check-catch-tag-type tag)
1464 (emit-nlx-start node block (lvar-value info-lvar) tag))
1465 (defoptimizer (%unwind-protect ir2-convert) ((info-lvar cleanup) node block)
1466 (emit-nlx-start node block (lvar-value info-lvar) nil))
1468 ;;; Emit the entry code for a non-local exit. We receive values and
1469 ;;; restore dynamic state.
1471 ;;; In the case of a lexical exit or CATCH, we look at the exit lvar's
1472 ;;; kind to determine which flavor of entry VOP to emit. If unknown
1473 ;;; values, emit the xxx-MULTIPLE variant to the lvar locs. If fixed
1474 ;;; values, make the appropriate number of temps in the standard
1475 ;;; values locations and use the other variant, delivering the temps
1476 ;;; to the lvar using MOVE-LVAR-RESULT.
1478 ;;; In the UNWIND-PROTECT case, we deliver the first register
1479 ;;; argument, the argument count and the argument pointer to our lvar
1480 ;;; as multiple values. These values are the block exited to and the
1481 ;;; values start and count.
1483 ;;; After receiving values, we restore dynamic state. Except in the
1484 ;;; UNWIND-PROTECT case, the values receiving restores the stack
1485 ;;; pointer. In an UNWIND-PROTECT cleanup, we want to leave the stack
1486 ;;; pointer alone, since the thrown values are still out there.
1487 (defoptimizer (%nlx-entry ir2-convert) ((info-lvar) node block)
1488 (let* ((info (lvar-value info-lvar))
1489 (lvar (nlx-info-lvar info))
1490 (2info (nlx-info-info info))
1491 (top-loc (ir2-nlx-info-save-sp 2info))
1492 (start-loc (make-nlx-entry-arg-start-location))
1493 (count-loc (make-arg-count-location))
1494 (target (ir2-nlx-info-target 2info)))
1496 (ecase (cleanup-kind (nlx-info-cleanup info))
1497 ((:catch :block :tagbody)
1498 (let ((2lvar (and lvar (lvar-info lvar))))
1499 (if (and 2lvar (eq (ir2-lvar-kind 2lvar) :unknown))
1500 (vop* nlx-entry-multiple node block
1501 (top-loc start-loc count-loc nil)
1502 ((reference-tn-list (ir2-lvar-locs 2lvar) t))
1503 target)
1504 (let ((locs (standard-result-tns lvar)))
1505 (vop* nlx-entry node block
1506 (top-loc start-loc count-loc nil)
1507 ((reference-tn-list locs t))
1508 target
1509 (length locs))
1510 (move-lvar-result node block locs lvar)))))
1511 (:unwind-protect
1512 (let ((block-loc (standard-arg-location 0)))
1513 (vop uwp-entry node block target block-loc start-loc count-loc)
1514 (move-lvar-result
1515 node block
1516 (list block-loc start-loc count-loc)
1517 lvar))))
1519 #!+sb-dyncount
1520 (when *collect-dynamic-statistics*
1521 (vop count-me node block *dynamic-counts-tn*
1522 (block-number (ir2-block-block block))))
1524 (vop* restore-dynamic-state node block
1525 ((reference-tn-list (cdr (ir2-nlx-info-dynamic-state 2info)) nil))
1526 (nil))
1527 (vop unbind-to-here node block
1528 (car (ir2-nlx-info-dynamic-state 2info)))))
1530 ;;;; n-argument functions
1532 (macrolet ((def (name)
1533 `(defoptimizer (,name ir2-convert) ((&rest args) node block)
1534 (let* ((refs (move-tail-full-call-args node block))
1535 (lvar (node-lvar node))
1536 (res (lvar-result-tns
1537 lvar
1538 (list (primitive-type (specifier-type 'list))))))
1539 (vop* ,name node block (refs) ((first res) nil)
1540 (length args))
1541 (move-lvar-result node block res lvar)))))
1542 (def list)
1543 (def list*))
1545 ;;; Convert the code in a component into VOPs.
1546 (defun ir2-convert (component)
1547 (declare (type component component))
1548 (let (#!+sb-dyncount
1549 (*dynamic-counts-tn*
1550 (when *collect-dynamic-statistics*
1551 (let* ((blocks
1552 (block-number (block-next (component-head component))))
1553 (counts (make-array blocks
1554 :element-type '(unsigned-byte 32)
1555 :initial-element 0))
1556 (info (make-dyncount-info
1557 :for (component-name component)
1558 :costs (make-array blocks
1559 :element-type '(unsigned-byte 32)
1560 :initial-element 0)
1561 :counts counts)))
1562 (setf (ir2-component-dyncount-info (component-info component))
1563 info)
1564 (emit-constant info)
1565 (emit-constant counts)))))
1566 (let ((num 0))
1567 (declare (type index num))
1568 (do-ir2-blocks (2block component)
1569 (let ((block (ir2-block-block 2block)))
1570 (when (block-start block)
1571 (setf (block-number block) num)
1572 #!+sb-dyncount
1573 (when *collect-dynamic-statistics*
1574 (let ((first-node (block-start-node block)))
1575 (unless (or (and (bind-p first-node)
1576 (xep-p (bind-lambda first-node)))
1577 (eq (lvar-fun-name
1578 (node-lvar first-node))
1579 '%nlx-entry))
1580 (vop count-me
1581 first-node
1582 2block
1583 #!+sb-dyncount *dynamic-counts-tn* #!-sb-dyncount nil
1584 num))))
1585 (ir2-convert-block block)
1586 (incf num))))))
1587 (values))
1589 ;;; If necessary, emit a terminal unconditional branch to go to the
1590 ;;; successor block. If the successor is the component tail, then
1591 ;;; there isn't really any successor, but if the end is an unknown,
1592 ;;; non-tail call, then we emit an error trap just in case the
1593 ;;; function really does return.
1594 (defun finish-ir2-block (block)
1595 (declare (type cblock block))
1596 (let* ((2block (block-info block))
1597 (last (block-last block))
1598 (succ (block-succ block)))
1599 (unless (if-p last)
1600 (aver (singleton-p succ))
1601 (let ((target (first succ)))
1602 (cond ((eq target (component-tail (block-component block)))
1603 (when (and (basic-combination-p last)
1604 (eq (basic-combination-kind last) :full))
1605 (let* ((fun (basic-combination-fun last))
1606 (use (lvar-uses fun))
1607 (name (and (ref-p use)
1608 (leaf-has-source-name-p (ref-leaf use))
1609 (leaf-source-name (ref-leaf use)))))
1610 (unless (or (node-tail-p last)
1611 (info :function :info name)
1612 (policy last (zerop safety)))
1613 (vop nil-fun-returned-error last 2block
1614 (if name
1615 (emit-constant name)
1616 (multiple-value-bind (tn named)
1617 (fun-lvar-tn last 2block fun)
1618 (aver (not named))
1619 tn)))))))
1620 ((not (eq (ir2-block-next 2block) (block-info target)))
1621 (vop branch last 2block (block-label target)))))))
1623 (values))
1625 ;;; Convert the code in a block into VOPs.
1626 (defun ir2-convert-block (block)
1627 (declare (type cblock block))
1628 (let ((2block (block-info block)))
1629 (do-nodes (node lvar block)
1630 (etypecase node
1631 (ref
1632 (when lvar
1633 (let ((2lvar (lvar-info lvar)))
1634 ;; function REF in a local call is not annotated
1635 (when (and 2lvar (not (eq (ir2-lvar-kind 2lvar) :delayed)))
1636 (ir2-convert-ref node 2block)))))
1637 (combination
1638 (let ((kind (basic-combination-kind node)))
1639 (case kind
1640 (:local
1641 (ir2-convert-local-call node 2block))
1642 (:full
1643 (ir2-convert-full-call node 2block))
1645 (let ((fun (fun-info-ir2-convert kind)))
1646 (cond (fun
1647 (funcall fun node 2block))
1648 ((eq (basic-combination-info node) :full)
1649 (ir2-convert-full-call node 2block))
1651 (ir2-convert-template node 2block))))))))
1652 (cif
1653 (when (lvar-info (if-test node))
1654 (ir2-convert-if node 2block)))
1655 (bind
1656 (let ((fun (bind-lambda node)))
1657 (when (eq (lambda-home fun) fun)
1658 (ir2-convert-bind node 2block))))
1659 (creturn
1660 (ir2-convert-return node 2block))
1661 (cset
1662 (ir2-convert-set node 2block))
1663 (cast
1664 (ir2-convert-cast node 2block))
1665 (mv-combination
1666 (cond
1667 ((eq (basic-combination-kind node) :local)
1668 (ir2-convert-mv-bind node 2block))
1669 ((eq (lvar-fun-name (basic-combination-fun node))
1670 '%throw)
1671 (ir2-convert-throw node 2block))
1673 (ir2-convert-mv-call node 2block))))
1674 (exit
1675 (when (exit-entry node)
1676 (ir2-convert-exit node 2block)))
1677 (entry
1678 (ir2-convert-entry node 2block)))))
1680 (finish-ir2-block block)
1682 (values))