2 #include "SJLJ-Exception.h"
6 inline llvm_sjlj_exception *get_sjlj_exception(llvm_exception *E) {
7 assert(E->ExceptionType == SJLJException);
8 return (llvm_sjlj_exception*)(E+1) - 1;
11 // SetJmpMapEntry - One entry in a linked list of setjmps for the current
13 struct SetJmpMapEntry {
19 static void SJLJDestructor(llvm_exception *E) {
20 free(get_sjlj_exception(E));
24 // __llvm_sjljeh_throw_longjmp - This function creates the longjmp exception and
25 // returns. It takes care of mapping the longjmp value from 0 -> 1 as
26 // appropriate. The caller should immediately call llvm.unwind after this
28 void __llvm_sjljeh_throw_longjmp(void *JmpBuffer, int Val) throw() {
29 llvm_sjlj_exception *E =
30 (llvm_sjlj_exception *)malloc(sizeof(llvm_sjlj_exception));
31 E->BaseException.ExceptionDestructor = SJLJDestructor;
32 E->BaseException.ExceptionType = SJLJException;
33 E->BaseException.HandlerCount = 0;
34 E->BaseException.isRethrown = 0;
35 E->JmpBuffer = JmpBuffer;
36 E->LongJmpValue = Val ? Val : 1;
38 __llvm_eh_add_uncaught_exception(&E->BaseException);
41 // __llvm_sjljeh_init_setjmpmap - This funciton initializes the pointer provided
42 // to an empty setjmp map, and should be called on entry to a function which
44 void __llvm_sjljeh_init_setjmpmap(void **SetJmpMap) throw() {
48 // __llvm_sjljeh_destroy_setjmpmap - This function frees all memory associated
49 // with the specified setjmpmap structure. It should be called on all exits
50 // (returns or unwinds) from the function which calls ...init_setjmpmap.
51 void __llvm_sjljeh_destroy_setjmpmap(void **SetJmpMap) throw() {
53 for (SetJmpMapEntry *SJE = *(SetJmpMapEntry**)SetJmpMap; SJE; SJE = Next) {
59 // __llvm_sjljeh_add_setjmp_to_map - This function adds or updates an entry to
60 // the map, to indicate which setjmp should be returned to if a longjmp happens.
61 void __llvm_sjljeh_add_setjmp_to_map(void **SetJmpMap, void *JmpBuf,
62 unsigned SetJmpID) throw() {
63 SetJmpMapEntry **SJE = (SetJmpMapEntry**)SetJmpMap;
65 // Scan for a pre-existing entry...
66 for (; *SJE; SJE = &(*SJE)->Next)
67 if ((*SJE)->JmpBuf == JmpBuf) {
68 (*SJE)->SetJmpID = SetJmpID;
72 // No prexisting entry found, append to the end of the list...
73 SetJmpMapEntry *New = (SetJmpMapEntry *)malloc(sizeof(SetJmpMapEntry));
76 New->SetJmpID = SetJmpID;
80 // __llvm_sjljeh_is_longjmp_exception - This function returns true if the
81 // current uncaught exception is a longjmp exception. This is the first step of
82 // catching a sjlj exception.
83 bool __llvm_sjljeh_is_longjmp_exception() throw() {
84 return __llvm_eh_current_uncaught_exception_type(SJLJException) != 0;
87 // __llvm_sjljeh_get_longjmp_value - This function returns the value that the
88 // setjmp call should "return". This requires that the current uncaught
89 // exception be a sjlj exception, though it does not require the exception to be
90 // caught by this function.
91 int __llvm_sjljeh_get_longjmp_value() throw() {
92 llvm_sjlj_exception *E =
93 get_sjlj_exception(__llvm_eh_get_uncaught_exception());
94 return E->LongJmpValue;
97 // __llvm_sjljeh_try_catching_longjmp_exception - This function checks to see if
98 // the current uncaught longjmp exception matches any of the setjmps collected
99 // in the setjmpmap structure. If so, it catches and destroys the exception,
100 // returning the index of the setjmp which caught the exception. If not, it
101 // leaves the exception uncaught and returns a value of ~0.
102 unsigned __llvm_sjljeh_try_catching_longjmp_exception(void **SetJmpMap) throw(){
103 llvm_sjlj_exception *E =
104 get_sjlj_exception(__llvm_eh_get_uncaught_exception());
106 // Scan for a matching entry in the SetJmpMap...
107 SetJmpMapEntry *SJE = *(SetJmpMapEntry**)SetJmpMap;
108 for (; SJE; SJE = SJE->Next)
109 if (SJE->JmpBuf == E->JmpBuffer) {
110 // "Catch" and destroy the exception...
111 __llvm_eh_pop_from_uncaught_stack();
113 // We know it's a longjmp exception, so we can just free it instead of
114 // calling the destructor.
117 // Return the setjmp ID which we should branch to...
118 return SJE->SetJmpID;
121 // No setjmp in this function catches the exception!