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