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