83081145690d52d4ac097ee4a1feceeeb963a8eb
[reactos.git] / reactos / include / wine / exception.h
1 /*
2 * Wine exception handling
3 *
4 * Copyright (c) 1999 Alexandre Julliard
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #ifndef __WINE_WINE_EXCEPTION_H
22 #define __WINE_WINE_EXCEPTION_H
23
24 #include <setjmp.h>
25 #include <windef.h>
26 #include <excpt.h>
27 #include <wine/port.h>
28
29 /* The following definitions allow using exceptions in Wine and Winelib code
30 *
31 * They should be used like this:
32 *
33 * __TRY
34 * {
35 * do some stuff that can raise an exception
36 * }
37 * __EXCEPT(filter_func,param)
38 * {
39 * handle the exception here
40 * }
41 * __ENDTRY
42 *
43 * or
44 *
45 * __TRY
46 * {
47 * do some stuff that can raise an exception
48 * }
49 * __FINALLY(finally_func,param)
50 *
51 * The filter_func must be defined with the WINE_EXCEPTION_FILTER
52 * macro, and return one of the EXCEPTION_* code; it can use
53 * GetExceptionInformation and GetExceptionCode to retrieve the
54 * exception info.
55 *
56 * The finally_func must be defined with the WINE_FINALLY_FUNC macro.
57 *
58 * Warning: inside a __TRY or __EXCEPT block, 'break' or 'continue' statements
59 * break out of the current block. You cannot use 'return', 'goto'
60 * or 'longjmp' to leave a __TRY block, as this will surely crash.
61 * You can use them to leave a __EXCEPT block though.
62 *
63 * -- AJ
64 */
65
66 typedef struct _EXCEPTION_REGISTRATION_RECORD
67 {
68 struct _EXCEPTION_REGISTRATION_RECORD *prev;
69 PEXCEPTION_HANDLER handler;
70 } EXCEPTION_REGISTRATION_RECORD, *PEXCEPTION_REGISTRATION_RECORD;
71
72 /* Define this if you want to use your compiler built-in __try/__except support.
73 * This is only useful when compiling to a native Windows binary, as the built-in
74 * compiler exceptions will most certainly not work under Winelib.
75 */
76 #ifdef USE_COMPILER_EXCEPTIONS
77
78 #define __TRY __try
79 #define __EXCEPT(func) __except((func)(GetExceptionInformation()))
80 #define __FINALLY(func) __finally { (func)(!AbnormalTermination()); }
81 #define __ENDTRY /*nothing*/
82
83 #else /* USE_COMPILER_EXCEPTIONS */
84
85 #ifndef __GNUC__
86 #define __attribute__(x) /* nothing */
87 #endif
88
89 #define __TRY \
90 do { __WINE_FRAME __f; \
91 int __first = 1; \
92 for (;;) if (!__first) \
93 { \
94 do {
95
96 #define __EXCEPT(func) \
97 } while(0); \
98 __wine_pop_frame( &__f.frame ); \
99 break; \
100 } else { \
101 __f.frame.Handler = __wine_exception_handler; \
102 __f.u.filter = (func); \
103 __wine_push_frame( &__f.frame ); \
104 if (sigsetjmp( __f.jmp, 1 )) { \
105 const __WINE_FRAME * const __eptr __attribute__((unused)) = &__f; \
106 do {
107
108 #define __ENDTRY \
109 } while (0); \
110 break; \
111 } \
112 __first = 0; \
113 } \
114 } while (0);
115
116 #define __FINALLY(func) \
117 } while(0); \
118 __wine_pop_frame( &__f.frame ); \
119 (func)(1); \
120 break; \
121 } else { \
122 __f.frame.Handler = __wine_finally_handler; \
123 __f.u.finally_func = (func); \
124 __wine_push_frame( &__f.frame ); \
125 __first = 0; \
126 } \
127 } while (0);
128
129
130 typedef DWORD (CALLBACK *__WINE_FILTER)(PEXCEPTION_POINTERS);
131 typedef void (CALLBACK *__WINE_FINALLY)(BOOL);
132
133 #define WINE_EXCEPTION_FILTER(func) DWORD WINAPI func( EXCEPTION_POINTERS *__eptr )
134 #define WINE_FINALLY_FUNC(func) void WINAPI func( BOOL __normal )
135
136 #define GetExceptionInformation() (__eptr)
137 #define GetExceptionCode() (__eptr->ExceptionRecord->ExceptionCode)
138
139 #undef AbnormalTermination
140 #define AbnormalTermination() (!__normal)
141
142 typedef struct __tagWINE_FRAME
143 {
144 EXCEPTION_REGISTRATION_RECORD frame;
145 union
146 {
147 /* exception data */
148 __WINE_FILTER filter;
149 /* finally data */
150 __WINE_FINALLY finally_func;
151 } u;
152 sigjmp_buf jmp;
153 /* hack to make GetExceptionCode() work in handler */
154 DWORD ExceptionCode;
155 const struct __tagWINE_FRAME *ExceptionRecord;
156 } __WINE_FRAME;
157
158 extern DWORD __wine_exception_handler( PEXCEPTION_RECORD record, EXCEPTION_REGISTRATION_RECORD *frame,
159 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **pdispatcher );
160 extern DWORD __wine_finally_handler( PEXCEPTION_RECORD record, EXCEPTION_REGISTRATION_RECORD *frame,
161 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **pdispatcher );
162
163 #endif /* USE_COMPILER_EXCEPTIONS */
164
165 static inline EXCEPTION_REGISTRATION_RECORD *__wine_push_frame( EXCEPTION_REGISTRATION_RECORD *frame )
166 {
167 #if defined(__GNUC__) && defined(__i386__)
168 EXCEPTION_REGISTRATION_RECORD *prev;
169 __asm__ __volatile__(".byte 0x64\n\tmovl (0),%0"
170 "\n\tmovl %0,(%1)"
171 "\n\t.byte 0x64\n\tmovl %1,(0)"
172 : "=&r" (prev) : "r" (frame) : "memory" );
173 return prev;
174 #else
175 NT_TIB *teb = (NT_TIB *)NtCurrentTeb();
176 frame->Prev = (void *)teb->ExceptionList;
177 teb->ExceptionList = (void *)frame;
178 return frame->Prev;
179 #endif
180 }
181
182 static inline EXCEPTION_REGISTRATION_RECORD *__wine_pop_frame( EXCEPTION_REGISTRATION_RECORD *frame )
183 {
184 #if defined(__GNUC__) && defined(__i386__)
185 __asm__ __volatile__(".byte 0x64\n\tmovl %0,(0)"
186 : : "r" (frame->prev) : "memory" );
187 return frame->prev;
188
189 #else
190 NT_TIB *teb = (NT_TIB *)NtCurrentTeb();
191 teb->ExceptionList = (void *)frame->Prev;
192 return frame->Prev;
193 #endif
194 }
195
196
197 /* Wine-specific exceptions codes */
198
199 #define EXCEPTION_WINE_STUB 0x80000100 /* stub entry point called */
200 #define EXCEPTION_WINE_ASSERTION 0x80000101 /* assertion failed */
201
202 /* unhandled return status from vm86 mode */
203 #define EXCEPTION_VM86_INTx 0x80000110
204 #define EXCEPTION_VM86_STI 0x80000111
205 #define EXCEPTION_VM86_PICRETURN 0x80000112
206
207 extern void __wine_enter_vm86( CONTEXT *context );
208
209 #endif /* __WINE_WINE_EXCEPTION_H */