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 ;;; The local call convention doesn't fit that well with x86-style
494 ;;; calls. Emit a header for local calls to pop the return address
495 ;;; in the right place.
496 (defun emit-block-header (start-label trampoline-label fall-thru-p alignp)
497 (declare (ignore alignp))
498 (when trampoline-label
500 (inst jmp start-label))
501 (emit-label trampoline-label)
502 (popw rbp-tn (frame-word-offset return-pc-save-offset)))
503 (emit-label start-label))
505 ;;; Non-TR local call for a fixed number of values passed according to
506 ;;; the unknown values convention.
508 ;;; FP is the frame pointer in install before doing the call.
510 ;;; NFP would be the number-stack frame pointer if we had a separate
513 ;;; Args are the argument passing locations, which are specified only
514 ;;; to terminate their lifetimes in the caller.
516 ;;; VALUES are the return value locations (wired to the standard
517 ;;; passing locations). NVALS is the number of values received.
519 ;;; Save is the save info, which we can ignore since saving has been
522 ;;; TARGET is a continuation pointing to the start of the called
524 (define-vop (call-local)
528 (:results (values :more t))
530 (:move-args :local-call)
531 (:info arg-locs callee target nvals)
533 (:ignore nfp arg-locs args callee)
536 (trace-table-entry trace-table-call-site)
538 (note-this-location vop :call-site)
540 (default-unknown-values vop values nvals node)
541 (trace-table-entry trace-table-normal)))
543 ;;; Non-TR local call for a variable number of return values passed according
544 ;;; to the unknown values convention. The results are the start of the values
545 ;;; glob and the number of values received.
546 (define-vop (multiple-call-local unknown-values-receiver)
551 (:move-args :local-call)
552 (:info save callee target)
553 (:ignore args save nfp callee)
557 (trace-table-entry trace-table-call-site)
559 (note-this-location vop :call-site)
561 (note-this-location vop :unknown-return)
562 (receive-unknown-values values-start nvals start count node)
563 (trace-table-entry trace-table-normal)))
565 ;;;; local call with known values return
567 ;;; Non-TR local call with known return locations. Known-value return
568 ;;; works just like argument passing in local call.
570 ;;; Note: we can't use normal load-tn allocation for the fixed args,
571 ;;; since all registers may be tied up by the more operand. Instead,
572 ;;; we use MAYBE-LOAD-STACK-TN.
573 (define-vop (known-call-local)
577 (:results (res :more t))
578 (:move-args :local-call)
580 (:info save callee target)
581 (:ignore args res save nfp callee)
584 (trace-table-entry trace-table-call-site)
586 (note-this-location vop :call-site)
588 (note-this-location vop :known-return)
589 (trace-table-entry trace-table-normal)))
591 ;;; From Douglas Crosher
592 ;;; Return from known values call. We receive the return locations as
593 ;;; arguments to terminate their lifetimes in the returning function. We
594 ;;; restore FP and CSP and jump to the Return-PC.
595 (define-vop (known-return)
599 (:move-args :known-return)
601 (:ignore val-locs vals)
604 (check-ocfp-and-return-pc old-fp return-pc)
605 (trace-table-entry trace-table-fun-epilogue)
606 ;; Zot all of the stack except for the old-fp and return-pc.
607 (inst mov esp-tn ebp-tn)
610 (trace-table-entry trace-table-normal)))
614 ;;; There is something of a cross-product effect with full calls.
615 ;;; Different versions are used depending on whether we know the
616 ;;; number of arguments or the name of the called function, and
617 ;;; whether we want fixed values, unknown values, or a tail call.
619 ;;; In full call, the arguments are passed creating a partial frame on
620 ;;; the stack top and storing stack arguments into that frame. On
621 ;;; entry to the callee, this partial frame is pointed to by FP.
623 ;;; This macro helps in the definition of full call VOPs by avoiding
624 ;;; code replication in defining the cross-product VOPs.
626 ;;; NAME is the name of the VOP to define.
628 ;;; NAMED is true if the first argument is an fdefinition object whose
629 ;;; definition is to be called.
631 ;;; RETURN is either :FIXED, :UNKNOWN or :TAIL:
632 ;;; -- If :FIXED, then the call is for a fixed number of values, returned in
633 ;;; the standard passing locations (passed as result operands).
634 ;;; -- If :UNKNOWN, then the result values are pushed on the stack, and the
635 ;;; result values are specified by the Start and Count as in the
636 ;;; unknown-values continuation representation.
637 ;;; -- If :TAIL, then do a tail-recursive call. No values are returned.
638 ;;; The Old-Fp and Return-PC are passed as the second and third arguments.
640 ;;; In non-tail calls, the pointer to the stack arguments is passed as
641 ;;; the last fixed argument. If Variable is false, then the passing
642 ;;; locations are passed as a more arg. Variable is true if there are
643 ;;; a variable number of arguments passed on the stack. Variable
644 ;;; cannot be specified with :TAIL return. TR variable argument call
645 ;;; is implemented separately.
647 ;;; In tail call with fixed arguments, the passing locations are
648 ;;; passed as a more arg, but there is no new-FP, since the arguments
649 ;;; have been set up in the current frame.
650 (macrolet ((define-full-call (name named return variable)
651 (aver (not (and variable (eq return :tail))))
653 ,@(when (eq return :unknown)
654 '(unknown-values-receiver)))
656 ,@(unless (eq return :tail)
657 '((new-fp :scs (any-reg) :to (:argument 1))))
659 (fun :scs (descriptor-reg control-stack)
660 :target eax :to (:argument 0))
662 ,@(when (eq return :tail)
666 ,@(unless variable '((args :more t :scs (descriptor-reg)))))
668 ,@(when (eq return :fixed)
669 '((:results (values :more t))))
671 (:save-p ,(if (eq return :tail) :compute-only t))
673 ,@(unless (or (eq return :tail) variable)
674 '((:move-args :full-call)))
678 ,@(unless (or variable (eq return :tail)) '(arg-locs))
679 ,@(unless variable '(nargs))
680 ,@(when (eq return :fixed) '(nvals))
684 ,@(unless (or variable (eq return :tail)) '(arg-locs))
685 ,@(unless variable '(args)))
687 ;; We pass either the fdefn object (for named call) or
688 ;; the actual function object (for unnamed call) in
689 ;; EAX. With named call, closure-tramp will replace it
690 ;; with the real function and invoke the real function
691 ;; for closures. Non-closures do not need this value,
692 ;; so don't care what shows up in it.
700 ;; We pass the number of arguments in ECX.
701 (:temporary (:sc unsigned-reg :offset ecx-offset :to :eval) ecx)
703 ;; With variable call, we have to load the
704 ;; register-args out of the (new) stack frame before
705 ;; doing the call. Therefore, we have to tell the
706 ;; lifetime stuff that we need to use them.
708 (mapcar (lambda (name offset)
709 `(:temporary (:sc descriptor-reg
714 *register-arg-names* *register-arg-offsets*))
716 ,@(when (eq return :tail)
717 '((:temporary (:sc unsigned-reg
721 ,@(unless (eq return :tail)
724 (:generator ,(+ (if named 5 0)
726 (if (eq return :tail) 0 10)
728 (if (eq return :unknown) 25 0))
729 (trace-table-entry trace-table-call-site)
731 ;; This has to be done before the frame pointer is
732 ;; changed! EAX stores the 'lexical environment' needed
738 ;; For variable call, compute the number of
739 ;; arguments and move some of the arguments to
742 ;; Compute the number of arguments.
743 (noise '(inst mov ecx new-fp))
744 (noise '(inst sub ecx esp-tn))
745 ;; Move the necessary args to registers,
746 ;; this moves them all even if they are
749 for name in *register-arg-names*
750 for index downfrom -1
751 do (noise `(loadw ,name new-fp ,index)))
755 (inst mov ecx (fixnumize nargs)))))
756 ,@(cond ((eq return :tail)
757 '(;; Python has figured out what frame we should
758 ;; return to so might as well use that clue.
759 ;; This seems really important to the
760 ;; implementation of things like
761 ;; (without-interrupts ...)
763 ;; dtc; Could be doing a tail call from a
764 ;; known-local-call etc in which the old-fp
765 ;; or ret-pc are in regs or in non-standard
766 ;; places. If the passing location were
767 ;; wired to the stack in standard locations
768 ;; then these moves will be un-necessary;
769 ;; this is probably best for the x86.
772 (unless (= ocfp-save-offset
774 ;; FIXME: FORMAT T for stale
775 ;; diagnostic output (several of
776 ;; them around here), ick
777 (error "** tail-call old-fp not S0~%")
778 (move old-fp-tmp old-fp)
781 (frame-word-offset ocfp-save-offset))))
782 ((any-reg descriptor-reg)
783 (error "** tail-call old-fp in reg not S0~%")
786 (frame-word-offset ocfp-save-offset))))
788 ;; For tail call, we have to push the
789 ;; return-pc so that it looks like we CALLed
790 ;; despite the fact that we are going to JMP.
791 (inst push return-pc)
794 ;; For non-tail call, we have to save our
795 ;; frame pointer and install the new frame
796 ;; pointer. We can't load stack tns after this
798 `(;; Python doesn't seem to allocate a frame
799 ;; here which doesn't leave room for the
802 ;; The variable args are on the stack and
803 ;; become the frame, but there may be <3
804 ;; args and 3 stack slots are assumed
805 ;; allocate on the call. So need to ensure
806 ;; there are at least 3 slots. This hack
809 '(inst sub esp-tn (fixnumize 3)))
811 ;; Bias the new-fp for use as an fp
813 '(inst sub new-fp (fixnumize sp->fp-offset)))
816 (storew ebp-tn new-fp
817 (frame-word-offset ocfp-save-offset))
819 (move ebp-tn new-fp) ; NB - now on new stack frame.
822 (when step-instrumenting
823 (emit-single-step-test)
825 (inst break single-step-around-trap))
828 (note-this-location vop :call-site)
830 (inst ,(if (eq return :tail) 'jmp 'call)
832 '(make-ea-for-object-slot eax fdefn-raw-addr-slot
833 other-pointer-lowtag)
834 '(make-ea-for-object-slot eax closure-fun-slot
835 fun-pointer-lowtag)))
838 '((default-unknown-values vop values nvals node)))
840 '((note-this-location vop :unknown-return)
841 (receive-unknown-values values-start nvals start count
844 (trace-table-entry trace-table-normal)))))
846 (define-full-call call nil :fixed nil)
847 (define-full-call call-named t :fixed nil)
848 (define-full-call multiple-call nil :unknown nil)
849 (define-full-call multiple-call-named t :unknown nil)
850 (define-full-call tail-call nil :tail nil)
851 (define-full-call tail-call-named t :tail nil)
853 (define-full-call call-variable nil :fixed t)
854 (define-full-call multiple-call-variable nil :unknown t))
856 ;;; This is defined separately, since it needs special code that BLT's
857 ;;; the arguments down. All the real work is done in the assembly
858 ;;; routine. We just set things up so that it can find what it needs.
859 (define-vop (tail-call-variable)
860 (:args (args :scs (any-reg control-stack) :target esi)
861 (function :scs (descriptor-reg control-stack) :target eax)
864 (:temporary (:sc unsigned-reg :offset esi-offset :from (:argument 0)) esi)
865 (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 1)) eax)
867 (check-ocfp-and-return-pc old-fp return-pc)
868 ;; Move these into the passing locations if they are not already there.
871 ;; And jump to the assembly routine.
872 (inst jmp (make-fixup 'tail-call-variable :assembly-routine))))
874 ;;;; unknown values return
876 ;;; Return a single-value using the Unknown-Values convention.
878 ;;; pfw--get wired-tn conflicts sometimes if register sc specd for args
879 ;;; having problems targeting args to regs -- using temps instead.
881 ;;; First off, modifying the return-pc defeats the branch-prediction
882 ;;; optimizations on modern CPUs quite handily. Second, we can do all
883 ;;; this without needing a temp register. Fixed the latter, at least.
884 ;;; -- AB 2006/Feb/04
885 (define-vop (return-single)
891 (check-ocfp-and-return-pc old-fp return-pc)
892 (trace-table-entry trace-table-fun-epilogue)
893 ;; Drop stack above old-fp
894 (inst mov esp-tn ebp-tn)
895 ;; Clear the multiple-value return flag
897 ;; Restore the old frame pointer
902 ;;; Do unknown-values return of a fixed (other than 1) number of
903 ;;; values. The VALUES are required to be set up in the standard
904 ;;; passing locations. NVALS is the number of values returned.
906 ;;; Basically, we just load ECX with the number of values returned and
907 ;;; EBX with a pointer to the values, set ESP to point to the end of
908 ;;; the values, and jump directly to return-pc.
911 (return-pc :to (:eval 1))
915 ;; In the case of other than one value, we need these registers to
916 ;; tell the caller where they are and how many there are.
917 (:temporary (:sc unsigned-reg :offset ebx-offset) ebx)
918 (:temporary (:sc unsigned-reg :offset ecx-offset) ecx)
919 ;; We need to stretch the lifetime of return-pc past the argument
920 ;; registers so that we can default the argument registers without
921 ;; trashing return-pc.
922 (:temporary (:sc unsigned-reg :offset (first *register-arg-offsets*)
924 (:temporary (:sc unsigned-reg :offset (second *register-arg-offsets*)
926 (:temporary (:sc unsigned-reg :offset (third *register-arg-offsets*)
930 (check-ocfp-and-return-pc old-fp return-pc)
932 ;; This is handled in RETURN-SINGLE.
933 (error "nvalues is 1"))
934 (trace-table-entry trace-table-fun-epilogue)
935 ;; Establish the values pointer and values count.
936 (inst lea ebx (make-ea :dword :base ebp-tn
937 :disp (* sp->fp-offset n-word-bytes)))
939 (inst xor ecx ecx) ; smaller
940 (inst mov ecx (fixnumize nvals)))
941 ;; Pre-default any argument register that need it.
942 (when (< nvals register-arg-count)
943 (let* ((arg-tns (nthcdr nvals (list a0 a1 a2)))
944 (first (first arg-tns)))
945 (inst mov first nil-value)
946 (dolist (tn (cdr arg-tns))
947 (inst mov tn first))))
948 ;; Set the multiple value return flag.
950 ;; And away we go. Except that return-pc is still on the
951 ;; stack and we've changed the stack pointer. So we have to
952 ;; tell it to index off of EBX instead of EBP.
953 (cond ((<= nvals register-arg-count)
954 (inst mov esp-tn ebp-tn)
958 ;; Some values are on the stack after RETURN-PC and OLD-FP,
959 ;; can't return normally and some slots of the frame will
960 ;; be used as temporaries by the receiver.
962 ;; Clear as much of the stack as possible, but not past the
963 ;; old frame address.
965 (make-ea :dword :base ebp-tn
966 :disp (frame-byte-offset (1- nvals))))
968 (inst push (make-ea :dword :base ebx
969 :disp (frame-byte-offset
971 (tn-offset return-pc)))))
974 (trace-table-entry trace-table-normal)))
976 ;;; Do unknown-values return of an arbitrary number of values (passed
977 ;;; on the stack.) We check for the common case of a single return
978 ;;; value, and do that inline using the normal single value return
979 ;;; convention. Otherwise, we branch off to code that calls an
980 ;;; assembly-routine.
982 ;;; The assembly routine takes the following args:
983 ;;; ECX -- number of values to find there.
984 ;;; ESI -- pointer to where to find the values.
985 (define-vop (return-multiple)
988 (vals :scs (any-reg) :target esi)
989 (nvals :scs (any-reg) :target ecx))
990 (:temporary (:sc unsigned-reg :offset esi-offset :from (:argument 2)) esi)
991 (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 3)) ecx)
992 (:temporary (:sc descriptor-reg :offset (first *register-arg-offsets*)
996 (check-ocfp-and-return-pc old-fp return-pc)
997 (trace-table-entry trace-table-fun-epilogue)
998 (unless (policy node (> space speed))
999 ;; Check for the single case.
1000 (let ((not-single (gen-label)))
1001 (inst cmp nvals (fixnumize 1))
1002 (inst jmp :ne not-single)
1003 ;; Return with one value.
1005 ;; Clear the stack until ocfp.
1006 (inst mov esp-tn ebp-tn)
1007 ;; clear the multiple-value return flag
1012 ;; Nope, not the single case. Jump to the assembly routine.
1013 (emit-label not-single)))
1016 (inst jmp (make-fixup 'return-multiple :assembly-routine))
1017 (trace-table-entry trace-table-normal)))
1021 ;;; We don't need to do anything special for regular functions.
1022 (define-vop (setup-environment)
1026 ;; Don't bother doing anything.
1029 ;;; Get the lexical environment from its passing location.
1030 (define-vop (setup-closure-environment)
1031 (:results (closure :scs (descriptor-reg)))
1036 (move closure eax-tn)))
1038 ;;; Copy a &MORE arg from the argument area to the end of the current
1039 ;;; frame. FIXED is the number of non-&MORE arguments.
1041 ;;; The tricky part is doing this without trashing any of the calling
1042 ;;; convention registers that are still needed. This vop is emitted
1043 ;;; directly after the xep-allocate frame. That means the registers
1044 ;;; are in use as follows:
1046 ;;; EAX -- The lexenv.
1047 ;;; EBX -- Available.
1048 ;;; ECX -- The total number of arguments * N-WORD-BYTES.
1049 ;;; EDX -- The first arg.
1050 ;;; EDI -- The second arg.
1051 ;;; ESI -- The third arg.
1053 ;;; So basically, we have one register available for our use: EBX.
1055 ;;; What we can do is push the other regs onto the stack, and then
1056 ;;; restore their values by looking directly below where we put the
1058 (define-vop (copy-more-arg)
1061 ;; Avoid the copy if there are no more args.
1062 (cond ((zerop fixed)
1063 (inst jecxz JUST-ALLOC-FRAME))
1065 (inst cmp ecx-tn (fixnumize fixed))
1066 (inst jmp :be JUST-ALLOC-FRAME)))
1068 ;; Allocate the space on the stack.
1069 ;; stack = ebp + sp->fp-offset - (max 3 frame-size) - (nargs - fixed)
1071 (make-ea :dword :base ebp-tn
1072 :disp (* n-word-bytes
1073 (- (+ sp->fp-offset fixed)
1074 (max 3 (sb-allocated-size 'stack))))))
1075 (inst sub ebx-tn ecx-tn) ; Got the new stack in ebx
1076 (inst mov esp-tn ebx-tn)
1078 ;; Now: nargs>=1 && nargs>fixed
1080 ;; Save the original count of args.
1081 (inst mov ebx-tn ecx-tn)
1083 (cond ((< fixed register-arg-count)
1084 ;; We must stop when we run out of stack args, not when we
1085 ;; run out of more args.
1086 ;; Number to copy = nargs-3
1087 (inst sub ecx-tn (fixnumize register-arg-count))
1088 ;; Everything of interest in registers.
1089 (inst jmp :be DO-REGS))
1091 ;; Number to copy = nargs-fixed
1092 (inst sub ecx-tn (fixnumize fixed))))
1094 ;; Save edi and esi register args.
1098 ;; Okay, we have pushed the register args. We can trash them
1101 ;; Initialize src to be end of args.
1102 (inst lea esi-tn (make-ea :dword :base ebp-tn
1103 :disp (* sp->fp-offset n-word-bytes)))
1104 (inst sub esi-tn ebx-tn)
1106 ;; We need to copy from downwards up to avoid overwriting some of
1107 ;; the yet uncopied args. So we need to use EBX as the copy index
1108 ;; and ECX as the loop counter, rather than using ECX for both.
1109 (inst xor ebx-tn ebx-tn)
1111 ;; We used to use REP MOVS here, but on modern x86 it performs
1112 ;; much worse than an explicit loop for small blocks.
1114 (inst mov edi-tn (make-ea :dword :base esi-tn :index ebx-tn))
1115 ;; The :DISP is to account for the registers saved on the stack
1116 (inst mov (make-ea :dword :base esp-tn :disp (* 3 n-word-bytes)
1119 (inst add ebx-tn n-word-bytes)
1120 (inst sub ecx-tn n-word-bytes)
1121 (inst jmp :nz COPY-LOOP)
1123 ;; So now we need to restore EDI and ESI.
1131 (inst mov ecx-tn ebx-tn)
1133 ;; Here: nargs>=1 && nargs>fixed
1134 (when (< fixed register-arg-count)
1135 ;; Now we have to deposit any more args that showed up in
1139 ;; Store it relative to ebp
1140 (inst mov (make-ea :dword :base ebp-tn
1141 :disp (* n-word-bytes
1145 (max 3 (sb-allocated-size
1147 (nth i *register-arg-tns*))
1150 (when (>= i register-arg-count)
1153 ;; Don't deposit any more than there are.
1155 (inst test ecx-tn ecx-tn)
1156 (inst cmp ecx-tn (fixnumize i)))
1157 (inst jmp :eq DONE)))
1163 (make-ea :dword :base ebp-tn
1164 :disp (* n-word-bytes
1166 (max 3 (sb-allocated-size 'stack))))))
1170 (define-vop (more-kw-arg)
1171 (:translate sb!c::%more-kw-arg)
1172 (:policy :fast-safe)
1173 (:args (object :scs (descriptor-reg) :to (:result 1))
1174 (index :scs (any-reg immediate) :to (:result 1) :target keyword))
1175 (:arg-types * tagged-num)
1176 (:results (value :scs (descriptor-reg any-reg))
1177 (keyword :scs (descriptor-reg any-reg)))
1182 (inst mov value (make-ea :dword :base object :disp (tn-value index)))
1183 (inst mov keyword (make-ea :dword :base object
1184 :disp (+ (tn-value index) n-word-bytes))))
1186 (inst mov value (make-ea :dword :base object :index index))
1187 (inst mov keyword (make-ea :dword :base object :index index
1188 :disp n-word-bytes))))))
1190 (define-vop (more-arg)
1191 (:translate sb!c::%more-arg)
1192 (:policy :fast-safe)
1193 (:args (object :scs (descriptor-reg) :to (:result 1))
1194 (index :scs (any-reg) :to (:result 1) :target value))
1195 (:arg-types * tagged-num)
1196 (:results (value :scs (descriptor-reg any-reg)))
1201 (inst mov value (make-ea :dword :base object :index value))))
1203 ;;; Turn more arg (context, count) into a list.
1204 (define-vop (listify-rest-args)
1205 (:translate %listify-rest-args)
1207 (:args (context :scs (descriptor-reg) :target src)
1208 (count :scs (any-reg) :target ecx))
1209 (:arg-types * tagged-num)
1210 (:temporary (:sc unsigned-reg :offset esi-offset :from (:argument 0)) src)
1211 (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 1)) ecx)
1212 (:temporary (:sc unsigned-reg :offset eax-offset) eax)
1213 (:temporary (:sc unsigned-reg) dst)
1214 (:results (result :scs (descriptor-reg)))
1217 (let ((enter (gen-label))
1220 (stack-allocate-p (node-stack-allocate-p node)))
1223 ;; Check to see whether there are no args, and just return NIL if so.
1224 (inst mov result nil-value)
1226 (inst lea dst (make-ea :dword :base ecx :index ecx))
1227 (maybe-pseudo-atomic stack-allocate-p
1228 (allocation dst dst node stack-allocate-p list-pointer-lowtag)
1229 ;; Set decrement mode (successive args at lower addresses)
1231 ;; Set up the result.
1233 ;; Jump into the middle of the loop, 'cause that's where we want
1237 ;; Compute a pointer to the next cons.
1238 (inst add dst (* cons-size n-word-bytes))
1239 ;; Store a pointer to this cons in the CDR of the previous cons.
1240 (storew dst dst -1 list-pointer-lowtag)
1242 ;; Grab one value and stash it in the car of this cons.
1244 (storew eax dst 0 list-pointer-lowtag)
1245 ;; Go back for more.
1246 (inst sub ecx n-word-bytes)
1248 ;; NIL out the last cons.
1249 (storew nil-value dst 1 list-pointer-lowtag)
1251 (emit-label done))))
1253 ;;; Return the location and size of the &MORE arg glob created by
1254 ;;; COPY-MORE-ARG. SUPPLIED is the total number of arguments supplied
1255 ;;; (originally passed in ECX). FIXED is the number of non-rest
1258 ;;; We must duplicate some of the work done by COPY-MORE-ARG, since at
1259 ;;; that time the environment is in a pretty brain-damaged state,
1260 ;;; preventing this info from being returned as values. What we do is
1261 ;;; compute supplied - fixed, and return a pointer that many words
1262 ;;; below the current stack top.
1263 (define-vop (more-arg-context)
1264 (:policy :fast-safe)
1265 (:translate sb!c::%more-arg-context)
1266 (:args (supplied :scs (any-reg) :target count))
1267 (:arg-types positive-fixnum (:constant fixnum))
1269 (:results (context :scs (descriptor-reg))
1270 (count :scs (any-reg)))
1271 (:result-types t tagged-num)
1272 (:note "more-arg-context")
1274 (move count supplied)
1275 ;; SP at this point points at the last arg pushed.
1276 ;; Point to the first more-arg, not above it.
1277 (inst lea context (make-ea :dword :base esp-tn
1278 :index count :scale 1
1279 :disp (- (+ (fixnumize fixed) n-word-bytes))))
1280 (unless (zerop fixed)
1281 (inst sub count (fixnumize fixed)))))
1283 ;;; Signal wrong argument count error if NARGS isn't equal to COUNT.
1284 (define-vop (verify-arg-count)
1285 (:policy :fast-safe)
1286 (:translate sb!c::%verify-arg-count)
1287 (:args (nargs :scs (any-reg)))
1288 (:arg-types positive-fixnum (:constant t))
1291 (:save-p :compute-only)
1294 (generate-error-code vop 'invalid-arg-count-error nargs)))
1296 (inst test nargs nargs) ; smaller instruction
1297 (inst cmp nargs (fixnumize count)))
1298 (inst jmp :ne err-lab))))
1300 ;;; Various other error signallers.
1301 (macrolet ((def (name error translate &rest args)
1302 `(define-vop (,name)
1304 `((:policy :fast-safe)
1305 (:translate ,translate)))
1306 (:args ,@(mapcar (lambda (arg)
1307 `(,arg :scs (any-reg descriptor-reg)))
1310 (:save-p :compute-only)
1312 (error-call vop ',error ,@args)))))
1313 (def arg-count-error invalid-arg-count-error
1314 sb!c::%arg-count-error nargs)
1315 (def type-check-error object-not-type-error sb!c::%type-check-error
1317 (def layout-invalid-error layout-invalid-error sb!c::%layout-invalid-error
1319 (def odd-key-args-error odd-key-args-error
1320 sb!c::%odd-key-args-error)
1321 (def unknown-key-arg-error unknown-key-arg-error
1322 sb!c::%unknown-key-arg-error key)
1323 (def nil-fun-returned-error nil-fun-returned-error nil fun))
1327 (defun emit-single-step-test ()
1328 ;; We use different ways of representing whether stepping is on on
1329 ;; +SB-THREAD / -SB-THREAD: on +SB-THREAD, we use a slot in the
1330 ;; thread structure. On -SB-THREAD we use the value of a static
1331 ;; symbol. Things are done this way, since reading a thread-local
1332 ;; slot from a symbol would require an extra register on +SB-THREAD,
1333 ;; and reading a slot from a thread structure would require an extra
1334 ;; register on -SB-THREAD.
1337 (inst cmp (make-ea :dword
1338 :disp (* thread-stepping-slot n-word-bytes))
1341 (inst cmp (make-ea-for-symbol-value sb!impl::*stepping*)
1344 (define-vop (step-instrument-before-vop)
1345 (:policy :fast-safe)
1348 (emit-single-step-test)
1350 (inst break single-step-before-trap)
1352 (note-this-location vop :step-before-vop)))