b10e80e4f0aae193ef0abf3414b9ba243bc9eb11
[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 \f
426 ;;; This hook in the codegen pass lets us insert code before fall-thru entry
427 ;;; points, local-call entry points, and tail-call entry points.  The default
428 ;;; does nothing.
429 (defun emit-block-header (start-label trampoline-label fall-thru-p alignp)
430   (declare (ignore fall-thru-p alignp))
431   (when trampoline-label
432     (emit-label trampoline-label))
433   (emit-label start-label))
434
435 \f
436 ;;;; Local call with unknown values convention return:
437
438 ;;; Non-TR local call for a fixed number of values passed according to the
439 ;;; unknown values convention.
440 ;;;
441 ;;; Args are the argument passing locations, which are specified only to
442 ;;; terminate their lifetimes in the caller.
443 ;;;
444 ;;; Values are the return value locations (wired to the standard passing
445 ;;; locations).
446 ;;;
447 ;;; Save is the save info, which we can ignore since saving has been done.
448 ;;; Return-PC is the TN that the return PC should be passed in.
449 ;;; Target is a continuation pointing to the start of the called function.
450 ;;; Nvals is the number of values received.
451 ;;;
452 ;;; Note: we can't use normal load-tn allocation for the fixed args, since all
453 ;;; registers may be tied up by the more operand.  Instead, we use
454 ;;; MAYBE-LOAD-STACK-TN.
455 ;;;
456 (define-vop (call-local)
457   (:args (fp)
458          (nfp)
459          (args :more t))
460   (:results (values :more t))
461   (:save-p t)
462   (:move-args :local-call)
463   (:info arg-locs callee target nvals)
464   (:vop-var vop)
465   (:temporary (:scs (descriptor-reg) :from :eval) move-temp)
466   (:temporary (:scs (non-descriptor-reg)) temp)
467   (:temporary (:sc control-stack :offset nfp-save-offset) nfp-save)
468   (:temporary (:sc any-reg :offset ocfp-offset :from :eval) ocfp)
469   (:ignore arg-locs args ocfp)
470   (:generator 5
471     (let ((label (gen-label))
472           (cur-nfp (current-nfp-tn vop)))
473       (when cur-nfp
474         (store-stack-tn nfp-save cur-nfp))
475       (let ((callee-nfp (callee-nfp-tn callee)))
476         (when callee-nfp
477           (maybe-load-stack-tn callee-nfp nfp)))
478       (maybe-load-stack-tn cfp-tn fp)
479       (trace-table-entry trace-table-call-site)
480       (inst compute-lra-from-code
481             (callee-return-pc-tn callee) code-tn label temp)
482       (note-this-location vop :call-site)
483       (inst b target)
484       (inst nop)
485       (trace-table-entry trace-table-normal)
486       (emit-return-pc label)
487       (default-unknown-values vop values nvals move-temp temp label)
488       (when cur-nfp
489         (load-stack-tn cur-nfp nfp-save)))))
490
491
492 ;;; Non-TR local call for a variable number of return values passed according
493 ;;; to the unknown values convention.  The results are the start of the values
494 ;;; glob and the number of values received.
495 ;;;
496 ;;; Note: we can't use normal load-tn allocation for the fixed args, since all
497 ;;; registers may be tied up by the more operand.  Instead, we use
498 ;;; MAYBE-LOAD-STACK-TN.
499 ;;;
500 (define-vop (multiple-call-local unknown-values-receiver)
501   (:args (fp)
502          (nfp)
503          (args :more t))
504   (:save-p t)
505   (:move-args :local-call)
506   (:info save callee target)
507   (:ignore args save)
508   (:vop-var vop)
509   (:temporary (:sc control-stack :offset nfp-save-offset) nfp-save)
510   (:temporary (:scs (non-descriptor-reg)) temp)
511   (:generator 20
512     (let ((label (gen-label))
513           (cur-nfp (current-nfp-tn vop)))
514       (when cur-nfp
515         (store-stack-tn nfp-save cur-nfp))
516       (let ((callee-nfp (callee-nfp-tn callee)))
517         (when callee-nfp
518           (maybe-load-stack-tn callee-nfp nfp)))
519       (maybe-load-stack-tn cfp-tn fp)
520       (trace-table-entry trace-table-call-site)
521       (inst compute-lra-from-code
522             (callee-return-pc-tn callee) code-tn label temp)
523       (note-this-location vop :call-site)
524       (inst b target)
525       (inst nop)
526       (trace-table-entry trace-table-normal)
527       (emit-return-pc label)
528       (note-this-location vop :unknown-return)
529       (receive-unknown-values values-start nvals start count label temp)
530       (when cur-nfp
531         (load-stack-tn cur-nfp nfp-save)))))
532
533 \f
534 ;;;; Local call with known values return:
535
536 ;;; Non-TR local call with known return locations.  Known-value return works
537 ;;; just like argument passing in local call.
538 ;;;
539 ;;; Note: we can't use normal load-tn allocation for the fixed args, since all
540 ;;; registers may be tied up by the more operand.  Instead, we use
541 ;;; MAYBE-LOAD-STACK-TN.
542 ;;;
543 (define-vop (known-call-local)
544   (:args (fp)
545          (nfp)
546          (args :more t))
547   (:results (res :more t))
548   (:move-args :local-call)
549   (:save-p t)
550   (:info save callee target)
551   (:ignore args res save)
552   (:vop-var vop)
553   (:temporary (:sc control-stack :offset nfp-save-offset) nfp-save)
554   (:temporary (:scs (non-descriptor-reg)) temp)
555   (:generator 5
556     (let ((label (gen-label))
557           (cur-nfp (current-nfp-tn vop)))
558       (when cur-nfp
559         (store-stack-tn nfp-save cur-nfp))
560       (let ((callee-nfp (callee-nfp-tn callee)))
561         (when callee-nfp
562           (maybe-load-stack-tn callee-nfp nfp)))
563       (maybe-load-stack-tn cfp-tn fp)
564       (trace-table-entry trace-table-call-site)
565       (inst compute-lra-from-code
566             (callee-return-pc-tn callee) code-tn label temp)
567       (note-this-location vop :call-site)
568       (inst b target)
569       (inst nop)
570       (trace-table-entry trace-table-normal)
571       (emit-return-pc label)
572       (note-this-location vop :known-return)
573       (when cur-nfp
574         (load-stack-tn cur-nfp nfp-save)))))
575
576 ;;; Return from known values call.  We receive the return locations as
577 ;;; arguments to terminate their lifetimes in the returning function.  We
578 ;;; restore FP and CSP and jump to the Return-PC.
579 ;;;
580 ;;; Note: we can't use normal load-tn allocation for the fixed args, since all
581 ;;; registers may be tied up by the more operand.  Instead, we use
582 ;;; MAYBE-LOAD-STACK-TN.
583 ;;;
584 (define-vop (known-return)
585   (:args (ocfp :target ocfp-temp)
586          (return-pc :target return-pc-temp)
587          (vals :more t))
588   (:temporary (:sc any-reg :from (:argument 0)) ocfp-temp)
589   (:temporary (:sc descriptor-reg :from (:argument 1))
590               return-pc-temp)
591   (:temporary (:scs (interior-reg)) lip)
592   (:move-args :known-return)
593   (:info val-locs)
594   (:ignore val-locs vals)
595   (:vop-var vop)
596   (:generator 6
597     (trace-table-entry trace-table-fun-epilogue)
598     (maybe-load-stack-tn ocfp-temp ocfp)
599     (maybe-load-stack-tn return-pc-temp return-pc)
600     (move csp-tn cfp-tn)
601     (let ((cur-nfp (current-nfp-tn vop)))
602       (when cur-nfp
603         (inst addu nsp-tn cur-nfp
604               (bytes-needed-for-non-descriptor-stack-frame))))
605     (inst addu lip return-pc-temp (- n-word-bytes other-pointer-lowtag))
606     (inst j lip)
607     (move cfp-tn ocfp-temp t)
608     (trace-table-entry trace-table-normal)))
609
610 \f
611 ;;;; Full call:
612 ;;;
613 ;;;    There is something of a cross-product effect with full calls.  Different
614 ;;; versions are used depending on whether we know the number of arguments or
615 ;;; the name of the called function, and whether we want fixed values, unknown
616 ;;; values, or a tail call.
617 ;;;
618 ;;; In full call, the arguments are passed creating a partial frame on the
619 ;;; stack top and storing stack arguments into that frame.  On entry to the
620 ;;; callee, this partial frame is pointed to by FP.  If there are no stack
621 ;;; arguments, we don't bother allocating a partial frame, and instead set FP
622 ;;; to SP just before the call.
623
624 ;;;    This macro helps in the definition of full call VOPs by avoiding code
625 ;;; replication in defining the cross-product VOPs.
626 ;;;
627 ;;; Name is the name of the VOP to define.
628 ;;;
629 ;;; Named is true if the first argument is a symbol whose global function
630 ;;; definition is to be called.
631 ;;;
632 ;;; Return is either :Fixed, :Unknown or :Tail:
633 ;;; -- If :Fixed, then the call is for a fixed number of values, returned in
634 ;;;    the standard passing locations (passed as result operands).
635 ;;; -- If :Unknown, then the result values are pushed on the stack, and the
636 ;;;    result values are specified by the Start and Count as in the
637 ;;;    unknown-values continuation representation.
638 ;;; -- If :Tail, then do a tail-recursive call.  No values are returned.
639 ;;;    The Ocfp and Return-PC are passed as the second and third arguments.
640 ;;;
641 ;;; In non-tail calls, the pointer to the stack arguments is passed as the last
642 ;;; fixed argument.  If Variable is false, then the passing locations are
643 ;;; passed as a more arg.  Variable is true if there are a variable number of
644 ;;; arguments passed on the stack.  Variable cannot be specified with :Tail
645 ;;; return.  TR variable argument call is implemented separately.
646 ;;;
647 ;;; In tail call with fixed arguments, the passing locations are passed as a
648 ;;; more arg, but there is no new-FP, since the arguments have been set up in
649 ;;; the current frame.
650 ;;;
651 (defmacro define-full-call (name named return variable)
652   (aver (not (and variable (eq return :tail))))
653   `(define-vop (,name
654                 ,@(when (eq return :unknown)
655                     '(unknown-values-receiver)))
656      (:args
657       ,@(unless (eq return :tail)
658           '((new-fp :scs (any-reg) :to :eval)))
659
660       ,(if named
661            '(name :target name-pass)
662            '(arg-fun :target lexenv))
663
664       ,@(when (eq return :tail)
665           '((ocfp :target ocfp-pass)
666             (return-pc :target return-pc-pass)))
667
668       ,@(unless variable '((args :more t :scs (descriptor-reg)))))
669
670      ,@(when (eq return :fixed)
671          '((:results (values :more t))))
672
673      (:save-p ,(if (eq return :tail) :compute-only t))
674
675      ,@(unless (or (eq return :tail) variable)
676          '((:move-args :full-call)))
677
678      (:vop-var vop)
679      (:info ,@(unless (or variable (eq return :tail)) '(arg-locs))
680             ,@(unless variable '(nargs))
681             ,@(when (eq return :fixed) '(nvals))
682             step-instrumenting)
683
684      (:ignore ,@(unless (or variable (eq return :tail)) '(arg-locs))
685               ,@(unless variable '(args)))
686
687      (:temporary (:sc descriptor-reg
688                   :offset ocfp-offset
689                   :from (:argument 1)
690                   ,@(unless (eq return :fixed)
691                       '(:to :eval)))
692                  ocfp-pass)
693
694      (:temporary (:sc descriptor-reg
695                   :offset lra-offset
696                   :from (:argument ,(if (eq return :tail) 2 1))
697                   :to :eval)
698                  return-pc-pass)
699
700      ,@(if named
701          `((:temporary (:sc descriptor-reg :offset fdefn-offset
702                         :from (:argument ,(if (eq return :tail) 0 1))
703                         :to :eval)
704                        name-pass))
705
706          `((:temporary (:sc descriptor-reg :offset lexenv-offset
707                         :from (:argument ,(if (eq return :tail) 0 1))
708                         :to :eval)
709                        lexenv)
710            (:temporary (:scs (descriptor-reg) :from (:argument 0) :to :eval)
711                        function)))
712
713      (:temporary (:sc any-reg :offset nargs-offset :to :eval)
714                  nargs-pass)
715
716      ,@(when variable
717          (mapcar #'(lambda (name offset)
718                      `(:temporary (:sc descriptor-reg
719                                    :offset ,offset
720                                    :to :eval)
721                          ,name))
722                  register-arg-names *register-arg-offsets*))
723      ,@(when (eq return :fixed)
724          '((:temporary (:scs (descriptor-reg) :from :eval) move-temp)))
725
726      (:temporary (:scs (descriptor-reg) :to :eval) stepping)
727
728      ,@(unless (eq return :tail)
729          '((:temporary (:scs (non-descriptor-reg)) temp)
730            (:temporary (:sc control-stack :offset nfp-save-offset) nfp-save)))
731
732      (:temporary (:sc interior-reg) entry-point)
733
734      (:generator ,(+ (if named 5 0)
735                      (if variable 19 1)
736                      (if (eq return :tail) 0 10)
737                      15
738                      (if (eq return :unknown) 25 0))
739        (let* ((cur-nfp (current-nfp-tn vop))
740               ,@(unless (eq return :tail)
741                   '((lra-label (gen-label))))
742               (step-done-label (gen-label))
743               (filler
744                (remove nil
745                        (list :load-nargs
746                              ,@(if (eq return :tail)
747                                    '((unless (location= ocfp ocfp-pass)
748                                        :load-ocfp)
749                                      (unless (location= return-pc
750                                                         return-pc-pass)
751                                        :load-return-pc)
752                                      (when cur-nfp
753                                        :frob-nfp))
754                                    '(:comp-lra
755                                      (when cur-nfp
756                                        :frob-nfp)
757                                      :save-fp
758                                      :load-fp))))))
759          (flet ((do-next-filler ()
760                   (let* ((next (pop filler))
761                          (what (if (consp next) (car next) next)))
762                     (ecase what
763                       (:load-nargs
764                        ,@(if variable
765                              `((inst subu nargs-pass csp-tn new-fp)
766                                ,@(let ((index -1))
767                                    (mapcar #'(lambda (name)
768                                                `(inst lw ,name new-fp
769                                                       ,(ash (incf index)
770                                                             word-shift)))
771                                            register-arg-names)))
772                              '((inst li nargs-pass (fixnumize nargs)))))
773                       ,@(if (eq return :tail)
774                             '((:load-ocfp
775                                (sc-case ocfp
776                                  (any-reg
777                                   (move ocfp-pass ocfp t))
778                                  (control-stack
779                                   (inst lw ocfp-pass cfp-tn
780                                         (ash (tn-offset ocfp)
781                                              word-shift)))))
782                               (:load-return-pc
783                                (sc-case return-pc
784                                  (descriptor-reg
785                                   (move return-pc-pass return-pc t))
786                                  (control-stack
787                                   (inst lw return-pc-pass cfp-tn
788                                         (ash (tn-offset return-pc)
789                                              word-shift)))))
790                               (:frob-nfp
791                                (inst addu nsp-tn cur-nfp
792                                      (bytes-needed-for-non-descriptor-stack-frame))))
793                             `((:comp-lra
794                                (inst compute-lra-from-code
795                                      return-pc-pass code-tn lra-label temp))
796                               (:frob-nfp
797                                (store-stack-tn nfp-save cur-nfp))
798                               (:save-fp
799                                (move ocfp-pass cfp-tn t))
800                               (:load-fp
801                                ,(if variable
802                                     '(move cfp-tn new-fp)
803                                     '(if (> nargs register-arg-count)
804                                          (move cfp-tn new-fp)
805                                          (move cfp-tn csp-tn)))
806                                (trace-table-entry trace-table-call-site))))
807                       ((nil)
808                        (inst nop)))))
809                 (insert-step-instrumenting (callable-tn)
810                   ;; Conditionally insert a conditional trap:
811                   (when step-instrumenting
812                     (load-symbol-value stepping sb!impl::*stepping*)
813                     ;; If it's not NIL, trap.
814                     (inst beq stepping null-tn step-done-label)
815                     (inst nop)
816                     ;; CONTEXT-PC will be pointing here when the
817                     ;; interrupt is handled, not after the BREAK.
818                     (note-this-location vop :step-before-vop)
819                     ;; Construct a trap code with the low bits from
820                     ;; SINGLE-STEP-AROUND-TRAP and the high bits from
821                     ;; the register number of CALLABLE-TN.
822                     (inst break 0 (logior single-step-around-trap
823                                           (ash (reg-tn-encoding callable-tn)
824                                                5)))
825                     (emit-label step-done-label))))
826
827            ,@(if named
828                  `((sc-case name
829                      (descriptor-reg (move name-pass name))
830                      (control-stack
831                       (inst lw name-pass cfp-tn
832                             (ash (tn-offset name) word-shift))
833                       (do-next-filler))
834                      (constant
835                       (inst lw name-pass code-tn
836                             (- (ash (tn-offset name) word-shift)
837                                other-pointer-lowtag))
838                       (do-next-filler)))
839                    ;; The step instrumenting must be done after
840                    ;; FUNCTION is loaded, but before ENTRY-POINT is
841                    ;; calculated.
842                    (insert-step-instrumenting name-pass)
843                    (inst lw entry-point name-pass
844                          (- (ash fdefn-raw-addr-slot word-shift)
845                             other-pointer-lowtag))
846                    (do-next-filler))
847                  `((sc-case arg-fun
848                      (descriptor-reg (move lexenv arg-fun))
849                      (control-stack
850                       (inst lw lexenv cfp-tn
851                             (ash (tn-offset arg-fun) word-shift))
852                       (do-next-filler))
853                      (constant
854                       (inst lw lexenv code-tn
855                             (- (ash (tn-offset arg-fun) word-shift)
856                                other-pointer-lowtag))
857                       (do-next-filler)))
858                    (inst lw function lexenv
859                          (- (ash closure-fun-slot word-shift)
860                             fun-pointer-lowtag))
861                    (do-next-filler)
862                    ;; The step instrumenting must be done before
863                    ;; after FUNCTION is loaded, but before ENTRY-POINT
864                    ;; is calculated.
865                    (insert-step-instrumenting function)
866                    (inst addu entry-point function
867                          (- (ash simple-fun-code-offset word-shift)
868                             fun-pointer-lowtag))))
869            (loop
870              (if (cdr filler)
871                  (do-next-filler)
872                  (return)))
873
874            (do-next-filler)
875            (note-this-location vop :call-site)
876            (inst j entry-point)
877            (inst nop))
878
879          ,@(ecase return
880              (:fixed
881               '((trace-table-entry trace-table-normal)
882                 (emit-return-pc lra-label)
883                 (default-unknown-values vop values nvals
884                                         move-temp temp lra-label)
885                 (when cur-nfp
886                   (load-stack-tn cur-nfp nfp-save))))
887              (:unknown
888               '((trace-table-entry trace-table-normal)
889                 (emit-return-pc lra-label)
890                 (note-this-location vop :unknown-return)
891                 (receive-unknown-values values-start nvals start count
892                                         lra-label temp)
893                 (when cur-nfp
894                   (load-stack-tn cur-nfp nfp-save))))
895              (:tail))))))
896
897
898 (define-full-call call nil :fixed nil)
899 (define-full-call call-named t :fixed nil)
900 (define-full-call multiple-call nil :unknown nil)
901 (define-full-call multiple-call-named t :unknown nil)
902 (define-full-call tail-call nil :tail nil)
903 (define-full-call tail-call-named t :tail nil)
904
905 (define-full-call call-variable nil :fixed t)
906 (define-full-call multiple-call-variable nil :unknown t)
907
908
909 ;;; Defined separately, since needs special code that BLT's the arguments
910 ;;; down.
911 ;;;
912 (define-vop (tail-call-variable)
913   (:args
914    (args-arg :scs (any-reg) :target args)
915    (function-arg :scs (descriptor-reg) :target lexenv)
916    (ocfp-arg :scs (any-reg) :target ocfp)
917    (lra-arg :scs (descriptor-reg) :target lra))
918
919   (:temporary (:sc any-reg :offset nl0-offset :from (:argument 0)) args)
920   (:temporary (:sc any-reg :offset lexenv-offset :from (:argument 1)) lexenv)
921   (:temporary (:sc any-reg :offset ocfp-offset :from (:argument 2)) ocfp)
922   (:temporary (:sc any-reg :offset lra-offset :from (:argument 3)) lra)
923
924   (:vop-var vop)
925
926   (:generator 75
927
928     ;; Move these into the passing locations if they are not already there.
929     (move args args-arg)
930     (move lexenv function-arg)
931     (move ocfp ocfp-arg)
932     (move lra lra-arg)
933
934     ;; Clear the number stack if anything is there and jump to the
935     ;; assembly-routine that does the bliting.
936     (inst j (make-fixup 'tail-call-variable :assembly-routine))
937     (let ((cur-nfp (current-nfp-tn vop)))
938       (if cur-nfp
939         (inst addu nsp-tn cur-nfp
940               (bytes-needed-for-non-descriptor-stack-frame))
941         (inst nop)))))
942
943 \f
944 ;;;; Unknown values return:
945
946 ;;; Return a single value using the unknown-values convention.
947 ;;;
948 (define-vop (return-single)
949   (:args (ocfp :scs (any-reg))
950          (return-pc :scs (descriptor-reg))
951          (value))
952   (:ignore value)
953   (:temporary (:scs (interior-reg)) lip)
954   (:vop-var vop)
955   (:generator 6
956     ;; Clear the number stack.
957     (trace-table-entry trace-table-fun-epilogue)
958     (let ((cur-nfp (current-nfp-tn vop)))
959       (when cur-nfp
960         (inst addu nsp-tn cur-nfp
961               (bytes-needed-for-non-descriptor-stack-frame))))
962     ;; Clear the control stack, and restore the frame pointer.
963     (move csp-tn cfp-tn)
964     (move cfp-tn ocfp)
965     ;; Out of here.
966     (lisp-return return-pc lip :offset 2)
967     (trace-table-entry trace-table-normal)))
968
969
970 ;;; Do unknown-values return of a fixed number of values.  The Values are
971 ;;; required to be set up in the standard passing locations.  Nvals is the
972 ;;; number of values returned.
973 ;;;
974 ;;; If returning a single value, then deallocate the current frame, restore
975 ;;; FP and jump to the single-value entry at Return-PC + 8.
976 ;;;
977 ;;; If returning other than one value, then load the number of values returned,
978 ;;; NIL out unsupplied values registers, restore FP and return at Return-PC.
979 ;;; When there are stack values, we must initialize the argument pointer to
980 ;;; point to the beginning of the values block (which is the beginning of the
981 ;;; current frame.)
982 ;;;
983 (define-vop (return)
984   (:args (ocfp :scs (any-reg))
985          (return-pc :scs (descriptor-reg) :to (:eval 1))
986          (values :more t))
987   (:ignore values)
988   (:info nvals)
989   (:temporary (:sc descriptor-reg :offset a0-offset :from (:eval 0)) a0)
990   (:temporary (:sc descriptor-reg :offset a1-offset :from (:eval 0)) a1)
991   (:temporary (:sc descriptor-reg :offset a2-offset :from (:eval 0)) a2)
992   (:temporary (:sc descriptor-reg :offset a3-offset :from (:eval 0)) a3)
993   (:temporary (:sc descriptor-reg :offset a4-offset :from (:eval 0)) a4)
994   (:temporary (:sc descriptor-reg :offset a5-offset :from (:eval 0)) a5)
995   (:temporary (:sc any-reg :offset nargs-offset) nargs)
996   (:temporary (:sc any-reg :offset ocfp-offset) val-ptr)
997   (:temporary (:scs (interior-reg)) lip)
998   (:vop-var vop)
999   (:generator 6
1000     ;; Clear the number stack.
1001     (trace-table-entry trace-table-fun-epilogue)
1002     (let ((cur-nfp (current-nfp-tn vop)))
1003       (when cur-nfp
1004         (inst addu nsp-tn cur-nfp
1005               (bytes-needed-for-non-descriptor-stack-frame))))
1006     (cond ((= nvals 1)
1007            ;; Clear the control stack, and restore the frame pointer.
1008            (move csp-tn cfp-tn)
1009            (move cfp-tn ocfp)
1010            ;; Out of here.
1011            (lisp-return return-pc lip :offset 2))
1012           (t
1013            ;; Establish the values pointer and values count.
1014            (move val-ptr cfp-tn)
1015            (inst li nargs (fixnumize nvals))
1016            ;; restore the frame pointer and clear as much of the control
1017            ;; stack as possible.
1018            (move cfp-tn ocfp)
1019            (inst addu csp-tn val-ptr (* nvals n-word-bytes))
1020            ;; pre-default any argument register that need it.
1021            (when (< nvals register-arg-count)
1022              (dolist (reg (subseq (list a0 a1 a2 a3 a4 a5) nvals))
1023                (move reg null-tn)))
1024            ;; And away we go.
1025            (lisp-return return-pc lip)))
1026     (trace-table-entry trace-table-normal)))
1027
1028 ;;; Do unknown-values return of an arbitrary number of values (passed on the
1029 ;;; stack.)  We check for the common case of a single return value, and do that
1030 ;;; inline using the normal single value return convention.  Otherwise, we
1031 ;;; branch off to code that calls an assembly-routine.
1032 ;;;
1033 (define-vop (return-multiple)
1034   (:args (ocfp-arg :scs (any-reg) :target ocfp)
1035          (lra-arg :scs (descriptor-reg) :target lra)
1036          (vals-arg :scs (any-reg) :target vals)
1037          (nvals-arg :scs (any-reg) :target nvals))
1038
1039   (:temporary (:sc any-reg :offset nl1-offset :from (:argument 0)) ocfp)
1040   (:temporary (:sc descriptor-reg :offset lra-offset :from (:argument 1)) lra)
1041   (:temporary (:sc any-reg :offset nl0-offset :from (:argument 2)) vals)
1042   (:temporary (:sc any-reg :offset nargs-offset :from (:argument 3)) nvals)
1043   (:temporary (:sc descriptor-reg :offset a0-offset) a0)
1044   (:temporary (:scs (interior-reg)) lip)
1045
1046   (:vop-var vop)
1047
1048   (:generator 13
1049     (trace-table-entry trace-table-fun-epilogue)
1050     (let ((not-single (gen-label)))
1051       ;; Clear the number stack.
1052       (let ((cur-nfp (current-nfp-tn vop)))
1053         (when cur-nfp
1054           (inst addu nsp-tn cur-nfp
1055                 (bytes-needed-for-non-descriptor-stack-frame))))
1056
1057       ;; Check for the single case.
1058       (inst li a0 (fixnumize 1))
1059       (inst bne nvals-arg a0 not-single)
1060       (inst lw a0 vals-arg)
1061
1062       ;; Return with one value.
1063       (move csp-tn cfp-tn)
1064       (move cfp-tn ocfp-arg)
1065       (lisp-return lra-arg lip :offset 2)
1066
1067       ;; Nope, not the single case.
1068       (emit-label not-single)
1069       (move ocfp ocfp-arg)
1070       (move lra lra-arg)
1071       (move vals vals-arg)
1072
1073       (inst j (make-fixup 'return-multiple :assembly-routine))
1074       (move nvals nvals-arg t))
1075     (trace-table-entry trace-table-normal)))
1076
1077
1078 \f
1079 ;;;; XEP hackery:
1080
1081
1082 ;;; We don't need to do anything special for regular functions.
1083 ;;;
1084 (define-vop (setup-environment)
1085   (:info label)
1086   (:ignore label)
1087   (:generator 0
1088     ;; Don't bother doing anything.
1089     ))
1090
1091 ;;; Get the lexical environment from its passing location.
1092 ;;;
1093 (define-vop (setup-closure-environment)
1094   (:temporary (:sc descriptor-reg :offset lexenv-offset :target closure
1095                :to (:result 0))
1096               lexenv)
1097   (:results (closure :scs (descriptor-reg)))
1098   (:info label)
1099   (:ignore label)
1100   (:generator 6
1101     ;; Get result.
1102     (move closure lexenv)))
1103
1104 ;;; Copy a more arg from the argument area to the end of the current frame.
1105 ;;; Fixed is the number of non-more arguments.
1106 ;;;
1107 (define-vop (copy-more-arg)
1108   (:temporary (:sc any-reg :offset nl0-offset) result)
1109   (:temporary (:sc any-reg :offset nl1-offset) count)
1110   (:temporary (:sc any-reg :offset nl2-offset) src)
1111   (:temporary (:sc any-reg :offset nl3-offset) dst)
1112   (:temporary (:sc descriptor-reg :offset l0-offset) temp)
1113   (:info fixed)
1114   (:generator 20
1115     (let ((loop (gen-label))
1116           (do-regs (gen-label))
1117           (done (gen-label)))
1118       (when (< fixed register-arg-count)
1119         ;; Save a pointer to the results so we can fill in register args.
1120         ;; We don't need this if there are more fixed args than reg args.
1121         (move result csp-tn))
1122       ;; Allocate the space on the stack.
1123       (cond ((zerop fixed)
1124              (inst beq nargs-tn done)
1125              (inst addu csp-tn csp-tn nargs-tn))
1126             (t
1127              (inst addu count nargs-tn (fixnumize (- fixed)))
1128              (inst blez count done)
1129              (inst nop)
1130              (inst addu csp-tn csp-tn count)))
1131       (when (< fixed register-arg-count)
1132         ;; We must stop when we run out of stack args, not when we run out of
1133         ;; more args.
1134         (inst addu count nargs-tn (fixnumize (- register-arg-count))))
1135       ;; Everything of interest in registers.
1136       (inst blez count do-regs)
1137       ;; Initialize dst to be end of stack.
1138       (move dst csp-tn t)
1139       ;; Initialize src to be end of args.
1140       (inst addu src cfp-tn nargs-tn)
1141
1142       (emit-label loop)
1143       ;; *--dst = *--src, --count
1144       (inst addu src src (- n-word-bytes))
1145       (inst addu count count (fixnumize -1))
1146       (loadw temp src)
1147       (inst addu dst dst (- n-word-bytes))
1148       (inst bgtz count loop)
1149       (storew temp dst)
1150
1151       (emit-label do-regs)
1152       (when (< fixed register-arg-count)
1153         ;; Now we have to deposit any more args that showed up in registers.
1154         ;; We know there is at least one more arg, otherwise we would have
1155         ;; branched to done up at the top.
1156         (inst subu count nargs-tn (fixnumize (1+ fixed)))
1157         (do ((i fixed (1+ i)))
1158             ((>= i register-arg-count))
1159           ;; Is this the last one?
1160           (inst beq count done)
1161           ;; Store it relative to the pointer saved at the start.
1162           (storew (nth i *register-arg-tns*) result (- i fixed))
1163           ;; Decrement count.
1164           (inst subu count (fixnumize 1))))
1165       (emit-label done))))
1166
1167
1168 ;;; More args are stored consecutively on the stack, starting
1169 ;;; immediately at the context pointer.  The context pointer is not
1170 ;;; typed, so the lowtag is 0.
1171 (define-full-reffer more-arg * 0 0 (descriptor-reg any-reg) * %more-arg)
1172
1173 ;;; Turn more arg (context, count) into a list.
1174 (define-vop (listify-rest-args)
1175   (:args (context-arg :target context :scs (descriptor-reg))
1176          (count-arg :target count :scs (any-reg)))
1177   (:arg-types * tagged-num)
1178   (:temporary (:scs (any-reg) :from (:argument 0)) context)
1179   (:temporary (:scs (any-reg) :from (:argument 1)) count)
1180   (:temporary (:scs (descriptor-reg) :from :eval) temp dst)
1181   (:temporary (:sc non-descriptor-reg :offset nl4-offset) pa-flag)
1182   (:results (result :scs (descriptor-reg)))
1183   (:translate %listify-rest-args)
1184   (:policy :safe)
1185   (:node-var node)
1186   (:generator 20
1187     (let* ((enter (gen-label))
1188            (loop (gen-label))
1189            (done (gen-label))
1190            (dx-p (node-stack-allocate-p node))
1191            (alloc-area-tn (if dx-p csp-tn alloc-tn)))
1192       (move context context-arg)
1193       (move count count-arg)
1194       ;; Check to see if there are any arguments.
1195       (inst beq count done)
1196       (move result null-tn t)
1197
1198       ;; We need to do this atomically.
1199       (pseudo-atomic (pa-flag)
1200         (when dx-p
1201           (align-csp temp))
1202         ;; Allocate a cons (2 words) for each item.
1203         (inst srl result alloc-area-tn n-lowtag-bits)
1204         (inst sll result n-lowtag-bits)
1205         (inst or result list-pointer-lowtag)
1206         (move dst result)
1207         (inst sll temp count 1)
1208         (inst b enter)
1209         (inst addu alloc-area-tn temp)
1210
1211         ;; Store the current cons in the cdr of the previous cons.
1212         (emit-label loop)
1213         (inst addu dst dst (* 2 n-word-bytes))
1214         (storew dst dst -1 list-pointer-lowtag)
1215
1216         (emit-label enter)
1217         ;; Grab one value.
1218         (loadw temp context)
1219         (inst addu context n-word-bytes)
1220
1221         ;; Dec count, and if != zero, go back for more.
1222         (inst addu count count (fixnumize -1))
1223         (inst bne count loop)
1224
1225         ;; Store the value in the car (in delay slot)
1226         (storew temp dst 0 list-pointer-lowtag)
1227
1228         ;; NIL out the last cons.
1229         (storew null-tn dst 1 list-pointer-lowtag))
1230       (emit-label done))))
1231
1232 ;;; Return the location and size of the more arg glob created by Copy-More-Arg.
1233 ;;; Supplied is the total number of arguments supplied (originally passed in
1234 ;;; NARGS.)  Fixed is the number of non-rest arguments.
1235 ;;;
1236 ;;; We must duplicate some of the work done by Copy-More-Arg, since at that
1237 ;;; time the environment is in a pretty brain-damaged state, preventing this
1238 ;;; info from being returned as values.  What we do is compute
1239 ;;; supplied - fixed, and return a pointer that many words below the current
1240 ;;; stack top.
1241 ;;;
1242 (define-vop (more-arg-context)
1243   (:policy :fast-safe)
1244   (:translate sb!c::%more-arg-context)
1245   (:args (supplied :scs (any-reg)))
1246   (:arg-types tagged-num (:constant fixnum))
1247   (:info fixed)
1248   (:results (context :scs (descriptor-reg))
1249             (count :scs (any-reg)))
1250   (:result-types t tagged-num)
1251   (:note "more-arg-context")
1252   (:generator 5
1253     (inst addu count supplied (fixnumize (- fixed)))
1254     (inst subu context csp-tn count)))
1255
1256
1257 ;;; Signal wrong argument count error if Nargs isn't = to Count.
1258 ;;;
1259 (define-vop (verify-arg-count)
1260   (:policy :fast-safe)
1261   (:translate sb!c::%verify-arg-count)
1262   (:args (nargs :scs (any-reg)))
1263   (:arg-types positive-fixnum (:constant t))
1264   (:temporary (:scs (any-reg) :type fixnum) temp)
1265   (:info count)
1266   (:vop-var vop)
1267   (:save-p :compute-only)
1268   (:generator 3
1269     (let ((err-lab
1270            (generate-error-code vop invalid-arg-count-error nargs)))
1271       (cond ((zerop count)
1272              (inst bne nargs err-lab)
1273              (inst nop))
1274             (t
1275              (inst li temp (fixnumize count))
1276              (inst bne nargs temp err-lab)
1277              (inst nop))))))
1278
1279 ;;; Various other error signalers.
1280 ;;;
1281 (macrolet ((frob (name error translate &rest args)
1282              `(define-vop (,name)
1283                 ,@(when translate
1284                     `((:policy :fast-safe)
1285                       (:translate ,translate)))
1286                 (:args ,@(mapcar #'(lambda (arg)
1287                                      `(,arg :scs (any-reg descriptor-reg)))
1288                                  args))
1289                 (:vop-var vop)
1290                 (:save-p :compute-only)
1291                 (:generator 1000
1292                   (error-call vop ,error ,@args)))))
1293   (frob arg-count-error invalid-arg-count-error
1294     sb!c::%arg-count-error nargs)
1295   (frob type-check-error object-not-type-error sb!c::%type-check-error
1296     object type)
1297   (frob layout-invalid-error layout-invalid-error sb!c::%layout-invalid-error
1298     object layout)
1299   (frob odd-key-args-error odd-key-args-error
1300     sb!c::%odd-key-args-error)
1301   (frob unknown-key-arg-error unknown-key-arg-error
1302     sb!c::%unknown-key-arg-error key)
1303   (frob nil-fun-returned-error nil-fun-returned-error nil fun))
1304
1305 ;;; Single-stepping
1306
1307 (define-vop (step-instrument-before-vop)
1308   (:temporary (:scs (descriptor-reg)) stepping)
1309   (:policy :fast-safe)
1310   (:vop-var vop)
1311   (:generator 3
1312     (load-symbol-value stepping sb!impl::*stepping*)
1313     ;; If it's not NIL, trap.
1314     (inst beq stepping null-tn DONE)
1315     (inst nop)
1316     ;; CONTEXT-PC will be pointing here when the interrupt is handled,
1317     ;; not after the BREAK.
1318     (note-this-location vop :step-before-vop)
1319     ;; CALLEE-REGISTER-OFFSET isn't needed for before-traps, so we
1320     ;; can just use a bare SINGLE-STEP-BEFORE-TRAP as the code.
1321     (inst break 0 single-step-before-trap)
1322     DONE))