Removed a few more #ifdef __WINE__.
[wine] / 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
27 /* The following definitions allow using exceptions in Wine and Winelib code
28  *
29  * They should be used like this:
30  *
31  * __TRY
32  * {
33  *     do some stuff that can raise an exception
34  * }
35  * __EXCEPT(filter_func,param)
36  * {
37  *     handle the exception here
38  * }
39  * __ENDTRY
40  *
41  * or
42  *
43  * __TRY
44  * {
45  *     do some stuff that can raise an exception
46  * }
47  * __FINALLY(finally_func,param)
48  *
49  * The filter_func must be defined with the WINE_EXCEPTION_FILTER
50  * macro, and return one of the EXCEPTION_* code; it can use
51  * GetExceptionInformation and GetExceptionCode to retrieve the
52  * exception info.
53  *
54  * The finally_func must be defined with the WINE_FINALLY_FUNC macro.
55  *
56  * Warning: inside a __TRY or __EXCEPT block, 'break' or 'continue' statements
57  *          break out of the current block. You cannot use 'return', 'goto'
58  *          or 'longjmp' to leave a __TRY block, as this will surely crash.
59  *          You can use them to leave a __EXCEPT block though.
60  *
61  * -- AJ
62  */
63
64 /* Define this if you want to use your compiler built-in __try/__except support.
65  * This is only useful when compiling to a native Windows binary, as the built-in
66  * compiler exceptions will most certainly not work under Winelib.
67  */
68 #ifdef USE_COMPILER_EXCEPTIONS
69
70 #define __TRY __try
71 #define __EXCEPT(func) __except((func)(GetExceptionInformation()))
72 #define __FINALLY(func) __finally { (func)(!AbnormalTermination()); }
73 #define __ENDTRY /*nothing*/
74
75 #else  /* USE_COMPILER_EXCEPTIONS */
76
77 #define __TRY \
78     do { __WINE_FRAME __f; \
79          int __first = 1; \
80          for (;;) if (!__first) \
81          { \
82              do {
83
84 #define __EXCEPT(func) \
85              } while(0); \
86              __wine_pop_frame( &__f.frame ); \
87              break; \
88          } else { \
89              __f.frame.Handler = (PEXCEPTION_HANDLER)__wine_exception_handler; \
90              __f.u.filter = (func); \
91              __wine_push_frame( &__f.frame ); \
92              if (setjmp( __f.jmp)) { \
93                  const __WINE_FRAME * const __eptr WINE_UNUSED = &__f; \
94                  do {
95
96 #define __ENDTRY \
97                  } while (0); \
98                  break; \
99              } \
100              __first = 0; \
101          } \
102     } while (0);
103
104 #define __FINALLY(func) \
105              } while(0); \
106              __wine_pop_frame( &__f.frame ); \
107              (func)(1); \
108              break; \
109          } else { \
110              __f.frame.Handler = (PEXCEPTION_HANDLER)__wine_finally_handler; \
111              __f.u.finally_func = (func); \
112              __wine_push_frame( &__f.frame ); \
113              __first = 0; \
114          } \
115     } while (0);
116
117
118 typedef DWORD (CALLBACK *__WINE_FILTER)(PEXCEPTION_POINTERS);
119 typedef void (CALLBACK *__WINE_FINALLY)(BOOL);
120
121 #define WINE_EXCEPTION_FILTER(func) DWORD WINAPI func( EXCEPTION_POINTERS *__eptr )
122 #define WINE_FINALLY_FUNC(func) void WINAPI func( BOOL __normal )
123
124 #define GetExceptionInformation() (__eptr)
125 #define GetExceptionCode()        (__eptr->ExceptionRecord->ExceptionCode)
126 #define AbnormalTermination()     (!__normal)
127
128 typedef struct __tagWINE_FRAME
129 {
130     EXCEPTION_FRAME frame;
131     union
132     {
133         /* exception data */
134         __WINE_FILTER filter;
135         /* finally data */
136         __WINE_FINALLY finally_func;
137     } u;
138     jmp_buf jmp;
139     /* hack to make GetExceptionCode() work in handler */
140     DWORD ExceptionCode;
141     const struct __tagWINE_FRAME *ExceptionRecord;
142 } __WINE_FRAME;
143
144 extern DWORD __wine_exception_handler( PEXCEPTION_RECORD record, EXCEPTION_FRAME *frame,
145                                        CONTEXT *context, LPVOID pdispatcher );
146 extern DWORD __wine_finally_handler( PEXCEPTION_RECORD record, EXCEPTION_FRAME *frame,
147                                      CONTEXT *context, LPVOID pdispatcher );
148
149 #endif /* USE_COMPILER_EXCEPTIONS */
150
151 static inline EXCEPTION_FRAME * WINE_UNUSED __wine_push_frame( EXCEPTION_FRAME *frame )
152 {
153 #if defined(__GNUC__) && defined(__i386__)
154     EXCEPTION_FRAME *prev;
155     __asm__ __volatile__(".byte 0x64\n\tmovl (0),%0"
156                          "\n\tmovl %0,(%1)"
157                          "\n\t.byte 0x64\n\tmovl %1,(0)"
158                          : "=&r" (prev) : "r" (frame) : "memory" );
159     return prev;
160 #else
161     NT_TIB *teb = (NT_TIB *)NtCurrentTeb();
162     frame->Prev = (void *)teb->ExceptionList;
163     teb->ExceptionList = (void *)frame;
164     return frame->Prev;
165 #endif
166 }
167
168 static inline EXCEPTION_FRAME * WINE_UNUSED __wine_pop_frame( EXCEPTION_FRAME *frame )
169 {
170 #if defined(__GNUC__) && defined(__i386__)
171     __asm__ __volatile__(".byte 0x64\n\tmovl %0,(0)"
172                          : : "r" (frame->Prev) : "memory" );
173     return frame->Prev;
174
175 #else
176     NT_TIB *teb = (NT_TIB *)NtCurrentTeb();
177     teb->ExceptionList = (void *)frame->Prev;
178     return frame->Prev;
179 #endif
180 }
181
182
183 /* Wine-specific exceptions codes */
184
185 #define EXCEPTION_WINE_STUB       0x80000100  /* stub entry point called */
186 #define EXCEPTION_WINE_ASSERTION  0x80000101  /* assertion failed */
187
188 /* unhandled return status from vm86 mode */
189 #define EXCEPTION_VM86_INTx       0x80000110
190 #define EXCEPTION_VM86_STI        0x80000111
191 #define EXCEPTION_VM86_PICRETURN  0x80000112
192
193 extern void __wine_enter_vm86( CONTEXT *context );
194
195 #endif  /* __WINE_WINE_EXCEPTION_H */