Fix comment about *code-coverage-info*.
[sbcl.git] / src / compiler / ir2tran.lisp
blob20052ac8fa0125ce40eb3553d2f9de6bdc3a1776
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 ;;; Determine whether we should emit a single-stepper breakpoint
25 ;;; around a call / before a vop.
26 (defun emit-step-p (node)
27 (if (and (policy node (> insert-step-conditions 1))
28 (typep node 'combination))
29 (combination-step-info node)
30 nil))
32 ;;; Allocate an indirect value cell.
33 (defevent make-value-cell-event "Allocate heap value cell for lexical var.")
34 (defun emit-make-value-cell (node block value res)
35 (event make-value-cell-event node)
36 (vop make-value-cell node block value nil res))
38 ;;;; leaf reference
40 ;;; Return the TN that holds the value of THING in the environment ENV.
41 (declaim (ftype (sfunction ((or nlx-info lambda-var clambda) physenv) tn)
42 find-in-physenv))
43 (defun find-in-physenv (thing physenv)
44 (or (cdr (assoc thing (ir2-physenv-closure (physenv-info physenv))))
45 (etypecase thing
46 (lambda-var
47 ;; I think that a failure of this assertion means that we're
48 ;; trying to access a variable which was improperly closed
49 ;; over. The PHYSENV describes a physical environment. Every
50 ;; variable that a form refers to should either be in its
51 ;; physical environment directly, or grabbed from a
52 ;; surrounding physical environment when it was closed over.
53 ;; The ASSOC expression above finds closed-over variables, so
54 ;; if we fell through the ASSOC expression, it wasn't closed
55 ;; over. Therefore, it must be in our physical environment
56 ;; directly. If instead it is in some other physical
57 ;; environment, then it's bogus for us to reference it here
58 ;; without it being closed over. -- WHN 2001-09-29
59 (aver (eq physenv (lambda-physenv (lambda-var-home thing))))
60 (leaf-info thing))
61 (nlx-info
62 (aver (eq physenv (block-physenv (nlx-info-target thing))))
63 (ir2-nlx-info-home (nlx-info-info thing)))
64 (clambda
65 (aver (xep-p thing))
66 (entry-info-closure-tn (lambda-info thing))))
67 (bug "~@<~2I~_~S ~_not found in ~_~S~:>" thing physenv)))
69 ;;; If LEAF already has a constant TN, return that, otherwise make a
70 ;;; TN for it.
71 (defun constant-tn (leaf boxedp)
72 (declare (type constant leaf))
73 ;; When convenient we can have both a boxed and unboxed TN for
74 ;; constant.
75 (if boxedp
76 (or (constant-boxed-tn leaf)
77 (setf (constant-boxed-tn leaf) (make-constant-tn leaf t)))
78 (or (leaf-info leaf)
79 (setf (leaf-info leaf) (make-constant-tn leaf nil)))))
81 ;;; Return a TN that represents the value of LEAF, or NIL if LEAF
82 ;;; isn't directly represented by a TN. ENV is the environment that
83 ;;; the reference is done in.
84 (defun leaf-tn (leaf env boxedp)
85 (declare (type leaf leaf) (type physenv env))
86 (typecase leaf
87 (lambda-var
88 (unless (lambda-var-indirect leaf)
89 (find-in-physenv leaf env)))
90 (constant (constant-tn leaf boxedp))
91 (t nil)))
93 ;;; This is used to conveniently get a handle on a constant TN during
94 ;;; IR2 conversion. It returns a constant TN representing the Lisp
95 ;;; object VALUE.
96 (defun emit-constant (value)
97 (constant-tn (find-constant value) t))
99 (defun %return-is-boxed (node)
100 (declare (type creturn node))
101 (let* ((fun (return-lambda node))
102 (returns (tail-set-info (lambda-tail-set fun))))
103 (or (xep-p fun)
104 (eq (return-info-kind returns) :unknown))))
106 (defun boxed-ref-p (ref)
107 (let ((dest (lvar-dest (ref-lvar ref))))
108 (cond ((and (basic-combination-p dest)
109 (call-full-like-p dest))
111 ((and (return-p dest) (%return-is-boxed dest)))
113 nil))))
115 ;;; Convert a REF node. The reference must not be delayed.
116 (defun ir2-convert-ref (node block)
117 (declare (type ref node) (type ir2-block block))
118 (let* ((lvar (node-lvar node))
119 (leaf (ref-leaf node))
120 (locs (lvar-result-tns
121 lvar (list (primitive-type (leaf-type leaf)))))
122 (res (first locs)))
123 (etypecase leaf
124 (lambda-var
125 (let ((tn (find-in-physenv leaf (node-physenv node)))
126 (indirect (lambda-var-indirect leaf))
127 (explicit (lambda-var-explicit-value-cell leaf)))
128 (cond
129 ((and indirect explicit)
130 (vop value-cell-ref node block tn res))
131 ((and indirect
132 (not (eq (node-physenv node)
133 (lambda-physenv (lambda-var-home leaf)))))
134 (let ((reffer (third (primitive-type-indirect-cell-type
135 (primitive-type (leaf-type leaf))))))
136 (if reffer
137 (funcall reffer node block tn (leaf-info leaf) res)
138 (vop ancestor-frame-ref node block tn (leaf-info leaf) res))))
139 (t (emit-move node block tn res)))))
140 (constant
141 (emit-move node block (constant-tn leaf (boxed-ref-p node)) res))
142 (functional
143 (ir2-convert-closure node block leaf res))
144 (global-var
145 (ir2-convert-global-var node block leaf res)))
146 (move-lvar-result node block locs lvar))
147 (values))
149 (defun ir2-convert-global-var (node block leaf res)
150 (let ((unsafe (policy node (zerop safety)))
151 (name (leaf-source-name leaf)))
152 (ecase (global-var-kind leaf)
153 ((:special :unknown)
154 (aver (symbolp name))
155 (let ((name-tn (emit-constant name)))
156 (if (or unsafe (always-boundp name))
157 (vop fast-symbol-value node block name-tn res)
158 (vop symbol-value node block name-tn res))))
159 (:global
160 (aver (symbolp name))
161 (let ((name-tn (emit-constant name)))
162 (if (or unsafe (always-boundp name))
163 (vop fast-symbol-global-value node block name-tn res)
164 (vop symbol-global-value node block name-tn res))))
165 (:global-function
166 ;; In cross-compilation, testing (INFO :function :definition) is not
167 ;; sensible (or possible) but we can assume that things with fun-info
168 ;; will eventually be defined. If that's untrue, e.g. if we referred
169 ;; to #'DESCRIBE during cold-load, we'd just fix it locally by declaring
170 ;; DESCRIBE notinline.
171 ;; But in the target, more caution is warranted because users might
172 ;; DEFKNOWN a function but fail to define it. And they shouldn't be
173 ;; expected to understand the failure mode and the remedy.
174 (cond ((and #-sb-xc-host (info :function :definition name)
175 (info :function :info name)
176 (let ((*lexenv* (node-lexenv node)))
177 (not (fun-lexically-notinline-p name))))
178 ;; Known functions can be dumped without going through fdefns.
179 ;; But if NOTINLINEd, don't early-bind to the functional value
180 ;; because that disallows redefinition, including but not limited
181 ;; to encapsulations, which in turn makes TRACE not work, which
182 ;; leads to extreme frustration when debugging.
183 (emit-move node block (make-load-time-constant-tn :known-fun name)
184 res))
186 (let ((fdefn-tn (make-load-time-constant-tn :fdefinition name)))
187 (if unsafe
188 (vop fdefn-fun node block fdefn-tn res)
189 (vop safe-fdefn-fun node block fdefn-tn res)))))))))
191 ;;; some sanity checks for a CLAMBDA passed to IR2-CONVERT-CLOSURE
192 (defun assertions-on-ir2-converted-clambda (clambda)
193 ;; This assertion was sort of an experiment. It would be nice and
194 ;; sane and easier to understand things if it were *always* true,
195 ;; but experimentally I observe that it's only *almost* always
196 ;; true. -- WHN 2001-01-02
197 #+nil
198 (aver (eql (lambda-component clambda)
199 (block-component (ir2-block-block ir2-block))))
200 ;; Check for some weirdness which came up in bug
201 ;; 138, 2002-01-02.
203 ;; The MAKE-LOAD-TIME-CONSTANT-TN call above puts an :ENTRY record
204 ;; into the IR2-COMPONENT-CONSTANTS table. The dump-a-COMPONENT
205 ;; code
206 ;; * treats every HANDLEless :ENTRY record into a
207 ;; patch, and
208 ;; * expects every patch to correspond to an
209 ;; IR2-COMPONENT-ENTRIES record.
210 ;; The IR2-COMPONENT-ENTRIES records are set by ENTRY-ANALYZE
211 ;; walking over COMPONENT-LAMBDAS. Bug 138b arose because there
212 ;; was a HANDLEless :ENTRY record which didn't correspond to an
213 ;; IR2-COMPONENT-ENTRIES record. That problem is hard to debug
214 ;; when it's caught at dump time, so this assertion tries to catch
215 ;; it here.
216 (aver (member clambda
217 (component-lambdas (lambda-component clambda))))
218 ;; another bug-138-related issue: COMPONENT-NEW-FUNCTIONALS is
219 ;; used as a queue for stuff pending to do in IR1, and now that
220 ;; we're doing IR2 it should've been completely flushed (but
221 ;; wasn't).
222 (aver (null (component-new-functionals (lambda-component clambda))))
223 (values))
225 ;;; Emit code to load a function object implementing FUNCTIONAL into
226 ;;; RES. This gets interesting when the referenced function is a
227 ;;; closure: we must make the closure and move the closed-over values
228 ;;; into it.
230 ;;; FUNCTIONAL is either a :TOPLEVEL-XEP functional or the XEP lambda
231 ;;; for the called function, since local call analysis converts all
232 ;;; closure references. If a :TOPLEVEL-XEP, we know it is not a
233 ;;; closure.
235 ;;; If a closed-over LAMBDA-VAR has no refs (is deleted), then we
236 ;;; don't initialize that slot. This can happen with closures over
237 ;;; top level variables, where optimization of the closure deleted the
238 ;;; variable. Since we committed to the closure format when we
239 ;;; pre-analyzed the top level code, we just leave an empty slot.
240 (defun ir2-convert-closure (ref ir2-block functional res)
241 (declare (type ref ref)
242 (type ir2-block ir2-block)
243 (type functional functional)
244 (type tn res))
245 (flet ((prepare ()
246 (aver (not (eql (functional-kind functional) :deleted)))
247 (unless (leaf-info functional)
248 (setf (leaf-info functional)
249 (make-entry-info :name
250 (functional-debug-name functional))))))
251 (let ((closure (etypecase functional
252 (clambda
253 (assertions-on-ir2-converted-clambda functional)
254 (physenv-closure (get-lambda-physenv functional)))
255 (functional
256 (aver (eq (functional-kind functional) :toplevel-xep))
257 nil)))
258 global-var)
259 (cond (closure
260 (prepare)
261 (let* ((physenv (node-physenv ref))
262 (tn (find-in-physenv functional physenv)))
263 (emit-move ref ir2-block tn res)))
264 ;; we're about to emit a reference to a "closure" that's actually
265 ;; an inlinable global function.
266 ((and (global-var-p (setf global-var
267 (functional-inline-expanded functional)))
268 (eq :global-function (global-var-kind global-var)))
269 (ir2-convert-global-var ref ir2-block global-var res))
271 ;; if we're here, we should have either a toplevel-xep (some
272 ;; global scope function in a different component) or an external
273 ;; reference to the "closure"'s body.
274 (prepare)
275 (aver (memq (functional-kind functional) '(:external :toplevel-xep)))
276 (let ((entry (make-load-time-constant-tn :entry functional)))
277 (emit-move ref ir2-block entry res))))))
278 (values))
280 (defun closure-initial-value (what this-env current-fp)
281 (declare (type (or nlx-info lambda-var clambda) what)
282 (type physenv this-env)
283 (type (or tn null) current-fp))
284 ;; If we have an indirect LAMBDA-VAR that does not require an
285 ;; EXPLICIT-VALUE-CELL, and is from this environment (not from being
286 ;; closed over), we need to store the current frame pointer.
287 (if (and (lambda-var-p what)
288 (lambda-var-indirect what)
289 (not (lambda-var-explicit-value-cell what))
290 (eq (lambda-physenv (lambda-var-home what))
291 this-env))
292 current-fp
293 (find-in-physenv what this-env)))
295 (defoptimizer (%allocate-closures ltn-annotate) ((leaves) node ltn-policy)
296 (declare (ignore ltn-policy))
297 (when (lvar-dynamic-extent leaves)
298 (let ((info (make-ir2-lvar *backend-t-primitive-type*)))
299 (setf (ir2-lvar-kind info) :delayed)
300 (setf (lvar-info leaves) info)
301 (setf (ir2-lvar-stack-pointer info)
302 (make-stack-pointer-tn)))))
304 (defoptimizer (%allocate-closures ir2-convert) ((leaves) call 2block)
305 (let ((dx-p (lvar-dynamic-extent leaves)))
306 (collect ((delayed))
307 (when dx-p
308 (vop current-stack-pointer call 2block
309 (ir2-lvar-stack-pointer (lvar-info leaves))))
310 (dolist (leaf (lvar-value leaves))
311 (binding* ((xep (awhen (functional-entry-fun leaf)
312 ;; if the xep's been deleted then we can skip it
313 (if (eq (functional-kind it) :deleted)
314 nil it))
315 :exit-if-null)
316 (nil (aver (xep-p xep)))
317 (entry-info (lambda-info xep) :exit-if-null)
318 (tn (entry-info-closure-tn entry-info) :exit-if-null)
319 (closure (physenv-closure (get-lambda-physenv xep)))
320 (entry (make-load-time-constant-tn :entry xep)))
321 (let ((this-env (node-physenv call))
322 (leaf-dx-p (and dx-p (leaf-dynamic-extent leaf))))
323 (vop make-closure call 2block entry (length closure)
324 leaf-dx-p tn)
325 (loop for what in closure and n from 0 do
326 (unless (and (lambda-var-p what)
327 (null (leaf-refs what)))
328 ;; In LABELS a closure may refer to another closure
329 ;; in the same group, so we must be sure that we
330 ;; store a closure only after its creation.
332 ;; TODO: Here is a simple solution: we postpone
333 ;; putting of all closures after all creations
334 ;; (though it may require more registers).
335 (if (lambda-p what)
336 (delayed (list tn (find-in-physenv what this-env) n))
337 (let ((initial-value (closure-initial-value
338 what this-env nil)))
339 (if initial-value
340 (vop closure-init call 2block
341 tn initial-value n)
342 ;; An initial-value of NIL means to stash
343 ;; the frame pointer... which requires a
344 ;; different VOP.
345 (vop closure-init-from-fp call 2block tn n)))))))))
346 (loop for (tn what n) in (delayed)
347 do (vop closure-init call 2block
348 tn what n))))
349 (values))
351 ;;; Convert a SET node. If the NODE's LVAR is annotated, then we also
352 ;;; deliver the value to that lvar. If the var is a lexical variable
353 ;;; with no refs, then we don't actually set anything, since the
354 ;;; variable has been deleted.
355 (defun ir2-convert-set (node block)
356 (declare (type cset node) (type ir2-block block))
357 (let* ((lvar (node-lvar node))
358 (leaf (set-var node))
359 (val (lvar-tn node block (set-value node)))
360 (locs (if lvar
361 (lvar-result-tns
362 lvar (list (primitive-type (leaf-type leaf))))
363 nil)))
364 (etypecase leaf
365 (lambda-var
366 (when (leaf-refs leaf)
367 (let ((tn (find-in-physenv leaf (node-physenv node)))
368 (indirect (lambda-var-indirect leaf))
369 (explicit (lambda-var-explicit-value-cell leaf)))
370 (cond
371 ((and indirect explicit)
372 (vop value-cell-set node block tn val))
373 ((and indirect
374 (not (eq (node-physenv node)
375 (lambda-physenv (lambda-var-home leaf)))))
376 (let ((setter (fourth (primitive-type-indirect-cell-type
377 (primitive-type (leaf-type leaf))))))
378 (if setter
379 (funcall setter node block tn val (leaf-info leaf))
380 (vop ancestor-frame-set node block tn val (leaf-info leaf)))))
381 (t (emit-move node block val tn))))))
382 (global-var
383 (aver (symbolp (leaf-source-name leaf)))
384 (ecase (global-var-kind leaf)
385 ((:special)
386 (vop set node block (emit-constant (leaf-source-name leaf)) val))
387 ((:global)
388 (vop %set-symbol-global-value node
389 block (emit-constant (leaf-source-name leaf)) val)))))
390 (when locs
391 (emit-move node block val (first locs))
392 (move-lvar-result node block locs lvar)))
393 (values))
395 ;;;; utilities for receiving fixed values
397 ;;; Return a TN that can be referenced to get the value of LVAR. LVAR
398 ;;; must be LTN-ANNOTATED either as a delayed leaf ref or as a fixed,
399 ;;; single-value lvar.
401 ;;; The primitive-type of the result will always be the same as the
402 ;;; IR2-LVAR-PRIMITIVE-TYPE, ensuring that VOPs are always called with
403 ;;; TNs that satisfy the operand primitive-type restriction. We may
404 ;;; have to make a temporary of the desired type and move the actual
405 ;;; lvar TN into it. This happens when we delete a type check in
406 ;;; unsafe code or when we locally know something about the type of an
407 ;;; argument variable.
408 (defun lvar-tn (node block lvar)
409 (declare (type node node) (type ir2-block block) (type lvar lvar))
410 (let* ((2lvar (lvar-info lvar))
411 (lvar-tn
412 (ecase (ir2-lvar-kind 2lvar)
413 (:delayed
414 (let ((ref (lvar-uses lvar)))
415 (leaf-tn (ref-leaf ref) (node-physenv ref) (boxed-ref-p ref))))
416 (:fixed
417 (aver (= (length (ir2-lvar-locs 2lvar)) 1))
418 (first (ir2-lvar-locs 2lvar)))))
419 (ptype (ir2-lvar-primitive-type 2lvar)))
421 (cond ((eq (tn-primitive-type lvar-tn) ptype) lvar-tn)
423 (let ((temp (make-normal-tn ptype)))
424 (emit-move node block lvar-tn temp)
425 temp)))))
427 ;;; This is similar to LVAR-TN, but hacks multiple values. We return
428 ;;; TNs holding the values of LVAR with PTYPES as their primitive
429 ;;; types. LVAR must be annotated for the same number of fixed values
430 ;;; are there are PTYPES.
432 ;;; If the lvar has a type check, check the values into temps and
433 ;;; return the temps. When we have more values than assertions, we
434 ;;; move the extra values with no check.
435 (defun lvar-tns (node block lvar ptypes)
436 (declare (type node node) (type ir2-block block)
437 (type lvar lvar) (list ptypes))
438 (let* ((locs (ir2-lvar-locs (lvar-info lvar)))
439 (nlocs (length locs)))
440 (aver (= nlocs (length ptypes)))
442 (mapcar (lambda (from to-type)
443 (if (eq (tn-primitive-type from) to-type)
444 from
445 (let ((temp (make-normal-tn to-type)))
446 (emit-move node block from temp)
447 temp)))
448 locs
449 ptypes)))
451 ;;;; utilities for delivering values to lvars
453 ;;; Return a list of TNs with the specifier TYPES that can be used as
454 ;;; result TNs to evaluate an expression into LVAR. This is used
455 ;;; together with MOVE-LVAR-RESULT to deliver fixed values to
456 ;;; an lvar.
458 ;;; If the lvar isn't annotated (meaning the values are discarded) or
459 ;;; is unknown-values, then we make temporaries for each supplied
460 ;;; value, providing a place to compute the result in until we decide
461 ;;; what to do with it (if anything.)
463 ;;; If the lvar is fixed-values, and wants the same number of values
464 ;;; as the user wants to deliver, then we just return the
465 ;;; IR2-LVAR-LOCS. Otherwise we make a new list padded as necessary by
466 ;;; discarded TNs. We always return a TN of the specified type, using
467 ;;; the lvar locs only when they are of the correct type.
468 (defun lvar-result-tns (lvar types)
469 (declare (type (or lvar null) lvar) (type list types))
470 (if (not lvar)
471 (mapcar #'make-normal-tn types)
472 (let ((2lvar (lvar-info lvar)))
473 (ecase (ir2-lvar-kind 2lvar)
474 (:fixed
475 (let* ((locs (ir2-lvar-locs 2lvar))
476 (nlocs (length locs))
477 (ntypes (length types)))
478 (if (and (= nlocs ntypes)
479 (do ((loc locs (cdr loc))
480 (type types (cdr type)))
481 ((null loc) t)
482 (unless (eq (tn-primitive-type (car loc)) (car type))
483 (return nil))))
484 locs
485 (mapcar (lambda (loc type)
486 (if (eq (tn-primitive-type loc) type)
488 (make-normal-tn type)))
489 (if (< nlocs ntypes)
490 (append locs
491 (mapcar #'make-normal-tn
492 (subseq types nlocs)))
493 locs)
494 types))))
495 (:unknown
496 (mapcar #'make-normal-tn types))))))
498 ;;; Make the first N standard value TNs, returning them in a list.
499 (defun make-standard-value-tns (n)
500 (declare (type unsigned-byte n))
501 (collect ((res))
502 (dotimes (i n)
503 (res (standard-arg-location i)))
504 (res)))
506 ;;; Return a list of TNs wired to the standard value passing
507 ;;; conventions that can be used to receive values according to the
508 ;;; unknown-values convention. This is used together with
509 ;;; MOVE-LVAR-RESULT for delivering unknown values to a fixed values
510 ;;; lvar.
512 ;;; If the lvar isn't annotated, then we treat as 0-values, returning
513 ;;; an empty list of temporaries.
515 ;;; If the lvar is annotated, then it must be :FIXED.
516 (defun standard-result-tns (lvar)
517 (declare (type (or lvar null) lvar))
518 (if lvar
519 (let ((2lvar (lvar-info lvar)))
520 (ecase (ir2-lvar-kind 2lvar)
521 (:fixed
522 (make-standard-value-tns (length (ir2-lvar-locs 2lvar))))))
523 nil))
525 ;;; Just move each SRC TN into the corresponding DEST TN, defaulting
526 ;;; any unsupplied source values to NIL. We let EMIT-MOVE worry about
527 ;;; doing the appropriate coercions.
528 (defun move-results-coerced (node block src dest)
529 (declare (type node node) (type ir2-block block) (list src dest))
530 (let ((nsrc (length src))
531 (ndest (length dest)))
532 (mapc (lambda (from to)
533 (unless (eq from to)
534 (emit-move node block from to)))
535 (if (> ndest nsrc)
536 (append src (make-list (- ndest nsrc)
537 :initial-element (emit-constant nil)))
538 src)
539 dest))
540 (values))
542 ;;; If necessary, emit coercion code needed to deliver the RESULTS to
543 ;;; the specified lvar. NODE and BLOCK provide context for emitting
544 ;;; code. Although usually obtained from STANDARD-RESULT-TNs or
545 ;;; LVAR-RESULT-TNs, RESULTS may be a list of any type or
546 ;;; number of TNs.
548 ;;; If the lvar is fixed values, then move the results into the lvar
549 ;;; locations. If the lvar is unknown values, then do the moves into
550 ;;; the standard value locations, and use PUSH-VALUES to put the
551 ;;; values on the stack.
552 (defun move-lvar-result (node block results lvar)
553 (declare (type node node) (type ir2-block block)
554 (list results) (type (or lvar null) lvar))
555 (when lvar
556 (let ((2lvar (lvar-info lvar)))
557 (ecase (ir2-lvar-kind 2lvar)
558 (:fixed
559 (let ((locs (ir2-lvar-locs 2lvar)))
560 (unless (eq locs results)
561 (move-results-coerced node block results locs))))
562 (:unknown
563 (let* ((nvals (length results))
564 (locs (make-standard-value-tns nvals)))
565 (move-results-coerced node block results locs)
566 (vop* push-values node block
567 ((reference-tn-list locs nil))
568 ((reference-tn-list (ir2-lvar-locs 2lvar) t))
569 nvals))))))
570 (values))
572 ;;; CAST
573 (defun ir2-convert-cast (node block)
574 (declare (type cast node)
575 (type ir2-block block))
576 (binding* ((lvar (node-lvar node) :exit-if-null)
577 (2lvar (lvar-info lvar))
578 (value (cast-value node))
579 (2value (lvar-info value)))
580 (ecase (ir2-lvar-kind 2lvar)
581 (:unused)
582 ((:unknown :fixed)
583 (aver (not (cast-type-check node)))
584 (move-results-coerced node block
585 (ir2-lvar-locs 2value)
586 (ir2-lvar-locs 2lvar))))))
588 (defoptimizer (%check-bound ir2-convert)
589 ((array bound index) node block)
590 (when (constant-lvar-p bound)
591 (let* ((bound-type (specifier-type `(integer 0 (,(lvar-value bound)))))
592 (index-type (lvar-type index)))
593 (when (eq (type-intersection bound-type index-type)
594 *empty-type*)
595 (let ((*compiler-error-context* node))
596 (compiler-warn "Derived type ~s is not a suitable index for ~s."
597 (type-specifier index-type)
598 (type-specifier (lvar-type array)))))))
599 (ir2-convert-template node block))
601 ;;;; template conversion
603 ;;; Build a TN-REFS list that represents access to the values of the
604 ;;; specified list of lvars ARGS for TEMPLATE. Any :CONSTANT arguments
605 ;;; are returned in the second value as a list rather than being
606 ;;; accessed as a normal argument. NODE and BLOCK provide the context
607 ;;; for emitting any necessary type-checking code.
608 (defun reference-args (node block args template)
609 (declare (type node node) (type ir2-block block) (list args)
610 (type template template))
611 (collect ((info-args))
612 (let ((last nil)
613 (first nil))
614 (do ((args args (cdr args))
615 (types (template-arg-types template) (cdr types)))
616 ((null args))
617 (let ((type (first types))
618 (arg (first args)))
619 (if (and (consp type) (eq (car type) ':constant))
620 (info-args (lvar-value arg))
621 (let ((ref (reference-tn (lvar-tn node block arg) nil)))
622 (if last
623 (setf (tn-ref-across last) ref)
624 (setf first ref))
625 (setq last ref)))))
627 (values (the (or tn-ref null) first) (info-args)))))
629 ;;; Convert a conditional template. We try to exploit any
630 ;;; drop-through, but emit an unconditional branch afterward if we
631 ;;; fail. NOT-P is true if the sense of the TEMPLATE's test should be
632 ;;; negated.
633 (defun ir2-convert-conditional (node block template args info-args if not-p)
634 (declare (type node node) (type ir2-block block)
635 (type template template) (type (or tn-ref null) args)
636 (list info-args) (type cif if) (type boolean not-p))
637 (let ((consequent (if-consequent if))
638 (alternative (if-alternative if))
639 (flags (and (consp (template-result-types template))
640 (rest (template-result-types template)))))
641 (aver (= (template-info-arg-count template)
642 (+ (length info-args)
643 (if flags 0 2))))
644 (when not-p
645 (rotatef consequent alternative)
646 (setf not-p nil))
647 (when (drop-thru-p if consequent)
648 (rotatef consequent alternative)
649 (setf not-p t))
650 (cond ((not flags)
651 (emit-template node block template args nil
652 (list* (block-label consequent) not-p
653 info-args))
654 (if (drop-thru-p if alternative)
655 (register-drop-thru alternative)
656 (vop branch node block (block-label alternative))))
658 (emit-template node block template args nil info-args)
659 (vop branch-if node block (block-label consequent) flags not-p)
660 (if (drop-thru-p if alternative)
661 (register-drop-thru alternative)
662 (vop branch node block (block-label alternative)))))))
664 ;;; Convert an IF that isn't the DEST of a conditional template.
665 (defun ir2-convert-if (node block)
666 (declare (type ir2-block block) (type cif node))
667 (let* ((test (if-test node))
668 (test-ref (reference-tn (lvar-tn node block test) nil))
669 (nil-ref (reference-tn (emit-constant nil) nil)))
670 (setf (tn-ref-across test-ref) nil-ref)
671 (ir2-convert-conditional node block (template-or-lose 'if-eq)
672 test-ref () node t)))
674 ;;; Return a list of primitive-types that we can pass to LVAR-RESULT-TNS
675 ;;; describing the result types we want for a template call. We are really
676 ;;; only interested in the number of results required: in normal case
677 ;;; TEMPLATE-RESULTS-OK has already checked them.
678 (defun find-template-result-types (call rtypes)
679 (let* ((type (node-derived-type call))
680 (types
681 (mapcar #'primitive-type
682 (if (args-type-p type)
683 (append (args-type-required type)
684 (args-type-optional type))
685 (list type))))
686 (primitive-t *backend-t-primitive-type*))
687 (mapcar (lambda (rtype)
688 (declare (ignore rtype))
689 (or (pop types) primitive-t)) rtypes)))
691 ;;; Return a list of TNs usable in a CALL to TEMPLATE delivering values to
692 ;;; LVAR. As an efficiency hack, we pick off the common case where the LVAR is
693 ;;; fixed values and has locations that satisfy the result restrictions. This
694 ;;; can fail when there is a type check or a values count mismatch.
695 (defun make-template-result-tns (call lvar rtypes)
696 (declare (type combination call) (type (or lvar null) lvar)
697 (list rtypes))
698 (let ((2lvar (when lvar (lvar-info lvar))))
699 (if (and 2lvar (eq (ir2-lvar-kind 2lvar) :fixed))
700 (let ((locs (ir2-lvar-locs 2lvar)))
701 (if (and (= (length rtypes) (length locs))
702 (do ((loc locs (cdr loc))
703 (rtypes rtypes (cdr rtypes)))
704 ((null loc) t)
705 (unless (operand-restriction-ok
706 (car rtypes)
707 (tn-primitive-type (car loc))
708 :t-ok nil)
709 (return nil))))
710 locs
711 (lvar-result-tns
712 lvar
713 (find-template-result-types call rtypes))))
714 (lvar-result-tns
715 lvar
716 (find-template-result-types call rtypes)))))
718 ;;; Get the operands into TNs, make TN-REFs for them, and then call
719 ;;; the template emit function.
720 (defun ir2-convert-template (call block)
721 (declare (type combination call) (type ir2-block block))
722 (let* ((template (combination-info call))
723 (lvar (node-lvar call))
724 (rtypes (template-result-types template)))
725 (multiple-value-bind (args info-args)
726 (reference-args call block (combination-args call) template)
727 (aver (not (template-more-results-type template)))
728 (if (template-conditional-p template)
729 (ir2-convert-conditional call block template args info-args
730 (lvar-dest lvar) nil)
731 (let* ((results (make-template-result-tns call lvar rtypes))
732 (r-refs (reference-tn-list results t)))
733 (aver (= (length info-args)
734 (template-info-arg-count template)))
735 (when (and lvar (lvar-dynamic-extent lvar))
736 (vop current-stack-pointer call block
737 (ir2-lvar-stack-pointer (lvar-info lvar))))
738 (when (emit-step-p call)
739 (vop sb!vm::step-instrument-before-vop call block))
740 (if info-args
741 (emit-template call block template args r-refs info-args)
742 (emit-template call block template args r-refs))
743 (move-lvar-result call block results lvar)))))
744 (values))
746 ;;; We don't have to do much because operand count checking is done by
747 ;;; IR1 conversion. The only difference between this and the function
748 ;;; case of IR2-CONVERT-TEMPLATE is that there can be codegen-info
749 ;;; arguments.
750 (defoptimizer (%%primitive ir2-convert) ((template info &rest args) call block)
751 (declare (ignore args))
752 (let* ((template (lvar-value template))
753 (info (lvar-value info))
754 (lvar (node-lvar call))
755 (rtypes (template-result-types template))
756 (results (make-template-result-tns call lvar rtypes))
757 (r-refs (reference-tn-list results t)))
758 (multiple-value-bind (args info-args)
759 (reference-args call block (cddr (combination-args call)) template)
760 (aver (not (template-more-results-type template)))
761 (aver (not (template-conditional-p template)))
762 (aver (null info-args))
764 (if info
765 (emit-template call block template args r-refs info)
766 (emit-template call block template args r-refs))
768 (move-lvar-result call block results lvar)))
769 (values))
771 (defoptimizer (%%primitive derive-type) ((template info &rest args))
772 (declare (ignore info args))
773 (let ((type (template-type (lvar-value template))))
774 (if (fun-type-p type)
775 (fun-type-returns type)
776 *wild-type*)))
778 ;;;; local call
780 ;;; Convert a LET by moving the argument values into the variables.
781 ;;; Since a LET doesn't have any passing locations, we move the
782 ;;; arguments directly into the variables. We must also allocate any
783 ;;; indirect value cells, since there is no function prologue to do
784 ;;; this.
785 (defun ir2-convert-let (node block fun)
786 (declare (type combination node) (type ir2-block block) (type clambda fun))
787 (mapc (lambda (var arg)
788 (when arg
789 (let ((src (lvar-tn node block arg))
790 (dest (leaf-info var)))
791 (if (and (lambda-var-indirect var)
792 (lambda-var-explicit-value-cell var))
793 (emit-make-value-cell node block src dest)
794 (emit-move node block src dest)))))
795 (lambda-vars fun) (basic-combination-args node))
796 (values))
798 ;;; Emit any necessary moves into assignment temps for a local call to
799 ;;; FUN. We return two lists of TNs: TNs holding the actual argument
800 ;;; values, and (possibly EQ) TNs that are the actual destination of
801 ;;; the arguments. When necessary, we allocate temporaries for
802 ;;; arguments to preserve parallel assignment semantics. These lists
803 ;;; exclude unused arguments and include implicit environment
804 ;;; arguments, i.e. they exactly correspond to the arguments passed.
806 ;;; OLD-FP is the TN currently holding the value we want to pass as
807 ;;; OLD-FP. If null, then the call is to the same environment (an
808 ;;; :ASSIGNMENT), so we only move the arguments, and leave the
809 ;;; environment alone.
811 ;;; CLOSURE-FP is for calling a closure that has "implicit" value
812 ;;; cells (stored in the allocating stack frame), and is the frame
813 ;;; pointer TN to use for values allocated in the outbound stack
814 ;;; frame. This is distinct from OLD-FP for the specific case of a
815 ;;; tail-local-call.
816 (defun emit-psetq-moves (node block fun old-fp &optional (closure-fp old-fp))
817 (declare (type combination node) (type ir2-block block) (type clambda fun)
818 (type (or tn null) old-fp closure-fp))
819 (let ((actuals (mapcar (lambda (x)
820 (when x
821 (lvar-tn node block x)))
822 (combination-args node))))
823 (collect ((temps)
824 (locs))
825 (dolist (var (lambda-vars fun))
826 (let ((actual (pop actuals))
827 (loc (leaf-info var)))
828 (when actual
829 (cond
830 ((and (lambda-var-indirect var)
831 (lambda-var-explicit-value-cell var))
832 (let ((temp
833 (make-normal-tn *backend-t-primitive-type*)))
834 (emit-make-value-cell node block actual temp)
835 (temps temp)))
836 ((member actual (locs))
837 (let ((temp (make-normal-tn (tn-primitive-type loc))))
838 (emit-move node block actual temp)
839 (temps temp)))
841 (temps actual)))
842 (locs loc))))
844 (when old-fp
845 (let ((this-1env (node-physenv node))
846 (called-env (physenv-info (lambda-physenv fun))))
847 (dolist (thing (ir2-physenv-closure called-env))
848 (temps (closure-initial-value (car thing) this-1env closure-fp))
849 (locs (cdr thing)))
850 (temps old-fp)
851 (locs (ir2-physenv-old-fp called-env))))
853 (values (temps) (locs)))))
855 ;;; A tail-recursive local call is done by emitting moves of stuff
856 ;;; into the appropriate passing locations. After setting up the args
857 ;;; and environment, we just move our return-pc into the called
858 ;;; function's passing location.
859 (defun ir2-convert-tail-local-call (node block fun)
860 (declare (type combination node) (type ir2-block block) (type clambda fun))
861 (let ((this-env (physenv-info (node-physenv node)))
862 (current-fp (make-stack-pointer-tn)))
863 (multiple-value-bind (temps locs)
864 (emit-psetq-moves node block fun
865 (ir2-physenv-old-fp this-env) current-fp)
867 ;; If we're about to emit a move from CURRENT-FP then we need to
868 ;; initialize it.
869 (when (find current-fp temps)
870 (vop current-fp node block current-fp))
872 (mapc (lambda (temp loc)
873 (emit-move node block temp loc))
874 temps locs))
876 (emit-move node block
877 (ir2-physenv-return-pc this-env)
878 (ir2-physenv-return-pc-pass
879 (physenv-info
880 (lambda-physenv fun)))))
882 (values))
884 ;;; Convert an :ASSIGNMENT call. This is just like a tail local call,
885 ;;; except that the caller and callee environment are the same, so we
886 ;;; don't need to mess with the environment locations, return PC, etc.
887 (defun ir2-convert-assignment (node block fun)
888 (declare (type combination node) (type ir2-block block) (type clambda fun))
889 (multiple-value-bind (temps locs) (emit-psetq-moves node block fun nil)
891 (mapc (lambda (temp loc)
892 (emit-move node block temp loc))
893 temps locs))
894 (values))
896 ;;; Do stuff to set up the arguments to a non-tail local call
897 ;;; (including implicit environment args.) We allocate a frame
898 ;;; (returning the FP and NFP), and also compute the TN-REFS list for
899 ;;; the values to pass and the list of passing location TNs.
900 (defun ir2-convert-local-call-args (node block fun)
901 (declare (type combination node) (type ir2-block block) (type clambda fun))
902 (let ((fp (make-stack-pointer-tn))
903 (nfp (make-number-stack-pointer-tn))
904 (old-fp (make-stack-pointer-tn)))
905 (multiple-value-bind (temps locs)
906 (emit-psetq-moves node block fun old-fp)
907 (vop current-fp node block old-fp)
908 (vop allocate-frame node block
909 (physenv-info (lambda-physenv fun))
910 fp nfp)
911 (values fp nfp temps (mapcar #'make-alias-tn locs)))))
913 ;;; Handle a non-TR known-values local call. We emit the call, then
914 ;;; move the results to the lvar's destination.
915 (defun ir2-convert-local-known-call (node block fun returns lvar start)
916 (declare (type node node) (type ir2-block block) (type clambda fun)
917 (type return-info returns) (type (or lvar null) lvar)
918 (type label start))
919 (multiple-value-bind (fp nfp temps arg-locs)
920 (ir2-convert-local-call-args node block fun)
921 (let ((locs (return-info-locations returns)))
922 (vop* known-call-local node block
923 (fp nfp (reference-tn-list temps nil))
924 ((reference-tn-list locs t))
925 arg-locs (physenv-info (lambda-physenv fun)) start)
926 (move-lvar-result node block locs lvar)))
927 (values))
929 ;;; Handle a non-TR unknown-values local call. We do different things
930 ;;; depending on what kind of values the lvar wants.
932 ;;; If LVAR is :UNKNOWN, then we use the "multiple-" variant, directly
933 ;;; specifying the lvar's LOCS as the VOP results so that we don't
934 ;;; have to do anything after the call.
936 ;;; Otherwise, we use STANDARD-RESULT-TNS to get wired result TNs, and
937 ;;; then call MOVE-LVAR-RESULT to do any necessary type checks or
938 ;;; coercions.
939 (defun ir2-convert-local-unknown-call (node block fun lvar start)
940 (declare (type node node) (type ir2-block block) (type clambda fun)
941 (type (or lvar null) lvar) (type label start))
942 (multiple-value-bind (fp nfp temps arg-locs)
943 (ir2-convert-local-call-args node block fun)
944 (let ((2lvar (and lvar (lvar-info lvar)))
945 (env (physenv-info (lambda-physenv fun)))
946 (temp-refs (reference-tn-list temps nil)))
947 (if (and 2lvar (eq (ir2-lvar-kind 2lvar) :unknown))
948 (vop* multiple-call-local node block (fp nfp temp-refs)
949 ((reference-tn-list (ir2-lvar-locs 2lvar) t))
950 arg-locs env start)
951 (let ((locs (standard-result-tns lvar)))
952 (vop* call-local node block
953 (fp nfp temp-refs)
954 ((reference-tn-list locs t))
955 arg-locs env start (length locs))
956 (move-lvar-result node block locs lvar)))))
957 (values))
959 ;;; Dispatch to the appropriate function, depending on whether we have
960 ;;; a let, tail or normal call. If the function doesn't return, call
961 ;;; it using the unknown-value convention. We could compile it as a
962 ;;; tail call, but that might seem confusing in the debugger.
963 (defun ir2-convert-local-call (node block)
964 (declare (type combination node) (type ir2-block block))
965 (let* ((fun (ref-leaf (lvar-uses (basic-combination-fun node))))
966 (kind (functional-kind fun)))
967 (cond ((eq kind :let)
968 (ir2-convert-let node block fun))
969 ((eq kind :assignment)
970 (ir2-convert-assignment node block fun))
971 ((node-tail-p node)
972 (ir2-convert-tail-local-call node block fun))
974 (let ((start (block-trampoline (lambda-block fun)))
975 (returns (tail-set-info (lambda-tail-set fun)))
976 (lvar (node-lvar node)))
977 (ecase (if returns
978 (return-info-kind returns)
979 :unknown)
980 (:unknown
981 (ir2-convert-local-unknown-call node block fun lvar start))
982 (:fixed
983 (ir2-convert-local-known-call node block fun returns
984 lvar start)))))))
985 (values))
987 ;;;; full call
989 ;;; Given a function lvar FUN, return (VALUES TN-TO-CALL NAMED-P),
990 ;;; where TN-TO-CALL is a TN holding the thing that we call NAMED-P is
991 ;;; true if the thing is named (false if it is a function).
993 ;;; There are two interesting non-named cases:
994 ;;; -- We know it's a function. No check needed: return the
995 ;;; lvar LOC.
996 ;;; -- We don't know what it is.
997 (defun fun-lvar-tn (node block lvar)
998 (declare (ignore node block))
999 (declare (type lvar lvar))
1000 (let ((2lvar (lvar-info lvar)))
1001 (if (eq (ir2-lvar-kind 2lvar) :delayed)
1002 (let ((name (lvar-fun-name lvar t)))
1003 (aver name)
1004 (values (make-load-time-constant-tn :fdefinition name) t))
1005 (let* ((locs (ir2-lvar-locs 2lvar))
1006 (loc (first locs))
1007 (function-ptype (primitive-type-or-lose 'function)))
1008 (aver (and (eq (ir2-lvar-kind 2lvar) :fixed)
1009 (= (length locs) 1)))
1010 (aver (eq (tn-primitive-type loc) function-ptype))
1011 (values loc nil)))))
1013 ;;; Set up the args to NODE in the current frame, and return a TN-REF
1014 ;;; list for the passing locations.
1015 (defun move-tail-full-call-args (node block)
1016 (declare (type combination node) (type ir2-block block))
1017 (let ((args (basic-combination-args node))
1018 (last nil)
1019 (first nil))
1020 (dotimes (num (length args))
1021 (let ((loc (standard-arg-location num)))
1022 (emit-move node block (lvar-tn node block (elt args num)) loc)
1023 (let ((ref (reference-tn loc nil)))
1024 (if last
1025 (setf (tn-ref-across last) ref)
1026 (setf first ref))
1027 (setq last ref))))
1028 first))
1030 ;;; Move the arguments into the passing locations and do a (possibly
1031 ;;; named) tail call.
1032 (defun ir2-convert-tail-full-call (node block)
1033 (declare (type combination node) (type ir2-block block))
1034 (let* ((env (physenv-info (node-physenv node)))
1035 (args (basic-combination-args node))
1036 (nargs (length args))
1037 (pass-refs (move-tail-full-call-args node block))
1038 (old-fp (ir2-physenv-old-fp env))
1039 (return-pc (ir2-physenv-return-pc env)))
1041 (multiple-value-bind (fun-tn named)
1042 (fun-lvar-tn node block (basic-combination-fun node))
1043 (if named
1044 (vop* tail-call-named node block
1045 (fun-tn old-fp return-pc pass-refs)
1046 (nil)
1047 nargs
1048 (emit-step-p node))
1049 (vop* tail-call node block
1050 (fun-tn old-fp return-pc pass-refs)
1051 (nil)
1052 nargs
1053 (emit-step-p node)))))
1055 (values))
1057 ;;; like IR2-CONVERT-LOCAL-CALL-ARGS, only different
1058 (defun ir2-convert-full-call-args (node block)
1059 (declare (type combination node) (type ir2-block block))
1060 (let* ((args (basic-combination-args node))
1061 (fp (make-stack-pointer-tn))
1062 (nargs (length args)))
1063 (vop allocate-full-call-frame node block nargs fp)
1064 (collect ((locs))
1065 (let ((last nil)
1066 (first nil))
1067 (dotimes (num nargs)
1068 (locs (standard-arg-location num))
1069 (let ((ref (reference-tn (lvar-tn node block (elt args num))
1070 nil)))
1071 (if last
1072 (setf (tn-ref-across last) ref)
1073 (setf first ref))
1074 (setq last ref)))
1076 (values fp first (locs) nargs)))))
1078 ;;; Do full call when a fixed number of values are desired. We make
1079 ;;; STANDARD-RESULT-TNS for our lvar, then deliver the result using
1080 ;;; MOVE-LVAR-RESULT. We do named or normal call, as appropriate.
1081 (defun ir2-convert-fixed-full-call (node block)
1082 (declare (type combination node) (type ir2-block block))
1083 (multiple-value-bind (fp args arg-locs nargs)
1084 (ir2-convert-full-call-args node block)
1085 (let* ((lvar (node-lvar node))
1086 (locs (standard-result-tns lvar))
1087 (loc-refs (reference-tn-list locs t))
1088 (nvals (length locs)))
1089 (multiple-value-bind (fun-tn named)
1090 (fun-lvar-tn node block (basic-combination-fun node))
1091 (if named
1092 (vop* call-named node block (fp fun-tn args) (loc-refs)
1093 arg-locs nargs nvals (emit-step-p node))
1094 (vop* call node block (fp fun-tn args) (loc-refs)
1095 arg-locs nargs nvals (emit-step-p node)))
1096 (move-lvar-result node block locs lvar))))
1097 (values))
1099 ;;; Do full call when unknown values are desired.
1100 (defun ir2-convert-multiple-full-call (node block)
1101 (declare (type combination node) (type ir2-block block))
1102 (multiple-value-bind (fp args arg-locs nargs)
1103 (ir2-convert-full-call-args node block)
1104 (let* ((lvar (node-lvar node))
1105 (locs (ir2-lvar-locs (lvar-info lvar)))
1106 (loc-refs (reference-tn-list locs t)))
1107 (multiple-value-bind (fun-tn named)
1108 (fun-lvar-tn node block (basic-combination-fun node))
1109 (if named
1110 (vop* multiple-call-named node block (fp fun-tn args) (loc-refs)
1111 arg-locs nargs (emit-step-p node))
1112 (vop* multiple-call node block (fp fun-tn args) (loc-refs)
1113 arg-locs nargs (emit-step-p node))))))
1114 (values))
1116 ;;; stuff to check in PONDER-FULL-CALL
1118 ;;; These came in handy when troubleshooting cold boot after making
1119 ;;; major changes in the package structure: various transforms and
1120 ;;; VOPs and stuff got attached to the wrong symbol, so that
1121 ;;; references to the right symbol were bogusly translated as full
1122 ;;; calls instead of primitives, sending the system off into infinite
1123 ;;; space. Having a report on all full calls generated makes it easier
1124 ;;; to figure out what form caused the problem this time.
1125 (declaim (type (member :minimal :detailed :very-detailed :maximal)
1126 *track-full-called-fnames*))
1127 (defvar *track-full-called-fnames* :minimal)
1129 ;;; Do some checks (and store some notes relevant for future checks)
1130 ;;; on a full call:
1131 ;;; * Is this a full call to something we have reason to know should
1132 ;;; never be full called? (Except as of sbcl-0.7.18 or so, we no
1133 ;;; longer try to ensure this behavior when *FAILURE-P* has already
1134 ;;; been detected.)
1135 (defun ponder-full-call (node)
1136 (let* ((lvar (basic-combination-fun node))
1137 (fname (lvar-fun-name lvar t)))
1138 (declare (type (or symbol cons) fname))
1140 ;; Warn about cross-compiling certain full-calls,
1141 ;; as it is indicative of dependency order problems.
1142 #+sb-xc-host
1143 (let ((compname (component-name (node-component node))))
1144 ;; Don't care too much about macro performance.
1145 (unless (and (stringp compname) (string/= compname "DEFMACRO"))
1146 ;; Catch FOO and (SETF FOO) both.
1147 (let ((stem (if (atom fname) fname (second fname))))
1148 (when (member stem
1149 sb-cold::*full-calls-to-warn-about*
1150 :test #'string=)
1151 (warn "Full call to ~S" fname)))))
1153 (let* ((inlineable-p (not (let ((*lexenv* (node-lexenv node)))
1154 (fun-lexically-notinline-p fname))))
1155 (inlineable-bit (if inlineable-p 1 0))
1156 (cell (info :function :emitted-full-calls fname)))
1157 (if (not cell)
1158 ;; The low bit indicates whether any not-NOTINLINE call was seen.
1159 ;; The next-lowest bit is magic. Refer to %COMPILER-DEFMACRO
1160 ;; and WARN-IF-INLINE-FAILED/CALL for the pertinent logic.
1161 (setf cell (list (logior 4 inlineable-bit))
1162 (info :function :emitted-full-calls fname) cell)
1163 (incf (car cell) (+ 4 (if (oddp (car cell)) 0 inlineable-bit))))
1164 ;; If the full call was wanted, don't record anything.
1165 ;; (This was originally for debugging SBCL self-compilation)
1166 (when inlineable-p
1167 (unless *failure-p*
1168 (warn-if-inline-failed/call fname (node-lexenv node) cell))
1169 (case *track-full-called-fnames*
1170 (:detailed
1171 (when (boundp 'sb!xc:*compile-file-pathname*)
1172 (pushnew sb!xc:*compile-file-pathname* (cdr cell)
1173 :test #'equal)))
1174 (:very-detailed
1175 (pushnew (component-name *component-being-compiled*)
1176 (cdr cell) :test #'equalp)))))
1178 ;; Special mode, usually only for the cross-compiler
1179 ;; and only with the feature enabled.
1180 #!+sb-show (when (eq *track-full-called-fnames* :maximal)
1181 (/show "converting full call to named function" fname)
1182 (/show (basic-combination-args node))
1183 (/show (policy node speed) (policy node safety))
1184 (/show (policy node compilation-speed))
1185 (let ((arg-types (mapcar (lambda (lvar)
1186 (when lvar
1187 (type-specifier
1188 (lvar-type lvar))))
1189 (basic-combination-args node))))
1190 (/show arg-types)))
1192 ;; When illegal code is compiled, all sorts of perverse paths
1193 ;; through the compiler can be taken, and it's much harder -- and
1194 ;; probably pointless -- to guarantee that always-optimized-away
1195 ;; functions are actually optimized away. Thus, we skip the check
1196 ;; in that case.
1197 (unless *failure-p*
1198 ;; check to see if we know anything about the function
1199 (let ((info (info :function :info fname)))
1200 ;; if we know something, check to see if the full call was valid
1201 (when (and info (ir1-attributep (fun-info-attributes info)
1202 always-translatable))
1203 (/show (policy node speed) (policy node safety))
1204 (/show (policy node compilation-speed))
1205 (bug "full call to ~S" fname))))
1207 (when (consp fname)
1208 (aver (legal-fun-name-p fname))))) ;; FIXME: needless check?
1210 ;;; If the call is in a tail recursive position and the return
1211 ;;; convention is standard, then do a tail full call. If one or fewer
1212 ;;; values are desired, then use a single-value call, otherwise use a
1213 ;;; multiple-values call.
1214 (defun ir2-convert-full-call (node block)
1215 (declare (type combination node) (type ir2-block block))
1216 (ponder-full-call node)
1217 (cond ((node-tail-p node)
1218 (ir2-convert-tail-full-call node block))
1219 ((let ((lvar (node-lvar node)))
1220 (and lvar
1221 (eq (ir2-lvar-kind (lvar-info lvar)) :unknown)))
1222 (ir2-convert-multiple-full-call node block))
1224 (ir2-convert-fixed-full-call node block)))
1225 (values))
1227 ;;;; entering functions
1229 #!+precise-arg-count-error
1230 (defun xep-verify-arg-count (node block fun arg-count-location)
1231 (when (policy fun (plusp verify-arg-count))
1232 (let* ((ef (functional-entry-fun fun))
1233 (optional (optional-dispatch-p ef))
1234 (min (and optional
1235 (optional-dispatch-min-args ef)))
1236 (max (cond ((not optional)
1237 (1- (length (lambda-vars fun))))
1238 ((and optional
1239 (not (optional-dispatch-more-entry ef)))
1240 (optional-dispatch-max-args ef)))))
1241 (unless (and (eql min 0) (not max))
1242 (vop verify-arg-count node block
1243 arg-count-location
1245 max)))))
1247 ;;; Do all the stuff that needs to be done on XEP entry:
1248 ;;; -- Create frame.
1249 ;;; -- Copy any more arg.
1250 ;;; -- Set up the environment, accessing any closure variables.
1251 ;;; -- Move args from the standard passing locations to their internal
1252 ;;; locations.
1253 (defun init-xep-environment (node block fun)
1254 (declare (type bind node) (type ir2-block block) (type clambda fun))
1255 (let ((start-label (entry-info-offset (leaf-info fun)))
1256 (env (physenv-info (node-physenv node)))
1257 arg-count-tn)
1258 (let ((ef (functional-entry-fun fun)))
1259 (vop xep-allocate-frame node block start-label)
1260 ;; Arg verification needs to be done before the stack pointer is adjusted
1261 ;; so that the extra arguments are still present when the error is signalled
1262 (unless (eq (functional-kind fun) :toplevel)
1263 (setf arg-count-tn (make-arg-count-location))
1264 #!+precise-arg-count-error
1265 (xep-verify-arg-count node block fun arg-count-tn))
1266 (cond ((and (optional-dispatch-p ef) (optional-dispatch-more-entry ef))
1267 ;; COPY-MORE-ARG should handle SP adjustemnt, but it
1268 ;; isn't done on all targets.
1269 #!-precise-arg-count-error
1270 (vop xep-setup-sp node block)
1271 (vop copy-more-arg node block (optional-dispatch-max-args ef)))
1273 (vop xep-setup-sp node block)))
1274 (when (ir2-physenv-closure env)
1275 (let ((closure (make-normal-tn *backend-t-primitive-type*)))
1276 (when (policy fun (> store-closure-debug-pointer 1))
1277 ;; Save the closure pointer on the stack.
1278 (let ((closure-save (make-representation-tn
1279 *backend-t-primitive-type*
1280 (sc-number-or-lose 'sb!vm::control-stack))))
1281 (vop setup-closure-environment node block start-label
1282 closure-save)
1283 (setf (ir2-physenv-closure-save-tn env) closure-save)
1284 (component-live-tn closure-save)))
1285 (vop setup-closure-environment node block start-label closure)
1286 (let ((n -1))
1287 (dolist (loc (ir2-physenv-closure env))
1288 (vop closure-ref node block closure (incf n) (cdr loc)))))))
1289 (unless (eq (functional-kind fun) :toplevel)
1290 (let ((vars (lambda-vars fun))
1291 (n 0))
1292 (when (leaf-refs (first vars))
1293 (emit-move node block arg-count-tn (leaf-info (first vars))))
1294 (dolist (arg (rest vars))
1295 (when (leaf-refs arg)
1296 (let ((pass (standard-arg-location n))
1297 (home (leaf-info arg)))
1298 (if (and (lambda-var-indirect arg)
1299 (lambda-var-explicit-value-cell arg))
1300 (emit-make-value-cell node block pass home)
1301 (emit-move node block pass home))))
1302 (incf n))))
1304 (emit-move node block (make-old-fp-passing-location t)
1305 (ir2-physenv-old-fp env)))
1307 (values))
1309 ;;; Emit function prolog code. This is only called on bind nodes for
1310 ;;; functions that allocate environments. All semantics of let calls
1311 ;;; are handled by IR2-CONVERT-LET.
1313 ;;; If not an XEP, all we do is move the return PC from its passing
1314 ;;; location, since in a local call, the caller allocates the frame
1315 ;;; and sets up the arguments.
1317 #!+unwind-to-frame-and-call-vop
1318 (defun save-bsp (node block env)
1319 ;; Save BSP on stack so that the binding environment can be restored
1320 ;; when restarting frames.
1321 ;; This is done inside functions, which leaves XEPs without saved
1322 ;; BSP, though the code in XEPs doesn't bind any variables, it can
1323 ;; call arbitrary code through the SATISFIES declaration.
1324 ;; And functions called by SATISFIES are not inlined, except for
1325 ;; source transforms, but these usually do not bind anything.
1326 ;; Thus when restarting it needs to check that the interrupt was in
1327 ;; the XEP itself.
1329 ;; It could be saved from the XEP, but some functions have both
1330 ;; external and internal entry points, so it will be saved twice.
1331 (let ((temp (make-normal-tn *backend-t-primitive-type*))
1332 (bsp-save-tn (make-representation-tn
1333 *backend-t-primitive-type*
1334 (sc-number-or-lose 'sb!vm::control-stack))))
1335 (vop current-binding-pointer node block temp)
1336 (emit-move node block temp bsp-save-tn)
1337 (setf (ir2-physenv-bsp-save-tn env) bsp-save-tn)
1338 (component-live-tn bsp-save-tn)))
1340 (defun ir2-convert-bind (node block)
1341 (declare (type bind node) (type ir2-block block))
1342 (let* ((fun (bind-lambda node))
1343 (env (physenv-info (lambda-physenv fun))))
1344 (aver (member (functional-kind fun)
1345 '(nil :external :optional :toplevel :cleanup)))
1347 (cond ((xep-p fun)
1348 (init-xep-environment node block fun)
1349 #!+sb-dyncount
1350 (when *collect-dynamic-statistics*
1351 (vop count-me node block *dynamic-counts-tn*
1352 (block-number (ir2-block-block block)))))
1353 ((policy fun (> store-closure-debug-pointer 1))
1354 ;; Propagate the location of the closure pointer from the
1355 ;; enclosing functions. (FIXME: Should make sure that this
1356 ;; handles closures inside closures correctly). [remark by JES]
1357 (let* ((entry-fun (lambda-entry-fun fun)))
1358 (when entry-fun
1359 (let ((2env (physenv-info (lambda-physenv fun)))
1360 (entry-2env (physenv-info (lambda-physenv entry-fun))))
1361 (setf (ir2-physenv-closure-save-tn 2env)
1362 (ir2-physenv-closure-save-tn entry-2env)))))))
1364 (emit-move node
1365 block
1366 (ir2-physenv-return-pc-pass env)
1367 (ir2-physenv-return-pc env))
1368 #!+unwind-to-frame-and-call-vop
1369 (when (and (lambda-allow-instrumenting fun)
1370 (not (lambda-inline-expanded fun))
1371 (policy fun (>= insert-debug-catch 1)))
1372 (save-bsp node block env))
1374 (let ((lab (gen-label)))
1375 (setf (ir2-physenv-environment-start env) lab)
1376 (vop note-environment-start node block lab)
1377 #!+sb-safepoint
1378 (unless (policy fun (>= inhibit-safepoints 2))
1379 (vop sb!vm::insert-safepoint node block))))
1381 (values))
1383 ;;;; function return
1385 ;;; Do stuff to return from a function with the specified values and
1386 ;;; convention. If the return convention is :FIXED and we aren't
1387 ;;; returning from an XEP, then we do a known return (letting
1388 ;;; representation selection insert the correct move-arg VOPs.)
1389 ;;; Otherwise, we use the unknown-values convention. If there is a
1390 ;;; fixed number of return values, then use RETURN, otherwise use
1391 ;;; RETURN-MULTIPLE.
1392 (defun ir2-convert-return (node block)
1393 (declare (type creturn node) (type ir2-block block))
1394 (let* ((lvar (return-result node))
1395 (2lvar (lvar-info lvar))
1396 (lvar-kind (ir2-lvar-kind 2lvar))
1397 (fun (return-lambda node))
1398 (env (physenv-info (lambda-physenv fun)))
1399 (old-fp (ir2-physenv-old-fp env))
1400 (return-pc (ir2-physenv-return-pc env))
1401 (returns (tail-set-info (lambda-tail-set fun))))
1402 (cond
1403 ((and (eq (return-info-kind returns) :fixed)
1404 (not (xep-p fun)))
1405 (let ((locs (lvar-tns node block lvar
1406 (return-info-types returns))))
1407 (vop* known-return node block
1408 (old-fp return-pc (reference-tn-list locs nil))
1409 (nil)
1410 (return-info-locations returns))))
1411 ((eq lvar-kind :fixed)
1412 (let* ((types (mapcar #'tn-primitive-type (ir2-lvar-locs 2lvar)))
1413 (lvar-locs (lvar-tns node block lvar types))
1414 (nvals (length lvar-locs))
1415 (locs (make-standard-value-tns nvals)))
1416 (mapc (lambda (val loc)
1417 (emit-move node block val loc))
1418 lvar-locs
1419 locs)
1420 (if (= nvals 1)
1421 (vop return-single node block old-fp return-pc (car locs))
1422 (vop* return node block
1423 (old-fp return-pc (reference-tn-list locs nil))
1424 (nil)
1425 nvals))))
1427 (aver (eq lvar-kind :unknown))
1428 (vop* return-multiple node block
1429 (old-fp return-pc
1430 (reference-tn-list (ir2-lvar-locs 2lvar) nil))
1431 (nil)))))
1433 (values))
1435 ;;;; debugger hooks
1436 ;;;;
1437 ;;;; These are used by the debugger to find the top function on the
1438 ;;;; stack. They return the OLD-FP and RETURN-PC for the current
1439 ;;;; function as multiple values.
1441 (defoptimizer (%caller-frame ir2-convert) (() node block)
1442 (let ((ir2-physenv (physenv-info (node-physenv node))))
1443 (move-lvar-result node block
1444 (list (ir2-physenv-old-fp ir2-physenv))
1445 (node-lvar node))))
1447 (defoptimizer (%caller-pc ir2-convert) (() node block)
1448 (let ((ir2-physenv (physenv-info (node-physenv node))))
1449 (move-lvar-result node block
1450 (list (ir2-physenv-return-pc ir2-physenv))
1451 (node-lvar node))))
1453 ;;;; multiple values
1455 ;;; This is almost identical to IR2-CONVERT-LET. Since LTN annotates
1456 ;;; the lvar for the correct number of values (with the lvar user
1457 ;;; responsible for defaulting), we can just pick them up from the
1458 ;;; lvar.
1459 (defun ir2-convert-mv-bind (node block)
1460 (declare (type mv-combination node) (type ir2-block block))
1461 (let* ((lvar (first (basic-combination-args node)))
1462 (fun (ref-leaf (lvar-uses (basic-combination-fun node))))
1463 (vars (lambda-vars fun)))
1464 (aver (eq (functional-kind fun) :mv-let))
1465 (mapc (lambda (src var)
1466 (when (leaf-refs var)
1467 (let ((dest (leaf-info var)))
1468 (if (and (lambda-var-indirect var)
1469 (lambda-var-explicit-value-cell var))
1470 (emit-make-value-cell node block src dest)
1471 (emit-move node block src dest)))))
1472 (lvar-tns node block lvar
1473 (mapcar (lambda (x)
1474 (primitive-type (leaf-type x)))
1475 vars))
1476 vars))
1477 (values))
1479 ;;; Emit the appropriate fixed value, unknown value or tail variant of
1480 ;;; CALL-VARIABLE. Note that we only need to pass the values start for
1481 ;;; the first argument: all the other argument lvar TNs are
1482 ;;; ignored. This is because we require all of the values globs to be
1483 ;;; contiguous and on stack top.
1484 (defun ir2-convert-mv-call (node block)
1485 (declare (type mv-combination node) (type ir2-block block))
1486 (aver (basic-combination-args node))
1487 (let* ((start-lvar (lvar-info (first (basic-combination-args node))))
1488 (start (first (ir2-lvar-locs start-lvar)))
1489 (tails (and (node-tail-p node)
1490 (lambda-tail-set (node-home-lambda node))))
1491 (lvar (node-lvar node))
1492 (2lvar (and lvar (lvar-info lvar))))
1493 (multiple-value-bind (fun named)
1494 (fun-lvar-tn node block (basic-combination-fun node))
1495 (aver (and (not named)
1496 (eq (ir2-lvar-kind start-lvar) :unknown)))
1497 (cond
1498 (tails
1499 (let ((env (physenv-info (node-physenv node))))
1500 (vop tail-call-variable node block start fun
1501 (ir2-physenv-old-fp env)
1502 (ir2-physenv-return-pc env))))
1503 ((and 2lvar
1504 (eq (ir2-lvar-kind 2lvar) :unknown))
1505 (vop* multiple-call-variable node block (start fun nil)
1506 ((reference-tn-list (ir2-lvar-locs 2lvar) t))
1507 (emit-step-p node)))
1509 (let ((locs (standard-result-tns lvar)))
1510 (vop* call-variable node block (start fun nil)
1511 ((reference-tn-list locs t)) (length locs)
1512 (emit-step-p node))
1513 (move-lvar-result node block locs lvar)))))))
1515 ;;; Reset the stack pointer to the start of the specified
1516 ;;; unknown-values lvar (discarding it and all values globs on top of
1517 ;;; it.)
1518 (defoptimizer (%pop-values ir2-convert) ((%lvar) node block)
1519 (let* ((lvar (lvar-value %lvar))
1520 (2lvar (lvar-info lvar)))
1521 (cond ((eq (ir2-lvar-kind 2lvar) :unknown)
1522 (vop reset-stack-pointer node block
1523 (first (ir2-lvar-locs 2lvar))))
1524 ((lvar-dynamic-extent lvar)
1525 (vop reset-stack-pointer node block
1526 (ir2-lvar-stack-pointer 2lvar)))
1527 (t (bug "Trying to pop a not stack-allocated LVAR ~S."
1528 lvar)))))
1530 (defoptimizer (%nip-values ir2-convert) ((last-nipped last-preserved
1531 &rest moved)
1532 node block)
1533 (let* ( ;; pointer immediately after the nipped block
1534 (after (lvar-value last-nipped))
1535 (2after (lvar-info after))
1536 ;; pointer to the first nipped word
1537 (first (lvar-value last-preserved))
1538 (2first (lvar-info first))
1540 (moved-tns (loop for lvar-ref in moved
1541 for lvar = (lvar-value lvar-ref)
1542 for 2lvar = (lvar-info lvar)
1543 ;when 2lvar
1544 collect (first (ir2-lvar-locs 2lvar)))))
1545 (aver (or (eq (ir2-lvar-kind 2after) :unknown)
1546 (lvar-dynamic-extent after)))
1547 (aver (eq (ir2-lvar-kind 2first) :unknown))
1548 (when *check-consistency*
1549 ;; we cannot move stack-allocated DX objects
1550 (dolist (moved-lvar moved)
1551 (aver (eq (ir2-lvar-kind (lvar-info (lvar-value moved-lvar)))
1552 :unknown))))
1553 (flet ((nip-aligned (nipped)
1554 (vop* %%nip-values node block
1555 (nipped
1556 (first (ir2-lvar-locs 2first))
1557 (reference-tn-list moved-tns nil))
1558 ((reference-tn-list moved-tns t)))))
1559 (cond ((eq (ir2-lvar-kind 2after) :unknown)
1560 (nip-aligned (first (ir2-lvar-locs 2after))))
1561 ((lvar-dynamic-extent after)
1562 (nip-aligned (ir2-lvar-stack-pointer 2after)))
1564 (bug "Trying to nip a not stack-allocated LVAR ~S." after))))))
1566 (defoptimizer (%dummy-dx-alloc ir2-convert) ((target source) node block)
1567 (let* ((target-lvar (lvar-value target))
1568 (source-lvar (lvar-value source))
1569 (target-2lvar (lvar-info target-lvar))
1570 (source-2lvar (and source-lvar (lvar-info source-lvar))))
1571 (aver (lvar-dynamic-extent target-lvar))
1572 (cond ((not source-lvar)
1573 (vop current-stack-pointer node block
1574 (ir2-lvar-stack-pointer target-2lvar)))
1575 ((lvar-dynamic-extent source-lvar)
1576 (emit-move node block
1577 (ir2-lvar-stack-pointer source-2lvar)
1578 (ir2-lvar-stack-pointer target-2lvar)))
1579 ((eq (ir2-lvar-kind source-2lvar) :unknown)
1580 (emit-move node block
1581 (first (ir2-lvar-locs source-2lvar))
1582 (ir2-lvar-stack-pointer target-2lvar)))
1583 (t (bug "Trying to dummy up DX allocation from a ~
1584 not stack-allocated LVAR ~S." source-lvar)))))
1586 ;;; Deliver the values TNs to LVAR using MOVE-LVAR-RESULT.
1587 (defoptimizer (values ir2-convert) ((&rest values) node block)
1588 (let ((tns (mapcar (lambda (x)
1589 (lvar-tn node block x))
1590 values)))
1591 (move-lvar-result node block tns (node-lvar node))))
1593 ;;; In the normal case where unknown values are desired, we use the
1594 ;;; VALUES-LIST VOP. In the relatively unimportant case of VALUES-LIST
1595 ;;; for a fixed number of values, we punt by doing a full call to the
1596 ;;; VALUES-LIST function. This gets the full call VOP to deal with
1597 ;;; defaulting any unsupplied values. It seems unworthwhile to
1598 ;;; optimize this case.
1599 (defoptimizer (values-list ir2-convert) ((list) node block)
1600 (let* ((lvar (node-lvar node))
1601 (2lvar (and lvar (lvar-info lvar))))
1602 (cond ((and 2lvar
1603 (eq (ir2-lvar-kind 2lvar) :unknown))
1604 (let ((locs (ir2-lvar-locs 2lvar)))
1605 (vop* values-list node block
1606 ((lvar-tn node block list) nil)
1607 ((reference-tn-list locs t)))))
1608 (t (aver (or (not 2lvar) ; i.e. we want to check the argument
1609 (eq (ir2-lvar-kind 2lvar) :fixed)))
1610 (ir2-convert-full-call node block)))))
1612 (defoptimizer (%more-arg-values ir2-convert) ((context start count) node block)
1613 (binding* ((lvar (node-lvar node) :exit-if-null)
1614 (2lvar (lvar-info lvar)))
1615 (ecase (ir2-lvar-kind 2lvar)
1616 (:fixed
1617 ;; KLUDGE: this is very much unsafe, and can leak random stack values.
1618 ;; OTOH, I think the :FIXED case can only happen with (safety 0) in the
1619 ;; first place.
1620 ;; -PK
1621 (loop for loc in (ir2-lvar-locs 2lvar)
1622 for idx upfrom 0
1623 do (vop sb!vm::more-arg node block
1624 (lvar-tn node block context)
1625 (emit-constant idx)
1626 loc)))
1627 (:unknown
1628 (let ((locs (ir2-lvar-locs 2lvar)))
1629 (vop* %more-arg-values node block
1630 ((lvar-tn node block context)
1631 (lvar-tn node block start)
1632 (lvar-tn node block count)
1633 nil)
1634 ((reference-tn-list locs t))))))))
1636 ;;;; special binding
1638 ;;; This is trivial, given our assumption of a shallow-binding
1639 ;;; implementation.
1640 (defoptimizer (%special-bind ir2-convert) ((var value) node block)
1641 (let ((name (leaf-source-name (lvar-value var))))
1642 ;; Emit either BIND or DYNBIND, preferring BIND if both exist.
1643 ;; If only one exists, it's DYNBIND.
1644 ;; Even if the backend supports load-time TLS index assignment,
1645 ;; there might be only one vop (as with arm64).
1646 (macrolet ((doit (bind dynbind)
1647 (if (gethash 'bind *backend-parsed-vops*) bind dynbind)))
1648 (doit
1649 (progn
1650 ;; Inform later SYMBOL-VALUE calls that they can
1651 ;; assume a nonzero tls-index.
1652 ;; FIXME: setting INFO is inefficient when not actually
1653 ;; changing anything
1654 (unless (info :variable :wired-tls name)
1655 (setf (info :variable :wired-tls name) :always-has-tls))
1656 ;; We force the symbol into the code constants in case BIND
1657 ;; does not actually reference it, as with x86.
1658 (emit-constant name)
1659 (vop bind node block (lvar-tn node block value) name))
1660 (vop dynbind node block (lvar-tn node block value)
1661 (emit-constant name))))))
1663 (defoptimizer (%special-unbind ir2-convert) ((var) node block)
1664 (declare (ignore var))
1665 (vop unbind node block))
1667 ;;; ### It's not clear that this really belongs in this file, or
1668 ;;; should really be done this way, but this is the least violation of
1669 ;;; abstraction in the current setup. We don't want to wire
1670 ;;; shallow-binding assumptions into IR1tran.
1671 (def-ir1-translator progv
1672 ((vars vals &body body) start next result)
1673 (ir1-convert
1674 start next result
1675 (with-unique-names (bind unbind)
1676 (once-only ((n-save-bs '(%primitive current-binding-pointer)))
1677 `(unwind-protect
1678 (progn
1679 (labels ((,unbind (vars)
1680 (declare (optimize (speed 2) (debug 0)))
1681 (let ((unbound-marker (%primitive make-unbound-marker)))
1682 (dolist (var vars)
1683 ;; CLHS says "bound and then made to have no value" -- user
1684 ;; should not be able to tell the difference between that and this.
1685 (about-to-modify-symbol-value var 'progv)
1686 (%primitive dynbind unbound-marker var))))
1687 (,bind (vars vals)
1688 (declare (optimize (speed 2) (debug 0)
1689 (insert-debug-catch 0)))
1690 (cond ((null vars))
1691 ((null vals) (,unbind vars))
1693 (let ((val (car vals))
1694 (var (car vars)))
1695 (about-to-modify-symbol-value var 'progv val t)
1696 (%primitive dynbind val var))
1697 (,bind (cdr vars) (cdr vals))))))
1698 (,bind ,vars ,vals))
1700 ,@body)
1701 ;; Technically ANSI CL doesn't allow declarations at the
1702 ;; start of the cleanup form. SBCL happens to allow for
1703 ;; them, due to the way the UNWIND-PROTECT ir1 translation
1704 ;; is implemented; the cleanup forms are directly spliced
1705 ;; into an FLET definition body. And a declaration here
1706 ;; actually has exactly the right scope for what we need
1707 ;; (ensure that debug instrumentation is not emitted for the
1708 ;; cleanup function). -- JES, 2007-06-16
1709 (declare (optimize (insert-debug-catch 0)))
1710 (%primitive unbind-to-here ,n-save-bs))))))
1712 ;;;; non-local exit
1714 ;;; Convert a non-local lexical exit. First find the NLX-INFO in our
1715 ;;; environment. Note that this is never called on the escape exits
1716 ;;; for CATCH and UNWIND-PROTECT, since the escape functions aren't
1717 ;;; IR2 converted.
1718 (defun ir2-convert-exit (node block)
1719 (declare (type exit node) (type ir2-block block))
1720 (let* ((nlx (exit-nlx-info node))
1721 (loc (find-in-physenv nlx (node-physenv node)))
1722 (temp (make-stack-pointer-tn))
1723 (value (exit-value node)))
1724 (if (nlx-info-safe-p nlx)
1725 (vop value-cell-ref node block loc temp)
1726 (emit-move node block loc temp))
1727 (if value
1728 (let ((locs (ir2-lvar-locs (lvar-info value))))
1729 (vop unwind node block temp (first locs) (second locs)))
1730 (let ((0-tn (emit-constant 0)))
1731 (vop unwind node block temp 0-tn 0-tn))))
1733 (values))
1735 ;;; %CLEANUP-POINT doesn't do anything except prevent the body from
1736 ;;; being entirely deleted.
1737 (defoptimizer (%cleanup-point ir2-convert) (() node block) node block)
1739 ;;; This function invalidates a lexical exit on exiting from the
1740 ;;; dynamic extent. This is done by storing 0 into the indirect value
1741 ;;; cell that holds the closed unwind block.
1742 (defoptimizer (%lexical-exit-breakup ir2-convert) ((info) node block)
1743 (let ((nlx (lvar-value info)))
1744 (when (nlx-info-safe-p nlx)
1745 (vop value-cell-set node block
1746 (find-in-physenv nlx (node-physenv node))
1747 (emit-constant 0)))))
1749 ;;; We have to do a spurious move of no values to the result lvar so
1750 ;;; that lifetime analysis won't get confused.
1751 (defun ir2-convert-throw (node block)
1752 (declare (type mv-combination node) (type ir2-block block))
1753 (let ((args (basic-combination-args node)))
1754 (check-catch-tag-type (first args))
1755 (vop* throw node block
1756 ((lvar-tn node block (first args))
1757 (reference-tn-list
1758 (ir2-lvar-locs (lvar-info (second args)))
1759 nil))
1760 (nil)))
1761 (move-lvar-result node block () (node-lvar node))
1762 (values))
1764 ;;; Emit code to set up a non-local exit. INFO is the NLX-INFO for the
1765 ;;; exit, and TAG is the lvar for the catch tag (if any.) We get at
1766 ;;; the target PC by passing in the label to the vop. The vop is
1767 ;;; responsible for building a return-PC object.
1768 (defun emit-nlx-start (node block info tag)
1769 (declare (type node node) (type ir2-block block) (type nlx-info info)
1770 (type (or lvar null) tag))
1771 (let* ((2info (nlx-info-info info))
1772 (kind (cleanup-kind (nlx-info-cleanup info)))
1773 (block-tn (physenv-live-tn
1774 (make-normal-tn
1775 (primitive-type-or-lose
1776 (ecase kind
1777 (:catch
1778 'catch-block)
1779 ((:unwind-protect :block :tagbody)
1780 'unwind-block))))
1781 (node-physenv node)))
1782 (res (make-stack-pointer-tn))
1783 (target-label (ir2-nlx-info-target 2info)))
1785 (vop current-binding-pointer node block
1786 (car (ir2-nlx-info-dynamic-state 2info)))
1787 (vop* save-dynamic-state node block
1788 (nil)
1789 ((reference-tn-list (cdr (ir2-nlx-info-dynamic-state 2info)) t)))
1790 (vop current-stack-pointer node block (ir2-nlx-info-save-sp 2info))
1792 (ecase kind
1793 (:catch
1794 (vop make-catch-block node block block-tn
1795 (lvar-tn node block tag) target-label res))
1796 ((:unwind-protect :block :tagbody)
1797 (vop make-unwind-block node block block-tn target-label res)))
1799 (ecase kind
1800 ((:block :tagbody)
1801 (if (nlx-info-safe-p info)
1802 (emit-make-value-cell node block res (ir2-nlx-info-home 2info))
1803 (emit-move node block res (ir2-nlx-info-home 2info))))
1804 (:unwind-protect
1805 (vop set-unwind-protect node block block-tn))
1806 (:catch)))
1808 (values))
1810 ;;; Scan each of ENTRY's exits, setting up the exit for each lexical exit.
1811 (defun ir2-convert-entry (node block)
1812 (declare (type entry node) (type ir2-block block))
1813 (let ((nlxes '()))
1814 (dolist (exit (entry-exits node))
1815 (let ((info (exit-nlx-info exit)))
1816 (when (and info
1817 (not (memq info nlxes))
1818 (member (cleanup-kind (nlx-info-cleanup info))
1819 '(:block :tagbody)))
1820 (push info nlxes)
1821 (emit-nlx-start node block info nil)))))
1822 (values))
1824 ;;; Set up the unwind block for these guys.
1825 (defoptimizer (%catch ir2-convert) ((info-lvar tag) node block)
1826 (check-catch-tag-type tag)
1827 (emit-nlx-start node block (lvar-value info-lvar) tag))
1828 (defoptimizer (%unwind-protect ir2-convert) ((info-lvar cleanup) node block)
1829 (declare (ignore cleanup))
1830 (emit-nlx-start node block (lvar-value info-lvar) nil))
1832 ;;; Emit the entry code for a non-local exit. We receive values and
1833 ;;; restore dynamic state.
1835 ;;; In the case of a lexical exit or CATCH, we look at the exit lvar's
1836 ;;; kind to determine which flavor of entry VOP to emit. If unknown
1837 ;;; values, emit the xxx-MULTIPLE variant to the lvar locs. If fixed
1838 ;;; values, make the appropriate number of temps in the standard
1839 ;;; values locations and use the other variant, delivering the temps
1840 ;;; to the lvar using MOVE-LVAR-RESULT.
1842 ;;; In the UNWIND-PROTECT case, we deliver the first register
1843 ;;; argument, the argument count and the argument pointer to our lvar
1844 ;;; as multiple values. These values are the block exited to and the
1845 ;;; values start and count.
1847 ;;; After receiving values, we restore dynamic state. Except in the
1848 ;;; UNWIND-PROTECT case, the values receiving restores the stack
1849 ;;; pointer. In an UNWIND-PROTECT cleanup, we want to leave the stack
1850 ;;; pointer alone, since the thrown values are still out there.
1851 (defoptimizer (%nlx-entry ir2-convert) ((info-lvar) node block)
1852 (let* ((info (lvar-value info-lvar))
1853 (lvar (node-lvar node))
1854 (2info (nlx-info-info info))
1855 (top-loc (ir2-nlx-info-save-sp 2info))
1856 (start-loc (make-nlx-entry-arg-start-location))
1857 (count-loc (make-arg-count-location))
1858 (target (ir2-nlx-info-target 2info)))
1860 (ecase (cleanup-kind (nlx-info-cleanup info))
1861 ((:catch :block :tagbody)
1862 (let ((2lvar (and lvar (lvar-info lvar))))
1863 (if (and 2lvar (eq (ir2-lvar-kind 2lvar) :unknown))
1864 (vop* nlx-entry-multiple node block
1865 (top-loc start-loc count-loc nil)
1866 ((reference-tn-list (ir2-lvar-locs 2lvar) t))
1867 target)
1868 (let ((locs (standard-result-tns lvar)))
1869 (vop* nlx-entry node block
1870 (top-loc start-loc count-loc nil)
1871 ((reference-tn-list locs t))
1872 target
1873 (length locs))
1874 (move-lvar-result node block locs lvar)))))
1875 (:unwind-protect
1876 (let ((block-loc (standard-arg-location 0)))
1877 (vop uwp-entry node block target block-loc start-loc count-loc)
1878 (move-lvar-result
1879 node block
1880 (list block-loc start-loc count-loc)
1881 lvar))))
1883 #!+sb-dyncount
1884 (when *collect-dynamic-statistics*
1885 (vop count-me node block *dynamic-counts-tn*
1886 (block-number (ir2-block-block block))))
1888 (vop* restore-dynamic-state node block
1889 ((reference-tn-list (cdr (ir2-nlx-info-dynamic-state 2info)) nil))
1890 (nil))
1891 (vop unbind-to-here node block
1892 (car (ir2-nlx-info-dynamic-state 2info)))))
1894 ;;;; n-argument functions
1896 (macrolet ((def (name)
1897 `(defoptimizer (,name ir2-convert) ((&rest args) node block)
1898 (cond #!+gencgc
1899 ((>= (length args)
1900 (/ sb!vm:large-object-size
1901 (* sb!vm:n-word-bytes 2)))
1902 ;; The VOPs will try to allocate all space at once
1903 ;; And it'll end up in large objects, and no conses
1904 ;; are welcome there.
1905 (ir2-convert-full-call node block))
1907 (let* ((refs (reference-tn-list
1908 (loop for arg in args
1909 for tn = (make-normal-tn *backend-t-primitive-type*)
1911 (emit-move node block (lvar-tn node block arg) tn)
1912 collect tn)
1913 nil))
1914 (lvar (node-lvar node))
1915 (res (lvar-result-tns
1916 lvar
1917 (list (primitive-type (specifier-type 'list))))))
1918 (when (and lvar (lvar-dynamic-extent lvar))
1919 (vop current-stack-pointer node block
1920 (ir2-lvar-stack-pointer (lvar-info lvar))))
1921 (vop* ,name node block (refs) ((first res) nil)
1922 (length args))
1923 (move-lvar-result node block res lvar)))))))
1924 (def list)
1925 (def list*))
1928 (defoptimizer (mask-signed-field ir2-convert) ((width x) node block)
1929 (block nil
1930 (when (constant-lvar-p width)
1931 (case (lvar-value width)
1932 (#.(- sb!vm:n-word-bits sb!vm:n-fixnum-tag-bits)
1933 (when (or (csubtypep (lvar-type x)
1934 (specifier-type 'word))
1935 (csubtypep (lvar-type x)
1936 (specifier-type 'sb!vm:signed-word)))
1937 (let* ((lvar (node-lvar node))
1938 (temp (make-normal-tn
1939 (if (csubtypep (lvar-type x)
1940 (specifier-type 'word))
1941 (primitive-type-of most-positive-word)
1942 (primitive-type-of
1943 (- (ash most-positive-word -1))))))
1944 (results (lvar-result-tns
1945 lvar
1946 (list (primitive-type-or-lose 'fixnum)))))
1947 (emit-move node block (lvar-tn node block x) temp)
1948 (vop sb!vm::move-from-word/fixnum node block
1949 temp (first results))
1950 (move-lvar-result node block results lvar)
1951 (return))))
1952 (#.sb!vm:n-word-bits
1953 (when (csubtypep (lvar-type x) (specifier-type 'word))
1954 (let* ((lvar (node-lvar node))
1955 (temp (make-normal-tn
1956 (primitive-type-of most-positive-word)))
1957 (results (lvar-result-tns
1958 lvar
1959 (list (primitive-type
1960 (specifier-type 'sb!vm:signed-word))))))
1961 (emit-move node block (lvar-tn node block x) temp)
1962 (vop sb!vm::word-move node block
1963 temp (first results))
1964 (move-lvar-result node block results lvar)
1965 (return))))))
1966 (if (template-p (basic-combination-info node))
1967 (ir2-convert-template node block)
1968 (ir2-convert-full-call node block))))
1970 ;; just a fancy identity
1971 (defoptimizer (%typep-wrapper ir2-convert) ((value variable type) node block)
1972 (declare (ignore variable type))
1973 (let* ((lvar (node-lvar node))
1974 (results (lvar-result-tns lvar (list (primitive-type-or-lose t)))))
1975 (emit-move node block (lvar-tn node block value) (first results))
1976 (move-lvar-result node block results lvar)))
1978 ;;; Convert the code in a component into VOPs.
1979 (defun ir2-convert (component)
1980 (declare (type component component))
1981 (let (#!+sb-dyncount
1982 (*dynamic-counts-tn*
1983 (when *collect-dynamic-statistics*
1984 (let* ((blocks
1985 (block-number (block-next (component-head component))))
1986 (counts (make-array blocks
1987 :element-type '(unsigned-byte 32)
1988 :initial-element 0))
1989 (info (make-dyncount-info
1990 :for (component-name component)
1991 :costs (make-array blocks
1992 :element-type '(unsigned-byte 32)
1993 :initial-element 0)
1994 :counts counts)))
1995 (setf (ir2-component-dyncount-info (component-info component))
1996 info)
1997 (emit-constant info)
1998 (emit-constant counts)))))
1999 (let ((num 0))
2000 (declare (type index num))
2001 (do-ir2-blocks (2block component)
2002 (let ((block (ir2-block-block 2block)))
2003 (when (block-start block)
2004 (setf (block-number block) num)
2005 #!+sb-dyncount
2006 (when *collect-dynamic-statistics*
2007 (let ((first-node (block-start-node block)))
2008 (unless (or (and (bind-p first-node)
2009 (xep-p (bind-lambda first-node)))
2010 (eq (lvar-fun-name
2011 (node-lvar first-node))
2012 '%nlx-entry))
2013 (vop count-me
2014 first-node
2015 2block
2016 #!+sb-dyncount *dynamic-counts-tn* #!-sb-dyncount nil
2017 num))))
2018 #!+sb-safepoint
2019 (let ((first-node (block-start-node block)))
2020 (unless (or (and (bind-p first-node)
2021 ;; Bind-nodes already have safepoints
2022 (eq (bind-lambda first-node)
2023 (lambda-home (bind-lambda first-node))))
2024 (and (valued-node-p first-node)
2025 (node-lvar first-node)
2026 (eq (lvar-fun-name
2027 (node-lvar first-node))
2028 '%nlx-entry)))
2029 (when (and (rest (block-pred block))
2030 (block-loop block)
2031 (member (loop-kind (block-loop block))
2032 '(:natural :strange))
2033 (eq block (loop-head (block-loop block)))
2034 (policy first-node (< inhibit-safepoints 2)))
2035 (vop sb!vm::insert-safepoint first-node 2block))))
2036 (ir2-convert-block block)
2037 (incf num))))))
2038 (values))
2040 ;;; If necessary, emit a terminal unconditional branch to go to the
2041 ;;; successor block. If the successor is the component tail, then
2042 ;;; there isn't really any successor, but if the end is a non-tail
2043 ;;; call to a function that's not *known* to never return, then we
2044 ;;; emit an error trap just in case the function really does return.
2046 ;;; Trapping after known calls makes it easier to understand type
2047 ;;; derivation bugs at runtime: they show up as nil-fun-returned-error,
2048 ;;; rather than the execution of arbitrary code or error traps.
2049 (defun finish-ir2-block (block)
2050 (declare (type cblock block))
2051 (let* ((2block (block-info block))
2052 (last (block-last block))
2053 (succ (block-succ block)))
2054 (unless (if-p last)
2055 (aver (singleton-p succ))
2056 (let ((target (first succ)))
2057 (cond ((eq target (component-tail (block-component block)))
2058 (when (and (basic-combination-p last)
2059 (or (eq (basic-combination-kind last) :full)
2060 (and (eq (basic-combination-kind last) :known)
2061 (eq (basic-combination-info last) :full))))
2062 (let* ((fun (basic-combination-fun last))
2063 (use (lvar-uses fun))
2064 (name (and (ref-p use)
2065 (leaf-has-source-name-p (ref-leaf use))
2066 (leaf-source-name (ref-leaf use))))
2067 (ftype (and (info :function :info name) ; only use the FTYPE if
2068 (proclaimed-ftype name)))) ; NAME was DEFKNOWN
2069 (unless (or (node-tail-p last)
2070 (policy last (zerop safety))
2071 (and (fun-type-p ftype)
2072 (eq *empty-type* (fun-type-returns ftype))))
2073 (vop nil-fun-returned-error last 2block
2074 (if name
2075 (emit-constant name)
2076 (multiple-value-bind (tn named)
2077 (fun-lvar-tn last 2block fun)
2078 (aver (not named))
2079 tn)))))))
2080 ((not (eq (ir2-block-next 2block) (block-info target)))
2081 (vop branch last 2block (block-label target)))
2083 (register-drop-thru target))))))
2085 (values))
2087 ;;; Convert the code in a block into VOPs.
2088 (defun ir2-convert-block (block)
2089 (declare (type cblock block))
2090 (let ((2block (block-info block)))
2091 (do-nodes (node lvar block)
2092 (etypecase node
2093 (ref
2094 (when lvar
2095 (let ((2lvar (lvar-info lvar)))
2096 ;; function REF in a local call is not annotated
2097 (when (and 2lvar (not (eq (ir2-lvar-kind 2lvar) :delayed)))
2098 (ir2-convert-ref node 2block)))))
2099 (combination
2100 (let ((kind (basic-combination-kind node)))
2101 (ecase kind
2102 (:local
2103 (ir2-convert-local-call node 2block))
2104 (:full
2105 (ir2-convert-full-call node 2block))
2106 (:known
2107 (let* ((info (basic-combination-fun-info node))
2108 (fun (fun-info-ir2-convert info)))
2109 (cond (fun
2110 (funcall fun node 2block))
2111 ((eq (basic-combination-info node) :full)
2112 (ir2-convert-full-call node 2block))
2114 (ir2-convert-template node 2block))))))))
2115 (cif
2116 (when (lvar-info (if-test node))
2117 (ir2-convert-if node 2block)))
2118 (bind
2119 (let ((fun (bind-lambda node)))
2120 (when (eq (lambda-home fun) fun)
2121 (ir2-convert-bind node 2block))))
2122 (creturn
2123 (ir2-convert-return node 2block))
2124 (cset
2125 (ir2-convert-set node 2block))
2126 (cast
2127 (ir2-convert-cast node 2block))
2128 (mv-combination
2129 (cond
2130 ((eq (basic-combination-kind node) :local)
2131 (ir2-convert-mv-bind node 2block))
2132 ((eq (lvar-fun-name (basic-combination-fun node))
2133 '%throw)
2134 (ir2-convert-throw node 2block))
2136 (ir2-convert-mv-call node 2block))))
2137 (exit
2138 (when (exit-entry node)
2139 (ir2-convert-exit node 2block)))
2140 (entry
2141 (ir2-convert-entry node 2block)))))
2143 (finish-ir2-block block)
2145 (values))