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