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