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