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