Fix several _killer_ bugs. This now actually WORKS for really complex testcases :)
[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
13 //===----------------------------------------------------------------------===//
14 // Generic exception support
15 //
16
17 // Thread local state for exception handling.
18 // FIXME: This should really be made thread-local!
19 //
20 static llvm_exception *CaughtExceptionStack = 0;
21
22 // UncaughtExceptionStack - The stack of exceptions currently being thrown.
23 static llvm_exception *UncaughtExceptionStack = 0;
24
25 // __llvm_eh_has_uncaught_exception - This is used to implement
26 // std::uncaught_exception.
27 //
28 bool __llvm_eh_has_uncaught_exception(void) {
29   return UncaughtExceptionStack != 0;
30 }
31
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.
35 //
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;
40   return 0;
41 }
42
43
44 //===----------------------------------------------------------------------===//
45 // C++ Specific exception handling support...
46 //
47
48 // __llvm_cxxeh_allocate_exception - This function allocates space for the
49 // specified number of bytes, plus a C++ exception object header.
50 //
51 void *__llvm_cxxeh_allocate_exception(unsigned NumBytes) {
52   // FIXME: This should eventually have back-up buffers for out-of-memory
53   // situations.
54   //
55   llvm_cxx_exception *E =
56     (llvm_cxx_exception *)malloc(NumBytes+sizeof(llvm_cxx_exception));
57   E->BaseException.ExceptionType = 0; // intialize to invalid
58
59   return E+1;   // return the pointer after the header
60 }
61
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.
66 //
67 void __llvm_cxxeh_free_exception(void *ObjectPtr) {
68   llvm_cxx_exception *E = (llvm_cxx_exception *)ObjectPtr - 1;
69   free(E);
70 }
71
72 // cxx_destructor - This function is called through the generic
73 // exception->ExceptionDestructor function pointer to destroy a caught
74 // exception.
75 //
76 static void cxx_destructor(llvm_exception *LE) {
77   llvm_cxx_exception *E = get_cxx_exception(LE);
78
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;
83
84   struct ExceptionFreer {
85     void *Ptr;
86     ExceptionFreer(void *P) : Ptr(P) {}
87     ~ExceptionFreer() {
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);
91     }
92   } EF(E+1);
93   
94   // Run the exception object dtor if it exists. */
95   if (E->ExceptionObjectDestructor)
96     E->ExceptionObjectDestructor(E);
97 }
98
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
102 //
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;
111
112   E->TypeInfo = TypeInfoPtr;
113   E->ExceptionObjectDestructor = DtorPtr;
114   E->UnexpectedHandler = 0;  // FIXME
115   E->TerminateHandler = 0;   // FIXME
116 }
117
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.
122 //
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!
128
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
131   // pointer.
132   //
133   llvm_cxx_exception *E = get_cxx_exception(UncaughtExceptionStack);
134
135   // ThrownPtr is a pointer to the object being thrown...
136   void *ThrownPtr = E+1;
137   const std::type_info *ThrownType = E->TypeInfo;
138
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!
141 #if 1
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;
148 #endif
149
150   if (CatchType->__do_catch(ThrownType, &ThrownPtr, 1))
151     return ThrownPtr;
152
153   return 0;
154 }
155
156
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.
161 //
162 void *__llvm_cxxeh_begin_catch(void) {
163   llvm_exception *E = UncaughtExceptionStack;
164   assert(UncaughtExceptionStack && "There are no uncaught exceptions!?!?");
165
166   // The exception is now no longer uncaught.
167   UncaughtExceptionStack = E->Next;
168   
169   // The exception is now caught.
170   E->Next = CaughtExceptionStack;
171   CaughtExceptionStack = E;
172
173   // Increment the handler count for this exception.
174   E->HandlerCount++;
175   
176   // Return a pointer to the raw exception object.
177   return E+1;
178 }
179
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).
185 //
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;
189   
190   // begin_catch, meaning that the object is now "caught", not "uncaught"
191   __llvm_cxxeh_begin_catch();
192   return ObjPtr;
193 }
194
195
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
198 // exception.
199 //
200 void __llvm_cxxeh_end_catch(void) {
201   llvm_exception *E = CaughtExceptionStack;
202   assert(E && "There are no caught exceptions!");
203   
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
207 }
208
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.
213 //
214 void __llvm_cxxeh_rethrow(void) {
215   llvm_exception *E = CaughtExceptionStack;
216   if (E == 0) {
217     // 15.1.8 - If there are no uncaught exceptions being thrown, 'throw;'
218     // should call terminate.
219     //
220     assert(0 && "FIXME: this should call E->Terminate!"); // FIXME!
221   }
222
223   // Otherwise we have an exception to rethrow. Move it back to the uncaught
224   // stack.
225   CaughtExceptionStack = E->Next;
226   E->Next = UncaughtExceptionStack;
227   UncaughtExceptionStack = E;
228   
229   // Return to the caller, which should perform the unwind now.
230 }
231