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