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 ;;; Non-TR local call for a fixed number of values passed according to
474 ;;; the unknown values convention.
476 ;;; FP is the frame pointer in install before doing the call.
478 ;;; NFP would be the number-stack frame pointer if we had a separate
481 ;;; Args are the argument passing locations, which are specified only
482 ;;; to terminate their lifetimes in the caller.
484 ;;; VALUES are the return value locations (wired to the standard
485 ;;; passing locations). NVALS is the number of values received.
487 ;;; Save is the save info, which we can ignore since saving has been
490 ;;; TARGET is a continuation pointing to the start of the called
492 (define-vop (call-local)
496 (:results (values :more t))
498 (:move-args :local-call)
499 (:info arg-locs callee target nvals)
501 (:ignore nfp arg-locs args #+nil callee)
504 (trace-table-entry trace-table-call-site)
507 (let ((ret-tn (callee-return-pc-tn callee)))
509 (format t "*call-local ~S; tn-kind ~S; tn-save-tn ~S; its tn-kind ~S~%"
510 ret-tn (sb!c::tn-kind ret-tn) (sb!c::tn-save-tn ret-tn)
511 (sb!c::tn-kind (sb!c::tn-save-tn ret-tn)))
513 ;; Is the return-pc on the stack or in a register?
516 (unless (= (tn-offset ret-tn) return-pc-save-offset)
517 (error "ret-tn ~A in wrong stack slot" ret-tn))
518 #+nil (format t "*call-local: ret-tn on stack; offset=~S~%"
520 (storew (make-fixup nil :code-object RETURN)
521 ebp-tn (frame-word-offset (tn-offset ret-tn))))
523 (error "ret-tn ~A in sap-reg" ret-tn))))
525 (note-this-location vop :call-site)
528 (default-unknown-values vop values nvals node)
529 (trace-table-entry trace-table-normal)))
531 ;;; Non-TR local call for a variable number of return values passed according
532 ;;; to the unknown values convention. The results are the start of the values
533 ;;; glob and the number of values received.
534 (define-vop (multiple-call-local unknown-values-receiver)
539 (:move-args :local-call)
540 (:info save callee target)
541 (:ignore args save nfp #+nil callee)
545 (trace-table-entry trace-table-call-site)
548 (let ((ret-tn (callee-return-pc-tn callee)))
550 (format t "*multiple-call-local ~S; tn-kind ~S; tn-save-tn ~S; its tn-kind ~S~%"
551 ret-tn (sb!c::tn-kind ret-tn) (sb!c::tn-save-tn ret-tn)
552 (sb!c::tn-kind (sb!c::tn-save-tn ret-tn)))
554 ;; Is the return-pc on the stack or in a register?
557 #+nil (format t "*multiple-call-local: ret-tn on stack; offset=~S~%"
560 (storew (make-fixup nil :code-object RETURN)
561 ebp-tn (frame-word-offset (tn-offset ret-tn))))
563 (error "multiple-call-local: return-pc not on stack."))))
565 (note-this-location vop :call-site)
568 (note-this-location vop :unknown-return)
569 (receive-unknown-values values-start nvals start count node)
570 (trace-table-entry trace-table-normal)))
572 ;;;; local call with known values return
574 ;;; Non-TR local call with known return locations. Known-value return
575 ;;; works just like argument passing in local call.
577 ;;; Note: we can't use normal load-tn allocation for the fixed args,
578 ;;; since all registers may be tied up by the more operand. Instead,
579 ;;; we use MAYBE-LOAD-STACK-TN.
580 (define-vop (known-call-local)
584 (:results (res :more t))
585 (:move-args :local-call)
587 (:info save callee target)
588 (:ignore args res save nfp #+nil callee)
591 (trace-table-entry trace-table-call-site)
594 (let ((ret-tn (callee-return-pc-tn callee)))
597 (format t "*known-call-local ~S; tn-kind ~S; tn-save-tn ~S; its tn-kind ~S~%"
598 ret-tn (sb!c::tn-kind ret-tn) (sb!c::tn-save-tn ret-tn)
599 (sb!c::tn-kind (sb!c::tn-save-tn ret-tn)))
601 ;; Is the return-pc on the stack or in a register?
604 #+nil (format t "*known-call-local: ret-tn on stack; offset=~S~%"
607 (storew (make-fixup nil :code-object RETURN)
608 ebp-tn (frame-word-offset (tn-offset ret-tn))))
610 (error "known-call-local: return-pc not on stack."))))
612 (note-this-location vop :call-site)
615 (note-this-location vop :known-return)
616 (trace-table-entry trace-table-normal)))
618 ;;; From Douglas Crosher
619 ;;; Return from known values call. We receive the return locations as
620 ;;; arguments to terminate their lifetimes in the returning function. We
621 ;;; restore FP and CSP and jump to the Return-PC.
622 (define-vop (known-return)
626 (:move-args :known-return)
628 (:ignore val-locs vals)
631 (check-ocfp-and-return-pc old-fp return-pc)
632 (trace-table-entry trace-table-fun-epilogue)
633 ;; Zot all of the stack except for the old-fp and return-pc.
634 (inst mov esp-tn ebp-tn)
637 (trace-table-entry trace-table-normal)))
641 ;;; There is something of a cross-product effect with full calls.
642 ;;; Different versions are used depending on whether we know the
643 ;;; number of arguments or the name of the called function, and
644 ;;; whether we want fixed values, unknown values, or a tail call.
646 ;;; In full call, the arguments are passed creating a partial frame on
647 ;;; the stack top and storing stack arguments into that frame. On
648 ;;; entry to the callee, this partial frame is pointed to by FP.
650 ;;; This macro helps in the definition of full call VOPs by avoiding
651 ;;; code replication in defining the cross-product VOPs.
653 ;;; NAME is the name of the VOP to define.
655 ;;; NAMED is true if the first argument is an fdefinition object whose
656 ;;; definition is to be called.
658 ;;; RETURN is either :FIXED, :UNKNOWN or :TAIL:
659 ;;; -- If :FIXED, then the call is for a fixed number of values, returned in
660 ;;; the standard passing locations (passed as result operands).
661 ;;; -- If :UNKNOWN, then the result values are pushed on the stack, and the
662 ;;; result values are specified by the Start and Count as in the
663 ;;; unknown-values continuation representation.
664 ;;; -- If :TAIL, then do a tail-recursive call. No values are returned.
665 ;;; The Old-Fp and Return-PC are passed as the second and third arguments.
667 ;;; In non-tail calls, the pointer to the stack arguments is passed as
668 ;;; the last fixed argument. If Variable is false, then the passing
669 ;;; locations are passed as a more arg. Variable is true if there are
670 ;;; a variable number of arguments passed on the stack. Variable
671 ;;; cannot be specified with :TAIL return. TR variable argument call
672 ;;; is implemented separately.
674 ;;; In tail call with fixed arguments, the passing locations are
675 ;;; passed as a more arg, but there is no new-FP, since the arguments
676 ;;; have been set up in the current frame.
677 (macrolet ((define-full-call (name named return variable)
678 (aver (not (and variable (eq return :tail))))
680 ,@(when (eq return :unknown)
681 '(unknown-values-receiver)))
683 ,@(unless (eq return :tail)
684 '((new-fp :scs (any-reg) :to (:argument 1))))
686 (fun :scs (descriptor-reg control-stack)
687 :target eax :to (:argument 0))
689 ,@(when (eq return :tail)
693 ,@(unless variable '((args :more t :scs (descriptor-reg)))))
695 ,@(when (eq return :fixed)
696 '((:results (values :more t))))
698 (:save-p ,(if (eq return :tail) :compute-only t))
700 ,@(unless (or (eq return :tail) variable)
701 '((:move-args :full-call)))
705 ,@(unless (or variable (eq return :tail)) '(arg-locs))
706 ,@(unless variable '(nargs))
707 ,@(when (eq return :fixed) '(nvals))
711 ,@(unless (or variable (eq return :tail)) '(arg-locs))
712 ,@(unless variable '(args)))
714 ;; We pass either the fdefn object (for named call) or
715 ;; the actual function object (for unnamed call) in
716 ;; EAX. With named call, closure-tramp will replace it
717 ;; with the real function and invoke the real function
718 ;; for closures. Non-closures do not need this value,
719 ;; so don't care what shows up in it.
727 ;; We pass the number of arguments in ECX.
728 (:temporary (:sc unsigned-reg :offset ecx-offset :to :eval) ecx)
730 ;; With variable call, we have to load the
731 ;; register-args out of the (new) stack frame before
732 ;; doing the call. Therefore, we have to tell the
733 ;; lifetime stuff that we need to use them.
735 (mapcar (lambda (name offset)
736 `(:temporary (:sc descriptor-reg
741 *register-arg-names* *register-arg-offsets*))
743 ,@(when (eq return :tail)
744 '((:temporary (:sc unsigned-reg
748 ,@(unless (eq return :tail)
751 (:generator ,(+ (if named 5 0)
753 (if (eq return :tail) 0 10)
755 (if (eq return :unknown) 25 0))
756 (trace-table-entry trace-table-call-site)
758 ;; This has to be done before the frame pointer is
759 ;; changed! EAX stores the 'lexical environment' needed
765 ;; For variable call, compute the number of
766 ;; arguments and move some of the arguments to
769 ;; Compute the number of arguments.
770 (noise '(inst mov ecx new-fp))
771 (noise '(inst sub ecx esp-tn))
772 ;; Move the necessary args to registers,
773 ;; this moves them all even if they are
776 for name in *register-arg-names*
777 for index downfrom -1
778 do (noise `(loadw ,name new-fp ,index)))
782 (inst mov ecx (fixnumize nargs)))))
783 ,@(cond ((eq return :tail)
784 '(;; Python has figured out what frame we should
785 ;; return to so might as well use that clue.
786 ;; This seems really important to the
787 ;; implementation of things like
788 ;; (without-interrupts ...)
790 ;; dtc; Could be doing a tail call from a
791 ;; known-local-call etc in which the old-fp
792 ;; or ret-pc are in regs or in non-standard
793 ;; places. If the passing location were
794 ;; wired to the stack in standard locations
795 ;; then these moves will be un-necessary;
796 ;; this is probably best for the x86.
799 (unless (= ocfp-save-offset
801 ;; FIXME: FORMAT T for stale
802 ;; diagnostic output (several of
803 ;; them around here), ick
804 (error "** tail-call old-fp not S0~%")
805 (move old-fp-tmp old-fp)
808 (frame-word-offset ocfp-save-offset))))
809 ((any-reg descriptor-reg)
810 (error "** tail-call old-fp in reg not S0~%")
813 (frame-word-offset ocfp-save-offset))))
815 ;; For tail call, we have to push the
816 ;; return-pc so that it looks like we CALLed
817 ;; despite the fact that we are going to JMP.
818 (inst push return-pc)
821 ;; For non-tail call, we have to save our
822 ;; frame pointer and install the new frame
823 ;; pointer. We can't load stack tns after this
825 `(;; Python doesn't seem to allocate a frame
826 ;; here which doesn't leave room for the
829 ;; The variable args are on the stack and
830 ;; become the frame, but there may be <3
831 ;; args and 3 stack slots are assumed
832 ;; allocate on the call. So need to ensure
833 ;; there are at least 3 slots. This hack
836 '(inst sub esp-tn (fixnumize 3)))
838 ;; Bias the new-fp for use as an fp
840 '(inst sub new-fp (fixnumize sp->fp-offset)))
843 (storew ebp-tn new-fp
844 (frame-word-offset ocfp-save-offset))
846 (move ebp-tn new-fp) ; NB - now on new stack frame.
849 (when step-instrumenting
850 (emit-single-step-test)
852 (inst break single-step-around-trap))
855 (note-this-location vop :call-site)
857 (inst ,(if (eq return :tail) 'jmp 'call)
859 '(make-ea-for-object-slot eax fdefn-raw-addr-slot
860 other-pointer-lowtag)
861 '(make-ea-for-object-slot eax closure-fun-slot
862 fun-pointer-lowtag)))
865 '((default-unknown-values vop values nvals node)))
867 '((note-this-location vop :unknown-return)
868 (receive-unknown-values values-start nvals start count
871 (trace-table-entry trace-table-normal)))))
873 (define-full-call call nil :fixed nil)
874 (define-full-call call-named t :fixed nil)
875 (define-full-call multiple-call nil :unknown nil)
876 (define-full-call multiple-call-named t :unknown nil)
877 (define-full-call tail-call nil :tail nil)
878 (define-full-call tail-call-named t :tail nil)
880 (define-full-call call-variable nil :fixed t)
881 (define-full-call multiple-call-variable nil :unknown t))
883 ;;; This is defined separately, since it needs special code that BLT's
884 ;;; the arguments down. All the real work is done in the assembly
885 ;;; routine. We just set things up so that it can find what it needs.
886 (define-vop (tail-call-variable)
887 (:args (args :scs (any-reg control-stack) :target esi)
888 (function :scs (descriptor-reg control-stack) :target eax)
891 (:temporary (:sc unsigned-reg :offset esi-offset :from (:argument 0)) esi)
892 (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 1)) eax)
894 (check-ocfp-and-return-pc old-fp return-pc)
895 ;; Move these into the passing locations if they are not already there.
898 ;; And jump to the assembly routine.
899 (inst jmp (make-fixup 'tail-call-variable :assembly-routine))))
901 ;;;; unknown values return
903 ;;; Return a single-value using the Unknown-Values convention.
905 ;;; pfw--get wired-tn conflicts sometimes if register sc specd for args
906 ;;; having problems targeting args to regs -- using temps instead.
908 ;;; First off, modifying the return-pc defeats the branch-prediction
909 ;;; optimizations on modern CPUs quite handily. Second, we can do all
910 ;;; this without needing a temp register. Fixed the latter, at least.
911 ;;; -- AB 2006/Feb/04
912 (define-vop (return-single)
918 (check-ocfp-and-return-pc old-fp return-pc)
919 (trace-table-entry trace-table-fun-epilogue)
920 ;; Drop stack above old-fp
921 (inst mov esp-tn ebp-tn)
922 ;; Clear the multiple-value return flag
924 ;; Restore the old frame pointer
929 ;;; Do unknown-values return of a fixed (other than 1) number of
930 ;;; values. The VALUES are required to be set up in the standard
931 ;;; passing locations. NVALS is the number of values returned.
933 ;;; Basically, we just load ECX with the number of values returned and
934 ;;; EBX with a pointer to the values, set ESP to point to the end of
935 ;;; the values, and jump directly to return-pc.
938 (return-pc :to (:eval 1))
942 ;; In the case of other than one value, we need these registers to
943 ;; tell the caller where they are and how many there are.
944 (:temporary (:sc unsigned-reg :offset ebx-offset) ebx)
945 (:temporary (:sc unsigned-reg :offset ecx-offset) ecx)
946 ;; We need to stretch the lifetime of return-pc past the argument
947 ;; registers so that we can default the argument registers without
948 ;; trashing return-pc.
949 (:temporary (:sc unsigned-reg :offset (first *register-arg-offsets*)
951 (:temporary (:sc unsigned-reg :offset (second *register-arg-offsets*)
953 (:temporary (:sc unsigned-reg :offset (third *register-arg-offsets*)
957 (check-ocfp-and-return-pc old-fp return-pc)
959 ;; This is handled in RETURN-SINGLE.
960 (error "nvalues is 1"))
961 (trace-table-entry trace-table-fun-epilogue)
962 ;; Establish the values pointer and values count.
963 (inst lea ebx (make-ea :dword :base ebp-tn
964 :disp (* sp->fp-offset n-word-bytes)))
966 (inst xor ecx ecx) ; smaller
967 (inst mov ecx (fixnumize nvals)))
968 ;; Pre-default any argument register that need it.
969 (when (< nvals register-arg-count)
970 (let* ((arg-tns (nthcdr nvals (list a0 a1 a2)))
971 (first (first arg-tns)))
972 (inst mov first nil-value)
973 (dolist (tn (cdr arg-tns))
974 (inst mov tn first))))
975 ;; Set the multiple value return flag.
977 ;; And away we go. Except that return-pc is still on the
978 ;; stack and we've changed the stack pointer. So we have to
979 ;; tell it to index off of EBX instead of EBP.
980 (cond ((<= nvals register-arg-count)
981 (inst mov esp-tn ebp-tn)
985 ;; Some values are on the stack after RETURN-PC and OLD-FP,
986 ;; can't return normally and some slots of the frame will
987 ;; be used as temporaries by the receiver.
989 ;; Clear as much of the stack as possible, but not past the
990 ;; old frame address.
992 (make-ea :dword :base ebp-tn
993 :disp (frame-byte-offset (1- nvals))))
995 (inst push (make-ea :dword :base ebx
996 :disp (frame-byte-offset
998 (tn-offset return-pc)))))
1001 (trace-table-entry trace-table-normal)))
1003 ;;; Do unknown-values return of an arbitrary number of values (passed
1004 ;;; on the stack.) We check for the common case of a single return
1005 ;;; value, and do that inline using the normal single value return
1006 ;;; convention. Otherwise, we branch off to code that calls an
1007 ;;; assembly-routine.
1009 ;;; The assembly routine takes the following args:
1010 ;;; ECX -- number of values to find there.
1011 ;;; ESI -- pointer to where to find the values.
1012 (define-vop (return-multiple)
1015 (vals :scs (any-reg) :target esi)
1016 (nvals :scs (any-reg) :target ecx))
1017 (:temporary (:sc unsigned-reg :offset esi-offset :from (:argument 2)) esi)
1018 (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 3)) ecx)
1019 (:temporary (:sc descriptor-reg :offset (first *register-arg-offsets*)
1020 :from (:eval 0)) a0)
1023 (check-ocfp-and-return-pc old-fp return-pc)
1024 (trace-table-entry trace-table-fun-epilogue)
1025 (unless (policy node (> space speed))
1026 ;; Check for the single case.
1027 (let ((not-single (gen-label)))
1028 (inst cmp nvals (fixnumize 1))
1029 (inst jmp :ne not-single)
1030 ;; Return with one value.
1032 ;; Clear the stack until ocfp.
1033 (inst mov esp-tn ebp-tn)
1034 ;; clear the multiple-value return flag
1039 ;; Nope, not the single case. Jump to the assembly routine.
1040 (emit-label not-single)))
1043 (inst jmp (make-fixup 'return-multiple :assembly-routine))
1044 (trace-table-entry trace-table-normal)))
1048 ;;; We don't need to do anything special for regular functions.
1049 (define-vop (setup-environment)
1053 ;; Don't bother doing anything.
1056 ;;; Get the lexical environment from its passing location.
1057 (define-vop (setup-closure-environment)
1058 (:results (closure :scs (descriptor-reg)))
1063 (move closure eax-tn)))
1065 ;;; Copy a &MORE arg from the argument area to the end of the current
1066 ;;; frame. FIXED is the number of non-&MORE arguments.
1068 ;;; The tricky part is doing this without trashing any of the calling
1069 ;;; convention registers that are still needed. This vop is emitted
1070 ;;; directly after the xep-allocate frame. That means the registers
1071 ;;; are in use as follows:
1073 ;;; EAX -- The lexenv.
1074 ;;; EBX -- Available.
1075 ;;; ECX -- The total number of arguments * N-WORD-BYTES.
1076 ;;; EDX -- The first arg.
1077 ;;; EDI -- The second arg.
1078 ;;; ESI -- The third arg.
1080 ;;; So basically, we have one register available for our use: EBX.
1082 ;;; What we can do is push the other regs onto the stack, and then
1083 ;;; restore their values by looking directly below where we put the
1085 (define-vop (copy-more-arg)
1088 ;; Avoid the copy if there are no more args.
1089 (cond ((zerop fixed)
1090 (inst jecxz JUST-ALLOC-FRAME))
1092 (inst cmp ecx-tn (fixnumize fixed))
1093 (inst jmp :be JUST-ALLOC-FRAME)))
1095 ;; Allocate the space on the stack.
1096 ;; stack = ebp + sp->fp-offset - (max 3 frame-size) - (nargs - fixed)
1098 (make-ea :dword :base ebp-tn
1099 :disp (* n-word-bytes
1100 (- (+ sp->fp-offset fixed)
1101 (max 3 (sb-allocated-size 'stack))))))
1102 (inst sub ebx-tn ecx-tn) ; Got the new stack in ebx
1103 (inst mov esp-tn ebx-tn)
1105 ;; Now: nargs>=1 && nargs>fixed
1107 ;; Save the original count of args.
1108 (inst mov ebx-tn ecx-tn)
1110 (cond ((< fixed register-arg-count)
1111 ;; We must stop when we run out of stack args, not when we
1112 ;; run out of more args.
1113 ;; Number to copy = nargs-3
1114 (inst sub ecx-tn (fixnumize register-arg-count))
1115 ;; Everything of interest in registers.
1116 (inst jmp :be DO-REGS))
1118 ;; Number to copy = nargs-fixed
1119 (inst sub ecx-tn (fixnumize fixed))))
1121 ;; Save edi and esi register args.
1125 ;; Okay, we have pushed the register args. We can trash them
1128 ;; Initialize src to be end of args.
1129 (inst lea esi-tn (make-ea :dword :base ebp-tn
1130 :disp (* sp->fp-offset n-word-bytes)))
1131 (inst sub esi-tn ebx-tn)
1133 ;; We need to copy from downwards up to avoid overwriting some of
1134 ;; the yet uncopied args. So we need to use EBX as the copy index
1135 ;; and ECX as the loop counter, rather than using ECX for both.
1136 (inst xor ebx-tn ebx-tn)
1138 ;; We used to use REP MOVS here, but on modern x86 it performs
1139 ;; much worse than an explicit loop for small blocks.
1141 (inst mov edi-tn (make-ea :dword :base esi-tn :index ebx-tn))
1142 ;; The :DISP is to account for the registers saved on the stack
1143 (inst mov (make-ea :dword :base esp-tn :disp (* 3 n-word-bytes)
1146 (inst add ebx-tn n-word-bytes)
1147 (inst sub ecx-tn n-word-bytes)
1148 (inst jmp :nz COPY-LOOP)
1150 ;; So now we need to restore EDI and ESI.
1158 (inst mov ecx-tn ebx-tn)
1160 ;; Here: nargs>=1 && nargs>fixed
1161 (when (< fixed register-arg-count)
1162 ;; Now we have to deposit any more args that showed up in
1166 ;; Store it relative to ebp
1167 (inst mov (make-ea :dword :base ebp-tn
1168 :disp (* n-word-bytes
1172 (max 3 (sb-allocated-size
1174 (nth i *register-arg-tns*))
1177 (when (>= i register-arg-count)
1180 ;; Don't deposit any more than there are.
1182 (inst test ecx-tn ecx-tn)
1183 (inst cmp ecx-tn (fixnumize i)))
1184 (inst jmp :eq DONE)))
1190 (make-ea :dword :base ebp-tn
1191 :disp (* n-word-bytes
1193 (max 3 (sb-allocated-size 'stack))))))
1197 (define-vop (more-kw-arg)
1198 (:translate sb!c::%more-kw-arg)
1199 (:policy :fast-safe)
1200 (:args (object :scs (descriptor-reg) :to (:result 1))
1201 (index :scs (any-reg immediate) :to (:result 1) :target keyword))
1202 (:arg-types * tagged-num)
1203 (:results (value :scs (descriptor-reg any-reg))
1204 (keyword :scs (descriptor-reg any-reg)))
1209 (inst mov value (make-ea :dword :base object :disp (tn-value index)))
1210 (inst mov keyword (make-ea :dword :base object
1211 :disp (+ (tn-value index) n-word-bytes))))
1213 (inst mov value (make-ea :dword :base object :index index))
1214 (inst mov keyword (make-ea :dword :base object :index index
1215 :disp n-word-bytes))))))
1217 (define-vop (more-arg)
1218 (:translate sb!c::%more-arg)
1219 (:policy :fast-safe)
1220 (:args (object :scs (descriptor-reg) :to (:result 1))
1221 (index :scs (any-reg) :to (:result 1) :target value))
1222 (:arg-types * tagged-num)
1223 (:results (value :scs (descriptor-reg any-reg)))
1228 (inst mov value (make-ea :dword :base object :index value))))
1230 ;;; Turn more arg (context, count) into a list.
1231 (define-vop (listify-rest-args)
1232 (:translate %listify-rest-args)
1234 (:args (context :scs (descriptor-reg) :target src)
1235 (count :scs (any-reg) :target ecx))
1236 (:arg-types * tagged-num)
1237 (:temporary (:sc unsigned-reg :offset esi-offset :from (:argument 0)) src)
1238 (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 1)) ecx)
1239 (:temporary (:sc unsigned-reg :offset eax-offset) eax)
1240 (:temporary (:sc unsigned-reg) dst)
1241 (:results (result :scs (descriptor-reg)))
1244 (let ((enter (gen-label))
1247 (stack-allocate-p (node-stack-allocate-p node)))
1250 ;; Check to see whether there are no args, and just return NIL if so.
1251 (inst mov result nil-value)
1253 (inst lea dst (make-ea :dword :base ecx :index ecx))
1254 (maybe-pseudo-atomic stack-allocate-p
1255 (allocation dst dst node stack-allocate-p list-pointer-lowtag)
1256 (inst shr ecx (1- n-lowtag-bits))
1257 ;; Set decrement mode (successive args at lower addresses)
1259 ;; Set up the result.
1261 ;; Jump into the middle of the loop, 'cause that's where we want
1265 ;; Compute a pointer to the next cons.
1266 (inst add dst (* cons-size n-word-bytes))
1267 ;; Store a pointer to this cons in the CDR of the previous cons.
1268 (storew dst dst -1 list-pointer-lowtag)
1270 ;; Grab one value and stash it in the car of this cons.
1272 (storew eax dst 0 list-pointer-lowtag)
1273 ;; Go back for more.
1276 ;; NIL out the last cons.
1277 (storew nil-value dst 1 list-pointer-lowtag)
1279 (emit-label done))))
1281 ;;; Return the location and size of the &MORE arg glob created by
1282 ;;; COPY-MORE-ARG. SUPPLIED is the total number of arguments supplied
1283 ;;; (originally passed in ECX). FIXED is the number of non-rest
1286 ;;; We must duplicate some of the work done by COPY-MORE-ARG, since at
1287 ;;; that time the environment is in a pretty brain-damaged state,
1288 ;;; preventing this info from being returned as values. What we do is
1289 ;;; compute supplied - fixed, and return a pointer that many words
1290 ;;; below the current stack top.
1291 (define-vop (more-arg-context)
1292 (:policy :fast-safe)
1293 (:translate sb!c::%more-arg-context)
1294 (:args (supplied :scs (any-reg) :target count))
1295 (:arg-types positive-fixnum (:constant fixnum))
1297 (:results (context :scs (descriptor-reg))
1298 (count :scs (any-reg)))
1299 (:result-types t tagged-num)
1300 (:note "more-arg-context")
1302 (move count supplied)
1303 ;; SP at this point points at the last arg pushed.
1304 ;; Point to the first more-arg, not above it.
1305 (inst lea context (make-ea :dword :base esp-tn
1306 :index count :scale 1
1307 :disp (- (+ (fixnumize fixed) n-word-bytes))))
1308 (unless (zerop fixed)
1309 (inst sub count (fixnumize fixed)))))
1311 ;;; Signal wrong argument count error if NARGS isn't equal to COUNT.
1312 (define-vop (verify-arg-count)
1313 (:policy :fast-safe)
1314 (:translate sb!c::%verify-arg-count)
1315 (:args (nargs :scs (any-reg)))
1316 (:arg-types positive-fixnum (:constant t))
1319 (:save-p :compute-only)
1322 (generate-error-code vop 'invalid-arg-count-error nargs)))
1324 (inst test nargs nargs) ; smaller instruction
1325 (inst cmp nargs (fixnumize count)))
1326 (inst jmp :ne err-lab))))
1328 ;;; Various other error signallers.
1329 (macrolet ((def (name error translate &rest args)
1330 `(define-vop (,name)
1332 `((:policy :fast-safe)
1333 (:translate ,translate)))
1334 (:args ,@(mapcar (lambda (arg)
1335 `(,arg :scs (any-reg descriptor-reg)))
1338 (:save-p :compute-only)
1340 (error-call vop ',error ,@args)))))
1341 (def arg-count-error invalid-arg-count-error
1342 sb!c::%arg-count-error nargs)
1343 (def type-check-error object-not-type-error sb!c::%type-check-error
1345 (def layout-invalid-error layout-invalid-error sb!c::%layout-invalid-error
1347 (def odd-key-args-error odd-key-args-error
1348 sb!c::%odd-key-args-error)
1349 (def unknown-key-arg-error unknown-key-arg-error
1350 sb!c::%unknown-key-arg-error key)
1351 (def nil-fun-returned-error nil-fun-returned-error nil fun))
1355 (defun emit-single-step-test ()
1356 ;; We use different ways of representing whether stepping is on on
1357 ;; +SB-THREAD / -SB-THREAD: on +SB-THREAD, we use a slot in the
1358 ;; thread structure. On -SB-THREAD we use the value of a static
1359 ;; symbol. Things are done this way, since reading a thread-local
1360 ;; slot from a symbol would require an extra register on +SB-THREAD,
1361 ;; and reading a slot from a thread structure would require an extra
1362 ;; register on -SB-THREAD.
1365 (inst cmp (make-ea :dword
1366 :disp (* thread-stepping-slot n-word-bytes))
1369 (inst cmp (make-ea-for-symbol-value sb!impl::*stepping*)
1372 (define-vop (step-instrument-before-vop)
1373 (:policy :fast-safe)
1376 (emit-single-step-test)
1378 (inst break single-step-before-trap)
1380 (note-this-location vop :step-before-vop)))