0.6.12.3:
[sbcl.git] / src / compiler / alpha / call.lisp
1 ;;;
2 ;;; **********************************************************************
3 ;;; This code was written as part of the CMU Common Lisp project at
4 ;;; Carnegie Mellon University, and has been placed in the public domain.
5 ;;;
6
7 ;;;
8 ;;; **********************************************************************
9 ;;;
10 ;;;    This file contains the VM definition of function call for the Alpha.
11 ;;;
12 ;;; Written by Rob MacLachlan
13 ;;;
14 ;;; Converted for the Alpha by Sean Hallgren
15 ;;;
16 (in-package "SB!VM")
17
18
19 \f
20 ;;;; Interfaces to IR2 conversion:
21
22 ;;; Standard-Argument-Location  --  Interface
23 ;;;
24 ;;;    Return a wired TN describing the N'th full call argument passing
25 ;;; location.
26 ;;;
27 (!def-vm-support-routine standard-argument-location (n)
28   (declare (type unsigned-byte n))
29   (if (< n register-arg-count)
30       (make-wired-tn *backend-t-primitive-type*
31                      register-arg-scn
32                      (elt *register-arg-offsets* n))
33       (make-wired-tn *backend-t-primitive-type*
34                      control-stack-arg-scn n)))
35
36
37 ;;; Make-Return-PC-Passing-Location  --  Interface
38 ;;;
39 ;;;    Make a passing location TN for a local call return PC.  If standard is
40 ;;; true, then use the standard (full call) location, otherwise use any legal
41 ;;; location.  Even in the non-standard case, this may be restricted by a
42 ;;; desire to use a subroutine call instruction.
43 ;;;
44 (!def-vm-support-routine make-return-pc-passing-location (standard)
45   #!+gengc (declare (ignore standard))
46   #!-gengc
47   (if standard
48       (make-wired-tn *backend-t-primitive-type* register-arg-scn lra-offset)
49       (make-restricted-tn *backend-t-primitive-type* register-arg-scn))
50   #!+gengc
51   (make-wired-tn *fixnum-primitive-type* immediate-arg-scn ra-offset))
52
53 ;;; Make-Old-FP-Passing-Location  --  Interface
54 ;;;
55 ;;;    Similar to Make-Return-PC-Passing-Location, but makes a location to pass
56 ;;; Old-FP in.  This is (obviously) wired in the standard convention, but is
57 ;;; totally unrestricted in non-standard conventions, since we can always fetch
58 ;;; it off of the stack using the arg pointer.
59 ;;;
60 (!def-vm-support-routine make-old-fp-passing-location (standard)
61   (if standard
62       (make-wired-tn *fixnum-primitive-type* immediate-arg-scn ocfp-offset)
63       (make-normal-tn *fixnum-primitive-type*)))
64
65 ;;; Make-Old-FP-Save-Location, Make-Return-PC-Save-Location  --  Interface
66 ;;;
67 ;;;    Make the TNs used to hold Old-FP and Return-PC within the current
68 ;;; function.  We treat these specially so that the debugger can find them at a
69 ;;; known location.
70 ;;;
71 (!def-vm-support-routine make-old-fp-save-location (env)
72   (specify-save-tn
73    (environment-debug-live-tn (make-normal-tn *fixnum-primitive-type*) env)
74    (make-wired-tn *fixnum-primitive-type*
75                   control-stack-arg-scn
76                   ocfp-save-offset)))
77 ;;;
78 (!def-vm-support-routine make-return-pc-save-location (env)
79   (let ((ptype #!-gengc *backend-t-primitive-type*
80                #!+gengc *fixnum-primitive-type*))
81     (specify-save-tn
82      (environment-debug-live-tn (make-normal-tn ptype) env)
83      (make-wired-tn ptype control-stack-arg-scn
84                     #!-gengc lra-save-offset #!+gengc ra-save-offset))))
85
86 ;;; Make-Argument-Count-Location  --  Interface
87 ;;;
88 ;;;    Make a TN for the standard argument count passing location.  We only
89 ;;; need to make the standard location, since a count is never passed when we
90 ;;; are using non-standard conventions.
91 ;;;
92 (!def-vm-support-routine make-argument-count-location ()
93   (make-wired-tn *fixnum-primitive-type* immediate-arg-scn nargs-offset))
94
95
96 ;;; MAKE-NFP-TN  --  Interface
97 ;;;
98 ;;;    Make a TN to hold the number-stack frame pointer.  This is allocated
99 ;;; once per component, and is component-live.
100 ;;;
101 (!def-vm-support-routine make-nfp-tn ()
102   (component-live-tn
103    (make-wired-tn *fixnum-primitive-type* immediate-arg-scn nfp-offset)))
104
105 ;;; MAKE-STACK-POINTER-TN ()
106 ;;; 
107 (!def-vm-support-routine make-stack-pointer-tn ()
108   (make-normal-tn *fixnum-primitive-type*))
109
110 ;;; MAKE-NUMBER-STACK-POINTER-TN ()
111 ;;; 
112 (!def-vm-support-routine make-number-stack-pointer-tn ()
113   (make-normal-tn *fixnum-primitive-type*))
114
115 ;;; Make-Unknown-Values-Locations  --  Interface
116 ;;;
117 ;;;    Return a list of TNs that can be used to represent an unknown-values
118 ;;; continuation within a function.
119 ;;;
120 (!def-vm-support-routine make-unknown-values-locations ()
121   (list (make-stack-pointer-tn)
122         (make-normal-tn *fixnum-primitive-type*)))
123
124
125 ;;; Select-Component-Format  --  Interface
126 ;;;
127 ;;;    This function is called by the Entry-Analyze phase, allowing
128 ;;; VM-dependent initialization of the IR2-Component structure.  We push
129 ;;; placeholder entries in the Constants to leave room for additional
130 ;;; noise in the code object header.
131 ;;;
132 (!def-vm-support-routine select-component-format (component)
133   (declare (type component component))
134   (dotimes (i code-constants-offset)
135     (vector-push-extend nil
136                         (ir2-component-constants (component-info component))))
137   (values))
138
139 \f
140 ;;;; Frame hackery:
141
142 ;;; BYTES-NEEDED-FOR-NON-DESCRIPTOR-STACK-FRAME -- internal
143 ;;;
144 ;;; Return the number of bytes needed for the current non-descriptor stack
145 ;;; frame.  Non-descriptor stack frames must be multiples of 8 bytes on
146 ;;; the PMAX.
147 ;;; 
148 (defun bytes-needed-for-non-descriptor-stack-frame ()
149   (* (logandc2 (1+ (sb-allocated-size 'non-descriptor-stack)) 1)
150      word-bytes))
151
152 ;;; Used for setting up the Old-FP in local call.
153 ;;;
154 (define-vop (current-fp)
155   (:results (val :scs (any-reg)))
156   (:generator 1
157     (move cfp-tn val)))
158
159 ;;; Used for computing the caller's NFP for use in known-values return.  Only
160 ;;; works assuming there is no variable size stuff on the nstack.
161 ;;;
162 (define-vop (compute-old-nfp)
163   (:results (val :scs (any-reg)))
164   (:vop-var vop)
165   (:generator 1
166     (let ((nfp (current-nfp-tn vop)))
167       (when nfp
168         (inst addq nfp (bytes-needed-for-non-descriptor-stack-frame) val)))))
169
170
171 (define-vop (xep-allocate-frame)
172   (:info start-lab copy-more-arg-follows)
173   (:ignore copy-more-arg-follows)
174   (:vop-var vop)
175   (:temporary (:scs (non-descriptor-reg)) temp)
176   (:generator 1
177     ;; Make sure the function is aligned, and drop a label pointing to this
178     ;; function header.
179     (align lowtag-bits)
180     (trace-table-entry trace-table-function-prologue)
181     (emit-label start-lab)
182     ;; Allocate function header.
183     (inst function-header-word)
184     (dotimes (i (1- function-code-offset))
185       (inst lword 0))
186     ;; The start of the actual code.
187     ;; Compute CODE from the address of this entry point.
188     (let ((entry-point (gen-label)))
189       (emit-label entry-point)
190       (inst compute-code-from-fn code-tn lip-tn entry-point temp)
191       ;; ### We should also save it on the stack so that the garbage collector
192       ;; won't forget about us if we call anyone else.
193       )
194     ;; Build our stack frames.
195     (inst lda csp-tn (* word-bytes (sb-allocated-size 'control-stack)) cfp-tn)
196     (let ((nfp (current-nfp-tn vop)))
197       (when nfp
198         (inst subq nsp-tn (bytes-needed-for-non-descriptor-stack-frame)
199               nsp-tn)
200         (move nsp-tn nfp)))
201     (trace-table-entry trace-table-normal)))
202
203 (define-vop (allocate-frame)
204   (:results (res :scs (any-reg))
205             (nfp :scs (any-reg)))
206   (:info callee)
207   (:generator 2
208     (trace-table-entry trace-table-function-prologue)
209     (move csp-tn res)
210     (inst lda csp-tn (* word-bytes (sb-allocated-size 'control-stack)) csp-tn)
211     (when (ir2-environment-number-stack-p callee)
212       (inst subq nsp-tn (bytes-needed-for-non-descriptor-stack-frame)
213             nsp-tn)
214       (move nsp-tn nfp))
215     (trace-table-entry trace-table-normal)))
216
217 ;;; Allocate a partial frame for passing stack arguments in a full call.  Nargs
218 ;;; is the number of arguments passed.  If no stack arguments are passed, then
219 ;;; we don't have to do anything.
220 ;;;
221 (define-vop (allocate-full-call-frame)
222   (:info nargs)
223   (:results (res :scs (any-reg)))
224   (:generator 2
225     (when (> nargs register-arg-count)
226       (move csp-tn res)
227       (inst lda csp-tn (* nargs word-bytes) csp-tn))))
228
229
230 \f
231 ;;; Default-Unknown-Values  --  Internal
232 ;;;
233 ;;;    Emit code needed at the return-point from an unknown-values call for a
234 ;;; fixed number of values.  Values is the head of the TN-Ref list for the
235 ;;; locations that the values are to be received into.  Nvals is the number of
236 ;;; values that are to be received (should equal the length of Values).
237 ;;;
238 ;;;    Move-Temp is a Descriptor-Reg TN used as a temporary.
239 ;;;
240 ;;;    This code exploits the fact that in the unknown-values convention, a
241 ;;; single value return returns at the return PC + 8, whereas a return of other
242 ;;; than one value returns directly at the return PC.
243 ;;;
244 ;;;    If 0 or 1 values are expected, then we just emit an instruction to reset
245 ;;; the SP (which will only be executed when other than 1 value is returned.)
246 ;;;
247 ;;; In the general case, we have to do three things:
248 ;;;  -- Default unsupplied register values.  This need only be done when a
249 ;;;     single value is returned, since register values are defaulted by the
250 ;;;     called in the non-single case.
251 ;;;  -- Default unsupplied stack values.  This needs to be done whenever there
252 ;;;     are stack values.
253 ;;;  -- Reset SP.  This must be done whenever other than 1 value is returned,
254 ;;;     regardless of the number of values desired.
255 ;;;
256 ;;; The general-case code looks like this:
257 #|
258         b regs-defaulted                ; Skip if MVs
259         nop
260
261         move a1 null-tn                 ; Default register values
262         ...
263         loadi nargs 1                   ; Force defaulting of stack values
264         move ocfp csp                   ; Set up args for SP resetting
265
266 regs-defaulted
267         subu temp nargs register-arg-count
268
269         bltz temp default-value-7       ; jump to default code
270         addu temp temp -1
271         loadw move-temp ocfp-tn 6       ; Move value to correct location.
272         store-stack-tn val4-tn move-temp
273
274         bltz temp default-value-8
275         addu temp temp -1
276         loadw move-temp ocfp-tn 7
277         store-stack-tn val5-tn move-temp
278
279         ...
280
281 defaulting-done
282         move sp ocfp                    ; Reset SP.
283 <end of code>
284
285 <elsewhere>
286 default-value-7
287         store-stack-tn val4-tn null-tn  ; Nil out 7'th value. (first on stack)
288
289 default-value-8
290         store-stack-tn val5-tn null-tn  ; Nil out 8'th value.
291
292         ...
293
294         br defaulting-done
295         nop
296 |#
297 ;;;
298 (defun default-unknown-values (vop values nvals move-temp temp lra-label)
299   (declare (type (or tn-ref null) values)
300            (type unsigned-byte nvals) (type tn move-temp temp))
301   (if (<= nvals 1)
302       (progn
303         ;; Note that this is a single-value return point.  This is actually
304         ;; the multiple-value entry point for a single desired value, but
305         ;; the code location has to be here, or the debugger backtrace
306         ;; gets confused.
307         (without-scheduling ()
308           (note-this-location vop :single-value-return)
309           (move ocfp-tn csp-tn)
310           (inst nop))
311         (when lra-label
312           #!-gengc (inst compute-code-from-lra code-tn code-tn lra-label temp)
313           #!+gengc (inst compute-code-from-ra code-tn ra-tn lra-label temp)))
314       (let ((regs-defaulted (gen-label))
315             (defaulting-done (gen-label))
316             (default-stack-vals (gen-label)))
317         (without-scheduling ()
318           ;; Note that this is an unknown-values return point.
319           (note-this-location vop :unknown-return)
320           ;; If there are no stack results, clear the stack now.
321           (if (> nvals register-arg-count)
322               (inst subq nargs-tn (fixnumize register-arg-count) temp)
323               (move ocfp-tn csp-tn))
324           ;; Branch off to the MV case.
325           (inst br zero-tn regs-defaulted))
326         
327         ;; Do the single value case.
328         (do ((i 1 (1+ i))
329              (val (tn-ref-across values) (tn-ref-across val)))
330             ((= i (min nvals register-arg-count)))
331           (move null-tn (tn-ref-tn val)))
332         (when (> nvals register-arg-count)
333           (move csp-tn ocfp-tn)
334           (inst br zero-tn default-stack-vals))
335         
336         (emit-label regs-defaulted)
337         
338         (when (> nvals register-arg-count)
339           ;; If there are stack results, we have to default them
340           ;; and clear the stack.
341           (collect ((defaults))
342             (do ((i register-arg-count (1+ i))
343                  (val (do ((i 0 (1+ i))
344                            (val values (tn-ref-across val)))
345                           ((= i register-arg-count) val))
346                       (tn-ref-across val)))
347                 ((null val))
348               
349               (let ((default-lab (gen-label))
350                     (tn (tn-ref-tn val)))
351                 (defaults (cons default-lab tn))
352                 
353                 (inst blt temp default-lab)
354                 (inst ldl move-temp (* i word-bytes) ocfp-tn)
355                 (inst subq temp (fixnumize 1) temp)
356                 (store-stack-tn tn move-temp)))
357             
358             (emit-label defaulting-done)
359             (move ocfp-tn csp-tn)
360             
361             (let ((defaults (defaults)))
362               (assert defaults)
363               (assemble (*elsewhere*)
364                 (emit-label default-stack-vals)
365                 (do ((remaining defaults (cdr remaining)))
366                     ((null remaining))
367                   (let ((def (car remaining)))
368                     (emit-label (car def))
369                     (when (null (cdr remaining))
370                       (inst br zero-tn defaulting-done))
371                     (store-stack-tn (cdr def) null-tn)))))))
372
373         (when lra-label
374           #!-gengc (inst compute-code-from-lra code-tn code-tn lra-label temp)
375           #!+gengc (inst compute-code-from-ra code-tn ra-tn lra-label temp))))
376   (values))
377
378 \f
379 ;;;; Unknown values receiving:
380
381 ;;; Receive-Unknown-Values  --  Internal
382 ;;;
383 ;;;    Emit code needed at the return point for an unknown-values call for an
384 ;;; arbitrary number of values.
385 ;;;
386 ;;;    We do the single and non-single cases with no shared code: there doesn't
387 ;;; seem to be any potential overlap, and receiving a single value is more
388 ;;; important efficiency-wise.
389 ;;;
390 ;;;    When there is a single value, we just push it on the stack, returning
391 ;;; the old SP and 1.
392 ;;;
393 ;;;    When there is a variable number of values, we move all of the argument
394 ;;; registers onto the stack, and return Args and Nargs.
395 ;;;
396 ;;;    Args and Nargs are TNs wired to the named locations.  We must
397 ;;; explicitly allocate these TNs, since their lifetimes overlap with the
398 ;;; results Start and Count (also, it's nice to be able to target them).
399 ;;;
400 (defun receive-unknown-values (args nargs start count lra-label temp)
401   (declare (type tn args nargs start count temp))
402   (let ((variable-values (gen-label))
403         (done (gen-label)))
404     (without-scheduling ()
405       (inst br zero-tn variable-values)
406       (inst nop))
407
408     (when lra-label
409       #!-gengc (inst compute-code-from-lra code-tn code-tn lra-label temp)
410       #!+gengc (inst compute-code-from-ra code-tn ra-tn lra-label temp))
411     (inst addq csp-tn 4 csp-tn)
412     (storew (first *register-arg-tns*) csp-tn -1)
413     (inst subq csp-tn 4 start)
414     (inst li (fixnumize 1) count)
415     
416     (emit-label done)
417     
418     (assemble (*elsewhere*)
419       (emit-label variable-values)
420       (when lra-label
421         #!-gengc (inst compute-code-from-lra code-tn code-tn lra-label temp)
422         #!+gengc (inst compute-code-from-ra code-tn ra-tn lra-label temp))
423       (do ((arg *register-arg-tns* (rest arg))
424            (i 0 (1+ i)))
425           ((null arg))
426         (storew (first arg) args i))
427       (move args start)
428       (move nargs count)
429       (inst br zero-tn done)))
430   (values))
431
432
433 ;;; VOP that can be inherited by unknown values receivers.  The main thing this
434 ;;; handles is allocation of the result temporaries.
435 ;;;
436 (define-vop (unknown-values-receiver)
437   (:results
438    (start :scs (any-reg))
439    (count :scs (any-reg)))
440   (:temporary (:sc descriptor-reg :offset ocfp-offset
441                    :from :eval :to (:result 0))
442               values-start)
443   (:temporary (:sc any-reg :offset nargs-offset
444                :from :eval :to (:result 1))
445               nvals)
446   (:temporary (:scs (non-descriptor-reg)) temp))
447
448
449 \f
450 ;;;; Local call with unknown values convention return:
451
452 ;;; Non-TR local call for a fixed number of values passed according to the
453 ;;; unknown values convention.
454 ;;;
455 ;;; Args are the argument passing locations, which are specified only to
456 ;;; terminate their lifetimes in the caller.
457 ;;;
458 ;;; Values are the return value locations (wired to the standard passing
459 ;;; locations).
460 ;;;
461 ;;; Save is the save info, which we can ignore since saving has been done.
462 ;;; Return-PC is the TN that the return PC should be passed in.
463 ;;; Target is a continuation pointing to the start of the called function.
464 ;;; Nvals is the number of values received.
465 ;;;
466 ;;; Note: we can't use normal load-tn allocation for the fixed args, since all
467 ;;; registers may be tied up by the more operand.  Instead, we use
468 ;;; MAYBE-LOAD-STACK-TN.
469 ;;;
470 (define-vop (call-local)
471   (:args (fp)
472          (nfp)
473          (args :more t))
474   (:results (values :more t))
475   (:save-p t)
476   (:move-args :local-call)
477   (:info arg-locs callee target nvals)
478   (:vop-var vop)
479   (:temporary (:scs (descriptor-reg) :from :eval) move-temp)
480   (:temporary (:scs (non-descriptor-reg)) temp)
481   (:temporary (:sc control-stack :offset nfp-save-offset) nfp-save)
482   (:temporary (:sc any-reg :offset ocfp-offset :from :eval) ocfp)
483   (:ignore arg-locs args ocfp)
484   (:generator 5
485     (let ((label (gen-label))
486           (cur-nfp (current-nfp-tn vop)))
487       (when cur-nfp
488         (store-stack-tn nfp-save cur-nfp))
489       (let ((callee-nfp (callee-nfp-tn callee)))
490         (maybe-load-stack-nfp-tn callee-nfp nfp temp))
491       (maybe-load-stack-tn cfp-tn fp)
492       (trace-table-entry trace-table-call-site)
493       (inst compute-lra-from-code
494             (callee-return-pc-tn callee) code-tn label temp)
495       (note-this-location vop :call-site)
496       (inst br zero-tn target)
497       (trace-table-entry trace-table-normal)
498       (emit-return-pc label)
499       (default-unknown-values vop values nvals move-temp temp label)
500       (maybe-load-stack-nfp-tn cur-nfp nfp-save temp))))
501
502
503 ;;; Non-TR local call for a variable number of return values passed according
504 ;;; to the unknown values convention.  The results are the start of the values
505 ;;; glob and the number of values received.
506 ;;;
507 ;;; Note: we can't use normal load-tn allocation for the fixed args, since all
508 ;;; registers may be tied up by the more operand.  Instead, we use
509 ;;; MAYBE-LOAD-STACK-TN.
510 ;;;
511 (define-vop (multiple-call-local unknown-values-receiver)
512   (:args (fp)
513          (nfp)
514          (args :more t))
515   (:save-p t)
516   (:move-args :local-call)
517   (:info save callee target)
518   (:ignore args save)
519   (:vop-var vop)
520   (:temporary (:sc control-stack :offset nfp-save-offset) nfp-save)
521   (:temporary (:scs (non-descriptor-reg)) temp)
522   (:generator 20
523     (let ((label (gen-label))
524           (cur-nfp (current-nfp-tn vop)))
525       (when cur-nfp
526         (store-stack-tn nfp-save cur-nfp))
527       (let ((callee-nfp (callee-nfp-tn callee)))
528         (maybe-load-stack-nfp-tn callee-nfp nfp temp))
529       (maybe-load-stack-tn cfp-tn fp)
530       (trace-table-entry trace-table-call-site)
531       (inst compute-lra-from-code
532             (callee-return-pc-tn callee) code-tn label temp)
533       (note-this-location vop :call-site)
534       (inst bsr zero-tn target)
535       (trace-table-entry trace-table-normal)
536       (emit-return-pc label)
537       (note-this-location vop :unknown-return)
538       (receive-unknown-values values-start nvals start count label temp)
539       (maybe-load-stack-nfp-tn cur-nfp nfp-save temp))))
540
541 \f
542 ;;;; Local call with known values return:
543
544 ;;; Non-TR local call with known return locations.  Known-value return works
545 ;;; just like argument passing in local call.
546 ;;;
547 ;;; Note: we can't use normal load-tn allocation for the fixed args, since all
548 ;;; registers may be tied up by the more operand.  Instead, we use
549 ;;; MAYBE-LOAD-STACK-TN.
550 ;;;
551 (define-vop (known-call-local)
552   (:args (fp)
553          (nfp)
554          (args :more t))
555   (:results (res :more t))
556   (:move-args :local-call)
557   (:save-p t)
558   (:info save callee target)
559   (:ignore args res save)
560   (:vop-var vop)
561   (:temporary (:sc control-stack :offset nfp-save-offset) nfp-save)
562   (:temporary (:scs (non-descriptor-reg)) temp)
563   (:generator 5
564     (let (#!-gengc (label (gen-label))
565           (cur-nfp (current-nfp-tn vop)))
566       (when cur-nfp
567         (store-stack-tn nfp-save cur-nfp))
568       (let ((callee-nfp (callee-nfp-tn callee)))
569         (maybe-load-stack-nfp-tn callee-nfp nfp temp))
570       (maybe-load-stack-tn cfp-tn fp)
571       (trace-table-entry trace-table-call-site)
572       (inst compute-lra-from-code
573             (callee-return-pc-tn callee) code-tn label temp)
574       (note-this-location vop :call-site)
575       (inst bsr zero-tn target)
576       (trace-table-entry trace-table-normal)
577       (emit-return-pc label)
578       (note-this-location vop :known-return)
579       (maybe-load-stack-nfp-tn cur-nfp nfp-save temp))))
580
581 ;;; Return from known values call.  We receive the return locations as
582 ;;; arguments to terminate their lifetimes in the returning function.  We
583 ;;; restore FP and CSP and jump to the Return-PC.
584 ;;;
585 ;;; Note: we can't use normal load-tn allocation for the fixed args, since all
586 ;;; registers may be tied up by the more operand.  Instead, we use
587 ;;; MAYBE-LOAD-STACK-TN.
588 ;;;
589 (define-vop (known-return)
590   (:args (ocfp :target ocfp-temp)
591          (return-pc :target return-pc-temp)
592          (vals :more t))
593   (:temporary (:sc any-reg :from (:argument 0)) ocfp-temp)
594   (:temporary (:sc #!-gengc descriptor-reg #!+gengc any-reg :from (:argument 1))
595               return-pc-temp)
596   #!-gengc (:temporary (:scs (interior-reg)) lip)
597   (:move-args :known-return)
598   (:info val-locs)
599   (:ignore val-locs vals)
600   (:vop-var vop)
601   (:generator 6
602     (trace-table-entry trace-table-function-epilogue)
603     (maybe-load-stack-tn ocfp-temp ocfp)
604     (maybe-load-stack-tn return-pc-temp return-pc)
605     (move cfp-tn csp-tn)
606     (let ((cur-nfp (current-nfp-tn vop)))
607       (when cur-nfp
608         (inst addq cur-nfp (bytes-needed-for-non-descriptor-stack-frame)
609               nsp-tn)))
610     (inst subq return-pc-temp (- other-pointer-type word-bytes) lip)
611     (move ocfp-temp cfp-tn)
612     (inst ret zero-tn lip 1)
613     (trace-table-entry trace-table-normal)))
614
615 \f
616 ;;;; Full call:
617 ;;;
618 ;;;    There is something of a cross-product effect with full calls.  Different
619 ;;; versions are used depending on whether we know the number of arguments or
620 ;;; the name of the called function, and whether we want fixed values, unknown
621 ;;; values, or a tail call.
622 ;;;
623 ;;; In full call, the arguments are passed creating a partial frame on the
624 ;;; stack top and storing stack arguments into that frame.  On entry to the
625 ;;; callee, this partial frame is pointed to by FP.  If there are no stack
626 ;;; arguments, we don't bother allocating a partial frame, and instead set FP
627 ;;; to SP just before the call.
628
629 ;;; Define-Full-Call  --  Internal
630 ;;;
631 ;;;    This macro helps in the definition of full call VOPs by avoiding code
632 ;;; replication in defining the cross-product VOPs.
633 ;;;
634 ;;; Name is the name of the VOP to define.
635 ;;; 
636 ;;; Named is true if the first argument is a symbol whose global function
637 ;;; definition is to be called.
638 ;;;
639 ;;; Return is either :Fixed, :Unknown or :Tail:
640 ;;; -- If :Fixed, then the call is for a fixed number of values, returned in
641 ;;;    the standard passing locations (passed as result operands).
642 ;;; -- If :Unknown, then the result values are pushed on the stack, and the
643 ;;;    result values are specified by the Start and Count as in the
644 ;;;    unknown-values continuation representation.
645 ;;; -- If :Tail, then do a tail-recursive call.  No values are returned.
646 ;;;    The Ocfp and Return-PC are passed as the second and third arguments.
647 ;;;
648 ;;; In non-tail calls, the pointer to the stack arguments is passed as the last
649 ;;; fixed argument.  If Variable is false, then the passing locations are
650 ;;; passed as a more arg.  Variable is true if there are a variable number of
651 ;;; arguments passed on the stack.  Variable cannot be specified with :Tail
652 ;;; return.  TR variable argument call is implemented separately.
653 ;;;
654 ;;; In tail call with fixed arguments, the passing locations are passed as a
655 ;;; more arg, but there is no new-FP, since the arguments have been set up in
656 ;;; the current frame.
657 ;;;
658 (defmacro define-full-call (name named return variable)
659   (assert (not (and variable (eq return :tail))))
660   `(define-vop (,name
661                 ,@(when (eq return :unknown)
662                     '(unknown-values-receiver)))
663      (:args
664       ,@(unless (eq return :tail)
665           '((new-fp :scs (any-reg) :to :eval)))
666
667       ,(if named
668            '(name :target name-pass)
669            '(arg-fun :target lexenv))
670       
671       ,@(when (eq return :tail)
672           '((ocfp :target ocfp-pass)
673             (return-pc :target return-pc-pass)))
674       
675       ,@(unless variable '((args :more t :scs (descriptor-reg)))))
676
677      ,@(when (eq return :fixed)
678          '((:results (values :more t))))
679    
680      (:save-p ,(if (eq return :tail) :compute-only t))
681
682      ,@(unless (or (eq return :tail) variable)
683          '((:move-args :full-call)))
684
685      (:vop-var vop)
686      (:info ,@(unless (or variable (eq return :tail)) '(arg-locs))
687             ,@(unless variable '(nargs))
688             ,@(when (eq return :fixed) '(nvals)))
689
690      (:ignore #!+gengc ,@(unless (eq return :tail) '(return-pc-pass))
691               ,@(unless (or variable (eq return :tail)) '(arg-locs))
692               ,@(unless variable '(args)))
693
694      (:temporary (:sc descriptor-reg
695                   :offset ocfp-offset
696                   :from (:argument 1)
697                   ,@(unless (eq return :fixed)
698                       '(:to :eval)))
699                  ocfp-pass)
700
701      (:temporary (:sc descriptor-reg
702                   :offset #!-gengc lra-offset #!+gengc ra-offset
703                   :from (:argument ,(if (eq return :tail) 2 1))
704                   :to :eval)
705                  return-pc-pass)
706
707      ,@(if named
708          `((:temporary (:sc descriptor-reg :offset fdefn-offset
709                         :from (:argument ,(if (eq return :tail) 0 1))
710                         :to :eval)
711                        name-pass))
712
713          `((:temporary (:sc descriptor-reg :offset lexenv-offset
714                         :from (:argument ,(if (eq return :tail) 0 1))
715                         :to :eval)
716                        lexenv)
717            #!-gengc
718            (:temporary (:scs (descriptor-reg) :from (:argument 0) :to :eval)
719                        function)))
720
721      (:temporary (:sc any-reg :offset nargs-offset :to :eval)
722                  nargs-pass)
723
724      ,@(when variable
725          (mapcar #'(lambda (name offset)
726                      `(:temporary (:sc descriptor-reg
727                                    :offset ,offset
728                                    :to :eval)
729                          ,name))
730                  register-arg-names *register-arg-offsets*))
731      ,@(when (eq return :fixed)
732          '((:temporary (:scs (descriptor-reg) :from :eval) move-temp)))
733
734      ,@(unless (eq return :tail)
735          '((:temporary (:scs (non-descriptor-reg)) temp)
736            (:temporary (:sc control-stack :offset nfp-save-offset) nfp-save)))
737
738      (:temporary (:sc interior-reg :offset lip-offset) entry-point)
739
740      (:generator ,(+ (if named 5 0)
741                      (if variable 19 1)
742                      (if (eq return :tail) 0 10)
743                      15
744                      (if (eq return :unknown) 25 0))
745        (let* ((cur-nfp (current-nfp-tn vop))
746               ,@(unless (eq return :tail)
747                   '((lra-label (gen-label))))
748               (filler
749                (remove nil
750                        (list :load-nargs
751                              ,@(if (eq return :tail)
752                                    '((unless (location= ocfp ocfp-pass)
753                                        :load-ocfp)
754                                      (unless (location= return-pc
755                                                         return-pc-pass)
756                                        :load-return-pc)
757                                      (when cur-nfp
758                                        :frob-nfp))
759                                    '(#!-gengc
760                                      :comp-lra
761                                      (when cur-nfp
762                                        :frob-nfp)
763                                      :save-fp
764                                      :load-fp))))))
765          (flet ((do-next-filler ()
766                   (let* ((next (pop filler))
767                          (what (if (consp next) (car next) next)))
768                     (ecase what
769                       (:load-nargs
770                        ,@(if variable
771                              `((inst subq csp-tn new-fp nargs-pass)
772                                ,@(let ((index -1))
773                                    (mapcar #'(lambda (name)
774                                                `(inst ldl ,name
775                                                       ,(ash (incf index)
776                                                             word-shift)
777                                                       new-fp))
778                                            register-arg-names)))
779                              '((inst li (fixnumize nargs) nargs-pass))))
780                       ,@(if (eq return :tail)
781                             '((:load-ocfp
782                                (sc-case ocfp
783                                  (any-reg
784                                   (inst move ocfp ocfp-pass))
785                                  (control-stack
786                                   (inst ldl ocfp-pass
787                                         (ash (tn-offset ocfp)
788                                              word-shift)
789                                         cfp-tn))))
790                               (:load-return-pc
791                                (sc-case return-pc
792                                  (#!-gengc descriptor-reg #!+gengc any-reg
793                                   (inst move return-pc return-pc-pass))
794                                  (control-stack
795                                   (inst ldl return-pc-pass
796                                         (ash (tn-offset return-pc)
797                                              word-shift)
798                                          cfp-tn))))
799                               (:frob-nfp
800                                (inst addq cur-nfp
801                                      (bytes-needed-for-non-descriptor-stack-frame)
802                                      nsp-tn)))
803                             `(#!-gengc
804                               (:comp-lra
805                                (inst compute-lra-from-code
806                                      return-pc-pass code-tn lra-label temp))
807                               (:frob-nfp
808                                (store-stack-tn nfp-save cur-nfp))
809                               (:save-fp
810                                (inst move cfp-tn ocfp-pass))
811                               (:load-fp
812                                ,(if variable
813                                     '(move new-fp cfp-tn)
814                                     '(if (> nargs register-arg-count)
815                                          (move new-fp cfp-tn)
816                                          (move csp-tn cfp-tn)))
817                                (trace-table-entry trace-table-call-site))))
818                       ((nil))))))
819
820            ,@(if named
821                  `((sc-case name
822                      (descriptor-reg (move name name-pass))
823                      (control-stack
824                       (inst ldl name-pass
825                             (ash (tn-offset name) word-shift) cfp-tn)
826                       (do-next-filler))
827                      (constant
828                       (inst ldl name-pass
829                             (- (ash (tn-offset name) word-shift)
830                                other-pointer-type) code-tn)
831                       (do-next-filler)))
832                    (inst ldl entry-point
833                          (- (ash fdefn-raw-addr-slot word-shift)
834                             other-pointer-type) name-pass)
835                    (do-next-filler))
836                  `((sc-case arg-fun
837                      (descriptor-reg (move arg-fun lexenv))
838                      (control-stack
839                       (inst ldl lexenv
840                             (ash (tn-offset arg-fun) word-shift) cfp-tn)
841                       (do-next-filler))
842                      (constant
843                       (inst ldl lexenv
844                             (- (ash (tn-offset arg-fun) word-shift)
845                                other-pointer-type) code-tn)
846                       (do-next-filler)))
847                    #!-gengc
848                    (inst ldl function
849                          (- (ash closure-function-slot word-shift)
850                             function-pointer-type) lexenv)
851                    #!-gengc
852                    (do-next-filler)
853                    #!-gengc
854                    (inst addq function
855                          (- (ash function-code-offset word-shift)
856                             function-pointer-type) entry-point)
857                    #!+gengc
858                    (inst ldl entry-point
859                          (- (ash closure-entry-point-slot word-shift)
860                             function-pointer-type) lexenv)
861                    #!+gengc
862                    (do-next-filler)))
863            (loop
864              (if (cdr filler)
865                  (do-next-filler)
866                  (return)))
867            
868            (note-this-location vop :call-site)
869            (do-next-filler)
870            (inst jsr zero-tn entry-point))
871
872          ,@(ecase return
873              (:fixed
874               '((trace-table-entry trace-table-normal)
875                 (emit-return-pc lra-label)
876                 (default-unknown-values vop values nvals
877                                         move-temp temp lra-label)
878                 (maybe-load-stack-nfp-tn cur-nfp nfp-save temp)))
879              (:unknown
880               '((trace-table-entry trace-table-normal)
881                 (emit-return-pc lra-label)
882                 (note-this-location vop :unknown-return)
883                 (receive-unknown-values values-start nvals start count
884                                         lra-label temp)
885                 (maybe-load-stack-nfp-tn cur-nfp nfp-save temp)))
886              (:tail))))))
887
888
889 (define-full-call call nil :fixed nil)
890 (define-full-call call-named t :fixed nil)
891 (define-full-call multiple-call nil :unknown nil)
892 (define-full-call multiple-call-named t :unknown nil)
893 (define-full-call tail-call nil :tail nil)
894 (define-full-call tail-call-named t :tail nil)
895
896 (define-full-call call-variable nil :fixed t)
897 (define-full-call multiple-call-variable nil :unknown t)
898
899
900 ;;; Defined separately, since needs special code that BLT's the arguments
901 ;;; down.
902 ;;;
903 (define-vop (tail-call-variable)
904   (:args
905    (args-arg :scs (any-reg) :target args)
906    (function-arg :scs (descriptor-reg) :target lexenv)
907    (ocfp-arg :scs (any-reg) :target ocfp)
908    (lra-arg :scs (#!-gengc descriptor-reg #!+gengc any-reg) :target lra))
909
910   (:temporary (:sc any-reg :offset nl0-offset :from (:argument 0)) args)
911   (:temporary (:sc any-reg :offset lexenv-offset :from (:argument 1)) lexenv)
912   (:temporary (:sc any-reg :offset ocfp-offset :from (:argument 2)) ocfp)
913   (:temporary (:sc any-reg :offset #!-gengc lra-offset #!+gengc ra-offset
914                    :from (:argument 3)) lra)
915   (:temporary (:scs (non-descriptor-reg)) temp)
916
917   (:vop-var vop)
918
919   (:generator 75
920
921     ;; Move these into the passing locations if they are not already there.
922     (move args-arg args)
923     (move function-arg lexenv)
924     (move ocfp-arg ocfp)
925     (move lra-arg lra)
926
927     ;; Clear the number stack if anything is there.
928     (let ((cur-nfp (current-nfp-tn vop)))
929       (when cur-nfp
930         (inst addq cur-nfp (bytes-needed-for-non-descriptor-stack-frame)
931               nsp-tn)))
932
933     ;; And jump to the assembly-routine that does the bliting.
934     (inst li (make-fixup 'tail-call-variable :assembly-routine) temp)
935     (inst jmp zero-tn temp)))
936
937 \f
938 ;;;; Unknown values return:
939
940 ;;; Return a single value using the unknown-values convention.
941 ;;; 
942 (define-vop (return-single)
943   (:args (ocfp :scs (any-reg))
944          #!-gengc (return-pc :scs (descriptor-reg))
945          #!+gengc (return-pc :scs (any-reg) :target ra)
946          (value))
947   (:ignore value)
948   #!-gengc (:temporary (:scs (interior-reg)) lip)
949   #!+gengc (:temporary (:sc any-reg :offset ra-offset :from (:argument 1)) ra)
950   #!+gengc (:temporary (:scs (any-reg) :from (:argument 1)) temp)
951   (:vop-var vop)
952   (:generator 6
953     ;; Clear the number stack.
954     (trace-table-entry trace-table-function-epilogue)
955     (let ((cur-nfp (current-nfp-tn vop)))
956       (when cur-nfp
957         (inst addq cur-nfp (bytes-needed-for-non-descriptor-stack-frame)
958               nsp-tn)))
959     ;; Clear the control stack, and restore the frame pointer.
960     (move cfp-tn csp-tn)
961     (move ocfp cfp-tn)
962     ;; Out of here.
963     #!-gengc (lisp-return return-pc lip :offset 2)
964     #!+gengc
965     (progn
966       (inst addq return-pc (* 2 word-bytes) temp)
967       (unless (location= ra return-pc)
968         (inst move ra return-pc))
969       (inst ret zero-tn temp 1))
970     (trace-table-entry trace-table-normal)))
971
972
973 ;;; Do unknown-values return of a fixed number of values.  The Values are
974 ;;; required to be set up in the standard passing locations.  Nvals is the
975 ;;; number of values returned.
976 ;;;
977 ;;; If returning a single value, then deallocate the current frame, restore
978 ;;; FP and jump to the single-value entry at Return-PC + 8.
979 ;;;
980 ;;; If returning other than one value, then load the number of values returned,
981 ;;; NIL out unsupplied values registers, restore FP and return at Return-PC.
982 ;;; When there are stack values, we must initialize the argument pointer to
983 ;;; point to the beginning of the values block (which is the beginning of the
984 ;;; current frame.)
985 ;;;
986 (define-vop (return)
987   (:args (ocfp :scs (any-reg))
988          (return-pc :scs (#!-gengc descriptor-reg #!+gengc any-reg) :to (:eval 1)
989                     #!+gengc :target #!+gengc ra)
990          (values :more t))
991   (:ignore values)
992   (:info nvals)
993   (:temporary (:sc descriptor-reg :offset a0-offset :from (:eval 0)) a0)
994   (:temporary (:sc descriptor-reg :offset a1-offset :from (:eval 0)) a1)
995   (:temporary (:sc descriptor-reg :offset a2-offset :from (:eval 0)) a2)
996   (:temporary (:sc descriptor-reg :offset a3-offset :from (:eval 0)) a3)
997   (:temporary (:sc descriptor-reg :offset a4-offset :from (:eval 0)) a4)
998   (:temporary (:sc descriptor-reg :offset a5-offset :from (:eval 0)) a5)
999   (:temporary (:sc any-reg :offset nargs-offset) nargs)
1000   (:temporary (:sc any-reg :offset ocfp-offset) val-ptr)
1001   #!-gengc (:temporary (:scs (interior-reg)) lip)
1002   #!+gengc (:temporary (:sc any-reg :offset ra-offset :from (:eval 1)) ra)
1003   (:vop-var vop)
1004   (:generator 6
1005     ;; Clear the number stack.
1006     (trace-table-entry trace-table-function-epilogue)
1007     (let ((cur-nfp (current-nfp-tn vop)))
1008       (when cur-nfp
1009         (inst addq cur-nfp (bytes-needed-for-non-descriptor-stack-frame)
1010               nsp-tn)))
1011     ;; Establish the values pointer and values count.
1012     (move cfp-tn val-ptr)
1013     (inst li (fixnumize nvals) nargs)
1014     ;; restore the frame pointer and clear as much of the control
1015     ;; stack as possible.
1016     (move ocfp cfp-tn)
1017     (inst addq val-ptr (* nvals word-bytes) csp-tn)
1018     ;; pre-default any argument register that need it.
1019     (when (< nvals register-arg-count)
1020       (dolist (reg (subseq (list a0 a1 a2 a3 a4 a5) nvals))
1021         (move null-tn reg)))
1022     ;; And away we go.
1023     (lisp-return return-pc lip)
1024     (trace-table-entry trace-table-normal)))
1025
1026 ;;; Do unknown-values return of an arbitrary number of values (passed on the
1027 ;;; stack.)  We check for the common case of a single return value, and do that
1028 ;;; inline using the normal single value return convention.  Otherwise, we
1029 ;;; branch off to code that calls an assembly-routine.
1030 ;;;
1031 (define-vop (return-multiple)
1032   (:args (ocfp-arg :scs (any-reg) :target ocfp)
1033          #!-gengc (lra-arg :scs (descriptor-reg) :target lra)
1034          #!+gengc (return-pc :scs (any-reg) :target ra)
1035          (vals-arg :scs (any-reg) :target vals)
1036          (nvals-arg :scs (any-reg) :target nvals))
1037
1038   (:temporary (:sc any-reg :offset nl1-offset :from (:argument 0)) ocfp)
1039   #!-gengc
1040   (:temporary (:sc descriptor-reg :offset lra-offset :from (:argument 1)) lra)
1041   #!+gengc
1042   (:temporary (:sc any-reg :offset ra-offset :from (:argument 1)) ra)
1043   (:temporary (:sc any-reg :offset nl0-offset :from (:argument 2)) vals)
1044   (:temporary (:sc any-reg :offset nargs-offset :from (:argument 3)) nvals)
1045   (:temporary (:sc descriptor-reg :offset a0-offset) a0)
1046   (:temporary (:scs (non-descriptor-reg)) temp)
1047   #!-gengc (:temporary (:scs (interior-reg)) lip)
1048   #!+gengc (:temporary (:scs (any-reg) :from (:argument 0)) temp)
1049
1050   (:vop-var vop)
1051
1052   (:generator 13
1053     (trace-table-entry trace-table-function-epilogue)
1054     (let ((not-single (gen-label)))
1055       ;; Clear the number stack.
1056       (let ((cur-nfp (current-nfp-tn vop)))
1057         (when cur-nfp
1058           (inst addq cur-nfp (bytes-needed-for-non-descriptor-stack-frame)
1059                 nsp-tn)))
1060
1061       ;; Check for the single case.
1062       (inst li (fixnumize 1) a0)
1063       (inst cmpeq nvals-arg a0 temp)
1064       (inst ldl a0 0 vals-arg)
1065       (inst beq temp not-single)
1066
1067       ;; Return with one value.
1068       (move cfp-tn csp-tn)
1069       (move ocfp-arg cfp-tn)
1070       (lisp-return lra-arg lip :offset 2)
1071                 
1072       ;; Nope, not the single case.
1073       (emit-label not-single)
1074       (move ocfp-arg ocfp)
1075       (move lra-arg lra)
1076       (move vals-arg vals)
1077       (move nvals-arg nvals)
1078       (inst li (make-fixup 'return-multiple :assembly-routine) temp)
1079       (inst jmp zero-tn temp))
1080     (trace-table-entry trace-table-normal)))
1081
1082
1083 \f
1084 ;;;; XEP hackery:
1085
1086
1087 ;;; We don't need to do anything special for regular functions.
1088 ;;;
1089 (define-vop (setup-environment)
1090   (:info label)
1091   (:ignore label)
1092   (:generator 0
1093     ;; Don't bother doing anything.
1094     ))
1095
1096 ;;; Get the lexical environment from its passing location.
1097 ;;;
1098 (define-vop (setup-closure-environment)
1099   (:temporary (:sc descriptor-reg :offset lexenv-offset :target closure
1100                :to (:result 0))
1101               lexenv)
1102   (:results (closure :scs (descriptor-reg)))
1103   (:info label)
1104   (:ignore label)
1105   (:generator 6
1106     ;; Get result.
1107     (move lexenv closure)))
1108
1109 ;;; Copy a more arg from the argument area to the end of the current frame.
1110 ;;; Fixed is the number of non-more arguments. 
1111 ;;;
1112 (define-vop (copy-more-arg)
1113   (:temporary (:sc any-reg :offset nl0-offset) result)
1114   (:temporary (:sc any-reg :offset nl1-offset) count)
1115   (:temporary (:sc any-reg :offset nl2-offset) src)
1116   (:temporary (:sc any-reg :offset nl4-offset) dst)
1117   (:temporary (:sc descriptor-reg :offset l0-offset) temp)
1118   (:info fixed)
1119   (:generator 20
1120     (let ((loop (gen-label))
1121           (do-regs (gen-label))
1122           (done (gen-label)))
1123       (when (< fixed register-arg-count)
1124         ;; Save a pointer to the results so we can fill in register args.
1125         ;; We don't need this if there are more fixed args than reg args.
1126         (move csp-tn result))
1127       ;; Allocate the space on the stack.
1128       (cond ((zerop fixed)
1129              (inst addq csp-tn nargs-tn csp-tn)
1130              (inst beq nargs-tn done))
1131             (t
1132              (inst subq nargs-tn (fixnumize fixed) count)
1133              (inst ble count done)
1134              (inst addq csp-tn count csp-tn)))
1135       (when (< fixed register-arg-count)
1136         ;; We must stop when we run out of stack args, not when we run out of
1137         ;; more args.
1138         (inst subq nargs-tn (fixnumize register-arg-count) count))
1139       ;; Initialize dst to be end of stack.
1140       (move csp-tn dst)
1141       ;; Everything of interest in registers.
1142       (inst ble count do-regs)
1143       ;; Initialize src to be end of args.
1144       (inst addq cfp-tn nargs-tn src)
1145
1146       (emit-label loop)
1147       ;; *--dst = *--src, --count
1148       (inst subq src word-bytes src)
1149       (inst subq count (fixnumize 1) count)
1150       (loadw temp src)
1151       (inst subq dst word-bytes dst)
1152       (storew temp dst)
1153       (inst bgt count loop)
1154
1155       (emit-label do-regs)
1156       (when (< fixed register-arg-count)
1157         ;; Now we have to deposit any more args that showed up in registers.
1158         ;; We know there is at least one more arg, otherwise we would have
1159         ;; branched to done up at the top.
1160         (inst subq nargs-tn (fixnumize (1+ fixed)) count)
1161         (do ((i fixed (1+ i)))
1162             ((>= i register-arg-count))
1163           ;; Store it relative to the pointer saved at the start.
1164           (storew (nth i *register-arg-tns*) result (- i fixed))
1165           ;; Is this the last one?
1166           (inst beq count done)
1167           ;; Decrement count.
1168           (inst subq count (fixnumize 1) count)))
1169       (emit-label done))))
1170
1171
1172 ;;; More args are stored consequtively on the stack, starting immediately at
1173 ;;; the context pointer.  The context pointer is not typed, so the lowtag is 0.
1174 ;;;
1175 (define-full-reffer more-arg * 0 0 (descriptor-reg any-reg) * %more-arg)
1176
1177
1178 ;;; Turn more arg (context, count) into a list.
1179 ;;;
1180 (define-vop (listify-rest-args)
1181   (:args (context-arg :target context :scs (descriptor-reg))
1182          (count-arg :target count :scs (any-reg)))
1183   (:arg-types * tagged-num)
1184   (:temporary (:scs (any-reg) :from (:argument 0)) context)
1185   (:temporary (:scs (any-reg) :from (:argument 1)) count)
1186   (:temporary (:scs (descriptor-reg) :from :eval) temp dst)
1187   (:results (result :scs (descriptor-reg)))
1188   (:translate %listify-rest-args)
1189   (:policy :safe)
1190   (:generator 20
1191     (let ((enter (gen-label))
1192           (loop (gen-label))
1193           (done (gen-label)))
1194       (move context-arg context)
1195       (move count-arg count)
1196       ;; Check to see if there are any arguments.
1197       (move null-tn result)
1198       (inst beq count done)
1199
1200       ;; We need to do this atomically.
1201       (pseudo-atomic ()
1202         ;; Allocate a cons (2 words) for each item.
1203         (inst bis alloc-tn list-pointer-type result)
1204         (move result dst)
1205         (inst sll count 1 temp)
1206         (inst addq alloc-tn temp alloc-tn)
1207         (inst br zero-tn enter)
1208
1209         ;; Store the current cons in the cdr of the previous cons.
1210         (emit-label loop)
1211         (inst addq dst (* 2 word-bytes) dst)
1212         (storew dst dst -1 list-pointer-type)
1213
1214         (emit-label enter)
1215         ;; Grab one value.
1216         (loadw temp context)
1217         (inst addq context word-bytes context)
1218
1219         ;; Store the value in the car (in delay slot)
1220         (storew temp dst 0 list-pointer-type)
1221
1222         ;; Dec count, and if != zero, go back for more.
1223         (inst subq count (fixnumize 1) count)
1224         (inst bne count loop)
1225
1226         ;; NIL out the last cons.
1227         (storew null-tn dst 1 list-pointer-type))
1228       (emit-label done))))
1229
1230 ;;; Return the location and size of the more arg glob created by Copy-More-Arg.
1231 ;;; Supplied is the total number of arguments supplied (originally passed in
1232 ;;; NARGS.)  Fixed is the number of non-rest arguments.
1233 ;;;
1234 ;;; We must duplicate some of the work done by Copy-More-Arg, since at that
1235 ;;; time the environment is in a pretty brain-damaged state, preventing this
1236 ;;; info from being returned as values.  What we do is compute
1237 ;;; supplied - fixed, and return a pointer that many words below the current
1238 ;;; stack top.
1239 ;;;
1240 (define-vop (more-arg-context)
1241   (:policy :fast-safe)
1242   (:translate sb!c::%more-arg-context)
1243   (:args (supplied :scs (any-reg)))
1244   (:arg-types tagged-num (:constant fixnum))
1245   (:info fixed)
1246   (:results (context :scs (descriptor-reg))
1247             (count :scs (any-reg)))
1248   (:result-types t tagged-num)
1249   (:note "more-arg-context")
1250   (:generator 5
1251     (inst subq supplied (fixnumize fixed) count)
1252     (inst subq csp-tn count context)))
1253
1254
1255 ;;; Signal wrong argument count error if Nargs isn't = to Count.
1256 ;;;
1257 (define-vop (verify-argument-count)
1258   (:policy :fast-safe)
1259   (:translate sb!c::%verify-argument-count)
1260   (:args (nargs :scs (any-reg)))
1261   (:arg-types positive-fixnum (:constant t))
1262   (:temporary (:scs (any-reg) :type fixnum) temp)
1263   (:info count)
1264   (:vop-var vop)
1265   (:save-p :compute-only)
1266   (:generator 3
1267     (let ((err-lab
1268            (generate-error-code vop invalid-argument-count-error nargs)))
1269       (cond ((zerop count)
1270              (inst bne nargs err-lab))
1271             (t
1272              (inst subq nargs (fixnumize count) temp)
1273              (inst bne temp err-lab))))))
1274
1275 ;;; Various other error signalers.
1276 ;;;
1277 (macrolet ((frob (name error translate &rest args)
1278              `(define-vop (,name)
1279                 ,@(when translate
1280                     `((:policy :fast-safe)
1281                       (:translate ,translate)))
1282                 (:args ,@(mapcar #'(lambda (arg)
1283                                      `(,arg :scs (any-reg descriptor-reg)))
1284                                  args))
1285                 (:vop-var vop)
1286                 (:save-p :compute-only)
1287                 (:generator 1000
1288                   (error-call vop ,error ,@args)))))
1289   (frob argument-count-error invalid-argument-count-error
1290     sb!c::%argument-count-error nargs)
1291   (frob type-check-error object-not-type-error sb!c::%type-check-error
1292     object type)
1293   (frob layout-invalid-error layout-invalid-error sb!c::%layout-invalid-error
1294     object layout)
1295   (frob odd-key-arguments-error odd-key-arguments-error
1296     sb!c::%odd-key-arguments-error)
1297   (frob unknown-key-argument-error unknown-key-argument-error
1298     sb!c::%unknown-key-argument-error key)
1299   (frob nil-function-returned-error nil-function-returned-error nil fun))