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-argument-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 ;;; Similar to Make-Return-PC-Passing-Location, but makes a location
35 ;;; 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 ;;; wire to the stack?
52 (!def-vm-support-routine make-old-fp-save-location (env)
53 (physenv-debug-live-tn (make-wired-tn *fixnum-primitive-type*
54 control-stack-sc-number
58 (!def-vm-support-routine make-return-pc-save-location (env)
59 (physenv-debug-live-tn
60 (make-wired-tn (primitive-type-or-lose 'system-area-pointer)
61 sap-stack-sc-number return-pc-save-offset)
64 ;;; Make a TN for the standard argument count passing location. We only
65 ;;; need to make the standard location, since a count is never passed when we
66 ;;; are using non-standard conventions.
67 (!def-vm-support-routine make-argument-count-location ()
68 (make-wired-tn *fixnum-primitive-type* any-reg-sc-number ecx-offset))
70 ;;; Make a TN to hold the number-stack frame pointer. This is allocated
71 ;;; once per component, and is component-live.
72 (!def-vm-support-routine make-nfp-tn ()
73 (make-restricted-tn *fixnum-primitive-type* ignore-me-sc-number))
75 (!def-vm-support-routine make-stack-pointer-tn ()
76 (make-normal-tn *fixnum-primitive-type*))
78 (!def-vm-support-routine make-number-stack-pointer-tn ()
79 (make-restricted-tn *fixnum-primitive-type* ignore-me-sc-number))
81 ;;; Return a list of TNs that can be used to represent an unknown-values
82 ;;; continuation within a function.
83 (!def-vm-support-routine make-unknown-values-locations ()
84 (list (make-stack-pointer-tn)
85 (make-normal-tn *fixnum-primitive-type*)))
87 ;;; This function is called by the Entry-Analyze phase, allowing
88 ;;; VM-dependent initialization of the IR2-COMPONENT structure. We
89 ;;; push placeholder entries in the Constants to leave room for
90 ;;; additional noise in the code object header.
92 ;;; For the x86 the first constant is a pointer to a list of fixups,
93 ;;; or NIL if the code object has none.
94 (!def-vm-support-routine select-component-format (component)
95 (declare (type component component))
96 (dotimes (i (1+ code-constants-offset))
97 (vector-push-extend nil
98 (ir2-component-constants (component-info component))))
103 ;;; This is used for setting up the Old-FP in local call.
104 (define-vop (current-fp)
105 (:results (val :scs (any-reg control-stack)))
109 ;;; We don't have a separate NFP, so we don't need to do anything here.
110 (define-vop (compute-old-nfp)
116 (define-vop (xep-allocate-frame)
117 (:info start-lab copy-more-arg-follows)
120 (align n-lowtag-bits)
121 (trace-table-entry trace-table-function-prologue)
122 (emit-label start-lab)
123 ;; Skip space for the function header.
124 (inst simple-fun-header-word)
125 (dotimes (i (1- simple-fun-code-offset))
128 ;; The start of the actual code.
129 ;; Save the return-pc.
130 (popw ebp-tn (- (1+ return-pc-save-offset)))
132 ;; If copy-more-arg follows it will allocate the correct stack
133 ;; size. The stack is not allocated first here as this may expose
134 ;; args on the stack if they take up more space than the frame!
135 (unless copy-more-arg-follows
136 ;; The args fit within the frame so just allocate the frame.
138 (make-ea :dword :base ebp-tn
139 :disp (- (* n-word-bytes
140 (max 3 (sb-allocated-size 'stack)))))))
142 (trace-table-entry trace-table-normal)))
144 ;;; This is emitted directly before either a known-call-local, call-local,
145 ;;; or a multiple-call-local. All it does is allocate stack space for the
146 ;;; callee (who has the same size stack as us).
147 (define-vop (allocate-frame)
148 (:results (res :scs (any-reg control-stack))
154 (inst sub esp-tn (* n-word-bytes (sb-allocated-size 'stack)))))
156 ;;; Allocate a partial frame for passing stack arguments in a full
157 ;;; call. NARGS is the number of arguments passed. We allocate at
158 ;;; least 3 slots, because the XEP noise is going to want to use them
159 ;;; before it can extend the stack.
160 (define-vop (allocate-full-call-frame)
162 (:results (res :scs (any-reg control-stack)))
165 (inst sub esp-tn (* (max nargs 3) n-word-bytes))))
167 ;;; Emit code needed at the return-point from an unknown-values call
168 ;;; for a fixed number of values. Values is the head of the TN-Ref
169 ;;; list for the locations that the values are to be received into.
170 ;;; Nvals is the number of values that are to be received (should
171 ;;; equal the length of Values).
173 ;;; Move-Temp is a Descriptor-Reg TN used as a temporary.
175 ;;; This code exploits the fact that in the unknown-values convention,
176 ;;; a single value return returns at the return PC + 2, whereas a
177 ;;; return of other than one value returns directly at the return PC.
179 ;;; If 0 or 1 values are expected, then we just emit an instruction to
180 ;;; reset the SP (which will only be executed when other than 1 value
183 ;;; In the general case we have to do three things:
184 ;;; -- Default unsupplied register values. This need only be done
185 ;;; when a single value is returned, since register values are
186 ;;; defaulted by the called in the non-single case.
187 ;;; -- Default unsupplied stack values. This needs to be done whenever
188 ;;; there are stack values.
189 ;;; -- Reset SP. This must be done whenever other than 1 value is
190 ;;; returned, regardless of the number of values desired.
191 (defun default-unknown-values (vop values nvals)
192 (declare (type (or tn-ref null) values)
193 (type unsigned-byte nvals))
196 (note-this-location vop :single-value-return)
197 (inst mov esp-tn ebx-tn))
198 ((<= nvals register-arg-count)
199 (let ((regs-defaulted (gen-label)))
200 (note-this-location vop :unknown-return)
201 (inst jmp-short regs-defaulted)
202 ;; Default the unsuppled registers.
203 (let* ((2nd-tn-ref (tn-ref-across values))
204 (2nd-tn (tn-ref-tn 2nd-tn-ref)))
205 (inst mov 2nd-tn nil-value)
208 for tn-ref = (tn-ref-across 2nd-tn-ref)
209 then (tn-ref-across tn-ref)
210 for count from 2 below register-arg-count
211 do (inst mov (tn-ref-tn tn-ref) 2nd-tn))))
212 (inst mov ebx-tn esp-tn)
213 (emit-label regs-defaulted)
214 (inst mov esp-tn ebx-tn)))
216 ;; The number of bytes depends on the relative jump instructions.
217 ;; Best case is 31+(n-3)*14, worst case is 35+(n-3)*18. For
218 ;; NVALS=6 that is 73/89 bytes, and for NVALS=7 that is 87/107
219 ;; bytes which is likely better than using the blt below.
220 (let ((regs-defaulted (gen-label))
221 (defaulting-done (gen-label))
222 (default-stack-slots (gen-label)))
223 (note-this-location vop :unknown-return)
224 ;; Branch off to the MV case.
225 (inst jmp-short regs-defaulted)
226 ;; Do the single value case.
227 ;; Default the register args
228 (inst mov eax-tn nil-value)
230 (val (tn-ref-across values) (tn-ref-across val)))
231 ((= i (min nvals register-arg-count)))
232 (inst mov (tn-ref-tn val) eax-tn))
234 ;; Fake other registers so it looks like we returned with all the
235 ;; registers filled in.
238 (inst jmp default-stack-slots)
240 (emit-label regs-defaulted)
242 (inst mov eax-tn nil-value)
243 (storew edx-tn ebx-tn -1)
244 (collect ((defaults))
245 (do ((i register-arg-count (1+ i))
246 (val (do ((i 0 (1+ i))
247 (val values (tn-ref-across val)))
248 ((= i register-arg-count) val))
249 (tn-ref-across val)))
251 (let ((default-lab (gen-label))
252 (tn (tn-ref-tn val)))
253 (defaults (cons default-lab tn))
255 (inst cmp ecx-tn (fixnumize i))
256 (inst jmp :be default-lab)
257 (loadw edx-tn ebx-tn (- (1+ i)))
258 (inst mov tn edx-tn)))
260 (emit-label defaulting-done)
261 (loadw edx-tn ebx-tn -1)
264 (let ((defaults (defaults)))
266 (assemble (*elsewhere*)
267 (trace-table-entry trace-table-function-prologue)
268 (emit-label default-stack-slots)
269 (dolist (default defaults)
270 (emit-label (car default))
271 (inst mov (cdr default) eax-tn))
272 (inst jmp defaulting-done)
273 (trace-table-entry trace-table-normal)))))))
275 ;; 91 bytes for this branch.
276 (let ((regs-defaulted (gen-label))
277 (restore-edi (gen-label))
278 (no-stack-args (gen-label))
279 (default-stack-vals (gen-label))
280 (count-okay (gen-label)))
281 (note-this-location vop :unknown-return)
282 ;; Branch off to the MV case.
283 (inst jmp-short regs-defaulted)
285 ;; Default the register args, and set up the stack as if we
286 ;; entered the MV return point.
287 (inst mov ebx-tn esp-tn)
289 (inst mov edi-tn nil-value)
291 (inst mov esi-tn edi-tn)
292 ;; Compute a pointer to where to put the [defaulted] stack values.
293 (emit-label no-stack-args)
295 (make-ea :dword :base ebp-tn
296 :disp (* (- (1+ register-arg-count)) n-word-bytes)))
297 ;; Load EAX with NIL so we can quickly store it, and set up
298 ;; stuff for the loop.
299 (inst mov eax-tn nil-value)
301 (inst mov ecx-tn (- nvals register-arg-count))
302 ;; Jump into the default loop.
303 (inst jmp default-stack-vals)
305 ;; The regs are defaulted. We need to copy any stack arguments,
306 ;; and then default the remaining stack arguments.
307 (emit-label regs-defaulted)
309 (storew edi-tn ebx-tn (- (1+ 1)))
310 ;; Compute the number of stack arguments, and if it's zero or
311 ;; less, don't copy any stack arguments.
312 (inst sub ecx-tn (fixnumize register-arg-count))
313 (inst jmp :le no-stack-args)
315 ;; Throw away any unwanted args.
316 (inst cmp ecx-tn (fixnumize (- nvals register-arg-count)))
317 (inst jmp :be count-okay)
318 (inst mov ecx-tn (fixnumize (- nvals register-arg-count)))
319 (emit-label count-okay)
320 ;; Save the number of stack values.
321 (inst mov eax-tn ecx-tn)
322 ;; Compute a pointer to where the stack args go.
324 (make-ea :dword :base ebp-tn
325 :disp (* (- (1+ register-arg-count)) n-word-bytes)))
326 ;; Save ESI, and compute a pointer to where the args come from.
327 (storew esi-tn ebx-tn (- (1+ 2)))
329 (make-ea :dword :base ebx-tn
330 :disp (* (- (1+ register-arg-count)) n-word-bytes)))
332 (inst shr ecx-tn word-shift) ; make word count
337 (loadw esi-tn ebx-tn (- (1+ 2)))
338 ;; Now we have to default the remaining args. Find out how many.
339 (inst sub eax-tn (fixnumize (- nvals register-arg-count)))
341 ;; If none, then just blow out of here.
342 (inst jmp :le restore-edi)
343 (inst mov ecx-tn eax-tn)
344 (inst shr ecx-tn word-shift) ; word count
345 ;; Load EAX with NIL for fast storing.
346 (inst mov eax-tn nil-value)
348 (emit-label default-stack-vals)
351 ;; Restore EDI, and reset the stack.
352 (emit-label restore-edi)
353 (loadw edi-tn ebx-tn (- (1+ 1)))
354 (inst mov esp-tn ebx-tn))))
357 ;;;; unknown values receiving
359 ;;; Emit code needed at the return point for an unknown-values call
360 ;;; for an arbitrary number of values.
362 ;;; We do the single and non-single cases with no shared code: there
363 ;;; doesn't seem to be any potential overlap, and receiving a single
364 ;;; value is more important efficiency-wise.
366 ;;; When there is a single value, we just push it on the stack,
367 ;;; returning the old SP and 1.
369 ;;; When there is a variable number of values, we move all of the
370 ;;; argument registers onto the stack, and return ARGS and NARGS.
372 ;;; ARGS and NARGS are TNs wired to the named locations. We must
373 ;;; explicitly allocate these TNs, since their lifetimes overlap with
374 ;;; the results start and count. (Also, it's nice to be able to target
376 (defun receive-unknown-values (args nargs start count)
377 (declare (type tn args nargs start count))
378 (let ((variable-values (gen-label))
380 (inst jmp-short variable-values)
382 (inst mov start esp-tn)
383 (inst push (first *register-arg-tns*))
384 (inst mov count (fixnumize 1))
387 (emit-label variable-values)
388 ;; dtc: this writes the registers onto the stack even if they are
389 ;; not needed, only the number specified in ecx are used and have
390 ;; stack allocated to them. No harm is done.
392 for arg in *register-arg-tns*
394 do (storew arg args i))
401 ;;; VOP that can be inherited by unknown values receivers. The main thing this
402 ;;; handles is allocation of the result temporaries.
403 (define-vop (unknown-values-receiver)
404 (:temporary (:sc descriptor-reg :offset ebx-offset
405 :from :eval :to (:result 0))
407 (:temporary (:sc any-reg :offset ecx-offset
408 :from :eval :to (:result 1))
410 (:results (start :scs (any-reg control-stack))
411 (count :scs (any-reg control-stack))))
413 ;;;; local call with unknown values convention return
415 ;;; Non-TR local call for a fixed number of values passed according to
416 ;;; the unknown values convention.
418 ;;; FP is the frame pointer in install before doing the call.
420 ;;; NFP would be the number-stack frame pointer if we had a separate
423 ;;; Args are the argument passing locations, which are specified only
424 ;;; to terminate their lifetimes in the caller.
426 ;;; VALUES are the return value locations (wired to the standard
427 ;;; passing locations). NVALS is the number of values received.
429 ;;; Save is the save info, which we can ignore since saving has been
432 ;;; TARGET is a continuation pointing to the start of the called
434 (define-vop (call-local)
438 (:results (values :more t))
440 (:move-args :local-call)
441 (:info arg-locs callee target nvals)
443 (:ignore nfp arg-locs args #+nil callee)
445 (trace-table-entry trace-table-call-site)
448 (let ((ret-tn (callee-return-pc-tn callee)))
450 (format t "*call-local ~S; tn-kind ~S; tn-save-tn ~S; its tn-kind ~S~%"
451 ret-tn (sb!c::tn-kind ret-tn) (sb!c::tn-save-tn ret-tn)
452 (sb!c::tn-kind (sb!c::tn-save-tn ret-tn)))
454 ;; Is the return-pc on the stack or in a register?
457 #+nil (format t "*call-local: ret-tn on stack; offset=~S~%"
459 (storew (make-fixup nil :code-object return)
460 ebp-tn (- (1+ (tn-offset ret-tn)))))
462 (inst lea ret-tn (make-fixup nil :code-object return)))))
464 (note-this-location vop :call-site)
467 (default-unknown-values vop values nvals)
468 (trace-table-entry trace-table-normal)))
470 ;;; Non-TR local call for a variable number of return values passed according
471 ;;; to the unknown values convention. The results are the start of the values
472 ;;; glob and the number of values received.
473 (define-vop (multiple-call-local unknown-values-receiver)
478 (:move-args :local-call)
479 (:info save callee target)
480 (:ignore args save nfp #+nil callee)
483 (trace-table-entry trace-table-call-site)
486 (let ((ret-tn (callee-return-pc-tn callee)))
488 (format t "*multiple-call-local ~S; tn-kind ~S; tn-save-tn ~S; its tn-kind ~S~%"
489 ret-tn (sb!c::tn-kind ret-tn) (sb!c::tn-save-tn ret-tn)
490 (sb!c::tn-kind (sb!c::tn-save-tn ret-tn)))
492 ;; Is the return-pc on the stack or in a register?
495 #+nil (format t "*multiple-call-local: ret-tn on stack; offset=~S~%"
498 (storew (make-fixup nil :code-object return)
499 ebp-tn (- (1+ (tn-offset ret-tn)))))
502 (inst lea ret-tn (make-fixup nil :code-object return)))))
504 (note-this-location vop :call-site)
507 (note-this-location vop :unknown-return)
508 (receive-unknown-values values-start nvals start count)
509 (trace-table-entry trace-table-normal)))
511 ;;;; local call with known values return
513 ;;; Non-TR local call with known return locations. Known-value return
514 ;;; works just like argument passing in local call.
516 ;;; Note: we can't use normal load-tn allocation for the fixed args,
517 ;;; since all registers may be tied up by the more operand. Instead,
518 ;;; we use MAYBE-LOAD-STACK-TN.
519 (define-vop (known-call-local)
523 (:results (res :more t))
524 (:move-args :local-call)
526 (:info save callee target)
527 (:ignore args res save nfp #+nil callee)
530 (trace-table-entry trace-table-call-site)
533 (let ((ret-tn (callee-return-pc-tn callee)))
536 (format t "*known-call-local ~S; tn-kind ~S; tn-save-tn ~S; its tn-kind ~S~%"
537 ret-tn (sb!c::tn-kind ret-tn) (sb!c::tn-save-tn ret-tn)
538 (sb!c::tn-kind (sb!c::tn-save-tn ret-tn)))
540 ;; Is the return-pc on the stack or in a register?
543 #+nil (format t "*known-call-local: ret-tn on stack; offset=~S~%"
546 (storew (make-fixup nil :code-object return)
547 ebp-tn (- (1+ (tn-offset ret-tn)))))
550 (inst lea ret-tn (make-fixup nil :code-object return)))))
552 (note-this-location vop :call-site)
555 (note-this-location vop :known-return)
556 (trace-table-entry trace-table-normal)))
558 ;;; Return from known values call. We receive the return locations as
559 ;;; arguments to terminate their lifetimes in the returning function. We
560 ;;; restore FP and CSP and jump to the Return-PC.
562 ;;; We can assume we know exactly where old-fp and return-pc are because
563 ;;; make-old-fp-save-location and make-return-pc-save-location always
564 ;;; return the same place.
566 (define-vop (known-return)
568 (return-pc :scs (any-reg immediate-stack) :target rpc)
570 (:move-args :known-return)
572 (:temporary (:sc unsigned-reg :from (:argument 1)) rpc)
573 (:ignore val-locs vals)
576 (trace-table-entry trace-table-function-epilogue)
577 ;; Save the return-pc in a register 'cause the frame-pointer is
578 ;; going away. Note this not in the usual stack location so we
581 ;; Restore the stack.
583 ;; Restore the old fp. We know OLD-FP is going to be in its stack
584 ;; save slot, which is a different frame that than this one,
585 ;; so we don't have to worry about having just cleared
586 ;; most of the stack.
589 (trace-table-entry trace-table-normal)))
591 ;;; From Douglas Crosher
592 ;;; Return from known values call. We receive the return locations as
593 ;;; arguments to terminate their lifetimes in the returning function. We
594 ;;; restore FP and CSP and jump to the Return-PC.
596 ;;; The old-fp may be either in a register or on the stack in its
597 ;;; standard save locations - slot 0.
599 ;;; The return-pc may be in a register or on the stack in any slot.
600 (define-vop (known-return)
604 (:move-args :known-return)
606 (:ignore val-locs vals)
609 (trace-table-entry trace-table-function-epilogue)
611 #+nil (format t "*known-return: old-fp ~S, tn-kind ~S; ~S ~S~%"
612 old-fp (sb!c::tn-kind old-fp) (sb!c::tn-save-tn old-fp)
613 (sb!c::tn-kind (sb!c::tn-save-tn old-fp)))
615 #+nil (format t "*known-return: return-pc ~S, tn-kind ~S; ~S ~S~%"
616 return-pc (sb!c::tn-kind return-pc) (sb!c::tn-save-tn return-pc)
617 (sb!c::tn-kind (sb!c::tn-save-tn return-pc)))
619 ;; return-pc may be either in a register or on the stack.
625 #+nil (format t "*known-return: old-fp ~S on stack; offset=~S~%"
626 old-fp (tn-offset old-fp))
628 (cond ((zerop (tn-offset old-fp))
629 ;; Zot all of the stack except for the old-fp.
630 (inst lea esp-tn (make-ea :dword :base ebp-tn
631 :disp (- (* (1+ ocfp-save-offset)
633 ;; Restore the old fp from its save location on the stack,
634 ;; and zot the stack.
638 (cerror "Continue any-way"
639 "VOP return-local doesn't work if old-fp (in slot %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 ebp-tn old-fp)))
648 ;; Return; return-pc is in a register.
649 (inst jmp return-pc))
653 #+nil (format t "*known-return: return-pc ~S on stack; offset=~S~%"
654 return-pc (tn-offset return-pc))
656 ;; Zot all of the stack except for the old-fp and return-pc.
658 (make-ea :dword :base ebp-tn
659 :disp (- (* (1+ (tn-offset return-pc)) n-word-bytes))))
660 ;; Restore the old fp. old-fp may be either on the stack in its
661 ;; save location or in a register, in either case this restores it.
663 ;; The return pops the return address (4 bytes), then we need
664 ;; to pop all the slots before the return-pc which includes the
665 ;; 4 bytes for the old-fp.
666 (inst ret (* (tn-offset return-pc) n-word-bytes))))
668 (trace-table-entry trace-table-normal)))
672 ;;; There is something of a cross-product effect with full calls.
673 ;;; Different versions are used depending on whether we know the
674 ;;; number of arguments or the name of the called function, and
675 ;;; whether we want fixed values, unknown values, or a tail call.
677 ;;; In full call, the arguments are passed creating a partial frame on
678 ;;; the stack top and storing stack arguments into that frame. On
679 ;;; entry to the callee, this partial frame is pointed to by FP.
681 ;;; This macro helps in the definition of full call VOPs by avoiding
682 ;;; code replication in defining the cross-product VOPs.
684 ;;; NAME is the name of the VOP to define.
686 ;;; NAMED is true if the first argument is an fdefinition object whose
687 ;;; definition is to be called.
689 ;;; RETURN is either :FIXED, :UNKNOWN or :TAIL:
690 ;;; -- If :FIXED, then the call is for a fixed number of values, returned in
691 ;;; the standard passing locations (passed as result operands).
692 ;;; -- If :UNKNOWN, then the result values are pushed on the stack, and the
693 ;;; result values are specified by the Start and Count as in the
694 ;;; unknown-values continuation representation.
695 ;;; -- If :TAIL, then do a tail-recursive call. No values are returned.
696 ;;; The Old-Fp and Return-PC are passed as the second and third arguments.
698 ;;; In non-tail calls, the pointer to the stack arguments is passed as
699 ;;; the last fixed argument. If Variable is false, then the passing
700 ;;; locations are passed as a more arg. Variable is true if there are
701 ;;; a variable number of arguments passed on the stack. Variable
702 ;;; cannot be specified with :Tail return. TR variable argument call
703 ;;; is implemented separately.
705 ;;; In tail call with fixed arguments, the passing locations are
706 ;;; passed as a more arg, but there is no new-FP, since the arguments
707 ;;; have been set up in the current frame.
708 (macrolet ((define-full-call (name named return variable)
709 (aver (not (and variable (eq return :tail))))
711 ,@(when (eq return :unknown)
712 '(unknown-values-receiver)))
714 ,@(unless (eq return :tail)
715 '((new-fp :scs (any-reg) :to (:argument 1))))
717 (fun :scs (descriptor-reg control-stack)
718 :target eax :to (:argument 0))
720 ,@(when (eq return :tail)
724 ,@(unless variable '((args :more t :scs (descriptor-reg)))))
726 ,@(when (eq return :fixed)
727 '((:results (values :more t))))
729 (:save-p ,(if (eq return :tail) :compute-only t))
731 ,@(unless (or (eq return :tail) variable)
732 '((:move-args :full-call)))
736 ,@(unless (or variable (eq return :tail)) '(arg-locs))
737 ,@(unless variable '(nargs))
738 ,@(when (eq return :fixed) '(nvals)))
741 ,@(unless (or variable (eq return :tail)) '(arg-locs))
742 ,@(unless variable '(args)))
744 ;; We pass either the fdefn object (for named call) or
745 ;; the actual function object (for unnamed call) in
746 ;; EAX. With named call, closure-tramp will replace it
747 ;; with the real function and invoke the real function
748 ;; for closures. Non-closures do not need this value,
749 ;; so don't care what shows up in it.
757 ;; We pass the number of arguments in ECX.
758 (:temporary (:sc unsigned-reg :offset ecx-offset :to :eval) ecx)
760 ;; With variable call, we have to load the
761 ;; register-args out of the (new) stack frame before
762 ;; doing the call. Therefore, we have to tell the
763 ;; lifetime stuff that we need to use them.
765 (mapcar #'(lambda (name offset)
766 `(:temporary (:sc descriptor-reg
771 *register-arg-names* *register-arg-offsets*))
773 ,@(when (eq return :tail)
774 '((:temporary (:sc unsigned-reg
779 (:generator ,(+ (if named 5 0)
781 (if (eq return :tail) 0 10)
783 (if (eq return :unknown) 25 0))
784 (trace-table-entry trace-table-call-site)
786 ;; This has to be done before the frame pointer is
787 ;; changed! EAX stores the 'lexical environment' needed
793 ;; For variable call, compute the number of
794 ;; arguments and move some of the arguments to
797 ;; Compute the number of arguments.
798 (noise '(inst mov ecx new-fp))
799 (noise '(inst sub ecx esp-tn))
800 ;; Move the necessary args to registers,
801 ;; this moves them all even if they are
804 for name in *register-arg-names*
805 for index downfrom -1
806 do (noise `(loadw ,name new-fp ,index)))
810 (inst mov ecx (fixnumize nargs)))))
811 ,@(cond ((eq return :tail)
812 '(;; Python has figured out what frame we should
813 ;; return to so might as well use that clue.
814 ;; This seems really important to the
815 ;; implementation of things like
816 ;; (without-interrupts ...)
818 ;; dtc; Could be doing a tail call from a
819 ;; known-local-call etc in which the old-fp
820 ;; or ret-pc are in regs or in non-standard
821 ;; places. If the passing location were
822 ;; wired to the stack in standard locations
823 ;; then these moves will be un-necessary;
824 ;; this is probably best for the x86.
827 (unless (= ocfp-save-offset
829 ;; FIXME: FORMAT T for stale
830 ;; diagnostic output (several of
831 ;; them around here), ick
832 (format t "** tail-call old-fp not S0~%")
833 (move old-fp-tmp old-fp)
836 (- (1+ ocfp-save-offset)))))
837 ((any-reg descriptor-reg)
838 (format t "** tail-call old-fp in reg not S0~%")
841 (- (1+ ocfp-save-offset)))))
843 ;; For tail call, we have to push the
844 ;; return-pc so that it looks like we CALLed
845 ;; despite the fact that we are going to JMP.
846 (inst push return-pc)
849 ;; For non-tail call, we have to save our
850 ;; frame pointer and install the new frame
851 ;; pointer. We can't load stack tns after this
853 `(;; Python doesn't seem to allocate a frame
854 ;; here which doesn't leave room for the
857 ;; The variable args are on the stack and
858 ;; become the frame, but there may be <3
859 ;; args and 3 stack slots are assumed
860 ;; allocate on the call. So need to ensure
861 ;; there are at least 3 slots. This hack
864 '(inst sub esp-tn (fixnumize 3)))
867 (storew ebp-tn new-fp (- (1+ ocfp-save-offset)))
869 (move ebp-tn new-fp) ; NB - now on new stack frame.
872 (note-this-location vop :call-site)
874 (inst ,(if (eq return :tail) 'jmp 'call)
875 (make-ea :dword :base eax
877 '(- (* fdefn-raw-addr-slot
879 other-pointer-lowtag)
880 '(- (* closure-fun-slot n-word-bytes)
881 fun-pointer-lowtag))))
884 '((default-unknown-values vop values nvals)))
886 '((note-this-location vop :unknown-return)
887 (receive-unknown-values values-start nvals start count)))
889 (trace-table-entry trace-table-normal)))))
891 (define-full-call call nil :fixed nil)
892 (define-full-call call-named t :fixed nil)
893 (define-full-call multiple-call nil :unknown nil)
894 (define-full-call multiple-call-named t :unknown nil)
895 (define-full-call tail-call nil :tail nil)
896 (define-full-call tail-call-named t :tail nil)
898 (define-full-call call-variable nil :fixed t)
899 (define-full-call multiple-call-variable nil :unknown t))
901 ;;; This is defined separately, since it needs special code that BLT's
902 ;;; the arguments down. All the real work is done in the assembly
903 ;;; routine. We just set things up so that it can find what it needs.
904 (define-vop (tail-call-variable)
905 (:args (args :scs (any-reg control-stack) :target esi)
906 (function :scs (descriptor-reg control-stack) :target eax)
909 (:temporary (:sc unsigned-reg :offset esi-offset :from (:argument 0)) esi)
910 (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 1)) eax)
911 ; (:ignore ret-addr old-fp)
913 ;; Move these into the passing locations if they are not already there.
917 ;; The following assumes that the return-pc and old-fp are on the
918 ;; stack in their standard save locations - Check this.
919 (unless (and (sc-is old-fp control-stack)
920 (= (tn-offset old-fp) ocfp-save-offset))
921 (error "tail-call-variable: ocfp not on stack in standard save location?"))
922 (unless (and (sc-is ret-addr sap-stack)
923 (= (tn-offset ret-addr) return-pc-save-offset))
924 (error "tail-call-variable: ret-addr not on stack in standard save location?"))
927 ;; And jump to the assembly routine.
928 (inst jmp (make-fixup 'tail-call-variable :assembly-routine))))
930 ;;;; unknown values return
932 ;;; Return a single-value using the Unknown-Values convention. Specifically,
933 ;;; we jump to clear the stack and jump to return-pc+2.
935 ;;; We require old-fp to be in a register, because we want to reset ESP before
936 ;;; restoring EBP. If old-fp were still on the stack, it could get clobbered
939 ;;; pfw--get wired-tn conflicts sometimes if register sc specd for args
940 ;;; having problems targeting args to regs -- using temps instead.
941 (define-vop (return-single)
945 (:temporary (:sc unsigned-reg) ofp)
946 (:temporary (:sc unsigned-reg) ret)
949 (trace-table-entry trace-table-function-epilogue)
951 ;; Clear the control stack
953 ;; Adjust the return address for the single value return.
955 ;; Restore the frame pointer.
961 ;;; Do unknown-values return of a fixed (other than 1) number of
962 ;;; values. The VALUES are required to be set up in the standard
963 ;;; passing locations. NVALS is the number of values returned.
965 ;;; Basically, we just load ECX with the number of values returned and
966 ;;; EBX with a pointer to the values, set ESP to point to the end of
967 ;;; the values, and jump directly to return-pc.
970 (return-pc :to (:eval 1))
975 ;; In the case of other than one value, we need these registers to
976 ;; tell the caller where they are and how many there are.
977 (:temporary (:sc unsigned-reg :offset ebx-offset) ebx)
978 (:temporary (:sc unsigned-reg :offset ecx-offset) ecx)
980 ;; We need to stretch the lifetime of return-pc past the argument
981 ;; registers so that we can default the argument registers without
982 ;; trashing return-pc.
983 (:temporary (:sc unsigned-reg :offset (first *register-arg-offsets*)
985 (:temporary (:sc unsigned-reg :offset (second *register-arg-offsets*)
987 (:temporary (:sc unsigned-reg :offset (third *register-arg-offsets*)
991 (trace-table-entry trace-table-function-epilogue)
992 ;; Establish the values pointer and values count.
995 (inst xor ecx ecx) ; smaller
996 (inst mov ecx (fixnumize nvals)))
997 ;; Restore the frame pointer.
999 ;; Clear as much of the stack as possible, but not past the return
1001 (inst lea esp-tn (make-ea :dword :base ebx
1002 :disp (- (* (max nvals 2) n-word-bytes))))
1003 ;; Pre-default any argument register that need it.
1004 (when (< nvals register-arg-count)
1005 (let* ((arg-tns (nthcdr nvals (list a0 a1 a2)))
1006 (first (first arg-tns)))
1007 (inst mov first nil-value)
1008 (dolist (tn (cdr arg-tns))
1009 (inst mov tn first))))
1010 ;; And away we go. Except that return-pc is still on the
1011 ;; stack and we've changed the stack pointer. So we have to
1012 ;; tell it to index off of EBX instead of EBP.
1013 (cond ((zerop nvals)
1014 ;; Return popping the return address and the OCFP.
1015 (inst ret n-word-bytes))
1017 ;; Return popping the return, leaving 1 slot. Can this
1018 ;; happen, or is a single value return handled elsewhere?
1021 (inst jmp (make-ea :dword :base ebx
1022 :disp (- (* (1+ (tn-offset return-pc))
1025 (trace-table-entry trace-table-normal)))
1027 ;;; Do unknown-values return of an arbitrary number of values (passed
1028 ;;; on the stack.) We check for the common case of a single return
1029 ;;; value, and do that inline using the normal single value return
1030 ;;; convention. Otherwise, we branch off to code that calls an
1031 ;;; assembly-routine.
1033 ;;; The assembly routine takes the following args:
1034 ;;; EAX -- the return-pc to finally jump to.
1035 ;;; EBX -- pointer to where to put the values.
1036 ;;; ECX -- number of values to find there.
1037 ;;; ESI -- pointer to where to find the values.
1038 (define-vop (return-multiple)
1039 (:args (old-fp :to (:eval 1) :target old-fp-temp)
1040 (return-pc :target eax)
1041 (vals :scs (any-reg) :target esi)
1042 (nvals :scs (any-reg) :target ecx))
1044 (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 1)) eax)
1045 (:temporary (:sc unsigned-reg :offset esi-offset :from (:argument 2)) esi)
1046 (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 3)) ecx)
1047 (:temporary (:sc unsigned-reg :offset ebx-offset :from (:eval 0)) ebx)
1048 (:temporary (:sc descriptor-reg :offset (first *register-arg-offsets*)
1049 :from (:eval 0)) a0)
1050 (:temporary (:sc unsigned-reg :from (:eval 1)) old-fp-temp)
1054 (trace-table-entry trace-table-function-epilogue)
1055 ;; Load the return-pc.
1056 (move eax return-pc)
1057 (unless (policy node (> space speed))
1058 ;; Check for the single case.
1059 (let ((not-single (gen-label)))
1060 (inst cmp nvals (fixnumize 1))
1061 (inst jmp :ne not-single)
1063 ;; Return with one value.
1065 ;; Clear the stack. We load old-fp into a register before clearing
1067 (move old-fp-temp old-fp)
1068 (move esp-tn ebp-tn)
1069 (move ebp-tn old-fp-temp)
1070 ;; Fix the return-pc to point at the single-value entry point.
1075 ;; Nope, not the single case. Jump to the assembly routine.
1076 (emit-label not-single)))
1080 (move ebp-tn old-fp)
1081 (inst jmp (make-fixup 'return-multiple :assembly-routine))
1082 (trace-table-entry trace-table-normal)))
1086 ;;; We don't need to do anything special for regular functions.
1087 (define-vop (setup-environment)
1091 ;; Don't bother doing anything.
1094 ;;; Get the lexical environment from its passing location.
1095 (define-vop (setup-closure-environment)
1096 (:results (closure :scs (descriptor-reg)))
1101 (move closure eax-tn)))
1103 ;;; Copy a &MORE arg from the argument area to the end of the current
1104 ;;; frame. FIXED is the number of non-&MORE arguments.
1106 ;;; The tricky part is doing this without trashing any of the calling
1107 ;;; convention registers that are still needed. This vop is emitted
1108 ;;; directly after the xep-allocate frame. That means the registers
1109 ;;; are in use as follows:
1111 ;;; EAX -- The lexenv.
1112 ;;; EBX -- Available.
1113 ;;; ECX -- The total number of arguments.
1114 ;;; EDX -- The first arg.
1115 ;;; EDI -- The second arg.
1116 ;;; ESI -- The third arg.
1118 ;;; So basically, we have one register available for our use: EBX.
1120 ;;; What we can do is push the other regs onto the stack, and then
1121 ;;; restore their values by looking directly below where we put the
1123 (define-vop (copy-more-arg)
1126 ;; Avoid the copy if there are no more args.
1127 (cond ((zerop fixed)
1128 (inst jecxz just-alloc-frame))
1130 (inst cmp ecx-tn (fixnumize fixed))
1131 (inst jmp :be just-alloc-frame)))
1133 ;; Allocate the space on the stack.
1134 ;; stack = ebp - (max 3 frame-size) - (nargs - fixed)
1136 (make-ea :dword :base ebp-tn
1137 :disp (- (fixnumize fixed)
1139 (max 3 (sb-allocated-size 'stack))))))
1140 (inst sub ebx-tn ecx-tn) ; Got the new stack in ebx
1141 (inst mov esp-tn ebx-tn)
1143 ;; Now: nargs>=1 && nargs>fixed
1145 ;; Save the original count of args.
1146 (inst mov ebx-tn ecx-tn)
1148 (cond ((< fixed register-arg-count)
1149 ;; We must stop when we run out of stack args, not when we
1150 ;; run out of more args.
1151 ;; Number to copy = nargs-3
1152 (inst sub ecx-tn (fixnumize register-arg-count))
1153 ;; Everything of interest in registers.
1154 (inst jmp :be do-regs))
1156 ;; Number to copy = nargs-fixed
1157 (inst sub ecx-tn (fixnumize fixed))))
1159 ;; Save edi and esi register args.
1162 ;; Okay, we have pushed the register args. We can trash them
1165 ;; Initialize dst to be end of stack; skiping the values pushed
1167 (inst lea edi-tn (make-ea :dword :base esp-tn :disp 8))
1169 ;; Initialize src to be end of args.
1170 (inst mov esi-tn ebp-tn)
1171 (inst sub esi-tn ebx-tn)
1173 (inst shr ecx-tn word-shift) ; make word count
1174 ;; And copy the args.
1175 (inst cld) ; auto-inc ESI and EDI.
1179 ;; So now we need to restore EDI and ESI.
1186 (inst mov ecx-tn ebx-tn)
1188 ;; Here: nargs>=1 && nargs>fixed
1189 (when (< fixed register-arg-count)
1190 ;; Now we have to deposit any more args that showed up in
1194 ;; Store it relative to ebp
1195 (inst mov (make-ea :dword :base ebp-tn
1198 (max 3 (sb-allocated-size 'stack))))))
1199 (nth i *register-arg-tns*))
1202 (when (>= i register-arg-count)
1205 ;; Don't deposit any more than there are.
1207 (inst test ecx-tn ecx-tn)
1208 (inst cmp ecx-tn (fixnumize i)))
1209 (inst jmp :eq done)))
1215 (make-ea :dword :base ebp-tn
1216 :disp (- (* n-word-bytes
1217 (max 3 (sb-allocated-size 'stack))))))
1221 ;;; &MORE args are stored contiguously on the stack, starting
1222 ;;; immediately at the context pointer. The context pointer is not
1223 ;;; typed, so the lowtag is 0.
1224 (define-vop (more-arg)
1225 (:translate %more-arg)
1226 (:policy :fast-safe)
1227 (:args (object :scs (descriptor-reg) :to :result)
1228 (index :scs (any-reg) :target temp))
1229 (:arg-types * tagged-num)
1230 (:temporary (:sc unsigned-reg :from (:argument 1) :to :result) temp)
1231 (:results (value :scs (any-reg descriptor-reg)))
1236 (inst mov value (make-ea :dword :base object :index temp))))
1238 (define-vop (more-arg-c)
1239 (:translate %more-arg)
1240 (:policy :fast-safe)
1241 (:args (object :scs (descriptor-reg)))
1243 (:arg-types * (:constant (signed-byte 30)))
1244 (:results (value :scs (any-reg descriptor-reg)))
1248 (make-ea :dword :base object :disp (- (* index n-word-bytes))))))
1251 ;;; Turn more arg (context, count) into a list.
1252 (define-vop (listify-rest-args)
1253 (:translate %listify-rest-args)
1255 (:args (context :scs (descriptor-reg) :target src)
1256 (count :scs (any-reg) :target ecx))
1257 (:arg-types * tagged-num)
1258 (:temporary (:sc unsigned-reg :offset esi-offset :from (:argument 0)) src)
1259 (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 1)) ecx)
1260 (:temporary (:sc unsigned-reg :offset eax-offset) eax)
1261 (:temporary (:sc unsigned-reg) dst)
1262 (:results (result :scs (descriptor-reg)))
1265 (let ((enter (gen-label))
1270 ;; Check to see whether there are no args, and just return NIL if so.
1271 (inst mov result nil-value)
1273 (inst lea dst (make-ea :dword :index ecx :scale 2))
1275 (allocation dst dst node)
1276 (inst lea dst (make-ea :byte :base dst :disp list-pointer-lowtag))
1277 ;; Convert the count into a raw value, so that we can use the
1278 ;; LOOP instruction.
1280 ;; Set decrement mode (successive args at lower addresses)
1282 ;; Set up the result.
1284 ;; Jump into the middle of the loop, 'cause that's were we want
1288 ;; Compute a pointer to the next cons.
1289 (inst add dst (* cons-size n-word-bytes))
1290 ;; Store a pointer to this cons in the CDR of the previous cons.
1291 (storew dst dst -1 list-pointer-lowtag)
1293 ;; Grab one value and stash it in the car of this cons.
1295 (storew eax dst 0 list-pointer-lowtag)
1296 ;; Go back for more.
1298 ;; NIL out the last cons.
1299 (storew nil-value dst 1 list-pointer-lowtag))
1300 (emit-label done))))
1302 ;;; Return the location and size of the &MORE arg glob created by
1303 ;;; COPY-MORE-ARG. SUPPLIED is the total number of arguments supplied
1304 ;;; (originally passed in ECX). FIXED is the number of non-rest
1307 ;;; We must duplicate some of the work done by COPY-MORE-ARG, since at
1308 ;;; that time the environment is in a pretty brain-damaged state,
1309 ;;; preventing this info from being returned as values. What we do is
1310 ;;; compute supplied - fixed, and return a pointer that many words
1311 ;;; below the current stack top.
1312 (define-vop (more-arg-context)
1313 (:policy :fast-safe)
1314 (:translate sb!c::%more-arg-context)
1315 (:args (supplied :scs (any-reg) :target count))
1316 (:arg-types positive-fixnum (:constant fixnum))
1318 (:results (context :scs (descriptor-reg))
1319 (count :scs (any-reg)))
1320 (:result-types t tagged-num)
1321 (:note "more-arg-context")
1323 (move count supplied)
1324 ;; SP at this point points at the last arg pushed.
1325 ;; Point to the first more-arg, not above it.
1326 (inst lea context (make-ea :dword :base esp-tn
1327 :index count :scale 1
1328 :disp (- (+ (fixnumize fixed) 4))))
1329 (unless (zerop fixed)
1330 (inst sub count (fixnumize fixed)))))
1332 ;;; Signal wrong argument count error if NARGS isn't equal to COUNT.
1333 (define-vop (verify-argument-count)
1334 (:policy :fast-safe)
1335 (:translate sb!c::%verify-argument-count)
1336 (:args (nargs :scs (any-reg)))
1337 (:arg-types positive-fixnum (:constant t))
1340 (:save-p :compute-only)
1343 (generate-error-code vop invalid-argument-count-error nargs)))
1345 (inst test nargs nargs) ; smaller instruction
1346 (inst cmp nargs (fixnumize count)))
1347 (inst jmp :ne err-lab))))
1349 ;;; Various other error signallers.
1350 (macrolet ((frob (name error translate &rest args)
1351 `(define-vop (,name)
1353 `((:policy :fast-safe)
1354 (:translate ,translate)))
1355 (:args ,@(mapcar #'(lambda (arg)
1356 `(,arg :scs (any-reg descriptor-reg)))
1359 (:save-p :compute-only)
1361 (error-call vop ,error ,@args)))))
1362 (frob argument-count-error invalid-argument-count-error
1363 sb!c::%argument-count-error nargs)
1364 (frob type-check-error object-not-type-error sb!c::%type-check-error
1366 (frob layout-invalid-error layout-invalid-error sb!c::%layout-invalid-error
1368 (frob odd-key-arguments-error odd-key-arguments-error
1369 sb!c::%odd-key-arguments-error)
1370 (frob unknown-key-argument-error unknown-key-argument-error
1371 sb!c::%unknown-key-argument-error key)
1372 (frob nil-function-returned-error nil-function-returned-error nil fun))