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)
195 (declare (type (or tn-ref null) values)
196 (type unsigned-byte nvals))
199 (note-this-location vop :single-value-return)
200 (inst cmov :c rsp-tn rbx-tn))
201 ((<= nvals register-arg-count)
202 (let ((regs-defaulted (gen-label)))
203 (note-this-location vop :unknown-return)
204 (inst jmp :c regs-defaulted)
205 ;; Default the unsupplied registers.
206 (let* ((2nd-tn-ref (tn-ref-across values))
207 (2nd-tn (tn-ref-tn 2nd-tn-ref)))
208 (inst mov 2nd-tn nil-value)
211 for tn-ref = (tn-ref-across 2nd-tn-ref)
212 then (tn-ref-across tn-ref)
213 for count from 2 below register-arg-count
214 do (inst mov (tn-ref-tn tn-ref) 2nd-tn))))
215 (inst mov rbx-tn rsp-tn)
216 (emit-label regs-defaulted)
217 (inst mov rsp-tn rbx-tn)))
219 ;; The number of bytes depends on the relative jump instructions.
220 ;; Best case is 31+(n-3)*14, worst case is 35+(n-3)*18. For
221 ;; NVALS=6 that is 73/89 bytes, and for NVALS=7 that is 87/107
222 ;; bytes which is likely better than using the blt below.
223 (let ((regs-defaulted (gen-label))
224 (defaulting-done (gen-label))
225 (default-stack-slots (gen-label)))
226 (note-this-location vop :unknown-return)
227 ;; Branch off to the MV case.
228 (inst jmp :c regs-defaulted)
229 ;; Do the single value case.
230 ;; Default the register args
231 (inst mov rax-tn nil-value)
233 (val (tn-ref-across values) (tn-ref-across val)))
234 ((= i (min nvals register-arg-count)))
235 (inst mov (tn-ref-tn val) rax-tn))
237 ;; Fake other registers so it looks like we returned with all the
238 ;; registers filled in.
240 (inst jmp default-stack-slots)
242 (emit-label regs-defaulted)
244 (inst mov rax-tn nil-value)
245 (collect ((defaults))
246 (do ((i register-arg-count (1+ i))
247 (val (do ((i 0 (1+ i))
248 (val values (tn-ref-across val)))
249 ((= i register-arg-count) val))
250 (tn-ref-across val)))
252 (let ((default-lab (gen-label))
254 (first-stack-arg-p (= i register-arg-count)))
255 (defaults (cons default-lab (cons tn first-stack-arg-p)))
257 (inst cmp rcx-tn (fixnumize i))
258 (inst jmp :be default-lab)
259 (when first-stack-arg-p
260 ;; There are stack args so the frame of the callee is
261 ;; still there, save RDX in its first slot temporalily.
262 (storew rdx-tn rbx-tn (frame-word-offset sp->fp-offset)))
263 (loadw rdx-tn rbx-tn (frame-word-offset (+ sp->fp-offset i)))
264 (inst mov tn rdx-tn)))
266 (emit-label defaulting-done)
267 (loadw rdx-tn rbx-tn (frame-word-offset sp->fp-offset))
270 (let ((defaults (defaults)))
272 (assemble (*elsewhere*)
273 (trace-table-entry trace-table-fun-prologue)
274 (emit-label default-stack-slots)
275 (dolist (default defaults)
276 (emit-label (car default))
278 ;; We are setting the first stack argument to NIL.
279 ;; The callee's stack frame is dead, save RDX by
280 ;; pushing it to the stack, it will end up at same
281 ;; place as in the (STOREW RDX-TN RBX-TN -1) case
284 (inst mov (second default) rax-tn))
285 (inst jmp defaulting-done)
286 (trace-table-entry trace-table-normal)))))))
288 (let ((regs-defaulted (gen-label))
289 (restore-edi (gen-label))
290 (no-stack-args (gen-label))
291 (default-stack-vals (gen-label))
292 (count-okay (gen-label)))
293 (note-this-location vop :unknown-return)
294 ;; Branch off to the MV case.
295 (inst jmp :c regs-defaulted)
297 ;; Default the register args, and set up the stack as if we
298 ;; entered the MV return point.
299 (inst mov rbx-tn rsp-tn)
300 (inst mov rdi-tn nil-value)
301 (inst mov rsi-tn rdi-tn)
302 ;; Compute a pointer to where to put the [defaulted] stack values.
303 (emit-label no-stack-args)
307 (make-ea :qword :base rbp-tn
308 :disp (frame-byte-offset register-arg-count)))
309 ;; Load RAX with NIL so we can quickly store it, and set up
310 ;; stuff for the loop.
311 (inst mov rax-tn nil-value)
313 (inst mov rcx-tn (- nvals register-arg-count))
314 ;; Jump into the default loop.
315 (inst jmp default-stack-vals)
317 ;; The regs are defaulted. We need to copy any stack arguments,
318 ;; and then default the remaining stack arguments.
319 (emit-label regs-defaulted)
321 (storew rdi-tn rbx-tn (frame-word-offset (+ sp->fp-offset 1)))
322 ;; Compute the number of stack arguments, and if it's zero or
323 ;; less, don't copy any stack arguments.
324 (inst sub rcx-tn (fixnumize register-arg-count))
325 (inst jmp :le no-stack-args)
327 ;; Throw away any unwanted args.
328 (inst cmp rcx-tn (fixnumize (- nvals register-arg-count)))
329 (inst jmp :be count-okay)
330 (inst mov rcx-tn (fixnumize (- nvals register-arg-count)))
331 (emit-label count-okay)
332 ;; Save the number of stack values.
333 (inst mov rax-tn rcx-tn)
334 ;; Compute a pointer to where the stack args go.
336 (make-ea :qword :base rbp-tn
337 :disp (frame-byte-offset register-arg-count)))
338 ;; Save ESI, and compute a pointer to where the args come from.
339 (storew rsi-tn rbx-tn (frame-word-offset (+ sp->fp-offset 2)))
341 (make-ea :qword :base rbx-tn
342 :disp (frame-byte-offset
343 (+ sp->fp-offset register-arg-count))))
345 (inst shr rcx-tn word-shift) ; make word count
350 (loadw rsi-tn rbx-tn (frame-word-offset (+ sp->fp-offset 2)))
351 ;; Now we have to default the remaining args. Find out how many.
352 (inst sub rax-tn (fixnumize (- nvals register-arg-count)))
354 ;; If none, then just blow out of here.
355 (inst jmp :le restore-edi)
356 (inst mov rcx-tn rax-tn)
357 (inst shr rcx-tn word-shift) ; word count
358 ;; Load RAX with NIL for fast storing.
359 (inst mov rax-tn nil-value)
361 (emit-label default-stack-vals)
364 ;; Restore EDI, and reset the stack.
365 (emit-label restore-edi)
366 (loadw rdi-tn rbx-tn (frame-word-offset (+ sp->fp-offset 1)))
367 (inst mov rsp-tn rbx-tn)
371 ;;;; unknown values receiving
373 ;;; Emit code needed at the return point for an unknown-values call
374 ;;; for an arbitrary number of values.
376 ;;; We do the single and non-single cases with no shared code: there
377 ;;; doesn't seem to be any potential overlap, and receiving a single
378 ;;; value is more important efficiency-wise.
380 ;;; When there is a single value, we just push it on the stack,
381 ;;; returning the old SP and 1.
383 ;;; When there is a variable number of values, we move all of the
384 ;;; argument registers onto the stack, and return ARGS and NARGS.
386 ;;; ARGS and NARGS are TNs wired to the named locations. We must
387 ;;; explicitly allocate these TNs, since their lifetimes overlap with
388 ;;; the results start and count. (Also, it's nice to be able to target
390 (defun receive-unknown-values (args nargs start count)
391 (declare (type tn args nargs start count))
392 (let ((variable-values (gen-label))
393 (stack-values (gen-label))
395 (inst jmp :c variable-values)
397 (cond ((location= start (first *register-arg-tns*))
398 (inst push (first *register-arg-tns*))
399 (inst lea start (make-ea :qword :base rsp-tn :disp n-word-bytes)))
400 (t (inst mov start rsp-tn)
401 (inst push (first *register-arg-tns*))))
402 (inst mov count (fixnumize 1))
405 (emit-label variable-values)
406 ;; The stack frame is burnt and RETurned from if there are no
407 ;; stack values. In this case quickly reallocate sufficient space.
408 (inst cmp nargs (fixnumize register-arg-count))
409 (inst jmp :g stack-values)
410 (inst sub rsp-tn nargs)
411 (emit-label stack-values)
412 ;; dtc: this writes the registers onto the stack even if they are
413 ;; not needed, only the number specified in rcx are used and have
414 ;; stack allocated to them. No harm is done.
416 for arg in *register-arg-tns*
418 do (storew arg args i))
425 ;;; VOP that can be inherited by unknown values receivers. The main thing this
426 ;;; handles is allocation of the result temporaries.
427 (define-vop (unknown-values-receiver)
428 (:temporary (:sc descriptor-reg :offset rbx-offset
429 :from :eval :to (:result 0))
431 (:temporary (:sc any-reg :offset rcx-offset
432 :from :eval :to (:result 1))
434 (:results (start :scs (any-reg control-stack))
435 (count :scs (any-reg control-stack))))
437 ;;;; local call with unknown values convention return
439 (defun check-ocfp-and-return-pc (old-fp return-pc)
441 (format t "*known-return: old-fp ~S, tn-kind ~S; ~S ~S~%"
442 old-fp (sb!c::tn-kind old-fp) (sb!c::tn-save-tn old-fp)
443 (sb!c::tn-kind (sb!c::tn-save-tn old-fp)))
445 (format t "*known-return: return-pc ~S, tn-kind ~S; ~S ~S~%"
446 return-pc (sb!c::tn-kind return-pc)
447 (sb!c::tn-save-tn return-pc)
448 (sb!c::tn-kind (sb!c::tn-save-tn return-pc)))
449 (unless (and (sc-is old-fp control-stack)
450 (= (tn-offset old-fp) ocfp-save-offset))
451 (error "ocfp not on stack in standard save location?"))
452 (unless (and (sc-is return-pc sap-stack)
453 (= (tn-offset return-pc) return-pc-save-offset))
454 (error "return-pc not on stack in standard save location?")))
456 ;;; Non-TR local call for a fixed number of values passed according to
457 ;;; the unknown values convention.
459 ;;; FP is the frame pointer in install before doing the call.
461 ;;; NFP would be the number-stack frame pointer if we had a separate
464 ;;; Args are the argument passing locations, which are specified only
465 ;;; to terminate their lifetimes in the caller.
467 ;;; VALUES are the return value locations (wired to the standard
468 ;;; passing locations). NVALS is the number of values received.
470 ;;; Save is the save info, which we can ignore since saving has been
473 ;;; TARGET is a continuation pointing to the start of the called
475 (define-vop (call-local)
479 (:temporary (:sc unsigned-reg) return-label)
480 (:results (values :more t))
482 (:move-args :local-call)
483 (:info arg-locs callee target nvals)
485 (:ignore nfp arg-locs args #+nil callee)
487 (trace-table-entry trace-table-call-site)
490 (let ((ret-tn (callee-return-pc-tn callee)))
492 (format t "*call-local ~S; tn-kind ~S; tn-save-tn ~S; its tn-kind ~S~%"
493 ret-tn (sb!c::tn-kind ret-tn) (sb!c::tn-save-tn ret-tn)
494 (sb!c::tn-kind (sb!c::tn-save-tn ret-tn)))
496 ;; Is the return-pc on the stack or in a register?
499 (unless (= (tn-offset ret-tn) return-pc-save-offset)
500 (error "ret-tn ~A in wrong stack slot" ret-tn))
501 #+nil (format t "*call-local: ret-tn on stack; offset=~S~%"
503 (inst lea return-label (make-fixup nil :code-object RETURN))
504 (storew return-label rbp-tn (frame-word-offset (tn-offset ret-tn))))
506 (error "ret-tn ~A in sap-reg" ret-tn))))
508 (note-this-location vop :call-site)
511 (default-unknown-values vop values nvals)
512 (trace-table-entry trace-table-normal)))
514 ;;; Non-TR local call for a variable number of return values passed according
515 ;;; to the unknown values convention. The results are the start of the values
516 ;;; glob and the number of values received.
517 (define-vop (multiple-call-local unknown-values-receiver)
521 (:temporary (:sc unsigned-reg) return-label)
523 (:move-args :local-call)
524 (:info save callee target)
525 (:ignore args save nfp #+nil callee)
528 (trace-table-entry trace-table-call-site)
531 (let ((ret-tn (callee-return-pc-tn callee)))
533 (format t "*multiple-call-local ~S; tn-kind ~S; tn-save-tn ~S; its tn-kind ~S~%"
534 ret-tn (sb!c::tn-kind ret-tn) (sb!c::tn-save-tn ret-tn)
535 (sb!c::tn-kind (sb!c::tn-save-tn ret-tn)))
537 ;; Is the return-pc on the stack or in a register?
540 #+nil (format t "*multiple-call-local: ret-tn on stack; offset=~S~%"
543 (inst lea return-label (make-fixup nil :code-object RETURN))
544 (storew return-label rbp-tn (frame-word-offset (tn-offset ret-tn))))
546 (error "multiple-call-local: return-pc not on stack."))))
548 (note-this-location vop :call-site)
551 (note-this-location vop :unknown-return)
552 (receive-unknown-values values-start nvals start count)
553 (trace-table-entry trace-table-normal)))
555 ;;;; local call with known values return
557 ;;; Non-TR local call with known return locations. Known-value return
558 ;;; works just like argument passing in local call.
560 ;;; Note: we can't use normal load-tn allocation for the fixed args,
561 ;;; since all registers may be tied up by the more operand. Instead,
562 ;;; we use MAYBE-LOAD-STACK-TN.
563 (define-vop (known-call-local)
567 (:temporary (:sc unsigned-reg) return-label)
568 (:results (res :more t))
569 (:move-args :local-call)
571 (:info save callee target)
572 (:ignore args res save nfp #+nil callee)
575 (trace-table-entry trace-table-call-site)
578 (let ((ret-tn (callee-return-pc-tn callee)))
581 (format t "*known-call-local ~S; tn-kind ~S; tn-save-tn ~S; its tn-kind ~S~%"
582 ret-tn (sb!c::tn-kind ret-tn) (sb!c::tn-save-tn ret-tn)
583 (sb!c::tn-kind (sb!c::tn-save-tn ret-tn)))
585 ;; Is the return-pc on the stack or in a register?
588 #+nil (format t "*known-call-local: ret-tn on stack; offset=~S~%"
591 (inst lea return-label (make-fixup nil :code-object RETURN))
592 (storew return-label rbp-tn (frame-word-offset (tn-offset ret-tn))))
594 (error "known-call-local: return-pc not on stack."))))
596 (note-this-location vop :call-site)
599 (note-this-location vop :known-return)
600 (trace-table-entry trace-table-normal)))
602 ;;; From Douglas Crosher
603 ;;; Return from known values call. We receive the return locations as
604 ;;; arguments to terminate their lifetimes in the returning function. We
605 ;;; restore FP and CSP and jump to the Return-PC.
606 (define-vop (known-return)
610 (:move-args :known-return)
612 (:ignore val-locs vals)
615 (check-ocfp-and-return-pc old-fp return-pc)
616 (trace-table-entry trace-table-fun-epilogue)
617 ;; Zot all of the stack except for the old-fp and return-pc.
618 (inst mov rsp-tn rbp-tn)
621 (trace-table-entry trace-table-normal)))
625 ;;; There is something of a cross-product effect with full calls.
626 ;;; Different versions are used depending on whether we know the
627 ;;; number of arguments or the name of the called function, and
628 ;;; whether we want fixed values, unknown values, or a tail call.
630 ;;; In full call, the arguments are passed creating a partial frame on
631 ;;; the stack top and storing stack arguments into that frame. On
632 ;;; entry to the callee, this partial frame is pointed to by FP.
634 ;;; This macro helps in the definition of full call VOPs by avoiding
635 ;;; code replication in defining the cross-product VOPs.
637 ;;; NAME is the name of the VOP to define.
639 ;;; NAMED is true if the first argument is an fdefinition object whose
640 ;;; definition is to be called.
642 ;;; RETURN is either :FIXED, :UNKNOWN or :TAIL:
643 ;;; -- If :FIXED, then the call is for a fixed number of values, returned in
644 ;;; the standard passing locations (passed as result operands).
645 ;;; -- If :UNKNOWN, then the result values are pushed on the stack, and the
646 ;;; result values are specified by the Start and Count as in the
647 ;;; unknown-values continuation representation.
648 ;;; -- If :TAIL, then do a tail-recursive call. No values are returned.
649 ;;; The Old-Fp and Return-PC are passed as the second and third arguments.
651 ;;; In non-tail calls, the pointer to the stack arguments is passed as
652 ;;; the last fixed argument. If Variable is false, then the passing
653 ;;; locations are passed as a more arg. Variable is true if there are
654 ;;; a variable number of arguments passed on the stack. Variable
655 ;;; cannot be specified with :TAIL return. TR variable argument call
656 ;;; is implemented separately.
658 ;;; In tail call with fixed arguments, the passing locations are
659 ;;; passed as a more arg, but there is no new-FP, since the arguments
660 ;;; have been set up in the current frame.
661 (macrolet ((define-full-call (name named return variable)
662 (aver (not (and variable (eq return :tail))))
664 ,@(when (eq return :unknown)
665 '(unknown-values-receiver)))
667 ,@(unless (eq return :tail)
668 '((new-fp :scs (any-reg) :to (:argument 1))))
670 (fun :scs (descriptor-reg control-stack)
671 :target rax :to (:argument 0))
673 ,@(when (eq return :tail)
677 ,@(unless variable '((args :more t :scs (descriptor-reg)))))
679 ,@(when (eq return :fixed)
680 '((:results (values :more t))))
682 (:save-p ,(if (eq return :tail) :compute-only t))
684 ,@(unless (or (eq return :tail) variable)
685 '((:move-args :full-call)))
689 ,@(unless (or variable (eq return :tail)) '(arg-locs))
690 ,@(unless variable '(nargs))
691 ,@(when (eq return :fixed) '(nvals))
695 ,@(unless (or variable (eq return :tail)) '(arg-locs))
696 ,@(unless variable '(args)))
698 ;; We pass either the fdefn object (for named call) or
699 ;; the actual function object (for unnamed call) in
700 ;; RAX. With named call, closure-tramp will replace it
701 ;; with the real function and invoke the real function
702 ;; for closures. Non-closures do not need this value,
703 ;; so don't care what shows up in it.
711 ;; We pass the number of arguments in RCX.
712 (:temporary (:sc unsigned-reg :offset rcx-offset :to :eval) rcx)
714 ;; With variable call, we have to load the
715 ;; register-args out of the (new) stack frame before
716 ;; doing the call. Therefore, we have to tell the
717 ;; lifetime stuff that we need to use them.
719 (mapcar (lambda (name offset)
720 `(:temporary (:sc descriptor-reg
725 *register-arg-names* *register-arg-offsets*))
727 ,@(when (eq return :tail)
728 '((:temporary (:sc unsigned-reg
733 (:generator ,(+ (if named 5 0)
735 (if (eq return :tail) 0 10)
737 (if (eq return :unknown) 25 0))
738 (trace-table-entry trace-table-call-site)
740 ;; This has to be done before the frame pointer is
741 ;; changed! RAX stores the 'lexical environment' needed
747 ;; For variable call, compute the number of
748 ;; arguments and move some of the arguments to
751 ;; Compute the number of arguments.
752 (noise '(inst mov rcx new-fp))
753 (noise '(inst sub rcx rsp-tn))
754 ;; Move the necessary args to registers,
755 ;; this moves them all even if they are
758 for name in *register-arg-names*
759 for index downfrom -1
760 do (noise `(loadw ,name new-fp ,index)))
764 (inst mov rcx (fixnumize nargs)))))
765 ,@(cond ((eq return :tail)
766 '(;; Python has figured out what frame we should
767 ;; return to so might as well use that clue.
768 ;; This seems really important to the
769 ;; implementation of things like
770 ;; (without-interrupts ...)
772 ;; dtc; Could be doing a tail call from a
773 ;; known-local-call etc in which the old-fp
774 ;; or ret-pc are in regs or in non-standard
775 ;; places. If the passing location were
776 ;; wired to the stack in standard locations
777 ;; then these moves will be un-necessary;
778 ;; this is probably best for the x86.
781 (unless (= ocfp-save-offset
783 ;; FIXME: FORMAT T for stale
784 ;; diagnostic output (several of
785 ;; them around here), ick
786 (error "** tail-call old-fp not S0~%")
787 (move old-fp-tmp old-fp)
790 (frame-word-offset ocfp-save-offset))))
791 ((any-reg descriptor-reg)
792 (error "** tail-call old-fp in reg not S0~%")
795 (frame-word-offset ocfp-save-offset))))
797 ;; For tail call, we have to push the
798 ;; return-pc so that it looks like we CALLed
799 ;; despite the fact that we are going to JMP.
800 (inst push return-pc)
803 ;; For non-tail call, we have to save our
804 ;; frame pointer and install the new frame
805 ;; pointer. We can't load stack tns after this
807 `(;; Python doesn't seem to allocate a frame
808 ;; here which doesn't leave room for the
811 ;; The variable args are on the stack and
812 ;; become the frame, but there may be <3
813 ;; args and 3 stack slots are assumed
814 ;; allocate on the call. So need to ensure
815 ;; there are at least 3 slots. This hack
818 '(inst sub rsp-tn (fixnumize 3)))
820 ;; Bias the new-fp for use as an fp
822 '(inst sub new-fp (fixnumize sp->fp-offset)))
825 (storew rbp-tn new-fp
826 (frame-word-offset ocfp-save-offset))
828 (move rbp-tn new-fp) ; NB - now on new stack frame.
831 (when step-instrumenting
832 (emit-single-step-test)
834 (inst break single-step-around-trap))
837 (note-this-location vop :call-site)
839 (inst ,(if (eq return :tail) 'jmp 'call)
840 (make-ea :qword :base rax
842 '(- (* fdefn-raw-addr-slot
844 other-pointer-lowtag)
845 '(- (* closure-fun-slot n-word-bytes)
846 fun-pointer-lowtag))))
849 '((default-unknown-values vop values nvals)))
851 '((note-this-location vop :unknown-return)
852 (receive-unknown-values values-start nvals start count)))
854 (trace-table-entry trace-table-normal)))))
856 (define-full-call call nil :fixed nil)
857 (define-full-call call-named t :fixed nil)
858 (define-full-call multiple-call nil :unknown nil)
859 (define-full-call multiple-call-named t :unknown nil)
860 (define-full-call tail-call nil :tail nil)
861 (define-full-call tail-call-named t :tail nil)
863 (define-full-call call-variable nil :fixed t)
864 (define-full-call multiple-call-variable nil :unknown t))
866 ;;; This is defined separately, since it needs special code that BLT's
867 ;;; the arguments down. All the real work is done in the assembly
868 ;;; routine. We just set things up so that it can find what it needs.
869 (define-vop (tail-call-variable)
870 (:args (args :scs (any-reg control-stack) :target rsi)
871 (function :scs (descriptor-reg control-stack) :target rax)
874 (:temporary (:sc unsigned-reg :offset rsi-offset :from (:argument 0)) rsi)
875 (:temporary (:sc unsigned-reg :offset rax-offset :from (:argument 1)) rax)
876 (:temporary (:sc unsigned-reg) call-target)
878 (check-ocfp-and-return-pc old-fp return-pc)
879 ;; Move these into the passing locations if they are not already there.
882 ;; And jump to the assembly routine.
883 (inst lea call-target
885 :disp (make-fixup 'tail-call-variable :assembly-routine)))
886 (inst jmp call-target)))
888 ;;;; unknown values return
890 ;;; Return a single-value using the Unknown-Values convention.
892 ;;; pfw--get wired-tn conflicts sometimes if register sc specd for args
893 ;;; having problems targeting args to regs -- using temps instead.
895 ;;; First off, modifying the return-pc defeats the branch-prediction
896 ;;; optimizations on modern CPUs quite handily. Second, we can do all
897 ;;; this without needing a temp register. Fixed the latter, at least.
898 ;;; -- AB 2006/Feb/04
899 (define-vop (return-single)
905 (check-ocfp-and-return-pc old-fp return-pc)
906 (trace-table-entry trace-table-fun-epilogue)
907 ;; Drop stack above old-fp
908 (inst mov rsp-tn rbp-tn)
909 ;; Clear the multiple-value return flag
911 ;; Restore the old frame pointer
916 ;;; Do unknown-values return of a fixed (other than 1) number of
917 ;;; values. The VALUES are required to be set up in the standard
918 ;;; passing locations. NVALS is the number of values returned.
920 ;;; Basically, we just load RCX with the number of values returned and
921 ;;; RBX with a pointer to the values, set RSP to point to the end of
922 ;;; the values, and jump directly to return-pc.
925 (return-pc :to (:eval 1))
929 ;; In the case of other than one value, we need these registers to
930 ;; tell the caller where they are and how many there are.
931 (:temporary (:sc unsigned-reg :offset rbx-offset) rbx)
932 (:temporary (:sc unsigned-reg :offset rcx-offset) rcx)
933 ;; We need to stretch the lifetime of return-pc past the argument
934 ;; registers so that we can default the argument registers without
935 ;; trashing return-pc.
936 (:temporary (:sc unsigned-reg :offset (first *register-arg-offsets*)
938 (:temporary (:sc unsigned-reg :offset (second *register-arg-offsets*)
940 (:temporary (:sc unsigned-reg :offset (third *register-arg-offsets*)
944 (check-ocfp-and-return-pc old-fp return-pc)
946 ;; This is handled in RETURN-SINGLE.
947 (error "nvalues is 1"))
948 (trace-table-entry trace-table-fun-epilogue)
949 ;; Establish the values pointer and values count.
950 (inst lea rbx (make-ea :qword :base rbp-tn
951 :disp (* sp->fp-offset n-word-bytes)))
953 (zeroize rcx) ; smaller
954 (inst mov rcx (fixnumize nvals)))
955 ;; Pre-default any argument register that need it.
956 (when (< nvals register-arg-count)
957 (let* ((arg-tns (nthcdr nvals (list a0 a1 a2)))
958 (first (first arg-tns)))
959 (inst mov first nil-value)
960 (dolist (tn (cdr arg-tns))
961 (inst mov tn first))))
962 ;; Set the multiple value return flag.
964 ;; And away we go. Except that return-pc is still on the
965 ;; stack and we've changed the stack pointer. So we have to
966 ;; tell it to index off of RBX instead of RBP.
967 (cond ((<= nvals register-arg-count)
968 (inst mov rsp-tn rbp-tn)
972 ;; Some values are on the stack after RETURN-PC and OLD-FP,
973 ;; can't return normally and some slots of the frame will
974 ;; be used as temporaries by the receiver.
976 ;; Clear as much of the stack as possible, but not past the
977 ;; old frame address.
979 (make-ea :qword :base rbp-tn
980 :disp (frame-byte-offset (1- nvals))))
982 (inst push (make-ea :qword :base rbx
983 :disp (frame-byte-offset
985 (tn-offset return-pc)))))
988 (trace-table-entry trace-table-normal)))
990 ;;; Do unknown-values return of an arbitrary number of values (passed
991 ;;; on the stack.) We check for the common case of a single return
992 ;;; value, and do that inline using the normal single value return
993 ;;; convention. Otherwise, we branch off to code that calls an
994 ;;; assembly-routine.
996 ;;; The assembly routine takes the following args:
997 ;;; RCX -- number of values to find there.
998 ;;; RSI -- pointer to where to find the values.
999 (define-vop (return-multiple)
1002 (vals :scs (any-reg) :target rsi)
1003 (nvals :scs (any-reg) :target rcx))
1004 (:temporary (:sc unsigned-reg :offset rsi-offset :from (:argument 2)) rsi)
1005 (:temporary (:sc unsigned-reg :offset rcx-offset :from (:argument 3)) rcx)
1006 (:temporary (:sc unsigned-reg) return-asm)
1007 (:temporary (:sc descriptor-reg :offset (first *register-arg-offsets*)
1008 :from (:eval 0)) a0)
1011 (check-ocfp-and-return-pc old-fp return-pc)
1012 (trace-table-entry trace-table-fun-epilogue)
1013 (unless (policy node (> space speed))
1014 ;; Check for the single case.
1015 (let ((not-single (gen-label)))
1016 (inst cmp nvals (fixnumize 1))
1017 (inst jmp :ne not-single)
1018 ;; Return with one value.
1020 ;; Clear the stack until ocfp.
1021 (inst mov rsp-tn rbp-tn)
1022 ;; clear the multiple-value return flag
1027 ;; Nope, not the single case. Jump to the assembly routine.
1028 (emit-label not-single)))
1031 (inst lea return-asm
1032 (make-ea :qword :disp (make-fixup 'return-multiple
1033 :assembly-routine)))
1034 (inst jmp return-asm)
1035 (trace-table-entry trace-table-normal)))
1039 ;;; We don't need to do anything special for regular functions.
1040 (define-vop (setup-environment)
1044 ;; Don't bother doing anything.
1047 ;;; Get the lexical environment from its passing location.
1048 (define-vop (setup-closure-environment)
1049 (:results (closure :scs (descriptor-reg)))
1054 (move closure rax-tn)))
1056 ;;; Copy a &MORE arg from the argument area to the end of the current
1057 ;;; frame. FIXED is the number of non-&MORE arguments.
1058 (define-vop (copy-more-arg)
1059 (:temporary (:sc any-reg :offset r8-offset) copy-index)
1060 (:temporary (:sc any-reg :offset r9-offset) source)
1061 (:temporary (:sc descriptor-reg :offset r10-offset) temp)
1064 ;; Avoid the copy if there are no more args.
1065 (cond ((zerop fixed)
1066 (inst jrcxz JUST-ALLOC-FRAME))
1068 (inst cmp rcx-tn (fixnumize fixed))
1069 (inst jmp :be JUST-ALLOC-FRAME)))
1071 ;; Allocate the space on the stack.
1072 ;; stack = rbp + sp->fp-offset - (max 3 frame-size) - (nargs - fixed)
1074 (make-ea :qword :base rbp-tn
1075 :disp (* n-word-bytes
1076 (- (+ sp->fp-offset fixed)
1077 (max 3 (sb-allocated-size 'stack))))))
1078 (inst sub rbx-tn rcx-tn) ; Got the new stack in rbx
1079 (inst mov rsp-tn rbx-tn)
1081 ;; Now: nargs>=1 && nargs>fixed
1083 ;; Save the original count of args.
1084 (inst mov rbx-tn rcx-tn)
1086 (cond ((< fixed register-arg-count)
1087 ;; We must stop when we run out of stack args, not when we
1088 ;; run out of more args.
1089 ;; Number to copy = nargs-3
1090 (inst sub rcx-tn (fixnumize register-arg-count))
1091 ;; Everything of interest in registers.
1092 (inst jmp :be DO-REGS))
1094 ;; Number to copy = nargs-fixed
1095 (inst sub rcx-tn (fixnumize fixed))))
1097 ;; Initialize R8 to be the end of args.
1098 (inst lea source (make-ea :qword :base rbp-tn
1099 :disp (* sp->fp-offset n-word-bytes)))
1100 (inst sub source rbx-tn)
1102 ;; We need to copy from downwards up to avoid overwriting some of
1103 ;; the yet uncopied args. So we need to use R9 as the copy index
1104 ;; and RCX as the loop counter, rather than using RCX for both.
1105 (zeroize copy-index)
1107 ;; We used to use REP MOVS here, but on modern x86 it performs
1108 ;; much worse than an explicit loop for small blocks.
1110 (inst mov temp (make-ea :qword :base source :index copy-index))
1111 (inst mov (make-ea :qword :base rsp-tn :index copy-index) temp)
1112 (inst add copy-index n-word-bytes)
1113 (inst sub rcx-tn n-word-bytes)
1114 (inst jmp :nz COPY-LOOP)
1119 (inst mov rcx-tn rbx-tn)
1121 ;; Here: nargs>=1 && nargs>fixed
1122 (when (< fixed register-arg-count)
1123 ;; Now we have to deposit any more args that showed up in
1127 ;; Store it relative to rbp
1128 (inst mov (make-ea :qword :base rbp-tn
1129 :disp (* n-word-bytes
1133 (max 3 (sb-allocated-size
1135 (nth i *register-arg-tns*))
1138 (when (>= i register-arg-count)
1141 ;; Don't deposit any more than there are.
1143 (inst test rcx-tn rcx-tn)
1144 (inst cmp rcx-tn (fixnumize i)))
1145 (inst jmp :eq DONE)))
1151 (make-ea :qword :base rbp-tn
1152 :disp (* n-word-bytes
1154 (max 3 (sb-allocated-size 'stack))))))
1158 (define-vop (more-kw-arg)
1159 (:translate sb!c::%more-kw-arg)
1160 (:policy :fast-safe)
1161 (:args (object :scs (descriptor-reg) :to (:result 1))
1162 (index :scs (any-reg) :to (:result 1) :target keyword))
1163 (:arg-types * tagged-num)
1164 (:results (value :scs (descriptor-reg any-reg))
1165 (keyword :scs (descriptor-reg any-reg)))
1168 (inst mov value (make-ea :qword :base object :index index))
1169 (inst mov keyword (make-ea :qword :base object :index index
1170 :disp n-word-bytes))))
1172 (define-vop (more-arg)
1173 (:translate sb!c::%more-arg)
1174 (:policy :fast-safe)
1175 (:args (object :scs (descriptor-reg) :to (:result 1))
1176 (index :scs (any-reg) :to (:result 1) :target value))
1177 (:arg-types * tagged-num)
1178 (:results (value :scs (descriptor-reg any-reg)))
1183 (inst mov value (make-ea :qword :base object :index value))))
1185 ;;; Turn more arg (context, count) into a list.
1186 (define-vop (listify-rest-args)
1187 (:translate %listify-rest-args)
1189 (:args (context :scs (descriptor-reg) :target src)
1190 (count :scs (any-reg) :target rcx))
1191 (:arg-types * tagged-num)
1192 (:temporary (:sc unsigned-reg :offset rsi-offset :from (:argument 0)) src)
1193 (:temporary (:sc unsigned-reg :offset rcx-offset :from (:argument 1)) rcx)
1194 (:temporary (:sc unsigned-reg :offset rax-offset) rax)
1195 (:temporary (:sc unsigned-reg) dst)
1196 (:results (result :scs (descriptor-reg)))
1199 (let ((enter (gen-label))
1202 (stack-allocate-p (node-stack-allocate-p node)))
1205 ;; Check to see whether there are no args, and just return NIL if so.
1206 (inst mov result nil-value)
1208 (inst lea dst (make-ea :qword :base rcx :index rcx))
1209 (maybe-pseudo-atomic stack-allocate-p
1210 (allocation dst dst node stack-allocate-p list-pointer-lowtag)
1211 (inst shr rcx (1- n-lowtag-bits))
1212 ;; Set decrement mode (successive args at lower addresses)
1214 ;; Set up the result.
1216 ;; Jump into the middle of the loop, 'cause that's where we want
1220 ;; Compute a pointer to the next cons.
1221 (inst add dst (* cons-size n-word-bytes))
1222 ;; Store a pointer to this cons in the CDR of the previous cons.
1223 (storew dst dst -1 list-pointer-lowtag)
1225 ;; Grab one value and stash it in the car of this cons.
1227 (storew rax dst 0 list-pointer-lowtag)
1228 ;; Go back for more.
1231 ;; NIL out the last cons.
1232 (storew nil-value dst 1 list-pointer-lowtag)
1234 (emit-label done))))
1236 ;;; Return the location and size of the &MORE arg glob created by
1237 ;;; COPY-MORE-ARG. SUPPLIED is the total number of arguments supplied
1238 ;;; (originally passed in RCX). FIXED is the number of non-rest
1241 ;;; We must duplicate some of the work done by COPY-MORE-ARG, since at
1242 ;;; that time the environment is in a pretty brain-damaged state,
1243 ;;; preventing this info from being returned as values. What we do is
1244 ;;; compute supplied - fixed, and return a pointer that many words
1245 ;;; below the current stack top.
1246 (define-vop (more-arg-context)
1247 (:policy :fast-safe)
1248 (:translate sb!c::%more-arg-context)
1249 (:args (supplied :scs (any-reg) :target count))
1250 (:arg-types positive-fixnum (:constant fixnum))
1252 (:results (context :scs (descriptor-reg))
1253 (count :scs (any-reg)))
1254 (:result-types t tagged-num)
1255 (:note "more-arg-context")
1257 (move count supplied)
1258 ;; SP at this point points at the last arg pushed.
1259 ;; Point to the first more-arg, not above it.
1260 (inst lea context (make-ea :qword :base rsp-tn
1261 :index count :scale 1
1262 :disp (- (+ (fixnumize fixed) n-word-bytes))))
1263 (unless (zerop fixed)
1264 (inst sub count (fixnumize fixed)))))
1266 ;;; Signal wrong argument count error if NARGS isn't equal to COUNT.
1267 (define-vop (verify-arg-count)
1268 (:policy :fast-safe)
1269 (:translate sb!c::%verify-arg-count)
1270 (:args (nargs :scs (any-reg)))
1271 (:arg-types positive-fixnum (:constant t))
1274 (:save-p :compute-only)
1277 (generate-error-code vop 'invalid-arg-count-error nargs)))
1279 (inst test nargs nargs) ; smaller instruction
1280 (inst cmp nargs (fixnumize count)))
1281 (inst jmp :ne err-lab))))
1283 ;;; Various other error signallers.
1284 (macrolet ((def (name error translate &rest args)
1285 `(define-vop (,name)
1287 `((:policy :fast-safe)
1288 (:translate ,translate)))
1289 (:args ,@(mapcar (lambda (arg)
1290 `(,arg :scs (any-reg descriptor-reg)))
1293 (:save-p :compute-only)
1295 (error-call vop ',error ,@args)))))
1296 (def arg-count-error invalid-arg-count-error
1297 sb!c::%arg-count-error nargs)
1298 (def type-check-error object-not-type-error sb!c::%type-check-error
1300 (def layout-invalid-error layout-invalid-error sb!c::%layout-invalid-error
1302 (def odd-key-args-error odd-key-args-error
1303 sb!c::%odd-key-args-error)
1304 (def unknown-key-arg-error unknown-key-arg-error
1305 sb!c::%unknown-key-arg-error key)
1306 (def nil-fun-returned-error nil-fun-returned-error nil fun))
1310 (defun emit-single-step-test ()
1311 ;; We use different ways of representing whether stepping is on on
1312 ;; +SB-THREAD / -SB-THREAD: on +SB-THREAD, we use a slot in the
1313 ;; thread structure. On -SB-THREAD we use the value of a static
1314 ;; symbol. Things are done this way, since reading a thread-local
1315 ;; slot from a symbol would require an extra register on +SB-THREAD,
1316 ;; and reading a slot from a thread structure would require an extra
1317 ;; register on -SB-THREAD. While this isn't critical for x86-64,
1318 ;; it's more serious for x86.
1320 (inst cmp (make-ea :qword
1321 :base thread-base-tn
1322 :disp (* thread-stepping-slot n-word-bytes))
1325 (inst cmp (make-ea :qword
1326 :disp (+ nil-value (static-symbol-offset
1327 'sb!impl::*stepping*)
1328 (* symbol-value-slot n-word-bytes)
1329 (- other-pointer-lowtag)))
1332 (define-vop (step-instrument-before-vop)
1333 (:policy :fast-safe)
1336 (emit-single-step-test)
1338 (inst break single-step-before-trap)
1340 (note-this-location vop :step-before-vop)))