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