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