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