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