OSDN Git Service

2012-10-15 Earnie Boyd <earnie@users.sourceforge.net>
[mingw/mingw-org-wsl.git] / include / excpt.h
1 /**
2  * @file excpt.h
3  * @copy 2012 MinGW.org project
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  * 
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  * 
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
24 #ifndef _EXCPT_H_
25 #define _EXCPT_H_
26 #pragma GCC system_header
27 #include <_mingw.h>
28
29 __PSHPACK8
30
31 #ifdef  __cplusplus
32 extern "C" {
33 #endif
34
35
36 typedef enum _EXCEPTION_DISPOSITION {
37         ExceptionContinueExecution,
38         ExceptionContinueSearch,
39         ExceptionNestedException,
40         ExceptionCollidedUnwind
41 } EXCEPTION_DISPOSITION;
42
43 /*
44  * The type of function that is expected as an exception handler to be
45  * installed with __try1.
46  */
47 typedef EXCEPTION_DISPOSITION (*PEXCEPTION_HANDLER)
48                 (struct _EXCEPTION_RECORD*, void*, struct _CONTEXT*, void*);
49
50 /*
51  * This is not entirely necessary, but it is the structure installed by
52  * the __try1 primitive below.
53  */
54 typedef struct _EXCEPTION_REGISTRATION
55 {
56         struct _EXCEPTION_REGISTRATION* prev;
57         PEXCEPTION_HANDLER              handler;
58 } EXCEPTION_REGISTRATION, *PEXCEPTION_REGISTRATION;
59
60 typedef EXCEPTION_REGISTRATION EXCEPTION_REGISTRATION_RECORD;
61 typedef PEXCEPTION_REGISTRATION PEXCEPTION_REGISTRATION_RECORD;
62
63 /*
64  * A macro which installs the supplied exception handler.
65  * Push the pointer to the new handler onto the stack,
66  * then push the pointer to the old registration structure (at fs:0)
67  * onto the stack, then put a pointer to the new registration
68  * structure (i.e. the current stack pointer) at fs:0.
69  */
70 #ifdef _WIN64
71 # define __try1(pHandler) \
72         __asm__ __volatile__ ("pushq %0;pushq %%gs:0;movq %%rsp,%%gs:0;" : : \
73         "g" (pHandler));
74 #else
75 # define __try1(pHandler) \
76         __asm__ __volatile__ ("pushl %0;pushl %%fs:0;movl %%esp,%%fs:0;" : : \
77         "g" (pHandler));
78 #endif
79
80 /*
81  * A macro which (despite its name) *removes* an installed
82  * exception handler. Should be used only in conjunction with the above
83  * install routine __try1.
84  * Move the pointer to the old reg. struct (at the current stack
85  * position) to fs:0, replacing the pointer we installed above,
86  * then add 8 to the stack pointer to get rid of the space we
87  * used when we pushed on our new reg. struct above. Notice that
88  * the stack must be in the exact state at this point that it was
89  * after we did __try1 or this will smash things.
90  */
91 #ifdef _WIN64
92 # define        __except1       \
93         __asm__ __volatile__ ("movq (%%rsp),%%rax;movq %%rax,%%gs:0;addq \
94         $16,%%rsp;" : : : "%rax");
95 #else
96 # define __except1      \
97         __asm__ __volatile__ ("movl (%%esp),%%eax;movl %%eax,%%fs:0;addl \
98         $8,%%esp;" : : : "%eax");
99 #endif
100
101 #ifdef  __cplusplus
102 }
103 #endif
104
105 __POPPACK8
106 #endif  /* _EXCPT_H_ not defined */