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"
13 //===----------------------------------------------------------------------===//
14 // Generic exception support
17 // Thread local state for exception handling.
18 // FIXME: This should really be made thread-local!
20 static llvm_exception *CaughtExceptionStack = 0;
22 // UncaughtExceptionStack - The stack of exceptions currently being thrown.
23 static llvm_exception *UncaughtExceptionStack = 0;
25 // __llvm_eh_has_uncaught_exception - This is used to implement
26 // std::uncaught_exception.
28 bool __llvm_eh_has_uncaught_exception(void) {
29 return UncaughtExceptionStack != 0;
32 // __llvm_eh_current_uncaught_exception - This function checks to see if the
33 // current uncaught exception is of the specified language type. If so, it
34 // returns a pointer to the exception area data.
36 void *__llvm_eh_current_uncaught_exception_type(unsigned HandlerType) {
37 assert(UncaughtExceptionStack && "No uncaught exception!");
38 if (UncaughtExceptionStack->ExceptionType == HandlerType)
39 return UncaughtExceptionStack+1;
44 //===----------------------------------------------------------------------===//
45 // C++ Specific exception handling support...
48 // __llvm_cxxeh_allocate_exception - This function allocates space for the
49 // specified number of bytes, plus a C++ exception object header.
51 void *__llvm_cxxeh_allocate_exception(unsigned NumBytes) {
52 // FIXME: This should eventually have back-up buffers for out-of-memory
55 llvm_cxx_exception *E =
56 (llvm_cxx_exception *)malloc(NumBytes+sizeof(llvm_cxx_exception));
57 E->BaseException.ExceptionType = 0; // intialize to invalid
59 return E+1; // return the pointer after the header
62 // __llvm_cxxeh_free_exception - Low-level function to free an exception. This
63 // is called directly from generated C++ code if evaluating the exception value
64 // into the exception location throws. Otherwise it is called from the C++
65 // exception object destructor.
67 void __llvm_cxxeh_free_exception(void *ObjectPtr) {
68 llvm_cxx_exception *E = (llvm_cxx_exception *)ObjectPtr - 1;
72 // cxx_destructor - This function is called through the generic
73 // exception->ExceptionDestructor function pointer to destroy a caught
76 static void cxx_destructor(llvm_exception *LE) {
77 llvm_cxx_exception *E = get_cxx_exception(LE);
79 // The exception is no longer caught.
80 assert(CaughtExceptionStack == LE &&
81 "Destroying an exception which is not the current caught exception?");
82 CaughtExceptionStack = LE->Next;
84 struct ExceptionFreer {
86 ExceptionFreer(void *P) : Ptr(P) {}
88 // Free the memory for the exception, when the function is left, even if
89 // the exception object dtor throws its own exception!
90 __llvm_cxxeh_free_exception(Ptr);
94 // Run the exception object dtor if it exists. */
95 if (E->ExceptionObjectDestructor)
96 E->ExceptionObjectDestructor(E);
99 // __llvm_cxxeh_throw - Given a pointer to memory which has an exception object
100 // evaluated into it, this sets up all of the fields of the exception allowing
101 // it to be thrown. After calling this, the code should call %llvm.unwind
103 void __llvm_cxxeh_throw(void *ObjectPtr, const std::type_info *TypeInfoPtr,
104 void (*DtorPtr)(void*)) {
105 llvm_cxx_exception *E = (llvm_cxx_exception *)ObjectPtr - 1;
106 E->BaseException.ExceptionDestructor = cxx_destructor;
107 E->BaseException.ExceptionType = CXXException;
108 E->BaseException.Next = UncaughtExceptionStack;
109 UncaughtExceptionStack = &E->BaseException;
110 E->BaseException.HandlerCount = 0;
112 E->TypeInfo = TypeInfoPtr;
113 E->ExceptionObjectDestructor = DtorPtr;
114 E->UnexpectedHandler = 0; // FIXME
115 E->TerminateHandler = 0; // FIXME
118 // __llvm_cxxeh_current_uncaught_exception_isa - This function checks to see if
119 // the current uncaught exception is a C++ exception, and if it is of the
120 // specified type id. If so, it returns a pointer to the object adjusted as
121 // appropriate, otherwise it returns null.
123 void *__llvm_cxxeh_current_uncaught_exception_isa(
124 const std::type_info *CatchType) {
125 assert(UncaughtExceptionStack && "No uncaught exception!");
126 if (UncaughtExceptionStack->ExceptionType != CXXException)
127 return 0; // If it's not a c++ exception, it doesn't match!
129 // If it is a C++ exception, use the type info object stored in the exception
130 // to see if TypeID matches and, if so, to adjust the exception object
133 llvm_cxx_exception *E = get_cxx_exception(UncaughtExceptionStack);
135 // ThrownPtr is a pointer to the object being thrown...
136 void *ThrownPtr = E+1;
137 const std::type_info *ThrownType = E->TypeInfo;
139 // FIXME: this code exists in the GCC exception handling library: I haven't
140 // thought about this yet, so it should be verified at some point!
142 // Pointer types need to adjust the actual pointer, not
143 // the pointer to pointer that is the exception object.
144 // This also has the effect of passing pointer types
145 // "by value" through the __cxa_begin_catch return value.
146 if (ThrownType->__is_pointer_p())
147 ThrownPtr = *(void **)ThrownPtr;
150 if (CatchType->__do_catch(ThrownType, &ThrownPtr, 1))
157 // __llvm_cxxeh_begin_catch - This function is called by "exception handlers",
158 // which transition an exception from being uncaught to being caught. It
159 // returns a pointer to the exception object portion of the exception. This
160 // function must work with foreign exceptions.
162 void *__llvm_cxxeh_begin_catch(void) {
163 llvm_exception *E = UncaughtExceptionStack;
164 assert(UncaughtExceptionStack && "There are no uncaught exceptions!?!?");
166 // The exception is now no longer uncaught.
167 UncaughtExceptionStack = E->Next;
169 // The exception is now caught.
170 E->Next = CaughtExceptionStack;
171 CaughtExceptionStack = E;
173 // Increment the handler count for this exception.
176 // Return a pointer to the raw exception object.
180 // __llvm_cxxeh_begin_catch_if_isa - This function checks to see if the current
181 // uncaught exception is of the specified type. If not, it returns a null
182 // pointer, otherwise it 'catches' the exception and returns a pointer to the
183 // object of the specified type. This function does never succeeds with foreign
184 // exceptions (because they can never be of type CatchType).
186 void *__llvm_cxxeh_begin_catch_if_isa(const std::type_info *CatchType) {
187 void *ObjPtr = __llvm_cxxeh_current_uncaught_exception_isa(CatchType);
188 if (!ObjPtr) return 0;
190 // begin_catch, meaning that the object is now "caught", not "uncaught"
191 __llvm_cxxeh_begin_catch();
196 // __llvm_cxxeh_end_catch - This function decrements the HandlerCount of the
197 // top-level caught exception, destroying it if this is the last handler for the
200 void __llvm_cxxeh_end_catch(void) {
201 llvm_exception *E = CaughtExceptionStack;
202 assert(E && "There are no caught exceptions!");
204 // If this is the last handler using the exception, destroy it now!
205 if (--E->HandlerCount == 0)
206 E->ExceptionDestructor(E); // Release memory for the exception
209 // __llvm_cxxeh_rethrow - This function turns the top-level caught exception
210 // into an uncaught exception, in preparation for an llvm.unwind, which should
211 // follow immediately after the call to this function. This function must be
212 // prepared to deal with foreign exceptions.
214 void __llvm_cxxeh_rethrow(void) {
215 llvm_exception *E = CaughtExceptionStack;
217 // 15.1.8 - If there are no uncaught exceptions being thrown, 'throw;'
218 // should call terminate.
220 assert(0 && "FIXME: this should call E->Terminate!"); // FIXME!
223 // Otherwise we have an exception to rethrow. Move it back to the uncaught
225 CaughtExceptionStack = E->Next;
226 E->Next = UncaughtExceptionStack;
227 UncaughtExceptionStack = E;
229 // Return to the caller, which should perform the unwind now.