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