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