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))
149 (trace-table-entry trace-table-normal)))
151 ;;; This is emitted directly before either a known-call-local, call-local,
152 ;;; or a multiple-call-local. All it does is allocate stack space for the
153 ;;; callee (who has the same size stack as us).
154 (define-vop (allocate-frame)
155 (:results (res :scs (any-reg))
160 (inst lea res (make-ea :qword :base rsp-tn
161 :disp (- (* sp->fp-offset n-word-bytes))))
162 (inst sub rsp-tn (* n-word-bytes (sb-allocated-size 'stack)))))
164 ;;; Allocate a partial frame for passing stack arguments in a full
165 ;;; call. NARGS is the number of arguments passed. We allocate at
166 ;;; least 3 slots, because the XEP noise is going to want to use them
167 ;;; before it can extend the stack.
168 (define-vop (allocate-full-call-frame)
170 (:results (res :scs (any-reg)))
172 (inst lea res (make-ea :qword :base rsp-tn
173 :disp (- (* sp->fp-offset n-word-bytes))))
174 (inst sub rsp-tn (* (max nargs 3) n-word-bytes))))
176 ;;; Emit code needed at the return-point from an unknown-values call
177 ;;; for a fixed number of values. Values is the head of the TN-REF
178 ;;; list for the locations that the values are to be received into.
179 ;;; Nvals is the number of values that are to be received (should
180 ;;; equal the length of Values).
182 ;;; If 0 or 1 values are expected, then we just emit an instruction to
183 ;;; reset the SP (which will only be executed when other than 1 value
186 ;;; In the general case we have to do three things:
187 ;;; -- Default unsupplied register values. This need only be done
188 ;;; when a single value is returned, since register values are
189 ;;; defaulted by the called in the non-single case.
190 ;;; -- Default unsupplied stack values. This needs to be done whenever
191 ;;; there are stack values.
192 ;;; -- Reset SP. This must be done whenever other than 1 value is
193 ;;; returned, regardless of the number of values desired.
194 (defun default-unknown-values (vop values nvals node)
195 (declare (type (or tn-ref null) values)
196 (type unsigned-byte nvals))
197 (let ((type (sb!c::basic-combination-derived-type node)))
200 (note-this-location vop :single-value-return)
202 ((<= (sb!kernel:values-type-max-value-count type)
204 (when (and (named-type-p type)
205 (eq nil (named-type-name type)))
206 ;; The function never returns, it may happen that the code
207 ;; ends right here leavig the :SINGLE-VALUE-RETURN note
208 ;; dangling. Let's emit a NOP.
210 ((not (sb!kernel:values-type-may-be-single-value-p type))
211 (inst mov rsp-tn rbx-tn))
213 (inst cmov :c rsp-tn rbx-tn))))
214 ((<= nvals register-arg-count)
215 (note-this-location vop :unknown-return)
216 (when (sb!kernel:values-type-may-be-single-value-p type)
217 (let ((regs-defaulted (gen-label)))
218 (inst jmp :c regs-defaulted)
219 ;; Default the unsupplied registers.
220 (let* ((2nd-tn-ref (tn-ref-across values))
221 (2nd-tn (tn-ref-tn 2nd-tn-ref)))
222 (inst mov 2nd-tn nil-value)
225 for tn-ref = (tn-ref-across 2nd-tn-ref)
226 then (tn-ref-across tn-ref)
227 for count from 2 below register-arg-count
228 do (inst mov (tn-ref-tn tn-ref) 2nd-tn))))
229 (inst mov rbx-tn rsp-tn)
230 (emit-label regs-defaulted)))
231 (when (< register-arg-count
232 (sb!kernel:values-type-max-value-count type))
233 (inst mov rsp-tn rbx-tn)))
235 ;; The number of bytes depends on the relative jump instructions.
236 ;; Best case is 31+(n-3)*14, worst case is 35+(n-3)*18. For
237 ;; NVALS=6 that is 73/89 bytes, and for NVALS=7 that is 87/107
238 ;; bytes which is likely better than using the blt below.
239 (let ((regs-defaulted (gen-label))
240 (defaulting-done (gen-label))
241 (default-stack-slots (gen-label)))
242 (note-this-location vop :unknown-return)
243 ;; Branch off to the MV case.
244 (inst jmp :c regs-defaulted)
245 ;; Do the single value case.
246 ;; Default the register args
247 (inst mov rax-tn nil-value)
249 (val (tn-ref-across values) (tn-ref-across val)))
250 ((= i (min nvals register-arg-count)))
251 (inst mov (tn-ref-tn val) rax-tn))
252 ;; Fake other registers so it looks like we returned with all the
253 ;; registers filled in.
255 (inst jmp default-stack-slots)
256 (emit-label regs-defaulted)
257 (inst mov rax-tn nil-value)
258 (collect ((defaults))
259 (do ((i register-arg-count (1+ i))
260 (val (do ((i 0 (1+ i))
261 (val values (tn-ref-across val)))
262 ((= i register-arg-count) val))
263 (tn-ref-across val)))
265 (let ((default-lab (gen-label))
267 (first-stack-arg-p (= i register-arg-count)))
268 (defaults (cons default-lab
269 (cons tn first-stack-arg-p)))
270 (inst cmp rcx-tn (fixnumize i))
271 (inst jmp :be default-lab)
272 (when first-stack-arg-p
273 ;; There are stack args so the frame of the callee is
274 ;; still there, save RDX in its first slot temporalily.
275 (storew rdx-tn rbx-tn (frame-word-offset sp->fp-offset)))
276 (loadw rdx-tn rbx-tn (frame-word-offset (+ sp->fp-offset i)))
277 (inst mov tn rdx-tn)))
278 (emit-label defaulting-done)
279 (loadw rdx-tn rbx-tn (frame-word-offset sp->fp-offset))
281 (let ((defaults (defaults)))
283 (assemble (*elsewhere*)
284 (trace-table-entry trace-table-fun-prologue)
285 (emit-label default-stack-slots)
286 (dolist (default defaults)
287 (emit-label (car default))
289 ;; We are setting the first stack argument to NIL.
290 ;; The callee's stack frame is dead, save RDX by
291 ;; pushing it to the stack, it will end up at same
292 ;; place as in the (STOREW RDX-TN RBX-TN -1) case
295 (inst mov (second default) rax-tn))
296 (inst jmp defaulting-done)
297 (trace-table-entry trace-table-normal)))))))
299 (let ((regs-defaulted (gen-label))
300 (restore-edi (gen-label))
301 (no-stack-args (gen-label))
302 (default-stack-vals (gen-label))
303 (count-okay (gen-label)))
304 (note-this-location vop :unknown-return)
305 ;; Branch off to the MV case.
306 (inst jmp :c regs-defaulted)
307 ;; Default the register args, and set up the stack as if we
308 ;; entered the MV return point.
309 (inst mov rbx-tn rsp-tn)
310 (inst mov rdi-tn nil-value)
311 (inst mov rsi-tn rdi-tn)
312 ;; Compute a pointer to where to put the [defaulted] stack values.
313 (emit-label no-stack-args)
317 (make-ea :qword :base rbp-tn
318 :disp (frame-byte-offset register-arg-count)))
319 ;; Load RAX with NIL so we can quickly store it, and set up
320 ;; stuff for the loop.
321 (inst mov rax-tn nil-value)
323 (inst mov rcx-tn (- nvals register-arg-count))
324 ;; Jump into the default loop.
325 (inst jmp default-stack-vals)
326 ;; The regs are defaulted. We need to copy any stack arguments,
327 ;; and then default the remaining stack arguments.
328 (emit-label regs-defaulted)
329 ;; Compute the number of stack arguments, and if it's zero or
330 ;; less, don't copy any stack arguments.
331 (inst sub rcx-tn (fixnumize register-arg-count))
332 (inst jmp :le no-stack-args)
334 (storew rdi-tn rbx-tn (frame-word-offset (+ sp->fp-offset 1)))
335 ;; Throw away any unwanted args.
336 (inst cmp rcx-tn (fixnumize (- nvals register-arg-count)))
337 (inst jmp :be count-okay)
338 (inst mov rcx-tn (fixnumize (- nvals register-arg-count)))
339 (emit-label count-okay)
340 ;; Save the number of stack values.
341 (inst mov rax-tn rcx-tn)
342 ;; Compute a pointer to where the stack args go.
344 (make-ea :qword :base rbp-tn
345 :disp (frame-byte-offset register-arg-count)))
346 ;; Save ESI, and compute a pointer to where the args come from.
347 (storew rsi-tn rbx-tn (frame-word-offset (+ sp->fp-offset 2)))
349 (make-ea :qword :base rbx-tn
350 :disp (frame-byte-offset
351 (+ sp->fp-offset register-arg-count))))
353 (inst shr rcx-tn word-shift) ; make word count
358 (loadw rsi-tn rbx-tn (frame-word-offset (+ sp->fp-offset 2)))
359 ;; Now we have to default the remaining args. Find out how many.
360 (inst sub rax-tn (fixnumize (- nvals register-arg-count)))
362 ;; If none, then just blow out of here.
363 (inst jmp :le restore-edi)
364 (inst mov rcx-tn rax-tn)
365 (inst shr rcx-tn word-shift) ; word count
366 ;; Load RAX with NIL for fast storing.
367 (inst mov rax-tn nil-value)
369 (emit-label default-stack-vals)
372 ;; Restore EDI, and reset the stack.
373 (emit-label restore-edi)
374 (loadw rdi-tn rbx-tn (frame-word-offset (+ sp->fp-offset 1)))
375 (inst mov rsp-tn rbx-tn)
379 ;;;; unknown values receiving
381 ;;; Emit code needed at the return point for an unknown-values call
382 ;;; for an arbitrary number of values.
384 ;;; We do the single and non-single cases with no shared code: there
385 ;;; doesn't seem to be any potential overlap, and receiving a single
386 ;;; value is more important efficiency-wise.
388 ;;; When there is a single value, we just push it on the stack,
389 ;;; returning the old SP and 1.
391 ;;; When there is a variable number of values, we move all of the
392 ;;; argument registers onto the stack, and return ARGS and NARGS.
394 ;;; ARGS and NARGS are TNs wired to the named locations. We must
395 ;;; explicitly allocate these TNs, since their lifetimes overlap with
396 ;;; the results start and count. (Also, it's nice to be able to target
398 (defun receive-unknown-values (args nargs start count node)
399 (declare (type tn args nargs start count))
400 (let ((type (sb!c::basic-combination-derived-type node))
401 (variable-values (gen-label))
402 (stack-values (gen-label))
404 (when (sb!kernel:values-type-may-be-single-value-p type)
405 (inst jmp :c variable-values)
406 (cond ((location= start (first *register-arg-tns*))
407 (inst push (first *register-arg-tns*))
408 (inst lea start (make-ea :qword :base rsp-tn :disp n-word-bytes)))
409 (t (inst mov start rsp-tn)
410 (inst push (first *register-arg-tns*))))
411 (inst mov count (fixnumize 1))
413 (emit-label variable-values))
414 ;; The stack frame is burnt and RETurned from if there are no
415 ;; stack values. In this case quickly reallocate sufficient space.
416 (when (<= (sb!kernel:values-type-min-value-count type)
418 (inst cmp nargs (fixnumize register-arg-count))
419 (inst jmp :g stack-values)
420 (inst sub rsp-tn nargs)
421 (emit-label stack-values))
422 ;; dtc: this writes the registers onto the stack even if they are
423 ;; not needed, only the number specified in rcx are used and have
424 ;; stack allocated to them. No harm is done.
426 for arg in *register-arg-tns*
428 for j below (sb!kernel:values-type-max-value-count type)
429 do (storew arg args i))
436 ;;; VOP that can be inherited by unknown values receivers. The main thing this
437 ;;; handles is allocation of the result temporaries.
438 (define-vop (unknown-values-receiver)
439 (:temporary (:sc descriptor-reg :offset rbx-offset
440 :from :eval :to (:result 0))
442 (:temporary (:sc any-reg :offset rcx-offset
443 :from :eval :to (:result 1))
445 (:results (start :scs (any-reg control-stack))
446 (count :scs (any-reg control-stack))))
448 ;;;; local call with unknown values convention return
450 (defun check-ocfp-and-return-pc (old-fp return-pc)
452 (format t "*known-return: old-fp ~S, tn-kind ~S; ~S ~S~%"
453 old-fp (sb!c::tn-kind old-fp) (sb!c::tn-save-tn old-fp)
454 (sb!c::tn-kind (sb!c::tn-save-tn old-fp)))
456 (format t "*known-return: return-pc ~S, tn-kind ~S; ~S ~S~%"
457 return-pc (sb!c::tn-kind return-pc)
458 (sb!c::tn-save-tn return-pc)
459 (sb!c::tn-kind (sb!c::tn-save-tn return-pc)))
460 (unless (and (sc-is old-fp control-stack)
461 (= (tn-offset old-fp) ocfp-save-offset))
462 (error "ocfp not on stack in standard save location?"))
463 (unless (and (sc-is return-pc sap-stack)
464 (= (tn-offset return-pc) return-pc-save-offset))
465 (error "return-pc not on stack in standard save location?")))
467 ;;; Non-TR local call for a fixed number of values passed according to
468 ;;; the unknown values convention.
470 ;;; FP is the frame pointer in install before doing the call.
472 ;;; NFP would be the number-stack frame pointer if we had a separate
475 ;;; Args are the argument passing locations, which are specified only
476 ;;; to terminate their lifetimes in the caller.
478 ;;; VALUES are the return value locations (wired to the standard
479 ;;; passing locations). NVALS is the number of values received.
481 ;;; Save is the save info, which we can ignore since saving has been
484 ;;; TARGET is a continuation pointing to the start of the called
486 (define-vop (call-local)
490 (:temporary (:sc unsigned-reg) return-label)
491 (:results (values :more t))
493 (:move-args :local-call)
494 (:info arg-locs callee target nvals)
496 (:ignore nfp arg-locs args #+nil callee)
499 (trace-table-entry trace-table-call-site)
502 (let ((ret-tn (callee-return-pc-tn callee)))
504 (format t "*call-local ~S; tn-kind ~S; tn-save-tn ~S; its tn-kind ~S~%"
505 ret-tn (sb!c::tn-kind ret-tn) (sb!c::tn-save-tn ret-tn)
506 (sb!c::tn-kind (sb!c::tn-save-tn ret-tn)))
508 ;; Is the return-pc on the stack or in a register?
511 (unless (= (tn-offset ret-tn) return-pc-save-offset)
512 (error "ret-tn ~A in wrong stack slot" ret-tn))
513 #+nil (format t "*call-local: ret-tn on stack; offset=~S~%"
515 (inst lea return-label (make-fixup nil :code-object RETURN))
516 (storew return-label rbp-tn (frame-word-offset (tn-offset ret-tn))))
518 (error "ret-tn ~A in sap-reg" ret-tn))))
520 (note-this-location vop :call-site)
523 (default-unknown-values vop values nvals node)
524 (trace-table-entry trace-table-normal)))
526 ;;; Non-TR local call for a variable number of return values passed according
527 ;;; to the unknown values convention. The results are the start of the values
528 ;;; glob and the number of values received.
529 (define-vop (multiple-call-local unknown-values-receiver)
533 (:temporary (:sc unsigned-reg) return-label)
535 (:move-args :local-call)
536 (:info save callee target)
537 (:ignore args save nfp #+nil callee)
541 (trace-table-entry trace-table-call-site)
544 (let ((ret-tn (callee-return-pc-tn callee)))
546 (format t "*multiple-call-local ~S; tn-kind ~S; tn-save-tn ~S; its tn-kind ~S~%"
547 ret-tn (sb!c::tn-kind ret-tn) (sb!c::tn-save-tn ret-tn)
548 (sb!c::tn-kind (sb!c::tn-save-tn ret-tn)))
550 ;; Is the return-pc on the stack or in a register?
553 #+nil (format t "*multiple-call-local: ret-tn on stack; offset=~S~%"
556 (inst lea return-label (make-fixup nil :code-object RETURN))
557 (storew return-label rbp-tn (frame-word-offset (tn-offset ret-tn))))
559 (error "multiple-call-local: return-pc not on stack."))))
561 (note-this-location vop :call-site)
564 (note-this-location vop :unknown-return)
565 (receive-unknown-values values-start nvals start count node)
566 (trace-table-entry trace-table-normal)))
568 ;;;; local call with known values return
570 ;;; Non-TR local call with known return locations. Known-value return
571 ;;; works just like argument passing in local call.
573 ;;; Note: we can't use normal load-tn allocation for the fixed args,
574 ;;; since all registers may be tied up by the more operand. Instead,
575 ;;; we use MAYBE-LOAD-STACK-TN.
576 (define-vop (known-call-local)
580 (:temporary (:sc unsigned-reg) return-label)
581 (:results (res :more t))
582 (:move-args :local-call)
584 (:info save callee target)
585 (:ignore args res save nfp #+nil callee)
588 (trace-table-entry trace-table-call-site)
591 (let ((ret-tn (callee-return-pc-tn callee)))
594 (format t "*known-call-local ~S; tn-kind ~S; tn-save-tn ~S; its tn-kind ~S~%"
595 ret-tn (sb!c::tn-kind ret-tn) (sb!c::tn-save-tn ret-tn)
596 (sb!c::tn-kind (sb!c::tn-save-tn ret-tn)))
598 ;; Is the return-pc on the stack or in a register?
601 #+nil (format t "*known-call-local: ret-tn on stack; offset=~S~%"
604 (inst lea return-label (make-fixup nil :code-object RETURN))
605 (storew return-label rbp-tn (frame-word-offset (tn-offset ret-tn))))
607 (error "known-call-local: return-pc not on stack."))))
609 (note-this-location vop :call-site)
612 (note-this-location vop :known-return)
613 (trace-table-entry trace-table-normal)))
615 ;;; From Douglas Crosher
616 ;;; Return from known values call. We receive the return locations as
617 ;;; arguments to terminate their lifetimes in the returning function. We
618 ;;; restore FP and CSP and jump to the Return-PC.
619 (define-vop (known-return)
623 (:move-args :known-return)
625 (:ignore val-locs vals)
628 (check-ocfp-and-return-pc old-fp return-pc)
629 (trace-table-entry trace-table-fun-epilogue)
630 ;; Zot all of the stack except for the old-fp and return-pc.
631 (inst mov rsp-tn rbp-tn)
634 (trace-table-entry trace-table-normal)))
638 ;;; There is something of a cross-product effect with full calls.
639 ;;; Different versions are used depending on whether we know the
640 ;;; number of arguments or the name of the called function, and
641 ;;; whether we want fixed values, unknown values, or a tail call.
643 ;;; In full call, the arguments are passed creating a partial frame on
644 ;;; the stack top and storing stack arguments into that frame. On
645 ;;; entry to the callee, this partial frame is pointed to by FP.
647 ;;; This macro helps in the definition of full call VOPs by avoiding
648 ;;; code replication in defining the cross-product VOPs.
650 ;;; NAME is the name of the VOP to define.
652 ;;; NAMED is true if the first argument is an fdefinition object whose
653 ;;; definition is to be called.
655 ;;; RETURN is either :FIXED, :UNKNOWN or :TAIL:
656 ;;; -- If :FIXED, then the call is for a fixed number of values, returned in
657 ;;; the standard passing locations (passed as result operands).
658 ;;; -- If :UNKNOWN, then the result values are pushed on the stack, and the
659 ;;; result values are specified by the Start and Count as in the
660 ;;; unknown-values continuation representation.
661 ;;; -- If :TAIL, then do a tail-recursive call. No values are returned.
662 ;;; The Old-Fp and Return-PC are passed as the second and third arguments.
664 ;;; In non-tail calls, the pointer to the stack arguments is passed as
665 ;;; the last fixed argument. If Variable is false, then the passing
666 ;;; locations are passed as a more arg. Variable is true if there are
667 ;;; a variable number of arguments passed on the stack. Variable
668 ;;; cannot be specified with :TAIL return. TR variable argument call
669 ;;; is implemented separately.
671 ;;; In tail call with fixed arguments, the passing locations are
672 ;;; passed as a more arg, but there is no new-FP, since the arguments
673 ;;; have been set up in the current frame.
674 (macrolet ((define-full-call (name named return variable)
675 (aver (not (and variable (eq return :tail))))
677 ,@(when (eq return :unknown)
678 '(unknown-values-receiver)))
680 ,@(unless (eq return :tail)
681 '((new-fp :scs (any-reg) :to (:argument 1))))
683 (fun :scs (descriptor-reg control-stack)
684 :target rax :to (:argument 0))
686 ,@(when (eq return :tail)
690 ,@(unless variable '((args :more t :scs (descriptor-reg)))))
692 ,@(when (eq return :fixed)
693 '((:results (values :more t))))
695 (:save-p ,(if (eq return :tail) :compute-only t))
697 ,@(unless (or (eq return :tail) variable)
698 '((:move-args :full-call)))
702 ,@(unless (or variable (eq return :tail)) '(arg-locs))
703 ,@(unless variable '(nargs))
704 ,@(when (eq return :fixed) '(nvals))
708 ,@(unless (or variable (eq return :tail)) '(arg-locs))
709 ,@(unless variable '(args)))
711 ;; We pass either the fdefn object (for named call) or
712 ;; the actual function object (for unnamed call) in
713 ;; RAX. With named call, closure-tramp will replace it
714 ;; with the real function and invoke the real function
715 ;; for closures. Non-closures do not need this value,
716 ;; so don't care what shows up in it.
724 ;; We pass the number of arguments in RCX.
725 (:temporary (:sc unsigned-reg :offset rcx-offset :to :eval) rcx)
727 ;; With variable call, we have to load the
728 ;; register-args out of the (new) stack frame before
729 ;; doing the call. Therefore, we have to tell the
730 ;; lifetime stuff that we need to use them.
732 (mapcar (lambda (name offset)
733 `(:temporary (:sc descriptor-reg
738 *register-arg-names* *register-arg-offsets*))
740 ,@(when (eq return :tail)
741 '((:temporary (:sc unsigned-reg
745 ,@(unless (eq return :tail)
748 (:generator ,(+ (if named 5 0)
750 (if (eq return :tail) 0 10)
752 (if (eq return :unknown) 25 0))
753 (trace-table-entry trace-table-call-site)
755 ;; This has to be done before the frame pointer is
756 ;; changed! RAX stores the 'lexical environment' needed
762 ;; For variable call, compute the number of
763 ;; arguments and move some of the arguments to
766 ;; Compute the number of arguments.
767 (noise '(inst mov rcx new-fp))
768 (noise '(inst sub rcx rsp-tn))
769 ;; Move the necessary args to registers,
770 ;; this moves them all even if they are
773 for name in *register-arg-names*
774 for index downfrom -1
775 do (noise `(loadw ,name new-fp ,index)))
779 (inst mov rcx (fixnumize nargs)))))
780 ,@(cond ((eq return :tail)
781 '(;; Python has figured out what frame we should
782 ;; return to so might as well use that clue.
783 ;; This seems really important to the
784 ;; implementation of things like
785 ;; (without-interrupts ...)
787 ;; dtc; Could be doing a tail call from a
788 ;; known-local-call etc in which the old-fp
789 ;; or ret-pc are in regs or in non-standard
790 ;; places. If the passing location were
791 ;; wired to the stack in standard locations
792 ;; then these moves will be un-necessary;
793 ;; this is probably best for the x86.
796 (unless (= ocfp-save-offset
798 ;; FIXME: FORMAT T for stale
799 ;; diagnostic output (several of
800 ;; them around here), ick
801 (error "** tail-call old-fp not S0~%")
802 (move old-fp-tmp old-fp)
805 (frame-word-offset ocfp-save-offset))))
806 ((any-reg descriptor-reg)
807 (error "** tail-call old-fp in reg not S0~%")
810 (frame-word-offset ocfp-save-offset))))
812 ;; For tail call, we have to push the
813 ;; return-pc so that it looks like we CALLed
814 ;; despite the fact that we are going to JMP.
815 (inst push return-pc)
818 ;; For non-tail call, we have to save our
819 ;; frame pointer and install the new frame
820 ;; pointer. We can't load stack tns after this
822 `(;; Python doesn't seem to allocate a frame
823 ;; here which doesn't leave room for the
826 ;; The variable args are on the stack and
827 ;; become the frame, but there may be <3
828 ;; args and 3 stack slots are assumed
829 ;; allocate on the call. So need to ensure
830 ;; there are at least 3 slots. This hack
833 '(inst sub rsp-tn (fixnumize 3)))
835 ;; Bias the new-fp for use as an fp
837 '(inst sub new-fp (fixnumize sp->fp-offset)))
840 (storew rbp-tn new-fp
841 (frame-word-offset ocfp-save-offset))
843 (move rbp-tn new-fp) ; NB - now on new stack frame.
846 (when step-instrumenting
847 (emit-single-step-test)
849 (inst break single-step-around-trap))
852 (note-this-location vop :call-site)
854 (inst ,(if (eq return :tail) 'jmp 'call)
855 (make-ea :qword :base rax
857 '(- (* fdefn-raw-addr-slot
859 other-pointer-lowtag)
860 '(- (* closure-fun-slot n-word-bytes)
861 fun-pointer-lowtag))))
864 '((default-unknown-values vop values nvals node)))
866 '((note-this-location vop :unknown-return)
867 (receive-unknown-values values-start nvals start count
870 (trace-table-entry trace-table-normal)))))
872 (define-full-call call nil :fixed nil)
873 (define-full-call call-named t :fixed nil)
874 (define-full-call multiple-call nil :unknown nil)
875 (define-full-call multiple-call-named t :unknown nil)
876 (define-full-call tail-call nil :tail nil)
877 (define-full-call tail-call-named t :tail nil)
879 (define-full-call call-variable nil :fixed t)
880 (define-full-call multiple-call-variable nil :unknown t))
882 ;;; This is defined separately, since it needs special code that BLT's
883 ;;; the arguments down. All the real work is done in the assembly
884 ;;; routine. We just set things up so that it can find what it needs.
885 (define-vop (tail-call-variable)
886 (:args (args :scs (any-reg control-stack) :target rsi)
887 (function :scs (descriptor-reg control-stack) :target rax)
890 (:temporary (:sc unsigned-reg :offset rsi-offset :from (:argument 0)) rsi)
891 (:temporary (:sc unsigned-reg :offset rax-offset :from (:argument 1)) rax)
892 (:temporary (:sc unsigned-reg) call-target)
894 (check-ocfp-and-return-pc old-fp return-pc)
895 ;; Move these into the passing locations if they are not already there.
898 ;; And jump to the assembly routine.
899 (inst lea call-target
901 :disp (make-fixup 'tail-call-variable :assembly-routine)))
902 (inst jmp call-target)))
904 ;;;; unknown values return
906 ;;; Return a single-value using the Unknown-Values convention.
908 ;;; pfw--get wired-tn conflicts sometimes if register sc specd for args
909 ;;; having problems targeting args to regs -- using temps instead.
911 ;;; First off, modifying the return-pc defeats the branch-prediction
912 ;;; optimizations on modern CPUs quite handily. Second, we can do all
913 ;;; this without needing a temp register. Fixed the latter, at least.
914 ;;; -- AB 2006/Feb/04
915 (define-vop (return-single)
921 (check-ocfp-and-return-pc old-fp return-pc)
922 (trace-table-entry trace-table-fun-epilogue)
923 ;; Drop stack above old-fp
924 (inst mov rsp-tn rbp-tn)
925 ;; Clear the multiple-value return flag
927 ;; Restore the old frame pointer
932 ;;; Do unknown-values return of a fixed (other than 1) number of
933 ;;; values. The VALUES are required to be set up in the standard
934 ;;; passing locations. NVALS is the number of values returned.
936 ;;; Basically, we just load RCX with the number of values returned and
937 ;;; RBX with a pointer to the values, set RSP to point to the end of
938 ;;; the values, and jump directly to return-pc.
941 (return-pc :to (:eval 1))
945 ;; In the case of other than one value, we need these registers to
946 ;; tell the caller where they are and how many there are.
947 (:temporary (:sc unsigned-reg :offset rbx-offset) rbx)
948 (:temporary (:sc unsigned-reg :offset rcx-offset) rcx)
949 ;; We need to stretch the lifetime of return-pc past the argument
950 ;; registers so that we can default the argument registers without
951 ;; trashing return-pc.
952 (:temporary (:sc unsigned-reg :offset (first *register-arg-offsets*)
954 (:temporary (:sc unsigned-reg :offset (second *register-arg-offsets*)
956 (:temporary (:sc unsigned-reg :offset (third *register-arg-offsets*)
960 (check-ocfp-and-return-pc old-fp return-pc)
962 ;; This is handled in RETURN-SINGLE.
963 (error "nvalues is 1"))
964 (trace-table-entry trace-table-fun-epilogue)
965 ;; Establish the values pointer and values count.
966 (inst lea rbx (make-ea :qword :base rbp-tn
967 :disp (* sp->fp-offset n-word-bytes)))
969 (zeroize rcx) ; smaller
970 (inst mov rcx (fixnumize nvals)))
971 ;; Pre-default any argument register that need it.
972 (when (< nvals register-arg-count)
973 (let* ((arg-tns (nthcdr nvals (list a0 a1 a2)))
974 (first (first arg-tns)))
975 (inst mov first nil-value)
976 (dolist (tn (cdr arg-tns))
977 (inst mov tn first))))
978 ;; Set the multiple value return flag.
980 ;; And away we go. Except that return-pc is still on the
981 ;; stack and we've changed the stack pointer. So we have to
982 ;; tell it to index off of RBX instead of RBP.
983 (cond ((<= nvals register-arg-count)
984 (inst mov rsp-tn rbp-tn)
988 ;; Some values are on the stack after RETURN-PC and OLD-FP,
989 ;; can't return normally and some slots of the frame will
990 ;; be used as temporaries by the receiver.
992 ;; Clear as much of the stack as possible, but not past the
993 ;; old frame address.
995 (make-ea :qword :base rbp-tn
996 :disp (frame-byte-offset (1- nvals))))
998 (inst push (make-ea :qword :base rbx
999 :disp (frame-byte-offset
1001 (tn-offset return-pc)))))
1004 (trace-table-entry trace-table-normal)))
1006 ;;; Do unknown-values return of an arbitrary number of values (passed
1007 ;;; on the stack.) We check for the common case of a single return
1008 ;;; value, and do that inline using the normal single value return
1009 ;;; convention. Otherwise, we branch off to code that calls an
1010 ;;; assembly-routine.
1012 ;;; The assembly routine takes the following args:
1013 ;;; RCX -- number of values to find there.
1014 ;;; RSI -- pointer to where to find the values.
1015 (define-vop (return-multiple)
1018 (vals :scs (any-reg) :target rsi)
1019 (nvals :scs (any-reg) :target rcx))
1020 (:temporary (:sc unsigned-reg :offset rsi-offset :from (:argument 2)) rsi)
1021 (:temporary (:sc unsigned-reg :offset rcx-offset :from (:argument 3)) rcx)
1022 (:temporary (:sc unsigned-reg) return-asm)
1023 (:temporary (:sc descriptor-reg :offset (first *register-arg-offsets*)
1024 :from (:eval 0)) a0)
1027 (check-ocfp-and-return-pc old-fp return-pc)
1028 (trace-table-entry trace-table-fun-epilogue)
1029 (unless (policy node (> space speed))
1030 ;; Check for the single case.
1031 (let ((not-single (gen-label)))
1032 (inst cmp nvals (fixnumize 1))
1033 (inst jmp :ne not-single)
1034 ;; Return with one value.
1036 ;; Clear the stack until ocfp.
1037 (inst mov rsp-tn rbp-tn)
1038 ;; clear the multiple-value return flag
1043 ;; Nope, not the single case. Jump to the assembly routine.
1044 (emit-label not-single)))
1047 (inst lea return-asm
1048 (make-ea :qword :disp (make-fixup 'return-multiple
1049 :assembly-routine)))
1050 (inst jmp return-asm)
1051 (trace-table-entry trace-table-normal)))
1055 ;;; We don't need to do anything special for regular functions.
1056 (define-vop (setup-environment)
1060 ;; Don't bother doing anything.
1063 ;;; Get the lexical environment from its passing location.
1064 (define-vop (setup-closure-environment)
1065 (:results (closure :scs (descriptor-reg)))
1070 (move closure rax-tn)))
1072 ;;; Copy a &MORE arg from the argument area to the end of the current
1073 ;;; frame. FIXED is the number of non-&MORE arguments.
1074 (define-vop (copy-more-arg)
1075 (:temporary (:sc any-reg :offset r8-offset) copy-index)
1076 (:temporary (:sc any-reg :offset r9-offset) source)
1077 (:temporary (:sc descriptor-reg :offset r10-offset) temp)
1080 ;; Avoid the copy if there are no more args.
1081 (cond ((zerop fixed)
1082 (inst jrcxz JUST-ALLOC-FRAME))
1084 (inst cmp rcx-tn (fixnumize fixed))
1085 (inst jmp :be JUST-ALLOC-FRAME)))
1087 ;; Allocate the space on the stack.
1088 ;; stack = rbp + sp->fp-offset - (max 3 frame-size) - (nargs - fixed)
1090 (make-ea :qword :base rbp-tn
1091 :disp (* n-word-bytes
1092 (- (+ sp->fp-offset fixed)
1093 (max 3 (sb-allocated-size 'stack))))))
1094 (inst sub rbx-tn rcx-tn) ; Got the new stack in rbx
1095 (inst mov rsp-tn rbx-tn)
1097 ;; Now: nargs>=1 && nargs>fixed
1099 ;; Save the original count of args.
1100 (inst mov rbx-tn rcx-tn)
1102 (cond ((< fixed register-arg-count)
1103 ;; We must stop when we run out of stack args, not when we
1104 ;; run out of more args.
1105 ;; Number to copy = nargs-3
1106 (inst sub rcx-tn (fixnumize register-arg-count))
1107 ;; Everything of interest in registers.
1108 (inst jmp :be DO-REGS))
1110 ;; Number to copy = nargs-fixed
1111 (inst sub rcx-tn (fixnumize fixed))))
1113 ;; Initialize R8 to be the end of args.
1114 (inst lea source (make-ea :qword :base rbp-tn
1115 :disp (* sp->fp-offset n-word-bytes)))
1116 (inst sub source rbx-tn)
1118 ;; We need to copy from downwards up to avoid overwriting some of
1119 ;; the yet uncopied args. So we need to use R9 as the copy index
1120 ;; and RCX as the loop counter, rather than using RCX for both.
1121 (zeroize copy-index)
1123 ;; We used to use REP MOVS here, but on modern x86 it performs
1124 ;; much worse than an explicit loop for small blocks.
1126 (inst mov temp (make-ea :qword :base source :index copy-index))
1127 (inst mov (make-ea :qword :base rsp-tn :index copy-index) temp)
1128 (inst add copy-index n-word-bytes)
1129 (inst sub rcx-tn n-word-bytes)
1130 (inst jmp :nz COPY-LOOP)
1135 (inst mov rcx-tn rbx-tn)
1137 ;; Here: nargs>=1 && nargs>fixed
1138 (when (< fixed register-arg-count)
1139 ;; Now we have to deposit any more args that showed up in
1143 ;; Store it relative to rbp
1144 (inst mov (make-ea :qword :base rbp-tn
1145 :disp (* n-word-bytes
1149 (max 3 (sb-allocated-size
1151 (nth i *register-arg-tns*))
1154 (when (>= i register-arg-count)
1157 ;; Don't deposit any more than there are.
1159 (inst test rcx-tn rcx-tn)
1160 (inst cmp rcx-tn (fixnumize i)))
1161 (inst jmp :eq DONE)))
1167 (make-ea :qword :base rbp-tn
1168 :disp (* n-word-bytes
1170 (max 3 (sb-allocated-size 'stack))))))
1174 (define-vop (more-kw-arg)
1175 (:translate sb!c::%more-kw-arg)
1176 (:policy :fast-safe)
1177 (:args (object :scs (descriptor-reg) :to (:result 1))
1178 (index :scs (any-reg) :to (:result 1) :target keyword))
1179 (:arg-types * tagged-num)
1180 (:results (value :scs (descriptor-reg any-reg))
1181 (keyword :scs (descriptor-reg any-reg)))
1184 (inst mov value (make-ea :qword :base object :index index))
1185 (inst mov keyword (make-ea :qword :base object :index index
1186 :disp n-word-bytes))))
1188 (define-vop (more-arg)
1189 (:translate sb!c::%more-arg)
1190 (:policy :fast-safe)
1191 (:args (object :scs (descriptor-reg) :to (:result 1))
1192 (index :scs (any-reg) :to (:result 1) :target value))
1193 (:arg-types * tagged-num)
1194 (:results (value :scs (descriptor-reg any-reg)))
1199 (inst mov value (make-ea :qword :base object :index value))))
1201 ;;; Turn more arg (context, count) into a list.
1202 (define-vop (listify-rest-args)
1203 (:translate %listify-rest-args)
1205 (:args (context :scs (descriptor-reg) :target src)
1206 (count :scs (any-reg) :target rcx))
1207 (:arg-types * tagged-num)
1208 (:temporary (:sc unsigned-reg :offset rsi-offset :from (:argument 0)) src)
1209 (:temporary (:sc unsigned-reg :offset rcx-offset :from (:argument 1)) rcx)
1210 (:temporary (:sc unsigned-reg :offset rax-offset) rax)
1211 (:temporary (:sc unsigned-reg) dst)
1212 (:results (result :scs (descriptor-reg)))
1215 (let ((enter (gen-label))
1218 (stack-allocate-p (node-stack-allocate-p node)))
1221 ;; Check to see whether there are no args, and just return NIL if so.
1222 (inst mov result nil-value)
1224 (inst lea dst (make-ea :qword :base rcx :index rcx))
1225 (maybe-pseudo-atomic stack-allocate-p
1226 (allocation dst dst node stack-allocate-p list-pointer-lowtag)
1227 (inst shr rcx (1- n-lowtag-bits))
1228 ;; Set decrement mode (successive args at lower addresses)
1230 ;; Set up the result.
1232 ;; Jump into the middle of the loop, 'cause that's where we want
1236 ;; Compute a pointer to the next cons.
1237 (inst add dst (* cons-size n-word-bytes))
1238 ;; Store a pointer to this cons in the CDR of the previous cons.
1239 (storew dst dst -1 list-pointer-lowtag)
1241 ;; Grab one value and stash it in the car of this cons.
1243 (storew rax dst 0 list-pointer-lowtag)
1244 ;; Go back for more.
1247 ;; NIL out the last cons.
1248 (storew nil-value dst 1 list-pointer-lowtag)
1250 (emit-label done))))
1252 ;;; Return the location and size of the &MORE arg glob created by
1253 ;;; COPY-MORE-ARG. SUPPLIED is the total number of arguments supplied
1254 ;;; (originally passed in RCX). FIXED is the number of non-rest
1257 ;;; We must duplicate some of the work done by COPY-MORE-ARG, since at
1258 ;;; that time the environment is in a pretty brain-damaged state,
1259 ;;; preventing this info from being returned as values. What we do is
1260 ;;; compute supplied - fixed, and return a pointer that many words
1261 ;;; below the current stack top.
1262 (define-vop (more-arg-context)
1263 (:policy :fast-safe)
1264 (:translate sb!c::%more-arg-context)
1265 (:args (supplied :scs (any-reg) :target count))
1266 (:arg-types positive-fixnum (:constant fixnum))
1268 (:results (context :scs (descriptor-reg))
1269 (count :scs (any-reg)))
1270 (:result-types t tagged-num)
1271 (:note "more-arg-context")
1273 (move count supplied)
1274 ;; SP at this point points at the last arg pushed.
1275 ;; Point to the first more-arg, not above it.
1276 (inst lea context (make-ea :qword :base rsp-tn
1277 :index count :scale 1
1278 :disp (- (+ (fixnumize fixed) n-word-bytes))))
1279 (unless (zerop fixed)
1280 (inst sub count (fixnumize fixed)))))
1282 ;;; Signal wrong argument count error if NARGS isn't equal to COUNT.
1283 (define-vop (verify-arg-count)
1284 (:policy :fast-safe)
1285 (:translate sb!c::%verify-arg-count)
1286 (:args (nargs :scs (any-reg)))
1287 (:arg-types positive-fixnum (:constant t))
1290 (:save-p :compute-only)
1293 (generate-error-code vop 'invalid-arg-count-error nargs)))
1295 (inst test nargs nargs) ; smaller instruction
1296 (inst cmp nargs (fixnumize count)))
1297 (inst jmp :ne err-lab))))
1299 ;;; Various other error signallers.
1300 (macrolet ((def (name error translate &rest args)
1301 `(define-vop (,name)
1303 `((:policy :fast-safe)
1304 (:translate ,translate)))
1305 (:args ,@(mapcar (lambda (arg)
1306 `(,arg :scs (any-reg descriptor-reg)))
1309 (:save-p :compute-only)
1311 (error-call vop ',error ,@args)))))
1312 (def arg-count-error invalid-arg-count-error
1313 sb!c::%arg-count-error nargs)
1314 (def type-check-error object-not-type-error sb!c::%type-check-error
1316 (def layout-invalid-error layout-invalid-error sb!c::%layout-invalid-error
1318 (def odd-key-args-error odd-key-args-error
1319 sb!c::%odd-key-args-error)
1320 (def unknown-key-arg-error unknown-key-arg-error
1321 sb!c::%unknown-key-arg-error key)
1322 (def nil-fun-returned-error nil-fun-returned-error nil fun))
1326 (defun emit-single-step-test ()
1327 ;; We use different ways of representing whether stepping is on on
1328 ;; +SB-THREAD / -SB-THREAD: on +SB-THREAD, we use a slot in the
1329 ;; thread structure. On -SB-THREAD we use the value of a static
1330 ;; symbol. Things are done this way, since reading a thread-local
1331 ;; slot from a symbol would require an extra register on +SB-THREAD,
1332 ;; and reading a slot from a thread structure would require an extra
1333 ;; register on -SB-THREAD. While this isn't critical for x86-64,
1334 ;; it's more serious for x86.
1336 (inst cmp (make-ea :qword
1337 :base thread-base-tn
1338 :disp (* thread-stepping-slot n-word-bytes))
1341 (inst cmp (make-ea :qword
1342 :disp (+ nil-value (static-symbol-offset
1343 'sb!impl::*stepping*)
1344 (* symbol-value-slot n-word-bytes)
1345 (- other-pointer-lowtag)))
1348 (define-vop (step-instrument-before-vop)
1349 (:policy :fast-safe)
1352 (emit-single-step-test)
1354 (inst break single-step-before-trap)
1356 (note-this-location vop :step-before-vop)))