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 ;;; Accessing a slot from an earlier stack frame is definite hackery.
123 (define-vop (ancestor-frame-ref)
124 (:args (frame-pointer :scs (descriptor-reg))
125 (variable-home-tn :load-if nil))
126 (:results (value :scs (descriptor-reg any-reg)))
129 (aver (sc-is variable-home-tn control-stack))
130 (loadw value frame-pointer
131 (frame-word-offset (tn-offset variable-home-tn)))))
132 (define-vop (ancestor-frame-set)
133 (:args (frame-pointer :scs (descriptor-reg))
134 (value :scs (descriptor-reg any-reg)))
135 (:results (variable-home-tn :load-if nil))
138 (aver (sc-is variable-home-tn control-stack))
139 (storew value frame-pointer
140 (frame-word-offset (tn-offset variable-home-tn)))))
142 (define-vop (xep-allocate-frame)
143 (:info start-lab copy-more-arg-follows)
146 (emit-alignment n-lowtag-bits)
147 (trace-table-entry trace-table-fun-prologue)
148 (emit-label start-lab)
149 ;; Skip space for the function header.
150 (inst simple-fun-header-word)
151 (dotimes (i (1- simple-fun-code-offset))
154 ;; The start of the actual code.
155 ;; Save the return-pc.
156 (popw ebp-tn (frame-word-offset return-pc-save-offset))
158 ;; If copy-more-arg follows it will allocate the correct stack
159 ;; size. The stack is not allocated first here as this may expose
160 ;; args on the stack if they take up more space than the frame!
161 (unless copy-more-arg-follows
162 ;; The args fit within the frame so just allocate the frame.
164 (make-ea :dword :base ebp-tn
165 :disp (- (* n-word-bytes
166 (- (max 3 (sb-allocated-size 'stack))
169 (trace-table-entry trace-table-normal)))
171 ;;; This is emitted directly before either a known-call-local, call-local,
172 ;;; or a multiple-call-local. All it does is allocate stack space for the
173 ;;; callee (who has the same size stack as us).
174 (define-vop (allocate-frame)
175 (:results (res :scs (any-reg))
180 (inst lea res (make-ea :dword :base esp-tn
181 :disp (- (* sp->fp-offset n-word-bytes))))
182 (inst sub esp-tn (* n-word-bytes (sb-allocated-size 'stack)))))
184 ;;; Allocate a partial frame for passing stack arguments in a full
185 ;;; call. NARGS is the number of arguments passed. We allocate at
186 ;;; least 3 slots, because the XEP noise is going to want to use them
187 ;;; before it can extend the stack.
188 (define-vop (allocate-full-call-frame)
190 (:results (res :scs (any-reg)))
192 (inst lea res (make-ea :dword :base esp-tn
193 :disp (- (* sp->fp-offset n-word-bytes))))
194 (inst sub esp-tn (* (max nargs 3) n-word-bytes))))
196 ;;; Emit code needed at the return-point from an unknown-values call
197 ;;; for a fixed number of values. Values is the head of the TN-REF
198 ;;; list for the locations that the values are to be received into.
199 ;;; Nvals is the number of values that are to be received (should
200 ;;; equal the length of Values).
202 ;;; If 0 or 1 values are expected, then we just emit an instruction to
203 ;;; reset the SP (which will only be executed when other than 1 value
206 ;;; In the general case we have to do three things:
207 ;;; -- Default unsupplied register values. This need only be done
208 ;;; when a single value is returned, since register values are
209 ;;; defaulted by the called in the non-single case.
210 ;;; -- Default unsupplied stack values. This needs to be done whenever
211 ;;; there are stack values.
212 ;;; -- Reset SP. This must be done whenever other than 1 value is
213 ;;; returned, regardless of the number of values desired.
214 (defun default-unknown-values (vop values nvals node)
215 (declare (type (or tn-ref null) values)
216 (type unsigned-byte nvals))
217 (let ((type (sb!c::basic-combination-derived-type node)))
220 (note-this-location vop :single-value-return)
222 ((<= (sb!kernel:values-type-max-value-count type)
224 (when (and (named-type-p type)
225 (eq nil (named-type-name type)))
226 ;; The function never returns, it may happen that the code
227 ;; ends right here leavig the :SINGLE-VALUE-RETURN note
228 ;; dangling. Let's emit a NOP.
230 ((not (sb!kernel:values-type-may-be-single-value-p type))
231 (inst mov esp-tn ebx-tn))
232 ((member :cmov *backend-subfeatures*)
233 (inst cmov :c esp-tn ebx-tn))
235 (let ((single-value (gen-label)))
236 (inst jmp :nc single-value)
237 (inst mov esp-tn ebx-tn)
238 (emit-label single-value)))))
239 ((<= nvals register-arg-count)
240 (note-this-location vop :unknown-return)
241 (when (sb!kernel:values-type-may-be-single-value-p type)
242 (let ((regs-defaulted (gen-label)))
243 (inst jmp :c regs-defaulted)
244 ;; Default the unsupplied registers.
245 (let* ((2nd-tn-ref (tn-ref-across values))
246 (2nd-tn (tn-ref-tn 2nd-tn-ref)))
247 (inst mov 2nd-tn nil-value)
250 for tn-ref = (tn-ref-across 2nd-tn-ref)
251 then (tn-ref-across tn-ref)
252 for count from 2 below register-arg-count
253 do (inst mov (tn-ref-tn tn-ref) 2nd-tn))))
254 (inst mov ebx-tn esp-tn)
255 (emit-label regs-defaulted)))
256 (when (< register-arg-count
257 (sb!kernel:values-type-max-value-count type))
258 (inst mov esp-tn ebx-tn)))
260 ;; The number of bytes depends on the relative jump instructions.
261 ;; Best case is 31+(n-3)*14, worst case is 35+(n-3)*18. For
262 ;; NVALS=6 that is 73/89 bytes, and for NVALS=7 that is 87/107
263 ;; bytes which is likely better than using the blt below.
264 (let ((regs-defaulted (gen-label))
265 (defaulting-done (gen-label))
266 (default-stack-slots (gen-label)))
267 (note-this-location vop :unknown-return)
268 ;; Branch off to the MV case.
269 (inst jmp :c regs-defaulted)
270 ;; Do the single value case.
271 ;; Default the register args
272 (inst mov eax-tn nil-value)
274 (val (tn-ref-across values) (tn-ref-across val)))
275 ((= i (min nvals register-arg-count)))
276 (inst mov (tn-ref-tn val) eax-tn))
277 ;; Fake other registers so it looks like we returned with all the
278 ;; registers filled in.
280 (inst jmp default-stack-slots)
281 (emit-label regs-defaulted)
282 (inst mov eax-tn nil-value)
283 (collect ((defaults))
284 (do ((i register-arg-count (1+ i))
285 (val (do ((i 0 (1+ i))
286 (val values (tn-ref-across val)))
287 ((= i register-arg-count) val))
288 (tn-ref-across val)))
290 (let ((default-lab (gen-label))
292 (first-stack-arg-p (= i register-arg-count)))
293 (defaults (cons default-lab
294 (cons tn first-stack-arg-p)))
295 (inst cmp ecx-tn (fixnumize i))
296 (inst jmp :be default-lab)
297 (when first-stack-arg-p
298 ;; There are stack args so the frame of the callee is
299 ;; still there, save EDX in its first slot temporalily.
300 (storew edx-tn ebx-tn (frame-word-offset sp->fp-offset)))
301 (loadw edx-tn ebx-tn (frame-word-offset (+ sp->fp-offset i)))
302 (inst mov tn edx-tn)))
303 (emit-label defaulting-done)
304 (loadw edx-tn ebx-tn (frame-word-offset sp->fp-offset))
306 (let ((defaults (defaults)))
308 (assemble (*elsewhere*)
309 (trace-table-entry trace-table-fun-prologue)
310 (emit-label default-stack-slots)
311 (dolist (default defaults)
312 (emit-label (car default))
314 ;; We are setting the first stack argument to NIL.
315 ;; The callee's stack frame is dead, save EDX by
316 ;; pushing it to the stack, it will end up at same
317 ;; place as in the (STOREW EDX-TN EBX-TN -1) case
320 (inst mov (second 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 :c regs-defaulted)
333 ;; Default the register args, and set up the stack as if we
334 ;; entered the MV return point.
335 (inst mov ebx-tn esp-tn)
336 (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)
343 (make-ea :dword :base ebp-tn
344 :disp (frame-byte-offset register-arg-count)))
345 ;; Load EAX with NIL so we can quickly store it, and set up
346 ;; stuff for the loop.
347 (inst mov eax-tn nil-value)
349 (inst mov ecx-tn (- nvals register-arg-count))
350 ;; Jump into the default loop.
351 (inst jmp default-stack-vals)
352 ;; The regs are defaulted. We need to copy any stack arguments,
353 ;; and then default the remaining stack arguments.
354 (emit-label regs-defaulted)
355 ;; Compute the number of stack arguments, and if it's zero or
356 ;; less, don't copy any stack arguments.
357 (inst sub ecx-tn (fixnumize register-arg-count))
358 (inst jmp :le no-stack-args)
360 (storew edi-tn ebx-tn (frame-word-offset (+ sp->fp-offset 1)))
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 (frame-byte-offset register-arg-count)))
372 ;; Save ESI, and compute a pointer to where the args come from.
373 (storew esi-tn ebx-tn (frame-word-offset (+ sp->fp-offset 2)))
375 (make-ea :dword :base ebx-tn
376 :disp (frame-byte-offset
377 (+ sp->fp-offset register-arg-count))))
379 (inst shr ecx-tn word-shift) ; make word count
384 (loadw esi-tn ebx-tn (frame-word-offset (+ sp->fp-offset 2)))
385 ;; Now we have to default the remaining args. Find out how many.
386 (inst sub eax-tn (fixnumize (- nvals register-arg-count)))
388 ;; If none, then just blow out of here.
389 (inst jmp :le restore-edi)
390 (inst mov ecx-tn eax-tn)
391 (inst shr ecx-tn word-shift) ; word count
392 ;; Load EAX with NIL for fast storing.
393 (inst mov eax-tn nil-value)
395 (emit-label default-stack-vals)
398 ;; Restore EDI, and reset the stack.
399 (emit-label restore-edi)
400 (loadw edi-tn ebx-tn (frame-word-offset (+ sp->fp-offset 1)))
401 (inst mov esp-tn ebx-tn)
405 ;;;; unknown values receiving
407 ;;; Emit code needed at the return point for an unknown-values call
408 ;;; for an arbitrary number of values.
410 ;;; We do the single and non-single cases with no shared code: there
411 ;;; doesn't seem to be any potential overlap, and receiving a single
412 ;;; value is more important efficiency-wise.
414 ;;; When there is a single value, we just push it on the stack,
415 ;;; returning the old SP and 1.
417 ;;; When there is a variable number of values, we move all of the
418 ;;; argument registers onto the stack, and return ARGS and NARGS.
420 ;;; ARGS and NARGS are TNs wired to the named locations. We must
421 ;;; explicitly allocate these TNs, since their lifetimes overlap with
422 ;;; the results start and count. (Also, it's nice to be able to target
424 (defun receive-unknown-values (args nargs start count node)
425 (declare (type tn args nargs start count))
426 (let ((type (sb!c::basic-combination-derived-type node))
427 (variable-values (gen-label))
428 (stack-values (gen-label))
430 (when (sb!kernel:values-type-may-be-single-value-p type)
431 (inst jmp :c variable-values)
432 (cond ((location= start (first *register-arg-tns*))
433 (inst push (first *register-arg-tns*))
434 (inst lea start (make-ea :dword :base esp-tn :disp n-word-bytes)))
435 (t (inst mov start esp-tn)
436 (inst push (first *register-arg-tns*))))
437 (inst mov count (fixnumize 1))
439 (emit-label variable-values))
440 ;; The stack frame is burnt and RETurned from if there are no
441 ;; stack values. In this case quickly reallocate sufficient space.
442 (when (<= (sb!kernel:values-type-min-value-count type)
444 (inst cmp nargs (fixnumize register-arg-count))
445 (inst jmp :g stack-values)
446 (inst sub esp-tn nargs)
447 (emit-label stack-values))
448 ;; dtc: this writes the registers onto the stack even if they are
449 ;; not needed, only the number specified in ecx are used and have
450 ;; stack allocated to them. No harm is done.
452 for arg in *register-arg-tns*
454 for j below (sb!kernel:values-type-max-value-count type)
455 do (storew arg args i))
462 ;;; VOP that can be inherited by unknown values receivers. The main thing this
463 ;;; handles is allocation of the result temporaries.
464 (define-vop (unknown-values-receiver)
465 (:temporary (:sc descriptor-reg :offset ebx-offset
466 :from :eval :to (:result 0))
468 (:temporary (:sc any-reg :offset ecx-offset
469 :from :eval :to (:result 1))
471 (:results (start :scs (any-reg control-stack))
472 (count :scs (any-reg control-stack))))
474 ;;;; local call with unknown values convention return
476 (defun check-ocfp-and-return-pc (old-fp return-pc)
478 (format t "*known-return: old-fp ~S, tn-kind ~S; ~S ~S~%"
479 old-fp (sb!c::tn-kind old-fp) (sb!c::tn-save-tn old-fp)
480 (sb!c::tn-kind (sb!c::tn-save-tn old-fp)))
482 (format t "*known-return: return-pc ~S, tn-kind ~S; ~S ~S~%"
483 return-pc (sb!c::tn-kind return-pc)
484 (sb!c::tn-save-tn return-pc)
485 (sb!c::tn-kind (sb!c::tn-save-tn return-pc)))
486 (unless (and (sc-is old-fp control-stack)
487 (= (tn-offset old-fp) ocfp-save-offset))
488 (error "ocfp not on stack in standard save location?"))
489 (unless (and (sc-is return-pc sap-stack)
490 (= (tn-offset return-pc) return-pc-save-offset))
491 (error "return-pc not on stack in standard save location?")))
493 ;;; Instead of JMPing to TARGET, CALL a trampoline that saves the
494 ;;; return pc and jumps. Although this is an incredibly stupid trick
495 ;;; the paired CALL/RET instructions are a big win.
496 (defun make-local-call (target)
497 (let ((tramp (gen-label)))
499 (assemble (*elsewhere*)
501 (popw ebp-tn (frame-word-offset return-pc-save-offset))
504 ;;; Non-TR local call for a fixed number of values passed according to
505 ;;; the unknown values convention.
507 ;;; FP is the frame pointer in install before doing the call.
509 ;;; NFP would be the number-stack frame pointer if we had a separate
512 ;;; Args are the argument passing locations, which are specified only
513 ;;; to terminate their lifetimes in the caller.
515 ;;; VALUES are the return value locations (wired to the standard
516 ;;; passing locations). NVALS is the number of values received.
518 ;;; Save is the save info, which we can ignore since saving has been
521 ;;; TARGET is a continuation pointing to the start of the called
523 (define-vop (call-local)
527 (:results (values :more t))
529 (:move-args :local-call)
530 (:info arg-locs callee target nvals)
532 (:ignore nfp arg-locs args callee)
535 (trace-table-entry trace-table-call-site)
537 (note-this-location vop :call-site)
538 (make-local-call target)
539 (default-unknown-values vop values nvals node)
540 (trace-table-entry trace-table-normal)))
542 ;;; Non-TR local call for a variable number of return values passed according
543 ;;; to the unknown values convention. The results are the start of the values
544 ;;; glob and the number of values received.
545 (define-vop (multiple-call-local unknown-values-receiver)
550 (:move-args :local-call)
551 (:info save callee target)
552 (:ignore args save nfp callee)
556 (trace-table-entry trace-table-call-site)
558 (note-this-location vop :call-site)
559 (make-local-call target)
560 (note-this-location vop :unknown-return)
561 (receive-unknown-values values-start nvals start count node)
562 (trace-table-entry trace-table-normal)))
564 ;;;; local call with known values return
566 ;;; Non-TR local call with known return locations. Known-value return
567 ;;; works just like argument passing in local call.
569 ;;; Note: we can't use normal load-tn allocation for the fixed args,
570 ;;; since all registers may be tied up by the more operand. Instead,
571 ;;; we use MAYBE-LOAD-STACK-TN.
572 (define-vop (known-call-local)
576 (:results (res :more t))
577 (:move-args :local-call)
579 (:info save callee target)
580 (:ignore args res save nfp callee)
583 (trace-table-entry trace-table-call-site)
585 (note-this-location vop :call-site)
586 (make-local-call target)
587 (note-this-location vop :known-return)
588 (trace-table-entry trace-table-normal)))
590 ;;; From Douglas Crosher
591 ;;; Return from known values call. We receive the return locations as
592 ;;; arguments to terminate their lifetimes in the returning function. We
593 ;;; restore FP and CSP and jump to the Return-PC.
594 (define-vop (known-return)
598 (:move-args :known-return)
600 (:ignore val-locs vals)
603 (check-ocfp-and-return-pc old-fp return-pc)
604 (trace-table-entry trace-table-fun-epilogue)
605 ;; Zot all of the stack except for the old-fp and return-pc.
606 (inst mov esp-tn ebp-tn)
609 (trace-table-entry trace-table-normal)))
613 ;;; There is something of a cross-product effect with full calls.
614 ;;; Different versions are used depending on whether we know the
615 ;;; number of arguments or the name of the called function, and
616 ;;; whether we want fixed values, unknown values, or a tail call.
618 ;;; In full call, the arguments are passed creating a partial frame on
619 ;;; the stack top and storing stack arguments into that frame. On
620 ;;; entry to the callee, this partial frame is pointed to by FP.
622 ;;; This macro helps in the definition of full call VOPs by avoiding
623 ;;; code replication in defining the cross-product VOPs.
625 ;;; NAME is the name of the VOP to define.
627 ;;; NAMED is true if the first argument is an fdefinition object whose
628 ;;; definition is to be called.
630 ;;; RETURN is either :FIXED, :UNKNOWN or :TAIL:
631 ;;; -- If :FIXED, then the call is for a fixed number of values, returned in
632 ;;; the standard passing locations (passed as result operands).
633 ;;; -- If :UNKNOWN, then the result values are pushed on the stack, and the
634 ;;; result values are specified by the Start and Count as in the
635 ;;; unknown-values continuation representation.
636 ;;; -- If :TAIL, then do a tail-recursive call. No values are returned.
637 ;;; The Old-Fp and Return-PC are passed as the second and third arguments.
639 ;;; In non-tail calls, the pointer to the stack arguments is passed as
640 ;;; the last fixed argument. If Variable is false, then the passing
641 ;;; locations are passed as a more arg. Variable is true if there are
642 ;;; a variable number of arguments passed on the stack. Variable
643 ;;; cannot be specified with :TAIL return. TR variable argument call
644 ;;; is implemented separately.
646 ;;; In tail call with fixed arguments, the passing locations are
647 ;;; passed as a more arg, but there is no new-FP, since the arguments
648 ;;; have been set up in the current frame.
649 (macrolet ((define-full-call (name named return variable)
650 (aver (not (and variable (eq return :tail))))
652 ,@(when (eq return :unknown)
653 '(unknown-values-receiver)))
655 ,@(unless (eq return :tail)
656 '((new-fp :scs (any-reg) :to (:argument 1))))
658 (fun :scs (descriptor-reg control-stack)
659 :target eax :to (:argument 0))
661 ,@(when (eq return :tail)
665 ,@(unless variable '((args :more t :scs (descriptor-reg)))))
667 ,@(when (eq return :fixed)
668 '((:results (values :more t))))
670 (:save-p ,(if (eq return :tail) :compute-only t))
672 ,@(unless (or (eq return :tail) variable)
673 '((:move-args :full-call)))
677 ,@(unless (or variable (eq return :tail)) '(arg-locs))
678 ,@(unless variable '(nargs))
679 ,@(when (eq return :fixed) '(nvals))
683 ,@(unless (or variable (eq return :tail)) '(arg-locs))
684 ,@(unless variable '(args)))
686 ;; We pass either the fdefn object (for named call) or
687 ;; the actual function object (for unnamed call) in
688 ;; EAX. With named call, closure-tramp will replace it
689 ;; with the real function and invoke the real function
690 ;; for closures. Non-closures do not need this value,
691 ;; so don't care what shows up in it.
699 ;; We pass the number of arguments in ECX.
700 (:temporary (:sc unsigned-reg :offset ecx-offset :to :eval) ecx)
702 ;; With variable call, we have to load the
703 ;; register-args out of the (new) stack frame before
704 ;; doing the call. Therefore, we have to tell the
705 ;; lifetime stuff that we need to use them.
707 (mapcar (lambda (name offset)
708 `(:temporary (:sc descriptor-reg
713 *register-arg-names* *register-arg-offsets*))
715 ,@(when (eq return :tail)
716 '((:temporary (:sc unsigned-reg
720 ,@(unless (eq return :tail)
723 (:generator ,(+ (if named 5 0)
725 (if (eq return :tail) 0 10)
727 (if (eq return :unknown) 25 0))
728 (trace-table-entry trace-table-call-site)
730 ;; This has to be done before the frame pointer is
731 ;; changed! EAX stores the 'lexical environment' needed
737 ;; For variable call, compute the number of
738 ;; arguments and move some of the arguments to
741 ;; Compute the number of arguments.
742 (noise '(inst mov ecx new-fp))
743 (noise '(inst sub ecx esp-tn))
744 ;; Move the necessary args to registers,
745 ;; this moves them all even if they are
748 for name in *register-arg-names*
749 for index downfrom -1
750 do (noise `(loadw ,name new-fp ,index)))
754 (inst mov ecx (fixnumize nargs)))))
755 ,@(cond ((eq return :tail)
756 '(;; Python has figured out what frame we should
757 ;; return to so might as well use that clue.
758 ;; This seems really important to the
759 ;; implementation of things like
760 ;; (without-interrupts ...)
762 ;; dtc; Could be doing a tail call from a
763 ;; known-local-call etc in which the old-fp
764 ;; or ret-pc are in regs or in non-standard
765 ;; places. If the passing location were
766 ;; wired to the stack in standard locations
767 ;; then these moves will be un-necessary;
768 ;; this is probably best for the x86.
771 (unless (= ocfp-save-offset
773 ;; FIXME: FORMAT T for stale
774 ;; diagnostic output (several of
775 ;; them around here), ick
776 (error "** tail-call old-fp not S0~%")
777 (move old-fp-tmp old-fp)
780 (frame-word-offset ocfp-save-offset))))
781 ((any-reg descriptor-reg)
782 (error "** tail-call old-fp in reg not S0~%")
785 (frame-word-offset ocfp-save-offset))))
787 ;; For tail call, we have to push the
788 ;; return-pc so that it looks like we CALLed
789 ;; despite the fact that we are going to JMP.
790 (inst push return-pc)
793 ;; For non-tail call, we have to save our
794 ;; frame pointer and install the new frame
795 ;; pointer. We can't load stack tns after this
797 `(;; Python doesn't seem to allocate a frame
798 ;; here which doesn't leave room for the
801 ;; The variable args are on the stack and
802 ;; become the frame, but there may be <3
803 ;; args and 3 stack slots are assumed
804 ;; allocate on the call. So need to ensure
805 ;; there are at least 3 slots. This hack
808 '(inst sub esp-tn (fixnumize 3)))
810 ;; Bias the new-fp for use as an fp
812 '(inst sub new-fp (fixnumize sp->fp-offset)))
815 (storew ebp-tn new-fp
816 (frame-word-offset ocfp-save-offset))
818 (move ebp-tn new-fp) ; NB - now on new stack frame.
821 (when step-instrumenting
822 (emit-single-step-test)
824 (inst break single-step-around-trap))
827 (note-this-location vop :call-site)
829 (inst ,(if (eq return :tail) 'jmp 'call)
831 '(make-ea-for-object-slot eax fdefn-raw-addr-slot
832 other-pointer-lowtag)
833 '(make-ea-for-object-slot eax closure-fun-slot
834 fun-pointer-lowtag)))
837 '((default-unknown-values vop values nvals node)))
839 '((note-this-location vop :unknown-return)
840 (receive-unknown-values values-start nvals start count
843 (trace-table-entry trace-table-normal)))))
845 (define-full-call call nil :fixed nil)
846 (define-full-call call-named t :fixed nil)
847 (define-full-call multiple-call nil :unknown nil)
848 (define-full-call multiple-call-named t :unknown nil)
849 (define-full-call tail-call nil :tail nil)
850 (define-full-call tail-call-named t :tail nil)
852 (define-full-call call-variable nil :fixed t)
853 (define-full-call multiple-call-variable nil :unknown t))
855 ;;; This is defined separately, since it needs special code that BLT's
856 ;;; the arguments down. All the real work is done in the assembly
857 ;;; routine. We just set things up so that it can find what it needs.
858 (define-vop (tail-call-variable)
859 (:args (args :scs (any-reg control-stack) :target esi)
860 (function :scs (descriptor-reg control-stack) :target eax)
863 (:temporary (:sc unsigned-reg :offset esi-offset :from (:argument 0)) esi)
864 (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 1)) eax)
866 (check-ocfp-and-return-pc old-fp return-pc)
867 ;; Move these into the passing locations if they are not already there.
870 ;; And jump to the assembly routine.
871 (inst jmp (make-fixup 'tail-call-variable :assembly-routine))))
873 ;;;; unknown values return
875 ;;; Return a single-value using the Unknown-Values convention.
877 ;;; pfw--get wired-tn conflicts sometimes if register sc specd for args
878 ;;; having problems targeting args to regs -- using temps instead.
880 ;;; First off, modifying the return-pc defeats the branch-prediction
881 ;;; optimizations on modern CPUs quite handily. Second, we can do all
882 ;;; this without needing a temp register. Fixed the latter, at least.
883 ;;; -- AB 2006/Feb/04
884 (define-vop (return-single)
890 (check-ocfp-and-return-pc old-fp return-pc)
891 (trace-table-entry trace-table-fun-epilogue)
892 ;; Drop stack above old-fp
893 (inst mov esp-tn ebp-tn)
894 ;; Clear the multiple-value return flag
896 ;; Restore the old frame pointer
901 ;;; Do unknown-values return of a fixed (other than 1) number of
902 ;;; values. The VALUES are required to be set up in the standard
903 ;;; passing locations. NVALS is the number of values returned.
905 ;;; Basically, we just load ECX with the number of values returned and
906 ;;; EBX with a pointer to the values, set ESP to point to the end of
907 ;;; the values, and jump directly to return-pc.
910 (return-pc :to (:eval 1))
914 ;; In the case of other than one value, we need these registers to
915 ;; tell the caller where they are and how many there are.
916 (:temporary (:sc unsigned-reg :offset ebx-offset) ebx)
917 (:temporary (:sc unsigned-reg :offset ecx-offset) ecx)
918 ;; We need to stretch the lifetime of return-pc past the argument
919 ;; registers so that we can default the argument registers without
920 ;; trashing return-pc.
921 (:temporary (:sc unsigned-reg :offset (first *register-arg-offsets*)
923 (:temporary (:sc unsigned-reg :offset (second *register-arg-offsets*)
925 (:temporary (:sc unsigned-reg :offset (third *register-arg-offsets*)
929 (check-ocfp-and-return-pc old-fp return-pc)
931 ;; This is handled in RETURN-SINGLE.
932 (error "nvalues is 1"))
933 (trace-table-entry trace-table-fun-epilogue)
934 ;; Establish the values pointer and values count.
935 (inst lea ebx (make-ea :dword :base ebp-tn
936 :disp (* sp->fp-offset n-word-bytes)))
938 (inst xor ecx ecx) ; smaller
939 (inst mov ecx (fixnumize nvals)))
940 ;; Pre-default any argument register that need it.
941 (when (< nvals register-arg-count)
942 (let* ((arg-tns (nthcdr nvals (list a0 a1 a2)))
943 (first (first arg-tns)))
944 (inst mov first nil-value)
945 (dolist (tn (cdr arg-tns))
946 (inst mov tn first))))
947 ;; Set the multiple value return flag.
949 ;; And away we go. Except that return-pc is still on the
950 ;; stack and we've changed the stack pointer. So we have to
951 ;; tell it to index off of EBX instead of EBP.
952 (cond ((<= nvals register-arg-count)
953 (inst mov esp-tn ebp-tn)
957 ;; Some values are on the stack after RETURN-PC and OLD-FP,
958 ;; can't return normally and some slots of the frame will
959 ;; be used as temporaries by the receiver.
961 ;; Clear as much of the stack as possible, but not past the
962 ;; old frame address.
964 (make-ea :dword :base ebp-tn
965 :disp (frame-byte-offset (1- nvals))))
967 (inst push (make-ea :dword :base ebx
968 :disp (frame-byte-offset
970 (tn-offset return-pc)))))
973 (trace-table-entry trace-table-normal)))
975 ;;; Do unknown-values return of an arbitrary number of values (passed
976 ;;; on the stack.) We check for the common case of a single return
977 ;;; value, and do that inline using the normal single value return
978 ;;; convention. Otherwise, we branch off to code that calls an
979 ;;; assembly-routine.
981 ;;; The assembly routine takes the following args:
982 ;;; ECX -- number of values to find there.
983 ;;; ESI -- pointer to where to find the values.
984 (define-vop (return-multiple)
987 (vals :scs (any-reg) :target esi)
988 (nvals :scs (any-reg) :target ecx))
989 (:temporary (:sc unsigned-reg :offset esi-offset :from (:argument 2)) esi)
990 (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 3)) ecx)
991 (:temporary (:sc descriptor-reg :offset (first *register-arg-offsets*)
995 (check-ocfp-and-return-pc old-fp return-pc)
996 (trace-table-entry trace-table-fun-epilogue)
997 (unless (policy node (> space speed))
998 ;; Check for the single case.
999 (let ((not-single (gen-label)))
1000 (inst cmp nvals (fixnumize 1))
1001 (inst jmp :ne not-single)
1002 ;; Return with one value.
1004 ;; Clear the stack until ocfp.
1005 (inst mov esp-tn ebp-tn)
1006 ;; clear the multiple-value return flag
1011 ;; Nope, not the single case. Jump to the assembly routine.
1012 (emit-label not-single)))
1015 (inst jmp (make-fixup 'return-multiple :assembly-routine))
1016 (trace-table-entry trace-table-normal)))
1020 ;;; We don't need to do anything special for regular functions.
1021 (define-vop (setup-environment)
1025 ;; Don't bother doing anything.
1028 ;;; Get the lexical environment from its passing location.
1029 (define-vop (setup-closure-environment)
1030 (:results (closure :scs (descriptor-reg)))
1035 (move closure eax-tn)))
1037 ;;; Copy a &MORE arg from the argument area to the end of the current
1038 ;;; frame. FIXED is the number of non-&MORE arguments.
1040 ;;; The tricky part is doing this without trashing any of the calling
1041 ;;; convention registers that are still needed. This vop is emitted
1042 ;;; directly after the xep-allocate frame. That means the registers
1043 ;;; are in use as follows:
1045 ;;; EAX -- The lexenv.
1046 ;;; EBX -- Available.
1047 ;;; ECX -- The total number of arguments * N-WORD-BYTES.
1048 ;;; EDX -- The first arg.
1049 ;;; EDI -- The second arg.
1050 ;;; ESI -- The third arg.
1052 ;;; So basically, we have one register available for our use: EBX.
1054 ;;; What we can do is push the other regs onto the stack, and then
1055 ;;; restore their values by looking directly below where we put the
1057 (define-vop (copy-more-arg)
1060 ;; Avoid the copy if there are no more args.
1061 (cond ((zerop fixed)
1062 (inst jecxz JUST-ALLOC-FRAME))
1064 (inst cmp ecx-tn (fixnumize fixed))
1065 (inst jmp :be JUST-ALLOC-FRAME)))
1067 ;; Allocate the space on the stack.
1068 ;; stack = ebp + sp->fp-offset - (max 3 frame-size) - (nargs - fixed)
1070 (make-ea :dword :base ebp-tn
1071 :disp (* n-word-bytes
1072 (- (+ sp->fp-offset fixed)
1073 (max 3 (sb-allocated-size 'stack))))))
1074 (inst sub ebx-tn ecx-tn) ; Got the new stack in ebx
1075 (inst mov esp-tn ebx-tn)
1077 ;; Now: nargs>=1 && nargs>fixed
1079 ;; Save the original count of args.
1080 (inst mov ebx-tn ecx-tn)
1082 (cond ((< fixed register-arg-count)
1083 ;; We must stop when we run out of stack args, not when we
1084 ;; run out of more args.
1085 ;; Number to copy = nargs-3
1086 (inst sub ecx-tn (fixnumize register-arg-count))
1087 ;; Everything of interest in registers.
1088 (inst jmp :be DO-REGS))
1090 ;; Number to copy = nargs-fixed
1091 (inst sub ecx-tn (fixnumize fixed))))
1093 ;; Save edi and esi register args.
1097 ;; Okay, we have pushed the register args. We can trash them
1100 ;; Initialize src to be end of args.
1101 (inst lea esi-tn (make-ea :dword :base ebp-tn
1102 :disp (* sp->fp-offset n-word-bytes)))
1103 (inst sub esi-tn ebx-tn)
1105 ;; We need to copy from downwards up to avoid overwriting some of
1106 ;; the yet uncopied args. So we need to use EBX as the copy index
1107 ;; and ECX as the loop counter, rather than using ECX for both.
1108 (inst xor ebx-tn ebx-tn)
1110 ;; We used to use REP MOVS here, but on modern x86 it performs
1111 ;; much worse than an explicit loop for small blocks.
1113 (inst mov edi-tn (make-ea :dword :base esi-tn :index ebx-tn))
1114 ;; The :DISP is to account for the registers saved on the stack
1115 (inst mov (make-ea :dword :base esp-tn :disp (* 3 n-word-bytes)
1118 (inst add ebx-tn n-word-bytes)
1119 (inst sub ecx-tn n-word-bytes)
1120 (inst jmp :nz COPY-LOOP)
1122 ;; So now we need to restore EDI and ESI.
1130 (inst mov ecx-tn ebx-tn)
1132 ;; Here: nargs>=1 && nargs>fixed
1133 (when (< fixed register-arg-count)
1134 ;; Now we have to deposit any more args that showed up in
1138 ;; Store it relative to ebp
1139 (inst mov (make-ea :dword :base ebp-tn
1140 :disp (* n-word-bytes
1144 (max 3 (sb-allocated-size
1146 (nth i *register-arg-tns*))
1149 (when (>= i register-arg-count)
1152 ;; Don't deposit any more than there are.
1154 (inst test ecx-tn ecx-tn)
1155 (inst cmp ecx-tn (fixnumize i)))
1156 (inst jmp :eq DONE)))
1162 (make-ea :dword :base ebp-tn
1163 :disp (* n-word-bytes
1165 (max 3 (sb-allocated-size 'stack))))))
1169 (define-vop (more-kw-arg)
1170 (:translate sb!c::%more-kw-arg)
1171 (:policy :fast-safe)
1172 (:args (object :scs (descriptor-reg) :to (:result 1))
1173 (index :scs (any-reg immediate) :to (:result 1) :target keyword))
1174 (:arg-types * tagged-num)
1175 (:results (value :scs (descriptor-reg any-reg))
1176 (keyword :scs (descriptor-reg any-reg)))
1181 (inst mov value (make-ea :dword :base object :disp (tn-value index)))
1182 (inst mov keyword (make-ea :dword :base object
1183 :disp (+ (tn-value index) n-word-bytes))))
1185 (inst mov value (make-ea :dword :base object :index index))
1186 (inst mov keyword (make-ea :dword :base object :index index
1187 :disp n-word-bytes))))))
1189 (define-vop (more-arg)
1190 (:translate sb!c::%more-arg)
1191 (:policy :fast-safe)
1192 (:args (object :scs (descriptor-reg) :to (:result 1))
1193 (index :scs (any-reg) :to (:result 1) :target value))
1194 (:arg-types * tagged-num)
1195 (:results (value :scs (descriptor-reg any-reg)))
1200 (inst mov value (make-ea :dword :base object :index value))))
1202 ;;; Turn more arg (context, count) into a list.
1203 (define-vop (listify-rest-args)
1204 (:translate %listify-rest-args)
1206 (:args (context :scs (descriptor-reg) :target src)
1207 (count :scs (any-reg) :target ecx))
1208 (:arg-types * tagged-num)
1209 (:temporary (:sc unsigned-reg :offset esi-offset :from (:argument 0)) src)
1210 (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 1)) ecx)
1211 (:temporary (:sc unsigned-reg :offset eax-offset) eax)
1212 (:temporary (:sc unsigned-reg) dst)
1213 (:results (result :scs (descriptor-reg)))
1216 (let ((enter (gen-label))
1219 (stack-allocate-p (node-stack-allocate-p node)))
1222 ;; Check to see whether there are no args, and just return NIL if so.
1223 (inst mov result nil-value)
1225 (inst lea dst (make-ea :dword :base ecx :index ecx))
1226 (maybe-pseudo-atomic stack-allocate-p
1227 (allocation dst dst node stack-allocate-p list-pointer-lowtag)
1228 ;; Set decrement mode (successive args at lower addresses)
1230 ;; Set up the result.
1232 ;; Jump into the middle of the loop, 'cause that's where we want
1236 ;; Compute a pointer to the next cons.
1237 (inst add dst (* cons-size n-word-bytes))
1238 ;; Store a pointer to this cons in the CDR of the previous cons.
1239 (storew dst dst -1 list-pointer-lowtag)
1241 ;; Grab one value and stash it in the car of this cons.
1243 (storew eax dst 0 list-pointer-lowtag)
1244 ;; Go back for more.
1245 (inst sub ecx n-word-bytes)
1247 ;; NIL out the last cons.
1248 (storew nil-value dst 1 list-pointer-lowtag)
1250 (emit-label done))))
1252 ;;; Return the location and size of the &MORE arg glob created by
1253 ;;; COPY-MORE-ARG. SUPPLIED is the total number of arguments supplied
1254 ;;; (originally passed in ECX). FIXED is the number of non-rest
1257 ;;; We must duplicate some of the work done by COPY-MORE-ARG, since at
1258 ;;; that time the environment is in a pretty brain-damaged state,
1259 ;;; preventing this info from being returned as values. What we do is
1260 ;;; compute supplied - fixed, and return a pointer that many words
1261 ;;; below the current stack top.
1262 (define-vop (more-arg-context)
1263 (:policy :fast-safe)
1264 (:translate sb!c::%more-arg-context)
1265 (:args (supplied :scs (any-reg) :target count))
1266 (:arg-types positive-fixnum (:constant fixnum))
1268 (:results (context :scs (descriptor-reg))
1269 (count :scs (any-reg)))
1270 (:result-types t tagged-num)
1271 (:note "more-arg-context")
1273 (move count supplied)
1274 ;; SP at this point points at the last arg pushed.
1275 ;; Point to the first more-arg, not above it.
1276 (inst lea context (make-ea :dword :base esp-tn
1277 :index count :scale 1
1278 :disp (- (+ (fixnumize fixed) n-word-bytes))))
1279 (unless (zerop fixed)
1280 (inst sub count (fixnumize fixed)))))
1282 ;;; Signal wrong argument count error if NARGS isn't equal to COUNT.
1283 (define-vop (verify-arg-count)
1284 (:policy :fast-safe)
1285 (:translate sb!c::%verify-arg-count)
1286 (:args (nargs :scs (any-reg)))
1287 (:arg-types positive-fixnum (:constant t))
1290 (:save-p :compute-only)
1293 (generate-error-code vop 'invalid-arg-count-error nargs)))
1295 (inst test nargs nargs) ; smaller instruction
1296 (inst cmp nargs (fixnumize count)))
1297 (inst jmp :ne err-lab))))
1299 ;;; Various other error signallers.
1300 (macrolet ((def (name error translate &rest args)
1301 `(define-vop (,name)
1303 `((:policy :fast-safe)
1304 (:translate ,translate)))
1305 (:args ,@(mapcar (lambda (arg)
1306 `(,arg :scs (any-reg descriptor-reg)))
1309 (:save-p :compute-only)
1311 (error-call vop ',error ,@args)))))
1312 (def arg-count-error invalid-arg-count-error
1313 sb!c::%arg-count-error nargs)
1314 (def type-check-error object-not-type-error sb!c::%type-check-error
1316 (def layout-invalid-error layout-invalid-error sb!c::%layout-invalid-error
1318 (def odd-key-args-error odd-key-args-error
1319 sb!c::%odd-key-args-error)
1320 (def unknown-key-arg-error unknown-key-arg-error
1321 sb!c::%unknown-key-arg-error key)
1322 (def nil-fun-returned-error nil-fun-returned-error nil fun))
1326 (defun emit-single-step-test ()
1327 ;; We use different ways of representing whether stepping is on on
1328 ;; +SB-THREAD / -SB-THREAD: on +SB-THREAD, we use a slot in the
1329 ;; thread structure. On -SB-THREAD we use the value of a static
1330 ;; symbol. Things are done this way, since reading a thread-local
1331 ;; slot from a symbol would require an extra register on +SB-THREAD,
1332 ;; and reading a slot from a thread structure would require an extra
1333 ;; register on -SB-THREAD.
1336 (inst cmp (make-ea :dword
1337 :disp (* thread-stepping-slot n-word-bytes))
1340 (inst cmp (make-ea-for-symbol-value sb!impl::*stepping*)
1343 (define-vop (step-instrument-before-vop)
1344 (:policy :fast-safe)
1347 (emit-single-step-test)
1349 (inst break single-step-before-trap)
1351 (note-this-location vop :step-before-vop)))