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