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