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