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