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