Enable debugger to work on constants encoded in break arguments.
[sbcl.git] / src / compiler / x86-64 / call.lisp
blobdb1ceee74b78c626c578c30c9132402130f60812
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 (defun 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 (defun standard-arg-location-sc (n)
26 (declare (type unsigned-byte n))
27 (if (< n register-arg-count)
28 (make-sc-offset descriptor-reg-sc-number
29 (nth n *register-arg-offsets*))
30 (make-sc-offset control-stack-sc-number n)))
32 (defconstant arg-count-sc (make-sc-offset any-reg-sc-number rcx-offset))
33 (defconstant closure-sc (make-sc-offset any-reg-sc-number rax-offset))
35 ;;; Make a passing location TN for a local call return PC.
36 ;;;
37 ;;; Always wire the return PC location to the stack in its standard
38 ;;; location.
39 (defun make-return-pc-passing-location (standard)
40 (declare (ignore standard))
41 (make-wired-tn (primitive-type-or-lose 'system-area-pointer)
42 sap-stack-sc-number return-pc-save-offset))
44 (defconstant return-pc-passing-offset
45 (make-sc-offset sap-stack-sc-number return-pc-save-offset))
47 ;;; This is similar to MAKE-RETURN-PC-PASSING-LOCATION, but makes a
48 ;;; location to pass OLD-FP in.
49 ;;;
50 ;;; This is wired in both the standard and the local-call conventions,
51 ;;; because we want to be able to assume it's always there. Besides,
52 ;;; the x86 doesn't have enough registers to really make it profitable
53 ;;; to pass it in a register.
54 (defun make-old-fp-passing-location (standard)
55 (declare (ignore standard))
56 (make-wired-tn *fixnum-primitive-type* control-stack-sc-number
57 ocfp-save-offset))
59 (defconstant old-fp-passing-offset
60 (make-sc-offset control-stack-sc-number ocfp-save-offset))
62 ;;; Make the TNs used to hold OLD-FP and RETURN-PC within the current
63 ;;; function. We treat these specially so that the debugger can find
64 ;;; them at a known location.
65 ;;;
66 ;;; Without using a save-tn - which does not make much sense if it is
67 ;;; wired to the stack?
68 (defun make-old-fp-save-location (physenv)
69 (physenv-debug-live-tn (make-wired-tn *fixnum-primitive-type*
70 control-stack-sc-number
71 ocfp-save-offset)
72 physenv))
73 (defun make-return-pc-save-location (physenv)
74 (physenv-debug-live-tn
75 (make-wired-tn (primitive-type-or-lose 'system-area-pointer)
76 sap-stack-sc-number return-pc-save-offset)
77 physenv))
79 ;;; Make a TN for the standard argument count passing location. We only
80 ;;; need to make the standard location, since a count is never passed when we
81 ;;; are using non-standard conventions.
82 (defun make-arg-count-location ()
83 (make-wired-tn *fixnum-primitive-type* any-reg-sc-number rcx-offset))
85 ;;; Make a TN to hold the number-stack frame pointer. This is allocated
86 ;;; once per component, and is component-live.
87 (defun make-nfp-tn ()
88 (make-restricted-tn *fixnum-primitive-type* ignore-me-sc-number))
90 (defun make-stack-pointer-tn ()
91 (make-normal-tn *fixnum-primitive-type*))
93 (defun make-number-stack-pointer-tn ()
94 (make-restricted-tn *fixnum-primitive-type* ignore-me-sc-number))
96 ;;; Return a list of TNs that can be used to represent an unknown-values
97 ;;; continuation within a function.
98 (defun make-unknown-values-locations ()
99 (list (make-stack-pointer-tn)
100 (make-normal-tn *fixnum-primitive-type*)))
102 ;;; This function is called by the ENTRY-ANALYZE phase, allowing
103 ;;; VM-dependent initialization of the IR2-COMPONENT structure. We
104 ;;; push placeholder entries in the CONSTANTS to leave room for
105 ;;; additional noise in the code object header.
106 (defun select-component-format (component)
107 (declare (type component component))
108 (dotimes (i code-constants-offset)
109 (vector-push-extend nil
110 (ir2-component-constants (component-info component))))
111 (values))
113 ;;;; frame hackery
115 ;;; This is used for setting up the Old-FP in local call.
116 (define-vop (current-fp)
117 (:results (val :scs (any-reg control-stack)))
118 (:generator 1
119 (move val rbp-tn)))
121 ;;; We don't have a separate NFP, so we don't need to do anything here.
122 (define-vop (compute-old-nfp)
123 (:results (val))
124 (:ignore val)
125 (:generator 1
126 nil))
128 ;;; Accessing a slot from an earlier stack frame is definite hackery.
129 (define-vop (ancestor-frame-ref)
130 (:args (frame-pointer :scs (descriptor-reg))
131 (variable-home-tn :load-if nil))
132 (:results (value :scs (descriptor-reg any-reg)))
133 (:policy :fast-safe)
134 (:generator 4
135 (aver (sc-is variable-home-tn control-stack))
136 (loadw value frame-pointer
137 (frame-word-offset (tn-offset variable-home-tn)))))
138 (define-vop (ancestor-frame-set)
139 (:args (frame-pointer :scs (descriptor-reg))
140 (value :scs (descriptor-reg any-reg)))
141 (:results (variable-home-tn :load-if nil))
142 (:policy :fast-safe)
143 (:generator 4
144 (aver (sc-is variable-home-tn control-stack))
145 (storew value frame-pointer
146 (frame-word-offset (tn-offset variable-home-tn)))))
148 (macrolet ((define-frame-op
149 (suffix sc stack-sc instruction
150 &optional (ea
151 `(make-ea :qword
152 :base frame-pointer
153 :disp (frame-byte-offset
154 (tn-offset variable-home-tn)))))
155 (let ((reffer (symbolicate 'ancestor-frame-ref '/ suffix))
156 (setter (symbolicate 'ancestor-frame-set '/ suffix)))
157 `(progn
158 (define-vop (,reffer ancestor-frame-ref)
159 (:results (value :scs (,sc)))
160 (:generator 4
161 (aver (sc-is variable-home-tn ,stack-sc))
162 (inst ,instruction value
163 ,ea)))
164 (define-vop (,setter ancestor-frame-set)
165 (:args (frame-pointer :scs (descriptor-reg))
166 (value :scs (,sc)))
167 (:generator 4
168 (aver (sc-is variable-home-tn ,stack-sc))
169 (inst ,instruction ,ea value)))))))
170 (define-frame-op double-float double-reg double-stack movsd)
171 (define-frame-op single-float single-reg single-stack movss)
172 (define-frame-op complex-double-float complex-double-reg complex-double-stack
173 movupd (ea-for-cdf-data-stack variable-home-tn frame-pointer))
174 (define-frame-op complex-single-float complex-single-reg complex-single-stack
175 movq (ea-for-csf-data-stack variable-home-tn frame-pointer))
176 (define-frame-op signed-byte-64 signed-reg signed-stack mov)
177 (define-frame-op unsigned-byte-64 unsigned-reg unsigned-stack mov)
178 (define-frame-op system-area-pointer sap-reg sap-stack mov))
180 (defun primitive-type-indirect-cell-type (ptype)
181 (declare (type primitive-type ptype))
182 (macrolet ((foo (&body data)
183 `(case (primitive-type-name ptype)
184 ,@(loop for (name stack-sc ref set) in data
185 collect
186 `(,name
187 (load-time-value
188 (list (primitive-type-or-lose ',name)
189 (sc-or-lose ',stack-sc)
190 (lambda (node block fp value res)
191 (sb!c::vop ,ref node block
192 fp value res))
193 (lambda (node block fp new-val value)
194 (sb!c::vop ,set node block
195 fp new-val value)))))))))
196 (foo (double-float double-stack
197 ancestor-frame-ref/double-float
198 ancestor-frame-set/double-float)
199 (single-float single-stack
200 ancestor-frame-ref/single-float
201 ancestor-frame-set/single-float)
202 (complex-double-float complex-double-stack
203 ancestor-frame-ref/complex-double-float
204 ancestor-frame-set/complex-double-float)
205 (complex-single-float complex-single-stack
206 ancestor-frame-ref/complex-single-float
207 ancestor-frame-set/complex-single-float)
208 (signed-byte-64 signed-stack
209 ancestor-frame-ref/signed-byte-64
210 ancestor-frame-set/signed-byte-64)
211 (unsigned-byte-64 unsigned-stack
212 ancestor-frame-ref/unsigned-byte-64
213 ancestor-frame-set/unsigned-byte-64)
214 (unsigned-byte-63 unsigned-stack
215 ancestor-frame-ref/unsigned-byte-64
216 ancestor-frame-set/unsigned-byte-64)
217 (system-area-pointer sap-stack
218 ancestor-frame-ref/system-area-pointer
219 ancestor-frame-set/system-area-pointer))))
221 (define-vop (xep-allocate-frame)
222 (:info start-lab)
223 (:generator 1
224 (emit-alignment n-lowtag-bits)
225 (emit-label start-lab)
226 ;; Skip space for the function header.
227 (inst simple-fun-header-word)
228 (dotimes (i (* n-word-bytes (1- simple-fun-code-offset)))
229 (inst byte 0))
231 ;; The start of the actual code.
232 ;; Save the return-pc.
233 (popw rbp-tn (frame-word-offset return-pc-save-offset))))
235 (define-vop (xep-setup-sp)
236 (:generator 1
237 (inst lea rsp-tn
238 (make-ea :qword :base rbp-tn
239 :disp (- (* n-word-bytes
240 (- (max 3 (sb-allocated-size 'stack))
241 sp->fp-offset)))))))
243 ;;; This is emitted directly before either a known-call-local, call-local,
244 ;;; or a multiple-call-local. All it does is allocate stack space for the
245 ;;; callee (who has the same size stack as us).
246 (define-vop (allocate-frame)
247 (:results (res :scs (any-reg))
248 (nfp))
249 (:info callee)
250 (:ignore nfp callee)
251 (:generator 2
252 (inst lea res (make-ea :qword :base rsp-tn
253 :disp (- (* sp->fp-offset n-word-bytes))))
254 (inst sub rsp-tn (* n-word-bytes (sb-allocated-size 'stack)))))
256 ;;; Allocate a partial frame for passing stack arguments in a full
257 ;;; call. NARGS is the number of arguments passed. We allocate at
258 ;;; least 3 slots, because the XEP noise is going to want to use them
259 ;;; before it can extend the stack.
260 (define-vop (allocate-full-call-frame)
261 (:info nargs)
262 (:results (res :scs (any-reg)))
263 (:generator 2
264 (inst lea res (make-ea :qword :base rsp-tn
265 :disp (- (* sp->fp-offset n-word-bytes))))
266 (inst sub rsp-tn (* (max nargs 3) n-word-bytes))))
268 ;;; Emit code needed at the return-point from an unknown-values call
269 ;;; for a fixed number of values. Values is the head of the TN-REF
270 ;;; list for the locations that the values are to be received into.
271 ;;; Nvals is the number of values that are to be received (should
272 ;;; equal the length of Values).
274 ;;; If 0 or 1 values are expected, then we just emit an instruction to
275 ;;; reset the SP (which will only be executed when other than 1 value
276 ;;; is returned.)
278 ;;; In the general case we have to do three things:
279 ;;; -- Default unsupplied register values. This need only be done
280 ;;; when a single value is returned, since register values are
281 ;;; defaulted by the called in the non-single case.
282 ;;; -- Default unsupplied stack values. This needs to be done whenever
283 ;;; there are stack values.
284 ;;; -- Reset SP. This must be done whenever other than 1 value is
285 ;;; returned, regardless of the number of values desired.
286 (defun default-unknown-values (vop values nvals node)
287 (declare (type (or tn-ref null) values)
288 (type unsigned-byte nvals))
289 (let ((type (sb!c::basic-combination-derived-type node)))
290 (cond
291 ((<= nvals 1)
292 (note-this-location vop :single-value-return)
293 (cond
294 ((<= (sb!kernel:values-type-max-value-count type)
295 register-arg-count)
296 (when (and (named-type-p type)
297 (eq nil (named-type-name type)))
298 ;; The function never returns, it may happen that the code
299 ;; ends right here leavig the :SINGLE-VALUE-RETURN note
300 ;; dangling. Let's emit a NOP.
301 (inst nop)))
302 ((not (sb!kernel:values-type-may-be-single-value-p type))
303 (inst mov rsp-tn rbx-tn))
305 (inst cmov :c rsp-tn rbx-tn))))
306 ((<= nvals register-arg-count)
307 (note-this-location vop :unknown-return)
308 (when (sb!kernel:values-type-may-be-single-value-p type)
309 (let ((regs-defaulted (gen-label)))
310 (inst jmp :c regs-defaulted)
311 ;; Default the unsupplied registers.
312 (let* ((2nd-tn-ref (tn-ref-across values))
313 (2nd-tn (tn-ref-tn 2nd-tn-ref)))
314 (inst mov 2nd-tn nil-value)
315 (when (> nvals 2)
316 (loop
317 for tn-ref = (tn-ref-across 2nd-tn-ref)
318 then (tn-ref-across tn-ref)
319 for count from 2 below register-arg-count
320 do (inst mov (tn-ref-tn tn-ref) 2nd-tn))))
321 (inst mov rbx-tn rsp-tn)
322 (emit-label regs-defaulted)))
323 (when (< register-arg-count
324 (sb!kernel:values-type-max-value-count type))
325 (inst mov rsp-tn rbx-tn)))
326 ((<= nvals 7)
327 ;; The number of bytes depends on the relative jump instructions.
328 ;; Best case is 31+(n-3)*14, worst case is 35+(n-3)*18. For
329 ;; NVALS=6 that is 73/89 bytes, and for NVALS=7 that is 87/107
330 ;; bytes which is likely better than using the blt below.
331 (let ((regs-defaulted (gen-label))
332 (defaulting-done (gen-label))
333 (default-stack-slots (gen-label)))
334 (note-this-location vop :unknown-return)
335 ;; Branch off to the MV case.
336 (inst jmp :c regs-defaulted)
337 ;; Do the single value case.
338 ;; Default the register args
339 (inst mov rax-tn nil-value)
340 (do ((i 1 (1+ i))
341 (val (tn-ref-across values) (tn-ref-across val)))
342 ((= i (min nvals register-arg-count)))
343 (inst mov (tn-ref-tn val) rax-tn))
344 ;; Fake other registers so it looks like we returned with all the
345 ;; registers filled in.
346 (move rbx-tn rsp-tn)
347 (inst jmp default-stack-slots)
348 (emit-label regs-defaulted)
349 (inst mov rax-tn nil-value)
350 (collect ((defaults))
351 (do ((i register-arg-count (1+ i))
352 (val (do ((i 0 (1+ i))
353 (val values (tn-ref-across val)))
354 ((= i register-arg-count) val))
355 (tn-ref-across val)))
356 ((null val))
357 (let ((default-lab (gen-label))
358 (tn (tn-ref-tn val))
359 (first-stack-arg-p (= i register-arg-count)))
360 (defaults (cons default-lab
361 (cons tn first-stack-arg-p)))
362 (inst cmp rcx-tn (fixnumize i))
363 (inst jmp :be default-lab)
364 (when first-stack-arg-p
365 ;; There are stack args so the frame of the callee is
366 ;; still there, save RDX in its first slot temporalily.
367 (storew rdx-tn rbx-tn (frame-word-offset sp->fp-offset)))
368 (loadw rdx-tn rbx-tn (frame-word-offset (+ sp->fp-offset i)))
369 (inst mov tn rdx-tn)))
370 (emit-label defaulting-done)
371 (loadw rdx-tn rbx-tn (frame-word-offset sp->fp-offset))
372 (move rsp-tn rbx-tn)
373 (let ((defaults (defaults)))
374 (when defaults
375 (assemble (*elsewhere*)
376 (emit-label default-stack-slots)
377 (dolist (default defaults)
378 (emit-label (car default))
379 (when (cddr default)
380 ;; We are setting the first stack argument to NIL.
381 ;; The callee's stack frame is dead, save RDX by
382 ;; pushing it to the stack, it will end up at same
383 ;; place as in the (STOREW RDX-TN RBX-TN -1) case
384 ;; above.
385 (inst push rdx-tn))
386 (inst mov (second default) rax-tn))
387 (inst jmp defaulting-done)))))))
389 (let ((regs-defaulted (gen-label))
390 (restore-edi (gen-label))
391 (no-stack-args (gen-label))
392 (default-stack-vals (gen-label))
393 (count-okay (gen-label)))
394 (note-this-location vop :unknown-return)
395 ;; Branch off to the MV case.
396 (inst jmp :c regs-defaulted)
397 ;; Default the register args, and set up the stack as if we
398 ;; entered the MV return point.
399 (inst mov rbx-tn rsp-tn)
400 (inst mov rdi-tn nil-value)
401 (inst mov rsi-tn rdi-tn)
402 ;; Compute a pointer to where to put the [defaulted] stack values.
403 (emit-label no-stack-args)
404 (inst push rdx-tn)
405 (inst push rdi-tn)
406 (inst lea rdi-tn
407 (make-ea :qword :base rbp-tn
408 :disp (frame-byte-offset register-arg-count)))
409 ;; Load RAX with NIL so we can quickly store it, and set up
410 ;; stuff for the loop.
411 (inst mov rax-tn nil-value)
412 (inst std)
413 (inst mov rcx-tn (- nvals register-arg-count))
414 ;; Jump into the default loop.
415 (inst jmp default-stack-vals)
416 ;; The regs are defaulted. We need to copy any stack arguments,
417 ;; and then default the remaining stack arguments.
418 (emit-label regs-defaulted)
419 ;; Compute the number of stack arguments, and if it's zero or
420 ;; less, don't copy any stack arguments.
421 (inst sub rcx-tn (fixnumize register-arg-count))
422 (inst jmp :le no-stack-args)
423 ;; Save EDI.
424 (storew rdi-tn rbx-tn (frame-word-offset (+ sp->fp-offset 1)))
425 ;; Throw away any unwanted args.
426 (inst cmp rcx-tn (fixnumize (- nvals register-arg-count)))
427 (inst jmp :be count-okay)
428 (inst mov rcx-tn (fixnumize (- nvals register-arg-count)))
429 (emit-label count-okay)
430 ;; Save the number of stack values.
431 (inst mov rax-tn rcx-tn)
432 ;; Compute a pointer to where the stack args go.
433 (inst lea rdi-tn
434 (make-ea :qword :base rbp-tn
435 :disp (frame-byte-offset register-arg-count)))
436 ;; Save ESI, and compute a pointer to where the args come from.
437 (storew rsi-tn rbx-tn (frame-word-offset (+ sp->fp-offset 2)))
438 (inst lea rsi-tn
439 (make-ea :qword :base rbx-tn
440 :disp (frame-byte-offset
441 (+ sp->fp-offset register-arg-count))))
442 ;; Do the copy.
443 (inst shr rcx-tn n-fixnum-tag-bits) ; make word count
444 (inst std)
445 (inst rep)
446 (inst movs :qword)
447 ;; Restore RSI.
448 (loadw rsi-tn rbx-tn (frame-word-offset (+ sp->fp-offset 2)))
449 ;; Now we have to default the remaining args. Find out how many.
450 (inst sub rax-tn (fixnumize (- nvals register-arg-count)))
451 (inst neg rax-tn)
452 ;; If none, then just blow out of here.
453 (inst jmp :le restore-edi)
454 (inst mov rcx-tn rax-tn)
455 (inst shr rcx-tn n-fixnum-tag-bits) ; word count
456 ;; Load RAX with NIL for fast storing.
457 (inst mov rax-tn nil-value)
458 ;; Do the store.
459 (emit-label default-stack-vals)
460 (inst rep)
461 (inst stos rax-tn)
462 ;; Restore EDI, and reset the stack.
463 (emit-label restore-edi)
464 (loadw rdi-tn rbx-tn (frame-word-offset (+ sp->fp-offset 1)))
465 (inst mov rsp-tn rbx-tn)
466 (inst cld)))))
467 (values))
469 ;;;; unknown values receiving
471 ;;; Emit code needed at the return point for an unknown-values call
472 ;;; for an arbitrary number of values.
474 ;;; We do the single and non-single cases with no shared code: there
475 ;;; doesn't seem to be any potential overlap, and receiving a single
476 ;;; value is more important efficiency-wise.
478 ;;; When there is a single value, we just push it on the stack,
479 ;;; returning the old SP and 1.
481 ;;; When there is a variable number of values, we move all of the
482 ;;; argument registers onto the stack, and return ARGS and NARGS.
484 ;;; ARGS and NARGS are TNs wired to the named locations. We must
485 ;;; explicitly allocate these TNs, since their lifetimes overlap with
486 ;;; the results start and count. (Also, it's nice to be able to target
487 ;;; them.)
488 (defun receive-unknown-values (args nargs start count node)
489 (declare (type tn args nargs start count))
490 (let ((type (sb!c::basic-combination-derived-type node))
491 (variable-values (gen-label))
492 (stack-values (gen-label))
493 (done (gen-label)))
494 (when (sb!kernel:values-type-may-be-single-value-p type)
495 (inst jmp :c variable-values)
496 (cond ((location= start (first *register-arg-tns*))
497 (inst push (first *register-arg-tns*))
498 (inst lea start (make-ea :qword :base rsp-tn :disp n-word-bytes)))
499 (t (inst mov start rsp-tn)
500 (inst push (first *register-arg-tns*))))
501 (inst mov count (fixnumize 1))
502 (inst jmp done)
503 (emit-label variable-values))
504 ;; The stack frame is burnt and RETurned from if there are no
505 ;; stack values. In this case quickly reallocate sufficient space.
506 (when (<= (sb!kernel:values-type-min-value-count type)
507 register-arg-count)
508 (inst cmp nargs (fixnumize register-arg-count))
509 (inst jmp :g stack-values)
510 #!+#.(cl:if (cl:= sb!vm:word-shift sb!vm:n-fixnum-tag-bits) '(and) '(or))
511 (inst sub rsp-tn nargs)
512 #!-#.(cl:if (cl:= sb!vm:word-shift sb!vm:n-fixnum-tag-bits) '(and) '(or))
513 (progn
514 ;; FIXME: This can't be efficient, but LEA (my first choice)
515 ;; doesn't do subtraction.
516 (inst shl nargs (- word-shift n-fixnum-tag-bits))
517 (inst sub rsp-tn nargs)
518 (inst shr nargs (- word-shift n-fixnum-tag-bits)))
519 (emit-label stack-values))
520 ;; dtc: this writes the registers onto the stack even if they are
521 ;; not needed, only the number specified in rcx are used and have
522 ;; stack allocated to them. No harm is done.
523 (loop
524 for arg in *register-arg-tns*
525 for i downfrom -1
526 for j below (sb!kernel:values-type-max-value-count type)
527 do (storew arg args i))
528 (move start args)
529 (move count nargs)
531 (emit-label done))
532 (values))
534 ;;; VOP that can be inherited by unknown values receivers. The main thing this
535 ;;; handles is allocation of the result temporaries.
536 (define-vop (unknown-values-receiver)
537 (:temporary (:sc descriptor-reg :offset rbx-offset
538 :from :eval :to (:result 0))
539 values-start)
540 (:temporary (:sc any-reg :offset rcx-offset
541 :from :eval :to (:result 1))
542 nvals)
543 (:results (start :scs (any-reg control-stack))
544 (count :scs (any-reg control-stack))))
546 ;;;; local call with unknown values convention return
548 (defun check-ocfp-and-return-pc (old-fp return-pc)
549 #+nil
550 (format t "*known-return: old-fp ~S, tn-kind ~S; ~S ~S~%"
551 old-fp (sb!c::tn-kind old-fp) (sb!c::tn-save-tn old-fp)
552 (sb!c::tn-kind (sb!c::tn-save-tn old-fp)))
553 #+nil
554 (format t "*known-return: return-pc ~S, tn-kind ~S; ~S ~S~%"
555 return-pc (sb!c::tn-kind return-pc)
556 (sb!c::tn-save-tn return-pc)
557 (sb!c::tn-kind (sb!c::tn-save-tn return-pc)))
558 (unless (and (sc-is old-fp control-stack)
559 (= (tn-offset old-fp) ocfp-save-offset))
560 (error "ocfp not on stack in standard save location?"))
561 (unless (and (sc-is return-pc sap-stack)
562 (= (tn-offset return-pc) return-pc-save-offset))
563 (error "return-pc not on stack in standard save location?")))
565 ;;; The local call convention doesn't fit that well with x86-style
566 ;;; calls. Emit a header for local calls to pop the return address
567 ;;; in the right place.
568 (defun emit-block-header (start-label trampoline-label fall-thru-p alignp)
569 (when (and fall-thru-p trampoline-label)
570 (inst jmp start-label))
571 (when trampoline-label
572 (emit-label trampoline-label)
573 (popw rbp-tn (frame-word-offset return-pc-save-offset)))
574 (when alignp
575 (emit-alignment n-lowtag-bits :long-nop))
576 (emit-label start-label))
578 ;;; Non-TR local call for a fixed number of values passed according to
579 ;;; the unknown values convention.
581 ;;; FP is the frame pointer in install before doing the call.
583 ;;; NFP would be the number-stack frame pointer if we had a separate
584 ;;; number stack.
586 ;;; Args are the argument passing locations, which are specified only
587 ;;; to terminate their lifetimes in the caller.
589 ;;; VALUES are the return value locations (wired to the standard
590 ;;; passing locations). NVALS is the number of values received.
592 ;;; Save is the save info, which we can ignore since saving has been
593 ;;; done.
595 ;;; TARGET is a continuation pointing to the start of the called
596 ;;; function.
597 (define-vop (call-local)
598 (:args (fp)
599 (nfp)
600 (args :more t))
601 (:results (values :more t))
602 (:save-p t)
603 (:move-args :local-call)
604 (:info arg-locs callee target nvals)
605 (:vop-var vop)
606 (:ignore nfp arg-locs args callee)
607 (:node-var node)
608 (:generator 5
609 (move rbp-tn fp)
610 (note-this-location vop :call-site)
611 (inst call target)
612 (default-unknown-values vop values nvals node)))
614 ;;; Non-TR local call for a variable number of return values passed according
615 ;;; to the unknown values convention. The results are the start of the values
616 ;;; glob and the number of values received.
617 (define-vop (multiple-call-local unknown-values-receiver)
618 (:args (fp)
619 (nfp)
620 (args :more t))
621 (:save-p t)
622 (:move-args :local-call)
623 (:info save callee target)
624 (:ignore args save nfp callee)
625 (:vop-var vop)
626 (:node-var node)
627 (:generator 20
628 (move rbp-tn fp)
629 (note-this-location vop :call-site)
630 (inst call target)
631 (note-this-location vop :unknown-return)
632 (receive-unknown-values values-start nvals start count node)))
634 ;;;; local call with known values return
636 ;;; Non-TR local call with known return locations. Known-value return
637 ;;; works just like argument passing in local call.
639 ;;; Note: we can't use normal load-tn allocation for the fixed args,
640 ;;; since all registers may be tied up by the more operand. Instead,
641 ;;; we use MAYBE-LOAD-STACK-TN.
642 (define-vop (known-call-local)
643 (:args (fp)
644 (nfp)
645 (args :more t))
646 (:results (res :more t))
647 (:move-args :local-call)
648 (:save-p t)
649 (:info save callee target)
650 (:ignore args res save nfp callee)
651 (:vop-var vop)
652 (:generator 5
653 (move rbp-tn fp)
654 (note-this-location vop :call-site)
655 (inst call target)
656 (note-this-location vop :known-return)))
658 ;;; From Douglas Crosher
659 ;;; Return from known values call. We receive the return locations as
660 ;;; arguments to terminate their lifetimes in the returning function. We
661 ;;; restore FP and CSP and jump to the Return-PC.
662 (define-vop (known-return)
663 (:args (old-fp)
664 (return-pc)
665 (vals :more t))
666 (:move-args :known-return)
667 (:info val-locs)
668 (:ignore val-locs vals)
669 (:vop-var vop)
670 (:generator 6
671 (check-ocfp-and-return-pc old-fp return-pc)
672 ;; Zot all of the stack except for the old-fp and return-pc.
673 (inst mov rsp-tn rbp-tn)
674 (inst pop rbp-tn)
675 (inst ret)))
677 ;;;; full call
679 ;;; There is something of a cross-product effect with full calls.
680 ;;; Different versions are used depending on whether we know the
681 ;;; number of arguments or the name of the called function, and
682 ;;; whether we want fixed values, unknown values, or a tail call.
684 ;;; In full call, the arguments are passed creating a partial frame on
685 ;;; the stack top and storing stack arguments into that frame. On
686 ;;; entry to the callee, this partial frame is pointed to by FP.
688 ;;; This macro helps in the definition of full call VOPs by avoiding
689 ;;; code replication in defining the cross-product VOPs.
691 ;;; NAME is the name of the VOP to define.
693 ;;; NAMED is true if the first argument is an fdefinition object whose
694 ;;; definition is to be called.
696 ;;; RETURN is either :FIXED, :UNKNOWN or :TAIL:
697 ;;; -- If :FIXED, then the call is for a fixed number of values, returned in
698 ;;; the standard passing locations (passed as result operands).
699 ;;; -- If :UNKNOWN, then the result values are pushed on the stack, and the
700 ;;; result values are specified by the Start and Count as in the
701 ;;; unknown-values continuation representation.
702 ;;; -- If :TAIL, then do a tail-recursive call. No values are returned.
703 ;;; The Old-Fp and Return-PC are passed as the second and third arguments.
705 ;;; In non-tail calls, the pointer to the stack arguments is passed as
706 ;;; the last fixed argument. If Variable is false, then the passing
707 ;;; locations are passed as a more arg. Variable is true if there are
708 ;;; a variable number of arguments passed on the stack. Variable
709 ;;; cannot be specified with :TAIL return. TR variable argument call
710 ;;; is implemented separately.
712 ;;; In tail call with fixed arguments, the passing locations are
713 ;;; passed as a more arg, but there is no new-FP, since the arguments
714 ;;; have been set up in the current frame.
715 (macrolet ((define-full-call (name named return variable)
716 (aver (not (and variable (eq return :tail))))
717 `(define-vop (,name
718 ,@(when (eq return :unknown)
719 '(unknown-values-receiver)))
720 (:args
721 ,@(unless (eq return :tail)
722 '((new-fp :scs (any-reg) :to (:argument 1))))
724 (fun :scs (descriptor-reg control-stack)
725 :target rax :to (:argument 0))
727 ,@(when (eq return :tail)
728 '((old-fp)
729 (return-pc)))
731 ,@(unless variable '((args :more t :scs (descriptor-reg)))))
733 ,@(when (eq return :fixed)
734 '((:results (values :more t))))
736 (:save-p ,(if (eq return :tail) :compute-only t))
738 ,@(unless (or (eq return :tail) variable)
739 '((:move-args :full-call)))
741 (:vop-var vop)
742 (:info
743 ,@(unless (or variable (eq return :tail)) '(arg-locs))
744 ,@(unless variable '(nargs))
745 ,@(when (eq return :fixed) '(nvals))
746 step-instrumenting)
748 (:ignore
749 ,@(unless (or variable (eq return :tail)) '(arg-locs))
750 ,@(unless variable '(args)))
752 ;; We pass either the fdefn object (for named call) or
753 ;; the actual function object (for unnamed call) in
754 ;; RAX. With named call, closure-tramp will replace it
755 ;; with the real function and invoke the real function
756 ;; for closures. Non-closures do not need this value,
757 ;; so don't care what shows up in it.
758 (:temporary
759 (:sc descriptor-reg
760 :offset rax-offset
761 :from (:argument 0)
762 :to :eval)
763 rax)
765 ;; We pass the number of arguments in RCX.
766 (:temporary (:sc unsigned-reg :offset rcx-offset :to :eval) rcx)
768 ;; With variable call, we have to load the
769 ;; register-args out of the (new) stack frame before
770 ;; doing the call. Therefore, we have to tell the
771 ;; lifetime stuff that we need to use them.
772 ,@(when variable
773 (mapcar (lambda (name offset)
774 `(:temporary (:sc descriptor-reg
775 :offset ,offset
776 :from (:argument 0)
777 :to :eval)
778 ,name))
779 *register-arg-names* *register-arg-offsets*))
781 ,@(when (eq return :tail)
782 '((:temporary (:sc unsigned-reg
783 :from (:argument 1)
784 :to (:argument 2))
785 old-fp-tmp)))
786 ,@(unless (eq return :tail)
787 '((:node-var node)))
789 (:generator ,(+ (if named 5 0)
790 (if variable 19 1)
791 (if (eq return :tail) 0 10)
793 (if (eq return :unknown) 25 0))
794 ;; This has to be done before the frame pointer is
795 ;; changed! RAX stores the 'lexical environment' needed
796 ;; for closures.
797 (move rax fun)
800 ,@(if variable
801 ;; For variable call, compute the number of
802 ;; arguments and move some of the arguments to
803 ;; registers.
804 (collect ((noise))
805 ;; Compute the number of arguments.
806 (noise '(inst mov rcx new-fp))
807 (noise '(inst sub rcx rsp-tn))
808 #.(unless (= word-shift n-fixnum-tag-bits)
809 '(noise '(inst shr rcx
810 (- word-shift n-fixnum-tag-bits))))
811 ;; Move the necessary args to registers,
812 ;; this moves them all even if they are
813 ;; not all needed.
814 (loop
815 for name in *register-arg-names*
816 for index downfrom -1
817 do (noise `(loadw ,name new-fp ,index)))
818 (noise))
819 '((if (zerop nargs)
820 (zeroize rcx)
821 (inst mov rcx (fixnumize nargs)))))
822 ,@(cond ((eq return :tail)
823 '(;; Python has figured out what frame we should
824 ;; return to so might as well use that clue.
825 ;; This seems really important to the
826 ;; implementation of things like
827 ;; (without-interrupts ...)
829 ;; dtc; Could be doing a tail call from a
830 ;; known-local-call etc in which the old-fp
831 ;; or ret-pc are in regs or in non-standard
832 ;; places. If the passing location were
833 ;; wired to the stack in standard locations
834 ;; then these moves will be un-necessary;
835 ;; this is probably best for the x86.
836 (sc-case old-fp
837 ((control-stack)
838 (unless (= ocfp-save-offset
839 (tn-offset old-fp))
840 ;; FIXME: FORMAT T for stale
841 ;; diagnostic output (several of
842 ;; them around here), ick
843 (error "** tail-call old-fp not S0~%")
844 (move old-fp-tmp old-fp)
845 (storew old-fp-tmp
846 rbp-tn
847 (frame-word-offset ocfp-save-offset))))
848 ((any-reg descriptor-reg)
849 (error "** tail-call old-fp in reg not S0~%")
850 (storew old-fp
851 rbp-tn
852 (frame-word-offset ocfp-save-offset))))
854 ;; For tail call, we have to push the
855 ;; return-pc so that it looks like we CALLed
856 ;; despite the fact that we are going to JMP.
857 (inst push return-pc)
860 ;; For non-tail call, we have to save our
861 ;; frame pointer and install the new frame
862 ;; pointer. We can't load stack tns after this
863 ;; point.
864 `(;; Python doesn't seem to allocate a frame
865 ;; here which doesn't leave room for the
866 ;; ofp/ret stuff.
868 ;; The variable args are on the stack and
869 ;; become the frame, but there may be <3
870 ;; args and 3 stack slots are assumed
871 ;; allocate on the call. So need to ensure
872 ;; there are at least 3 slots. This hack
873 ;; just adds 3 more.
874 ,(if variable
875 '(inst sub rsp-tn (* 3 n-word-bytes)))
877 ;; Bias the new-fp for use as an fp
878 ,(if variable
879 '(inst sub new-fp (* sp->fp-offset n-word-bytes)))
881 ;; Save the fp
882 (storew rbp-tn new-fp
883 (frame-word-offset ocfp-save-offset))
885 (move rbp-tn new-fp) ; NB - now on new stack frame.
888 (when step-instrumenting
889 (emit-single-step-test)
890 (inst jmp :eq DONE)
891 (inst break single-step-around-trap))
892 DONE
894 (note-this-location vop :call-site)
896 (inst ,(if (eq return :tail) 'jmp 'call)
897 (make-ea :qword :base rax
898 :disp ,(if named
899 '(- (* fdefn-raw-addr-slot
900 n-word-bytes)
901 other-pointer-lowtag)
902 '(- (* closure-fun-slot n-word-bytes)
903 fun-pointer-lowtag))))
904 ,@(ecase return
905 (:fixed
906 '((default-unknown-values vop values nvals node)))
907 (:unknown
908 '((note-this-location vop :unknown-return)
909 (receive-unknown-values values-start nvals start count
910 node)))
911 (:tail))))))
913 (define-full-call call nil :fixed nil)
914 (define-full-call call-named t :fixed nil)
915 (define-full-call multiple-call nil :unknown nil)
916 (define-full-call multiple-call-named t :unknown nil)
917 (define-full-call tail-call nil :tail nil)
918 (define-full-call tail-call-named t :tail nil)
920 (define-full-call call-variable nil :fixed t)
921 (define-full-call multiple-call-variable nil :unknown t))
923 ;;; This is defined separately, since it needs special code that BLT's
924 ;;; the arguments down. All the real work is done in the assembly
925 ;;; routine. We just set things up so that it can find what it needs.
926 (define-vop (tail-call-variable)
927 (:args (args :scs (any-reg control-stack) :target rsi)
928 (function :scs (descriptor-reg control-stack) :target rax)
929 (old-fp)
930 (return-pc))
931 (:temporary (:sc unsigned-reg :offset rsi-offset :from (:argument 0)) rsi)
932 (:temporary (:sc unsigned-reg :offset rax-offset :from (:argument 1)) rax)
933 (:temporary (:sc unsigned-reg) call-target)
934 (:generator 75
935 (check-ocfp-and-return-pc old-fp return-pc)
936 ;; Move these into the passing locations if they are not already there.
937 (move rsi args)
938 (move rax function)
939 ;; And jump to the assembly routine.
940 (inst mov call-target (make-fixup 'tail-call-variable :assembly-routine))
941 (inst jmp call-target)))
943 ;;;; unknown values return
945 ;;; Return a single-value using the Unknown-Values convention.
947 ;;; pfw--get wired-tn conflicts sometimes if register sc specd for args
948 ;;; having problems targeting args to regs -- using temps instead.
950 ;;; First off, modifying the return-pc defeats the branch-prediction
951 ;;; optimizations on modern CPUs quite handily. Second, we can do all
952 ;;; this without needing a temp register. Fixed the latter, at least.
953 ;;; -- AB 2006/Feb/04
954 (define-vop (return-single)
955 (:args (old-fp)
956 (return-pc)
957 (value))
958 (:ignore value)
959 (:generator 6
960 (check-ocfp-and-return-pc old-fp return-pc)
961 ;; Drop stack above old-fp
962 (inst mov rsp-tn rbp-tn)
963 ;; Clear the multiple-value return flag
964 (inst clc)
965 ;; Restore the old frame pointer
966 (inst pop rbp-tn)
967 ;; And return.
968 (inst ret)))
970 ;;; Do unknown-values return of a fixed (other than 1) number of
971 ;;; values. The VALUES are required to be set up in the standard
972 ;;; passing locations. NVALS is the number of values returned.
974 ;;; Basically, we just load RCX with the number of values returned and
975 ;;; RBX with a pointer to the values, set RSP to point to the end of
976 ;;; the values, and jump directly to return-pc.
977 (define-vop (return)
978 (:args (old-fp)
979 (return-pc :to (:eval 1))
980 (values :more t))
981 (:ignore values)
982 (:info nvals)
983 ;; In the case of other than one value, we need these registers to
984 ;; tell the caller where they are and how many there are.
985 (:temporary (:sc unsigned-reg :offset rbx-offset) rbx)
986 (:temporary (:sc unsigned-reg :offset rcx-offset) rcx)
987 ;; We need to stretch the lifetime of return-pc past the argument
988 ;; registers so that we can default the argument registers without
989 ;; trashing return-pc.
990 (:temporary (:sc unsigned-reg :offset (first *register-arg-offsets*)
991 :from :eval) a0)
992 (:temporary (:sc unsigned-reg :offset (second *register-arg-offsets*)
993 :from :eval) a1)
994 (:temporary (:sc unsigned-reg :offset (third *register-arg-offsets*)
995 :from :eval) a2)
997 (:generator 6
998 (check-ocfp-and-return-pc old-fp return-pc)
999 (when (= nvals 1)
1000 ;; This is handled in RETURN-SINGLE.
1001 (error "nvalues is 1"))
1002 ;; Establish the values pointer and values count.
1003 (inst lea rbx (make-ea :qword :base rbp-tn
1004 :disp (* sp->fp-offset n-word-bytes)))
1005 (if (zerop nvals)
1006 (zeroize rcx) ; smaller
1007 (inst mov rcx (fixnumize nvals)))
1008 ;; Pre-default any argument register that need it.
1009 (when (< nvals register-arg-count)
1010 (let* ((arg-tns (nthcdr nvals (list a0 a1 a2)))
1011 (first (first arg-tns)))
1012 (inst mov first nil-value)
1013 (dolist (tn (cdr arg-tns))
1014 (inst mov tn first))))
1015 ;; Set the multiple value return flag.
1016 (inst stc)
1017 ;; And away we go. Except that return-pc is still on the
1018 ;; stack and we've changed the stack pointer. So we have to
1019 ;; tell it to index off of RBX instead of RBP.
1020 (cond ((<= nvals register-arg-count)
1021 (inst mov rsp-tn rbp-tn)
1022 (inst pop rbp-tn)
1023 (inst ret))
1025 ;; Some values are on the stack after RETURN-PC and OLD-FP,
1026 ;; can't return normally and some slots of the frame will
1027 ;; be used as temporaries by the receiver.
1029 ;; Clear as much of the stack as possible, but not past the
1030 ;; old frame address.
1031 (inst lea rsp-tn
1032 (make-ea :qword :base rbp-tn
1033 :disp (frame-byte-offset (1- nvals))))
1034 (move rbp-tn old-fp)
1035 (inst push (make-ea :qword :base rbx
1036 :disp (frame-byte-offset
1037 (+ sp->fp-offset
1038 (tn-offset return-pc)))))
1039 (inst ret)))))
1041 ;;; Do unknown-values return of an arbitrary number of values (passed
1042 ;;; on the stack.) We check for the common case of a single return
1043 ;;; value, and do that inline using the normal single value return
1044 ;;; convention. Otherwise, we branch off to code that calls an
1045 ;;; assembly-routine.
1047 ;;; The assembly routine takes the following args:
1048 ;;; RCX -- number of values to find there.
1049 ;;; RSI -- pointer to where to find the values.
1050 (define-vop (return-multiple)
1051 (:args (old-fp)
1052 (return-pc)
1053 (vals :scs (any-reg) :target rsi)
1054 (nvals :scs (any-reg) :target rcx))
1055 (:temporary (:sc unsigned-reg :offset rsi-offset :from (:argument 2)) rsi)
1056 (:temporary (:sc unsigned-reg :offset rcx-offset :from (:argument 3)) rcx)
1057 (:temporary (:sc unsigned-reg) return-asm)
1058 (:temporary (:sc descriptor-reg :offset (first *register-arg-offsets*)
1059 :from (:eval 0)) a0)
1060 (:node-var node)
1061 (:generator 13
1062 (check-ocfp-and-return-pc old-fp return-pc)
1063 (unless (policy node (> space speed))
1064 ;; Check for the single case.
1065 (let ((not-single (gen-label)))
1066 (inst cmp nvals (fixnumize 1))
1067 (inst jmp :ne not-single)
1068 ;; Return with one value.
1069 (loadw a0 vals -1)
1070 ;; Clear the stack until ocfp.
1071 (inst mov rsp-tn rbp-tn)
1072 ;; clear the multiple-value return flag
1073 (inst clc)
1074 ;; Out of here.
1075 (inst pop rbp-tn)
1076 (inst ret)
1077 ;; Nope, not the single case. Jump to the assembly routine.
1078 (emit-label not-single)))
1079 (move rsi vals)
1080 (move rcx nvals)
1081 (inst mov return-asm (make-fixup 'return-multiple :assembly-routine))
1082 (inst jmp return-asm)))
1084 ;;;; XEP hackery
1086 ;;; Get the lexical environment from its passing location.
1087 (define-vop (setup-closure-environment)
1088 (:results (closure :scs (descriptor-reg)))
1089 (:info label)
1090 (:ignore label)
1091 (:generator 6
1092 ;; Get result.
1093 (move closure rax-tn)))
1095 ;;; Copy a &MORE arg from the argument area to the end of the current
1096 ;;; frame. FIXED is the number of non-&MORE arguments.
1097 (define-vop (copy-more-arg)
1098 (:temporary (:sc any-reg :offset r8-offset) copy-index)
1099 (:temporary (:sc any-reg :offset r9-offset) source)
1100 (:temporary (:sc descriptor-reg :offset r10-offset) temp)
1101 (:info fixed)
1102 (:generator 20
1103 ;; Avoid the copy if there are no more args.
1104 (cond ((zerop fixed)
1105 (inst jrcxz JUST-ALLOC-FRAME))
1107 (inst cmp rcx-tn (fixnumize fixed))
1108 (inst jmp :be JUST-ALLOC-FRAME)))
1110 ;; Create a negated copy of the number of arguments to allow us to
1111 ;; use EA calculations in order to do scaled subtraction.
1112 (inst mov temp rcx-tn)
1113 (inst neg temp)
1115 ;; Allocate the space on the stack.
1116 ;; stack = rbp + sp->fp-offset - (max 3 frame-size) - (nargs - fixed)
1117 ;; if we'd move SP backward, swap the meaning of rsp and source;
1118 ;; otherwise, we'd be accessing values below SP, and that's no good
1119 ;; if a signal interrupts this code sequence. In that case, store
1120 ;; the final value in rsp after the stack-stack memmove loop.
1121 (inst lea (if (<= fixed (max 3 (sb-allocated-size 'stack)))
1122 rsp-tn
1123 source)
1124 (make-ea :qword :base rbp-tn
1125 :index temp :scale (ash 1 (- word-shift n-fixnum-tag-bits))
1126 :disp (* n-word-bytes
1127 (- (+ sp->fp-offset fixed)
1128 (max 3 (sb-allocated-size 'stack))))))
1130 ;; Now: nargs>=1 && nargs>fixed
1132 ;; Save the original count of args.
1133 (inst mov rbx-tn rcx-tn)
1135 (cond ((< fixed register-arg-count)
1136 ;; the code above only moves the final value of rsp in
1137 ;; rsp directly if that condition is satisfied. Currently,
1138 ;; r-a-c is 3, so the aver is OK. If the calling convention
1139 ;; ever changes, the logic above with LEA will have to be
1140 ;; adjusted.
1141 (aver (<= fixed (max 3 (sb-allocated-size 'stack))))
1142 ;; We must stop when we run out of stack args, not when we
1143 ;; run out of more args.
1144 ;; Number to copy = nargs-3
1145 (inst sub rbx-tn (fixnumize register-arg-count))
1146 ;; Everything of interest in registers.
1147 (inst jmp :be DO-REGS))
1149 ;; Number to copy = nargs-fixed
1150 (inst sub rbx-tn (fixnumize fixed))))
1152 ;; Initialize R8 to be the end of args.
1153 ;; Swap with SP if necessary to mirror the previous condition
1154 (inst lea (if (<= fixed (max 3 (sb-allocated-size 'stack)))
1155 source
1156 rsp-tn)
1157 (make-ea :qword :base rbp-tn
1158 :index temp :scale (ash 1 (- word-shift n-fixnum-tag-bits))
1159 :disp (* sp->fp-offset n-word-bytes)))
1161 ;; src: rbp + temp + sp->fp
1162 ;; dst: rbp + temp + sp->fp + (fixed - (max 3 [stack-size]))
1163 (let ((delta (- fixed (max 3 (sb-allocated-size 'stack))))
1164 (loop (gen-label))
1165 (fixnum->word (ash 1 (- word-shift n-fixnum-tag-bits))))
1166 (cond ((zerop delta)) ; no-op move
1167 ((minusp delta)
1168 ;; dst is lower than src, copy forward
1169 (zeroize copy-index)
1170 ;; We used to use REP MOVS here, but on modern x86 it performs
1171 ;; much worse than an explicit loop for small blocks.
1173 (emit-label loop)
1174 (inst mov temp (make-ea :qword :base source :index copy-index))
1175 (inst mov (make-ea :qword :base rsp-tn :index copy-index) temp)
1176 (inst add copy-index n-word-bytes)
1177 (inst sub rbx-tn (fixnumize 1))
1178 (inst jmp :nz loop))
1179 ((plusp delta)
1180 ;; dst is higher than src; copy backward
1181 (emit-label loop)
1182 (inst sub rbx-tn (fixnumize 1))
1183 (inst mov temp (make-ea :qword :base rsp-tn
1184 :index rbx-tn :scale fixnum->word))
1185 (inst mov (make-ea :qword :base source
1186 :index rbx-tn :scale fixnum->word)
1187 temp)
1188 (inst jmp :nz loop)
1189 ;; done with the stack--stack copy. Reset RSP to its final
1190 ;; value
1191 (inst mov rsp-tn source))))
1192 DO-REGS
1194 ;; Here: nargs>=1 && nargs>fixed
1195 (when (< fixed register-arg-count)
1196 ;; Now we have to deposit any more args that showed up in
1197 ;; registers.
1198 (do ((i fixed))
1199 ( nil )
1200 ;; Store it relative to rbp
1201 (inst mov (make-ea :qword :base rbp-tn
1202 :disp (* n-word-bytes
1203 (- sp->fp-offset
1204 (+ 1
1205 (- i fixed)
1206 (max 3 (sb-allocated-size
1207 'stack))))))
1208 (nth i *register-arg-tns*))
1210 (incf i)
1211 (when (>= i register-arg-count)
1212 (return))
1214 ;; Don't deposit any more than there are.
1215 (if (zerop i)
1216 (inst test rcx-tn rcx-tn)
1217 (inst cmp rcx-tn (fixnumize i)))
1218 (inst jmp :eq DONE)))
1220 (inst jmp DONE)
1222 JUST-ALLOC-FRAME
1223 (inst lea rsp-tn
1224 (make-ea :qword :base rbp-tn
1225 :disp (* n-word-bytes
1226 (- sp->fp-offset
1227 (max 3 (sb-allocated-size 'stack))))))
1229 DONE))
1231 (define-vop (more-kw-arg)
1232 (:translate sb!c::%more-kw-arg)
1233 (:policy :fast-safe)
1234 (:args (object :scs (descriptor-reg) :to (:result 1))
1235 (index :scs (any-reg) :to (:result 1) :target keyword))
1236 (:arg-types * tagged-num)
1237 (:results (value :scs (descriptor-reg any-reg))
1238 (keyword :scs (descriptor-reg any-reg)))
1239 (:result-types * *)
1240 (:generator 4
1241 (inst mov value (make-ea :qword :base object :index index
1242 :scale (ash 1 (- word-shift n-fixnum-tag-bits))))
1243 (inst mov keyword (make-ea :qword :base object :index index
1244 :scale (ash 1 (- word-shift n-fixnum-tag-bits))
1245 :disp n-word-bytes))))
1247 (define-vop (more-arg/c)
1248 (:translate sb!c::%more-arg)
1249 (:policy :fast-safe)
1250 (:args (object :scs (descriptor-reg) :to (:result 1)))
1251 (:info index)
1252 (:arg-types * (:constant (signed-byte 32)))
1253 (:results (value :scs (descriptor-reg any-reg)))
1254 (:result-types *)
1255 (:generator 3
1256 (inst mov value (make-ea :qword :base object
1257 :disp (- (* index 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
1271 :scale (ash 1 (- word-shift n-fixnum-tag-bits))))))
1273 ;;; Turn more arg (context, count) into a list.
1274 (define-vop (listify-rest-args)
1275 (:translate %listify-rest-args)
1276 (:policy :safe)
1277 (:args (context :scs (descriptor-reg) :target src)
1278 (count :scs (any-reg) :target rcx))
1279 (:arg-types * tagged-num)
1280 (:temporary (:sc unsigned-reg :offset rsi-offset :from (:argument 0)) src)
1281 (:temporary (:sc unsigned-reg :offset rcx-offset :from (:argument 1)) rcx)
1282 (:temporary (:sc unsigned-reg :offset rax-offset) rax)
1283 (:temporary (:sc unsigned-reg) dst)
1284 (:results (result :scs (descriptor-reg)))
1285 (:node-var node)
1286 (:generator 20
1287 (let ((enter (gen-label))
1288 (loop (gen-label))
1289 (done (gen-label))
1290 (stack-allocate-p (node-stack-allocate-p node)))
1291 (move src context)
1292 (move rcx count)
1293 ;; Check to see whether there are no args, and just return NIL if so.
1294 (inst mov result nil-value)
1295 (inst jrcxz done)
1296 (inst lea dst (make-ea :qword :index rcx :scale (ash 2 (- word-shift n-fixnum-tag-bits))))
1297 (maybe-pseudo-atomic stack-allocate-p
1298 (allocation dst dst node stack-allocate-p list-pointer-lowtag)
1299 ;; Set up the result.
1300 (move result dst)
1301 ;; Jump into the middle of the loop, 'cause that's where we want
1302 ;; to start.
1303 (inst jmp enter)
1304 (emit-label loop)
1305 ;; Compute a pointer to the next cons.
1306 (inst add dst (* cons-size n-word-bytes))
1307 ;; Store a pointer to this cons in the CDR of the previous cons.
1308 (storew dst dst -1 list-pointer-lowtag)
1309 (emit-label enter)
1310 ;; Grab one value and stash it in the car of this cons.
1311 (inst mov rax (make-ea :qword :base src))
1312 (inst sub src n-word-bytes)
1313 (storew rax dst 0 list-pointer-lowtag)
1314 ;; Go back for more.
1315 (inst sub rcx (fixnumize 1))
1316 (inst jmp :nz loop)
1317 ;; NIL out the last cons.
1318 (storew nil-value dst 1 list-pointer-lowtag))
1319 (emit-label done))))
1321 ;;; Return the location and size of the &MORE arg glob created by
1322 ;;; COPY-MORE-ARG. SUPPLIED is the total number of arguments supplied
1323 ;;; (originally passed in RCX). FIXED is the number of non-rest
1324 ;;; arguments.
1326 ;;; We must duplicate some of the work done by COPY-MORE-ARG, since at
1327 ;;; that time the environment is in a pretty brain-damaged state,
1328 ;;; preventing this info from being returned as values. What we do is
1329 ;;; compute supplied - fixed, and return a pointer that many words
1330 ;;; below the current stack top.
1331 (define-vop (more-arg-context)
1332 (:policy :fast-safe)
1333 (:translate sb!c::%more-arg-context)
1334 (:args (supplied :scs (any-reg) :target count))
1335 (:arg-types positive-fixnum (:constant fixnum))
1336 (:info fixed)
1337 (:results (context :scs (descriptor-reg))
1338 (count :scs (any-reg)))
1339 (:result-types t tagged-num)
1340 (:note "more-arg-context")
1341 (:generator 5
1342 (move count supplied)
1343 ;; SP at this point points at the last arg pushed.
1344 ;; Point to the first more-arg, not above it.
1345 (inst lea context (make-ea :qword :base rsp-tn
1346 :index count
1347 :scale (ash 1 (- word-shift n-fixnum-tag-bits))
1348 :disp (- (* (1+ fixed) n-word-bytes))))
1349 (unless (zerop fixed)
1350 (inst sub count (fixnumize fixed)))))
1352 (define-vop (verify-arg-count)
1353 (:policy :fast-safe)
1354 (:args (nargs :scs (any-reg)))
1355 (:arg-types positive-fixnum (:constant t) (:constant t))
1356 (:info min max)
1357 (:vop-var vop)
1358 (:save-p :compute-only)
1359 (:generator 3
1360 (let ((err-lab (generate-error-code vop 'invalid-arg-count-error)))
1361 (cond ((not min)
1362 (if (zerop max)
1363 (inst test nargs nargs)
1364 (inst cmp nargs (fixnumize max)))
1365 (inst jmp :ne err-lab))
1366 (max
1367 (when (plusp min)
1368 (inst cmp nargs (fixnumize min))
1369 (inst jmp :b err-lab))
1370 (inst cmp nargs (fixnumize max))
1371 (inst jmp :a err-lab))
1372 ((plusp min)
1373 (inst cmp nargs (fixnumize min))
1374 (inst jmp :b err-lab))))))
1376 ;;; Various other error signallers.
1377 (macrolet ((def (name error translate &rest args)
1378 `(define-vop (,name)
1379 ,@(when translate
1380 `((:policy :fast-safe)
1381 (:translate ,translate)))
1382 (:args ,@(mapcar (lambda (arg)
1383 `(,arg :scs (any-reg descriptor-reg
1384 control-stack constant)))
1385 args))
1386 (:vop-var vop)
1387 (:save-p :compute-only)
1388 (:generator 1000
1389 (error-call vop ',error ,@args)))))
1390 (def arg-count-error invalid-arg-count-error
1391 sb!c::%arg-count-error nargs fname)
1392 (def type-check-error object-not-type-error sb!c::%type-check-error
1393 object type)
1394 (def layout-invalid-error layout-invalid-error sb!c::%layout-invalid-error
1395 object layout)
1396 (def odd-key-args-error odd-key-args-error
1397 sb!c::%odd-key-args-error)
1398 (def unknown-key-arg-error unknown-key-arg-error
1399 sb!c::%unknown-key-arg-error key)
1400 (def nil-fun-returned-error nil-fun-returned-error nil fun))
1402 ;; Signal an error about an untagged number.
1403 ;; These are pretty much boilerplate and could be generic except:
1404 ;; - the names of the SCs could differ between backends (or maybe not?)
1405 ;; - in the "/c" case, the older backends don't eval the errcode
1406 ;; And the 6 vops above ought to be generic too...
1407 ;; FIXME: there are still some occurrences of
1408 ;; note: doing signed word to integer coercion
1409 ;; in regard to SB-C::%TYPE-CHECK-ERROR. Figure out why.
1410 (define-vop (type-check-error/word)
1411 (:policy :fast-safe)
1412 (:translate sb!c::%type-check-error)
1413 (:args (object :scs (signed-reg unsigned-reg))
1414 ;; Types are trees of symbols, so 'any-reg' is not
1415 ;; really possible.
1416 (type :scs (any-reg descriptor-reg)))
1417 (:arg-types untagged-num *)
1418 (:vop-var vop)
1419 (:save-p :compute-only)
1420 ;; cost is a smidgen less than type-check-error
1421 ;; otherwise this does not get selected.
1422 (:generator 999
1423 (error-call vop 'object-not-type-error object type)))
1424 (define-vop (type-check-error/word/c)
1425 (:policy :fast-safe)
1426 (:translate sb!c::%type-check-error/c)
1427 (:args (object :scs (signed-reg unsigned-reg)))
1428 (:arg-types untagged-num (:constant symbol))
1429 (:info errcode)
1430 (:vop-var vop)
1431 (:save-p :compute-only)
1432 (:generator 899 ; smidgen less than type-check-error/c
1433 (error-call vop errcode object)))
1435 ;;; Single-stepping
1437 (defun emit-single-step-test ()
1438 ;; We use different ways of representing whether stepping is on on
1439 ;; +SB-THREAD / -SB-THREAD: on +SB-THREAD, we use a slot in the
1440 ;; thread structure. On -SB-THREAD we use the value of a static
1441 ;; symbol. Things are done this way, since reading a thread-local
1442 ;; slot from a symbol would require an extra register on +SB-THREAD,
1443 ;; and reading a slot from a thread structure would require an extra
1444 ;; register on -SB-THREAD. While this isn't critical for x86-64,
1445 ;; it's more serious for x86.
1446 #!+sb-thread
1447 (inst cmp (make-ea :qword
1448 :base thread-base-tn
1449 :disp (* thread-stepping-slot n-word-bytes))
1450 nil-value)
1451 #!-sb-thread
1452 (inst cmp (make-ea :qword
1453 :disp (+ nil-value (static-symbol-offset
1454 'sb!impl::*stepping*)
1455 (* symbol-value-slot n-word-bytes)
1456 (- other-pointer-lowtag)))
1457 nil-value))
1459 (define-vop (step-instrument-before-vop)
1460 (:policy :fast-safe)
1461 (:vop-var vop)
1462 (:generator 3
1463 (emit-single-step-test)
1464 (inst jmp :eq DONE)
1465 (inst break single-step-before-trap)
1466 DONE
1467 (note-this-location vop :step-before-vop)))