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