0.7.1.20:
[sbcl.git] / src / compiler / sparc / call.lisp
1 ;;;; the VM definition of function call for the Sparc
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
14 \f
15 ;;;; Interfaces to IR2 conversion:
16
17 ;;; Return a wired TN describing the N'th full call argument passing
18 ;;; location.
19 (!def-vm-support-routine standard-arg-location (n)
20   (declare (type unsigned-byte n))
21   (if (< n register-arg-count)
22       (make-wired-tn *backend-t-primitive-type* register-arg-scn
23                      (elt *register-arg-offsets* n))
24       (make-wired-tn *backend-t-primitive-type* control-stack-arg-scn n)))
25
26
27 ;;; Make a passing location TN for a local call return PC.  If
28 ;;; standard is true, then use the standard (full call) location,
29 ;;; otherwise use any legal location.  Even in the non-standard case,
30 ;;; this may be restricted by a desire to use a subroutine call
31 ;;; instruction.
32 (!def-vm-support-routine make-return-pc-passing-location (standard)
33   (if standard
34       (make-wired-tn *backend-t-primitive-type* register-arg-scn lra-offset)
35       (make-restricted-tn *backend-t-primitive-type* register-arg-scn)))
36
37 ;;; Similar to Make-Return-PC-Passing-Location, but makes a location
38 ;;; to pass Old-FP in.  This is (obviously) wired in the standard
39 ;;; convention, but is totally unrestricted in non-standard
40 ;;; conventions, since we can always fetch it off of the stack using
41 ;;; the arg pointer.
42 (!def-vm-support-routine make-old-fp-passing-location (standard)
43   (if standard
44       (make-wired-tn *fixnum-primitive-type* immediate-arg-scn ocfp-offset)
45       (make-normal-tn *fixnum-primitive-type*)))
46
47 ;;; Make the TNs used to hold Old-FP and Return-PC within the current
48 ;;; function.  We treat these specially so that the debugger can find
49 ;;; them at a known location.
50 (!def-vm-support-routine make-old-fp-save-location (env)
51   (specify-save-tn
52    (physenv-debug-live-tn (make-normal-tn *fixnum-primitive-type*) env)
53    (make-wired-tn *fixnum-primitive-type*
54                   control-stack-arg-scn
55                   ocfp-save-offset)))
56
57 (!def-vm-support-routine make-return-pc-save-location (env)
58   (specify-save-tn
59    (physenv-debug-live-tn (make-normal-tn *backend-t-primitive-type*) env)
60    (make-wired-tn *backend-t-primitive-type*
61                   control-stack-arg-scn
62                   lra-save-offset)))
63
64 ;;; Make a TN for the standard argument count passing location.  We
65 ;;; only need to make the standard location, since a count is never
66 ;;; passed when we are using non-standard conventions.
67 (!def-vm-support-routine make-arg-count-location ()
68   (make-wired-tn *fixnum-primitive-type* immediate-arg-scn nargs-offset))
69
70
71 ;;; Make a TN to hold the number-stack frame pointer.  This is
72 ;;; allocated once per component, and is component-live.
73 (!def-vm-support-routine make-nfp-tn ()
74   (component-live-tn
75    (make-wired-tn *fixnum-primitive-type* immediate-arg-scn nfp-offset)))
76
77 (!def-vm-support-routine make-stack-pointer-tn ()
78   (make-normal-tn *fixnum-primitive-type*))
79
80 (!def-vm-support-routine make-number-stack-pointer-tn ()
81   (make-normal-tn *fixnum-primitive-type*))
82
83 ;;; Return a list of TNs that can be used to represent an
84 ;;; unknown-values continuation within a function.
85 (!def-vm-support-routine make-unknown-values-locations ()
86   (list (make-stack-pointer-tn)
87         (make-normal-tn *fixnum-primitive-type*)))
88
89
90 ;;; This function is called by the Entry-Analyze phase, allowing
91 ;;; VM-dependent initialization of the IR2-Component structure.  We push
92 ;;; placeholder entries in the Constants to leave room for additional
93 ;;; noise in the code object header.
94 (!def-vm-support-routine select-component-format (component)
95   (declare (type component component))
96   (dotimes (i code-constants-offset)
97     (vector-push-extend nil
98                         (ir2-component-constants (component-info component))))
99   (values))
100 \f
101 ;;;; Frame hackery:
102
103 ;;; Return the number of bytes needed for the current non-descriptor
104 ;;; stack frame.  Non-descriptor stack frames must be multiples of 8
105 ;;; bytes on the PMAX.
106 (defun bytes-needed-for-non-descriptor-stack-frame ()
107   (* (logandc2 (1+ (sb-allocated-size 'non-descriptor-stack)) 1)
108      n-word-bytes))
109
110 ;;; Used for setting up the Old-FP in local call.
111 (define-vop (current-fp)
112   (:results (val :scs (any-reg)))
113   (:generator 1
114     (move val cfp-tn)))
115
116 ;;; Used for computing the caller's NFP for use in known-values return.  Only
117 ;;; works assuming there is no variable size stuff on the nstack.
118 ;;;
119 (define-vop (compute-old-nfp)
120   (:results (val :scs (any-reg)))
121   (:vop-var vop)
122   (:generator 1
123     (let ((nfp (current-nfp-tn vop)))
124       (when nfp
125         (inst add val nfp (bytes-needed-for-non-descriptor-stack-frame))))))
126
127
128 (define-vop (xep-allocate-frame)
129   (:info start-lab copy-more-arg-follows)
130   (:ignore copy-more-arg-follows)
131   (:vop-var vop)
132   (:temporary (:scs (non-descriptor-reg)) temp)
133   (:generator 1
134     ;; Make sure the function is aligned, and drop a label pointing to this
135     ;; function header.
136     (align n-lowtag-bits)
137     (trace-table-entry trace-table-fun-prologue)
138     (emit-label start-lab)
139     ;; Allocate function header.
140     (inst simple-fun-header-word)
141     (dotimes (i (1- simple-fun-code-offset))
142       (inst word 0))
143     ;; The start of the actual code.
144     ;; Fix CODE, cause the function object was passed in.
145     (inst compute-code-from-fn code-tn code-tn start-lab temp)
146     ;; Build our stack frames.
147     (inst add csp-tn cfp-tn
148           (* n-word-bytes (sb-allocated-size 'control-stack)))
149     (let ((nfp-tn (current-nfp-tn vop)))
150       (when nfp-tn
151         (inst sub nsp-tn (bytes-needed-for-non-descriptor-stack-frame))
152         (inst add 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 add csp-tn csp-tn
163           (* n-word-bytes (sb-allocated-size 'control-stack)))
164     (when (ir2-physenv-number-stack-p callee)
165       (inst sub nsp-tn (bytes-needed-for-non-descriptor-stack-frame))
166       (inst add nfp nsp-tn number-stack-displacement))
167     (trace-table-entry trace-table-normal)))
168
169 ;;; Allocate a partial frame for passing stack arguments in a full call.  Nargs
170 ;;; is the number of arguments passed.  If no stack arguments are passed, then
171 ;;; we don't have to do anything.
172 ;;;
173 (define-vop (allocate-full-call-frame)
174   (:info nargs)
175   (:results (res :scs (any-reg)))
176   (:generator 2
177     (when (> nargs register-arg-count)
178       (move res csp-tn)
179       (inst add csp-tn csp-tn (* nargs n-word-bytes)))))
180
181
182
183 \f
184 ;;; Emit code needed at the return-point from an unknown-values call
185 ;;; for a fixed number of values.  Values is the head of the TN-Ref
186 ;;; list for the locations that the values are to be received into.
187 ;;; Nvals is the number of values that are to be received (should
188 ;;; equal the length of Values).
189 ;;;
190 ;;; Move-Temp is a Descriptor-Reg TN used as a temporary.
191 ;;;
192 ;;; This code exploits the fact that in the unknown-values convention,
193 ;;; a single value return returns at the return PC + 8, whereas a
194 ;;; return of other than one value returns directly at the return PC.
195 ;;;
196 ;;; If 0 or 1 values are expected, then we just emit an instruction to
197 ;;; reset the SP (which will only be executed when other than 1 value
198 ;;; is returned.)
199 ;;;
200 ;;; In the general case, we have to do three things:
201 ;;;  -- Default unsupplied register values.  This need only be done when a
202 ;;;     single value is returned, since register values are defaulted by the
203 ;;;     called in the non-single case.
204 ;;;  -- Default unsupplied stack values.  This needs to be done whenever there
205 ;;;     are stack values.
206 ;;;  -- Reset SP.  This must be done whenever other than 1 value is returned,
207 ;;;     regardless of the number of values desired.
208 ;;;
209 ;;; The general-case code looks like this:
210 #|
211         b regs-defaulted                ; Skip if MVs
212         nop
213
214         move a1 null-tn                 ; Default register values
215         ...
216         loadi nargs 1                   ; Force defaulting of stack values
217         move old-fp csp                 ; Set up args for SP resetting
218
219 regs-defaulted
220         subcc temp nargs register-arg-count
221
222         b :lt default-value-7   ; jump to default code
223         loadw move-temp ocfp-tn 6       ; Move value to correct location.
224         subcc temp 1
225         store-stack-tn val4-tn move-temp
226
227         b :lt default-value-8
228         loadw move-temp ocfp-tn 7
229         subcc temp 1
230         store-stack-tn val5-tn move-temp
231
232         ...
233
234 defaulting-done
235         move csp ocfp                   ; Reset SP.
236 <end of code>
237
238 <elsewhere>
239 default-value-7
240         store-stack-tn val4-tn null-tn  ; Nil out 7'th value. (first on stack)
241
242 default-value-8
243         store-stack-tn val5-tn null-tn  ; Nil out 8'th value.
244
245         ...
246
247         br defaulting-done
248         nop
249 |#
250 (defun default-unknown-values (vop values nvals move-temp temp lra-label)
251   (declare (type (or tn-ref null) values)
252            (type unsigned-byte nvals) (type tn move-temp temp))
253   (if (<= nvals 1)
254       (progn
255         (without-scheduling ()
256           (note-this-location vop :single-value-return)
257           (move csp-tn ocfp-tn)
258           (inst nop))
259         (inst compute-code-from-lra code-tn code-tn lra-label temp))
260       (let ((regs-defaulted (gen-label))
261             (defaulting-done (gen-label))
262             (default-stack-vals (gen-label)))
263         ;; Branch off to the MV case.
264         (without-scheduling ()
265           (note-this-location vop :unknown-return)
266           (inst b regs-defaulted)
267           (if (> nvals register-arg-count)
268               (inst subcc temp nargs-tn (fixnumize register-arg-count))
269               (move csp-tn ocfp-tn)))
270         
271         ;; Do the single value calse.
272         (do ((i 1 (1+ i))
273              (val (tn-ref-across values) (tn-ref-across val)))
274             ((= i (min nvals register-arg-count)))
275           (move (tn-ref-tn val) null-tn))
276         (when (> nvals register-arg-count)
277           (inst b default-stack-vals)
278           (move ocfp-tn csp-tn))
279         
280         (emit-label regs-defaulted)
281         (when (> nvals register-arg-count)
282           (collect ((defaults))
283             (do ((i register-arg-count (1+ i))
284                  (val (do ((i 0 (1+ i))
285                            (val values (tn-ref-across val)))
286                           ((= i register-arg-count) val))
287                       (tn-ref-across val)))
288                 ((null val))
289               
290               (let ((default-lab (gen-label))
291                     (tn (tn-ref-tn val)))
292                 (defaults (cons default-lab tn))
293                 
294                 (inst b :le default-lab)
295                 (inst ld move-temp ocfp-tn (* i n-word-bytes))
296                 (inst subcc temp (fixnumize 1))
297                 (store-stack-tn tn move-temp)))
298             
299             (emit-label defaulting-done)
300             (move csp-tn ocfp-tn)
301             
302             (let ((defaults (defaults)))
303               (when defaults
304                 (assemble (*elsewhere*)
305                   (emit-label default-stack-vals)
306                   (trace-table-entry trace-table-fun-prologue)
307                   (do ((remaining defaults (cdr remaining)))
308                       ((null remaining))
309                     (let ((def (car remaining)))
310                       (emit-label (car def))
311                       (when (null (cdr remaining))
312                         (inst b defaulting-done))
313                       (store-stack-tn (cdr def) null-tn)))
314                   (trace-table-entry trace-table-normal))))))
315
316         (inst compute-code-from-lra code-tn code-tn lra-label temp)))
317   (values))
318
319 \f
320 ;;; Receive-Unknown-Values  --  Internal
321 ;;;
322 ;;; Emit code needed at the return point for an unknown-values call
323 ;;; for an arbitrary number of values.
324 ;;;
325 ;;; We do the single and non-single cases with no shared code: there
326 ;;; doesn't seem to be any potential overlap, and receiving a single
327 ;;; value is more important efficiency-wise.
328 ;;;
329 ;;; When there is a single value, we just push it on the stack,
330 ;;; returning the old SP and 1.
331 ;;;
332 ;;; When there is a variable number of values, we move all of the
333 ;;; argument registers onto the stack, and return Args and Nargs.
334 ;;;
335 ;;; Args and Nargs are TNs wired to the named locations.  We must
336 ;;; explicitly allocate these TNs, since their lifetimes overlap with
337 ;;; the results Start and Count (also, it's nice to be able to target
338 ;;; 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     (without-scheduling ()
344       (inst b variable-values)
345       (inst nop))
346     
347     (inst compute-code-from-lra code-tn code-tn lra-label temp)
348     (inst add csp-tn 4)
349     (storew (first *register-arg-tns*) csp-tn -1)
350     (inst sub 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 code-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       (inst nop)
367       (trace-table-entry trace-table-normal)))
368   (values))
369
370
371 ;;; VOP that can be inherited by unknown values receivers.  The main
372 ;;; thing this handles is allocation of the result temporaries.
373 (define-vop (unknown-values-receiver)
374   (:results
375    (start :scs (any-reg))
376    (count :scs (any-reg)))
377   (:temporary (:sc descriptor-reg :offset ocfp-offset
378                    :from :eval :to (:result 0))
379               values-start)
380   (:temporary (:sc any-reg :offset nargs-offset
381                :from :eval :to (:result 1))
382               nvals)
383   (:temporary (:scs (non-descriptor-reg)) temp))
384
385
386 \f
387 ;;;; Local call with unknown values convention return:
388
389 ;;; Non-TR local call for a fixed number of values passed according to the
390 ;;; unknown values convention.
391 ;;;
392 ;;; Args are the argument passing locations, which are specified only to
393 ;;; terminate their lifetimes in the caller.
394 ;;;
395 ;;; Values are the return value locations (wired to the standard passing
396 ;;; locations).
397 ;;;
398 ;;; Save is the save info, which we can ignore since saving has been done.
399 ;;; Return-PC is the TN that the return PC should be passed in.
400 ;;; Target is a continuation pointing to the start of the called function.
401 ;;; Nvals is the number of values received.
402 ;;;
403 ;;; Note: we can't use normal load-tn allocation for the fixed args, since all
404 ;;; registers may be tied up by the more operand.  Instead, we use
405 ;;; MAYBE-LOAD-STACK-TN.
406 (define-vop (call-local)
407   (:args (fp)
408          (nfp)
409          (args :more t))
410   (:results (values :more t))
411   (:save-p t)
412   (:move-args :local-call)
413   (:info arg-locs callee target nvals)
414   (:vop-var vop)
415   (:temporary (:scs (descriptor-reg) :from (:eval 0)) move-temp)
416   (:temporary (:scs (non-descriptor-reg)) temp)
417   (:temporary (:sc control-stack :offset nfp-save-offset) nfp-save)
418   (:temporary (:sc any-reg :offset ocfp-offset :from (:eval 0)) ocfp)
419   (:ignore arg-locs args ocfp)
420   (:generator 5
421     (trace-table-entry trace-table-call-site)
422     (let ((label (gen-label))
423           (cur-nfp (current-nfp-tn vop)))
424       (when cur-nfp
425         (store-stack-tn nfp-save cur-nfp))
426       (let ((callee-nfp (callee-nfp-tn callee)))
427         (when callee-nfp
428           (maybe-load-stack-tn callee-nfp nfp)))
429       (maybe-load-stack-tn cfp-tn fp)
430       (inst compute-lra-from-code
431             (callee-return-pc-tn callee) code-tn label temp)
432       (note-this-location vop :call-site)
433       (inst b target)
434       (inst nop)
435       (emit-return-pc label)
436       (default-unknown-values vop values nvals move-temp temp label)
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   (:generator 20
460     (trace-table-entry trace-table-call-site)
461     (let ((label (gen-label))
462           (cur-nfp (current-nfp-tn vop)))
463       (when cur-nfp
464         (store-stack-tn nfp-save cur-nfp))
465       (let ((callee-nfp (callee-nfp-tn callee)))
466         (when callee-nfp
467           (maybe-load-stack-tn callee-nfp nfp)))
468       (maybe-load-stack-tn cfp-tn fp)
469       (inst compute-lra-from-code
470             (callee-return-pc-tn callee) code-tn label temp)
471       (note-this-location vop :call-site)
472       (inst b target)
473       (inst nop)
474       (emit-return-pc label)
475       (note-this-location vop :unknown-return)
476       (receive-unknown-values values-start nvals start count label temp)
477       (when cur-nfp
478         (load-stack-tn cur-nfp nfp-save)))
479     (trace-table-entry trace-table-normal)))
480
481 \f
482 ;;;; Local call with known values return:
483
484 ;;; Non-TR local call with known return locations.  Known-value return works
485 ;;; just like argument passing in local call.
486 ;;;
487 ;;; Note: we can't use normal load-tn allocation for the fixed args, since all
488 ;;; registers may be tied up by the more operand.  Instead, we use
489 ;;; MAYBE-LOAD-STACK-TN.
490 (define-vop (known-call-local)
491   (:args (fp)
492          (nfp)
493          (args :more t))
494   (:results (res :more t))
495   (:move-args :local-call)
496   (:save-p t)
497   (:info save callee target)
498   (:ignore args res save)
499   (:vop-var vop)
500   (:temporary (:sc control-stack :offset nfp-save-offset) nfp-save)
501   (:temporary (:scs (non-descriptor-reg)) temp)
502   (:generator 5
503     (trace-table-entry trace-table-call-site)
504     (let ((label (gen-label))
505           (cur-nfp (current-nfp-tn vop)))
506       (when cur-nfp
507         (store-stack-tn nfp-save cur-nfp))
508       (let ((callee-nfp (callee-nfp-tn callee)))
509         (when callee-nfp
510           (maybe-load-stack-tn callee-nfp nfp)))
511       (maybe-load-stack-tn cfp-tn fp)
512       (inst compute-lra-from-code
513             (callee-return-pc-tn callee) code-tn label temp)
514       (note-this-location vop :call-site)
515       (inst b target)
516       (inst nop)
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 add nsp-tn cur-nfp 
548               (- (bytes-needed-for-non-descriptor-stack-frame)
549                  number-stack-displacement))))
550     (inst j return-pc-temp (- n-word-bytes other-pointer-lowtag))
551     (move cfp-tn old-fp-temp)
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.
558 ;;; Different versions are used depending on whether we know the
559 ;;; number of arguments or the name of the called function, and
560 ;;; whether we want fixed values, unknown values, or a tail call.
561 ;;;
562 ;;; In full call, the arguments are passed creating a partial frame on
563 ;;; the stack top and storing stack arguments into that frame.  On
564 ;;; entry to the callee, this partial frame is pointed to by FP.  If
565 ;;; there are no stack arguments, we don't bother allocating a partial
566 ;;; frame, and instead set FP 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   (assert (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
626      (:ignore
627       ,@(unless (or variable (eq return :tail)) '(arg-locs))
628       ,@(unless variable '(args)))
629
630      (:temporary (:sc descriptor-reg
631                   :offset ocfp-offset
632                   :from (:argument 1)
633                   ,@(unless (eq return :fixed)
634                       '(:to :eval)))
635                  old-fp-pass)
636
637      (:temporary (:sc descriptor-reg
638                   :offset lra-offset
639                   :from (:argument ,(if (eq return :tail) 2 1))
640                   :to :eval)
641                  return-pc-pass)
642
643      ,(if named
644           `(:temporary (:sc descriptor-reg :offset cname-offset
645                             :from (:argument ,(if (eq return :tail) 0 1))
646                             :to :eval)
647                        name-pass)
648           `(:temporary (:sc descriptor-reg :offset lexenv-offset
649                             :from (:argument ,(if (eq return :tail) 0 1))
650                             :to :eval)
651                        lexenv))
652
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      ,@(unless (eq return :tail)
669          '((:temporary (:scs (non-descriptor-reg)) temp)
670            (:temporary (:sc control-stack :offset nfp-save-offset) nfp-save)))
671
672      (:generator ,(+ (if named 5 0)
673                      (if variable 19 1)
674                      (if (eq return :tail) 0 10)
675                      15
676                      (if (eq return :unknown) 25 0))
677        (trace-table-entry trace-table-call-site)
678        (let* ((cur-nfp (current-nfp-tn vop))
679               ,@(unless (eq return :tail)
680                   '((lra-label (gen-label))))
681               (filler
682                (remove nil
683                        (list :load-nargs
684                              ,@(if (eq return :tail)
685                                    '((unless (location= old-fp old-fp-pass)
686                                        :load-old-fp)
687                                      (unless (location= return-pc
688                                                         return-pc-pass)
689                                        :load-return-pc)
690                                      (when cur-nfp
691                                        :frob-nfp))
692                                    '(:comp-lra
693                                      (when cur-nfp
694                                        :frob-nfp)
695                                      :save-fp
696                                      :load-fp))))))
697          (flet ((do-next-filler ()
698                   (let* ((next (pop filler))
699                          (what (if (consp next) (car next) next)))
700                     (ecase what
701                       (:load-nargs
702                        ,@(if variable
703                              `((inst sub nargs-pass csp-tn new-fp)
704                                ,@(let ((index -1))
705                                    (mapcar #'(lambda (name)
706                                                `(loadw ,name new-fp
707                                                        ,(incf index)))
708                                            register-arg-names)))
709                              '((inst li nargs-pass (fixnumize nargs)))))
710                       ,@(if (eq return :tail)
711                             '((:load-old-fp
712                                (sc-case old-fp
713                                  (any-reg
714                                   (inst move old-fp-pass old-fp))
715                                  (control-stack
716                                   (loadw old-fp-pass cfp-tn
717                                          (tn-offset old-fp)))))
718                               (:load-return-pc
719                                (sc-case return-pc
720                                  (descriptor-reg
721                                   (inst move return-pc-pass return-pc))
722                                  (control-stack
723                                   (loadw return-pc-pass cfp-tn
724                                          (tn-offset return-pc)))))
725                               (:frob-nfp
726                                (inst add nsp-tn cur-nfp
727                                      (- (bytes-needed-for-non-descriptor-stack-frame)
728                                         number-stack-displacement))))
729                             `((:comp-lra
730                                (inst compute-lra-from-code
731                                      return-pc-pass code-tn lra-label temp))
732                               (:frob-nfp
733                                (store-stack-tn nfp-save cur-nfp))
734                               (:save-fp
735                                (inst move old-fp-pass cfp-tn))
736                               (:load-fp
737                                ,(if variable
738                                     '(move cfp-tn new-fp)
739                                     '(if (> nargs register-arg-count)
740                                          (move cfp-tn new-fp)
741                                          (move cfp-tn csp-tn))))))
742                       ((nil))))))
743
744            ,@(if named
745                  `((sc-case name
746                      (descriptor-reg (move name-pass name))
747                      (control-stack
748                       (loadw name-pass cfp-tn (tn-offset name))
749                       (do-next-filler))
750                      (constant
751                       (loadw name-pass code-tn (tn-offset name)
752                              other-pointer-lowtag)
753                       (do-next-filler)))
754                    (loadw function name-pass fdefn-raw-addr-slot
755                           other-pointer-lowtag)
756                    (do-next-filler))
757                  `((sc-case arg-fun
758                      (descriptor-reg (move lexenv arg-fun))
759                      (control-stack
760                       (loadw lexenv cfp-tn (tn-offset arg-fun))
761                       (do-next-filler))
762                      (constant
763                       (loadw lexenv code-tn (tn-offset arg-fun)
764                              other-pointer-lowtag)
765                       (do-next-filler)))
766                    (loadw function lexenv closure-fun-slot
767                           fun-pointer-lowtag)
768                    (do-next-filler)))
769            (loop
770              (if filler
771                  (do-next-filler)
772                  (return)))
773            
774            (note-this-location vop :call-site)
775            (inst j function
776                  (- (ash simple-fun-code-offset word-shift)
777                     fun-pointer-lowtag))
778            (inst move code-tn function))
779
780          ,@(ecase return
781              (:fixed
782               '((emit-return-pc lra-label)
783                 (default-unknown-values vop values nvals move-temp
784                                         temp lra-label)
785                 (when cur-nfp
786                   (load-stack-tn cur-nfp nfp-save))))
787              (:unknown
788               '((emit-return-pc lra-label)
789                 (note-this-location vop :unknown-return)
790                 (receive-unknown-values values-start nvals start count
791                                         lra-label temp)
792                 (when cur-nfp
793                   (load-stack-tn cur-nfp nfp-save))))
794              (:tail)))
795        (trace-table-entry trace-table-normal))))
796
797
798 (define-full-call call nil :fixed nil)
799 (define-full-call call-named t :fixed nil)
800 (define-full-call multiple-call nil :unknown nil)
801 (define-full-call multiple-call-named t :unknown nil)
802 (define-full-call tail-call nil :tail nil)
803 (define-full-call tail-call-named t :tail nil)
804
805 (define-full-call call-variable nil :fixed t)
806 (define-full-call multiple-call-variable nil :unknown t)
807
808
809 ;;; Defined separately, since needs special code that BLT's the
810 ;;; arguments down.
811 (define-vop (tail-call-variable)
812   (:args
813    (args-arg :scs (any-reg) :target args)
814    (function-arg :scs (descriptor-reg) :target lexenv)
815    (old-fp-arg :scs (any-reg) :target old-fp)
816    (lra-arg :scs (descriptor-reg) :target lra))
817
818   (:temporary (:sc any-reg :offset nl0-offset :from (:argument 0)) args)
819   (:temporary (:sc any-reg :offset lexenv-offset :from (:argument 1)) lexenv)
820   (:temporary (:sc any-reg :offset ocfp-offset :from (:argument 2)) old-fp)
821   (:temporary (:sc any-reg :offset lra-offset :from (:argument 3)) lra)
822
823   (:temporary (:scs (any-reg) :from :eval) temp)
824
825   (:vop-var vop)
826
827   (:generator 75
828
829     ;; Move these into the passing locations if they are not already there.
830     (move args args-arg)
831     (move lexenv function-arg)
832     (move old-fp old-fp-arg)
833     (move lra lra-arg)
834
835     ;; Clear the number stack if anything is there.
836     (let ((cur-nfp (current-nfp-tn vop)))
837       (when cur-nfp
838         (inst add nsp-tn cur-nfp
839               (- (bytes-needed-for-non-descriptor-stack-frame)
840                  number-stack-displacement))))
841
842     ;; And jump to the assembly-routine that does the bliting.
843     (inst ji temp (make-fixup 'tail-call-variable :assembly-routine))
844     (inst nop)))
845
846 \f
847 ;;;; Unknown values return:
848
849
850 ;;; Return a single value using the unknown-values convention.
851 (define-vop (return-single)
852   (:args (old-fp :scs (any-reg))
853          (return-pc :scs (descriptor-reg))
854          (value))
855   (:ignore value)
856   (:vop-var vop)
857   (:generator 6
858     (trace-table-entry trace-table-fun-epilogue)
859     ;; Clear the number stack.
860     (let ((cur-nfp (current-nfp-tn vop)))
861       (when cur-nfp
862         (inst add nsp-tn cur-nfp
863               (- (bytes-needed-for-non-descriptor-stack-frame)
864                  number-stack-displacement))))
865     ;; Clear the control stack, and restore the frame pointer.
866     (move csp-tn cfp-tn)
867     (move cfp-tn old-fp)
868     ;; Out of here.
869     (lisp-return return-pc :offset 2)
870     (trace-table-entry trace-table-normal)))
871
872 ;;; Do unknown-values return of a fixed number of values.  The Values are
873 ;;; required to be set up in the standard passing locations.  Nvals is the
874 ;;; number of values returned.
875 ;;;
876 ;;; If returning a single value, then deallocate the current frame, restore
877 ;;; FP and jump to the single-value entry at Return-PC + 8.
878 ;;;
879 ;;; If returning other than one value, then load the number of values returned,
880 ;;; NIL out unsupplied values registers, restore FP and return at Return-PC.
881 ;;; When there are stack values, we must initialize the argument pointer to
882 ;;; point to the beginning of the values block (which is the beginning of the
883 ;;; current frame.)
884 (define-vop (return)
885   (:args
886    (old-fp :scs (any-reg))
887    (return-pc :scs (descriptor-reg) :to (:eval 1))
888    (values :more t))
889   (:ignore values)
890   (:info nvals)
891   (:temporary (:sc descriptor-reg :offset a0-offset :from (:eval 0)) a0)
892   (:temporary (:sc descriptor-reg :offset a1-offset :from (:eval 0)) a1)
893   (:temporary (:sc descriptor-reg :offset a2-offset :from (:eval 0)) a2)
894   (:temporary (:sc descriptor-reg :offset a3-offset :from (:eval 0)) a3)
895   (:temporary (:sc descriptor-reg :offset a4-offset :from (:eval 0)) a4)
896   (:temporary (:sc descriptor-reg :offset a5-offset :from (:eval 0)) a5)
897   (:temporary (:sc any-reg :offset nargs-offset) nargs)
898   (:temporary (:sc any-reg :offset ocfp-offset) val-ptr)
899   (:vop-var vop)
900   (:generator 6
901     (trace-table-entry trace-table-fun-epilogue)
902     ;; Clear the number stack.
903     (let ((cur-nfp (current-nfp-tn vop)))
904       (when cur-nfp
905         (inst add nsp-tn cur-nfp
906               (- (bytes-needed-for-non-descriptor-stack-frame)
907                  number-stack-displacement))))
908     (cond ((= nvals 1)
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 return-pc :offset 2))
914           (t
915            ;; Establish the values pointer and values count.
916            (move val-ptr cfp-tn)
917            (inst li nargs (fixnumize nvals))
918            ;; restore the frame pointer and clear as much of the control
919            ;; stack as possible.
920            (move cfp-tn old-fp)
921            (inst add csp-tn val-ptr (* nvals n-word-bytes))
922            ;; pre-default any argument register that need it.
923            (when (< nvals register-arg-count)
924              (dolist (reg (subseq (list a0 a1 a2 a3 a4 a5) nvals))
925                (move reg null-tn)))
926            ;; And away we go.
927            (lisp-return return-pc)))
928     (trace-table-entry trace-table-normal)))
929
930 ;;; Do unknown-values return of an arbitrary number of values (passed on the
931 ;;; stack.)  We check for the common case of a single return value, and do that
932 ;;; inline using the normal single value return convention.  Otherwise, we
933 ;;; branch off to code that calls an assembly-routine.
934 (define-vop (return-multiple)
935   (:args
936    (old-fp-arg :scs (any-reg) :to (:eval 1))
937    (lra-arg :scs (descriptor-reg) :to (:eval 1))
938    (vals-arg :scs (any-reg) :target vals)
939    (nvals-arg :scs (any-reg) :target nvals))
940
941   (:temporary (:sc any-reg :offset nl1-offset :from (:argument 0)) old-fp)
942   (:temporary (:sc descriptor-reg :offset lra-offset :from (:argument 1)) lra)
943   (:temporary (:sc any-reg :offset nl0-offset :from (:argument 2)) vals)
944   (:temporary (:sc any-reg :offset nargs-offset :from (:argument 3)) nvals)
945   (:temporary (:sc descriptor-reg :offset a0-offset) a0)
946
947   (:temporary (:scs (any-reg) :from (:eval 1)) temp)
948
949   (:vop-var vop)
950
951   (:generator 13
952     (trace-table-entry trace-table-fun-epilogue)
953     (let ((not-single (gen-label)))
954       ;; Clear the number stack.
955       (let ((cur-nfp (current-nfp-tn vop)))
956         (when cur-nfp
957           (inst add nsp-tn cur-nfp
958                 (- (bytes-needed-for-non-descriptor-stack-frame)
959                    number-stack-displacement))))
960
961       ;; Check for the single case.
962       (inst cmp nvals-arg (fixnumize 1))
963       (inst b :ne not-single)
964       (inst ld a0 vals-arg)
965
966       ;; Return with one value.
967       (move csp-tn cfp-tn)
968       (move cfp-tn old-fp-arg)
969       (lisp-return lra-arg :offset 2)
970                 
971       ;; Nope, not the single case.
972       (emit-label not-single)
973       (move old-fp old-fp-arg)
974       (move lra lra-arg)
975       (move vals vals-arg)
976       (move nvals nvals-arg)
977       (inst ji temp (make-fixup 'return-multiple :assembly-routine))
978       (inst nop))
979     (trace-table-entry trace-table-normal)))
980
981
982 \f
983 ;;;; XEP hackery:
984
985
986 ;;; We don't need to do anything special for regular functions.
987 (define-vop (setup-environment)
988   (:info label)
989   (:ignore label)
990   (:generator 0
991     ;; Don't bother doing anything.
992     ))
993
994 ;;; Get the lexical environment from it's passing location.
995 (define-vop (setup-closure-environment)
996   (:temporary (:sc descriptor-reg :offset lexenv-offset :target closure
997                :to (:result 0))
998               lexenv)
999   (:results (closure :scs (descriptor-reg)))
1000   (:info label)
1001   (:ignore label)
1002   (:generator 6
1003     ;; Get result.
1004     (move closure lexenv)))
1005
1006 ;;; Copy a more arg from the argument area to the end of the current frame.
1007 ;;; Fixed is the number of non-more arguments. 
1008 (define-vop (copy-more-arg)
1009   (:temporary (:sc any-reg :offset nl0-offset) result)
1010   (:temporary (:sc any-reg :offset nl1-offset) count)
1011   (:temporary (:sc any-reg :offset nl2-offset) src)
1012   (:temporary (:sc any-reg :offset nl3-offset) dst)
1013   (:temporary (:sc descriptor-reg :offset l0-offset) temp)
1014   (:info fixed)
1015   (:generator 20
1016     (let ((loop (gen-label))
1017           (do-regs (gen-label))
1018           (done (gen-label)))
1019       (when (< fixed register-arg-count)
1020         ;; Save a pointer to the results so we can fill in register args.
1021         ;; We don't need this if there are more fixed args than reg args.
1022         (move result csp-tn))
1023       ;; Allocate the space on the stack.
1024       (cond ((zerop fixed)
1025              (inst cmp nargs-tn)
1026              (inst b :eq done)
1027              (inst add csp-tn csp-tn nargs-tn))
1028             (t
1029              (inst subcc count nargs-tn (fixnumize fixed))
1030              (inst b :le done)
1031              (inst nop)
1032              (inst add csp-tn csp-tn count)))
1033       (when (< fixed register-arg-count)
1034         ;; We must stop when we run out of stack args, not when we run out of
1035         ;; more args.
1036         (inst subcc count nargs-tn (fixnumize register-arg-count))
1037         ;; Everything of interest in registers.
1038         (inst b :le do-regs))
1039       ;; Initialize dst to be end of stack.
1040       (move dst csp-tn)
1041       ;; Initialize src to be end of args.
1042       (inst add src cfp-tn nargs-tn)
1043
1044       (emit-label loop)
1045       ;; *--dst = *--src, --count
1046       (inst add src src (- n-word-bytes))
1047       (inst subcc count count (fixnumize 1))
1048       (loadw temp src)
1049       (inst add dst dst (- n-word-bytes))
1050       (inst b :gt loop)
1051       (storew temp dst)
1052
1053       (emit-label do-regs)
1054       (when (< fixed register-arg-count)
1055         ;; Now we have to deposit any more args that showed up in registers.
1056         (inst subcc count nargs-tn (fixnumize fixed))
1057         (do ((i fixed (1+ i)))
1058             ((>= i register-arg-count))
1059           ;; Don't deposit any more than there are.
1060           (inst b :eq done)
1061           (inst subcc count (fixnumize 1))
1062           ;; Store it relative to the pointer saved at the start.
1063           (storew (nth i *register-arg-tns*) result (- i fixed))))
1064       (emit-label done))))
1065
1066
1067 ;;; More args are stored consequtively on the stack, starting immediately at
1068 ;;; the context pointer.  The context pointer is not typed, so the lowtag is 0.
1069 (define-vop (more-arg word-index-ref)
1070   (:variant 0 0)
1071   (:translate %more-arg))
1072
1073
1074 ;;; Turn more arg (context, count) into a list.
1075 (define-vop (listify-rest-args)
1076   (:args (context-arg :target context :scs (descriptor-reg))
1077          (count-arg :target count :scs (any-reg)))
1078   (:arg-types * tagged-num)
1079   (:temporary (:scs (any-reg) :from (:argument 0)) context)
1080   (:temporary (:scs (any-reg) :from (:argument 1)) count)
1081   (:temporary (:scs (descriptor-reg) :from :eval) temp)
1082   (:temporary (:scs (non-descriptor-reg) :from :eval) dst)
1083   (:results (result :scs (descriptor-reg)))
1084   (:translate %listify-rest-args)
1085   (:policy :safe)
1086   (:generator 20
1087     (move context context-arg)
1088     (move count count-arg)
1089     ;; Check to see if there are any arguments.
1090     (inst cmp count)
1091     (inst b :eq done)
1092     (move result null-tn)
1093
1094     ;; We need to do this atomically.
1095     (pseudo-atomic ()
1096       (assemble ()
1097         ;; Allocate a cons (2 words) for each item.
1098         (inst andn result alloc-tn lowtag-mask)
1099         (inst or result list-pointer-lowtag)
1100         (move dst result)
1101         (inst sll temp count 1)
1102         (inst b enter)
1103         (inst add alloc-tn temp)
1104
1105         ;; Compute the next cons and store it in the current one.
1106         LOOP
1107         (inst add dst dst (* 2 n-word-bytes))
1108         (storew dst dst -1 list-pointer-lowtag)
1109
1110         ;; Grab one value.
1111         ENTER
1112         (loadw temp context)
1113         (inst add context context n-word-bytes)
1114
1115         ;; Dec count, and if != zero, go back for more.
1116         (inst subcc count (fixnumize 1))
1117         (inst b :gt loop)
1118
1119         ;; Store the value into the car of the current cons (in the delay
1120         ;; slot).
1121         (storew temp dst 0 list-pointer-lowtag)
1122
1123         ;; NIL out the last cons.
1124         (storew null-tn dst 1 list-pointer-lowtag)))
1125     DONE))
1126
1127
1128 ;;; Return the location and size of the more arg glob created by Copy-More-Arg.
1129 ;;; Supplied is the total number of arguments supplied (originally passed in
1130 ;;; NARGS.)  Fixed is the number of non-rest arguments.
1131 ;;;
1132 ;;; We must duplicate some of the work done by Copy-More-Arg, since at that
1133 ;;; time the environment is in a pretty brain-damaged state, preventing this
1134 ;;; info from being returned as values.  What we do is compute
1135 ;;; supplied - fixed, and return a pointer that many words below the current
1136 ;;; stack top.
1137 (define-vop (more-arg-context)
1138   (:policy :fast-safe)
1139   (:translate sb!c::%more-arg-context)
1140   (:args (supplied :scs (any-reg)))
1141   (:arg-types tagged-num (:constant fixnum))
1142   (:info fixed)
1143   (:results (context :scs (descriptor-reg))
1144             (count :scs (any-reg)))
1145   (:result-types t tagged-num)
1146   (:note "more-arg-context")
1147   (:generator 5
1148     (inst sub count supplied (fixnumize fixed))
1149     (inst sub context csp-tn count)))
1150
1151
1152 ;;; Signal wrong argument count error if Nargs isn't = to Count.
1153 ;;;
1154 (define-vop (verify-arg-count)
1155   (:policy :fast-safe)
1156   (:translate sb!c::%verify-arg-count)
1157   (:args (nargs :scs (any-reg)))
1158   (:arg-types positive-fixnum (:constant t))
1159   (:info count)
1160   (:vop-var vop)
1161   (:save-p :compute-only)
1162   (:generator 3
1163     (let ((err-lab
1164            (generate-error-code vop invalid-arg-count-error nargs)))
1165       (inst cmp nargs (fixnumize count))
1166       ;; Assume we don't take the branch
1167       (inst b :ne err-lab #!+sparc-v9 :pn)
1168       (inst nop))))
1169
1170 ;;; Signal various errors.
1171 (macrolet ((frob (name error translate &rest args)
1172              `(define-vop (,name)
1173                 ,@(when translate
1174                     `((:policy :fast-safe)
1175                       (:translate ,translate)))
1176                 (:args ,@(mapcar #'(lambda (arg)
1177                                      `(,arg :scs (any-reg descriptor-reg)))
1178                                  args))
1179                 (:vop-var vop)
1180                 (:save-p :compute-only)
1181                 (:generator 1000
1182                   (error-call vop ,error ,@args)))))
1183   (frob arg-count-error invalid-arg-count-error
1184     sb!c::%arg-count-error nargs)
1185   (frob type-check-error object-not-type-error sb!c::%type-check-error
1186     object type)
1187   (frob layout-invalid-error layout-invalid-error sb!c::%layout-invalid-error
1188     object layout)
1189   (frob odd-key-args-error odd-key-args-error
1190     sb!c::%odd-key-args-error)
1191   (frob unknown-key-arg-error unknown-key-arg-error
1192     sb!c::%unknown-key-arg-error key)
1193   (frob nil-fun-returned-error nil-fun-returned-error nil fun))