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 | |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 14 | #include "clang/AST/StmtCXX.h" |
| 15 | |
| 16 | #include "llvm/Intrinsics.h" |
| 17 | |
Anders Carlsson | 756b5c4 | 2009-10-30 01:42:31 +0000 | [diff] [blame] | 18 | #include "CodeGenFunction.h" |
| 19 | using namespace clang; |
| 20 | using namespace CodeGen; |
| 21 | |
Anders Carlsson | d337929 | 2009-10-30 02:27:02 +0000 | [diff] [blame] | 22 | static llvm::Constant *getAllocateExceptionFn(CodeGenFunction &CGF) { |
| 23 | // void *__cxa_allocate_exception(size_t thrown_size); |
| 24 | const llvm::Type *SizeTy = CGF.ConvertType(CGF.getContext().getSizeType()); |
| 25 | std::vector<const llvm::Type*> Args(1, SizeTy); |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 26 | |
| 27 | const llvm::FunctionType *FTy = |
Anders Carlsson | d337929 | 2009-10-30 02:27:02 +0000 | [diff] [blame] | 28 | llvm::FunctionType::get(llvm::Type::getInt8PtrTy(CGF.getLLVMContext()), |
| 29 | Args, false); |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 30 | |
Anders Carlsson | d337929 | 2009-10-30 02:27:02 +0000 | [diff] [blame] | 31 | return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_allocate_exception"); |
| 32 | } |
| 33 | |
Mike Stump | 9953383 | 2009-12-02 07:41:41 +0000 | [diff] [blame] | 34 | static llvm::Constant *getFreeExceptionFn(CodeGenFunction &CGF) { |
| 35 | // void __cxa_free_exception(void *thrown_exception); |
| 36 | const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); |
| 37 | std::vector<const llvm::Type*> Args(1, Int8PtrTy); |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 38 | |
| 39 | const llvm::FunctionType *FTy = |
Mike Stump | 9953383 | 2009-12-02 07:41:41 +0000 | [diff] [blame] | 40 | llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), |
| 41 | Args, false); |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 42 | |
Mike Stump | 9953383 | 2009-12-02 07:41:41 +0000 | [diff] [blame] | 43 | return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_free_exception"); |
| 44 | } |
| 45 | |
Anders Carlsson | d337929 | 2009-10-30 02:27:02 +0000 | [diff] [blame] | 46 | static llvm::Constant *getThrowFn(CodeGenFunction &CGF) { |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 47 | // void __cxa_throw(void *thrown_exception, std::type_info *tinfo, |
Mike Stump | 9953383 | 2009-12-02 07:41:41 +0000 | [diff] [blame] | 48 | // void (*dest) (void *)); |
Anders Carlsson | d337929 | 2009-10-30 02:27:02 +0000 | [diff] [blame] | 49 | |
| 50 | const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); |
| 51 | std::vector<const llvm::Type*> Args(3, Int8PtrTy); |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 52 | |
| 53 | const llvm::FunctionType *FTy = |
Mike Stump | b4eea69 | 2009-11-20 00:56:31 +0000 | [diff] [blame] | 54 | llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), |
| 55 | Args, false); |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 56 | |
Anders Carlsson | d337929 | 2009-10-30 02:27:02 +0000 | [diff] [blame] | 57 | return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_throw"); |
| 58 | } |
| 59 | |
Mike Stump | b4eea69 | 2009-11-20 00:56:31 +0000 | [diff] [blame] | 60 | static llvm::Constant *getReThrowFn(CodeGenFunction &CGF) { |
Mike Stump | 9953383 | 2009-12-02 07:41:41 +0000 | [diff] [blame] | 61 | // void __cxa_rethrow(); |
Mike Stump | b4eea69 | 2009-11-20 00:56:31 +0000 | [diff] [blame] | 62 | |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 63 | const llvm::FunctionType *FTy = |
Mike Stump | b4eea69 | 2009-11-20 00:56:31 +0000 | [diff] [blame] | 64 | llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), false); |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 65 | |
Mike Stump | b4eea69 | 2009-11-20 00:56:31 +0000 | [diff] [blame] | 66 | return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_rethrow"); |
| 67 | } |
| 68 | |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 69 | static llvm::Constant *getBeginCatchFn(CodeGenFunction &CGF) { |
Mike Stump | 9953383 | 2009-12-02 07:41:41 +0000 | [diff] [blame] | 70 | // void* __cxa_begin_catch(); |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 71 | |
| 72 | const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); |
| 73 | std::vector<const llvm::Type*> Args(1, Int8PtrTy); |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 74 | |
| 75 | const llvm::FunctionType *FTy = |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 76 | llvm::FunctionType::get(Int8PtrTy, Args, false); |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 77 | |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 78 | return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_begin_catch"); |
| 79 | } |
| 80 | |
| 81 | static llvm::Constant *getEndCatchFn(CodeGenFunction &CGF) { |
Mike Stump | 9953383 | 2009-12-02 07:41:41 +0000 | [diff] [blame] | 82 | // void __cxa_end_catch(); |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 83 | |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 84 | const llvm::FunctionType *FTy = |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 85 | llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), false); |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 86 | |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 87 | return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_end_catch"); |
| 88 | } |
| 89 | |
Mike Stump | cce3d4f | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 90 | static llvm::Constant *getUnexpectedFn(CodeGenFunction &CGF) { |
| 91 | // void __cxa_call_unexepcted(void *thrown_exception); |
| 92 | |
| 93 | const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); |
| 94 | std::vector<const llvm::Type*> Args(1, Int8PtrTy); |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 95 | |
| 96 | const llvm::FunctionType *FTy = |
Mike Stump | cce3d4f | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 97 | llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), |
| 98 | Args, false); |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 99 | |
Mike Stump | cce3d4f | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 100 | return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_call_unexpected"); |
| 101 | } |
| 102 | |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 103 | static llvm::Constant *getUnwindResumeOrRethrowFn(CodeGenFunction &CGF) { |
| 104 | const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); |
| 105 | std::vector<const llvm::Type*> Args(1, Int8PtrTy); |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 106 | |
| 107 | const llvm::FunctionType *FTy = |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 108 | llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), Args, |
| 109 | false); |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 110 | |
Daniel Dunbar | b2987d1 | 2010-02-10 18:49:11 +0000 | [diff] [blame] | 111 | if (CGF.CGM.getLangOptions().SjLjExceptions) |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 112 | return CGF.CGM.CreateRuntimeFunction(FTy, "_Unwind_SjLj_Resume"); |
| 113 | return CGF.CGM.CreateRuntimeFunction(FTy, "_Unwind_Resume_or_Rethrow"); |
| 114 | } |
| 115 | |
Mike Stump | 9953383 | 2009-12-02 07:41:41 +0000 | [diff] [blame] | 116 | static llvm::Constant *getTerminateFn(CodeGenFunction &CGF) { |
| 117 | // void __terminate(); |
| 118 | |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 119 | const llvm::FunctionType *FTy = |
Mike Stump | 9953383 | 2009-12-02 07:41:41 +0000 | [diff] [blame] | 120 | llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), false); |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 121 | |
Mike Stump | 9953383 | 2009-12-02 07:41:41 +0000 | [diff] [blame] | 122 | return CGF.CGM.CreateRuntimeFunction(FTy, "_ZSt9terminatev"); |
| 123 | } |
| 124 | |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 125 | // CopyObject - Utility to copy an object. Calls copy constructor as necessary. |
Anders Carlsson | 8370c58 | 2009-12-11 00:32:37 +0000 | [diff] [blame] | 126 | // DestPtr is casted to the right type. |
| 127 | static void CopyObject(CodeGenFunction &CGF, const Expr *E, |
| 128 | llvm::Value *DestPtr, llvm::Value *ExceptionPtrPtr) { |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 129 | QualType ObjectType = E->getType(); |
| 130 | |
| 131 | // Store the throw exception in the exception object. |
| 132 | if (!CGF.hasAggregateLLVMType(ObjectType)) { |
| 133 | llvm::Value *Value = CGF.EmitScalarExpr(E); |
Anders Carlsson | 8370c58 | 2009-12-11 00:32:37 +0000 | [diff] [blame] | 134 | const llvm::Type *ValuePtrTy = Value->getType()->getPointerTo(); |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 135 | |
Anders Carlsson | 8370c58 | 2009-12-11 00:32:37 +0000 | [diff] [blame] | 136 | CGF.Builder.CreateStore(Value, |
| 137 | CGF.Builder.CreateBitCast(DestPtr, ValuePtrTy)); |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 138 | } else { |
Anders Carlsson | 8370c58 | 2009-12-11 00:32:37 +0000 | [diff] [blame] | 139 | const llvm::Type *Ty = CGF.ConvertType(ObjectType)->getPointerTo(); |
| 140 | const CXXRecordDecl *RD = |
| 141 | cast<CXXRecordDecl>(ObjectType->getAs<RecordType>()->getDecl()); |
| 142 | |
| 143 | llvm::Value *This = CGF.Builder.CreateBitCast(DestPtr, Ty); |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 144 | if (RD->hasTrivialCopyConstructor()) { |
| 145 | CGF.EmitAggExpr(E, This, false); |
| 146 | } else if (CXXConstructorDecl *CopyCtor |
| 147 | = RD->getCopyConstructor(CGF.getContext(), 0)) { |
Mike Stump | f2945c0 | 2009-12-17 06:08:47 +0000 | [diff] [blame] | 148 | llvm::Value *CondPtr = 0; |
Mike Stump | 5030a98 | 2009-12-10 01:52:30 +0000 | [diff] [blame] | 149 | if (CGF.Exceptions) { |
| 150 | CodeGenFunction::EHCleanupBlock Cleanup(CGF); |
Mike Stump | 9953383 | 2009-12-02 07:41:41 +0000 | [diff] [blame] | 151 | llvm::Constant *FreeExceptionFn = getFreeExceptionFn(CGF); |
Anders Carlsson | 8370c58 | 2009-12-11 00:32:37 +0000 | [diff] [blame] | 152 | |
Mike Stump | f2945c0 | 2009-12-17 06:08:47 +0000 | [diff] [blame] | 153 | llvm::BasicBlock *CondBlock = CGF.createBasicBlock("cond.free"); |
| 154 | llvm::BasicBlock *Cont = CGF.createBasicBlock("cont"); |
| 155 | CondPtr = CGF.CreateTempAlloca(llvm::Type::getInt1Ty(CGF.getLLVMContext()), |
| 156 | "doEHfree"); |
| 157 | |
| 158 | CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CondPtr), |
| 159 | CondBlock, Cont); |
| 160 | CGF.EmitBlock(CondBlock); |
| 161 | |
Anders Carlsson | 8370c58 | 2009-12-11 00:32:37 +0000 | [diff] [blame] | 162 | // Load the exception pointer. |
| 163 | llvm::Value *ExceptionPtr = CGF.Builder.CreateLoad(ExceptionPtrPtr); |
Mike Stump | 9953383 | 2009-12-02 07:41:41 +0000 | [diff] [blame] | 164 | CGF.Builder.CreateCall(FreeExceptionFn, ExceptionPtr); |
Mike Stump | f2945c0 | 2009-12-17 06:08:47 +0000 | [diff] [blame] | 165 | |
| 166 | CGF.EmitBlock(Cont); |
Mike Stump | 9953383 | 2009-12-02 07:41:41 +0000 | [diff] [blame] | 167 | } |
Mike Stump | 0924a8a | 2009-12-11 00:02:10 +0000 | [diff] [blame] | 168 | |
Mike Stump | f2945c0 | 2009-12-17 06:08:47 +0000 | [diff] [blame] | 169 | if (CondPtr) |
| 170 | CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(CGF.getLLVMContext()), |
| 171 | CondPtr); |
| 172 | |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 173 | llvm::Value *Src = CGF.EmitLValue(E).getAddress(); |
Mike Stump | f2945c0 | 2009-12-17 06:08:47 +0000 | [diff] [blame] | 174 | |
Mike Stump | f2945c0 | 2009-12-17 06:08:47 +0000 | [diff] [blame] | 175 | if (CondPtr) |
| 176 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(CGF.getLLVMContext()), |
| 177 | CondPtr); |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 178 | |
Mike Stump | 7695809 | 2009-12-09 23:31:35 +0000 | [diff] [blame] | 179 | llvm::BasicBlock *TerminateHandler = CGF.getTerminateHandler(); |
Mike Stump | 63df2ae | 2009-12-24 06:52:05 +0000 | [diff] [blame] | 180 | llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest(); |
Mike Stump | 7695809 | 2009-12-09 23:31:35 +0000 | [diff] [blame] | 181 | CGF.setInvokeDest(TerminateHandler); |
| 182 | |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 183 | // Stolen from EmitClassAggrMemberwiseCopy |
| 184 | llvm::Value *Callee = CGF.CGM.GetAddrOfCXXConstructor(CopyCtor, |
| 185 | Ctor_Complete); |
| 186 | CallArgList CallArgs; |
| 187 | CallArgs.push_back(std::make_pair(RValue::get(This), |
| 188 | CopyCtor->getThisType(CGF.getContext()))); |
| 189 | |
| 190 | // Push the Src ptr. |
| 191 | CallArgs.push_back(std::make_pair(RValue::get(Src), |
| 192 | CopyCtor->getParamDecl(0)->getType())); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 193 | const FunctionProtoType *FPT |
| 194 | = CopyCtor->getType()->getAs<FunctionProtoType>(); |
| 195 | CGF.EmitCall(CGF.CGM.getTypes().getFunctionInfo(CallArgs, FPT), |
Anders Carlsson | f3c47c9 | 2009-12-24 19:25:24 +0000 | [diff] [blame] | 196 | Callee, ReturnValueSlot(), CallArgs, CopyCtor); |
Mike Stump | 7695809 | 2009-12-09 23:31:35 +0000 | [diff] [blame] | 197 | CGF.setInvokeDest(PrevLandingPad); |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 198 | } else |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 199 | llvm_unreachable("uncopyable object"); |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | |
| 203 | // CopyObject - Utility to copy an object. Calls copy constructor as necessary. |
| 204 | // N is casted to the right type. |
| 205 | static void CopyObject(CodeGenFunction &CGF, QualType ObjectType, |
Mike Stump | 13b2f92 | 2010-01-01 03:20:32 +0000 | [diff] [blame] | 206 | bool WasPointer, bool WasPointerReference, |
| 207 | llvm::Value *E, llvm::Value *N) { |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 208 | // Store the throw exception in the exception object. |
Mike Stump | b2debeb | 2009-12-08 01:29:31 +0000 | [diff] [blame] | 209 | if (WasPointer || !CGF.hasAggregateLLVMType(ObjectType)) { |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 210 | llvm::Value *Value = E; |
Mike Stump | d9cb7e9 | 2009-12-03 22:38:15 +0000 | [diff] [blame] | 211 | if (!WasPointer) |
| 212 | Value = CGF.Builder.CreateLoad(Value); |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 213 | const llvm::Type *ValuePtrTy = Value->getType()->getPointerTo(0); |
Mike Stump | 13b2f92 | 2010-01-01 03:20:32 +0000 | [diff] [blame] | 214 | if (WasPointerReference) { |
Mike Stump | b606c38 | 2010-01-01 02:51:52 +0000 | [diff] [blame] | 215 | llvm::Value *Tmp = CGF.CreateTempAlloca(Value->getType(), "catch.param"); |
| 216 | CGF.Builder.CreateStore(Value, Tmp); |
| 217 | Value = Tmp; |
Mike Stump | 13b2f92 | 2010-01-01 03:20:32 +0000 | [diff] [blame] | 218 | ValuePtrTy = Value->getType()->getPointerTo(0); |
| 219 | } |
| 220 | N = CGF.Builder.CreateBitCast(N, ValuePtrTy); |
Mike Stump | b606c38 | 2010-01-01 02:51:52 +0000 | [diff] [blame] | 221 | CGF.Builder.CreateStore(Value, N); |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 222 | } else { |
| 223 | const llvm::Type *Ty = CGF.ConvertType(ObjectType)->getPointerTo(0); |
| 224 | const CXXRecordDecl *RD; |
| 225 | RD = cast<CXXRecordDecl>(ObjectType->getAs<RecordType>()->getDecl()); |
| 226 | llvm::Value *This = CGF.Builder.CreateBitCast(N, Ty); |
| 227 | if (RD->hasTrivialCopyConstructor()) { |
| 228 | CGF.EmitAggregateCopy(This, E, ObjectType); |
| 229 | } else if (CXXConstructorDecl *CopyCtor |
| 230 | = RD->getCopyConstructor(CGF.getContext(), 0)) { |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 231 | llvm::Value *Src = E; |
| 232 | |
| 233 | // Stolen from EmitClassAggrMemberwiseCopy |
| 234 | llvm::Value *Callee = CGF.CGM.GetAddrOfCXXConstructor(CopyCtor, |
| 235 | Ctor_Complete); |
| 236 | CallArgList CallArgs; |
| 237 | CallArgs.push_back(std::make_pair(RValue::get(This), |
| 238 | CopyCtor->getThisType(CGF.getContext()))); |
| 239 | |
| 240 | // Push the Src ptr. |
| 241 | CallArgs.push_back(std::make_pair(RValue::get(Src), |
| 242 | CopyCtor->getParamDecl(0)->getType())); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 243 | |
| 244 | const FunctionProtoType *FPT |
| 245 | = CopyCtor->getType()->getAs<FunctionProtoType>(); |
| 246 | CGF.EmitCall(CGF.CGM.getTypes().getFunctionInfo(CallArgs, FPT), |
Anders Carlsson | f3c47c9 | 2009-12-24 19:25:24 +0000 | [diff] [blame] | 247 | Callee, ReturnValueSlot(), CallArgs, CopyCtor); |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 248 | } else |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 249 | llvm_unreachable("uncopyable object"); |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | |
Anders Carlsson | 756b5c4 | 2009-10-30 01:42:31 +0000 | [diff] [blame] | 253 | void CodeGenFunction::EmitCXXThrowExpr(const CXXThrowExpr *E) { |
Anders Carlsson | d337929 | 2009-10-30 02:27:02 +0000 | [diff] [blame] | 254 | if (!E->getSubExpr()) { |
Mike Stump | 0a3816e | 2009-12-04 01:51:45 +0000 | [diff] [blame] | 255 | if (getInvokeDest()) { |
| 256 | llvm::BasicBlock *Cont = createBasicBlock("invoke.cont"); |
| 257 | Builder.CreateInvoke(getReThrowFn(*this), Cont, getInvokeDest()) |
| 258 | ->setDoesNotReturn(); |
| 259 | EmitBlock(Cont); |
| 260 | } else |
| 261 | Builder.CreateCall(getReThrowFn(*this))->setDoesNotReturn(); |
Mike Stump | b4eea69 | 2009-11-20 00:56:31 +0000 | [diff] [blame] | 262 | Builder.CreateUnreachable(); |
| 263 | |
| 264 | // Clear the insertion point to indicate we are in unreachable code. |
| 265 | Builder.ClearInsertionPoint(); |
Anders Carlsson | d337929 | 2009-10-30 02:27:02 +0000 | [diff] [blame] | 266 | return; |
| 267 | } |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 268 | |
Anders Carlsson | d337929 | 2009-10-30 02:27:02 +0000 | [diff] [blame] | 269 | QualType ThrowType = E->getSubExpr()->getType(); |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 270 | |
Anders Carlsson | d337929 | 2009-10-30 02:27:02 +0000 | [diff] [blame] | 271 | // Now allocate the exception object. |
| 272 | const llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); |
John McCall | 3d3ec1c | 2010-04-21 10:05:39 +0000 | [diff] [blame] | 273 | uint64_t TypeSize = getContext().getTypeSizeInChars(ThrowType).getQuantity(); |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 274 | |
Anders Carlsson | d337929 | 2009-10-30 02:27:02 +0000 | [diff] [blame] | 275 | llvm::Constant *AllocExceptionFn = getAllocateExceptionFn(*this); |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 276 | llvm::Value *ExceptionPtr = |
| 277 | Builder.CreateCall(AllocExceptionFn, |
Anders Carlsson | d337929 | 2009-10-30 02:27:02 +0000 | [diff] [blame] | 278 | llvm::ConstantInt::get(SizeTy, TypeSize), |
| 279 | "exception"); |
Anders Carlsson | 8370c58 | 2009-12-11 00:32:37 +0000 | [diff] [blame] | 280 | |
| 281 | llvm::Value *ExceptionPtrPtr = |
| 282 | CreateTempAlloca(ExceptionPtr->getType(), "exception.ptr"); |
| 283 | Builder.CreateStore(ExceptionPtr, ExceptionPtrPtr); |
Anders Carlsson | d337929 | 2009-10-30 02:27:02 +0000 | [diff] [blame] | 284 | |
Anders Carlsson | 8370c58 | 2009-12-11 00:32:37 +0000 | [diff] [blame] | 285 | |
| 286 | CopyObject(*this, E->getSubExpr(), ExceptionPtr, ExceptionPtrPtr); |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 287 | |
Anders Carlsson | d337929 | 2009-10-30 02:27:02 +0000 | [diff] [blame] | 288 | // Now throw the exception. |
| 289 | const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext()); |
Anders Carlsson | 1d7088d | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 290 | llvm::Constant *TypeInfo = CGM.GetAddrOfRTTIDescriptor(ThrowType); |
Anders Carlsson | d337929 | 2009-10-30 02:27:02 +0000 | [diff] [blame] | 291 | llvm::Constant *Dtor = llvm::Constant::getNullValue(Int8PtrTy); |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 292 | |
Mike Stump | 0a3816e | 2009-12-04 01:51:45 +0000 | [diff] [blame] | 293 | if (getInvokeDest()) { |
| 294 | llvm::BasicBlock *Cont = createBasicBlock("invoke.cont"); |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 295 | llvm::InvokeInst *ThrowCall = |
Mike Stump | 0a3816e | 2009-12-04 01:51:45 +0000 | [diff] [blame] | 296 | Builder.CreateInvoke3(getThrowFn(*this), Cont, getInvokeDest(), |
| 297 | ExceptionPtr, TypeInfo, Dtor); |
| 298 | ThrowCall->setDoesNotReturn(); |
| 299 | EmitBlock(Cont); |
| 300 | } else { |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 301 | llvm::CallInst *ThrowCall = |
Mike Stump | 0a3816e | 2009-12-04 01:51:45 +0000 | [diff] [blame] | 302 | Builder.CreateCall3(getThrowFn(*this), ExceptionPtr, TypeInfo, Dtor); |
| 303 | ThrowCall->setDoesNotReturn(); |
| 304 | } |
Anders Carlsson | d337929 | 2009-10-30 02:27:02 +0000 | [diff] [blame] | 305 | Builder.CreateUnreachable(); |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 306 | |
Anders Carlsson | d337929 | 2009-10-30 02:27:02 +0000 | [diff] [blame] | 307 | // Clear the insertion point to indicate we are in unreachable code. |
| 308 | Builder.ClearInsertionPoint(); |
Mike Stump | c2ab486 | 2009-12-07 20:12:14 +0000 | [diff] [blame] | 309 | |
| 310 | // FIXME: For now, emit a dummy basic block because expr emitters in generally |
| 311 | // are not ready to handle emitting expressions at unreachable points. |
| 312 | EnsureInsertPoint(); |
Anders Carlsson | 756b5c4 | 2009-10-30 01:42:31 +0000 | [diff] [blame] | 313 | } |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 314 | |
Mike Stump | cce3d4f | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 315 | void CodeGenFunction::EmitStartEHSpec(const Decl *D) { |
Anders Carlsson | a994ee4 | 2010-02-06 23:59:05 +0000 | [diff] [blame] | 316 | if (!Exceptions) |
| 317 | return; |
| 318 | |
Mike Stump | cce3d4f | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 319 | const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D); |
| 320 | if (FD == 0) |
| 321 | return; |
| 322 | const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>(); |
| 323 | if (Proto == 0) |
| 324 | return; |
| 325 | |
| 326 | assert(!Proto->hasAnyExceptionSpec() && "function with parameter pack"); |
| 327 | |
| 328 | if (!Proto->hasExceptionSpec()) |
| 329 | return; |
| 330 | |
| 331 | llvm::Constant *Personality = |
| 332 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty |
| 333 | (VMContext), |
| 334 | true), |
| 335 | "__gxx_personality_v0"); |
| 336 | Personality = llvm::ConstantExpr::getBitCast(Personality, PtrToInt8Ty); |
| 337 | llvm::Value *llvm_eh_exception = |
| 338 | CGM.getIntrinsic(llvm::Intrinsic::eh_exception); |
| 339 | llvm::Value *llvm_eh_selector = |
| 340 | CGM.getIntrinsic(llvm::Intrinsic::eh_selector); |
| 341 | const llvm::IntegerType *Int8Ty; |
| 342 | const llvm::PointerType *PtrToInt8Ty; |
| 343 | Int8Ty = llvm::Type::getInt8Ty(VMContext); |
| 344 | // C string type. Used in lots of places. |
| 345 | PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty); |
| 346 | llvm::Constant *Null = llvm::ConstantPointerNull::get(PtrToInt8Ty); |
| 347 | llvm::SmallVector<llvm::Value*, 8> SelectorArgs; |
| 348 | |
| 349 | llvm::BasicBlock *PrevLandingPad = getInvokeDest(); |
| 350 | llvm::BasicBlock *EHSpecHandler = createBasicBlock("ehspec.handler"); |
| 351 | llvm::BasicBlock *Match = createBasicBlock("match"); |
| 352 | llvm::BasicBlock *Unwind = 0; |
| 353 | |
| 354 | assert(PrevLandingPad == 0 && "EHSpec has invoke context"); |
Chandler Carruth | 60cfcec | 2009-12-13 01:37:04 +0000 | [diff] [blame] | 355 | (void)PrevLandingPad; |
Mike Stump | cce3d4f | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 356 | |
| 357 | llvm::BasicBlock *Cont = createBasicBlock("cont"); |
| 358 | |
| 359 | EmitBranchThroughCleanup(Cont); |
| 360 | |
| 361 | // Emit the statements in the try {} block |
| 362 | setInvokeDest(EHSpecHandler); |
| 363 | |
| 364 | EmitBlock(EHSpecHandler); |
| 365 | // Exception object |
| 366 | llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc"); |
| 367 | llvm::Value *RethrowPtr = CreateTempAlloca(Exc->getType(), "_rethrow"); |
| 368 | |
| 369 | SelectorArgs.push_back(Exc); |
| 370 | SelectorArgs.push_back(Personality); |
| 371 | SelectorArgs.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), |
| 372 | Proto->getNumExceptions()+1)); |
| 373 | |
| 374 | for (unsigned i = 0; i < Proto->getNumExceptions(); ++i) { |
| 375 | QualType Ty = Proto->getExceptionType(i); |
Douglas Gregor | 154fe98 | 2009-12-23 22:04:40 +0000 | [diff] [blame] | 376 | QualType ExceptType |
| 377 | = Ty.getNonReferenceType().getUnqualifiedType(); |
| 378 | llvm::Value *EHType = CGM.GetAddrOfRTTIDescriptor(ExceptType); |
Mike Stump | cce3d4f | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 379 | SelectorArgs.push_back(EHType); |
| 380 | } |
| 381 | if (Proto->getNumExceptions()) |
| 382 | SelectorArgs.push_back(Null); |
| 383 | |
| 384 | // Find which handler was matched. |
| 385 | llvm::Value *Selector |
| 386 | = Builder.CreateCall(llvm_eh_selector, SelectorArgs.begin(), |
| 387 | SelectorArgs.end(), "selector"); |
| 388 | if (Proto->getNumExceptions()) { |
| 389 | Unwind = createBasicBlock("Unwind"); |
| 390 | |
| 391 | Builder.CreateStore(Exc, RethrowPtr); |
| 392 | Builder.CreateCondBr(Builder.CreateICmpSLT(Selector, |
| 393 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), |
| 394 | 0)), |
| 395 | Match, Unwind); |
| 396 | |
| 397 | EmitBlock(Match); |
| 398 | } |
| 399 | Builder.CreateCall(getUnexpectedFn(*this), Exc)->setDoesNotReturn(); |
| 400 | Builder.CreateUnreachable(); |
| 401 | |
| 402 | if (Proto->getNumExceptions()) { |
| 403 | EmitBlock(Unwind); |
| 404 | Builder.CreateCall(getUnwindResumeOrRethrowFn(*this), |
| 405 | Builder.CreateLoad(RethrowPtr)); |
| 406 | Builder.CreateUnreachable(); |
| 407 | } |
| 408 | |
| 409 | EmitBlock(Cont); |
| 410 | } |
| 411 | |
| 412 | void CodeGenFunction::EmitEndEHSpec(const Decl *D) { |
Anders Carlsson | a994ee4 | 2010-02-06 23:59:05 +0000 | [diff] [blame] | 413 | if (!Exceptions) |
| 414 | return; |
| 415 | |
Mike Stump | cce3d4f | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 416 | const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D); |
| 417 | if (FD == 0) |
| 418 | return; |
| 419 | const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>(); |
| 420 | if (Proto == 0) |
| 421 | return; |
| 422 | |
| 423 | if (!Proto->hasExceptionSpec()) |
| 424 | return; |
| 425 | |
| 426 | setInvokeDest(0); |
| 427 | } |
| 428 | |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 429 | void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) { |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 430 | CXXTryStmtInfo Info = EnterCXXTryStmt(S); |
| 431 | EmitStmt(S.getTryBlock()); |
| 432 | ExitCXXTryStmt(S, Info); |
| 433 | } |
| 434 | |
| 435 | CodeGenFunction::CXXTryStmtInfo |
| 436 | CodeGenFunction::EnterCXXTryStmt(const CXXTryStmt &S) { |
| 437 | CXXTryStmtInfo Info; |
| 438 | Info.SavedLandingPad = getInvokeDest(); |
| 439 | Info.HandlerBlock = createBasicBlock("try.handler"); |
| 440 | Info.FinallyBlock = createBasicBlock("finally"); |
| 441 | |
| 442 | PushCleanupBlock(Info.FinallyBlock); |
| 443 | setInvokeDest(Info.HandlerBlock); |
| 444 | |
| 445 | return Info; |
| 446 | } |
| 447 | |
| 448 | void CodeGenFunction::ExitCXXTryStmt(const CXXTryStmt &S, |
| 449 | CXXTryStmtInfo TryInfo) { |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 450 | // Pointer to the personality function |
| 451 | llvm::Constant *Personality = |
| 452 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty |
| 453 | (VMContext), |
| 454 | true), |
| 455 | "__gxx_personality_v0"); |
| 456 | Personality = llvm::ConstantExpr::getBitCast(Personality, PtrToInt8Ty); |
Mike Stump | 639787c | 2009-12-02 19:53:57 +0000 | [diff] [blame] | 457 | llvm::Value *llvm_eh_exception = |
| 458 | CGM.getIntrinsic(llvm::Intrinsic::eh_exception); |
| 459 | llvm::Value *llvm_eh_selector = |
| 460 | CGM.getIntrinsic(llvm::Intrinsic::eh_selector); |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 461 | |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 462 | llvm::BasicBlock *PrevLandingPad = TryInfo.SavedLandingPad; |
| 463 | llvm::BasicBlock *TryHandler = TryInfo.HandlerBlock; |
| 464 | llvm::BasicBlock *FinallyBlock = TryInfo.FinallyBlock; |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 465 | llvm::BasicBlock *FinallyRethrow = createBasicBlock("finally.throw"); |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 466 | llvm::BasicBlock *FinallyEnd = createBasicBlock("finally.end"); |
| 467 | |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 468 | // Jump to end if there is no exception |
| 469 | EmitBranchThroughCleanup(FinallyEnd); |
| 470 | |
Mike Stump | 9b39c51 | 2009-12-09 22:59:31 +0000 | [diff] [blame] | 471 | llvm::BasicBlock *TerminateHandler = getTerminateHandler(); |
Mike Stump | 639787c | 2009-12-02 19:53:57 +0000 | [diff] [blame] | 472 | |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 473 | // Emit the handlers |
| 474 | EmitBlock(TryHandler); |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 475 | |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 476 | const llvm::IntegerType *Int8Ty; |
| 477 | const llvm::PointerType *PtrToInt8Ty; |
| 478 | Int8Ty = llvm::Type::getInt8Ty(VMContext); |
| 479 | // C string type. Used in lots of places. |
| 480 | PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty); |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 481 | llvm::Constant *Null = llvm::ConstantPointerNull::get(PtrToInt8Ty); |
| 482 | llvm::SmallVector<llvm::Value*, 8> SelectorArgs; |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 483 | llvm::Value *llvm_eh_typeid_for = |
| 484 | CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for); |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 485 | // Exception object |
Mike Stump | 9b39c51 | 2009-12-09 22:59:31 +0000 | [diff] [blame] | 486 | llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc"); |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 487 | llvm::Value *RethrowPtr = CreateTempAlloca(Exc->getType(), "_rethrow"); |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 488 | |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 489 | SelectorArgs.push_back(Exc); |
| 490 | SelectorArgs.push_back(Personality); |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 491 | |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 492 | bool HasCatchAll = false; |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 493 | for (unsigned i = 0; i<S.getNumHandlers(); ++i) { |
| 494 | const CXXCatchStmt *C = S.getHandler(i); |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 495 | VarDecl *CatchParam = C->getExceptionDecl(); |
| 496 | if (CatchParam) { |
Douglas Gregor | 154fe98 | 2009-12-23 22:04:40 +0000 | [diff] [blame] | 497 | // C++ [except.handle]p3 indicates that top-level cv-qualifiers |
| 498 | // are ignored. |
| 499 | QualType CaughtType = C->getCaughtType().getNonReferenceType(); |
Anders Carlsson | 1d7088d | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 500 | llvm::Value *EHTypeInfo |
Douglas Gregor | 154fe98 | 2009-12-23 22:04:40 +0000 | [diff] [blame] | 501 | = CGM.GetAddrOfRTTIDescriptor(CaughtType.getUnqualifiedType()); |
Anders Carlsson | 1d7088d | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 502 | SelectorArgs.push_back(EHTypeInfo); |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 503 | } else { |
| 504 | // null indicates catch all |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 505 | SelectorArgs.push_back(Null); |
| 506 | HasCatchAll = true; |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 507 | } |
| 508 | } |
| 509 | |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 510 | // We use a cleanup unless there was already a catch all. |
| 511 | if (!HasCatchAll) { |
| 512 | SelectorArgs.push_back(Null); |
| 513 | } |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 514 | |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 515 | // Find which handler was matched. |
| 516 | llvm::Value *Selector |
| 517 | = Builder.CreateCall(llvm_eh_selector, SelectorArgs.begin(), |
| 518 | SelectorArgs.end(), "selector"); |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 519 | for (unsigned i = 0; i<S.getNumHandlers(); ++i) { |
| 520 | const CXXCatchStmt *C = S.getHandler(i); |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 521 | VarDecl *CatchParam = C->getExceptionDecl(); |
| 522 | Stmt *CatchBody = C->getHandlerBlock(); |
| 523 | |
| 524 | llvm::BasicBlock *Next = 0; |
| 525 | |
| 526 | if (SelectorArgs[i+2] != Null) { |
| 527 | llvm::BasicBlock *Match = createBasicBlock("match"); |
| 528 | Next = createBasicBlock("catch.next"); |
| 529 | const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext()); |
| 530 | llvm::Value *Id |
| 531 | = Builder.CreateCall(llvm_eh_typeid_for, |
| 532 | Builder.CreateBitCast(SelectorArgs[i+2], |
| 533 | Int8PtrTy)); |
| 534 | Builder.CreateCondBr(Builder.CreateICmpEQ(Selector, Id), |
| 535 | Match, Next); |
| 536 | EmitBlock(Match); |
| 537 | } |
| 538 | |
| 539 | llvm::BasicBlock *MatchEnd = createBasicBlock("match.end"); |
| 540 | llvm::BasicBlock *MatchHandler = createBasicBlock("match.handler"); |
| 541 | |
| 542 | PushCleanupBlock(MatchEnd); |
| 543 | setInvokeDest(MatchHandler); |
| 544 | |
| 545 | llvm::Value *ExcObject = Builder.CreateCall(getBeginCatchFn(*this), Exc); |
| 546 | |
Mike Stump | f7f7467 | 2009-12-02 23:37:16 +0000 | [diff] [blame] | 547 | { |
| 548 | CleanupScope CatchScope(*this); |
| 549 | // Bind the catch parameter if it exists. |
| 550 | if (CatchParam) { |
| 551 | QualType CatchType = CatchParam->getType().getNonReferenceType(); |
| 552 | setInvokeDest(TerminateHandler); |
Mike Stump | d9cb7e9 | 2009-12-03 22:38:15 +0000 | [diff] [blame] | 553 | bool WasPointer = true; |
Mike Stump | 13b2f92 | 2010-01-01 03:20:32 +0000 | [diff] [blame] | 554 | bool WasPointerReference = false; |
Mike Stump | b606c38 | 2010-01-01 02:51:52 +0000 | [diff] [blame] | 555 | CatchType = CGM.getContext().getCanonicalType(CatchType); |
Mike Stump | 13b2f92 | 2010-01-01 03:20:32 +0000 | [diff] [blame] | 556 | if (CatchType.getTypePtr()->isPointerType()) { |
| 557 | if (isa<ReferenceType>(CatchParam->getType())) |
| 558 | WasPointerReference = true; |
| 559 | } else { |
Mike Stump | 0a3816e | 2009-12-04 01:51:45 +0000 | [diff] [blame] | 560 | if (!isa<ReferenceType>(CatchParam->getType())) |
| 561 | WasPointer = false; |
Mike Stump | f7f7467 | 2009-12-02 23:37:16 +0000 | [diff] [blame] | 562 | CatchType = getContext().getPointerType(CatchType); |
Mike Stump | d9cb7e9 | 2009-12-03 22:38:15 +0000 | [diff] [blame] | 563 | } |
Mike Stump | f7f7467 | 2009-12-02 23:37:16 +0000 | [diff] [blame] | 564 | ExcObject = Builder.CreateBitCast(ExcObject, ConvertType(CatchType)); |
Mike Stump | f7f7467 | 2009-12-02 23:37:16 +0000 | [diff] [blame] | 565 | EmitLocalBlockVarDecl(*CatchParam); |
Mike Stump | f668bd0 | 2009-12-03 03:40:14 +0000 | [diff] [blame] | 566 | // FIXME: we need to do this sooner so that the EH region for the |
| 567 | // cleanup doesn't start until after the ctor completes, use a decl |
| 568 | // init? |
Mike Stump | f7f7467 | 2009-12-02 23:37:16 +0000 | [diff] [blame] | 569 | CopyObject(*this, CatchParam->getType().getNonReferenceType(), |
Mike Stump | 13b2f92 | 2010-01-01 03:20:32 +0000 | [diff] [blame] | 570 | WasPointer, WasPointerReference, ExcObject, |
Mike Stump | b606c38 | 2010-01-01 02:51:52 +0000 | [diff] [blame] | 571 | GetAddrOfLocalVar(CatchParam)); |
Mike Stump | f7f7467 | 2009-12-02 23:37:16 +0000 | [diff] [blame] | 572 | setInvokeDest(MatchHandler); |
| 573 | } |
| 574 | |
| 575 | EmitStmt(CatchBody); |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 576 | } |
| 577 | |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 578 | EmitBranchThroughCleanup(FinallyEnd); |
| 579 | |
| 580 | EmitBlock(MatchHandler); |
| 581 | |
| 582 | llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc"); |
| 583 | // We are required to emit this call to satisfy LLVM, even |
| 584 | // though we don't use the result. |
Benjamin Kramer | 7c647a1 | 2010-03-18 16:59:57 +0000 | [diff] [blame] | 585 | llvm::Value *Args[] = { |
| 586 | Exc, Personality, |
| 587 | llvm::ConstantInt::getNullValue(llvm::Type::getInt32Ty(VMContext)) |
| 588 | }; |
| 589 | Builder.CreateCall(llvm_eh_selector, &Args[0], llvm::array_endof(Args)); |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 590 | Builder.CreateStore(Exc, RethrowPtr); |
| 591 | EmitBranchThroughCleanup(FinallyRethrow); |
| 592 | |
| 593 | CodeGenFunction::CleanupBlockInfo Info = PopCleanupBlock(); |
| 594 | |
| 595 | EmitBlock(MatchEnd); |
| 596 | |
Mike Stump | 9953383 | 2009-12-02 07:41:41 +0000 | [diff] [blame] | 597 | llvm::BasicBlock *Cont = createBasicBlock("invoke.cont"); |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 598 | Builder.CreateInvoke(getEndCatchFn(*this), |
Mike Stump | 9953383 | 2009-12-02 07:41:41 +0000 | [diff] [blame] | 599 | Cont, TerminateHandler, |
Benjamin Kramer | 7c647a1 | 2010-03-18 16:59:57 +0000 | [diff] [blame] | 600 | &Args[0], &Args[0]); |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 601 | EmitBlock(Cont); |
| 602 | if (Info.SwitchBlock) |
| 603 | EmitBlock(Info.SwitchBlock); |
| 604 | if (Info.EndBlock) |
| 605 | EmitBlock(Info.EndBlock); |
| 606 | |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 607 | Exc = Builder.CreateCall(llvm_eh_exception, "exc"); |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 608 | Builder.CreateStore(Exc, RethrowPtr); |
| 609 | EmitBranchThroughCleanup(FinallyRethrow); |
| 610 | |
| 611 | if (Next) |
| 612 | EmitBlock(Next); |
| 613 | } |
Mike Stump | a086783 | 2009-12-04 19:03:47 +0000 | [diff] [blame] | 614 | if (!HasCatchAll) { |
| 615 | Builder.CreateStore(Exc, RethrowPtr); |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 616 | EmitBranchThroughCleanup(FinallyRethrow); |
Mike Stump | a086783 | 2009-12-04 19:03:47 +0000 | [diff] [blame] | 617 | } |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 618 | |
| 619 | CodeGenFunction::CleanupBlockInfo Info = PopCleanupBlock(); |
| 620 | |
| 621 | setInvokeDest(PrevLandingPad); |
| 622 | |
| 623 | EmitBlock(FinallyBlock); |
| 624 | |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 625 | if (Info.SwitchBlock) |
| 626 | EmitBlock(Info.SwitchBlock); |
| 627 | if (Info.EndBlock) |
| 628 | EmitBlock(Info.EndBlock); |
| 629 | |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 630 | // Branch around the rethrow code. |
| 631 | EmitBranch(FinallyEnd); |
| 632 | |
| 633 | EmitBlock(FinallyRethrow); |
Mike Stump | b2c9c0b | 2009-12-04 19:21:57 +0000 | [diff] [blame] | 634 | // FIXME: Eventually we can chain the handlers together and just do a call |
| 635 | // here. |
| 636 | if (getInvokeDest()) { |
| 637 | llvm::BasicBlock *Cont = createBasicBlock("invoke.cont"); |
| 638 | Builder.CreateInvoke(getUnwindResumeOrRethrowFn(*this), Cont, |
| 639 | getInvokeDest(), |
| 640 | Builder.CreateLoad(RethrowPtr)); |
| 641 | EmitBlock(Cont); |
| 642 | } else |
| 643 | Builder.CreateCall(getUnwindResumeOrRethrowFn(*this), |
| 644 | Builder.CreateLoad(RethrowPtr)); |
| 645 | |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 646 | Builder.CreateUnreachable(); |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 647 | |
| 648 | EmitBlock(FinallyEnd); |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 649 | } |
Mike Stump | d88ea56 | 2009-12-09 03:35:49 +0000 | [diff] [blame] | 650 | |
| 651 | CodeGenFunction::EHCleanupBlock::~EHCleanupBlock() { |
Mike Stump | d88ea56 | 2009-12-09 03:35:49 +0000 | [diff] [blame] | 652 | CGF.setInvokeDest(PreviousInvokeDest); |
| 653 | |
John McCall | 3d3ec1c | 2010-04-21 10:05:39 +0000 | [diff] [blame] | 654 | llvm::BasicBlock *EndOfCleanup = CGF.Builder.GetInsertBlock(); |
Mike Stump | d88ea56 | 2009-12-09 03:35:49 +0000 | [diff] [blame] | 655 | |
John McCall | 3d3ec1c | 2010-04-21 10:05:39 +0000 | [diff] [blame] | 656 | // Jump to the beginning of the cleanup. |
| 657 | CGF.Builder.SetInsertPoint(CleanupHandler, CleanupHandler->begin()); |
| 658 | |
| 659 | // The libstdc++ personality function. |
| 660 | // TODO: generalize to work with other libraries. |
Mike Stump | d88ea56 | 2009-12-09 03:35:49 +0000 | [diff] [blame] | 661 | llvm::Constant *Personality = |
| 662 | CGF.CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty |
| 663 | (CGF.VMContext), |
| 664 | true), |
| 665 | "__gxx_personality_v0"); |
| 666 | Personality = llvm::ConstantExpr::getBitCast(Personality, CGF.PtrToInt8Ty); |
John McCall | 3d3ec1c | 2010-04-21 10:05:39 +0000 | [diff] [blame] | 667 | |
| 668 | // %exception = call i8* @llvm.eh.exception() |
| 669 | // Magic intrinsic which tells gives us a handle to the caught |
| 670 | // exception. |
Mike Stump | d88ea56 | 2009-12-09 03:35:49 +0000 | [diff] [blame] | 671 | llvm::Value *llvm_eh_exception = |
| 672 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception); |
John McCall | 3d3ec1c | 2010-04-21 10:05:39 +0000 | [diff] [blame] | 673 | llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 674 | |
| 675 | llvm::Constant *Null = llvm::ConstantPointerNull::get(CGF.PtrToInt8Ty); |
| 676 | |
| 677 | // %ignored = call i32 @llvm.eh.selector(i8* %exception, |
| 678 | // i8* @__gxx_personality_v0, |
| 679 | // i8* null) |
| 680 | // Magic intrinsic which tells LLVM that this invoke landing pad is |
| 681 | // just a cleanup block. |
| 682 | llvm::Value *Args[] = { Exc, Personality, Null }; |
Mike Stump | d88ea56 | 2009-12-09 03:35:49 +0000 | [diff] [blame] | 683 | llvm::Value *llvm_eh_selector = |
| 684 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector); |
Benjamin Kramer | 7c647a1 | 2010-03-18 16:59:57 +0000 | [diff] [blame] | 685 | CGF.Builder.CreateCall(llvm_eh_selector, &Args[0], llvm::array_endof(Args)); |
Mike Stump | d88ea56 | 2009-12-09 03:35:49 +0000 | [diff] [blame] | 686 | |
John McCall | 3d3ec1c | 2010-04-21 10:05:39 +0000 | [diff] [blame] | 687 | // And then we fall through into the code that the user put there. |
| 688 | // Jump back to the end of the cleanup. |
| 689 | CGF.Builder.SetInsertPoint(EndOfCleanup); |
Mike Stump | d88ea56 | 2009-12-09 03:35:49 +0000 | [diff] [blame] | 690 | |
John McCall | 3d3ec1c | 2010-04-21 10:05:39 +0000 | [diff] [blame] | 691 | // Rethrow the exception. |
Mike Stump | d88ea56 | 2009-12-09 03:35:49 +0000 | [diff] [blame] | 692 | if (CGF.getInvokeDest()) { |
| 693 | llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont"); |
| 694 | CGF.Builder.CreateInvoke(getUnwindResumeOrRethrowFn(CGF), Cont, |
| 695 | CGF.getInvokeDest(), Exc); |
| 696 | CGF.EmitBlock(Cont); |
| 697 | } else |
| 698 | CGF.Builder.CreateCall(getUnwindResumeOrRethrowFn(CGF), Exc); |
Mike Stump | d88ea56 | 2009-12-09 03:35:49 +0000 | [diff] [blame] | 699 | CGF.Builder.CreateUnreachable(); |
| 700 | |
John McCall | 3d3ec1c | 2010-04-21 10:05:39 +0000 | [diff] [blame] | 701 | // Resume inserting where we started, but put the new cleanup |
| 702 | // handler in place. |
| 703 | CGF.Builder.SetInsertPoint(PreviousInsertionBlock); |
Mike Stump | d88ea56 | 2009-12-09 03:35:49 +0000 | [diff] [blame] | 704 | if (CGF.Exceptions) |
| 705 | CGF.setInvokeDest(CleanupHandler); |
| 706 | } |
Mike Stump | 9b39c51 | 2009-12-09 22:59:31 +0000 | [diff] [blame] | 707 | |
| 708 | llvm::BasicBlock *CodeGenFunction::getTerminateHandler() { |
Mike Stump | 182f383 | 2009-12-10 00:02:42 +0000 | [diff] [blame] | 709 | if (TerminateHandler) |
| 710 | return TerminateHandler; |
| 711 | |
John McCall | 3d3ec1c | 2010-04-21 10:05:39 +0000 | [diff] [blame] | 712 | // We don't want to change anything at the current location, so |
| 713 | // save it aside and clear the insert point. |
| 714 | llvm::BasicBlock *SavedInsertBlock = Builder.GetInsertBlock(); |
| 715 | llvm::BasicBlock::iterator SavedInsertPoint = Builder.GetInsertPoint(); |
| 716 | Builder.ClearInsertionPoint(); |
Mike Stump | 7695809 | 2009-12-09 23:31:35 +0000 | [diff] [blame] | 717 | |
Mike Stump | 9b39c51 | 2009-12-09 22:59:31 +0000 | [diff] [blame] | 718 | llvm::Constant *Personality = |
| 719 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty |
| 720 | (VMContext), |
| 721 | true), |
| 722 | "__gxx_personality_v0"); |
| 723 | Personality = llvm::ConstantExpr::getBitCast(Personality, PtrToInt8Ty); |
| 724 | llvm::Value *llvm_eh_exception = |
| 725 | CGM.getIntrinsic(llvm::Intrinsic::eh_exception); |
| 726 | llvm::Value *llvm_eh_selector = |
| 727 | CGM.getIntrinsic(llvm::Intrinsic::eh_selector); |
| 728 | |
| 729 | // Set up terminate handler |
Mike Stump | 182f383 | 2009-12-10 00:02:42 +0000 | [diff] [blame] | 730 | TerminateHandler = createBasicBlock("terminate.handler"); |
Mike Stump | 9b39c51 | 2009-12-09 22:59:31 +0000 | [diff] [blame] | 731 | EmitBlock(TerminateHandler); |
| 732 | llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc"); |
| 733 | // We are required to emit this call to satisfy LLVM, even |
| 734 | // though we don't use the result. |
Benjamin Kramer | 7c647a1 | 2010-03-18 16:59:57 +0000 | [diff] [blame] | 735 | llvm::Value *Args[] = { |
| 736 | Exc, Personality, |
| 737 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1) |
| 738 | }; |
| 739 | Builder.CreateCall(llvm_eh_selector, &Args[0], llvm::array_endof(Args)); |
Mike Stump | 8755ec3 | 2009-12-10 00:06:18 +0000 | [diff] [blame] | 740 | llvm::CallInst *TerminateCall = |
Mike Stump | 9b39c51 | 2009-12-09 22:59:31 +0000 | [diff] [blame] | 741 | Builder.CreateCall(getTerminateFn(*this)); |
| 742 | TerminateCall->setDoesNotReturn(); |
| 743 | TerminateCall->setDoesNotThrow(); |
| 744 | Builder.CreateUnreachable(); |
| 745 | |
John McCall | 3d3ec1c | 2010-04-21 10:05:39 +0000 | [diff] [blame] | 746 | // Restore the saved insertion state. |
| 747 | Builder.SetInsertPoint(SavedInsertBlock, SavedInsertPoint); |
Mike Stump | 7695809 | 2009-12-09 23:31:35 +0000 | [diff] [blame] | 748 | |
Mike Stump | 9b39c51 | 2009-12-09 22:59:31 +0000 | [diff] [blame] | 749 | return TerminateHandler; |
| 750 | } |