blob: fea2d4128feb13c64a919bddc450726668af77ff [file] [log] [blame]
Chris Lattner214191f2003-08-30 23:29:22 +00001
2#include "SJLJ-Exception.h"
3#include <cstdlib>
4#include <cassert>
5
6inline llvm_sjlj_exception *get_sjlj_exception(llvm_exception *E) {
7 assert(E->ExceptionType == SJLJException);
8 return (llvm_sjlj_exception*)(E+1) - 1;
9}
10
11// SetJmpMapEntry - One entry in a linked list of setjmps for the current
12// function.
13struct SetJmpMapEntry {
14 void *JmpBuf;
15 unsigned SetJmpID;
16 SetJmpMapEntry *Next;
17};
18
19static void SJLJDestructor(llvm_exception *E) {
20 free(get_sjlj_exception(E));
21}
22
23
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
27// function call.
28void __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;
37
38 __llvm_eh_add_uncaught_exception(&E->BaseException);
39}
40
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
43// calls setjmp.
44void __llvm_sjljeh_init_setjmpmap(void **SetJmpMap) throw() {
45 *SetJmpMap = 0;
46}
47
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.
51void __llvm_sjljeh_destroy_setjmpmap(void **SetJmpMap) throw() {
52 SetJmpMapEntry *Next;
53 for (SetJmpMapEntry *SJE = *(SetJmpMapEntry**)SetJmpMap; SJE; SJE = Next) {
54 Next = SJE->Next;
55 free(SJE);
56 }
57}
58
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.
61void __llvm_sjljeh_add_setjmp_to_map(void **SetJmpMap, void *JmpBuf,
62 unsigned SetJmpID) throw() {
63 SetJmpMapEntry **SJE = (SetJmpMapEntry**)SetJmpMap;
64
65 // Scan for a pre-existing entry...
66 for (; *SJE; SJE = &(*SJE)->Next)
67 if ((*SJE)->JmpBuf == JmpBuf) {
68 (*SJE)->SetJmpID = SetJmpID;
69 return;
70 }
71
72 // No prexisting entry found, append to the end of the list...
73 SetJmpMapEntry *New = (SetJmpMapEntry *)malloc(sizeof(SetJmpMapEntry));
74 *SJE = New;
75 New->JmpBuf = JmpBuf;
76 New->SetJmpID = SetJmpID;
77 New->Next = 0;
78}
79
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.
83bool __llvm_sjljeh_is_longjmp_exception() throw() {
84 return __llvm_eh_current_uncaught_exception_type(SJLJException) != 0;
85}
86
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.
91int __llvm_sjljeh_get_longjmp_value() throw() {
92 llvm_sjlj_exception *E =
93 get_sjlj_exception(__llvm_eh_get_uncaught_exception());
94 return E->LongJmpValue;
95}
96
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.
102unsigned __llvm_sjljeh_try_catching_longjmp_exception(void **SetJmpMap) throw(){
103 llvm_sjlj_exception *E =
104 get_sjlj_exception(__llvm_eh_get_uncaught_exception());
105
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();
112
113 // We know it's a longjmp exception, so we can just free it instead of
114 // calling the destructor.
115 free(E);
116
117 // Return the setjmp ID which we should branch to...
118 return SJE->SetJmpID;
119 }
120
121 // No setjmp in this function catches the exception!
122 return ~0;
123}