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