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