1 ;;;; function call for the x86 VM
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
14 ;;;; interfaces to IR2 conversion
16 ;;; Return a wired TN describing the N'th full call argument passing
18 (!def-vm-support-routine standard-arg-location (n)
19 (declare (type unsigned-byte n))
20 (if (< n register-arg-count)
21 (make-wired-tn *backend-t-primitive-type* descriptor-reg-sc-number
22 (nth n *register-arg-offsets*))
23 (make-wired-tn *backend-t-primitive-type* control-stack-sc-number n)))
25 ;;; Make a passing location TN for a local call return PC.
27 ;;; Always wire the return PC location to the stack in its standard
29 (!def-vm-support-routine make-return-pc-passing-location (standard)
30 (declare (ignore standard))
31 (make-wired-tn (primitive-type-or-lose 'system-area-pointer)
32 sap-stack-sc-number return-pc-save-offset))
34 ;;; This is similar to MAKE-RETURN-PC-PASSING-LOCATION, but makes a
35 ;;; location to pass OLD-FP in.
37 ;;; This is wired in both the standard and the local-call conventions,
38 ;;; because we want to be able to assume it's always there. Besides,
39 ;;; the x86 doesn't have enough registers to really make it profitable
40 ;;; to pass it in a register.
41 (!def-vm-support-routine make-old-fp-passing-location (standard)
42 (declare (ignore standard))
43 (make-wired-tn *fixnum-primitive-type* control-stack-sc-number
46 ;;; Make the TNs used to hold OLD-FP and RETURN-PC within the current
47 ;;; function. We treat these specially so that the debugger can find
48 ;;; them at a known location.
50 ;;; Without using a save-tn - which does not make much sense if it is
51 ;;; wired to the stack?
52 (!def-vm-support-routine make-old-fp-save-location (physenv)
53 (physenv-debug-live-tn (make-wired-tn *fixnum-primitive-type*
54 control-stack-sc-number
57 (!def-vm-support-routine make-return-pc-save-location (physenv)
58 (physenv-debug-live-tn
59 (make-wired-tn (primitive-type-or-lose 'system-area-pointer)
60 sap-stack-sc-number return-pc-save-offset)
63 ;;; Make a TN for the standard argument count passing location. We only
64 ;;; need to make the standard location, since a count is never passed when we
65 ;;; are using non-standard conventions.
66 (!def-vm-support-routine make-arg-count-location ()
67 (make-wired-tn *fixnum-primitive-type* any-reg-sc-number ecx-offset))
69 ;;; Make a TN to hold the number-stack frame pointer. This is allocated
70 ;;; once per component, and is component-live.
71 (!def-vm-support-routine make-nfp-tn ()
72 (make-restricted-tn *fixnum-primitive-type* ignore-me-sc-number))
74 (!def-vm-support-routine make-stack-pointer-tn ()
75 (make-normal-tn *fixnum-primitive-type*))
77 (!def-vm-support-routine make-number-stack-pointer-tn ()
78 (make-restricted-tn *fixnum-primitive-type* ignore-me-sc-number))
80 ;;; Return a list of TNs that can be used to represent an unknown-values
81 ;;; continuation within a function.
82 (!def-vm-support-routine make-unknown-values-locations ()
83 (list (make-stack-pointer-tn)
84 (make-normal-tn *fixnum-primitive-type*)))
86 ;;; This function is called by the ENTRY-ANALYZE phase, allowing
87 ;;; VM-dependent initialization of the IR2-COMPONENT structure. We
88 ;;; push placeholder entries in the CONSTANTS to leave room for
89 ;;; additional noise in the code object header.
90 (!def-vm-support-routine select-component-format (component)
91 (declare (type component component))
92 ;; The 1+ here is because for the x86 the first constant is a
93 ;; pointer to a list of fixups, or NIL if the code object has none.
94 ;; (If I understand correctly, the fixups are needed at GC copy
95 ;; time because the X86 code isn't relocatable.)
97 ;; KLUDGE: It'd be cleaner to have the fixups entry be a named
98 ;; element of the CODE (aka component) primitive object. However,
99 ;; it's currently a large, tricky, error-prone chore to change
100 ;; the layout of any primitive object, so for the foreseeable future
101 ;; we'll just live with this ugliness. -- WHN 2002-01-02
102 (dotimes (i (1+ code-constants-offset))
103 (vector-push-extend nil
104 (ir2-component-constants (component-info component))))
109 ;;; This is used for setting up the Old-FP in local call.
110 (define-vop (current-fp)
111 (:results (val :scs (any-reg control-stack)))
115 ;;; We don't have a separate NFP, so we don't need to do anything here.
116 (define-vop (compute-old-nfp)
122 (define-vop (xep-allocate-frame)
123 (:info start-lab copy-more-arg-follows)
126 (emit-alignment n-lowtag-bits)
127 (trace-table-entry trace-table-fun-prologue)
128 (emit-label start-lab)
129 ;; Skip space for the function header.
130 (inst simple-fun-header-word)
131 (dotimes (i (1- simple-fun-code-offset))
134 ;; The start of the actual code.
135 ;; Save the return-pc.
136 (popw ebp-tn (frame-word-offset return-pc-save-offset))
138 ;; If copy-more-arg follows it will allocate the correct stack
139 ;; size. The stack is not allocated first here as this may expose
140 ;; args on the stack if they take up more space than the frame!
141 (unless copy-more-arg-follows
142 ;; The args fit within the frame so just allocate the frame.
144 (make-ea :dword :base ebp-tn
145 :disp (- (* n-word-bytes
146 (- (max 3 (sb-allocated-size 'stack))
149 (trace-table-entry trace-table-normal)))
151 ;;; This is emitted directly before either a known-call-local, call-local,
152 ;;; or a multiple-call-local. All it does is allocate stack space for the
153 ;;; callee (who has the same size stack as us).
154 (define-vop (allocate-frame)
155 (:results (res :scs (any-reg))
160 (inst lea res (make-ea :dword :base esp-tn
161 :disp (- (* sp->fp-offset n-word-bytes))))
162 (inst sub esp-tn (* n-word-bytes (sb-allocated-size 'stack)))))
164 ;;; Allocate a partial frame for passing stack arguments in a full
165 ;;; call. NARGS is the number of arguments passed. We allocate at
166 ;;; least 3 slots, because the XEP noise is going to want to use them
167 ;;; before it can extend the stack.
168 (define-vop (allocate-full-call-frame)
170 (:results (res :scs (any-reg)))
172 (inst lea res (make-ea :dword :base esp-tn
173 :disp (- (* sp->fp-offset n-word-bytes))))
174 (inst sub esp-tn (* (max nargs 3) n-word-bytes))))
176 ;;; Emit code needed at the return-point from an unknown-values call
177 ;;; for a fixed number of values. Values is the head of the TN-REF
178 ;;; list for the locations that the values are to be received into.
179 ;;; Nvals is the number of values that are to be received (should
180 ;;; equal the length of Values).
182 ;;; If 0 or 1 values are expected, then we just emit an instruction to
183 ;;; reset the SP (which will only be executed when other than 1 value
186 ;;; In the general case we have to do three things:
187 ;;; -- Default unsupplied register values. This need only be done
188 ;;; when a single value is returned, since register values are
189 ;;; defaulted by the called in the non-single case.
190 ;;; -- Default unsupplied stack values. This needs to be done whenever
191 ;;; there are stack values.
192 ;;; -- Reset SP. This must be done whenever other than 1 value is
193 ;;; returned, regardless of the number of values desired.
194 (defun default-unknown-values (vop values nvals)
195 (declare (type (or tn-ref null) values)
196 (type unsigned-byte nvals))
199 (note-this-location vop :single-value-return)
201 ((member :cmov *backend-subfeatures*)
202 (inst cmov :c esp-tn ebx-tn))
204 (let ((single-value (gen-label)))
205 (inst jmp :nc single-value)
206 (inst mov esp-tn ebx-tn)
207 (emit-label single-value)))))
208 ((<= nvals register-arg-count)
209 (let ((regs-defaulted (gen-label)))
210 (note-this-location vop :unknown-return)
211 (inst jmp :c regs-defaulted)
212 ;; Default the unsupplied registers.
213 (let* ((2nd-tn-ref (tn-ref-across values))
214 (2nd-tn (tn-ref-tn 2nd-tn-ref)))
215 (inst mov 2nd-tn nil-value)
218 for tn-ref = (tn-ref-across 2nd-tn-ref)
219 then (tn-ref-across tn-ref)
220 for count from 2 below register-arg-count
221 do (inst mov (tn-ref-tn tn-ref) 2nd-tn))))
222 (inst mov ebx-tn esp-tn)
223 (emit-label regs-defaulted)
224 (inst mov esp-tn ebx-tn)))
226 ;; The number of bytes depends on the relative jump instructions.
227 ;; Best case is 31+(n-3)*14, worst case is 35+(n-3)*18. For
228 ;; NVALS=6 that is 73/89 bytes, and for NVALS=7 that is 87/107
229 ;; bytes which is likely better than using the blt below.
230 (let ((regs-defaulted (gen-label))
231 (defaulting-done (gen-label))
232 (default-stack-slots (gen-label)))
233 (note-this-location vop :unknown-return)
234 ;; Branch off to the MV case.
235 (inst jmp :c regs-defaulted)
236 ;; Do the single value case.
237 ;; Default the register args
238 (inst mov eax-tn nil-value)
240 (val (tn-ref-across values) (tn-ref-across val)))
241 ((= i (min nvals register-arg-count)))
242 (inst mov (tn-ref-tn val) eax-tn))
244 ;; Fake other registers so it looks like we returned with all the
245 ;; registers filled in.
247 (inst jmp default-stack-slots)
249 (emit-label regs-defaulted)
251 (inst mov eax-tn nil-value)
252 (collect ((defaults))
253 (do ((i register-arg-count (1+ i))
254 (val (do ((i 0 (1+ i))
255 (val values (tn-ref-across val)))
256 ((= i register-arg-count) val))
257 (tn-ref-across val)))
259 (let ((default-lab (gen-label))
261 (first-stack-arg-p (= i register-arg-count)))
262 (defaults (cons default-lab (cons tn first-stack-arg-p)))
264 (inst cmp ecx-tn (fixnumize i))
265 (inst jmp :be default-lab)
266 (when first-stack-arg-p
267 ;; There are stack args so the frame of the callee is
268 ;; still there, save EDX in its first slot temporalily.
269 (storew edx-tn ebx-tn (frame-word-offset sp->fp-offset)))
270 (loadw edx-tn ebx-tn (frame-word-offset (+ sp->fp-offset i)))
271 (inst mov tn edx-tn)))
273 (emit-label defaulting-done)
274 (loadw edx-tn ebx-tn (frame-word-offset sp->fp-offset))
277 (let ((defaults (defaults)))
279 (assemble (*elsewhere*)
280 (trace-table-entry trace-table-fun-prologue)
281 (emit-label default-stack-slots)
282 (dolist (default defaults)
283 (emit-label (car default))
285 ;; We are setting the first stack argument to NIL.
286 ;; The callee's stack frame is dead, save EDX by
287 ;; pushing it to the stack, it will end up at same
288 ;; place as in the (STOREW EDX-TN EBX-TN -1) case
291 (inst mov (second default) eax-tn))
292 (inst jmp defaulting-done)
293 (trace-table-entry trace-table-normal)))))))
295 ;; 91 bytes for this branch.
296 (let ((regs-defaulted (gen-label))
297 (restore-edi (gen-label))
298 (no-stack-args (gen-label))
299 (default-stack-vals (gen-label))
300 (count-okay (gen-label)))
301 (note-this-location vop :unknown-return)
302 ;; Branch off to the MV case.
303 (inst jmp :c regs-defaulted)
305 ;; Default the register args, and set up the stack as if we
306 ;; entered the MV return point.
307 (inst mov ebx-tn esp-tn)
308 (inst mov edi-tn nil-value)
309 (inst mov esi-tn edi-tn)
310 ;; Compute a pointer to where to put the [defaulted] stack values.
311 (emit-label no-stack-args)
315 (make-ea :dword :base ebp-tn
316 :disp (frame-byte-offset register-arg-count)))
317 ;; Load EAX with NIL so we can quickly store it, and set up
318 ;; stuff for the loop.
319 (inst mov eax-tn nil-value)
321 (inst mov ecx-tn (- nvals register-arg-count))
322 ;; Jump into the default loop.
323 (inst jmp default-stack-vals)
325 ;; The regs are defaulted. We need to copy any stack arguments,
326 ;; and then default the remaining stack arguments.
327 (emit-label regs-defaulted)
329 (storew edi-tn ebx-tn (frame-word-offset (+ sp->fp-offset 1)))
330 ;; Compute the number of stack arguments, and if it's zero or
331 ;; less, don't copy any stack arguments.
332 (inst sub ecx-tn (fixnumize register-arg-count))
333 (inst jmp :le no-stack-args)
335 ;; Throw away any unwanted args.
336 (inst cmp ecx-tn (fixnumize (- nvals register-arg-count)))
337 (inst jmp :be count-okay)
338 (inst mov ecx-tn (fixnumize (- nvals register-arg-count)))
339 (emit-label count-okay)
340 ;; Save the number of stack values.
341 (inst mov eax-tn ecx-tn)
342 ;; Compute a pointer to where the stack args go.
344 (make-ea :dword :base ebp-tn
345 :disp (frame-byte-offset register-arg-count)))
346 ;; Save ESI, and compute a pointer to where the args come from.
347 (storew esi-tn ebx-tn (frame-word-offset (+ sp->fp-offset 2)))
349 (make-ea :dword :base ebx-tn
350 :disp (frame-byte-offset
351 (+ sp->fp-offset register-arg-count))))
353 (inst shr ecx-tn word-shift) ; make word count
358 (loadw esi-tn ebx-tn (frame-word-offset (+ sp->fp-offset 2)))
359 ;; Now we have to default the remaining args. Find out how many.
360 (inst sub eax-tn (fixnumize (- nvals register-arg-count)))
362 ;; If none, then just blow out of here.
363 (inst jmp :le restore-edi)
364 (inst mov ecx-tn eax-tn)
365 (inst shr ecx-tn word-shift) ; word count
366 ;; Load EAX with NIL for fast storing.
367 (inst mov eax-tn nil-value)
369 (emit-label default-stack-vals)
372 ;; Restore EDI, and reset the stack.
373 (emit-label restore-edi)
374 (loadw edi-tn ebx-tn (frame-word-offset (+ sp->fp-offset 1)))
375 (inst mov esp-tn ebx-tn)
379 ;;;; unknown values receiving
381 ;;; Emit code needed at the return point for an unknown-values call
382 ;;; for an arbitrary number of values.
384 ;;; We do the single and non-single cases with no shared code: there
385 ;;; doesn't seem to be any potential overlap, and receiving a single
386 ;;; value is more important efficiency-wise.
388 ;;; When there is a single value, we just push it on the stack,
389 ;;; returning the old SP and 1.
391 ;;; When there is a variable number of values, we move all of the
392 ;;; argument registers onto the stack, and return ARGS and NARGS.
394 ;;; ARGS and NARGS are TNs wired to the named locations. We must
395 ;;; explicitly allocate these TNs, since their lifetimes overlap with
396 ;;; the results start and count. (Also, it's nice to be able to target
398 (defun receive-unknown-values (args nargs start count)
399 (declare (type tn args nargs start count))
400 (let ((variable-values (gen-label))
401 (stack-values (gen-label))
403 (inst jmp :c variable-values)
405 (cond ((location= start (first *register-arg-tns*))
406 (inst push (first *register-arg-tns*))
407 (inst lea start (make-ea :dword :base esp-tn :disp n-word-bytes)))
408 (t (inst mov start esp-tn)
409 (inst push (first *register-arg-tns*))))
410 (inst mov count (fixnumize 1))
413 (emit-label variable-values)
414 ;; The stack frame is burnt and RETurned from if there are no
415 ;; stack values. In this case quickly reallocate sufficient space.
416 (inst cmp nargs (fixnumize register-arg-count))
417 (inst jmp :g stack-values)
418 (inst sub esp-tn nargs)
419 (emit-label stack-values)
420 ;; dtc: this writes the registers onto the stack even if they are
421 ;; not needed, only the number specified in ecx are used and have
422 ;; stack allocated to them. No harm is done.
424 for arg in *register-arg-tns*
426 do (storew arg args i))
433 ;;; VOP that can be inherited by unknown values receivers. The main thing this
434 ;;; handles is allocation of the result temporaries.
435 (define-vop (unknown-values-receiver)
436 (:temporary (:sc descriptor-reg :offset ebx-offset
437 :from :eval :to (:result 0))
439 (:temporary (:sc any-reg :offset ecx-offset
440 :from :eval :to (:result 1))
442 (:results (start :scs (any-reg control-stack))
443 (count :scs (any-reg control-stack))))
445 ;;;; local call with unknown values convention return
447 (defun check-ocfp-and-return-pc (old-fp return-pc)
449 (format t "*known-return: old-fp ~S, tn-kind ~S; ~S ~S~%"
450 old-fp (sb!c::tn-kind old-fp) (sb!c::tn-save-tn old-fp)
451 (sb!c::tn-kind (sb!c::tn-save-tn old-fp)))
453 (format t "*known-return: return-pc ~S, tn-kind ~S; ~S ~S~%"
454 return-pc (sb!c::tn-kind return-pc)
455 (sb!c::tn-save-tn return-pc)
456 (sb!c::tn-kind (sb!c::tn-save-tn return-pc)))
457 (unless (and (sc-is old-fp control-stack)
458 (= (tn-offset old-fp) ocfp-save-offset))
459 (error "ocfp not on stack in standard save location?"))
460 (unless (and (sc-is return-pc sap-stack)
461 (= (tn-offset return-pc) return-pc-save-offset))
462 (error "return-pc not on stack in standard save location?")))
464 ;;; Non-TR local call for a fixed number of values passed according to
465 ;;; the unknown values convention.
467 ;;; FP is the frame pointer in install before doing the call.
469 ;;; NFP would be the number-stack frame pointer if we had a separate
472 ;;; Args are the argument passing locations, which are specified only
473 ;;; to terminate their lifetimes in the caller.
475 ;;; VALUES are the return value locations (wired to the standard
476 ;;; passing locations). NVALS is the number of values received.
478 ;;; Save is the save info, which we can ignore since saving has been
481 ;;; TARGET is a continuation pointing to the start of the called
483 (define-vop (call-local)
487 (:results (values :more t))
489 (:move-args :local-call)
490 (:info arg-locs callee target nvals)
492 (:ignore nfp arg-locs args #+nil callee)
494 (trace-table-entry trace-table-call-site)
497 (let ((ret-tn (callee-return-pc-tn callee)))
499 (format t "*call-local ~S; tn-kind ~S; tn-save-tn ~S; its tn-kind ~S~%"
500 ret-tn (sb!c::tn-kind ret-tn) (sb!c::tn-save-tn ret-tn)
501 (sb!c::tn-kind (sb!c::tn-save-tn ret-tn)))
503 ;; Is the return-pc on the stack or in a register?
506 (unless (= (tn-offset ret-tn) return-pc-save-offset)
507 (error "ret-tn ~A in wrong stack slot" ret-tn))
508 #+nil (format t "*call-local: ret-tn on stack; offset=~S~%"
510 (storew (make-fixup nil :code-object RETURN)
511 ebp-tn (frame-word-offset (tn-offset ret-tn))))
513 (error "ret-tn ~A in sap-reg" ret-tn))))
515 (note-this-location vop :call-site)
518 (default-unknown-values vop values nvals)
519 (trace-table-entry trace-table-normal)))
521 ;;; Non-TR local call for a variable number of return values passed according
522 ;;; to the unknown values convention. The results are the start of the values
523 ;;; glob and the number of values received.
524 (define-vop (multiple-call-local unknown-values-receiver)
529 (:move-args :local-call)
530 (:info save callee target)
531 (:ignore args save nfp #+nil callee)
534 (trace-table-entry trace-table-call-site)
537 (let ((ret-tn (callee-return-pc-tn callee)))
539 (format t "*multiple-call-local ~S; tn-kind ~S; tn-save-tn ~S; its tn-kind ~S~%"
540 ret-tn (sb!c::tn-kind ret-tn) (sb!c::tn-save-tn ret-tn)
541 (sb!c::tn-kind (sb!c::tn-save-tn ret-tn)))
543 ;; Is the return-pc on the stack or in a register?
546 #+nil (format t "*multiple-call-local: ret-tn on stack; offset=~S~%"
549 (storew (make-fixup nil :code-object RETURN)
550 ebp-tn (frame-word-offset (tn-offset ret-tn))))
552 (error "multiple-call-local: return-pc not on stack."))))
554 (note-this-location vop :call-site)
557 (note-this-location vop :unknown-return)
558 (receive-unknown-values values-start nvals start count)
559 (trace-table-entry trace-table-normal)))
561 ;;;; local call with known values return
563 ;;; Non-TR local call with known return locations. Known-value return
564 ;;; works just like argument passing in local call.
566 ;;; Note: we can't use normal load-tn allocation for the fixed args,
567 ;;; since all registers may be tied up by the more operand. Instead,
568 ;;; we use MAYBE-LOAD-STACK-TN.
569 (define-vop (known-call-local)
573 (:results (res :more t))
574 (:move-args :local-call)
576 (:info save callee target)
577 (:ignore args res save nfp #+nil callee)
580 (trace-table-entry trace-table-call-site)
583 (let ((ret-tn (callee-return-pc-tn callee)))
586 (format t "*known-call-local ~S; tn-kind ~S; tn-save-tn ~S; its tn-kind ~S~%"
587 ret-tn (sb!c::tn-kind ret-tn) (sb!c::tn-save-tn ret-tn)
588 (sb!c::tn-kind (sb!c::tn-save-tn ret-tn)))
590 ;; Is the return-pc on the stack or in a register?
593 #+nil (format t "*known-call-local: ret-tn on stack; offset=~S~%"
596 (storew (make-fixup nil :code-object RETURN)
597 ebp-tn (frame-word-offset (tn-offset ret-tn))))
599 (error "known-call-local: return-pc not on stack."))))
601 (note-this-location vop :call-site)
604 (note-this-location vop :known-return)
605 (trace-table-entry trace-table-normal)))
607 ;;; From Douglas Crosher
608 ;;; Return from known values call. We receive the return locations as
609 ;;; arguments to terminate their lifetimes in the returning function. We
610 ;;; restore FP and CSP and jump to the Return-PC.
611 (define-vop (known-return)
615 (:move-args :known-return)
617 (:ignore val-locs vals)
620 (check-ocfp-and-return-pc old-fp return-pc)
621 (trace-table-entry trace-table-fun-epilogue)
622 ;; Zot all of the stack except for the old-fp and return-pc.
623 (inst mov esp-tn ebp-tn)
626 (trace-table-entry trace-table-normal)))
630 ;;; There is something of a cross-product effect with full calls.
631 ;;; Different versions are used depending on whether we know the
632 ;;; number of arguments or the name of the called function, and
633 ;;; whether we want fixed values, unknown values, or a tail call.
635 ;;; In full call, the arguments are passed creating a partial frame on
636 ;;; the stack top and storing stack arguments into that frame. On
637 ;;; entry to the callee, this partial frame is pointed to by FP.
639 ;;; This macro helps in the definition of full call VOPs by avoiding
640 ;;; code replication in defining the cross-product VOPs.
642 ;;; NAME is the name of the VOP to define.
644 ;;; NAMED is true if the first argument is an fdefinition object whose
645 ;;; definition is to be called.
647 ;;; RETURN is either :FIXED, :UNKNOWN or :TAIL:
648 ;;; -- If :FIXED, then the call is for a fixed number of values, returned in
649 ;;; the standard passing locations (passed as result operands).
650 ;;; -- If :UNKNOWN, then the result values are pushed on the stack, and the
651 ;;; result values are specified by the Start and Count as in the
652 ;;; unknown-values continuation representation.
653 ;;; -- If :TAIL, then do a tail-recursive call. No values are returned.
654 ;;; The Old-Fp and Return-PC are passed as the second and third arguments.
656 ;;; In non-tail calls, the pointer to the stack arguments is passed as
657 ;;; the last fixed argument. If Variable is false, then the passing
658 ;;; locations are passed as a more arg. Variable is true if there are
659 ;;; a variable number of arguments passed on the stack. Variable
660 ;;; cannot be specified with :TAIL return. TR variable argument call
661 ;;; is implemented separately.
663 ;;; In tail call with fixed arguments, the passing locations are
664 ;;; passed as a more arg, but there is no new-FP, since the arguments
665 ;;; have been set up in the current frame.
666 (macrolet ((define-full-call (name named return variable)
667 (aver (not (and variable (eq return :tail))))
669 ,@(when (eq return :unknown)
670 '(unknown-values-receiver)))
672 ,@(unless (eq return :tail)
673 '((new-fp :scs (any-reg) :to (:argument 1))))
675 (fun :scs (descriptor-reg control-stack)
676 :target eax :to (:argument 0))
678 ,@(when (eq return :tail)
682 ,@(unless variable '((args :more t :scs (descriptor-reg)))))
684 ,@(when (eq return :fixed)
685 '((:results (values :more t))))
687 (:save-p ,(if (eq return :tail) :compute-only t))
689 ,@(unless (or (eq return :tail) variable)
690 '((:move-args :full-call)))
694 ,@(unless (or variable (eq return :tail)) '(arg-locs))
695 ,@(unless variable '(nargs))
696 ,@(when (eq return :fixed) '(nvals))
700 ,@(unless (or variable (eq return :tail)) '(arg-locs))
701 ,@(unless variable '(args)))
703 ;; We pass either the fdefn object (for named call) or
704 ;; the actual function object (for unnamed call) in
705 ;; EAX. With named call, closure-tramp will replace it
706 ;; with the real function and invoke the real function
707 ;; for closures. Non-closures do not need this value,
708 ;; so don't care what shows up in it.
716 ;; We pass the number of arguments in ECX.
717 (:temporary (:sc unsigned-reg :offset ecx-offset :to :eval) ecx)
719 ;; With variable call, we have to load the
720 ;; register-args out of the (new) stack frame before
721 ;; doing the call. Therefore, we have to tell the
722 ;; lifetime stuff that we need to use them.
724 (mapcar (lambda (name offset)
725 `(:temporary (:sc descriptor-reg
730 *register-arg-names* *register-arg-offsets*))
732 ,@(when (eq return :tail)
733 '((:temporary (:sc unsigned-reg
738 (:generator ,(+ (if named 5 0)
740 (if (eq return :tail) 0 10)
742 (if (eq return :unknown) 25 0))
743 (trace-table-entry trace-table-call-site)
745 ;; This has to be done before the frame pointer is
746 ;; changed! EAX stores the 'lexical environment' needed
752 ;; For variable call, compute the number of
753 ;; arguments and move some of the arguments to
756 ;; Compute the number of arguments.
757 (noise '(inst mov ecx new-fp))
758 (noise '(inst sub ecx esp-tn))
759 ;; Move the necessary args to registers,
760 ;; this moves them all even if they are
763 for name in *register-arg-names*
764 for index downfrom -1
765 do (noise `(loadw ,name new-fp ,index)))
769 (inst mov ecx (fixnumize nargs)))))
770 ,@(cond ((eq return :tail)
771 '(;; Python has figured out what frame we should
772 ;; return to so might as well use that clue.
773 ;; This seems really important to the
774 ;; implementation of things like
775 ;; (without-interrupts ...)
777 ;; dtc; Could be doing a tail call from a
778 ;; known-local-call etc in which the old-fp
779 ;; or ret-pc are in regs or in non-standard
780 ;; places. If the passing location were
781 ;; wired to the stack in standard locations
782 ;; then these moves will be un-necessary;
783 ;; this is probably best for the x86.
786 (unless (= ocfp-save-offset
788 ;; FIXME: FORMAT T for stale
789 ;; diagnostic output (several of
790 ;; them around here), ick
791 (error "** tail-call old-fp not S0~%")
792 (move old-fp-tmp old-fp)
795 (frame-word-offset ocfp-save-offset))))
796 ((any-reg descriptor-reg)
797 (error "** tail-call old-fp in reg not S0~%")
800 (frame-word-offset ocfp-save-offset))))
802 ;; For tail call, we have to push the
803 ;; return-pc so that it looks like we CALLed
804 ;; despite the fact that we are going to JMP.
805 (inst push return-pc)
808 ;; For non-tail call, we have to save our
809 ;; frame pointer and install the new frame
810 ;; pointer. We can't load stack tns after this
812 `(;; Python doesn't seem to allocate a frame
813 ;; here which doesn't leave room for the
816 ;; The variable args are on the stack and
817 ;; become the frame, but there may be <3
818 ;; args and 3 stack slots are assumed
819 ;; allocate on the call. So need to ensure
820 ;; there are at least 3 slots. This hack
823 '(inst sub esp-tn (fixnumize 3)))
825 ;; Bias the new-fp for use as an fp
827 '(inst sub new-fp (fixnumize sp->fp-offset)))
830 (storew ebp-tn new-fp
831 (frame-word-offset ocfp-save-offset))
833 (move ebp-tn new-fp) ; NB - now on new stack frame.
836 (when step-instrumenting
837 (emit-single-step-test)
839 (inst break single-step-around-trap))
842 (note-this-location vop :call-site)
844 (inst ,(if (eq return :tail) 'jmp 'call)
846 '(make-ea-for-object-slot eax fdefn-raw-addr-slot
847 other-pointer-lowtag)
848 '(make-ea-for-object-slot eax closure-fun-slot
849 fun-pointer-lowtag)))
852 '((default-unknown-values vop values nvals)))
854 '((note-this-location vop :unknown-return)
855 (receive-unknown-values values-start nvals start count)))
857 (trace-table-entry trace-table-normal)))))
859 (define-full-call call nil :fixed nil)
860 (define-full-call call-named t :fixed nil)
861 (define-full-call multiple-call nil :unknown nil)
862 (define-full-call multiple-call-named t :unknown nil)
863 (define-full-call tail-call nil :tail nil)
864 (define-full-call tail-call-named t :tail nil)
866 (define-full-call call-variable nil :fixed t)
867 (define-full-call multiple-call-variable nil :unknown t))
869 ;;; This is defined separately, since it needs special code that BLT's
870 ;;; the arguments down. All the real work is done in the assembly
871 ;;; routine. We just set things up so that it can find what it needs.
872 (define-vop (tail-call-variable)
873 (:args (args :scs (any-reg control-stack) :target esi)
874 (function :scs (descriptor-reg control-stack) :target eax)
877 (:temporary (:sc unsigned-reg :offset esi-offset :from (:argument 0)) esi)
878 (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 1)) eax)
880 (check-ocfp-and-return-pc old-fp return-pc)
881 ;; Move these into the passing locations if they are not already there.
884 ;; And jump to the assembly routine.
885 (inst jmp (make-fixup 'tail-call-variable :assembly-routine))))
887 ;;;; unknown values return
889 ;;; Return a single-value using the Unknown-Values convention.
891 ;;; pfw--get wired-tn conflicts sometimes if register sc specd for args
892 ;;; having problems targeting args to regs -- using temps instead.
894 ;;; First off, modifying the return-pc defeats the branch-prediction
895 ;;; optimizations on modern CPUs quite handily. Second, we can do all
896 ;;; this without needing a temp register. Fixed the latter, at least.
897 ;;; -- AB 2006/Feb/04
898 (define-vop (return-single)
904 (check-ocfp-and-return-pc old-fp return-pc)
905 (trace-table-entry trace-table-fun-epilogue)
906 ;; Drop stack above old-fp
907 (inst mov esp-tn ebp-tn)
908 ;; Clear the multiple-value return flag
910 ;; Restore the old frame pointer
915 ;;; Do unknown-values return of a fixed (other than 1) number of
916 ;;; values. The VALUES are required to be set up in the standard
917 ;;; passing locations. NVALS is the number of values returned.
919 ;;; Basically, we just load ECX with the number of values returned and
920 ;;; EBX with a pointer to the values, set ESP to point to the end of
921 ;;; the values, and jump directly to return-pc.
924 (return-pc :to (:eval 1))
928 ;; In the case of other than one value, we need these registers to
929 ;; tell the caller where they are and how many there are.
930 (:temporary (:sc unsigned-reg :offset ebx-offset) ebx)
931 (:temporary (:sc unsigned-reg :offset ecx-offset) ecx)
932 ;; We need to stretch the lifetime of return-pc past the argument
933 ;; registers so that we can default the argument registers without
934 ;; trashing return-pc.
935 (:temporary (:sc unsigned-reg :offset (first *register-arg-offsets*)
937 (:temporary (:sc unsigned-reg :offset (second *register-arg-offsets*)
939 (:temporary (:sc unsigned-reg :offset (third *register-arg-offsets*)
943 (check-ocfp-and-return-pc old-fp return-pc)
945 ;; This is handled in RETURN-SINGLE.
946 (error "nvalues is 1"))
947 (trace-table-entry trace-table-fun-epilogue)
948 ;; Establish the values pointer and values count.
949 (inst lea ebx (make-ea :dword :base ebp-tn
950 :disp (* sp->fp-offset n-word-bytes)))
952 (inst xor ecx ecx) ; smaller
953 (inst mov ecx (fixnumize nvals)))
954 ;; Pre-default any argument register that need it.
955 (when (< nvals register-arg-count)
956 (let* ((arg-tns (nthcdr nvals (list a0 a1 a2)))
957 (first (first arg-tns)))
958 (inst mov first nil-value)
959 (dolist (tn (cdr arg-tns))
960 (inst mov tn first))))
961 ;; Set the multiple value return flag.
963 ;; And away we go. Except that return-pc is still on the
964 ;; stack and we've changed the stack pointer. So we have to
965 ;; tell it to index off of EBX instead of EBP.
966 (cond ((<= nvals register-arg-count)
967 (inst mov esp-tn ebp-tn)
971 ;; Some values are on the stack after RETURN-PC and OLD-FP,
972 ;; can't return normally and some slots of the frame will
973 ;; be used as temporaries by the receiver.
975 ;; Clear as much of the stack as possible, but not past the
976 ;; old frame address.
978 (make-ea :dword :base ebp-tn
979 :disp (frame-byte-offset (1- nvals))))
981 (inst push (make-ea :dword :base ebx
982 :disp (frame-byte-offset
984 (tn-offset return-pc)))))
987 (trace-table-entry trace-table-normal)))
989 ;;; Do unknown-values return of an arbitrary number of values (passed
990 ;;; on the stack.) We check for the common case of a single return
991 ;;; value, and do that inline using the normal single value return
992 ;;; convention. Otherwise, we branch off to code that calls an
993 ;;; assembly-routine.
995 ;;; The assembly routine takes the following args:
996 ;;; ECX -- number of values to find there.
997 ;;; ESI -- pointer to where to find the values.
998 (define-vop (return-multiple)
1001 (vals :scs (any-reg) :target esi)
1002 (nvals :scs (any-reg) :target ecx))
1003 (:temporary (:sc unsigned-reg :offset esi-offset :from (:argument 2)) esi)
1004 (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 3)) ecx)
1005 (:temporary (:sc descriptor-reg :offset (first *register-arg-offsets*)
1006 :from (:eval 0)) a0)
1009 (check-ocfp-and-return-pc old-fp return-pc)
1010 (trace-table-entry trace-table-fun-epilogue)
1011 (unless (policy node (> space speed))
1012 ;; Check for the single case.
1013 (let ((not-single (gen-label)))
1014 (inst cmp nvals (fixnumize 1))
1015 (inst jmp :ne not-single)
1016 ;; Return with one value.
1018 ;; Clear the stack until ocfp.
1019 (inst mov esp-tn ebp-tn)
1020 ;; clear the multiple-value return flag
1025 ;; Nope, not the single case. Jump to the assembly routine.
1026 (emit-label not-single)))
1029 (inst jmp (make-fixup 'return-multiple :assembly-routine))
1030 (trace-table-entry trace-table-normal)))
1034 ;;; We don't need to do anything special for regular functions.
1035 (define-vop (setup-environment)
1039 ;; Don't bother doing anything.
1042 ;;; Get the lexical environment from its passing location.
1043 (define-vop (setup-closure-environment)
1044 (:results (closure :scs (descriptor-reg)))
1049 (move closure eax-tn)))
1051 ;;; Copy a &MORE arg from the argument area to the end of the current
1052 ;;; frame. FIXED is the number of non-&MORE arguments.
1054 ;;; The tricky part is doing this without trashing any of the calling
1055 ;;; convention registers that are still needed. This vop is emitted
1056 ;;; directly after the xep-allocate frame. That means the registers
1057 ;;; are in use as follows:
1059 ;;; EAX -- The lexenv.
1060 ;;; EBX -- Available.
1061 ;;; ECX -- The total number of arguments.
1062 ;;; EDX -- The first arg.
1063 ;;; EDI -- The second arg.
1064 ;;; ESI -- The third arg.
1066 ;;; So basically, we have one register available for our use: EBX.
1068 ;;; What we can do is push the other regs onto the stack, and then
1069 ;;; restore their values by looking directly below where we put the
1071 (define-vop (copy-more-arg)
1074 ;; Avoid the copy if there are no more args.
1075 (cond ((zerop fixed)
1076 (inst jecxz JUST-ALLOC-FRAME))
1078 (inst cmp ecx-tn (fixnumize fixed))
1079 (inst jmp :be JUST-ALLOC-FRAME)))
1081 ;; Allocate the space on the stack.
1082 ;; stack = ebp + sp->fp-offset - (max 3 frame-size) - (nargs - fixed)
1084 (make-ea :dword :base ebp-tn
1085 :disp (* n-word-bytes
1086 (- (+ sp->fp-offset fixed)
1087 (max 3 (sb-allocated-size 'stack))))))
1088 (inst sub ebx-tn ecx-tn) ; Got the new stack in ebx
1089 (inst mov esp-tn ebx-tn)
1091 ;; Now: nargs>=1 && nargs>fixed
1093 ;; Save the original count of args.
1094 (inst mov ebx-tn ecx-tn)
1096 (cond ((< fixed register-arg-count)
1097 ;; We must stop when we run out of stack args, not when we
1098 ;; run out of more args.
1099 ;; Number to copy = nargs-3
1100 (inst sub ecx-tn (fixnumize register-arg-count))
1101 ;; Everything of interest in registers.
1102 (inst jmp :be DO-REGS))
1104 ;; Number to copy = nargs-fixed
1105 (inst sub ecx-tn (fixnumize fixed))))
1107 ;; Save edi and esi register args.
1111 ;; Okay, we have pushed the register args. We can trash them
1114 ;; Initialize src to be end of args.
1115 (inst lea esi-tn (make-ea :dword :base ebp-tn
1116 :disp (* sp->fp-offset n-word-bytes)))
1117 (inst sub esi-tn ebx-tn)
1119 ;; We need to copy from downwards up to avoid overwriting some of
1120 ;; the yet uncopied args. So we need to use EBX as the copy index
1121 ;; and ECX as the loop counter, rather than using ECX for both.
1122 (inst xor ebx-tn ebx-tn)
1124 ;; We used to use REP MOVS here, but on modern x86 it performs
1125 ;; much worse than an explicit loop for small blocks.
1127 (inst mov edi-tn (make-ea :dword :base esi-tn :index ebx-tn))
1128 ;; The :DISP is to account for the registers saved on the stack
1129 (inst mov (make-ea :dword :base esp-tn :disp (* 3 n-word-bytes)
1132 (inst add ebx-tn n-word-bytes)
1133 (inst sub ecx-tn n-word-bytes)
1134 (inst jmp :nz COPY-LOOP)
1136 ;; So now we need to restore EDI and ESI.
1144 (inst mov ecx-tn ebx-tn)
1146 ;; Here: nargs>=1 && nargs>fixed
1147 (when (< fixed register-arg-count)
1148 ;; Now we have to deposit any more args that showed up in
1152 ;; Store it relative to ebp
1153 (inst mov (make-ea :dword :base ebp-tn
1154 :disp (* n-word-bytes
1158 (max 3 (sb-allocated-size
1160 (nth i *register-arg-tns*))
1163 (when (>= i register-arg-count)
1166 ;; Don't deposit any more than there are.
1168 (inst test ecx-tn ecx-tn)
1169 (inst cmp ecx-tn (fixnumize i)))
1170 (inst jmp :eq DONE)))
1176 (make-ea :dword :base ebp-tn
1177 :disp (* n-word-bytes
1179 (max 3 (sb-allocated-size 'stack))))))
1183 (define-vop (more-kw-arg)
1184 (:translate sb!c::%more-kw-arg)
1185 (:policy :fast-safe)
1186 (:args (object :scs (descriptor-reg) :to (:result 1))
1187 (index :scs (any-reg immediate) :to (:result 1) :target keyword))
1188 (:arg-types * tagged-num)
1189 (:results (value :scs (descriptor-reg any-reg))
1190 (keyword :scs (descriptor-reg any-reg)))
1195 (inst mov value (make-ea :dword :base object :disp (tn-value index)))
1196 (inst mov keyword (make-ea :dword :base object
1197 :disp (+ (tn-value index) n-word-bytes))))
1199 (inst mov value (make-ea :dword :base object :index index))
1200 (inst mov keyword (make-ea :dword :base object :index index
1201 :disp n-word-bytes))))))
1203 (define-vop (more-arg)
1204 (:translate sb!c::%more-arg)
1205 (:policy :fast-safe)
1206 (:args (object :scs (descriptor-reg) :to (:result 1))
1207 (index :scs (any-reg) :to (:result 1) :target value))
1208 (:arg-types * tagged-num)
1209 (:results (value :scs (descriptor-reg any-reg)))
1214 (inst mov value (make-ea :dword :base object :index value))))
1216 ;;; Turn more arg (context, count) into a list.
1217 (define-vop (listify-rest-args)
1218 (:translate %listify-rest-args)
1220 (:args (context :scs (descriptor-reg) :target src)
1221 (count :scs (any-reg) :target ecx))
1222 (:arg-types * tagged-num)
1223 (:temporary (:sc unsigned-reg :offset esi-offset :from (:argument 0)) src)
1224 (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 1)) ecx)
1225 (:temporary (:sc unsigned-reg :offset eax-offset) eax)
1226 (:temporary (:sc unsigned-reg) dst)
1227 (:results (result :scs (descriptor-reg)))
1230 (let ((enter (gen-label))
1233 (stack-allocate-p (node-stack-allocate-p node)))
1236 ;; Check to see whether there are no args, and just return NIL if so.
1237 (inst mov result nil-value)
1239 (inst lea dst (make-ea :dword :base ecx :index ecx))
1240 (maybe-pseudo-atomic stack-allocate-p
1241 (allocation dst dst node stack-allocate-p list-pointer-lowtag)
1242 (inst shr ecx (1- n-lowtag-bits))
1243 ;; Set decrement mode (successive args at lower addresses)
1245 ;; Set up the result.
1247 ;; Jump into the middle of the loop, 'cause that's where we want
1251 ;; Compute a pointer to the next cons.
1252 (inst add dst (* cons-size n-word-bytes))
1253 ;; Store a pointer to this cons in the CDR of the previous cons.
1254 (storew dst dst -1 list-pointer-lowtag)
1256 ;; Grab one value and stash it in the car of this cons.
1258 (storew eax dst 0 list-pointer-lowtag)
1259 ;; Go back for more.
1262 ;; NIL out the last cons.
1263 (storew nil-value dst 1 list-pointer-lowtag)
1265 (emit-label done))))
1267 ;;; Return the location and size of the &MORE arg glob created by
1268 ;;; COPY-MORE-ARG. SUPPLIED is the total number of arguments supplied
1269 ;;; (originally passed in ECX). FIXED is the number of non-rest
1272 ;;; We must duplicate some of the work done by COPY-MORE-ARG, since at
1273 ;;; that time the environment is in a pretty brain-damaged state,
1274 ;;; preventing this info from being returned as values. What we do is
1275 ;;; compute supplied - fixed, and return a pointer that many words
1276 ;;; below the current stack top.
1277 (define-vop (more-arg-context)
1278 (:policy :fast-safe)
1279 (:translate sb!c::%more-arg-context)
1280 (:args (supplied :scs (any-reg) :target count))
1281 (:arg-types positive-fixnum (:constant fixnum))
1283 (:results (context :scs (descriptor-reg))
1284 (count :scs (any-reg)))
1285 (:result-types t tagged-num)
1286 (:note "more-arg-context")
1288 (move count supplied)
1289 ;; SP at this point points at the last arg pushed.
1290 ;; Point to the first more-arg, not above it.
1291 (inst lea context (make-ea :dword :base esp-tn
1292 :index count :scale 1
1293 :disp (- (+ (fixnumize fixed) n-word-bytes))))
1294 (unless (zerop fixed)
1295 (inst sub count (fixnumize fixed)))))
1297 ;;; Signal wrong argument count error if NARGS isn't equal to COUNT.
1298 (define-vop (verify-arg-count)
1299 (:policy :fast-safe)
1300 (:translate sb!c::%verify-arg-count)
1301 (:args (nargs :scs (any-reg)))
1302 (:arg-types positive-fixnum (:constant t))
1305 (:save-p :compute-only)
1308 (generate-error-code vop 'invalid-arg-count-error nargs)))
1310 (inst test nargs nargs) ; smaller instruction
1311 (inst cmp nargs (fixnumize count)))
1312 (inst jmp :ne err-lab))))
1314 ;;; Various other error signallers.
1315 (macrolet ((def (name error translate &rest args)
1316 `(define-vop (,name)
1318 `((:policy :fast-safe)
1319 (:translate ,translate)))
1320 (:args ,@(mapcar (lambda (arg)
1321 `(,arg :scs (any-reg descriptor-reg)))
1324 (:save-p :compute-only)
1326 (error-call vop ',error ,@args)))))
1327 (def arg-count-error invalid-arg-count-error
1328 sb!c::%arg-count-error nargs)
1329 (def type-check-error object-not-type-error sb!c::%type-check-error
1331 (def layout-invalid-error layout-invalid-error sb!c::%layout-invalid-error
1333 (def odd-key-args-error odd-key-args-error
1334 sb!c::%odd-key-args-error)
1335 (def unknown-key-arg-error unknown-key-arg-error
1336 sb!c::%unknown-key-arg-error key)
1337 (def nil-fun-returned-error nil-fun-returned-error nil fun))
1341 (defun emit-single-step-test ()
1342 ;; We use different ways of representing whether stepping is on on
1343 ;; +SB-THREAD / -SB-THREAD: on +SB-THREAD, we use a slot in the
1344 ;; thread structure. On -SB-THREAD we use the value of a static
1345 ;; symbol. Things are done this way, since reading a thread-local
1346 ;; slot from a symbol would require an extra register on +SB-THREAD,
1347 ;; and reading a slot from a thread structure would require an extra
1348 ;; register on -SB-THREAD.
1351 (inst cmp (make-ea :dword
1352 :disp (* thread-stepping-slot n-word-bytes))
1355 (inst cmp (make-ea-for-symbol-value sb!impl::*stepping*)
1358 (define-vop (step-instrument-before-vop)
1359 (:policy :fast-safe)
1362 (emit-single-step-test)
1364 (inst break single-step-before-trap)
1366 (note-this-location vop :step-before-vop)))