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