d2ab004094dd9ff907b473dfd9274499b8d44fd1
[sbcl.git] / src / compiler / x86 / call.lisp
1 ;;;; function call for the x86 VM
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* descriptor-reg-sc-number
22                      (nth n *register-arg-offsets*))
23       (make-wired-tn *backend-t-primitive-type* control-stack-sc-number n)))
24
25 ;;; Make a passing location TN for a local call return PC.
26 ;;;
27 ;;; Always wire the return PC location to the stack in its standard
28 ;;; location.
29 (!def-vm-support-routine make-return-pc-passing-location (standard)
30   (declare (ignore standard))
31   (make-wired-tn (primitive-type-or-lose 'system-area-pointer)
32                  sap-stack-sc-number return-pc-save-offset))
33
34 ;;; This is similar to MAKE-RETURN-PC-PASSING-LOCATION, but makes a
35 ;;; location to pass OLD-FP in.
36 ;;;
37 ;;; This is wired in both the standard and the local-call conventions,
38 ;;; because we want to be able to assume it's always there. Besides,
39 ;;; the x86 doesn't have enough registers to really make it profitable
40 ;;; to pass it in a register.
41 (!def-vm-support-routine make-old-fp-passing-location (standard)
42   (declare (ignore standard))
43   (make-wired-tn *fixnum-primitive-type* control-stack-sc-number
44                  ocfp-save-offset))
45
46 ;;; Make the TNs used to hold OLD-FP and RETURN-PC within the current
47 ;;; function. We treat these specially so that the debugger can find
48 ;;; them at a known location.
49 ;;;
50 ;;; Without using a save-tn - which does not make much sense if it is
51 ;;; wired to the stack?
52 (!def-vm-support-routine make-old-fp-save-location (physenv)
53   (physenv-debug-live-tn (make-wired-tn *fixnum-primitive-type*
54                                         control-stack-sc-number
55                                         ocfp-save-offset)
56                          physenv))
57 (!def-vm-support-routine make-return-pc-save-location (physenv)
58   (physenv-debug-live-tn
59    (make-wired-tn (primitive-type-or-lose 'system-area-pointer)
60                   sap-stack-sc-number return-pc-save-offset)
61    physenv))
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* any-reg-sc-number ecx-offset))
68
69 ;;; Make a TN to hold the number-stack frame pointer. This is allocated
70 ;;; once per component, and is component-live.
71 (!def-vm-support-routine make-nfp-tn ()
72   (make-restricted-tn *fixnum-primitive-type* ignore-me-sc-number))
73
74 (!def-vm-support-routine make-stack-pointer-tn ()
75   (make-normal-tn *fixnum-primitive-type*))
76
77 (!def-vm-support-routine make-number-stack-pointer-tn ()
78   (make-restricted-tn *fixnum-primitive-type* ignore-me-sc-number))
79
80 ;;; Return a list of TNs that can be used to represent an unknown-values
81 ;;; continuation within a function.
82 (!def-vm-support-routine make-unknown-values-locations ()
83   (list (make-stack-pointer-tn)
84         (make-normal-tn *fixnum-primitive-type*)))
85
86 ;;; This function is called by the ENTRY-ANALYZE phase, allowing
87 ;;; VM-dependent initialization of the IR2-COMPONENT structure. We
88 ;;; push placeholder entries in the CONSTANTS to leave room for
89 ;;; additional noise in the code object header.
90 (!def-vm-support-routine select-component-format (component)
91   (declare (type component component))
92   ;; The 1+ here is because for the x86 the first constant is a
93   ;; pointer to a list of fixups, or NIL if the code object has none.
94   ;; (If I understand correctly, the fixups are needed at GC copy
95   ;; time because the X86 code isn't relocatable.)
96   ;;
97   ;; KLUDGE: It'd be cleaner to have the fixups entry be a named
98   ;; element of the CODE (aka component) primitive object. However,
99   ;; it's currently a large, tricky, error-prone chore to change
100   ;; the layout of any primitive object, so for the foreseeable future
101   ;; we'll just live with this ugliness. -- WHN 2002-01-02
102   (dotimes (i (1+ code-constants-offset))
103     (vector-push-extend nil
104                         (ir2-component-constants (component-info component))))
105   (values))
106 \f
107 ;;;; frame hackery
108
109 ;;; This is used for setting up the Old-FP in local call.
110 (define-vop (current-fp)
111   (:results (val :scs (any-reg control-stack)))
112   (:generator 1
113     (move val ebp-tn)))
114
115 ;;; We don't have a separate NFP, so we don't need to do anything here.
116 (define-vop (compute-old-nfp)
117   (:results (val))
118   (:ignore val)
119   (:generator 1
120     nil))
121
122 (define-vop (xep-allocate-frame)
123   (:info start-lab copy-more-arg-follows)
124   (:vop-var vop)
125   (:generator 1
126     (align n-lowtag-bits)
127     (trace-table-entry trace-table-fun-prologue)
128     (emit-label start-lab)
129     ;; Skip space for the function header.
130     (inst simple-fun-header-word)
131     (dotimes (i (1- simple-fun-code-offset))
132       (inst dword 0))
133
134     ;; The start of the actual code.
135     ;; Save the return-pc.
136     (popw ebp-tn (- (1+ return-pc-save-offset)))
137
138     ;; If copy-more-arg follows it will allocate the correct stack
139     ;; size. The stack is not allocated first here as this may expose
140     ;; args on the stack if they take up more space than the frame!
141     (unless copy-more-arg-follows
142       ;; The args fit within the frame so just allocate the frame.
143       (inst lea esp-tn
144             (make-ea :dword :base ebp-tn
145                      :disp (- (* n-word-bytes
146                                  (max 3 (sb-allocated-size 'stack)))))))
147
148     (trace-table-entry trace-table-normal)))
149
150 ;;; This is emitted directly before either a known-call-local, call-local,
151 ;;; or a multiple-call-local. All it does is allocate stack space for the
152 ;;; callee (who has the same size stack as us).
153 (define-vop (allocate-frame)
154   (:results (res :scs (any-reg control-stack))
155             (nfp))
156   (:info callee)
157   (:ignore nfp callee)
158   (:generator 2
159     (move res esp-tn)
160     (inst sub esp-tn (* n-word-bytes (sb-allocated-size 'stack)))))
161
162 ;;; Allocate a partial frame for passing stack arguments in a full
163 ;;; call. NARGS is the number of arguments passed. We allocate at
164 ;;; least 3 slots, because the XEP noise is going to want to use them
165 ;;; before it can extend the stack.
166 (define-vop (allocate-full-call-frame)
167   (:info nargs)
168   (:results (res :scs (any-reg control-stack)))
169   (:generator 2
170     (move res esp-tn)
171     (inst sub esp-tn (* (max nargs 3) n-word-bytes))))
172 \f
173 ;;; Emit code needed at the return-point from an unknown-values call
174 ;;; for a fixed number of values. Values is the head of the TN-REF
175 ;;; list for the locations that the values are to be received into.
176 ;;; Nvals is the number of values that are to be received (should
177 ;;; 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,
182 ;;; a single value return returns at the return PC + 2, whereas a
183 ;;; return of other 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
186 ;;; reset the SP (which will only be executed when other than 1 value
187 ;;; is returned.)
188 ;;;
189 ;;; In the general case we have to do three things:
190 ;;;  -- Default unsupplied register values. This need only be done
191 ;;;     when a single value is returned, since register values are
192 ;;;     defaulted by the called in the non-single case.
193 ;;;  -- Default unsupplied stack values. This needs to be done whenever
194 ;;;     there are stack values.
195 ;;;  -- Reset SP. This must be done whenever other than 1 value is
196 ;;;     returned, regardless of the number of values desired.
197 (defun default-unknown-values (vop values nvals)
198   (declare (type (or tn-ref null) values)
199            (type unsigned-byte nvals))
200   (cond
201    ((<= nvals 1)
202     (note-this-location vop :single-value-return)
203     (inst mov esp-tn ebx-tn))
204    ((<= nvals register-arg-count)
205     (let ((regs-defaulted (gen-label)))
206       (note-this-location vop :unknown-return)
207       (inst jmp-short regs-defaulted)
208       ;; Default the unsuppled registers.
209       (let* ((2nd-tn-ref (tn-ref-across values))
210              (2nd-tn (tn-ref-tn 2nd-tn-ref)))
211         (inst mov 2nd-tn nil-value)
212         (when (> nvals 2)
213           (loop
214             for tn-ref = (tn-ref-across 2nd-tn-ref)
215             then (tn-ref-across tn-ref)
216             for count from 2 below register-arg-count
217             do (inst mov (tn-ref-tn tn-ref) 2nd-tn))))
218       (inst mov ebx-tn esp-tn)
219       (emit-label regs-defaulted)
220       (inst mov esp-tn ebx-tn)))
221    ((<= nvals 7)
222     ;; The number of bytes depends on the relative jump instructions.
223     ;; Best case is 31+(n-3)*14, worst case is 35+(n-3)*18. For
224     ;; NVALS=6 that is 73/89 bytes, and for NVALS=7 that is 87/107
225     ;; bytes which is likely better than using the blt below.
226     (let ((regs-defaulted (gen-label))
227           (defaulting-done (gen-label))
228           (default-stack-slots (gen-label)))
229       (note-this-location vop :unknown-return)
230       ;; Branch off to the MV case.
231       (inst jmp-short regs-defaulted)
232       ;; Do the single value case.
233       ;; Default the register args
234       (inst mov eax-tn nil-value)
235       (do ((i 1 (1+ i))
236            (val (tn-ref-across values) (tn-ref-across val)))
237           ((= i (min nvals register-arg-count)))
238         (inst mov (tn-ref-tn val) eax-tn))
239
240       ;; Fake other registers so it looks like we returned with all the
241       ;; registers filled in.
242       (move ebx-tn esp-tn)
243       (inst push edx-tn)
244       (inst jmp default-stack-slots)
245
246       (emit-label regs-defaulted)
247
248       (inst mov eax-tn nil-value)
249       (storew edx-tn ebx-tn -1)
250       (collect ((defaults))
251         (do ((i register-arg-count (1+ i))
252              (val (do ((i 0 (1+ i))
253                        (val values (tn-ref-across val)))
254                       ((= i register-arg-count) val))
255                   (tn-ref-across val)))
256             ((null val))
257           (let ((default-lab (gen-label))
258                 (tn (tn-ref-tn val)))
259             (defaults (cons default-lab tn))
260
261             (inst cmp ecx-tn (fixnumize i))
262             (inst jmp :be default-lab)
263             (loadw edx-tn ebx-tn (- (1+ i)))
264             (inst mov tn edx-tn)))
265
266         (emit-label defaulting-done)
267         (loadw edx-tn ebx-tn -1)
268         (move esp-tn ebx-tn)
269
270         (let ((defaults (defaults)))
271           (when defaults
272             (assemble (*elsewhere*)
273               (trace-table-entry trace-table-fun-prologue)
274               (emit-label default-stack-slots)
275               (dolist (default defaults)
276                 (emit-label (car default))
277                 (inst mov (cdr default) eax-tn))
278               (inst jmp defaulting-done)
279               (trace-table-entry trace-table-normal)))))))
280    (t
281     ;; 91 bytes for this branch.
282     (let ((regs-defaulted (gen-label))
283           (restore-edi (gen-label))
284           (no-stack-args (gen-label))
285           (default-stack-vals (gen-label))
286           (count-okay (gen-label)))
287       (note-this-location vop :unknown-return)
288       ;; Branch off to the MV case.
289       (inst jmp-short regs-defaulted)
290
291       ;; Default the register args, and set up the stack as if we
292       ;; entered the MV return point.
293       (inst mov ebx-tn esp-tn)
294       (inst push edx-tn)
295       (inst mov edi-tn nil-value)
296       (inst push edi-tn)
297       (inst mov esi-tn edi-tn)
298       ;; Compute a pointer to where to put the [defaulted] stack values.
299       (emit-label no-stack-args)
300       (inst lea edi-tn
301             (make-ea :dword :base ebp-tn
302                      :disp (* (- (1+ register-arg-count)) n-word-bytes)))
303       ;; Load EAX with NIL so we can quickly store it, and set up
304       ;; stuff for the loop.
305       (inst mov eax-tn nil-value)
306       (inst std)
307       (inst mov ecx-tn (- nvals register-arg-count))
308       ;; solaris requires DF being zero.
309       #!+sunos (inst cld)
310       ;; Jump into the default loop.
311       (inst jmp default-stack-vals)
312
313       ;; The regs are defaulted. We need to copy any stack arguments,
314       ;; and then default the remaining stack arguments.
315       (emit-label regs-defaulted)
316       ;; Save EDI.
317       (storew edi-tn ebx-tn (- (1+ 1)))
318       ;; Compute the number of stack arguments, and if it's zero or
319       ;; less, don't copy any stack arguments.
320       (inst sub ecx-tn (fixnumize register-arg-count))
321       (inst jmp :le no-stack-args)
322
323       ;; Throw away any unwanted args.
324       (inst cmp ecx-tn (fixnumize (- nvals register-arg-count)))
325       (inst jmp :be count-okay)
326       (inst mov ecx-tn (fixnumize (- nvals register-arg-count)))
327       (emit-label count-okay)
328       ;; Save the number of stack values.
329       (inst mov eax-tn ecx-tn)
330       ;; Compute a pointer to where the stack args go.
331       (inst lea edi-tn
332             (make-ea :dword :base ebp-tn
333                      :disp (* (- (1+ register-arg-count)) n-word-bytes)))
334       ;; Save ESI, and compute a pointer to where the args come from.
335       (storew esi-tn ebx-tn (- (1+ 2)))
336       (inst lea esi-tn
337             (make-ea :dword :base ebx-tn
338                      :disp (* (- (1+ register-arg-count)) n-word-bytes)))
339       ;; Do the copy.
340       (inst shr ecx-tn word-shift)              ; make word count
341       (inst std)
342       (inst rep)
343       (inst movs :dword)
344       ;; solaris requires DF being zero.
345       #!+sunos (inst cld)
346       ;; Restore ESI.
347       (loadw esi-tn ebx-tn (- (1+ 2)))
348       ;; Now we have to default the remaining args. Find out how many.
349       (inst sub eax-tn (fixnumize (- nvals register-arg-count)))
350       (inst neg eax-tn)
351       ;; If none, then just blow out of here.
352       (inst jmp :le restore-edi)
353       (inst mov ecx-tn eax-tn)
354       (inst shr ecx-tn word-shift)      ; word count
355       ;; Load EAX with NIL for fast storing.
356       (inst mov eax-tn nil-value)
357       ;; Do the store.
358       (emit-label default-stack-vals)
359       (inst rep)
360       (inst stos eax-tn)
361       ;; solaris requires DF being zero.
362       #!+sunos (inst cld)
363       ;; Restore EDI, and reset the stack.
364       (emit-label restore-edi)
365       (loadw edi-tn ebx-tn (- (1+ 1)))
366       (inst mov esp-tn ebx-tn))))
367   (values))
368 \f
369 ;;;; unknown values receiving
370
371 ;;; Emit code needed at the return point for an unknown-values call
372 ;;; for an arbitrary number of values.
373 ;;;
374 ;;; We do the single and non-single cases with no shared code: there
375 ;;; doesn't seem to be any potential overlap, and receiving a single
376 ;;; value is more important efficiency-wise.
377 ;;;
378 ;;; When there is a single value, we just push it on the stack,
379 ;;; returning the old SP and 1.
380 ;;;
381 ;;; When there is a variable number of values, we move all of the
382 ;;; argument registers onto the stack, and return ARGS and NARGS.
383 ;;;
384 ;;; ARGS and NARGS are TNs wired to the named locations. We must
385 ;;; explicitly allocate these TNs, since their lifetimes overlap with
386 ;;; the results start and count. (Also, it's nice to be able to target
387 ;;; them.)
388 (defun receive-unknown-values (args nargs start count)
389   (declare (type tn args nargs start count))
390   (let ((variable-values (gen-label))
391         (done (gen-label)))
392     (inst jmp-short variable-values)
393
394     (cond ((location= start (first *register-arg-tns*))
395            (inst push (first *register-arg-tns*))
396            (inst lea start (make-ea :dword :base esp-tn :disp 4)))
397           (t (inst mov start esp-tn)
398              (inst push (first *register-arg-tns*))))
399     (inst mov count (fixnumize 1))
400     (inst jmp done)
401
402     (emit-label variable-values)
403     ;; dtc: this writes the registers onto the stack even if they are
404     ;; not needed, only the number specified in ecx are used and have
405     ;; stack allocated to them. No harm is done.
406     (loop
407       for arg in *register-arg-tns*
408       for i downfrom -1
409       do (storew arg args i))
410     (move start args)
411     (move count nargs)
412
413     (emit-label done))
414   (values))
415
416 ;;; VOP that can be inherited by unknown values receivers. The main thing this
417 ;;; handles is allocation of the result temporaries.
418 (define-vop (unknown-values-receiver)
419   (:temporary (:sc descriptor-reg :offset ebx-offset
420                    :from :eval :to (:result 0))
421               values-start)
422   (:temporary (:sc any-reg :offset ecx-offset
423                :from :eval :to (:result 1))
424               nvals)
425   (:results (start :scs (any-reg control-stack))
426             (count :scs (any-reg control-stack))))
427 \f
428 ;;;; local call with unknown values convention return
429
430 ;;; Non-TR local call for a fixed number of values passed according to
431 ;;; the unknown values convention.
432 ;;;
433 ;;; FP is the frame pointer in install before doing the call.
434 ;;;
435 ;;; NFP would be the number-stack frame pointer if we had a separate
436 ;;; number stack.
437 ;;;
438 ;;; Args are the argument passing locations, which are specified only
439 ;;; to terminate their lifetimes in the caller.
440 ;;;
441 ;;; VALUES are the return value locations (wired to the standard
442 ;;; passing locations). NVALS is the number of values received.
443 ;;;
444 ;;; Save is the save info, which we can ignore since saving has been
445 ;;; done.
446 ;;;
447 ;;; TARGET is a continuation pointing to the start of the called
448 ;;; function.
449 (define-vop (call-local)
450   (:args (fp)
451          (nfp)
452          (args :more t))
453   (:results (values :more t))
454   (:save-p t)
455   (:move-args :local-call)
456   (:info arg-locs callee target nvals)
457   (:vop-var vop)
458   (:ignore nfp arg-locs args #+nil callee)
459   (:generator 5
460     (trace-table-entry trace-table-call-site)
461     (move ebp-tn fp)
462
463     (let ((ret-tn (callee-return-pc-tn callee)))
464       #+nil
465       (format t "*call-local ~S; tn-kind ~S; tn-save-tn ~S; its tn-kind ~S~%"
466               ret-tn (sb!c::tn-kind ret-tn) (sb!c::tn-save-tn ret-tn)
467               (sb!c::tn-kind (sb!c::tn-save-tn ret-tn)))
468
469       ;; Is the return-pc on the stack or in a register?
470       (sc-case ret-tn
471         ((sap-stack)
472          #+nil (format t "*call-local: ret-tn on stack; offset=~S~%"
473                        (tn-offset ret-tn))
474          (storew (make-fixup nil :code-object return)
475                  ebp-tn (- (1+ (tn-offset ret-tn)))))
476         ((sap-reg)
477          (inst lea ret-tn (make-fixup nil :code-object return)))))
478
479     (note-this-location vop :call-site)
480     (inst jmp target)
481     RETURN
482     (default-unknown-values vop values nvals)
483     (trace-table-entry trace-table-normal)))
484
485 ;;; Non-TR local call for a variable number of return values passed according
486 ;;; to the unknown values convention. The results are the start of the values
487 ;;; glob and the number of values received.
488 (define-vop (multiple-call-local unknown-values-receiver)
489   (:args (fp)
490          (nfp)
491          (args :more t))
492   (:save-p t)
493   (:move-args :local-call)
494   (:info save callee target)
495   (:ignore args save nfp #+nil callee)
496   (:vop-var vop)
497   (:generator 20
498     (trace-table-entry trace-table-call-site)
499     (move ebp-tn fp)
500
501     (let ((ret-tn (callee-return-pc-tn callee)))
502       #+nil
503       (format t "*multiple-call-local ~S; tn-kind ~S; tn-save-tn ~S; its tn-kind ~S~%"
504               ret-tn (sb!c::tn-kind ret-tn) (sb!c::tn-save-tn ret-tn)
505               (sb!c::tn-kind (sb!c::tn-save-tn ret-tn)))
506
507       ;; Is the return-pc on the stack or in a register?
508       (sc-case ret-tn
509         ((sap-stack)
510          #+nil (format t "*multiple-call-local: ret-tn on stack; offset=~S~%"
511                        (tn-offset ret-tn))
512          ;; Stack
513          (storew (make-fixup nil :code-object return)
514                  ebp-tn (- (1+ (tn-offset ret-tn)))))
515         ((sap-reg)
516          ;; Register
517          (inst lea ret-tn (make-fixup nil :code-object return)))))
518
519     (note-this-location vop :call-site)
520     (inst jmp target)
521     RETURN
522     (note-this-location vop :unknown-return)
523     (receive-unknown-values values-start nvals start count)
524     (trace-table-entry trace-table-normal)))
525 \f
526 ;;;; local call with known values return
527
528 ;;; Non-TR local call with known return locations. Known-value return
529 ;;; works just like argument passing in local call.
530 ;;;
531 ;;; Note: we can't use normal load-tn allocation for the fixed args,
532 ;;; since all registers may be tied up by the more operand. Instead,
533 ;;; we use MAYBE-LOAD-STACK-TN.
534 (define-vop (known-call-local)
535   (:args (fp)
536          (nfp)
537          (args :more t))
538   (:results (res :more t))
539   (:move-args :local-call)
540   (:save-p t)
541   (:info save callee target)
542   (:ignore args res save nfp #+nil callee)
543   (:vop-var vop)
544   (:generator 5
545     (trace-table-entry trace-table-call-site)
546     (move ebp-tn fp)
547
548     (let ((ret-tn (callee-return-pc-tn callee)))
549
550       #+nil
551       (format t "*known-call-local ~S; tn-kind ~S; tn-save-tn ~S; its tn-kind ~S~%"
552               ret-tn (sb!c::tn-kind ret-tn) (sb!c::tn-save-tn ret-tn)
553               (sb!c::tn-kind (sb!c::tn-save-tn ret-tn)))
554
555       ;; Is the return-pc on the stack or in a register?
556       (sc-case ret-tn
557         ((sap-stack)
558          #+nil (format t "*known-call-local: ret-tn on stack; offset=~S~%"
559                        (tn-offset ret-tn))
560          ;; Stack
561          (storew (make-fixup nil :code-object return)
562                  ebp-tn (- (1+ (tn-offset ret-tn)))))
563         ((sap-reg)
564          ;; Register
565          (inst lea ret-tn (make-fixup nil :code-object return)))))
566
567     (note-this-location vop :call-site)
568     (inst jmp target)
569     RETURN
570     (note-this-location vop :known-return)
571     (trace-table-entry trace-table-normal)))
572 \f
573 ;;; Return from known values call. We receive the return locations as
574 ;;; arguments to terminate their lifetimes in the returning function. We
575 ;;; restore FP and CSP and jump to the Return-PC.
576 ;;;
577 ;;; We can assume we know exactly where old-fp and return-pc are because
578 ;;; make-old-fp-save-location and make-return-pc-save-location always
579 ;;; return the same place.
580 #+nil
581 (define-vop (known-return)
582   (:args (old-fp)
583          (return-pc :scs (any-reg immediate-stack) :target rpc)
584          (vals :more t))
585   (:move-args :known-return)
586   (:info val-locs)
587   (:temporary (:sc unsigned-reg :from (:argument 1)) rpc)
588   (:ignore val-locs vals)
589   (:vop-var vop)
590   (:generator 6
591     (trace-table-entry trace-table-fun-epilogue)
592     ;; Save the return-pc in a register 'cause the frame-pointer is
593     ;; going away. Note this not in the usual stack location so we
594     ;; can't use RET
595     (move rpc return-pc)
596     ;; Restore the stack.
597     (move esp-tn ebp-tn)
598     ;; Restore the old fp. We know OLD-FP is going to be in its stack
599     ;; save slot, which is a different frame that than this one,
600     ;; so we don't have to worry about having just cleared
601     ;; most of the stack.
602     (move ebp-tn old-fp)
603     (inst jmp rpc)
604     (trace-table-entry trace-table-normal)))
605 \f
606 ;;; From Douglas Crosher
607 ;;; Return from known values call. We receive the return locations as
608 ;;; arguments to terminate their lifetimes in the returning function. We
609 ;;; restore FP and CSP and jump to the Return-PC.
610 ;;;
611 ;;; The old-fp may be either in a register or on the stack in its
612 ;;; standard save locations - slot 0.
613 ;;;
614 ;;; The return-pc may be in a register or on the stack in any slot.
615 (define-vop (known-return)
616   (:args (old-fp)
617          (return-pc)
618          (vals :more t))
619   (:move-args :known-return)
620   (:info val-locs)
621   (:ignore val-locs vals)
622   (:vop-var vop)
623   (:generator 6
624     (trace-table-entry trace-table-fun-epilogue)
625
626     #+nil (format t "*known-return: old-fp ~S, tn-kind ~S; ~S ~S~%"
627                   old-fp (sb!c::tn-kind old-fp) (sb!c::tn-save-tn old-fp)
628                   (sb!c::tn-kind (sb!c::tn-save-tn old-fp)))
629
630     #+nil (format t "*known-return: return-pc ~S, tn-kind ~S; ~S ~S~%"
631                   return-pc (sb!c::tn-kind return-pc)
632                   (sb!c::tn-save-tn return-pc)
633                   (sb!c::tn-kind (sb!c::tn-save-tn return-pc)))
634
635     ;; return-pc may be either in a register or on the stack.
636     (sc-case return-pc
637       ((sap-reg)
638        (sc-case old-fp
639          ((control-stack)
640
641           #+nil (format t "*known-return: old-fp ~S on stack; offset=~S~%"
642                         old-fp (tn-offset old-fp))
643
644           (cond ((zerop (tn-offset old-fp))
645                  ;; Zot all of the stack except for the old-fp.
646                  (inst lea esp-tn (make-ea :dword :base ebp-tn
647                                            :disp (- (* (1+ ocfp-save-offset)
648                                                        n-word-bytes))))
649                  ;; Restore the old fp from its save location on the stack,
650                  ;; and zot the stack.
651                  (inst pop ebp-tn))
652
653                 (t
654                  (cerror "Continue anyway"
655                          "VOP return-local doesn't work if old-fp (in slot ~
656                           ~S) is not in slot 0"
657                          (tn-offset old-fp)))))
658
659          ((any-reg descriptor-reg)
660           ;; Zot all the stack.
661           (move esp-tn ebp-tn)
662           ;; Restore the old-fp.
663           (move ebp-tn old-fp)))
664
665        ;; Return; return-pc is in a register.
666        (inst jmp return-pc))
667
668       ((sap-stack)
669
670        #+nil (format t "*known-return: return-pc ~S on stack; offset=~S~%"
671                      return-pc (tn-offset return-pc))
672
673        ;; Zot all of the stack except for the old-fp and return-pc.
674        (inst lea esp-tn
675              (make-ea :dword :base ebp-tn
676                       :disp (- (* (1+ (tn-offset return-pc)) n-word-bytes))))
677        ;; Restore the old fp. old-fp may be either on the stack in its
678        ;; save location or in a register, in either case this restores it.
679        (move ebp-tn old-fp)
680        ;; The return pops the return address (4 bytes), then we need
681        ;; to pop all the slots before the return-pc which includes the
682        ;; 4 bytes for the old-fp.
683        (inst ret (* (tn-offset return-pc) n-word-bytes))))
684
685     (trace-table-entry trace-table-normal)))
686 \f
687 ;;;; full call
688 ;;;
689 ;;; There is something of a cross-product effect with full calls.
690 ;;; Different versions are used depending on whether we know the
691 ;;; number of arguments or the name of the called function, and
692 ;;; whether we want fixed values, unknown values, or a tail call.
693 ;;;
694 ;;; In full call, the arguments are passed creating a partial frame on
695 ;;; the stack top and storing stack arguments into that frame. On
696 ;;; entry to the callee, this partial frame is pointed to by FP.
697
698 ;;; This macro helps in the definition of full call VOPs by avoiding
699 ;;; code replication in defining the cross-product VOPs.
700 ;;;
701 ;;; NAME is the name of the VOP to define.
702 ;;;
703 ;;; NAMED is true if the first argument is an fdefinition object whose
704 ;;; definition is to be called.
705 ;;;
706 ;;; RETURN is either :FIXED, :UNKNOWN or :TAIL:
707 ;;; -- If :FIXED, then the call is for a fixed number of values, returned in
708 ;;;    the standard passing locations (passed as result operands).
709 ;;; -- If :UNKNOWN, then the result values are pushed on the stack, and the
710 ;;;    result values are specified by the Start and Count as in the
711 ;;;    unknown-values continuation representation.
712 ;;; -- If :TAIL, then do a tail-recursive call. No values are returned.
713 ;;;    The Old-Fp and Return-PC are passed as the second and third arguments.
714 ;;;
715 ;;; In non-tail calls, the pointer to the stack arguments is passed as
716 ;;; the last fixed argument. If Variable is false, then the passing
717 ;;; locations are passed as a more arg. Variable is true if there are
718 ;;; a variable number of arguments passed on the stack. Variable
719 ;;; cannot be specified with :TAIL return. TR variable argument call
720 ;;; is implemented separately.
721 ;;;
722 ;;; In tail call with fixed arguments, the passing locations are
723 ;;; passed as a more arg, but there is no new-FP, since the arguments
724 ;;; have been set up in the current frame.
725 (macrolet ((define-full-call (name named return variable)
726             (aver (not (and variable (eq return :tail))))
727             `(define-vop (,name
728                           ,@(when (eq return :unknown)
729                               '(unknown-values-receiver)))
730                (:args
731                ,@(unless (eq return :tail)
732                    '((new-fp :scs (any-reg) :to (:argument 1))))
733
734                (fun :scs (descriptor-reg control-stack)
735                     :target eax :to (:argument 0))
736
737                ,@(when (eq return :tail)
738                    '((old-fp)
739                      (return-pc)))
740
741                ,@(unless variable '((args :more t :scs (descriptor-reg)))))
742
743                ,@(when (eq return :fixed)
744                '((:results (values :more t))))
745
746                (:save-p ,(if (eq return :tail) :compute-only t))
747
748                ,@(unless (or (eq return :tail) variable)
749                '((:move-args :full-call)))
750
751                (:vop-var vop)
752                (:info
753                ,@(unless (or variable (eq return :tail)) '(arg-locs))
754                ,@(unless variable '(nargs))
755                ,@(when (eq return :fixed) '(nvals)))
756
757                (:ignore
758                ,@(unless (or variable (eq return :tail)) '(arg-locs))
759                ,@(unless variable '(args)))
760
761                ;; We pass either the fdefn object (for named call) or
762                ;; the actual function object (for unnamed call) in
763                ;; EAX. With named call, closure-tramp will replace it
764                ;; with the real function and invoke the real function
765                ;; for closures. Non-closures do not need this value,
766                ;; so don't care what shows up in it.
767                (:temporary
768                (:sc descriptor-reg
769                     :offset eax-offset
770                     :from (:argument 0)
771                     :to :eval)
772                eax)
773
774                ;; We pass the number of arguments in ECX.
775                (:temporary (:sc unsigned-reg :offset ecx-offset :to :eval) ecx)
776
777                ;; With variable call, we have to load the
778                ;; register-args out of the (new) stack frame before
779                ;; doing the call. Therefore, we have to tell the
780                ;; lifetime stuff that we need to use them.
781                ,@(when variable
782                    (mapcar (lambda (name offset)
783                              `(:temporary (:sc descriptor-reg
784                                                :offset ,offset
785                                                :from (:argument 0)
786                                                :to :eval)
787                                           ,name))
788                            *register-arg-names* *register-arg-offsets*))
789
790                ,@(when (eq return :tail)
791                    '((:temporary (:sc unsigned-reg
792                                       :from (:argument 1)
793                                       :to (:argument 2))
794                                  old-fp-tmp)))
795
796                (:generator ,(+ (if named 5 0)
797                                (if variable 19 1)
798                                (if (eq return :tail) 0 10)
799                                15
800                                (if (eq return :unknown) 25 0))
801                (trace-table-entry trace-table-call-site)
802
803                ;; This has to be done before the frame pointer is
804                ;; changed! EAX stores the 'lexical environment' needed
805                ;; for closures.
806                (move eax fun)
807
808
809                ,@(if variable
810                      ;; For variable call, compute the number of
811                      ;; arguments and move some of the arguments to
812                      ;; registers.
813                      (collect ((noise))
814                               ;; Compute the number of arguments.
815                               (noise '(inst mov ecx new-fp))
816                               (noise '(inst sub ecx esp-tn))
817                               ;; Move the necessary args to registers,
818                               ;; this moves them all even if they are
819                               ;; not all needed.
820                               (loop
821                                for name in *register-arg-names*
822                                for index downfrom -1
823                                do (noise `(loadw ,name new-fp ,index)))
824                               (noise))
825                    '((if (zerop nargs)
826                          (inst xor ecx ecx)
827                        (inst mov ecx (fixnumize nargs)))))
828                ,@(cond ((eq return :tail)
829                         '(;; Python has figured out what frame we should
830                           ;; return to so might as well use that clue.
831                           ;; This seems really important to the
832                           ;; implementation of things like
833                           ;; (without-interrupts ...)
834                           ;;
835                           ;; dtc; Could be doing a tail call from a
836                           ;; known-local-call etc in which the old-fp
837                           ;; or ret-pc are in regs or in non-standard
838                           ;; places. If the passing location were
839                           ;; wired to the stack in standard locations
840                           ;; then these moves will be un-necessary;
841                           ;; this is probably best for the x86.
842                           (sc-case old-fp
843                                    ((control-stack)
844                                     (unless (= ocfp-save-offset
845                                                (tn-offset old-fp))
846                                       ;; FIXME: FORMAT T for stale
847                                       ;; diagnostic output (several of
848                                       ;; them around here), ick
849                                       (format t "** tail-call old-fp not S0~%")
850                                       (move old-fp-tmp old-fp)
851                                       (storew old-fp-tmp
852                                               ebp-tn
853                                               (- (1+ ocfp-save-offset)))))
854                                    ((any-reg descriptor-reg)
855                                     (format t "** tail-call old-fp in reg not S0~%")
856                                     (storew old-fp
857                                             ebp-tn
858                                             (- (1+ ocfp-save-offset)))))
859
860                           ;; For tail call, we have to push the
861                           ;; return-pc so that it looks like we CALLed
862                           ;; despite the fact that we are going to JMP.
863                           (inst push return-pc)
864                           ))
865                        (t
866                         ;; For non-tail call, we have to save our
867                         ;; frame pointer and install the new frame
868                         ;; pointer. We can't load stack tns after this
869                         ;; point.
870                         `(;; Python doesn't seem to allocate a frame
871                           ;; here which doesn't leave room for the
872                           ;; ofp/ret stuff.
873
874                           ;; The variable args are on the stack and
875                           ;; become the frame, but there may be <3
876                           ;; args and 3 stack slots are assumed
877                           ;; allocate on the call. So need to ensure
878                           ;; there are at least 3 slots. This hack
879                           ;; just adds 3 more.
880                           ,(if variable
881                                '(inst sub esp-tn (fixnumize 3)))
882
883                           ;; Save the fp
884                           (storew ebp-tn new-fp (- (1+ ocfp-save-offset)))
885
886                           (move ebp-tn new-fp) ; NB - now on new stack frame.
887                           )))
888
889                (note-this-location vop :call-site)
890
891                (inst ,(if (eq return :tail) 'jmp 'call)
892                      (make-ea :dword :base eax
893                               :disp ,(if named
894                                          '(- (* fdefn-raw-addr-slot
895                                                 n-word-bytes)
896                                              other-pointer-lowtag)
897                                        '(- (* closure-fun-slot n-word-bytes)
898                                            fun-pointer-lowtag))))
899                ,@(ecase return
900                    (:fixed
901                     '((default-unknown-values vop values nvals)))
902                    (:unknown
903                     '((note-this-location vop :unknown-return)
904                       (receive-unknown-values values-start nvals start count)))
905                    (:tail))
906                (trace-table-entry trace-table-normal)))))
907
908   (define-full-call call nil :fixed nil)
909   (define-full-call call-named t :fixed nil)
910   (define-full-call multiple-call nil :unknown nil)
911   (define-full-call multiple-call-named t :unknown nil)
912   (define-full-call tail-call nil :tail nil)
913   (define-full-call tail-call-named t :tail nil)
914
915   (define-full-call call-variable nil :fixed t)
916   (define-full-call multiple-call-variable nil :unknown t))
917
918 ;;; This is defined separately, since it needs special code that BLT's
919 ;;; the arguments down. All the real work is done in the assembly
920 ;;; routine. We just set things up so that it can find what it needs.
921 (define-vop (tail-call-variable)
922   (:args (args :scs (any-reg control-stack) :target esi)
923          (function :scs (descriptor-reg control-stack) :target eax)
924          (old-fp)
925          (ret-addr))
926   (:temporary (:sc unsigned-reg :offset esi-offset :from (:argument 0)) esi)
927   (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 1)) eax)
928 ;  (:ignore ret-addr old-fp)
929   (:generator 75
930     ;; Move these into the passing locations if they are not already there.
931     (move esi args)
932     (move eax function)
933
934     ;; The following assumes that the return-pc and old-fp are on the
935     ;; stack in their standard save locations - Check this.
936     (unless (and (sc-is old-fp control-stack)
937                  (= (tn-offset old-fp) ocfp-save-offset))
938             (error "tail-call-variable: ocfp not on stack in standard save location?"))
939     (unless (and (sc-is ret-addr sap-stack)
940                  (= (tn-offset ret-addr) return-pc-save-offset))
941             (error "tail-call-variable: ret-addr not on stack in standard save location?"))
942
943
944     ;; And jump to the assembly routine.
945     (inst jmp (make-fixup 'tail-call-variable :assembly-routine))))
946 \f
947 ;;;; unknown values return
948
949 ;;; Return a single-value using the Unknown-Values convention. Specifically,
950 ;;; we jump to clear the stack and jump to return-pc+2.
951 ;;;
952 ;;; We require old-fp to be in a register, because we want to reset ESP before
953 ;;; restoring EBP. If old-fp were still on the stack, it could get clobbered
954 ;;; by a signal.
955 ;;;
956 ;;; pfw--get wired-tn conflicts sometimes if register sc specd for args
957 ;;; having problems targeting args to regs -- using temps instead.
958 (define-vop (return-single)
959   (:args (old-fp)
960          (return-pc)
961          (value))
962   (:temporary (:sc unsigned-reg) ofp)
963   (:temporary (:sc unsigned-reg) ret)
964   (:ignore value)
965   (:generator 6
966     (trace-table-entry trace-table-fun-epilogue)
967     (move ret return-pc)
968     ;; Clear the control stack
969     (move ofp old-fp)
970     ;; Adjust the return address for the single value return.
971     (inst add ret 2)
972     ;; Restore the frame pointer.
973     (move esp-tn ebp-tn)
974     (move ebp-tn ofp)
975     ;; Out of here.
976     (inst jmp ret)))
977
978 ;;; Do unknown-values return of a fixed (other than 1) number of
979 ;;; values. The VALUES are required to be set up in the standard
980 ;;; passing locations. NVALS is the number of values returned.
981 ;;;
982 ;;; Basically, we just load ECX with the number of values returned and
983 ;;; EBX with a pointer to the values, set ESP to point to the end of
984 ;;; the values, and jump directly to return-pc.
985 (define-vop (return)
986   (:args (old-fp)
987          (return-pc :to (:eval 1))
988          (values :more t))
989   (:ignore values)
990   (:info nvals)
991
992   ;; In the case of other than one value, we need these registers to
993   ;; tell the caller where they are and how many there are.
994   (:temporary (:sc unsigned-reg :offset ebx-offset) ebx)
995   (:temporary (:sc unsigned-reg :offset ecx-offset) ecx)
996
997   ;; We need to stretch the lifetime of return-pc past the argument
998   ;; registers so that we can default the argument registers without
999   ;; trashing return-pc.
1000   (:temporary (:sc unsigned-reg :offset (first *register-arg-offsets*)
1001                    :from :eval) a0)
1002   (:temporary (:sc unsigned-reg :offset (second *register-arg-offsets*)
1003                    :from :eval) a1)
1004   (:temporary (:sc unsigned-reg :offset (third *register-arg-offsets*)
1005                    :from :eval) a2)
1006
1007   (:generator 6
1008     (trace-table-entry trace-table-fun-epilogue)
1009     ;; Establish the values pointer and values count.
1010     (move ebx ebp-tn)
1011     (if (zerop nvals)
1012         (inst xor ecx ecx) ; smaller
1013       (inst mov ecx (fixnumize nvals)))
1014     ;; Restore the frame pointer.
1015     (move ebp-tn old-fp)
1016     ;; Clear as much of the stack as possible, but not past the return
1017     ;; address.
1018     (inst lea esp-tn (make-ea :dword :base ebx
1019                               :disp (- (* (max nvals 2) n-word-bytes))))
1020     ;; Pre-default any argument register that need it.
1021     (when (< nvals register-arg-count)
1022       (let* ((arg-tns (nthcdr nvals (list a0 a1 a2)))
1023              (first (first arg-tns)))
1024         (inst mov first nil-value)
1025         (dolist (tn (cdr arg-tns))
1026           (inst mov tn first))))
1027     ;; And away we go. Except that return-pc is still on the
1028     ;; stack and we've changed the stack pointer. So we have to
1029     ;; tell it to index off of EBX instead of EBP.
1030     (cond ((zerop nvals)
1031            ;; Return popping the return address and the OCFP.
1032            (inst ret n-word-bytes))
1033           ((= nvals 1)
1034            ;; Return popping the return, leaving 1 slot. Can this
1035            ;; happen, or is a single value return handled elsewhere?
1036            (inst ret))
1037           (t
1038            (inst jmp (make-ea :dword :base ebx
1039                               :disp (- (* (1+ (tn-offset return-pc))
1040                                           n-word-bytes))))))
1041
1042     (trace-table-entry trace-table-normal)))
1043
1044 ;;; Do unknown-values return of an arbitrary number of values (passed
1045 ;;; on the stack.) We check for the common case of a single return
1046 ;;; value, and do that inline using the normal single value return
1047 ;;; convention. Otherwise, we branch off to code that calls an
1048 ;;; assembly-routine.
1049 ;;;
1050 ;;; The assembly routine takes the following args:
1051 ;;;  EAX -- the return-pc to finally jump to.
1052 ;;;  EBX -- pointer to where to put the values.
1053 ;;;  ECX -- number of values to find there.
1054 ;;;  ESI -- pointer to where to find the values.
1055 (define-vop (return-multiple)
1056   (:args (old-fp :to (:eval 1) :target old-fp-temp)
1057          (return-pc :target eax)
1058          (vals :scs (any-reg) :target esi)
1059          (nvals :scs (any-reg) :target ecx))
1060
1061   (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 1)) eax)
1062   (:temporary (:sc unsigned-reg :offset esi-offset :from (:argument 2)) esi)
1063   (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 3)) ecx)
1064   (:temporary (:sc unsigned-reg :offset ebx-offset :from (:eval 0)) ebx)
1065   (:temporary (:sc descriptor-reg :offset (first *register-arg-offsets*)
1066                    :from (:eval 0)) a0)
1067   (:temporary (:sc unsigned-reg :from (:eval 1)) old-fp-temp)
1068   (:node-var node)
1069
1070   (:generator 13
1071     (trace-table-entry trace-table-fun-epilogue)
1072     ;; Load the return-pc.
1073     (move eax return-pc)
1074     (unless (policy node (> space speed))
1075       ;; Check for the single case.
1076       (let ((not-single (gen-label)))
1077         (inst cmp nvals (fixnumize 1))
1078         (inst jmp :ne not-single)
1079
1080         ;; Return with one value.
1081         (loadw a0 vals -1)
1082         ;; Clear the stack. We load old-fp into a register before clearing
1083         ;; the stack.
1084         (move old-fp-temp old-fp)
1085         (move esp-tn ebp-tn)
1086         (move ebp-tn old-fp-temp)
1087         ;; Fix the return-pc to point at the single-value entry point.
1088         (inst add eax 2)
1089         ;; Out of here.
1090         (inst jmp eax)
1091
1092         ;; Nope, not the single case. Jump to the assembly routine.
1093         (emit-label not-single)))
1094     (move esi vals)
1095     (move ecx nvals)
1096     (move ebx ebp-tn)
1097     (move ebp-tn old-fp)
1098     (inst jmp (make-fixup 'return-multiple :assembly-routine))
1099     (trace-table-entry trace-table-normal)))
1100 \f
1101 ;;;; XEP hackery
1102
1103 ;;; We don't need to do anything special for regular functions.
1104 (define-vop (setup-environment)
1105   (:info label)
1106   (:ignore label)
1107   (:generator 0
1108     ;; Don't bother doing anything.
1109     nil))
1110
1111 ;;; Get the lexical environment from its passing location.
1112 (define-vop (setup-closure-environment)
1113   (:results (closure :scs (descriptor-reg)))
1114   (:info label)
1115   (:ignore label)
1116   (:generator 6
1117     ;; Get result.
1118     (move closure eax-tn)))
1119
1120 ;;; Copy a &MORE arg from the argument area to the end of the current
1121 ;;; frame. FIXED is the number of non-&MORE arguments.
1122 ;;;
1123 ;;; The tricky part is doing this without trashing any of the calling
1124 ;;; convention registers that are still needed. This vop is emitted
1125 ;;; directly after the xep-allocate frame. That means the registers
1126 ;;; are in use as follows:
1127 ;;;
1128 ;;;  EAX -- The lexenv.
1129 ;;;  EBX -- Available.
1130 ;;;  ECX -- The total number of arguments.
1131 ;;;  EDX -- The first arg.
1132 ;;;  EDI -- The second arg.
1133 ;;;  ESI -- The third arg.
1134 ;;;
1135 ;;; So basically, we have one register available for our use: EBX.
1136 ;;;
1137 ;;; What we can do is push the other regs onto the stack, and then
1138 ;;; restore their values by looking directly below where we put the
1139 ;;; more-args.
1140 (define-vop (copy-more-arg)
1141   (:info fixed)
1142   (:generator 20
1143     ;; Avoid the copy if there are no more args.
1144     (cond ((zerop fixed)
1145            (inst jecxz just-alloc-frame))
1146           (t
1147            (inst cmp ecx-tn (fixnumize fixed))
1148            (inst jmp :be just-alloc-frame)))
1149
1150     ;; Allocate the space on the stack.
1151     ;; stack = ebp - (max 3 frame-size) - (nargs - fixed)
1152     (inst lea ebx-tn
1153           (make-ea :dword :base ebp-tn
1154                    :disp (- (fixnumize fixed)
1155                             (* n-word-bytes
1156                                (max 3 (sb-allocated-size 'stack))))))
1157     (inst sub ebx-tn ecx-tn)  ; Got the new stack in ebx
1158     (inst mov esp-tn ebx-tn)
1159
1160     ;; Now: nargs>=1 && nargs>fixed
1161
1162     ;; Save the original count of args.
1163     (inst mov ebx-tn ecx-tn)
1164
1165     (cond ((< fixed register-arg-count)
1166            ;; We must stop when we run out of stack args, not when we
1167            ;; run out of more args.
1168            ;; Number to copy = nargs-3
1169            (inst sub ecx-tn (fixnumize register-arg-count))
1170            ;; Everything of interest in registers.
1171            (inst jmp :be do-regs))
1172           (t
1173            ;; Number to copy = nargs-fixed
1174            (inst sub ecx-tn (fixnumize fixed))))
1175
1176     ;; Save edi and esi register args.
1177     (inst push edi-tn)
1178     (inst push esi-tn)
1179     ;; Okay, we have pushed the register args. We can trash them
1180     ;; now.
1181
1182     ;; Initialize dst to be end of stack; skiping the values pushed
1183     ;; above.
1184     (inst lea edi-tn (make-ea :dword :base esp-tn :disp 8))
1185
1186     ;; Initialize src to be end of args.
1187     (inst mov esi-tn ebp-tn)
1188     (inst sub esi-tn ebx-tn)
1189
1190     (inst shr ecx-tn word-shift)        ; make word count
1191     ;; And copy the args.
1192     (inst cld)                          ; auto-inc ESI and EDI.
1193     (inst rep)
1194     (inst movs :dword)
1195
1196     ;; So now we need to restore EDI and ESI.
1197     (inst pop esi-tn)
1198     (inst pop edi-tn)
1199
1200     DO-REGS
1201
1202     ;; Restore ECX
1203     (inst mov ecx-tn ebx-tn)
1204
1205     ;; Here: nargs>=1 && nargs>fixed
1206     (when (< fixed register-arg-count)
1207           ;; Now we have to deposit any more args that showed up in
1208           ;; registers.
1209           (do ((i fixed))
1210               ( nil )
1211               ;; Store it relative to ebp
1212               (inst mov (make-ea :dword :base ebp-tn
1213                                  :disp (- (* 4
1214                                              (+ 1 (- i fixed)
1215                                                 (max 3 (sb-allocated-size 'stack))))))
1216                     (nth i *register-arg-tns*))
1217
1218               (incf i)
1219               (when (>= i register-arg-count)
1220                     (return))
1221
1222               ;; Don't deposit any more than there are.
1223               (if (zerop i)
1224                   (inst test ecx-tn ecx-tn)
1225                 (inst cmp ecx-tn (fixnumize i)))
1226               (inst jmp :eq done)))
1227
1228     (inst jmp done)
1229
1230     JUST-ALLOC-FRAME
1231     (inst lea esp-tn
1232           (make-ea :dword :base ebp-tn
1233                    :disp (- (* n-word-bytes
1234                                (max 3 (sb-allocated-size 'stack))))))
1235
1236     DONE))
1237
1238 ;;; &MORE args are stored contiguously on the stack, starting
1239 ;;; immediately at the context pointer. The context pointer is not
1240 ;;; typed, so the lowtag is 0.
1241 (define-vop (more-arg)
1242   (:translate %more-arg)
1243   (:policy :fast-safe)
1244   (:args (object :scs (descriptor-reg) :to :result)
1245          (index :scs (any-reg) :target temp))
1246   (:arg-types * tagged-num)
1247   (:temporary (:sc unsigned-reg :from (:argument 1) :to :result) temp)
1248   (:results (value :scs (any-reg descriptor-reg)))
1249   (:result-types *)
1250   (:generator 5
1251     (move temp index)
1252     (inst neg temp)
1253     (inst mov value (make-ea :dword :base object :index temp))))
1254
1255 (define-vop (more-arg-c)
1256   (:translate %more-arg)
1257   (:policy :fast-safe)
1258   (:args (object :scs (descriptor-reg)))
1259   (:info index)
1260   (:arg-types * (:constant (signed-byte 30)))
1261   (:results (value :scs (any-reg descriptor-reg)))
1262   (:result-types *)
1263   (:generator 4
1264    (inst mov value
1265          (make-ea :dword :base object :disp (- (* index n-word-bytes))))))
1266
1267
1268 ;;; Turn more arg (context, count) into a list.
1269 (defoptimizer (%listify-rest-args stack-allocate-result) ((&rest args))
1270   t)
1271
1272 (define-vop (listify-rest-args)
1273   (:translate %listify-rest-args)
1274   (:policy :safe)
1275   (:args (context :scs (descriptor-reg) :target src)
1276          (count :scs (any-reg) :target ecx))
1277   (:arg-types * tagged-num)
1278   (:temporary (:sc unsigned-reg :offset esi-offset :from (:argument 0)) src)
1279   (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 1)) ecx)
1280   (:temporary (:sc unsigned-reg :offset eax-offset) eax)
1281   (:temporary (:sc unsigned-reg) dst)
1282   (:results (result :scs (descriptor-reg)))
1283   (:node-var node)
1284   (:generator 20
1285     (let ((enter (gen-label))
1286           (loop (gen-label))
1287           (done (gen-label))
1288           (stack-allocate-p (node-stack-allocate-p node)))
1289       (move src context)
1290       (move ecx count)
1291       ;; Check to see whether there are no args, and just return NIL if so.
1292       (inst mov result nil-value)
1293       (inst jecxz done)
1294       (inst lea dst (make-ea :dword :index ecx :scale 2))
1295       (maybe-pseudo-atomic stack-allocate-p
1296        (allocation dst dst node stack-allocate-p)
1297        (inst lea dst (make-ea :byte :base dst :disp list-pointer-lowtag))
1298        ;; Convert the count into a raw value, so that we can use the
1299        ;; LOOP instruction.
1300        (inst shr ecx 2)
1301        ;; Set decrement mode (successive args at lower addresses)
1302        (inst std)
1303        ;; Set up the result.
1304        (move result dst)
1305        ;; Jump into the middle of the loop, 'cause that's were we want
1306        ;; to start.
1307        (inst jmp enter)
1308        (emit-label loop)
1309        ;; Compute a pointer to the next cons.
1310        (inst add dst (* cons-size n-word-bytes))
1311        ;; Store a pointer to this cons in the CDR of the previous cons.
1312        (storew dst dst -1 list-pointer-lowtag)
1313        (emit-label enter)
1314        ;; Grab one value and stash it in the car of this cons.
1315        (inst lods eax)
1316        (storew eax dst 0 list-pointer-lowtag)
1317        ;; Go back for more.
1318        (inst loop loop)
1319        ;; NIL out the last cons.
1320        (storew nil-value dst 1 list-pointer-lowtag))
1321       (emit-label done)
1322       ;; solaris requires DF being zero.
1323       #!+sunos (inst cld))))
1324
1325 ;;; Return the location and size of the &MORE arg glob created by
1326 ;;; COPY-MORE-ARG. SUPPLIED is the total number of arguments supplied
1327 ;;; (originally passed in ECX). FIXED is the number of non-rest
1328 ;;; arguments.
1329 ;;;
1330 ;;; We must duplicate some of the work done by COPY-MORE-ARG, since at
1331 ;;; that time the environment is in a pretty brain-damaged state,
1332 ;;; preventing this info from being returned as values. What we do is
1333 ;;; compute supplied - fixed, and return a pointer that many words
1334 ;;; below the current stack top.
1335 (define-vop (more-arg-context)
1336   (:policy :fast-safe)
1337   (:translate sb!c::%more-arg-context)
1338   (:args (supplied :scs (any-reg) :target count))
1339   (:arg-types positive-fixnum (:constant fixnum))
1340   (:info fixed)
1341   (:results (context :scs (descriptor-reg))
1342             (count :scs (any-reg)))
1343   (:result-types t tagged-num)
1344   (:note "more-arg-context")
1345   (:generator 5
1346     (move count supplied)
1347     ;; SP at this point points at the last arg pushed.
1348     ;; Point to the first more-arg, not above it.
1349     (inst lea context (make-ea :dword :base esp-tn
1350                                :index count :scale 1
1351                                :disp (- (+ (fixnumize fixed) 4))))
1352     (unless (zerop fixed)
1353       (inst sub count (fixnumize fixed)))))
1354
1355 ;;; Signal wrong argument count error if NARGS isn't equal to COUNT.
1356 (define-vop (verify-arg-count)
1357   (:policy :fast-safe)
1358   (:translate sb!c::%verify-arg-count)
1359   (:args (nargs :scs (any-reg)))
1360   (:arg-types positive-fixnum (:constant t))
1361   (:info count)
1362   (:vop-var vop)
1363   (:save-p :compute-only)
1364   (:generator 3
1365     (let ((err-lab
1366            (generate-error-code vop invalid-arg-count-error nargs)))
1367       (if (zerop count)
1368           (inst test nargs nargs)  ; smaller instruction
1369         (inst cmp nargs (fixnumize count)))
1370       (inst jmp :ne err-lab))))
1371
1372 ;;; Various other error signallers.
1373 (macrolet ((def (name error translate &rest args)
1374              `(define-vop (,name)
1375                 ,@(when translate
1376                     `((:policy :fast-safe)
1377                       (:translate ,translate)))
1378                 (:args ,@(mapcar (lambda (arg)
1379                                    `(,arg :scs (any-reg descriptor-reg)))
1380                                  args))
1381                 (:vop-var vop)
1382                 (:save-p :compute-only)
1383                 (:generator 1000
1384                   (error-call vop ,error ,@args)))))
1385   (def arg-count-error invalid-arg-count-error
1386     sb!c::%arg-count-error nargs)
1387   (def type-check-error object-not-type-error sb!c::%type-check-error
1388     object type)
1389   (def layout-invalid-error layout-invalid-error sb!c::%layout-invalid-error
1390     object layout)
1391   (def odd-key-args-error odd-key-args-error
1392     sb!c::%odd-key-args-error)
1393   (def unknown-key-arg-error unknown-key-arg-error
1394     sb!c::%unknown-key-arg-error key)
1395   (def nil-fun-returned-error nil-fun-returned-error nil fun))