1 //===- c++-exception.cpp - Exception handling support for C++ exceptions --===//
3 // This file defines the methods used to implement C++ exception handling in
4 // terms of the invoke and %llvm.unwind intrinsic. These primitives implement
5 // an exception handling ABI similar (but simpler and more efficient than) the
6 // Itanium C++ ABI exception handling standard.
8 //===----------------------------------------------------------------------===//
10 #include "c++-exception.h"
14 //===----------------------------------------------------------------------===//
15 // Generic exception support
18 // Thread local state for exception handling.
19 // FIXME: This should really be made thread-local!
21 static llvm_exception *CaughtExceptionStack = 0;
23 // UncaughtExceptionStack - The stack of exceptions currently being thrown.
24 static llvm_exception *UncaughtExceptionStack = 0;
26 // __llvm_eh_has_uncaught_exception - This is used to implement
27 // std::uncaught_exception.
29 bool __llvm_eh_has_uncaught_exception(void) {
30 return UncaughtExceptionStack != 0;
33 // __llvm_eh_current_uncaught_exception - This function checks to see if the
34 // current uncaught exception is of the specified language type. If so, it
35 // returns a pointer to the exception area data.
37 void *__llvm_eh_current_uncaught_exception_type(unsigned HandlerType) {
38 assert(UncaughtExceptionStack && "No uncaught exception!");
39 if (UncaughtExceptionStack->ExceptionType == HandlerType)
40 return UncaughtExceptionStack+1;
45 //===----------------------------------------------------------------------===//
46 // C++ Specific exception handling support...
49 // __llvm_cxxeh_allocate_exception - This function allocates space for the
50 // specified number of bytes, plus a C++ exception object header.
52 void *__llvm_cxxeh_allocate_exception(unsigned NumBytes) {
53 // FIXME: This should eventually have back-up buffers for out-of-memory
56 llvm_cxx_exception *E =
57 (llvm_cxx_exception *)malloc(NumBytes+sizeof(llvm_cxx_exception));
58 E->BaseException.ExceptionType = 0; // intialize to invalid
60 return E+1; // return the pointer after the header
63 // __llvm_cxxeh_free_exception - Low-level function to free an exception. This
64 // is called directly from generated C++ code if evaluating the exception value
65 // into the exception location throws. Otherwise it is called from the C++
66 // exception object destructor.
68 void __llvm_cxxeh_free_exception(void *ObjectPtr) {
69 llvm_cxx_exception *E = (llvm_cxx_exception *)ObjectPtr - 1;
73 // cxx_destructor - This function is called through the generic
74 // exception->ExceptionDestructor function pointer to destroy a caught
77 static void cxx_destructor(llvm_exception *LE) {
78 void *ObjectPtr = LE+1;
79 llvm_cxx_exception *E = (llvm_cxx_exception *)ObjectPtr - 1;
81 // The exception is no longer caught.
82 assert(CaughtExceptionStack == LE &&
83 "Destroying an exception which is not the current caught exception?");
84 CaughtExceptionStack = LE->Next;
86 struct ExceptionFreer {
88 ExceptionFreer(void *P) : Ptr(P) {}
90 // Free the memory for the exception, when the function is left, even if
91 // the exception object dtor throws its own exception!
92 __llvm_cxxeh_free_exception(Ptr);
96 // Run the exception object dtor if it exists. */
97 if (E->ExceptionObjectDestructor)
98 E->ExceptionObjectDestructor(E);
101 // __llvm_cxxeh_throw - Given a pointer to memory which has an exception object
102 // evaluated into it, this sets up all of the fields of the exception allowing
103 // it to be thrown. After calling this, the code should call %llvm.unwind
105 void __llvm_cxxeh_throw(void *ObjectPtr, const std::type_info *TypeInfoPtr,
106 void (*DtorPtr)(void*)) {
107 llvm_cxx_exception *E = (llvm_cxx_exception *)ObjectPtr - 1;
108 E->BaseException.ExceptionDestructor = cxx_destructor;
109 E->BaseException.ExceptionType = CXXException;
110 E->BaseException.Next = UncaughtExceptionStack;
111 UncaughtExceptionStack = &E->BaseException;
112 E->BaseException.HandlerCount = 0;
114 E->TypeInfo = TypeInfoPtr;
115 E->ExceptionObjectDestructor = DtorPtr;
116 E->UnexpectedHandler = 0; // FIXME
117 E->TerminateHandler = 0; // FIXME
120 // __llvm_cxxeh_current_uncaught_exception_isa - This function checks to see if
121 // the current uncaught exception is a C++ exception, and if it is of the
122 // specified type id. If so, it returns a pointer to the object adjusted as
123 // appropriate, otherwise it returns null.
125 void *__llvm_cxxeh_current_uncaught_exception_isa(
126 const std::type_info *CatchType) {
127 assert(UncaughtExceptionStack && "No uncaught exception!");
128 if (UncaughtExceptionStack->ExceptionType != CXXException)
129 return 0; // If it's not a c++ exception, it doesn't match!
131 // If it is a C++ exception, use the type info object stored in the exception
132 // to see if TypeID matches and, if so, to adjust the exception object
135 llvm_cxx_exception *E = (llvm_cxx_exception*)UncaughtExceptionStack;
137 // ThrownPtr is a pointer to the object being thrown...
138 void *ThrownPtr = E+1;
139 const std::type_info *ThrownType = E->TypeInfo;
141 // FIXME: this code exists in the GCC exception handling library: I haven't
142 // thought about this yet, so it should be verified at some point!
144 // Pointer types need to adjust the actual pointer, not
145 // the pointer to pointer that is the exception object.
146 // This also has the effect of passing pointer types
147 // "by value" through the __cxa_begin_catch return value.
148 if (ThrownType->__is_pointer_p())
149 ThrownPtr = *(void **)ThrownPtr;
152 if (CatchType->__do_catch(ThrownType, &ThrownPtr, 1))
159 // __llvm_cxxeh_begin_catch - This function is called by "exception handlers",
160 // which transition an exception from being uncaught to being caught. It
161 // returns a pointer to the exception object portion of the exception. This
162 // function must work with foreign exceptions.
164 void *__llvm_cxxeh_begin_catch(void) {
165 llvm_exception *E = UncaughtExceptionStack;
166 assert(UncaughtExceptionStack && "There are no uncaught exceptions!?!?");
168 // The exception is now no longer uncaught.
169 UncaughtExceptionStack = E->Next;
171 // The exception is now caught.
172 E->Next = CaughtExceptionStack;
173 CaughtExceptionStack = E->Next;
175 // Increment the handler count for this exception.
178 // Return a pointer to the raw exception object.
182 // __llvm_cxxeh_begin_catch_if_isa - This function checks to see if the current
183 // uncaught exception is of the specified type. If not, it returns a null
184 // pointer, otherwise it 'catches' the exception and returns a pointer to the
185 // object of the specified type. This function does never succeeds with foreign
186 // exceptions (because they can never be of type CatchType).
188 void *__llvm_cxxeh_begin_catch_if_isa(const std::type_info *CatchType) {
189 void *ObjPtr = __llvm_cxxeh_current_uncaught_exception_isa(CatchType);
190 if (!ObjPtr) return 0;
192 // begin_catch, meaning that the object is now "caught", not "uncaught"
193 __llvm_cxxeh_begin_catch();
198 // __llvm_cxxeh_end_catch - This function decrements the HandlerCount of the
199 // top-level caught exception, destroying it if this is the last handler for the
202 void __llvm_cxxeh_end_catch(void) {
203 llvm_exception *E = CaughtExceptionStack;
204 assert(E && "There are no caught exceptions!");
206 // If this is the last handler using the exception, destroy it now!
207 if (--E->HandlerCount == 0) {
208 CaughtExceptionStack = E->Next; // Unlink from the stack
209 E->ExceptionDestructor(E); // Release memory for the exception
213 // __llvm_cxxeh_rethrow - This function turns the top-level caught exception
214 // into an uncaught exception, in preparation for an llvm.unwind, which should
215 // follow immediately after the call to this function. This function must be
216 // prepared to deal with foreign exceptions.
218 void __llvm_cxxeh_rethrow(void) {
219 llvm_exception *E = CaughtExceptionStack;
221 // 15.1.8 - If there are no uncaught exceptions being thrown, 'throw;'
222 // should call terminate.
224 assert(0 && "FIXME: this should call E->Terminate!"); // FIXME!
227 // Otherwise we have an exception to rethrow. Move it back to the uncaught
229 CaughtExceptionStack = E->Next;
230 E->Next = UncaughtExceptionStack;
231 UncaughtExceptionStack = E;
233 // Decrement the number of handlers which are using the exception.
236 // Return to the caller, which should perform the unwind now.