26f2f12b4475389cd97e9370560b3bea870ae7e7
[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
28 /* The following definitions allow using exceptions in Wine and Winelib code
29 *
30 * They should be used like this:
31 *
32 * __TRY
33 * {
34 * do some stuff that can raise an exception
35 * }
36 * __EXCEPT(filter_func,param)
37 * {
38 * handle the exception here
39 * }
40 * __ENDTRY
41 *
42 * or
43 *
44 * __TRY
45 * {
46 * do some stuff that can raise an exception
47 * }
48 * __FINALLY(finally_func,param)
49 *
50 * The filter_func must be defined with the WINE_EXCEPTION_FILTER
51 * macro, and return one of the EXCEPTION_* code; it can use
52 * GetExceptionInformation and GetExceptionCode to retrieve the
53 * exception info.
54 *
55 * The finally_func must be defined with the WINE_FINALLY_FUNC macro.
56 *
57 * Warning: inside a __TRY or __EXCEPT block, 'break' or 'continue' statements
58 * break out of the current block. You cannot use 'return', 'goto'
59 * or 'longjmp' to leave a __TRY block, as this will surely crash.
60 * You can use them to leave a __EXCEPT block though.
61 *
62 * -- AJ
63 */
64
65 typedef struct _EXCEPTION_REGISTRATION_RECORD
66 {
67 struct _EXCEPTION_REGISTRATION_RECORD *Prev;
68 PEXCEPTION_HANDLER Handler;
69 } EXCEPTION_REGISTRATION_RECORD, *PEXCEPTION_REGISTRATION_RECORD;
70
71 /* Define this if you want to use your compiler built-in __try/__except support.
72 * This is only useful when compiling to a native Windows binary, as the built-in
73 * compiler exceptions will most certainly not work under Winelib.
74 */
75 #ifdef USE_COMPILER_EXCEPTIONS
76
77 #define __TRY __try
78 #define __EXCEPT(func) __except((func)(GetExceptionInformation()))
79 #define __FINALLY(func) __finally { (func)(!AbnormalTermination()); }
80 #define __ENDTRY /*nothing*/
81
82 #else /* USE_COMPILER_EXCEPTIONS */
83
84 #ifndef __GNUC__
85 #define __attribute__(x) /* nothing */
86 #endif
87
88 #define __TRY \
89 do { __WINE_FRAME __f; \
90 int __first = 1; \
91 for (;;) if (!__first) \
92 { \
93 do {
94
95 #define __EXCEPT(func) \
96 } while(0); \
97 __wine_pop_frame( &__f.frame ); \
98 break; \
99 } else { \
100 __f.frame.Handler = __wine_exception_handler; \
101 __f.u.filter = (func); \
102 __wine_push_frame( &__f.frame ); \
103 if (setjmp( __f.jmp )) { \
104 const __WINE_FRAME * const __eptr __attribute__((unused)) = &__f; \
105 do {
106
107 #define __ENDTRY \
108 } while (0); \
109 break; \
110 } \
111 __first = 0; \
112 } \
113 } while (0);
114
115 #define __FINALLY(func) \
116 } while(0); \
117 __wine_pop_frame( &__f.frame ); \
118 (func)(1); \
119 break; \
120 } else { \
121 __f.frame.Handler = __wine_finally_handler; \
122 __f.u.finally_func = (func); \
123 __wine_push_frame( &__f.frame ); \
124 __first = 0; \
125 } \
126 } while (0);
127
128
129 typedef DWORD (CALLBACK *__WINE_FILTER)(PEXCEPTION_POINTERS);
130 typedef void (CALLBACK *__WINE_FINALLY)(BOOL);
131
132 #define WINE_EXCEPTION_FILTER(func) DWORD CALLBACK func( PEXCEPTION_POINTERS __eptr )
133 #define WINE_FINALLY_FUNC(func) void CALLBACK func( BOOL __normal )
134
135 #define GetExceptionInformation() (__eptr)
136 #define GetExceptionCode() (__eptr->ExceptionRecord->ExceptionCode)
137
138 #undef AbnormalTermination
139 #define AbnormalTermination() (!__normal)
140
141 typedef struct __tagWINE_FRAME
142 {
143 EXCEPTION_REGISTRATION_RECORD frame;
144 union
145 {
146 /* exception data */
147 __WINE_FILTER filter;
148 /* finally data */
149 __WINE_FINALLY finally_func;
150 } u;
151 jmp_buf jmp;
152 /* hack to make GetExceptionCode() work in handler */
153 DWORD ExceptionCode;
154 const struct __tagWINE_FRAME *ExceptionRecord;
155 } __WINE_FRAME;
156
157 #endif /* USE_COMPILER_EXCEPTIONS */
158
159 static inline EXCEPTION_REGISTRATION_RECORD *__wine_push_frame( EXCEPTION_REGISTRATION_RECORD *frame )
160 {
161 #if defined(__GNUC__) && defined(__i386__)
162 EXCEPTION_REGISTRATION_RECORD *prev;
163 __asm__ __volatile__(".byte 0x64\n\tmovl (0),%0"
164 "\n\tmovl %0,(%1)"
165 "\n\t.byte 0x64\n\tmovl %1,(0)"
166 : "=&r" (prev) : "r" (frame) : "memory" );
167 return prev;
168 #else
169 NT_TIB *teb = (NT_TIB *)NtCurrentTeb();
170 frame->Prev = (void *)teb->ExceptionList;
171 teb->ExceptionList = (void *)frame;
172 return frame->Prev;
173 #endif
174 }
175
176 static inline EXCEPTION_REGISTRATION_RECORD *__wine_pop_frame( EXCEPTION_REGISTRATION_RECORD *frame )
177 {
178 #if defined(__GNUC__) && defined(__i386__)
179 __asm__ __volatile__(".byte 0x64\n\tmovl %0,(0)"
180 : : "r" (frame->Prev) : "memory" );
181 return frame->Prev;
182
183 #else
184 NT_TIB *teb = (NT_TIB *)NtCurrentTeb();
185 teb->ExceptionList = (void *)frame->Prev;
186 return frame->Prev;
187 #endif
188 }
189
190 #ifndef USE_COMPILER_EXCEPTIONS
191
192 extern VOID NTAPI RtlUnwind(PVOID,PVOID,PEXCEPTION_RECORD,PVOID);
193
194 static __inline EXCEPTION_DISPOSITION
195 __wine_exception_handler( struct _EXCEPTION_RECORD *record, void *frame,
196 struct _CONTEXT *context, void *pdispatcher )
197 {
198 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
199
200 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))
201 return ExceptionContinueSearch;
202 if (wine_frame->u.filter)
203 {
204 EXCEPTION_POINTERS ptrs;
205 ptrs.ExceptionRecord = record;
206 ptrs.ContextRecord = context;
207 switch(wine_frame->u.filter( &ptrs ))
208 {
209 case EXCEPTION_CONTINUE_SEARCH:
210 return ExceptionContinueSearch;
211 case EXCEPTION_CONTINUE_EXECUTION:
212 return ExceptionContinueExecution;
213 case EXCEPTION_EXECUTE_HANDLER:
214 break;
215 default:
216 break;
217 }
218 }
219 /* hack to make GetExceptionCode() work in handler */
220 wine_frame->ExceptionCode = record->ExceptionCode;
221 wine_frame->ExceptionRecord = wine_frame;
222
223 RtlUnwind( frame, 0, record, 0 );
224 __wine_pop_frame( frame );
225 longjmp( wine_frame->jmp, 1 );
226 }
227
228
229 static __inline EXCEPTION_DISPOSITION
230 __wine_finally_handler( struct _EXCEPTION_RECORD *record, void *frame,
231 struct _CONTEXT *context, void *pdispatcher )
232 {
233 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
234 {
235 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
236 wine_frame->u.finally_func( FALSE );
237 }
238 return ExceptionContinueSearch;
239 }
240
241 #endif /* USE_COMPILER_EXCEPTIONS */
242
243
244 /* Wine-specific exceptions codes */
245
246 #define EXCEPTION_WINE_STUB 0x80000100 /* stub entry point called */
247 #define EXCEPTION_WINE_ASSERTION 0x80000101 /* assertion failed */
248
249 /* unhandled return status from vm86 mode */
250 #define EXCEPTION_VM86_INTx 0x80000110
251 #define EXCEPTION_VM86_STI 0x80000111
252 #define EXCEPTION_VM86_PICRETURN 0x80000112
253
254 extern void __wine_enter_vm86( CONTEXT *context );
255
256 #endif /* __WINE_WINE_EXCEPTION_H */