+
+;;;; Win32 UWP block SEH interface.
+
+;; We want no VOP for this one and for it to only happen on Win32
+;; targets. Hence the following disaster.
+#!+#.(cl:if (cl:member sb-assembling cl:*features*) win32 '(or))
+(define-assembly-routine
+ (uwp-seh-handler (:return-style :none))
+ ((:temp block unsigned-reg eax-offset))
+
+ ;; We get called for any exception which happens within our
+ ;; dynamic contour that isn't handled below us, and for
+ ;; unwinding.
+
+ ;; For the exceptions we just return ExceptionContinueSearch.
+
+ ;; Find the exception record.
+ (inst mov eax-tn (make-ea :dword :base esp-tn :disp 4))
+
+ ;; Check unwind flags.
+ (inst test (make-ea :byte :base eax-tn :disp 4) 6) ; EH_UNWINDING | EH_EXIT_UNWIND
+
+ ;; To see if we're unwinding or not.
+ (inst jmp :nz UNWINDING)
+
+ ;; We're not unwinding, so we're not interested.
+ (inst mov eax-tn 1) ;; exception-continue-search
+ (inst ret)
+
+ ;; For the unwinds we establish a basic environment as per
+ ;; call_into_lisp, but without the extra SEH frame (the theory
+ ;; being that we're already in a Lisp SEH context), and invoke
+ ;; our UWP block to unwind itself.
+
+ ;; FIXME: Do we need to establish an SEH frame anyway? And do
+ ;; we need to do the same stack frame hackery for the debugger
+ ;; as we do for the main exception handler?
+
+ ;; When the UWP block calls %continue-unwind, we come back to
+ ;; the next assembly routine, below, which reinitializes for C
+ ;; and returns to the Win32 unwind machinery.
+
+ ;; If the UWP block sees fit to do a non-local exit, things
+ ;; Just Work, thanks to the Win32 API being sanely designed
+ ;; and our complying with it.
+
+ ;; We also must update *current-unwind-protect-block* before
+ ;; calling the cleanup function.
+
+ UNWINDING
+
+ ;; Save all registers (overkill)
+ (inst pusha)
+
+ ;; Establish our stack frame.
+ (inst mov ebp-tn esp-tn)
+
+ ;; This section based on VOP CALL-OUT.
+ ;; Restore the NPX for lisp; ensure no regs are empty
+ (dotimes (i 8)
+ (inst fldz))
+
+ ;; Find our unwind-block by way of our SEH frame.
+ (inst mov block (make-ea :dword :base ebp-tn :disp #x28))
+ (inst lea block (make-ea :dword :base block
+ :disp (- (* unwind-block-next-seh-frame-slot
+ n-word-bytes))))
+
+ ;; Update *CURRENT-UNWIND-PROTECT-BLOCK*.
+ (loadw ebx-tn block unwind-block-current-uwp-slot)
+ (store-tl-symbol-value ebx-tn *current-unwind-protect-block* ecx-tn)
+
+ ;; Uwp-entry expects some things in known locations so that they can
+ ;; be saved on the stack: the block in edx-tn, start in ebx-tn, and
+ ;; count in ecx-tn. We don't actually have any of that here, but we
+ ;; do need to have access to our own stack frame, so we hijack the
+ ;; known locations to cover our own state.
+
+ (inst xor ebx-tn ebx-tn)
+ (inst xor ecx-tn ecx-tn)
+ (inst mov ebx-tn ebp-tn)
+ (loadw ebp-tn block unwind-block-current-cont-slot)