532c3ec30d311ab9a1c6445a1272d11d794efec6
[oota-llvm.git] / runtime / GCCLibraries / crtend / C++-Exception.cpp
1 //===- c++-exception.cpp - Exception handling support for C++ exceptions --===//
2 //
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.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "c++-exception.h"
11 #include <cstdlib>
12 #include <cassert>
13
14 //===----------------------------------------------------------------------===//
15 // Generic exception support
16 //
17
18 // Thread local state for exception handling.
19 // FIXME: This should really be made thread-local!
20 //
21 static llvm_exception *CaughtExceptionStack = 0;
22
23 // UncaughtExceptionStack - The stack of exceptions currently being thrown.
24 static llvm_exception *UncaughtExceptionStack = 0;
25
26 // __llvm_eh_has_uncaught_exception - This is used to implement
27 // std::uncaught_exception.
28 //
29 bool __llvm_eh_has_uncaught_exception(void) {
30   return UncaughtExceptionStack != 0;
31 }
32
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.
36 //
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;
41   return 0;
42 }
43
44
45 //===----------------------------------------------------------------------===//
46 // C++ Specific exception handling support...
47 //
48
49 // __llvm_cxxeh_allocate_exception - This function allocates space for the
50 // specified number of bytes, plus a C++ exception object header.
51 //
52 void *__llvm_cxxeh_allocate_exception(unsigned NumBytes) {
53   // FIXME: This should eventually have back-up buffers for out-of-memory
54   // situations.
55   //
56   llvm_cxx_exception *E =
57     (llvm_cxx_exception *)malloc(NumBytes+sizeof(llvm_cxx_exception));
58   E->BaseException.ExceptionType = 0; // intialize to invalid
59
60   return E+1;   // return the pointer after the header
61 }
62
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.
67 //
68 void __llvm_cxxeh_free_exception(void *ObjectPtr) {
69   llvm_cxx_exception *E = (llvm_cxx_exception *)ObjectPtr - 1;
70   free(E);
71 }
72
73 // cxx_destructor - This function is called through the generic
74 // exception->ExceptionDestructor function pointer to destroy a caught
75 // exception.
76 //
77 static void cxx_destructor(llvm_exception *LE) {
78   void *ObjectPtr = LE+1;
79   llvm_cxx_exception *E = (llvm_cxx_exception *)ObjectPtr - 1;
80
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;
85
86   struct ExceptionFreer {
87     void *Ptr;
88     ExceptionFreer(void *P) : Ptr(P) {}
89     ~ExceptionFreer() {
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);
93     }
94   } EF(E+1);
95   
96   // Run the exception object dtor if it exists. */
97   if (E->ExceptionObjectDestructor)
98     E->ExceptionObjectDestructor(E);
99 }
100
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
104 //
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;
113
114   E->TypeInfo = TypeInfoPtr;
115   E->ExceptionObjectDestructor = DtorPtr;
116   E->UnexpectedHandler = 0;  // FIXME
117   E->TerminateHandler = 0;   // FIXME
118 }
119
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.
124 //
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!
130
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
133   // pointer.
134   //
135   llvm_cxx_exception *E = (llvm_cxx_exception*)UncaughtExceptionStack;
136
137   // ThrownPtr is a pointer to the object being thrown...
138   void *ThrownPtr = E+1;
139   const std::type_info *ThrownType = E->TypeInfo;
140
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!
143 #if 1
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;
150 #endif
151
152   if (CatchType->__do_catch(ThrownType, &ThrownPtr, 1))
153     return ThrownPtr;
154
155   return 0;
156 }
157
158
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.
163 //
164 void *__llvm_cxxeh_begin_catch(void) {
165   llvm_exception *E = UncaughtExceptionStack;
166   assert(UncaughtExceptionStack && "There are no uncaught exceptions!?!?");
167
168   // The exception is now no longer uncaught.
169   UncaughtExceptionStack = E->Next;
170   
171   // The exception is now caught.
172   E->Next = CaughtExceptionStack;
173   CaughtExceptionStack = E->Next;
174
175   // Increment the handler count for this exception.
176   E->HandlerCount++;
177   
178   // Return a pointer to the raw exception object.
179   return E+1;
180 }
181
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).
187 //
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;
191   
192   // begin_catch, meaning that the object is now "caught", not "uncaught"
193   __llvm_cxxeh_begin_catch();
194   return ObjPtr;
195 }
196
197
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
200 // exception.
201 //
202 void __llvm_cxxeh_end_catch(void) {
203   llvm_exception *E = CaughtExceptionStack;
204   assert(E && "There are no caught exceptions!");
205   
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
210   }
211 }
212
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.
217 //
218 void __llvm_cxxeh_rethrow(void) {
219   llvm_exception *E = CaughtExceptionStack;
220   if (E == 0) {
221     // 15.1.8 - If there are no uncaught exceptions being thrown, 'throw;'
222     // should call terminate.
223     //
224     assert(0 && "FIXME: this should call E->Terminate!"); // FIXME!
225   }
226
227   // Otherwise we have an exception to rethrow. Move it back to the uncaught
228   // stack.
229   CaughtExceptionStack = E->Next;
230   E->Next = UncaughtExceptionStack;
231   UncaughtExceptionStack = E;
232
233   // Decrement the number of handlers which are using the exception.
234   --E->HandlerCount;
235   
236   // Return to the caller, which should perform the unwind now.
237 }
238