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 node)
195 (declare (type (or tn-ref null) values)
196 (type unsigned-byte nvals))
197 (let ((type (sb!c::basic-combination-derived-type node)))
200 (note-this-location vop :single-value-return)
202 ((<= (sb!kernel:values-type-max-value-count type)
204 (when (and (named-type-p type)
205 (eq nil (named-type-name type)))
206 ;; The function never returns, it may happen that the code
207 ;; ends right here leavig the :SINGLE-VALUE-RETURN note
208 ;; dangling. Let's emit a NOP.
210 ((not (sb!kernel:values-type-may-be-single-value-p type))
211 (inst mov esp-tn ebx-tn))
212 ((member :cmov *backend-subfeatures*)
213 (inst cmov :c esp-tn ebx-tn))
215 (let ((single-value (gen-label)))
216 (inst jmp :nc single-value)
217 (inst mov esp-tn ebx-tn)
218 (emit-label single-value)))))
219 ((<= nvals register-arg-count)
220 (note-this-location vop :unknown-return)
221 (when (sb!kernel:values-type-may-be-single-value-p type)
222 (let ((regs-defaulted (gen-label)))
223 (inst jmp :c regs-defaulted)
224 ;; Default the unsupplied registers.
225 (let* ((2nd-tn-ref (tn-ref-across values))
226 (2nd-tn (tn-ref-tn 2nd-tn-ref)))
227 (inst mov 2nd-tn nil-value)
230 for tn-ref = (tn-ref-across 2nd-tn-ref)
231 then (tn-ref-across tn-ref)
232 for count from 2 below register-arg-count
233 do (inst mov (tn-ref-tn tn-ref) 2nd-tn))))
234 (inst mov ebx-tn esp-tn)
235 (emit-label regs-defaulted)))
236 (when (< register-arg-count
237 (sb!kernel:values-type-max-value-count type))
238 (inst mov esp-tn ebx-tn)))
240 ;; The number of bytes depends on the relative jump instructions.
241 ;; Best case is 31+(n-3)*14, worst case is 35+(n-3)*18. For
242 ;; NVALS=6 that is 73/89 bytes, and for NVALS=7 that is 87/107
243 ;; bytes which is likely better than using the blt below.
244 (let ((regs-defaulted (gen-label))
245 (defaulting-done (gen-label))
246 (default-stack-slots (gen-label)))
247 (note-this-location vop :unknown-return)
248 ;; Branch off to the MV case.
249 (inst jmp :c regs-defaulted)
250 ;; Do the single value case.
251 ;; Default the register args
252 (inst mov eax-tn nil-value)
254 (val (tn-ref-across values) (tn-ref-across val)))
255 ((= i (min nvals register-arg-count)))
256 (inst mov (tn-ref-tn val) eax-tn))
257 ;; Fake other registers so it looks like we returned with all the
258 ;; registers filled in.
260 (inst jmp default-stack-slots)
261 (emit-label regs-defaulted)
262 (inst mov eax-tn nil-value)
263 (collect ((defaults))
264 (do ((i register-arg-count (1+ i))
265 (val (do ((i 0 (1+ i))
266 (val values (tn-ref-across val)))
267 ((= i register-arg-count) val))
268 (tn-ref-across val)))
270 (let ((default-lab (gen-label))
272 (first-stack-arg-p (= i register-arg-count)))
273 (defaults (cons default-lab
274 (cons tn first-stack-arg-p)))
275 (inst cmp ecx-tn (fixnumize i))
276 (inst jmp :be default-lab)
277 (when first-stack-arg-p
278 ;; There are stack args so the frame of the callee is
279 ;; still there, save EDX in its first slot temporalily.
280 (storew edx-tn ebx-tn (frame-word-offset sp->fp-offset)))
281 (loadw edx-tn ebx-tn (frame-word-offset (+ sp->fp-offset i)))
282 (inst mov tn edx-tn)))
283 (emit-label defaulting-done)
284 (loadw edx-tn ebx-tn (frame-word-offset sp->fp-offset))
286 (let ((defaults (defaults)))
288 (assemble (*elsewhere*)
289 (trace-table-entry trace-table-fun-prologue)
290 (emit-label default-stack-slots)
291 (dolist (default defaults)
292 (emit-label (car default))
294 ;; We are setting the first stack argument to NIL.
295 ;; The callee's stack frame is dead, save EDX by
296 ;; pushing it to the stack, it will end up at same
297 ;; place as in the (STOREW EDX-TN EBX-TN -1) case
300 (inst mov (second default) eax-tn))
301 (inst jmp defaulting-done)
302 (trace-table-entry trace-table-normal)))))))
304 ;; 91 bytes for this branch.
305 (let ((regs-defaulted (gen-label))
306 (restore-edi (gen-label))
307 (no-stack-args (gen-label))
308 (default-stack-vals (gen-label))
309 (count-okay (gen-label)))
310 (note-this-location vop :unknown-return)
311 ;; Branch off to the MV case.
312 (inst jmp :c regs-defaulted)
313 ;; Default the register args, and set up the stack as if we
314 ;; entered the MV return point.
315 (inst mov ebx-tn esp-tn)
316 (inst mov edi-tn nil-value)
317 (inst mov esi-tn edi-tn)
318 ;; Compute a pointer to where to put the [defaulted] stack values.
319 (emit-label no-stack-args)
323 (make-ea :dword :base ebp-tn
324 :disp (frame-byte-offset register-arg-count)))
325 ;; Load EAX with NIL so we can quickly store it, and set up
326 ;; stuff for the loop.
327 (inst mov eax-tn nil-value)
329 (inst mov ecx-tn (- nvals register-arg-count))
330 ;; Jump into the default loop.
331 (inst jmp default-stack-vals)
332 ;; The regs are defaulted. We need to copy any stack arguments,
333 ;; and then default the remaining stack arguments.
334 (emit-label regs-defaulted)
335 ;; Compute the number of stack arguments, and if it's zero or
336 ;; less, don't copy any stack arguments.
337 (inst sub ecx-tn (fixnumize register-arg-count))
338 (inst jmp :le no-stack-args)
340 (storew edi-tn ebx-tn (frame-word-offset (+ sp->fp-offset 1)))
341 ;; Throw away any unwanted args.
342 (inst cmp ecx-tn (fixnumize (- nvals register-arg-count)))
343 (inst jmp :be count-okay)
344 (inst mov ecx-tn (fixnumize (- nvals register-arg-count)))
345 (emit-label count-okay)
346 ;; Save the number of stack values.
347 (inst mov eax-tn ecx-tn)
348 ;; Compute a pointer to where the stack args go.
350 (make-ea :dword :base ebp-tn
351 :disp (frame-byte-offset register-arg-count)))
352 ;; Save ESI, and compute a pointer to where the args come from.
353 (storew esi-tn ebx-tn (frame-word-offset (+ sp->fp-offset 2)))
355 (make-ea :dword :base ebx-tn
356 :disp (frame-byte-offset
357 (+ sp->fp-offset register-arg-count))))
359 (inst shr ecx-tn word-shift) ; make word count
364 (loadw esi-tn ebx-tn (frame-word-offset (+ sp->fp-offset 2)))
365 ;; Now we have to default the remaining args. Find out how many.
366 (inst sub eax-tn (fixnumize (- nvals register-arg-count)))
368 ;; If none, then just blow out of here.
369 (inst jmp :le restore-edi)
370 (inst mov ecx-tn eax-tn)
371 (inst shr ecx-tn word-shift) ; word count
372 ;; Load EAX with NIL for fast storing.
373 (inst mov eax-tn nil-value)
375 (emit-label default-stack-vals)
378 ;; Restore EDI, and reset the stack.
379 (emit-label restore-edi)
380 (loadw edi-tn ebx-tn (frame-word-offset (+ sp->fp-offset 1)))
381 (inst mov esp-tn ebx-tn)
385 ;;;; unknown values receiving
387 ;;; Emit code needed at the return point for an unknown-values call
388 ;;; for an arbitrary number of values.
390 ;;; We do the single and non-single cases with no shared code: there
391 ;;; doesn't seem to be any potential overlap, and receiving a single
392 ;;; value is more important efficiency-wise.
394 ;;; When there is a single value, we just push it on the stack,
395 ;;; returning the old SP and 1.
397 ;;; When there is a variable number of values, we move all of the
398 ;;; argument registers onto the stack, and return ARGS and NARGS.
400 ;;; ARGS and NARGS are TNs wired to the named locations. We must
401 ;;; explicitly allocate these TNs, since their lifetimes overlap with
402 ;;; the results start and count. (Also, it's nice to be able to target
404 (defun receive-unknown-values (args nargs start count node)
405 (declare (type tn args nargs start count))
406 (let ((type (sb!c::basic-combination-derived-type node))
407 (variable-values (gen-label))
408 (stack-values (gen-label))
410 (when (sb!kernel:values-type-may-be-single-value-p type)
411 (inst jmp :c variable-values)
412 (cond ((location= start (first *register-arg-tns*))
413 (inst push (first *register-arg-tns*))
414 (inst lea start (make-ea :dword :base esp-tn :disp n-word-bytes)))
415 (t (inst mov start esp-tn)
416 (inst push (first *register-arg-tns*))))
417 (inst mov count (fixnumize 1))
419 (emit-label variable-values))
420 ;; The stack frame is burnt and RETurned from if there are no
421 ;; stack values. In this case quickly reallocate sufficient space.
422 (when (<= (sb!kernel:values-type-min-value-count type)
424 (inst cmp nargs (fixnumize register-arg-count))
425 (inst jmp :g stack-values)
426 (inst sub esp-tn nargs)
427 (emit-label stack-values))
428 ;; dtc: this writes the registers onto the stack even if they are
429 ;; not needed, only the number specified in ecx are used and have
430 ;; stack allocated to them. No harm is done.
432 for arg in *register-arg-tns*
434 for j below (sb!kernel:values-type-max-value-count type)
435 do (storew arg args i))
442 ;;; VOP that can be inherited by unknown values receivers. The main thing this
443 ;;; handles is allocation of the result temporaries.
444 (define-vop (unknown-values-receiver)
445 (:temporary (:sc descriptor-reg :offset ebx-offset
446 :from :eval :to (:result 0))
448 (:temporary (:sc any-reg :offset ecx-offset
449 :from :eval :to (:result 1))
451 (:results (start :scs (any-reg control-stack))
452 (count :scs (any-reg control-stack))))
454 ;;;; local call with unknown values convention return
456 (defun check-ocfp-and-return-pc (old-fp return-pc)
458 (format t "*known-return: old-fp ~S, tn-kind ~S; ~S ~S~%"
459 old-fp (sb!c::tn-kind old-fp) (sb!c::tn-save-tn old-fp)
460 (sb!c::tn-kind (sb!c::tn-save-tn old-fp)))
462 (format t "*known-return: return-pc ~S, tn-kind ~S; ~S ~S~%"
463 return-pc (sb!c::tn-kind return-pc)
464 (sb!c::tn-save-tn return-pc)
465 (sb!c::tn-kind (sb!c::tn-save-tn return-pc)))
466 (unless (and (sc-is old-fp control-stack)
467 (= (tn-offset old-fp) ocfp-save-offset))
468 (error "ocfp not on stack in standard save location?"))
469 (unless (and (sc-is return-pc sap-stack)
470 (= (tn-offset return-pc) return-pc-save-offset))
471 (error "return-pc not on stack in standard save location?")))
473 ;;; Instead of JMPing to TARGET, CALL a trampoline that saves the
474 ;;; return pc and jumps. Although this is an incredibly stupid trick
475 ;;; the paired CALL/RET instructions are a big win.
476 (defun make-local-call (target)
477 (let ((tramp (gen-label)))
479 (assemble (*elsewhere*)
481 (popw ebp-tn (frame-word-offset return-pc-save-offset))
484 ;;; Non-TR local call for a fixed number of values passed according to
485 ;;; the unknown values convention.
487 ;;; FP is the frame pointer in install before doing the call.
489 ;;; NFP would be the number-stack frame pointer if we had a separate
492 ;;; Args are the argument passing locations, which are specified only
493 ;;; to terminate their lifetimes in the caller.
495 ;;; VALUES are the return value locations (wired to the standard
496 ;;; passing locations). NVALS is the number of values received.
498 ;;; Save is the save info, which we can ignore since saving has been
501 ;;; TARGET is a continuation pointing to the start of the called
503 (define-vop (call-local)
507 (:results (values :more t))
509 (:move-args :local-call)
510 (:info arg-locs callee target nvals)
512 (:ignore nfp arg-locs args callee)
515 (trace-table-entry trace-table-call-site)
517 (note-this-location vop :call-site)
518 (make-local-call target)
519 (default-unknown-values vop values nvals node)
520 (trace-table-entry trace-table-normal)))
522 ;;; Non-TR local call for a variable number of return values passed according
523 ;;; to the unknown values convention. The results are the start of the values
524 ;;; glob and the number of values received.
525 (define-vop (multiple-call-local unknown-values-receiver)
530 (:move-args :local-call)
531 (:info save callee target)
532 (:ignore args save nfp callee)
536 (trace-table-entry trace-table-call-site)
538 (note-this-location vop :call-site)
539 (make-local-call target)
540 (note-this-location vop :unknown-return)
541 (receive-unknown-values values-start nvals start count node)
542 (trace-table-entry trace-table-normal)))
544 ;;;; local call with known values return
546 ;;; Non-TR local call with known return locations. Known-value return
547 ;;; works just like argument passing in local call.
549 ;;; Note: we can't use normal load-tn allocation for the fixed args,
550 ;;; since all registers may be tied up by the more operand. Instead,
551 ;;; we use MAYBE-LOAD-STACK-TN.
552 (define-vop (known-call-local)
556 (:results (res :more t))
557 (:move-args :local-call)
559 (:info save callee target)
560 (:ignore args res save nfp callee)
563 (trace-table-entry trace-table-call-site)
565 (note-this-location vop :call-site)
566 (make-local-call target)
567 (note-this-location vop :known-return)
568 (trace-table-entry trace-table-normal)))
570 ;;; From Douglas Crosher
571 ;;; Return from known values call. We receive the return locations as
572 ;;; arguments to terminate their lifetimes in the returning function. We
573 ;;; restore FP and CSP and jump to the Return-PC.
574 (define-vop (known-return)
578 (:move-args :known-return)
580 (:ignore val-locs vals)
583 (check-ocfp-and-return-pc old-fp return-pc)
584 (trace-table-entry trace-table-fun-epilogue)
585 ;; Zot all of the stack except for the old-fp and return-pc.
586 (inst mov esp-tn ebp-tn)
589 (trace-table-entry trace-table-normal)))
593 ;;; There is something of a cross-product effect with full calls.
594 ;;; Different versions are used depending on whether we know the
595 ;;; number of arguments or the name of the called function, and
596 ;;; whether we want fixed values, unknown values, or a tail call.
598 ;;; In full call, the arguments are passed creating a partial frame on
599 ;;; the stack top and storing stack arguments into that frame. On
600 ;;; entry to the callee, this partial frame is pointed to by FP.
602 ;;; This macro helps in the definition of full call VOPs by avoiding
603 ;;; code replication in defining the cross-product VOPs.
605 ;;; NAME is the name of the VOP to define.
607 ;;; NAMED is true if the first argument is an fdefinition object whose
608 ;;; definition is to be called.
610 ;;; RETURN is either :FIXED, :UNKNOWN or :TAIL:
611 ;;; -- If :FIXED, then the call is for a fixed number of values, returned in
612 ;;; the standard passing locations (passed as result operands).
613 ;;; -- If :UNKNOWN, then the result values are pushed on the stack, and the
614 ;;; result values are specified by the Start and Count as in the
615 ;;; unknown-values continuation representation.
616 ;;; -- If :TAIL, then do a tail-recursive call. No values are returned.
617 ;;; The Old-Fp and Return-PC are passed as the second and third arguments.
619 ;;; In non-tail calls, the pointer to the stack arguments is passed as
620 ;;; the last fixed argument. If Variable is false, then the passing
621 ;;; locations are passed as a more arg. Variable is true if there are
622 ;;; a variable number of arguments passed on the stack. Variable
623 ;;; cannot be specified with :TAIL return. TR variable argument call
624 ;;; is implemented separately.
626 ;;; In tail call with fixed arguments, the passing locations are
627 ;;; passed as a more arg, but there is no new-FP, since the arguments
628 ;;; have been set up in the current frame.
629 (macrolet ((define-full-call (name named return variable)
630 (aver (not (and variable (eq return :tail))))
632 ,@(when (eq return :unknown)
633 '(unknown-values-receiver)))
635 ,@(unless (eq return :tail)
636 '((new-fp :scs (any-reg) :to (:argument 1))))
638 (fun :scs (descriptor-reg control-stack)
639 :target eax :to (:argument 0))
641 ,@(when (eq return :tail)
645 ,@(unless variable '((args :more t :scs (descriptor-reg)))))
647 ,@(when (eq return :fixed)
648 '((:results (values :more t))))
650 (:save-p ,(if (eq return :tail) :compute-only t))
652 ,@(unless (or (eq return :tail) variable)
653 '((:move-args :full-call)))
657 ,@(unless (or variable (eq return :tail)) '(arg-locs))
658 ,@(unless variable '(nargs))
659 ,@(when (eq return :fixed) '(nvals))
663 ,@(unless (or variable (eq return :tail)) '(arg-locs))
664 ,@(unless variable '(args)))
666 ;; We pass either the fdefn object (for named call) or
667 ;; the actual function object (for unnamed call) in
668 ;; EAX. With named call, closure-tramp will replace it
669 ;; with the real function and invoke the real function
670 ;; for closures. Non-closures do not need this value,
671 ;; so don't care what shows up in it.
679 ;; We pass the number of arguments in ECX.
680 (:temporary (:sc unsigned-reg :offset ecx-offset :to :eval) ecx)
682 ;; With variable call, we have to load the
683 ;; register-args out of the (new) stack frame before
684 ;; doing the call. Therefore, we have to tell the
685 ;; lifetime stuff that we need to use them.
687 (mapcar (lambda (name offset)
688 `(:temporary (:sc descriptor-reg
693 *register-arg-names* *register-arg-offsets*))
695 ,@(when (eq return :tail)
696 '((:temporary (:sc unsigned-reg
700 ,@(unless (eq return :tail)
703 (:generator ,(+ (if named 5 0)
705 (if (eq return :tail) 0 10)
707 (if (eq return :unknown) 25 0))
708 (trace-table-entry trace-table-call-site)
710 ;; This has to be done before the frame pointer is
711 ;; changed! EAX stores the 'lexical environment' needed
717 ;; For variable call, compute the number of
718 ;; arguments and move some of the arguments to
721 ;; Compute the number of arguments.
722 (noise '(inst mov ecx new-fp))
723 (noise '(inst sub ecx esp-tn))
724 ;; Move the necessary args to registers,
725 ;; this moves them all even if they are
728 for name in *register-arg-names*
729 for index downfrom -1
730 do (noise `(loadw ,name new-fp ,index)))
734 (inst mov ecx (fixnumize nargs)))))
735 ,@(cond ((eq return :tail)
736 '(;; Python has figured out what frame we should
737 ;; return to so might as well use that clue.
738 ;; This seems really important to the
739 ;; implementation of things like
740 ;; (without-interrupts ...)
742 ;; dtc; Could be doing a tail call from a
743 ;; known-local-call etc in which the old-fp
744 ;; or ret-pc are in regs or in non-standard
745 ;; places. If the passing location were
746 ;; wired to the stack in standard locations
747 ;; then these moves will be un-necessary;
748 ;; this is probably best for the x86.
751 (unless (= ocfp-save-offset
753 ;; FIXME: FORMAT T for stale
754 ;; diagnostic output (several of
755 ;; them around here), ick
756 (error "** tail-call old-fp not S0~%")
757 (move old-fp-tmp old-fp)
760 (frame-word-offset ocfp-save-offset))))
761 ((any-reg descriptor-reg)
762 (error "** tail-call old-fp in reg not S0~%")
765 (frame-word-offset ocfp-save-offset))))
767 ;; For tail call, we have to push the
768 ;; return-pc so that it looks like we CALLed
769 ;; despite the fact that we are going to JMP.
770 (inst push return-pc)
773 ;; For non-tail call, we have to save our
774 ;; frame pointer and install the new frame
775 ;; pointer. We can't load stack tns after this
777 `(;; Python doesn't seem to allocate a frame
778 ;; here which doesn't leave room for the
781 ;; The variable args are on the stack and
782 ;; become the frame, but there may be <3
783 ;; args and 3 stack slots are assumed
784 ;; allocate on the call. So need to ensure
785 ;; there are at least 3 slots. This hack
788 '(inst sub esp-tn (fixnumize 3)))
790 ;; Bias the new-fp for use as an fp
792 '(inst sub new-fp (fixnumize sp->fp-offset)))
795 (storew ebp-tn new-fp
796 (frame-word-offset ocfp-save-offset))
798 (move ebp-tn new-fp) ; NB - now on new stack frame.
801 (when step-instrumenting
802 (emit-single-step-test)
804 (inst break single-step-around-trap))
807 (note-this-location vop :call-site)
809 (inst ,(if (eq return :tail) 'jmp 'call)
811 '(make-ea-for-object-slot eax fdefn-raw-addr-slot
812 other-pointer-lowtag)
813 '(make-ea-for-object-slot eax closure-fun-slot
814 fun-pointer-lowtag)))
817 '((default-unknown-values vop values nvals node)))
819 '((note-this-location vop :unknown-return)
820 (receive-unknown-values values-start nvals start count
823 (trace-table-entry trace-table-normal)))))
825 (define-full-call call nil :fixed nil)
826 (define-full-call call-named t :fixed nil)
827 (define-full-call multiple-call nil :unknown nil)
828 (define-full-call multiple-call-named t :unknown nil)
829 (define-full-call tail-call nil :tail nil)
830 (define-full-call tail-call-named t :tail nil)
832 (define-full-call call-variable nil :fixed t)
833 (define-full-call multiple-call-variable nil :unknown t))
835 ;;; This is defined separately, since it needs special code that BLT's
836 ;;; the arguments down. All the real work is done in the assembly
837 ;;; routine. We just set things up so that it can find what it needs.
838 (define-vop (tail-call-variable)
839 (:args (args :scs (any-reg control-stack) :target esi)
840 (function :scs (descriptor-reg control-stack) :target eax)
843 (:temporary (:sc unsigned-reg :offset esi-offset :from (:argument 0)) esi)
844 (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 1)) eax)
846 (check-ocfp-and-return-pc old-fp return-pc)
847 ;; Move these into the passing locations if they are not already there.
850 ;; And jump to the assembly routine.
851 (inst jmp (make-fixup 'tail-call-variable :assembly-routine))))
853 ;;;; unknown values return
855 ;;; Return a single-value using the Unknown-Values convention.
857 ;;; pfw--get wired-tn conflicts sometimes if register sc specd for args
858 ;;; having problems targeting args to regs -- using temps instead.
860 ;;; First off, modifying the return-pc defeats the branch-prediction
861 ;;; optimizations on modern CPUs quite handily. Second, we can do all
862 ;;; this without needing a temp register. Fixed the latter, at least.
863 ;;; -- AB 2006/Feb/04
864 (define-vop (return-single)
870 (check-ocfp-and-return-pc old-fp return-pc)
871 (trace-table-entry trace-table-fun-epilogue)
872 ;; Drop stack above old-fp
873 (inst mov esp-tn ebp-tn)
874 ;; Clear the multiple-value return flag
876 ;; Restore the old frame pointer
881 ;;; Do unknown-values return of a fixed (other than 1) number of
882 ;;; values. The VALUES are required to be set up in the standard
883 ;;; passing locations. NVALS is the number of values returned.
885 ;;; Basically, we just load ECX with the number of values returned and
886 ;;; EBX with a pointer to the values, set ESP to point to the end of
887 ;;; the values, and jump directly to return-pc.
890 (return-pc :to (:eval 1))
894 ;; In the case of other than one value, we need these registers to
895 ;; tell the caller where they are and how many there are.
896 (:temporary (:sc unsigned-reg :offset ebx-offset) ebx)
897 (:temporary (:sc unsigned-reg :offset ecx-offset) ecx)
898 ;; We need to stretch the lifetime of return-pc past the argument
899 ;; registers so that we can default the argument registers without
900 ;; trashing return-pc.
901 (:temporary (:sc unsigned-reg :offset (first *register-arg-offsets*)
903 (:temporary (:sc unsigned-reg :offset (second *register-arg-offsets*)
905 (:temporary (:sc unsigned-reg :offset (third *register-arg-offsets*)
909 (check-ocfp-and-return-pc old-fp return-pc)
911 ;; This is handled in RETURN-SINGLE.
912 (error "nvalues is 1"))
913 (trace-table-entry trace-table-fun-epilogue)
914 ;; Establish the values pointer and values count.
915 (inst lea ebx (make-ea :dword :base ebp-tn
916 :disp (* sp->fp-offset n-word-bytes)))
918 (inst xor ecx ecx) ; smaller
919 (inst mov ecx (fixnumize nvals)))
920 ;; Pre-default any argument register that need it.
921 (when (< nvals register-arg-count)
922 (let* ((arg-tns (nthcdr nvals (list a0 a1 a2)))
923 (first (first arg-tns)))
924 (inst mov first nil-value)
925 (dolist (tn (cdr arg-tns))
926 (inst mov tn first))))
927 ;; Set the multiple value return flag.
929 ;; And away we go. Except that return-pc is still on the
930 ;; stack and we've changed the stack pointer. So we have to
931 ;; tell it to index off of EBX instead of EBP.
932 (cond ((<= nvals register-arg-count)
933 (inst mov esp-tn ebp-tn)
937 ;; Some values are on the stack after RETURN-PC and OLD-FP,
938 ;; can't return normally and some slots of the frame will
939 ;; be used as temporaries by the receiver.
941 ;; Clear as much of the stack as possible, but not past the
942 ;; old frame address.
944 (make-ea :dword :base ebp-tn
945 :disp (frame-byte-offset (1- nvals))))
947 (inst push (make-ea :dword :base ebx
948 :disp (frame-byte-offset
950 (tn-offset return-pc)))))
953 (trace-table-entry trace-table-normal)))
955 ;;; Do unknown-values return of an arbitrary number of values (passed
956 ;;; on the stack.) We check for the common case of a single return
957 ;;; value, and do that inline using the normal single value return
958 ;;; convention. Otherwise, we branch off to code that calls an
959 ;;; assembly-routine.
961 ;;; The assembly routine takes the following args:
962 ;;; ECX -- number of values to find there.
963 ;;; ESI -- pointer to where to find the values.
964 (define-vop (return-multiple)
967 (vals :scs (any-reg) :target esi)
968 (nvals :scs (any-reg) :target ecx))
969 (:temporary (:sc unsigned-reg :offset esi-offset :from (:argument 2)) esi)
970 (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 3)) ecx)
971 (:temporary (:sc descriptor-reg :offset (first *register-arg-offsets*)
975 (check-ocfp-and-return-pc old-fp return-pc)
976 (trace-table-entry trace-table-fun-epilogue)
977 (unless (policy node (> space speed))
978 ;; Check for the single case.
979 (let ((not-single (gen-label)))
980 (inst cmp nvals (fixnumize 1))
981 (inst jmp :ne not-single)
982 ;; Return with one value.
984 ;; Clear the stack until ocfp.
985 (inst mov esp-tn ebp-tn)
986 ;; clear the multiple-value return flag
991 ;; Nope, not the single case. Jump to the assembly routine.
992 (emit-label not-single)))
995 (inst jmp (make-fixup 'return-multiple :assembly-routine))
996 (trace-table-entry trace-table-normal)))
1000 ;;; We don't need to do anything special for regular functions.
1001 (define-vop (setup-environment)
1005 ;; Don't bother doing anything.
1008 ;;; Get the lexical environment from its passing location.
1009 (define-vop (setup-closure-environment)
1010 (:results (closure :scs (descriptor-reg)))
1015 (move closure eax-tn)))
1017 ;;; Copy a &MORE arg from the argument area to the end of the current
1018 ;;; frame. FIXED is the number of non-&MORE arguments.
1020 ;;; The tricky part is doing this without trashing any of the calling
1021 ;;; convention registers that are still needed. This vop is emitted
1022 ;;; directly after the xep-allocate frame. That means the registers
1023 ;;; are in use as follows:
1025 ;;; EAX -- The lexenv.
1026 ;;; EBX -- Available.
1027 ;;; ECX -- The total number of arguments * N-WORD-BYTES.
1028 ;;; EDX -- The first arg.
1029 ;;; EDI -- The second arg.
1030 ;;; ESI -- The third arg.
1032 ;;; So basically, we have one register available for our use: EBX.
1034 ;;; What we can do is push the other regs onto the stack, and then
1035 ;;; restore their values by looking directly below where we put the
1037 (define-vop (copy-more-arg)
1040 ;; Avoid the copy if there are no more args.
1041 (cond ((zerop fixed)
1042 (inst jecxz JUST-ALLOC-FRAME))
1044 (inst cmp ecx-tn (fixnumize fixed))
1045 (inst jmp :be JUST-ALLOC-FRAME)))
1047 ;; Allocate the space on the stack.
1048 ;; stack = ebp + sp->fp-offset - (max 3 frame-size) - (nargs - fixed)
1050 (make-ea :dword :base ebp-tn
1051 :disp (* n-word-bytes
1052 (- (+ sp->fp-offset fixed)
1053 (max 3 (sb-allocated-size 'stack))))))
1054 (inst sub ebx-tn ecx-tn) ; Got the new stack in ebx
1055 (inst mov esp-tn ebx-tn)
1057 ;; Now: nargs>=1 && nargs>fixed
1059 ;; Save the original count of args.
1060 (inst mov ebx-tn ecx-tn)
1062 (cond ((< fixed register-arg-count)
1063 ;; We must stop when we run out of stack args, not when we
1064 ;; run out of more args.
1065 ;; Number to copy = nargs-3
1066 (inst sub ecx-tn (fixnumize register-arg-count))
1067 ;; Everything of interest in registers.
1068 (inst jmp :be DO-REGS))
1070 ;; Number to copy = nargs-fixed
1071 (inst sub ecx-tn (fixnumize fixed))))
1073 ;; Save edi and esi register args.
1077 ;; Okay, we have pushed the register args. We can trash them
1080 ;; Initialize src to be end of args.
1081 (inst lea esi-tn (make-ea :dword :base ebp-tn
1082 :disp (* sp->fp-offset n-word-bytes)))
1083 (inst sub esi-tn ebx-tn)
1085 ;; We need to copy from downwards up to avoid overwriting some of
1086 ;; the yet uncopied args. So we need to use EBX as the copy index
1087 ;; and ECX as the loop counter, rather than using ECX for both.
1088 (inst xor ebx-tn ebx-tn)
1090 ;; We used to use REP MOVS here, but on modern x86 it performs
1091 ;; much worse than an explicit loop for small blocks.
1093 (inst mov edi-tn (make-ea :dword :base esi-tn :index ebx-tn))
1094 ;; The :DISP is to account for the registers saved on the stack
1095 (inst mov (make-ea :dword :base esp-tn :disp (* 3 n-word-bytes)
1098 (inst add ebx-tn n-word-bytes)
1099 (inst sub ecx-tn n-word-bytes)
1100 (inst jmp :nz COPY-LOOP)
1102 ;; So now we need to restore EDI and ESI.
1110 (inst mov ecx-tn ebx-tn)
1112 ;; Here: nargs>=1 && nargs>fixed
1113 (when (< fixed register-arg-count)
1114 ;; Now we have to deposit any more args that showed up in
1118 ;; Store it relative to ebp
1119 (inst mov (make-ea :dword :base ebp-tn
1120 :disp (* n-word-bytes
1124 (max 3 (sb-allocated-size
1126 (nth i *register-arg-tns*))
1129 (when (>= i register-arg-count)
1132 ;; Don't deposit any more than there are.
1134 (inst test ecx-tn ecx-tn)
1135 (inst cmp ecx-tn (fixnumize i)))
1136 (inst jmp :eq DONE)))
1142 (make-ea :dword :base ebp-tn
1143 :disp (* n-word-bytes
1145 (max 3 (sb-allocated-size 'stack))))))
1149 (define-vop (more-kw-arg)
1150 (:translate sb!c::%more-kw-arg)
1151 (:policy :fast-safe)
1152 (:args (object :scs (descriptor-reg) :to (:result 1))
1153 (index :scs (any-reg immediate) :to (:result 1) :target keyword))
1154 (:arg-types * tagged-num)
1155 (:results (value :scs (descriptor-reg any-reg))
1156 (keyword :scs (descriptor-reg any-reg)))
1161 (inst mov value (make-ea :dword :base object :disp (tn-value index)))
1162 (inst mov keyword (make-ea :dword :base object
1163 :disp (+ (tn-value index) n-word-bytes))))
1165 (inst mov value (make-ea :dword :base object :index index))
1166 (inst mov keyword (make-ea :dword :base object :index index
1167 :disp n-word-bytes))))))
1169 (define-vop (more-arg)
1170 (:translate sb!c::%more-arg)
1171 (:policy :fast-safe)
1172 (:args (object :scs (descriptor-reg) :to (:result 1))
1173 (index :scs (any-reg) :to (:result 1) :target value))
1174 (:arg-types * tagged-num)
1175 (:results (value :scs (descriptor-reg any-reg)))
1180 (inst mov value (make-ea :dword :base object :index value))))
1182 ;;; Turn more arg (context, count) into a list.
1183 (define-vop (listify-rest-args)
1184 (:translate %listify-rest-args)
1186 (:args (context :scs (descriptor-reg) :target src)
1187 (count :scs (any-reg) :target ecx))
1188 (:arg-types * tagged-num)
1189 (:temporary (:sc unsigned-reg :offset esi-offset :from (:argument 0)) src)
1190 (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 1)) ecx)
1191 (:temporary (:sc unsigned-reg :offset eax-offset) eax)
1192 (:temporary (:sc unsigned-reg) dst)
1193 (:results (result :scs (descriptor-reg)))
1196 (let ((enter (gen-label))
1199 (stack-allocate-p (node-stack-allocate-p node)))
1202 ;; Check to see whether there are no args, and just return NIL if so.
1203 (inst mov result nil-value)
1205 (inst lea dst (make-ea :dword :base ecx :index ecx))
1206 (maybe-pseudo-atomic stack-allocate-p
1207 (allocation dst dst node stack-allocate-p list-pointer-lowtag)
1208 ;; Set decrement mode (successive args at lower addresses)
1210 ;; Set up the result.
1212 ;; Jump into the middle of the loop, 'cause that's where we want
1216 ;; Compute a pointer to the next cons.
1217 (inst add dst (* cons-size n-word-bytes))
1218 ;; Store a pointer to this cons in the CDR of the previous cons.
1219 (storew dst dst -1 list-pointer-lowtag)
1221 ;; Grab one value and stash it in the car of this cons.
1223 (storew eax dst 0 list-pointer-lowtag)
1224 ;; Go back for more.
1225 (inst sub ecx n-word-bytes)
1227 ;; NIL out the last cons.
1228 (storew nil-value dst 1 list-pointer-lowtag)
1230 (emit-label done))))
1232 ;;; Return the location and size of the &MORE arg glob created by
1233 ;;; COPY-MORE-ARG. SUPPLIED is the total number of arguments supplied
1234 ;;; (originally passed in ECX). FIXED is the number of non-rest
1237 ;;; We must duplicate some of the work done by COPY-MORE-ARG, since at
1238 ;;; that time the environment is in a pretty brain-damaged state,
1239 ;;; preventing this info from being returned as values. What we do is
1240 ;;; compute supplied - fixed, and return a pointer that many words
1241 ;;; below the current stack top.
1242 (define-vop (more-arg-context)
1243 (:policy :fast-safe)
1244 (:translate sb!c::%more-arg-context)
1245 (:args (supplied :scs (any-reg) :target count))
1246 (:arg-types positive-fixnum (:constant fixnum))
1248 (:results (context :scs (descriptor-reg))
1249 (count :scs (any-reg)))
1250 (:result-types t tagged-num)
1251 (:note "more-arg-context")
1253 (move count supplied)
1254 ;; SP at this point points at the last arg pushed.
1255 ;; Point to the first more-arg, not above it.
1256 (inst lea context (make-ea :dword :base esp-tn
1257 :index count :scale 1
1258 :disp (- (+ (fixnumize fixed) n-word-bytes))))
1259 (unless (zerop fixed)
1260 (inst sub count (fixnumize fixed)))))
1262 ;;; Signal wrong argument count error if NARGS isn't equal to COUNT.
1263 (define-vop (verify-arg-count)
1264 (:policy :fast-safe)
1265 (:translate sb!c::%verify-arg-count)
1266 (:args (nargs :scs (any-reg)))
1267 (:arg-types positive-fixnum (:constant t))
1270 (:save-p :compute-only)
1273 (generate-error-code vop 'invalid-arg-count-error nargs)))
1275 (inst test nargs nargs) ; smaller instruction
1276 (inst cmp nargs (fixnumize count)))
1277 (inst jmp :ne err-lab))))
1279 ;;; Various other error signallers.
1280 (macrolet ((def (name error translate &rest args)
1281 `(define-vop (,name)
1283 `((:policy :fast-safe)
1284 (:translate ,translate)))
1285 (:args ,@(mapcar (lambda (arg)
1286 `(,arg :scs (any-reg descriptor-reg)))
1289 (:save-p :compute-only)
1291 (error-call vop ',error ,@args)))))
1292 (def arg-count-error invalid-arg-count-error
1293 sb!c::%arg-count-error nargs)
1294 (def type-check-error object-not-type-error sb!c::%type-check-error
1296 (def layout-invalid-error layout-invalid-error sb!c::%layout-invalid-error
1298 (def odd-key-args-error odd-key-args-error
1299 sb!c::%odd-key-args-error)
1300 (def unknown-key-arg-error unknown-key-arg-error
1301 sb!c::%unknown-key-arg-error key)
1302 (def nil-fun-returned-error nil-fun-returned-error nil fun))
1306 (defun emit-single-step-test ()
1307 ;; We use different ways of representing whether stepping is on on
1308 ;; +SB-THREAD / -SB-THREAD: on +SB-THREAD, we use a slot in the
1309 ;; thread structure. On -SB-THREAD we use the value of a static
1310 ;; symbol. Things are done this way, since reading a thread-local
1311 ;; slot from a symbol would require an extra register on +SB-THREAD,
1312 ;; and reading a slot from a thread structure would require an extra
1313 ;; register on -SB-THREAD.
1316 (inst cmp (make-ea :dword
1317 :disp (* thread-stepping-slot n-word-bytes))
1320 (inst cmp (make-ea-for-symbol-value sb!impl::*stepping*)
1323 (define-vop (step-instrument-before-vop)
1324 (:policy :fast-safe)
1327 (emit-single-step-test)
1329 (inst break single-step-before-trap)
1331 (note-this-location vop :step-before-vop)))