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