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