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