blob: 0221df1e437556b35d194a8093476ac05e26189b [file] [log] [blame]
Chris Lattner7b3092d2003-08-30 23:31:08 +00001//===- SJLJ-Exception.cpp - SetJmp/LongJmp Exception Handling -------------===//
2//
3// This file implements the API used by the Setjmp/Longjmp exception handling
4// runtime library.
5//
6//===----------------------------------------------------------------------===//
Chris Lattner214191f2003-08-30 23:29:22 +00007
8#include "SJLJ-Exception.h"
9#include <cstdlib>
10#include <cassert>
11
Chris Lattnerd338a232003-11-09 00:29:51 +000012// Assert should only be used for debugging the runtime library. Enabling it in
13// CVS will break some platforms!
14#undef assert
15#define assert(X)
16
Chris Lattner7b3092d2003-08-30 23:31:08 +000017// get_sjlj_exception - Adjust the llvm_exception pointer to be an appropriate
18// llvm_sjlj_exception pointer.
Chris Lattner214191f2003-08-30 23:29:22 +000019inline llvm_sjlj_exception *get_sjlj_exception(llvm_exception *E) {
20 assert(E->ExceptionType == SJLJException);
21 return (llvm_sjlj_exception*)(E+1) - 1;
22}
23
24// SetJmpMapEntry - One entry in a linked list of setjmps for the current
25// function.
26struct SetJmpMapEntry {
27 void *JmpBuf;
28 unsigned SetJmpID;
29 SetJmpMapEntry *Next;
30};
31
Chris Lattner7b3092d2003-08-30 23:31:08 +000032// SJLJDestructor - This function is used to free the exception when
33// language-indent code needs to destroy the exception without knowing exactly
34// what type it is.
Chris Lattner214191f2003-08-30 23:29:22 +000035static void SJLJDestructor(llvm_exception *E) {
36 free(get_sjlj_exception(E));
37}
38
39
40// __llvm_sjljeh_throw_longjmp - This function creates the longjmp exception and
41// returns. It takes care of mapping the longjmp value from 0 -> 1 as
42// appropriate. The caller should immediately call llvm.unwind after this
43// function call.
44void __llvm_sjljeh_throw_longjmp(void *JmpBuffer, int Val) throw() {
45 llvm_sjlj_exception *E =
46 (llvm_sjlj_exception *)malloc(sizeof(llvm_sjlj_exception));
47 E->BaseException.ExceptionDestructor = SJLJDestructor;
48 E->BaseException.ExceptionType = SJLJException;
49 E->BaseException.HandlerCount = 0;
50 E->BaseException.isRethrown = 0;
51 E->JmpBuffer = JmpBuffer;
52 E->LongJmpValue = Val ? Val : 1;
53
54 __llvm_eh_add_uncaught_exception(&E->BaseException);
55}
56
57// __llvm_sjljeh_init_setjmpmap - This funciton initializes the pointer provided
58// to an empty setjmp map, and should be called on entry to a function which
59// calls setjmp.
60void __llvm_sjljeh_init_setjmpmap(void **SetJmpMap) throw() {
61 *SetJmpMap = 0;
62}
63
64// __llvm_sjljeh_destroy_setjmpmap - This function frees all memory associated
65// with the specified setjmpmap structure. It should be called on all exits
66// (returns or unwinds) from the function which calls ...init_setjmpmap.
67void __llvm_sjljeh_destroy_setjmpmap(void **SetJmpMap) throw() {
68 SetJmpMapEntry *Next;
69 for (SetJmpMapEntry *SJE = *(SetJmpMapEntry**)SetJmpMap; SJE; SJE = Next) {
70 Next = SJE->Next;
71 free(SJE);
72 }
73}
74
75// __llvm_sjljeh_add_setjmp_to_map - This function adds or updates an entry to
76// the map, to indicate which setjmp should be returned to if a longjmp happens.
77void __llvm_sjljeh_add_setjmp_to_map(void **SetJmpMap, void *JmpBuf,
78 unsigned SetJmpID) throw() {
79 SetJmpMapEntry **SJE = (SetJmpMapEntry**)SetJmpMap;
80
81 // Scan for a pre-existing entry...
82 for (; *SJE; SJE = &(*SJE)->Next)
83 if ((*SJE)->JmpBuf == JmpBuf) {
84 (*SJE)->SetJmpID = SetJmpID;
85 return;
86 }
87
88 // No prexisting entry found, append to the end of the list...
89 SetJmpMapEntry *New = (SetJmpMapEntry *)malloc(sizeof(SetJmpMapEntry));
90 *SJE = New;
91 New->JmpBuf = JmpBuf;
92 New->SetJmpID = SetJmpID;
93 New->Next = 0;
94}
95
96// __llvm_sjljeh_is_longjmp_exception - This function returns true if the
97// current uncaught exception is a longjmp exception. This is the first step of
98// catching a sjlj exception.
99bool __llvm_sjljeh_is_longjmp_exception() throw() {
100 return __llvm_eh_current_uncaught_exception_type(SJLJException) != 0;
101}
102
103// __llvm_sjljeh_get_longjmp_value - This function returns the value that the
104// setjmp call should "return". This requires that the current uncaught
105// exception be a sjlj exception, though it does not require the exception to be
106// caught by this function.
107int __llvm_sjljeh_get_longjmp_value() throw() {
108 llvm_sjlj_exception *E =
109 get_sjlj_exception(__llvm_eh_get_uncaught_exception());
110 return E->LongJmpValue;
111}
112
113// __llvm_sjljeh_try_catching_longjmp_exception - This function checks to see if
114// the current uncaught longjmp exception matches any of the setjmps collected
115// in the setjmpmap structure. If so, it catches and destroys the exception,
116// returning the index of the setjmp which caught the exception. If not, it
117// leaves the exception uncaught and returns a value of ~0.
118unsigned __llvm_sjljeh_try_catching_longjmp_exception(void **SetJmpMap) throw(){
119 llvm_sjlj_exception *E =
120 get_sjlj_exception(__llvm_eh_get_uncaught_exception());
121
122 // Scan for a matching entry in the SetJmpMap...
123 SetJmpMapEntry *SJE = *(SetJmpMapEntry**)SetJmpMap;
124 for (; SJE; SJE = SJE->Next)
125 if (SJE->JmpBuf == E->JmpBuffer) {
126 // "Catch" and destroy the exception...
127 __llvm_eh_pop_from_uncaught_stack();
128
129 // We know it's a longjmp exception, so we can just free it instead of
130 // calling the destructor.
131 free(E);
132
133 // Return the setjmp ID which we should branch to...
134 return SJE->SetJmpID;
135 }
136
137 // No setjmp in this function catches the exception!
138 return ~0;
139}