Micro-optimize verify-arg-count on x86oids and ARM64.
[sbcl.git] / src / compiler / x86 / call.lisp
blobbc341bf9ad06ce6c9497e6c7e3b99b69417c6ce6
1 ;;;; function call for the x86 VM
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
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.
12 (in-package "SB!VM")
14 (defconstant arg-count-sc (make-sc-offset any-reg-sc-number ecx-offset))
15 (defconstant closure-sc (make-sc-offset descriptor-reg-sc-number eax-offset))
17 ;;; Make a passing location TN for a local call return PC.
18 ;;;
19 ;;; Always wire the return PC location to the stack in its standard
20 ;;; location.
21 (defun make-return-pc-passing-location (standard)
22 (declare (ignore standard))
23 (make-wired-tn (primitive-type-or-lose 'system-area-pointer)
24 sap-stack-sc-number return-pc-save-offset))
26 (defconstant return-pc-passing-offset
27 (make-sc-offset sap-stack-sc-number return-pc-save-offset))
29 ;;; This is similar to MAKE-RETURN-PC-PASSING-LOCATION, but makes a
30 ;;; location to pass OLD-FP in.
31 ;;;
32 ;;; This is wired in both the standard and the local-call conventions,
33 ;;; because we want to be able to assume it's always there. Besides,
34 ;;; the x86 doesn't have enough registers to really make it profitable
35 ;;; to pass it in a register.
36 (defun make-old-fp-passing-location (standard)
37 (declare (ignore standard))
38 (make-wired-tn *fixnum-primitive-type* control-stack-sc-number
39 ocfp-save-offset))
41 (defconstant old-fp-passing-offset
42 (make-sc-offset control-stack-sc-number ocfp-save-offset))
44 ;;; Make the TNs used to hold OLD-FP and RETURN-PC within the current
45 ;;; function. We treat these specially so that the debugger can find
46 ;;; them at a known location.
47 ;;;
48 ;;; Without using a save-tn - which does not make much sense if it is
49 ;;; wired to the stack?
50 (defun make-old-fp-save-location (physenv)
51 (physenv-debug-live-tn (make-wired-tn *fixnum-primitive-type*
52 control-stack-sc-number
53 ocfp-save-offset)
54 physenv))
55 (defun make-return-pc-save-location (physenv)
56 (physenv-debug-live-tn
57 (make-wired-tn (primitive-type-or-lose 'system-area-pointer)
58 sap-stack-sc-number return-pc-save-offset)
59 physenv))
61 ;;; Make a TN for the standard argument count passing location. We only
62 ;;; need to make the standard location, since a count is never passed when we
63 ;;; are using non-standard conventions.
64 (defun make-arg-count-location ()
65 (make-wired-tn *fixnum-primitive-type* any-reg-sc-number ecx-offset))
67 ;;;; frame hackery
69 ;;; This is used for setting up the Old-FP in local call.
70 (define-vop (current-fp)
71 (:results (val :scs (any-reg control-stack)))
72 (:generator 1
73 (move val ebp-tn)))
75 ;;; We don't have a separate NFP, so we don't need to do anything here.
76 (define-vop (compute-old-nfp)
77 (:results (val))
78 (:ignore val)
79 (:generator 1
80 nil))
82 ;;; Accessing a slot from an earlier stack frame is definite hackery.
83 (define-vop (ancestor-frame-ref)
84 (:args (frame-pointer :scs (descriptor-reg))
85 (variable-home-tn :load-if nil))
86 (:results (value :scs (descriptor-reg any-reg)))
87 (:policy :fast-safe)
88 (:generator 4
89 (aver (sc-is variable-home-tn control-stack))
90 (loadw value frame-pointer
91 (frame-word-offset (tn-offset variable-home-tn)))))
92 (define-vop (ancestor-frame-set)
93 (:args (frame-pointer :scs (descriptor-reg))
94 (value :scs (descriptor-reg any-reg)))
95 (:results (variable-home-tn :load-if nil))
96 (:policy :fast-safe)
97 (:generator 4
98 (aver (sc-is variable-home-tn control-stack))
99 (storew value frame-pointer
100 (frame-word-offset (tn-offset variable-home-tn)))))
102 (macrolet ((define-frame-op
103 (suffix sc stack-sc instruction
104 &optional (ea
105 `(make-ea :dword
106 :base frame-pointer
107 :disp (frame-byte-offset
108 (tn-offset variable-home-tn)))))
109 (let ((reffer (symbolicate 'ancestor-frame-ref '/ suffix))
110 (setter (symbolicate 'ancestor-frame-set '/ suffix)))
111 `(progn
112 (define-vop (,reffer ancestor-frame-ref)
113 (:results (value :scs (,sc)))
114 (:generator 4
115 (aver (sc-is variable-home-tn ,stack-sc))
116 (inst ,instruction value
117 ,ea)))
118 (define-vop (,setter ancestor-frame-set)
119 (:args (frame-pointer :scs (descriptor-reg))
120 (value :scs (,sc)))
121 (:generator 4
122 (aver (sc-is variable-home-tn ,stack-sc))
123 (inst ,instruction ,ea value))))))
124 (define-x87-frame-op
125 (suffix sc stack-sc (load set)
126 &optional (ea
127 `(make-ea :dword
128 :base frame-pointer
129 :disp (frame-byte-offset
130 (tn-offset variable-home-tn)))))
131 (let ((reffer (symbolicate 'ancestor-frame-ref '/ suffix))
132 (setter (symbolicate 'ancestor-frame-set '/ suffix)))
133 `(progn
134 (define-vop (,reffer ancestor-frame-ref)
135 (:results (value :scs (,sc)))
136 (:generator 4
137 (aver (sc-is variable-home-tn ,stack-sc))
138 ,(if (symbolp load)
139 `(with-empty-tn@fp-top (value)
140 (inst ,load ,ea))
141 load)))
142 (define-vop (,setter ancestor-frame-set)
143 (:args (frame-pointer :scs (descriptor-reg))
144 (value :scs (,sc)))
145 (:generator 4
146 (aver (sc-is variable-home-tn ,stack-sc))
147 ,(if (symbolp set)
148 `(with-tn@fp-top (value)
149 (inst ,set ,ea))
150 set)))))))
151 (define-frame-op signed-byte-32 signed-reg signed-stack mov)
152 (define-frame-op unsigned-byte-32 unsigned-reg unsigned-stack mov)
153 (define-frame-op system-area-pointer sap-reg sap-stack mov)
155 (define-x87-frame-op double-float double-reg double-stack
156 (fldd fstd) (make-ea :dword
157 :base frame-pointer
158 :disp (frame-byte-offset
159 (1+ (tn-offset variable-home-tn)))))
160 (define-x87-frame-op single-float single-reg single-stack
161 (fld fst))
163 (define-x87-frame-op complex-double-float complex-double-reg
164 complex-double-stack
165 ((let ((real (complex-double-reg-real-tn value))
166 (imag (complex-double-reg-imag-tn value)))
167 (with-empty-tn@fp-top (real)
168 (inst fldd (ea-for-cdf-real-stack variable-home-tn frame-pointer)))
169 (with-empty-tn@fp-top (imag)
170 (inst fldd (ea-for-cdf-imag-stack variable-home-tn frame-pointer))))
171 (let ((real (complex-double-reg-real-tn value))
172 (imag (complex-double-reg-imag-tn value)))
173 (with-tn@fp-top (real)
174 (inst fstd (ea-for-cdf-real-stack variable-home-tn frame-pointer)))
175 (with-tn@fp-top (imag)
176 (inst fstd (ea-for-cdf-imag-stack variable-home-tn frame-pointer))))))
177 (define-x87-frame-op complex-single-float complex-single-reg
178 complex-single-stack
179 ((let ((real (complex-single-reg-real-tn value))
180 (imag (complex-single-reg-imag-tn value)))
181 (with-empty-tn@fp-top (real)
182 (inst fld (ea-for-csf-real-stack variable-home-tn frame-pointer)))
183 (with-empty-tn@fp-top (imag)
184 (inst fld (ea-for-csf-imag-stack variable-home-tn frame-pointer))))
185 (let ((real (complex-single-reg-real-tn value))
186 (imag (complex-single-reg-imag-tn value)))
187 (with-tn@fp-top (real)
188 (inst fst (ea-for-csf-real-stack variable-home-tn frame-pointer)))
189 (with-tn@fp-top (imag)
190 (inst fst (ea-for-csf-imag-stack variable-home-tn frame-pointer)))))))
192 (defun primitive-type-indirect-cell-type (ptype)
193 (declare (type primitive-type ptype))
194 (macrolet ((foo (&body data)
195 `(case (primitive-type-name ptype)
196 ,@(loop for (name stack-sc ref set) in data
197 collect
198 `(,name
199 (load-time-value
200 (list (primitive-type-or-lose ',name)
201 (sc-or-lose ',stack-sc)
202 (lambda (node block fp value res)
203 (sb!c::vop ,ref node block
204 fp value res))
205 (lambda (node block fp new-val value)
206 (sb!c::vop ,set node block
207 fp new-val value)))))))))
208 (foo (double-float double-stack
209 ancestor-frame-ref/double-float
210 ancestor-frame-set/double-float)
211 (single-float single-stack
212 ancestor-frame-ref/single-float
213 ancestor-frame-set/single-float)
214 (complex-double-float complex-double-stack
215 ancestor-frame-ref/complex-double-float
216 ancestor-frame-set/complex-double-float)
217 (complex-single-float complex-single-stack
218 ancestor-frame-ref/complex-single-float
219 ancestor-frame-set/complex-single-float)
220 (signed-byte-32 signed-stack
221 ancestor-frame-ref/signed-byte-32
222 ancestor-frame-set/signed-byte-32)
223 (unsigned-byte-32 unsigned-stack
224 ancestor-frame-ref/unsigned-byte-32
225 ancestor-frame-set/unsigned-byte-32)
226 (unsigned-byte-31 unsigned-stack
227 ancestor-frame-ref/unsigned-byte-32
228 ancestor-frame-set/unsigned-byte-32)
229 (system-area-pointer sap-stack
230 ancestor-frame-ref/system-area-pointer
231 ancestor-frame-set/system-area-pointer))))
233 (define-vop (xep-allocate-frame)
234 (:info start-lab)
235 (:generator 1
236 (emit-alignment n-lowtag-bits)
237 (emit-label start-lab)
238 ;; Skip space for the function header.
239 (inst simple-fun-header-word)
240 (dotimes (i (1- simple-fun-code-offset))
241 (inst dword 0))
243 ;; The start of the actual code.
244 ;; Save the return-pc.
245 (popw ebp-tn (frame-word-offset return-pc-save-offset))))
247 (define-vop (xep-setup-sp)
248 (:generator 1
249 (inst lea esp-tn
250 (make-ea :dword :base ebp-tn
251 :disp (- (* n-word-bytes
252 (- (max 3 (sb-allocated-size 'stack))
253 sp->fp-offset)))))))
255 ;;; This is emitted directly before either a known-call-local, call-local,
256 ;;; or a multiple-call-local. All it does is allocate stack space for the
257 ;;; callee (who has the same size stack as us).
258 (define-vop (allocate-frame)
259 (:results (res :scs (any-reg))
260 (nfp))
261 (:info callee)
262 (:ignore nfp callee)
263 (:generator 2
264 (inst lea res (make-ea :dword :base esp-tn
265 :disp (- (* sp->fp-offset n-word-bytes))))
266 (inst sub esp-tn (* n-word-bytes (sb-allocated-size 'stack)))))
268 ;;; Allocate a partial frame for passing stack arguments in a full
269 ;;; call. NARGS is the number of arguments passed. We allocate at
270 ;;; least 3 slots, because the XEP noise is going to want to use them
271 ;;; before it can extend the stack.
272 (define-vop (allocate-full-call-frame)
273 (:info nargs)
274 (:results (res :scs (any-reg)))
275 (:generator 2
276 (inst lea res (make-ea :dword :base esp-tn
277 :disp (- (* sp->fp-offset n-word-bytes))))
278 (inst sub esp-tn (* (max nargs 3) n-word-bytes))))
280 ;;; Emit code needed at the return-point from an unknown-values call
281 ;;; for a fixed number of values. Values is the head of the TN-REF
282 ;;; list for the locations that the values are to be received into.
283 ;;; Nvals is the number of values that are to be received (should
284 ;;; equal the length of Values).
286 ;;; If 0 or 1 values are expected, then we just emit an instruction to
287 ;;; reset the SP (which will only be executed when other than 1 value
288 ;;; is returned.)
290 ;;; In the general case we have to do three things:
291 ;;; -- Default unsupplied register values. This need only be done
292 ;;; when a single value is returned, since register values are
293 ;;; defaulted by the called in the non-single case.
294 ;;; -- Default unsupplied stack values. This needs to be done whenever
295 ;;; there are stack values.
296 ;;; -- Reset SP. This must be done whenever other than 1 value is
297 ;;; returned, regardless of the number of values desired.
298 (defun default-unknown-values (vop values nvals node)
299 (declare (type (or tn-ref null) values)
300 (type unsigned-byte nvals))
301 (let ((type (sb!c::basic-combination-derived-type node)))
302 (cond
303 ((<= nvals 1)
304 (note-this-location vop :single-value-return)
305 (cond
306 ((<= (values-type-max-value-count type)
307 register-arg-count)
308 (when (and (named-type-p type)
309 (eq nil (named-type-name type)))
310 ;; The function never returns, it may happen that the code
311 ;; ends right here leavig the :SINGLE-VALUE-RETURN note
312 ;; dangling. Let's emit a NOP.
313 (inst nop)))
314 ((not (values-type-may-be-single-value-p type))
315 (inst mov esp-tn ebx-tn))
316 ((member :cmov *backend-subfeatures*)
317 (inst cmov :c esp-tn ebx-tn))
319 (let ((single-value (gen-label)))
320 (inst jmp :nc single-value)
321 (inst mov esp-tn ebx-tn)
322 (emit-label single-value)))))
323 ((<= nvals register-arg-count)
324 (note-this-location vop :unknown-return)
325 (when (values-type-may-be-single-value-p type)
326 (let ((regs-defaulted (gen-label)))
327 (inst jmp :c regs-defaulted)
328 ;; Default the unsupplied registers.
329 (let* ((2nd-tn-ref (tn-ref-across values))
330 (2nd-tn (tn-ref-tn 2nd-tn-ref)))
331 (inst mov 2nd-tn nil-value)
332 (when (> nvals 2)
333 (loop
334 for tn-ref = (tn-ref-across 2nd-tn-ref)
335 then (tn-ref-across tn-ref)
336 for count from 2 below register-arg-count
337 do (inst mov (tn-ref-tn tn-ref) 2nd-tn))))
338 (inst mov ebx-tn esp-tn)
339 (emit-label regs-defaulted)))
340 (when (< register-arg-count
341 (values-type-max-value-count type))
342 (inst mov esp-tn ebx-tn)))
343 ((<= nvals 7)
344 ;; The number of bytes depends on the relative jump instructions.
345 ;; Best case is 31+(n-3)*14, worst case is 35+(n-3)*18. For
346 ;; NVALS=6 that is 73/89 bytes, and for NVALS=7 that is 87/107
347 ;; bytes which is likely better than using the blt below.
348 (let ((regs-defaulted (gen-label))
349 (defaulting-done (gen-label))
350 (default-stack-slots (gen-label)))
351 (note-this-location vop :unknown-return)
352 ;; Branch off to the MV case.
353 (inst jmp :c regs-defaulted)
354 ;; Do the single value case.
355 ;; Default the register args
356 (inst mov eax-tn nil-value)
357 (do ((i 1 (1+ i))
358 (val (tn-ref-across values) (tn-ref-across val)))
359 ((= i (min nvals register-arg-count)))
360 (inst mov (tn-ref-tn val) eax-tn))
361 ;; Fake other registers so it looks like we returned with all the
362 ;; registers filled in.
363 (move ebx-tn esp-tn)
364 (inst jmp default-stack-slots)
365 (emit-label regs-defaulted)
366 (inst mov eax-tn nil-value)
367 (collect ((defaults))
368 (do ((i register-arg-count (1+ i))
369 (val (do ((i 0 (1+ i))
370 (val values (tn-ref-across val)))
371 ((= i register-arg-count) val))
372 (tn-ref-across val)))
373 ((null val))
374 (let ((default-lab (gen-label))
375 (tn (tn-ref-tn val))
376 (first-stack-arg-p (= i register-arg-count)))
377 (defaults (cons default-lab
378 (cons tn first-stack-arg-p)))
379 (inst cmp ecx-tn (fixnumize i))
380 (inst jmp :be default-lab)
381 (when first-stack-arg-p
382 ;; There are stack args so the frame of the callee is
383 ;; still there, save EDX in its first slot temporalily.
384 (storew edx-tn ebx-tn (frame-word-offset sp->fp-offset)))
385 (loadw edx-tn ebx-tn (frame-word-offset (+ sp->fp-offset i)))
386 (inst mov tn edx-tn)))
387 (emit-label defaulting-done)
388 (loadw edx-tn ebx-tn (frame-word-offset sp->fp-offset))
389 (move esp-tn ebx-tn)
390 (let ((defaults (defaults)))
391 (when defaults
392 (assemble (*elsewhere*)
393 (emit-label default-stack-slots)
394 (dolist (default defaults)
395 (emit-label (car default))
396 (when (cddr default)
397 ;; We are setting the first stack argument to NIL.
398 ;; The callee's stack frame is dead, save EDX by
399 ;; pushing it to the stack, it will end up at same
400 ;; place as in the (STOREW EDX-TN EBX-TN -1) case
401 ;; above.
402 (inst push edx-tn))
403 (inst mov (second default) eax-tn))
404 (inst jmp defaulting-done)))))))
406 ;; 91 bytes for this branch.
407 (let ((regs-defaulted (gen-label))
408 (restore-edi (gen-label))
409 (no-stack-args (gen-label))
410 (default-stack-vals (gen-label))
411 (count-okay (gen-label)))
412 (note-this-location vop :unknown-return)
413 ;; Branch off to the MV case.
414 (inst jmp :c regs-defaulted)
415 ;; Default the register args, and set up the stack as if we
416 ;; entered the MV return point.
417 (inst mov ebx-tn esp-tn)
418 (inst mov edi-tn nil-value)
419 (inst mov esi-tn edi-tn)
420 ;; Compute a pointer to where to put the [defaulted] stack values.
421 (emit-label no-stack-args)
422 (inst push edx-tn)
423 (inst push edi-tn)
424 (inst lea edi-tn
425 (make-ea :dword :base ebp-tn
426 :disp (frame-byte-offset register-arg-count)))
427 ;; Load EAX with NIL so we can quickly store it, and set up
428 ;; stuff for the loop.
429 (inst mov eax-tn nil-value)
430 (inst std)
431 (inst mov ecx-tn (- nvals register-arg-count))
432 ;; Jump into the default loop.
433 (inst jmp default-stack-vals)
434 ;; The regs are defaulted. We need to copy any stack arguments,
435 ;; and then default the remaining stack arguments.
436 (emit-label regs-defaulted)
437 ;; Compute the number of stack arguments, and if it's zero or
438 ;; less, don't copy any stack arguments.
439 (inst sub ecx-tn (fixnumize register-arg-count))
440 (inst jmp :le no-stack-args)
441 ;; Save EDI.
442 (storew edi-tn ebx-tn (frame-word-offset (+ sp->fp-offset 1)))
443 ;; Throw away any unwanted args.
444 (inst cmp ecx-tn (fixnumize (- nvals register-arg-count)))
445 (inst jmp :be count-okay)
446 (inst mov ecx-tn (fixnumize (- nvals register-arg-count)))
447 (emit-label count-okay)
448 ;; Save the number of stack values.
449 (inst mov eax-tn ecx-tn)
450 ;; Compute a pointer to where the stack args go.
451 (inst lea edi-tn
452 (make-ea :dword :base ebp-tn
453 :disp (frame-byte-offset register-arg-count)))
454 ;; Save ESI, and compute a pointer to where the args come from.
455 (storew esi-tn ebx-tn (frame-word-offset (+ sp->fp-offset 2)))
456 (inst lea esi-tn
457 (make-ea :dword :base ebx-tn
458 :disp (frame-byte-offset
459 (+ sp->fp-offset register-arg-count))))
460 ;; Do the copy.
461 (inst shr ecx-tn word-shift) ; make word count
462 (inst std)
463 (inst rep)
464 (inst movs :dword)
465 ;; Restore ESI.
466 (loadw esi-tn ebx-tn (frame-word-offset (+ sp->fp-offset 2)))
467 ;; Now we have to default the remaining args. Find out how many.
468 (inst sub eax-tn (fixnumize (- nvals register-arg-count)))
469 (inst neg eax-tn)
470 ;; If none, then just blow out of here.
471 (inst jmp :le restore-edi)
472 (inst mov ecx-tn eax-tn)
473 (inst shr ecx-tn word-shift) ; word count
474 ;; Load EAX with NIL for fast storing.
475 (inst mov eax-tn nil-value)
476 ;; Do the store.
477 (emit-label default-stack-vals)
478 (inst rep)
479 (inst stos eax-tn)
480 ;; Restore EDI, and reset the stack.
481 (emit-label restore-edi)
482 (loadw edi-tn ebx-tn (frame-word-offset (+ sp->fp-offset 1)))
483 (inst mov esp-tn ebx-tn)
484 (inst cld)))))
485 (values))
487 ;;;; unknown values receiving
489 ;;; Emit code needed at the return point for an unknown-values call
490 ;;; for an arbitrary number of values.
492 ;;; We do the single and non-single cases with no shared code: there
493 ;;; doesn't seem to be any potential overlap, and receiving a single
494 ;;; value is more important efficiency-wise.
496 ;;; When there is a single value, we just push it on the stack,
497 ;;; returning the old SP and 1.
499 ;;; When there is a variable number of values, we move all of the
500 ;;; argument registers onto the stack, and return ARGS and NARGS.
502 ;;; ARGS and NARGS are TNs wired to the named locations. We must
503 ;;; explicitly allocate these TNs, since their lifetimes overlap with
504 ;;; the results start and count. (Also, it's nice to be able to target
505 ;;; them.)
506 (defun receive-unknown-values (args nargs start count node)
507 (declare (type tn args nargs start count))
508 (let ((type (sb!c::basic-combination-derived-type node))
509 (variable-values (gen-label))
510 (stack-values (gen-label))
511 (done (gen-label)))
512 (when (values-type-may-be-single-value-p type)
513 (inst jmp :c variable-values)
514 (cond ((location= start (first *register-arg-tns*))
515 (inst push (first *register-arg-tns*))
516 (inst lea start (make-ea :dword :base esp-tn :disp n-word-bytes)))
517 (t (inst mov start esp-tn)
518 (inst push (first *register-arg-tns*))))
519 (inst mov count (fixnumize 1))
520 (inst jmp done)
521 (emit-label variable-values))
522 ;; The stack frame is burnt and RETurned from if there are no
523 ;; stack values. In this case quickly reallocate sufficient space.
524 (when (<= (values-type-min-value-count type)
525 register-arg-count)
526 (inst cmp nargs (fixnumize register-arg-count))
527 (inst jmp :g stack-values)
528 (inst sub esp-tn nargs)
529 (emit-label stack-values))
530 ;; dtc: this writes the registers onto the stack even if they are
531 ;; not needed, only the number specified in ecx are used and have
532 ;; stack allocated to them. No harm is done.
533 (loop
534 for arg in *register-arg-tns*
535 for i downfrom -1
536 for j below (values-type-max-value-count type)
537 do (storew arg args i))
538 (move start args)
539 (move count nargs)
541 (emit-label done))
542 (values))
544 ;;; VOP that can be inherited by unknown values receivers. The main thing this
545 ;;; handles is allocation of the result temporaries.
546 (define-vop (unknown-values-receiver)
547 (:temporary (:sc descriptor-reg :offset ebx-offset
548 :from :eval :to (:result 0))
549 values-start)
550 (:temporary (:sc any-reg :offset ecx-offset
551 :from :eval :to (:result 1))
552 nvals)
553 (:results (start :scs (any-reg control-stack))
554 (count :scs (any-reg control-stack))))
556 ;;;; local call with unknown values convention return
558 (defun check-ocfp-and-return-pc (old-fp return-pc)
559 #+nil
560 (format t "*known-return: old-fp ~S, tn-kind ~S; ~S ~S~%"
561 old-fp (sb!c::tn-kind old-fp) (sb!c::tn-save-tn old-fp)
562 (sb!c::tn-kind (sb!c::tn-save-tn old-fp)))
563 #+nil
564 (format t "*known-return: return-pc ~S, tn-kind ~S; ~S ~S~%"
565 return-pc (sb!c::tn-kind return-pc)
566 (sb!c::tn-save-tn return-pc)
567 (sb!c::tn-kind (sb!c::tn-save-tn return-pc)))
568 (unless (and (sc-is old-fp control-stack)
569 (= (tn-offset old-fp) ocfp-save-offset))
570 (error "ocfp not on stack in standard save location?"))
571 (unless (and (sc-is return-pc sap-stack)
572 (= (tn-offset return-pc) return-pc-save-offset))
573 (error "return-pc not on stack in standard save location?")))
575 ;;; The local call convention doesn't fit that well with x86-style
576 ;;; calls. Emit a header for local calls to pop the return address
577 ;;; in the right place.
578 (defun emit-block-header (start-label trampoline-label fall-thru-p alignp)
579 (declare (ignore alignp))
580 (when trampoline-label
581 (when fall-thru-p
582 (inst jmp start-label))
583 (emit-label trampoline-label)
584 (popw ebp-tn (frame-word-offset return-pc-save-offset)))
585 (emit-label start-label))
587 ;;; Non-TR local call for a fixed number of values passed according to
588 ;;; the unknown values convention.
590 ;;; FP is the frame pointer in install before doing the call.
592 ;;; NFP would be the number-stack frame pointer if we had a separate
593 ;;; number stack.
595 ;;; Args are the argument passing locations, which are specified only
596 ;;; to terminate their lifetimes in the caller.
598 ;;; VALUES are the return value locations (wired to the standard
599 ;;; passing locations). NVALS is the number of values received.
601 ;;; Save is the save info, which we can ignore since saving has been
602 ;;; done.
604 ;;; TARGET is a continuation pointing to the start of the called
605 ;;; function.
606 (define-vop (call-local)
607 (:args (fp)
608 (nfp)
609 (args :more t))
610 (:results (values :more t))
611 (:save-p t)
612 (:move-args :local-call)
613 (:info arg-locs callee target nvals)
614 (:vop-var vop)
615 (:ignore nfp arg-locs args callee)
616 (:node-var node)
617 (:generator 5
618 (move ebp-tn fp)
619 (note-this-location vop :call-site)
620 (inst call target)
621 (default-unknown-values vop values nvals node)))
623 ;;; Non-TR local call for a variable number of return values passed according
624 ;;; to the unknown values convention. The results are the start of the values
625 ;;; glob and the number of values received.
626 (define-vop (multiple-call-local unknown-values-receiver)
627 (:args (fp)
628 (nfp)
629 (args :more t))
630 (:save-p t)
631 (:move-args :local-call)
632 (:info save callee target)
633 (:ignore args save nfp callee)
634 (:vop-var vop)
635 (:node-var node)
636 (:generator 20
637 (move ebp-tn fp)
638 (note-this-location vop :call-site)
639 (inst call target)
640 (note-this-location vop :unknown-return)
641 (receive-unknown-values values-start nvals start count node)))
643 ;;;; local call with known values return
645 ;;; Non-TR local call with known return locations. Known-value return
646 ;;; works just like argument passing in local call.
648 ;;; Note: we can't use normal load-tn allocation for the fixed args,
649 ;;; since all registers may be tied up by the more operand. Instead,
650 ;;; we use MAYBE-LOAD-STACK-TN.
651 (define-vop (known-call-local)
652 (:args (fp)
653 (nfp)
654 (args :more t))
655 (:results (res :more t))
656 (:move-args :local-call)
657 (:save-p t)
658 (:info save callee target)
659 (:ignore args res save nfp callee)
660 (:vop-var vop)
661 (:generator 5
662 (move ebp-tn fp)
663 (note-this-location vop :call-site)
664 (inst call target)
665 (note-this-location vop :known-return)))
667 ;;; From Douglas Crosher
668 ;;; Return from known values call. We receive the return locations as
669 ;;; arguments to terminate their lifetimes in the returning function. We
670 ;;; restore FP and CSP and jump to the Return-PC.
671 (define-vop (known-return)
672 (:args (old-fp)
673 (return-pc)
674 (vals :more t))
675 (:move-args :known-return)
676 (:info val-locs)
677 (:ignore val-locs vals)
678 (:vop-var vop)
679 (:generator 6
680 (check-ocfp-and-return-pc old-fp return-pc)
681 ;; Zot all of the stack except for the old-fp and return-pc.
682 (inst mov esp-tn ebp-tn)
683 (inst pop ebp-tn)
684 (inst ret)))
686 ;;;; full call
688 ;;; There is something of a cross-product effect with full calls.
689 ;;; Different versions are used depending on whether we know the
690 ;;; number of arguments or the name of the called function, and
691 ;;; whether we want fixed values, unknown values, or a tail call.
693 ;;; In full call, the arguments are passed creating a partial frame on
694 ;;; the stack top and storing stack arguments into that frame. On
695 ;;; entry to the callee, this partial frame is pointed to by FP.
697 ;;; This macro helps in the definition of full call VOPs by avoiding
698 ;;; code replication in defining the cross-product VOPs.
700 ;;; NAME is the name of the VOP to define.
702 ;;; NAMED is true if the first argument is an fdefinition object whose
703 ;;; definition is to be called.
705 ;;; RETURN is either :FIXED, :UNKNOWN or :TAIL:
706 ;;; -- If :FIXED, then the call is for a fixed number of values, returned in
707 ;;; the standard passing locations (passed as result operands).
708 ;;; -- If :UNKNOWN, then the result values are pushed on the stack, and the
709 ;;; result values are specified by the Start and Count as in the
710 ;;; unknown-values continuation representation.
711 ;;; -- If :TAIL, then do a tail-recursive call. No values are returned.
712 ;;; The Old-Fp and Return-PC are passed as the second and third arguments.
714 ;;; In non-tail calls, the pointer to the stack arguments is passed as
715 ;;; the last fixed argument. If Variable is false, then the passing
716 ;;; locations are passed as a more arg. Variable is true if there are
717 ;;; a variable number of arguments passed on the stack. Variable
718 ;;; cannot be specified with :TAIL return. TR variable argument call
719 ;;; is implemented separately.
721 ;;; In tail call with fixed arguments, the passing locations are
722 ;;; passed as a more arg, but there is no new-FP, since the arguments
723 ;;; have been set up in the current frame.
724 (macrolet ((define-full-call (name named return variable)
725 (aver (not (and variable (eq return :tail))))
726 `(define-vop (,name
727 ,@(when (eq return :unknown)
728 '(unknown-values-receiver)))
729 (:args
730 ,@(unless (eq return :tail)
731 '((new-fp :scs (any-reg) :to (:argument 1))))
733 (fun :scs (descriptor-reg control-stack)
734 :target eax :to (:argument 0))
736 ,@(when (eq return :tail)
737 '((old-fp)
738 (return-pc)))
740 ,@(unless variable '((args :more t :scs (descriptor-reg)))))
742 ,@(when (eq return :fixed)
743 '((:results (values :more t))))
745 (:save-p ,(if (eq return :tail) :compute-only t))
747 ,@(unless (or (eq return :tail) variable)
748 '((:move-args :full-call)))
750 (:vop-var vop)
751 (:info
752 ,@(unless (or variable (eq return :tail)) '(arg-locs))
753 ,@(unless variable '(nargs))
754 ,@(when (eq return :fixed) '(nvals))
755 step-instrumenting)
757 (:ignore
758 ,@(unless (or variable (eq return :tail)) '(arg-locs))
759 ,@(unless variable '(args)))
761 ;; We pass either the fdefn object (for named call) or
762 ;; the actual function object (for unnamed call) in
763 ;; EAX. With named call, closure-tramp will replace it
764 ;; with the real function and invoke the real function
765 ;; for closures. Non-closures do not need this value,
766 ;; so don't care what shows up in it.
767 (:temporary
768 (:sc descriptor-reg
769 :offset eax-offset
770 :from (:argument 0)
771 :to :eval)
772 eax)
774 ;; We pass the number of arguments in ECX.
775 (:temporary (:sc unsigned-reg :offset ecx-offset :to :eval) ecx)
777 ;; With variable call, we have to load the
778 ;; register-args out of the (new) stack frame before
779 ;; doing the call. Therefore, we have to tell the
780 ;; lifetime stuff that we need to use them.
781 ,@(when variable
782 (mapcar (lambda (name offset)
783 `(:temporary (:sc descriptor-reg
784 :offset ,offset
785 :from (:argument 0)
786 :to :eval)
787 ,name))
788 *register-arg-names* *register-arg-offsets*))
790 ,@(when (eq return :tail)
791 '((:temporary (:sc unsigned-reg
792 :from (:argument 1)
793 :to (:argument 2))
794 old-fp-tmp)))
795 ,@(unless (eq return :tail)
796 '((:node-var node)))
798 (:generator ,(+ (if named 5 0)
799 (if variable 19 1)
800 (if (eq return :tail) 0 10)
802 (if (eq return :unknown) 25 0))
804 ;; This has to be done before the frame pointer is
805 ;; changed! EAX stores the 'lexical environment' needed
806 ;; for closures.
807 (move eax fun)
810 ,@(if variable
811 ;; For variable call, compute the number of
812 ;; arguments and move some of the arguments to
813 ;; registers.
814 (collect ((noise))
815 ;; Compute the number of arguments.
816 (noise '(inst mov ecx new-fp))
817 (noise '(inst sub ecx esp-tn))
818 ;; Move the necessary args to registers,
819 ;; this moves them all even if they are
820 ;; not all needed.
821 (loop
822 for name in *register-arg-names*
823 for index downfrom -1
824 do (noise `(loadw ,name new-fp ,index)))
825 (noise))
826 '((if (zerop nargs)
827 (inst xor ecx ecx)
828 (inst mov ecx (fixnumize nargs)))))
829 ,@(cond ((eq return :tail)
830 '(;; Python has figured out what frame we should
831 ;; return to so might as well use that clue.
832 ;; This seems really important to the
833 ;; implementation of things like
834 ;; (without-interrupts ...)
836 ;; dtc; Could be doing a tail call from a
837 ;; known-local-call etc in which the old-fp
838 ;; or ret-pc are in regs or in non-standard
839 ;; places. If the passing location were
840 ;; wired to the stack in standard locations
841 ;; then these moves will be un-necessary;
842 ;; this is probably best for the x86.
843 (sc-case old-fp
844 ((control-stack)
845 (unless (= ocfp-save-offset
846 (tn-offset old-fp))
847 ;; FIXME: FORMAT T for stale
848 ;; diagnostic output (several of
849 ;; them around here), ick
850 (error "** tail-call old-fp not S0~%")
851 (move old-fp-tmp old-fp)
852 (storew old-fp-tmp
853 ebp-tn
854 (frame-word-offset ocfp-save-offset))))
855 ((any-reg descriptor-reg)
856 (error "** tail-call old-fp in reg not S0~%")
857 (storew old-fp
858 ebp-tn
859 (frame-word-offset ocfp-save-offset))))
861 ;; For tail call, we have to push the
862 ;; return-pc so that it looks like we CALLed
863 ;; despite the fact that we are going to JMP.
864 (inst push return-pc)
867 ;; For non-tail call, we have to save our
868 ;; frame pointer and install the new frame
869 ;; pointer. We can't load stack tns after this
870 ;; point.
871 `(;; Python doesn't seem to allocate a frame
872 ;; here which doesn't leave room for the
873 ;; ofp/ret stuff.
875 ;; The variable args are on the stack and
876 ;; become the frame, but there may be <3
877 ;; args and 3 stack slots are assumed
878 ;; allocate on the call. So need to ensure
879 ;; there are at least 3 slots. This hack
880 ;; just adds 3 more.
881 ,(if variable
882 '(inst sub esp-tn (fixnumize 3)))
884 ;; Bias the new-fp for use as an fp
885 ,(if variable
886 '(inst sub new-fp (fixnumize sp->fp-offset)))
888 ;; Save the fp
889 (storew ebp-tn new-fp
890 (frame-word-offset ocfp-save-offset))
892 (move ebp-tn new-fp) ; NB - now on new stack frame.
895 (when step-instrumenting
896 (emit-single-step-test)
897 (inst jmp :eq DONE)
898 (inst break single-step-around-trap))
899 DONE
901 (note-this-location vop :call-site)
903 (inst ,(if (eq return :tail) 'jmp 'call)
904 ,(if named
905 '(make-ea-for-object-slot eax fdefn-raw-addr-slot
906 other-pointer-lowtag)
907 '(make-ea-for-object-slot eax closure-fun-slot
908 fun-pointer-lowtag)))
909 ,@(ecase return
910 (:fixed
911 '((default-unknown-values vop values nvals node)))
912 (:unknown
913 '((note-this-location vop :unknown-return)
914 (receive-unknown-values values-start nvals start count
915 node)))
916 (:tail))))))
918 (define-full-call call nil :fixed nil)
919 (define-full-call call-named t :fixed nil)
920 (define-full-call multiple-call nil :unknown nil)
921 (define-full-call multiple-call-named t :unknown nil)
922 (define-full-call tail-call nil :tail nil)
923 (define-full-call tail-call-named t :tail nil)
925 (define-full-call call-variable nil :fixed t)
926 (define-full-call multiple-call-variable nil :unknown t))
928 ;;; This is defined separately, since it needs special code that BLT's
929 ;;; the arguments down. All the real work is done in the assembly
930 ;;; routine. We just set things up so that it can find what it needs.
931 (define-vop (tail-call-variable)
932 (:args (args :scs (any-reg control-stack) :target esi)
933 (function :scs (descriptor-reg control-stack) :target eax)
934 (old-fp)
935 (return-pc))
936 (:temporary (:sc unsigned-reg :offset esi-offset :from (:argument 0)) esi)
937 (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 1)) eax)
938 (:generator 75
939 (check-ocfp-and-return-pc old-fp return-pc)
940 ;; Move these into the passing locations if they are not already there.
941 (move esi args)
942 (move eax function)
943 ;; And jump to the assembly routine.
944 (inst jmp (make-fixup 'tail-call-variable :assembly-routine))))
946 ;;;; unknown values return
948 ;;; Return a single-value using the Unknown-Values convention.
950 ;;; pfw--get wired-tn conflicts sometimes if register sc specd for args
951 ;;; having problems targeting args to regs -- using temps instead.
953 ;;; First off, modifying the return-pc defeats the branch-prediction
954 ;;; optimizations on modern CPUs quite handily. Second, we can do all
955 ;;; this without needing a temp register. Fixed the latter, at least.
956 ;;; -- AB 2006/Feb/04
957 (define-vop (return-single)
958 (:args (old-fp)
959 (return-pc)
960 (value))
961 (:ignore value)
962 (:generator 6
963 (check-ocfp-and-return-pc old-fp return-pc)
964 ;; Drop stack above old-fp
965 (inst mov esp-tn ebp-tn)
966 ;; Clear the multiple-value return flag
967 (inst clc)
968 ;; Restore the old frame pointer
969 (inst pop ebp-tn)
970 ;; And return.
971 (inst ret)))
973 ;;; Do unknown-values return of a fixed (other than 1) number of
974 ;;; values. The VALUES are required to be set up in the standard
975 ;;; passing locations. NVALS is the number of values returned.
977 ;;; Basically, we just load ECX with the number of values returned and
978 ;;; EBX with a pointer to the values, set ESP to point to the end of
979 ;;; the values, and jump directly to return-pc.
980 (define-vop (return)
981 (:args (old-fp)
982 (return-pc :to (:eval 1))
983 (values :more t))
984 (:ignore values)
985 (:info nvals)
986 ;; In the case of other than one value, we need these registers to
987 ;; tell the caller where they are and how many there are.
988 (:temporary (:sc unsigned-reg :offset ebx-offset) ebx)
989 (:temporary (:sc unsigned-reg :offset ecx-offset) ecx)
990 ;; We need to stretch the lifetime of return-pc past the argument
991 ;; registers so that we can default the argument registers without
992 ;; trashing return-pc.
993 (:temporary (:sc unsigned-reg :offset (first *register-arg-offsets*)
994 :from :eval) a0)
995 (:temporary (:sc unsigned-reg :offset (second *register-arg-offsets*)
996 :from :eval) a1)
997 (:temporary (:sc unsigned-reg :offset (third *register-arg-offsets*)
998 :from :eval) a2)
1000 (:generator 6
1001 (check-ocfp-and-return-pc old-fp return-pc)
1002 (when (= nvals 1)
1003 ;; This is handled in RETURN-SINGLE.
1004 (error "nvalues is 1"))
1005 ;; Establish the values pointer and values count.
1006 (inst lea ebx (make-ea :dword :base ebp-tn
1007 :disp (* sp->fp-offset n-word-bytes)))
1008 (if (zerop nvals)
1009 (inst xor ecx ecx) ; smaller
1010 (inst mov ecx (fixnumize nvals)))
1011 ;; Pre-default any argument register that need it.
1012 (when (< nvals register-arg-count)
1013 (let* ((arg-tns (nthcdr nvals (list a0 a1 a2)))
1014 (first (first arg-tns)))
1015 (inst mov first nil-value)
1016 (dolist (tn (cdr arg-tns))
1017 (inst mov tn first))))
1018 ;; Set the multiple value return flag.
1019 (inst stc)
1020 ;; And away we go. Except that return-pc is still on the
1021 ;; stack and we've changed the stack pointer. So we have to
1022 ;; tell it to index off of EBX instead of EBP.
1023 (cond ((<= nvals register-arg-count)
1024 (inst mov esp-tn ebp-tn)
1025 (inst pop ebp-tn)
1026 (inst ret))
1028 ;; Some values are on the stack after RETURN-PC and OLD-FP,
1029 ;; can't return normally and some slots of the frame will
1030 ;; be used as temporaries by the receiver.
1032 ;; Clear as much of the stack as possible, but not past the
1033 ;; old frame address.
1034 (inst lea esp-tn
1035 (make-ea :dword :base ebp-tn
1036 :disp (frame-byte-offset (1- nvals))))
1037 (move ebp-tn old-fp)
1038 (inst push (make-ea :dword :base ebx
1039 :disp (frame-byte-offset
1040 (+ sp->fp-offset
1041 (tn-offset return-pc)))))
1042 (inst ret)))))
1044 ;;; Do unknown-values return of an arbitrary number of values (passed
1045 ;;; on the stack.) We check for the common case of a single return
1046 ;;; value, and do that inline using the normal single value return
1047 ;;; convention. Otherwise, we branch off to code that calls an
1048 ;;; assembly-routine.
1050 ;;; The assembly routine takes the following args:
1051 ;;; ECX -- number of values to find there.
1052 ;;; ESI -- pointer to where to find the values.
1053 (define-vop (return-multiple)
1054 (:args (old-fp)
1055 (return-pc)
1056 (vals :scs (any-reg) :target esi)
1057 (nvals :scs (any-reg) :target ecx))
1058 (:temporary (:sc unsigned-reg :offset esi-offset :from (:argument 2)) esi)
1059 (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 3)) ecx)
1060 (:temporary (:sc descriptor-reg :offset (first *register-arg-offsets*)
1061 :from (:eval 0)) a0)
1062 (:node-var node)
1063 (:generator 13
1064 (check-ocfp-and-return-pc old-fp return-pc)
1065 (unless (policy node (> space speed))
1066 ;; Check for the single case.
1067 (let ((not-single (gen-label)))
1068 (inst cmp nvals (fixnumize 1))
1069 (inst jmp :ne not-single)
1070 ;; Return with one value.
1071 (loadw a0 vals -1)
1072 ;; Clear the stack until ocfp.
1073 (inst mov esp-tn ebp-tn)
1074 ;; clear the multiple-value return flag
1075 (inst clc)
1076 ;; Out of here.
1077 (inst pop ebp-tn)
1078 (inst ret)
1079 ;; Nope, not the single case. Jump to the assembly routine.
1080 (emit-label not-single)))
1081 (move esi vals)
1082 (move ecx nvals)
1083 (inst jmp (make-fixup 'return-multiple :assembly-routine))))
1085 ;;;; XEP hackery
1087 ;;; Get the lexical environment from its passing location.
1088 (define-vop (setup-closure-environment)
1089 (:results (closure :scs (descriptor-reg)))
1090 (:info label)
1091 (:ignore label)
1092 (:generator 6
1093 ;; Get result.
1094 (move closure eax-tn)))
1096 ;;; Copy a &MORE arg from the argument area to the end of the current
1097 ;;; frame. FIXED is the number of non-&MORE arguments.
1099 ;;; The tricky part is doing this without trashing any of the calling
1100 ;;; convention registers that are still needed. This vop is emitted
1101 ;;; directly after the xep-allocate frame. That means the registers
1102 ;;; are in use as follows:
1104 ;;; EAX -- The lexenv.
1105 ;;; EBX -- Available.
1106 ;;; ECX -- The total number of arguments * N-WORD-BYTES.
1107 ;;; EDX -- The first arg.
1108 ;;; EDI -- The second arg.
1109 ;;; ESI -- The third arg.
1111 ;;; So basically, we have one register available for our use: EBX.
1113 ;;; What we can do is push the other regs onto the stack, and then
1114 ;;; restore their values by looking directly below where we put the
1115 ;;; more-args.
1116 (define-vop (copy-more-arg)
1117 (:info fixed)
1118 (:generator 20
1119 ;; Avoid the copy if there are no more args.
1120 (cond ((zerop fixed)
1121 (inst jecxz JUST-ALLOC-FRAME))
1123 (inst cmp ecx-tn (fixnumize fixed))
1124 (inst jmp :be JUST-ALLOC-FRAME)))
1126 ;; Allocate the space on the stack.
1127 ;; stack = ebp + sp->fp-offset - (max 3 frame-size) - (nargs - fixed)
1129 ;; Problem: this might leave some &more args outside esp, so
1130 ;; clamp the movement for now. If fixed > frame-size, reset
1131 ;; esp to the end of the current &more args (which *should*
1132 ;; be a noop?), and only set esp to its final value after the
1133 ;; stack-stack memmove loop. Otherwise, an unlucky signal
1134 ;; could end up overwriting the &more arguments before they're
1135 ;; moved in their final place.
1136 (inst lea ebx-tn
1137 (make-ea :dword :base ebp-tn
1138 :disp (* n-word-bytes
1139 (- sp->fp-offset
1140 (max 0
1141 (- (max 3 (sb-allocated-size 'stack))
1142 fixed))))))
1143 (inst sub ebx-tn ecx-tn) ; Got the new stack in ebx
1144 (inst mov esp-tn ebx-tn)
1146 ;; Now: nargs>=1 && nargs>fixed
1148 ;; Save the original count of args.
1149 (inst mov ebx-tn ecx-tn)
1151 (cond ((< fixed register-arg-count)
1152 ;; We must stop when we run out of stack args, not when we
1153 ;; run out of more args.
1154 ;; Number to copy = nargs-3
1155 (inst sub ecx-tn (fixnumize register-arg-count))
1156 ;; Everything of interest in registers.
1157 (inst jmp :be DO-REGS))
1159 ;; Number to copy = nargs-fixed
1160 (inst sub ecx-tn (fixnumize fixed))))
1162 (let ((delta (* n-word-bytes
1163 (- (max 3 (sb-allocated-size 'stack))
1164 fixed)))
1165 (LOOP (gen-label)))
1166 (cond ((zerop delta)
1167 ;; nothing to move!
1169 ((minusp delta)
1170 ;; stack frame smaller than fixed; moving args to higher
1171 ;; addresses (stack grows downard), so copy from the
1172 ;; end. Moreover, because we'd have to shrink the frame,
1173 ;; esp currently points at the end of the source args.
1174 (inst push ebx-tn)
1176 (emit-label LOOP)
1177 (inst sub ecx-tn n-word-bytes)
1178 (inst mov ebx-tn (make-ea :dword
1179 :base esp-tn :index ecx-tn
1180 ;; compensate for PUSH above
1181 :disp n-word-bytes))
1182 (inst mov (make-ea :dword
1183 :base esp-tn :index ecx-tn
1184 ;; compensate for PUSH, and
1185 ;; add (abs delta)
1186 :disp (- n-word-bytes delta))
1187 ebx-tn)
1188 (inst jmp :nz LOOP)
1190 (inst pop ebx-tn))
1191 ((plusp delta)
1192 ;; stack frame larger than fixed. Moving args to lower
1193 ;; addresses, so copy from the lowest address. esp
1194 ;; already points to the lowest address of the destination.
1195 (inst push ebx-tn)
1196 (inst push esi-tn)
1198 (inst xor ebx-tn ebx-tn)
1199 (emit-label LOOP)
1200 (inst mov esi-tn (make-ea :dword
1201 :base esp-tn :index ebx-tn
1202 ;; PUSHed 2 words
1203 :disp (+ (* 2 n-word-bytes)
1204 delta)))
1205 (inst mov (make-ea :dword
1206 :base esp-tn :index ebx-tn
1207 :disp (* 2 n-word-bytes))
1208 esi-tn)
1209 (inst add ebx-tn n-word-bytes)
1210 (inst sub ecx-tn n-word-bytes)
1211 (inst jmp :nz LOOP)
1213 (inst pop esi-tn)
1214 (inst pop ebx-tn))))
1215 DO-REGS
1216 ;; stack can now be set to its final size
1217 (when (< (max 3 (sb-allocated-size 'stack)) fixed)
1218 (inst add esp-tn (* n-word-bytes
1219 (- fixed
1220 (max 3 (sb-allocated-size 'stack))))))
1222 ;; Restore ECX
1223 (inst mov ecx-tn ebx-tn)
1225 ;; Here: nargs>=1 && nargs>fixed
1226 (when (< fixed register-arg-count)
1227 ;; Now we have to deposit any more args that showed up in
1228 ;; registers.
1229 (do ((i fixed))
1230 ( nil )
1231 ;; Store it relative to ebp
1232 (inst mov (make-ea :dword :base ebp-tn
1233 :disp (* n-word-bytes
1234 (- sp->fp-offset
1235 (+ 1
1236 (- i fixed)
1237 (max 3 (sb-allocated-size
1238 'stack))))))
1239 (nth i *register-arg-tns*))
1241 (incf i)
1242 (when (>= i register-arg-count)
1243 (return))
1245 ;; Don't deposit any more than there are.
1246 (if (zerop i)
1247 (inst test ecx-tn ecx-tn)
1248 (inst cmp ecx-tn (fixnumize i)))
1249 (inst jmp :eq DONE)))
1251 (inst jmp DONE)
1253 JUST-ALLOC-FRAME
1254 (inst lea esp-tn
1255 (make-ea :dword :base ebp-tn
1256 :disp (* n-word-bytes
1257 (- sp->fp-offset
1258 (max 3 (sb-allocated-size 'stack))))))
1260 DONE))
1262 (define-vop (more-kw-arg)
1263 (:translate sb!c::%more-kw-arg)
1264 (:policy :fast-safe)
1265 (:args (object :scs (descriptor-reg) :to (:result 1))
1266 (index :scs (any-reg immediate) :to (:result 1) :target keyword))
1267 (:arg-types * tagged-num)
1268 (:results (value :scs (descriptor-reg any-reg))
1269 (keyword :scs (descriptor-reg any-reg)))
1270 (:result-types * *)
1271 (:generator 4
1272 (sc-case index
1273 (immediate
1274 (inst mov value (make-ea :dword :base object :disp (tn-value index)))
1275 (inst mov keyword (make-ea :dword :base object
1276 :disp (+ (tn-value index) n-word-bytes))))
1278 (inst mov value (make-ea :dword :base object :index index))
1279 (inst mov keyword (make-ea :dword :base object :index index
1280 :disp n-word-bytes))))))
1282 (define-vop (more-arg/c)
1283 (:translate sb!c::%more-arg)
1284 (:policy :fast-safe)
1285 (:args (object :scs (descriptor-reg) :to (:result 1)))
1286 (:info index)
1287 (:arg-types * (:constant (signed-byte 32)))
1288 (:results (value :scs (descriptor-reg any-reg)))
1289 (:result-types *)
1290 (:generator 3
1291 (inst mov value (make-ea :dword :base object
1292 :disp (- (* index n-word-bytes))))))
1294 (define-vop (more-arg)
1295 (:translate sb!c::%more-arg)
1296 (:policy :fast-safe)
1297 (:args (object :scs (descriptor-reg) :to (:result 1))
1298 (index :scs (any-reg) :to (:result 1) :target value))
1299 (:arg-types * tagged-num)
1300 (:results (value :scs (descriptor-reg any-reg)))
1301 (:result-types *)
1302 (:generator 4
1303 (move value index)
1304 (inst neg value)
1305 (inst mov value (make-ea :dword :base object :index value))))
1307 ;;; Turn more arg (context, count) into a list.
1308 (define-vop (listify-rest-args)
1309 (:translate %listify-rest-args)
1310 (:policy :safe)
1311 (:args (context :scs (descriptor-reg) :target src)
1312 (count :scs (any-reg) :target ecx))
1313 (:arg-types * tagged-num)
1314 (:temporary (:sc unsigned-reg :offset esi-offset :from (:argument 0)) src)
1315 (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 1)) ecx)
1316 (:temporary (:sc unsigned-reg :offset eax-offset) eax)
1317 (:temporary (:sc unsigned-reg) dst)
1318 (:results (result :scs (descriptor-reg)))
1319 (:node-var node)
1320 (:generator 20
1321 (let ((enter (gen-label))
1322 (loop (gen-label))
1323 (done (gen-label))
1324 (stack-allocate-p (node-stack-allocate-p node)))
1325 (move src context)
1326 (move ecx count)
1327 ;; Check to see whether there are no args, and just return NIL if so.
1328 (inst mov result nil-value)
1329 (inst jecxz done)
1330 (inst lea dst (make-ea :dword :base ecx :index ecx))
1331 (maybe-pseudo-atomic stack-allocate-p
1332 (allocation dst dst node stack-allocate-p list-pointer-lowtag)
1333 ;; Set decrement mode (successive args at lower addresses)
1334 (inst std)
1335 ;; Set up the result.
1336 (move result dst)
1337 ;; Jump into the middle of the loop, 'cause that's where we want
1338 ;; to start.
1339 (inst jmp enter)
1340 (emit-label loop)
1341 ;; Compute a pointer to the next cons.
1342 (inst add dst (* cons-size n-word-bytes))
1343 ;; Store a pointer to this cons in the CDR of the previous cons.
1344 (storew dst dst -1 list-pointer-lowtag)
1345 (emit-label enter)
1346 ;; Grab one value and stash it in the car of this cons.
1347 (inst lods eax)
1348 (storew eax dst 0 list-pointer-lowtag)
1349 ;; Go back for more.
1350 (inst sub ecx n-word-bytes)
1351 (inst jmp :nz loop)
1352 ;; NIL out the last cons.
1353 (storew nil-value dst 1 list-pointer-lowtag)
1354 (inst cld))
1355 (emit-label done))))
1357 ;;; Return the location and size of the &MORE arg glob created by
1358 ;;; COPY-MORE-ARG. SUPPLIED is the total number of arguments supplied
1359 ;;; (originally passed in ECX). FIXED is the number of non-rest
1360 ;;; arguments.
1362 ;;; We must duplicate some of the work done by COPY-MORE-ARG, since at
1363 ;;; that time the environment is in a pretty brain-damaged state,
1364 ;;; preventing this info from being returned as values. What we do is
1365 ;;; compute supplied - fixed, and return a pointer that many words
1366 ;;; below the current stack top.
1367 (define-vop (more-arg-context)
1368 (:policy :fast-safe)
1369 (:translate sb!c::%more-arg-context)
1370 (:args (supplied :scs (any-reg) :target count))
1371 (:arg-types positive-fixnum (:constant fixnum))
1372 (:info fixed)
1373 (:results (context :scs (descriptor-reg))
1374 (count :scs (any-reg)))
1375 (:result-types t tagged-num)
1376 (:note "more-arg-context")
1377 (:generator 5
1378 (move count supplied)
1379 ;; SP at this point points at the last arg pushed.
1380 ;; Point to the first more-arg, not above it.
1381 (inst lea context (make-ea :dword :base esp-tn
1382 :index count :scale 1
1383 :disp (- (+ (fixnumize fixed) n-word-bytes))))
1384 (unless (zerop fixed)
1385 (inst sub count (fixnumize fixed)))))
1387 ;;; Signal wrong argument count error if NARGS isn't equal to COUNT.
1388 (define-vop (verify-arg-count)
1389 (:policy :fast-safe)
1390 (:args (nargs :scs (any-reg)))
1391 (:arg-types positive-fixnum (:constant t) (:constant t))
1392 (:info min max)
1393 (:vop-var vop)
1394 (:save-p :compute-only)
1395 (:generator 3
1396 (let ((err-lab
1397 (generate-error-code vop 'invalid-arg-count-error nargs)))
1398 (flet ((check-min ()
1399 (cond ((= min 1)
1400 (inst test nargs nargs)
1401 (inst jmp :e err-lab))
1402 ((plusp min)
1403 (inst cmp nargs (fixnumize min))
1404 (inst jmp :b err-lab)))))
1405 (cond ((not min)
1406 (if (zerop max)
1407 (inst test nargs nargs)
1408 (inst cmp nargs (fixnumize max)))
1409 (inst jmp :ne err-lab))
1410 (max
1411 (check-min)
1412 (inst cmp nargs (fixnumize max))
1413 (inst jmp :a err-lab))
1415 (check-min)))))))
1417 ;;; Single-stepping
1418 (defun emit-single-step-test ()
1419 ;; We use different ways of representing whether stepping is on on
1420 ;; +SB-THREAD / -SB-THREAD: on +SB-THREAD, we use a slot in the
1421 ;; thread structure. On -SB-THREAD we use the value of a static
1422 ;; symbol. Things are done this way, since reading a thread-local
1423 ;; slot from a symbol would require an extra register on +SB-THREAD,
1424 ;; and reading a slot from a thread structure would require an extra
1425 ;; register on -SB-THREAD.
1426 #!+sb-thread
1427 (progn
1428 #!+win32 (inst push eax-tn)
1429 (with-tls-ea (EA :base #!+win32 eax-tn #!-win32 :unused
1430 :disp-type :constant
1431 :disp (* thread-stepping-slot n-word-bytes))
1432 (inst cmp EA 0 :maybe-fs))
1433 #!+win32 (inst pop eax-tn))
1434 #!-sb-thread
1435 (inst cmp (make-ea-for-symbol-value sb!impl::*stepping*) 0))
1437 (define-vop (step-instrument-before-vop)
1438 (:policy :fast-safe)
1439 (:vop-var vop)
1440 (:generator 3
1441 (emit-single-step-test)
1442 (inst jmp :eq DONE)
1443 (inst break single-step-before-trap)
1444 DONE
1445 (note-this-location vop :step-before-vop)))