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