Add ir2-hook fun optimizer.
[sbcl.git] / src / compiler / ir2tran.lisp
blobce0c01b6aba1026c6767a0ec0c9d56c1c953aecf
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-hook) ((array bound index) node block)
589 (declare (ignore 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))))))))
600 ;;;; template conversion
602 ;;; Build a TN-REFS list that represents access to the values of the
603 ;;; specified list of lvars ARGS for TEMPLATE. Any :CONSTANT arguments
604 ;;; are returned in the second value as a list rather than being
605 ;;; accessed as a normal argument. NODE and BLOCK provide the context
606 ;;; for emitting any necessary type-checking code.
607 (defun reference-args (node block args template)
608 (declare (type node node) (type ir2-block block) (list args)
609 (type template template))
610 (collect ((info-args))
611 (let ((last nil)
612 (first nil))
613 (do ((args args (cdr args))
614 (types (template-arg-types template) (cdr types)))
615 ((null args))
616 (let ((type (first types))
617 (arg (first args)))
618 (if (and (consp type) (eq (car type) ':constant))
619 (info-args (lvar-value arg))
620 (let ((ref (reference-tn (lvar-tn node block arg) nil)))
621 (if last
622 (setf (tn-ref-across last) ref)
623 (setf first ref))
624 (setq last ref)))))
626 (values (the (or tn-ref null) first) (info-args)))))
628 ;;; Convert a conditional template. We try to exploit any
629 ;;; drop-through, but emit an unconditional branch afterward if we
630 ;;; fail. NOT-P is true if the sense of the TEMPLATE's test should be
631 ;;; negated.
632 (defun ir2-convert-conditional (node block template args info-args if not-p)
633 (declare (type node node) (type ir2-block block)
634 (type template template) (type (or tn-ref null) args)
635 (list info-args) (type cif if) (type boolean not-p))
636 (let ((consequent (if-consequent if))
637 (alternative (if-alternative if))
638 (flags (and (consp (template-result-types template))
639 (rest (template-result-types template)))))
640 (aver (= (template-info-arg-count template)
641 (+ (length info-args)
642 (if flags 0 2))))
643 (when not-p
644 (rotatef consequent alternative)
645 (setf not-p nil))
646 (when (drop-thru-p if consequent)
647 (rotatef consequent alternative)
648 (setf not-p t))
649 (cond ((not flags)
650 (emit-template node block template args nil
651 (list* (block-label consequent) not-p
652 info-args))
653 (if (drop-thru-p if alternative)
654 (register-drop-thru alternative)
655 (vop branch node block (block-label alternative))))
657 (emit-template node block template args nil info-args)
658 (vop branch-if node block (block-label consequent) flags not-p)
659 (if (drop-thru-p if alternative)
660 (register-drop-thru alternative)
661 (vop branch node block (block-label alternative)))))))
663 ;;; Convert an IF that isn't the DEST of a conditional template.
664 (defun ir2-convert-if (node block)
665 (declare (type ir2-block block) (type cif node))
666 (let* ((test (if-test node))
667 (test-ref (reference-tn (lvar-tn node block test) nil))
668 (nil-ref (reference-tn (emit-constant nil) nil)))
669 (setf (tn-ref-across test-ref) nil-ref)
670 (ir2-convert-conditional node block (template-or-lose 'if-eq)
671 test-ref () node t)))
673 ;;; Return a list of primitive-types that we can pass to LVAR-RESULT-TNS
674 ;;; describing the result types we want for a template call. We are really
675 ;;; only interested in the number of results required: in normal case
676 ;;; TEMPLATE-RESULTS-OK has already checked them.
677 (defun find-template-result-types (call rtypes)
678 (let* ((type (node-derived-type call))
679 (types
680 (mapcar #'primitive-type
681 (if (args-type-p type)
682 (append (args-type-required type)
683 (args-type-optional type))
684 (list type))))
685 (primitive-t *backend-t-primitive-type*))
686 (mapcar (lambda (rtype)
687 (declare (ignore rtype))
688 (or (pop types) primitive-t)) rtypes)))
690 ;;; Return a list of TNs usable in a CALL to TEMPLATE delivering values to
691 ;;; LVAR. As an efficiency hack, we pick off the common case where the LVAR is
692 ;;; fixed values and has locations that satisfy the result restrictions. This
693 ;;; can fail when there is a type check or a values count mismatch.
694 (defun make-template-result-tns (call lvar rtypes)
695 (declare (type combination call) (type (or lvar null) lvar)
696 (list rtypes))
697 (let ((2lvar (when lvar (lvar-info lvar))))
698 (if (and 2lvar (eq (ir2-lvar-kind 2lvar) :fixed))
699 (let ((locs (ir2-lvar-locs 2lvar)))
700 (if (and (= (length rtypes) (length locs))
701 (do ((loc locs (cdr loc))
702 (rtypes rtypes (cdr rtypes)))
703 ((null loc) t)
704 (unless (operand-restriction-ok
705 (car rtypes)
706 (tn-primitive-type (car loc))
707 :t-ok nil)
708 (return nil))))
709 locs
710 (lvar-result-tns
711 lvar
712 (find-template-result-types call rtypes))))
713 (lvar-result-tns
714 lvar
715 (find-template-result-types call rtypes)))))
717 ;;; Get the operands into TNs, make TN-REFs for them, and then call
718 ;;; the template emit function.
719 (defun ir2-convert-template (call block)
720 (declare (type combination call) (type ir2-block block))
721 (let* ((template (combination-info call))
722 (lvar (node-lvar call))
723 (rtypes (template-result-types template)))
724 (multiple-value-bind (args info-args)
725 (reference-args call block (combination-args call) template)
726 (aver (not (template-more-results-type template)))
727 (if (template-conditional-p template)
728 (ir2-convert-conditional call block template args info-args
729 (lvar-dest lvar) nil)
730 (let* ((results (make-template-result-tns call lvar rtypes))
731 (r-refs (reference-tn-list results t)))
732 (aver (= (length info-args)
733 (template-info-arg-count template)))
734 (when (and lvar (lvar-dynamic-extent lvar))
735 (vop current-stack-pointer call block
736 (ir2-lvar-stack-pointer (lvar-info lvar))))
737 (when (emit-step-p call)
738 (vop sb!vm::step-instrument-before-vop call block))
739 (if info-args
740 (emit-template call block template args r-refs info-args)
741 (emit-template call block template args r-refs))
742 (move-lvar-result call block results lvar)))))
743 (values))
745 ;;; We don't have to do much because operand count checking is done by
746 ;;; IR1 conversion. The only difference between this and the function
747 ;;; case of IR2-CONVERT-TEMPLATE is that there can be codegen-info
748 ;;; arguments.
749 (defoptimizer (%%primitive ir2-convert) ((template info &rest args) call block)
750 (declare (ignore args))
751 (let* ((template (lvar-value template))
752 (info (lvar-value info))
753 (lvar (node-lvar call))
754 (rtypes (template-result-types template))
755 (results (make-template-result-tns call lvar rtypes))
756 (r-refs (reference-tn-list results t)))
757 (multiple-value-bind (args info-args)
758 (reference-args call block (cddr (combination-args call)) template)
759 (aver (not (template-more-results-type template)))
760 (aver (not (template-conditional-p template)))
761 (aver (null info-args))
763 (if info
764 (emit-template call block template args r-refs info)
765 (emit-template call block template args r-refs))
767 (move-lvar-result call block results lvar)))
768 (values))
770 (defoptimizer (%%primitive derive-type) ((template info &rest args))
771 (declare (ignore info args))
772 (let ((type (template-type (lvar-value template))))
773 (if (fun-type-p type)
774 (fun-type-returns type)
775 *wild-type*)))
777 ;;;; local call
779 ;;; Convert a LET by moving the argument values into the variables.
780 ;;; Since a LET doesn't have any passing locations, we move the
781 ;;; arguments directly into the variables. We must also allocate any
782 ;;; indirect value cells, since there is no function prologue to do
783 ;;; this.
784 (defun ir2-convert-let (node block fun)
785 (declare (type combination node) (type ir2-block block) (type clambda fun))
786 (mapc (lambda (var arg)
787 (when arg
788 (let ((src (lvar-tn node block arg))
789 (dest (leaf-info var)))
790 (if (and (lambda-var-indirect var)
791 (lambda-var-explicit-value-cell var))
792 (emit-make-value-cell node block src dest)
793 (emit-move node block src dest)))))
794 (lambda-vars fun) (basic-combination-args node))
795 (values))
797 ;;; Emit any necessary moves into assignment temps for a local call to
798 ;;; FUN. We return two lists of TNs: TNs holding the actual argument
799 ;;; values, and (possibly EQ) TNs that are the actual destination of
800 ;;; the arguments. When necessary, we allocate temporaries for
801 ;;; arguments to preserve parallel assignment semantics. These lists
802 ;;; exclude unused arguments and include implicit environment
803 ;;; arguments, i.e. they exactly correspond to the arguments passed.
805 ;;; OLD-FP is the TN currently holding the value we want to pass as
806 ;;; OLD-FP. If null, then the call is to the same environment (an
807 ;;; :ASSIGNMENT), so we only move the arguments, and leave the
808 ;;; environment alone.
810 ;;; CLOSURE-FP is for calling a closure that has "implicit" value
811 ;;; cells (stored in the allocating stack frame), and is the frame
812 ;;; pointer TN to use for values allocated in the outbound stack
813 ;;; frame. This is distinct from OLD-FP for the specific case of a
814 ;;; tail-local-call.
815 (defun emit-psetq-moves (node block fun old-fp &optional (closure-fp old-fp))
816 (declare (type combination node) (type ir2-block block) (type clambda fun)
817 (type (or tn null) old-fp closure-fp))
818 (let ((actuals (mapcar (lambda (x)
819 (when x
820 (lvar-tn node block x)))
821 (combination-args node))))
822 (collect ((temps)
823 (locs))
824 (dolist (var (lambda-vars fun))
825 (let ((actual (pop actuals))
826 (loc (leaf-info var)))
827 (when actual
828 (cond
829 ((and (lambda-var-indirect var)
830 (lambda-var-explicit-value-cell var))
831 (let ((temp
832 (make-normal-tn *backend-t-primitive-type*)))
833 (emit-make-value-cell node block actual temp)
834 (temps temp)))
835 ((member actual (locs))
836 (let ((temp (make-normal-tn (tn-primitive-type loc))))
837 (emit-move node block actual temp)
838 (temps temp)))
840 (temps actual)))
841 (locs loc))))
843 (when old-fp
844 (let ((this-1env (node-physenv node))
845 (called-env (physenv-info (lambda-physenv fun))))
846 (dolist (thing (ir2-physenv-closure called-env))
847 (temps (closure-initial-value (car thing) this-1env closure-fp))
848 (locs (cdr thing)))
849 (temps old-fp)
850 (locs (ir2-physenv-old-fp called-env))))
852 (values (temps) (locs)))))
854 ;;; A tail-recursive local call is done by emitting moves of stuff
855 ;;; into the appropriate passing locations. After setting up the args
856 ;;; and environment, we just move our return-pc into the called
857 ;;; function's passing location.
858 (defun ir2-convert-tail-local-call (node block fun)
859 (declare (type combination node) (type ir2-block block) (type clambda fun))
860 (let ((this-env (physenv-info (node-physenv node)))
861 (current-fp (make-stack-pointer-tn)))
862 (multiple-value-bind (temps locs)
863 (emit-psetq-moves node block fun
864 (ir2-physenv-old-fp this-env) current-fp)
866 ;; If we're about to emit a move from CURRENT-FP then we need to
867 ;; initialize it.
868 (when (find current-fp temps)
869 (vop current-fp node block current-fp))
871 (mapc (lambda (temp loc)
872 (emit-move node block temp loc))
873 temps locs))
875 (emit-move node block
876 (ir2-physenv-return-pc this-env)
877 (ir2-physenv-return-pc-pass
878 (physenv-info
879 (lambda-physenv fun)))))
881 (values))
883 ;;; Convert an :ASSIGNMENT call. This is just like a tail local call,
884 ;;; except that the caller and callee environment are the same, so we
885 ;;; don't need to mess with the environment locations, return PC, etc.
886 (defun ir2-convert-assignment (node block fun)
887 (declare (type combination node) (type ir2-block block) (type clambda fun))
888 (multiple-value-bind (temps locs) (emit-psetq-moves node block fun nil)
890 (mapc (lambda (temp loc)
891 (emit-move node block temp loc))
892 temps locs))
893 (values))
895 ;;; Do stuff to set up the arguments to a non-tail local call
896 ;;; (including implicit environment args.) We allocate a frame
897 ;;; (returning the FP and NFP), and also compute the TN-REFS list for
898 ;;; the values to pass and the list of passing location TNs.
899 (defun ir2-convert-local-call-args (node block fun)
900 (declare (type combination node) (type ir2-block block) (type clambda fun))
901 (let ((fp (make-stack-pointer-tn))
902 (nfp (make-number-stack-pointer-tn))
903 (old-fp (make-stack-pointer-tn)))
904 (multiple-value-bind (temps locs)
905 (emit-psetq-moves node block fun old-fp)
906 (vop current-fp node block old-fp)
907 (vop allocate-frame node block
908 (physenv-info (lambda-physenv fun))
909 fp nfp)
910 (values fp nfp temps (mapcar #'make-alias-tn locs)))))
912 ;;; Handle a non-TR known-values local call. We emit the call, then
913 ;;; move the results to the lvar's destination.
914 (defun ir2-convert-local-known-call (node block fun returns lvar start)
915 (declare (type node node) (type ir2-block block) (type clambda fun)
916 (type return-info returns) (type (or lvar null) lvar)
917 (type label start))
918 (multiple-value-bind (fp nfp temps arg-locs)
919 (ir2-convert-local-call-args node block fun)
920 (let ((locs (return-info-locations returns)))
921 (vop* known-call-local node block
922 (fp nfp (reference-tn-list temps nil))
923 ((reference-tn-list locs t))
924 arg-locs (physenv-info (lambda-physenv fun)) start)
925 (move-lvar-result node block locs lvar)))
926 (values))
928 ;;; Handle a non-TR unknown-values local call. We do different things
929 ;;; depending on what kind of values the lvar wants.
931 ;;; If LVAR is :UNKNOWN, then we use the "multiple-" variant, directly
932 ;;; specifying the lvar's LOCS as the VOP results so that we don't
933 ;;; have to do anything after the call.
935 ;;; Otherwise, we use STANDARD-RESULT-TNS to get wired result TNs, and
936 ;;; then call MOVE-LVAR-RESULT to do any necessary type checks or
937 ;;; coercions.
938 (defun ir2-convert-local-unknown-call (node block fun lvar start)
939 (declare (type node node) (type ir2-block block) (type clambda fun)
940 (type (or lvar null) lvar) (type label start))
941 (multiple-value-bind (fp nfp temps arg-locs)
942 (ir2-convert-local-call-args node block fun)
943 (let ((2lvar (and lvar (lvar-info lvar)))
944 (env (physenv-info (lambda-physenv fun)))
945 (temp-refs (reference-tn-list temps nil)))
946 (if (and 2lvar (eq (ir2-lvar-kind 2lvar) :unknown))
947 (vop* multiple-call-local node block (fp nfp temp-refs)
948 ((reference-tn-list (ir2-lvar-locs 2lvar) t))
949 arg-locs env start)
950 (let ((locs (standard-result-tns lvar)))
951 (vop* call-local node block
952 (fp nfp temp-refs)
953 ((reference-tn-list locs t))
954 arg-locs env start (length locs))
955 (move-lvar-result node block locs lvar)))))
956 (values))
958 ;;; Dispatch to the appropriate function, depending on whether we have
959 ;;; a let, tail or normal call. If the function doesn't return, call
960 ;;; it using the unknown-value convention. We could compile it as a
961 ;;; tail call, but that might seem confusing in the debugger.
962 (defun ir2-convert-local-call (node block)
963 (declare (type combination node) (type ir2-block block))
964 (let* ((fun (ref-leaf (lvar-uses (basic-combination-fun node))))
965 (kind (functional-kind fun)))
966 (cond ((eq kind :let)
967 (ir2-convert-let node block fun))
968 ((eq kind :assignment)
969 (ir2-convert-assignment node block fun))
970 ((node-tail-p node)
971 (ir2-convert-tail-local-call node block fun))
973 (let ((start (block-trampoline (lambda-block fun)))
974 (returns (tail-set-info (lambda-tail-set fun)))
975 (lvar (node-lvar node)))
976 (ecase (if returns
977 (return-info-kind returns)
978 :unknown)
979 (:unknown
980 (ir2-convert-local-unknown-call node block fun lvar start))
981 (:fixed
982 (ir2-convert-local-known-call node block fun returns
983 lvar start)))))))
984 (values))
986 ;;;; full call
988 ;;; Given a function lvar FUN, return (VALUES TN-TO-CALL NAMED-P),
989 ;;; where TN-TO-CALL is a TN holding the thing that we call NAMED-P is
990 ;;; true if the thing is named (false if it is a function).
992 ;;; There are two interesting non-named cases:
993 ;;; -- We know it's a function. No check needed: return the
994 ;;; lvar LOC.
995 ;;; -- We don't know what it is.
996 (defun fun-lvar-tn (node block lvar)
997 (declare (ignore node block))
998 (declare (type lvar lvar))
999 (let ((2lvar (lvar-info lvar)))
1000 (if (eq (ir2-lvar-kind 2lvar) :delayed)
1001 (let ((name (lvar-fun-name lvar t)))
1002 (aver name)
1003 (values (make-load-time-constant-tn :fdefinition name) name))
1004 (let* ((locs (ir2-lvar-locs 2lvar))
1005 (loc (first locs))
1006 (function-ptype (primitive-type-or-lose 'function)))
1007 (aver (and (eq (ir2-lvar-kind 2lvar) :fixed)
1008 (= (length locs) 1)))
1009 (aver (eq (tn-primitive-type loc) function-ptype))
1010 (values loc nil)))))
1012 ;;; Set up the args to NODE in the current frame, and return a TN-REF
1013 ;;; list for the passing locations.
1014 (defun move-tail-full-call-args (node block)
1015 (declare (type combination node) (type ir2-block block))
1016 (let ((args (basic-combination-args node))
1017 (last nil)
1018 (first nil))
1019 (dotimes (num (length args))
1020 (let ((loc (standard-arg-location num)))
1021 (emit-move node block (lvar-tn node block (elt args num)) loc)
1022 (let ((ref (reference-tn loc nil)))
1023 (if last
1024 (setf (tn-ref-across last) ref)
1025 (setf first ref))
1026 (setq last ref))))
1027 first))
1029 ;;; Move the arguments into the passing locations and do a (possibly
1030 ;;; named) tail call.
1031 (defun ir2-convert-tail-full-call (node block)
1032 (declare (type combination node) (type ir2-block block))
1033 (let* ((env (physenv-info (node-physenv node)))
1034 (args (basic-combination-args node))
1035 (nargs (length args))
1036 (pass-refs (move-tail-full-call-args node block))
1037 (old-fp (ir2-physenv-old-fp env))
1038 (return-pc (ir2-physenv-return-pc env)))
1040 (multiple-value-bind (fun-tn named)
1041 (fun-lvar-tn node block (basic-combination-fun node))
1042 (if named
1043 (vop* tail-call-named node block
1044 (fun-tn old-fp return-pc pass-refs)
1045 (nil)
1046 nargs
1047 (emit-step-p node))
1048 (vop* tail-call node block
1049 (fun-tn old-fp return-pc pass-refs)
1050 (nil)
1051 nargs
1052 (emit-step-p node)))))
1054 (values))
1056 ;;; like IR2-CONVERT-LOCAL-CALL-ARGS, only different
1057 (defun ir2-convert-full-call-args (node block)
1058 (declare (type combination node) (type ir2-block block))
1059 (let* ((args (basic-combination-args node))
1060 (fp (make-stack-pointer-tn))
1061 (nargs (length args)))
1062 (vop allocate-full-call-frame node block nargs fp)
1063 (collect ((locs))
1064 (let ((last nil)
1065 (first nil))
1066 (dotimes (num nargs)
1067 (locs (standard-arg-location num))
1068 (let ((ref (reference-tn (lvar-tn node block (elt args num))
1069 nil)))
1070 (if last
1071 (setf (tn-ref-across last) ref)
1072 (setf first ref))
1073 (setq last ref)))
1075 (values fp first (locs) nargs)))))
1077 ;;; Do full call when a fixed number of values are desired. We make
1078 ;;; STANDARD-RESULT-TNS for our lvar, then deliver the result using
1079 ;;; MOVE-LVAR-RESULT. We do named or normal call, as appropriate.
1080 (defun ir2-convert-fixed-full-call (node block)
1081 (declare (type combination node) (type ir2-block block))
1082 (multiple-value-bind (fp args arg-locs nargs)
1083 (ir2-convert-full-call-args node block)
1084 (let* ((lvar (node-lvar node))
1085 (locs (standard-result-tns lvar))
1086 (loc-refs (reference-tn-list locs t))
1087 (nvals (length locs)))
1088 (multiple-value-bind (fun-tn named)
1089 (fun-lvar-tn node block (basic-combination-fun node))
1090 (if named
1091 (vop* call-named node block (fp fun-tn args) (loc-refs)
1092 arg-locs nargs nvals (emit-step-p node))
1093 (vop* call node block (fp fun-tn args) (loc-refs)
1094 arg-locs nargs nvals (emit-step-p node)))
1095 (move-lvar-result node block locs lvar))))
1096 (values))
1098 ;;; Do full call when unknown values are desired.
1099 (defun ir2-convert-multiple-full-call (node block)
1100 (declare (type combination node) (type ir2-block block))
1101 (multiple-value-bind (fp args arg-locs nargs)
1102 (ir2-convert-full-call-args node block)
1103 (let* ((lvar (node-lvar node))
1104 (locs (ir2-lvar-locs (lvar-info lvar)))
1105 (loc-refs (reference-tn-list locs t)))
1106 (multiple-value-bind (fun-tn named)
1107 (fun-lvar-tn node block (basic-combination-fun node))
1108 (if named
1109 (vop* multiple-call-named node block (fp fun-tn args) (loc-refs)
1110 arg-locs nargs (emit-step-p node))
1111 (vop* multiple-call node block (fp fun-tn args) (loc-refs)
1112 arg-locs nargs (emit-step-p node))))))
1113 (values))
1115 ;;; stuff to check in PONDER-FULL-CALL
1117 ;;; These came in handy when troubleshooting cold boot after making
1118 ;;; major changes in the package structure: various transforms and
1119 ;;; VOPs and stuff got attached to the wrong symbol, so that
1120 ;;; references to the right symbol were bogusly translated as full
1121 ;;; calls instead of primitives, sending the system off into infinite
1122 ;;; space. Having a report on all full calls generated makes it easier
1123 ;;; to figure out what form caused the problem this time.
1124 (declaim (type (member :minimal :detailed :very-detailed :maximal)
1125 *track-full-called-fnames*))
1126 (defvar *track-full-called-fnames* :minimal)
1128 ;;; Do some checks (and store some notes relevant for future checks)
1129 ;;; on a full call:
1130 ;;; * Is this a full call to something we have reason to know should
1131 ;;; never be full called? (Except as of sbcl-0.7.18 or so, we no
1132 ;;; longer try to ensure this behavior when *FAILURE-P* has already
1133 ;;; been detected.)
1134 (defun ponder-full-call (node)
1135 (let* ((lvar (basic-combination-fun node))
1136 (fname (lvar-fun-name lvar t)))
1137 (declare (type (or symbol cons) fname))
1139 ;; Warn about cross-compiling certain full-calls,
1140 ;; as it is indicative of dependency order problems.
1141 #+sb-xc-host
1142 (let ((compname (component-name (node-component node))))
1143 ;; Don't care too much about macro performance.
1144 (unless (and (stringp compname) (string/= compname "DEFMACRO"))
1145 ;; Catch FOO and (SETF FOO) both.
1146 (let ((stem (if (atom fname) fname (second fname))))
1147 (when (member stem
1148 sb-cold::*full-calls-to-warn-about*
1149 :test #'string=)
1150 (warn "Full call to ~S" fname)))))
1152 (let* ((inlineable-p (not (let ((*lexenv* (node-lexenv node)))
1153 (fun-lexically-notinline-p fname))))
1154 (inlineable-bit (if inlineable-p 1 0))
1155 (cell (info :function :emitted-full-calls fname)))
1156 (if (not cell)
1157 ;; The low bit indicates whether any not-NOTINLINE call was seen.
1158 ;; The next-lowest bit is magic. Refer to %COMPILER-DEFMACRO
1159 ;; and WARN-IF-INLINE-FAILED/CALL for the pertinent logic.
1160 (setf cell (list (logior 4 inlineable-bit))
1161 (info :function :emitted-full-calls fname) cell)
1162 (incf (car cell) (+ 4 (if (oddp (car cell)) 0 inlineable-bit))))
1163 ;; If the full call was wanted, don't record anything.
1164 ;; (This was originally for debugging SBCL self-compilation)
1165 (when inlineable-p
1166 (unless *failure-p*
1167 (warn-if-inline-failed/call fname (node-lexenv node) cell))
1168 (case *track-full-called-fnames*
1169 (:detailed
1170 (when (boundp 'sb!xc:*compile-file-pathname*)
1171 (pushnew sb!xc:*compile-file-pathname* (cdr cell)
1172 :test #'equal)))
1173 (:very-detailed
1174 (pushnew (component-name *component-being-compiled*)
1175 (cdr cell) :test #'equalp)))))
1177 ;; Special mode, usually only for the cross-compiler
1178 ;; and only with the feature enabled.
1179 #!+sb-show (when (eq *track-full-called-fnames* :maximal)
1180 (/show "converting full call to named function" fname)
1181 (/show (basic-combination-args node))
1182 (/show (policy node speed) (policy node safety))
1183 (/show (policy node compilation-speed))
1184 (let ((arg-types (mapcar (lambda (lvar)
1185 (when lvar
1186 (type-specifier
1187 (lvar-type lvar))))
1188 (basic-combination-args node))))
1189 (/show arg-types)))
1191 ;; When illegal code is compiled, all sorts of perverse paths
1192 ;; through the compiler can be taken, and it's much harder -- and
1193 ;; probably pointless -- to guarantee that always-optimized-away
1194 ;; functions are actually optimized away. Thus, we skip the check
1195 ;; in that case.
1196 (unless *failure-p*
1197 ;; check to see if we know anything about the function
1198 (let ((info (info :function :info fname)))
1199 ;; if we know something, check to see if the full call was valid
1200 (when (and info (ir1-attributep (fun-info-attributes info)
1201 always-translatable))
1202 (/show (policy node speed) (policy node safety))
1203 (/show (policy node compilation-speed))
1204 (bug "full call to ~S" fname))))
1206 (when (consp fname)
1207 (aver (legal-fun-name-p fname))))) ;; FIXME: needless check?
1209 ;;; If the call is in a tail recursive position and the return
1210 ;;; convention is standard, then do a tail full call. If one or fewer
1211 ;;; values are desired, then use a single-value call, otherwise use a
1212 ;;; multiple-values call.
1213 (defun ir2-convert-full-call (node block)
1214 (declare (type combination node) (type ir2-block block))
1215 (ponder-full-call node)
1216 (cond ((node-tail-p node)
1217 (ir2-convert-tail-full-call node block))
1218 ((let ((lvar (node-lvar node)))
1219 (and lvar
1220 (eq (ir2-lvar-kind (lvar-info lvar)) :unknown)))
1221 (ir2-convert-multiple-full-call node block))
1223 (ir2-convert-fixed-full-call node block)))
1224 (values))
1226 ;;;; entering functions
1228 #!+precise-arg-count-error
1229 (defun xep-verify-arg-count (node block fun arg-count-location)
1230 (when (policy fun (plusp verify-arg-count))
1231 (let* ((ef (functional-entry-fun fun))
1232 (optional (optional-dispatch-p ef))
1233 (min (and optional
1234 (optional-dispatch-min-args ef)))
1235 (max (cond ((not optional)
1236 (1- (length (lambda-vars fun))))
1237 ((and optional
1238 (not (optional-dispatch-more-entry ef)))
1239 (optional-dispatch-max-args ef)))))
1240 (unless (and (eql min 0) (not max))
1241 (vop verify-arg-count node block
1242 arg-count-location
1244 max)
1245 min))))
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 (let ((verified (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 #!-x86-64
1267 (declare (ignore verified))
1268 (cond ((and (optional-dispatch-p ef) (optional-dispatch-more-entry ef))
1269 ;; COPY-MORE-ARG should handle SP adjustemnt, but it
1270 ;; isn't done on all targets.
1271 #!-precise-arg-count-error
1272 (vop xep-setup-sp node block)
1273 (vop copy-more-arg node block (optional-dispatch-max-args ef)
1274 #!+x86-64 verified))
1276 (vop xep-setup-sp node block))))
1277 (when (ir2-physenv-closure env)
1278 (let ((closure (make-normal-tn *backend-t-primitive-type*)))
1279 (when (policy fun (> store-closure-debug-pointer 1))
1280 ;; Save the closure pointer on the stack.
1281 (let ((closure-save (make-representation-tn
1282 *backend-t-primitive-type*
1283 (sc-number-or-lose 'sb!vm::control-stack))))
1284 (vop setup-closure-environment node block start-label
1285 closure-save)
1286 (setf (ir2-physenv-closure-save-tn env) closure-save)
1287 (component-live-tn closure-save)))
1288 (vop setup-closure-environment node block start-label closure)
1289 (let ((n -1))
1290 (dolist (loc (ir2-physenv-closure env))
1291 (vop closure-ref node block closure (incf n) (cdr loc)))))))
1292 (unless (eq (functional-kind fun) :toplevel)
1293 (let ((vars (lambda-vars fun))
1294 (n 0))
1295 (when (leaf-refs (first vars))
1296 (emit-move node block arg-count-tn (leaf-info (first vars))))
1297 (dolist (arg (rest vars))
1298 (when (leaf-refs arg)
1299 (let ((pass (standard-arg-location n))
1300 (home (leaf-info arg)))
1301 (if (and (lambda-var-indirect arg)
1302 (lambda-var-explicit-value-cell arg))
1303 (emit-make-value-cell node block pass home)
1304 (emit-move node block pass home))))
1305 (incf n))))
1307 (emit-move node block (make-old-fp-passing-location t)
1308 (ir2-physenv-old-fp env)))
1310 (values))
1312 ;;; Emit function prolog code. This is only called on bind nodes for
1313 ;;; functions that allocate environments. All semantics of let calls
1314 ;;; are handled by IR2-CONVERT-LET.
1316 ;;; If not an XEP, all we do is move the return PC from its passing
1317 ;;; location, since in a local call, the caller allocates the frame
1318 ;;; and sets up the arguments.
1320 #!+unwind-to-frame-and-call-vop
1321 (defun save-bsp (node block env)
1322 ;; Save BSP on stack so that the binding environment can be restored
1323 ;; when restarting frames.
1324 ;; This is done inside functions, which leaves XEPs without saved
1325 ;; BSP, though the code in XEPs doesn't bind any variables, it can
1326 ;; call arbitrary code through the SATISFIES declaration.
1327 ;; And functions called by SATISFIES are not inlined, except for
1328 ;; source transforms, but these usually do not bind anything.
1329 ;; Thus when restarting it needs to check that the interrupt was in
1330 ;; the XEP itself.
1332 ;; It could be saved from the XEP, but some functions have both
1333 ;; external and internal entry points, so it will be saved twice.
1334 (let ((temp (make-normal-tn *backend-t-primitive-type*))
1335 (bsp-save-tn (make-representation-tn
1336 *backend-t-primitive-type*
1337 (sc-number-or-lose 'sb!vm::control-stack))))
1338 (vop current-binding-pointer node block temp)
1339 (emit-move node block temp bsp-save-tn)
1340 (setf (ir2-physenv-bsp-save-tn env) bsp-save-tn)
1341 (component-live-tn bsp-save-tn)))
1343 (defun ir2-convert-bind (node block)
1344 (declare (type bind node) (type ir2-block block))
1345 (let* ((fun (bind-lambda node))
1346 (env (physenv-info (lambda-physenv fun))))
1347 (aver (member (functional-kind fun)
1348 '(nil :external :optional :toplevel :cleanup)))
1350 (cond ((xep-p fun)
1351 (init-xep-environment node block fun)
1352 #!+sb-dyncount
1353 (when *collect-dynamic-statistics*
1354 (vop count-me node block *dynamic-counts-tn*
1355 (block-number (ir2-block-block block)))))
1356 ((policy fun (> store-closure-debug-pointer 1))
1357 ;; Propagate the location of the closure pointer from the
1358 ;; enclosing functions. (FIXME: Should make sure that this
1359 ;; handles closures inside closures correctly). [remark by JES]
1360 (let* ((entry-fun (lambda-entry-fun fun)))
1361 (when entry-fun
1362 (let ((2env (physenv-info (lambda-physenv fun)))
1363 (entry-2env (physenv-info (lambda-physenv entry-fun))))
1364 (setf (ir2-physenv-closure-save-tn 2env)
1365 (ir2-physenv-closure-save-tn entry-2env)))))))
1367 (emit-move node
1368 block
1369 (ir2-physenv-return-pc-pass env)
1370 (ir2-physenv-return-pc env))
1371 #!+unwind-to-frame-and-call-vop
1372 (when (and (lambda-allow-instrumenting fun)
1373 (not (lambda-inline-expanded fun))
1374 (policy fun (>= insert-debug-catch 1)))
1375 (save-bsp node block env))
1377 (let ((lab (gen-label)))
1378 (setf (ir2-physenv-environment-start env) lab)
1379 (vop note-environment-start node block lab)
1380 #!+sb-safepoint
1381 (unless (policy fun (>= inhibit-safepoints 2))
1382 (vop sb!vm::insert-safepoint node block))))
1384 (values))
1386 ;;;; function return
1388 ;;; Do stuff to return from a function with the specified values and
1389 ;;; convention. If the return convention is :FIXED and we aren't
1390 ;;; returning from an XEP, then we do a known return (letting
1391 ;;; representation selection insert the correct move-arg VOPs.)
1392 ;;; Otherwise, we use the unknown-values convention. If there is a
1393 ;;; fixed number of return values, then use RETURN, otherwise use
1394 ;;; RETURN-MULTIPLE.
1395 (defun ir2-convert-return (node block)
1396 (declare (type creturn node) (type ir2-block block))
1397 (let* ((lvar (return-result node))
1398 (2lvar (lvar-info lvar))
1399 (lvar-kind (ir2-lvar-kind 2lvar))
1400 (fun (return-lambda node))
1401 (env (physenv-info (lambda-physenv fun)))
1402 (old-fp (ir2-physenv-old-fp env))
1403 (return-pc (ir2-physenv-return-pc env))
1404 (returns (tail-set-info (lambda-tail-set fun))))
1405 (cond
1406 ((and (eq (return-info-kind returns) :fixed)
1407 (not (xep-p fun)))
1408 (let ((locs (lvar-tns node block lvar
1409 (return-info-types returns))))
1410 (vop* known-return node block
1411 (old-fp return-pc (reference-tn-list locs nil))
1412 (nil)
1413 (return-info-locations returns))))
1414 ((eq lvar-kind :fixed)
1415 (let* ((types (mapcar #'tn-primitive-type (ir2-lvar-locs 2lvar)))
1416 (lvar-locs (lvar-tns node block lvar types))
1417 (nvals (length lvar-locs))
1418 (locs (make-standard-value-tns nvals)))
1419 (mapc (lambda (val loc)
1420 (emit-move node block val loc))
1421 lvar-locs
1422 locs)
1423 (if (= nvals 1)
1424 (vop return-single node block old-fp return-pc (car locs))
1425 (vop* return node block
1426 (old-fp return-pc (reference-tn-list locs nil))
1427 (nil)
1428 nvals))))
1430 (aver (eq lvar-kind :unknown))
1431 (vop* return-multiple node block
1432 (old-fp return-pc
1433 (reference-tn-list (ir2-lvar-locs 2lvar) nil))
1434 (nil)))))
1436 (values))
1438 ;;;; debugger hooks
1439 ;;;;
1440 ;;;; These are used by the debugger to find the top function on the
1441 ;;;; stack. They return the OLD-FP and RETURN-PC for the current
1442 ;;;; function as multiple values.
1444 (defoptimizer (%caller-frame ir2-convert) (() node block)
1445 (let ((ir2-physenv (physenv-info (node-physenv node))))
1446 (move-lvar-result node block
1447 (list (ir2-physenv-old-fp ir2-physenv))
1448 (node-lvar node))))
1450 (defoptimizer (%caller-pc ir2-convert) (() node block)
1451 (let ((ir2-physenv (physenv-info (node-physenv node))))
1452 (move-lvar-result node block
1453 (list (ir2-physenv-return-pc ir2-physenv))
1454 (node-lvar node))))
1456 ;;;; multiple values
1458 ;;; This is almost identical to IR2-CONVERT-LET. Since LTN annotates
1459 ;;; the lvar for the correct number of values (with the lvar user
1460 ;;; responsible for defaulting), we can just pick them up from the
1461 ;;; lvar.
1462 (defun ir2-convert-mv-bind (node block)
1463 (declare (type mv-combination node) (type ir2-block block))
1464 (let* ((fun (ref-leaf (lvar-uses (basic-combination-fun node))))
1465 (args (basic-combination-args node))
1466 (vars (lambda-vars fun)))
1467 (aver (eq (functional-kind fun) :mv-let))
1468 (mapc (lambda (src var)
1469 (when (leaf-refs var)
1470 (let ((dest (leaf-info var)))
1471 (if (and (lambda-var-indirect var)
1472 (lambda-var-explicit-value-cell var))
1473 (emit-make-value-cell node block src dest)
1474 (emit-move node block src dest)))))
1475 (if (singleton-p args)
1476 (lvar-tns node block (first args)
1477 (mapcar (lambda (x)
1478 (primitive-type (leaf-type x)))
1479 vars))
1480 (let ((vars vars))
1481 (loop for lvar in args
1482 for values = (nth-value 1 (values-types
1483 (lvar-derived-type lvar)))
1484 while vars
1485 nconc
1486 (lvar-tns node block lvar (loop repeat values
1487 collect (primitive-type (leaf-type (pop vars))))))))
1488 vars))
1489 (values))
1491 ;;; Emit the appropriate fixed value, unknown value or tail variant of
1492 ;;; CALL-VARIABLE. Note that we only need to pass the values start for
1493 ;;; the first argument: all the other argument lvar TNs are
1494 ;;; ignored. This is because we require all of the values globs to be
1495 ;;; contiguous and on stack top.
1496 (defun ir2-convert-mv-call (node block)
1497 (declare (type mv-combination node) (type ir2-block block))
1498 (aver (basic-combination-args node))
1499 (let* ((start-lvar (lvar-info (first (basic-combination-args node))))
1500 (start (first (ir2-lvar-locs start-lvar)))
1501 (tails (and (node-tail-p node)
1502 (lambda-tail-set (node-home-lambda node))))
1503 (lvar (node-lvar node))
1504 (2lvar (and lvar (lvar-info lvar))))
1505 (multiple-value-bind (fun named)
1506 (fun-lvar-tn node block (basic-combination-fun node))
1507 (aver (and (not named)
1508 (eq (ir2-lvar-kind start-lvar) :unknown)))
1509 (cond
1510 (tails
1511 (let ((env (physenv-info (node-physenv node))))
1512 (vop tail-call-variable node block start fun
1513 (ir2-physenv-old-fp env)
1514 (ir2-physenv-return-pc env))))
1515 ((and 2lvar
1516 (eq (ir2-lvar-kind 2lvar) :unknown))
1517 (vop* multiple-call-variable node block (start fun nil)
1518 ((reference-tn-list (ir2-lvar-locs 2lvar) t))
1519 (emit-step-p node)))
1521 (let ((locs (standard-result-tns lvar)))
1522 (vop* call-variable node block (start fun nil)
1523 ((reference-tn-list locs t)) (length locs)
1524 (emit-step-p node))
1525 (move-lvar-result node block locs lvar)))))))
1527 ;;; Reset the stack pointer to the start of the specified
1528 ;;; unknown-values lvar (discarding it and all values globs on top of
1529 ;;; it.)
1530 (defoptimizer (%pop-values ir2-convert) ((%lvar) node block)
1531 (let* ((lvar (lvar-value %lvar))
1532 (2lvar (lvar-info lvar)))
1533 (cond ((eq (ir2-lvar-kind 2lvar) :unknown)
1534 (vop reset-stack-pointer node block
1535 (first (ir2-lvar-locs 2lvar))))
1536 ((lvar-dynamic-extent lvar)
1537 (vop reset-stack-pointer node block
1538 (ir2-lvar-stack-pointer 2lvar)))
1539 (t (bug "Trying to pop a not stack-allocated LVAR ~S."
1540 lvar)))))
1542 (defoptimizer (%nip-values ir2-convert) ((last-nipped last-preserved
1543 &rest moved)
1544 node block)
1545 (let* ( ;; pointer immediately after the nipped block
1546 (after (lvar-value last-nipped))
1547 (2after (lvar-info after))
1548 ;; pointer to the first nipped word
1549 (first (lvar-value last-preserved))
1550 (2first (lvar-info first))
1552 (moved-tns (loop for lvar-ref in moved
1553 for lvar = (lvar-value lvar-ref)
1554 for 2lvar = (lvar-info lvar)
1555 ;when 2lvar
1556 collect (first (ir2-lvar-locs 2lvar)))))
1557 (aver (or (eq (ir2-lvar-kind 2after) :unknown)
1558 (lvar-dynamic-extent after)))
1559 (aver (eq (ir2-lvar-kind 2first) :unknown))
1560 (when *check-consistency*
1561 ;; we cannot move stack-allocated DX objects
1562 (dolist (moved-lvar moved)
1563 (aver (eq (ir2-lvar-kind (lvar-info (lvar-value moved-lvar)))
1564 :unknown))))
1565 (flet ((nip-aligned (nipped)
1566 (vop* %%nip-values node block
1567 (nipped
1568 (first (ir2-lvar-locs 2first))
1569 (reference-tn-list moved-tns nil))
1570 ((reference-tn-list moved-tns t)))))
1571 (cond ((eq (ir2-lvar-kind 2after) :unknown)
1572 (nip-aligned (first (ir2-lvar-locs 2after))))
1573 ((lvar-dynamic-extent after)
1574 (nip-aligned (ir2-lvar-stack-pointer 2after)))
1576 (bug "Trying to nip a not stack-allocated LVAR ~S." after))))))
1578 (defoptimizer (%dummy-dx-alloc ir2-convert) ((target source) node block)
1579 (let* ((target-lvar (lvar-value target))
1580 (source-lvar (lvar-value source))
1581 (target-2lvar (lvar-info target-lvar))
1582 (source-2lvar (and source-lvar (lvar-info source-lvar))))
1583 (aver (lvar-dynamic-extent target-lvar))
1584 (cond ((not source-lvar)
1585 (vop current-stack-pointer node block
1586 (ir2-lvar-stack-pointer target-2lvar)))
1587 ((lvar-dynamic-extent source-lvar)
1588 (emit-move node block
1589 (ir2-lvar-stack-pointer source-2lvar)
1590 (ir2-lvar-stack-pointer target-2lvar)))
1591 ((eq (ir2-lvar-kind source-2lvar) :unknown)
1592 (emit-move node block
1593 (first (ir2-lvar-locs source-2lvar))
1594 (ir2-lvar-stack-pointer target-2lvar)))
1595 (t (bug "Trying to dummy up DX allocation from a ~
1596 not stack-allocated LVAR ~S." source-lvar)))))
1598 ;;; Deliver the values TNs to LVAR using MOVE-LVAR-RESULT.
1599 (defoptimizer (values ir2-convert) ((&rest values) node block)
1600 (let ((tns (mapcar (lambda (x)
1601 (lvar-tn node block x))
1602 values)))
1604 (move-lvar-result node block tns (node-lvar node))))
1606 ;;; In the normal case where unknown values are desired, we use the
1607 ;;; VALUES-LIST VOP. In the relatively unimportant case of VALUES-LIST
1608 ;;; for a fixed number of values, we punt by doing a full call to the
1609 ;;; VALUES-LIST function. This gets the full call VOP to deal with
1610 ;;; defaulting any unsupplied values. It seems unworthwhile to
1611 ;;; optimize this case.
1612 (defoptimizer (values-list ir2-convert) ((list) node block)
1613 (let* ((lvar (node-lvar node))
1614 (2lvar (and lvar (lvar-info lvar))))
1615 (cond ((and 2lvar
1616 (eq (ir2-lvar-kind 2lvar) :unknown))
1617 (let ((locs (ir2-lvar-locs 2lvar)))
1618 (vop* values-list node block
1619 ((lvar-tn node block list) nil)
1620 ((reference-tn-list locs t)))))
1621 (t (aver (or (not 2lvar) ; i.e. we want to check the argument
1622 (eq (ir2-lvar-kind 2lvar) :fixed)))
1623 (ir2-convert-full-call node block)))))
1625 (defoptimizer (%more-arg-values ir2-convert) ((context start count) node block)
1626 (binding* ((lvar (node-lvar node) :exit-if-null)
1627 (2lvar (lvar-info lvar)))
1628 (ecase (ir2-lvar-kind 2lvar)
1629 (:fixed
1630 ;; KLUDGE: this is very much unsafe, and can leak random stack values.
1631 ;; OTOH, I think the :FIXED case can only happen with (safety 0) in the
1632 ;; first place.
1633 ;; -PK
1634 (loop for loc in (ir2-lvar-locs 2lvar)
1635 for idx upfrom 0
1636 do (vop sb!vm::more-arg node block
1637 (lvar-tn node block context)
1638 (emit-constant idx)
1639 loc)))
1640 (:unknown
1641 (let ((locs (ir2-lvar-locs 2lvar)))
1642 (vop* %more-arg-values node block
1643 ((lvar-tn node block context)
1644 (lvar-tn node block start)
1645 (lvar-tn node block count)
1646 nil)
1647 ((reference-tn-list locs t))))))))
1649 ;;;; special binding
1651 ;;; This is trivial, given our assumption of a shallow-binding
1652 ;;; implementation.
1653 (defoptimizer (%special-bind ir2-convert) ((var value) node block)
1654 (let ((name (leaf-source-name (lvar-value var))))
1655 ;; Emit either BIND or DYNBIND, preferring BIND if both exist.
1656 ;; If only one exists, it's DYNBIND.
1657 ;; Even if the backend supports load-time TLS index assignment,
1658 ;; there might be only one vop (as with arm64).
1659 (macrolet ((doit (bind dynbind)
1660 (if (gethash 'bind *backend-parsed-vops*) bind dynbind)))
1661 (doit
1662 (progn
1663 ;; Inform later SYMBOL-VALUE calls that they can
1664 ;; assume a nonzero tls-index.
1665 ;; FIXME: setting INFO is inefficient when not actually
1666 ;; changing anything
1667 (unless (info :variable :wired-tls name)
1668 (setf (info :variable :wired-tls name) :always-has-tls))
1669 ;; We force the symbol into the code constants in case BIND
1670 ;; does not actually reference it, as with x86.
1671 (emit-constant name)
1672 (vop bind node block (lvar-tn node block value) name))
1673 (vop dynbind node block (lvar-tn node block value)
1674 (emit-constant name))))))
1676 (defoptimizer (%special-unbind ir2-convert) ((n) node block)
1677 (declare (ignorable n))
1678 (vop unbind node block #!+(and sb-thread unbind-n-vop) (lvar-value n)))
1680 ;;; ### It's not clear that this really belongs in this file, or
1681 ;;; should really be done this way, but this is the least violation of
1682 ;;; abstraction in the current setup. We don't want to wire
1683 ;;; shallow-binding assumptions into IR1tran.
1684 (def-ir1-translator progv
1685 ((vars vals &body body) start next result)
1686 (ir1-convert
1687 start next result
1688 (with-unique-names (bind unbind)
1689 (once-only ((n-save-bs '(%primitive current-binding-pointer)))
1690 `(unwind-protect
1691 (progn
1692 (labels ((,unbind (vars)
1693 (declare (optimize (speed 2) (debug 0)))
1694 (let ((unbound-marker (%primitive make-unbound-marker)))
1695 (dolist (var vars)
1696 ;; CLHS says "bound and then made to have no value" -- user
1697 ;; should not be able to tell the difference between that and this.
1698 (about-to-modify-symbol-value var 'progv)
1699 (%primitive dynbind unbound-marker var))))
1700 (,bind (vars vals)
1701 (declare (optimize (speed 2) (debug 0)
1702 (insert-debug-catch 0)))
1703 (cond ((null vars))
1704 ((null vals) (,unbind vars))
1706 (let ((val (car vals))
1707 (var (car vars)))
1708 (about-to-modify-symbol-value var 'progv val t)
1709 (%primitive dynbind val var))
1710 (,bind (cdr vars) (cdr vals))))))
1711 (,bind ,vars ,vals))
1713 ,@body)
1714 ;; Technically ANSI CL doesn't allow declarations at the
1715 ;; start of the cleanup form. SBCL happens to allow for
1716 ;; them, due to the way the UNWIND-PROTECT ir1 translation
1717 ;; is implemented; the cleanup forms are directly spliced
1718 ;; into an FLET definition body. And a declaration here
1719 ;; actually has exactly the right scope for what we need
1720 ;; (ensure that debug instrumentation is not emitted for the
1721 ;; cleanup function). -- JES, 2007-06-16
1722 (declare (optimize (insert-debug-catch 0)))
1723 (%primitive unbind-to-here ,n-save-bs))))))
1725 ;;;; non-local exit
1727 ;;; Convert a non-local lexical exit. First find the NLX-INFO in our
1728 ;;; environment. Note that this is never called on the escape exits
1729 ;;; for CATCH and UNWIND-PROTECT, since the escape functions aren't
1730 ;;; IR2 converted.
1731 (defun ir2-convert-exit (node block)
1732 (declare (type exit node) (type ir2-block block))
1733 (let* ((nlx (exit-nlx-info node))
1734 (loc (find-in-physenv nlx (node-physenv node)))
1735 (temp (make-stack-pointer-tn))
1736 (value (exit-value node)))
1737 (if (nlx-info-safe-p nlx)
1738 (vop value-cell-ref node block loc temp)
1739 (emit-move node block loc temp))
1740 (if value
1741 (let ((locs (ir2-lvar-locs (lvar-info value))))
1742 (vop unwind node block temp (first locs) (second locs)))
1743 (let ((0-tn (emit-constant 0)))
1744 (vop unwind node block temp 0-tn 0-tn))))
1746 (values))
1748 ;;; %CLEANUP-POINT doesn't do anything except prevent the body from
1749 ;;; being entirely deleted.
1750 (defoptimizer (%cleanup-point ir2-convert) (() node block) node block)
1752 ;;; This function invalidates a lexical exit on exiting from the
1753 ;;; dynamic extent. This is done by storing 0 into the indirect value
1754 ;;; cell that holds the closed unwind block.
1755 (defoptimizer (%lexical-exit-breakup ir2-convert) ((info) node block)
1756 (let ((nlx (lvar-value info)))
1757 (when (nlx-info-safe-p nlx)
1758 (vop value-cell-set node block
1759 (find-in-physenv nlx (node-physenv node))
1760 (emit-constant 0)))))
1762 ;;; We have to do a spurious move of no values to the result lvar so
1763 ;;; that lifetime analysis won't get confused.
1764 (defun ir2-convert-throw (node block)
1765 (declare (type mv-combination node) (type ir2-block block))
1766 (let ((args (basic-combination-args node)))
1767 (check-catch-tag-type (first args))
1768 (vop* throw node block
1769 ((lvar-tn node block (first args))
1770 (reference-tn-list
1771 (ir2-lvar-locs (lvar-info (second args)))
1772 nil))
1773 (nil)))
1774 (move-lvar-result node block () (node-lvar node))
1775 (values))
1777 ;;; Emit code to set up a non-local exit. INFO is the NLX-INFO for the
1778 ;;; exit, and TAG is the lvar for the catch tag (if any.) We get at
1779 ;;; the target PC by passing in the label to the vop. The vop is
1780 ;;; responsible for building a return-PC object.
1781 (defun emit-nlx-start (node block info tag)
1782 (declare (type node node) (type ir2-block block) (type nlx-info info)
1783 (type (or lvar null) tag))
1784 (let* ((2info (nlx-info-info info))
1785 (kind (cleanup-kind (nlx-info-cleanup info)))
1786 (block-tn (physenv-live-tn
1787 (make-normal-tn
1788 (primitive-type-or-lose
1789 (ecase kind
1790 (:catch
1791 'catch-block)
1792 ((:unwind-protect :block :tagbody)
1793 'unwind-block))))
1794 (node-physenv node)))
1795 (res (make-stack-pointer-tn))
1796 (target-label (ir2-nlx-info-target 2info)))
1798 (vop current-binding-pointer node block
1799 (car (ir2-nlx-info-dynamic-state 2info)))
1800 (vop* save-dynamic-state node block
1801 (nil)
1802 ((reference-tn-list (cdr (ir2-nlx-info-dynamic-state 2info)) t)))
1803 (vop current-stack-pointer node block (ir2-nlx-info-save-sp 2info))
1805 (ecase kind
1806 (:catch
1807 (vop make-catch-block node block block-tn
1808 (lvar-tn node block tag) target-label res))
1809 ((:unwind-protect :block :tagbody)
1810 (vop make-unwind-block node block block-tn target-label res)))
1812 (ecase kind
1813 ((:block :tagbody)
1814 (if (nlx-info-safe-p info)
1815 (emit-make-value-cell node block res (ir2-nlx-info-home 2info))
1816 (emit-move node block res (ir2-nlx-info-home 2info))))
1817 (:unwind-protect
1818 (vop set-unwind-protect node block block-tn))
1819 (:catch)))
1821 (values))
1823 ;;; Scan each of ENTRY's exits, setting up the exit for each lexical exit.
1824 (defun ir2-convert-entry (node block)
1825 (declare (type entry node) (type ir2-block block))
1826 (let ((nlxes '()))
1827 (dolist (exit (entry-exits node))
1828 (let ((info (exit-nlx-info exit)))
1829 (when (and info
1830 (not (memq info nlxes))
1831 (member (cleanup-kind (nlx-info-cleanup info))
1832 '(:block :tagbody)))
1833 (push info nlxes)
1834 (emit-nlx-start node block info nil)))))
1835 (values))
1837 ;;; Set up the unwind block for these guys.
1838 (defoptimizer (%catch ir2-convert) ((info-lvar tag) node block)
1839 (check-catch-tag-type tag)
1840 (emit-nlx-start node block (lvar-value info-lvar) tag))
1841 (defoptimizer (%unwind-protect ir2-convert) ((info-lvar cleanup) node block)
1842 (declare (ignore cleanup))
1843 (emit-nlx-start node block (lvar-value info-lvar) nil))
1845 ;;; Emit the entry code for a non-local exit. We receive values and
1846 ;;; restore dynamic state.
1848 ;;; In the case of a lexical exit or CATCH, we look at the exit lvar's
1849 ;;; kind to determine which flavor of entry VOP to emit. If unknown
1850 ;;; values, emit the xxx-MULTIPLE variant to the lvar locs. If fixed
1851 ;;; values, make the appropriate number of temps in the standard
1852 ;;; values locations and use the other variant, delivering the temps
1853 ;;; to the lvar using MOVE-LVAR-RESULT.
1855 ;;; In the UNWIND-PROTECT case, we deliver the first register
1856 ;;; argument, the argument count and the argument pointer to our lvar
1857 ;;; as multiple values. These values are the block exited to and the
1858 ;;; values start and count.
1860 ;;; After receiving values, we restore dynamic state. Except in the
1861 ;;; UNWIND-PROTECT case, the values receiving restores the stack
1862 ;;; pointer. In an UNWIND-PROTECT cleanup, we want to leave the stack
1863 ;;; pointer alone, since the thrown values are still out there.
1864 (defoptimizer (%nlx-entry ir2-convert) ((info-lvar) node block)
1865 (let* ((info (lvar-value info-lvar))
1866 (lvar (node-lvar node))
1867 (2info (nlx-info-info info))
1868 (top-loc (ir2-nlx-info-save-sp 2info))
1869 (start-loc (make-nlx-entry-arg-start-location))
1870 (count-loc (make-arg-count-location))
1871 (target (ir2-nlx-info-target 2info)))
1873 (ecase (cleanup-kind (nlx-info-cleanup info))
1874 ((:catch :block :tagbody)
1875 (let ((2lvar (and lvar (lvar-info lvar))))
1876 (if (and 2lvar (eq (ir2-lvar-kind 2lvar) :unknown))
1877 (vop* nlx-entry-multiple node block
1878 (top-loc start-loc count-loc nil)
1879 ((reference-tn-list (ir2-lvar-locs 2lvar) t))
1880 target)
1881 (let ((locs (standard-result-tns lvar)))
1882 (vop* nlx-entry node block
1883 (top-loc start-loc count-loc nil)
1884 ((reference-tn-list locs t))
1885 target
1886 (length locs))
1887 (move-lvar-result node block locs lvar)))))
1888 (:unwind-protect
1889 (let ((block-loc (standard-arg-location 0)))
1890 (vop uwp-entry node block target block-loc start-loc count-loc)
1891 (move-lvar-result
1892 node block
1893 (list block-loc start-loc count-loc)
1894 lvar))))
1896 #!+sb-dyncount
1897 (when *collect-dynamic-statistics*
1898 (vop count-me node block *dynamic-counts-tn*
1899 (block-number (ir2-block-block block))))
1901 (vop* restore-dynamic-state node block
1902 ((reference-tn-list (cdr (ir2-nlx-info-dynamic-state 2info)) nil))
1903 (nil))
1904 (vop unbind-to-here node block
1905 (car (ir2-nlx-info-dynamic-state 2info)))))
1907 ;;;; n-argument functions
1909 (macrolet ((def (name)
1910 `(defoptimizer (,name ir2-convert) ((&rest args) node block)
1911 (cond #!+gencgc
1912 ((>= (length args)
1913 (/ sb!vm:large-object-size
1914 (* sb!vm:n-word-bytes 2)))
1915 ;; The VOPs will try to allocate all space at once
1916 ;; And it'll end up in large objects, and no conses
1917 ;; are welcome there.
1918 (ir2-convert-full-call node block))
1920 (let* ((refs (reference-tn-list
1921 (loop for arg in args
1922 for tn = (make-normal-tn *backend-t-primitive-type*)
1924 (emit-move node block (lvar-tn node block arg) tn)
1925 collect tn)
1926 nil))
1927 (lvar (node-lvar node))
1928 (res (lvar-result-tns
1929 lvar
1930 (list (primitive-type (specifier-type 'list))))))
1931 (when (and lvar (lvar-dynamic-extent lvar))
1932 (vop current-stack-pointer node block
1933 (ir2-lvar-stack-pointer (lvar-info lvar))))
1934 (vop* ,name node block (refs) ((first res) nil)
1935 (length args))
1936 (move-lvar-result node block res lvar)))))))
1937 (def list)
1938 (def list*))
1941 (defoptimizer (mask-signed-field ir2-convert) ((width x) node block)
1942 (block nil
1943 (when (constant-lvar-p width)
1944 (case (lvar-value width)
1945 (#.(- sb!vm:n-word-bits sb!vm:n-fixnum-tag-bits)
1946 (when (or (csubtypep (lvar-type x)
1947 (specifier-type 'word))
1948 (csubtypep (lvar-type x)
1949 (specifier-type 'sb!vm:signed-word)))
1950 (let* ((lvar (node-lvar node))
1951 (temp (make-normal-tn
1952 (if (csubtypep (lvar-type x)
1953 (specifier-type 'word))
1954 (primitive-type-of most-positive-word)
1955 (primitive-type-of
1956 (- (ash most-positive-word -1))))))
1957 (results (lvar-result-tns
1958 lvar
1959 (list (primitive-type-or-lose 'fixnum)))))
1960 (emit-move node block (lvar-tn node block x) temp)
1961 (vop sb!vm::move-from-word/fixnum node block
1962 temp (first results))
1963 (move-lvar-result node block results lvar)
1964 (return))))
1965 (#.sb!vm:n-word-bits
1966 (when (csubtypep (lvar-type x) (specifier-type 'word))
1967 (let* ((lvar (node-lvar node))
1968 (temp (make-normal-tn
1969 (primitive-type-of most-positive-word)))
1970 (results (lvar-result-tns
1971 lvar
1972 (list (primitive-type
1973 (specifier-type 'sb!vm:signed-word))))))
1974 (emit-move node block (lvar-tn node block x) temp)
1975 (vop sb!vm::word-move node block
1976 temp (first results))
1977 (move-lvar-result node block results lvar)
1978 (return))))))
1979 (if (template-p (basic-combination-info node))
1980 (ir2-convert-template node block)
1981 (ir2-convert-full-call node block))))
1983 ;; just a fancy identity
1984 (defoptimizer (%typep-wrapper ir2-convert) ((value variable type) node block)
1985 (declare (ignore variable type))
1986 (let* ((lvar (node-lvar node))
1987 (results (lvar-result-tns lvar (list (primitive-type-or-lose t)))))
1988 (emit-move node block (lvar-tn node block value) (first results))
1989 (move-lvar-result node block results lvar)))
1991 ;;; An identity to avoid complaints about constant modification
1992 (defoptimizer (ltv-wrapper ir2-convert) ((x) node block)
1993 (let* ((lvar (node-lvar node))
1994 (results (lvar-result-tns lvar (list (primitive-type-or-lose t)))))
1995 (emit-move node block (lvar-tn node block x) (first results))
1996 (move-lvar-result node block results lvar)))
1998 #-sb-xc-host ;; package-lock-violation-p is not present yet
1999 (defoptimizer (set ir2-hook) ((symbol value) node block)
2000 (declare (ignore value block))
2001 (when (constant-lvar-p symbol)
2002 (let* ((symbol (lvar-value symbol))
2003 (kind (info :variable :kind symbol)))
2004 (when (and (eq kind :unknown)
2005 (sb!impl::package-lock-violation-p (symbol-package symbol) symbol))
2006 (let ((*compiler-error-context* node))
2007 (compiler-warn "violating package lock on ~/sb-impl:print-symbol-with-prefix/"
2008 symbol))))))
2010 ;;; Convert the code in a component into VOPs.
2011 (defun ir2-convert (component)
2012 (declare (type component component))
2013 (let (#!+sb-dyncount
2014 (*dynamic-counts-tn*
2015 (when *collect-dynamic-statistics*
2016 (let* ((blocks
2017 (block-number (block-next (component-head component))))
2018 (counts (make-array blocks
2019 :element-type '(unsigned-byte 32)
2020 :initial-element 0))
2021 (info (make-dyncount-info
2022 :for (component-name component)
2023 :costs (make-array blocks
2024 :element-type '(unsigned-byte 32)
2025 :initial-element 0)
2026 :counts counts)))
2027 (setf (ir2-component-dyncount-info (component-info component))
2028 info)
2029 (emit-constant info)
2030 (emit-constant counts)))))
2031 (let ((num 0))
2032 (declare (type index num))
2033 (do-ir2-blocks (2block component)
2034 (let ((block (ir2-block-block 2block)))
2035 (when (block-start block)
2036 (setf (block-number block) num)
2037 #!+sb-dyncount
2038 (when *collect-dynamic-statistics*
2039 (let ((first-node (block-start-node block)))
2040 (unless (or (and (bind-p first-node)
2041 (xep-p (bind-lambda first-node)))
2042 (eq (lvar-fun-name
2043 (node-lvar first-node))
2044 '%nlx-entry))
2045 (vop count-me
2046 first-node
2047 2block
2048 #!+sb-dyncount *dynamic-counts-tn* #!-sb-dyncount nil
2049 num))))
2050 #!+sb-safepoint
2051 (let ((first-node (block-start-node block)))
2052 (unless (or (and (bind-p first-node)
2053 ;; Bind-nodes already have safepoints
2054 (eq (bind-lambda first-node)
2055 (lambda-home (bind-lambda first-node))))
2056 (and (valued-node-p first-node)
2057 (node-lvar first-node)
2058 (eq (lvar-fun-name
2059 (node-lvar first-node))
2060 '%nlx-entry)))
2061 (when (and (rest (block-pred block))
2062 (block-loop block)
2063 (member (loop-kind (block-loop block))
2064 '(:natural :strange))
2065 (eq block (loop-head (block-loop block)))
2066 (policy first-node (< inhibit-safepoints 2)))
2067 (vop sb!vm::insert-safepoint first-node 2block))))
2068 (ir2-convert-block block)
2069 (incf num))))))
2070 (values))
2072 ;;; If necessary, emit a terminal unconditional branch to go to the
2073 ;;; successor block. If the successor is the component tail, then
2074 ;;; there isn't really any successor, but if the end is a non-tail
2075 ;;; call to a function that's not *known* to never return, then we
2076 ;;; emit an error trap just in case the function really does return.
2078 ;;; Trapping after known calls makes it easier to understand type
2079 ;;; derivation bugs at runtime: they show up as nil-fun-returned-error,
2080 ;;; rather than the execution of arbitrary code or error traps.
2081 (defun finish-ir2-block (block)
2082 (declare (type cblock block))
2083 (let* ((2block (block-info block))
2084 (last (block-last block))
2085 (succ (block-succ block)))
2086 (unless (if-p last)
2087 (aver (singleton-p succ))
2088 (let ((target (first succ)))
2089 (cond ((eq target (component-tail (block-component block)))
2090 (when (and (basic-combination-p last)
2091 (or (eq (basic-combination-kind last) :full)
2092 (and (eq (basic-combination-kind last) :known)
2093 (eq (basic-combination-info last) :full))))
2094 (let* ((fun (basic-combination-fun last))
2095 (use (lvar-uses fun))
2096 (name (and (ref-p use)
2097 (leaf-has-source-name-p (ref-leaf use))
2098 (leaf-source-name (ref-leaf use))))
2099 (ftype (and (info :function :info name) ; only use the FTYPE if
2100 (proclaimed-ftype name)))) ; NAME was DEFKNOWN
2101 (unless (or (node-tail-p last)
2102 (policy last (zerop safety))
2103 (and (fun-type-p ftype)
2104 (eq *empty-type* (fun-type-returns ftype))))
2105 (vop nil-fun-returned-error last 2block
2106 (if name
2107 (emit-constant name)
2108 (multiple-value-bind (tn named)
2109 (fun-lvar-tn last 2block fun)
2110 (aver (not named))
2111 tn)))))))
2112 ((not (eq (ir2-block-next 2block) (block-info target)))
2113 (vop branch last 2block (block-label target)))
2115 (register-drop-thru target))))))
2117 (values))
2119 ;;; Convert the code in a block into VOPs.
2120 (defun ir2-convert-block (block)
2121 (declare (type cblock block))
2122 (let ((2block (block-info block)))
2123 (do-nodes (node lvar block)
2124 (etypecase node
2125 (ref
2126 (when lvar
2127 (let ((2lvar (lvar-info lvar)))
2128 ;; function REF in a local call is not annotated
2129 (when (and 2lvar (not (eq (ir2-lvar-kind 2lvar) :delayed)))
2130 (ir2-convert-ref node 2block)))))
2131 (combination
2132 (let ((kind (basic-combination-kind node)))
2133 (ecase kind
2134 (:local
2135 (ir2-convert-local-call node 2block))
2136 (:full
2137 (ir2-convert-full-call node 2block))
2138 (:known
2139 (let* ((info (basic-combination-fun-info node))
2140 (fun (fun-info-ir2-convert info))
2141 (hook (fun-info-ir2-hook info)))
2142 (when hook
2143 (funcall hook node 2block))
2144 (cond (fun
2145 (funcall fun node 2block))
2146 ((eq (basic-combination-info node) :full)
2147 (ir2-convert-full-call node 2block))
2149 (ir2-convert-template node 2block))))))))
2150 (cif
2151 (when (lvar-info (if-test node))
2152 (ir2-convert-if node 2block)))
2153 (bind
2154 (let ((fun (bind-lambda node)))
2155 (when (eq (lambda-home fun) fun)
2156 (ir2-convert-bind node 2block))))
2157 (creturn
2158 (ir2-convert-return node 2block))
2159 (cset
2160 (ir2-convert-set node 2block))
2161 (cast
2162 (ir2-convert-cast node 2block))
2163 (mv-combination
2164 (cond
2165 ((eq (basic-combination-kind node) :local)
2166 (ir2-convert-mv-bind node 2block))
2167 ((eq (lvar-fun-name (basic-combination-fun node))
2168 '%throw)
2169 (ir2-convert-throw node 2block))
2171 (ir2-convert-mv-call node 2block))))
2172 (exit
2173 (when (exit-entry node)
2174 (ir2-convert-exit node 2block)))
2175 (entry
2176 (ir2-convert-entry node 2block)))))
2178 (finish-ir2-block block)
2180 (values))