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