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