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