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