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