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