1 ;;;; function call for the x86 VM
3 ;;;; This software is part of the SBCL system. See the README file for
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.
14 ;;;; interfaces to IR2 conversion
16 ;;; Return a wired TN describing the N'th full call argument passing
18 (!def-vm-support-routine standard-arg-location (n)
19 (declare (type unsigned-byte n))
20 (if (< n register-arg-count)
21 (make-wired-tn *backend-t-primitive-type* descriptor-reg-sc-number
22 (nth n *register-arg-offsets*))
23 (make-wired-tn *backend-t-primitive-type* control-stack-sc-number n)))
25 ;;; Make a passing location TN for a local call return PC.
27 ;;; Always wire the return PC location to the stack in its standard
29 (!def-vm-support-routine make-return-pc-passing-location (standard)
30 (declare (ignore standard))
31 (make-wired-tn (primitive-type-or-lose 'system-area-pointer)
32 sap-stack-sc-number return-pc-save-offset))
34 ;;; This is similar to MAKE-RETURN-PC-PASSING-LOCATION, but makes a
35 ;;; location to pass OLD-FP in.
37 ;;; This is wired in both the standard and the local-call conventions,
38 ;;; because we want to be able to assume it's always there. Besides,
39 ;;; the x86 doesn't have enough registers to really make it profitable
40 ;;; to pass it in a register.
41 (!def-vm-support-routine make-old-fp-passing-location (standard)
42 (declare (ignore standard))
43 (make-wired-tn *fixnum-primitive-type* control-stack-sc-number
46 ;;; Make the TNs used to hold OLD-FP and RETURN-PC within the current
47 ;;; function. We treat these specially so that the debugger can find
48 ;;; them at a known location.
50 ;;; Without using a save-tn - which does not make much sense if it is
51 ;;; wired to the stack?
52 (!def-vm-support-routine make-old-fp-save-location (physenv)
53 (physenv-debug-live-tn (make-wired-tn *fixnum-primitive-type*
54 control-stack-sc-number
57 (!def-vm-support-routine make-return-pc-save-location (physenv)
58 (physenv-debug-live-tn
59 (make-wired-tn (primitive-type-or-lose 'system-area-pointer)
60 sap-stack-sc-number return-pc-save-offset)
63 ;;; Make a TN for the standard argument count passing location. We only
64 ;;; need to make the standard location, since a count is never passed when we
65 ;;; are using non-standard conventions.
66 (!def-vm-support-routine make-arg-count-location ()
67 (make-wired-tn *fixnum-primitive-type* any-reg-sc-number rcx-offset))
69 ;;; Make a TN to hold the number-stack frame pointer. This is allocated
70 ;;; once per component, and is component-live.
71 (!def-vm-support-routine make-nfp-tn ()
72 (make-restricted-tn *fixnum-primitive-type* ignore-me-sc-number))
74 (!def-vm-support-routine make-stack-pointer-tn ()
75 (make-normal-tn *fixnum-primitive-type*))
77 (!def-vm-support-routine make-number-stack-pointer-tn ()
78 (make-restricted-tn *fixnum-primitive-type* ignore-me-sc-number))
80 ;;; Return a list of TNs that can be used to represent an unknown-values
81 ;;; continuation within a function.
82 (!def-vm-support-routine make-unknown-values-locations ()
83 (list (make-stack-pointer-tn)
84 (make-normal-tn *fixnum-primitive-type*)))
86 ;;; This function is called by the ENTRY-ANALYZE phase, allowing
87 ;;; VM-dependent initialization of the IR2-COMPONENT structure. We
88 ;;; push placeholder entries in the CONSTANTS to leave room for
89 ;;; additional noise in the code object header.
90 (!def-vm-support-routine select-component-format (component)
91 (declare (type component component))
92 ;; The 1+ here is because for the x86 the first constant is a
93 ;; pointer to a list of fixups, or NIL if the code object has none.
94 ;; (If I understand correctly, the fixups are needed at GC copy
95 ;; time because the X86 code isn't relocatable.)
97 ;; KLUDGE: It'd be cleaner to have the fixups entry be a named
98 ;; element of the CODE (aka component) primitive object. However,
99 ;; it's currently a large, tricky, error-prone chore to change
100 ;; the layout of any primitive object, so for the foreseeable future
101 ;; we'll just live with this ugliness. -- WHN 2002-01-02
102 (dotimes (i (1+ code-constants-offset))
103 (vector-push-extend nil
104 (ir2-component-constants (component-info component))))
109 ;;; This is used for setting up the Old-FP in local call.
110 (define-vop (current-fp)
111 (:results (val :scs (any-reg control-stack)))
115 ;;; We don't have a separate NFP, so we don't need to do anything here.
116 (define-vop (compute-old-nfp)
122 (define-vop (xep-allocate-frame)
123 (:info start-lab copy-more-arg-follows)
126 (emit-alignment n-lowtag-bits)
127 (trace-table-entry trace-table-fun-prologue)
128 (emit-label start-lab)
129 ;; Skip space for the function header.
130 (inst simple-fun-header-word)
131 (dotimes (i (* n-word-bytes (1- simple-fun-code-offset)))
134 ;; The start of the actual code.
135 ;; Save the return-pc.
136 (popw rbp-tn (frame-word-offset return-pc-save-offset))
138 ;; If copy-more-arg follows it will allocate the correct stack
139 ;; size. The stack is not allocated first here as this may expose
140 ;; args on the stack if they take up more space than the frame!
141 (unless copy-more-arg-follows
142 ;; The args fit within the frame so just allocate the frame.
144 (make-ea :qword :base rbp-tn
145 :disp (- (* n-word-bytes
146 (max 3 (sb-allocated-size 'stack)))))))
148 (trace-table-entry trace-table-normal)))
150 ;;; This is emitted directly before either a known-call-local, call-local,
151 ;;; or a multiple-call-local. All it does is allocate stack space for the
152 ;;; callee (who has the same size stack as us).
153 (define-vop (allocate-frame)
154 (:results (res :scs (any-reg control-stack))
160 (inst sub rsp-tn (* n-word-bytes (sb-allocated-size 'stack)))))
162 ;;; Allocate a partial frame for passing stack arguments in a full
163 ;;; call. NARGS is the number of arguments passed. We allocate at
164 ;;; least 3 slots, because the XEP noise is going to want to use them
165 ;;; before it can extend the stack.
166 (define-vop (allocate-full-call-frame)
168 (:results (res :scs (any-reg control-stack)))
171 (inst sub rsp-tn (* (max nargs 3) n-word-bytes))))
173 ;;; Emit code needed at the return-point from an unknown-values call
174 ;;; for a fixed number of values. Values is the head of the TN-REF
175 ;;; list for the locations that the values are to be received into.
176 ;;; Nvals is the number of values that are to be received (should
177 ;;; equal the length of Values).
179 ;;; If 0 or 1 values are expected, then we just emit an instruction to
180 ;;; reset the SP (which will only be executed when other than 1 value
183 ;;; In the general case we have to do three things:
184 ;;; -- Default unsupplied register values. This need only be done
185 ;;; when a single value is returned, since register values are
186 ;;; defaulted by the called in the non-single case.
187 ;;; -- Default unsupplied stack values. This needs to be done whenever
188 ;;; there are stack values.
189 ;;; -- Reset SP. This must be done whenever other than 1 value is
190 ;;; returned, regardless of the number of values desired.
191 (defun default-unknown-values (vop values nvals)
192 (declare (type (or tn-ref null) values)
193 (type unsigned-byte nvals))
196 (note-this-location vop :single-value-return)
197 (inst cmov :c rsp-tn rbx-tn))
198 ((<= nvals register-arg-count)
199 (let ((regs-defaulted (gen-label)))
200 (note-this-location vop :unknown-return)
201 (inst jmp :c regs-defaulted)
202 ;; Default the unsupplied registers.
203 (let* ((2nd-tn-ref (tn-ref-across values))
204 (2nd-tn (tn-ref-tn 2nd-tn-ref)))
205 (inst mov 2nd-tn nil-value)
208 for tn-ref = (tn-ref-across 2nd-tn-ref)
209 then (tn-ref-across tn-ref)
210 for count from 2 below register-arg-count
211 do (inst mov (tn-ref-tn tn-ref) 2nd-tn))))
212 (inst mov rbx-tn rsp-tn)
213 (emit-label regs-defaulted)
214 (inst mov rsp-tn rbx-tn)))
216 ;; The number of bytes depends on the relative jump instructions.
217 ;; Best case is 31+(n-3)*14, worst case is 35+(n-3)*18. For
218 ;; NVALS=6 that is 73/89 bytes, and for NVALS=7 that is 87/107
219 ;; bytes which is likely better than using the blt below.
220 (let ((regs-defaulted (gen-label))
221 (defaulting-done (gen-label))
222 (default-stack-slots (gen-label)))
223 (note-this-location vop :unknown-return)
224 ;; Branch off to the MV case.
225 (inst jmp :c regs-defaulted)
226 ;; Do the single value case.
227 ;; Default the register args
228 (inst mov rax-tn nil-value)
230 (val (tn-ref-across values) (tn-ref-across val)))
231 ((= i (min nvals register-arg-count)))
232 (inst mov (tn-ref-tn val) rax-tn))
234 ;; Fake other registers so it looks like we returned with all the
235 ;; registers filled in.
238 (inst jmp default-stack-slots)
240 (emit-label regs-defaulted)
242 (inst mov rax-tn nil-value)
243 (storew rdx-tn rbx-tn -1)
244 (collect ((defaults))
245 (do ((i register-arg-count (1+ i))
246 (val (do ((i 0 (1+ i))
247 (val values (tn-ref-across val)))
248 ((= i register-arg-count) val))
249 (tn-ref-across val)))
251 (let ((default-lab (gen-label))
252 (tn (tn-ref-tn val)))
253 (defaults (cons default-lab tn))
255 (inst cmp rcx-tn (fixnumize i))
256 (inst jmp :be default-lab)
257 (loadw rdx-tn rbx-tn (frame-word-offset i))
258 (inst mov tn rdx-tn)))
260 (emit-label defaulting-done)
261 (loadw rdx-tn rbx-tn -1)
264 (let ((defaults (defaults)))
266 (assemble (*elsewhere*)
267 (trace-table-entry trace-table-fun-prologue)
268 (emit-label default-stack-slots)
269 (dolist (default defaults)
270 (emit-label (car default))
271 (inst mov (cdr default) rax-tn))
272 (inst jmp defaulting-done)
273 (trace-table-entry trace-table-normal)))))))
275 (let ((regs-defaulted (gen-label))
276 (restore-edi (gen-label))
277 (no-stack-args (gen-label))
278 (default-stack-vals (gen-label))
279 (count-okay (gen-label)))
280 (note-this-location vop :unknown-return)
281 ;; Branch off to the MV case.
282 (inst jmp :c regs-defaulted)
284 ;; Default the register args, and set up the stack as if we
285 ;; entered the MV return point.
286 (inst mov rbx-tn rsp-tn)
288 (inst mov rdi-tn nil-value)
290 (inst mov rsi-tn rdi-tn)
291 ;; Compute a pointer to where to put the [defaulted] stack values.
292 (emit-label no-stack-args)
294 (make-ea :qword :base rbp-tn
295 :disp (frame-byte-offset register-arg-count)))
296 ;; Load RAX with NIL so we can quickly store it, and set up
297 ;; stuff for the loop.
298 (inst mov rax-tn nil-value)
300 (inst mov rcx-tn (- nvals register-arg-count))
301 ;; Jump into the default loop.
302 (inst jmp default-stack-vals)
304 ;; The regs are defaulted. We need to copy any stack arguments,
305 ;; and then default the remaining stack arguments.
306 (emit-label regs-defaulted)
308 (storew rdi-tn rbx-tn (frame-word-offset 1))
309 ;; Compute the number of stack arguments, and if it's zero or
310 ;; less, don't copy any stack arguments.
311 (inst sub rcx-tn (fixnumize register-arg-count))
312 (inst jmp :le no-stack-args)
314 ;; Throw away any unwanted args.
315 (inst cmp rcx-tn (fixnumize (- nvals register-arg-count)))
316 (inst jmp :be count-okay)
317 (inst mov rcx-tn (fixnumize (- nvals register-arg-count)))
318 (emit-label count-okay)
319 ;; Save the number of stack values.
320 (inst mov rax-tn rcx-tn)
321 ;; Compute a pointer to where the stack args go.
323 (make-ea :qword :base rbp-tn
324 :disp (frame-byte-offset register-arg-count)))
325 ;; Save ESI, and compute a pointer to where the args come from.
326 (storew rsi-tn rbx-tn (frame-word-offset 2))
328 (make-ea :qword :base rbx-tn
329 :disp (frame-byte-offset register-arg-count)))
331 (inst shr rcx-tn word-shift) ; make word count
336 (loadw rsi-tn rbx-tn (frame-word-offset 2))
337 ;; Now we have to default the remaining args. Find out how many.
338 (inst sub rax-tn (fixnumize (- nvals register-arg-count)))
340 ;; If none, then just blow out of here.
341 (inst jmp :le restore-edi)
342 (inst mov rcx-tn rax-tn)
343 (inst shr rcx-tn word-shift) ; word count
344 ;; Load RAX with NIL for fast storing.
345 (inst mov rax-tn nil-value)
347 (emit-label default-stack-vals)
350 ;; Restore EDI, and reset the stack.
351 (emit-label restore-edi)
352 (loadw rdi-tn rbx-tn (frame-word-offset 1))
353 (inst mov rsp-tn rbx-tn)
357 ;;;; unknown values receiving
359 ;;; Emit code needed at the return point for an unknown-values call
360 ;;; for an arbitrary number of values.
362 ;;; We do the single and non-single cases with no shared code: there
363 ;;; doesn't seem to be any potential overlap, and receiving a single
364 ;;; value is more important efficiency-wise.
366 ;;; When there is a single value, we just push it on the stack,
367 ;;; returning the old SP and 1.
369 ;;; When there is a variable number of values, we move all of the
370 ;;; argument registers onto the stack, and return ARGS and NARGS.
372 ;;; ARGS and NARGS are TNs wired to the named locations. We must
373 ;;; explicitly allocate these TNs, since their lifetimes overlap with
374 ;;; the results start and count. (Also, it's nice to be able to target
376 (defun receive-unknown-values (args nargs start count)
377 (declare (type tn args nargs start count))
378 (let ((variable-values (gen-label))
380 (inst jmp :c variable-values)
382 (cond ((location= start (first *register-arg-tns*))
383 (inst push (first *register-arg-tns*))
384 (inst lea start (make-ea :qword :base rsp-tn :disp n-word-bytes)))
385 (t (inst mov start rsp-tn)
386 (inst push (first *register-arg-tns*))))
387 (inst mov count (fixnumize 1))
390 (emit-label variable-values)
391 ;; dtc: this writes the registers onto the stack even if they are
392 ;; not needed, only the number specified in rcx are used and have
393 ;; stack allocated to them. No harm is done.
395 for arg in *register-arg-tns*
397 do (storew arg args i))
404 ;;; VOP that can be inherited by unknown values receivers. The main thing this
405 ;;; handles is allocation of the result temporaries.
406 (define-vop (unknown-values-receiver)
407 (:temporary (:sc descriptor-reg :offset rbx-offset
408 :from :eval :to (:result 0))
410 (:temporary (:sc any-reg :offset rcx-offset
411 :from :eval :to (:result 1))
413 (:results (start :scs (any-reg control-stack))
414 (count :scs (any-reg control-stack))))
416 ;;;; local call with unknown values convention return
418 (defun check-ocfp-and-return-pc (old-fp return-pc)
420 (format t "*known-return: old-fp ~S, tn-kind ~S; ~S ~S~%"
421 old-fp (sb!c::tn-kind old-fp) (sb!c::tn-save-tn old-fp)
422 (sb!c::tn-kind (sb!c::tn-save-tn old-fp)))
424 (format t "*known-return: return-pc ~S, tn-kind ~S; ~S ~S~%"
425 return-pc (sb!c::tn-kind return-pc)
426 (sb!c::tn-save-tn return-pc)
427 (sb!c::tn-kind (sb!c::tn-save-tn return-pc)))
428 (unless (and (sc-is old-fp control-stack)
429 (= (tn-offset old-fp) ocfp-save-offset))
430 (error "ocfp not on stack in standard save location?"))
431 (unless (and (sc-is return-pc sap-stack)
432 (= (tn-offset return-pc) return-pc-save-offset))
433 (error "return-pc not on stack in standard save location?")))
435 ;;; Non-TR local call for a fixed number of values passed according to
436 ;;; the unknown values convention.
438 ;;; FP is the frame pointer in install before doing the call.
440 ;;; NFP would be the number-stack frame pointer if we had a separate
443 ;;; Args are the argument passing locations, which are specified only
444 ;;; to terminate their lifetimes in the caller.
446 ;;; VALUES are the return value locations (wired to the standard
447 ;;; passing locations). NVALS is the number of values received.
449 ;;; Save is the save info, which we can ignore since saving has been
452 ;;; TARGET is a continuation pointing to the start of the called
454 (define-vop (call-local)
458 (:temporary (:sc unsigned-reg) return-label)
459 (:results (values :more t))
461 (:move-args :local-call)
462 (:info arg-locs callee target nvals)
464 (:ignore nfp arg-locs args #+nil callee)
466 (trace-table-entry trace-table-call-site)
469 (let ((ret-tn (callee-return-pc-tn callee)))
471 (format t "*call-local ~S; tn-kind ~S; tn-save-tn ~S; its tn-kind ~S~%"
472 ret-tn (sb!c::tn-kind ret-tn) (sb!c::tn-save-tn ret-tn)
473 (sb!c::tn-kind (sb!c::tn-save-tn ret-tn)))
475 ;; Is the return-pc on the stack or in a register?
478 (unless (= (tn-offset ret-tn) return-pc-save-offset)
479 (error "ret-tn ~A in wrong stack slot" ret-tn))
480 #+nil (format t "*call-local: ret-tn on stack; offset=~S~%"
482 (inst lea return-label (make-fixup nil :code-object RETURN))
483 (storew return-label rbp-tn (frame-word-offset (tn-offset ret-tn))))
485 (error "ret-tn ~A in sap-reg" ret-tn))))
487 (note-this-location vop :call-site)
490 (default-unknown-values vop values nvals)
491 (trace-table-entry trace-table-normal)))
493 ;;; Non-TR local call for a variable number of return values passed according
494 ;;; to the unknown values convention. The results are the start of the values
495 ;;; glob and the number of values received.
496 (define-vop (multiple-call-local unknown-values-receiver)
500 (:temporary (:sc unsigned-reg) return-label)
502 (:move-args :local-call)
503 (:info save callee target)
504 (:ignore args save nfp #+nil callee)
507 (trace-table-entry trace-table-call-site)
510 (let ((ret-tn (callee-return-pc-tn callee)))
512 (format t "*multiple-call-local ~S; tn-kind ~S; tn-save-tn ~S; its tn-kind ~S~%"
513 ret-tn (sb!c::tn-kind ret-tn) (sb!c::tn-save-tn ret-tn)
514 (sb!c::tn-kind (sb!c::tn-save-tn ret-tn)))
516 ;; Is the return-pc on the stack or in a register?
519 #+nil (format t "*multiple-call-local: ret-tn on stack; offset=~S~%"
522 (inst lea return-label (make-fixup nil :code-object RETURN))
523 (storew return-label rbp-tn (frame-word-offset (tn-offset ret-tn))))
525 (error "multiple-call-local: return-pc not on stack."))))
527 (note-this-location vop :call-site)
530 (note-this-location vop :unknown-return)
531 (receive-unknown-values values-start nvals start count)
532 (trace-table-entry trace-table-normal)))
534 ;;;; local call with known values return
536 ;;; Non-TR local call with known return locations. Known-value return
537 ;;; works just like argument passing in local call.
539 ;;; Note: we can't use normal load-tn allocation for the fixed args,
540 ;;; since all registers may be tied up by the more operand. Instead,
541 ;;; we use MAYBE-LOAD-STACK-TN.
542 (define-vop (known-call-local)
546 (:temporary (:sc unsigned-reg) return-label)
547 (:results (res :more t))
548 (:move-args :local-call)
550 (:info save callee target)
551 (:ignore args res save nfp #+nil callee)
554 (trace-table-entry trace-table-call-site)
557 (let ((ret-tn (callee-return-pc-tn callee)))
560 (format t "*known-call-local ~S; tn-kind ~S; tn-save-tn ~S; its tn-kind ~S~%"
561 ret-tn (sb!c::tn-kind ret-tn) (sb!c::tn-save-tn ret-tn)
562 (sb!c::tn-kind (sb!c::tn-save-tn ret-tn)))
564 ;; Is the return-pc on the stack or in a register?
567 #+nil (format t "*known-call-local: ret-tn on stack; offset=~S~%"
570 (inst lea return-label (make-fixup nil :code-object RETURN))
571 (storew return-label rbp-tn (frame-word-offset (tn-offset ret-tn))))
573 (error "known-call-local: return-pc not on stack."))))
575 (note-this-location vop :call-site)
578 (note-this-location vop :known-return)
579 (trace-table-entry trace-table-normal)))
581 ;;; From Douglas Crosher
582 ;;; Return from known values call. We receive the return locations as
583 ;;; arguments to terminate their lifetimes in the returning function. We
584 ;;; restore FP and CSP and jump to the Return-PC.
585 (define-vop (known-return)
589 (:move-args :known-return)
591 (:ignore val-locs vals)
594 (check-ocfp-and-return-pc old-fp return-pc)
595 (trace-table-entry trace-table-fun-epilogue)
596 ;; Zot all of the stack except for the old-fp and return-pc.
598 (make-ea :qword :base rbp-tn
599 :disp (frame-byte-offset ocfp-save-offset)))
602 (trace-table-entry trace-table-normal)))
606 ;;; There is something of a cross-product effect with full calls.
607 ;;; Different versions are used depending on whether we know the
608 ;;; number of arguments or the name of the called function, and
609 ;;; whether we want fixed values, unknown values, or a tail call.
611 ;;; In full call, the arguments are passed creating a partial frame on
612 ;;; the stack top and storing stack arguments into that frame. On
613 ;;; entry to the callee, this partial frame is pointed to by FP.
615 ;;; This macro helps in the definition of full call VOPs by avoiding
616 ;;; code replication in defining the cross-product VOPs.
618 ;;; NAME is the name of the VOP to define.
620 ;;; NAMED is true if the first argument is an fdefinition object whose
621 ;;; definition is to be called.
623 ;;; RETURN is either :FIXED, :UNKNOWN or :TAIL:
624 ;;; -- If :FIXED, then the call is for a fixed number of values, returned in
625 ;;; the standard passing locations (passed as result operands).
626 ;;; -- If :UNKNOWN, then the result values are pushed on the stack, and the
627 ;;; result values are specified by the Start and Count as in the
628 ;;; unknown-values continuation representation.
629 ;;; -- If :TAIL, then do a tail-recursive call. No values are returned.
630 ;;; The Old-Fp and Return-PC are passed as the second and third arguments.
632 ;;; In non-tail calls, the pointer to the stack arguments is passed as
633 ;;; the last fixed argument. If Variable is false, then the passing
634 ;;; locations are passed as a more arg. Variable is true if there are
635 ;;; a variable number of arguments passed on the stack. Variable
636 ;;; cannot be specified with :TAIL return. TR variable argument call
637 ;;; is implemented separately.
639 ;;; In tail call with fixed arguments, the passing locations are
640 ;;; passed as a more arg, but there is no new-FP, since the arguments
641 ;;; have been set up in the current frame.
642 (macrolet ((define-full-call (name named return variable)
643 (aver (not (and variable (eq return :tail))))
645 ,@(when (eq return :unknown)
646 '(unknown-values-receiver)))
648 ,@(unless (eq return :tail)
649 '((new-fp :scs (any-reg) :to (:argument 1))))
651 (fun :scs (descriptor-reg control-stack)
652 :target rax :to (:argument 0))
654 ,@(when (eq return :tail)
658 ,@(unless variable '((args :more t :scs (descriptor-reg)))))
660 ,@(when (eq return :fixed)
661 '((:results (values :more t))))
663 (:save-p ,(if (eq return :tail) :compute-only t))
665 ,@(unless (or (eq return :tail) variable)
666 '((:move-args :full-call)))
670 ,@(unless (or variable (eq return :tail)) '(arg-locs))
671 ,@(unless variable '(nargs))
672 ,@(when (eq return :fixed) '(nvals))
676 ,@(unless (or variable (eq return :tail)) '(arg-locs))
677 ,@(unless variable '(args)))
679 ;; We pass either the fdefn object (for named call) or
680 ;; the actual function object (for unnamed call) in
681 ;; RAX. With named call, closure-tramp will replace it
682 ;; with the real function and invoke the real function
683 ;; for closures. Non-closures do not need this value,
684 ;; so don't care what shows up in it.
692 ;; We pass the number of arguments in RCX.
693 (:temporary (:sc unsigned-reg :offset rcx-offset :to :eval) rcx)
695 ;; With variable call, we have to load the
696 ;; register-args out of the (new) stack frame before
697 ;; doing the call. Therefore, we have to tell the
698 ;; lifetime stuff that we need to use them.
700 (mapcar (lambda (name offset)
701 `(:temporary (:sc descriptor-reg
706 *register-arg-names* *register-arg-offsets*))
708 ,@(when (eq return :tail)
709 '((:temporary (:sc unsigned-reg
714 (:generator ,(+ (if named 5 0)
716 (if (eq return :tail) 0 10)
718 (if (eq return :unknown) 25 0))
719 (trace-table-entry trace-table-call-site)
721 ;; This has to be done before the frame pointer is
722 ;; changed! RAX stores the 'lexical environment' needed
728 ;; For variable call, compute the number of
729 ;; arguments and move some of the arguments to
732 ;; Compute the number of arguments.
733 (noise '(inst mov rcx new-fp))
734 (noise '(inst sub rcx rsp-tn))
735 ;; Move the necessary args to registers,
736 ;; this moves them all even if they are
739 for name in *register-arg-names*
740 for index downfrom -1
741 do (noise `(loadw ,name new-fp ,index)))
745 (inst mov rcx (fixnumize nargs)))))
746 ,@(cond ((eq return :tail)
747 '(;; Python has figured out what frame we should
748 ;; return to so might as well use that clue.
749 ;; This seems really important to the
750 ;; implementation of things like
751 ;; (without-interrupts ...)
753 ;; dtc; Could be doing a tail call from a
754 ;; known-local-call etc in which the old-fp
755 ;; or ret-pc are in regs or in non-standard
756 ;; places. If the passing location were
757 ;; wired to the stack in standard locations
758 ;; then these moves will be un-necessary;
759 ;; this is probably best for the x86.
762 (unless (= ocfp-save-offset
764 ;; FIXME: FORMAT T for stale
765 ;; diagnostic output (several of
766 ;; them around here), ick
767 (error "** tail-call old-fp not S0~%")
768 (move old-fp-tmp old-fp)
771 (frame-word-offset ocfp-save-offset))))
772 ((any-reg descriptor-reg)
773 (error "** tail-call old-fp in reg not S0~%")
776 (frame-word-offset ocfp-save-offset))))
778 ;; For tail call, we have to push the
779 ;; return-pc so that it looks like we CALLed
780 ;; despite the fact that we are going to JMP.
781 (inst push return-pc)
784 ;; For non-tail call, we have to save our
785 ;; frame pointer and install the new frame
786 ;; pointer. We can't load stack tns after this
788 `(;; Python doesn't seem to allocate a frame
789 ;; here which doesn't leave room for the
792 ;; The variable args are on the stack and
793 ;; become the frame, but there may be <3
794 ;; args and 3 stack slots are assumed
795 ;; allocate on the call. So need to ensure
796 ;; there are at least 3 slots. This hack
799 '(inst sub rsp-tn (fixnumize 3)))
802 (storew rbp-tn new-fp
803 (frame-word-offset ocfp-save-offset))
805 (move rbp-tn new-fp) ; NB - now on new stack frame.
808 (when step-instrumenting
809 (emit-single-step-test)
811 (inst break single-step-around-trap))
814 (note-this-location vop :call-site)
816 (inst ,(if (eq return :tail) 'jmp 'call)
817 (make-ea :qword :base rax
819 '(- (* fdefn-raw-addr-slot
821 other-pointer-lowtag)
822 '(- (* closure-fun-slot n-word-bytes)
823 fun-pointer-lowtag))))
826 '((default-unknown-values vop values nvals)))
828 '((note-this-location vop :unknown-return)
829 (receive-unknown-values values-start nvals start count)))
831 (trace-table-entry trace-table-normal)))))
833 (define-full-call call nil :fixed nil)
834 (define-full-call call-named t :fixed nil)
835 (define-full-call multiple-call nil :unknown nil)
836 (define-full-call multiple-call-named t :unknown nil)
837 (define-full-call tail-call nil :tail nil)
838 (define-full-call tail-call-named t :tail nil)
840 (define-full-call call-variable nil :fixed t)
841 (define-full-call multiple-call-variable nil :unknown t))
843 ;;; This is defined separately, since it needs special code that BLT's
844 ;;; the arguments down. All the real work is done in the assembly
845 ;;; routine. We just set things up so that it can find what it needs.
846 (define-vop (tail-call-variable)
847 (:args (args :scs (any-reg control-stack) :target rsi)
848 (function :scs (descriptor-reg control-stack) :target rax)
851 (:temporary (:sc unsigned-reg :offset rsi-offset :from (:argument 0)) rsi)
852 (:temporary (:sc unsigned-reg :offset rax-offset :from (:argument 1)) rax)
853 (:temporary (:sc unsigned-reg) call-target)
855 (check-ocfp-and-return-pc old-fp return-pc)
856 ;; Move these into the passing locations if they are not already there.
859 ;; And jump to the assembly routine.
860 (inst lea call-target
862 :disp (make-fixup 'tail-call-variable :assembly-routine)))
863 (inst jmp call-target)))
865 ;;;; unknown values return
867 ;;; Return a single-value using the Unknown-Values convention.
869 ;;; pfw--get wired-tn conflicts sometimes if register sc specd for args
870 ;;; having problems targeting args to regs -- using temps instead.
872 ;;; First off, modifying the return-pc defeats the branch-prediction
873 ;;; optimizations on modern CPUs quite handily. Second, we can do all
874 ;;; this without needing a temp register. Fixed the latter, at least.
875 ;;; -- AB 2006/Feb/04
876 (define-vop (return-single)
882 (check-ocfp-and-return-pc old-fp return-pc)
883 (trace-table-entry trace-table-fun-epilogue)
884 ;; Drop stack above old-fp
885 (inst lea rsp-tn (make-ea :qword :base rbp-tn
886 :disp (frame-byte-offset (tn-offset old-fp))))
887 ;; Clear the multiple-value return flag
889 ;; Restore the old frame pointer
891 ;; And return, dropping the rest of the stack as we go.
894 ;;; Do unknown-values return of a fixed (other than 1) number of
895 ;;; values. The VALUES are required to be set up in the standard
896 ;;; passing locations. NVALS is the number of values returned.
898 ;;; Basically, we just load RCX with the number of values returned and
899 ;;; RBX with a pointer to the values, set RSP to point to the end of
900 ;;; the values, and jump directly to return-pc.
903 (return-pc :to (:eval 1))
907 ;; In the case of other than one value, we need these registers to
908 ;; tell the caller where they are and how many there are.
909 (:temporary (:sc unsigned-reg :offset rbx-offset) rbx)
910 (:temporary (:sc unsigned-reg :offset rcx-offset) rcx)
911 ;; We need to stretch the lifetime of return-pc past the argument
912 ;; registers so that we can default the argument registers without
913 ;; trashing return-pc.
914 (:temporary (:sc unsigned-reg :offset (first *register-arg-offsets*)
916 (:temporary (:sc unsigned-reg :offset (second *register-arg-offsets*)
918 (:temporary (:sc unsigned-reg :offset (third *register-arg-offsets*)
922 (check-ocfp-and-return-pc old-fp return-pc)
923 (trace-table-entry trace-table-fun-epilogue)
924 ;; Establish the values pointer and values count.
927 (zeroize rcx) ; smaller
928 (inst mov rcx (fixnumize nvals)))
929 ;; Restore the frame pointer.
931 ;; Clear as much of the stack as possible, but not past the return
934 (make-ea :qword :base rbx
935 :disp (frame-byte-offset (max (1- nvals)
936 return-pc-save-offset))))
937 ;; Pre-default any argument register that need it.
938 (when (< nvals register-arg-count)
939 (let* ((arg-tns (nthcdr nvals (list a0 a1 a2)))
940 (first (first arg-tns)))
941 (inst mov first nil-value)
942 (dolist (tn (cdr arg-tns))
943 (inst mov tn first))))
944 ;; Set the multiple value return flag.
946 ;; And away we go. Except that return-pc is still on the
947 ;; stack and we've changed the stack pointer. So we have to
948 ;; tell it to index off of RBX instead of RBP.
950 ;; Return popping the return address and what's earlier in
952 (inst ret (* return-pc-save-offset n-word-bytes)))
954 ;; This is handled in RETURN-SINGLE.
955 (error "nvalues is 1"))
957 ;; Thou shalt not JMP unto thy return address.
958 (inst push (make-ea :qword :base rbx
959 :disp (frame-byte-offset (tn-offset return-pc))))
962 (trace-table-entry trace-table-normal)))
964 ;;; Do unknown-values return of an arbitrary number of values (passed
965 ;;; on the stack.) We check for the common case of a single return
966 ;;; value, and do that inline using the normal single value return
967 ;;; convention. Otherwise, we branch off to code that calls an
968 ;;; assembly-routine.
970 ;;; The assembly routine takes the following args:
971 ;;; RAX -- the return-pc to finally jump to.
972 ;;; RBX -- pointer to where to put the values.
973 ;;; RCX -- number of values to find there.
974 ;;; RSI -- pointer to where to find the values.
975 (define-vop (return-multiple)
976 (:args (old-fp :to (:eval 1) :target old-fp-temp)
977 (return-pc :target rax)
978 (vals :scs (any-reg) :target rsi)
979 (nvals :scs (any-reg) :target rcx))
980 (:temporary (:sc unsigned-reg :offset rax-offset :from (:argument 1)) rax)
981 (:temporary (:sc unsigned-reg :offset rsi-offset :from (:argument 2)) rsi)
982 (:temporary (:sc unsigned-reg :offset rcx-offset :from (:argument 3)) rcx)
983 (:temporary (:sc unsigned-reg :offset rbx-offset :from (:eval 0)) rbx)
984 (:temporary (:sc unsigned-reg) return-asm)
985 (:temporary (:sc descriptor-reg :offset (first *register-arg-offsets*)
987 (:temporary (:sc unsigned-reg :from (:eval 1)) old-fp-temp)
990 (check-ocfp-and-return-pc old-fp return-pc)
991 (trace-table-entry trace-table-fun-epilogue)
992 ;; Load the return-pc.
994 (unless (policy node (> space speed))
995 ;; Check for the single case.
996 (let ((not-single (gen-label)))
997 (inst cmp nvals (fixnumize 1))
998 (inst jmp :ne not-single)
999 ;; Return with one value.
1001 ;; Clear the stack. We load old-fp into a register before clearing
1003 (move old-fp-temp old-fp)
1004 (move rsp-tn rbp-tn)
1005 (move rbp-tn old-fp-temp)
1006 ;; clear the multiple-value return flag
1011 ;; Nope, not the single case. Jump to the assembly routine.
1012 (emit-label not-single)))
1016 (move rbp-tn old-fp)
1017 (inst lea return-asm
1018 (make-ea :qword :disp (make-fixup 'return-multiple
1019 :assembly-routine)))
1020 (inst jmp return-asm)
1021 (trace-table-entry trace-table-normal)))
1025 ;;; We don't need to do anything special for regular functions.
1026 (define-vop (setup-environment)
1030 ;; Don't bother doing anything.
1033 ;;; Get the lexical environment from its passing location.
1034 (define-vop (setup-closure-environment)
1035 (:results (closure :scs (descriptor-reg)))
1040 (move closure rax-tn)))
1042 ;;; Copy a &MORE arg from the argument area to the end of the current
1043 ;;; frame. FIXED is the number of non-&MORE arguments.
1044 (define-vop (copy-more-arg)
1045 (:temporary (:sc any-reg :offset r8-offset) copy-index)
1046 (:temporary (:sc any-reg :offset r9-offset) source)
1047 (:temporary (:sc descriptor-reg :offset r10-offset) temp)
1050 ;; Avoid the copy if there are no more args.
1051 (cond ((zerop fixed)
1052 (inst jrcxz JUST-ALLOC-FRAME))
1054 (inst cmp rcx-tn (fixnumize fixed))
1055 (inst jmp :be JUST-ALLOC-FRAME)))
1057 ;; Allocate the space on the stack.
1058 ;; stack = rbp - (max 3 frame-size) - (nargs - fixed)
1060 (make-ea :qword :base rbp-tn
1061 :disp (- (fixnumize fixed)
1063 (max 3 (sb-allocated-size 'stack))))))
1064 (inst sub rbx-tn rcx-tn) ; Got the new stack in rbx
1065 (inst mov rsp-tn rbx-tn)
1067 ;; Now: nargs>=1 && nargs>fixed
1069 ;; Save the original count of args.
1070 (inst mov rbx-tn rcx-tn)
1072 (cond ((< fixed register-arg-count)
1073 ;; We must stop when we run out of stack args, not when we
1074 ;; run out of more args.
1075 ;; Number to copy = nargs-3
1076 (inst sub rcx-tn (fixnumize register-arg-count))
1077 ;; Everything of interest in registers.
1078 (inst jmp :be DO-REGS))
1080 ;; Number to copy = nargs-fixed
1081 (inst sub rcx-tn (fixnumize fixed))))
1083 ;; Initialize R8 to be the end of args.
1084 (inst mov source rbp-tn)
1085 (inst sub source rbx-tn)
1087 ;; We need to copy from downwards up to avoid overwriting some of
1088 ;; the yet uncopied args. So we need to use R9 as the copy index
1089 ;; and RCX as the loop counter, rather than using RCX for both.
1090 (zeroize copy-index)
1092 ;; We used to use REP MOVS here, but on modern x86 it performs
1093 ;; much worse than an explicit loop for small blocks.
1095 (inst mov temp (make-ea :qword :base source :index copy-index))
1096 (inst mov (make-ea :qword :base rsp-tn :index copy-index) temp)
1097 (inst add copy-index n-word-bytes)
1098 (inst sub rcx-tn n-word-bytes)
1099 (inst jmp :nz COPY-LOOP)
1104 (inst mov rcx-tn rbx-tn)
1106 ;; Here: nargs>=1 && nargs>fixed
1107 (when (< fixed register-arg-count)
1108 ;; Now we have to deposit any more args that showed up in
1112 ;; Store it relative to rbp
1113 (inst mov (make-ea :qword :base rbp-tn
1114 :disp (- (* n-word-bytes
1116 (max 3 (sb-allocated-size 'stack))))))
1117 (nth i *register-arg-tns*))
1120 (when (>= i register-arg-count)
1123 ;; Don't deposit any more than there are.
1125 (inst test rcx-tn rcx-tn)
1126 (inst cmp rcx-tn (fixnumize i)))
1127 (inst jmp :eq DONE)))
1133 (make-ea :qword :base rbp-tn
1134 :disp (- (* n-word-bytes
1135 (max 3 (sb-allocated-size 'stack))))))
1139 (define-vop (more-kw-arg)
1140 (:translate sb!c::%more-kw-arg)
1141 (:policy :fast-safe)
1142 (:args (object :scs (descriptor-reg) :to (:result 1))
1143 (index :scs (any-reg) :to (:result 1) :target keyword))
1144 (:arg-types * tagged-num)
1145 (:results (value :scs (descriptor-reg any-reg))
1146 (keyword :scs (descriptor-reg any-reg)))
1149 (inst mov value (make-ea :qword :base object :index index))
1150 (inst mov keyword (make-ea :qword :base object :index index
1151 :disp n-word-bytes))))
1153 (define-vop (more-arg)
1154 (:translate sb!c::%more-arg)
1155 (:policy :fast-safe)
1156 (:args (object :scs (descriptor-reg) :to (:result 1))
1157 (index :scs (any-reg) :to (:result 1) :target value))
1158 (:arg-types * tagged-num)
1159 (:results (value :scs (descriptor-reg any-reg)))
1164 (inst mov value (make-ea :qword :base object :index value))))
1166 ;;; Turn more arg (context, count) into a list.
1167 (define-vop (listify-rest-args)
1168 (:translate %listify-rest-args)
1170 (:args (context :scs (descriptor-reg) :target src)
1171 (count :scs (any-reg) :target rcx))
1172 (:arg-types * tagged-num)
1173 (:temporary (:sc unsigned-reg :offset rsi-offset :from (:argument 0)) src)
1174 (:temporary (:sc unsigned-reg :offset rcx-offset :from (:argument 1)) rcx)
1175 (:temporary (:sc unsigned-reg :offset rax-offset) rax)
1176 (:temporary (:sc unsigned-reg) dst)
1177 (:results (result :scs (descriptor-reg)))
1180 (let ((enter (gen-label))
1183 (stack-allocate-p (node-stack-allocate-p node)))
1186 ;; Check to see whether there are no args, and just return NIL if so.
1187 (inst mov result nil-value)
1189 (inst lea dst (make-ea :qword :base rcx :index rcx))
1190 (maybe-pseudo-atomic stack-allocate-p
1191 (allocation dst dst node stack-allocate-p list-pointer-lowtag)
1192 (inst shr rcx (1- n-lowtag-bits))
1193 ;; Set decrement mode (successive args at lower addresses)
1195 ;; Set up the result.
1197 ;; Jump into the middle of the loop, 'cause that's where we want
1201 ;; Compute a pointer to the next cons.
1202 (inst add dst (* cons-size n-word-bytes))
1203 ;; Store a pointer to this cons in the CDR of the previous cons.
1204 (storew dst dst -1 list-pointer-lowtag)
1206 ;; Grab one value and stash it in the car of this cons.
1208 (storew rax dst 0 list-pointer-lowtag)
1209 ;; Go back for more.
1212 ;; NIL out the last cons.
1213 (storew nil-value dst 1 list-pointer-lowtag)
1215 (emit-label done))))
1217 ;;; Return the location and size of the &MORE arg glob created by
1218 ;;; COPY-MORE-ARG. SUPPLIED is the total number of arguments supplied
1219 ;;; (originally passed in RCX). FIXED is the number of non-rest
1222 ;;; We must duplicate some of the work done by COPY-MORE-ARG, since at
1223 ;;; that time the environment is in a pretty brain-damaged state,
1224 ;;; preventing this info from being returned as values. What we do is
1225 ;;; compute supplied - fixed, and return a pointer that many words
1226 ;;; below the current stack top.
1227 (define-vop (more-arg-context)
1228 (:policy :fast-safe)
1229 (:translate sb!c::%more-arg-context)
1230 (:args (supplied :scs (any-reg) :target count))
1231 (:arg-types positive-fixnum (:constant fixnum))
1233 (:results (context :scs (descriptor-reg))
1234 (count :scs (any-reg)))
1235 (:result-types t tagged-num)
1236 (:note "more-arg-context")
1238 (move count supplied)
1239 ;; SP at this point points at the last arg pushed.
1240 ;; Point to the first more-arg, not above it.
1241 (inst lea context (make-ea :qword :base rsp-tn
1242 :index count :scale 1
1243 :disp (- (+ (fixnumize fixed) n-word-bytes))))
1244 (unless (zerop fixed)
1245 (inst sub count (fixnumize fixed)))))
1247 ;;; Signal wrong argument count error if NARGS isn't equal to COUNT.
1248 (define-vop (verify-arg-count)
1249 (:policy :fast-safe)
1250 (:translate sb!c::%verify-arg-count)
1251 (:args (nargs :scs (any-reg)))
1252 (:arg-types positive-fixnum (:constant t))
1255 (:save-p :compute-only)
1258 (generate-error-code vop 'invalid-arg-count-error nargs)))
1260 (inst test nargs nargs) ; smaller instruction
1261 (inst cmp nargs (fixnumize count)))
1262 (inst jmp :ne err-lab))))
1264 ;;; Various other error signallers.
1265 (macrolet ((def (name error translate &rest args)
1266 `(define-vop (,name)
1268 `((:policy :fast-safe)
1269 (:translate ,translate)))
1270 (:args ,@(mapcar (lambda (arg)
1271 `(,arg :scs (any-reg descriptor-reg)))
1274 (:save-p :compute-only)
1276 (error-call vop ',error ,@args)))))
1277 (def arg-count-error invalid-arg-count-error
1278 sb!c::%arg-count-error nargs)
1279 (def type-check-error object-not-type-error sb!c::%type-check-error
1281 (def layout-invalid-error layout-invalid-error sb!c::%layout-invalid-error
1283 (def odd-key-args-error odd-key-args-error
1284 sb!c::%odd-key-args-error)
1285 (def unknown-key-arg-error unknown-key-arg-error
1286 sb!c::%unknown-key-arg-error key)
1287 (def nil-fun-returned-error nil-fun-returned-error nil fun))
1291 (defun emit-single-step-test ()
1292 ;; We use different ways of representing whether stepping is on on
1293 ;; +SB-THREAD / -SB-THREAD: on +SB-THREAD, we use a slot in the
1294 ;; thread structure. On -SB-THREAD we use the value of a static
1295 ;; symbol. Things are done this way, since reading a thread-local
1296 ;; slot from a symbol would require an extra register on +SB-THREAD,
1297 ;; and reading a slot from a thread structure would require an extra
1298 ;; register on -SB-THREAD. While this isn't critical for x86-64,
1299 ;; it's more serious for x86.
1301 (inst cmp (make-ea :qword
1302 :base thread-base-tn
1303 :disp (* thread-stepping-slot n-word-bytes))
1306 (inst cmp (make-ea :qword
1307 :disp (+ nil-value (static-symbol-offset
1308 'sb!impl::*stepping*)
1309 (* symbol-value-slot n-word-bytes)
1310 (- other-pointer-lowtag)))
1313 (define-vop (step-instrument-before-vop)
1314 (:policy :fast-safe)
1317 (emit-single-step-test)
1319 (inst break single-step-before-trap)
1321 (note-this-location vop :step-before-vop)))