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