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 | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 90 | // FIXME: Eventually this will all go into the backend. Set from the target for |
| 91 | // now. |
| 92 | static int using_sjlj_exceptions = 0; |
| 93 | |
| 94 | static llvm::Constant *getUnwindResumeOrRethrowFn(CodeGenFunction &CGF) { |
| 95 | const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); |
| 96 | std::vector<const llvm::Type*> Args(1, Int8PtrTy); |
| 97 | |
| 98 | const llvm::FunctionType *FTy = |
| 99 | llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), Args, |
| 100 | false); |
| 101 | |
| 102 | if (using_sjlj_exceptions) |
| 103 | return CGF.CGM.CreateRuntimeFunction(FTy, "_Unwind_SjLj_Resume"); |
| 104 | return CGF.CGM.CreateRuntimeFunction(FTy, "_Unwind_Resume_or_Rethrow"); |
| 105 | } |
| 106 | |
Mike Stump | 9953383 | 2009-12-02 07:41:41 +0000 | [diff] [blame] | 107 | static llvm::Constant *getTerminateFn(CodeGenFunction &CGF) { |
| 108 | // void __terminate(); |
| 109 | |
| 110 | const llvm::FunctionType *FTy = |
| 111 | llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), false); |
| 112 | |
| 113 | return CGF.CGM.CreateRuntimeFunction(FTy, "_ZSt9terminatev"); |
| 114 | } |
| 115 | |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 116 | // CopyObject - Utility to copy an object. Calls copy constructor as necessary. |
| 117 | // N is casted to the right type. |
| 118 | static void CopyObject(CodeGenFunction &CGF, const Expr *E, llvm::Value *N) { |
| 119 | QualType ObjectType = E->getType(); |
| 120 | |
| 121 | // Store the throw exception in the exception object. |
| 122 | if (!CGF.hasAggregateLLVMType(ObjectType)) { |
| 123 | llvm::Value *Value = CGF.EmitScalarExpr(E); |
| 124 | const llvm::Type *ValuePtrTy = Value->getType()->getPointerTo(0); |
| 125 | |
| 126 | CGF.Builder.CreateStore(Value, CGF.Builder.CreateBitCast(N, ValuePtrTy)); |
| 127 | } else { |
| 128 | const llvm::Type *Ty = CGF.ConvertType(ObjectType)->getPointerTo(0); |
| 129 | const CXXRecordDecl *RD; |
| 130 | RD = cast<CXXRecordDecl>(ObjectType->getAs<RecordType>()->getDecl()); |
| 131 | llvm::Value *This = CGF.Builder.CreateBitCast(N, Ty); |
| 132 | if (RD->hasTrivialCopyConstructor()) { |
| 133 | CGF.EmitAggExpr(E, This, false); |
| 134 | } else if (CXXConstructorDecl *CopyCtor |
| 135 | = RD->getCopyConstructor(CGF.getContext(), 0)) { |
Mike Stump | 9953383 | 2009-12-02 07:41:41 +0000 | [diff] [blame] | 136 | // All temporaries end before we call __cxa_throw |
Mike Stump | ce03390 | 2009-12-04 03:55:53 +0000 | [diff] [blame^] | 137 | // FIXME: Doesn't work well with eh31.C and PopCXXTemporary |
| 138 | // CodeGenFunction::CleanupScope TryScope(CGF); |
Mike Stump | 9953383 | 2009-12-02 07:41:41 +0000 | [diff] [blame] | 139 | { |
| 140 | // These actions are only on the exceptional edge. |
Mike Stump | ce03390 | 2009-12-04 03:55:53 +0000 | [diff] [blame^] | 141 | #if 0 |
| 142 | // FIXME: Doesn't work well with eh31.C and PopCXXTemporary |
Mike Stump | 9953383 | 2009-12-02 07:41:41 +0000 | [diff] [blame] | 143 | CodeGenFunction::DelayedCleanupBlock Scope(CGF, true); |
| 144 | |
| 145 | llvm::Constant *FreeExceptionFn = getFreeExceptionFn(CGF); |
| 146 | const llvm::Type *Int8PtrTy |
| 147 | = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); |
| 148 | llvm::Value *ExceptionPtr = CGF.Builder.CreateBitCast(N, Int8PtrTy); |
| 149 | CGF.Builder.CreateCall(FreeExceptionFn, ExceptionPtr); |
Mike Stump | ce03390 | 2009-12-04 03:55:53 +0000 | [diff] [blame^] | 150 | #endif |
Mike Stump | 9953383 | 2009-12-02 07:41:41 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 153 | llvm::Value *Src = CGF.EmitLValue(E).getAddress(); |
| 154 | |
| 155 | // Stolen from EmitClassAggrMemberwiseCopy |
| 156 | llvm::Value *Callee = CGF.CGM.GetAddrOfCXXConstructor(CopyCtor, |
| 157 | Ctor_Complete); |
| 158 | CallArgList CallArgs; |
| 159 | CallArgs.push_back(std::make_pair(RValue::get(This), |
| 160 | CopyCtor->getThisType(CGF.getContext()))); |
| 161 | |
| 162 | // Push the Src ptr. |
| 163 | CallArgs.push_back(std::make_pair(RValue::get(Src), |
| 164 | CopyCtor->getParamDecl(0)->getType())); |
| 165 | QualType ResultType = |
| 166 | CopyCtor->getType()->getAs<FunctionType>()->getResultType(); |
| 167 | CGF.EmitCall(CGF.CGM.getTypes().getFunctionInfo(ResultType, CallArgs), |
| 168 | Callee, CallArgs, CopyCtor); |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 169 | } else |
Mike Stump | 9953383 | 2009-12-02 07:41:41 +0000 | [diff] [blame] | 170 | llvm::llvm_unreachable("uncopyable object"); |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
| 174 | // CopyObject - Utility to copy an object. Calls copy constructor as necessary. |
| 175 | // N is casted to the right type. |
| 176 | static void CopyObject(CodeGenFunction &CGF, QualType ObjectType, |
Mike Stump | d9cb7e9 | 2009-12-03 22:38:15 +0000 | [diff] [blame] | 177 | bool WasPointer, llvm::Value *E, llvm::Value *N) { |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 178 | // Store the throw exception in the exception object. |
| 179 | if (!CGF.hasAggregateLLVMType(ObjectType)) { |
| 180 | llvm::Value *Value = E; |
Mike Stump | d9cb7e9 | 2009-12-03 22:38:15 +0000 | [diff] [blame] | 181 | if (!WasPointer) |
| 182 | Value = CGF.Builder.CreateLoad(Value); |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 183 | const llvm::Type *ValuePtrTy = Value->getType()->getPointerTo(0); |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 184 | CGF.Builder.CreateStore(Value, CGF.Builder.CreateBitCast(N, ValuePtrTy)); |
| 185 | } else { |
| 186 | const llvm::Type *Ty = CGF.ConvertType(ObjectType)->getPointerTo(0); |
| 187 | const CXXRecordDecl *RD; |
| 188 | RD = cast<CXXRecordDecl>(ObjectType->getAs<RecordType>()->getDecl()); |
| 189 | llvm::Value *This = CGF.Builder.CreateBitCast(N, Ty); |
| 190 | if (RD->hasTrivialCopyConstructor()) { |
| 191 | CGF.EmitAggregateCopy(This, E, ObjectType); |
| 192 | } else if (CXXConstructorDecl *CopyCtor |
| 193 | = RD->getCopyConstructor(CGF.getContext(), 0)) { |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 194 | llvm::Value *Src = E; |
| 195 | |
| 196 | // Stolen from EmitClassAggrMemberwiseCopy |
| 197 | llvm::Value *Callee = CGF.CGM.GetAddrOfCXXConstructor(CopyCtor, |
| 198 | Ctor_Complete); |
| 199 | CallArgList CallArgs; |
| 200 | CallArgs.push_back(std::make_pair(RValue::get(This), |
| 201 | CopyCtor->getThisType(CGF.getContext()))); |
| 202 | |
| 203 | // Push the Src ptr. |
| 204 | CallArgs.push_back(std::make_pair(RValue::get(Src), |
| 205 | CopyCtor->getParamDecl(0)->getType())); |
| 206 | QualType ResultType = |
| 207 | CopyCtor->getType()->getAs<FunctionType>()->getResultType(); |
| 208 | CGF.EmitCall(CGF.CGM.getTypes().getFunctionInfo(ResultType, CallArgs), |
| 209 | Callee, CallArgs, CopyCtor); |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 210 | } else |
| 211 | llvm::llvm_unreachable("uncopyable object"); |
| 212 | } |
| 213 | } |
| 214 | |
Anders Carlsson | 756b5c4 | 2009-10-30 01:42:31 +0000 | [diff] [blame] | 215 | void CodeGenFunction::EmitCXXThrowExpr(const CXXThrowExpr *E) { |
Anders Carlsson | d337929 | 2009-10-30 02:27:02 +0000 | [diff] [blame] | 216 | if (!E->getSubExpr()) { |
Mike Stump | 0a3816e | 2009-12-04 01:51:45 +0000 | [diff] [blame] | 217 | if (getInvokeDest()) { |
| 218 | llvm::BasicBlock *Cont = createBasicBlock("invoke.cont"); |
| 219 | Builder.CreateInvoke(getReThrowFn(*this), Cont, getInvokeDest()) |
| 220 | ->setDoesNotReturn(); |
| 221 | EmitBlock(Cont); |
| 222 | } else |
| 223 | Builder.CreateCall(getReThrowFn(*this))->setDoesNotReturn(); |
Mike Stump | b4eea69 | 2009-11-20 00:56:31 +0000 | [diff] [blame] | 224 | Builder.CreateUnreachable(); |
| 225 | |
| 226 | // Clear the insertion point to indicate we are in unreachable code. |
| 227 | Builder.ClearInsertionPoint(); |
Anders Carlsson | d337929 | 2009-10-30 02:27:02 +0000 | [diff] [blame] | 228 | return; |
| 229 | } |
| 230 | |
| 231 | QualType ThrowType = E->getSubExpr()->getType(); |
Anders Carlsson | d337929 | 2009-10-30 02:27:02 +0000 | [diff] [blame] | 232 | |
| 233 | // Now allocate the exception object. |
| 234 | const llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); |
| 235 | uint64_t TypeSize = getContext().getTypeSize(ThrowType) / 8; |
| 236 | |
| 237 | llvm::Constant *AllocExceptionFn = getAllocateExceptionFn(*this); |
| 238 | llvm::Value *ExceptionPtr = |
| 239 | Builder.CreateCall(AllocExceptionFn, |
| 240 | llvm::ConstantInt::get(SizeTy, TypeSize), |
| 241 | "exception"); |
| 242 | |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 243 | CopyObject(*this, E->getSubExpr(), ExceptionPtr); |
Anders Carlsson | d337929 | 2009-10-30 02:27:02 +0000 | [diff] [blame] | 244 | |
| 245 | // Now throw the exception. |
| 246 | const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext()); |
Mike Stump | de05057 | 2009-12-02 18:57:08 +0000 | [diff] [blame] | 247 | llvm::Constant *TypeInfo = CGM.GenerateRTTI(ThrowType); |
Anders Carlsson | d337929 | 2009-10-30 02:27:02 +0000 | [diff] [blame] | 248 | llvm::Constant *Dtor = llvm::Constant::getNullValue(Int8PtrTy); |
| 249 | |
Mike Stump | 0a3816e | 2009-12-04 01:51:45 +0000 | [diff] [blame] | 250 | if (getInvokeDest()) { |
| 251 | llvm::BasicBlock *Cont = createBasicBlock("invoke.cont"); |
| 252 | llvm::InvokeInst *ThrowCall = |
| 253 | Builder.CreateInvoke3(getThrowFn(*this), Cont, getInvokeDest(), |
| 254 | ExceptionPtr, TypeInfo, Dtor); |
| 255 | ThrowCall->setDoesNotReturn(); |
| 256 | EmitBlock(Cont); |
| 257 | } else { |
| 258 | llvm::CallInst *ThrowCall = |
| 259 | Builder.CreateCall3(getThrowFn(*this), ExceptionPtr, TypeInfo, Dtor); |
| 260 | ThrowCall->setDoesNotReturn(); |
| 261 | } |
Anders Carlsson | d337929 | 2009-10-30 02:27:02 +0000 | [diff] [blame] | 262 | Builder.CreateUnreachable(); |
| 263 | |
| 264 | // Clear the insertion point to indicate we are in unreachable code. |
| 265 | Builder.ClearInsertionPoint(); |
Anders Carlsson | 756b5c4 | 2009-10-30 01:42:31 +0000 | [diff] [blame] | 266 | } |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 267 | |
| 268 | void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) { |
Mike Stump | c88b673 | 2009-12-02 18:20:18 +0000 | [diff] [blame] | 269 | if (1) { |
| 270 | EmitStmt(S.getTryBlock()); |
| 271 | return; |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 272 | } |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 273 | // 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] | 274 | // Pointer to the personality function |
| 275 | llvm::Constant *Personality = |
| 276 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty |
| 277 | (VMContext), |
| 278 | true), |
| 279 | "__gxx_personality_v0"); |
| 280 | Personality = llvm::ConstantExpr::getBitCast(Personality, PtrToInt8Ty); |
Mike Stump | 639787c | 2009-12-02 19:53:57 +0000 | [diff] [blame] | 281 | llvm::Value *llvm_eh_exception = |
| 282 | CGM.getIntrinsic(llvm::Intrinsic::eh_exception); |
| 283 | llvm::Value *llvm_eh_selector = |
| 284 | CGM.getIntrinsic(llvm::Intrinsic::eh_selector); |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 285 | |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 286 | llvm::BasicBlock *PrevLandingPad = getInvokeDest(); |
| 287 | llvm::BasicBlock *TryHandler = createBasicBlock("try.handler"); |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 288 | llvm::BasicBlock *FinallyBlock = createBasicBlock("finally"); |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 289 | llvm::BasicBlock *FinallyRethrow = createBasicBlock("finally.throw"); |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 290 | llvm::BasicBlock *FinallyEnd = createBasicBlock("finally.end"); |
| 291 | |
| 292 | // Push an EH context entry, used for handling rethrows. |
| 293 | PushCleanupBlock(FinallyBlock); |
| 294 | |
| 295 | // Emit the statements in the try {} block |
| 296 | setInvokeDest(TryHandler); |
| 297 | |
| 298 | EmitStmt(S.getTryBlock()); |
| 299 | |
| 300 | // Jump to end if there is no exception |
| 301 | EmitBranchThroughCleanup(FinallyEnd); |
| 302 | |
Mike Stump | 639787c | 2009-12-02 19:53:57 +0000 | [diff] [blame] | 303 | // Set up terminate handler |
| 304 | llvm::BasicBlock *TerminateHandler = createBasicBlock("terminate.handler"); |
| 305 | EmitBlock(TerminateHandler); |
| 306 | llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc"); |
| 307 | // We are required to emit this call to satisfy LLVM, even |
| 308 | // though we don't use the result. |
| 309 | llvm::SmallVector<llvm::Value*, 8> Args; |
| 310 | Args.clear(); |
| 311 | Args.push_back(Exc); |
| 312 | Args.push_back(Personality); |
| 313 | Args.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), |
| 314 | 0)); |
| 315 | Builder.CreateCall(llvm_eh_selector, Args.begin(), Args.end()); |
| 316 | llvm::CallInst *TerminateCall = |
| 317 | Builder.CreateCall(getTerminateFn(*this)); |
| 318 | TerminateCall->setDoesNotReturn(); |
| 319 | TerminateCall->setDoesNotThrow(); |
| 320 | Builder.CreateUnreachable(); |
| 321 | |
| 322 | // Clear the insertion point to indicate we are in unreachable code. |
| 323 | Builder.ClearInsertionPoint(); |
| 324 | |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 325 | // Emit the handlers |
| 326 | EmitBlock(TryHandler); |
| 327 | |
| 328 | const llvm::IntegerType *Int8Ty; |
| 329 | const llvm::PointerType *PtrToInt8Ty; |
| 330 | Int8Ty = llvm::Type::getInt8Ty(VMContext); |
| 331 | // C string type. Used in lots of places. |
| 332 | PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty); |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 333 | llvm::Constant *Null = llvm::ConstantPointerNull::get(PtrToInt8Ty); |
| 334 | llvm::SmallVector<llvm::Value*, 8> SelectorArgs; |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 335 | llvm::Value *llvm_eh_typeid_for = |
| 336 | CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for); |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 337 | // Exception object |
Mike Stump | 639787c | 2009-12-02 19:53:57 +0000 | [diff] [blame] | 338 | Exc = Builder.CreateCall(llvm_eh_exception, "exc"); |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 339 | llvm::Value *RethrowPtr = CreateTempAlloca(Exc->getType(), "_rethrow"); |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 340 | |
Mike Stump | 639787c | 2009-12-02 19:53:57 +0000 | [diff] [blame] | 341 | Args.clear(); |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 342 | SelectorArgs.push_back(Exc); |
| 343 | SelectorArgs.push_back(Personality); |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 344 | |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 345 | bool HasCatchAll = false; |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 346 | for (unsigned i = 0; i<S.getNumHandlers(); ++i) { |
| 347 | const CXXCatchStmt *C = S.getHandler(i); |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 348 | VarDecl *CatchParam = C->getExceptionDecl(); |
| 349 | if (CatchParam) { |
Mike Stump | de05057 | 2009-12-02 18:57:08 +0000 | [diff] [blame] | 350 | llvm::Value *EHType |
| 351 | = CGM.GenerateRTTI(C->getCaughtType().getNonReferenceType()); |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 352 | SelectorArgs.push_back(EHType); |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 353 | } else { |
| 354 | // null indicates catch all |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 355 | SelectorArgs.push_back(Null); |
| 356 | HasCatchAll = true; |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 360 | // We use a cleanup unless there was already a catch all. |
| 361 | if (!HasCatchAll) { |
| 362 | SelectorArgs.push_back(Null); |
| 363 | } |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 364 | |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 365 | // Find which handler was matched. |
| 366 | llvm::Value *Selector |
| 367 | = Builder.CreateCall(llvm_eh_selector, SelectorArgs.begin(), |
| 368 | SelectorArgs.end(), "selector"); |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 369 | for (unsigned i = 0; i<S.getNumHandlers(); ++i) { |
| 370 | const CXXCatchStmt *C = S.getHandler(i); |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 371 | VarDecl *CatchParam = C->getExceptionDecl(); |
| 372 | Stmt *CatchBody = C->getHandlerBlock(); |
| 373 | |
| 374 | llvm::BasicBlock *Next = 0; |
| 375 | |
| 376 | if (SelectorArgs[i+2] != Null) { |
| 377 | llvm::BasicBlock *Match = createBasicBlock("match"); |
| 378 | Next = createBasicBlock("catch.next"); |
| 379 | const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext()); |
| 380 | llvm::Value *Id |
| 381 | = Builder.CreateCall(llvm_eh_typeid_for, |
| 382 | Builder.CreateBitCast(SelectorArgs[i+2], |
| 383 | Int8PtrTy)); |
| 384 | Builder.CreateCondBr(Builder.CreateICmpEQ(Selector, Id), |
| 385 | Match, Next); |
| 386 | EmitBlock(Match); |
| 387 | } |
| 388 | |
| 389 | llvm::BasicBlock *MatchEnd = createBasicBlock("match.end"); |
| 390 | llvm::BasicBlock *MatchHandler = createBasicBlock("match.handler"); |
| 391 | |
| 392 | PushCleanupBlock(MatchEnd); |
| 393 | setInvokeDest(MatchHandler); |
| 394 | |
| 395 | llvm::Value *ExcObject = Builder.CreateCall(getBeginCatchFn(*this), Exc); |
| 396 | |
Mike Stump | f7f7467 | 2009-12-02 23:37:16 +0000 | [diff] [blame] | 397 | { |
| 398 | CleanupScope CatchScope(*this); |
| 399 | // Bind the catch parameter if it exists. |
| 400 | if (CatchParam) { |
| 401 | QualType CatchType = CatchParam->getType().getNonReferenceType(); |
| 402 | setInvokeDest(TerminateHandler); |
Mike Stump | d9cb7e9 | 2009-12-03 22:38:15 +0000 | [diff] [blame] | 403 | bool WasPointer = true; |
| 404 | if (!CatchType.getTypePtr()->isPointerType()) { |
Mike Stump | 0a3816e | 2009-12-04 01:51:45 +0000 | [diff] [blame] | 405 | if (!isa<ReferenceType>(CatchParam->getType())) |
| 406 | WasPointer = false; |
Mike Stump | f7f7467 | 2009-12-02 23:37:16 +0000 | [diff] [blame] | 407 | CatchType = getContext().getPointerType(CatchType); |
Mike Stump | d9cb7e9 | 2009-12-03 22:38:15 +0000 | [diff] [blame] | 408 | } |
Mike Stump | f7f7467 | 2009-12-02 23:37:16 +0000 | [diff] [blame] | 409 | ExcObject = Builder.CreateBitCast(ExcObject, ConvertType(CatchType)); |
Mike Stump | f7f7467 | 2009-12-02 23:37:16 +0000 | [diff] [blame] | 410 | EmitLocalBlockVarDecl(*CatchParam); |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 411 | #if 0 |
Mike Stump | f7f7467 | 2009-12-02 23:37:16 +0000 | [diff] [blame] | 412 | // FIXME: objects with ctors, references |
| 413 | Builder.CreateStore(ExcObject, GetAddrOfLocalVar(CatchParam)); |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 414 | #else |
Mike Stump | f668bd0 | 2009-12-03 03:40:14 +0000 | [diff] [blame] | 415 | // FIXME: we need to do this sooner so that the EH region for the |
| 416 | // cleanup doesn't start until after the ctor completes, use a decl |
| 417 | // init? |
Mike Stump | f7f7467 | 2009-12-02 23:37:16 +0000 | [diff] [blame] | 418 | CopyObject(*this, CatchParam->getType().getNonReferenceType(), |
Mike Stump | d9cb7e9 | 2009-12-03 22:38:15 +0000 | [diff] [blame] | 419 | WasPointer, ExcObject, GetAddrOfLocalVar(CatchParam)); |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 420 | #endif |
Mike Stump | f7f7467 | 2009-12-02 23:37:16 +0000 | [diff] [blame] | 421 | setInvokeDest(MatchHandler); |
| 422 | } |
| 423 | |
| 424 | EmitStmt(CatchBody); |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 425 | } |
| 426 | |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 427 | EmitBranchThroughCleanup(FinallyEnd); |
| 428 | |
| 429 | EmitBlock(MatchHandler); |
| 430 | |
| 431 | llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc"); |
| 432 | // We are required to emit this call to satisfy LLVM, even |
| 433 | // though we don't use the result. |
Mike Stump | 639787c | 2009-12-02 19:53:57 +0000 | [diff] [blame] | 434 | Args.clear(); |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 435 | Args.push_back(Exc); |
| 436 | Args.push_back(Personality); |
| 437 | Args.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), |
| 438 | 0)); |
| 439 | Builder.CreateCall(llvm_eh_selector, Args.begin(), Args.end()); |
| 440 | Builder.CreateStore(Exc, RethrowPtr); |
| 441 | EmitBranchThroughCleanup(FinallyRethrow); |
| 442 | |
| 443 | CodeGenFunction::CleanupBlockInfo Info = PopCleanupBlock(); |
| 444 | |
| 445 | EmitBlock(MatchEnd); |
| 446 | |
Mike Stump | 9953383 | 2009-12-02 07:41:41 +0000 | [diff] [blame] | 447 | llvm::BasicBlock *Cont = createBasicBlock("invoke.cont"); |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 448 | Builder.CreateInvoke(getEndCatchFn(*this), |
Mike Stump | 9953383 | 2009-12-02 07:41:41 +0000 | [diff] [blame] | 449 | Cont, TerminateHandler, |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 450 | Args.begin(), Args.begin()); |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 451 | EmitBlock(Cont); |
| 452 | if (Info.SwitchBlock) |
| 453 | EmitBlock(Info.SwitchBlock); |
| 454 | if (Info.EndBlock) |
| 455 | EmitBlock(Info.EndBlock); |
| 456 | |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 457 | Exc = Builder.CreateCall(llvm_eh_exception, "exc"); |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 458 | Builder.CreateStore(Exc, RethrowPtr); |
| 459 | EmitBranchThroughCleanup(FinallyRethrow); |
| 460 | |
| 461 | if (Next) |
| 462 | EmitBlock(Next); |
| 463 | } |
| 464 | if (!HasCatchAll) |
| 465 | EmitBranchThroughCleanup(FinallyRethrow); |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 466 | |
| 467 | CodeGenFunction::CleanupBlockInfo Info = PopCleanupBlock(); |
| 468 | |
| 469 | setInvokeDest(PrevLandingPad); |
| 470 | |
| 471 | EmitBlock(FinallyBlock); |
| 472 | |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 473 | if (Info.SwitchBlock) |
| 474 | EmitBlock(Info.SwitchBlock); |
| 475 | if (Info.EndBlock) |
| 476 | EmitBlock(Info.EndBlock); |
| 477 | |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 478 | // Branch around the rethrow code. |
| 479 | EmitBranch(FinallyEnd); |
| 480 | |
| 481 | EmitBlock(FinallyRethrow); |
Mike Stump | 0f590be | 2009-12-01 03:41:18 +0000 | [diff] [blame] | 482 | Builder.CreateCall(getUnwindResumeOrRethrowFn(*this), |
| 483 | Builder.CreateLoad(RethrowPtr)); |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 484 | Builder.CreateUnreachable(); |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 485 | |
| 486 | EmitBlock(FinallyEnd); |
Mike Stump | 2bf701e | 2009-11-20 23:44:51 +0000 | [diff] [blame] | 487 | } |