Anders Carlsson | 756b5c4 | 2009-10-30 01:42:31 +0000 | [diff] [blame] | 1 | //===--- CGException.cpp - Emit LLVM Code for C++ exceptions --------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This contains code dealing with C++ exception related code generation. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CodeGenFunction.h" |
| 15 | using namespace clang; |
| 16 | using namespace CodeGen; |
| 17 | |
Anders Carlsson | d337929 | 2009-10-30 02:27:02 +0000 | [diff] [blame] | 18 | static llvm::Constant *getAllocateExceptionFn(CodeGenFunction &CGF) { |
| 19 | // void *__cxa_allocate_exception(size_t thrown_size); |
| 20 | const llvm::Type *SizeTy = CGF.ConvertType(CGF.getContext().getSizeType()); |
| 21 | std::vector<const llvm::Type*> Args(1, SizeTy); |
| 22 | |
| 23 | const llvm::FunctionType *FTy = |
| 24 | llvm::FunctionType::get(llvm::Type::getInt8PtrTy(CGF.getLLVMContext()), |
| 25 | Args, false); |
| 26 | |
| 27 | return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_allocate_exception"); |
| 28 | } |
| 29 | |
| 30 | static llvm::Constant *getThrowFn(CodeGenFunction &CGF) { |
| 31 | // void __cxa_throw (void *thrown_exception, std::type_info *tinfo, |
| 32 | // void (*dest) (void *) ); |
| 33 | |
| 34 | const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); |
| 35 | std::vector<const llvm::Type*> Args(3, Int8PtrTy); |
| 36 | |
| 37 | const llvm::FunctionType *FTy = |
| 38 | llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), |
| 39 | Args, false); |
| 40 | |
| 41 | return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_throw"); |
| 42 | } |
| 43 | |
Anders Carlsson | 756b5c4 | 2009-10-30 01:42:31 +0000 | [diff] [blame] | 44 | void CodeGenFunction::EmitCXXThrowExpr(const CXXThrowExpr *E) { |
Anders Carlsson | d337929 | 2009-10-30 02:27:02 +0000 | [diff] [blame] | 45 | // FIXME: Handle rethrows. |
| 46 | if (!E->getSubExpr()) { |
| 47 | ErrorUnsupported(E, "rethrow expression"); |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | QualType ThrowType = E->getSubExpr()->getType(); |
| 52 | // FIXME: We only handle non-class types for now. |
| 53 | if (ThrowType->isRecordType()) { |
| 54 | ErrorUnsupported(E, "throw expression"); |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | // FIXME: Handle cleanup. |
| 59 | if (!CleanupEntries.empty()){ |
| 60 | ErrorUnsupported(E, "throw expression"); |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | // Now allocate the exception object. |
| 65 | const llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); |
| 66 | uint64_t TypeSize = getContext().getTypeSize(ThrowType) / 8; |
| 67 | |
| 68 | llvm::Constant *AllocExceptionFn = getAllocateExceptionFn(*this); |
| 69 | llvm::Value *ExceptionPtr = |
| 70 | Builder.CreateCall(AllocExceptionFn, |
| 71 | llvm::ConstantInt::get(SizeTy, TypeSize), |
| 72 | "exception"); |
| 73 | |
| 74 | // Store the throw exception in the exception object. |
| 75 | if (!hasAggregateLLVMType(ThrowType)) { |
| 76 | llvm::Value *Value = EmitScalarExpr(E->getSubExpr()); |
| 77 | const llvm::Type *ValuePtrTy = Value->getType()->getPointerTo(0); |
| 78 | |
| 79 | Builder.CreateStore(Value, Builder.CreateBitCast(ExceptionPtr, ValuePtrTy)); |
| 80 | } else { |
| 81 | // FIXME: Handle complex and aggregate expressions. |
| 82 | ErrorUnsupported(E, "throw expression"); |
| 83 | } |
| 84 | |
| 85 | // Now throw the exception. |
| 86 | const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext()); |
| 87 | |
| 88 | llvm::SmallString<256> OutName; |
| 89 | llvm::raw_svector_ostream Out(OutName); |
| 90 | mangleCXXRtti(CGM.getMangleContext(), ThrowType, Out); |
| 91 | |
| 92 | // FIXME: Is it OK to use CreateRuntimeVariable for this? |
| 93 | llvm::Constant *TypeInfo = |
| 94 | CGM.CreateRuntimeVariable(llvm::Type::getInt8Ty(getLLVMContext()), |
| 95 | OutName.c_str()); |
| 96 | llvm::Constant *Dtor = llvm::Constant::getNullValue(Int8PtrTy); |
| 97 | |
| 98 | llvm::CallInst *ThrowCall = |
| 99 | Builder.CreateCall3(getThrowFn(*this), ExceptionPtr, TypeInfo, Dtor); |
| 100 | ThrowCall->setDoesNotReturn(); |
| 101 | Builder.CreateUnreachable(); |
| 102 | |
| 103 | // Clear the insertion point to indicate we are in unreachable code. |
| 104 | Builder.ClearInsertionPoint(); |
Anders Carlsson | 756b5c4 | 2009-10-30 01:42:31 +0000 | [diff] [blame] | 105 | } |