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