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