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 (align 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 (- (1+ return-pc-save-offset)))
138 ;; If copy-more-arg follows it will allocate the correct stack
139 ;; size. The stack is not allocated first here as this may expose
140 ;; args on the stack if they take up more space than the frame!
141 (unless copy-more-arg-follows
142 ;; The args fit within the frame so just allocate the frame.
144 (make-ea :qword :base rbp-tn
145 :disp (- (* n-word-bytes
146 (max 3 (sb-allocated-size 'stack)))))))
148 (trace-table-entry trace-table-normal)))
150 ;;; This is emitted directly before either a known-call-local, call-local,
151 ;;; or a multiple-call-local. All it does is allocate stack space for the
152 ;;; callee (who has the same size stack as us).
153 (define-vop (allocate-frame)
154 (:results (res :scs (any-reg control-stack))
160 (inst sub rsp-tn (* n-word-bytes (sb-allocated-size 'stack)))))
162 ;;; Allocate a partial frame for passing stack arguments in a full
163 ;;; call. NARGS is the number of arguments passed. We allocate at
164 ;;; least 3 slots, because the XEP noise is going to want to use them
165 ;;; before it can extend the stack.
166 (define-vop (allocate-full-call-frame)
168 (:results (res :scs (any-reg control-stack)))
171 (inst sub rsp-tn (* (max nargs 3) n-word-bytes))))
173 ;;; Emit code needed at the return-point from an unknown-values call
174 ;;; for a fixed number of values. Values is the head of the TN-REF
175 ;;; list for the locations that the values are to be received into.
176 ;;; Nvals is the number of values that are to be received (should
177 ;;; equal the length of Values).
179 ;;; MOVE-TEMP is a DESCRIPTOR-REG TN used as a temporary.
181 ;;; This code exploits the fact that in the unknown-values convention,
182 ;;; a single value return returns at the return PC + 2, whereas a
183 ;;; return of other than one value returns directly at the return PC.
185 ;;; If 0 or 1 values are expected, then we just emit an instruction to
186 ;;; reset the SP (which will only be executed when other than 1 value
189 ;;; In the general case we have to do three things:
190 ;;; -- Default unsupplied register values. This need only be done
191 ;;; when a single value is returned, since register values are
192 ;;; defaulted by the called in the non-single case.
193 ;;; -- Default unsupplied stack values. This needs to be done whenever
194 ;;; there are stack values.
195 ;;; -- Reset SP. This must be done whenever other than 1 value is
196 ;;; returned, regardless of the number of values desired.
197 (defun default-unknown-values (vop values nvals)
198 (declare (type (or tn-ref null) values)
199 (type unsigned-byte nvals))
202 (note-this-location vop :single-value-return)
203 (inst cmov :c rsp-tn rbx-tn))
204 ((<= nvals register-arg-count)
205 (let ((regs-defaulted (gen-label)))
206 (note-this-location vop :unknown-return)
207 (inst jmp :c regs-defaulted)
208 ;; Default the unsupplied registers.
209 (let* ((2nd-tn-ref (tn-ref-across values))
210 (2nd-tn (tn-ref-tn 2nd-tn-ref)))
211 (inst mov 2nd-tn nil-value)
214 for tn-ref = (tn-ref-across 2nd-tn-ref)
215 then (tn-ref-across tn-ref)
216 for count from 2 below register-arg-count
217 do (inst mov (tn-ref-tn tn-ref) 2nd-tn))))
218 (inst mov rbx-tn rsp-tn)
219 (emit-label regs-defaulted)
220 (inst mov rsp-tn rbx-tn)))
222 ;; The number of bytes depends on the relative jump instructions.
223 ;; Best case is 31+(n-3)*14, worst case is 35+(n-3)*18. For
224 ;; NVALS=6 that is 73/89 bytes, and for NVALS=7 that is 87/107
225 ;; bytes which is likely better than using the blt below.
226 (let ((regs-defaulted (gen-label))
227 (defaulting-done (gen-label))
228 (default-stack-slots (gen-label)))
229 (note-this-location vop :unknown-return)
230 ;; Branch off to the MV case.
231 (inst jmp :c regs-defaulted)
232 ;; Do the single value case.
233 ;; Default the register args
234 (inst mov rax-tn nil-value)
236 (val (tn-ref-across values) (tn-ref-across val)))
237 ((= i (min nvals register-arg-count)))
238 (inst mov (tn-ref-tn val) rax-tn))
240 ;; Fake other registers so it looks like we returned with all the
241 ;; registers filled in.
244 (inst jmp default-stack-slots)
246 (emit-label regs-defaulted)
248 (inst mov rax-tn nil-value)
249 (storew rdx-tn rbx-tn -1)
250 (collect ((defaults))
251 (do ((i register-arg-count (1+ i))
252 (val (do ((i 0 (1+ i))
253 (val values (tn-ref-across val)))
254 ((= i register-arg-count) val))
255 (tn-ref-across val)))
257 (let ((default-lab (gen-label))
258 (tn (tn-ref-tn val)))
259 (defaults (cons default-lab tn))
261 (inst cmp rcx-tn (fixnumize i))
262 (inst jmp :be default-lab)
263 (loadw rdx-tn rbx-tn (- (1+ i)))
264 (inst mov tn rdx-tn)))
266 (emit-label defaulting-done)
267 (loadw rdx-tn rbx-tn -1)
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))
277 (inst mov (cdr default) rax-tn))
278 (inst jmp defaulting-done)
279 (trace-table-entry trace-table-normal)))))))
281 (let ((regs-defaulted (gen-label))
282 (restore-edi (gen-label))
283 (no-stack-args (gen-label))
284 (default-stack-vals (gen-label))
285 (count-okay (gen-label)))
286 (note-this-location vop :unknown-return)
287 ;; Branch off to the MV case.
289 (inst jmp-short regs-defaulted)
291 ;; Default the register args, and set up the stack as if we
292 ;; entered the MV return point.
293 (inst mov rbx-tn rsp-tn)
295 (inst mov rdi-tn nil-value)
297 (inst mov rsi-tn rdi-tn)
298 ;; Compute a pointer to where to put the [defaulted] stack values.
299 (emit-label no-stack-args)
301 (make-ea :qword :base rbp-tn
302 :disp (* (- (1+ register-arg-count)) n-word-bytes)))
303 ;; Load RAX with NIL so we can quickly store it, and set up
304 ;; stuff for the loop.
305 (inst mov rax-tn nil-value)
307 (inst mov rcx-tn (- nvals register-arg-count))
308 ;; Jump into the default loop.
309 (inst jmp default-stack-vals)
311 ;; The regs are defaulted. We need to copy any stack arguments,
312 ;; and then default the remaining stack arguments.
313 (emit-label regs-defaulted)
315 (storew rdi-tn rbx-tn (- (1+ 1)))
316 ;; Compute the number of stack arguments, and if it's zero or
317 ;; less, don't copy any stack arguments.
318 (inst sub rcx-tn (fixnumize register-arg-count))
319 (inst jmp :le no-stack-args)
321 ;; Throw away any unwanted args.
322 (inst cmp rcx-tn (fixnumize (- nvals register-arg-count)))
323 (inst jmp :be count-okay)
324 (inst mov rcx-tn (fixnumize (- nvals register-arg-count)))
325 (emit-label count-okay)
326 ;; Save the number of stack values.
327 (inst mov rax-tn rcx-tn)
328 ;; Compute a pointer to where the stack args go.
330 (make-ea :qword :base rbp-tn
331 :disp (* (- (1+ register-arg-count)) n-word-bytes)))
332 ;; Save ESI, and compute a pointer to where the args come from.
333 (storew rsi-tn rbx-tn (- (1+ 2)))
335 (make-ea :qword :base rbx-tn
336 :disp (* (- (1+ register-arg-count)) n-word-bytes)))
338 (inst shr rcx-tn word-shift) ; make word count
343 (loadw rsi-tn rbx-tn (- (1+ 2)))
344 ;; Now we have to default the remaining args. Find out how many.
345 (inst sub rax-tn (fixnumize (- nvals register-arg-count)))
347 ;; If none, then just blow out of here.
348 (inst jmp :le restore-edi)
349 (inst mov rcx-tn rax-tn)
350 (inst shr rcx-tn word-shift) ; word count
351 ;; Load RAX with NIL for fast storing.
352 (inst mov rax-tn nil-value)
354 (emit-label default-stack-vals)
357 ;; Restore EDI, and reset the stack.
358 (emit-label restore-edi)
359 (loadw rdi-tn rbx-tn (- (1+ 1)))
360 (inst mov rsp-tn rbx-tn))))
363 ;;;; unknown values receiving
365 ;;; Emit code needed at the return point for an unknown-values call
366 ;;; for an arbitrary number of values.
368 ;;; We do the single and non-single cases with no shared code: there
369 ;;; doesn't seem to be any potential overlap, and receiving a single
370 ;;; value is more important efficiency-wise.
372 ;;; When there is a single value, we just push it on the stack,
373 ;;; returning the old SP and 1.
375 ;;; When there is a variable number of values, we move all of the
376 ;;; argument registers onto the stack, and return ARGS and NARGS.
378 ;;; ARGS and NARGS are TNs wired to the named locations. We must
379 ;;; explicitly allocate these TNs, since their lifetimes overlap with
380 ;;; the results start and count. (Also, it's nice to be able to target
382 (defun receive-unknown-values (args nargs start count)
383 (declare (type tn args nargs start count))
384 (let ((variable-values (gen-label))
386 (inst jmp :c variable-values)
388 (cond ((location= start (first *register-arg-tns*))
389 (inst push (first *register-arg-tns*))
390 (inst lea start (make-ea :qword :base rsp-tn :disp 8)))
391 (t (inst mov start rsp-tn)
392 (inst push (first *register-arg-tns*))))
393 (inst mov count (fixnumize 1))
396 (emit-label variable-values)
397 ;; dtc: this writes the registers onto the stack even if they are
398 ;; not needed, only the number specified in rcx are used and have
399 ;; stack allocated to them. No harm is done.
401 for arg in *register-arg-tns*
403 do (storew arg args i))
410 ;;; VOP that can be inherited by unknown values receivers. The main thing this
411 ;;; handles is allocation of the result temporaries.
412 (define-vop (unknown-values-receiver)
413 (:temporary (:sc descriptor-reg :offset rbx-offset
414 :from :eval :to (:result 0))
416 (:temporary (:sc any-reg :offset rcx-offset
417 :from :eval :to (:result 1))
419 (:results (start :scs (any-reg control-stack))
420 (count :scs (any-reg control-stack))))
422 ;;;; local call with unknown values convention return
424 ;;; Non-TR local call for a fixed number of values passed according to
425 ;;; the unknown values convention.
427 ;;; FP is the frame pointer in install before doing the call.
429 ;;; NFP would be the number-stack frame pointer if we had a separate
432 ;;; Args are the argument passing locations, which are specified only
433 ;;; to terminate their lifetimes in the caller.
435 ;;; VALUES are the return value locations (wired to the standard
436 ;;; passing locations). NVALS is the number of values received.
438 ;;; Save is the save info, which we can ignore since saving has been
441 ;;; TARGET is a continuation pointing to the start of the called
443 (define-vop (call-local)
447 (:temporary (:sc unsigned-reg) return-label)
448 (:results (values :more t))
450 (:move-args :local-call)
451 (:info arg-locs callee target nvals)
453 (:ignore nfp arg-locs args #+nil callee)
455 (trace-table-entry trace-table-call-site)
458 (let ((ret-tn (callee-return-pc-tn callee)))
460 (format t "*call-local ~S; tn-kind ~S; tn-save-tn ~S; its tn-kind ~S~%"
461 ret-tn (sb!c::tn-kind ret-tn) (sb!c::tn-save-tn ret-tn)
462 (sb!c::tn-kind (sb!c::tn-save-tn ret-tn)))
464 ;; Is the return-pc on the stack or in a register?
467 #+nil (format t "*call-local: ret-tn on stack; offset=~S~%"
469 (inst lea return-label (make-fixup nil :code-object RETURN))
470 (storew return-label rbp-tn (- (1+ (tn-offset ret-tn)))))
472 (inst lea ret-tn (make-fixup nil :code-object RETURN)))))
474 (note-this-location vop :call-site)
477 (default-unknown-values vop values nvals)
478 (trace-table-entry trace-table-normal)))
480 ;;; Non-TR local call for a variable number of return values passed according
481 ;;; to the unknown values convention. The results are the start of the values
482 ;;; glob and the number of values received.
483 (define-vop (multiple-call-local unknown-values-receiver)
487 (:temporary (:sc unsigned-reg) return-label)
489 (:move-args :local-call)
490 (:info save callee target)
491 (:ignore args save nfp #+nil callee)
494 (trace-table-entry trace-table-call-site)
497 (let ((ret-tn (callee-return-pc-tn callee)))
499 (format t "*multiple-call-local ~S; tn-kind ~S; tn-save-tn ~S; its tn-kind ~S~%"
500 ret-tn (sb!c::tn-kind ret-tn) (sb!c::tn-save-tn ret-tn)
501 (sb!c::tn-kind (sb!c::tn-save-tn ret-tn)))
503 ;; Is the return-pc on the stack or in a register?
506 #+nil (format t "*multiple-call-local: ret-tn on stack; offset=~S~%"
509 (inst lea return-label (make-fixup nil :code-object RETURN))
510 (storew return-label rbp-tn (- (1+ (tn-offset ret-tn)))))
513 (inst lea ret-tn (make-fixup nil :code-object RETURN)))))
515 (note-this-location vop :call-site)
518 (note-this-location vop :unknown-return)
519 (receive-unknown-values values-start nvals start count)
520 (trace-table-entry trace-table-normal)))
522 ;;;; local call with known values return
524 ;;; Non-TR local call with known return locations. Known-value return
525 ;;; works just like argument passing in local call.
527 ;;; Note: we can't use normal load-tn allocation for the fixed args,
528 ;;; since all registers may be tied up by the more operand. Instead,
529 ;;; we use MAYBE-LOAD-STACK-TN.
530 (define-vop (known-call-local)
534 (:temporary (:sc unsigned-reg) return-label)
535 (:results (res :more t))
536 (:move-args :local-call)
538 (:info save callee target)
539 (:ignore args res save nfp #+nil callee)
542 (trace-table-entry trace-table-call-site)
545 (let ((ret-tn (callee-return-pc-tn callee)))
548 (format t "*known-call-local ~S; tn-kind ~S; tn-save-tn ~S; its tn-kind ~S~%"
549 ret-tn (sb!c::tn-kind ret-tn) (sb!c::tn-save-tn ret-tn)
550 (sb!c::tn-kind (sb!c::tn-save-tn ret-tn)))
552 ;; Is the return-pc on the stack or in a register?
555 #+nil (format t "*known-call-local: ret-tn on stack; offset=~S~%"
558 (inst lea return-label (make-fixup nil :code-object RETURN))
559 (storew return-label rbp-tn (- (1+ (tn-offset ret-tn)))))
562 (inst lea ret-tn (make-fixup nil :code-object RETURN)))))
564 (note-this-location vop :call-site)
567 (note-this-location vop :known-return)
568 (trace-table-entry trace-table-normal)))
570 ;;; Return from known values call. We receive the return locations as
571 ;;; arguments to terminate their lifetimes in the returning function. We
572 ;;; restore FP and CSP and jump to the Return-PC.
574 ;;; We can assume we know exactly where old-fp and return-pc are because
575 ;;; make-old-fp-save-location and make-return-pc-save-location always
576 ;;; return the same place.
578 (define-vop (known-return)
580 (return-pc :scs (any-reg immediate-stack) :target rpc)
582 (:move-args :known-return)
584 (:temporary (:sc unsigned-reg :from (:argument 1)) rpc)
585 (:ignore val-locs vals)
588 (trace-table-entry trace-table-fun-epilogue)
589 ;; Save the return-pc in a register 'cause the frame-pointer is
590 ;; going away. Note this not in the usual stack location so we
593 ;; Restore the stack.
595 ;; Restore the old fp. We know OLD-FP is going to be in its stack
596 ;; save slot, which is a different frame that than this one,
597 ;; so we don't have to worry about having just cleared
598 ;; most of the stack.
601 (trace-table-entry trace-table-normal)))
603 ;;; From Douglas Crosher
604 ;;; Return from known values call. We receive the return locations as
605 ;;; arguments to terminate their lifetimes in the returning function. We
606 ;;; restore FP and CSP and jump to the Return-PC.
608 ;;; The old-fp may be either in a register or on the stack in its
609 ;;; standard save locations - slot 0.
611 ;;; The return-pc may be in a register or on the stack in any slot.
612 (define-vop (known-return)
616 (:move-args :known-return)
618 (:ignore val-locs vals)
621 (trace-table-entry trace-table-fun-epilogue)
622 ;; return-pc may be either in a register or on the stack.
627 (cond ((zerop (tn-offset old-fp))
628 ;; Zot all of the stack except for the old-fp.
629 (inst lea rsp-tn (make-ea :qword :base rbp-tn
630 :disp (- (* (1+ ocfp-save-offset)
632 ;; Restore the old fp from its save location on the stack,
633 ;; and zot the stack.
637 (cerror "Continue anyway"
638 "VOP return-local doesn't work if old-fp (in slot ~
639 ~S) is not in slot 0"
640 (tn-offset old-fp)))))
642 ((any-reg descriptor-reg)
643 ;; Zot all the stack.
645 ;; Restore the old-fp.
646 (move rbp-tn old-fp)))
648 ;; Return; return-pc is in a register.
649 (inst jmp return-pc))
653 (make-ea :qword :base rbp-tn
654 :disp (- (* (1+ (tn-offset return-pc)) n-word-bytes))))
656 (inst ret (* (tn-offset return-pc) n-word-bytes))))
658 (trace-table-entry trace-table-normal)))
662 ;;; There is something of a cross-product effect with full calls.
663 ;;; Different versions are used depending on whether we know the
664 ;;; number of arguments or the name of the called function, and
665 ;;; whether we want fixed values, unknown values, or a tail call.
667 ;;; In full call, the arguments are passed creating a partial frame on
668 ;;; the stack top and storing stack arguments into that frame. On
669 ;;; entry to the callee, this partial frame is pointed to by FP.
671 ;;; This macro helps in the definition of full call VOPs by avoiding
672 ;;; code replication in defining the cross-product VOPs.
674 ;;; NAME is the name of the VOP to define.
676 ;;; NAMED is true if the first argument is an fdefinition object whose
677 ;;; definition is to be called.
679 ;;; RETURN is either :FIXED, :UNKNOWN or :TAIL:
680 ;;; -- If :FIXED, then the call is for a fixed number of values, returned in
681 ;;; the standard passing locations (passed as result operands).
682 ;;; -- If :UNKNOWN, then the result values are pushed on the stack, and the
683 ;;; result values are specified by the Start and Count as in the
684 ;;; unknown-values continuation representation.
685 ;;; -- If :TAIL, then do a tail-recursive call. No values are returned.
686 ;;; The Old-Fp and Return-PC are passed as the second and third arguments.
688 ;;; In non-tail calls, the pointer to the stack arguments is passed as
689 ;;; the last fixed argument. If Variable is false, then the passing
690 ;;; locations are passed as a more arg. Variable is true if there are
691 ;;; a variable number of arguments passed on the stack. Variable
692 ;;; cannot be specified with :TAIL return. TR variable argument call
693 ;;; is implemented separately.
695 ;;; In tail call with fixed arguments, the passing locations are
696 ;;; passed as a more arg, but there is no new-FP, since the arguments
697 ;;; have been set up in the current frame.
698 (macrolet ((define-full-call (name named return variable)
699 (aver (not (and variable (eq return :tail))))
701 ,@(when (eq return :unknown)
702 '(unknown-values-receiver)))
704 ,@(unless (eq return :tail)
705 '((new-fp :scs (any-reg) :to (:argument 1))))
707 (fun :scs (descriptor-reg control-stack)
708 :target rax :to (:argument 0))
710 ,@(when (eq return :tail)
714 ,@(unless variable '((args :more t :scs (descriptor-reg)))))
716 ,@(when (eq return :fixed)
717 '((:results (values :more t))))
719 (:save-p ,(if (eq return :tail) :compute-only t))
721 ,@(unless (or (eq return :tail) variable)
722 '((:move-args :full-call)))
726 ,@(unless (or variable (eq return :tail)) '(arg-locs))
727 ,@(unless variable '(nargs))
728 ,@(when (eq return :fixed) '(nvals)))
731 ,@(unless (or variable (eq return :tail)) '(arg-locs))
732 ,@(unless variable '(args)))
734 ;; We pass either the fdefn object (for named call) or
735 ;; the actual function object (for unnamed call) in
736 ;; RAX. With named call, closure-tramp will replace it
737 ;; with the real function and invoke the real function
738 ;; for closures. Non-closures do not need this value,
739 ;; so don't care what shows up in it.
747 ;; We pass the number of arguments in RCX.
748 (:temporary (:sc unsigned-reg :offset rcx-offset :to :eval) rcx)
750 ;; With variable call, we have to load the
751 ;; register-args out of the (new) stack frame before
752 ;; doing the call. Therefore, we have to tell the
753 ;; lifetime stuff that we need to use them.
755 (mapcar (lambda (name offset)
756 `(:temporary (:sc descriptor-reg
761 *register-arg-names* *register-arg-offsets*))
763 ,@(when (eq return :tail)
764 '((:temporary (:sc unsigned-reg
769 (:generator ,(+ (if named 5 0)
771 (if (eq return :tail) 0 10)
773 (if (eq return :unknown) 25 0))
774 (trace-table-entry trace-table-call-site)
776 ;; This has to be done before the frame pointer is
777 ;; changed! RAX stores the 'lexical environment' needed
783 ;; For variable call, compute the number of
784 ;; arguments and move some of the arguments to
787 ;; Compute the number of arguments.
788 (noise '(inst mov rcx new-fp))
789 (noise '(inst sub rcx rsp-tn))
790 ;; Move the necessary args to registers,
791 ;; this moves them all even if they are
794 for name in *register-arg-names*
795 for index downfrom -1
796 do (noise `(loadw ,name new-fp ,index)))
800 (inst mov rcx (fixnumize nargs)))))
801 ,@(cond ((eq return :tail)
802 '(;; Python has figured out what frame we should
803 ;; return to so might as well use that clue.
804 ;; This seems really important to the
805 ;; implementation of things like
806 ;; (without-interrupts ...)
808 ;; dtc; Could be doing a tail call from a
809 ;; known-local-call etc in which the old-fp
810 ;; or ret-pc are in regs or in non-standard
811 ;; places. If the passing location were
812 ;; wired to the stack in standard locations
813 ;; then these moves will be un-necessary;
814 ;; this is probably best for the x86.
817 (unless (= ocfp-save-offset
819 ;; FIXME: FORMAT T for stale
820 ;; diagnostic output (several of
821 ;; them around here), ick
822 (format t "** tail-call old-fp not S0~%")
823 (move old-fp-tmp old-fp)
826 (- (1+ ocfp-save-offset)))))
827 ((any-reg descriptor-reg)
828 (format t "** tail-call old-fp in reg not S0~%")
831 (- (1+ ocfp-save-offset)))))
833 ;; For tail call, we have to push the
834 ;; return-pc so that it looks like we CALLed
835 ;; drspite the fact that we are going to JMP.
836 (inst push return-pc)
839 ;; For non-tail call, we have to save our
840 ;; frame pointer and install the new frame
841 ;; pointer. We can't load stack tns after this
843 `(;; Python doesn't seem to allocate a frame
844 ;; here which doesn't leave room for the
847 ;; The variable args are on the stack and
848 ;; become the frame, but there may be <3
849 ;; args and 3 stack slots are assumed
850 ;; allocate on the call. So need to ensure
851 ;; there are at least 3 slots. This hack
854 '(inst sub rsp-tn (fixnumize 3)))
857 (storew rbp-tn new-fp (- (1+ ocfp-save-offset)))
859 (move rbp-tn new-fp) ; NB - now on new stack frame.
862 (note-this-location vop :call-site)
864 (inst ,(if (eq return :tail) 'jmp 'call)
865 (make-ea :qword :base rax
867 '(- (* fdefn-raw-addr-slot
869 other-pointer-lowtag)
870 '(- (* closure-fun-slot n-word-bytes)
871 fun-pointer-lowtag))))
874 '((default-unknown-values vop values nvals)))
876 '((note-this-location vop :unknown-return)
877 (receive-unknown-values values-start nvals start count)))
879 (trace-table-entry trace-table-normal)))))
881 (define-full-call call nil :fixed nil)
882 (define-full-call call-named t :fixed nil)
883 (define-full-call multiple-call nil :unknown nil)
884 (define-full-call multiple-call-named t :unknown nil)
885 (define-full-call tail-call nil :tail nil)
886 (define-full-call tail-call-named t :tail nil)
888 (define-full-call call-variable nil :fixed t)
889 (define-full-call multiple-call-variable nil :unknown t))
891 ;;; This is defined separately, since it needs special code that BLT's
892 ;;; the arguments down. All the real work is done in the assembly
893 ;;; routine. We just set things up so that it can find what it needs.
894 (define-vop (tail-call-variable)
895 (:args (args :scs (any-reg control-stack) :target rsi)
896 (function :scs (descriptor-reg control-stack) :target rax)
899 (:temporary (:sc unsigned-reg :offset rsi-offset :from (:argument 0)) rsi)
900 (:temporary (:sc unsigned-reg :offset rax-offset :from (:argument 1)) rax)
901 (:temporary (:sc unsigned-reg) call-target)
902 ; (:ignore ret-addr old-fp)
904 ;; Move these into the passing locations if they are not already there.
908 ;; The following assumes that the return-pc and old-fp are on the
909 ;; stack in their standard save locations - Check this.
910 (unless (and (sc-is old-fp control-stack)
911 (= (tn-offset old-fp) ocfp-save-offset))
912 (error "tail-call-variable: ocfp not on stack in standard save location?"))
913 (unless (and (sc-is ret-addr sap-stack)
914 (= (tn-offset ret-addr) return-pc-save-offset))
915 (error "tail-call-variable: ret-addr not on stack in standard save location?"))
918 (inst lea call-target
920 :disp (make-fixup 'tail-call-variable :assembly-routine)))
921 ;; And jump to the assembly routine.
922 (inst jmp call-target)))
924 ;;;; unknown values return
926 ;;; Return a single-value using the Unknown-Values convention. Specifically,
927 ;;; we jump to clear the stack and jump to return-pc+3.
929 ;;; We require old-fp to be in a register, because we want to reset RSP before
930 ;;; restoring RBP. If old-fp were still on the stack, it could get clobbered
933 ;;; pfw--get wired-tn conflicts sometimes if register sc specd for args
934 ;;; having problems targeting args to regs -- using temps instead.
935 (define-vop (return-single)
941 (trace-table-entry trace-table-fun-epilogue)
942 ;; Code structure lifted from known-return.
945 ;; return PC in register for some reason (local call?)
946 ;; we jmp to the return pc after fixing the stack and frame.
949 ;; ofp on stack must be in slot 0 (the traditional storage place).
950 ;; Drop the stack above it and pop it off.
951 (cond ((zerop (tn-offset old-fp))
952 (inst lea rsp-tn (make-ea :dword :base rbp-tn
953 :disp (- (* (1+ ocfp-save-offset)
957 ;; Should this ever happen, we do the same as above, but
958 ;; using (tn-offset old-fp) instead of ocfp-save-offset
959 ;; (which is 0 anyway, see src/compiler/x86/vm.lisp) and
960 ;; then lea rsp again against itself with a displacement
961 ;; of (* (tn-offset old-fp) n-word-bytes) to clear the
962 ;; rest of the stack.
963 (cerror "Continue anyway"
964 "VOP return-single doesn't work if old-fp (in slot ~S) is not in slot 0" (tn-offset old-fp)))))
965 ((any-reg descriptor-reg)
966 ;; ofp in reg, drop the stack and load the real fp.
968 (move rbp-tn old-fp)))
970 ;; Set single-value-return flag
973 (inst jmp return-pc))
976 ;; Note that this will only work right if, when old-fp is on
977 ;; the stack, it has a lower tn-offset than return-pc. One of
978 ;; the comments in known-return indicate that this is the case
979 ;; (in that it will be in its save location), but we may wish
980 ;; to assert that (in either the weaker or stronger forms).
981 ;; Should this ever not be the case, we should load old-fp
982 ;; into a temp reg while we fix the stack.
983 ;; Drop stack above return-pc
984 (inst lea rsp-tn (make-ea :dword :base rbp-tn
985 :disp (- (* (1+ (tn-offset return-pc))
987 ;; Set single-value return flag
989 ;; Restore the old frame pointer
991 ;; And return, dropping the rest of the stack as we go.
992 (inst ret (* (tn-offset return-pc) n-word-bytes))))))
994 ;;; Do unknown-values return of a fixed (other than 1) number of
995 ;;; values. The VALUES are required to be set up in the standard
996 ;;; passing locations. NVALS is the number of values returned.
998 ;;; Basically, we just load RCX with the number of values returned and
999 ;;; RBX with a pointer to the values, set RSP to point to the end of
1000 ;;; the values, and jump directly to return-pc.
1001 (define-vop (return)
1003 (return-pc :to (:eval 1))
1008 ;; In the case of other than one value, we need these registers to
1009 ;; tell the caller where they are and how many there are.
1010 (:temporary (:sc unsigned-reg :offset rbx-offset) rbx)
1011 (:temporary (:sc unsigned-reg :offset rcx-offset) rcx)
1013 ;; We need to stretch the lifetime of return-pc past the argument
1014 ;; registers so that we can default the argument registers without
1015 ;; trashing return-pc.
1016 (:temporary (:sc unsigned-reg :offset (first *register-arg-offsets*)
1018 (:temporary (:sc unsigned-reg :offset (second *register-arg-offsets*)
1020 (:temporary (:sc unsigned-reg :offset (third *register-arg-offsets*)
1024 (trace-table-entry trace-table-fun-epilogue)
1025 ;; Establish the values pointer and values count.
1028 (inst xor rcx rcx) ; smaller
1029 (inst mov rcx (fixnumize nvals)))
1030 ;; Restore the frame pointer.
1031 (move rbp-tn old-fp)
1032 ;; Clear as much of the stack as possible, but not past the return
1034 (inst lea rsp-tn (make-ea :qword :base rbx
1035 :disp (- (* (max nvals 2) n-word-bytes))))
1036 ;; Pre-default any argument register that need it.
1037 (when (< nvals register-arg-count)
1038 (let* ((arg-tns (nthcdr nvals (list a0 a1 a2)))
1039 (first (first arg-tns)))
1040 (inst mov first nil-value)
1041 (dolist (tn (cdr arg-tns))
1042 (inst mov tn first))))
1043 ;; Set the multiple value return flag.
1045 ;; And away we go. Except that return-pc is still on the
1046 ;; stack and we've changed the stack pointer. So we have to
1047 ;; tell it to index off of RBX instead of RBP.
1048 (cond ((zerop nvals)
1049 ;; Return popping the return address and the OCFP.
1050 (inst ret n-word-bytes))
1052 ;; Return popping the return, leaving 1 slot. Can this
1053 ;; happen, or is a single value return handled elsewhere?
1056 (inst jmp (make-ea :qword :base rbx
1057 :disp (- (* (1+ (tn-offset return-pc))
1060 (trace-table-entry trace-table-normal)))
1062 ;;; Do unknown-values return of an arbitrary number of values (passed
1063 ;;; on the stack.) We check for the common case of a single return
1064 ;;; value, and do that inline using the normal single value return
1065 ;;; convention. Otherwise, we branch off to code that calls an
1066 ;;; assembly-routine.
1068 ;;; The assembly routine takes the following args:
1069 ;;; RAX -- the return-pc to finally jump to.
1070 ;;; RBX -- pointer to where to put the values.
1071 ;;; RCX -- number of values to find there.
1072 ;;; RSI -- pointer to where to find the values.
1073 (define-vop (return-multiple)
1074 (:args (old-fp :to (:eval 1) :target old-fp-temp)
1075 (return-pc :target rax)
1076 (vals :scs (any-reg) :target rsi)
1077 (nvals :scs (any-reg) :target rcx))
1079 (:temporary (:sc unsigned-reg :offset rax-offset :from (:argument 1)) rax)
1080 (:temporary (:sc unsigned-reg :offset rsi-offset :from (:argument 2)) rsi)
1081 (:temporary (:sc unsigned-reg :offset rcx-offset :from (:argument 3)) rcx)
1082 (:temporary (:sc unsigned-reg :offset rbx-offset :from (:eval 0)) rbx)
1083 (:temporary (:sc unsigned-reg) return-asm)
1084 (:temporary (:sc descriptor-reg :offset (first *register-arg-offsets*)
1085 :from (:eval 0)) a0)
1086 (:temporary (:sc unsigned-reg :from (:eval 1)) old-fp-temp)
1090 (trace-table-entry trace-table-fun-epilogue)
1091 ;; Load the return-pc.
1092 (move rax return-pc)
1093 (unless (policy node (> space speed))
1094 ;; Check for the single case.
1095 (let ((not-single (gen-label)))
1096 (inst cmp nvals (fixnumize 1))
1097 (inst jmp :ne not-single)
1099 ;; Return with one value.
1101 ;; Clear the stack. We load old-fp into a register before clearing
1103 (move old-fp-temp old-fp)
1104 (move rsp-tn rbp-tn)
1105 (move rbp-tn old-fp-temp)
1106 ;; clear the multiple-value return flag
1111 ;; Nope, not the single case. Jump to the assembly routine.
1112 (emit-label not-single)))
1116 (move rbp-tn old-fp)
1117 (inst lea return-asm
1118 (make-ea :qword :disp (make-fixup 'return-multiple
1119 :assembly-routine)))
1120 (inst jmp return-asm)
1121 (trace-table-entry trace-table-normal)))
1125 ;;; We don't need to do anything special for regular functions.
1126 (define-vop (setup-environment)
1130 ;; Don't bother doing anything.
1133 ;;; Get the lexical environment from its passing location.
1134 (define-vop (setup-closure-environment)
1135 (:results (closure :scs (descriptor-reg)))
1140 (move closure rax-tn)))
1142 ;;; Copy a &MORE arg from the argument area to the end of the current
1143 ;;; frame. FIXED is the number of non-&MORE arguments.
1145 ;;; The tricky part is doing this without trashing any of the calling
1146 ;;; convention registers that are still needed. This vop is emitted
1147 ;;; directly after the xep-allocate frame. That means the registers
1148 ;;; are in use as follows:
1150 ;;; RAX -- The lexenv.
1151 ;;; RBX -- Available.
1152 ;;; RCX -- The total number of arguments.
1153 ;;; RDX -- The first arg.
1154 ;;; RDI -- The second arg.
1155 ;;; RSI -- The third arg.
1157 ;;; So basically, we have one register available for our use: RBX.
1159 ;;; What we can do is push the other regs onto the stack, and then
1160 ;;; restore their values by looking directly below where we put the
1162 (define-vop (copy-more-arg)
1165 ;; Avoid the copy if there are no more args.
1166 (cond ((zerop fixed)
1167 (inst jecxz JUST-ALLOC-FRAME))
1169 (inst cmp rcx-tn (fixnumize fixed))
1170 (inst jmp :be JUST-ALLOC-FRAME)))
1172 ;; Allocate the space on the stack.
1173 ;; stack = rbp - (max 3 frame-size) - (nargs - fixed)
1175 (make-ea :qword :base rbp-tn
1176 :disp (- (fixnumize fixed)
1178 (max 3 (sb-allocated-size 'stack))))))
1179 (inst sub rbx-tn rcx-tn) ; Got the new stack in rbx
1180 (inst mov rsp-tn rbx-tn)
1182 ;; Now: nargs>=1 && nargs>fixed
1184 ;; Save the original count of args.
1185 (inst mov rbx-tn rcx-tn)
1187 (cond ((< fixed register-arg-count)
1188 ;; We must stop when we run out of stack args, not when we
1189 ;; run out of more args.
1190 ;; Number to copy = nargs-3
1191 (inst sub rcx-tn (fixnumize register-arg-count))
1192 ;; Everything of interest in registers.
1193 (inst jmp :be DO-REGS))
1195 ;; Number to copy = nargs-fixed
1196 (inst sub rcx-tn (fixnumize fixed))))
1198 ;; Save rdi and rsi register args.
1201 ;; Okay, we have pushed the register args. We can trash them
1204 ;; Initialize dst to be end of stack; skiping the values pushed
1206 (inst lea rdi-tn (make-ea :qword :base rsp-tn :disp 16))
1208 ;; Initialize src to be end of args.
1209 (inst mov rsi-tn rbp-tn)
1210 (inst sub rsi-tn rbx-tn)
1212 (inst shr rcx-tn word-shift) ; make word count
1213 ;; And copy the args.
1214 (inst cld) ; auto-inc RSI and RDI.
1218 ;; So now we need to restore RDI and RSI.
1225 (inst mov rcx-tn rbx-tn)
1227 ;; Here: nargs>=1 && nargs>fixed
1228 (when (< fixed register-arg-count)
1229 ;; Now we have to deposit any more args that showed up in
1233 ;; Store it relative to rbp
1234 (inst mov (make-ea :qword :base rbp-tn
1235 :disp (- (* n-word-bytes
1237 (max 3 (sb-allocated-size 'stack))))))
1238 (nth i *register-arg-tns*))
1241 (when (>= i register-arg-count)
1244 ;; Don't deposit any more than there are.
1246 (inst test rcx-tn rcx-tn)
1247 (inst cmp rcx-tn (fixnumize i)))
1248 (inst jmp :eq DONE)))
1254 (make-ea :qword :base rbp-tn
1255 :disp (- (* n-word-bytes
1256 (max 3 (sb-allocated-size 'stack))))))
1260 ;;; &MORE args are stored contiguously on the stack, starting
1261 ;;; immediately at the context pointer. The context pointer is not
1262 ;;; typed, so the lowtag is 0.
1263 (define-vop (more-arg)
1264 (:translate %more-arg)
1265 (:policy :fast-safe)
1266 (:args (object :scs (descriptor-reg) :to :result)
1267 (index :scs (any-reg) :target temp))
1268 (:arg-types * tagged-num)
1269 (:temporary (:sc unsigned-reg :from (:argument 1) :to :result) temp)
1270 (:results (value :scs (any-reg descriptor-reg)))
1275 (inst mov value (make-ea :qword :base object :index temp))))
1277 (define-vop (more-arg-c)
1278 (:translate %more-arg)
1279 (:policy :fast-safe)
1280 (:args (object :scs (descriptor-reg)))
1282 (:arg-types * (:constant (signed-byte 30)))
1283 (:results (value :scs (any-reg descriptor-reg)))
1287 (make-ea :qword :base object :disp (- (* index n-word-bytes))))))
1289 ;;; Turn more arg (context, count) into a list.
1290 (defoptimizer (%listify-rest-args stack-allocate-result) ((&rest args))
1293 (define-vop (listify-rest-args)
1294 (:translate %listify-rest-args)
1296 (:args (context :scs (descriptor-reg) :target src)
1297 (count :scs (any-reg) :target rcx))
1298 (:arg-types * tagged-num)
1299 (:temporary (:sc unsigned-reg :offset rsi-offset :from (:argument 0)) src)
1300 (:temporary (:sc unsigned-reg :offset rcx-offset :from (:argument 1)) rcx)
1301 (:temporary (:sc unsigned-reg :offset rax-offset) rax)
1302 (:temporary (:sc unsigned-reg) dst)
1303 (:results (result :scs (descriptor-reg)))
1306 (let ((enter (gen-label))
1309 (stack-allocate-p (node-stack-allocate-p node)))
1312 ;; Check to see whether there are no args, and just return NIL if so.
1313 (inst mov result nil-value)
1315 (inst lea dst (make-ea :qword :index rcx :scale 2))
1316 (maybe-pseudo-atomic stack-allocate-p
1317 (allocation dst dst node stack-allocate-p)
1318 (inst lea dst (make-ea :byte :base dst :disp list-pointer-lowtag))
1319 ;; Convert the count into a raw value, so that we can use the
1320 ;; LOOP instruction.
1321 (inst shr rcx (1- n-lowtag-bits))
1322 ;; Set decrement mode (successive args at lower addresses)
1324 ;; Set up the result.
1326 ;; Jump into the middle of the loop, 'cause that's where we want
1330 ;; Compute a pointer to the next cons.
1331 (inst add dst (* cons-size n-word-bytes))
1332 ;; Store a pointer to this cons in the CDR of the previous cons.
1333 (storew dst dst -1 list-pointer-lowtag)
1335 ;; Grab one value and stash it in the car of this cons.
1337 (storew rax dst 0 list-pointer-lowtag)
1338 ;; Go back for more.
1340 ;; NIL out the last cons.
1341 (storew nil-value dst 1 list-pointer-lowtag))
1342 (emit-label done))))
1344 ;;; Return the location and size of the &MORE arg glob created by
1345 ;;; COPY-MORE-ARG. SUPPLIED is the total number of arguments supplied
1346 ;;; (originally passed in RCX). FIXED is the number of non-rest
1349 ;;; We must duplicate some of the work done by COPY-MORE-ARG, since at
1350 ;;; that time the environment is in a pretty brain-damaged state,
1351 ;;; preventing this info from being returned as values. What we do is
1352 ;;; compute supplied - fixed, and return a pointer that many words
1353 ;;; below the current stack top.
1354 (define-vop (more-arg-context)
1355 (:policy :fast-safe)
1356 (:translate sb!c::%more-arg-context)
1357 (:args (supplied :scs (any-reg) :target count))
1358 (:arg-types positive-fixnum (:constant fixnum))
1360 (:results (context :scs (descriptor-reg))
1361 (count :scs (any-reg)))
1362 (:result-types t tagged-num)
1363 (:note "more-arg-context")
1365 (move count supplied)
1366 ;; SP at this point points at the last arg pushed.
1367 ;; Point to the first more-arg, not above it.
1368 (inst lea context (make-ea :qword :base rsp-tn
1369 :index count :scale 1
1370 :disp (- (+ (fixnumize fixed) n-word-bytes))))
1371 (unless (zerop fixed)
1372 (inst sub count (fixnumize fixed)))))
1374 ;;; Signal wrong argument count error if NARGS isn't equal to COUNT.
1375 (define-vop (verify-arg-count)
1376 (:policy :fast-safe)
1377 (:translate sb!c::%verify-arg-count)
1378 (:args (nargs :scs (any-reg)))
1379 (:arg-types positive-fixnum (:constant t))
1382 (:save-p :compute-only)
1385 (generate-error-code vop invalid-arg-count-error nargs)))
1387 (inst test nargs nargs) ; smaller instruction
1388 (inst cmp nargs (fixnumize count)))
1389 (inst jmp :ne err-lab))))
1391 ;;; Various other error signallers.
1392 (macrolet ((def (name error translate &rest args)
1393 `(define-vop (,name)
1395 `((:policy :fast-safe)
1396 (:translate ,translate)))
1397 (:args ,@(mapcar (lambda (arg)
1398 `(,arg :scs (any-reg descriptor-reg)))
1401 (:save-p :compute-only)
1403 (error-call vop ,error ,@args)))))
1404 (def arg-count-error invalid-arg-count-error
1405 sb!c::%arg-count-error nargs)
1406 (def type-check-error object-not-type-error sb!c::%type-check-error
1408 (def layout-invalid-error layout-invalid-error sb!c::%layout-invalid-error
1410 (def odd-key-args-error odd-key-args-error
1411 sb!c::%odd-key-args-error)
1412 (def unknown-key-arg-error unknown-key-arg-error
1413 sb!c::%unknown-key-arg-error key)
1414 (def nil-fun-returned-error nil-fun-returned-error nil fun))