2 Copyright (c) 2008 KJK::Hyperion
4 Permission is hereby granted, free of charge, to any person obtaining a
5 copy of this software and associated documentation files (the "Software"),
6 to deal in the Software without restriction, including without limitation
7 the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 and/or sell copies of the Software, and to permit persons to whom the
9 Software is furnished to do so, subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 DEALINGS IN THE SOFTWARE.
29 struct _EXCEPTION_RECORD
;
30 struct _EXCEPTION_POINTERS
;
33 typedef int (__cdecl
* _SEH2FrameHandler_t
)
35 struct _EXCEPTION_RECORD
*,
41 typedef struct __SEH2Registration
43 struct __SEH2Registration
* SER_Prev
;
44 _SEH2FrameHandler_t SER_Handler
;
48 typedef struct __SEH2Frame
50 _SEH2Registration_t SF_Registration
;
51 volatile struct __SEH2TryLevel
* volatile SF_TopTryLevel
;
52 struct _EXCEPTION_POINTERS
* volatile SF_ExceptionInformation
;
53 volatile unsigned long SF_Code
;
57 typedef struct __SEH2TryLevel
59 volatile struct __SEH2TryLevel
* ST_Next
;
62 void * volatile ST_Ebp
;
63 void * volatile ST_Esp
;
72 extern void __cdecl
_SEH2EnterFrame(_SEH2Frame_t
*);
73 extern void __cdecl
_SEH2LeaveFrame(void);
74 extern void __cdecl
_SEH2Return(void);
83 typedef struct __SEHTrampoline
85 unsigned char STR_MovEcx
;
86 unsigned char STR_Closure
[4];
87 unsigned char STR_Jmp
;
88 unsigned char STR_Function
[4];
94 __attribute__((always_inline
))
95 int _SEHIsTrampoline(_SEHTrampoline_t
* trampoline_
)
97 return trampoline_
->STR_MovEcx
== 0xb9 && trampoline_
->STR_Jmp
== 0xe9;
102 __attribute__((always_inline
))
103 void * _SEHFunctionFromTrampoline(_SEHTrampoline_t
* trampoline_
)
105 return (void *)(*(int *)(&trampoline_
->STR_Function
[0]) + (int)(trampoline_
+ 1));
110 __attribute__((always_inline
))
111 void * _SEHClosureFromTrampoline(_SEHTrampoline_t
* trampoline_
)
113 return (void *)*(int *)(&trampoline_
->STR_Closure
[0]);
119 /* A no-op side effect that scares GCC */
120 #define __SEH_SIDE_EFFECT __asm__ __volatile__("#")
122 /* A no-op without any real side effects, but silences warnings */
123 #define __SEH_PRETEND_SIDE_EFFECT (void)0
125 /* Forces GCC to consider the specified label reachable */
126 #define __SEH_USE_LABEL(L_) if(__SEH_VOLATILE_FALSE) goto L_;
128 /* Makes GCC pretend the specified label is reachable, to silence warnings */
129 #define __SEH_PRETEND_USE_LABEL(L_) (void)(&&L_)
131 /* Soft memory barrier */
132 #define __SEH_BARRIER __asm__ __volatile__("#":::"memory")
134 /* GCC doesn't know that this equals zero */
135 #define __SEH_VOLATILE_ZERO ({ int zero = 0; __asm__ __volatile__("#" : "+g" (zero)); zero; })
137 /* GCC believes this is setjmp */
138 #define __SEH_PRETEND_SETJMP() (_SEH2PretendSetjmp(), 0)
140 #define __SEH_VOLATILE_FALSE __builtin_expect(__SEH_VOLATILE_ZERO, 0)
141 #define __SEH_VOLATILE_TRUE __builtin_expect(!__SEH_VOLATILE_ZERO, 1)
143 #define ___SEH_STRINGIFY(X_) # X_
144 #define __SEH_STRINGIFY(X_) ___SEH_STRINGIFY(X_)
148 __attribute__((returns_twice
))
149 __attribute__((always_inline
))
150 void _SEH2PretendSetjmp(void)
154 #define __SEH_FORCE_NEST \
155 __asm__ __volatile__("#%0" : : "r" (&_SEHFrame))
157 #define __SEH_DECLARE_EXCEPT_PFN(NAME_) int (__cdecl * NAME_)(void)
158 #define __SEH_DECLARE_EXCEPT(NAME_) int __cdecl NAME_(void)
159 #define __SEH_DEFINE_EXCEPT(NAME_) int __cdecl NAME_(void)
161 #define __SEH_DECLARE_FINALLY_PFN(NAME_) void (__cdecl * NAME_)(void)
162 #define __SEH_DECLARE_FINALLY(NAME_) void __cdecl NAME_(void)
163 #define __SEH_DEFINE_FINALLY(NAME_) void __cdecl NAME_(void)
165 #define __SEH_RETURN_EXCEPT(R_) return (int)(R_)
166 #define __SEH_RETURN_FINALLY() return
168 #define __SEH_BEGIN_TRY \
169 if(!__SEH_PRETEND_SETJMP()) \
171 __label__ _SEHEndTry; \
173 __SEH_PRETEND_USE_LABEL(_SEHEndTry); \
178 #define __SEH_END_TRY \
184 #define __SEH_SET_TRYLEVEL(TRYLEVEL_) \
186 __SEH_BARRIER; _SEH2FrameP->SF_TopTryLevel = (TRYLEVEL_); __SEH_BARRIER; \
189 #define __SEH_ENTER_TRYLEVEL() __SEH_SET_TRYLEVEL(&_SEHTryLevel)
190 #define __SEH_LEAVE_TRYLEVEL() __SEH_SET_TRYLEVEL(_SEHPrevTryLevelP)
192 #define __SEH_END_SCOPE_CHAIN \
193 static const int _SEH2ScopeKind = 1; \
194 static _SEH2Frame_t * const _SEH2FrameP = 0; \
195 static _SEH2TryLevel_t * const _SEH2TryLevelP = 0;
197 #define __SEH_BEGIN_SCOPE \
200 const int _SEHTopTryLevel = (_SEH2ScopeKind != 0); \
201 _SEH2Frame_t * const _SEHCurFrameP = _SEH2FrameP; \
202 volatile _SEH2TryLevel_t * const _SEHPrevTryLevelP = _SEH2TryLevelP; \
203 __attribute__((unused)) int _SEHAbnormalTermination; \
205 (void)_SEHTopTryLevel; \
206 (void)_SEHCurFrameP; \
207 (void)_SEHPrevTryLevelP; \
210 __label__ _SEHBeforeTry; \
211 __label__ _SEHDoTry; \
212 __label__ _SEHAfterTry; \
213 static const int _SEH2ScopeKind = 0; \
214 _SEH2Frame_t _SEHFrame; \
215 volatile _SEH2TryLevel_t _SEHTryLevel; \
216 _SEH2Frame_t * const _SEH2FrameP = _SEHTopTryLevel ? &_SEHFrame : _SEHCurFrameP; \
217 volatile _SEH2TryLevel_t * const _SEH2TryLevelP = &_SEHTryLevel; \
219 (void)_SEH2ScopeKind; \
221 (void)_SEHTryLevel; \
223 (void)_SEH2TryLevelP; \
225 _SEHTryLevel.ST_Next = _SEHPrevTryLevelP; \
226 goto _SEHBeforeTry; \
229 __SEH_ENTER_TRYLEVEL(); \
231 if(_SEHTopTryLevel) \
232 _SEH2EnterFrame(&_SEHFrame); \
234 #define __SEH_END_SCOPE \
240 #define __SEH_SCOPE_LOCALS \
241 __label__ _SEHBeginExcept; \
242 __label__ _SEHEndExcept; \
244 auto __SEH_DECLARE_FINALLY(_SEHFinally);
249 __SEH_SCOPE_LOCALS; \
254 #define _SEH2_FINALLY \
261 __SEH_PRETEND_USE_LABEL(_SEHBeginExcept); \
262 __SEH_PRETEND_USE_LABEL(_SEHEndExcept); \
264 _SEHTryLevel.ST_Filter = 0; \
265 _SEHTryLevel.ST_Body = &_SEHFinally; \
267 _SEHAbnormalTermination = 1; \
272 _SEHAbnormalTermination = 0; \
274 if(_SEHTopTryLevel) \
278 __SEH_LEAVE_TRYLEVEL(); \
282 goto _SEHEndExcept; \
286 __attribute__((noinline)) __SEH_DEFINE_FINALLY(_SEHFinally) \
288 __SEH_END_SCOPE_CHAIN; \
290 (void)_SEH2ScopeKind; \
292 (void)_SEH2TryLevelP; \
294 for(;; ({ __SEH_RETURN_FINALLY(); })) \
297 #define _SEH2_EXCEPT(...) \
305 if(__builtin_constant_p((__VA_ARGS__))) \
307 if((__VA_ARGS__) > 0) \
309 _SEHTryLevel.ST_Filter = (void *)1; \
310 _SEHTryLevel.ST_Body = &&_SEHBeginExcept; \
311 __SEH_USE_LABEL(_SEHBeginExcept); \
313 else if((__VA_ARGS__) < 0) \
315 _SEHTryLevel.ST_Filter = (void *)-1; \
316 _SEHTryLevel.ST_Body = NULL; \
320 _SEHTryLevel.ST_Filter = (void *)0; \
321 _SEHTryLevel.ST_Body = NULL; \
326 __SEH_DEFINE_EXCEPT(_SEHExcept) \
328 __SEH_RETURN_EXCEPT((__VA_ARGS__)); \
331 _SEHTryLevel.ST_Filter = &_SEHExcept; \
332 _SEHTryLevel.ST_Body = &&_SEHBeginExcept; \
333 __SEH_USE_LABEL(_SEHBeginExcept); \
338 __asm__ __volatile__ \
342 "=m" (_SEHTryLevel.ST_Ebp), \
343 "=m" (_SEHTryLevel.ST_Esp) \
350 __attribute__((unused)) __SEH_DEFINE_FINALLY(_SEHFinally) { __SEH_RETURN_FINALLY(); } \
353 if(_SEHTopTryLevel) \
357 __SEH_LEAVE_TRYLEVEL(); \
360 goto _SEHEndExcept; \
365 _SEH2Frame_t * const _SEH2FrameP = _SEHTopTryLevel ? &_SEHFrame : _SEHCurFrameP; \
378 #define _SEH2_GetExceptionInformation() ((_SEH2FrameP)->SF_ExceptionInformation)
379 #define _SEH2_GetExceptionCode() ((_SEH2FrameP)->SF_Code)
380 #define _SEH2_AbnormalTermination() (_SEHAbnormalTermination)
382 #define _SEH2_YIELD(STMT_) \
385 if(!_SEH2ScopeKind) \
391 #define _SEH2_LEAVE goto _SEHEndTry
393 __SEH_END_SCOPE_CHAIN
;
399 #define _SEH2_TRY __try
400 #define _SEH2_FINALLY __finally
401 #define _SEH2_EXCEPT(...) __except(__VA_ARGS__)
404 #define _SEH2_GetExceptionInformation() (GetExceptionInformation())
405 #define _SEH2_GetExceptionCode() (GetExceptionCode())
406 #define _SEH2_AbnormalTermination() (AbnormalTermination())
408 #define _SEH2_YIELD(STMT_) STMT_
409 #define _SEH2_LEAVE __leave