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 "CodeGenFunction.h" 15 #include "CGCXXABI.h" 16 #include "CGCleanup.h" 17 #include "CGObjCRuntime.h" 18 #include "TargetInfo.h" 19 #include "clang/AST/Mangle.h" 20 #include "clang/AST/StmtCXX.h" 21 #include "clang/AST/StmtObjC.h" 22 #include "llvm/IR/CallSite.h" 23 #include "llvm/IR/Intrinsics.h" 24 25 using namespace clang; 26 using namespace CodeGen; 27 28 static llvm::Constant *getAllocateExceptionFn(CodeGenModule &CGM) { 29 // void *__cxa_allocate_exception(size_t thrown_size); 30 31 llvm::FunctionType *FTy = 32 llvm::FunctionType::get(CGM.Int8PtrTy, CGM.SizeTy, /*IsVarArgs=*/false); 33 34 return CGM.CreateRuntimeFunction(FTy, "__cxa_allocate_exception"); 35 } 36 37 static llvm::Constant *getFreeExceptionFn(CodeGenModule &CGM) { 38 // void __cxa_free_exception(void *thrown_exception); 39 40 llvm::FunctionType *FTy = 41 llvm::FunctionType::get(CGM.VoidTy, CGM.Int8PtrTy, /*IsVarArgs=*/false); 42 43 return CGM.CreateRuntimeFunction(FTy, "__cxa_free_exception"); 44 } 45 46 static llvm::Constant *getThrowFn(CodeGenModule &CGM) { 47 // void __cxa_throw(void *thrown_exception, std::type_info *tinfo, 48 // void (*dest) (void *)); 49 50 llvm::Type *Args[3] = { CGM.Int8PtrTy, CGM.Int8PtrTy, CGM.Int8PtrTy }; 51 llvm::FunctionType *FTy = 52 llvm::FunctionType::get(CGM.VoidTy, Args, /*IsVarArgs=*/false); 53 54 return CGM.CreateRuntimeFunction(FTy, "__cxa_throw"); 55 } 56 57 static llvm::Constant *getUnexpectedFn(CodeGenModule &CGM) { 58 // void __cxa_call_unexpected(void *thrown_exception); 59 60 llvm::FunctionType *FTy = 61 llvm::FunctionType::get(CGM.VoidTy, CGM.Int8PtrTy, /*IsVarArgs=*/false); 62 63 return CGM.CreateRuntimeFunction(FTy, "__cxa_call_unexpected"); 64 } 65 66 llvm::Constant *CodeGenModule::getTerminateFn() { 67 // void __terminate(); 68 69 llvm::FunctionType *FTy = 70 llvm::FunctionType::get(VoidTy, /*IsVarArgs=*/false); 71 72 StringRef name; 73 74 // In C++, use std::terminate(). 75 if (getLangOpts().CPlusPlus && 76 getTarget().getCXXABI().isItaniumFamily()) { 77 name = "_ZSt9terminatev"; 78 } else if (getLangOpts().CPlusPlus && 79 getTarget().getCXXABI().isMicrosoft()) { 80 name = "\01?terminate@@YAXXZ"; 81 } else if (getLangOpts().ObjC1 && 82 getLangOpts().ObjCRuntime.hasTerminate()) 83 name = "objc_terminate"; 84 else 85 name = "abort"; 86 return CreateRuntimeFunction(FTy, name); 87 } 88 89 static llvm::Constant *getCatchallRethrowFn(CodeGenModule &CGM, 90 StringRef Name) { 91 llvm::FunctionType *FTy = 92 llvm::FunctionType::get(CGM.VoidTy, CGM.Int8PtrTy, /*IsVarArgs=*/false); 93 94 return CGM.CreateRuntimeFunction(FTy, Name); 95 } 96 97 namespace { 98 /// The exceptions personality for a function. 99 struct EHPersonality { 100 const char *PersonalityFn; 101 102 // If this is non-null, this personality requires a non-standard 103 // function for rethrowing an exception after a catchall cleanup. 104 // This function must have prototype void(void*). 105 const char *CatchallRethrowFn; 106 107 static const EHPersonality &get(CodeGenModule &CGM, 108 const FunctionDecl *FD); 109 static const EHPersonality &get(CodeGenFunction &CGF) { 110 return get(CGF.CGM, dyn_cast_or_null<FunctionDecl>(CGF.CurCodeDecl)); 111 } 112 113 static const EHPersonality GNU_C; 114 static const EHPersonality GNU_C_SJLJ; 115 static const EHPersonality GNU_C_SEH; 116 static const EHPersonality GNU_ObjC; 117 static const EHPersonality GNUstep_ObjC; 118 static const EHPersonality GNU_ObjCXX; 119 static const EHPersonality NeXT_ObjC; 120 static const EHPersonality GNU_CPlusPlus; 121 static const EHPersonality GNU_CPlusPlus_SJLJ; 122 static const EHPersonality GNU_CPlusPlus_SEH; 123 static const EHPersonality MSVC_except_handler; 124 static const EHPersonality MSVC_C_specific_handler; 125 static const EHPersonality MSVC_CxxFrameHandler3; 126 }; 127 } 128 129 const EHPersonality EHPersonality::GNU_C = { "__gcc_personality_v0", nullptr }; 130 const EHPersonality 131 EHPersonality::GNU_C_SJLJ = { "__gcc_personality_sj0", nullptr }; 132 const EHPersonality 133 EHPersonality::GNU_C_SEH = { "__gcc_personality_seh0", nullptr }; 134 const EHPersonality 135 EHPersonality::NeXT_ObjC = { "__objc_personality_v0", nullptr }; 136 const EHPersonality 137 EHPersonality::GNU_CPlusPlus = { "__gxx_personality_v0", nullptr }; 138 const EHPersonality 139 EHPersonality::GNU_CPlusPlus_SJLJ = { "__gxx_personality_sj0", nullptr }; 140 const EHPersonality 141 EHPersonality::GNU_CPlusPlus_SEH = { "__gxx_personality_seh0", nullptr }; 142 const EHPersonality 143 EHPersonality::GNU_ObjC = {"__gnu_objc_personality_v0", "objc_exception_throw"}; 144 const EHPersonality 145 EHPersonality::GNU_ObjCXX = { "__gnustep_objcxx_personality_v0", nullptr }; 146 const EHPersonality 147 EHPersonality::GNUstep_ObjC = { "__gnustep_objc_personality_v0", nullptr }; 148 const EHPersonality 149 EHPersonality::MSVC_except_handler = { "_except_handler3", nullptr }; 150 const EHPersonality 151 EHPersonality::MSVC_C_specific_handler = { "__C_specific_handler", nullptr }; 152 const EHPersonality 153 EHPersonality::MSVC_CxxFrameHandler3 = { "__CxxFrameHandler3", nullptr }; 154 155 /// On Win64, use libgcc's SEH personality function. We fall back to dwarf on 156 /// other platforms, unless the user asked for SjLj exceptions. 157 static bool useLibGCCSEHPersonality(const llvm::Triple &T) { 158 return T.isOSWindows() && T.getArch() == llvm::Triple::x86_64; 159 } 160 161 static const EHPersonality &getCPersonality(const llvm::Triple &T, 162 const LangOptions &L) { 163 if (L.SjLjExceptions) 164 return EHPersonality::GNU_C_SJLJ; 165 else if (useLibGCCSEHPersonality(T)) 166 return EHPersonality::GNU_C_SEH; 167 return EHPersonality::GNU_C; 168 } 169 170 static const EHPersonality &getObjCPersonality(const llvm::Triple &T, 171 const LangOptions &L) { 172 switch (L.ObjCRuntime.getKind()) { 173 case ObjCRuntime::FragileMacOSX: 174 return getCPersonality(T, L); 175 case ObjCRuntime::MacOSX: 176 case ObjCRuntime::iOS: 177 return EHPersonality::NeXT_ObjC; 178 case ObjCRuntime::GNUstep: 179 if (L.ObjCRuntime.getVersion() >= VersionTuple(1, 7)) 180 return EHPersonality::GNUstep_ObjC; 181 // fallthrough 182 case ObjCRuntime::GCC: 183 case ObjCRuntime::ObjFW: 184 return EHPersonality::GNU_ObjC; 185 } 186 llvm_unreachable("bad runtime kind"); 187 } 188 189 static const EHPersonality &getCXXPersonality(const llvm::Triple &T, 190 const LangOptions &L) { 191 if (L.SjLjExceptions) 192 return EHPersonality::GNU_CPlusPlus_SJLJ; 193 else if (useLibGCCSEHPersonality(T)) 194 return EHPersonality::GNU_CPlusPlus_SEH; 195 return EHPersonality::GNU_CPlusPlus; 196 } 197 198 /// Determines the personality function to use when both C++ 199 /// and Objective-C exceptions are being caught. 200 static const EHPersonality &getObjCXXPersonality(const llvm::Triple &T, 201 const LangOptions &L) { 202 switch (L.ObjCRuntime.getKind()) { 203 // The ObjC personality defers to the C++ personality for non-ObjC 204 // handlers. Unlike the C++ case, we use the same personality 205 // function on targets using (backend-driven) SJLJ EH. 206 case ObjCRuntime::MacOSX: 207 case ObjCRuntime::iOS: 208 return EHPersonality::NeXT_ObjC; 209 210 // In the fragile ABI, just use C++ exception handling and hope 211 // they're not doing crazy exception mixing. 212 case ObjCRuntime::FragileMacOSX: 213 return getCXXPersonality(T, L); 214 215 // The GCC runtime's personality function inherently doesn't support 216 // mixed EH. Use the C++ personality just to avoid returning null. 217 case ObjCRuntime::GCC: 218 case ObjCRuntime::ObjFW: // XXX: this will change soon 219 return EHPersonality::GNU_ObjC; 220 case ObjCRuntime::GNUstep: 221 return EHPersonality::GNU_ObjCXX; 222 } 223 llvm_unreachable("bad runtime kind"); 224 } 225 226 static const EHPersonality &getSEHPersonalityMSVC(const llvm::Triple &T) { 227 if (T.getArch() == llvm::Triple::x86) 228 return EHPersonality::MSVC_except_handler; 229 return EHPersonality::MSVC_C_specific_handler; 230 } 231 232 const EHPersonality &EHPersonality::get(CodeGenModule &CGM, 233 const FunctionDecl *FD) { 234 const llvm::Triple &T = CGM.getTarget().getTriple(); 235 const LangOptions &L = CGM.getLangOpts(); 236 237 // Try to pick a personality function that is compatible with MSVC if we're 238 // not compiling Obj-C. Obj-C users better have an Obj-C runtime that supports 239 // the GCC-style personality function. 240 if (T.isWindowsMSVCEnvironment() && !L.ObjC1) { 241 if (L.SjLjExceptions) 242 return EHPersonality::GNU_CPlusPlus_SJLJ; 243 else if (FD && FD->usesSEHTry()) 244 return getSEHPersonalityMSVC(T); 245 else 246 return EHPersonality::MSVC_CxxFrameHandler3; 247 } 248 249 if (L.CPlusPlus && L.ObjC1) 250 return getObjCXXPersonality(T, L); 251 else if (L.CPlusPlus) 252 return getCXXPersonality(T, L); 253 else if (L.ObjC1) 254 return getObjCPersonality(T, L); 255 else 256 return getCPersonality(T, L); 257 } 258 259 static llvm::Constant *getPersonalityFn(CodeGenModule &CGM, 260 const EHPersonality &Personality) { 261 llvm::Constant *Fn = 262 CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.Int32Ty, true), 263 Personality.PersonalityFn); 264 return Fn; 265 } 266 267 static llvm::Constant *getOpaquePersonalityFn(CodeGenModule &CGM, 268 const EHPersonality &Personality) { 269 llvm::Constant *Fn = getPersonalityFn(CGM, Personality); 270 return llvm::ConstantExpr::getBitCast(Fn, CGM.Int8PtrTy); 271 } 272 273 /// Check whether a personality function could reasonably be swapped 274 /// for a C++ personality function. 275 static bool PersonalityHasOnlyCXXUses(llvm::Constant *Fn) { 276 for (llvm::User *U : Fn->users()) { 277 // Conditionally white-list bitcasts. 278 if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(U)) { 279 if (CE->getOpcode() != llvm::Instruction::BitCast) return false; 280 if (!PersonalityHasOnlyCXXUses(CE)) 281 return false; 282 continue; 283 } 284 285 // Otherwise, it has to be a landingpad instruction. 286 llvm::LandingPadInst *LPI = dyn_cast<llvm::LandingPadInst>(U); 287 if (!LPI) return false; 288 289 for (unsigned I = 0, E = LPI->getNumClauses(); I != E; ++I) { 290 // Look for something that would've been returned by the ObjC 291 // runtime's GetEHType() method. 292 llvm::Value *Val = LPI->getClause(I)->stripPointerCasts(); 293 if (LPI->isCatch(I)) { 294 // Check if the catch value has the ObjC prefix. 295 if (llvm::GlobalVariable *GV = dyn_cast<llvm::GlobalVariable>(Val)) 296 // ObjC EH selector entries are always global variables with 297 // names starting like this. 298 if (GV->getName().startswith("OBJC_EHTYPE")) 299 return false; 300 } else { 301 // Check if any of the filter values have the ObjC prefix. 302 llvm::Constant *CVal = cast<llvm::Constant>(Val); 303 for (llvm::User::op_iterator 304 II = CVal->op_begin(), IE = CVal->op_end(); II != IE; ++II) { 305 if (llvm::GlobalVariable *GV = 306 cast<llvm::GlobalVariable>((*II)->stripPointerCasts())) 307 // ObjC EH selector entries are always global variables with 308 // names starting like this. 309 if (GV->getName().startswith("OBJC_EHTYPE")) 310 return false; 311 } 312 } 313 } 314 } 315 316 return true; 317 } 318 319 /// Try to use the C++ personality function in ObjC++. Not doing this 320 /// can cause some incompatibilities with gcc, which is more 321 /// aggressive about only using the ObjC++ personality in a function 322 /// when it really needs it. 323 void CodeGenModule::SimplifyPersonality() { 324 // If we're not in ObjC++ -fexceptions, there's nothing to do. 325 if (!LangOpts.CPlusPlus || !LangOpts.ObjC1 || !LangOpts.Exceptions) 326 return; 327 328 // Both the problem this endeavors to fix and the way the logic 329 // above works is specific to the NeXT runtime. 330 if (!LangOpts.ObjCRuntime.isNeXTFamily()) 331 return; 332 333 const EHPersonality &ObjCXX = EHPersonality::get(*this, /*FD=*/nullptr); 334 const EHPersonality &CXX = 335 getCXXPersonality(getTarget().getTriple(), LangOpts); 336 if (&ObjCXX == &CXX) 337 return; 338 339 assert(std::strcmp(ObjCXX.PersonalityFn, CXX.PersonalityFn) != 0 && 340 "Different EHPersonalities using the same personality function."); 341 342 llvm::Function *Fn = getModule().getFunction(ObjCXX.PersonalityFn); 343 344 // Nothing to do if it's unused. 345 if (!Fn || Fn->use_empty()) return; 346 347 // Can't do the optimization if it has non-C++ uses. 348 if (!PersonalityHasOnlyCXXUses(Fn)) return; 349 350 // Create the C++ personality function and kill off the old 351 // function. 352 llvm::Constant *CXXFn = getPersonalityFn(*this, CXX); 353 354 // This can happen if the user is screwing with us. 355 if (Fn->getType() != CXXFn->getType()) return; 356 357 Fn->replaceAllUsesWith(CXXFn); 358 Fn->eraseFromParent(); 359 } 360 361 /// Returns the value to inject into a selector to indicate the 362 /// presence of a catch-all. 363 static llvm::Constant *getCatchAllValue(CodeGenFunction &CGF) { 364 // Possibly we should use @llvm.eh.catch.all.value here. 365 return llvm::ConstantPointerNull::get(CGF.Int8PtrTy); 366 } 367 368 namespace { 369 /// A cleanup to free the exception object if its initialization 370 /// throws. 371 struct FreeException : EHScopeStack::Cleanup { 372 llvm::Value *exn; 373 FreeException(llvm::Value *exn) : exn(exn) {} 374 void Emit(CodeGenFunction &CGF, Flags flags) override { 375 CGF.EmitNounwindRuntimeCall(getFreeExceptionFn(CGF.CGM), exn); 376 } 377 }; 378 } 379 380 // Emits an exception expression into the given location. This 381 // differs from EmitAnyExprToMem only in that, if a final copy-ctor 382 // call is required, an exception within that copy ctor causes 383 // std::terminate to be invoked. 384 static void EmitAnyExprToExn(CodeGenFunction &CGF, const Expr *e, 385 llvm::Value *addr) { 386 // Make sure the exception object is cleaned up if there's an 387 // exception during initialization. 388 CGF.pushFullExprCleanup<FreeException>(EHCleanup, addr); 389 EHScopeStack::stable_iterator cleanup = CGF.EHStack.stable_begin(); 390 391 // __cxa_allocate_exception returns a void*; we need to cast this 392 // to the appropriate type for the object. 393 llvm::Type *ty = CGF.ConvertTypeForMem(e->getType())->getPointerTo(); 394 llvm::Value *typedAddr = CGF.Builder.CreateBitCast(addr, ty); 395 396 // FIXME: this isn't quite right! If there's a final unelided call 397 // to a copy constructor, then according to [except.terminate]p1 we 398 // must call std::terminate() if that constructor throws, because 399 // technically that copy occurs after the exception expression is 400 // evaluated but before the exception is caught. But the best way 401 // to handle that is to teach EmitAggExpr to do the final copy 402 // differently if it can't be elided. 403 CGF.EmitAnyExprToMem(e, typedAddr, e->getType().getQualifiers(), 404 /*IsInit*/ true); 405 406 // Deactivate the cleanup block. 407 CGF.DeactivateCleanupBlock(cleanup, cast<llvm::Instruction>(typedAddr)); 408 } 409 410 llvm::Value *CodeGenFunction::getExceptionSlot() { 411 if (!ExceptionSlot) 412 ExceptionSlot = CreateTempAlloca(Int8PtrTy, "exn.slot"); 413 return ExceptionSlot; 414 } 415 416 llvm::Value *CodeGenFunction::getEHSelectorSlot() { 417 if (!EHSelectorSlot) 418 EHSelectorSlot = CreateTempAlloca(Int32Ty, "ehselector.slot"); 419 return EHSelectorSlot; 420 } 421 422 llvm::Value *CodeGenFunction::getExceptionFromSlot() { 423 return Builder.CreateLoad(getExceptionSlot(), "exn"); 424 } 425 426 llvm::Value *CodeGenFunction::getSelectorFromSlot() { 427 return Builder.CreateLoad(getEHSelectorSlot(), "sel"); 428 } 429 430 llvm::Value *CodeGenFunction::getAbnormalTerminationSlot() { 431 if (!AbnormalTerminationSlot) 432 AbnormalTerminationSlot = 433 CreateTempAlloca(Int8Ty, "abnormal.termination.slot"); 434 return AbnormalTerminationSlot; 435 } 436 437 void CodeGenFunction::EmitCXXThrowExpr(const CXXThrowExpr *E, 438 bool KeepInsertionPoint) { 439 if (!E->getSubExpr()) { 440 CGM.getCXXABI().emitRethrow(*this, /*isNoReturn*/true); 441 442 // throw is an expression, and the expression emitters expect us 443 // to leave ourselves at a valid insertion point. 444 if (KeepInsertionPoint) 445 EmitBlock(createBasicBlock("throw.cont")); 446 447 return; 448 } 449 450 if (CGM.getTarget().getTriple().isKnownWindowsMSVCEnvironment()) { 451 // Call std::terminate(). 452 llvm::CallInst *TermCall = EmitNounwindRuntimeCall(CGM.getTerminateFn()); 453 TermCall->setDoesNotReturn(); 454 455 // throw is an expression, and the expression emitters expect us 456 // to leave ourselves at a valid insertion point. 457 if (KeepInsertionPoint) 458 EmitBlock(createBasicBlock("throw.cont")); 459 460 return; 461 } 462 463 QualType ThrowType = E->getSubExpr()->getType(); 464 465 if (ThrowType->isObjCObjectPointerType()) { 466 const Stmt *ThrowStmt = E->getSubExpr(); 467 const ObjCAtThrowStmt S(E->getExprLoc(), 468 const_cast<Stmt *>(ThrowStmt)); 469 CGM.getObjCRuntime().EmitThrowStmt(*this, S, false); 470 // This will clear insertion point which was not cleared in 471 // call to EmitThrowStmt. 472 if (KeepInsertionPoint) 473 EmitBlock(createBasicBlock("throw.cont")); 474 return; 475 } 476 477 // Now allocate the exception object. 478 llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); 479 uint64_t TypeSize = getContext().getTypeSizeInChars(ThrowType).getQuantity(); 480 481 llvm::Constant *AllocExceptionFn = getAllocateExceptionFn(CGM); 482 llvm::CallInst *ExceptionPtr = 483 EmitNounwindRuntimeCall(AllocExceptionFn, 484 llvm::ConstantInt::get(SizeTy, TypeSize), 485 "exception"); 486 487 EmitAnyExprToExn(*this, E->getSubExpr(), ExceptionPtr); 488 489 // Now throw the exception. 490 llvm::Constant *TypeInfo = CGM.GetAddrOfRTTIDescriptor(ThrowType, 491 /*ForEH=*/true); 492 493 // The address of the destructor. If the exception type has a 494 // trivial destructor (or isn't a record), we just pass null. 495 llvm::Constant *Dtor = nullptr; 496 if (const RecordType *RecordTy = ThrowType->getAs<RecordType>()) { 497 CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordTy->getDecl()); 498 if (!Record->hasTrivialDestructor()) { 499 CXXDestructorDecl *DtorD = Record->getDestructor(); 500 Dtor = CGM.getAddrOfCXXStructor(DtorD, StructorType::Complete); 501 Dtor = llvm::ConstantExpr::getBitCast(Dtor, Int8PtrTy); 502 } 503 } 504 if (!Dtor) Dtor = llvm::Constant::getNullValue(Int8PtrTy); 505 506 llvm::Value *args[] = { ExceptionPtr, TypeInfo, Dtor }; 507 EmitNoreturnRuntimeCallOrInvoke(getThrowFn(CGM), args); 508 509 // throw is an expression, and the expression emitters expect us 510 // to leave ourselves at a valid insertion point. 511 if (KeepInsertionPoint) 512 EmitBlock(createBasicBlock("throw.cont")); 513 } 514 515 void CodeGenFunction::EmitStartEHSpec(const Decl *D) { 516 if (!CGM.getLangOpts().CXXExceptions) 517 return; 518 519 const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D); 520 if (!FD) { 521 // Check if CapturedDecl is nothrow and create terminate scope for it. 522 if (const CapturedDecl* CD = dyn_cast_or_null<CapturedDecl>(D)) { 523 if (CD->isNothrow()) 524 EHStack.pushTerminate(); 525 } 526 return; 527 } 528 const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>(); 529 if (!Proto) 530 return; 531 532 ExceptionSpecificationType EST = Proto->getExceptionSpecType(); 533 if (isNoexceptExceptionSpec(EST)) { 534 if (Proto->getNoexceptSpec(getContext()) == FunctionProtoType::NR_Nothrow) { 535 // noexcept functions are simple terminate scopes. 536 EHStack.pushTerminate(); 537 } 538 } else if (EST == EST_Dynamic || EST == EST_DynamicNone) { 539 unsigned NumExceptions = Proto->getNumExceptions(); 540 EHFilterScope *Filter = EHStack.pushFilter(NumExceptions); 541 542 for (unsigned I = 0; I != NumExceptions; ++I) { 543 QualType Ty = Proto->getExceptionType(I); 544 QualType ExceptType = Ty.getNonReferenceType().getUnqualifiedType(); 545 llvm::Value *EHType = CGM.GetAddrOfRTTIDescriptor(ExceptType, 546 /*ForEH=*/true); 547 Filter->setFilter(I, EHType); 548 } 549 } 550 } 551 552 /// Emit the dispatch block for a filter scope if necessary. 553 static void emitFilterDispatchBlock(CodeGenFunction &CGF, 554 EHFilterScope &filterScope) { 555 llvm::BasicBlock *dispatchBlock = filterScope.getCachedEHDispatchBlock(); 556 if (!dispatchBlock) return; 557 if (dispatchBlock->use_empty()) { 558 delete dispatchBlock; 559 return; 560 } 561 562 CGF.EmitBlockAfterUses(dispatchBlock); 563 564 // If this isn't a catch-all filter, we need to check whether we got 565 // here because the filter triggered. 566 if (filterScope.getNumFilters()) { 567 // Load the selector value. 568 llvm::Value *selector = CGF.getSelectorFromSlot(); 569 llvm::BasicBlock *unexpectedBB = CGF.createBasicBlock("ehspec.unexpected"); 570 571 llvm::Value *zero = CGF.Builder.getInt32(0); 572 llvm::Value *failsFilter = 573 CGF.Builder.CreateICmpSLT(selector, zero, "ehspec.fails"); 574 CGF.Builder.CreateCondBr(failsFilter, unexpectedBB, 575 CGF.getEHResumeBlock(false)); 576 577 CGF.EmitBlock(unexpectedBB); 578 } 579 580 // Call __cxa_call_unexpected. This doesn't need to be an invoke 581 // because __cxa_call_unexpected magically filters exceptions 582 // according to the last landing pad the exception was thrown 583 // into. Seriously. 584 llvm::Value *exn = CGF.getExceptionFromSlot(); 585 CGF.EmitRuntimeCall(getUnexpectedFn(CGF.CGM), exn) 586 ->setDoesNotReturn(); 587 CGF.Builder.CreateUnreachable(); 588 } 589 590 void CodeGenFunction::EmitEndEHSpec(const Decl *D) { 591 if (!CGM.getLangOpts().CXXExceptions) 592 return; 593 594 const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D); 595 if (!FD) { 596 // Check if CapturedDecl is nothrow and pop terminate scope for it. 597 if (const CapturedDecl* CD = dyn_cast_or_null<CapturedDecl>(D)) { 598 if (CD->isNothrow()) 599 EHStack.popTerminate(); 600 } 601 return; 602 } 603 const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>(); 604 if (!Proto) 605 return; 606 607 ExceptionSpecificationType EST = Proto->getExceptionSpecType(); 608 if (isNoexceptExceptionSpec(EST)) { 609 if (Proto->getNoexceptSpec(getContext()) == FunctionProtoType::NR_Nothrow) { 610 EHStack.popTerminate(); 611 } 612 } else if (EST == EST_Dynamic || EST == EST_DynamicNone) { 613 EHFilterScope &filterScope = cast<EHFilterScope>(*EHStack.begin()); 614 emitFilterDispatchBlock(*this, filterScope); 615 EHStack.popFilter(); 616 } 617 } 618 619 void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) { 620 EnterCXXTryStmt(S); 621 EmitStmt(S.getTryBlock()); 622 ExitCXXTryStmt(S); 623 } 624 625 void CodeGenFunction::EnterCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock) { 626 unsigned NumHandlers = S.getNumHandlers(); 627 EHCatchScope *CatchScope = EHStack.pushCatch(NumHandlers); 628 629 for (unsigned I = 0; I != NumHandlers; ++I) { 630 const CXXCatchStmt *C = S.getHandler(I); 631 632 llvm::BasicBlock *Handler = createBasicBlock("catch"); 633 if (C->getExceptionDecl()) { 634 // FIXME: Dropping the reference type on the type into makes it 635 // impossible to correctly implement catch-by-reference 636 // semantics for pointers. Unfortunately, this is what all 637 // existing compilers do, and it's not clear that the standard 638 // personality routine is capable of doing this right. See C++ DR 388: 639 // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#388 640 Qualifiers CaughtTypeQuals; 641 QualType CaughtType = CGM.getContext().getUnqualifiedArrayType( 642 C->getCaughtType().getNonReferenceType(), CaughtTypeQuals); 643 644 llvm::Constant *TypeInfo = nullptr; 645 if (CaughtType->isObjCObjectPointerType()) 646 TypeInfo = CGM.getObjCRuntime().GetEHType(CaughtType); 647 else 648 TypeInfo = CGM.GetAddrOfRTTIDescriptor(CaughtType, /*ForEH=*/true); 649 CatchScope->setHandler(I, TypeInfo, Handler); 650 } else { 651 // No exception decl indicates '...', a catch-all. 652 CatchScope->setCatchAllHandler(I, Handler); 653 } 654 } 655 } 656 657 llvm::BasicBlock * 658 CodeGenFunction::getEHDispatchBlock(EHScopeStack::stable_iterator si) { 659 // The dispatch block for the end of the scope chain is a block that 660 // just resumes unwinding. 661 if (si == EHStack.stable_end()) 662 return getEHResumeBlock(true); 663 664 // Otherwise, we should look at the actual scope. 665 EHScope &scope = *EHStack.find(si); 666 667 llvm::BasicBlock *dispatchBlock = scope.getCachedEHDispatchBlock(); 668 if (!dispatchBlock) { 669 switch (scope.getKind()) { 670 case EHScope::Catch: { 671 // Apply a special case to a single catch-all. 672 EHCatchScope &catchScope = cast<EHCatchScope>(scope); 673 if (catchScope.getNumHandlers() == 1 && 674 catchScope.getHandler(0).isCatchAll()) { 675 dispatchBlock = catchScope.getHandler(0).Block; 676 677 // Otherwise, make a dispatch block. 678 } else { 679 dispatchBlock = createBasicBlock("catch.dispatch"); 680 } 681 break; 682 } 683 684 case EHScope::Cleanup: 685 dispatchBlock = createBasicBlock("ehcleanup"); 686 break; 687 688 case EHScope::Filter: 689 dispatchBlock = createBasicBlock("filter.dispatch"); 690 break; 691 692 case EHScope::Terminate: 693 dispatchBlock = getTerminateHandler(); 694 break; 695 } 696 scope.setCachedEHDispatchBlock(dispatchBlock); 697 } 698 return dispatchBlock; 699 } 700 701 /// Check whether this is a non-EH scope, i.e. a scope which doesn't 702 /// affect exception handling. Currently, the only non-EH scopes are 703 /// normal-only cleanup scopes. 704 static bool isNonEHScope(const EHScope &S) { 705 switch (S.getKind()) { 706 case EHScope::Cleanup: 707 return !cast<EHCleanupScope>(S).isEHCleanup(); 708 case EHScope::Filter: 709 case EHScope::Catch: 710 case EHScope::Terminate: 711 return false; 712 } 713 714 llvm_unreachable("Invalid EHScope Kind!"); 715 } 716 717 llvm::BasicBlock *CodeGenFunction::getInvokeDestImpl() { 718 assert(EHStack.requiresLandingPad()); 719 assert(!EHStack.empty()); 720 721 // If exceptions are disabled, there are usually no landingpads. However, when 722 // SEH is enabled, functions using SEH still get landingpads. 723 const LangOptions &LO = CGM.getLangOpts(); 724 if (!LO.Exceptions) { 725 if (!LO.Borland && !LO.MicrosoftExt) 726 return nullptr; 727 if (!currentFunctionUsesSEHTry()) 728 return nullptr; 729 } 730 731 // Check the innermost scope for a cached landing pad. If this is 732 // a non-EH cleanup, we'll check enclosing scopes in EmitLandingPad. 733 llvm::BasicBlock *LP = EHStack.begin()->getCachedLandingPad(); 734 if (LP) return LP; 735 736 // Build the landing pad for this scope. 737 LP = EmitLandingPad(); 738 assert(LP); 739 740 // Cache the landing pad on the innermost scope. If this is a 741 // non-EH scope, cache the landing pad on the enclosing scope, too. 742 for (EHScopeStack::iterator ir = EHStack.begin(); true; ++ir) { 743 ir->setCachedLandingPad(LP); 744 if (!isNonEHScope(*ir)) break; 745 } 746 747 return LP; 748 } 749 750 llvm::BasicBlock *CodeGenFunction::EmitLandingPad() { 751 assert(EHStack.requiresLandingPad()); 752 753 EHScope &innermostEHScope = *EHStack.find(EHStack.getInnermostEHScope()); 754 switch (innermostEHScope.getKind()) { 755 case EHScope::Terminate: 756 return getTerminateLandingPad(); 757 758 case EHScope::Catch: 759 case EHScope::Cleanup: 760 case EHScope::Filter: 761 if (llvm::BasicBlock *lpad = innermostEHScope.getCachedLandingPad()) 762 return lpad; 763 } 764 765 // Save the current IR generation state. 766 CGBuilderTy::InsertPoint savedIP = Builder.saveAndClearIP(); 767 auto DL = ApplyDebugLocation::CreateDefaultArtificial(*this, CurEHLocation); 768 769 const EHPersonality &personality = EHPersonality::get(*this); 770 771 // Create and configure the landing pad. 772 llvm::BasicBlock *lpad = createBasicBlock("lpad"); 773 EmitBlock(lpad); 774 775 llvm::LandingPadInst *LPadInst = 776 Builder.CreateLandingPad(llvm::StructType::get(Int8PtrTy, Int32Ty, nullptr), 777 getOpaquePersonalityFn(CGM, personality), 0); 778 779 llvm::Value *LPadExn = Builder.CreateExtractValue(LPadInst, 0); 780 Builder.CreateStore(LPadExn, getExceptionSlot()); 781 llvm::Value *LPadSel = Builder.CreateExtractValue(LPadInst, 1); 782 Builder.CreateStore(LPadSel, getEHSelectorSlot()); 783 784 // Save the exception pointer. It's safe to use a single exception 785 // pointer per function because EH cleanups can never have nested 786 // try/catches. 787 // Build the landingpad instruction. 788 789 // Accumulate all the handlers in scope. 790 bool hasCatchAll = false; 791 bool hasCleanup = false; 792 bool hasFilter = false; 793 SmallVector<llvm::Value*, 4> filterTypes; 794 llvm::SmallPtrSet<llvm::Value*, 4> catchTypes; 795 for (EHScopeStack::iterator I = EHStack.begin(), E = EHStack.end(); I != E; 796 ++I) { 797 798 switch (I->getKind()) { 799 case EHScope::Cleanup: 800 // If we have a cleanup, remember that. 801 hasCleanup = (hasCleanup || cast<EHCleanupScope>(*I).isEHCleanup()); 802 continue; 803 804 case EHScope::Filter: { 805 assert(I.next() == EHStack.end() && "EH filter is not end of EH stack"); 806 assert(!hasCatchAll && "EH filter reached after catch-all"); 807 808 // Filter scopes get added to the landingpad in weird ways. 809 EHFilterScope &filter = cast<EHFilterScope>(*I); 810 hasFilter = true; 811 812 // Add all the filter values. 813 for (unsigned i = 0, e = filter.getNumFilters(); i != e; ++i) 814 filterTypes.push_back(filter.getFilter(i)); 815 goto done; 816 } 817 818 case EHScope::Terminate: 819 // Terminate scopes are basically catch-alls. 820 assert(!hasCatchAll); 821 hasCatchAll = true; 822 goto done; 823 824 case EHScope::Catch: 825 break; 826 } 827 828 EHCatchScope &catchScope = cast<EHCatchScope>(*I); 829 for (unsigned hi = 0, he = catchScope.getNumHandlers(); hi != he; ++hi) { 830 EHCatchScope::Handler handler = catchScope.getHandler(hi); 831 832 // If this is a catch-all, register that and abort. 833 if (!handler.Type) { 834 assert(!hasCatchAll); 835 hasCatchAll = true; 836 goto done; 837 } 838 839 // Check whether we already have a handler for this type. 840 if (catchTypes.insert(handler.Type).second) 841 // If not, add it directly to the landingpad. 842 LPadInst->addClause(handler.Type); 843 } 844 } 845 846 done: 847 // If we have a catch-all, add null to the landingpad. 848 assert(!(hasCatchAll && hasFilter)); 849 if (hasCatchAll) { 850 LPadInst->addClause(getCatchAllValue(*this)); 851 852 // If we have an EH filter, we need to add those handlers in the 853 // right place in the landingpad, which is to say, at the end. 854 } else if (hasFilter) { 855 // Create a filter expression: a constant array indicating which filter 856 // types there are. The personality routine only lands here if the filter 857 // doesn't match. 858 SmallVector<llvm::Constant*, 8> Filters; 859 llvm::ArrayType *AType = 860 llvm::ArrayType::get(!filterTypes.empty() ? 861 filterTypes[0]->getType() : Int8PtrTy, 862 filterTypes.size()); 863 864 for (unsigned i = 0, e = filterTypes.size(); i != e; ++i) 865 Filters.push_back(cast<llvm::Constant>(filterTypes[i])); 866 llvm::Constant *FilterArray = llvm::ConstantArray::get(AType, Filters); 867 LPadInst->addClause(FilterArray); 868 869 // Also check whether we need a cleanup. 870 if (hasCleanup) 871 LPadInst->setCleanup(true); 872 873 // Otherwise, signal that we at least have cleanups. 874 } else if (hasCleanup) { 875 LPadInst->setCleanup(true); 876 } 877 878 assert((LPadInst->getNumClauses() > 0 || LPadInst->isCleanup()) && 879 "landingpad instruction has no clauses!"); 880 881 // Tell the backend how to generate the landing pad. 882 Builder.CreateBr(getEHDispatchBlock(EHStack.getInnermostEHScope())); 883 884 // Restore the old IR generation state. 885 Builder.restoreIP(savedIP); 886 887 return lpad; 888 } 889 890 /// Emit the structure of the dispatch block for the given catch scope. 891 /// It is an invariant that the dispatch block already exists. 892 static void emitCatchDispatchBlock(CodeGenFunction &CGF, 893 EHCatchScope &catchScope) { 894 llvm::BasicBlock *dispatchBlock = catchScope.getCachedEHDispatchBlock(); 895 assert(dispatchBlock); 896 897 // If there's only a single catch-all, getEHDispatchBlock returned 898 // that catch-all as the dispatch block. 899 if (catchScope.getNumHandlers() == 1 && 900 catchScope.getHandler(0).isCatchAll()) { 901 assert(dispatchBlock == catchScope.getHandler(0).Block); 902 return; 903 } 904 905 CGBuilderTy::InsertPoint savedIP = CGF.Builder.saveIP(); 906 CGF.EmitBlockAfterUses(dispatchBlock); 907 908 // Select the right handler. 909 llvm::Value *llvm_eh_typeid_for = 910 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for); 911 912 // Load the selector value. 913 llvm::Value *selector = CGF.getSelectorFromSlot(); 914 915 // Test against each of the exception types we claim to catch. 916 for (unsigned i = 0, e = catchScope.getNumHandlers(); ; ++i) { 917 assert(i < e && "ran off end of handlers!"); 918 const EHCatchScope::Handler &handler = catchScope.getHandler(i); 919 920 llvm::Value *typeValue = handler.Type; 921 assert(typeValue && "fell into catch-all case!"); 922 typeValue = CGF.Builder.CreateBitCast(typeValue, CGF.Int8PtrTy); 923 924 // Figure out the next block. 925 bool nextIsEnd; 926 llvm::BasicBlock *nextBlock; 927 928 // If this is the last handler, we're at the end, and the next 929 // block is the block for the enclosing EH scope. 930 if (i + 1 == e) { 931 nextBlock = CGF.getEHDispatchBlock(catchScope.getEnclosingEHScope()); 932 nextIsEnd = true; 933 934 // If the next handler is a catch-all, we're at the end, and the 935 // next block is that handler. 936 } else if (catchScope.getHandler(i+1).isCatchAll()) { 937 nextBlock = catchScope.getHandler(i+1).Block; 938 nextIsEnd = true; 939 940 // Otherwise, we're not at the end and we need a new block. 941 } else { 942 nextBlock = CGF.createBasicBlock("catch.fallthrough"); 943 nextIsEnd = false; 944 } 945 946 // Figure out the catch type's index in the LSDA's type table. 947 llvm::CallInst *typeIndex = 948 CGF.Builder.CreateCall(llvm_eh_typeid_for, typeValue); 949 typeIndex->setDoesNotThrow(); 950 951 llvm::Value *matchesTypeIndex = 952 CGF.Builder.CreateICmpEQ(selector, typeIndex, "matches"); 953 CGF.Builder.CreateCondBr(matchesTypeIndex, handler.Block, nextBlock); 954 955 // If the next handler is a catch-all, we're completely done. 956 if (nextIsEnd) { 957 CGF.Builder.restoreIP(savedIP); 958 return; 959 } 960 // Otherwise we need to emit and continue at that block. 961 CGF.EmitBlock(nextBlock); 962 } 963 } 964 965 void CodeGenFunction::popCatchScope() { 966 EHCatchScope &catchScope = cast<EHCatchScope>(*EHStack.begin()); 967 if (catchScope.hasEHBranches()) 968 emitCatchDispatchBlock(*this, catchScope); 969 EHStack.popCatch(); 970 } 971 972 void CodeGenFunction::ExitCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock) { 973 unsigned NumHandlers = S.getNumHandlers(); 974 EHCatchScope &CatchScope = cast<EHCatchScope>(*EHStack.begin()); 975 assert(CatchScope.getNumHandlers() == NumHandlers); 976 977 // If the catch was not required, bail out now. 978 if (!CatchScope.hasEHBranches()) { 979 CatchScope.clearHandlerBlocks(); 980 EHStack.popCatch(); 981 return; 982 } 983 984 // Emit the structure of the EH dispatch for this catch. 985 emitCatchDispatchBlock(*this, CatchScope); 986 987 // Copy the handler blocks off before we pop the EH stack. Emitting 988 // the handlers might scribble on this memory. 989 SmallVector<EHCatchScope::Handler, 8> Handlers(NumHandlers); 990 memcpy(Handlers.data(), CatchScope.begin(), 991 NumHandlers * sizeof(EHCatchScope::Handler)); 992 993 EHStack.popCatch(); 994 995 // The fall-through block. 996 llvm::BasicBlock *ContBB = createBasicBlock("try.cont"); 997 998 // We just emitted the body of the try; jump to the continue block. 999 if (HaveInsertPoint()) 1000 Builder.CreateBr(ContBB); 1001 1002 // Determine if we need an implicit rethrow for all these catch handlers; 1003 // see the comment below. 1004 bool doImplicitRethrow = false; 1005 if (IsFnTryBlock) 1006 doImplicitRethrow = isa<CXXDestructorDecl>(CurCodeDecl) || 1007 isa<CXXConstructorDecl>(CurCodeDecl); 1008 1009 // Perversely, we emit the handlers backwards precisely because we 1010 // want them to appear in source order. In all of these cases, the 1011 // catch block will have exactly one predecessor, which will be a 1012 // particular block in the catch dispatch. However, in the case of 1013 // a catch-all, one of the dispatch blocks will branch to two 1014 // different handlers, and EmitBlockAfterUses will cause the second 1015 // handler to be moved before the first. 1016 for (unsigned I = NumHandlers; I != 0; --I) { 1017 llvm::BasicBlock *CatchBlock = Handlers[I-1].Block; 1018 EmitBlockAfterUses(CatchBlock); 1019 1020 // Catch the exception if this isn't a catch-all. 1021 const CXXCatchStmt *C = S.getHandler(I-1); 1022 1023 // Enter a cleanup scope, including the catch variable and the 1024 // end-catch. 1025 RunCleanupsScope CatchScope(*this); 1026 1027 // Initialize the catch variable and set up the cleanups. 1028 CGM.getCXXABI().emitBeginCatch(*this, C); 1029 1030 // Emit the PGO counter increment. 1031 RegionCounter CatchCnt = getPGORegionCounter(C); 1032 CatchCnt.beginRegion(Builder); 1033 1034 // Perform the body of the catch. 1035 EmitStmt(C->getHandlerBlock()); 1036 1037 // [except.handle]p11: 1038 // The currently handled exception is rethrown if control 1039 // reaches the end of a handler of the function-try-block of a 1040 // constructor or destructor. 1041 1042 // It is important that we only do this on fallthrough and not on 1043 // return. Note that it's illegal to put a return in a 1044 // constructor function-try-block's catch handler (p14), so this 1045 // really only applies to destructors. 1046 if (doImplicitRethrow && HaveInsertPoint()) { 1047 CGM.getCXXABI().emitRethrow(*this, /*isNoReturn*/false); 1048 Builder.CreateUnreachable(); 1049 Builder.ClearInsertionPoint(); 1050 } 1051 1052 // Fall out through the catch cleanups. 1053 CatchScope.ForceCleanup(); 1054 1055 // Branch out of the try. 1056 if (HaveInsertPoint()) 1057 Builder.CreateBr(ContBB); 1058 } 1059 1060 RegionCounter ContCnt = getPGORegionCounter(&S); 1061 EmitBlock(ContBB); 1062 ContCnt.beginRegion(Builder); 1063 } 1064 1065 namespace { 1066 struct CallEndCatchForFinally : EHScopeStack::Cleanup { 1067 llvm::Value *ForEHVar; 1068 llvm::Value *EndCatchFn; 1069 CallEndCatchForFinally(llvm::Value *ForEHVar, llvm::Value *EndCatchFn) 1070 : ForEHVar(ForEHVar), EndCatchFn(EndCatchFn) {} 1071 1072 void Emit(CodeGenFunction &CGF, Flags flags) override { 1073 llvm::BasicBlock *EndCatchBB = CGF.createBasicBlock("finally.endcatch"); 1074 llvm::BasicBlock *CleanupContBB = 1075 CGF.createBasicBlock("finally.cleanup.cont"); 1076 1077 llvm::Value *ShouldEndCatch = 1078 CGF.Builder.CreateLoad(ForEHVar, "finally.endcatch"); 1079 CGF.Builder.CreateCondBr(ShouldEndCatch, EndCatchBB, CleanupContBB); 1080 CGF.EmitBlock(EndCatchBB); 1081 CGF.EmitRuntimeCallOrInvoke(EndCatchFn); // catch-all, so might throw 1082 CGF.EmitBlock(CleanupContBB); 1083 } 1084 }; 1085 1086 struct PerformFinally : EHScopeStack::Cleanup { 1087 const Stmt *Body; 1088 llvm::Value *ForEHVar; 1089 llvm::Value *EndCatchFn; 1090 llvm::Value *RethrowFn; 1091 llvm::Value *SavedExnVar; 1092 1093 PerformFinally(const Stmt *Body, llvm::Value *ForEHVar, 1094 llvm::Value *EndCatchFn, 1095 llvm::Value *RethrowFn, llvm::Value *SavedExnVar) 1096 : Body(Body), ForEHVar(ForEHVar), EndCatchFn(EndCatchFn), 1097 RethrowFn(RethrowFn), SavedExnVar(SavedExnVar) {} 1098 1099 void Emit(CodeGenFunction &CGF, Flags flags) override { 1100 // Enter a cleanup to call the end-catch function if one was provided. 1101 if (EndCatchFn) 1102 CGF.EHStack.pushCleanup<CallEndCatchForFinally>(NormalAndEHCleanup, 1103 ForEHVar, EndCatchFn); 1104 1105 // Save the current cleanup destination in case there are 1106 // cleanups in the finally block. 1107 llvm::Value *SavedCleanupDest = 1108 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot(), 1109 "cleanup.dest.saved"); 1110 1111 // Emit the finally block. 1112 CGF.EmitStmt(Body); 1113 1114 // If the end of the finally is reachable, check whether this was 1115 // for EH. If so, rethrow. 1116 if (CGF.HaveInsertPoint()) { 1117 llvm::BasicBlock *RethrowBB = CGF.createBasicBlock("finally.rethrow"); 1118 llvm::BasicBlock *ContBB = CGF.createBasicBlock("finally.cont"); 1119 1120 llvm::Value *ShouldRethrow = 1121 CGF.Builder.CreateLoad(ForEHVar, "finally.shouldthrow"); 1122 CGF.Builder.CreateCondBr(ShouldRethrow, RethrowBB, ContBB); 1123 1124 CGF.EmitBlock(RethrowBB); 1125 if (SavedExnVar) { 1126 CGF.EmitRuntimeCallOrInvoke(RethrowFn, 1127 CGF.Builder.CreateLoad(SavedExnVar)); 1128 } else { 1129 CGF.EmitRuntimeCallOrInvoke(RethrowFn); 1130 } 1131 CGF.Builder.CreateUnreachable(); 1132 1133 CGF.EmitBlock(ContBB); 1134 1135 // Restore the cleanup destination. 1136 CGF.Builder.CreateStore(SavedCleanupDest, 1137 CGF.getNormalCleanupDestSlot()); 1138 } 1139 1140 // Leave the end-catch cleanup. As an optimization, pretend that 1141 // the fallthrough path was inaccessible; we've dynamically proven 1142 // that we're not in the EH case along that path. 1143 if (EndCatchFn) { 1144 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP(); 1145 CGF.PopCleanupBlock(); 1146 CGF.Builder.restoreIP(SavedIP); 1147 } 1148 1149 // Now make sure we actually have an insertion point or the 1150 // cleanup gods will hate us. 1151 CGF.EnsureInsertPoint(); 1152 } 1153 }; 1154 } 1155 1156 /// Enters a finally block for an implementation using zero-cost 1157 /// exceptions. This is mostly general, but hard-codes some 1158 /// language/ABI-specific behavior in the catch-all sections. 1159 void CodeGenFunction::FinallyInfo::enter(CodeGenFunction &CGF, 1160 const Stmt *body, 1161 llvm::Constant *beginCatchFn, 1162 llvm::Constant *endCatchFn, 1163 llvm::Constant *rethrowFn) { 1164 assert((beginCatchFn != nullptr) == (endCatchFn != nullptr) && 1165 "begin/end catch functions not paired"); 1166 assert(rethrowFn && "rethrow function is required"); 1167 1168 BeginCatchFn = beginCatchFn; 1169 1170 // The rethrow function has one of the following two types: 1171 // void (*)() 1172 // void (*)(void*) 1173 // In the latter case we need to pass it the exception object. 1174 // But we can't use the exception slot because the @finally might 1175 // have a landing pad (which would overwrite the exception slot). 1176 llvm::FunctionType *rethrowFnTy = 1177 cast<llvm::FunctionType>( 1178 cast<llvm::PointerType>(rethrowFn->getType())->getElementType()); 1179 SavedExnVar = nullptr; 1180 if (rethrowFnTy->getNumParams()) 1181 SavedExnVar = CGF.CreateTempAlloca(CGF.Int8PtrTy, "finally.exn"); 1182 1183 // A finally block is a statement which must be executed on any edge 1184 // out of a given scope. Unlike a cleanup, the finally block may 1185 // contain arbitrary control flow leading out of itself. In 1186 // addition, finally blocks should always be executed, even if there 1187 // are no catch handlers higher on the stack. Therefore, we 1188 // surround the protected scope with a combination of a normal 1189 // cleanup (to catch attempts to break out of the block via normal 1190 // control flow) and an EH catch-all (semantically "outside" any try 1191 // statement to which the finally block might have been attached). 1192 // The finally block itself is generated in the context of a cleanup 1193 // which conditionally leaves the catch-all. 1194 1195 // Jump destination for performing the finally block on an exception 1196 // edge. We'll never actually reach this block, so unreachable is 1197 // fine. 1198 RethrowDest = CGF.getJumpDestInCurrentScope(CGF.getUnreachableBlock()); 1199 1200 // Whether the finally block is being executed for EH purposes. 1201 ForEHVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(), "finally.for-eh"); 1202 CGF.Builder.CreateStore(CGF.Builder.getFalse(), ForEHVar); 1203 1204 // Enter a normal cleanup which will perform the @finally block. 1205 CGF.EHStack.pushCleanup<PerformFinally>(NormalCleanup, body, 1206 ForEHVar, endCatchFn, 1207 rethrowFn, SavedExnVar); 1208 1209 // Enter a catch-all scope. 1210 llvm::BasicBlock *catchBB = CGF.createBasicBlock("finally.catchall"); 1211 EHCatchScope *catchScope = CGF.EHStack.pushCatch(1); 1212 catchScope->setCatchAllHandler(0, catchBB); 1213 } 1214 1215 void CodeGenFunction::FinallyInfo::exit(CodeGenFunction &CGF) { 1216 // Leave the finally catch-all. 1217 EHCatchScope &catchScope = cast<EHCatchScope>(*CGF.EHStack.begin()); 1218 llvm::BasicBlock *catchBB = catchScope.getHandler(0).Block; 1219 1220 CGF.popCatchScope(); 1221 1222 // If there are any references to the catch-all block, emit it. 1223 if (catchBB->use_empty()) { 1224 delete catchBB; 1225 } else { 1226 CGBuilderTy::InsertPoint savedIP = CGF.Builder.saveAndClearIP(); 1227 CGF.EmitBlock(catchBB); 1228 1229 llvm::Value *exn = nullptr; 1230 1231 // If there's a begin-catch function, call it. 1232 if (BeginCatchFn) { 1233 exn = CGF.getExceptionFromSlot(); 1234 CGF.EmitNounwindRuntimeCall(BeginCatchFn, exn); 1235 } 1236 1237 // If we need to remember the exception pointer to rethrow later, do so. 1238 if (SavedExnVar) { 1239 if (!exn) exn = CGF.getExceptionFromSlot(); 1240 CGF.Builder.CreateStore(exn, SavedExnVar); 1241 } 1242 1243 // Tell the cleanups in the finally block that we're do this for EH. 1244 CGF.Builder.CreateStore(CGF.Builder.getTrue(), ForEHVar); 1245 1246 // Thread a jump through the finally cleanup. 1247 CGF.EmitBranchThroughCleanup(RethrowDest); 1248 1249 CGF.Builder.restoreIP(savedIP); 1250 } 1251 1252 // Finally, leave the @finally cleanup. 1253 CGF.PopCleanupBlock(); 1254 } 1255 1256 llvm::BasicBlock *CodeGenFunction::getTerminateLandingPad() { 1257 if (TerminateLandingPad) 1258 return TerminateLandingPad; 1259 1260 CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP(); 1261 1262 // This will get inserted at the end of the function. 1263 TerminateLandingPad = createBasicBlock("terminate.lpad"); 1264 Builder.SetInsertPoint(TerminateLandingPad); 1265 1266 // Tell the backend that this is a landing pad. 1267 const EHPersonality &Personality = EHPersonality::get(*this); 1268 llvm::LandingPadInst *LPadInst = 1269 Builder.CreateLandingPad(llvm::StructType::get(Int8PtrTy, Int32Ty, nullptr), 1270 getOpaquePersonalityFn(CGM, Personality), 0); 1271 LPadInst->addClause(getCatchAllValue(*this)); 1272 1273 llvm::Value *Exn = 0; 1274 if (getLangOpts().CPlusPlus) 1275 Exn = Builder.CreateExtractValue(LPadInst, 0); 1276 llvm::CallInst *terminateCall = 1277 CGM.getCXXABI().emitTerminateForUnexpectedException(*this, Exn); 1278 terminateCall->setDoesNotReturn(); 1279 Builder.CreateUnreachable(); 1280 1281 // Restore the saved insertion state. 1282 Builder.restoreIP(SavedIP); 1283 1284 return TerminateLandingPad; 1285 } 1286 1287 llvm::BasicBlock *CodeGenFunction::getTerminateHandler() { 1288 if (TerminateHandler) 1289 return TerminateHandler; 1290 1291 CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP(); 1292 1293 // Set up the terminate handler. This block is inserted at the very 1294 // end of the function by FinishFunction. 1295 TerminateHandler = createBasicBlock("terminate.handler"); 1296 Builder.SetInsertPoint(TerminateHandler); 1297 llvm::Value *Exn = 0; 1298 if (getLangOpts().CPlusPlus) 1299 Exn = getExceptionFromSlot(); 1300 llvm::CallInst *terminateCall = 1301 CGM.getCXXABI().emitTerminateForUnexpectedException(*this, Exn); 1302 terminateCall->setDoesNotReturn(); 1303 Builder.CreateUnreachable(); 1304 1305 // Restore the saved insertion state. 1306 Builder.restoreIP(SavedIP); 1307 1308 return TerminateHandler; 1309 } 1310 1311 llvm::BasicBlock *CodeGenFunction::getEHResumeBlock(bool isCleanup) { 1312 if (EHResumeBlock) return EHResumeBlock; 1313 1314 CGBuilderTy::InsertPoint SavedIP = Builder.saveIP(); 1315 1316 // We emit a jump to a notional label at the outermost unwind state. 1317 EHResumeBlock = createBasicBlock("eh.resume"); 1318 Builder.SetInsertPoint(EHResumeBlock); 1319 1320 const EHPersonality &Personality = EHPersonality::get(*this); 1321 1322 // This can always be a call because we necessarily didn't find 1323 // anything on the EH stack which needs our help. 1324 const char *RethrowName = Personality.CatchallRethrowFn; 1325 if (RethrowName != nullptr && !isCleanup) { 1326 EmitRuntimeCall(getCatchallRethrowFn(CGM, RethrowName), 1327 getExceptionFromSlot())->setDoesNotReturn(); 1328 Builder.CreateUnreachable(); 1329 Builder.restoreIP(SavedIP); 1330 return EHResumeBlock; 1331 } 1332 1333 // Recreate the landingpad's return value for the 'resume' instruction. 1334 llvm::Value *Exn = getExceptionFromSlot(); 1335 llvm::Value *Sel = getSelectorFromSlot(); 1336 1337 llvm::Type *LPadType = llvm::StructType::get(Exn->getType(), 1338 Sel->getType(), nullptr); 1339 llvm::Value *LPadVal = llvm::UndefValue::get(LPadType); 1340 LPadVal = Builder.CreateInsertValue(LPadVal, Exn, 0, "lpad.val"); 1341 LPadVal = Builder.CreateInsertValue(LPadVal, Sel, 1, "lpad.val"); 1342 1343 Builder.CreateResume(LPadVal); 1344 Builder.restoreIP(SavedIP); 1345 return EHResumeBlock; 1346 } 1347 1348 void CodeGenFunction::EmitSEHTryStmt(const SEHTryStmt &S) { 1349 // FIXME: Implement SEH on other architectures. 1350 const llvm::Triple &T = CGM.getTarget().getTriple(); 1351 if (T.getArch() != llvm::Triple::x86_64 || 1352 !T.isKnownWindowsMSVCEnvironment()) { 1353 ErrorUnsupported(&S, "__try statement"); 1354 return; 1355 } 1356 1357 SEHFinallyInfo FI; 1358 EnterSEHTryStmt(S, FI); 1359 { 1360 JumpDest TryExit = getJumpDestInCurrentScope("__try.__leave"); 1361 1362 SEHTryEpilogueStack.push_back(&TryExit); 1363 EmitStmt(S.getTryBlock()); 1364 SEHTryEpilogueStack.pop_back(); 1365 1366 if (!TryExit.getBlock()->use_empty()) 1367 EmitBlock(TryExit.getBlock(), /*IsFinished=*/true); 1368 else 1369 delete TryExit.getBlock(); 1370 } 1371 ExitSEHTryStmt(S, FI); 1372 } 1373 1374 namespace { 1375 struct PerformSEHFinally : EHScopeStack::Cleanup { 1376 CodeGenFunction::SEHFinallyInfo *FI; 1377 PerformSEHFinally(CodeGenFunction::SEHFinallyInfo *FI) : FI(FI) {} 1378 1379 void Emit(CodeGenFunction &CGF, Flags F) override { 1380 // Cleanups are emitted at most twice: once for normal control flow and once 1381 // for exception control flow. Branch into the finally block, and remember 1382 // the continuation block so we can branch out later. 1383 if (!FI->FinallyBB) { 1384 FI->FinallyBB = CGF.createBasicBlock("__finally"); 1385 FI->FinallyBB->insertInto(CGF.CurFn); 1386 FI->FinallyBB->moveAfter(CGF.Builder.GetInsertBlock()); 1387 } 1388 1389 // Set the termination status and branch in. 1390 CGF.Builder.CreateStore( 1391 llvm::ConstantInt::get(CGF.Int8Ty, F.isForEHCleanup()), 1392 CGF.getAbnormalTerminationSlot()); 1393 CGF.Builder.CreateBr(FI->FinallyBB); 1394 1395 // Create a continuation block for normal or exceptional control. 1396 if (F.isForEHCleanup()) { 1397 assert(!FI->ResumeBB && "double emission for EH"); 1398 FI->ResumeBB = CGF.createBasicBlock("__finally.resume"); 1399 CGF.EmitBlock(FI->ResumeBB); 1400 } else { 1401 assert(F.isForNormalCleanup() && !FI->ContBB && "double normal emission"); 1402 FI->ContBB = CGF.createBasicBlock("__finally.cont"); 1403 CGF.EmitBlock(FI->ContBB); 1404 // Try to keep source order. 1405 FI->ContBB->moveAfter(FI->FinallyBB); 1406 } 1407 } 1408 }; 1409 } 1410 1411 /// Create a stub filter function that will ultimately hold the code of the 1412 /// filter expression. The EH preparation passes in LLVM will outline the code 1413 /// from the main function body into this stub. 1414 llvm::Function * 1415 CodeGenFunction::GenerateSEHFilterFunction(CodeGenFunction &ParentCGF, 1416 const SEHExceptStmt &Except) { 1417 const Decl *ParentCodeDecl = ParentCGF.CurCodeDecl; 1418 llvm::Function *ParentFn = ParentCGF.CurFn; 1419 1420 Expr *FilterExpr = Except.getFilterExpr(); 1421 1422 // Get the mangled function name. 1423 SmallString<128> Name; 1424 { 1425 llvm::raw_svector_ostream OS(Name); 1426 const NamedDecl *Parent = dyn_cast_or_null<NamedDecl>(ParentCodeDecl); 1427 assert(Parent && "FIXME: handle unnamed decls (lambdas, blocks) with SEH"); 1428 CGM.getCXXABI().getMangleContext().mangleSEHFilterExpression(Parent, OS); 1429 } 1430 1431 // Arrange a function with the declaration: 1432 // int filt(EXCEPTION_POINTERS *exception_pointers, void *frame_pointer) 1433 QualType RetTy = getContext().IntTy; 1434 FunctionArgList Args; 1435 SEHPointersDecl = ImplicitParamDecl::Create( 1436 getContext(), nullptr, FilterExpr->getLocStart(), 1437 &getContext().Idents.get("exception_pointers"), getContext().VoidPtrTy); 1438 Args.push_back(SEHPointersDecl); 1439 Args.push_back(ImplicitParamDecl::Create( 1440 getContext(), nullptr, FilterExpr->getLocStart(), 1441 &getContext().Idents.get("frame_pointer"), getContext().VoidPtrTy)); 1442 const CGFunctionInfo &FnInfo = CGM.getTypes().arrangeFreeFunctionDeclaration( 1443 RetTy, Args, FunctionType::ExtInfo(), /*isVariadic=*/false); 1444 llvm::FunctionType *FnTy = CGM.getTypes().GetFunctionType(FnInfo); 1445 llvm::Function *Fn = llvm::Function::Create(FnTy, ParentFn->getLinkage(), 1446 Name.str(), &CGM.getModule()); 1447 // The filter is either in the same comdat as the function, or it's internal. 1448 if (llvm::Comdat *C = ParentFn->getComdat()) { 1449 Fn->setComdat(C); 1450 } else if (ParentFn->hasWeakLinkage() || ParentFn->hasLinkOnceLinkage()) { 1451 // FIXME: Unreachable with Rafael's changes? 1452 llvm::Comdat *C = CGM.getModule().getOrInsertComdat(ParentFn->getName()); 1453 ParentFn->setComdat(C); 1454 Fn->setComdat(C); 1455 } else { 1456 Fn->setLinkage(llvm::GlobalValue::InternalLinkage); 1457 } 1458 1459 StartFunction(GlobalDecl(), RetTy, Fn, FnInfo, Args, 1460 FilterExpr->getLocStart(), FilterExpr->getLocStart()); 1461 1462 EmitSEHExceptionCodeSave(); 1463 1464 // Insert dummy allocas for every local variable in scope. We'll initialize 1465 // them and prune the unused ones after we find out which ones were 1466 // referenced. 1467 for (const auto &DeclPtrs : ParentCGF.LocalDeclMap) { 1468 const Decl *VD = DeclPtrs.first; 1469 llvm::Value *Ptr = DeclPtrs.second; 1470 auto *ValTy = cast<llvm::PointerType>(Ptr->getType())->getElementType(); 1471 LocalDeclMap[VD] = CreateTempAlloca(ValTy, Ptr->getName() + ".filt"); 1472 } 1473 1474 // Emit the original filter expression, convert to i32, and return. 1475 llvm::Value *R = EmitScalarExpr(FilterExpr); 1476 R = Builder.CreateIntCast(R, CGM.IntTy, 1477 FilterExpr->getType()->isSignedIntegerType()); 1478 Builder.CreateStore(R, ReturnValue); 1479 1480 FinishFunction(FilterExpr->getLocEnd()); 1481 1482 for (const auto &DeclPtrs : ParentCGF.LocalDeclMap) { 1483 const Decl *VD = DeclPtrs.first; 1484 auto *Alloca = cast<llvm::AllocaInst>(LocalDeclMap[VD]); 1485 if (Alloca->hasNUses(0)) { 1486 Alloca->eraseFromParent(); 1487 continue; 1488 } 1489 ErrorUnsupported(FilterExpr, 1490 "SEH filter expression local variable capture"); 1491 } 1492 1493 return Fn; 1494 } 1495 1496 void CodeGenFunction::EmitSEHExceptionCodeSave() { 1497 // Save the exception code in the exception slot to unify exception access in 1498 // the filter function and the landing pad. 1499 // struct EXCEPTION_POINTERS { 1500 // EXCEPTION_RECORD *ExceptionRecord; 1501 // CONTEXT *ContextRecord; 1502 // }; 1503 // void *exn.slot = 1504 // (void *)(uintptr_t)exception_pointers->ExceptionRecord->ExceptionCode; 1505 llvm::Value *Ptrs = Builder.CreateLoad(GetAddrOfLocalVar(SEHPointersDecl)); 1506 llvm::Type *RecordTy = CGM.Int32Ty->getPointerTo(); 1507 llvm::Type *PtrsTy = llvm::StructType::get(RecordTy, CGM.VoidPtrTy, nullptr); 1508 Ptrs = Builder.CreateBitCast(Ptrs, PtrsTy->getPointerTo()); 1509 llvm::Value *Rec = Builder.CreateStructGEP(Ptrs, 0); 1510 Rec = Builder.CreateLoad(Rec); 1511 llvm::Value *Code = Builder.CreateLoad(Rec); 1512 Code = Builder.CreateZExt(Code, CGM.IntPtrTy); 1513 // FIXME: Change landing pads to produce {i32, i32} and make the exception 1514 // slot an i32. 1515 Code = Builder.CreateIntToPtr(Code, CGM.VoidPtrTy); 1516 Builder.CreateStore(Code, getExceptionSlot()); 1517 } 1518 1519 llvm::Value *CodeGenFunction::EmitSEHExceptionInfo() { 1520 // Sema should diagnose calling this builtin outside of a filter context, but 1521 // don't crash if we screw up. 1522 if (!SEHPointersDecl) 1523 return llvm::UndefValue::get(Int8PtrTy); 1524 return Builder.CreateLoad(GetAddrOfLocalVar(SEHPointersDecl)); 1525 } 1526 1527 llvm::Value *CodeGenFunction::EmitSEHExceptionCode() { 1528 // If we're in a landing pad or filter function, the exception slot contains 1529 // the code. 1530 assert(ExceptionSlot); 1531 llvm::Value *Code = 1532 Builder.CreatePtrToInt(getExceptionFromSlot(), CGM.IntPtrTy); 1533 return Builder.CreateTrunc(Code, CGM.Int32Ty); 1534 } 1535 1536 llvm::Value *CodeGenFunction::EmitSEHAbnormalTermination() { 1537 // Load from the abnormal termination slot. It will be uninitialized outside 1538 // of __finally blocks, which we should warn or error on. 1539 llvm::Value *IsEH = Builder.CreateLoad(getAbnormalTerminationSlot()); 1540 return Builder.CreateZExt(IsEH, Int32Ty); 1541 } 1542 1543 void CodeGenFunction::EnterSEHTryStmt(const SEHTryStmt &S, SEHFinallyInfo &FI) { 1544 if (S.getFinallyHandler()) { 1545 // Push a cleanup for __finally blocks. 1546 EHStack.pushCleanup<PerformSEHFinally>(NormalAndEHCleanup, &FI); 1547 return; 1548 } 1549 1550 // Otherwise, we must have an __except block. 1551 SEHExceptStmt *Except = S.getExceptHandler(); 1552 assert(Except); 1553 EHCatchScope *CatchScope = EHStack.pushCatch(1); 1554 1555 // If the filter is known to evaluate to 1, then we can use the clause "catch 1556 // i8* null". 1557 llvm::Constant *C = 1558 CGM.EmitConstantExpr(Except->getFilterExpr(), getContext().IntTy, this); 1559 if (C && C->isOneValue()) { 1560 CatchScope->setCatchAllHandler(0, createBasicBlock("__except")); 1561 return; 1562 } 1563 1564 // In general, we have to emit an outlined filter function. Use the function 1565 // in place of the RTTI typeinfo global that C++ EH uses. 1566 CodeGenFunction FilterCGF(CGM, /*suppressNewContext=*/true); 1567 llvm::Function *FilterFunc = 1568 FilterCGF.GenerateSEHFilterFunction(*this, *Except); 1569 llvm::Constant *OpaqueFunc = 1570 llvm::ConstantExpr::getBitCast(FilterFunc, Int8PtrTy); 1571 CatchScope->setHandler(0, OpaqueFunc, createBasicBlock("__except")); 1572 } 1573 1574 void CodeGenFunction::ExitSEHTryStmt(const SEHTryStmt &S, SEHFinallyInfo &FI) { 1575 // Just pop the cleanup if it's a __finally block. 1576 if (const SEHFinallyStmt *Finally = S.getFinallyHandler()) { 1577 PopCleanupBlock(); 1578 assert(FI.ContBB && "did not emit normal cleanup"); 1579 1580 // Emit the code into FinallyBB. 1581 CGBuilderTy::InsertPoint SavedIP = Builder.saveIP(); 1582 Builder.SetInsertPoint(FI.FinallyBB); 1583 EmitStmt(Finally->getBlock()); 1584 1585 if (HaveInsertPoint()) { 1586 if (FI.ResumeBB) { 1587 llvm::Value *IsEH = Builder.CreateLoad(getAbnormalTerminationSlot(), 1588 "abnormal.termination"); 1589 IsEH = Builder.CreateICmpEQ(IsEH, llvm::ConstantInt::get(Int8Ty, 0)); 1590 Builder.CreateCondBr(IsEH, FI.ContBB, FI.ResumeBB); 1591 } else { 1592 // There was nothing exceptional in the try body, so we only have normal 1593 // control flow. 1594 Builder.CreateBr(FI.ContBB); 1595 } 1596 } 1597 1598 Builder.restoreIP(SavedIP); 1599 1600 return; 1601 } 1602 1603 // Otherwise, we must have an __except block. 1604 const SEHExceptStmt *Except = S.getExceptHandler(); 1605 assert(Except && "__try must have __finally xor __except"); 1606 EHCatchScope &CatchScope = cast<EHCatchScope>(*EHStack.begin()); 1607 1608 // Don't emit the __except block if the __try block lacked invokes. 1609 // TODO: Model unwind edges from instructions, either with iload / istore or 1610 // a try body function. 1611 if (!CatchScope.hasEHBranches()) { 1612 CatchScope.clearHandlerBlocks(); 1613 EHStack.popCatch(); 1614 return; 1615 } 1616 1617 // The fall-through block. 1618 llvm::BasicBlock *ContBB = createBasicBlock("__try.cont"); 1619 1620 // We just emitted the body of the __try; jump to the continue block. 1621 if (HaveInsertPoint()) 1622 Builder.CreateBr(ContBB); 1623 1624 // Check if our filter function returned true. 1625 emitCatchDispatchBlock(*this, CatchScope); 1626 1627 // Grab the block before we pop the handler. 1628 llvm::BasicBlock *ExceptBB = CatchScope.getHandler(0).Block; 1629 EHStack.popCatch(); 1630 1631 EmitBlockAfterUses(ExceptBB); 1632 1633 // Emit the __except body. 1634 EmitStmt(Except->getBlock()); 1635 1636 if (HaveInsertPoint()) 1637 Builder.CreateBr(ContBB); 1638 1639 EmitBlock(ContBB); 1640 } 1641 1642 void CodeGenFunction::EmitSEHLeaveStmt(const SEHLeaveStmt &S) { 1643 // If this code is reachable then emit a stop point (if generating 1644 // debug info). We have to do this ourselves because we are on the 1645 // "simple" statement path. 1646 if (HaveInsertPoint()) 1647 EmitStopPoint(&S); 1648 1649 assert(!SEHTryEpilogueStack.empty() && 1650 "sema should have rejected this __leave"); 1651 EmitBranchThroughCleanup(*SEHTryEpilogueStack.back()); 1652 } 1653