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