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