1.0.27.11: swap ocfp and return-pc slots in x86oid call frames
[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     (emit-alignment 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 (frame-word-offset 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     (let ((single-value (gen-label)))
204       (cond
205        ((member :cmov *backend-subfeatures*)
206         (inst cmov :c esp-tn ebx-tn))
207        (t
208         (inst jmp :nc single-value)
209         (inst mov esp-tn ebx-tn)
210         (emit-label single-value)))))
211    ((<= nvals register-arg-count)
212     (let ((regs-defaulted (gen-label)))
213       (note-this-location vop :unknown-return)
214       (inst jmp :c regs-defaulted)
215       ;; Default the unsupplied registers.
216       (let* ((2nd-tn-ref (tn-ref-across values))
217              (2nd-tn (tn-ref-tn 2nd-tn-ref)))
218         (inst mov 2nd-tn nil-value)
219         (when (> nvals 2)
220           (loop
221             for tn-ref = (tn-ref-across 2nd-tn-ref)
222             then (tn-ref-across tn-ref)
223             for count from 2 below register-arg-count
224             do (inst mov (tn-ref-tn tn-ref) 2nd-tn))))
225       (inst mov ebx-tn esp-tn)
226       (emit-label regs-defaulted)
227       (inst mov esp-tn ebx-tn)))
228    ((<= nvals 7)
229     ;; The number of bytes depends on the relative jump instructions.
230     ;; Best case is 31+(n-3)*14, worst case is 35+(n-3)*18. For
231     ;; NVALS=6 that is 73/89 bytes, and for NVALS=7 that is 87/107
232     ;; bytes which is likely better than using the blt below.
233     (let ((regs-defaulted (gen-label))
234           (defaulting-done (gen-label))
235           (default-stack-slots (gen-label)))
236       (note-this-location vop :unknown-return)
237       ;; Branch off to the MV case.
238       (inst jmp :c regs-defaulted)
239       ;; Do the single value case.
240       ;; Default the register args
241       (inst mov eax-tn nil-value)
242       (do ((i 1 (1+ i))
243            (val (tn-ref-across values) (tn-ref-across val)))
244           ((= i (min nvals register-arg-count)))
245         (inst mov (tn-ref-tn val) eax-tn))
246
247       ;; Fake other registers so it looks like we returned with all the
248       ;; registers filled in.
249       (move ebx-tn esp-tn)
250       (inst push edx-tn)
251       (inst jmp default-stack-slots)
252
253       (emit-label regs-defaulted)
254
255       (inst mov eax-tn nil-value)
256       (storew edx-tn ebx-tn -1)
257       (collect ((defaults))
258         (do ((i register-arg-count (1+ i))
259              (val (do ((i 0 (1+ i))
260                        (val values (tn-ref-across val)))
261                       ((= i register-arg-count) val))
262                   (tn-ref-across val)))
263             ((null val))
264           (let ((default-lab (gen-label))
265                 (tn (tn-ref-tn val)))
266             (defaults (cons default-lab tn))
267
268             (inst cmp ecx-tn (fixnumize i))
269             (inst jmp :be default-lab)
270             (loadw edx-tn ebx-tn (frame-word-offset i))
271             (inst mov tn edx-tn)))
272
273         (emit-label defaulting-done)
274         (loadw edx-tn ebx-tn -1)
275         (move esp-tn ebx-tn)
276
277         (let ((defaults (defaults)))
278           (when defaults
279             (assemble (*elsewhere*)
280               (trace-table-entry trace-table-fun-prologue)
281               (emit-label default-stack-slots)
282               (dolist (default defaults)
283                 (emit-label (car default))
284                 (inst mov (cdr default) eax-tn))
285               (inst jmp defaulting-done)
286               (trace-table-entry trace-table-normal)))))))
287    (t
288     ;; 91 bytes for this branch.
289     (let ((regs-defaulted (gen-label))
290           (restore-edi (gen-label))
291           (no-stack-args (gen-label))
292           (default-stack-vals (gen-label))
293           (count-okay (gen-label)))
294       (note-this-location vop :unknown-return)
295       ;; Branch off to the MV case.
296       (inst jmp :c regs-defaulted)
297
298       ;; Default the register args, and set up the stack as if we
299       ;; entered the MV return point.
300       (inst mov ebx-tn esp-tn)
301       (inst push edx-tn)
302       (inst mov edi-tn nil-value)
303       (inst push edi-tn)
304       (inst mov esi-tn edi-tn)
305       ;; Compute a pointer to where to put the [defaulted] stack values.
306       (emit-label no-stack-args)
307       (inst lea edi-tn
308             (make-ea :dword :base ebp-tn
309                      :disp (frame-byte-offset register-arg-count)))
310       ;; Load EAX with NIL so we can quickly store it, and set up
311       ;; stuff for the loop.
312       (inst mov eax-tn nil-value)
313       (inst std)
314       (inst mov ecx-tn (- nvals register-arg-count))
315       ;; Jump into the default loop.
316       (inst jmp default-stack-vals)
317
318       ;; The regs are defaulted. We need to copy any stack arguments,
319       ;; and then default the remaining stack arguments.
320       (emit-label regs-defaulted)
321       ;; Save EDI.
322       (storew edi-tn ebx-tn (frame-word-offset 1))
323       ;; Compute the number of stack arguments, and if it's zero or
324       ;; less, don't copy any stack arguments.
325       (inst sub ecx-tn (fixnumize register-arg-count))
326       (inst jmp :le no-stack-args)
327
328       ;; Throw away any unwanted args.
329       (inst cmp ecx-tn (fixnumize (- nvals register-arg-count)))
330       (inst jmp :be count-okay)
331       (inst mov ecx-tn (fixnumize (- nvals register-arg-count)))
332       (emit-label count-okay)
333       ;; Save the number of stack values.
334       (inst mov eax-tn ecx-tn)
335       ;; Compute a pointer to where the stack args go.
336       (inst lea edi-tn
337             (make-ea :dword :base ebp-tn
338                      :disp (frame-byte-offset register-arg-count)))
339       ;; Save ESI, and compute a pointer to where the args come from.
340       (storew esi-tn ebx-tn (frame-word-offset 2))
341       (inst lea esi-tn
342             (make-ea :dword :base ebx-tn
343                      :disp (frame-byte-offset register-arg-count)))
344       ;; Do the copy.
345       (inst shr ecx-tn word-shift)              ; make word count
346       (inst std)
347       (inst rep)
348       (inst movs :dword)
349       ;; Restore ESI.
350       (loadw esi-tn ebx-tn (frame-word-offset 2))
351       ;; Now we have to default the remaining args. Find out how many.
352       (inst sub eax-tn (fixnumize (- nvals register-arg-count)))
353       (inst neg eax-tn)
354       ;; If none, then just blow out of here.
355       (inst jmp :le restore-edi)
356       (inst mov ecx-tn eax-tn)
357       (inst shr ecx-tn word-shift)      ; word count
358       ;; Load EAX with NIL for fast storing.
359       (inst mov eax-tn nil-value)
360       ;; Do the store.
361       (emit-label default-stack-vals)
362       (inst rep)
363       (inst stos eax-tn)
364       ;; Restore EDI, and reset the stack.
365       (emit-label restore-edi)
366       (loadw edi-tn ebx-tn (frame-word-offset 1))
367       (inst mov esp-tn ebx-tn)
368       (inst cld))))
369   (values))
370 \f
371 ;;;; unknown values receiving
372
373 ;;; Emit code needed at the return point for an unknown-values call
374 ;;; for an arbitrary number of values.
375 ;;;
376 ;;; We do the single and non-single cases with no shared code: there
377 ;;; doesn't seem to be any potential overlap, and receiving a single
378 ;;; value is more important efficiency-wise.
379 ;;;
380 ;;; When there is a single value, we just push it on the stack,
381 ;;; returning the old SP and 1.
382 ;;;
383 ;;; When there is a variable number of values, we move all of the
384 ;;; argument registers onto the stack, and return ARGS and NARGS.
385 ;;;
386 ;;; ARGS and NARGS are TNs wired to the named locations. We must
387 ;;; explicitly allocate these TNs, since their lifetimes overlap with
388 ;;; the results start and count. (Also, it's nice to be able to target
389 ;;; them.)
390 (defun receive-unknown-values (args nargs start count)
391   (declare (type tn args nargs start count))
392   (let ((variable-values (gen-label))
393         (done (gen-label)))
394     (inst jmp :c variable-values)
395
396     (cond ((location= start (first *register-arg-tns*))
397            (inst push (first *register-arg-tns*))
398            (inst lea start (make-ea :dword :base esp-tn :disp n-word-bytes)))
399           (t (inst mov start esp-tn)
400              (inst push (first *register-arg-tns*))))
401     (inst mov count (fixnumize 1))
402     (inst jmp done)
403
404     (emit-label variable-values)
405     ;; dtc: this writes the registers onto the stack even if they are
406     ;; not needed, only the number specified in ecx are used and have
407     ;; stack allocated to them. No harm is done.
408     (loop
409       for arg in *register-arg-tns*
410       for i downfrom -1
411       do (storew arg args i))
412     (move start args)
413     (move count nargs)
414
415     (emit-label done))
416   (values))
417
418 ;;; VOP that can be inherited by unknown values receivers. The main thing this
419 ;;; handles is allocation of the result temporaries.
420 (define-vop (unknown-values-receiver)
421   (:temporary (:sc descriptor-reg :offset ebx-offset
422                    :from :eval :to (:result 0))
423               values-start)
424   (:temporary (:sc any-reg :offset ecx-offset
425                :from :eval :to (:result 1))
426               nvals)
427   (:results (start :scs (any-reg control-stack))
428             (count :scs (any-reg control-stack))))
429 \f
430 ;;;; local call with unknown values convention return
431
432 ;;; Non-TR local call for a fixed number of values passed according to
433 ;;; the unknown values convention.
434 ;;;
435 ;;; FP is the frame pointer in install before doing the call.
436 ;;;
437 ;;; NFP would be the number-stack frame pointer if we had a separate
438 ;;; number stack.
439 ;;;
440 ;;; Args are the argument passing locations, which are specified only
441 ;;; to terminate their lifetimes in the caller.
442 ;;;
443 ;;; VALUES are the return value locations (wired to the standard
444 ;;; passing locations). NVALS is the number of values received.
445 ;;;
446 ;;; Save is the save info, which we can ignore since saving has been
447 ;;; done.
448 ;;;
449 ;;; TARGET is a continuation pointing to the start of the called
450 ;;; function.
451 (define-vop (call-local)
452   (:args (fp)
453          (nfp)
454          (args :more t))
455   (:results (values :more t))
456   (:save-p t)
457   (:move-args :local-call)
458   (:info arg-locs callee target nvals)
459   (:vop-var vop)
460   (:ignore nfp arg-locs args #+nil callee)
461   (:generator 5
462     (trace-table-entry trace-table-call-site)
463     (move ebp-tn fp)
464
465     (let ((ret-tn (callee-return-pc-tn callee)))
466       #+nil
467       (format t "*call-local ~S; tn-kind ~S; tn-save-tn ~S; its tn-kind ~S~%"
468               ret-tn (sb!c::tn-kind ret-tn) (sb!c::tn-save-tn ret-tn)
469               (sb!c::tn-kind (sb!c::tn-save-tn ret-tn)))
470
471       ;; Is the return-pc on the stack or in a register?
472       (sc-case ret-tn
473         ((sap-stack)
474          (unless (= (tn-offset ret-tn) return-pc-save-offset)
475            (error "ret-tn ~A in wrong stack slot" ret-tn))
476          #+nil (format t "*call-local: ret-tn on stack; offset=~S~%"
477                        (tn-offset ret-tn))
478          (storew (make-fixup nil :code-object RETURN)
479                  ebp-tn (frame-word-offset (tn-offset ret-tn))))
480         (t
481          (error "ret-tn ~A in sap-reg" ret-tn))))
482
483     (note-this-location vop :call-site)
484     (inst jmp target)
485     RETURN
486     (default-unknown-values vop values nvals)
487     (trace-table-entry trace-table-normal)))
488
489 ;;; Non-TR local call for a variable number of return values passed according
490 ;;; to the unknown values convention. The results are the start of the values
491 ;;; glob and the number of values received.
492 (define-vop (multiple-call-local unknown-values-receiver)
493   (:args (fp)
494          (nfp)
495          (args :more t))
496   (:save-p t)
497   (:move-args :local-call)
498   (:info save callee target)
499   (:ignore args save nfp #+nil callee)
500   (:vop-var vop)
501   (:generator 20
502     (trace-table-entry trace-table-call-site)
503     (move ebp-tn fp)
504
505     (let ((ret-tn (callee-return-pc-tn callee)))
506       #+nil
507       (format t "*multiple-call-local ~S; tn-kind ~S; tn-save-tn ~S; its tn-kind ~S~%"
508               ret-tn (sb!c::tn-kind ret-tn) (sb!c::tn-save-tn ret-tn)
509               (sb!c::tn-kind (sb!c::tn-save-tn ret-tn)))
510
511       ;; Is the return-pc on the stack or in a register?
512       (sc-case ret-tn
513         ((sap-stack)
514          #+nil (format t "*multiple-call-local: ret-tn on stack; offset=~S~%"
515                        (tn-offset ret-tn))
516          ;; Stack
517          (storew (make-fixup nil :code-object RETURN)
518                  ebp-tn (frame-word-offset (tn-offset ret-tn))))
519         (t
520          (error "multiple-call-local: return-pc not on stack."))))
521
522     (note-this-location vop :call-site)
523     (inst jmp target)
524     RETURN
525     (note-this-location vop :unknown-return)
526     (receive-unknown-values values-start nvals start count)
527     (trace-table-entry trace-table-normal)))
528 \f
529 ;;;; local call with known values return
530
531 ;;; Non-TR local call with known return locations. Known-value return
532 ;;; works just like argument passing in local call.
533 ;;;
534 ;;; Note: we can't use normal load-tn allocation for the fixed args,
535 ;;; since all registers may be tied up by the more operand. Instead,
536 ;;; we use MAYBE-LOAD-STACK-TN.
537 (define-vop (known-call-local)
538   (:args (fp)
539          (nfp)
540          (args :more t))
541   (:results (res :more t))
542   (:move-args :local-call)
543   (:save-p t)
544   (:info save callee target)
545   (:ignore args res save nfp #+nil callee)
546   (:vop-var vop)
547   (:generator 5
548     (trace-table-entry trace-table-call-site)
549     (move ebp-tn fp)
550
551     (let ((ret-tn (callee-return-pc-tn callee)))
552
553       #+nil
554       (format t "*known-call-local ~S; tn-kind ~S; tn-save-tn ~S; its tn-kind ~S~%"
555               ret-tn (sb!c::tn-kind ret-tn) (sb!c::tn-save-tn ret-tn)
556               (sb!c::tn-kind (sb!c::tn-save-tn ret-tn)))
557
558       ;; Is the return-pc on the stack or in a register?
559       (sc-case ret-tn
560         ((sap-stack)
561          #+nil (format t "*known-call-local: ret-tn on stack; offset=~S~%"
562                        (tn-offset ret-tn))
563          ;; Stack
564          (storew (make-fixup nil :code-object RETURN)
565                  ebp-tn (frame-word-offset (tn-offset ret-tn))))
566         (t
567          (error "known-call-local: return-pc not on stack."))))
568
569     (note-this-location vop :call-site)
570     (inst jmp target)
571     RETURN
572     (note-this-location vop :known-return)
573     (trace-table-entry trace-table-normal)))
574 \f
575 ;;; Return from known values call. We receive the return locations as
576 ;;; arguments to terminate their lifetimes in the returning function. We
577 ;;; restore FP and CSP and jump to the Return-PC.
578 ;;;
579 ;;; We can assume we know exactly where old-fp and return-pc are because
580 ;;; make-old-fp-save-location and make-return-pc-save-location always
581 ;;; return the same place.
582 #+nil
583 (define-vop (known-return)
584   (:args (old-fp)
585          (return-pc :scs (any-reg immediate-stack) :target rpc)
586          (vals :more t))
587   (:move-args :known-return)
588   (:info val-locs)
589   (:temporary (:sc unsigned-reg :from (:argument 1)) rpc)
590   (:ignore val-locs vals)
591   (:vop-var vop)
592   (:generator 6
593     (trace-table-entry trace-table-fun-epilogue)
594     ;; Save the return-pc in a register 'cause the frame-pointer is
595     ;; going away. Note this not in the usual stack location so we
596     ;; can't use RET
597     (move rpc return-pc)
598     ;; Restore the stack.
599     (move esp-tn ebp-tn)
600     ;; Restore the old fp. We know OLD-FP is going to be in its stack
601     ;; save slot, which is a different frame that than this one,
602     ;; so we don't have to worry about having just cleared
603     ;; most of the stack.
604     (move ebp-tn old-fp)
605     (inst jmp rpc)
606     (trace-table-entry trace-table-normal)))
607 \f
608 ;;; From Douglas Crosher
609 ;;; Return from known values call. We receive the return locations as
610 ;;; arguments to terminate their lifetimes in the returning function. We
611 ;;; restore FP and CSP and jump to the Return-PC.
612 ;;;
613 ;;; The old-fp may be either in a register or on the stack in its
614 ;;; standard save locations - slot 0.
615 ;;;
616 ;;; The return-pc may be in a register or on the stack in any slot.
617 (define-vop (known-return)
618   (:args (old-fp)
619          (return-pc)
620          (vals :more t))
621   (:move-args :known-return)
622   (:info val-locs)
623   (:ignore val-locs vals)
624   (:vop-var vop)
625   (:generator 6
626     (trace-table-entry trace-table-fun-epilogue)
627
628     #+nil (format t "*known-return: old-fp ~S, tn-kind ~S; ~S ~S~%"
629                   old-fp (sb!c::tn-kind old-fp) (sb!c::tn-save-tn old-fp)
630                   (sb!c::tn-kind (sb!c::tn-save-tn old-fp)))
631
632     #+nil (format t "*known-return: return-pc ~S, tn-kind ~S; ~S ~S~%"
633                   return-pc (sb!c::tn-kind return-pc)
634                   (sb!c::tn-save-tn return-pc)
635                   (sb!c::tn-kind (sb!c::tn-save-tn return-pc)))
636
637     ;; return-pc may be either in a register or on the stack.
638     (sc-case return-pc
639       ((sap-stack)
640        #+nil (format t "*known-return: return-pc ~S on stack; offset=~S~%"
641                      return-pc (tn-offset return-pc))
642        (unless (and (sc-is old-fp control-stack)
643                     (= (tn-offset old-fp) ocfp-save-offset))
644          (error "known-return: ocfp not on stack in standard save location?"))
645        (unless (and (sc-is return-pc sap-stack)
646                     (= (tn-offset return-pc) return-pc-save-offset))
647          (error
648           "known-return: return-pc not on stack in standard save location?"))
649
650        ;; Zot all of the stack except for the old-fp and return-pc.
651        (inst lea esp-tn
652              (make-ea :dword :base ebp-tn
653                       :disp (frame-byte-offset ocfp-save-offset)))
654        (inst pop ebp-tn)
655        (inst ret (* (tn-offset return-pc) n-word-bytes)))
656       (t
657        (error "known-return, return-pc not on stack")))
658
659     (trace-table-entry trace-table-normal)))
660 \f
661 ;;;; full call
662 ;;;
663 ;;; There is something of a cross-product effect with full calls.
664 ;;; Different versions are used depending on whether we know the
665 ;;; number of arguments or the name of the called function, and
666 ;;; whether we want fixed values, unknown values, or a tail call.
667 ;;;
668 ;;; In full call, the arguments are passed creating a partial frame on
669 ;;; the stack top and storing stack arguments into that frame. On
670 ;;; entry to the callee, this partial frame is pointed to by FP.
671
672 ;;; This macro helps in the definition of full call VOPs by avoiding
673 ;;; code replication in defining the cross-product VOPs.
674 ;;;
675 ;;; NAME is the name of the VOP to define.
676 ;;;
677 ;;; NAMED is true if the first argument is an fdefinition object whose
678 ;;; definition is to be called.
679 ;;;
680 ;;; RETURN is either :FIXED, :UNKNOWN or :TAIL:
681 ;;; -- If :FIXED, then the call is for a fixed number of values, returned in
682 ;;;    the standard passing locations (passed as result operands).
683 ;;; -- If :UNKNOWN, then the result values are pushed on the stack, and the
684 ;;;    result values are specified by the Start and Count as in the
685 ;;;    unknown-values continuation representation.
686 ;;; -- If :TAIL, then do a tail-recursive call. No values are returned.
687 ;;;    The Old-Fp and Return-PC are passed as the second and third arguments.
688 ;;;
689 ;;; In non-tail calls, the pointer to the stack arguments is passed as
690 ;;; the last fixed argument. If Variable is false, then the passing
691 ;;; locations are passed as a more arg. Variable is true if there are
692 ;;; a variable number of arguments passed on the stack. Variable
693 ;;; cannot be specified with :TAIL return. TR variable argument call
694 ;;; is implemented separately.
695 ;;;
696 ;;; In tail call with fixed arguments, the passing locations are
697 ;;; passed as a more arg, but there is no new-FP, since the arguments
698 ;;; have been set up in the current frame.
699 (macrolet ((define-full-call (name named return variable)
700             (aver (not (and variable (eq return :tail))))
701             `(define-vop (,name
702                           ,@(when (eq return :unknown)
703                               '(unknown-values-receiver)))
704                (:args
705                ,@(unless (eq return :tail)
706                    '((new-fp :scs (any-reg) :to (:argument 1))))
707
708                (fun :scs (descriptor-reg control-stack)
709                     :target eax :to (:argument 0))
710
711                ,@(when (eq return :tail)
712                    '((old-fp)
713                      (return-pc)))
714
715                ,@(unless variable '((args :more t :scs (descriptor-reg)))))
716
717                ,@(when (eq return :fixed)
718                '((:results (values :more t))))
719
720                (:save-p ,(if (eq return :tail) :compute-only t))
721
722                ,@(unless (or (eq return :tail) variable)
723                '((:move-args :full-call)))
724
725                (:vop-var vop)
726                (:info
727                ,@(unless (or variable (eq return :tail)) '(arg-locs))
728                ,@(unless variable '(nargs))
729                ,@(when (eq return :fixed) '(nvals))
730                step-instrumenting)
731
732                (:ignore
733                ,@(unless (or variable (eq return :tail)) '(arg-locs))
734                ,@(unless variable '(args)))
735
736                ;; We pass either the fdefn object (for named call) or
737                ;; the actual function object (for unnamed call) in
738                ;; EAX. With named call, closure-tramp will replace it
739                ;; with the real function and invoke the real function
740                ;; for closures. Non-closures do not need this value,
741                ;; so don't care what shows up in it.
742                (:temporary
743                (:sc descriptor-reg
744                     :offset eax-offset
745                     :from (:argument 0)
746                     :to :eval)
747                eax)
748
749                ;; We pass the number of arguments in ECX.
750                (:temporary (:sc unsigned-reg :offset ecx-offset :to :eval) ecx)
751
752                ;; With variable call, we have to load the
753                ;; register-args out of the (new) stack frame before
754                ;; doing the call. Therefore, we have to tell the
755                ;; lifetime stuff that we need to use them.
756                ,@(when variable
757                    (mapcar (lambda (name offset)
758                              `(:temporary (:sc descriptor-reg
759                                                :offset ,offset
760                                                :from (:argument 0)
761                                                :to :eval)
762                                           ,name))
763                            *register-arg-names* *register-arg-offsets*))
764
765                ,@(when (eq return :tail)
766                    '((:temporary (:sc unsigned-reg
767                                       :from (:argument 1)
768                                       :to (:argument 2))
769                                  old-fp-tmp)))
770
771                (:generator ,(+ (if named 5 0)
772                                (if variable 19 1)
773                                (if (eq return :tail) 0 10)
774                                15
775                                (if (eq return :unknown) 25 0))
776                (trace-table-entry trace-table-call-site)
777
778                ;; This has to be done before the frame pointer is
779                ;; changed! EAX stores the 'lexical environment' needed
780                ;; for closures.
781                (move eax fun)
782
783
784                ,@(if variable
785                      ;; For variable call, compute the number of
786                      ;; arguments and move some of the arguments to
787                      ;; registers.
788                      (collect ((noise))
789                               ;; Compute the number of arguments.
790                               (noise '(inst mov ecx new-fp))
791                               (noise '(inst sub ecx esp-tn))
792                               ;; Move the necessary args to registers,
793                               ;; this moves them all even if they are
794                               ;; not all needed.
795                               (loop
796                                for name in *register-arg-names*
797                                for index downfrom -1
798                                do (noise `(loadw ,name new-fp ,index)))
799                               (noise))
800                    '((if (zerop nargs)
801                          (inst xor ecx ecx)
802                        (inst mov ecx (fixnumize nargs)))))
803                ,@(cond ((eq return :tail)
804                         '(;; Python has figured out what frame we should
805                           ;; return to so might as well use that clue.
806                           ;; This seems really important to the
807                           ;; implementation of things like
808                           ;; (without-interrupts ...)
809                           ;;
810                           ;; dtc; Could be doing a tail call from a
811                           ;; known-local-call etc in which the old-fp
812                           ;; or ret-pc are in regs or in non-standard
813                           ;; places. If the passing location were
814                           ;; wired to the stack in standard locations
815                           ;; then these moves will be un-necessary;
816                           ;; this is probably best for the x86.
817                           (sc-case old-fp
818                                    ((control-stack)
819                                     (unless (= ocfp-save-offset
820                                                (tn-offset old-fp))
821                                       ;; FIXME: FORMAT T for stale
822                                       ;; diagnostic output (several of
823                                       ;; them around here), ick
824                                       (error "** tail-call old-fp not S0~%")
825                                       (move old-fp-tmp old-fp)
826                                       (storew old-fp-tmp
827                                               ebp-tn
828                                               (frame-word-offset ocfp-save-offset))))
829                                    ((any-reg descriptor-reg)
830                                     (error "** tail-call old-fp in reg not S0~%")
831                                     (storew old-fp
832                                             ebp-tn
833                                             (frame-word-offset ocfp-save-offset))))
834
835                           ;; For tail call, we have to push the
836                           ;; return-pc so that it looks like we CALLed
837                           ;; despite the fact that we are going to JMP.
838                           (inst push return-pc)
839                           ))
840                        (t
841                         ;; For non-tail call, we have to save our
842                         ;; frame pointer and install the new frame
843                         ;; pointer. We can't load stack tns after this
844                         ;; point.
845                         `(;; Python doesn't seem to allocate a frame
846                           ;; here which doesn't leave room for the
847                           ;; ofp/ret stuff.
848
849                           ;; The variable args are on the stack and
850                           ;; become the frame, but there may be <3
851                           ;; args and 3 stack slots are assumed
852                           ;; allocate on the call. So need to ensure
853                           ;; there are at least 3 slots. This hack
854                           ;; just adds 3 more.
855                           ,(if variable
856                                '(inst sub esp-tn (fixnumize 3)))
857
858                           ;; Save the fp
859                           (storew ebp-tn new-fp
860                                   (frame-word-offset ocfp-save-offset))
861
862                           (move ebp-tn new-fp) ; NB - now on new stack frame.
863                           )))
864
865                (when step-instrumenting
866                  (emit-single-step-test)
867                  (inst jmp :eq DONE)
868                  (inst break single-step-around-trap))
869                DONE
870
871                (note-this-location vop :call-site)
872
873                (inst ,(if (eq return :tail) 'jmp 'call)
874                      ,(if named
875                           '(make-ea-for-object-slot eax fdefn-raw-addr-slot
876                                                     other-pointer-lowtag)
877                           '(make-ea-for-object-slot eax closure-fun-slot
878                                                     fun-pointer-lowtag)))
879                ,@(ecase return
880                    (:fixed
881                     '((default-unknown-values vop values nvals)))
882                    (:unknown
883                     '((note-this-location vop :unknown-return)
884                       (receive-unknown-values values-start nvals start count)))
885                    (:tail))
886                (trace-table-entry trace-table-normal)))))
887
888   (define-full-call call nil :fixed nil)
889   (define-full-call call-named t :fixed nil)
890   (define-full-call multiple-call nil :unknown nil)
891   (define-full-call multiple-call-named t :unknown nil)
892   (define-full-call tail-call nil :tail nil)
893   (define-full-call tail-call-named t :tail nil)
894
895   (define-full-call call-variable nil :fixed t)
896   (define-full-call multiple-call-variable nil :unknown t))
897
898 ;;; This is defined separately, since it needs special code that BLT's
899 ;;; the arguments down. All the real work is done in the assembly
900 ;;; routine. We just set things up so that it can find what it needs.
901 (define-vop (tail-call-variable)
902   (:args (args :scs (any-reg control-stack) :target esi)
903          (function :scs (descriptor-reg control-stack) :target eax)
904          (old-fp)
905          (ret-addr))
906   (:temporary (:sc unsigned-reg :offset esi-offset :from (:argument 0)) esi)
907   (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 1)) eax)
908 ;  (:ignore ret-addr old-fp)
909   (:generator 75
910     ;; Move these into the passing locations if they are not already there.
911     (move esi args)
912     (move eax function)
913
914     ;; The following assumes that the return-pc and old-fp are on the
915     ;; stack in their standard save locations - Check this.
916     (unless (and (sc-is old-fp control-stack)
917                  (= (tn-offset old-fp) ocfp-save-offset))
918             (error "tail-call-variable: ocfp not on stack in standard save location?"))
919     (unless (and (sc-is ret-addr sap-stack)
920                  (= (tn-offset ret-addr) return-pc-save-offset))
921             (error "tail-call-variable: ret-addr not on stack in standard save location?"))
922
923
924     ;; And jump to the assembly routine.
925     (inst jmp (make-fixup 'tail-call-variable :assembly-routine))))
926 \f
927 ;;;; unknown values return
928
929 ;;; Return a single-value using the Unknown-Values convention.
930 ;;;
931 ;;; pfw--get wired-tn conflicts sometimes if register sc specd for args
932 ;;; having problems targeting args to regs -- using temps instead.
933 ;;;
934 ;;; First off, modifying the return-pc defeats the branch-prediction
935 ;;; optimizations on modern CPUs quite handily. Second, we can do all
936 ;;; this without needing a temp register. Fixed the latter, at least.
937 ;;; -- AB 2006/Feb/04
938 (define-vop (return-single)
939   (:args (old-fp)
940          (return-pc)
941          (value))
942   (:ignore value)
943   (:generator 6
944     (trace-table-entry trace-table-fun-epilogue)
945     ;; Code structure lifted from known-return.
946     (sc-case return-pc
947       ((sap-stack)
948        ;; Note that this will only work right if, when old-fp is on
949        ;; the stack, it has a lower tn-offset than return-pc. One of
950        ;; the comments in known-return indicate that this is the case
951        ;; (in that it will be in its save location), but we may wish
952        ;; to assert that (in either the weaker or stronger forms).
953        ;; Should this ever not be the case, we should load old-fp into
954        ;; a temp reg while we fix the stack.
955        (unless (and (sc-is old-fp control-stack)
956                     (= (tn-offset old-fp) ocfp-save-offset))
957          (error "ocfp not on stack in standard save location?"))
958        (unless (and (sc-is return-pc sap-stack)
959                     (= (tn-offset return-pc) return-pc-save-offset))
960          (error "return-pc not on stack in standard save location?"))
961        ;; Drop stack above old-fp
962        (inst lea esp-tn (make-ea :dword :base ebp-tn
963                                  :disp (frame-byte-offset (tn-offset old-fp))))
964        ;; Set single-value return flag
965        (inst clc)
966        ;; Restore the old frame pointer
967        (inst pop ebp-tn)
968        ;; And return, dropping the rest of the stack as we go.
969        (inst ret (* (tn-offset return-pc) n-word-bytes)))
970       (t
971        (error "return pc not on stack")))))
972
973 ;;; Do unknown-values return of a fixed (other than 1) number of
974 ;;; values. The VALUES are required to be set up in the standard
975 ;;; passing locations. NVALS is the number of values returned.
976 ;;;
977 ;;; Basically, we just load ECX with the number of values returned and
978 ;;; EBX with a pointer to the values, set ESP to point to the end of
979 ;;; the values, and jump directly to return-pc.
980 (define-vop (return)
981   (:args (old-fp)
982          (return-pc :to (:eval 1))
983          (values :more t))
984   (:ignore values)
985   (:info nvals)
986
987   ;; In the case of other than one value, we need these registers to
988   ;; tell the caller where they are and how many there are.
989   (:temporary (:sc unsigned-reg :offset ebx-offset) ebx)
990   (:temporary (:sc unsigned-reg :offset ecx-offset) ecx)
991
992   ;; We need to stretch the lifetime of return-pc past the argument
993   ;; registers so that we can default the argument registers without
994   ;; trashing return-pc.
995   (:temporary (:sc unsigned-reg :offset (first *register-arg-offsets*)
996                    :from :eval) a0)
997   (:temporary (:sc unsigned-reg :offset (second *register-arg-offsets*)
998                    :from :eval) a1)
999   (:temporary (:sc unsigned-reg :offset (third *register-arg-offsets*)
1000                    :from :eval) a2)
1001
1002   (:generator 6
1003     (unless (and (sc-is old-fp control-stack)
1004                  (= (tn-offset old-fp) ocfp-save-offset))
1005       (error "ocfp not on stack in standard save location?"))
1006     (unless (and (sc-is return-pc sap-stack)
1007                  (= (tn-offset return-pc) return-pc-save-offset))
1008       (error "return-pc not on stack in standard save location?"))
1009
1010     (trace-table-entry trace-table-fun-epilogue)
1011     ;; Establish the values pointer and values count.
1012     (move ebx ebp-tn)
1013     (if (zerop nvals)
1014         (inst xor ecx ecx)              ; smaller
1015         (inst mov ecx (fixnumize nvals)))
1016     ;; Restore the frame pointer.
1017     (move ebp-tn old-fp)
1018     ;; Clear as much of the stack as possible, but not past the return
1019     ;; address.
1020     (inst lea esp-tn
1021           (make-ea :dword :base ebx
1022                    :disp (frame-byte-offset (max (1- nvals)
1023                                                  return-pc-save-offset))))
1024     ;; Pre-default any argument register that need it.
1025     (when (< nvals register-arg-count)
1026       (let* ((arg-tns (nthcdr nvals (list a0 a1 a2)))
1027              (first (first arg-tns)))
1028         (inst mov first nil-value)
1029         (dolist (tn (cdr arg-tns))
1030           (inst mov tn first))))
1031     ;; Set the multiple value return flag.
1032     (inst stc)
1033     ;; And away we go. Except that return-pc is still on the
1034     ;; stack and we've changed the stack pointer. So we have to
1035     ;; tell it to index off of EBX instead of EBP.
1036     (cond ((zerop nvals)
1037            ;; Return popping the return address and what's earlier in
1038            ;; the frame.
1039            (inst ret (* return-pc-save-offset n-word-bytes)))
1040           ((= nvals 1)
1041            ;; This is handled in RETURN-SINGLE.
1042            (error "nvalues is 1"))
1043           (t
1044            ;; Thou shalt not JMP unto thy return address.
1045            (inst push (make-ea :dword :base ebx
1046                                :disp (frame-byte-offset (tn-offset return-pc))))
1047            (inst ret)))
1048
1049     (trace-table-entry trace-table-normal)))
1050
1051 ;;; Do unknown-values return of an arbitrary number of values (passed
1052 ;;; on the stack.) We check for the common case of a single return
1053 ;;; value, and do that inline using the normal single value return
1054 ;;; convention. Otherwise, we branch off to code that calls an
1055 ;;; assembly-routine.
1056 ;;;
1057 ;;; The assembly routine takes the following args:
1058 ;;;  EAX -- the return-pc to finally jump to.
1059 ;;;  EBX -- pointer to where to put the values.
1060 ;;;  ECX -- number of values to find there.
1061 ;;;  ESI -- pointer to where to find the values.
1062 (define-vop (return-multiple)
1063   (:args (old-fp :to (:eval 1) :target old-fp-temp)
1064          (return-pc :target eax)
1065          (vals :scs (any-reg) :target esi)
1066          (nvals :scs (any-reg) :target ecx))
1067
1068   (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 1)) eax)
1069   (:temporary (:sc unsigned-reg :offset esi-offset :from (:argument 2)) esi)
1070   (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 3)) ecx)
1071   (:temporary (:sc unsigned-reg :offset ebx-offset :from (:eval 0)) ebx)
1072   (:temporary (:sc descriptor-reg :offset (first *register-arg-offsets*)
1073                    :from (:eval 0)) a0)
1074   (:temporary (:sc unsigned-reg :from (:eval 1)) old-fp-temp)
1075   (:node-var node)
1076
1077   (:generator 13
1078     (trace-table-entry trace-table-fun-epilogue)
1079     ;; Load the return-pc.
1080     (move eax return-pc)
1081     (unless (policy node (> space speed))
1082       ;; Check for the single case.
1083       (let ((not-single (gen-label)))
1084         (inst cmp nvals (fixnumize 1))
1085         (inst jmp :ne not-single)
1086
1087         ;; Return with one value.
1088         (loadw a0 vals -1)
1089         ;; Clear the stack. We load old-fp into a register before clearing
1090         ;; the stack.
1091         (move old-fp-temp old-fp)
1092         (move esp-tn ebp-tn)
1093         (move ebp-tn old-fp-temp)
1094         ;; clear the multiple-value return flag
1095         (inst clc)
1096         ;; Out of here.
1097         (inst push eax)
1098         (inst ret)
1099         ;; Nope, not the single case. Jump to the assembly routine.
1100         (emit-label not-single)))
1101     (move esi vals)
1102     (move ecx nvals)
1103     (move ebx ebp-tn)
1104     (move ebp-tn old-fp)
1105     (inst jmp (make-fixup 'return-multiple :assembly-routine))
1106     (trace-table-entry trace-table-normal)))
1107 \f
1108 ;;;; XEP hackery
1109
1110 ;;; We don't need to do anything special for regular functions.
1111 (define-vop (setup-environment)
1112   (:info label)
1113   (:ignore label)
1114   (:generator 0
1115     ;; Don't bother doing anything.
1116     nil))
1117
1118 ;;; Get the lexical environment from its passing location.
1119 (define-vop (setup-closure-environment)
1120   (:results (closure :scs (descriptor-reg)))
1121   (:info label)
1122   (:ignore label)
1123   (:generator 6
1124     ;; Get result.
1125     (move closure eax-tn)))
1126
1127 ;;; Copy a &MORE arg from the argument area to the end of the current
1128 ;;; frame. FIXED is the number of non-&MORE arguments.
1129 ;;;
1130 ;;; The tricky part is doing this without trashing any of the calling
1131 ;;; convention registers that are still needed. This vop is emitted
1132 ;;; directly after the xep-allocate frame. That means the registers
1133 ;;; are in use as follows:
1134 ;;;
1135 ;;;  EAX -- The lexenv.
1136 ;;;  EBX -- Available.
1137 ;;;  ECX -- The total number of arguments.
1138 ;;;  EDX -- The first arg.
1139 ;;;  EDI -- The second arg.
1140 ;;;  ESI -- The third arg.
1141 ;;;
1142 ;;; So basically, we have one register available for our use: EBX.
1143 ;;;
1144 ;;; What we can do is push the other regs onto the stack, and then
1145 ;;; restore their values by looking directly below where we put the
1146 ;;; 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                             (* n-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     (inst push ebx-tn)
1187     ;; Okay, we have pushed the register args. We can trash them
1188     ;; now.
1189
1190     ;; Initialize src to be end of args.
1191     (inst mov esi-tn ebp-tn)
1192     (inst sub esi-tn ebx-tn)
1193
1194     ;; We need to copy from downwards up to avoid overwriting some of
1195     ;; the yet uncopied args. So we need to use EBX as the copy index
1196     ;; and ECX as the loop counter, rather than using ECX for both.
1197     (inst xor ebx-tn ebx-tn)
1198
1199     ;; We used to use REP MOVS here, but on modern x86 it performs
1200     ;; much worse than an explicit loop for small blocks.
1201     COPY-LOOP
1202     (inst mov edi-tn (make-ea :dword :base esi-tn :index ebx-tn))
1203     ;; The :DISP is to account for the registers saved on the stack
1204     (inst mov (make-ea :dword :base esp-tn :disp (* 3 n-word-bytes)
1205                        :index ebx-tn)
1206           edi-tn)
1207     (inst add ebx-tn n-word-bytes)
1208     (inst sub ecx-tn n-word-bytes)
1209     (inst jmp :nz COPY-LOOP)
1210
1211     ;; So now we need to restore EDI and ESI.
1212     (inst pop ebx-tn)
1213     (inst pop esi-tn)
1214     (inst pop edi-tn)
1215
1216     DO-REGS
1217
1218     ;; Restore ECX
1219     (inst mov ecx-tn ebx-tn)
1220
1221     ;; Here: nargs>=1 && nargs>fixed
1222     (when (< fixed register-arg-count)
1223           ;; Now we have to deposit any more args that showed up in
1224           ;; registers.
1225           (do ((i fixed))
1226               ( nil )
1227               ;; Store it relative to ebp
1228               (inst mov (make-ea :dword :base ebp-tn
1229                                  :disp (- (* n-word-bytes
1230                                              (+ 1 (- i fixed)
1231                                                 (max 3 (sb-allocated-size 'stack))))))
1232                     (nth i *register-arg-tns*))
1233
1234               (incf i)
1235               (when (>= i register-arg-count)
1236                     (return))
1237
1238               ;; Don't deposit any more than there are.
1239               (if (zerop i)
1240                   (inst test ecx-tn ecx-tn)
1241                 (inst cmp ecx-tn (fixnumize i)))
1242               (inst jmp :eq DONE)))
1243
1244     (inst jmp DONE)
1245
1246     JUST-ALLOC-FRAME
1247     (inst lea esp-tn
1248           (make-ea :dword :base ebp-tn
1249                    :disp (- (* n-word-bytes
1250                                (max 3 (sb-allocated-size 'stack))))))
1251
1252     DONE))
1253
1254 (define-vop (more-kw-arg)
1255   (:translate sb!c::%more-kw-arg)
1256   (:policy :fast-safe)
1257   (:args (object :scs (descriptor-reg) :to (:result 1))
1258          (index :scs (any-reg immediate) :to (:result 1) :target keyword))
1259   (:arg-types * tagged-num)
1260   (:results (value :scs (descriptor-reg any-reg))
1261             (keyword :scs (descriptor-reg any-reg)))
1262   (:result-types * *)
1263   (:generator 4
1264     (sc-case index
1265       (immediate
1266        (inst mov value (make-ea :dword :base object :disp (tn-value index)))
1267        (inst mov keyword (make-ea :dword :base object
1268                                   :disp (+ (tn-value index) n-word-bytes))))
1269       (t
1270        (inst mov value (make-ea :dword :base object :index index))
1271        (inst mov keyword (make-ea :dword :base object :index index
1272                                   :disp n-word-bytes))))))
1273
1274 (define-vop (more-arg)
1275     (:translate sb!c::%more-arg)
1276   (:policy :fast-safe)
1277   (:args (object :scs (descriptor-reg) :to (:result 1))
1278          (index :scs (any-reg) :to (:result 1) :target value))
1279   (:arg-types * tagged-num)
1280   (:results (value :scs (descriptor-reg any-reg)))
1281   (:result-types *)
1282   (:generator 4
1283     (move value index)
1284     (inst neg value)
1285     (inst mov value (make-ea :dword :base object :index value))))
1286
1287 ;;; Turn more arg (context, count) into a list.
1288 (define-vop (listify-rest-args)
1289   (:translate %listify-rest-args)
1290   (:policy :safe)
1291   (:args (context :scs (descriptor-reg) :target src)
1292          (count :scs (any-reg) :target ecx))
1293   (:arg-types * tagged-num)
1294   (:temporary (:sc unsigned-reg :offset esi-offset :from (:argument 0)) src)
1295   (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 1)) ecx)
1296   (:temporary (:sc unsigned-reg :offset eax-offset) eax)
1297   (:temporary (:sc unsigned-reg) dst)
1298   (:results (result :scs (descriptor-reg)))
1299   (:node-var node)
1300   (:generator 20
1301     (let ((enter (gen-label))
1302           (loop (gen-label))
1303           (done (gen-label))
1304           (stack-allocate-p (node-stack-allocate-p node)))
1305       (move src context)
1306       (move ecx count)
1307       ;; Check to see whether there are no args, and just return NIL if so.
1308       (inst mov result nil-value)
1309       (inst jecxz done)
1310       (inst lea dst (make-ea :dword :base ecx :index ecx))
1311       (maybe-pseudo-atomic stack-allocate-p
1312        (allocation dst dst node stack-allocate-p list-pointer-lowtag)
1313        (inst shr ecx (1- n-lowtag-bits))
1314        ;; Set decrement mode (successive args at lower addresses)
1315        (inst std)
1316        ;; Set up the result.
1317        (move result dst)
1318        ;; Jump into the middle of the loop, 'cause that's where we want
1319        ;; to start.
1320        (inst jmp enter)
1321        (emit-label loop)
1322        ;; Compute a pointer to the next cons.
1323        (inst add dst (* cons-size n-word-bytes))
1324        ;; Store a pointer to this cons in the CDR of the previous cons.
1325        (storew dst dst -1 list-pointer-lowtag)
1326        (emit-label enter)
1327        ;; Grab one value and stash it in the car of this cons.
1328        (inst lods eax)
1329        (storew eax dst 0 list-pointer-lowtag)
1330        ;; Go back for more.
1331        (inst sub ecx 1)
1332        (inst jmp :nz loop)
1333        ;; NIL out the last cons.
1334        (storew nil-value dst 1 list-pointer-lowtag)
1335        (inst cld))
1336       (emit-label done))))
1337
1338 ;;; Return the location and size of the &MORE arg glob created by
1339 ;;; COPY-MORE-ARG. SUPPLIED is the total number of arguments supplied
1340 ;;; (originally passed in ECX). FIXED is the number of non-rest
1341 ;;; arguments.
1342 ;;;
1343 ;;; We must duplicate some of the work done by COPY-MORE-ARG, since at
1344 ;;; that time the environment is in a pretty brain-damaged state,
1345 ;;; preventing this info from being returned as values. What we do is
1346 ;;; compute supplied - fixed, and return a pointer that many words
1347 ;;; below the current stack top.
1348 (define-vop (more-arg-context)
1349   (:policy :fast-safe)
1350   (:translate sb!c::%more-arg-context)
1351   (:args (supplied :scs (any-reg) :target count))
1352   (:arg-types positive-fixnum (:constant fixnum))
1353   (:info fixed)
1354   (:results (context :scs (descriptor-reg))
1355             (count :scs (any-reg)))
1356   (:result-types t tagged-num)
1357   (:note "more-arg-context")
1358   (:generator 5
1359     (move count supplied)
1360     ;; SP at this point points at the last arg pushed.
1361     ;; Point to the first more-arg, not above it.
1362     (inst lea context (make-ea :dword :base esp-tn
1363                                :index count :scale 1
1364                                :disp (- (+ (fixnumize fixed) n-word-bytes))))
1365     (unless (zerop fixed)
1366       (inst sub count (fixnumize fixed)))))
1367
1368 ;;; Signal wrong argument count error if NARGS isn't equal to COUNT.
1369 (define-vop (verify-arg-count)
1370   (:policy :fast-safe)
1371   (:translate sb!c::%verify-arg-count)
1372   (:args (nargs :scs (any-reg)))
1373   (:arg-types positive-fixnum (:constant t))
1374   (:info count)
1375   (:vop-var vop)
1376   (:save-p :compute-only)
1377   (:generator 3
1378     (let ((err-lab
1379            (generate-error-code vop 'invalid-arg-count-error nargs)))
1380       (if (zerop count)
1381           (inst test nargs nargs)  ; smaller instruction
1382         (inst cmp nargs (fixnumize count)))
1383       (inst jmp :ne err-lab))))
1384
1385 ;;; Various other error signallers.
1386 (macrolet ((def (name error translate &rest args)
1387              `(define-vop (,name)
1388                 ,@(when translate
1389                     `((:policy :fast-safe)
1390                       (:translate ,translate)))
1391                 (:args ,@(mapcar (lambda (arg)
1392                                    `(,arg :scs (any-reg descriptor-reg)))
1393                                  args))
1394                 (:vop-var vop)
1395                 (:save-p :compute-only)
1396                 (:generator 1000
1397                   (error-call vop ',error ,@args)))))
1398   (def arg-count-error invalid-arg-count-error
1399     sb!c::%arg-count-error nargs)
1400   (def type-check-error object-not-type-error sb!c::%type-check-error
1401     object type)
1402   (def layout-invalid-error layout-invalid-error sb!c::%layout-invalid-error
1403     object layout)
1404   (def odd-key-args-error odd-key-args-error
1405     sb!c::%odd-key-args-error)
1406   (def unknown-key-arg-error unknown-key-arg-error
1407     sb!c::%unknown-key-arg-error key)
1408   (def nil-fun-returned-error nil-fun-returned-error nil fun))
1409
1410 ;;; Single-stepping
1411
1412 (defun emit-single-step-test ()
1413   ;; We use different ways of representing whether stepping is on on
1414   ;; +SB-THREAD / -SB-THREAD: on +SB-THREAD, we use a slot in the
1415   ;; thread structure. On -SB-THREAD we use the value of a static
1416   ;; symbol. Things are done this way, since reading a thread-local
1417   ;; slot from a symbol would require an extra register on +SB-THREAD,
1418   ;; and reading a slot from a thread structure would require an extra
1419   ;; register on -SB-THREAD.
1420   #!+sb-thread
1421   (progn
1422     (inst cmp (make-ea :dword
1423                        :disp (* thread-stepping-slot n-word-bytes))
1424           nil-value :fs))
1425   #!-sb-thread
1426   (inst cmp (make-ea-for-symbol-value sb!impl::*stepping*)
1427         nil-value))
1428
1429 (define-vop (step-instrument-before-vop)
1430   (:policy :fast-safe)
1431   (:vop-var vop)
1432   (:generator 3
1433      (emit-single-step-test)
1434      (inst jmp :eq DONE)
1435      (inst break single-step-before-trap)
1436      DONE
1437      (note-this-location vop :step-before-vop)))