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 ;;; Accessing a slot from an earlier stack frame is definite hackery.
123 (define-vop (ancestor-frame-ref)
124 (:args (frame-pointer :scs (descriptor-reg))
125 (variable-home-tn :load-if nil))
126 (:results (value :scs (descriptor-reg any-reg)))
129 (aver (sc-is variable-home-tn control-stack))
130 (loadw value frame-pointer
131 (frame-word-offset (tn-offset variable-home-tn)))))
132 (define-vop (ancestor-frame-set)
133 (:args (frame-pointer :scs (descriptor-reg))
134 (value :scs (descriptor-reg any-reg)))
135 (:results (variable-home-tn :load-if nil))
138 (aver (sc-is variable-home-tn control-stack))
139 (storew value frame-pointer
140 (frame-word-offset (tn-offset variable-home-tn)))))
142 (define-vop (xep-allocate-frame)
143 (:info start-lab copy-more-arg-follows)
146 (emit-alignment n-lowtag-bits)
147 (trace-table-entry trace-table-fun-prologue)
148 (emit-label start-lab)
149 ;; Skip space for the function header.
150 (inst simple-fun-header-word)
151 (dotimes (i (* n-word-bytes (1- simple-fun-code-offset)))
154 ;; The start of the actual code.
155 ;; Save the return-pc.
156 (popw rbp-tn (frame-word-offset return-pc-save-offset))
158 ;; If copy-more-arg follows it will allocate the correct stack
159 ;; size. The stack is not allocated first here as this may expose
160 ;; args on the stack if they take up more space than the frame!
161 (unless copy-more-arg-follows
162 ;; The args fit within the frame so just allocate the frame.
164 (make-ea :qword :base rbp-tn
165 :disp (- (* n-word-bytes
166 (- (max 3 (sb-allocated-size 'stack))
169 (trace-table-entry trace-table-normal)))
171 ;;; This is emitted directly before either a known-call-local, call-local,
172 ;;; or a multiple-call-local. All it does is allocate stack space for the
173 ;;; callee (who has the same size stack as us).
174 (define-vop (allocate-frame)
175 (:results (res :scs (any-reg))
180 (inst lea res (make-ea :qword :base rsp-tn
181 :disp (- (* sp->fp-offset n-word-bytes))))
182 (inst sub rsp-tn (* n-word-bytes (sb-allocated-size 'stack)))))
184 ;;; Allocate a partial frame for passing stack arguments in a full
185 ;;; call. NARGS is the number of arguments passed. We allocate at
186 ;;; least 3 slots, because the XEP noise is going to want to use them
187 ;;; before it can extend the stack.
188 (define-vop (allocate-full-call-frame)
190 (:results (res :scs (any-reg)))
192 (inst lea res (make-ea :qword :base rsp-tn
193 :disp (- (* sp->fp-offset n-word-bytes))))
194 (inst sub rsp-tn (* (max nargs 3) n-word-bytes))))
196 ;;; Emit code needed at the return-point from an unknown-values call
197 ;;; for a fixed number of values. Values is the head of the TN-REF
198 ;;; list for the locations that the values are to be received into.
199 ;;; Nvals is the number of values that are to be received (should
200 ;;; equal the length of Values).
202 ;;; If 0 or 1 values are expected, then we just emit an instruction to
203 ;;; reset the SP (which will only be executed when other than 1 value
206 ;;; In the general case we have to do three things:
207 ;;; -- Default unsupplied register values. This need only be done
208 ;;; when a single value is returned, since register values are
209 ;;; defaulted by the called in the non-single case.
210 ;;; -- Default unsupplied stack values. This needs to be done whenever
211 ;;; there are stack values.
212 ;;; -- Reset SP. This must be done whenever other than 1 value is
213 ;;; returned, regardless of the number of values desired.
214 (defun default-unknown-values (vop values nvals node)
215 (declare (type (or tn-ref null) values)
216 (type unsigned-byte nvals))
217 (let ((type (sb!c::basic-combination-derived-type node)))
220 (note-this-location vop :single-value-return)
222 ((<= (sb!kernel:values-type-max-value-count type)
224 (when (and (named-type-p type)
225 (eq nil (named-type-name type)))
226 ;; The function never returns, it may happen that the code
227 ;; ends right here leavig the :SINGLE-VALUE-RETURN note
228 ;; dangling. Let's emit a NOP.
230 ((not (sb!kernel:values-type-may-be-single-value-p type))
231 (inst mov rsp-tn rbx-tn))
233 (inst cmov :c rsp-tn rbx-tn))))
234 ((<= nvals register-arg-count)
235 (note-this-location vop :unknown-return)
236 (when (sb!kernel:values-type-may-be-single-value-p type)
237 (let ((regs-defaulted (gen-label)))
238 (inst jmp :c regs-defaulted)
239 ;; Default the unsupplied registers.
240 (let* ((2nd-tn-ref (tn-ref-across values))
241 (2nd-tn (tn-ref-tn 2nd-tn-ref)))
242 (inst mov 2nd-tn nil-value)
245 for tn-ref = (tn-ref-across 2nd-tn-ref)
246 then (tn-ref-across tn-ref)
247 for count from 2 below register-arg-count
248 do (inst mov (tn-ref-tn tn-ref) 2nd-tn))))
249 (inst mov rbx-tn rsp-tn)
250 (emit-label regs-defaulted)))
251 (when (< register-arg-count
252 (sb!kernel:values-type-max-value-count type))
253 (inst mov rsp-tn rbx-tn)))
255 ;; The number of bytes depends on the relative jump instructions.
256 ;; Best case is 31+(n-3)*14, worst case is 35+(n-3)*18. For
257 ;; NVALS=6 that is 73/89 bytes, and for NVALS=7 that is 87/107
258 ;; bytes which is likely better than using the blt below.
259 (let ((regs-defaulted (gen-label))
260 (defaulting-done (gen-label))
261 (default-stack-slots (gen-label)))
262 (note-this-location vop :unknown-return)
263 ;; Branch off to the MV case.
264 (inst jmp :c regs-defaulted)
265 ;; Do the single value case.
266 ;; Default the register args
267 (inst mov rax-tn nil-value)
269 (val (tn-ref-across values) (tn-ref-across val)))
270 ((= i (min nvals register-arg-count)))
271 (inst mov (tn-ref-tn val) rax-tn))
272 ;; Fake other registers so it looks like we returned with all the
273 ;; registers filled in.
275 (inst jmp default-stack-slots)
276 (emit-label regs-defaulted)
277 (inst mov rax-tn nil-value)
278 (collect ((defaults))
279 (do ((i register-arg-count (1+ i))
280 (val (do ((i 0 (1+ i))
281 (val values (tn-ref-across val)))
282 ((= i register-arg-count) val))
283 (tn-ref-across val)))
285 (let ((default-lab (gen-label))
287 (first-stack-arg-p (= i register-arg-count)))
288 (defaults (cons default-lab
289 (cons tn first-stack-arg-p)))
290 (inst cmp rcx-tn (fixnumize i))
291 (inst jmp :be default-lab)
292 (when first-stack-arg-p
293 ;; There are stack args so the frame of the callee is
294 ;; still there, save RDX in its first slot temporalily.
295 (storew rdx-tn rbx-tn (frame-word-offset sp->fp-offset)))
296 (loadw rdx-tn rbx-tn (frame-word-offset (+ sp->fp-offset i)))
297 (inst mov tn rdx-tn)))
298 (emit-label defaulting-done)
299 (loadw rdx-tn rbx-tn (frame-word-offset sp->fp-offset))
301 (let ((defaults (defaults)))
303 (assemble (*elsewhere*)
304 (trace-table-entry trace-table-fun-prologue)
305 (emit-label default-stack-slots)
306 (dolist (default defaults)
307 (emit-label (car default))
309 ;; We are setting the first stack argument to NIL.
310 ;; The callee's stack frame is dead, save RDX by
311 ;; pushing it to the stack, it will end up at same
312 ;; place as in the (STOREW RDX-TN RBX-TN -1) case
315 (inst mov (second default) rax-tn))
316 (inst jmp defaulting-done)
317 (trace-table-entry trace-table-normal)))))))
319 (let ((regs-defaulted (gen-label))
320 (restore-edi (gen-label))
321 (no-stack-args (gen-label))
322 (default-stack-vals (gen-label))
323 (count-okay (gen-label)))
324 (note-this-location vop :unknown-return)
325 ;; Branch off to the MV case.
326 (inst jmp :c regs-defaulted)
327 ;; Default the register args, and set up the stack as if we
328 ;; entered the MV return point.
329 (inst mov rbx-tn rsp-tn)
330 (inst mov rdi-tn nil-value)
331 (inst mov rsi-tn rdi-tn)
332 ;; Compute a pointer to where to put the [defaulted] stack values.
333 (emit-label no-stack-args)
337 (make-ea :qword :base rbp-tn
338 :disp (frame-byte-offset register-arg-count)))
339 ;; Load RAX with NIL so we can quickly store it, and set up
340 ;; stuff for the loop.
341 (inst mov rax-tn nil-value)
343 (inst mov rcx-tn (- nvals register-arg-count))
344 ;; Jump into the default loop.
345 (inst jmp default-stack-vals)
346 ;; The regs are defaulted. We need to copy any stack arguments,
347 ;; and then default the remaining stack arguments.
348 (emit-label regs-defaulted)
349 ;; Compute the number of stack arguments, and if it's zero or
350 ;; less, don't copy any stack arguments.
351 (inst sub rcx-tn (fixnumize register-arg-count))
352 (inst jmp :le no-stack-args)
354 (storew rdi-tn rbx-tn (frame-word-offset (+ sp->fp-offset 1)))
355 ;; Throw away any unwanted args.
356 (inst cmp rcx-tn (fixnumize (- nvals register-arg-count)))
357 (inst jmp :be count-okay)
358 (inst mov rcx-tn (fixnumize (- nvals register-arg-count)))
359 (emit-label count-okay)
360 ;; Save the number of stack values.
361 (inst mov rax-tn rcx-tn)
362 ;; Compute a pointer to where the stack args go.
364 (make-ea :qword :base rbp-tn
365 :disp (frame-byte-offset register-arg-count)))
366 ;; Save ESI, and compute a pointer to where the args come from.
367 (storew rsi-tn rbx-tn (frame-word-offset (+ sp->fp-offset 2)))
369 (make-ea :qword :base rbx-tn
370 :disp (frame-byte-offset
371 (+ sp->fp-offset register-arg-count))))
373 (inst shr rcx-tn word-shift) ; make word count
378 (loadw rsi-tn rbx-tn (frame-word-offset (+ sp->fp-offset 2)))
379 ;; Now we have to default the remaining args. Find out how many.
380 (inst sub rax-tn (fixnumize (- nvals register-arg-count)))
382 ;; If none, then just blow out of here.
383 (inst jmp :le restore-edi)
384 (inst mov rcx-tn rax-tn)
385 (inst shr rcx-tn word-shift) ; word count
386 ;; Load RAX with NIL for fast storing.
387 (inst mov rax-tn nil-value)
389 (emit-label default-stack-vals)
392 ;; Restore EDI, and reset the stack.
393 (emit-label restore-edi)
394 (loadw rdi-tn rbx-tn (frame-word-offset (+ sp->fp-offset 1)))
395 (inst mov rsp-tn rbx-tn)
399 ;;;; unknown values receiving
401 ;;; Emit code needed at the return point for an unknown-values call
402 ;;; for an arbitrary number of values.
404 ;;; We do the single and non-single cases with no shared code: there
405 ;;; doesn't seem to be any potential overlap, and receiving a single
406 ;;; value is more important efficiency-wise.
408 ;;; When there is a single value, we just push it on the stack,
409 ;;; returning the old SP and 1.
411 ;;; When there is a variable number of values, we move all of the
412 ;;; argument registers onto the stack, and return ARGS and NARGS.
414 ;;; ARGS and NARGS are TNs wired to the named locations. We must
415 ;;; explicitly allocate these TNs, since their lifetimes overlap with
416 ;;; the results start and count. (Also, it's nice to be able to target
418 (defun receive-unknown-values (args nargs start count node)
419 (declare (type tn args nargs start count))
420 (let ((type (sb!c::basic-combination-derived-type node))
421 (variable-values (gen-label))
422 (stack-values (gen-label))
424 (when (sb!kernel:values-type-may-be-single-value-p type)
425 (inst jmp :c variable-values)
426 (cond ((location= start (first *register-arg-tns*))
427 (inst push (first *register-arg-tns*))
428 (inst lea start (make-ea :qword :base rsp-tn :disp n-word-bytes)))
429 (t (inst mov start rsp-tn)
430 (inst push (first *register-arg-tns*))))
431 (inst mov count (fixnumize 1))
433 (emit-label variable-values))
434 ;; The stack frame is burnt and RETurned from if there are no
435 ;; stack values. In this case quickly reallocate sufficient space.
436 (when (<= (sb!kernel:values-type-min-value-count type)
438 (inst cmp nargs (fixnumize register-arg-count))
439 (inst jmp :g stack-values)
440 (inst sub rsp-tn nargs)
441 (emit-label stack-values))
442 ;; dtc: this writes the registers onto the stack even if they are
443 ;; not needed, only the number specified in rcx are used and have
444 ;; stack allocated to them. No harm is done.
446 for arg in *register-arg-tns*
448 for j below (sb!kernel:values-type-max-value-count type)
449 do (storew arg args i))
456 ;;; VOP that can be inherited by unknown values receivers. The main thing this
457 ;;; handles is allocation of the result temporaries.
458 (define-vop (unknown-values-receiver)
459 (:temporary (:sc descriptor-reg :offset rbx-offset
460 :from :eval :to (:result 0))
462 (:temporary (:sc any-reg :offset rcx-offset
463 :from :eval :to (:result 1))
465 (:results (start :scs (any-reg control-stack))
466 (count :scs (any-reg control-stack))))
468 ;;;; local call with unknown values convention return
470 (defun check-ocfp-and-return-pc (old-fp return-pc)
472 (format t "*known-return: old-fp ~S, tn-kind ~S; ~S ~S~%"
473 old-fp (sb!c::tn-kind old-fp) (sb!c::tn-save-tn old-fp)
474 (sb!c::tn-kind (sb!c::tn-save-tn old-fp)))
476 (format t "*known-return: return-pc ~S, tn-kind ~S; ~S ~S~%"
477 return-pc (sb!c::tn-kind return-pc)
478 (sb!c::tn-save-tn return-pc)
479 (sb!c::tn-kind (sb!c::tn-save-tn return-pc)))
480 (unless (and (sc-is old-fp control-stack)
481 (= (tn-offset old-fp) ocfp-save-offset))
482 (error "ocfp not on stack in standard save location?"))
483 (unless (and (sc-is return-pc sap-stack)
484 (= (tn-offset return-pc) return-pc-save-offset))
485 (error "return-pc not on stack in standard save location?")))
487 ;;; Instead of JMPing to TARGET, CALL a trampoline that saves the
488 ;;; return pc and jumps. Although this is an incredibly stupid trick
489 ;;; the paired CALL/RET instructions are a big win.
490 (defun make-local-call (target)
491 (let ((tramp (gen-label)))
493 (assemble (*elsewhere*)
495 (popw rbp-tn (frame-word-offset return-pc-save-offset))
498 ;;; Non-TR local call for a fixed number of values passed according to
499 ;;; the unknown values convention.
501 ;;; FP is the frame pointer in install before doing the call.
503 ;;; NFP would be the number-stack frame pointer if we had a separate
506 ;;; Args are the argument passing locations, which are specified only
507 ;;; to terminate their lifetimes in the caller.
509 ;;; VALUES are the return value locations (wired to the standard
510 ;;; passing locations). NVALS is the number of values received.
512 ;;; Save is the save info, which we can ignore since saving has been
515 ;;; TARGET is a continuation pointing to the start of the called
517 (define-vop (call-local)
521 (:results (values :more t))
523 (:move-args :local-call)
524 (:info arg-locs callee target nvals)
526 (:ignore nfp arg-locs args callee)
529 (trace-table-entry trace-table-call-site)
531 (note-this-location vop :call-site)
532 (make-local-call target)
533 (default-unknown-values vop values nvals node)
534 (trace-table-entry trace-table-normal)))
536 ;;; Non-TR local call for a variable number of return values passed according
537 ;;; to the unknown values convention. The results are the start of the values
538 ;;; glob and the number of values received.
539 (define-vop (multiple-call-local unknown-values-receiver)
544 (:move-args :local-call)
545 (:info save callee target)
546 (:ignore args save nfp callee)
550 (trace-table-entry trace-table-call-site)
552 (note-this-location vop :call-site)
553 (make-local-call target)
554 (note-this-location vop :unknown-return)
555 (receive-unknown-values values-start nvals start count node)
556 (trace-table-entry trace-table-normal)))
558 ;;;; local call with known values return
560 ;;; Non-TR local call with known return locations. Known-value return
561 ;;; works just like argument passing in local call.
563 ;;; Note: we can't use normal load-tn allocation for the fixed args,
564 ;;; since all registers may be tied up by the more operand. Instead,
565 ;;; we use MAYBE-LOAD-STACK-TN.
566 (define-vop (known-call-local)
570 (:results (res :more t))
571 (:move-args :local-call)
573 (:info save callee target)
574 (:ignore args res save nfp callee)
577 (trace-table-entry trace-table-call-site)
579 (note-this-location vop :call-site)
580 (make-local-call target)
581 (note-this-location vop :known-return)
582 (trace-table-entry trace-table-normal)))
584 ;;; From Douglas Crosher
585 ;;; Return from known values call. We receive the return locations as
586 ;;; arguments to terminate their lifetimes in the returning function. We
587 ;;; restore FP and CSP and jump to the Return-PC.
588 (define-vop (known-return)
592 (:move-args :known-return)
594 (:ignore val-locs vals)
597 (check-ocfp-and-return-pc old-fp return-pc)
598 (trace-table-entry trace-table-fun-epilogue)
599 ;; Zot all of the stack except for the old-fp and return-pc.
600 (inst mov rsp-tn rbp-tn)
603 (trace-table-entry trace-table-normal)))
607 ;;; There is something of a cross-product effect with full calls.
608 ;;; Different versions are used depending on whether we know the
609 ;;; number of arguments or the name of the called function, and
610 ;;; whether we want fixed values, unknown values, or a tail call.
612 ;;; In full call, the arguments are passed creating a partial frame on
613 ;;; the stack top and storing stack arguments into that frame. On
614 ;;; entry to the callee, this partial frame is pointed to by FP.
616 ;;; This macro helps in the definition of full call VOPs by avoiding
617 ;;; code replication in defining the cross-product VOPs.
619 ;;; NAME is the name of the VOP to define.
621 ;;; NAMED is true if the first argument is an fdefinition object whose
622 ;;; definition is to be called.
624 ;;; RETURN is either :FIXED, :UNKNOWN or :TAIL:
625 ;;; -- If :FIXED, then the call is for a fixed number of values, returned in
626 ;;; the standard passing locations (passed as result operands).
627 ;;; -- If :UNKNOWN, then the result values are pushed on the stack, and the
628 ;;; result values are specified by the Start and Count as in the
629 ;;; unknown-values continuation representation.
630 ;;; -- If :TAIL, then do a tail-recursive call. No values are returned.
631 ;;; The Old-Fp and Return-PC are passed as the second and third arguments.
633 ;;; In non-tail calls, the pointer to the stack arguments is passed as
634 ;;; the last fixed argument. If Variable is false, then the passing
635 ;;; locations are passed as a more arg. Variable is true if there are
636 ;;; a variable number of arguments passed on the stack. Variable
637 ;;; cannot be specified with :TAIL return. TR variable argument call
638 ;;; is implemented separately.
640 ;;; In tail call with fixed arguments, the passing locations are
641 ;;; passed as a more arg, but there is no new-FP, since the arguments
642 ;;; have been set up in the current frame.
643 (macrolet ((define-full-call (name named return variable)
644 (aver (not (and variable (eq return :tail))))
646 ,@(when (eq return :unknown)
647 '(unknown-values-receiver)))
649 ,@(unless (eq return :tail)
650 '((new-fp :scs (any-reg) :to (:argument 1))))
652 (fun :scs (descriptor-reg control-stack)
653 :target rax :to (:argument 0))
655 ,@(when (eq return :tail)
659 ,@(unless variable '((args :more t :scs (descriptor-reg)))))
661 ,@(when (eq return :fixed)
662 '((:results (values :more t))))
664 (:save-p ,(if (eq return :tail) :compute-only t))
666 ,@(unless (or (eq return :tail) variable)
667 '((:move-args :full-call)))
671 ,@(unless (or variable (eq return :tail)) '(arg-locs))
672 ,@(unless variable '(nargs))
673 ,@(when (eq return :fixed) '(nvals))
677 ,@(unless (or variable (eq return :tail)) '(arg-locs))
678 ,@(unless variable '(args)))
680 ;; We pass either the fdefn object (for named call) or
681 ;; the actual function object (for unnamed call) in
682 ;; RAX. With named call, closure-tramp will replace it
683 ;; with the real function and invoke the real function
684 ;; for closures. Non-closures do not need this value,
685 ;; so don't care what shows up in it.
693 ;; We pass the number of arguments in RCX.
694 (:temporary (:sc unsigned-reg :offset rcx-offset :to :eval) rcx)
696 ;; With variable call, we have to load the
697 ;; register-args out of the (new) stack frame before
698 ;; doing the call. Therefore, we have to tell the
699 ;; lifetime stuff that we need to use them.
701 (mapcar (lambda (name offset)
702 `(:temporary (:sc descriptor-reg
707 *register-arg-names* *register-arg-offsets*))
709 ,@(when (eq return :tail)
710 '((:temporary (:sc unsigned-reg
714 ,@(unless (eq return :tail)
717 (:generator ,(+ (if named 5 0)
719 (if (eq return :tail) 0 10)
721 (if (eq return :unknown) 25 0))
722 (trace-table-entry trace-table-call-site)
724 ;; This has to be done before the frame pointer is
725 ;; changed! RAX stores the 'lexical environment' needed
731 ;; For variable call, compute the number of
732 ;; arguments and move some of the arguments to
735 ;; Compute the number of arguments.
736 (noise '(inst mov rcx new-fp))
737 (noise '(inst sub rcx rsp-tn))
738 ;; Move the necessary args to registers,
739 ;; this moves them all even if they are
742 for name in *register-arg-names*
743 for index downfrom -1
744 do (noise `(loadw ,name new-fp ,index)))
748 (inst mov rcx (fixnumize nargs)))))
749 ,@(cond ((eq return :tail)
750 '(;; Python has figured out what frame we should
751 ;; return to so might as well use that clue.
752 ;; This seems really important to the
753 ;; implementation of things like
754 ;; (without-interrupts ...)
756 ;; dtc; Could be doing a tail call from a
757 ;; known-local-call etc in which the old-fp
758 ;; or ret-pc are in regs or in non-standard
759 ;; places. If the passing location were
760 ;; wired to the stack in standard locations
761 ;; then these moves will be un-necessary;
762 ;; this is probably best for the x86.
765 (unless (= ocfp-save-offset
767 ;; FIXME: FORMAT T for stale
768 ;; diagnostic output (several of
769 ;; them around here), ick
770 (error "** tail-call old-fp not S0~%")
771 (move old-fp-tmp old-fp)
774 (frame-word-offset ocfp-save-offset))))
775 ((any-reg descriptor-reg)
776 (error "** tail-call old-fp in reg not S0~%")
779 (frame-word-offset ocfp-save-offset))))
781 ;; For tail call, we have to push the
782 ;; return-pc so that it looks like we CALLed
783 ;; despite the fact that we are going to JMP.
784 (inst push return-pc)
787 ;; For non-tail call, we have to save our
788 ;; frame pointer and install the new frame
789 ;; pointer. We can't load stack tns after this
791 `(;; Python doesn't seem to allocate a frame
792 ;; here which doesn't leave room for the
795 ;; The variable args are on the stack and
796 ;; become the frame, but there may be <3
797 ;; args and 3 stack slots are assumed
798 ;; allocate on the call. So need to ensure
799 ;; there are at least 3 slots. This hack
802 '(inst sub rsp-tn (fixnumize 3)))
804 ;; Bias the new-fp for use as an fp
806 '(inst sub new-fp (fixnumize sp->fp-offset)))
809 (storew rbp-tn new-fp
810 (frame-word-offset ocfp-save-offset))
812 (move rbp-tn new-fp) ; NB - now on new stack frame.
815 (when step-instrumenting
816 (emit-single-step-test)
818 (inst break single-step-around-trap))
821 (note-this-location vop :call-site)
823 (inst ,(if (eq return :tail) 'jmp 'call)
824 (make-ea :qword :base rax
826 '(- (* fdefn-raw-addr-slot
828 other-pointer-lowtag)
829 '(- (* closure-fun-slot n-word-bytes)
830 fun-pointer-lowtag))))
833 '((default-unknown-values vop values nvals node)))
835 '((note-this-location vop :unknown-return)
836 (receive-unknown-values values-start nvals start count
839 (trace-table-entry trace-table-normal)))))
841 (define-full-call call nil :fixed nil)
842 (define-full-call call-named t :fixed nil)
843 (define-full-call multiple-call nil :unknown nil)
844 (define-full-call multiple-call-named t :unknown nil)
845 (define-full-call tail-call nil :tail nil)
846 (define-full-call tail-call-named t :tail nil)
848 (define-full-call call-variable nil :fixed t)
849 (define-full-call multiple-call-variable nil :unknown t))
851 ;;; This is defined separately, since it needs special code that BLT's
852 ;;; the arguments down. All the real work is done in the assembly
853 ;;; routine. We just set things up so that it can find what it needs.
854 (define-vop (tail-call-variable)
855 (:args (args :scs (any-reg control-stack) :target rsi)
856 (function :scs (descriptor-reg control-stack) :target rax)
859 (:temporary (:sc unsigned-reg :offset rsi-offset :from (:argument 0)) rsi)
860 (:temporary (:sc unsigned-reg :offset rax-offset :from (:argument 1)) rax)
861 (:temporary (:sc unsigned-reg) call-target)
863 (check-ocfp-and-return-pc old-fp return-pc)
864 ;; Move these into the passing locations if they are not already there.
867 ;; And jump to the assembly routine.
868 (inst lea call-target
870 :disp (make-fixup 'tail-call-variable :assembly-routine)))
871 (inst jmp call-target)))
873 ;;;; unknown values return
875 ;;; Return a single-value using the Unknown-Values convention.
877 ;;; pfw--get wired-tn conflicts sometimes if register sc specd for args
878 ;;; having problems targeting args to regs -- using temps instead.
880 ;;; First off, modifying the return-pc defeats the branch-prediction
881 ;;; optimizations on modern CPUs quite handily. Second, we can do all
882 ;;; this without needing a temp register. Fixed the latter, at least.
883 ;;; -- AB 2006/Feb/04
884 (define-vop (return-single)
890 (check-ocfp-and-return-pc old-fp return-pc)
891 (trace-table-entry trace-table-fun-epilogue)
892 ;; Drop stack above old-fp
893 (inst mov rsp-tn rbp-tn)
894 ;; Clear the multiple-value return flag
896 ;; Restore the old frame pointer
901 ;;; Do unknown-values return of a fixed (other than 1) number of
902 ;;; values. The VALUES are required to be set up in the standard
903 ;;; passing locations. NVALS is the number of values returned.
905 ;;; Basically, we just load RCX with the number of values returned and
906 ;;; RBX with a pointer to the values, set RSP to point to the end of
907 ;;; the values, and jump directly to return-pc.
910 (return-pc :to (:eval 1))
914 ;; In the case of other than one value, we need these registers to
915 ;; tell the caller where they are and how many there are.
916 (:temporary (:sc unsigned-reg :offset rbx-offset) rbx)
917 (:temporary (:sc unsigned-reg :offset rcx-offset) rcx)
918 ;; We need to stretch the lifetime of return-pc past the argument
919 ;; registers so that we can default the argument registers without
920 ;; trashing return-pc.
921 (:temporary (:sc unsigned-reg :offset (first *register-arg-offsets*)
923 (:temporary (:sc unsigned-reg :offset (second *register-arg-offsets*)
925 (:temporary (:sc unsigned-reg :offset (third *register-arg-offsets*)
929 (check-ocfp-and-return-pc old-fp return-pc)
931 ;; This is handled in RETURN-SINGLE.
932 (error "nvalues is 1"))
933 (trace-table-entry trace-table-fun-epilogue)
934 ;; Establish the values pointer and values count.
935 (inst lea rbx (make-ea :qword :base rbp-tn
936 :disp (* sp->fp-offset n-word-bytes)))
938 (zeroize rcx) ; smaller
939 (inst mov rcx (fixnumize nvals)))
940 ;; Pre-default any argument register that need it.
941 (when (< nvals register-arg-count)
942 (let* ((arg-tns (nthcdr nvals (list a0 a1 a2)))
943 (first (first arg-tns)))
944 (inst mov first nil-value)
945 (dolist (tn (cdr arg-tns))
946 (inst mov tn first))))
947 ;; Set the multiple value return flag.
949 ;; And away we go. Except that return-pc is still on the
950 ;; stack and we've changed the stack pointer. So we have to
951 ;; tell it to index off of RBX instead of RBP.
952 (cond ((<= nvals register-arg-count)
953 (inst mov rsp-tn rbp-tn)
957 ;; Some values are on the stack after RETURN-PC and OLD-FP,
958 ;; can't return normally and some slots of the frame will
959 ;; be used as temporaries by the receiver.
961 ;; Clear as much of the stack as possible, but not past the
962 ;; old frame address.
964 (make-ea :qword :base rbp-tn
965 :disp (frame-byte-offset (1- nvals))))
967 (inst push (make-ea :qword :base rbx
968 :disp (frame-byte-offset
970 (tn-offset return-pc)))))
973 (trace-table-entry trace-table-normal)))
975 ;;; Do unknown-values return of an arbitrary number of values (passed
976 ;;; on the stack.) We check for the common case of a single return
977 ;;; value, and do that inline using the normal single value return
978 ;;; convention. Otherwise, we branch off to code that calls an
979 ;;; assembly-routine.
981 ;;; The assembly routine takes the following args:
982 ;;; RCX -- number of values to find there.
983 ;;; RSI -- pointer to where to find the values.
984 (define-vop (return-multiple)
987 (vals :scs (any-reg) :target rsi)
988 (nvals :scs (any-reg) :target rcx))
989 (:temporary (:sc unsigned-reg :offset rsi-offset :from (:argument 2)) rsi)
990 (:temporary (:sc unsigned-reg :offset rcx-offset :from (:argument 3)) rcx)
991 (:temporary (:sc unsigned-reg) return-asm)
992 (:temporary (:sc descriptor-reg :offset (first *register-arg-offsets*)
996 (check-ocfp-and-return-pc old-fp return-pc)
997 (trace-table-entry trace-table-fun-epilogue)
998 (unless (policy node (> space speed))
999 ;; Check for the single case.
1000 (let ((not-single (gen-label)))
1001 (inst cmp nvals (fixnumize 1))
1002 (inst jmp :ne not-single)
1003 ;; Return with one value.
1005 ;; Clear the stack until ocfp.
1006 (inst mov rsp-tn rbp-tn)
1007 ;; clear the multiple-value return flag
1012 ;; Nope, not the single case. Jump to the assembly routine.
1013 (emit-label not-single)))
1016 (inst lea return-asm
1017 (make-ea :qword :disp (make-fixup 'return-multiple
1018 :assembly-routine)))
1019 (inst jmp return-asm)
1020 (trace-table-entry trace-table-normal)))
1024 ;;; We don't need to do anything special for regular functions.
1025 (define-vop (setup-environment)
1029 ;; Don't bother doing anything.
1032 ;;; Get the lexical environment from its passing location.
1033 (define-vop (setup-closure-environment)
1034 (:results (closure :scs (descriptor-reg)))
1039 (move closure rax-tn)))
1041 ;;; Copy a &MORE arg from the argument area to the end of the current
1042 ;;; frame. FIXED is the number of non-&MORE arguments.
1043 (define-vop (copy-more-arg)
1044 (:temporary (:sc any-reg :offset r8-offset) copy-index)
1045 (:temporary (:sc any-reg :offset r9-offset) source)
1046 (:temporary (:sc descriptor-reg :offset r10-offset) temp)
1049 ;; Avoid the copy if there are no more args.
1050 (cond ((zerop fixed)
1051 (inst jrcxz JUST-ALLOC-FRAME))
1053 (inst cmp rcx-tn (fixnumize fixed))
1054 (inst jmp :be JUST-ALLOC-FRAME)))
1056 ;; Allocate the space on the stack.
1057 ;; stack = rbp + sp->fp-offset - (max 3 frame-size) - (nargs - fixed)
1059 (make-ea :qword :base rbp-tn
1060 :disp (* n-word-bytes
1061 (- (+ sp->fp-offset fixed)
1062 (max 3 (sb-allocated-size 'stack))))))
1063 (inst sub rbx-tn rcx-tn) ; Got the new stack in rbx
1064 (inst mov rsp-tn rbx-tn)
1066 ;; Now: nargs>=1 && nargs>fixed
1068 ;; Save the original count of args.
1069 (inst mov rbx-tn rcx-tn)
1071 (cond ((< fixed register-arg-count)
1072 ;; We must stop when we run out of stack args, not when we
1073 ;; run out of more args.
1074 ;; Number to copy = nargs-3
1075 (inst sub rcx-tn (fixnumize register-arg-count))
1076 ;; Everything of interest in registers.
1077 (inst jmp :be DO-REGS))
1079 ;; Number to copy = nargs-fixed
1080 (inst sub rcx-tn (fixnumize fixed))))
1082 ;; Initialize R8 to be the end of args.
1083 (inst lea source (make-ea :qword :base rbp-tn
1084 :disp (* sp->fp-offset n-word-bytes)))
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
1118 (max 3 (sb-allocated-size
1120 (nth i *register-arg-tns*))
1123 (when (>= i register-arg-count)
1126 ;; Don't deposit any more than there are.
1128 (inst test rcx-tn rcx-tn)
1129 (inst cmp rcx-tn (fixnumize i)))
1130 (inst jmp :eq DONE)))
1136 (make-ea :qword :base rbp-tn
1137 :disp (* n-word-bytes
1139 (max 3 (sb-allocated-size 'stack))))))
1143 (define-vop (more-kw-arg)
1144 (:translate sb!c::%more-kw-arg)
1145 (:policy :fast-safe)
1146 (:args (object :scs (descriptor-reg) :to (:result 1))
1147 (index :scs (any-reg) :to (:result 1) :target keyword))
1148 (:arg-types * tagged-num)
1149 (:results (value :scs (descriptor-reg any-reg))
1150 (keyword :scs (descriptor-reg any-reg)))
1153 (inst mov value (make-ea :qword :base object :index index))
1154 (inst mov keyword (make-ea :qword :base object :index index
1155 :disp n-word-bytes))))
1157 (define-vop (more-arg)
1158 (:translate sb!c::%more-arg)
1159 (:policy :fast-safe)
1160 (:args (object :scs (descriptor-reg) :to (:result 1))
1161 (index :scs (any-reg) :to (:result 1) :target value))
1162 (:arg-types * tagged-num)
1163 (:results (value :scs (descriptor-reg any-reg)))
1168 (inst mov value (make-ea :qword :base object :index value))))
1170 ;;; Turn more arg (context, count) into a list.
1171 (define-vop (listify-rest-args)
1172 (:translate %listify-rest-args)
1174 (:args (context :scs (descriptor-reg) :target src)
1175 (count :scs (any-reg) :target rcx))
1176 (:arg-types * tagged-num)
1177 (:temporary (:sc unsigned-reg :offset rsi-offset :from (:argument 0)) src)
1178 (:temporary (:sc unsigned-reg :offset rcx-offset :from (:argument 1)) rcx)
1179 (:temporary (:sc unsigned-reg :offset rax-offset) rax)
1180 (:temporary (:sc unsigned-reg) dst)
1181 (:results (result :scs (descriptor-reg)))
1184 (let ((enter (gen-label))
1187 (stack-allocate-p (node-stack-allocate-p node)))
1190 ;; Check to see whether there are no args, and just return NIL if so.
1191 (inst mov result nil-value)
1193 (inst lea dst (make-ea :qword :base rcx :index rcx))
1194 (maybe-pseudo-atomic stack-allocate-p
1195 (allocation dst dst node stack-allocate-p list-pointer-lowtag)
1196 ;; Set decrement mode (successive args at lower addresses)
1198 ;; Set up the result.
1200 ;; Jump into the middle of the loop, 'cause that's where we want
1204 ;; Compute a pointer to the next cons.
1205 (inst add dst (* cons-size n-word-bytes))
1206 ;; Store a pointer to this cons in the CDR of the previous cons.
1207 (storew dst dst -1 list-pointer-lowtag)
1209 ;; Grab one value and stash it in the car of this cons.
1210 (inst mov rax (make-ea :qword :base src))
1211 (inst sub src n-word-bytes)
1212 (storew rax dst 0 list-pointer-lowtag)
1213 ;; Go back for more.
1214 (inst sub rcx n-word-bytes)
1216 ;; NIL out the last cons.
1217 (storew nil-value dst 1 list-pointer-lowtag)
1219 (emit-label done))))
1221 ;;; Return the location and size of the &MORE arg glob created by
1222 ;;; COPY-MORE-ARG. SUPPLIED is the total number of arguments supplied
1223 ;;; (originally passed in RCX). FIXED is the number of non-rest
1226 ;;; We must duplicate some of the work done by COPY-MORE-ARG, since at
1227 ;;; that time the environment is in a pretty brain-damaged state,
1228 ;;; preventing this info from being returned as values. What we do is
1229 ;;; compute supplied - fixed, and return a pointer that many words
1230 ;;; below the current stack top.
1231 (define-vop (more-arg-context)
1232 (:policy :fast-safe)
1233 (:translate sb!c::%more-arg-context)
1234 (:args (supplied :scs (any-reg) :target count))
1235 (:arg-types positive-fixnum (:constant fixnum))
1237 (:results (context :scs (descriptor-reg))
1238 (count :scs (any-reg)))
1239 (:result-types t tagged-num)
1240 (:note "more-arg-context")
1242 (move count supplied)
1243 ;; SP at this point points at the last arg pushed.
1244 ;; Point to the first more-arg, not above it.
1245 (inst lea context (make-ea :qword :base rsp-tn
1246 :index count :scale 1
1247 :disp (- (+ (fixnumize fixed) n-word-bytes))))
1248 (unless (zerop fixed)
1249 (inst sub count (fixnumize fixed)))))
1251 ;;; Signal wrong argument count error if NARGS isn't equal to COUNT.
1252 (define-vop (verify-arg-count)
1253 (:policy :fast-safe)
1254 (:translate sb!c::%verify-arg-count)
1255 (:args (nargs :scs (any-reg)))
1256 (:arg-types positive-fixnum (:constant t))
1259 (:save-p :compute-only)
1262 (generate-error-code vop 'invalid-arg-count-error nargs)))
1264 (inst test nargs nargs) ; smaller instruction
1265 (inst cmp nargs (fixnumize count)))
1266 (inst jmp :ne err-lab))))
1268 ;;; Various other error signallers.
1269 (macrolet ((def (name error translate &rest args)
1270 `(define-vop (,name)
1272 `((:policy :fast-safe)
1273 (:translate ,translate)))
1274 (:args ,@(mapcar (lambda (arg)
1275 `(,arg :scs (any-reg descriptor-reg)))
1278 (:save-p :compute-only)
1280 (error-call vop ',error ,@args)))))
1281 (def arg-count-error invalid-arg-count-error
1282 sb!c::%arg-count-error nargs)
1283 (def type-check-error object-not-type-error sb!c::%type-check-error
1285 (def layout-invalid-error layout-invalid-error sb!c::%layout-invalid-error
1287 (def odd-key-args-error odd-key-args-error
1288 sb!c::%odd-key-args-error)
1289 (def unknown-key-arg-error unknown-key-arg-error
1290 sb!c::%unknown-key-arg-error key)
1291 (def nil-fun-returned-error nil-fun-returned-error nil fun))
1295 (defun emit-single-step-test ()
1296 ;; We use different ways of representing whether stepping is on on
1297 ;; +SB-THREAD / -SB-THREAD: on +SB-THREAD, we use a slot in the
1298 ;; thread structure. On -SB-THREAD we use the value of a static
1299 ;; symbol. Things are done this way, since reading a thread-local
1300 ;; slot from a symbol would require an extra register on +SB-THREAD,
1301 ;; and reading a slot from a thread structure would require an extra
1302 ;; register on -SB-THREAD. While this isn't critical for x86-64,
1303 ;; it's more serious for x86.
1305 (inst cmp (make-ea :qword
1306 :base thread-base-tn
1307 :disp (* thread-stepping-slot n-word-bytes))
1310 (inst cmp (make-ea :qword
1311 :disp (+ nil-value (static-symbol-offset
1312 'sb!impl::*stepping*)
1313 (* symbol-value-slot n-word-bytes)
1314 (- other-pointer-lowtag)))
1317 (define-vop (step-instrument-before-vop)
1318 (:policy :fast-safe)
1321 (emit-single-step-test)
1323 (inst break single-step-before-trap)
1325 (note-this-location vop :step-before-vop)))