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 ;;; The local call convention doesn't fit that well with x86-style
488 ;;; calls. Emit a header for local calls to pop the return address
489 ;;; in the right place.
490 (defun emit-block-header (start-label trampoline-label fall-thru-p alignp)
491 (when (and fall-thru-p (or trampoline-label alignp))
492 (inst jmp start-label))
494 (emit-alignment n-lowtag-bits #x90))
495 (when trampoline-label
496 (emit-label trampoline-label)
497 (popw rbp-tn (frame-word-offset return-pc-save-offset)))
498 (emit-label start-label))
500 ;;; Non-TR local call for a fixed number of values passed according to
501 ;;; the unknown values convention.
503 ;;; FP is the frame pointer in install before doing the call.
505 ;;; NFP would be the number-stack frame pointer if we had a separate
508 ;;; Args are the argument passing locations, which are specified only
509 ;;; to terminate their lifetimes in the caller.
511 ;;; VALUES are the return value locations (wired to the standard
512 ;;; passing locations). NVALS is the number of values received.
514 ;;; Save is the save info, which we can ignore since saving has been
517 ;;; TARGET is a continuation pointing to the start of the called
519 (define-vop (call-local)
523 (:results (values :more t))
525 (:move-args :local-call)
526 (:info arg-locs callee target nvals)
528 (:ignore nfp arg-locs args callee)
531 (trace-table-entry trace-table-call-site)
533 (note-this-location vop :call-site)
535 (default-unknown-values vop values nvals node)
536 (trace-table-entry trace-table-normal)))
538 ;;; Non-TR local call for a variable number of return values passed according
539 ;;; to the unknown values convention. The results are the start of the values
540 ;;; glob and the number of values received.
541 (define-vop (multiple-call-local unknown-values-receiver)
546 (:move-args :local-call)
547 (:info save callee target)
548 (:ignore args save nfp callee)
552 (trace-table-entry trace-table-call-site)
554 (note-this-location vop :call-site)
556 (note-this-location vop :unknown-return)
557 (receive-unknown-values values-start nvals start count node)
558 (trace-table-entry trace-table-normal)))
560 ;;;; local call with known values return
562 ;;; Non-TR local call with known return locations. Known-value return
563 ;;; works just like argument passing in local call.
565 ;;; Note: we can't use normal load-tn allocation for the fixed args,
566 ;;; since all registers may be tied up by the more operand. Instead,
567 ;;; we use MAYBE-LOAD-STACK-TN.
568 (define-vop (known-call-local)
572 (:results (res :more t))
573 (:move-args :local-call)
575 (:info save callee target)
576 (:ignore args res save nfp callee)
579 (trace-table-entry trace-table-call-site)
581 (note-this-location vop :call-site)
583 (note-this-location vop :known-return)
584 (trace-table-entry trace-table-normal)))
586 ;;; From Douglas Crosher
587 ;;; Return from known values call. We receive the return locations as
588 ;;; arguments to terminate their lifetimes in the returning function. We
589 ;;; restore FP and CSP and jump to the Return-PC.
590 (define-vop (known-return)
594 (:move-args :known-return)
596 (:ignore val-locs vals)
599 (check-ocfp-and-return-pc old-fp return-pc)
600 (trace-table-entry trace-table-fun-epilogue)
601 ;; Zot all of the stack except for the old-fp and return-pc.
602 (inst mov rsp-tn rbp-tn)
605 (trace-table-entry trace-table-normal)))
609 ;;; There is something of a cross-product effect with full calls.
610 ;;; Different versions are used depending on whether we know the
611 ;;; number of arguments or the name of the called function, and
612 ;;; whether we want fixed values, unknown values, or a tail call.
614 ;;; In full call, the arguments are passed creating a partial frame on
615 ;;; the stack top and storing stack arguments into that frame. On
616 ;;; entry to the callee, this partial frame is pointed to by FP.
618 ;;; This macro helps in the definition of full call VOPs by avoiding
619 ;;; code replication in defining the cross-product VOPs.
621 ;;; NAME is the name of the VOP to define.
623 ;;; NAMED is true if the first argument is an fdefinition object whose
624 ;;; definition is to be called.
626 ;;; RETURN is either :FIXED, :UNKNOWN or :TAIL:
627 ;;; -- If :FIXED, then the call is for a fixed number of values, returned in
628 ;;; the standard passing locations (passed as result operands).
629 ;;; -- If :UNKNOWN, then the result values are pushed on the stack, and the
630 ;;; result values are specified by the Start and Count as in the
631 ;;; unknown-values continuation representation.
632 ;;; -- If :TAIL, then do a tail-recursive call. No values are returned.
633 ;;; The Old-Fp and Return-PC are passed as the second and third arguments.
635 ;;; In non-tail calls, the pointer to the stack arguments is passed as
636 ;;; the last fixed argument. If Variable is false, then the passing
637 ;;; locations are passed as a more arg. Variable is true if there are
638 ;;; a variable number of arguments passed on the stack. Variable
639 ;;; cannot be specified with :TAIL return. TR variable argument call
640 ;;; is implemented separately.
642 ;;; In tail call with fixed arguments, the passing locations are
643 ;;; passed as a more arg, but there is no new-FP, since the arguments
644 ;;; have been set up in the current frame.
645 (macrolet ((define-full-call (name named return variable)
646 (aver (not (and variable (eq return :tail))))
648 ,@(when (eq return :unknown)
649 '(unknown-values-receiver)))
651 ,@(unless (eq return :tail)
652 '((new-fp :scs (any-reg) :to (:argument 1))))
654 (fun :scs (descriptor-reg control-stack)
655 :target rax :to (:argument 0))
657 ,@(when (eq return :tail)
661 ,@(unless variable '((args :more t :scs (descriptor-reg)))))
663 ,@(when (eq return :fixed)
664 '((:results (values :more t))))
666 (:save-p ,(if (eq return :tail) :compute-only t))
668 ,@(unless (or (eq return :tail) variable)
669 '((:move-args :full-call)))
673 ,@(unless (or variable (eq return :tail)) '(arg-locs))
674 ,@(unless variable '(nargs))
675 ,@(when (eq return :fixed) '(nvals))
679 ,@(unless (or variable (eq return :tail)) '(arg-locs))
680 ,@(unless variable '(args)))
682 ;; We pass either the fdefn object (for named call) or
683 ;; the actual function object (for unnamed call) in
684 ;; RAX. With named call, closure-tramp will replace it
685 ;; with the real function and invoke the real function
686 ;; for closures. Non-closures do not need this value,
687 ;; so don't care what shows up in it.
695 ;; We pass the number of arguments in RCX.
696 (:temporary (:sc unsigned-reg :offset rcx-offset :to :eval) rcx)
698 ;; With variable call, we have to load the
699 ;; register-args out of the (new) stack frame before
700 ;; doing the call. Therefore, we have to tell the
701 ;; lifetime stuff that we need to use them.
703 (mapcar (lambda (name offset)
704 `(:temporary (:sc descriptor-reg
709 *register-arg-names* *register-arg-offsets*))
711 ,@(when (eq return :tail)
712 '((:temporary (:sc unsigned-reg
716 ,@(unless (eq return :tail)
719 (:generator ,(+ (if named 5 0)
721 (if (eq return :tail) 0 10)
723 (if (eq return :unknown) 25 0))
724 (trace-table-entry trace-table-call-site)
726 ;; This has to be done before the frame pointer is
727 ;; changed! RAX stores the 'lexical environment' needed
733 ;; For variable call, compute the number of
734 ;; arguments and move some of the arguments to
737 ;; Compute the number of arguments.
738 (noise '(inst mov rcx new-fp))
739 (noise '(inst sub rcx rsp-tn))
740 ;; Move the necessary args to registers,
741 ;; this moves them all even if they are
744 for name in *register-arg-names*
745 for index downfrom -1
746 do (noise `(loadw ,name new-fp ,index)))
750 (inst mov rcx (fixnumize nargs)))))
751 ,@(cond ((eq return :tail)
752 '(;; Python has figured out what frame we should
753 ;; return to so might as well use that clue.
754 ;; This seems really important to the
755 ;; implementation of things like
756 ;; (without-interrupts ...)
758 ;; dtc; Could be doing a tail call from a
759 ;; known-local-call etc in which the old-fp
760 ;; or ret-pc are in regs or in non-standard
761 ;; places. If the passing location were
762 ;; wired to the stack in standard locations
763 ;; then these moves will be un-necessary;
764 ;; this is probably best for the x86.
767 (unless (= ocfp-save-offset
769 ;; FIXME: FORMAT T for stale
770 ;; diagnostic output (several of
771 ;; them around here), ick
772 (error "** tail-call old-fp not S0~%")
773 (move old-fp-tmp old-fp)
776 (frame-word-offset ocfp-save-offset))))
777 ((any-reg descriptor-reg)
778 (error "** tail-call old-fp in reg not S0~%")
781 (frame-word-offset ocfp-save-offset))))
783 ;; For tail call, we have to push the
784 ;; return-pc so that it looks like we CALLed
785 ;; despite the fact that we are going to JMP.
786 (inst push return-pc)
789 ;; For non-tail call, we have to save our
790 ;; frame pointer and install the new frame
791 ;; pointer. We can't load stack tns after this
793 `(;; Python doesn't seem to allocate a frame
794 ;; here which doesn't leave room for the
797 ;; The variable args are on the stack and
798 ;; become the frame, but there may be <3
799 ;; args and 3 stack slots are assumed
800 ;; allocate on the call. So need to ensure
801 ;; there are at least 3 slots. This hack
804 '(inst sub rsp-tn (fixnumize 3)))
806 ;; Bias the new-fp for use as an fp
808 '(inst sub new-fp (fixnumize sp->fp-offset)))
811 (storew rbp-tn new-fp
812 (frame-word-offset ocfp-save-offset))
814 (move rbp-tn new-fp) ; NB - now on new stack frame.
817 (when step-instrumenting
818 (emit-single-step-test)
820 (inst break single-step-around-trap))
823 (note-this-location vop :call-site)
825 (inst ,(if (eq return :tail) 'jmp 'call)
826 (make-ea :qword :base rax
828 '(- (* fdefn-raw-addr-slot
830 other-pointer-lowtag)
831 '(- (* closure-fun-slot n-word-bytes)
832 fun-pointer-lowtag))))
835 '((default-unknown-values vop values nvals node)))
837 '((note-this-location vop :unknown-return)
838 (receive-unknown-values values-start nvals start count
841 (trace-table-entry trace-table-normal)))))
843 (define-full-call call nil :fixed nil)
844 (define-full-call call-named t :fixed nil)
845 (define-full-call multiple-call nil :unknown nil)
846 (define-full-call multiple-call-named t :unknown nil)
847 (define-full-call tail-call nil :tail nil)
848 (define-full-call tail-call-named t :tail nil)
850 (define-full-call call-variable nil :fixed t)
851 (define-full-call multiple-call-variable nil :unknown t))
853 ;;; This is defined separately, since it needs special code that BLT's
854 ;;; the arguments down. All the real work is done in the assembly
855 ;;; routine. We just set things up so that it can find what it needs.
856 (define-vop (tail-call-variable)
857 (:args (args :scs (any-reg control-stack) :target rsi)
858 (function :scs (descriptor-reg control-stack) :target rax)
861 (:temporary (:sc unsigned-reg :offset rsi-offset :from (:argument 0)) rsi)
862 (:temporary (:sc unsigned-reg :offset rax-offset :from (:argument 1)) rax)
863 (:temporary (:sc unsigned-reg) call-target)
865 (check-ocfp-and-return-pc old-fp return-pc)
866 ;; Move these into the passing locations if they are not already there.
869 ;; And jump to the assembly routine.
870 (inst lea call-target
872 :disp (make-fixup 'tail-call-variable :assembly-routine)))
873 (inst jmp call-target)))
875 ;;;; unknown values return
877 ;;; Return a single-value using the Unknown-Values convention.
879 ;;; pfw--get wired-tn conflicts sometimes if register sc specd for args
880 ;;; having problems targeting args to regs -- using temps instead.
882 ;;; First off, modifying the return-pc defeats the branch-prediction
883 ;;; optimizations on modern CPUs quite handily. Second, we can do all
884 ;;; this without needing a temp register. Fixed the latter, at least.
885 ;;; -- AB 2006/Feb/04
886 (define-vop (return-single)
892 (check-ocfp-and-return-pc old-fp return-pc)
893 (trace-table-entry trace-table-fun-epilogue)
894 ;; Drop stack above old-fp
895 (inst mov rsp-tn rbp-tn)
896 ;; Clear the multiple-value return flag
898 ;; Restore the old frame pointer
903 ;;; Do unknown-values return of a fixed (other than 1) number of
904 ;;; values. The VALUES are required to be set up in the standard
905 ;;; passing locations. NVALS is the number of values returned.
907 ;;; Basically, we just load RCX with the number of values returned and
908 ;;; RBX with a pointer to the values, set RSP to point to the end of
909 ;;; the values, and jump directly to return-pc.
912 (return-pc :to (:eval 1))
916 ;; In the case of other than one value, we need these registers to
917 ;; tell the caller where they are and how many there are.
918 (:temporary (:sc unsigned-reg :offset rbx-offset) rbx)
919 (:temporary (:sc unsigned-reg :offset rcx-offset) rcx)
920 ;; We need to stretch the lifetime of return-pc past the argument
921 ;; registers so that we can default the argument registers without
922 ;; trashing return-pc.
923 (:temporary (:sc unsigned-reg :offset (first *register-arg-offsets*)
925 (:temporary (:sc unsigned-reg :offset (second *register-arg-offsets*)
927 (:temporary (:sc unsigned-reg :offset (third *register-arg-offsets*)
931 (check-ocfp-and-return-pc old-fp return-pc)
933 ;; This is handled in RETURN-SINGLE.
934 (error "nvalues is 1"))
935 (trace-table-entry trace-table-fun-epilogue)
936 ;; Establish the values pointer and values count.
937 (inst lea rbx (make-ea :qword :base rbp-tn
938 :disp (* sp->fp-offset n-word-bytes)))
940 (zeroize rcx) ; smaller
941 (inst mov rcx (fixnumize nvals)))
942 ;; Pre-default any argument register that need it.
943 (when (< nvals register-arg-count)
944 (let* ((arg-tns (nthcdr nvals (list a0 a1 a2)))
945 (first (first arg-tns)))
946 (inst mov first nil-value)
947 (dolist (tn (cdr arg-tns))
948 (inst mov tn first))))
949 ;; Set the multiple value return flag.
951 ;; And away we go. Except that return-pc is still on the
952 ;; stack and we've changed the stack pointer. So we have to
953 ;; tell it to index off of RBX instead of RBP.
954 (cond ((<= nvals register-arg-count)
955 (inst mov rsp-tn rbp-tn)
959 ;; Some values are on the stack after RETURN-PC and OLD-FP,
960 ;; can't return normally and some slots of the frame will
961 ;; be used as temporaries by the receiver.
963 ;; Clear as much of the stack as possible, but not past the
964 ;; old frame address.
966 (make-ea :qword :base rbp-tn
967 :disp (frame-byte-offset (1- nvals))))
969 (inst push (make-ea :qword :base rbx
970 :disp (frame-byte-offset
972 (tn-offset return-pc)))))
975 (trace-table-entry trace-table-normal)))
977 ;;; Do unknown-values return of an arbitrary number of values (passed
978 ;;; on the stack.) We check for the common case of a single return
979 ;;; value, and do that inline using the normal single value return
980 ;;; convention. Otherwise, we branch off to code that calls an
981 ;;; assembly-routine.
983 ;;; The assembly routine takes the following args:
984 ;;; RCX -- number of values to find there.
985 ;;; RSI -- pointer to where to find the values.
986 (define-vop (return-multiple)
989 (vals :scs (any-reg) :target rsi)
990 (nvals :scs (any-reg) :target rcx))
991 (:temporary (:sc unsigned-reg :offset rsi-offset :from (:argument 2)) rsi)
992 (:temporary (:sc unsigned-reg :offset rcx-offset :from (:argument 3)) rcx)
993 (:temporary (:sc unsigned-reg) return-asm)
994 (:temporary (:sc descriptor-reg :offset (first *register-arg-offsets*)
998 (check-ocfp-and-return-pc old-fp return-pc)
999 (trace-table-entry trace-table-fun-epilogue)
1000 (unless (policy node (> space speed))
1001 ;; Check for the single case.
1002 (let ((not-single (gen-label)))
1003 (inst cmp nvals (fixnumize 1))
1004 (inst jmp :ne not-single)
1005 ;; Return with one value.
1007 ;; Clear the stack until ocfp.
1008 (inst mov rsp-tn rbp-tn)
1009 ;; clear the multiple-value return flag
1014 ;; Nope, not the single case. Jump to the assembly routine.
1015 (emit-label not-single)))
1018 (inst lea return-asm
1019 (make-ea :qword :disp (make-fixup 'return-multiple
1020 :assembly-routine)))
1021 (inst jmp return-asm)
1022 (trace-table-entry trace-table-normal)))
1026 ;;; We don't need to do anything special for regular functions.
1027 (define-vop (setup-environment)
1031 ;; Don't bother doing anything.
1034 ;;; Get the lexical environment from its passing location.
1035 (define-vop (setup-closure-environment)
1036 (:results (closure :scs (descriptor-reg)))
1041 (move closure rax-tn)))
1043 ;;; Copy a &MORE arg from the argument area to the end of the current
1044 ;;; frame. FIXED is the number of non-&MORE arguments.
1045 (define-vop (copy-more-arg)
1046 (:temporary (:sc any-reg :offset r8-offset) copy-index)
1047 (:temporary (:sc any-reg :offset r9-offset) source)
1048 (:temporary (:sc descriptor-reg :offset r10-offset) temp)
1051 ;; Avoid the copy if there are no more args.
1052 (cond ((zerop fixed)
1053 (inst jrcxz JUST-ALLOC-FRAME))
1055 (inst cmp rcx-tn (fixnumize fixed))
1056 (inst jmp :be JUST-ALLOC-FRAME)))
1058 ;; Allocate the space on the stack.
1059 ;; stack = rbp + sp->fp-offset - (max 3 frame-size) - (nargs - fixed)
1061 (make-ea :qword :base rbp-tn
1062 :disp (* n-word-bytes
1063 (- (+ sp->fp-offset fixed)
1064 (max 3 (sb-allocated-size 'stack))))))
1065 (inst sub rbx-tn rcx-tn) ; Got the new stack in rbx
1066 (inst mov rsp-tn rbx-tn)
1068 ;; Now: nargs>=1 && nargs>fixed
1070 ;; Save the original count of args.
1071 (inst mov rbx-tn rcx-tn)
1073 (cond ((< fixed register-arg-count)
1074 ;; We must stop when we run out of stack args, not when we
1075 ;; run out of more args.
1076 ;; Number to copy = nargs-3
1077 (inst sub rcx-tn (fixnumize register-arg-count))
1078 ;; Everything of interest in registers.
1079 (inst jmp :be DO-REGS))
1081 ;; Number to copy = nargs-fixed
1082 (inst sub rcx-tn (fixnumize fixed))))
1084 ;; Initialize R8 to be the end of args.
1085 (inst lea source (make-ea :qword :base rbp-tn
1086 :disp (* sp->fp-offset n-word-bytes)))
1087 (inst sub source rbx-tn)
1089 ;; We need to copy from downwards up to avoid overwriting some of
1090 ;; the yet uncopied args. So we need to use R9 as the copy index
1091 ;; and RCX as the loop counter, rather than using RCX for both.
1092 (zeroize copy-index)
1094 ;; We used to use REP MOVS here, but on modern x86 it performs
1095 ;; much worse than an explicit loop for small blocks.
1097 (inst mov temp (make-ea :qword :base source :index copy-index))
1098 (inst mov (make-ea :qword :base rsp-tn :index copy-index) temp)
1099 (inst add copy-index n-word-bytes)
1100 (inst sub rcx-tn n-word-bytes)
1101 (inst jmp :nz COPY-LOOP)
1106 (inst mov rcx-tn rbx-tn)
1108 ;; Here: nargs>=1 && nargs>fixed
1109 (when (< fixed register-arg-count)
1110 ;; Now we have to deposit any more args that showed up in
1114 ;; Store it relative to rbp
1115 (inst mov (make-ea :qword :base rbp-tn
1116 :disp (* n-word-bytes
1120 (max 3 (sb-allocated-size
1122 (nth i *register-arg-tns*))
1125 (when (>= i register-arg-count)
1128 ;; Don't deposit any more than there are.
1130 (inst test rcx-tn rcx-tn)
1131 (inst cmp rcx-tn (fixnumize i)))
1132 (inst jmp :eq DONE)))
1138 (make-ea :qword :base rbp-tn
1139 :disp (* n-word-bytes
1141 (max 3 (sb-allocated-size 'stack))))))
1145 (define-vop (more-kw-arg)
1146 (:translate sb!c::%more-kw-arg)
1147 (:policy :fast-safe)
1148 (:args (object :scs (descriptor-reg) :to (:result 1))
1149 (index :scs (any-reg) :to (:result 1) :target keyword))
1150 (:arg-types * tagged-num)
1151 (:results (value :scs (descriptor-reg any-reg))
1152 (keyword :scs (descriptor-reg any-reg)))
1155 (inst mov value (make-ea :qword :base object :index index))
1156 (inst mov keyword (make-ea :qword :base object :index index
1157 :disp n-word-bytes))))
1159 (define-vop (more-arg)
1160 (:translate sb!c::%more-arg)
1161 (:policy :fast-safe)
1162 (:args (object :scs (descriptor-reg) :to (:result 1))
1163 (index :scs (any-reg) :to (:result 1) :target value))
1164 (:arg-types * tagged-num)
1165 (:results (value :scs (descriptor-reg any-reg)))
1170 (inst mov value (make-ea :qword :base object :index value))))
1172 ;;; Turn more arg (context, count) into a list.
1173 (define-vop (listify-rest-args)
1174 (:translate %listify-rest-args)
1176 (:args (context :scs (descriptor-reg) :target src)
1177 (count :scs (any-reg) :target rcx))
1178 (:arg-types * tagged-num)
1179 (:temporary (:sc unsigned-reg :offset rsi-offset :from (:argument 0)) src)
1180 (:temporary (:sc unsigned-reg :offset rcx-offset :from (:argument 1)) rcx)
1181 (:temporary (:sc unsigned-reg :offset rax-offset) rax)
1182 (:temporary (:sc unsigned-reg) dst)
1183 (:results (result :scs (descriptor-reg)))
1186 (let ((enter (gen-label))
1189 (stack-allocate-p (node-stack-allocate-p node)))
1192 ;; Check to see whether there are no args, and just return NIL if so.
1193 (inst mov result nil-value)
1195 (inst lea dst (make-ea :qword :base rcx :index rcx))
1196 (maybe-pseudo-atomic stack-allocate-p
1197 (allocation dst dst node stack-allocate-p list-pointer-lowtag)
1198 ;; Set decrement mode (successive args at lower addresses)
1200 ;; Set up the result.
1202 ;; Jump into the middle of the loop, 'cause that's where we want
1206 ;; Compute a pointer to the next cons.
1207 (inst add dst (* cons-size n-word-bytes))
1208 ;; Store a pointer to this cons in the CDR of the previous cons.
1209 (storew dst dst -1 list-pointer-lowtag)
1211 ;; Grab one value and stash it in the car of this cons.
1212 (inst mov rax (make-ea :qword :base src))
1213 (inst sub src n-word-bytes)
1214 (storew rax dst 0 list-pointer-lowtag)
1215 ;; Go back for more.
1216 (inst sub rcx n-word-bytes)
1218 ;; NIL out the last cons.
1219 (storew nil-value dst 1 list-pointer-lowtag)
1221 (emit-label done))))
1223 ;;; Return the location and size of the &MORE arg glob created by
1224 ;;; COPY-MORE-ARG. SUPPLIED is the total number of arguments supplied
1225 ;;; (originally passed in RCX). FIXED is the number of non-rest
1228 ;;; We must duplicate some of the work done by COPY-MORE-ARG, since at
1229 ;;; that time the environment is in a pretty brain-damaged state,
1230 ;;; preventing this info from being returned as values. What we do is
1231 ;;; compute supplied - fixed, and return a pointer that many words
1232 ;;; below the current stack top.
1233 (define-vop (more-arg-context)
1234 (:policy :fast-safe)
1235 (:translate sb!c::%more-arg-context)
1236 (:args (supplied :scs (any-reg) :target count))
1237 (:arg-types positive-fixnum (:constant fixnum))
1239 (:results (context :scs (descriptor-reg))
1240 (count :scs (any-reg)))
1241 (:result-types t tagged-num)
1242 (:note "more-arg-context")
1244 (move count supplied)
1245 ;; SP at this point points at the last arg pushed.
1246 ;; Point to the first more-arg, not above it.
1247 (inst lea context (make-ea :qword :base rsp-tn
1248 :index count :scale 1
1249 :disp (- (+ (fixnumize fixed) n-word-bytes))))
1250 (unless (zerop fixed)
1251 (inst sub count (fixnumize fixed)))))
1253 ;;; Signal wrong argument count error if NARGS isn't equal to COUNT.
1254 (define-vop (verify-arg-count)
1255 (:policy :fast-safe)
1256 (:translate sb!c::%verify-arg-count)
1257 (:args (nargs :scs (any-reg)))
1258 (:arg-types positive-fixnum (:constant t))
1261 (:save-p :compute-only)
1264 (generate-error-code vop 'invalid-arg-count-error nargs)))
1266 (inst test nargs nargs) ; smaller instruction
1267 (inst cmp nargs (fixnumize count)))
1268 (inst jmp :ne err-lab))))
1270 ;;; Various other error signallers.
1271 (macrolet ((def (name error translate &rest args)
1272 `(define-vop (,name)
1274 `((:policy :fast-safe)
1275 (:translate ,translate)))
1276 (:args ,@(mapcar (lambda (arg)
1277 `(,arg :scs (any-reg descriptor-reg)))
1280 (:save-p :compute-only)
1282 (error-call vop ',error ,@args)))))
1283 (def arg-count-error invalid-arg-count-error
1284 sb!c::%arg-count-error nargs)
1285 (def type-check-error object-not-type-error sb!c::%type-check-error
1287 (def layout-invalid-error layout-invalid-error sb!c::%layout-invalid-error
1289 (def odd-key-args-error odd-key-args-error
1290 sb!c::%odd-key-args-error)
1291 (def unknown-key-arg-error unknown-key-arg-error
1292 sb!c::%unknown-key-arg-error key)
1293 (def nil-fun-returned-error nil-fun-returned-error nil fun))
1297 (defun emit-single-step-test ()
1298 ;; We use different ways of representing whether stepping is on on
1299 ;; +SB-THREAD / -SB-THREAD: on +SB-THREAD, we use a slot in the
1300 ;; thread structure. On -SB-THREAD we use the value of a static
1301 ;; symbol. Things are done this way, since reading a thread-local
1302 ;; slot from a symbol would require an extra register on +SB-THREAD,
1303 ;; and reading a slot from a thread structure would require an extra
1304 ;; register on -SB-THREAD. While this isn't critical for x86-64,
1305 ;; it's more serious for x86.
1307 (inst cmp (make-ea :qword
1308 :base thread-base-tn
1309 :disp (* thread-stepping-slot n-word-bytes))
1312 (inst cmp (make-ea :qword
1313 :disp (+ nil-value (static-symbol-offset
1314 'sb!impl::*stepping*)
1315 (* symbol-value-slot n-word-bytes)
1316 (- other-pointer-lowtag)))
1319 (define-vop (step-instrument-before-vop)
1320 (:policy :fast-safe)
1323 (emit-single-step-test)
1325 (inst break single-step-before-trap)
1327 (note-this-location vop :step-before-vop)))