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