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