Move language independent exception handling routines OUT of C++Exception.cpp

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@8232 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner
2003-08-30 23:17:51 +00:00
parent e86159df9d
commit 7f45519784
6 changed files with 156 additions and 108 deletions

View File

@@ -17,44 +17,14 @@
#include <stdio.h> #include <stdio.h>
#endif #endif
//===----------------------------------------------------------------------===//
// Generic exception support
//
// Thread local state for exception handling.
// FIXME: This should really be made thread-local!
//
// LastCaughtException - The last exception caught by this handler. This is for // LastCaughtException - The last exception caught by this handler. This is for
// implementation of _rethrow and _get_last_caught. // implementation of _rethrow and _get_last_caught.
// //
// FIXME: This should be thread-local!
//
static llvm_exception *LastCaughtException = 0; static llvm_exception *LastCaughtException = 0;
// UncaughtExceptionStack - The stack of exceptions currently being thrown.
static llvm_exception *UncaughtExceptionStack = 0;
// __llvm_eh_has_uncaught_exception - This is used to implement
// std::uncaught_exception.
//
bool __llvm_eh_has_uncaught_exception() throw() {
return UncaughtExceptionStack != 0;
}
// __llvm_eh_current_uncaught_exception - This function checks to see if the
// current uncaught exception is of the specified language type. If so, it
// returns a pointer to the exception area data.
//
void *__llvm_eh_current_uncaught_exception_type(unsigned HandlerType) throw() {
assert(UncaughtExceptionStack && "No uncaught exception!");
if (UncaughtExceptionStack->ExceptionType == HandlerType)
return UncaughtExceptionStack+1;
return 0;
}
//===----------------------------------------------------------------------===//
// C++ Specific exception handling support...
//
using namespace __cxxabiv1; using namespace __cxxabiv1;
// __llvm_cxxeh_allocate_exception - This function allocates space for the // __llvm_cxxeh_allocate_exception - This function allocates space for the
@@ -113,8 +83,6 @@ void __llvm_cxxeh_throw(void *ObjectPtr, void *TypeInfoPtr,
llvm_cxx_exception *E = (llvm_cxx_exception *)ObjectPtr - 1; llvm_cxx_exception *E = (llvm_cxx_exception *)ObjectPtr - 1;
E->BaseException.ExceptionDestructor = cxx_destructor; E->BaseException.ExceptionDestructor = cxx_destructor;
E->BaseException.ExceptionType = CXXException; E->BaseException.ExceptionType = CXXException;
E->BaseException.Next = UncaughtExceptionStack;
UncaughtExceptionStack = &E->BaseException;
E->BaseException.HandlerCount = 0; E->BaseException.HandlerCount = 0;
E->BaseException.isRethrown = 0; E->BaseException.isRethrown = 0;
@@ -122,6 +90,8 @@ void __llvm_cxxeh_throw(void *ObjectPtr, void *TypeInfoPtr,
E->ExceptionObjectDestructor = DtorPtr; E->ExceptionObjectDestructor = DtorPtr;
E->UnexpectedHandler = __unexpected_handler; E->UnexpectedHandler = __unexpected_handler;
E->TerminateHandler = __terminate_handler; E->TerminateHandler = __terminate_handler;
__llvm_eh_add_uncaught_exception(&E->BaseException);
} }
@@ -163,16 +133,15 @@ static void *CXXExceptionISA(llvm_cxx_exception *E,
// appropriate, otherwise it returns null. // appropriate, otherwise it returns null.
// //
void *__llvm_cxxeh_current_uncaught_exception_isa(void *CatchType) throw() { void *__llvm_cxxeh_current_uncaught_exception_isa(void *CatchType) throw() {
assert(UncaughtExceptionStack && "No uncaught exception!"); void *EPtr = __llvm_eh_current_uncaught_exception_type(CXXException);
if (UncaughtExceptionStack->ExceptionType != CXXException) if (EPtr == 0) return 0; // If it's not a c++ exception, it doesn't match!
return 0; // If it's not a c++ exception, it doesn't match!
// If it is a C++ exception, use the type info object stored in the exception // If it is a C++ exception, use the type info object stored in the exception
// to see if TypeID matches and, if so, to adjust the exception object // to see if TypeID matches and, if so, to adjust the exception object
// pointer. // pointer.
// //
const std::type_info *Info = (const std::type_info *)CatchType; const std::type_info *Info = (const std::type_info *)CatchType;
return CXXExceptionISA(get_cxx_exception(UncaughtExceptionStack), Info); return CXXExceptionISA((llvm_cxx_exception*)EPtr - 1, Info);
} }
@@ -182,12 +151,8 @@ void *__llvm_cxxeh_current_uncaught_exception_isa(void *CatchType) throw() {
// function must work with foreign exceptions. // function must work with foreign exceptions.
// //
void *__llvm_cxxeh_begin_catch() throw() { void *__llvm_cxxeh_begin_catch() throw() {
llvm_exception *E = UncaughtExceptionStack; llvm_exception *E = __llvm_eh_pop_from_uncaught_stack();
assert(UncaughtExceptionStack && "There are no uncaught exceptions!?!?");
// The exception is now no longer uncaught.
UncaughtExceptionStack = E->Next;
// The exception is now caught. // The exception is now caught.
LastCaughtException = E; LastCaughtException = E;
E->Next = 0; E->Next = 0;
@@ -254,9 +219,9 @@ void __llvm_cxxeh_end_catch(void *Ex) /* might throw */ {
// time. // time.
void __llvm_cxxeh_call_terminate() throw() { void __llvm_cxxeh_call_terminate() throw() {
void (*Handler)(void) = __terminate_handler; void (*Handler)(void) = __terminate_handler;
if (UncaughtExceptionStack) if (__llvm_eh_has_uncaught_exception())
if (UncaughtExceptionStack->ExceptionType == CXXException) if (void *EPtr = __llvm_eh_current_uncaught_exception_type(CXXException))
Handler = get_cxx_exception(UncaughtExceptionStack)->TerminateHandler; Handler = ((llvm_cxx_exception*)EPtr - 1)->TerminateHandler;
__terminate(Handler); __terminate(Handler);
} }
@@ -279,8 +244,7 @@ void __llvm_cxxeh_rethrow() throw() {
// Add the exception to the top of the uncaught stack, to preserve the // Add the exception to the top of the uncaught stack, to preserve the
// invariant that the top of the uncaught stack is the current exception. // invariant that the top of the uncaught stack is the current exception.
E->Next = UncaughtExceptionStack; __llvm_eh_add_uncaught_exception(E);
UncaughtExceptionStack = E;
// Return to the caller, which should perform the unwind now. // Return to the caller, which should perform the unwind now.
} }
@@ -320,8 +284,6 @@ static bool ExceptionSpecificationPermitsException(llvm_exception *E,
// //
void __llvm_cxxeh_check_eh_spec(void *Info, ...) { void __llvm_cxxeh_check_eh_spec(void *Info, ...) {
const std::type_info *TypeInfo = (const std::type_info *)Info; const std::type_info *TypeInfo = (const std::type_info *)Info;
llvm_exception *E = UncaughtExceptionStack;
assert(E && "No uncaught exceptions!");
if (TypeInfo == 0) { // Empty exception specification if (TypeInfo == 0) { // Empty exception specification
// Whatever exception this is, it is not allowed by the (empty) spec, call // Whatever exception this is, it is not allowed by the (empty) spec, call
@@ -337,6 +299,9 @@ void __llvm_cxxeh_check_eh_spec(void *Info, ...) {
} }
} }
llvm_exception *E = __llvm_eh_get_uncaught_exception();
assert(E && "No uncaught exceptions!");
// Check to see if the exception matches one of the types allowed by the // Check to see if the exception matches one of the types allowed by the
// exception specification. If so, return to the caller to have the // exception specification. If so, return to the caller to have the
// exception rethrown. // exception rethrown.
@@ -369,8 +334,7 @@ void __llvm_cxxeh_check_eh_spec(void *Info, ...) {
// Grab the newly caught exception. If this exception is permitted by the // Grab the newly caught exception. If this exception is permitted by the
// specification, allow it to be thrown. // specification, allow it to be thrown.
E = UncaughtExceptionStack; E = __llvm_eh_get_uncaught_exception();
assert(E && "No uncaught exceptions!");
va_start(Args, Info); va_start(Args, Info);
Ok = ExceptionSpecificationPermitsException(E, TypeInfo, Args); Ok = ExceptionSpecificationPermitsException(E, TypeInfo, Args);
@@ -385,8 +349,7 @@ void __llvm_cxxeh_check_eh_spec(void *Info, ...) {
} }
// Grab the new bad_exception... // Grab the new bad_exception...
E = UncaughtExceptionStack; E = __llvm_eh_get_uncaught_exception();
assert(E && "No uncaught exceptions!");
// If it's permitted, allow it to be thrown instead. // If it's permitted, allow it to be thrown instead.
va_start(Args, Info); va_start(Args, Info);

View File

@@ -0,0 +1,57 @@
//===- Exception.cpp - Generic language-independent exceptions ------------===//
//
// This file defines the the shared data structures used by all language
// specific exception handling runtime libraries.
//
//===----------------------------------------------------------------------===//
#include "Exception.h"
#include <cassert>
// Thread local state for exception handling. FIXME: This should really be made
// thread-local!
// UncaughtExceptionStack - The stack of exceptions currently being thrown.
static llvm_exception *UncaughtExceptionStack = 0;
// __llvm_eh_has_uncaught_exception - This is used to implement
// std::uncaught_exception.
//
bool __llvm_eh_has_uncaught_exception() throw() {
return UncaughtExceptionStack != 0;
}
// __llvm_eh_current_uncaught_exception - This function checks to see if the
// current uncaught exception is of the specified language type. If so, it
// returns a pointer to the exception area data.
//
void *__llvm_eh_current_uncaught_exception_type(unsigned HandlerType) throw() {
assert(UncaughtExceptionStack && "No uncaught exception!");
if (UncaughtExceptionStack->ExceptionType == HandlerType)
return UncaughtExceptionStack+1;
return 0;
}
// __llvm_eh_add_uncaught_exception - This adds the specified exception to the
// top of the uncaught exception stack. The exception should not already be on
// the stack!
void __llvm_eh_add_uncaught_exception(llvm_exception *E) throw() {
E->Next = UncaughtExceptionStack;
UncaughtExceptionStack = E;
}
// __llvm_eh_get_uncaught_exception - Returns the current uncaught exception.
// There must be an uncaught exception for this to work!
llvm_exception *__llvm_eh_get_uncaught_exception() throw() {
assert(UncaughtExceptionStack && "There are no uncaught exceptions!?!?");
return UncaughtExceptionStack;
}
// __llvm_eh_pop_from_uncaught_stack - Remove the current uncaught exception
// from the top of the stack.
llvm_exception *__llvm_eh_pop_from_uncaught_stack() throw() {
llvm_exception *E = __llvm_eh_get_uncaught_exception();
UncaughtExceptionStack = E->Next;
return E;
}

View File

@@ -52,6 +52,10 @@ enum {
extern "C" { extern "C" {
bool __llvm_eh_has_uncaught_exception() throw(); bool __llvm_eh_has_uncaught_exception() throw();
void *__llvm_eh_current_uncaught_exception_type(unsigned HandlerType) throw(); void *__llvm_eh_current_uncaught_exception_type(unsigned HandlerType) throw();
void __llvm_eh_add_uncaught_exception(llvm_exception *E) throw();
llvm_exception *__llvm_eh_get_uncaught_exception() throw();
llvm_exception *__llvm_eh_pop_from_uncaught_stack() throw();
} }
#endif #endif

View File

@@ -17,44 +17,14 @@
#include <stdio.h> #include <stdio.h>
#endif #endif
//===----------------------------------------------------------------------===//
// Generic exception support
//
// Thread local state for exception handling.
// FIXME: This should really be made thread-local!
//
// LastCaughtException - The last exception caught by this handler. This is for // LastCaughtException - The last exception caught by this handler. This is for
// implementation of _rethrow and _get_last_caught. // implementation of _rethrow and _get_last_caught.
// //
// FIXME: This should be thread-local!
//
static llvm_exception *LastCaughtException = 0; static llvm_exception *LastCaughtException = 0;
// UncaughtExceptionStack - The stack of exceptions currently being thrown.
static llvm_exception *UncaughtExceptionStack = 0;
// __llvm_eh_has_uncaught_exception - This is used to implement
// std::uncaught_exception.
//
bool __llvm_eh_has_uncaught_exception() throw() {
return UncaughtExceptionStack != 0;
}
// __llvm_eh_current_uncaught_exception - This function checks to see if the
// current uncaught exception is of the specified language type. If so, it
// returns a pointer to the exception area data.
//
void *__llvm_eh_current_uncaught_exception_type(unsigned HandlerType) throw() {
assert(UncaughtExceptionStack && "No uncaught exception!");
if (UncaughtExceptionStack->ExceptionType == HandlerType)
return UncaughtExceptionStack+1;
return 0;
}
//===----------------------------------------------------------------------===//
// C++ Specific exception handling support...
//
using namespace __cxxabiv1; using namespace __cxxabiv1;
// __llvm_cxxeh_allocate_exception - This function allocates space for the // __llvm_cxxeh_allocate_exception - This function allocates space for the
@@ -113,8 +83,6 @@ void __llvm_cxxeh_throw(void *ObjectPtr, void *TypeInfoPtr,
llvm_cxx_exception *E = (llvm_cxx_exception *)ObjectPtr - 1; llvm_cxx_exception *E = (llvm_cxx_exception *)ObjectPtr - 1;
E->BaseException.ExceptionDestructor = cxx_destructor; E->BaseException.ExceptionDestructor = cxx_destructor;
E->BaseException.ExceptionType = CXXException; E->BaseException.ExceptionType = CXXException;
E->BaseException.Next = UncaughtExceptionStack;
UncaughtExceptionStack = &E->BaseException;
E->BaseException.HandlerCount = 0; E->BaseException.HandlerCount = 0;
E->BaseException.isRethrown = 0; E->BaseException.isRethrown = 0;
@@ -122,6 +90,8 @@ void __llvm_cxxeh_throw(void *ObjectPtr, void *TypeInfoPtr,
E->ExceptionObjectDestructor = DtorPtr; E->ExceptionObjectDestructor = DtorPtr;
E->UnexpectedHandler = __unexpected_handler; E->UnexpectedHandler = __unexpected_handler;
E->TerminateHandler = __terminate_handler; E->TerminateHandler = __terminate_handler;
__llvm_eh_add_uncaught_exception(&E->BaseException);
} }
@@ -163,16 +133,15 @@ static void *CXXExceptionISA(llvm_cxx_exception *E,
// appropriate, otherwise it returns null. // appropriate, otherwise it returns null.
// //
void *__llvm_cxxeh_current_uncaught_exception_isa(void *CatchType) throw() { void *__llvm_cxxeh_current_uncaught_exception_isa(void *CatchType) throw() {
assert(UncaughtExceptionStack && "No uncaught exception!"); void *EPtr = __llvm_eh_current_uncaught_exception_type(CXXException);
if (UncaughtExceptionStack->ExceptionType != CXXException) if (EPtr == 0) return 0; // If it's not a c++ exception, it doesn't match!
return 0; // If it's not a c++ exception, it doesn't match!
// If it is a C++ exception, use the type info object stored in the exception // If it is a C++ exception, use the type info object stored in the exception
// to see if TypeID matches and, if so, to adjust the exception object // to see if TypeID matches and, if so, to adjust the exception object
// pointer. // pointer.
// //
const std::type_info *Info = (const std::type_info *)CatchType; const std::type_info *Info = (const std::type_info *)CatchType;
return CXXExceptionISA(get_cxx_exception(UncaughtExceptionStack), Info); return CXXExceptionISA((llvm_cxx_exception*)EPtr - 1, Info);
} }
@@ -182,12 +151,8 @@ void *__llvm_cxxeh_current_uncaught_exception_isa(void *CatchType) throw() {
// function must work with foreign exceptions. // function must work with foreign exceptions.
// //
void *__llvm_cxxeh_begin_catch() throw() { void *__llvm_cxxeh_begin_catch() throw() {
llvm_exception *E = UncaughtExceptionStack; llvm_exception *E = __llvm_eh_pop_from_uncaught_stack();
assert(UncaughtExceptionStack && "There are no uncaught exceptions!?!?");
// The exception is now no longer uncaught.
UncaughtExceptionStack = E->Next;
// The exception is now caught. // The exception is now caught.
LastCaughtException = E; LastCaughtException = E;
E->Next = 0; E->Next = 0;
@@ -254,9 +219,9 @@ void __llvm_cxxeh_end_catch(void *Ex) /* might throw */ {
// time. // time.
void __llvm_cxxeh_call_terminate() throw() { void __llvm_cxxeh_call_terminate() throw() {
void (*Handler)(void) = __terminate_handler; void (*Handler)(void) = __terminate_handler;
if (UncaughtExceptionStack) if (__llvm_eh_has_uncaught_exception())
if (UncaughtExceptionStack->ExceptionType == CXXException) if (void *EPtr = __llvm_eh_current_uncaught_exception_type(CXXException))
Handler = get_cxx_exception(UncaughtExceptionStack)->TerminateHandler; Handler = ((llvm_cxx_exception*)EPtr - 1)->TerminateHandler;
__terminate(Handler); __terminate(Handler);
} }
@@ -279,8 +244,7 @@ void __llvm_cxxeh_rethrow() throw() {
// Add the exception to the top of the uncaught stack, to preserve the // Add the exception to the top of the uncaught stack, to preserve the
// invariant that the top of the uncaught stack is the current exception. // invariant that the top of the uncaught stack is the current exception.
E->Next = UncaughtExceptionStack; __llvm_eh_add_uncaught_exception(E);
UncaughtExceptionStack = E;
// Return to the caller, which should perform the unwind now. // Return to the caller, which should perform the unwind now.
} }
@@ -320,8 +284,6 @@ static bool ExceptionSpecificationPermitsException(llvm_exception *E,
// //
void __llvm_cxxeh_check_eh_spec(void *Info, ...) { void __llvm_cxxeh_check_eh_spec(void *Info, ...) {
const std::type_info *TypeInfo = (const std::type_info *)Info; const std::type_info *TypeInfo = (const std::type_info *)Info;
llvm_exception *E = UncaughtExceptionStack;
assert(E && "No uncaught exceptions!");
if (TypeInfo == 0) { // Empty exception specification if (TypeInfo == 0) { // Empty exception specification
// Whatever exception this is, it is not allowed by the (empty) spec, call // Whatever exception this is, it is not allowed by the (empty) spec, call
@@ -337,6 +299,9 @@ void __llvm_cxxeh_check_eh_spec(void *Info, ...) {
} }
} }
llvm_exception *E = __llvm_eh_get_uncaught_exception();
assert(E && "No uncaught exceptions!");
// Check to see if the exception matches one of the types allowed by the // Check to see if the exception matches one of the types allowed by the
// exception specification. If so, return to the caller to have the // exception specification. If so, return to the caller to have the
// exception rethrown. // exception rethrown.
@@ -369,8 +334,7 @@ void __llvm_cxxeh_check_eh_spec(void *Info, ...) {
// Grab the newly caught exception. If this exception is permitted by the // Grab the newly caught exception. If this exception is permitted by the
// specification, allow it to be thrown. // specification, allow it to be thrown.
E = UncaughtExceptionStack; E = __llvm_eh_get_uncaught_exception();
assert(E && "No uncaught exceptions!");
va_start(Args, Info); va_start(Args, Info);
Ok = ExceptionSpecificationPermitsException(E, TypeInfo, Args); Ok = ExceptionSpecificationPermitsException(E, TypeInfo, Args);
@@ -385,8 +349,7 @@ void __llvm_cxxeh_check_eh_spec(void *Info, ...) {
} }
// Grab the new bad_exception... // Grab the new bad_exception...
E = UncaughtExceptionStack; E = __llvm_eh_get_uncaught_exception();
assert(E && "No uncaught exceptions!");
// If it's permitted, allow it to be thrown instead. // If it's permitted, allow it to be thrown instead.
va_start(Args, Info); va_start(Args, Info);

View File

@@ -0,0 +1,57 @@
//===- Exception.cpp - Generic language-independent exceptions ------------===//
//
// This file defines the the shared data structures used by all language
// specific exception handling runtime libraries.
//
//===----------------------------------------------------------------------===//
#include "Exception.h"
#include <cassert>
// Thread local state for exception handling. FIXME: This should really be made
// thread-local!
// UncaughtExceptionStack - The stack of exceptions currently being thrown.
static llvm_exception *UncaughtExceptionStack = 0;
// __llvm_eh_has_uncaught_exception - This is used to implement
// std::uncaught_exception.
//
bool __llvm_eh_has_uncaught_exception() throw() {
return UncaughtExceptionStack != 0;
}
// __llvm_eh_current_uncaught_exception - This function checks to see if the
// current uncaught exception is of the specified language type. If so, it
// returns a pointer to the exception area data.
//
void *__llvm_eh_current_uncaught_exception_type(unsigned HandlerType) throw() {
assert(UncaughtExceptionStack && "No uncaught exception!");
if (UncaughtExceptionStack->ExceptionType == HandlerType)
return UncaughtExceptionStack+1;
return 0;
}
// __llvm_eh_add_uncaught_exception - This adds the specified exception to the
// top of the uncaught exception stack. The exception should not already be on
// the stack!
void __llvm_eh_add_uncaught_exception(llvm_exception *E) throw() {
E->Next = UncaughtExceptionStack;
UncaughtExceptionStack = E;
}
// __llvm_eh_get_uncaught_exception - Returns the current uncaught exception.
// There must be an uncaught exception for this to work!
llvm_exception *__llvm_eh_get_uncaught_exception() throw() {
assert(UncaughtExceptionStack && "There are no uncaught exceptions!?!?");
return UncaughtExceptionStack;
}
// __llvm_eh_pop_from_uncaught_stack - Remove the current uncaught exception
// from the top of the stack.
llvm_exception *__llvm_eh_pop_from_uncaught_stack() throw() {
llvm_exception *E = __llvm_eh_get_uncaught_exception();
UncaughtExceptionStack = E->Next;
return E;
}

View File

@@ -52,6 +52,10 @@ enum {
extern "C" { extern "C" {
bool __llvm_eh_has_uncaught_exception() throw(); bool __llvm_eh_has_uncaught_exception() throw();
void *__llvm_eh_current_uncaught_exception_type(unsigned HandlerType) throw(); void *__llvm_eh_current_uncaught_exception_type(unsigned HandlerType) throw();
void __llvm_eh_add_uncaught_exception(llvm_exception *E) throw();
llvm_exception *__llvm_eh_get_uncaught_exception() throw();
llvm_exception *__llvm_eh_pop_from_uncaught_stack() throw();
} }
#endif #endif