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