2009-10-30 09:42:31 +08:00
|
|
|
//===--- CGException.cpp - Emit LLVM Code for C++ exceptions --------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This contains code dealing with C++ exception related code generation.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-11-21 07:44:51 +08:00
|
|
|
#include "clang/AST/StmtCXX.h"
|
|
|
|
|
|
|
|
#include "llvm/Intrinsics.h"
|
|
|
|
|
2009-10-30 09:42:31 +08:00
|
|
|
#include "CodeGenFunction.h"
|
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
2009-10-30 10:27:02 +08:00
|
|
|
static llvm::Constant *getAllocateExceptionFn(CodeGenFunction &CGF) {
|
|
|
|
// void *__cxa_allocate_exception(size_t thrown_size);
|
|
|
|
const llvm::Type *SizeTy = CGF.ConvertType(CGF.getContext().getSizeType());
|
|
|
|
std::vector<const llvm::Type*> Args(1, SizeTy);
|
2009-12-10 08:06:18 +08:00
|
|
|
|
|
|
|
const llvm::FunctionType *FTy =
|
2009-10-30 10:27:02 +08:00
|
|
|
llvm::FunctionType::get(llvm::Type::getInt8PtrTy(CGF.getLLVMContext()),
|
|
|
|
Args, false);
|
2009-12-10 08:06:18 +08:00
|
|
|
|
2009-10-30 10:27:02 +08:00
|
|
|
return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_allocate_exception");
|
|
|
|
}
|
|
|
|
|
2009-12-02 15:41:41 +08:00
|
|
|
static llvm::Constant *getFreeExceptionFn(CodeGenFunction &CGF) {
|
|
|
|
// void __cxa_free_exception(void *thrown_exception);
|
|
|
|
const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
|
|
|
|
std::vector<const llvm::Type*> Args(1, Int8PtrTy);
|
2009-12-10 08:06:18 +08:00
|
|
|
|
|
|
|
const llvm::FunctionType *FTy =
|
2009-12-02 15:41:41 +08:00
|
|
|
llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()),
|
|
|
|
Args, false);
|
2009-12-10 08:06:18 +08:00
|
|
|
|
2009-12-02 15:41:41 +08:00
|
|
|
return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_free_exception");
|
|
|
|
}
|
|
|
|
|
2009-10-30 10:27:02 +08:00
|
|
|
static llvm::Constant *getThrowFn(CodeGenFunction &CGF) {
|
2009-12-10 08:06:18 +08:00
|
|
|
// void __cxa_throw(void *thrown_exception, std::type_info *tinfo,
|
2009-12-02 15:41:41 +08:00
|
|
|
// void (*dest) (void *));
|
2009-10-30 10:27:02 +08:00
|
|
|
|
|
|
|
const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
|
|
|
|
std::vector<const llvm::Type*> Args(3, Int8PtrTy);
|
2009-12-10 08:06:18 +08:00
|
|
|
|
|
|
|
const llvm::FunctionType *FTy =
|
2009-11-20 08:56:31 +08:00
|
|
|
llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()),
|
|
|
|
Args, false);
|
2009-12-10 08:06:18 +08:00
|
|
|
|
2009-10-30 10:27:02 +08:00
|
|
|
return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_throw");
|
|
|
|
}
|
|
|
|
|
2009-11-20 08:56:31 +08:00
|
|
|
static llvm::Constant *getReThrowFn(CodeGenFunction &CGF) {
|
2009-12-02 15:41:41 +08:00
|
|
|
// void __cxa_rethrow();
|
2009-11-20 08:56:31 +08:00
|
|
|
|
2009-12-10 08:06:18 +08:00
|
|
|
const llvm::FunctionType *FTy =
|
2009-11-20 08:56:31 +08:00
|
|
|
llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), false);
|
2009-12-10 08:06:18 +08:00
|
|
|
|
2009-11-20 08:56:31 +08:00
|
|
|
return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_rethrow");
|
|
|
|
}
|
|
|
|
|
2009-11-21 07:44:51 +08:00
|
|
|
static llvm::Constant *getBeginCatchFn(CodeGenFunction &CGF) {
|
2009-12-02 15:41:41 +08:00
|
|
|
// void* __cxa_begin_catch();
|
2009-11-21 07:44:51 +08:00
|
|
|
|
|
|
|
const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
|
|
|
|
std::vector<const llvm::Type*> Args(1, Int8PtrTy);
|
2009-12-10 08:06:18 +08:00
|
|
|
|
|
|
|
const llvm::FunctionType *FTy =
|
2009-12-01 11:41:18 +08:00
|
|
|
llvm::FunctionType::get(Int8PtrTy, Args, false);
|
2009-12-10 08:06:18 +08:00
|
|
|
|
2009-11-21 07:44:51 +08:00
|
|
|
return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_begin_catch");
|
|
|
|
}
|
|
|
|
|
|
|
|
static llvm::Constant *getEndCatchFn(CodeGenFunction &CGF) {
|
2009-12-02 15:41:41 +08:00
|
|
|
// void __cxa_end_catch();
|
2009-11-21 07:44:51 +08:00
|
|
|
|
2009-12-10 08:06:18 +08:00
|
|
|
const llvm::FunctionType *FTy =
|
2009-11-21 07:44:51 +08:00
|
|
|
llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), false);
|
2009-12-10 08:06:18 +08:00
|
|
|
|
2009-11-21 07:44:51 +08:00
|
|
|
return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_end_catch");
|
|
|
|
}
|
|
|
|
|
2009-12-08 07:38:24 +08:00
|
|
|
static llvm::Constant *getUnexpectedFn(CodeGenFunction &CGF) {
|
|
|
|
// void __cxa_call_unexepcted(void *thrown_exception);
|
|
|
|
|
|
|
|
const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
|
|
|
|
std::vector<const llvm::Type*> Args(1, Int8PtrTy);
|
2009-12-10 08:06:18 +08:00
|
|
|
|
|
|
|
const llvm::FunctionType *FTy =
|
2009-12-08 07:38:24 +08:00
|
|
|
llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()),
|
|
|
|
Args, false);
|
2009-12-10 08:06:18 +08:00
|
|
|
|
2009-12-08 07:38:24 +08:00
|
|
|
return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_call_unexpected");
|
|
|
|
}
|
|
|
|
|
2009-12-01 11:41:18 +08:00
|
|
|
static llvm::Constant *getUnwindResumeOrRethrowFn(CodeGenFunction &CGF) {
|
|
|
|
const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
|
|
|
|
std::vector<const llvm::Type*> Args(1, Int8PtrTy);
|
2009-12-10 08:06:18 +08:00
|
|
|
|
|
|
|
const llvm::FunctionType *FTy =
|
2009-12-01 11:41:18 +08:00
|
|
|
llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), Args,
|
|
|
|
false);
|
2009-12-10 08:06:18 +08:00
|
|
|
|
2010-02-11 02:49:11 +08:00
|
|
|
if (CGF.CGM.getLangOptions().SjLjExceptions)
|
2009-12-01 11:41:18 +08:00
|
|
|
return CGF.CGM.CreateRuntimeFunction(FTy, "_Unwind_SjLj_Resume");
|
|
|
|
return CGF.CGM.CreateRuntimeFunction(FTy, "_Unwind_Resume_or_Rethrow");
|
|
|
|
}
|
|
|
|
|
2009-12-02 15:41:41 +08:00
|
|
|
static llvm::Constant *getTerminateFn(CodeGenFunction &CGF) {
|
|
|
|
// void __terminate();
|
|
|
|
|
2009-12-10 08:06:18 +08:00
|
|
|
const llvm::FunctionType *FTy =
|
2009-12-02 15:41:41 +08:00
|
|
|
llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), false);
|
2009-12-10 08:06:18 +08:00
|
|
|
|
2009-12-02 15:41:41 +08:00
|
|
|
return CGF.CGM.CreateRuntimeFunction(FTy, "_ZSt9terminatev");
|
|
|
|
}
|
|
|
|
|
2009-12-01 11:41:18 +08:00
|
|
|
// CopyObject - Utility to copy an object. Calls copy constructor as necessary.
|
2009-12-11 08:32:37 +08:00
|
|
|
// DestPtr is casted to the right type.
|
|
|
|
static void CopyObject(CodeGenFunction &CGF, const Expr *E,
|
|
|
|
llvm::Value *DestPtr, llvm::Value *ExceptionPtrPtr) {
|
2009-12-01 11:41:18 +08:00
|
|
|
QualType ObjectType = E->getType();
|
|
|
|
|
|
|
|
// Store the throw exception in the exception object.
|
|
|
|
if (!CGF.hasAggregateLLVMType(ObjectType)) {
|
|
|
|
llvm::Value *Value = CGF.EmitScalarExpr(E);
|
2009-12-11 08:32:37 +08:00
|
|
|
const llvm::Type *ValuePtrTy = Value->getType()->getPointerTo();
|
2009-12-10 08:06:18 +08:00
|
|
|
|
2009-12-11 08:32:37 +08:00
|
|
|
CGF.Builder.CreateStore(Value,
|
|
|
|
CGF.Builder.CreateBitCast(DestPtr, ValuePtrTy));
|
2009-12-01 11:41:18 +08:00
|
|
|
} else {
|
2009-12-11 08:32:37 +08:00
|
|
|
const llvm::Type *Ty = CGF.ConvertType(ObjectType)->getPointerTo();
|
|
|
|
const CXXRecordDecl *RD =
|
|
|
|
cast<CXXRecordDecl>(ObjectType->getAs<RecordType>()->getDecl());
|
|
|
|
|
|
|
|
llvm::Value *This = CGF.Builder.CreateBitCast(DestPtr, Ty);
|
2009-12-01 11:41:18 +08:00
|
|
|
if (RD->hasTrivialCopyConstructor()) {
|
|
|
|
CGF.EmitAggExpr(E, This, false);
|
|
|
|
} else if (CXXConstructorDecl *CopyCtor
|
|
|
|
= RD->getCopyConstructor(CGF.getContext(), 0)) {
|
2009-12-17 14:08:47 +08:00
|
|
|
llvm::Value *CondPtr = 0;
|
2009-12-10 09:52:30 +08:00
|
|
|
if (CGF.Exceptions) {
|
|
|
|
CodeGenFunction::EHCleanupBlock Cleanup(CGF);
|
2009-12-02 15:41:41 +08:00
|
|
|
llvm::Constant *FreeExceptionFn = getFreeExceptionFn(CGF);
|
2009-12-11 08:32:37 +08:00
|
|
|
|
2009-12-17 14:08:47 +08:00
|
|
|
llvm::BasicBlock *CondBlock = CGF.createBasicBlock("cond.free");
|
|
|
|
llvm::BasicBlock *Cont = CGF.createBasicBlock("cont");
|
|
|
|
CondPtr = CGF.CreateTempAlloca(llvm::Type::getInt1Ty(CGF.getLLVMContext()),
|
|
|
|
"doEHfree");
|
|
|
|
|
|
|
|
CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CondPtr),
|
|
|
|
CondBlock, Cont);
|
|
|
|
CGF.EmitBlock(CondBlock);
|
|
|
|
|
2009-12-11 08:32:37 +08:00
|
|
|
// Load the exception pointer.
|
|
|
|
llvm::Value *ExceptionPtr = CGF.Builder.CreateLoad(ExceptionPtrPtr);
|
2009-12-02 15:41:41 +08:00
|
|
|
CGF.Builder.CreateCall(FreeExceptionFn, ExceptionPtr);
|
2009-12-17 14:08:47 +08:00
|
|
|
|
|
|
|
CGF.EmitBlock(Cont);
|
2009-12-02 15:41:41 +08:00
|
|
|
}
|
2009-12-11 08:02:10 +08:00
|
|
|
|
2009-12-17 14:08:47 +08:00
|
|
|
if (CondPtr)
|
|
|
|
CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(CGF.getLLVMContext()),
|
|
|
|
CondPtr);
|
|
|
|
|
2009-12-01 11:41:18 +08:00
|
|
|
llvm::Value *Src = CGF.EmitLValue(E).getAddress();
|
2009-12-17 14:08:47 +08:00
|
|
|
|
|
|
|
if (CondPtr)
|
|
|
|
CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(CGF.getLLVMContext()),
|
|
|
|
CondPtr);
|
2009-12-01 11:41:18 +08:00
|
|
|
|
2009-12-10 07:31:35 +08:00
|
|
|
llvm::BasicBlock *TerminateHandler = CGF.getTerminateHandler();
|
2009-12-24 14:52:05 +08:00
|
|
|
llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest();
|
2009-12-10 07:31:35 +08:00
|
|
|
CGF.setInvokeDest(TerminateHandler);
|
|
|
|
|
2009-12-01 11:41:18 +08:00
|
|
|
// Stolen from EmitClassAggrMemberwiseCopy
|
|
|
|
llvm::Value *Callee = CGF.CGM.GetAddrOfCXXConstructor(CopyCtor,
|
|
|
|
Ctor_Complete);
|
|
|
|
CallArgList CallArgs;
|
|
|
|
CallArgs.push_back(std::make_pair(RValue::get(This),
|
|
|
|
CopyCtor->getThisType(CGF.getContext())));
|
|
|
|
|
|
|
|
// Push the Src ptr.
|
|
|
|
CallArgs.push_back(std::make_pair(RValue::get(Src),
|
|
|
|
CopyCtor->getParamDecl(0)->getType()));
|
2010-02-06 05:31:56 +08:00
|
|
|
const FunctionProtoType *FPT
|
|
|
|
= CopyCtor->getType()->getAs<FunctionProtoType>();
|
|
|
|
CGF.EmitCall(CGF.CGM.getTypes().getFunctionInfo(CallArgs, FPT),
|
2009-12-25 03:25:24 +08:00
|
|
|
Callee, ReturnValueSlot(), CallArgs, CopyCtor);
|
2009-12-10 07:31:35 +08:00
|
|
|
CGF.setInvokeDest(PrevLandingPad);
|
2009-12-01 11:41:18 +08:00
|
|
|
} else
|
2009-12-12 13:05:38 +08:00
|
|
|
llvm_unreachable("uncopyable object");
|
2009-12-01 11:41:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CopyObject - Utility to copy an object. Calls copy constructor as necessary.
|
|
|
|
// N is casted to the right type.
|
|
|
|
static void CopyObject(CodeGenFunction &CGF, QualType ObjectType,
|
2010-01-01 11:20:32 +08:00
|
|
|
bool WasPointer, bool WasPointerReference,
|
|
|
|
llvm::Value *E, llvm::Value *N) {
|
2009-12-01 11:41:18 +08:00
|
|
|
// Store the throw exception in the exception object.
|
2009-12-08 09:29:31 +08:00
|
|
|
if (WasPointer || !CGF.hasAggregateLLVMType(ObjectType)) {
|
2009-12-01 11:41:18 +08:00
|
|
|
llvm::Value *Value = E;
|
2009-12-04 06:38:15 +08:00
|
|
|
if (!WasPointer)
|
|
|
|
Value = CGF.Builder.CreateLoad(Value);
|
2009-12-01 11:41:18 +08:00
|
|
|
const llvm::Type *ValuePtrTy = Value->getType()->getPointerTo(0);
|
2010-01-01 11:20:32 +08:00
|
|
|
if (WasPointerReference) {
|
2010-01-01 10:51:52 +08:00
|
|
|
llvm::Value *Tmp = CGF.CreateTempAlloca(Value->getType(), "catch.param");
|
|
|
|
CGF.Builder.CreateStore(Value, Tmp);
|
|
|
|
Value = Tmp;
|
2010-01-01 11:20:32 +08:00
|
|
|
ValuePtrTy = Value->getType()->getPointerTo(0);
|
|
|
|
}
|
|
|
|
N = CGF.Builder.CreateBitCast(N, ValuePtrTy);
|
2010-01-01 10:51:52 +08:00
|
|
|
CGF.Builder.CreateStore(Value, N);
|
2009-12-01 11:41:18 +08:00
|
|
|
} else {
|
|
|
|
const llvm::Type *Ty = CGF.ConvertType(ObjectType)->getPointerTo(0);
|
|
|
|
const CXXRecordDecl *RD;
|
|
|
|
RD = cast<CXXRecordDecl>(ObjectType->getAs<RecordType>()->getDecl());
|
|
|
|
llvm::Value *This = CGF.Builder.CreateBitCast(N, Ty);
|
|
|
|
if (RD->hasTrivialCopyConstructor()) {
|
|
|
|
CGF.EmitAggregateCopy(This, E, ObjectType);
|
|
|
|
} else if (CXXConstructorDecl *CopyCtor
|
|
|
|
= RD->getCopyConstructor(CGF.getContext(), 0)) {
|
|
|
|
llvm::Value *Src = E;
|
|
|
|
|
|
|
|
// Stolen from EmitClassAggrMemberwiseCopy
|
|
|
|
llvm::Value *Callee = CGF.CGM.GetAddrOfCXXConstructor(CopyCtor,
|
|
|
|
Ctor_Complete);
|
|
|
|
CallArgList CallArgs;
|
|
|
|
CallArgs.push_back(std::make_pair(RValue::get(This),
|
|
|
|
CopyCtor->getThisType(CGF.getContext())));
|
|
|
|
|
|
|
|
// Push the Src ptr.
|
|
|
|
CallArgs.push_back(std::make_pair(RValue::get(Src),
|
|
|
|
CopyCtor->getParamDecl(0)->getType()));
|
2010-02-06 05:31:56 +08:00
|
|
|
|
|
|
|
const FunctionProtoType *FPT
|
|
|
|
= CopyCtor->getType()->getAs<FunctionProtoType>();
|
|
|
|
CGF.EmitCall(CGF.CGM.getTypes().getFunctionInfo(CallArgs, FPT),
|
2009-12-25 03:25:24 +08:00
|
|
|
Callee, ReturnValueSlot(), CallArgs, CopyCtor);
|
2009-12-01 11:41:18 +08:00
|
|
|
} else
|
2009-12-12 13:05:38 +08:00
|
|
|
llvm_unreachable("uncopyable object");
|
2009-12-01 11:41:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-30 09:42:31 +08:00
|
|
|
void CodeGenFunction::EmitCXXThrowExpr(const CXXThrowExpr *E) {
|
2009-10-30 10:27:02 +08:00
|
|
|
if (!E->getSubExpr()) {
|
2009-12-04 09:51:45 +08:00
|
|
|
if (getInvokeDest()) {
|
|
|
|
llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
|
|
|
|
Builder.CreateInvoke(getReThrowFn(*this), Cont, getInvokeDest())
|
|
|
|
->setDoesNotReturn();
|
|
|
|
EmitBlock(Cont);
|
|
|
|
} else
|
|
|
|
Builder.CreateCall(getReThrowFn(*this))->setDoesNotReturn();
|
2009-11-20 08:56:31 +08:00
|
|
|
Builder.CreateUnreachable();
|
|
|
|
|
|
|
|
// Clear the insertion point to indicate we are in unreachable code.
|
|
|
|
Builder.ClearInsertionPoint();
|
2009-10-30 10:27:02 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-12-10 08:06:18 +08:00
|
|
|
|
2009-10-30 10:27:02 +08:00
|
|
|
QualType ThrowType = E->getSubExpr()->getType();
|
2009-12-10 08:06:18 +08:00
|
|
|
|
2009-10-30 10:27:02 +08:00
|
|
|
// Now allocate the exception object.
|
|
|
|
const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
|
|
|
|
uint64_t TypeSize = getContext().getTypeSize(ThrowType) / 8;
|
2009-12-10 08:06:18 +08:00
|
|
|
|
2009-10-30 10:27:02 +08:00
|
|
|
llvm::Constant *AllocExceptionFn = getAllocateExceptionFn(*this);
|
2009-12-10 08:06:18 +08:00
|
|
|
llvm::Value *ExceptionPtr =
|
|
|
|
Builder.CreateCall(AllocExceptionFn,
|
2009-10-30 10:27:02 +08:00
|
|
|
llvm::ConstantInt::get(SizeTy, TypeSize),
|
|
|
|
"exception");
|
2009-12-11 08:32:37 +08:00
|
|
|
|
|
|
|
llvm::Value *ExceptionPtrPtr =
|
|
|
|
CreateTempAlloca(ExceptionPtr->getType(), "exception.ptr");
|
|
|
|
Builder.CreateStore(ExceptionPtr, ExceptionPtrPtr);
|
|
|
|
|
2009-10-30 10:27:02 +08:00
|
|
|
|
2009-12-11 08:32:37 +08:00
|
|
|
CopyObject(*this, E->getSubExpr(), ExceptionPtr, ExceptionPtrPtr);
|
2009-12-10 08:06:18 +08:00
|
|
|
|
2009-10-30 10:27:02 +08:00
|
|
|
// Now throw the exception.
|
|
|
|
const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext());
|
2009-12-17 15:09:17 +08:00
|
|
|
llvm::Constant *TypeInfo = CGM.GetAddrOfRTTIDescriptor(ThrowType);
|
2009-10-30 10:27:02 +08:00
|
|
|
llvm::Constant *Dtor = llvm::Constant::getNullValue(Int8PtrTy);
|
2009-12-10 08:06:18 +08:00
|
|
|
|
2009-12-04 09:51:45 +08:00
|
|
|
if (getInvokeDest()) {
|
|
|
|
llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
|
2009-12-10 08:06:18 +08:00
|
|
|
llvm::InvokeInst *ThrowCall =
|
2009-12-04 09:51:45 +08:00
|
|
|
Builder.CreateInvoke3(getThrowFn(*this), Cont, getInvokeDest(),
|
|
|
|
ExceptionPtr, TypeInfo, Dtor);
|
|
|
|
ThrowCall->setDoesNotReturn();
|
|
|
|
EmitBlock(Cont);
|
|
|
|
} else {
|
2009-12-10 08:06:18 +08:00
|
|
|
llvm::CallInst *ThrowCall =
|
2009-12-04 09:51:45 +08:00
|
|
|
Builder.CreateCall3(getThrowFn(*this), ExceptionPtr, TypeInfo, Dtor);
|
|
|
|
ThrowCall->setDoesNotReturn();
|
|
|
|
}
|
2009-10-30 10:27:02 +08:00
|
|
|
Builder.CreateUnreachable();
|
2009-12-10 08:06:18 +08:00
|
|
|
|
2009-10-30 10:27:02 +08:00
|
|
|
// Clear the insertion point to indicate we are in unreachable code.
|
|
|
|
Builder.ClearInsertionPoint();
|
2009-12-08 04:12:14 +08:00
|
|
|
|
|
|
|
// FIXME: For now, emit a dummy basic block because expr emitters in generally
|
|
|
|
// are not ready to handle emitting expressions at unreachable points.
|
|
|
|
EnsureInsertPoint();
|
2009-10-30 09:42:31 +08:00
|
|
|
}
|
2009-11-21 07:44:51 +08:00
|
|
|
|
2009-12-08 07:38:24 +08:00
|
|
|
void CodeGenFunction::EmitStartEHSpec(const Decl *D) {
|
2010-02-07 07:59:05 +08:00
|
|
|
if (!Exceptions)
|
|
|
|
return;
|
|
|
|
|
2009-12-08 07:38:24 +08:00
|
|
|
const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D);
|
|
|
|
if (FD == 0)
|
|
|
|
return;
|
|
|
|
const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>();
|
|
|
|
if (Proto == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
assert(!Proto->hasAnyExceptionSpec() && "function with parameter pack");
|
|
|
|
|
|
|
|
if (!Proto->hasExceptionSpec())
|
|
|
|
return;
|
|
|
|
|
|
|
|
llvm::Constant *Personality =
|
|
|
|
CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty
|
|
|
|
(VMContext),
|
|
|
|
true),
|
|
|
|
"__gxx_personality_v0");
|
|
|
|
Personality = llvm::ConstantExpr::getBitCast(Personality, PtrToInt8Ty);
|
|
|
|
llvm::Value *llvm_eh_exception =
|
|
|
|
CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
|
|
|
|
llvm::Value *llvm_eh_selector =
|
|
|
|
CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
|
|
|
|
const llvm::IntegerType *Int8Ty;
|
|
|
|
const llvm::PointerType *PtrToInt8Ty;
|
|
|
|
Int8Ty = llvm::Type::getInt8Ty(VMContext);
|
|
|
|
// C string type. Used in lots of places.
|
|
|
|
PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty);
|
|
|
|
llvm::Constant *Null = llvm::ConstantPointerNull::get(PtrToInt8Ty);
|
|
|
|
llvm::SmallVector<llvm::Value*, 8> SelectorArgs;
|
|
|
|
|
|
|
|
llvm::BasicBlock *PrevLandingPad = getInvokeDest();
|
|
|
|
llvm::BasicBlock *EHSpecHandler = createBasicBlock("ehspec.handler");
|
|
|
|
llvm::BasicBlock *Match = createBasicBlock("match");
|
|
|
|
llvm::BasicBlock *Unwind = 0;
|
|
|
|
|
|
|
|
assert(PrevLandingPad == 0 && "EHSpec has invoke context");
|
2009-12-13 09:37:04 +08:00
|
|
|
(void)PrevLandingPad;
|
2009-12-08 07:38:24 +08:00
|
|
|
|
|
|
|
llvm::BasicBlock *Cont = createBasicBlock("cont");
|
|
|
|
|
|
|
|
EmitBranchThroughCleanup(Cont);
|
|
|
|
|
|
|
|
// Emit the statements in the try {} block
|
|
|
|
setInvokeDest(EHSpecHandler);
|
|
|
|
|
|
|
|
EmitBlock(EHSpecHandler);
|
|
|
|
// Exception object
|
|
|
|
llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
|
|
|
|
llvm::Value *RethrowPtr = CreateTempAlloca(Exc->getType(), "_rethrow");
|
|
|
|
|
|
|
|
SelectorArgs.push_back(Exc);
|
|
|
|
SelectorArgs.push_back(Personality);
|
|
|
|
SelectorArgs.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
|
|
|
|
Proto->getNumExceptions()+1));
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < Proto->getNumExceptions(); ++i) {
|
|
|
|
QualType Ty = Proto->getExceptionType(i);
|
2009-12-24 06:04:40 +08:00
|
|
|
QualType ExceptType
|
|
|
|
= Ty.getNonReferenceType().getUnqualifiedType();
|
|
|
|
llvm::Value *EHType = CGM.GetAddrOfRTTIDescriptor(ExceptType);
|
2009-12-08 07:38:24 +08:00
|
|
|
SelectorArgs.push_back(EHType);
|
|
|
|
}
|
|
|
|
if (Proto->getNumExceptions())
|
|
|
|
SelectorArgs.push_back(Null);
|
|
|
|
|
|
|
|
// Find which handler was matched.
|
|
|
|
llvm::Value *Selector
|
|
|
|
= Builder.CreateCall(llvm_eh_selector, SelectorArgs.begin(),
|
|
|
|
SelectorArgs.end(), "selector");
|
|
|
|
if (Proto->getNumExceptions()) {
|
|
|
|
Unwind = createBasicBlock("Unwind");
|
|
|
|
|
|
|
|
Builder.CreateStore(Exc, RethrowPtr);
|
|
|
|
Builder.CreateCondBr(Builder.CreateICmpSLT(Selector,
|
|
|
|
llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
|
|
|
|
0)),
|
|
|
|
Match, Unwind);
|
|
|
|
|
|
|
|
EmitBlock(Match);
|
|
|
|
}
|
|
|
|
Builder.CreateCall(getUnexpectedFn(*this), Exc)->setDoesNotReturn();
|
|
|
|
Builder.CreateUnreachable();
|
|
|
|
|
|
|
|
if (Proto->getNumExceptions()) {
|
|
|
|
EmitBlock(Unwind);
|
|
|
|
Builder.CreateCall(getUnwindResumeOrRethrowFn(*this),
|
|
|
|
Builder.CreateLoad(RethrowPtr));
|
|
|
|
Builder.CreateUnreachable();
|
|
|
|
}
|
|
|
|
|
|
|
|
EmitBlock(Cont);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenFunction::EmitEndEHSpec(const Decl *D) {
|
2010-02-07 07:59:05 +08:00
|
|
|
if (!Exceptions)
|
|
|
|
return;
|
|
|
|
|
2009-12-08 07:38:24 +08:00
|
|
|
const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D);
|
|
|
|
if (FD == 0)
|
|
|
|
return;
|
|
|
|
const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>();
|
|
|
|
if (Proto == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!Proto->hasExceptionSpec())
|
|
|
|
return;
|
|
|
|
|
|
|
|
setInvokeDest(0);
|
|
|
|
}
|
|
|
|
|
2009-11-21 07:44:51 +08:00
|
|
|
void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) {
|
2010-02-19 17:25:03 +08:00
|
|
|
CXXTryStmtInfo Info = EnterCXXTryStmt(S);
|
|
|
|
EmitStmt(S.getTryBlock());
|
|
|
|
ExitCXXTryStmt(S, Info);
|
|
|
|
}
|
|
|
|
|
|
|
|
CodeGenFunction::CXXTryStmtInfo
|
|
|
|
CodeGenFunction::EnterCXXTryStmt(const CXXTryStmt &S) {
|
|
|
|
CXXTryStmtInfo Info;
|
|
|
|
Info.SavedLandingPad = getInvokeDest();
|
|
|
|
Info.HandlerBlock = createBasicBlock("try.handler");
|
|
|
|
Info.FinallyBlock = createBasicBlock("finally");
|
|
|
|
|
|
|
|
PushCleanupBlock(Info.FinallyBlock);
|
|
|
|
setInvokeDest(Info.HandlerBlock);
|
|
|
|
|
|
|
|
return Info;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenFunction::ExitCXXTryStmt(const CXXTryStmt &S,
|
|
|
|
CXXTryStmtInfo TryInfo) {
|
2009-11-21 07:44:51 +08:00
|
|
|
// Pointer to the personality function
|
|
|
|
llvm::Constant *Personality =
|
|
|
|
CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty
|
|
|
|
(VMContext),
|
|
|
|
true),
|
|
|
|
"__gxx_personality_v0");
|
|
|
|
Personality = llvm::ConstantExpr::getBitCast(Personality, PtrToInt8Ty);
|
2009-12-03 03:53:57 +08:00
|
|
|
llvm::Value *llvm_eh_exception =
|
|
|
|
CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
|
|
|
|
llvm::Value *llvm_eh_selector =
|
|
|
|
CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
|
2009-11-21 07:44:51 +08:00
|
|
|
|
2010-02-19 17:25:03 +08:00
|
|
|
llvm::BasicBlock *PrevLandingPad = TryInfo.SavedLandingPad;
|
|
|
|
llvm::BasicBlock *TryHandler = TryInfo.HandlerBlock;
|
|
|
|
llvm::BasicBlock *FinallyBlock = TryInfo.FinallyBlock;
|
2009-12-01 11:41:18 +08:00
|
|
|
llvm::BasicBlock *FinallyRethrow = createBasicBlock("finally.throw");
|
2009-11-21 07:44:51 +08:00
|
|
|
llvm::BasicBlock *FinallyEnd = createBasicBlock("finally.end");
|
|
|
|
|
|
|
|
// Jump to end if there is no exception
|
|
|
|
EmitBranchThroughCleanup(FinallyEnd);
|
|
|
|
|
2009-12-10 06:59:31 +08:00
|
|
|
llvm::BasicBlock *TerminateHandler = getTerminateHandler();
|
2009-12-03 03:53:57 +08:00
|
|
|
|
2009-11-21 07:44:51 +08:00
|
|
|
// Emit the handlers
|
|
|
|
EmitBlock(TryHandler);
|
2009-12-10 08:06:18 +08:00
|
|
|
|
2009-11-21 07:44:51 +08:00
|
|
|
const llvm::IntegerType *Int8Ty;
|
|
|
|
const llvm::PointerType *PtrToInt8Ty;
|
|
|
|
Int8Ty = llvm::Type::getInt8Ty(VMContext);
|
|
|
|
// C string type. Used in lots of places.
|
|
|
|
PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty);
|
2009-12-01 11:41:18 +08:00
|
|
|
llvm::Constant *Null = llvm::ConstantPointerNull::get(PtrToInt8Ty);
|
|
|
|
llvm::SmallVector<llvm::Value*, 8> SelectorArgs;
|
|
|
|
llvm::Value *llvm_eh_typeid_for =
|
|
|
|
CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for);
|
2009-11-21 07:44:51 +08:00
|
|
|
// Exception object
|
2009-12-10 06:59:31 +08:00
|
|
|
llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
|
2009-12-01 11:41:18 +08:00
|
|
|
llvm::Value *RethrowPtr = CreateTempAlloca(Exc->getType(), "_rethrow");
|
2009-11-21 07:44:51 +08:00
|
|
|
|
2009-12-10 06:59:31 +08:00
|
|
|
llvm::SmallVector<llvm::Value*, 8> Args;
|
2009-12-03 03:53:57 +08:00
|
|
|
Args.clear();
|
2009-12-01 11:41:18 +08:00
|
|
|
SelectorArgs.push_back(Exc);
|
|
|
|
SelectorArgs.push_back(Personality);
|
2009-11-21 07:44:51 +08:00
|
|
|
|
2009-12-01 11:41:18 +08:00
|
|
|
bool HasCatchAll = false;
|
2009-11-21 07:44:51 +08:00
|
|
|
for (unsigned i = 0; i<S.getNumHandlers(); ++i) {
|
|
|
|
const CXXCatchStmt *C = S.getHandler(i);
|
2009-12-01 11:41:18 +08:00
|
|
|
VarDecl *CatchParam = C->getExceptionDecl();
|
|
|
|
if (CatchParam) {
|
2009-12-24 06:04:40 +08:00
|
|
|
// C++ [except.handle]p3 indicates that top-level cv-qualifiers
|
|
|
|
// are ignored.
|
|
|
|
QualType CaughtType = C->getCaughtType().getNonReferenceType();
|
2009-12-17 15:09:17 +08:00
|
|
|
llvm::Value *EHTypeInfo
|
2009-12-24 06:04:40 +08:00
|
|
|
= CGM.GetAddrOfRTTIDescriptor(CaughtType.getUnqualifiedType());
|
2009-12-17 15:09:17 +08:00
|
|
|
SelectorArgs.push_back(EHTypeInfo);
|
2009-11-21 07:44:51 +08:00
|
|
|
} else {
|
|
|
|
// null indicates catch all
|
2009-12-01 11:41:18 +08:00
|
|
|
SelectorArgs.push_back(Null);
|
|
|
|
HasCatchAll = true;
|
2009-11-21 07:44:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-01 11:41:18 +08:00
|
|
|
// We use a cleanup unless there was already a catch all.
|
|
|
|
if (!HasCatchAll) {
|
|
|
|
SelectorArgs.push_back(Null);
|
|
|
|
}
|
2009-11-21 07:44:51 +08:00
|
|
|
|
2009-12-01 11:41:18 +08:00
|
|
|
// Find which handler was matched.
|
|
|
|
llvm::Value *Selector
|
|
|
|
= Builder.CreateCall(llvm_eh_selector, SelectorArgs.begin(),
|
|
|
|
SelectorArgs.end(), "selector");
|
2009-11-21 07:44:51 +08:00
|
|
|
for (unsigned i = 0; i<S.getNumHandlers(); ++i) {
|
|
|
|
const CXXCatchStmt *C = S.getHandler(i);
|
2009-12-01 11:41:18 +08:00
|
|
|
VarDecl *CatchParam = C->getExceptionDecl();
|
|
|
|
Stmt *CatchBody = C->getHandlerBlock();
|
|
|
|
|
|
|
|
llvm::BasicBlock *Next = 0;
|
|
|
|
|
|
|
|
if (SelectorArgs[i+2] != Null) {
|
|
|
|
llvm::BasicBlock *Match = createBasicBlock("match");
|
|
|
|
Next = createBasicBlock("catch.next");
|
|
|
|
const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext());
|
|
|
|
llvm::Value *Id
|
|
|
|
= Builder.CreateCall(llvm_eh_typeid_for,
|
|
|
|
Builder.CreateBitCast(SelectorArgs[i+2],
|
|
|
|
Int8PtrTy));
|
|
|
|
Builder.CreateCondBr(Builder.CreateICmpEQ(Selector, Id),
|
|
|
|
Match, Next);
|
|
|
|
EmitBlock(Match);
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::BasicBlock *MatchEnd = createBasicBlock("match.end");
|
|
|
|
llvm::BasicBlock *MatchHandler = createBasicBlock("match.handler");
|
|
|
|
|
|
|
|
PushCleanupBlock(MatchEnd);
|
|
|
|
setInvokeDest(MatchHandler);
|
|
|
|
|
|
|
|
llvm::Value *ExcObject = Builder.CreateCall(getBeginCatchFn(*this), Exc);
|
|
|
|
|
2009-12-03 07:37:16 +08:00
|
|
|
{
|
|
|
|
CleanupScope CatchScope(*this);
|
|
|
|
// Bind the catch parameter if it exists.
|
|
|
|
if (CatchParam) {
|
|
|
|
QualType CatchType = CatchParam->getType().getNonReferenceType();
|
|
|
|
setInvokeDest(TerminateHandler);
|
2009-12-04 06:38:15 +08:00
|
|
|
bool WasPointer = true;
|
2010-01-01 11:20:32 +08:00
|
|
|
bool WasPointerReference = false;
|
2010-01-01 10:51:52 +08:00
|
|
|
CatchType = CGM.getContext().getCanonicalType(CatchType);
|
2010-01-01 11:20:32 +08:00
|
|
|
if (CatchType.getTypePtr()->isPointerType()) {
|
|
|
|
if (isa<ReferenceType>(CatchParam->getType()))
|
|
|
|
WasPointerReference = true;
|
|
|
|
} else {
|
2009-12-04 09:51:45 +08:00
|
|
|
if (!isa<ReferenceType>(CatchParam->getType()))
|
|
|
|
WasPointer = false;
|
2009-12-03 07:37:16 +08:00
|
|
|
CatchType = getContext().getPointerType(CatchType);
|
2009-12-04 06:38:15 +08:00
|
|
|
}
|
2009-12-03 07:37:16 +08:00
|
|
|
ExcObject = Builder.CreateBitCast(ExcObject, ConvertType(CatchType));
|
|
|
|
EmitLocalBlockVarDecl(*CatchParam);
|
2009-12-03 11:40:14 +08:00
|
|
|
// FIXME: we need to do this sooner so that the EH region for the
|
|
|
|
// cleanup doesn't start until after the ctor completes, use a decl
|
|
|
|
// init?
|
2009-12-03 07:37:16 +08:00
|
|
|
CopyObject(*this, CatchParam->getType().getNonReferenceType(),
|
2010-01-01 11:20:32 +08:00
|
|
|
WasPointer, WasPointerReference, ExcObject,
|
2010-01-01 10:51:52 +08:00
|
|
|
GetAddrOfLocalVar(CatchParam));
|
2009-12-03 07:37:16 +08:00
|
|
|
setInvokeDest(MatchHandler);
|
|
|
|
}
|
|
|
|
|
|
|
|
EmitStmt(CatchBody);
|
2009-12-01 11:41:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
EmitBranchThroughCleanup(FinallyEnd);
|
|
|
|
|
|
|
|
EmitBlock(MatchHandler);
|
|
|
|
|
|
|
|
llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
|
|
|
|
// We are required to emit this call to satisfy LLVM, even
|
|
|
|
// though we don't use the result.
|
2009-12-03 03:53:57 +08:00
|
|
|
Args.clear();
|
2009-12-01 11:41:18 +08:00
|
|
|
Args.push_back(Exc);
|
|
|
|
Args.push_back(Personality);
|
|
|
|
Args.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
|
|
|
|
0));
|
|
|
|
Builder.CreateCall(llvm_eh_selector, Args.begin(), Args.end());
|
|
|
|
Builder.CreateStore(Exc, RethrowPtr);
|
|
|
|
EmitBranchThroughCleanup(FinallyRethrow);
|
|
|
|
|
|
|
|
CodeGenFunction::CleanupBlockInfo Info = PopCleanupBlock();
|
|
|
|
|
|
|
|
EmitBlock(MatchEnd);
|
|
|
|
|
2009-12-02 15:41:41 +08:00
|
|
|
llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
|
2009-12-01 11:41:18 +08:00
|
|
|
Builder.CreateInvoke(getEndCatchFn(*this),
|
2009-12-02 15:41:41 +08:00
|
|
|
Cont, TerminateHandler,
|
2009-12-01 11:41:18 +08:00
|
|
|
Args.begin(), Args.begin());
|
|
|
|
EmitBlock(Cont);
|
|
|
|
if (Info.SwitchBlock)
|
|
|
|
EmitBlock(Info.SwitchBlock);
|
|
|
|
if (Info.EndBlock)
|
|
|
|
EmitBlock(Info.EndBlock);
|
|
|
|
|
|
|
|
Exc = Builder.CreateCall(llvm_eh_exception, "exc");
|
|
|
|
Builder.CreateStore(Exc, RethrowPtr);
|
|
|
|
EmitBranchThroughCleanup(FinallyRethrow);
|
|
|
|
|
|
|
|
if (Next)
|
|
|
|
EmitBlock(Next);
|
|
|
|
}
|
2009-12-05 03:03:47 +08:00
|
|
|
if (!HasCatchAll) {
|
|
|
|
Builder.CreateStore(Exc, RethrowPtr);
|
2009-12-01 11:41:18 +08:00
|
|
|
EmitBranchThroughCleanup(FinallyRethrow);
|
2009-12-05 03:03:47 +08:00
|
|
|
}
|
2009-11-21 07:44:51 +08:00
|
|
|
|
|
|
|
CodeGenFunction::CleanupBlockInfo Info = PopCleanupBlock();
|
|
|
|
|
|
|
|
setInvokeDest(PrevLandingPad);
|
|
|
|
|
|
|
|
EmitBlock(FinallyBlock);
|
|
|
|
|
2009-12-01 11:41:18 +08:00
|
|
|
if (Info.SwitchBlock)
|
|
|
|
EmitBlock(Info.SwitchBlock);
|
|
|
|
if (Info.EndBlock)
|
|
|
|
EmitBlock(Info.EndBlock);
|
|
|
|
|
2009-11-21 07:44:51 +08:00
|
|
|
// Branch around the rethrow code.
|
|
|
|
EmitBranch(FinallyEnd);
|
|
|
|
|
|
|
|
EmitBlock(FinallyRethrow);
|
2009-12-05 03:21:57 +08:00
|
|
|
// FIXME: Eventually we can chain the handlers together and just do a call
|
|
|
|
// here.
|
|
|
|
if (getInvokeDest()) {
|
|
|
|
llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
|
|
|
|
Builder.CreateInvoke(getUnwindResumeOrRethrowFn(*this), Cont,
|
|
|
|
getInvokeDest(),
|
|
|
|
Builder.CreateLoad(RethrowPtr));
|
|
|
|
EmitBlock(Cont);
|
|
|
|
} else
|
|
|
|
Builder.CreateCall(getUnwindResumeOrRethrowFn(*this),
|
|
|
|
Builder.CreateLoad(RethrowPtr));
|
|
|
|
|
2009-11-21 07:44:51 +08:00
|
|
|
Builder.CreateUnreachable();
|
|
|
|
|
|
|
|
EmitBlock(FinallyEnd);
|
|
|
|
}
|
2009-12-09 11:35:49 +08:00
|
|
|
|
|
|
|
CodeGenFunction::EHCleanupBlock::~EHCleanupBlock() {
|
|
|
|
llvm::BasicBlock *Cont1 = CGF.createBasicBlock("cont");
|
|
|
|
CGF.EmitBranch(Cont1);
|
|
|
|
CGF.setInvokeDest(PreviousInvokeDest);
|
|
|
|
|
|
|
|
|
|
|
|
CGF.EmitBlock(CleanupHandler);
|
|
|
|
|
|
|
|
llvm::Constant *Personality =
|
|
|
|
CGF.CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty
|
|
|
|
(CGF.VMContext),
|
|
|
|
true),
|
|
|
|
"__gxx_personality_v0");
|
|
|
|
Personality = llvm::ConstantExpr::getBitCast(Personality, CGF.PtrToInt8Ty);
|
|
|
|
llvm::Value *llvm_eh_exception =
|
|
|
|
CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
|
|
|
|
llvm::Value *llvm_eh_selector =
|
|
|
|
CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
|
|
|
|
|
|
|
|
llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
|
|
|
|
const llvm::IntegerType *Int8Ty;
|
|
|
|
const llvm::PointerType *PtrToInt8Ty;
|
|
|
|
Int8Ty = llvm::Type::getInt8Ty(CGF.VMContext);
|
|
|
|
// C string type. Used in lots of places.
|
|
|
|
PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty);
|
|
|
|
llvm::Constant *Null = llvm::ConstantPointerNull::get(PtrToInt8Ty);
|
|
|
|
llvm::SmallVector<llvm::Value*, 8> Args;
|
|
|
|
Args.clear();
|
|
|
|
Args.push_back(Exc);
|
|
|
|
Args.push_back(Personality);
|
|
|
|
Args.push_back(Null);
|
|
|
|
CGF.Builder.CreateCall(llvm_eh_selector, Args.begin(), Args.end());
|
|
|
|
|
|
|
|
CGF.EmitBlock(CleanupEntryBB);
|
|
|
|
|
|
|
|
CGF.EmitBlock(Cont1);
|
|
|
|
|
|
|
|
if (CGF.getInvokeDest()) {
|
|
|
|
llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
|
|
|
|
CGF.Builder.CreateInvoke(getUnwindResumeOrRethrowFn(CGF), Cont,
|
|
|
|
CGF.getInvokeDest(), Exc);
|
|
|
|
CGF.EmitBlock(Cont);
|
|
|
|
} else
|
|
|
|
CGF.Builder.CreateCall(getUnwindResumeOrRethrowFn(CGF), Exc);
|
|
|
|
|
|
|
|
CGF.Builder.CreateUnreachable();
|
|
|
|
|
|
|
|
CGF.EmitBlock(Cont);
|
|
|
|
if (CGF.Exceptions)
|
|
|
|
CGF.setInvokeDest(CleanupHandler);
|
|
|
|
}
|
2009-12-10 06:59:31 +08:00
|
|
|
|
|
|
|
llvm::BasicBlock *CodeGenFunction::getTerminateHandler() {
|
2009-12-10 08:02:42 +08:00
|
|
|
if (TerminateHandler)
|
|
|
|
return TerminateHandler;
|
|
|
|
|
2009-12-10 07:31:35 +08:00
|
|
|
llvm::BasicBlock *Cont = 0;
|
|
|
|
|
|
|
|
if (HaveInsertPoint()) {
|
|
|
|
Cont = createBasicBlock("cont");
|
|
|
|
EmitBranch(Cont);
|
|
|
|
}
|
|
|
|
|
2009-12-10 06:59:31 +08:00
|
|
|
llvm::Constant *Personality =
|
|
|
|
CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty
|
|
|
|
(VMContext),
|
|
|
|
true),
|
|
|
|
"__gxx_personality_v0");
|
|
|
|
Personality = llvm::ConstantExpr::getBitCast(Personality, PtrToInt8Ty);
|
|
|
|
llvm::Value *llvm_eh_exception =
|
|
|
|
CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
|
|
|
|
llvm::Value *llvm_eh_selector =
|
|
|
|
CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
|
|
|
|
|
|
|
|
// Set up terminate handler
|
2009-12-10 08:02:42 +08:00
|
|
|
TerminateHandler = createBasicBlock("terminate.handler");
|
2009-12-10 06:59:31 +08:00
|
|
|
EmitBlock(TerminateHandler);
|
|
|
|
llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
|
|
|
|
// We are required to emit this call to satisfy LLVM, even
|
|
|
|
// though we don't use the result.
|
|
|
|
llvm::SmallVector<llvm::Value*, 8> Args;
|
|
|
|
Args.push_back(Exc);
|
|
|
|
Args.push_back(Personality);
|
|
|
|
Args.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
|
2009-12-10 07:31:35 +08:00
|
|
|
1));
|
2009-12-10 06:59:31 +08:00
|
|
|
Builder.CreateCall(llvm_eh_selector, Args.begin(), Args.end());
|
2009-12-10 08:06:18 +08:00
|
|
|
llvm::CallInst *TerminateCall =
|
2009-12-10 06:59:31 +08:00
|
|
|
Builder.CreateCall(getTerminateFn(*this));
|
|
|
|
TerminateCall->setDoesNotReturn();
|
|
|
|
TerminateCall->setDoesNotThrow();
|
|
|
|
Builder.CreateUnreachable();
|
|
|
|
|
|
|
|
// Clear the insertion point to indicate we are in unreachable code.
|
|
|
|
Builder.ClearInsertionPoint();
|
|
|
|
|
2009-12-10 07:31:35 +08:00
|
|
|
if (Cont)
|
|
|
|
EmitBlock(Cont);
|
|
|
|
|
2009-12-10 06:59:31 +08:00
|
|
|
return TerminateHandler;
|
|
|
|
}
|