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
32 (!def-vm-support-routine make-return-pc-passing-location (standard)
33 (declare (ignore standard))
34 (make-wired-tn (primitive-type-or-lose 'system-area-pointer)
35 sap-stack-sc-number return-pc-save-offset))
36 ;;; If STANDARD is true, then use the standard (full call) location,
37 ;;; otherwise use any legal location.
41 (!def-vm-support-routine make-return-pc-passing-location (standard)
42 (let ((ptype (primitive-type-or-lose 'system-area-pointer)))
44 (make-wired-tn ptype sap-stack-sc-number return-pc-save-offset)
45 (make-normal-tn ptype))))
47 ;;; Similar to Make-Return-PC-Passing-Location, but makes a location to pass
50 ;;; This is wired in both the standard and the local-call
51 ;;; conventions, because we want to be able to assume it's always there.
52 ;;; Besides, the x86 doesn't have enough registers to really make it
53 ;;; profitable to pass it in a register.
57 (!def-vm-support-routine make-old-fp-passing-location (standard)
58 (declare (ignore standard))
59 (make-wired-tn *fixnum-primitive-type* control-stack-sc-number
61 ;;; If standard is true, then use the standard (full call) location,
62 ;;; otherwise use any legal location.
66 (!def-vm-support-routine make-old-fp-passing-location (standard)
68 (make-wired-tn *fixnum-primitive-type* control-stack-sc-number
70 (make-normal-tn *fixnum-primitive-type*)))
72 ;;; Make the TNs used to hold Old-FP and Return-PC within the current
73 ;;; function. We treat these specially so that the debugger can find them at a
76 ;;; Without using a save-tn - which does not make much sense if it is
77 ;;; wire to the stack? No problems.
78 (!def-vm-support-routine make-old-fp-save-location (env)
79 (environment-debug-live-tn (make-wired-tn *fixnum-primitive-type*
80 control-stack-sc-number
83 ;;; Using a save-tn. No problems.
85 (!def-vm-support-routine make-old-fp-save-location (env)
87 (environment-debug-live-tn (make-normal-tn *fixnum-primitive-type*) env)
88 (make-wired-tn *fixnum-primitive-type* control-stack-sc-number
91 ;;; Without using a save-tn - which does not make much sense if it is
92 ;;; wire to the stack? No problems.
93 (!def-vm-support-routine make-return-pc-save-location (env)
94 (environment-debug-live-tn
95 (make-wired-tn (primitive-type-or-lose 'system-area-pointer)
96 sap-stack-sc-number return-pc-save-offset)
98 ;;; Using a save-tn. No problems.
100 (!def-vm-support-routine make-return-pc-save-location (env)
101 (let ((ptype (primitive-type-or-lose 'system-area-pointer)))
103 (environment-debug-live-tn (make-normal-tn ptype) env)
104 (make-wired-tn ptype sap-stack-sc-number return-pc-save-offset))))
106 ;;; Make a TN for the standard argument count passing location. We only
107 ;;; need to make the standard location, since a count is never passed when we
108 ;;; are using non-standard conventions.
109 (!def-vm-support-routine make-argument-count-location ()
110 (make-wired-tn *fixnum-primitive-type* any-reg-sc-number ecx-offset))
113 ;;; Make a TN to hold the number-stack frame pointer. This is allocated
114 ;;; once per component, and is component-live.
115 (!def-vm-support-routine make-nfp-tn ()
116 (make-restricted-tn *fixnum-primitive-type* ignore-me-sc-number))
118 (!def-vm-support-routine make-stack-pointer-tn ()
119 (make-normal-tn *fixnum-primitive-type*))
121 (!def-vm-support-routine make-number-stack-pointer-tn ()
122 (make-restricted-tn *fixnum-primitive-type* ignore-me-sc-number))
124 ;;; Return a list of TNs that can be used to represent an unknown-values
125 ;;; continuation within a function.
126 (!def-vm-support-routine make-unknown-values-locations ()
127 (list (make-stack-pointer-tn)
128 (make-normal-tn *fixnum-primitive-type*)))
131 ;;; This function is called by the Entry-Analyze phase, allowing
132 ;;; VM-dependent initialization of the IR2-Component structure. We push
133 ;;; placeholder entries in the Constants to leave room for additional
134 ;;; noise in the code object header.
136 ;;; For the x86 the first constant is a pointer to a list of fixups,
137 ;;; or nil if the code object has none.
138 (!def-vm-support-routine select-component-format (component)
139 (declare (type component component))
140 (dotimes (i (1+ code-constants-offset))
141 (vector-push-extend nil
142 (ir2-component-constants (component-info component))))
147 ;;; Used for setting up the Old-FP in local call.
148 (define-vop (current-fp)
149 (:results (val :scs (any-reg control-stack)))
153 ;;; We don't have a separate NFP, so we don't need to do anything here.
154 (define-vop (compute-old-nfp)
161 (define-vop (xep-allocate-frame)
162 (:info start-lab copy-more-arg-follows)
165 ;; Make sure the function is aligned, and drop a label pointing to this
168 (trace-table-entry trace-table-function-prologue)
169 (emit-label start-lab)
170 ;; Skip space for the function header.
171 (inst function-header-word)
172 (dotimes (i (1- sb!vm:function-code-offset))
175 ;; The start of the actual code.
176 ;; Save the return-pc.
177 (popw ebp-tn (- (1+ return-pc-save-offset)))
179 ;; If copy-more-arg follows it will allocate the correct stack
180 ;; size. The stack is not allocated first here as this may expose
181 ;; args on the stack if they take up more space than the frame!
182 (unless copy-more-arg-follows
183 ;; The args fit within the frame so just allocate the frame.
185 (make-ea :dword :base ebp-tn
186 :disp (- (* sb!vm:word-bytes
187 (max 3 (sb-allocated-size 'stack)))))))
189 (trace-table-entry trace-table-normal)))
191 ;;; This is emitted directly before either a known-call-local, call-local,
192 ;;; or a multiple-call-local. All it does is allocate stack space for the
193 ;;; callee (who has the same size stack as us).
194 (define-vop (allocate-frame)
195 (:results (res :scs (any-reg control-stack))
201 (inst sub esp-tn (* sb!vm:word-bytes (sb-allocated-size 'stack)))))
203 ;;; Allocate a partial frame for passing stack arguments in a full call. Nargs
204 ;;; is the number of arguments passed. We allocate at least 3 slots, because
205 ;;; the XEP noise is going to want to use them before it can extend the stack.
206 (define-vop (allocate-full-call-frame)
208 (:results (res :scs (any-reg control-stack)))
211 (inst sub esp-tn (* (max nargs 3) sb!vm:word-bytes))))
215 ;;; Emit code needed at the return-point from an unknown-values call for a
216 ;;; fixed number of values. Values is the head of the TN-Ref list for the
217 ;;; locations that the values are to be received into. Nvals is the number of
218 ;;; values that are to be received (should equal the length of Values).
220 ;;; Move-Temp is a Descriptor-Reg TN used as a temporary.
222 ;;; This code exploits the fact that in the unknown-values convention, a
223 ;;; single value return returns at the return PC + 2, whereas a return of other
224 ;;; than one value returns directly at the return PC.
226 ;;; If 0 or 1 values are expected, then we just emit an instruction to reset
227 ;;; the SP (which will only be executed when other than 1 value is returned.)
229 ;;; In the general case we have to do three things:
230 ;;; -- Default unsupplied register values. This need only be done when a
231 ;;; single value is returned, since register values are defaulted by the
232 ;;; called in the non-single case.
233 ;;; -- Default unsupplied stack values. This needs to be done whenever there
234 ;;; are stack values.
235 ;;; -- Reset SP. This must be done whenever other than 1 value is returned,
236 ;;; regardless of the number of values desired.
237 (defun default-unknown-values (vop values nvals)
238 (declare (type (or tn-ref null) values)
239 (type unsigned-byte nvals))
242 (note-this-location vop :single-value-return)
243 (inst mov esp-tn ebx-tn))
244 ((<= nvals register-arg-count)
245 (let ((regs-defaulted (gen-label)))
246 (note-this-location vop :unknown-return)
247 (inst jmp-short regs-defaulted)
248 ;; Default the unsuppled registers.
249 (let* ((2nd-tn-ref (tn-ref-across values))
250 (2nd-tn (tn-ref-tn 2nd-tn-ref)))
251 (inst mov 2nd-tn nil-value)
254 for tn-ref = (tn-ref-across 2nd-tn-ref)
255 then (tn-ref-across tn-ref)
256 for count from 2 below register-arg-count
257 do (inst mov (tn-ref-tn tn-ref) 2nd-tn))))
258 (inst mov ebx-tn esp-tn)
259 (emit-label regs-defaulted)
260 (inst mov esp-tn ebx-tn)))
262 ;; Number of bytes depends on the relative jump instructions. Best
263 ;; case is 31+(n-3)*14, worst case is 35+(n-3)*18. For nvals=6
264 ;; that is 73/89 bytes, and for nvals=7 that is 87/107 bytes which
265 ;; is likely better than using the blt below.
266 (let ((regs-defaulted (gen-label))
267 (defaulting-done (gen-label))
268 (default-stack-slots (gen-label)))
269 (note-this-location vop :unknown-return)
270 ;; Branch off to the MV case.
271 (inst jmp-short regs-defaulted)
272 ;; Do the single value case.
273 ;; Default the register args
274 (inst mov eax-tn nil-value)
276 (val (tn-ref-across values) (tn-ref-across val)))
277 ((= i (min nvals register-arg-count)))
278 (inst mov (tn-ref-tn val) eax-tn))
280 ;; Fake other registers so it looks like we returned with all the
281 ;; registers filled in.
284 (inst jmp default-stack-slots)
286 (emit-label regs-defaulted)
288 (inst mov eax-tn nil-value)
289 (storew edx-tn ebx-tn -1)
290 (collect ((defaults))
291 (do ((i register-arg-count (1+ i))
292 (val (do ((i 0 (1+ i))
293 (val values (tn-ref-across val)))
294 ((= i register-arg-count) val))
295 (tn-ref-across val)))
297 (let ((default-lab (gen-label))
298 (tn (tn-ref-tn val)))
299 (defaults (cons default-lab tn))
301 (inst cmp ecx-tn (fixnumize i))
302 (inst jmp :be default-lab)
303 (loadw edx-tn ebx-tn (- (1+ i)))
304 (inst mov tn edx-tn)))
306 (emit-label defaulting-done)
307 (loadw edx-tn ebx-tn -1)
310 (let ((defaults (defaults)))
312 (assemble (*elsewhere*)
313 (trace-table-entry trace-table-function-prologue)
314 (emit-label default-stack-slots)
315 (dolist (default defaults)
316 (emit-label (car default))
317 (inst mov (cdr default) eax-tn))
318 (inst jmp defaulting-done)
319 (trace-table-entry trace-table-normal)))))))
321 ;; 91 bytes for this branch.
322 (let ((regs-defaulted (gen-label))
323 (restore-edi (gen-label))
324 (no-stack-args (gen-label))
325 (default-stack-vals (gen-label))
326 (count-okay (gen-label)))
327 (note-this-location vop :unknown-return)
328 ;; Branch off to the MV case.
329 (inst jmp-short regs-defaulted)
331 ;; Default the register args, and set up the stack as if we entered
332 ;; the MV return point.
333 (inst mov ebx-tn esp-tn)
335 (inst mov edi-tn nil-value)
337 (inst mov esi-tn edi-tn)
338 ;; Compute a pointer to where to put the [defaulted] stack values.
339 (emit-label no-stack-args)
341 (make-ea :dword :base ebp-tn
342 :disp (* (- (1+ register-arg-count)) word-bytes)))
343 ;; Load EAX with NIL so we can quickly store it, and set up stuff
345 (inst mov eax-tn nil-value)
347 (inst mov ecx-tn (- nvals register-arg-count))
348 ;; Jump into the default loop.
349 (inst jmp default-stack-vals)
351 ;; The regs are defaulted. We need to copy any stack arguments,
352 ;; and then default the remaining stack arguments.
353 (emit-label regs-defaulted)
355 (storew edi-tn ebx-tn (- (1+ 1)))
356 ;; Compute the number of stack arguments, and if it's zero or less,
357 ;; don't copy any stack arguments.
358 (inst sub ecx-tn (fixnumize register-arg-count))
359 (inst jmp :le no-stack-args)
361 ;; Throw away any unwanted args.
362 (inst cmp ecx-tn (fixnumize (- nvals register-arg-count)))
363 (inst jmp :be count-okay)
364 (inst mov ecx-tn (fixnumize (- nvals register-arg-count)))
365 (emit-label count-okay)
366 ;; Save the number of stack values.
367 (inst mov eax-tn ecx-tn)
368 ;; Compute a pointer to where the stack args go.
370 (make-ea :dword :base ebp-tn
371 :disp (* (- (1+ register-arg-count)) word-bytes)))
372 ;; Save ESI, and compute a pointer to where the args come from.
373 (storew esi-tn ebx-tn (- (1+ 2)))
375 (make-ea :dword :base ebx-tn
376 :disp (* (- (1+ register-arg-count)) word-bytes)))
378 (inst shr ecx-tn word-shift) ; make word count
383 (loadw esi-tn ebx-tn (- (1+ 2)))
384 ;; Now we have to default the remaining args. Find out how many.
385 (inst sub eax-tn (fixnumize (- nvals register-arg-count)))
387 ;; If none, then just blow out of here.
388 (inst jmp :le restore-edi)
389 (inst mov ecx-tn eax-tn)
390 (inst shr ecx-tn word-shift) ; word count
391 ;; Load EAX with NIL for fast storing.
392 (inst mov eax-tn nil-value)
394 (emit-label default-stack-vals)
397 ;; Restore EDI, and reset the stack.
398 (emit-label restore-edi)
399 (loadw edi-tn ebx-tn (- (1+ 1)))
400 (inst mov esp-tn ebx-tn))))
403 ;;;; unknown values receiving
405 ;;; Emit code needed at the return point for an unknown-values call for an
406 ;;; arbitrary number of values.
408 ;;; We do the single and non-single cases with no shared code: there doesn't
409 ;;; seem to be any potential overlap, and receiving a single value is more
410 ;;; important efficiency-wise.
412 ;;; When there is a single value, we just push it on the stack, returning
413 ;;; the old SP and 1.
415 ;;; When there is a variable number of values, we move all of the argument
416 ;;; registers onto the stack, and return Args and Nargs.
418 ;;; Args and Nargs are TNs wired to the named locations. We must
419 ;;; explicitly allocate these TNs, since their lifetimes overlap with the
420 ;;; results Start and Count (also, it's nice to be able to target them).
421 (defun receive-unknown-values (args nargs start count)
422 (declare (type tn args nargs start count))
423 (let ((variable-values (gen-label))
425 (inst jmp-short variable-values)
427 (inst mov start esp-tn)
428 (inst push (first *register-arg-tns*))
429 (inst mov count (fixnumize 1))
432 (emit-label variable-values)
433 ;; dtc: this writes the registers onto the stack even if they are
434 ;; not needed, only the number specified in ecx are used and have
435 ;; stack allocated to them. No harm is done.
437 for arg in *register-arg-tns*
439 do (storew arg args i))
446 ;;; VOP that can be inherited by unknown values receivers. The main thing this
447 ;;; handles is allocation of the result temporaries.
448 (define-vop (unknown-values-receiver)
449 (:temporary (:sc descriptor-reg :offset ebx-offset
450 :from :eval :to (:result 0))
452 (:temporary (:sc any-reg :offset ecx-offset
453 :from :eval :to (:result 1))
455 (:results (start :scs (any-reg control-stack))
456 (count :scs (any-reg control-stack))))
458 ;;;; local call with unknown values convention return
460 ;;; Non-TR local call for a fixed number of values passed according to the
461 ;;; unknown values convention.
463 ;;; FP is the frame pointer in install before doing the call.
465 ;;; NFP would be the number-stack frame pointer if we had a separate number
468 ;;; Args are the argument passing locations, which are specified only to
469 ;;; terminate their lifetimes in the caller.
471 ;;; Values are the return value locations (wired to the standard passing
473 ;;; Nvals is the number of values received.
475 ;;; Save is the save info, which we can ignore since saving has been done.
477 ;;; Target is a continuation pointing to the start of the called function.
478 (define-vop (call-local)
482 (:results (values :more t))
484 (:move-args :local-call)
485 (:info arg-locs callee target nvals)
487 (:ignore nfp arg-locs args #+nil callee)
489 (trace-table-entry trace-table-call-site)
492 (let ((ret-tn (callee-return-pc-tn callee)))
494 (format t "*call-local ~S; tn-kind ~S; tn-save-tn ~S; its tn-kind ~S~%"
495 ret-tn (sb!c::tn-kind ret-tn) (sb!c::tn-save-tn ret-tn)
496 (sb!c::tn-kind (sb!c::tn-save-tn ret-tn)))
498 ;; Is the return-pc on the stack or in a register?
501 #+nil (format t "*call-local: ret-tn on stack; offset=~S~%"
503 (storew (make-fixup nil :code-object return)
504 ebp-tn (- (1+ (tn-offset ret-tn)))))
506 (inst lea ret-tn (make-fixup nil :code-object return)))))
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)
522 (:move-args :local-call)
523 (:info save callee target)
524 (:ignore args save nfp #+nil callee)
527 (trace-table-entry trace-table-call-site)
530 (let ((ret-tn (callee-return-pc-tn callee)))
532 (format t "*multiple-call-local ~S; tn-kind ~S; tn-save-tn ~S; its tn-kind ~S~%"
533 ret-tn (sb!c::tn-kind ret-tn) (sb!c::tn-save-tn ret-tn)
534 (sb!c::tn-kind (sb!c::tn-save-tn ret-tn)))
536 ;; Is the return-pc on the stack or in a register?
539 #+nil (format t "*multiple-call-local: ret-tn on stack; offset=~S~%"
542 (storew (make-fixup nil :code-object return)
543 ebp-tn (- (1+ (tn-offset ret-tn)))))
546 (inst lea ret-tn (make-fixup nil :code-object return)))))
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 works
558 ;;; just like argument passing in local call.
560 ;;; Note: we can't use normal load-tn allocation for the fixed args, since all
561 ;;; registers may be tied up by the more operand. Instead, we use
562 ;;; MAYBE-LOAD-STACK-TN.
563 (define-vop (known-call-local)
567 (:results (res :more t))
568 (:move-args :local-call)
570 (:info save callee target)
571 (:ignore args res save nfp #+nil callee)
574 (trace-table-entry trace-table-call-site)
577 (let ((ret-tn (callee-return-pc-tn callee)))
580 (format t "*known-call-local ~S; tn-kind ~S; tn-save-tn ~S; its tn-kind ~S~%"
581 ret-tn (sb!c::tn-kind ret-tn) (sb!c::tn-save-tn ret-tn)
582 (sb!c::tn-kind (sb!c::tn-save-tn ret-tn)))
584 ;; Is the return-pc on the stack or in a register?
587 #+nil (format t "*known-call-local: ret-tn on stack; offset=~S~%"
590 (storew (make-fixup nil :code-object return)
591 ebp-tn (- (1+ (tn-offset ret-tn)))))
594 (inst lea ret-tn (make-fixup nil :code-object return)))))
596 (note-this-location vop :call-site)
599 (note-this-location vop :known-return)
600 (trace-table-entry trace-table-normal)))
602 ;;; Return from known values call. We receive the return locations as
603 ;;; arguments to terminate their lifetimes in the returning function. We
604 ;;; restore FP and CSP and jump to the Return-PC.
606 ;;; We can assume we know exactly where old-fp and return-pc are because
607 ;;; make-old-fp-save-location and make-return-pc-save-location always
608 ;;; return the same place.
610 (define-vop (known-return)
612 (return-pc :scs (any-reg immediate-stack) :target rpc)
614 (:move-args :known-return)
616 (:temporary (:sc unsigned-reg :from (:argument 1)) rpc)
617 (:ignore val-locs vals)
620 (trace-table-entry trace-table-function-epilogue)
621 ;; Save the return-pc in a register 'cause the frame-pointer is going away.
622 ;; Note this not in the usual stack location so we can't use RET
624 ;; Restore the stack.
626 ;; Restore the old fp. We know OLD-FP is going to be in its stack
627 ;; save slot, which is a different frame that than this one,
628 ;; so we don't have to worry about having just cleared
629 ;; most of the stack.
632 (trace-table-entry trace-table-normal)))
634 ;;; From Douglas Crosher
635 ;;; Return from known values call. We receive the return locations as
636 ;;; arguments to terminate their lifetimes in the returning function. We
637 ;;; restore FP and CSP and jump to the Return-PC.
639 ;;; The old-fp may be either in a register or on the stack in its
640 ;;; standard save locations - slot 0.
642 ;;; The return-pc may be in a register or on the stack in any slot.
643 (define-vop (known-return)
647 (:move-args :known-return)
649 (:ignore val-locs vals)
652 (trace-table-entry trace-table-function-epilogue)
654 #+nil (format t "*known-return: old-fp ~S, tn-kind ~S; ~S ~S~%"
655 old-fp (sb!c::tn-kind old-fp) (sb!c::tn-save-tn old-fp)
656 (sb!c::tn-kind (sb!c::tn-save-tn old-fp)))
658 #+nil (format t "*known-return: return-pc ~S, tn-kind ~S; ~S ~S~%"
659 return-pc (sb!c::tn-kind return-pc) (sb!c::tn-save-tn return-pc)
660 (sb!c::tn-kind (sb!c::tn-save-tn return-pc)))
662 ;; return-pc may be either in a register or on the stack.
668 #+nil (format t "*known-return: old-fp ~S on stack; offset=~S~%"
669 old-fp (tn-offset old-fp))
671 (cond ((zerop (tn-offset old-fp))
672 ;; Zot all of the stack except for the old-fp.
673 (inst lea esp-tn (make-ea :dword :base ebp-tn
674 :disp (- (* (1+ ocfp-save-offset)
676 ;; Restore the old fp from its save location on the stack,
677 ;; and zot the stack.
681 (cerror "Continue any-way"
682 "VOP return-local doesn't work if old-fp (in slot %s) is not in slot 0"
683 (tn-offset old-fp)))))
685 ((any-reg descriptor-reg)
686 ;; Zot all the stack.
688 ;; Restore the old-fp.
689 (move ebp-tn old-fp)))
691 ;; Return; return-pc is in a register.
692 (inst jmp return-pc))
696 #+nil (format t "*known-return: return-pc ~S on stack; offset=~S~%"
697 return-pc (tn-offset return-pc))
699 ;; Zot all of the stack except for the old-fp and return-pc.
701 (make-ea :dword :base ebp-tn
702 :disp (- (* (1+ (tn-offset return-pc)) word-bytes))))
703 ;; Restore the old fp. old-fp may be either on the stack in its
704 ;; save location or in a register, in either case this restores it.
706 ;; The return pops the return address (4 bytes), then we need
707 ;; to pop all the slots before the return-pc which includes the
708 ;; 4 bytes for the old-fp.
709 (inst ret (* (tn-offset return-pc) word-bytes))))
711 (trace-table-entry trace-table-normal)))
715 ;;; There is something of a cross-product effect with full calls. Different
716 ;;; versions are used depending on whether we know the number of arguments or
717 ;;; the name of the called function, and whether we want fixed values, unknown
718 ;;; values, or a tail call.
720 ;;; In full call, the arguments are passed creating a partial frame on the
721 ;;; stack top and storing stack arguments into that frame. On entry to the
722 ;;; callee, this partial frame is pointed to by FP.
724 ;;; This macro helps in the definition of full call VOPs by avoiding code
725 ;;; replication in defining the cross-product VOPs.
727 ;;; Name is the name of the VOP to define.
729 ;;; Named is true if the first argument is an fdefinition object whose
730 ;;; definition is to be called.
732 ;;; Return is either :Fixed, :Unknown or :Tail:
733 ;;; -- If :Fixed, then the call is for a fixed number of values, returned in
734 ;;; the standard passing locations (passed as result operands).
735 ;;; -- If :Unknown, then the result values are pushed on the stack, and the
736 ;;; result values are specified by the Start and Count as in the
737 ;;; unknown-values continuation representation.
738 ;;; -- If :Tail, then do a tail-recursive call. No values are returned.
739 ;;; The Old-Fp and Return-PC are passed as the second and third arguments.
741 ;;; In non-tail calls, the pointer to the stack arguments is passed as the last
742 ;;; fixed argument. If Variable is false, then the passing locations are
743 ;;; passed as a more arg. Variable is true if there are a variable number of
744 ;;; arguments passed on the stack. Variable cannot be specified with :Tail
745 ;;; return. TR variable argument call is implemented separately.
747 ;;; In tail call with fixed arguments, the passing locations are passed as a
748 ;;; more arg, but there is no new-FP, since the arguments have been set up in
749 ;;; the current frame.
750 (macrolet ((define-full-call (name named return variable)
751 (aver (not (and variable (eq return :tail))))
753 ,@(when (eq return :unknown)
754 '(unknown-values-receiver)))
756 ,@(unless (eq return :tail)
757 '((new-fp :scs (any-reg) :to (:argument 1))))
759 (fun :scs (descriptor-reg control-stack)
760 :target eax :to (:argument 0))
762 ,@(when (eq return :tail)
766 ,@(unless variable '((args :more t :scs (descriptor-reg)))))
768 ,@(when (eq return :fixed)
769 '((:results (values :more t))))
771 (:save-p ,(if (eq return :tail) :compute-only t))
773 ,@(unless (or (eq return :tail) variable)
774 '((:move-args :full-call)))
778 ,@(unless (or variable (eq return :tail)) '(arg-locs))
779 ,@(unless variable '(nargs))
780 ,@(when (eq return :fixed) '(nvals)))
783 ,@(unless (or variable (eq return :tail)) '(arg-locs))
784 ,@(unless variable '(args)))
786 ;; We pass either the fdefn object (for named call) or the actual
787 ;; function object (for unnamed call) in EAX. With named call,
788 ;; closure-tramp will replace it with the real function and invoke
789 ;; the real function for closures. Non-closures do not need this
790 ;; value, so don't care what shows up in it.
792 (:sc descriptor-reg :offset eax-offset :from (:argument 0) :to :eval)
795 ;; We pass the number of arguments in ECX.
796 (:temporary (:sc unsigned-reg :offset ecx-offset :to :eval) ecx)
798 ;; With variable call, we have to load the register-args out
799 ;; of the (new) stack frame before doing the call. Therefore,
800 ;; we have to tell the lifetime stuff that we need to use them.
802 (mapcar #'(lambda (name offset)
803 `(:temporary (:sc descriptor-reg
808 *register-arg-names* *register-arg-offsets*))
810 ,@(when (eq return :tail)
811 '((:temporary (:sc unsigned-reg
816 (:generator ,(+ (if named 5 0)
818 (if (eq return :tail) 0 10)
820 (if (eq return :unknown) 25 0))
821 (trace-table-entry trace-table-call-site)
823 ;; This has to be done before the frame pointer is changed!
824 ;; eax stores the 'lexical environment' needed for closures
829 ;; For variable call, compute the number of
830 ;; arguments and move some of the arguments to
833 ;; Compute the number of arguments.
834 (noise '(inst mov ecx new-fp))
835 (noise '(inst sub ecx esp-tn))
836 ;; Move the necessary args to registers,
837 ;; this moves them all even if they are
840 for name in *register-arg-names*
841 for index downfrom -1
842 do (noise `(loadw ,name new-fp ,index)))
846 (inst mov ecx (fixnumize nargs)))))
847 ,@(cond ((eq return :tail)
848 '(;; Python has figured out what frame we should
849 ;; return to so might as well use that clue.
850 ;; This seems really important to the
851 ;; implementation of things like
852 ;; (without-interrupts ...)
854 ;; dtc; Could be doing a tail call from a
855 ;; known-local-call etc in which the old-fp
856 ;; or ret-pc are in regs or in non-standard
857 ;; places. If the passing location were
858 ;; wired to the stack in standard locations
859 ;; then these moves will be un-necessary;
860 ;; this is probably best for the x86.
863 (unless (= ocfp-save-offset
865 ;; FIXME: FORMAT T for stale
866 ;; diagnostic output (several of
867 ;; them around here), ick
868 (format t "** tail-call old-fp not S0~%")
869 (move old-fp-tmp old-fp)
872 (- (1+ ocfp-save-offset)))))
873 ((any-reg descriptor-reg)
874 (format t "** tail-call old-fp in reg not S0~%")
877 (- (1+ ocfp-save-offset)))))
879 ;; For tail call, we have to push the
880 ;; return-pc so that it looks like we CALLed
881 ;; despite the fact that we are going to JMP.
882 (inst push return-pc)
885 ;; For non-tail call, we have to save our
886 ;; frame pointer and install the new frame
887 ;; pointer. We can't load stack tns after this
889 `(;; Python doesn't seem to allocate a frame
890 ;; here which doesn't leave room for the
893 ;; The variable args are on the stack and
894 ;; become the frame, but there may be <3
895 ;; args and 3 stack slots are assumed
896 ;; allocate on the call. So need to ensure
897 ;; there are at least 3 slots. This hack
900 '(inst sub esp-tn (fixnumize 3)))
903 (storew ebp-tn new-fp (- (1+ ocfp-save-offset)))
905 (move ebp-tn new-fp) ; NB - now on new stack frame.
908 (note-this-location vop :call-site)
910 (inst ,(if (eq return :tail) 'jmp 'call)
911 (make-ea :dword :base eax
913 '(- (* fdefn-raw-addr-slot word-bytes)
915 '(- (* closure-function-slot word-bytes)
916 function-pointer-type))))
919 '((default-unknown-values vop values nvals)))
921 '((note-this-location vop :unknown-return)
922 (receive-unknown-values values-start nvals start count)))
924 (trace-table-entry trace-table-normal)))))
926 (define-full-call call nil :fixed nil)
927 (define-full-call call-named t :fixed nil)
928 (define-full-call multiple-call nil :unknown nil)
929 (define-full-call multiple-call-named t :unknown nil)
930 (define-full-call tail-call nil :tail nil)
931 (define-full-call tail-call-named t :tail nil)
933 (define-full-call call-variable nil :fixed t)
934 (define-full-call multiple-call-variable nil :unknown t))
936 ;;; This is defined separately, since it needs special code that BLT's the
937 ;;; arguments down. All the real work is done in the assembly routine. We just
938 ;;; set things up so that it can find what it needs.
939 (define-vop (tail-call-variable)
940 (:args (args :scs (any-reg control-stack) :target esi)
941 (function :scs (descriptor-reg control-stack) :target eax)
944 (:temporary (:sc unsigned-reg :offset esi-offset :from (:argument 0)) esi)
945 (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 1)) eax)
946 ; (:ignore ret-addr old-fp)
948 ;; Move these into the passing locations if they are not already there.
952 ;; The following assumes that the return-pc and old-fp are on the
953 ;; stack in their standard save locations - Check this.
954 (unless (and (sc-is old-fp control-stack)
955 (= (tn-offset old-fp) ocfp-save-offset))
956 (error "tail-call-variable: ocfp not on stack in standard save location?"))
957 (unless (and (sc-is ret-addr sap-stack)
958 (= (tn-offset ret-addr) return-pc-save-offset))
959 (error "tail-call-variable: ret-addr not on stack in standard save location?"))
962 ;; And jump to the assembly routine.
963 (inst jmp (make-fixup 'tail-call-variable :assembly-routine))))
965 ;;;; unknown values return
967 ;;; Return a single-value using the Unknown-Values convention. Specifically,
968 ;;; we jump to clear the stack and jump to return-pc+2.
970 ;;; We require old-fp to be in a register, because we want to reset ESP before
971 ;;; restoring EBP. If old-fp were still on the stack, it could get clobbered
974 ;;; pfw--get wired-tn conflicts sometimes if register sc specd for args
975 ;;; having problems targeting args to regs -- using temps instead.
976 (define-vop (return-single)
980 (:temporary (:sc unsigned-reg) ofp)
981 (:temporary (:sc unsigned-reg) ret)
984 (trace-table-entry trace-table-function-epilogue)
986 ;; Clear the control stack
988 ;; Adjust the return address for the single value return.
990 ;; Restore the frame pointer.
996 ;;; Do unknown-values return of a fixed (other than 1) number of values. The
997 ;;; Values are required to be set up in the standard passing locations. Nvals
998 ;;; is the number of values returned.
1000 ;;; Basically, we just load ECX with the number of values returned and EBX
1001 ;;; with a pointer to the values, set ESP to point to the end of the values,
1002 ;;; and jump directly to return-pc.
1003 (define-vop (return)
1005 (return-pc :to (:eval 1))
1010 ;; In the case of other than one value, we need these registers to tell
1011 ;; the caller where they are and how many there are.
1012 (:temporary (:sc unsigned-reg :offset ebx-offset) ebx)
1013 (:temporary (:sc unsigned-reg :offset ecx-offset) ecx)
1015 ;; We need to stretch the lifetime of return-pc past the argument
1016 ;; registers so that we can default the argument registers without
1017 ;; trashing return-pc.
1018 (:temporary (:sc unsigned-reg :offset (first *register-arg-offsets*)
1020 (:temporary (:sc unsigned-reg :offset (second *register-arg-offsets*)
1022 (:temporary (:sc unsigned-reg :offset (third *register-arg-offsets*)
1026 (trace-table-entry trace-table-function-epilogue)
1027 ;; Establish the values pointer and values count.
1030 (inst xor ecx ecx) ; smaller
1031 (inst mov ecx (fixnumize nvals)))
1032 ;; restore the frame pointer.
1033 (move ebp-tn old-fp)
1034 ;; clear as much of the stack as possible, but not past the return
1036 (inst lea esp-tn (make-ea :dword :base ebx
1037 :disp (- (* (max nvals 2) word-bytes))))
1038 ;; pre-default any argument register that need it.
1039 (when (< nvals register-arg-count)
1040 (let* ((arg-tns (nthcdr nvals (list a0 a1 a2)))
1041 (first (first arg-tns)))
1042 (inst mov first nil-value)
1043 (dolist (tn (cdr arg-tns))
1044 (inst mov tn first))))
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 EBX instead of EBP.
1048 (cond ((zerop nvals)
1049 ;; Return popping the return address and the OCFP.
1050 (inst ret 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 :dword :base ebx
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 on the
1063 ;;; stack.) We check for the common case of a single return value, and do that
1064 ;;; inline using the normal single value return convention. Otherwise, we
1065 ;;; branch off to code that calls an assembly-routine.
1067 ;;; The assembly routine takes the following args:
1068 ;;; EAX -- the return-pc to finally jump to.
1069 ;;; EBX -- pointer to where to put the values.
1070 ;;; ECX -- number of values to find there.
1071 ;;; ESI -- pointer to where to find the values.
1072 (define-vop (return-multiple)
1073 (:args (old-fp :to (:eval 1) :target old-fp-temp)
1074 (return-pc :target eax)
1075 (vals :scs (any-reg) :target esi)
1076 (nvals :scs (any-reg) :target ecx))
1078 (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 1)) eax)
1079 (:temporary (:sc unsigned-reg :offset esi-offset :from (:argument 2)) esi)
1080 (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 3)) ecx)
1081 (:temporary (:sc unsigned-reg :offset ebx-offset :from (:eval 0)) ebx)
1082 (:temporary (:sc descriptor-reg :offset (first *register-arg-offsets*)
1083 :from (:eval 0)) a0)
1084 (:temporary (:sc unsigned-reg :from (:eval 1)) old-fp-temp)
1088 (trace-table-entry trace-table-function-epilogue)
1089 ;; Load the return-pc.
1090 (move eax return-pc)
1091 (unless (policy node (> space speed))
1092 ;; Check for the single case.
1093 (let ((not-single (gen-label)))
1094 (inst cmp nvals (fixnumize 1))
1095 (inst jmp :ne not-single)
1097 ;; Return with one value.
1099 ;; Clear the stack. We load old-fp into a register before clearing
1101 (move old-fp-temp old-fp)
1102 (move esp-tn ebp-tn)
1103 (move ebp-tn old-fp-temp)
1104 ;; Fix the return-pc to point at the single-value entry point.
1109 ;; Nope, not the single case. Jump to the assembly routine.
1110 (emit-label not-single)))
1114 (move ebp-tn old-fp)
1115 (inst jmp (make-fixup 'return-multiple :assembly-routine))
1116 (trace-table-entry trace-table-normal)))
1120 ;;; We don't need to do anything special for regular functions.
1121 (define-vop (setup-environment)
1125 ;; Don't bother doing anything.
1128 ;;; Get the lexical environment from its passing location.
1129 (define-vop (setup-closure-environment)
1130 (:results (closure :scs (descriptor-reg)))
1135 (move closure eax-tn)))
1137 ;;; Copy a more arg from the argument area to the end of the current frame.
1138 ;;; Fixed is the number of non-more arguments.
1140 ;;; The tricky part is doing this without trashing any of the calling
1141 ;;; convention registers that are still needed. This vop is emitted directly
1142 ;;; after the xep-allocate frame. That means the registers are in use as
1145 ;;; EAX -- The lexenv.
1146 ;;; EBX -- Available.
1147 ;;; ECX -- The total number of arguments.
1148 ;;; EDX -- The first arg.
1149 ;;; EDI -- The second arg.
1150 ;;; ESI -- The third arg.
1152 ;;; So basically, we have one register available for our use: EBX.
1154 ;;; What we can do is push the other regs onto the stack, and then restore
1155 ;;; their values by looking directly below where we put the more-args.
1156 (define-vop (copy-more-arg)
1159 ;; Avoid the copy if there are no more args.
1160 (cond ((zerop fixed)
1161 (inst jecxz just-alloc-frame))
1163 (inst cmp ecx-tn (fixnumize fixed))
1164 (inst jmp :be just-alloc-frame)))
1166 ;; Allocate the space on the stack.
1167 ;; stack = ebp - (max 3 frame-size) - (nargs - fixed)
1169 (make-ea :dword :base ebp-tn
1170 :disp (- (fixnumize fixed)
1172 (max 3 (sb-allocated-size 'stack))))))
1173 (inst sub ebx-tn ecx-tn) ; Got the new stack in ebx
1174 (inst mov esp-tn ebx-tn)
1176 ;; Now: nargs>=1 && nargs>fixed
1178 ;; Save the original count of args.
1179 (inst mov ebx-tn ecx-tn)
1181 (cond ((< fixed register-arg-count)
1182 ;; We must stop when we run out of stack args, not when we
1183 ;; run out of more args.
1184 ;; Number to copy = nargs-3
1185 (inst sub ecx-tn (fixnumize register-arg-count))
1186 ;; Everything of interest in registers.
1187 (inst jmp :be do-regs))
1189 ;; Number to copy = nargs-fixed
1190 (inst sub ecx-tn (fixnumize fixed))))
1192 ;; Save edi and esi register args.
1195 ;; Okay, we have pushed the register args. We can trash them
1198 ;; Initialize dst to be end of stack; skiping the values pushed
1200 (inst lea edi-tn (make-ea :dword :base esp-tn :disp 8))
1202 ;; Initialize src to be end of args.
1203 (inst mov esi-tn ebp-tn)
1204 (inst sub esi-tn ebx-tn)
1206 (inst shr ecx-tn word-shift) ; make word count
1207 ;; And copy the args.
1208 (inst cld) ; auto-inc ESI and EDI.
1212 ;; So now we need to restore EDI and ESI.
1219 (inst mov ecx-tn ebx-tn)
1221 ;; Here: nargs>=1 && nargs>fixed
1222 (when (< fixed register-arg-count)
1223 ;; Now we have to deposit any more args that showed up in
1227 ;; Store it relative to ebp
1228 (inst mov (make-ea :dword :base ebp-tn
1231 (max 3 (sb-allocated-size 'stack))))))
1232 (nth i *register-arg-tns*))
1235 (when (>= i register-arg-count)
1238 ;; Don't deposit any more than there are.
1240 (inst test ecx-tn ecx-tn)
1241 (inst cmp ecx-tn (fixnumize i)))
1242 (inst jmp :eq done)))
1248 (make-ea :dword :base ebp-tn
1249 :disp (- (* sb!vm:word-bytes
1250 (max 3 (sb-allocated-size 'stack))))))
1254 ;;; More args are stored contiguously on the stack, starting immediately at the
1255 ;;; context pointer. The context pointer is not typed, so the lowtag is 0.
1256 (define-vop (more-arg)
1257 (:translate %more-arg)
1258 (:policy :fast-safe)
1259 (:args (object :scs (descriptor-reg) :to :result)
1260 (index :scs (any-reg) :target temp))
1261 (:arg-types * tagged-num)
1262 (:temporary (:sc unsigned-reg :from (:argument 1) :to :result) temp)
1263 (:results (value :scs (any-reg descriptor-reg)))
1268 (inst mov value (make-ea :dword :base object :index temp))))
1270 (define-vop (more-arg-c)
1271 (:translate %more-arg)
1272 (:policy :fast-safe)
1273 (:args (object :scs (descriptor-reg)))
1275 (:arg-types * (:constant (signed-byte 30)))
1276 (:results (value :scs (any-reg descriptor-reg)))
1280 (make-ea :dword :base object :disp (- (* index word-bytes))))))
1283 ;;; Turn more arg (context, count) into a list.
1284 (define-vop (listify-rest-args)
1285 (:translate %listify-rest-args)
1287 (:args (context :scs (descriptor-reg) :target src)
1288 (count :scs (any-reg) :target ecx))
1289 (:arg-types * tagged-num)
1290 (:temporary (:sc unsigned-reg :offset esi-offset :from (:argument 0)) src)
1291 (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 1)) ecx)
1292 (:temporary (:sc unsigned-reg :offset eax-offset) eax)
1293 (:temporary (:sc unsigned-reg) dst)
1294 (:results (result :scs (descriptor-reg)))
1297 (let ((enter (gen-label))
1302 ;; Check to see whether there are no args, and just return NIL if so.
1303 (inst mov result nil-value)
1305 (inst lea dst (make-ea :dword :index ecx :scale 2))
1307 (allocation dst dst node)
1308 (inst lea dst (make-ea :byte :base dst :disp list-pointer-type))
1309 ;; Convert the count into a raw value, so that we can use the LOOP inst.
1311 ;; Set decrement mode (successive args at lower addresses)
1313 ;; Set up the result.
1315 ;; Jump into the middle of the loop, 'cause that's were we want
1319 ;; Compute a pointer to the next cons.
1320 (inst add dst (* cons-size word-bytes))
1321 ;; Store a pointer to this cons in the CDR of the previous cons.
1322 (storew dst dst -1 list-pointer-type)
1324 ;; Grab one value and stash it in the car of this cons.
1326 (storew eax dst 0 list-pointer-type)
1327 ;; Go back for more.
1329 ;; NIL out the last cons.
1330 (storew nil-value dst 1 sb!vm:list-pointer-type))
1331 (emit-label done))))
1333 ;;; Return the location and size of the more arg glob created by Copy-More-Arg.
1334 ;;; Supplied is the total number of arguments supplied (originally passed in
1335 ;;; ECX.) Fixed is the number of non-rest arguments.
1337 ;;; We must duplicate some of the work done by Copy-More-Arg, since at that
1338 ;;; time the environment is in a pretty brain-damaged state, preventing this
1339 ;;; info from being returned as values. What we do is compute
1340 ;;; supplied - fixed, and return a pointer that many words below the current
1342 (define-vop (more-arg-context)
1343 (:policy :fast-safe)
1344 (:translate sb!c::%more-arg-context)
1345 (:args (supplied :scs (any-reg) :target count))
1346 (:arg-types positive-fixnum (:constant fixnum))
1348 (:results (context :scs (descriptor-reg))
1349 (count :scs (any-reg)))
1350 (:result-types t tagged-num)
1351 (:note "more-arg-context")
1353 (move count supplied)
1354 ;; SP at this point points at the last arg pushed.
1355 ;; Point to the first more-arg, not above it.
1356 (inst lea context (make-ea :dword :base esp-tn
1357 :index count :scale 1
1358 :disp (- (+ (fixnumize fixed) 4))))
1359 (unless (zerop fixed)
1360 (inst sub count (fixnumize fixed)))))
1362 ;;; Signal wrong argument count error if Nargs isn't = to Count.
1363 (define-vop (verify-argument-count)
1364 (:policy :fast-safe)
1365 (:translate sb!c::%verify-argument-count)
1366 (:args (nargs :scs (any-reg)))
1367 (:arg-types positive-fixnum (:constant t))
1370 (:save-p :compute-only)
1373 (generate-error-code vop invalid-argument-count-error nargs)))
1375 (inst test nargs nargs) ; smaller instruction
1376 (inst cmp nargs (fixnumize count)))
1377 (inst jmp :ne err-lab))))
1379 ;;; Various other error signallers.
1380 (macrolet ((frob (name error translate &rest args)
1381 `(define-vop (,name)
1383 `((:policy :fast-safe)
1384 (:translate ,translate)))
1385 (:args ,@(mapcar #'(lambda (arg)
1386 `(,arg :scs (any-reg descriptor-reg)))
1389 (:save-p :compute-only)
1391 (error-call vop ,error ,@args)))))
1392 (frob argument-count-error invalid-argument-count-error
1393 sb!c::%argument-count-error nargs)
1394 (frob type-check-error object-not-type-error sb!c::%type-check-error
1396 (frob layout-invalid-error layout-invalid-error sb!c::%layout-invalid-error
1398 (frob odd-key-arguments-error odd-key-arguments-error
1399 sb!c::%odd-key-arguments-error)
1400 (frob unknown-key-argument-error unknown-key-argument-error
1401 sb!c::%unknown-key-argument-error key)
1402 (frob nil-function-returned-error nil-function-returned-error nil fun))