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