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