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.
17 ;;;; interfaces to IR2 conversion
19 ;;; Return a wired TN describing the N'th full call argument passing
21 (def-vm-support-routine standard-argument-location (n)
22 (declare (type unsigned-byte n))
23 (if (< n register-arg-count)
24 (make-wired-tn *backend-t-primitive-type* descriptor-reg-sc-number
25 (nth n register-arg-offsets))
26 (make-wired-tn *backend-t-primitive-type* control-stack-sc-number n)))
28 ;;; Make a passing location TN for a local call return PC.
30 ;;; Always wire the return PC location to the stack in its standard
35 (def-vm-support-routine make-return-pc-passing-location (standard)
36 (declare (ignore standard))
37 (make-wired-tn (primitive-type-or-lose 'system-area-pointer)
38 sap-stack-sc-number return-pc-save-offset))
39 ;;; If standard is true, then use the standard (full call) location,
40 ;;; otherwise use any legal location.
44 (def-vm-support-routine make-return-pc-passing-location (standard)
45 (let ((ptype (primitive-type-or-lose 'system-area-pointer)))
47 (make-wired-tn ptype sap-stack-sc-number return-pc-save-offset)
48 (make-normal-tn ptype))))
50 ;;; Similar to Make-Return-PC-Passing-Location, but makes a location to pass
53 ;;; This is wired in both the standard and the local-call
54 ;;; conventions, because we want to be able to assume it's always there.
55 ;;; Besides, the x86 doesn't have enough registers to really make it
56 ;;; profitable to pass it in a register.
60 (def-vm-support-routine make-old-fp-passing-location (standard)
61 (declare (ignore standard))
62 (make-wired-tn *fixnum-primitive-type* control-stack-sc-number
64 ;;; If standard is true, then use the standard (full call) location,
65 ;;; otherwise use any legal location.
69 (def-vm-support-routine make-old-fp-passing-location (standard)
71 (make-wired-tn *fixnum-primitive-type* control-stack-sc-number
73 (make-normal-tn *fixnum-primitive-type*)))
75 ;;; Make the TNs used to hold Old-FP and Return-PC within the current
76 ;;; function. We treat these specially so that the debugger can find them at a
79 ;;; Without using a save-tn - which does not make much sense if it is
80 ;;; wire to the stack? No problems.
81 (def-vm-support-routine make-old-fp-save-location (env)
82 (environment-debug-live-tn (make-wired-tn *fixnum-primitive-type*
83 control-stack-sc-number
86 ;;; Using a save-tn. No problems.
88 (def-vm-support-routine make-old-fp-save-location (env)
90 (environment-debug-live-tn (make-normal-tn *fixnum-primitive-type*) env)
91 (make-wired-tn *fixnum-primitive-type* control-stack-sc-number
94 ;;; Without using a save-tn - which does not make much sense if it is
95 ;;; wire to the stack? No problems.
96 (def-vm-support-routine make-return-pc-save-location (env)
97 (environment-debug-live-tn
98 (make-wired-tn (primitive-type-or-lose 'system-area-pointer)
99 sap-stack-sc-number return-pc-save-offset)
101 ;;; Using a save-tn. No problems.
103 (def-vm-support-routine make-return-pc-save-location (env)
104 (let ((ptype (primitive-type-or-lose 'system-area-pointer)))
106 (environment-debug-live-tn (make-normal-tn ptype) env)
107 (make-wired-tn ptype sap-stack-sc-number return-pc-save-offset))))
109 ;;; Make a TN for the standard argument count passing location. We only
110 ;;; need to make the standard location, since a count is never passed when we
111 ;;; are using non-standard conventions.
112 (def-vm-support-routine make-argument-count-location ()
113 (make-wired-tn *fixnum-primitive-type* any-reg-sc-number ecx-offset))
116 ;;; Make a TN to hold the number-stack frame pointer. This is allocated
117 ;;; once per component, and is component-live.
118 (def-vm-support-routine make-nfp-tn ()
119 (make-restricted-tn *fixnum-primitive-type* ignore-me-sc-number))
121 (def-vm-support-routine make-stack-pointer-tn ()
122 (make-normal-tn *fixnum-primitive-type*))
124 (def-vm-support-routine make-number-stack-pointer-tn ()
125 (make-restricted-tn *fixnum-primitive-type* ignore-me-sc-number))
127 ;;; Return a list of TNs that can be used to represent an unknown-values
128 ;;; continuation within a function.
129 (def-vm-support-routine make-unknown-values-locations ()
130 (list (make-stack-pointer-tn)
131 (make-normal-tn *fixnum-primitive-type*)))
134 ;;; This function is called by the Entry-Analyze phase, allowing
135 ;;; VM-dependent initialization of the IR2-Component structure. We push
136 ;;; placeholder entries in the Constants to leave room for additional
137 ;;; noise in the code object header.
139 ;;; For the x86 the first constant is a pointer to a list of fixups,
140 ;;; or nil if the code object has none.
141 (def-vm-support-routine select-component-format (component)
142 (declare (type component component))
143 (dotimes (i (1+ code-constants-offset))
144 (vector-push-extend nil
145 (ir2-component-constants (component-info component))))
150 ;;; Used for setting up the Old-FP in local call.
151 (define-vop (current-fp)
152 (:results (val :scs (any-reg control-stack)))
156 ;;; We don't have a separate NFP, so we don't need to do anything here.
157 (define-vop (compute-old-nfp)
164 (define-vop (xep-allocate-frame)
165 (:info start-lab copy-more-arg-follows)
168 ;; Make sure the function is aligned, and drop a label pointing to this
171 (trace-table-entry trace-table-function-prologue)
172 (emit-label start-lab)
173 ;; Skip space for the function header.
174 (inst function-header-word)
175 (dotimes (i (1- sb!vm:function-code-offset))
178 ;; The start of the actual code.
179 ;; Save the return-pc.
180 (popw ebp-tn (- (1+ return-pc-save-offset)))
182 ;; If copy-more-arg follows it will allocate the correct stack
183 ;; size. The stack is not allocated first here as this may expose
184 ;; args on the stack if they take up more space than the frame!
185 (unless copy-more-arg-follows
186 ;; The args fit within the frame so just allocate the frame.
188 (make-ea :dword :base ebp-tn
189 :disp (- (* sb!vm:word-bytes
190 (max 3 (sb-allocated-size 'stack)))))))
192 (trace-table-entry trace-table-normal)))
194 ;;; This is emitted directly before either a known-call-local, call-local,
195 ;;; or a multiple-call-local. All it does is allocate stack space for the
196 ;;; callee (who has the same size stack as us).
197 (define-vop (allocate-frame)
198 (:results (res :scs (any-reg control-stack))
204 (inst sub esp-tn (* sb!vm:word-bytes (sb-allocated-size 'stack)))))
206 ;;; Allocate a partial frame for passing stack arguments in a full call. Nargs
207 ;;; is the number of arguments passed. We allocate at least 3 slots, because
208 ;;; the XEP noise is going to want to use them before it can extend the stack.
209 (define-vop (allocate-full-call-frame)
211 (:results (res :scs (any-reg control-stack)))
214 (inst sub esp-tn (* (max nargs 3) sb!vm:word-bytes))))
218 ;;; Emit code needed at the return-point from an unknown-values call for a
219 ;;; fixed number of values. Values is the head of the TN-Ref list for the
220 ;;; locations that the values are to be received into. Nvals is the number of
221 ;;; values that are to be received (should equal the length of Values).
223 ;;; Move-Temp is a Descriptor-Reg TN used as a temporary.
225 ;;; This code exploits the fact that in the unknown-values convention, a
226 ;;; single value return returns at the return PC + 2, whereas a return of other
227 ;;; than one value returns directly at the return PC.
229 ;;; If 0 or 1 values are expected, then we just emit an instruction to reset
230 ;;; the SP (which will only be executed when other than 1 value is returned.)
232 ;;; In the general case we have to do three things:
233 ;;; -- Default unsupplied register values. This need only be done when a
234 ;;; single value is returned, since register values are defaulted by the
235 ;;; called in the non-single case.
236 ;;; -- Default unsupplied stack values. This needs to be done whenever there
237 ;;; are stack values.
238 ;;; -- Reset SP. This must be done whenever other than 1 value is returned,
239 ;;; regardless of the number of values desired.
240 (defun default-unknown-values (vop values nvals)
241 (declare (type (or tn-ref null) values)
242 (type unsigned-byte nvals))
245 (note-this-location vop :single-value-return)
246 (inst mov esp-tn ebx-tn))
247 ((<= nvals register-arg-count)
248 (let ((regs-defaulted (gen-label)))
249 (note-this-location vop :unknown-return)
250 (inst jmp-short regs-defaulted)
251 ;; Default the unsuppled registers.
252 (let* ((2nd-tn-ref (tn-ref-across values))
253 (2nd-tn (tn-ref-tn 2nd-tn-ref)))
254 (inst mov 2nd-tn nil-value)
257 for tn-ref = (tn-ref-across 2nd-tn-ref)
258 then (tn-ref-across tn-ref)
259 for count from 2 below register-arg-count
260 do count (inst mov (tn-ref-tn tn-ref) 2nd-tn))))
261 (inst mov ebx-tn esp-tn)
262 (emit-label regs-defaulted)
263 (inst mov esp-tn ebx-tn)))
265 ;; Number of bytes depends on the relative jump instructions. Best
266 ;; case is 31+(n-3)*14, worst case is 35+(n-3)*18. For nvals=6
267 ;; that is 73/89 bytes, and for nvals=7 that is 87/107 bytes which
268 ;; is likely better than using the blt below.
269 (let ((regs-defaulted (gen-label))
270 (defaulting-done (gen-label))
271 (default-stack-slots (gen-label)))
272 (note-this-location vop :unknown-return)
273 ;; Branch off to the MV case.
274 (inst jmp-short regs-defaulted)
275 ;; Do the single value case.
276 ;; Default the register args
277 (inst mov eax-tn nil-value)
279 (val (tn-ref-across values) (tn-ref-across val)))
280 ((= i (min nvals register-arg-count)))
281 (inst mov (tn-ref-tn val) eax-tn))
283 ;; Fake other registers so it looks like we returned with all the
284 ;; registers filled in.
287 (inst jmp default-stack-slots)
289 (emit-label regs-defaulted)
291 (inst mov eax-tn nil-value)
292 (storew edx-tn ebx-tn -1)
293 (collect ((defaults))
294 (do ((i register-arg-count (1+ i))
295 (val (do ((i 0 (1+ i))
296 (val values (tn-ref-across val)))
297 ((= i register-arg-count) val))
298 (tn-ref-across val)))
300 (let ((default-lab (gen-label))
301 (tn (tn-ref-tn val)))
302 (defaults (cons default-lab tn))
304 (inst cmp ecx-tn (fixnumize i))
305 (inst jmp :be default-lab)
306 (loadw edx-tn ebx-tn (- (1+ i)))
307 (inst mov tn edx-tn)))
309 (emit-label defaulting-done)
310 (loadw edx-tn ebx-tn -1)
313 (let ((defaults (defaults)))
315 (assemble (*elsewhere*)
316 (trace-table-entry trace-table-function-prologue)
317 (emit-label default-stack-slots)
318 (dolist (default defaults)
319 (emit-label (car default))
320 (inst mov (cdr default) eax-tn))
321 (inst jmp defaulting-done)
322 (trace-table-entry trace-table-normal)))))))
324 ;; 91 bytes for this branch.
325 (let ((regs-defaulted (gen-label))
326 (restore-edi (gen-label))
327 (no-stack-args (gen-label))
328 (default-stack-vals (gen-label))
329 (count-okay (gen-label)))
330 (note-this-location vop :unknown-return)
331 ;; Branch off to the MV case.
332 (inst jmp-short regs-defaulted)
334 ;; Default the register args, and set up the stack as if we entered
335 ;; the MV return point.
336 (inst mov ebx-tn esp-tn)
338 (inst mov edi-tn nil-value)
340 (inst mov esi-tn edi-tn)
341 ;; Compute a pointer to where to put the [defaulted] stack values.
342 (emit-label no-stack-args)
344 (make-ea :dword :base ebp-tn
345 :disp (* (- (1+ register-arg-count)) word-bytes)))
346 ;; Load EAX with NIL so we can quickly store it, and set up stuff
348 (inst mov eax-tn nil-value)
350 (inst mov ecx-tn (- nvals register-arg-count))
351 ;; Jump into the default loop.
352 (inst jmp default-stack-vals)
354 ;; The regs are defaulted. We need to copy any stack arguments,
355 ;; and then default the remaining stack arguments.
356 (emit-label regs-defaulted)
358 (storew edi-tn ebx-tn (- (1+ 1)))
359 ;; Compute the number of stack arguments, and if it's zero or less,
360 ;; don't copy any stack arguments.
361 (inst sub ecx-tn (fixnumize register-arg-count))
362 (inst jmp :le no-stack-args)
364 ;; Throw away any unwanted args.
365 (inst cmp ecx-tn (fixnumize (- nvals register-arg-count)))
366 (inst jmp :be count-okay)
367 (inst mov ecx-tn (fixnumize (- nvals register-arg-count)))
368 (emit-label count-okay)
369 ;; Save the number of stack values.
370 (inst mov eax-tn ecx-tn)
371 ;; Compute a pointer to where the stack args go.
373 (make-ea :dword :base ebp-tn
374 :disp (* (- (1+ register-arg-count)) word-bytes)))
375 ;; Save ESI, and compute a pointer to where the args come from.
376 (storew esi-tn ebx-tn (- (1+ 2)))
378 (make-ea :dword :base ebx-tn
379 :disp (* (- (1+ register-arg-count)) word-bytes)))
381 (inst shr ecx-tn word-shift) ; make word count
386 (loadw esi-tn ebx-tn (- (1+ 2)))
387 ;; Now we have to default the remaining args. Find out how many.
388 (inst sub eax-tn (fixnumize (- nvals register-arg-count)))
390 ;; If none, then just blow out of here.
391 (inst jmp :le restore-edi)
392 (inst mov ecx-tn eax-tn)
393 (inst shr ecx-tn word-shift) ; word count
394 ;; Load EAX with NIL for fast storing.
395 (inst mov eax-tn nil-value)
397 (emit-label default-stack-vals)
400 ;; Restore EDI, and reset the stack.
401 (emit-label restore-edi)
402 (loadw edi-tn ebx-tn (- (1+ 1)))
403 (inst mov esp-tn ebx-tn))))
406 ;;;; unknown values receiving
408 ;;; Emit code needed at the return point for an unknown-values call for an
409 ;;; arbitrary number of values.
411 ;;; We do the single and non-single cases with no shared code: there doesn't
412 ;;; seem to be any potential overlap, and receiving a single value is more
413 ;;; important efficiency-wise.
415 ;;; When there is a single value, we just push it on the stack, returning
416 ;;; the old SP and 1.
418 ;;; When there is a variable number of values, we move all of the argument
419 ;;; registers onto the stack, and return Args and Nargs.
421 ;;; Args and Nargs are TNs wired to the named locations. We must
422 ;;; explicitly allocate these TNs, since their lifetimes overlap with the
423 ;;; results Start and Count (also, it's nice to be able to target them).
424 (defun receive-unknown-values (args nargs start count)
425 (declare (type tn args nargs start count))
426 (let ((variable-values (gen-label))
428 (inst jmp-short variable-values)
430 (inst mov start esp-tn)
431 (inst push (first *register-arg-tns*))
432 (inst mov count (fixnumize 1))
435 (emit-label variable-values)
436 ;; dtc: this writes the registers onto the stack even if they are
437 ;; not needed, only the number specified in ecx are used and have
438 ;; stack allocated to them. No harm is done.
440 for arg in *register-arg-tns*
442 do (storew arg args i))
449 ;;; VOP that can be inherited by unknown values receivers. The main thing this
450 ;;; handles is allocation of the result temporaries.
451 (define-vop (unknown-values-receiver)
452 (:temporary (:sc descriptor-reg :offset ebx-offset
453 :from :eval :to (:result 0))
455 (:temporary (:sc any-reg :offset ecx-offset
456 :from :eval :to (:result 1))
458 (:results (start :scs (any-reg control-stack))
459 (count :scs (any-reg control-stack))))
461 ;;;; local call with unknown values convention return
463 ;;; Non-TR local call for a fixed number of values passed according to the
464 ;;; unknown values convention.
466 ;;; FP is the frame pointer in install before doing the call.
468 ;;; NFP would be the number-stack frame pointer if we had a separate number
471 ;;; Args are the argument passing locations, which are specified only to
472 ;;; terminate their lifetimes in the caller.
474 ;;; Values are the return value locations (wired to the standard passing
476 ;;; Nvals is the number of values received.
478 ;;; Save is the save info, which we can ignore since saving has been done.
480 ;;; Target is a continuation pointing to the start of the called function.
481 (define-vop (call-local)
485 (:results (values :more t))
487 (:move-args :local-call)
488 (:info arg-locs callee target nvals)
490 (:ignore nfp arg-locs args #+nil callee)
492 (trace-table-entry trace-table-call-site)
495 (let ((ret-tn (callee-return-pc-tn callee)))
497 (format t "*call-local ~S; tn-kind ~S; tn-save-tn ~S; its tn-kind ~S~%"
498 ret-tn (sb!c::tn-kind ret-tn) (sb!c::tn-save-tn ret-tn)
499 (sb!c::tn-kind (sb!c::tn-save-tn ret-tn)))
501 ;; Is the return-pc on the stack or in a register?
504 #+nil (format t "*call-local: ret-tn on stack; offset=~S~%"
506 (storew (make-fixup nil :code-object return)
507 ebp-tn (- (1+ (tn-offset ret-tn)))))
509 (inst lea ret-tn (make-fixup nil :code-object return)))))
511 (note-this-location vop :call-site)
514 (default-unknown-values vop values nvals)
515 (trace-table-entry trace-table-normal)))
517 ;;; Non-TR local call for a variable number of return values passed according
518 ;;; to the unknown values convention. The results are the start of the values
519 ;;; glob and the number of values received.
520 (define-vop (multiple-call-local unknown-values-receiver)
525 (:move-args :local-call)
526 (:info save callee target)
527 (:ignore args save nfp #+nil callee)
530 (trace-table-entry trace-table-call-site)
533 (let ((ret-tn (callee-return-pc-tn callee)))
535 (format t "*multiple-call-local ~S; tn-kind ~S; tn-save-tn ~S; its tn-kind ~S~%"
536 ret-tn (sb!c::tn-kind ret-tn) (sb!c::tn-save-tn ret-tn)
537 (sb!c::tn-kind (sb!c::tn-save-tn ret-tn)))
539 ;; Is the return-pc on the stack or in a register?
542 #+nil (format t "*multiple-call-local: ret-tn on stack; offset=~S~%"
545 (storew (make-fixup nil :code-object return)
546 ebp-tn (- (1+ (tn-offset ret-tn)))))
549 (inst lea ret-tn (make-fixup nil :code-object return)))))
551 (note-this-location vop :call-site)
554 (note-this-location vop :unknown-return)
555 (receive-unknown-values values-start nvals start count)
556 (trace-table-entry trace-table-normal)))
558 ;;;; local call with known values return
560 ;;; Non-TR local call with known return locations. Known-value return works
561 ;;; just like argument passing in local call.
563 ;;; Note: we can't use normal load-tn allocation for the fixed args, since all
564 ;;; registers may be tied up by the more operand. Instead, we use
565 ;;; MAYBE-LOAD-STACK-TN.
566 (define-vop (known-call-local)
570 (:results (res :more t))
571 (:move-args :local-call)
573 (:info save callee target)
574 (:ignore args res save nfp #+nil callee)
577 (trace-table-entry trace-table-call-site)
580 (let ((ret-tn (callee-return-pc-tn callee)))
583 (format t "*known-call-local ~S; tn-kind ~S; tn-save-tn ~S; its tn-kind ~S~%"
584 ret-tn (sb!c::tn-kind ret-tn) (sb!c::tn-save-tn ret-tn)
585 (sb!c::tn-kind (sb!c::tn-save-tn ret-tn)))
587 ;; Is the return-pc on the stack or in a register?
590 #+nil (format t "*known-call-local: ret-tn on stack; offset=~S~%"
593 (storew (make-fixup nil :code-object return)
594 ebp-tn (- (1+ (tn-offset ret-tn)))))
597 (inst lea ret-tn (make-fixup nil :code-object return)))))
599 (note-this-location vop :call-site)
602 (note-this-location vop :known-return)
603 (trace-table-entry trace-table-normal)))
605 ;;; Return from known values call. We receive the return locations as
606 ;;; arguments to terminate their lifetimes in the returning function. We
607 ;;; restore FP and CSP and jump to the Return-PC.
609 ;;; We can assume we know exactly where old-fp and return-pc are because
610 ;;; make-old-fp-save-location and make-return-pc-save-location always
611 ;;; return the same place.
613 (define-vop (known-return)
615 (return-pc :scs (any-reg immediate-stack) :target rpc)
617 (:move-args :known-return)
619 (:temporary (:sc unsigned-reg :from (:argument 1)) rpc)
620 (:ignore val-locs vals)
623 (trace-table-entry trace-table-function-epilogue)
624 ;; Save the return-pc in a register 'cause the frame-pointer is going away.
625 ;; Note this not in the usual stack location so we can't use RET
627 ;; Restore the stack.
629 ;; Restore the old fp. We know OLD-FP is going to be in its stack
630 ;; save slot, which is a different frame that than this one,
631 ;; so we don't have to worry about having just cleared
632 ;; most of the stack.
635 (trace-table-entry trace-table-normal)))
637 ;;; From Douglas Crosher
638 ;;; Return from known values call. We receive the return locations as
639 ;;; arguments to terminate their lifetimes in the returning function. We
640 ;;; restore FP and CSP and jump to the Return-PC.
642 ;;; The old-fp may be either in a register or on the stack in its
643 ;;; standard save locations - slot 0.
645 ;;; The return-pc may be in a register or on the stack in any slot.
646 (define-vop (known-return)
650 (:move-args :known-return)
652 (:ignore val-locs vals)
655 (trace-table-entry trace-table-function-epilogue)
657 #+nil (format t "*known-return: old-fp ~S, tn-kind ~S; ~S ~S~%"
658 old-fp (sb!c::tn-kind old-fp) (sb!c::tn-save-tn old-fp)
659 (sb!c::tn-kind (sb!c::tn-save-tn old-fp)))
661 #+nil (format t "*known-return: return-pc ~S, tn-kind ~S; ~S ~S~%"
662 return-pc (sb!c::tn-kind return-pc) (sb!c::tn-save-tn return-pc)
663 (sb!c::tn-kind (sb!c::tn-save-tn return-pc)))
665 ;; return-pc may be either in a register or on the stack.
671 #+nil (format t "*known-return: old-fp ~S on stack; offset=~S~%"
672 old-fp (tn-offset old-fp))
674 (cond ((zerop (tn-offset old-fp))
675 ;; Zot all of the stack except for the old-fp.
676 (inst lea esp-tn (make-ea :dword :base ebp-tn
677 :disp (- (* (1+ ocfp-save-offset)
679 ;; Restore the old fp from its save location on the stack,
680 ;; and zot the stack.
684 (cerror "Continue any-way"
685 "VOP return-local doesn't work if old-fp (in slot %s) is not in slot 0"
686 (tn-offset old-fp)))))
688 ((any-reg descriptor-reg)
689 ;; Zot all the stack.
691 ;; Restore the old-fp.
692 (move ebp-tn old-fp)))
694 ;; Return; return-pc is in a register.
695 (inst jmp return-pc))
699 #+nil (format t "*known-return: return-pc ~S on stack; offset=~S~%"
700 return-pc (tn-offset return-pc))
702 ;; Zot all of the stack except for the old-fp and return-pc.
704 (make-ea :dword :base ebp-tn
705 :disp (- (* (1+ (tn-offset return-pc)) word-bytes))))
706 ;; Restore the old fp. old-fp may be either on the stack in its
707 ;; save location or in a register, in either case this restores it.
709 ;; The return pops the return address (4 bytes), then we need
710 ;; to pop all the slots before the return-pc which includes the
711 ;; 4 bytes for the old-fp.
712 (inst ret (* (tn-offset return-pc) word-bytes))))
714 (trace-table-entry trace-table-normal)))
718 ;;; There is something of a cross-product effect with full calls. Different
719 ;;; versions are used depending on whether we know the number of arguments or
720 ;;; the name of the called function, and whether we want fixed values, unknown
721 ;;; values, or a tail call.
723 ;;; In full call, the arguments are passed creating a partial frame on the
724 ;;; stack top and storing stack arguments into that frame. On entry to the
725 ;;; callee, this partial frame is pointed to by FP.
727 ;;; This macro helps in the definition of full call VOPs by avoiding code
728 ;;; replication in defining the cross-product VOPs.
730 ;;; Name is the name of the VOP to define.
732 ;;; Named is true if the first argument is an fdefinition object whose
733 ;;; definition is to be called.
735 ;;; Return is either :Fixed, :Unknown or :Tail:
736 ;;; -- If :Fixed, then the call is for a fixed number of values, returned in
737 ;;; the standard passing locations (passed as result operands).
738 ;;; -- If :Unknown, then the result values are pushed on the stack, and the
739 ;;; result values are specified by the Start and Count as in the
740 ;;; unknown-values continuation representation.
741 ;;; -- If :Tail, then do a tail-recursive call. No values are returned.
742 ;;; The Old-Fp and Return-PC are passed as the second and third arguments.
744 ;;; In non-tail calls, the pointer to the stack arguments is passed as the last
745 ;;; fixed argument. If Variable is false, then the passing locations are
746 ;;; passed as a more arg. Variable is true if there are a variable number of
747 ;;; arguments passed on the stack. Variable cannot be specified with :Tail
748 ;;; return. TR variable argument call is implemented separately.
750 ;;; In tail call with fixed arguments, the passing locations are passed as a
751 ;;; more arg, but there is no new-FP, since the arguments have been set up in
752 ;;; the current frame.
753 (macrolet ((define-full-call (name named return variable)
754 (assert (not (and variable (eq return :tail))))
756 ,@(when (eq return :unknown)
757 '(unknown-values-receiver)))
759 ,@(unless (eq return :tail)
760 '((new-fp :scs (any-reg) :to (:argument 1))))
762 (fun :scs (descriptor-reg control-stack)
763 :target eax :to (:argument 0))
765 ,@(when (eq return :tail)
769 ,@(unless variable '((args :more t :scs (descriptor-reg)))))
771 ,@(when (eq return :fixed)
772 '((:results (values :more t))))
774 (:save-p ,(if (eq return :tail) :compute-only t))
776 ,@(unless (or (eq return :tail) variable)
777 '((:move-args :full-call)))
781 ,@(unless (or variable (eq return :tail)) '(arg-locs))
782 ,@(unless variable '(nargs))
783 ,@(when (eq return :fixed) '(nvals)))
786 ,@(unless (or variable (eq return :tail)) '(arg-locs))
787 ,@(unless variable '(args)))
789 ;; We pass either the fdefn object (for named call) or the actual
790 ;; function object (for unnamed call) in EAX. With named call,
791 ;; closure-tramp will replace it with the real function and invoke
792 ;; the real function for closures. Non-closures do not need this
793 ;; value, so don't care what shows up in it.
795 (:sc descriptor-reg :offset eax-offset :from (:argument 0) :to :eval)
798 ;; We pass the number of arguments in ECX.
799 (:temporary (:sc unsigned-reg :offset ecx-offset :to :eval) ecx)
801 ;; With variable call, we have to load the register-args out
802 ;; of the (new) stack frame before doing the call. Therefore,
803 ;; we have to tell the lifetime stuff that we need to use them.
805 (mapcar #'(lambda (name offset)
806 `(:temporary (:sc descriptor-reg
811 register-arg-names register-arg-offsets))
813 ,@(when (eq return :tail)
814 '((:temporary (:sc unsigned-reg
815 :from (:argument 1) :to (:argument 2)) old-fp-tmp)))
817 (:generator ,(+ (if named 5 0)
819 (if (eq return :tail) 0 10)
821 (if (eq return :unknown) 25 0))
822 (trace-table-entry trace-table-call-site)
824 ;; This has to be done before the frame pointer is changed!
825 ;; eax stores the 'lexical environment' needed for closures
830 ;; For variable call, compute the number of arguments and
831 ;; move some of the arguments to registers.
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, this
837 ;; moves them all even if they are not all needed.
839 for name in register-arg-names
840 for index downfrom -1
841 do (noise `(loadw ,name new-fp ,index)))
845 (inst mov ecx (fixnumize nargs)))))
846 ,@(cond ((eq return :tail)
847 '(;; Python has figured out what frame we should return
848 ;; to so might as well use that clue. This seems
849 ;; really important to the implementation of things
850 ;; like (without-interrupts ...)
852 ;; dtc; Could be doing a tail call from a
853 ;; known-local-call etc in which the old-fp or ret-pc
854 ;; are in regs or in non-standard places. If the
855 ;; passing location were wired to the stack in
856 ;; standard locations then these moves will be
857 ;; un-necessary; this is probably best for the x86.
860 (unless (= ocfp-save-offset
862 ;; FIXME: FORMAT T for stale diagnostic
863 ;; output (several of them around here),
865 (format t "** tail-call old-fp not S0~%")
866 (move old-fp-tmp old-fp)
869 (- (1+ ocfp-save-offset)))))
870 ((any-reg descriptor-reg)
871 (format t "** tail-call old-fp in reg not S0~%")
874 (- (1+ ocfp-save-offset)))))
876 ;; For tail call, we have to push the return-pc so
877 ;; that it looks like we CALLed despite the fact that
878 ;; we are going to JMP.
879 (inst push return-pc)
882 ;; For non-tail call, we have to save our frame pointer
883 ;; and install the new frame pointer. We can't load
884 ;; stack tns after this point.
885 `(;; Python doesn't seem to allocate a frame here which
886 ;; doesn't leave room for the ofp/ret stuff.
888 ;; The variable args are on the stack and become the
889 ;; frame, but there may be <3 args and 3 stack slots
890 ;; are assumed allocate on the call. So need to
891 ;; ensure there are at least 3 slots. This hack just
894 '(inst sub esp-tn (fixnumize 3)))
897 (storew ebp-tn new-fp (- (1+ ocfp-save-offset)))
899 (move ebp-tn new-fp) ; NB - now on new stack frame.
902 (note-this-location vop :call-site)
904 (inst ,(if (eq return :tail) 'jmp 'call)
905 (make-ea :dword :base eax
907 '(- (* fdefn-raw-addr-slot word-bytes)
909 '(- (* closure-function-slot word-bytes)
910 function-pointer-type))))
913 '((default-unknown-values vop values nvals)))
915 '((note-this-location vop :unknown-return)
916 (receive-unknown-values values-start nvals start count)))
918 (trace-table-entry trace-table-normal)))))
920 (define-full-call call nil :fixed nil)
921 (define-full-call call-named t :fixed nil)
922 (define-full-call multiple-call nil :unknown nil)
923 (define-full-call multiple-call-named t :unknown nil)
924 (define-full-call tail-call nil :tail nil)
925 (define-full-call tail-call-named t :tail nil)
927 (define-full-call call-variable nil :fixed t)
928 (define-full-call multiple-call-variable nil :unknown t))
930 ;;; This is defined separately, since it needs special code that BLT's the
931 ;;; arguments down. All the real work is done in the assembly routine. We just
932 ;;; set things up so that it can find what it needs.
933 (define-vop (tail-call-variable)
934 (:args (args :scs (any-reg control-stack) :target esi)
935 (function :scs (descriptor-reg control-stack) :target eax)
938 (:temporary (:sc unsigned-reg :offset esi-offset :from (:argument 0)) esi)
939 (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 1)) eax)
940 ; (:ignore ret-addr old-fp)
942 ;; Move these into the passing locations if they are not already there.
946 ;; The following assumes that the return-pc and old-fp are on the
947 ;; stack in their standard save locations - Check this.
948 (unless (and (sc-is old-fp control-stack)
949 (= (tn-offset old-fp) ocfp-save-offset))
950 (error "tail-call-variable: ocfp not on stack in standard save location?"))
951 (unless (and (sc-is ret-addr sap-stack)
952 (= (tn-offset ret-addr) return-pc-save-offset))
953 (error "tail-call-variable: ret-addr not on stack in standard save location?"))
956 ;; And jump to the assembly routine.
957 (inst jmp (make-fixup 'tail-call-variable :assembly-routine))))
959 ;;;; unknown values return
961 ;;; Return a single-value using the Unknown-Values convention. Specifically,
962 ;;; we jump to clear the stack and jump to return-pc+2.
964 ;;; We require old-fp to be in a register, because we want to reset ESP before
965 ;;; restoring EBP. If old-fp were still on the stack, it could get clobbered
968 ;;; pfw--get wired-tn conflicts sometimes if register sc specd for args
969 ;;; having problems targeting args to regs -- using temps instead.
970 (define-vop (return-single)
974 (:temporary (:sc unsigned-reg) ofp)
975 (:temporary (:sc unsigned-reg) ret)
978 (trace-table-entry trace-table-function-epilogue)
980 ;; Clear the control stack
982 ;; Adjust the return address for the single value return.
984 ;; Restore the frame pointer.
990 ;;; Do unknown-values return of a fixed (other than 1) number of values. The
991 ;;; Values are required to be set up in the standard passing locations. Nvals
992 ;;; is the number of values returned.
994 ;;; Basically, we just load ECX with the number of values returned and EBX
995 ;;; with a pointer to the values, set ESP to point to the end of the values,
996 ;;; and jump directly to return-pc.
999 (return-pc :to (:eval 1))
1004 ;; In the case of other than one value, we need these registers to tell
1005 ;; the caller where they are and how many there are.
1006 (:temporary (:sc unsigned-reg :offset ebx-offset) ebx)
1007 (:temporary (:sc unsigned-reg :offset ecx-offset) ecx)
1009 ;; We need to stretch the lifetime of return-pc past the argument
1010 ;; registers so that we can default the argument registers without
1011 ;; trashing return-pc.
1012 (:temporary (:sc unsigned-reg :offset (first register-arg-offsets)
1014 (:temporary (:sc unsigned-reg :offset (second register-arg-offsets)
1016 (:temporary (:sc unsigned-reg :offset (third register-arg-offsets)
1020 (trace-table-entry trace-table-function-epilogue)
1021 ;; Establish the values pointer and values count.
1024 (inst xor ecx ecx) ; smaller
1025 (inst mov ecx (fixnumize nvals)))
1026 ;; restore the frame pointer.
1027 (move ebp-tn old-fp)
1028 ;; clear as much of the stack as possible, but not past the return
1030 (inst lea esp-tn (make-ea :dword :base ebx
1031 :disp (- (* (max nvals 2) word-bytes))))
1032 ;; pre-default any argument register that need it.
1033 (when (< nvals register-arg-count)
1034 (let* ((arg-tns (nthcdr nvals (list a0 a1 a2)))
1035 (first (first arg-tns)))
1036 (inst mov first nil-value)
1037 (dolist (tn (cdr arg-tns))
1038 (inst mov tn first))))
1039 ;; And away we go. Except that return-pc is still on the
1040 ;; stack and we've changed the stack pointer. So we have to
1041 ;; tell it to index off of EBX instead of EBP.
1042 (cond ((zerop nvals)
1043 ;; Return popping the return address and the OCFP.
1044 (inst ret word-bytes))
1046 ;; Return popping the return, leaving 1 slot. Can this
1047 ;; happen, or is a single value return handled elsewhere?
1050 (inst jmp (make-ea :dword :base ebx
1051 :disp (- (* (1+ (tn-offset return-pc))
1054 (trace-table-entry trace-table-normal)))
1056 ;;; Do unknown-values return of an arbitrary number of values (passed on the
1057 ;;; stack.) We check for the common case of a single return value, and do that
1058 ;;; inline using the normal single value return convention. Otherwise, we
1059 ;;; branch off to code that calls an assembly-routine.
1061 ;;; The assembly routine takes the following args:
1062 ;;; EAX -- the return-pc to finally jump to.
1063 ;;; EBX -- pointer to where to put the values.
1064 ;;; ECX -- number of values to find there.
1065 ;;; ESI -- pointer to where to find the values.
1066 (define-vop (return-multiple)
1067 (:args (old-fp :to (:eval 1) :target old-fp-temp)
1068 (return-pc :target eax)
1069 (vals :scs (any-reg) :target esi)
1070 (nvals :scs (any-reg) :target ecx))
1072 (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 1)) eax)
1073 (:temporary (:sc unsigned-reg :offset esi-offset :from (:argument 2)) esi)
1074 (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 3)) ecx)
1075 (:temporary (:sc unsigned-reg :offset ebx-offset :from (:eval 0)) ebx)
1076 (:temporary (:sc descriptor-reg :offset (first register-arg-offsets)
1077 :from (:eval 0)) a0)
1078 (:temporary (:sc unsigned-reg :from (:eval 1)) old-fp-temp)
1082 (trace-table-entry trace-table-function-epilogue)
1083 ;; Load the return-pc.
1084 (move eax return-pc)
1085 (unless (policy node (> space speed))
1086 ;; Check for the single case.
1087 (let ((not-single (gen-label)))
1088 (inst cmp nvals (fixnumize 1))
1089 (inst jmp :ne not-single)
1091 ;; Return with one value.
1093 ;; Clear the stack. We load old-fp into a register before clearing
1095 (move old-fp-temp old-fp)
1096 (move esp-tn ebp-tn)
1097 (move ebp-tn old-fp-temp)
1098 ;; Fix the return-pc to point at the single-value entry point.
1103 ;; Nope, not the single case. Jump to the assembly routine.
1104 (emit-label not-single)))
1108 (move ebp-tn old-fp)
1109 (inst jmp (make-fixup 'return-multiple :assembly-routine))
1110 (trace-table-entry trace-table-normal)))
1114 ;;; We don't need to do anything special for regular functions.
1115 (define-vop (setup-environment)
1119 ;; Don't bother doing anything.
1122 ;;; Get the lexical environment from its passing location.
1123 (define-vop (setup-closure-environment)
1124 (:results (closure :scs (descriptor-reg)))
1129 (move closure eax-tn)))
1131 ;;; Copy a more arg from the argument area to the end of the current frame.
1132 ;;; Fixed is the number of non-more arguments.
1134 ;;; The tricky part is doing this without trashing any of the calling
1135 ;;; convention registers that are still needed. This vop is emitted directly
1136 ;;; after the xep-allocate frame. That means the registers are in use as
1139 ;;; EAX -- The lexenv.
1140 ;;; EBX -- Available.
1141 ;;; ECX -- The total number of arguments.
1142 ;;; EDX -- The first arg.
1143 ;;; EDI -- The second arg.
1144 ;;; ESI -- The third arg.
1146 ;;; So basically, we have one register available for our use: EBX.
1148 ;;; What we can do is push the other regs onto the stack, and then restore
1149 ;;; their values by looking directly below where we put the more-args.
1150 (define-vop (copy-more-arg)
1153 ;; Avoid the copy if there are no more args.
1154 (cond ((zerop fixed)
1155 (inst jecxz just-alloc-frame))
1157 (inst cmp ecx-tn (fixnumize fixed))
1158 (inst jmp :be just-alloc-frame)))
1160 ;; Allocate the space on the stack.
1161 ;; stack = ebp - (max 3 frame-size) - (nargs - fixed)
1163 (make-ea :dword :base ebp-tn
1164 :disp (- (fixnumize fixed)
1166 (max 3 (sb-allocated-size 'stack))))))
1167 (inst sub ebx-tn ecx-tn) ; Got the new stack in ebx
1168 (inst mov esp-tn ebx-tn)
1170 ;; Now: nargs>=1 && nargs>fixed
1172 ;; Save the original count of args.
1173 (inst mov ebx-tn ecx-tn)
1175 (cond ((< fixed register-arg-count)
1176 ;; We must stop when we run out of stack args, not when we
1177 ;; run out of more args.
1178 ;; Number to copy = nargs-3
1179 (inst sub ecx-tn (fixnumize register-arg-count))
1180 ;; Everything of interest in registers.
1181 (inst jmp :be do-regs))
1183 ;; Number to copy = nargs-fixed
1184 (inst sub ecx-tn (fixnumize fixed))))
1186 ;; Save edi and esi register args.
1189 ;; Okay, we have pushed the register args. We can trash them
1192 ;; Initialize dst to be end of stack; skiping the values pushed
1194 (inst lea edi-tn (make-ea :dword :base esp-tn :disp 8))
1196 ;; Initialize src to be end of args.
1197 (inst mov esi-tn ebp-tn)
1198 (inst sub esi-tn ebx-tn)
1200 (inst shr ecx-tn word-shift) ; make word count
1201 ;; And copy the args.
1202 (inst cld) ; auto-inc ESI and EDI.
1206 ;; So now we need to restore EDI and ESI.
1213 (inst mov ecx-tn ebx-tn)
1215 ;; Here: nargs>=1 && nargs>fixed
1216 (when (< fixed register-arg-count)
1217 ;; Now we have to deposit any more args that showed up in
1221 ;; Store it relative to ebp
1222 (inst mov (make-ea :dword :base ebp-tn
1225 (max 3 (sb-allocated-size 'stack))))))
1226 (nth i *register-arg-tns*))
1229 (when (>= i register-arg-count)
1232 ;; Don't deposit any more than there are.
1234 (inst test ecx-tn ecx-tn)
1235 (inst cmp ecx-tn (fixnumize i)))
1236 (inst jmp :eq done)))
1242 (make-ea :dword :base ebp-tn
1243 :disp (- (* sb!vm:word-bytes
1244 (max 3 (sb-allocated-size 'stack))))))
1248 ;;; More args are stored contiguously on the stack, starting immediately at the
1249 ;;; context pointer. The context pointer is not typed, so the lowtag is 0.
1250 (define-vop (more-arg)
1251 (:translate %more-arg)
1252 (:policy :fast-safe)
1253 (:args (object :scs (descriptor-reg) :to :result)
1254 (index :scs (any-reg) :target temp))
1255 (:arg-types * tagged-num)
1256 (:temporary (:sc unsigned-reg :from (:argument 1) :to :result) temp)
1257 (:results (value :scs (any-reg descriptor-reg)))
1262 (inst mov value (make-ea :dword :base object :index temp))))
1264 (define-vop (more-arg-c)
1265 (:translate %more-arg)
1266 (:policy :fast-safe)
1267 (:args (object :scs (descriptor-reg)))
1269 (:arg-types * (:constant (signed-byte 30)))
1270 (:results (value :scs (any-reg descriptor-reg)))
1274 (make-ea :dword :base object :disp (- (* index word-bytes))))))
1277 ;;; Turn more arg (context, count) into a list.
1278 (define-vop (listify-rest-args)
1279 (:translate %listify-rest-args)
1281 (:args (context :scs (descriptor-reg) :target src)
1282 (count :scs (any-reg) :target ecx))
1283 (:arg-types * tagged-num)
1284 (:temporary (:sc unsigned-reg :offset esi-offset :from (:argument 0)) src)
1285 (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 1)) ecx)
1286 (:temporary (:sc unsigned-reg :offset eax-offset) eax)
1287 (:temporary (:sc unsigned-reg) dst)
1288 (:results (result :scs (descriptor-reg)))
1291 (let ((enter (gen-label))
1296 ;; Check to see whether there are no args, and just return NIL if so.
1297 (inst mov result nil-value)
1299 (inst lea dst (make-ea :dword :index ecx :scale 2))
1301 (allocation dst dst node)
1302 (inst lea dst (make-ea :byte :base dst :disp list-pointer-type))
1303 ;; Convert the count into a raw value, so that we can use the LOOP inst.
1305 ;; Set decrement mode (successive args at lower addresses)
1307 ;; Set up the result.
1309 ;; Jump into the middle of the loop, 'cause that's were we want
1313 ;; Compute a pointer to the next cons.
1314 (inst add dst (* cons-size word-bytes))
1315 ;; Store a pointer to this cons in the CDR of the previous cons.
1316 (storew dst dst -1 list-pointer-type)
1318 ;; Grab one value and stash it in the car of this cons.
1320 (storew eax dst 0 list-pointer-type)
1321 ;; Go back for more.
1323 ;; NIL out the last cons.
1324 (storew nil-value dst 1 sb!vm:list-pointer-type))
1325 (emit-label done))))
1327 ;;; Return the location and size of the more arg glob created by Copy-More-Arg.
1328 ;;; Supplied is the total number of arguments supplied (originally passed in
1329 ;;; ECX.) Fixed is the number of non-rest arguments.
1331 ;;; We must duplicate some of the work done by Copy-More-Arg, since at that
1332 ;;; time the environment is in a pretty brain-damaged state, preventing this
1333 ;;; info from being returned as values. What we do is compute
1334 ;;; supplied - fixed, and return a pointer that many words below the current
1336 (define-vop (more-arg-context)
1337 (:policy :fast-safe)
1338 (:translate sb!c::%more-arg-context)
1339 (:args (supplied :scs (any-reg) :target count))
1340 (:arg-types positive-fixnum (:constant fixnum))
1342 (:results (context :scs (descriptor-reg))
1343 (count :scs (any-reg)))
1344 (:result-types t tagged-num)
1345 (:note "more-arg-context")
1347 (move count supplied)
1348 ;; SP at this point points at the last arg pushed.
1349 ;; Point to the first more-arg, not above it.
1350 (inst lea context (make-ea :dword :base esp-tn
1351 :index count :scale 1
1352 :disp (- (+ (fixnumize fixed) 4))))
1353 (unless (zerop fixed)
1354 (inst sub count (fixnumize fixed)))))
1356 ;;; Signal wrong argument count error if Nargs isn't = to Count.
1357 (define-vop (verify-argument-count)
1358 (:policy :fast-safe)
1359 (:translate sb!c::%verify-argument-count)
1360 (:args (nargs :scs (any-reg)))
1361 (:arg-types positive-fixnum (:constant t))
1364 (:save-p :compute-only)
1367 (generate-error-code vop invalid-argument-count-error nargs)))
1369 (inst test nargs nargs) ; smaller instruction
1370 (inst cmp nargs (fixnumize count)))
1371 (inst jmp :ne err-lab))))
1373 ;;; Various other error signallers.
1374 (macrolet ((frob (name error translate &rest args)
1375 `(define-vop (,name)
1377 `((:policy :fast-safe)
1378 (:translate ,translate)))
1379 (:args ,@(mapcar #'(lambda (arg)
1380 `(,arg :scs (any-reg descriptor-reg)))
1383 (:save-p :compute-only)
1385 (error-call vop ,error ,@args)))))
1386 (frob argument-count-error invalid-argument-count-error
1387 sb!c::%argument-count-error nargs)
1388 (frob type-check-error object-not-type-error sb!c::%type-check-error
1390 (frob layout-invalid-error layout-invalid-error sb!c::%layout-invalid-error
1392 (frob odd-keyword-arguments-error odd-keyword-arguments-error
1393 sb!c::%odd-keyword-arguments-error)
1394 (frob unknown-keyword-argument-error unknown-keyword-argument-error
1395 sb!c::%unknown-keyword-argument-error key)
1396 (frob nil-function-returned-error nil-function-returned-error nil fun))