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