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.
237 (inst jmp default-stack-slots)
239 (emit-label regs-defaulted)
241 (inst mov rax-tn nil-value)
242 (collect ((defaults))
243 (do ((i register-arg-count (1+ i))
244 (val (do ((i 0 (1+ i))
245 (val values (tn-ref-across val)))
246 ((= i register-arg-count) val))
247 (tn-ref-across val)))
249 (let ((default-lab (gen-label))
251 (first-stack-arg-p (= i register-arg-count)))
252 (defaults (cons default-lab (cons tn first-stack-arg-p)))
254 (inst cmp rcx-tn (fixnumize i))
255 (inst jmp :be default-lab)
256 (when first-stack-arg-p
257 ;; There are stack args so the frame of the callee is
258 ;; still there, save RDX in its first slot temporalily.
259 (storew rdx-tn rbx-tn -1))
260 (loadw rdx-tn rbx-tn (frame-word-offset i))
261 (inst mov tn rdx-tn)))
263 (emit-label defaulting-done)
264 (loadw rdx-tn rbx-tn -1)
267 (let ((defaults (defaults)))
269 (assemble (*elsewhere*)
270 (trace-table-entry trace-table-fun-prologue)
271 (emit-label default-stack-slots)
272 (dolist (default defaults)
273 (emit-label (car default))
275 ;; We are setting the first stack argument to NIL.
276 ;; The callee's stack frame is dead, save RDX by
277 ;; pushing it to the stack, it will end up at same
278 ;; place as in the (STOREW RDX-TN RBX-TN -1) case
281 (inst mov (second default) rax-tn))
282 (inst jmp defaulting-done)
283 (trace-table-entry trace-table-normal)))))))
285 (let ((regs-defaulted (gen-label))
286 (restore-edi (gen-label))
287 (no-stack-args (gen-label))
288 (default-stack-vals (gen-label))
289 (count-okay (gen-label)))
290 (note-this-location vop :unknown-return)
291 ;; Branch off to the MV case.
292 (inst jmp :c regs-defaulted)
294 ;; Default the register args, and set up the stack as if we
295 ;; entered the MV return point.
296 (inst mov rbx-tn rsp-tn)
297 (inst mov rdi-tn nil-value)
298 (inst mov rsi-tn rdi-tn)
299 ;; Compute a pointer to where to put the [defaulted] stack values.
300 (emit-label no-stack-args)
304 (make-ea :qword :base rbp-tn
305 :disp (frame-byte-offset register-arg-count)))
306 ;; Load RAX with NIL so we can quickly store it, and set up
307 ;; stuff for the loop.
308 (inst mov rax-tn nil-value)
310 (inst mov rcx-tn (- nvals register-arg-count))
311 ;; Jump into the default loop.
312 (inst jmp default-stack-vals)
314 ;; The regs are defaulted. We need to copy any stack arguments,
315 ;; and then default the remaining stack arguments.
316 (emit-label regs-defaulted)
318 (storew rdi-tn rbx-tn (frame-word-offset 1))
319 ;; Compute the number of stack arguments, and if it's zero or
320 ;; less, don't copy any stack arguments.
321 (inst sub rcx-tn (fixnumize register-arg-count))
322 (inst jmp :le no-stack-args)
324 ;; Throw away any unwanted args.
325 (inst cmp rcx-tn (fixnumize (- nvals register-arg-count)))
326 (inst jmp :be count-okay)
327 (inst mov rcx-tn (fixnumize (- nvals register-arg-count)))
328 (emit-label count-okay)
329 ;; Save the number of stack values.
330 (inst mov rax-tn rcx-tn)
331 ;; Compute a pointer to where the stack args go.
333 (make-ea :qword :base rbp-tn
334 :disp (frame-byte-offset register-arg-count)))
335 ;; Save ESI, and compute a pointer to where the args come from.
336 (storew rsi-tn rbx-tn (frame-word-offset 2))
338 (make-ea :qword :base rbx-tn
339 :disp (frame-byte-offset register-arg-count)))
341 (inst shr rcx-tn word-shift) ; make word count
346 (loadw rsi-tn rbx-tn (frame-word-offset 2))
347 ;; Now we have to default the remaining args. Find out how many.
348 (inst sub rax-tn (fixnumize (- nvals register-arg-count)))
350 ;; If none, then just blow out of here.
351 (inst jmp :le restore-edi)
352 (inst mov rcx-tn rax-tn)
353 (inst shr rcx-tn word-shift) ; word count
354 ;; Load RAX with NIL for fast storing.
355 (inst mov rax-tn nil-value)
357 (emit-label default-stack-vals)
360 ;; Restore EDI, and reset the stack.
361 (emit-label restore-edi)
362 (loadw rdi-tn rbx-tn (frame-word-offset 1))
363 (inst mov rsp-tn rbx-tn)
367 ;;;; unknown values receiving
369 ;;; Emit code needed at the return point for an unknown-values call
370 ;;; for an arbitrary number of values.
372 ;;; We do the single and non-single cases with no shared code: there
373 ;;; doesn't seem to be any potential overlap, and receiving a single
374 ;;; value is more important efficiency-wise.
376 ;;; When there is a single value, we just push it on the stack,
377 ;;; returning the old SP and 1.
379 ;;; When there is a variable number of values, we move all of the
380 ;;; argument registers onto the stack, and return ARGS and NARGS.
382 ;;; ARGS and NARGS are TNs wired to the named locations. We must
383 ;;; explicitly allocate these TNs, since their lifetimes overlap with
384 ;;; the results start and count. (Also, it's nice to be able to target
386 (defun receive-unknown-values (args nargs start count)
387 (declare (type tn args nargs start count))
388 (let ((variable-values (gen-label))
389 (stack-values (gen-label))
391 (inst jmp :c variable-values)
393 (cond ((location= start (first *register-arg-tns*))
394 (inst push (first *register-arg-tns*))
395 (inst lea start (make-ea :qword :base rsp-tn :disp n-word-bytes)))
396 (t (inst mov start rsp-tn)
397 (inst push (first *register-arg-tns*))))
398 (inst mov count (fixnumize 1))
401 (emit-label variable-values)
402 ;; The stack frame is burnt and RETurned from if there are no
403 ;; stack values. In this case quickly reallocate sufficient space.
404 (inst cmp nargs (fixnumize register-arg-count))
405 (inst jmp :g stack-values)
406 (inst sub rsp-tn nargs)
407 (emit-label stack-values)
408 ;; dtc: this writes the registers onto the stack even if they are
409 ;; not needed, only the number specified in rcx are used and have
410 ;; stack allocated to them. No harm is done.
412 for arg in *register-arg-tns*
414 do (storew arg args i))
421 ;;; VOP that can be inherited by unknown values receivers. The main thing this
422 ;;; handles is allocation of the result temporaries.
423 (define-vop (unknown-values-receiver)
424 (:temporary (:sc descriptor-reg :offset rbx-offset
425 :from :eval :to (:result 0))
427 (:temporary (:sc any-reg :offset rcx-offset
428 :from :eval :to (:result 1))
430 (:results (start :scs (any-reg control-stack))
431 (count :scs (any-reg control-stack))))
433 ;;;; local call with unknown values convention return
435 (defun check-ocfp-and-return-pc (old-fp return-pc)
437 (format t "*known-return: old-fp ~S, tn-kind ~S; ~S ~S~%"
438 old-fp (sb!c::tn-kind old-fp) (sb!c::tn-save-tn old-fp)
439 (sb!c::tn-kind (sb!c::tn-save-tn old-fp)))
441 (format t "*known-return: return-pc ~S, tn-kind ~S; ~S ~S~%"
442 return-pc (sb!c::tn-kind return-pc)
443 (sb!c::tn-save-tn return-pc)
444 (sb!c::tn-kind (sb!c::tn-save-tn return-pc)))
445 (unless (and (sc-is old-fp control-stack)
446 (= (tn-offset old-fp) ocfp-save-offset))
447 (error "ocfp not on stack in standard save location?"))
448 (unless (and (sc-is return-pc sap-stack)
449 (= (tn-offset return-pc) return-pc-save-offset))
450 (error "return-pc not on stack in standard save location?")))
452 ;;; Non-TR local call for a fixed number of values passed according to
453 ;;; the unknown values convention.
455 ;;; FP is the frame pointer in install before doing the call.
457 ;;; NFP would be the number-stack frame pointer if we had a separate
460 ;;; Args are the argument passing locations, which are specified only
461 ;;; to terminate their lifetimes in the caller.
463 ;;; VALUES are the return value locations (wired to the standard
464 ;;; passing locations). NVALS is the number of values received.
466 ;;; Save is the save info, which we can ignore since saving has been
469 ;;; TARGET is a continuation pointing to the start of the called
471 (define-vop (call-local)
475 (:temporary (:sc unsigned-reg) return-label)
476 (:results (values :more t))
478 (:move-args :local-call)
479 (:info arg-locs callee target nvals)
481 (:ignore nfp arg-locs args #+nil callee)
483 (trace-table-entry trace-table-call-site)
486 (let ((ret-tn (callee-return-pc-tn callee)))
488 (format t "*call-local ~S; tn-kind ~S; tn-save-tn ~S; its tn-kind ~S~%"
489 ret-tn (sb!c::tn-kind ret-tn) (sb!c::tn-save-tn ret-tn)
490 (sb!c::tn-kind (sb!c::tn-save-tn ret-tn)))
492 ;; Is the return-pc on the stack or in a register?
495 (unless (= (tn-offset ret-tn) return-pc-save-offset)
496 (error "ret-tn ~A in wrong stack slot" ret-tn))
497 #+nil (format t "*call-local: ret-tn on stack; offset=~S~%"
499 (inst lea return-label (make-fixup nil :code-object RETURN))
500 (storew return-label rbp-tn (frame-word-offset (tn-offset ret-tn))))
502 (error "ret-tn ~A in sap-reg" ret-tn))))
504 (note-this-location vop :call-site)
507 (default-unknown-values vop values nvals)
508 (trace-table-entry trace-table-normal)))
510 ;;; Non-TR local call for a variable number of return values passed according
511 ;;; to the unknown values convention. The results are the start of the values
512 ;;; glob and the number of values received.
513 (define-vop (multiple-call-local unknown-values-receiver)
517 (:temporary (:sc unsigned-reg) return-label)
519 (:move-args :local-call)
520 (:info save callee target)
521 (:ignore args save nfp #+nil callee)
524 (trace-table-entry trace-table-call-site)
527 (let ((ret-tn (callee-return-pc-tn callee)))
529 (format t "*multiple-call-local ~S; tn-kind ~S; tn-save-tn ~S; its tn-kind ~S~%"
530 ret-tn (sb!c::tn-kind ret-tn) (sb!c::tn-save-tn ret-tn)
531 (sb!c::tn-kind (sb!c::tn-save-tn ret-tn)))
533 ;; Is the return-pc on the stack or in a register?
536 #+nil (format t "*multiple-call-local: ret-tn on stack; offset=~S~%"
539 (inst lea return-label (make-fixup nil :code-object RETURN))
540 (storew return-label rbp-tn (frame-word-offset (tn-offset ret-tn))))
542 (error "multiple-call-local: return-pc not on stack."))))
544 (note-this-location vop :call-site)
547 (note-this-location vop :unknown-return)
548 (receive-unknown-values values-start nvals start count)
549 (trace-table-entry trace-table-normal)))
551 ;;;; local call with known values return
553 ;;; Non-TR local call with known return locations. Known-value return
554 ;;; works just like argument passing in local call.
556 ;;; Note: we can't use normal load-tn allocation for the fixed args,
557 ;;; since all registers may be tied up by the more operand. Instead,
558 ;;; we use MAYBE-LOAD-STACK-TN.
559 (define-vop (known-call-local)
563 (:temporary (:sc unsigned-reg) return-label)
564 (:results (res :more t))
565 (:move-args :local-call)
567 (:info save callee target)
568 (:ignore args res save nfp #+nil callee)
571 (trace-table-entry trace-table-call-site)
574 (let ((ret-tn (callee-return-pc-tn callee)))
577 (format t "*known-call-local ~S; tn-kind ~S; tn-save-tn ~S; its tn-kind ~S~%"
578 ret-tn (sb!c::tn-kind ret-tn) (sb!c::tn-save-tn ret-tn)
579 (sb!c::tn-kind (sb!c::tn-save-tn ret-tn)))
581 ;; Is the return-pc on the stack or in a register?
584 #+nil (format t "*known-call-local: ret-tn on stack; offset=~S~%"
587 (inst lea return-label (make-fixup nil :code-object RETURN))
588 (storew return-label rbp-tn (frame-word-offset (tn-offset ret-tn))))
590 (error "known-call-local: return-pc not on stack."))))
592 (note-this-location vop :call-site)
595 (note-this-location vop :known-return)
596 (trace-table-entry trace-table-normal)))
598 ;;; From Douglas Crosher
599 ;;; Return from known values call. We receive the return locations as
600 ;;; arguments to terminate their lifetimes in the returning function. We
601 ;;; restore FP and CSP and jump to the Return-PC.
602 (define-vop (known-return)
606 (:move-args :known-return)
608 (:ignore val-locs vals)
611 (check-ocfp-and-return-pc old-fp return-pc)
612 (trace-table-entry trace-table-fun-epilogue)
613 ;; Zot all of the stack except for the old-fp and return-pc.
615 (make-ea :qword :base rbp-tn
616 :disp (frame-byte-offset ocfp-save-offset)))
619 (trace-table-entry trace-table-normal)))
623 ;;; There is something of a cross-product effect with full calls.
624 ;;; Different versions are used depending on whether we know the
625 ;;; number of arguments or the name of the called function, and
626 ;;; whether we want fixed values, unknown values, or a tail call.
628 ;;; In full call, the arguments are passed creating a partial frame on
629 ;;; the stack top and storing stack arguments into that frame. On
630 ;;; entry to the callee, this partial frame is pointed to by FP.
632 ;;; This macro helps in the definition of full call VOPs by avoiding
633 ;;; code replication in defining the cross-product VOPs.
635 ;;; NAME is the name of the VOP to define.
637 ;;; NAMED is true if the first argument is an fdefinition object whose
638 ;;; definition is to be called.
640 ;;; RETURN is either :FIXED, :UNKNOWN or :TAIL:
641 ;;; -- If :FIXED, then the call is for a fixed number of values, returned in
642 ;;; the standard passing locations (passed as result operands).
643 ;;; -- If :UNKNOWN, then the result values are pushed on the stack, and the
644 ;;; result values are specified by the Start and Count as in the
645 ;;; unknown-values continuation representation.
646 ;;; -- If :TAIL, then do a tail-recursive call. No values are returned.
647 ;;; The Old-Fp and Return-PC are passed as the second and third arguments.
649 ;;; In non-tail calls, the pointer to the stack arguments is passed as
650 ;;; the last fixed argument. If Variable is false, then the passing
651 ;;; locations are passed as a more arg. Variable is true if there are
652 ;;; a variable number of arguments passed on the stack. Variable
653 ;;; cannot be specified with :TAIL return. TR variable argument call
654 ;;; is implemented separately.
656 ;;; In tail call with fixed arguments, the passing locations are
657 ;;; passed as a more arg, but there is no new-FP, since the arguments
658 ;;; have been set up in the current frame.
659 (macrolet ((define-full-call (name named return variable)
660 (aver (not (and variable (eq return :tail))))
662 ,@(when (eq return :unknown)
663 '(unknown-values-receiver)))
665 ,@(unless (eq return :tail)
666 '((new-fp :scs (any-reg) :to (:argument 1))))
668 (fun :scs (descriptor-reg control-stack)
669 :target rax :to (:argument 0))
671 ,@(when (eq return :tail)
675 ,@(unless variable '((args :more t :scs (descriptor-reg)))))
677 ,@(when (eq return :fixed)
678 '((:results (values :more t))))
680 (:save-p ,(if (eq return :tail) :compute-only t))
682 ,@(unless (or (eq return :tail) variable)
683 '((:move-args :full-call)))
687 ,@(unless (or variable (eq return :tail)) '(arg-locs))
688 ,@(unless variable '(nargs))
689 ,@(when (eq return :fixed) '(nvals))
693 ,@(unless (or variable (eq return :tail)) '(arg-locs))
694 ,@(unless variable '(args)))
696 ;; We pass either the fdefn object (for named call) or
697 ;; the actual function object (for unnamed call) in
698 ;; RAX. With named call, closure-tramp will replace it
699 ;; with the real function and invoke the real function
700 ;; for closures. Non-closures do not need this value,
701 ;; so don't care what shows up in it.
709 ;; We pass the number of arguments in RCX.
710 (:temporary (:sc unsigned-reg :offset rcx-offset :to :eval) rcx)
712 ;; With variable call, we have to load the
713 ;; register-args out of the (new) stack frame before
714 ;; doing the call. Therefore, we have to tell the
715 ;; lifetime stuff that we need to use them.
717 (mapcar (lambda (name offset)
718 `(:temporary (:sc descriptor-reg
723 *register-arg-names* *register-arg-offsets*))
725 ,@(when (eq return :tail)
726 '((:temporary (:sc unsigned-reg
731 (:generator ,(+ (if named 5 0)
733 (if (eq return :tail) 0 10)
735 (if (eq return :unknown) 25 0))
736 (trace-table-entry trace-table-call-site)
738 ;; This has to be done before the frame pointer is
739 ;; changed! RAX stores the 'lexical environment' needed
745 ;; For variable call, compute the number of
746 ;; arguments and move some of the arguments to
749 ;; Compute the number of arguments.
750 (noise '(inst mov rcx new-fp))
751 (noise '(inst sub rcx rsp-tn))
752 ;; Move the necessary args to registers,
753 ;; this moves them all even if they are
756 for name in *register-arg-names*
757 for index downfrom -1
758 do (noise `(loadw ,name new-fp ,index)))
762 (inst mov rcx (fixnumize nargs)))))
763 ,@(cond ((eq return :tail)
764 '(;; Python has figured out what frame we should
765 ;; return to so might as well use that clue.
766 ;; This seems really important to the
767 ;; implementation of things like
768 ;; (without-interrupts ...)
770 ;; dtc; Could be doing a tail call from a
771 ;; known-local-call etc in which the old-fp
772 ;; or ret-pc are in regs or in non-standard
773 ;; places. If the passing location were
774 ;; wired to the stack in standard locations
775 ;; then these moves will be un-necessary;
776 ;; this is probably best for the x86.
779 (unless (= ocfp-save-offset
781 ;; FIXME: FORMAT T for stale
782 ;; diagnostic output (several of
783 ;; them around here), ick
784 (error "** tail-call old-fp not S0~%")
785 (move old-fp-tmp old-fp)
788 (frame-word-offset ocfp-save-offset))))
789 ((any-reg descriptor-reg)
790 (error "** tail-call old-fp in reg not S0~%")
793 (frame-word-offset ocfp-save-offset))))
795 ;; For tail call, we have to push the
796 ;; return-pc so that it looks like we CALLed
797 ;; despite the fact that we are going to JMP.
798 (inst push return-pc)
801 ;; For non-tail call, we have to save our
802 ;; frame pointer and install the new frame
803 ;; pointer. We can't load stack tns after this
805 `(;; Python doesn't seem to allocate a frame
806 ;; here which doesn't leave room for the
809 ;; The variable args are on the stack and
810 ;; become the frame, but there may be <3
811 ;; args and 3 stack slots are assumed
812 ;; allocate on the call. So need to ensure
813 ;; there are at least 3 slots. This hack
816 '(inst sub rsp-tn (fixnumize 3)))
819 (storew rbp-tn new-fp
820 (frame-word-offset ocfp-save-offset))
822 (move rbp-tn new-fp) ; NB - now on new stack frame.
825 (when step-instrumenting
826 (emit-single-step-test)
828 (inst break single-step-around-trap))
831 (note-this-location vop :call-site)
833 (inst ,(if (eq return :tail) 'jmp 'call)
834 (make-ea :qword :base rax
836 '(- (* fdefn-raw-addr-slot
838 other-pointer-lowtag)
839 '(- (* closure-fun-slot n-word-bytes)
840 fun-pointer-lowtag))))
843 '((default-unknown-values vop values nvals)))
845 '((note-this-location vop :unknown-return)
846 (receive-unknown-values values-start nvals start count)))
848 (trace-table-entry trace-table-normal)))))
850 (define-full-call call nil :fixed nil)
851 (define-full-call call-named t :fixed nil)
852 (define-full-call multiple-call nil :unknown nil)
853 (define-full-call multiple-call-named t :unknown nil)
854 (define-full-call tail-call nil :tail nil)
855 (define-full-call tail-call-named t :tail nil)
857 (define-full-call call-variable nil :fixed t)
858 (define-full-call multiple-call-variable nil :unknown t))
860 ;;; This is defined separately, since it needs special code that BLT's
861 ;;; the arguments down. All the real work is done in the assembly
862 ;;; routine. We just set things up so that it can find what it needs.
863 (define-vop (tail-call-variable)
864 (:args (args :scs (any-reg control-stack) :target rsi)
865 (function :scs (descriptor-reg control-stack) :target rax)
868 (:temporary (:sc unsigned-reg :offset rsi-offset :from (:argument 0)) rsi)
869 (:temporary (:sc unsigned-reg :offset rax-offset :from (:argument 1)) rax)
870 (:temporary (:sc unsigned-reg) call-target)
872 (check-ocfp-and-return-pc old-fp return-pc)
873 ;; Move these into the passing locations if they are not already there.
876 ;; And jump to the assembly routine.
877 (inst lea call-target
879 :disp (make-fixup 'tail-call-variable :assembly-routine)))
880 (inst jmp call-target)))
882 ;;;; unknown values return
884 ;;; Return a single-value using the Unknown-Values convention.
886 ;;; pfw--get wired-tn conflicts sometimes if register sc specd for args
887 ;;; having problems targeting args to regs -- using temps instead.
889 ;;; First off, modifying the return-pc defeats the branch-prediction
890 ;;; optimizations on modern CPUs quite handily. Second, we can do all
891 ;;; this without needing a temp register. Fixed the latter, at least.
892 ;;; -- AB 2006/Feb/04
893 (define-vop (return-single)
899 (check-ocfp-and-return-pc old-fp return-pc)
900 (trace-table-entry trace-table-fun-epilogue)
901 ;; Drop stack above old-fp
902 (inst lea rsp-tn (make-ea :qword :base rbp-tn
903 :disp (frame-byte-offset (tn-offset old-fp))))
904 ;; Clear the multiple-value return flag
906 ;; Restore the old frame pointer
911 ;;; Do unknown-values return of a fixed (other than 1) number of
912 ;;; values. The VALUES are required to be set up in the standard
913 ;;; passing locations. NVALS is the number of values returned.
915 ;;; Basically, we just load RCX with the number of values returned and
916 ;;; RBX with a pointer to the values, set RSP to point to the end of
917 ;;; the values, and jump directly to return-pc.
920 (return-pc :to (:eval 1))
924 ;; In the case of other than one value, we need these registers to
925 ;; tell the caller where they are and how many there are.
926 (:temporary (:sc unsigned-reg :offset rbx-offset) rbx)
927 (:temporary (:sc unsigned-reg :offset rcx-offset) rcx)
928 ;; We need to stretch the lifetime of return-pc past the argument
929 ;; registers so that we can default the argument registers without
930 ;; trashing return-pc.
931 (:temporary (:sc unsigned-reg :offset (first *register-arg-offsets*)
933 (:temporary (:sc unsigned-reg :offset (second *register-arg-offsets*)
935 (:temporary (:sc unsigned-reg :offset (third *register-arg-offsets*)
939 (check-ocfp-and-return-pc old-fp return-pc)
941 ;; This is handled in RETURN-SINGLE.
942 (error "nvalues is 1"))
943 (trace-table-entry trace-table-fun-epilogue)
944 ;; Establish the values pointer and values count.
947 (zeroize rcx) ; smaller
948 (inst mov rcx (fixnumize nvals)))
949 ;; Clear as much of the stack as possible, but not past the old
952 (make-ea :qword :base rbx
953 :disp (frame-byte-offset
954 (if (< register-arg-count nvals)
957 ;; Pre-default any argument register that need it.
958 (when (< nvals register-arg-count)
959 (let* ((arg-tns (nthcdr nvals (list a0 a1 a2)))
960 (first (first arg-tns)))
961 (inst mov first nil-value)
962 (dolist (tn (cdr arg-tns))
963 (inst mov tn first))))
964 ;; Set the multiple value return flag.
966 ;; And away we go. Except that return-pc is still on the
967 ;; stack and we've changed the stack pointer. So we have to
968 ;; tell it to index off of RBX instead of RBP.
969 (cond ((<= nvals register-arg-count)
973 ;; Some values are on the stack after RETURN-PC and OLD-FP,
974 ;; can't return normally and some slots of the frame will
975 ;; be used as temporaries by the receiver.
977 (inst push (make-ea :qword :base rbx
978 :disp (frame-byte-offset (tn-offset return-pc))))
981 (trace-table-entry trace-table-normal)))
983 ;;; Do unknown-values return of an arbitrary number of values (passed
984 ;;; on the stack.) We check for the common case of a single return
985 ;;; value, and do that inline using the normal single value return
986 ;;; convention. Otherwise, we branch off to code that calls an
987 ;;; assembly-routine.
989 ;;; The assembly routine takes the following args:
990 ;;; RAX -- the return-pc to finally jump to.
991 ;;; RBX -- pointer to where to put the values.
992 ;;; RCX -- number of values to find there.
993 ;;; RSI -- pointer to where to find the values.
994 (define-vop (return-multiple)
997 (vals :scs (any-reg) :target rsi)
998 (nvals :scs (any-reg) :target rcx))
999 (:temporary (:sc unsigned-reg :offset rsi-offset :from (:argument 2)) rsi)
1000 (:temporary (:sc unsigned-reg :offset rcx-offset :from (:argument 3)) rcx)
1001 (:temporary (:sc unsigned-reg) return-asm)
1002 (:temporary (:sc descriptor-reg :offset (first *register-arg-offsets*)
1003 :from (:eval 0)) a0)
1006 (check-ocfp-and-return-pc old-fp return-pc)
1007 (trace-table-entry trace-table-fun-epilogue)
1008 (unless (policy node (> space speed))
1009 ;; Check for the single case.
1010 (let ((not-single (gen-label)))
1011 (inst cmp nvals (fixnumize 1))
1012 (inst jmp :ne not-single)
1013 ;; Return with one value.
1015 (inst lea rsp-tn (make-ea :qword :base rbp-tn
1016 :disp (frame-byte-offset ocfp-save-offset)))
1017 ;; clear the multiple-value return flag
1022 ;; Nope, not the single case. Jump to the assembly routine.
1023 (emit-label not-single)))
1026 (inst lea return-asm
1027 (make-ea :qword :disp (make-fixup 'return-multiple
1028 :assembly-routine)))
1029 (inst jmp return-asm)
1030 (trace-table-entry trace-table-normal)))
1034 ;;; We don't need to do anything special for regular functions.
1035 (define-vop (setup-environment)
1039 ;; Don't bother doing anything.
1042 ;;; Get the lexical environment from its passing location.
1043 (define-vop (setup-closure-environment)
1044 (:results (closure :scs (descriptor-reg)))
1049 (move closure rax-tn)))
1051 ;;; Copy a &MORE arg from the argument area to the end of the current
1052 ;;; frame. FIXED is the number of non-&MORE arguments.
1053 (define-vop (copy-more-arg)
1054 (:temporary (:sc any-reg :offset r8-offset) copy-index)
1055 (:temporary (:sc any-reg :offset r9-offset) source)
1056 (:temporary (:sc descriptor-reg :offset r10-offset) temp)
1059 ;; Avoid the copy if there are no more args.
1060 (cond ((zerop fixed)
1061 (inst jrcxz JUST-ALLOC-FRAME))
1063 (inst cmp rcx-tn (fixnumize fixed))
1064 (inst jmp :be JUST-ALLOC-FRAME)))
1066 ;; Allocate the space on the stack.
1067 ;; stack = rbp - (max 3 frame-size) - (nargs - fixed)
1069 (make-ea :qword :base rbp-tn
1070 :disp (- (fixnumize fixed)
1072 (max 3 (sb-allocated-size 'stack))))))
1073 (inst sub rbx-tn rcx-tn) ; Got the new stack in rbx
1074 (inst mov rsp-tn rbx-tn)
1076 ;; Now: nargs>=1 && nargs>fixed
1078 ;; Save the original count of args.
1079 (inst mov rbx-tn rcx-tn)
1081 (cond ((< fixed register-arg-count)
1082 ;; We must stop when we run out of stack args, not when we
1083 ;; run out of more args.
1084 ;; Number to copy = nargs-3
1085 (inst sub rcx-tn (fixnumize register-arg-count))
1086 ;; Everything of interest in registers.
1087 (inst jmp :be DO-REGS))
1089 ;; Number to copy = nargs-fixed
1090 (inst sub rcx-tn (fixnumize fixed))))
1092 ;; Initialize R8 to be the end of args.
1093 (inst mov source rbp-tn)
1094 (inst sub source rbx-tn)
1096 ;; We need to copy from downwards up to avoid overwriting some of
1097 ;; the yet uncopied args. So we need to use R9 as the copy index
1098 ;; and RCX as the loop counter, rather than using RCX for both.
1099 (zeroize copy-index)
1101 ;; We used to use REP MOVS here, but on modern x86 it performs
1102 ;; much worse than an explicit loop for small blocks.
1104 (inst mov temp (make-ea :qword :base source :index copy-index))
1105 (inst mov (make-ea :qword :base rsp-tn :index copy-index) temp)
1106 (inst add copy-index n-word-bytes)
1107 (inst sub rcx-tn n-word-bytes)
1108 (inst jmp :nz COPY-LOOP)
1113 (inst mov rcx-tn rbx-tn)
1115 ;; Here: nargs>=1 && nargs>fixed
1116 (when (< fixed register-arg-count)
1117 ;; Now we have to deposit any more args that showed up in
1121 ;; Store it relative to rbp
1122 (inst mov (make-ea :qword :base rbp-tn
1123 :disp (- (* n-word-bytes
1125 (max 3 (sb-allocated-size 'stack))))))
1126 (nth i *register-arg-tns*))
1129 (when (>= i register-arg-count)
1132 ;; Don't deposit any more than there are.
1134 (inst test rcx-tn rcx-tn)
1135 (inst cmp rcx-tn (fixnumize i)))
1136 (inst jmp :eq DONE)))
1142 (make-ea :qword :base rbp-tn
1143 :disp (- (* n-word-bytes
1144 (max 3 (sb-allocated-size 'stack))))))
1148 (define-vop (more-kw-arg)
1149 (:translate sb!c::%more-kw-arg)
1150 (:policy :fast-safe)
1151 (:args (object :scs (descriptor-reg) :to (:result 1))
1152 (index :scs (any-reg) :to (:result 1) :target keyword))
1153 (:arg-types * tagged-num)
1154 (:results (value :scs (descriptor-reg any-reg))
1155 (keyword :scs (descriptor-reg any-reg)))
1158 (inst mov value (make-ea :qword :base object :index index))
1159 (inst mov keyword (make-ea :qword :base object :index index
1160 :disp n-word-bytes))))
1162 (define-vop (more-arg)
1163 (:translate sb!c::%more-arg)
1164 (:policy :fast-safe)
1165 (:args (object :scs (descriptor-reg) :to (:result 1))
1166 (index :scs (any-reg) :to (:result 1) :target value))
1167 (:arg-types * tagged-num)
1168 (:results (value :scs (descriptor-reg any-reg)))
1173 (inst mov value (make-ea :qword :base object :index value))))
1175 ;;; Turn more arg (context, count) into a list.
1176 (define-vop (listify-rest-args)
1177 (:translate %listify-rest-args)
1179 (:args (context :scs (descriptor-reg) :target src)
1180 (count :scs (any-reg) :target rcx))
1181 (:arg-types * tagged-num)
1182 (:temporary (:sc unsigned-reg :offset rsi-offset :from (:argument 0)) src)
1183 (:temporary (:sc unsigned-reg :offset rcx-offset :from (:argument 1)) rcx)
1184 (:temporary (:sc unsigned-reg :offset rax-offset) rax)
1185 (:temporary (:sc unsigned-reg) dst)
1186 (:results (result :scs (descriptor-reg)))
1189 (let ((enter (gen-label))
1192 (stack-allocate-p (node-stack-allocate-p node)))
1195 ;; Check to see whether there are no args, and just return NIL if so.
1196 (inst mov result nil-value)
1198 (inst lea dst (make-ea :qword :base rcx :index rcx))
1199 (maybe-pseudo-atomic stack-allocate-p
1200 (allocation dst dst node stack-allocate-p list-pointer-lowtag)
1201 (inst shr rcx (1- n-lowtag-bits))
1202 ;; Set decrement mode (successive args at lower addresses)
1204 ;; Set up the result.
1206 ;; Jump into the middle of the loop, 'cause that's where we want
1210 ;; Compute a pointer to the next cons.
1211 (inst add dst (* cons-size n-word-bytes))
1212 ;; Store a pointer to this cons in the CDR of the previous cons.
1213 (storew dst dst -1 list-pointer-lowtag)
1215 ;; Grab one value and stash it in the car of this cons.
1217 (storew rax dst 0 list-pointer-lowtag)
1218 ;; Go back for more.
1221 ;; NIL out the last cons.
1222 (storew nil-value dst 1 list-pointer-lowtag)
1224 (emit-label done))))
1226 ;;; Return the location and size of the &MORE arg glob created by
1227 ;;; COPY-MORE-ARG. SUPPLIED is the total number of arguments supplied
1228 ;;; (originally passed in RCX). FIXED is the number of non-rest
1231 ;;; We must duplicate some of the work done by COPY-MORE-ARG, since at
1232 ;;; that time the environment is in a pretty brain-damaged state,
1233 ;;; preventing this info from being returned as values. What we do is
1234 ;;; compute supplied - fixed, and return a pointer that many words
1235 ;;; below the current stack top.
1236 (define-vop (more-arg-context)
1237 (:policy :fast-safe)
1238 (:translate sb!c::%more-arg-context)
1239 (:args (supplied :scs (any-reg) :target count))
1240 (:arg-types positive-fixnum (:constant fixnum))
1242 (:results (context :scs (descriptor-reg))
1243 (count :scs (any-reg)))
1244 (:result-types t tagged-num)
1245 (:note "more-arg-context")
1247 (move count supplied)
1248 ;; SP at this point points at the last arg pushed.
1249 ;; Point to the first more-arg, not above it.
1250 (inst lea context (make-ea :qword :base rsp-tn
1251 :index count :scale 1
1252 :disp (- (+ (fixnumize fixed) n-word-bytes))))
1253 (unless (zerop fixed)
1254 (inst sub count (fixnumize fixed)))))
1256 ;;; Signal wrong argument count error if NARGS isn't equal to COUNT.
1257 (define-vop (verify-arg-count)
1258 (:policy :fast-safe)
1259 (:translate sb!c::%verify-arg-count)
1260 (:args (nargs :scs (any-reg)))
1261 (:arg-types positive-fixnum (:constant t))
1264 (:save-p :compute-only)
1267 (generate-error-code vop 'invalid-arg-count-error nargs)))
1269 (inst test nargs nargs) ; smaller instruction
1270 (inst cmp nargs (fixnumize count)))
1271 (inst jmp :ne err-lab))))
1273 ;;; Various other error signallers.
1274 (macrolet ((def (name error translate &rest args)
1275 `(define-vop (,name)
1277 `((:policy :fast-safe)
1278 (:translate ,translate)))
1279 (:args ,@(mapcar (lambda (arg)
1280 `(,arg :scs (any-reg descriptor-reg)))
1283 (:save-p :compute-only)
1285 (error-call vop ',error ,@args)))))
1286 (def arg-count-error invalid-arg-count-error
1287 sb!c::%arg-count-error nargs)
1288 (def type-check-error object-not-type-error sb!c::%type-check-error
1290 (def layout-invalid-error layout-invalid-error sb!c::%layout-invalid-error
1292 (def odd-key-args-error odd-key-args-error
1293 sb!c::%odd-key-args-error)
1294 (def unknown-key-arg-error unknown-key-arg-error
1295 sb!c::%unknown-key-arg-error key)
1296 (def nil-fun-returned-error nil-fun-returned-error nil fun))
1300 (defun emit-single-step-test ()
1301 ;; We use different ways of representing whether stepping is on on
1302 ;; +SB-THREAD / -SB-THREAD: on +SB-THREAD, we use a slot in the
1303 ;; thread structure. On -SB-THREAD we use the value of a static
1304 ;; symbol. Things are done this way, since reading a thread-local
1305 ;; slot from a symbol would require an extra register on +SB-THREAD,
1306 ;; and reading a slot from a thread structure would require an extra
1307 ;; register on -SB-THREAD. While this isn't critical for x86-64,
1308 ;; it's more serious for x86.
1310 (inst cmp (make-ea :qword
1311 :base thread-base-tn
1312 :disp (* thread-stepping-slot n-word-bytes))
1315 (inst cmp (make-ea :qword
1316 :disp (+ nil-value (static-symbol-offset
1317 'sb!impl::*stepping*)
1318 (* symbol-value-slot n-word-bytes)
1319 (- other-pointer-lowtag)))
1322 (define-vop (step-instrument-before-vop)
1323 (:policy :fast-safe)
1326 (emit-single-step-test)
1328 (inst break single-step-before-trap)
1330 (note-this-location vop :step-before-vop)))