1.0.27.17: faster local calls on x86/x86-64
[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 ;;; Instead of JMPing to TARGET, CALL a trampoline that saves the
474 ;;; return pc and jumps. Although this is an incredibly stupid trick
475 ;;; the paired CALL/RET instructions are a big win.
476 (defun make-local-call (target)
477   (let ((tramp (gen-label)))
478     (inst call tramp)
479     (assemble (*elsewhere*)
480       (emit-label tramp)
481       (popw ebp-tn (frame-word-offset return-pc-save-offset))
482       (inst jmp target))))
483
484 ;;; Non-TR local call for a fixed number of values passed according to
485 ;;; the unknown values convention.
486 ;;;
487 ;;; FP is the frame pointer in install before doing the call.
488 ;;;
489 ;;; NFP would be the number-stack frame pointer if we had a separate
490 ;;; number stack.
491 ;;;
492 ;;; Args are the argument passing locations, which are specified only
493 ;;; to terminate their lifetimes in the caller.
494 ;;;
495 ;;; VALUES are the return value locations (wired to the standard
496 ;;; passing locations). NVALS is the number of values received.
497 ;;;
498 ;;; Save is the save info, which we can ignore since saving has been
499 ;;; done.
500 ;;;
501 ;;; TARGET is a continuation pointing to the start of the called
502 ;;; function.
503 (define-vop (call-local)
504   (:args (fp)
505          (nfp)
506          (args :more t))
507   (:results (values :more t))
508   (:save-p t)
509   (:move-args :local-call)
510   (:info arg-locs callee target nvals)
511   (:vop-var vop)
512   (:ignore nfp arg-locs args callee)
513   (:node-var node)
514   (:generator 5
515     (trace-table-entry trace-table-call-site)
516     (move ebp-tn fp)
517     (note-this-location vop :call-site)
518     (make-local-call target)
519     (default-unknown-values vop values nvals node)
520     (trace-table-entry trace-table-normal)))
521
522 ;;; Non-TR local call for a variable number of return values passed according
523 ;;; to the unknown values convention. The results are the start of the values
524 ;;; glob and the number of values received.
525 (define-vop (multiple-call-local unknown-values-receiver)
526   (:args (fp)
527          (nfp)
528          (args :more t))
529   (:save-p t)
530   (:move-args :local-call)
531   (:info save callee target)
532   (:ignore args save nfp callee)
533   (:vop-var vop)
534   (:node-var node)
535   (:generator 20
536     (trace-table-entry trace-table-call-site)
537     (move ebp-tn fp)
538     (note-this-location vop :call-site)
539     (make-local-call target)
540     (note-this-location vop :unknown-return)
541     (receive-unknown-values values-start nvals start count node)
542     (trace-table-entry trace-table-normal)))
543 \f
544 ;;;; local call with known values return
545
546 ;;; Non-TR local call with known return locations. Known-value return
547 ;;; works just like argument passing in local call.
548 ;;;
549 ;;; Note: we can't use normal load-tn allocation for the fixed args,
550 ;;; since all registers may be tied up by the more operand. Instead,
551 ;;; we use MAYBE-LOAD-STACK-TN.
552 (define-vop (known-call-local)
553   (:args (fp)
554          (nfp)
555          (args :more t))
556   (:results (res :more t))
557   (:move-args :local-call)
558   (:save-p t)
559   (:info save callee target)
560   (:ignore args res save nfp callee)
561   (:vop-var vop)
562   (:generator 5
563     (trace-table-entry trace-table-call-site)
564     (move ebp-tn fp)
565     (note-this-location vop :call-site)
566     (make-local-call target)
567     (note-this-location vop :known-return)
568     (trace-table-entry trace-table-normal)))
569 \f
570 ;;; From Douglas Crosher
571 ;;; Return from known values call. We receive the return locations as
572 ;;; arguments to terminate their lifetimes in the returning function. We
573 ;;; restore FP and CSP and jump to the Return-PC.
574 (define-vop (known-return)
575   (:args (old-fp)
576          (return-pc)
577          (vals :more t))
578   (:move-args :known-return)
579   (:info val-locs)
580   (:ignore val-locs vals)
581   (:vop-var vop)
582   (:generator 6
583     (check-ocfp-and-return-pc old-fp return-pc)
584     (trace-table-entry trace-table-fun-epilogue)
585     ;; Zot all of the stack except for the old-fp and return-pc.
586     (inst mov esp-tn ebp-tn)
587     (inst pop ebp-tn)
588     (inst ret)
589     (trace-table-entry trace-table-normal)))
590 \f
591 ;;;; full call
592 ;;;
593 ;;; There is something of a cross-product effect with full calls.
594 ;;; Different versions are used depending on whether we know the
595 ;;; number of arguments or the name of the called function, and
596 ;;; whether we want fixed values, unknown values, or a tail call.
597 ;;;
598 ;;; In full call, the arguments are passed creating a partial frame on
599 ;;; the stack top and storing stack arguments into that frame. On
600 ;;; entry to the callee, this partial frame is pointed to by FP.
601
602 ;;; This macro helps in the definition of full call VOPs by avoiding
603 ;;; code replication in defining the cross-product VOPs.
604 ;;;
605 ;;; NAME is the name of the VOP to define.
606 ;;;
607 ;;; NAMED is true if the first argument is an fdefinition object whose
608 ;;; definition is to be called.
609 ;;;
610 ;;; RETURN is either :FIXED, :UNKNOWN or :TAIL:
611 ;;; -- If :FIXED, then the call is for a fixed number of values, returned in
612 ;;;    the standard passing locations (passed as result operands).
613 ;;; -- If :UNKNOWN, then the result values are pushed on the stack, and the
614 ;;;    result values are specified by the Start and Count as in the
615 ;;;    unknown-values continuation representation.
616 ;;; -- If :TAIL, then do a tail-recursive call. No values are returned.
617 ;;;    The Old-Fp and Return-PC are passed as the second and third arguments.
618 ;;;
619 ;;; In non-tail calls, the pointer to the stack arguments is passed as
620 ;;; the last fixed argument. If Variable is false, then the passing
621 ;;; locations are passed as a more arg. Variable is true if there are
622 ;;; a variable number of arguments passed on the stack. Variable
623 ;;; cannot be specified with :TAIL return. TR variable argument call
624 ;;; is implemented separately.
625 ;;;
626 ;;; In tail call with fixed arguments, the passing locations are
627 ;;; passed as a more arg, but there is no new-FP, since the arguments
628 ;;; have been set up in the current frame.
629 (macrolet ((define-full-call (name named return variable)
630             (aver (not (and variable (eq return :tail))))
631             `(define-vop (,name
632                           ,@(when (eq return :unknown)
633                               '(unknown-values-receiver)))
634                (:args
635                ,@(unless (eq return :tail)
636                    '((new-fp :scs (any-reg) :to (:argument 1))))
637
638                (fun :scs (descriptor-reg control-stack)
639                     :target eax :to (:argument 0))
640
641                ,@(when (eq return :tail)
642                    '((old-fp)
643                      (return-pc)))
644
645                ,@(unless variable '((args :more t :scs (descriptor-reg)))))
646
647                ,@(when (eq return :fixed)
648                '((:results (values :more t))))
649
650                (:save-p ,(if (eq return :tail) :compute-only t))
651
652                ,@(unless (or (eq return :tail) variable)
653                '((:move-args :full-call)))
654
655                (:vop-var vop)
656                (:info
657                ,@(unless (or variable (eq return :tail)) '(arg-locs))
658                ,@(unless variable '(nargs))
659                ,@(when (eq return :fixed) '(nvals))
660                step-instrumenting)
661
662                (:ignore
663                ,@(unless (or variable (eq return :tail)) '(arg-locs))
664                ,@(unless variable '(args)))
665
666                ;; We pass either the fdefn object (for named call) or
667                ;; the actual function object (for unnamed call) in
668                ;; EAX. With named call, closure-tramp will replace it
669                ;; with the real function and invoke the real function
670                ;; for closures. Non-closures do not need this value,
671                ;; so don't care what shows up in it.
672                (:temporary
673                (:sc descriptor-reg
674                     :offset eax-offset
675                     :from (:argument 0)
676                     :to :eval)
677                eax)
678
679                ;; We pass the number of arguments in ECX.
680                (:temporary (:sc unsigned-reg :offset ecx-offset :to :eval) ecx)
681
682                ;; With variable call, we have to load the
683                ;; register-args out of the (new) stack frame before
684                ;; doing the call. Therefore, we have to tell the
685                ;; lifetime stuff that we need to use them.
686                ,@(when variable
687                    (mapcar (lambda (name offset)
688                              `(:temporary (:sc descriptor-reg
689                                                :offset ,offset
690                                                :from (:argument 0)
691                                                :to :eval)
692                                           ,name))
693                            *register-arg-names* *register-arg-offsets*))
694
695                ,@(when (eq return :tail)
696                    '((:temporary (:sc unsigned-reg
697                                       :from (:argument 1)
698                                       :to (:argument 2))
699                                  old-fp-tmp)))
700                ,@(unless (eq return :tail)
701                    '((:node-var node)))
702
703                (:generator ,(+ (if named 5 0)
704                                (if variable 19 1)
705                                (if (eq return :tail) 0 10)
706                                15
707                                (if (eq return :unknown) 25 0))
708                (trace-table-entry trace-table-call-site)
709
710                ;; This has to be done before the frame pointer is
711                ;; changed! EAX stores the 'lexical environment' needed
712                ;; for closures.
713                (move eax fun)
714
715
716                ,@(if variable
717                      ;; For variable call, compute the number of
718                      ;; arguments and move some of the arguments to
719                      ;; registers.
720                      (collect ((noise))
721                               ;; Compute the number of arguments.
722                               (noise '(inst mov ecx new-fp))
723                               (noise '(inst sub ecx esp-tn))
724                               ;; Move the necessary args to registers,
725                               ;; this moves them all even if they are
726                               ;; not all needed.
727                               (loop
728                                for name in *register-arg-names*
729                                for index downfrom -1
730                                do (noise `(loadw ,name new-fp ,index)))
731                               (noise))
732                    '((if (zerop nargs)
733                          (inst xor ecx ecx)
734                        (inst mov ecx (fixnumize nargs)))))
735                ,@(cond ((eq return :tail)
736                         '(;; Python has figured out what frame we should
737                           ;; return to so might as well use that clue.
738                           ;; This seems really important to the
739                           ;; implementation of things like
740                           ;; (without-interrupts ...)
741                           ;;
742                           ;; dtc; Could be doing a tail call from a
743                           ;; known-local-call etc in which the old-fp
744                           ;; or ret-pc are in regs or in non-standard
745                           ;; places. If the passing location were
746                           ;; wired to the stack in standard locations
747                           ;; then these moves will be un-necessary;
748                           ;; this is probably best for the x86.
749                           (sc-case old-fp
750                                    ((control-stack)
751                                     (unless (= ocfp-save-offset
752                                                (tn-offset old-fp))
753                                       ;; FIXME: FORMAT T for stale
754                                       ;; diagnostic output (several of
755                                       ;; them around here), ick
756                                       (error "** tail-call old-fp not S0~%")
757                                       (move old-fp-tmp old-fp)
758                                       (storew old-fp-tmp
759                                               ebp-tn
760                                               (frame-word-offset ocfp-save-offset))))
761                                    ((any-reg descriptor-reg)
762                                     (error "** tail-call old-fp in reg not S0~%")
763                                     (storew old-fp
764                                             ebp-tn
765                                             (frame-word-offset ocfp-save-offset))))
766
767                           ;; For tail call, we have to push the
768                           ;; return-pc so that it looks like we CALLed
769                           ;; despite the fact that we are going to JMP.
770                           (inst push return-pc)
771                           ))
772                        (t
773                         ;; For non-tail call, we have to save our
774                         ;; frame pointer and install the new frame
775                         ;; pointer. We can't load stack tns after this
776                         ;; point.
777                         `(;; Python doesn't seem to allocate a frame
778                           ;; here which doesn't leave room for the
779                           ;; ofp/ret stuff.
780
781                           ;; The variable args are on the stack and
782                           ;; become the frame, but there may be <3
783                           ;; args and 3 stack slots are assumed
784                           ;; allocate on the call. So need to ensure
785                           ;; there are at least 3 slots. This hack
786                           ;; just adds 3 more.
787                           ,(if variable
788                                '(inst sub esp-tn (fixnumize 3)))
789
790                           ;; Bias the new-fp for use as an fp
791                           ,(if variable
792                                '(inst sub new-fp (fixnumize sp->fp-offset)))
793
794                           ;; Save the fp
795                           (storew ebp-tn new-fp
796                                   (frame-word-offset ocfp-save-offset))
797
798                           (move ebp-tn new-fp) ; NB - now on new stack frame.
799                           )))
800
801                (when step-instrumenting
802                  (emit-single-step-test)
803                  (inst jmp :eq DONE)
804                  (inst break single-step-around-trap))
805                DONE
806
807                (note-this-location vop :call-site)
808
809                (inst ,(if (eq return :tail) 'jmp 'call)
810                      ,(if named
811                           '(make-ea-for-object-slot eax fdefn-raw-addr-slot
812                                                     other-pointer-lowtag)
813                           '(make-ea-for-object-slot eax closure-fun-slot
814                                                     fun-pointer-lowtag)))
815                ,@(ecase return
816                    (:fixed
817                     '((default-unknown-values vop values nvals node)))
818                    (:unknown
819                     '((note-this-location vop :unknown-return)
820                       (receive-unknown-values values-start nvals start count
821                                               node)))
822                    (:tail))
823                (trace-table-entry trace-table-normal)))))
824
825   (define-full-call call nil :fixed nil)
826   (define-full-call call-named t :fixed nil)
827   (define-full-call multiple-call nil :unknown nil)
828   (define-full-call multiple-call-named t :unknown nil)
829   (define-full-call tail-call nil :tail nil)
830   (define-full-call tail-call-named t :tail nil)
831
832   (define-full-call call-variable nil :fixed t)
833   (define-full-call multiple-call-variable nil :unknown t))
834
835 ;;; This is defined separately, since it needs special code that BLT's
836 ;;; the arguments down. All the real work is done in the assembly
837 ;;; routine. We just set things up so that it can find what it needs.
838 (define-vop (tail-call-variable)
839   (:args (args :scs (any-reg control-stack) :target esi)
840          (function :scs (descriptor-reg control-stack) :target eax)
841          (old-fp)
842          (return-pc))
843   (:temporary (:sc unsigned-reg :offset esi-offset :from (:argument 0)) esi)
844   (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 1)) eax)
845   (:generator 75
846     (check-ocfp-and-return-pc old-fp return-pc)
847     ;; Move these into the passing locations if they are not already there.
848     (move esi args)
849     (move eax function)
850     ;; And jump to the assembly routine.
851     (inst jmp (make-fixup 'tail-call-variable :assembly-routine))))
852 \f
853 ;;;; unknown values return
854
855 ;;; Return a single-value using the Unknown-Values convention.
856 ;;;
857 ;;; pfw--get wired-tn conflicts sometimes if register sc specd for args
858 ;;; having problems targeting args to regs -- using temps instead.
859 ;;;
860 ;;; First off, modifying the return-pc defeats the branch-prediction
861 ;;; optimizations on modern CPUs quite handily. Second, we can do all
862 ;;; this without needing a temp register. Fixed the latter, at least.
863 ;;; -- AB 2006/Feb/04
864 (define-vop (return-single)
865   (:args (old-fp)
866          (return-pc)
867          (value))
868   (:ignore value)
869   (:generator 6
870     (check-ocfp-and-return-pc old-fp return-pc)
871     (trace-table-entry trace-table-fun-epilogue)
872     ;; Drop stack above old-fp
873     (inst mov esp-tn ebp-tn)
874     ;; Clear the multiple-value return flag
875     (inst clc)
876     ;; Restore the old frame pointer
877     (inst pop ebp-tn)
878     ;; And return.
879     (inst ret)))
880
881 ;;; Do unknown-values return of a fixed (other than 1) number of
882 ;;; values. The VALUES are required to be set up in the standard
883 ;;; passing locations. NVALS is the number of values returned.
884 ;;;
885 ;;; Basically, we just load ECX with the number of values returned and
886 ;;; EBX with a pointer to the values, set ESP to point to the end of
887 ;;; the values, and jump directly to return-pc.
888 (define-vop (return)
889   (:args (old-fp)
890          (return-pc :to (:eval 1))
891          (values :more t))
892   (:ignore values)
893   (:info nvals)
894   ;; In the case of other than one value, we need these registers to
895   ;; tell the caller where they are and how many there are.
896   (:temporary (:sc unsigned-reg :offset ebx-offset) ebx)
897   (:temporary (:sc unsigned-reg :offset ecx-offset) ecx)
898   ;; We need to stretch the lifetime of return-pc past the argument
899   ;; registers so that we can default the argument registers without
900   ;; trashing return-pc.
901   (:temporary (:sc unsigned-reg :offset (first *register-arg-offsets*)
902                    :from :eval) a0)
903   (:temporary (:sc unsigned-reg :offset (second *register-arg-offsets*)
904                    :from :eval) a1)
905   (:temporary (:sc unsigned-reg :offset (third *register-arg-offsets*)
906                    :from :eval) a2)
907
908   (:generator 6
909     (check-ocfp-and-return-pc old-fp return-pc)
910     (when (= nvals 1)
911       ;; This is handled in RETURN-SINGLE.
912       (error "nvalues is 1"))
913     (trace-table-entry trace-table-fun-epilogue)
914     ;; Establish the values pointer and values count.
915     (inst lea ebx (make-ea :dword :base ebp-tn
916                            :disp (* sp->fp-offset n-word-bytes)))
917     (if (zerop nvals)
918         (inst xor ecx ecx)              ; smaller
919         (inst mov ecx (fixnumize nvals)))
920     ;; Pre-default any argument register that need it.
921     (when (< nvals register-arg-count)
922       (let* ((arg-tns (nthcdr nvals (list a0 a1 a2)))
923              (first (first arg-tns)))
924         (inst mov first nil-value)
925         (dolist (tn (cdr arg-tns))
926           (inst mov tn first))))
927     ;; Set the multiple value return flag.
928     (inst stc)
929     ;; And away we go. Except that return-pc is still on the
930     ;; stack and we've changed the stack pointer. So we have to
931     ;; tell it to index off of EBX instead of EBP.
932     (cond ((<= nvals register-arg-count)
933            (inst mov esp-tn ebp-tn)
934            (inst pop ebp-tn)
935            (inst ret))
936           (t
937            ;; Some values are on the stack after RETURN-PC and OLD-FP,
938            ;; can't return normally and some slots of the frame will
939            ;; be used as temporaries by the receiver.
940            ;;
941            ;; Clear as much of the stack as possible, but not past the
942            ;; old frame address.
943            (inst lea esp-tn
944                  (make-ea :dword :base ebp-tn
945                           :disp (frame-byte-offset (1- nvals))))
946            (move ebp-tn old-fp)
947            (inst push (make-ea :dword :base ebx
948                                :disp (frame-byte-offset
949                                       (+ sp->fp-offset
950                                          (tn-offset return-pc)))))
951            (inst ret)))
952
953     (trace-table-entry trace-table-normal)))
954
955 ;;; Do unknown-values return of an arbitrary number of values (passed
956 ;;; on the stack.) We check for the common case of a single return
957 ;;; value, and do that inline using the normal single value return
958 ;;; convention. Otherwise, we branch off to code that calls an
959 ;;; assembly-routine.
960 ;;;
961 ;;; The assembly routine takes the following args:
962 ;;;  ECX -- number of values to find there.
963 ;;;  ESI -- pointer to where to find the values.
964 (define-vop (return-multiple)
965   (:args (old-fp)
966          (return-pc)
967          (vals :scs (any-reg) :target esi)
968          (nvals :scs (any-reg) :target ecx))
969   (:temporary (:sc unsigned-reg :offset esi-offset :from (:argument 2)) esi)
970   (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 3)) ecx)
971   (:temporary (:sc descriptor-reg :offset (first *register-arg-offsets*)
972                    :from (:eval 0)) a0)
973   (:node-var node)
974   (:generator 13
975     (check-ocfp-and-return-pc old-fp return-pc)
976     (trace-table-entry trace-table-fun-epilogue)
977     (unless (policy node (> space speed))
978       ;; Check for the single case.
979       (let ((not-single (gen-label)))
980         (inst cmp nvals (fixnumize 1))
981         (inst jmp :ne not-single)
982         ;; Return with one value.
983         (loadw a0 vals -1)
984         ;; Clear the stack until ocfp.
985         (inst mov esp-tn ebp-tn)
986         ;; clear the multiple-value return flag
987         (inst clc)
988         ;; Out of here.
989         (inst pop ebp-tn)
990         (inst ret)
991         ;; Nope, not the single case. Jump to the assembly routine.
992         (emit-label not-single)))
993     (move esi vals)
994     (move ecx nvals)
995     (inst jmp (make-fixup 'return-multiple :assembly-routine))
996     (trace-table-entry trace-table-normal)))
997 \f
998 ;;;; XEP hackery
999
1000 ;;; We don't need to do anything special for regular functions.
1001 (define-vop (setup-environment)
1002   (:info label)
1003   (:ignore label)
1004   (:generator 0
1005     ;; Don't bother doing anything.
1006     nil))
1007
1008 ;;; Get the lexical environment from its passing location.
1009 (define-vop (setup-closure-environment)
1010   (:results (closure :scs (descriptor-reg)))
1011   (:info label)
1012   (:ignore label)
1013   (:generator 6
1014     ;; Get result.
1015     (move closure eax-tn)))
1016
1017 ;;; Copy a &MORE arg from the argument area to the end of the current
1018 ;;; frame. FIXED is the number of non-&MORE arguments.
1019 ;;;
1020 ;;; The tricky part is doing this without trashing any of the calling
1021 ;;; convention registers that are still needed. This vop is emitted
1022 ;;; directly after the xep-allocate frame. That means the registers
1023 ;;; are in use as follows:
1024 ;;;
1025 ;;;  EAX -- The lexenv.
1026 ;;;  EBX -- Available.
1027 ;;;  ECX -- The total number of arguments * N-WORD-BYTES.
1028 ;;;  EDX -- The first arg.
1029 ;;;  EDI -- The second arg.
1030 ;;;  ESI -- The third arg.
1031 ;;;
1032 ;;; So basically, we have one register available for our use: EBX.
1033 ;;;
1034 ;;; What we can do is push the other regs onto the stack, and then
1035 ;;; restore their values by looking directly below where we put the
1036 ;;; more-args.
1037 (define-vop (copy-more-arg)
1038   (:info fixed)
1039   (:generator 20
1040     ;; Avoid the copy if there are no more args.
1041     (cond ((zerop fixed)
1042            (inst jecxz JUST-ALLOC-FRAME))
1043           (t
1044            (inst cmp ecx-tn (fixnumize fixed))
1045            (inst jmp :be JUST-ALLOC-FRAME)))
1046
1047     ;; Allocate the space on the stack.
1048     ;; stack = ebp + sp->fp-offset - (max 3 frame-size) - (nargs - fixed)
1049     (inst lea ebx-tn
1050           (make-ea :dword :base ebp-tn
1051                    :disp (* n-word-bytes
1052                             (- (+ sp->fp-offset fixed)
1053                                (max 3 (sb-allocated-size 'stack))))))
1054     (inst sub ebx-tn ecx-tn)  ; Got the new stack in ebx
1055     (inst mov esp-tn ebx-tn)
1056
1057     ;; Now: nargs>=1 && nargs>fixed
1058
1059     ;; Save the original count of args.
1060     (inst mov ebx-tn ecx-tn)
1061
1062     (cond ((< fixed register-arg-count)
1063            ;; We must stop when we run out of stack args, not when we
1064            ;; run out of more args.
1065            ;; Number to copy = nargs-3
1066            (inst sub ecx-tn (fixnumize register-arg-count))
1067            ;; Everything of interest in registers.
1068            (inst jmp :be DO-REGS))
1069           (t
1070            ;; Number to copy = nargs-fixed
1071            (inst sub ecx-tn (fixnumize fixed))))
1072
1073     ;; Save edi and esi register args.
1074     (inst push edi-tn)
1075     (inst push esi-tn)
1076     (inst push ebx-tn)
1077     ;; Okay, we have pushed the register args. We can trash them
1078     ;; now.
1079
1080     ;; Initialize src to be end of args.
1081     (inst lea esi-tn (make-ea :dword :base ebp-tn
1082                               :disp (* sp->fp-offset n-word-bytes)))
1083     (inst sub esi-tn ebx-tn)
1084
1085     ;; We need to copy from downwards up to avoid overwriting some of
1086     ;; the yet uncopied args. So we need to use EBX as the copy index
1087     ;; and ECX as the loop counter, rather than using ECX for both.
1088     (inst xor ebx-tn ebx-tn)
1089
1090     ;; We used to use REP MOVS here, but on modern x86 it performs
1091     ;; much worse than an explicit loop for small blocks.
1092     COPY-LOOP
1093     (inst mov edi-tn (make-ea :dword :base esi-tn :index ebx-tn))
1094     ;; The :DISP is to account for the registers saved on the stack
1095     (inst mov (make-ea :dword :base esp-tn :disp (* 3 n-word-bytes)
1096                        :index ebx-tn)
1097           edi-tn)
1098     (inst add ebx-tn n-word-bytes)
1099     (inst sub ecx-tn n-word-bytes)
1100     (inst jmp :nz COPY-LOOP)
1101
1102     ;; So now we need to restore EDI and ESI.
1103     (inst pop ebx-tn)
1104     (inst pop esi-tn)
1105     (inst pop edi-tn)
1106
1107     DO-REGS
1108
1109     ;; Restore ECX
1110     (inst mov ecx-tn ebx-tn)
1111
1112     ;; Here: nargs>=1 && nargs>fixed
1113     (when (< fixed register-arg-count)
1114       ;; Now we have to deposit any more args that showed up in
1115       ;; registers.
1116       (do ((i fixed))
1117           ( nil )
1118         ;; Store it relative to ebp
1119         (inst mov (make-ea :dword :base ebp-tn
1120                            :disp (* n-word-bytes
1121                                     (- sp->fp-offset
1122                                        (+ 1
1123                                           (- i fixed)
1124                                           (max 3 (sb-allocated-size
1125                                                   'stack))))))
1126               (nth i *register-arg-tns*))
1127
1128         (incf i)
1129         (when (>= i register-arg-count)
1130           (return))
1131
1132         ;; Don't deposit any more than there are.
1133         (if (zerop i)
1134             (inst test ecx-tn ecx-tn)
1135             (inst cmp ecx-tn (fixnumize i)))
1136         (inst jmp :eq DONE)))
1137
1138     (inst jmp DONE)
1139
1140     JUST-ALLOC-FRAME
1141     (inst lea esp-tn
1142           (make-ea :dword :base ebp-tn
1143                    :disp (* n-word-bytes
1144                             (- sp->fp-offset
1145                                (max 3 (sb-allocated-size 'stack))))))
1146
1147     DONE))
1148
1149 (define-vop (more-kw-arg)
1150   (:translate sb!c::%more-kw-arg)
1151   (:policy :fast-safe)
1152   (:args (object :scs (descriptor-reg) :to (:result 1))
1153          (index :scs (any-reg immediate) :to (:result 1) :target keyword))
1154   (:arg-types * tagged-num)
1155   (:results (value :scs (descriptor-reg any-reg))
1156             (keyword :scs (descriptor-reg any-reg)))
1157   (:result-types * *)
1158   (:generator 4
1159     (sc-case index
1160       (immediate
1161        (inst mov value (make-ea :dword :base object :disp (tn-value index)))
1162        (inst mov keyword (make-ea :dword :base object
1163                                   :disp (+ (tn-value index) n-word-bytes))))
1164       (t
1165        (inst mov value (make-ea :dword :base object :index index))
1166        (inst mov keyword (make-ea :dword :base object :index index
1167                                   :disp n-word-bytes))))))
1168
1169 (define-vop (more-arg)
1170     (:translate sb!c::%more-arg)
1171   (:policy :fast-safe)
1172   (:args (object :scs (descriptor-reg) :to (:result 1))
1173          (index :scs (any-reg) :to (:result 1) :target value))
1174   (:arg-types * tagged-num)
1175   (:results (value :scs (descriptor-reg any-reg)))
1176   (:result-types *)
1177   (:generator 4
1178     (move value index)
1179     (inst neg value)
1180     (inst mov value (make-ea :dword :base object :index value))))
1181
1182 ;;; Turn more arg (context, count) into a list.
1183 (define-vop (listify-rest-args)
1184   (:translate %listify-rest-args)
1185   (:policy :safe)
1186   (:args (context :scs (descriptor-reg) :target src)
1187          (count :scs (any-reg) :target ecx))
1188   (:arg-types * tagged-num)
1189   (:temporary (:sc unsigned-reg :offset esi-offset :from (:argument 0)) src)
1190   (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 1)) ecx)
1191   (:temporary (:sc unsigned-reg :offset eax-offset) eax)
1192   (:temporary (:sc unsigned-reg) dst)
1193   (:results (result :scs (descriptor-reg)))
1194   (:node-var node)
1195   (:generator 20
1196     (let ((enter (gen-label))
1197           (loop (gen-label))
1198           (done (gen-label))
1199           (stack-allocate-p (node-stack-allocate-p node)))
1200       (move src context)
1201       (move ecx count)
1202       ;; Check to see whether there are no args, and just return NIL if so.
1203       (inst mov result nil-value)
1204       (inst jecxz done)
1205       (inst lea dst (make-ea :dword :base ecx :index ecx))
1206       (maybe-pseudo-atomic stack-allocate-p
1207        (allocation dst dst node stack-allocate-p list-pointer-lowtag)
1208        ;; Set decrement mode (successive args at lower addresses)
1209        (inst std)
1210        ;; Set up the result.
1211        (move result dst)
1212        ;; Jump into the middle of the loop, 'cause that's where we want
1213        ;; to start.
1214        (inst jmp enter)
1215        (emit-label loop)
1216        ;; Compute a pointer to the next cons.
1217        (inst add dst (* cons-size n-word-bytes))
1218        ;; Store a pointer to this cons in the CDR of the previous cons.
1219        (storew dst dst -1 list-pointer-lowtag)
1220        (emit-label enter)
1221        ;; Grab one value and stash it in the car of this cons.
1222        (inst lods eax)
1223        (storew eax dst 0 list-pointer-lowtag)
1224        ;; Go back for more.
1225        (inst sub ecx n-word-bytes)
1226        (inst jmp :nz loop)
1227        ;; NIL out the last cons.
1228        (storew nil-value dst 1 list-pointer-lowtag)
1229        (inst cld))
1230       (emit-label done))))
1231
1232 ;;; Return the location and size of the &MORE arg glob created by
1233 ;;; COPY-MORE-ARG. SUPPLIED is the total number of arguments supplied
1234 ;;; (originally passed in ECX). FIXED is the number of non-rest
1235 ;;; arguments.
1236 ;;;
1237 ;;; We must duplicate some of the work done by COPY-MORE-ARG, since at
1238 ;;; that time the environment is in a pretty brain-damaged state,
1239 ;;; preventing this info from being returned as values. What we do is
1240 ;;; compute supplied - fixed, and return a pointer that many words
1241 ;;; below the current stack top.
1242 (define-vop (more-arg-context)
1243   (:policy :fast-safe)
1244   (:translate sb!c::%more-arg-context)
1245   (:args (supplied :scs (any-reg) :target count))
1246   (:arg-types positive-fixnum (:constant fixnum))
1247   (:info fixed)
1248   (:results (context :scs (descriptor-reg))
1249             (count :scs (any-reg)))
1250   (:result-types t tagged-num)
1251   (:note "more-arg-context")
1252   (:generator 5
1253     (move count supplied)
1254     ;; SP at this point points at the last arg pushed.
1255     ;; Point to the first more-arg, not above it.
1256     (inst lea context (make-ea :dword :base esp-tn
1257                                :index count :scale 1
1258                                :disp (- (+ (fixnumize fixed) n-word-bytes))))
1259     (unless (zerop fixed)
1260       (inst sub count (fixnumize fixed)))))
1261
1262 ;;; Signal wrong argument count error if NARGS isn't equal to COUNT.
1263 (define-vop (verify-arg-count)
1264   (:policy :fast-safe)
1265   (:translate sb!c::%verify-arg-count)
1266   (:args (nargs :scs (any-reg)))
1267   (:arg-types positive-fixnum (:constant t))
1268   (:info count)
1269   (:vop-var vop)
1270   (:save-p :compute-only)
1271   (:generator 3
1272     (let ((err-lab
1273            (generate-error-code vop 'invalid-arg-count-error nargs)))
1274       (if (zerop count)
1275           (inst test nargs nargs)  ; smaller instruction
1276         (inst cmp nargs (fixnumize count)))
1277       (inst jmp :ne err-lab))))
1278
1279 ;;; Various other error signallers.
1280 (macrolet ((def (name error translate &rest args)
1281              `(define-vop (,name)
1282                 ,@(when translate
1283                     `((:policy :fast-safe)
1284                       (:translate ,translate)))
1285                 (:args ,@(mapcar (lambda (arg)
1286                                    `(,arg :scs (any-reg descriptor-reg)))
1287                                  args))
1288                 (:vop-var vop)
1289                 (:save-p :compute-only)
1290                 (:generator 1000
1291                   (error-call vop ',error ,@args)))))
1292   (def arg-count-error invalid-arg-count-error
1293     sb!c::%arg-count-error nargs)
1294   (def type-check-error object-not-type-error sb!c::%type-check-error
1295     object type)
1296   (def layout-invalid-error layout-invalid-error sb!c::%layout-invalid-error
1297     object layout)
1298   (def odd-key-args-error odd-key-args-error
1299     sb!c::%odd-key-args-error)
1300   (def unknown-key-arg-error unknown-key-arg-error
1301     sb!c::%unknown-key-arg-error key)
1302   (def nil-fun-returned-error nil-fun-returned-error nil fun))
1303
1304 ;;; Single-stepping
1305
1306 (defun emit-single-step-test ()
1307   ;; We use different ways of representing whether stepping is on on
1308   ;; +SB-THREAD / -SB-THREAD: on +SB-THREAD, we use a slot in the
1309   ;; thread structure. On -SB-THREAD we use the value of a static
1310   ;; symbol. Things are done this way, since reading a thread-local
1311   ;; slot from a symbol would require an extra register on +SB-THREAD,
1312   ;; and reading a slot from a thread structure would require an extra
1313   ;; register on -SB-THREAD.
1314   #!+sb-thread
1315   (progn
1316     (inst cmp (make-ea :dword
1317                        :disp (* thread-stepping-slot n-word-bytes))
1318           nil-value :fs))
1319   #!-sb-thread
1320   (inst cmp (make-ea-for-symbol-value sb!impl::*stepping*)
1321         nil-value))
1322
1323 (define-vop (step-instrument-before-vop)
1324   (:policy :fast-safe)
1325   (:vop-var vop)
1326   (:generator 3
1327      (emit-single-step-test)
1328      (inst jmp :eq DONE)
1329      (inst break single-step-before-trap)
1330      DONE
1331      (note-this-location vop :step-before-vop)))