1 ;;;; the machine specific support routines needed by the file assembler
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.
16 ;;; For RETURN-MULTIPLE, we have to move the results from the end of
17 ;;; the frame for the function that is returning to the end of the
18 ;;; frame for the function being returned to.
20 #+sb-assembling ;; We don't want a vop for this one.
21 (define-assembly-routine
22 (return-multiple (:return-style :none))
23 (;; These are really arguments.
24 (:temp ecx unsigned-reg ecx-offset)
25 (:temp esi unsigned-reg esi-offset)
27 ;; These we need as temporaries.
28 (:temp eax unsigned-reg eax-offset)
29 (:temp ebx unsigned-reg ebx-offset)
30 (:temp edx unsigned-reg edx-offset)
31 (:temp edi unsigned-reg edi-offset))
33 ;; Pick off the cases where everything fits in register args.
34 (inst jecxz ZERO-VALUES)
35 (inst cmp ecx (fixnumize 1))
36 (inst jmp :e ONE-VALUE)
37 (inst cmp ecx (fixnumize 2))
38 (inst jmp :e TWO-VALUES)
39 (inst cmp ecx (fixnumize 3))
40 (inst jmp :e THREE-VALUES)
42 ;; As per the calling convention EBX is expected to point at the SP
43 ;; before the stack frame.
44 (inst lea ebx (make-ea :dword :base ebp-tn
45 :disp (* sp->fp-offset n-word-bytes)))
47 ;; Save the count, the return address and restore the frame pointer,
48 ;; because the loop is going to destroy them.
50 (inst mov eax (make-ea :dword :base ebp-tn
51 :disp (frame-byte-offset return-pc-save-offset)))
52 (inst mov ebp-tn (make-ea :dword :base ebp-tn
53 :disp (frame-byte-offset ocfp-save-offset)))
54 ;; Blit the values down the stack. Note: there might be overlap, so
55 ;; we have to be careful not to clobber values before we've read
56 ;; them. Because the stack builds down, we are copying to a larger
57 ;; address. Therefore, we need to iterate from larger addresses to
58 ;; smaller addresses. pfw-this says copy ecx words from esi to edi
60 (inst shr ecx (1- n-lowtag-bits))
61 (inst std) ; count down
62 (inst sub esi n-word-bytes)
63 (inst lea edi (make-ea :dword :base ebx :disp (- n-word-bytes)))
71 ;; Set the stack top to the last result.
72 (inst lea esp-tn (make-ea :dword :base edi :disp n-word-bytes))
74 ;; Load the register args.
84 ;; Handle the register arg cases.
86 (inst lea ebx (make-ea :dword :base ebp-tn
87 :disp (* sp->fp-offset n-word-bytes)))
88 (inst mov edx nil-value)
91 (inst mov esp-tn ebp-tn)
96 ;; Note: we can get this, because the return-multiple vop doesn't
97 ;; check for this case when size > speed.
100 (inst mov esp-tn ebp-tn)
106 (inst lea ebx (make-ea :dword :base ebp-tn
107 :disp (* sp->fp-offset n-word-bytes)))
110 (inst mov esi nil-value)
111 (inst mov esp-tn ebp-tn)
117 (inst lea ebx (make-ea :dword :base ebp-tn
118 :disp (* sp->fp-offset n-word-bytes)))
122 (inst mov esp-tn ebp-tn)
127 ;;;; TAIL-CALL-VARIABLE
129 ;;; For tail-call-variable, we have to copy the arguments from the end
130 ;;; of our stack frame (were args are produced) to the start of our
131 ;;; stack frame (were args are expected).
133 ;;; We take the function to call in EAX and a pointer to the arguments in
134 ;;; ESI. EBP says the same over the jump, and the old frame pointer is
135 ;;; still saved in the first stack slot. The return-pc is saved in
136 ;;; the second stack slot, so we have to push it to make it look like
137 ;;; we actually called. We also have to compute ECX from the difference
138 ;;; between ESI and the stack top.
139 #+sb-assembling ;; No vop for this one either.
140 (define-assembly-routine
142 (:return-style :none))
144 ((:temp eax unsigned-reg eax-offset)
145 (:temp ebx unsigned-reg ebx-offset)
146 (:temp ecx unsigned-reg ecx-offset)
147 (:temp edx unsigned-reg edx-offset)
148 (:temp edi unsigned-reg edi-offset)
149 (:temp esi unsigned-reg esi-offset))
151 ;; Calculate NARGS (as a fixnum)
153 (inst sub ecx esp-tn)
155 ;; Check for all the args fitting the registers.
156 (inst cmp ecx (fixnumize 3))
157 (inst jmp :le REGISTER-ARGS)
159 ;; Save the OLD-FP and RETURN-PC because the blit is going to trash
160 ;; those stack locations. Save the ECX, because the loop is going to
162 (pushw ebp-tn (frame-word-offset ocfp-save-offset))
163 (loadw ebx ebp-tn (frame-word-offset return-pc-save-offset))
166 ;; Do the blit. Because we are coping from smaller addresses to
167 ;; larger addresses, we have to start at the largest pair and work
169 (inst shr ecx (1- n-lowtag-bits))
170 (inst std) ; count down
171 (inst lea edi (make-ea :dword :base ebp-tn :disp (frame-byte-offset 0)))
172 (inst sub esi (fixnumize 1))
177 ;; Load the register arguments carefully.
178 (loadw edx ebp-tn (frame-word-offset ocfp-save-offset))
180 ;; Restore OLD-FP and ECX.
183 (popw ebp-tn (frame-word-offset ocfp-save-offset))
185 ;; Blow off the stack above the arguments.
186 (inst lea esp-tn (make-ea :dword :base edi :disp n-word-bytes))
188 ;; remaining register args
190 (loadw edx ebp-tn (frame-word-offset 0))
191 (loadw esi ebp-tn (frame-word-offset 2))
193 ;; Push the (saved) return-pc so it looks like we just called.
196 ;; And jump into the function.
197 (inst jmp (make-ea-for-object-slot eax closure-fun-slot fun-pointer-lowtag))
199 ;; All the arguments fit in registers, so load them.
205 ;; Clear most of the stack.
207 (make-ea :dword :base ebp-tn :disp (* (- sp->fp-offset 3) n-word-bytes)))
209 ;; Push the return-pc so it looks like we just called.
210 (pushw ebp-tn (frame-word-offset return-pc-save-offset))
213 (inst jmp (make-ea-for-object-slot eax closure-fun-slot fun-pointer-lowtag)))
215 (define-assembly-routine (throw
216 (:return-style :raw))
217 ((:arg target (descriptor-reg any-reg) edx-offset)
218 (:arg start any-reg ebx-offset)
219 (:arg count any-reg ecx-offset)
220 (:temp catch any-reg eax-offset))
222 (declare (ignore start count))
224 (load-tl-symbol-value catch *current-catch-block*)
228 (let ((error (gen-label)))
229 (assemble (*elsewhere*)
232 ;; Fake up a stack frame so that backtraces come out right.
234 (inst mov ebp-tn esp-tn)
236 (emit-error-break nil error-trap
237 (error-number-or-lose 'unseen-throw-tag-error)
239 (inst test catch catch) ; check for NULL pointer
242 (inst cmp target (make-ea-for-object-slot catch catch-block-tag-slot 0))
245 (loadw catch catch catch-block-previous-catch-slot)
250 ;; Here EAX points to catch block containing symbol pointed to by EDX.
251 (inst jmp (make-fixup 'unwind :assembly-routine)))
253 ;;;; non-local exit noise
256 (define-assembly-routine (unwind
257 (:return-style :none)
258 (:translate %continue-unwind)
259 (:policy :fast-safe))
260 ((:arg block (any-reg descriptor-reg) eax-offset)
261 (:arg start (any-reg descriptor-reg) ebx-offset)
262 (:arg count (any-reg descriptor-reg) ecx-offset)
263 (:temp uwp unsigned-reg esi-offset))
264 (declare (ignore start count))
266 (let ((error (generate-error-code nil 'invalid-unwind-error)))
267 (inst test block block) ; check for NULL pointer
270 (load-tl-symbol-value uwp *current-unwind-protect-block*)
272 ;; Does *CURRENT-UNWIND-PROTECT-BLOCK* match the value stored in
273 ;; argument's CURRENT-UWP-SLOT?
275 (make-ea-for-object-slot block unwind-block-current-uwp-slot 0))
276 ;; If a match, return to context in arg block.
277 (inst jmp :e DO-EXIT)
279 ;; Not a match - return to *CURRENT-UNWIND-PROTECT-BLOCK* context.
280 ;; Important! Must save (and return) the arg 'block' for later use!!
283 ;; Set next unwind protect context.
284 (loadw uwp uwp unwind-block-current-uwp-slot)
285 ;; we're about to reload ebp anyway, so let's borrow it here as a
286 ;; temporary. Hope this works
287 (store-tl-symbol-value uwp *current-unwind-protect-block* ebp-tn)
291 (loadw ebp-tn block unwind-block-current-cont-slot)
293 ;; Uwp-entry expects some things in known locations so that they can
294 ;; be saved on the stack: the block in edx-tn, start in ebx-tn, and
297 (inst jmp (make-ea-for-object-slot block unwind-block-entry-pc-slot 0)))
300 ;;;; Win32 non-local exit noise
303 (define-assembly-routine (unwind
304 (:return-style :none)
305 (:policy :fast-safe))
306 ((:arg block (any-reg descriptor-reg) eax-offset)
307 (:arg start (any-reg descriptor-reg) ebx-offset)
308 (:arg count (any-reg descriptor-reg) ecx-offset))
309 (declare (ignore start count))
311 (let ((error (generate-error-code nil 'invalid-unwind-error)))
312 (inst test block block) ; check for NULL pointer
315 ;; Save all our registers, as we're about to clobber them.
318 ;; Find the SEH frame surrounding our target.
319 (loadw ecx-tn block unwind-block-next-seh-frame-slot)
321 ;; This section copied from VOP CALL-OUT.
322 ;; Setup the NPX for C; all the FP registers need to be
323 ;; empty; pop them all.
327 ;; I'm unlikely to ever forget this again.
330 ;; Set up a bogus stack frame for RtlUnwind to pick its return
331 ;; address from. (Yes, this is how RtlUnwind works.)
332 (inst push (make-fixup 'win32-unwind-tail :assembly-routine))
334 (inst mov ebp-tn esp-tn)
336 ;; Actually call out for the unwind.
341 (inst call (make-fixup "RtlUnwind" :foreign)))
343 ;; We want no VOP for this one and for it to only happen on Win32
344 ;; targets. Hence the following disaster.
345 #!+#.(cl:if (cl:member sb-assembling cl:*features*) win32 '(or))
346 (define-assembly-routine
347 (win32-unwind-tail (:return-style :none))
348 ((:temp block unsigned-reg eax-offset))
350 ;; The unwind returns here. Had to use a VOP for this because
351 ;; PUSH won't accept a label as an argument.
353 ;; Clean up the bogus stack frame we pushed for the unwind.
355 (inst pop esi-tn) ;; Random scratch register.
357 ;; This section based on VOP CALL-OUT.
358 ;; Restore the NPX for lisp; ensure no regs are empty
365 ;; By now we've unwound all the UWP frames required, so we
366 ;; just jump to our target block.
367 (loadw ebp-tn block unwind-block-current-cont-slot)
369 ;; Nlx-entry expects the arg start in ebx-tn and the arg count
370 ;; in ecx-tn. Fortunately, that's where they are already.
371 (inst jmp (make-ea-for-object-slot block unwind-block-entry-pc-slot 0)))
374 ;;;; Win32 UWP block SEH interface.
376 ;; We want no VOP for this one and for it to only happen on Win32
377 ;; targets. Hence the following disaster.
378 #!+#.(cl:if (cl:member sb-assembling cl:*features*) win32 '(or))
379 (define-assembly-routine
380 (uwp-seh-handler (:return-style :none))
381 ((:temp block unsigned-reg eax-offset))
383 ;; We get called for any exception which happens within our
384 ;; dynamic contour that isn't handled below us, and for
387 ;; For the exceptions we just return ExceptionContinueSearch.
389 ;; Find the exception record.
390 (inst mov eax-tn (make-ea :dword :base esp-tn :disp 4))
392 ;; Check unwind flags.
393 (inst test (make-ea :byte :base eax-tn :disp 4) 6) ; EH_UNWINDING | EH_EXIT_UNWIND
395 ;; To see if we're unwinding or not.
396 (inst jmp :nz UNWINDING)
398 ;; We're not unwinding, so we're not interested.
399 (inst mov eax-tn 1) ;; exception-continue-search
402 ;; For the unwinds we establish a basic environment as per
403 ;; call_into_lisp, but without the extra SEH frame (the theory
404 ;; being that we're already in a Lisp SEH context), and invoke
405 ;; our UWP block to unwind itself.
407 ;; FIXME: Do we need to establish an SEH frame anyway? And do
408 ;; we need to do the same stack frame hackery for the debugger
409 ;; as we do for the main exception handler?
411 ;; When the UWP block calls %continue-unwind, we come back to
412 ;; the next assembly routine, below, which reinitializes for C
413 ;; and returns to the Win32 unwind machinery.
415 ;; If the UWP block sees fit to do a non-local exit, things
416 ;; Just Work, thanks to the Win32 API being sanely designed
417 ;; and our complying with it.
419 ;; We also must update *current-unwind-protect-block* before
420 ;; calling the cleanup function.
424 ;; Save all registers (overkill)
427 ;; Establish our stack frame.
428 (inst mov ebp-tn esp-tn)
430 ;; This section based on VOP CALL-OUT.
431 ;; Restore the NPX for lisp; ensure no regs are empty
435 ;; Find our unwind-block by way of our SEH frame.
436 (inst mov block (make-ea :dword :base ebp-tn :disp #x28))
437 (inst lea block (make-ea :dword :base block
438 :disp (- (* unwind-block-next-seh-frame-slot
441 ;; Update *CURRENT-UNWIND-PROTECT-BLOCK*.
442 (loadw ebx-tn block unwind-block-current-uwp-slot)
443 (store-tl-symbol-value ebx-tn *current-unwind-protect-block* ecx-tn)
445 ;; Uwp-entry expects some things in known locations so that they can
446 ;; be saved on the stack: the block in edx-tn, start in ebx-tn, and
447 ;; count in ecx-tn. We don't actually have any of that here, but we
448 ;; do need to have access to our own stack frame, so we hijack the
449 ;; known locations to cover our own state.
451 (inst xor ebx-tn ebx-tn)
452 (inst xor ecx-tn ecx-tn)
453 (inst mov ebx-tn ebp-tn)
454 (loadw ebp-tn block unwind-block-current-cont-slot)
455 (inst jmp (make-ea-for-object-slot block unwind-block-entry-pc-slot 0)))
458 (define-assembly-routine (continue-unwind
459 (:return-style :none)
460 (:translate %continue-unwind)
461 (:policy :fast-safe))
462 ((:arg block (any-reg descriptor-reg) eax-offset)
463 (:arg start (any-reg descriptor-reg) ebx-offset)
464 (:arg count (any-reg descriptor-reg) ecx-offset))
465 (declare (ignore block count))
466 ;; The args here are mostly ignored because we're using the
467 ;; win32 unwind mechanism and keep all that elsewhere. The
468 ;; exception is START, which we use to pass the saved EBP for
469 ;; our exception handler.
471 ;; "All" we have to do here is reload our EBP, reestablish a C
472 ;; environment, and return ExceptionContinueSearch. The OS
475 ;; Restore our frame pointer.
476 (inst mov esp-tn start)
478 ;; This section copied from VOP CALL-OUT.
479 ;; Setup the NPX for C; all the FP registers need to be
480 ;; empty; pop them all.
484 ;; I'm unlikely to ever forget this again.
487 ;; Restore our saved registers
491 (inst mov eax-tn 1) ;; exception-continue-search