LCOV - code coverage report
Current view: top level - lib/CodeGen/AsmPrinter - WinException.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 480 495 97.0 %
Date: 2018-10-20 13:21:21 Functions: 23 25 92.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===-- CodeGen/AsmPrinter/WinException.cpp - Dwarf Exception Impl ------===//
       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 file contains support for writing Win64 exception info into asm files.
      11             : //
      12             : //===----------------------------------------------------------------------===//
      13             : 
      14             : #include "WinException.h"
      15             : #include "llvm/ADT/Twine.h"
      16             : #include "llvm/BinaryFormat/COFF.h"
      17             : #include "llvm/BinaryFormat/Dwarf.h"
      18             : #include "llvm/CodeGen/AsmPrinter.h"
      19             : #include "llvm/CodeGen/MachineFrameInfo.h"
      20             : #include "llvm/CodeGen/MachineFunction.h"
      21             : #include "llvm/CodeGen/MachineModuleInfo.h"
      22             : #include "llvm/CodeGen/TargetFrameLowering.h"
      23             : #include "llvm/CodeGen/TargetLowering.h"
      24             : #include "llvm/CodeGen/TargetSubtargetInfo.h"
      25             : #include "llvm/CodeGen/WinEHFuncInfo.h"
      26             : #include "llvm/IR/DataLayout.h"
      27             : #include "llvm/IR/Mangler.h"
      28             : #include "llvm/IR/Module.h"
      29             : #include "llvm/MC/MCAsmInfo.h"
      30             : #include "llvm/MC/MCContext.h"
      31             : #include "llvm/MC/MCExpr.h"
      32             : #include "llvm/MC/MCSection.h"
      33             : #include "llvm/MC/MCStreamer.h"
      34             : #include "llvm/MC/MCSymbol.h"
      35             : #include "llvm/Support/ErrorHandling.h"
      36             : #include "llvm/Support/FormattedStream.h"
      37             : #include "llvm/Target/TargetLoweringObjectFile.h"
      38             : #include "llvm/Target/TargetOptions.h"
      39             : using namespace llvm;
      40             : 
      41         604 : WinException::WinException(AsmPrinter *A) : EHStreamer(A) {
      42             :   // MSVC's EH tables are always composed of 32-bit words.  All known 64-bit
      43             :   // platforms use an imagerel32 relocation to refer to symbols.
      44         604 :   useImageRel32 = (A->getDataLayout().getPointerSizeInBits() == 64);
      45         604 : }
      46             : 
      47         600 : WinException::~WinException() {}
      48             : 
      49             : /// endModule - Emit all exception information that should come after the
      50             : /// content.
      51         600 : void WinException::endModule() {
      52         600 :   auto &OS = *Asm->OutStreamer;
      53         600 :   const Module *M = MMI->getModule();
      54        4703 :   for (const Function &F : *M)
      55        4103 :     if (F.hasFnAttribute("safeseh"))
      56          31 :       OS.EmitCOFFSafeSEH(Asm->getSymbol(&F));
      57         600 : }
      58             : 
      59        2494 : void WinException::beginFunction(const MachineFunction *MF) {
      60        2494 :   shouldEmitMoves = shouldEmitPersonality = shouldEmitLSDA = false;
      61             : 
      62             :   // If any landing pads survive, we need an EH table.
      63        2494 :   bool hasLandingPads = !MF->getLandingPads().empty();
      64        2494 :   bool hasEHFunclets = MF->hasEHFunclets();
      65             : 
      66        2494 :   const Function &F = MF->getFunction();
      67             : 
      68        2494 :   shouldEmitMoves = Asm->needsSEHMoves() && MF->hasWinCFI();
      69             : 
      70        2494 :   const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
      71        2494 :   unsigned PerEncoding = TLOF.getPersonalityEncoding();
      72             : 
      73             :   EHPersonality Per = EHPersonality::Unknown;
      74             :   const Function *PerFn = nullptr;
      75        2494 :   if (F.hasPersonalityFn()) {
      76         124 :     PerFn = dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts());
      77         124 :     Per = classifyEHPersonality(PerFn);
      78             :   }
      79             : 
      80             :   bool forceEmitPersonality = F.hasPersonalityFn() &&
      81        2502 :                               !isNoOpWithoutInvoke(Per) &&
      82           8 :                               F.needsUnwindTableEntry();
      83             : 
      84        2494 :   shouldEmitPersonality =
      85        2486 :       forceEmitPersonality || ((hasLandingPads || hasEHFunclets) &&
      86         111 :                                PerEncoding != dwarf::DW_EH_PE_omit && PerFn);
      87             : 
      88        2494 :   unsigned LSDAEncoding = TLOF.getLSDAEncoding();
      89        2494 :   shouldEmitLSDA = shouldEmitPersonality &&
      90             :     LSDAEncoding != dwarf::DW_EH_PE_omit;
      91             : 
      92             :   // If we're not using CFI, we don't want the CFI or the personality, but we
      93             :   // might want EH tables if we had EH pads.
      94        2494 :   if (!Asm->MAI->usesWindowsCFI()) {
      95         931 :     if (Per == EHPersonality::MSVC_X86SEH && !hasEHFunclets) {
      96             :       // If this is 32-bit SEH and we don't have any funclets (really invokes),
      97             :       // make sure we emit the parent offset label. Some unreferenced filter
      98             :       // functions may still refer to it.
      99           1 :       const WinEHFuncInfo &FuncInfo = *MF->getWinEHFuncInfo();
     100             :       StringRef FLinkageName =
     101           1 :           GlobalValue::dropLLVMManglingEscape(MF->getFunction().getName());
     102           1 :       emitEHRegistrationOffsetLabel(FuncInfo, FLinkageName);
     103             :     }
     104         931 :     shouldEmitLSDA = hasEHFunclets;
     105         931 :     shouldEmitPersonality = false;
     106         931 :     return;
     107             :   }
     108             : 
     109        3126 :   beginFunclet(MF->front(), Asm->CurrentFnSym);
     110             : }
     111             : 
     112             : /// endFunction - Gather and emit post-function exception information.
     113             : ///
     114        2494 : void WinException::endFunction(const MachineFunction *MF) {
     115        2494 :   if (!shouldEmitPersonality && !shouldEmitMoves && !shouldEmitLSDA)
     116             :     return;
     117             : 
     118         496 :   const Function &F = MF->getFunction();
     119             :   EHPersonality Per = EHPersonality::Unknown;
     120         496 :   if (F.hasPersonalityFn())
     121         222 :     Per = classifyEHPersonality(F.getPersonalityFn()->stripPointerCasts());
     122             : 
     123             :   // Get rid of any dead landing pads if we're not using funclets. In funclet
     124             :   // schemes, the landing pad is not actually reachable. It only exists so
     125             :   // that we can emit the right table data.
     126             :   if (!isFuncletEHPersonality(Per)) {
     127             :     MachineFunction *NonConstMF = const_cast<MachineFunction*>(MF);
     128         408 :     NonConstMF->tidyLandingPads();
     129             :   }
     130             : 
     131         496 :   endFunclet();
     132             : 
     133             :   // endFunclet will emit the necessary .xdata tables for x64 SEH.
     134         496 :   if (Per == EHPersonality::MSVC_Win64SEH && MF->hasEHFunclets())
     135             :     return;
     136             : 
     137         485 :   if (shouldEmitPersonality || shouldEmitLSDA) {
     138         194 :     Asm->OutStreamer->PushSection();
     139             : 
     140             :     // Just switch sections to the right xdata section.
     141         194 :     MCSection *XData = Asm->OutStreamer->getAssociatedXDataSection(
     142             :         Asm->OutStreamer->getCurrentSectionOnly());
     143         194 :     Asm->OutStreamer->SwitchSection(XData);
     144             : 
     145             :     // Emit the tables appropriate to the personality function in use. If we
     146             :     // don't recognize the personality, assume it uses an Itanium-style LSDA.
     147          97 :     if (Per == EHPersonality::MSVC_Win64SEH)
     148           0 :       emitCSpecificHandlerTable(MF);
     149          97 :     else if (Per == EHPersonality::MSVC_X86SEH)
     150          13 :       emitExceptHandlerTable(MF);
     151          84 :     else if (Per == EHPersonality::MSVC_CXX)
     152          56 :       emitCXXFrameHandler3Table(MF);
     153          28 :     else if (Per == EHPersonality::CoreCLR)
     154           7 :       emitCLRExceptionTable(MF);
     155             :     else
     156          21 :       emitExceptionTable();
     157             : 
     158         194 :     Asm->OutStreamer->PopSection();
     159             :   }
     160             : }
     161             : 
     162             : /// Retrieve the MCSymbol for a GlobalValue or MachineBasicBlock.
     163           0 : static MCSymbol *getMCSymbolForMBB(AsmPrinter *Asm,
     164             :                                    const MachineBasicBlock *MBB) {
     165           0 :   if (!MBB)
     166           0 :     return nullptr;
     167             : 
     168             :   assert(MBB->isEHFuncletEntry());
     169             : 
     170             :   // Give catches and cleanups a name based off of their parent function and
     171             :   // their funclet entry block's number.
     172           0 :   const MachineFunction *MF = MBB->getParent();
     173           0 :   const Function &F = MF->getFunction();
     174           0 :   StringRef FuncLinkageName = GlobalValue::dropLLVMManglingEscape(F.getName());
     175           0 :   MCContext &Ctx = MF->getContext();
     176           0 :   StringRef HandlerPrefix = MBB->isCleanupFuncletEntry() ? "dtor" : "catch";
     177           0 :   return Ctx.getOrCreateSymbol("?" + HandlerPrefix + "$" +
     178           0 :                                Twine(MBB->getNumber()) + "@?0?" +
     179           0 :                                FuncLinkageName + "@4HA");
     180             : }
     181             : 
     182        1680 : void WinException::beginFunclet(const MachineBasicBlock &MBB,
     183             :                                 MCSymbol *Sym) {
     184        1680 :   CurrentFuncletEntry = &MBB;
     185             : 
     186        1680 :   const Function &F = Asm->MF->getFunction();
     187             :   // If a symbol was not provided for the funclet, invent one.
     188        1680 :   if (!Sym) {
     189         117 :     Sym = getMCSymbolForMBB(Asm, &MBB);
     190             : 
     191             :     // Describe our funclet symbol as a function with internal linkage.
     192         234 :     Asm->OutStreamer->BeginCOFFSymbolDef(Sym);
     193         234 :     Asm->OutStreamer->EmitCOFFSymbolStorageClass(COFF::IMAGE_SYM_CLASS_STATIC);
     194         117 :     Asm->OutStreamer->EmitCOFFSymbolType(COFF::IMAGE_SYM_DTYPE_FUNCTION
     195         117 :                                          << COFF::SCT_COMPLEX_TYPE_SHIFT);
     196         234 :     Asm->OutStreamer->EndCOFFSymbolDef();
     197             : 
     198             :     // We want our funclet's entry point to be aligned such that no nops will be
     199             :     // present after the label.
     200         117 :     Asm->EmitAlignment(std::max(Asm->MF->getAlignment(), MBB.getAlignment()),
     201             :                        &F);
     202             : 
     203             :     // Now that we've emitted the alignment directive, point at our funclet.
     204         234 :     Asm->OutStreamer->EmitLabel(Sym);
     205             :   }
     206             : 
     207             :   // Mark 'Sym' as starting our funclet.
     208        1680 :   if (shouldEmitMoves || shouldEmitPersonality) {
     209         547 :     CurrentFuncletTextSection = Asm->OutStreamer->getCurrentSectionOnly();
     210         547 :     Asm->OutStreamer->EmitWinCFIStartProc(Sym);
     211             :   }
     212             : 
     213        1680 :   if (shouldEmitPersonality) {
     214         159 :     const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
     215             :     const Function *PerFn = nullptr;
     216             : 
     217             :     // Determine which personality routine we are using for this funclet.
     218         159 :     if (F.hasPersonalityFn())
     219         159 :       PerFn = dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts());
     220             :     const MCSymbol *PersHandlerSym =
     221         159 :         TLOF.getCFIPersonalitySymbol(PerFn, Asm->TM, MMI);
     222             : 
     223             :     // Do not emit a .seh_handler directives for cleanup funclets.
     224             :     // FIXME: This means cleanup funclets cannot handle exceptions. Given that
     225             :     // Clang doesn't produce EH constructs inside cleanup funclets and LLVM's
     226             :     // inliner doesn't allow inlining them, this isn't a major problem in
     227             :     // practice.
     228         159 :     if (!CurrentFuncletEntry->isCleanupFuncletEntry())
     229         262 :       Asm->OutStreamer->EmitWinEHHandler(PersHandlerSym, true, true);
     230             :   }
     231        1680 : }
     232             : 
     233         613 : void WinException::endFunclet() {
     234             :   // No funclet to process?  Great, we have nothing to do.
     235         613 :   if (!CurrentFuncletEntry)
     236             :     return;
     237             : 
     238         579 :   const MachineFunction *MF = Asm->MF;
     239         579 :   if (shouldEmitMoves || shouldEmitPersonality) {
     240         547 :     const Function &F = MF->getFunction();
     241             :     EHPersonality Per = EHPersonality::Unknown;
     242         547 :     if (F.hasPersonalityFn())
     243         324 :       Per = classifyEHPersonality(F.getPersonalityFn()->stripPointerCasts());
     244             : 
     245             :     // Emit an UNWIND_INFO struct describing the prologue.
     246        1094 :     Asm->OutStreamer->EmitWinEHHandlerData();
     247             : 
     248         547 :     if (Per == EHPersonality::MSVC_CXX && shouldEmitPersonality &&
     249          96 :         !CurrentFuncletEntry->isCleanupFuncletEntry()) {
     250             :       // If this is a C++ catch funclet (or the parent function),
     251             :       // emit a reference to the LSDA for the parent function.
     252          81 :       StringRef FuncLinkageName = GlobalValue::dropLLVMManglingEscape(F.getName());
     253         162 :       MCSymbol *FuncInfoXData = Asm->OutContext.getOrCreateSymbol(
     254          81 :           Twine("$cppxdata$", FuncLinkageName));
     255         162 :       Asm->OutStreamer->EmitValue(create32bitRef(FuncInfoXData), 4);
     256         466 :     } else if (Per == EHPersonality::MSVC_Win64SEH && MF->hasEHFunclets() &&
     257          16 :                !CurrentFuncletEntry->isEHFuncletEntry()) {
     258             :       // If this is the parent function in Win64 SEH, emit the LSDA immediately
     259             :       // following .seh_handlerdata.
     260          11 :       emitCSpecificHandlerTable(MF);
     261             :     }
     262             : 
     263             :     // Switch back to the funclet start .text section now that we are done
     264             :     // writing to .xdata, and emit an .seh_endproc directive to mark the end of
     265             :     // the function.
     266        1094 :     Asm->OutStreamer->SwitchSection(CurrentFuncletTextSection);
     267        1094 :     Asm->OutStreamer->EmitWinCFIEndProc();
     268             :   }
     269             : 
     270             :   // Let's make sure we don't try to end the same funclet twice.
     271         579 :   CurrentFuncletEntry = nullptr;
     272             : }
     273             : 
     274         820 : const MCExpr *WinException::create32bitRef(const MCSymbol *Value) {
     275         820 :   if (!Value)
     276         150 :     return MCConstantExpr::create(0, Asm->OutContext);
     277         670 :   return MCSymbolRefExpr::create(Value, useImageRel32
     278             :                                             ? MCSymbolRefExpr::VK_COFF_IMGREL32
     279             :                                             : MCSymbolRefExpr::VK_None,
     280         670 :                                  Asm->OutContext);
     281             : }
     282             : 
     283          98 : const MCExpr *WinException::create32bitRef(const GlobalValue *GV) {
     284          98 :   if (!GV)
     285          49 :     return MCConstantExpr::create(0, Asm->OutContext);
     286          49 :   return create32bitRef(Asm->getSymbol(GV));
     287             : }
     288             : 
     289         130 : const MCExpr *WinException::getLabelPlusOne(const MCSymbol *Label) {
     290         130 :   return MCBinaryExpr::createAdd(create32bitRef(Label),
     291         130 :                                  MCConstantExpr::create(1, Asm->OutContext),
     292         130 :                                  Asm->OutContext);
     293             : }
     294             : 
     295         141 : const MCExpr *WinException::getOffset(const MCSymbol *OffsetOf,
     296             :                                       const MCSymbol *OffsetFrom) {
     297         141 :   return MCBinaryExpr::createSub(
     298         141 :       MCSymbolRefExpr::create(OffsetOf, Asm->OutContext),
     299         141 :       MCSymbolRefExpr::create(OffsetFrom, Asm->OutContext), Asm->OutContext);
     300             : }
     301             : 
     302          52 : const MCExpr *WinException::getOffsetPlusOne(const MCSymbol *OffsetOf,
     303             :                                              const MCSymbol *OffsetFrom) {
     304          52 :   return MCBinaryExpr::createAdd(getOffset(OffsetOf, OffsetFrom),
     305          52 :                                  MCConstantExpr::create(1, Asm->OutContext),
     306          52 :                                  Asm->OutContext);
     307             : }
     308             : 
     309          43 : int WinException::getFrameIndexOffset(int FrameIndex,
     310             :                                       const WinEHFuncInfo &FuncInfo) {
     311          43 :   const TargetFrameLowering &TFI = *Asm->MF->getSubtarget().getFrameLowering();
     312             :   unsigned UnusedReg;
     313          43 :   if (Asm->MAI->usesWindowsCFI()) {
     314             :     int Offset =
     315          41 :         TFI.getFrameIndexReferencePreferSP(*Asm->MF, FrameIndex, UnusedReg,
     316          41 :                                            /*IgnoreSPUpdates*/ true);
     317             :     assert(UnusedReg ==
     318             :            Asm->MF->getSubtarget()
     319             :                .getTargetLowering()
     320             :                ->getStackPointerRegisterToSaveRestore());
     321          41 :     return Offset;
     322             :   }
     323             : 
     324             :   // For 32-bit, offsets should be relative to the end of the EH registration
     325             :   // node. For 64-bit, it's relative to SP at the end of the prologue.
     326             :   assert(FuncInfo.EHRegNodeEndOffset != INT_MAX);
     327           2 :   int Offset = TFI.getFrameIndexReference(*Asm->MF, FrameIndex, UnusedReg);
     328           2 :   Offset += FuncInfo.EHRegNodeEndOffset;
     329           2 :   return Offset;
     330             : }
     331             : 
     332             : namespace {
     333             : 
     334             : /// Top-level state used to represent unwind to caller
     335             : const int NullState = -1;
     336             : 
     337             : struct InvokeStateChange {
     338             :   /// EH Label immediately after the last invoke in the previous state, or
     339             :   /// nullptr if the previous state was the null state.
     340             :   const MCSymbol *PreviousEndLabel;
     341             : 
     342             :   /// EH label immediately before the first invoke in the new state, or nullptr
     343             :   /// if the new state is the null state.
     344             :   const MCSymbol *NewStartLabel;
     345             : 
     346             :   /// State of the invoke following NewStartLabel, or NullState to indicate
     347             :   /// the presence of calls which may unwind to caller.
     348             :   int NewState;
     349             : };
     350             : 
     351             : /// Iterator that reports all the invoke state changes in a range of machine
     352             : /// basic blocks.  Changes to the null state are reported whenever a call that
     353             : /// may unwind to caller is encountered.  The MBB range is expected to be an
     354             : /// entire function or funclet, and the start and end of the range are treated
     355             : /// as being in the NullState even if there's not an unwind-to-caller call
     356             : /// before the first invoke or after the last one (i.e., the first state change
     357             : /// reported is the first change to something other than NullState, and a
     358             : /// change back to NullState is always reported at the end of iteration).
     359             : class InvokeStateChangeIterator {
     360             :   InvokeStateChangeIterator(const WinEHFuncInfo &EHInfo,
     361             :                             MachineFunction::const_iterator MFI,
     362             :                             MachineFunction::const_iterator MFE,
     363             :                             MachineBasicBlock::const_iterator MBBI,
     364             :                             int BaseState)
     365         236 :       : EHInfo(EHInfo), MFI(MFI), MFE(MFE), MBBI(MBBI), BaseState(BaseState) {
     366         236 :     LastStateChange.PreviousEndLabel = nullptr;
     367         236 :     LastStateChange.NewStartLabel = nullptr;
     368         236 :     LastStateChange.NewState = BaseState;
     369         118 :     scan();
     370             :   }
     371             : 
     372             : public:
     373             :   static iterator_range<InvokeStateChangeIterator>
     374         118 :   range(const WinEHFuncInfo &EHInfo, MachineFunction::const_iterator Begin,
     375             :         MachineFunction::const_iterator End, int BaseState = NullState) {
     376             :     // Reject empty ranges to simplify bookkeeping by ensuring that we can get
     377             :     // the end of the last block.
     378             :     assert(Begin != End);
     379             :     auto BlockBegin = Begin->begin();
     380             :     auto BlockEnd = std::prev(End)->end();
     381             :     return make_range(
     382             :         InvokeStateChangeIterator(EHInfo, Begin, End, BlockBegin, BaseState),
     383         236 :         InvokeStateChangeIterator(EHInfo, End, End, BlockEnd, BaseState));
     384             :   }
     385             : 
     386             :   // Iterator methods.
     387             :   bool operator==(const InvokeStateChangeIterator &O) const {
     388             :     assert(BaseState == O.BaseState);
     389             :     // Must be visiting same block.
     390         267 :     if (MFI != O.MFI)
     391             :       return false;
     392             :     // Must be visiting same isntr.
     393         176 :     if (MBBI != O.MBBI)
     394             :       return false;
     395             :     // At end of block/instr iteration, we can still have two distinct states:
     396             :     // one to report the final EndLabel, and another indicating the end of the
     397             :     // state change iteration.  Check for CurrentEndLabel equality to
     398             :     // distinguish these.
     399         176 :     return CurrentEndLabel == O.CurrentEndLabel;
     400             :   }
     401             : 
     402             :   bool operator!=(const InvokeStateChangeIterator &O) const {
     403             :     return !operator==(O);
     404             :   }
     405             :   InvokeStateChange &operator*() { return LastStateChange; }
     406             :   InvokeStateChange *operator->() { return &LastStateChange; }
     407          35 :   InvokeStateChangeIterator &operator++() { return scan(); }
     408             : 
     409             : private:
     410             :   InvokeStateChangeIterator &scan();
     411             : 
     412             :   const WinEHFuncInfo &EHInfo;
     413             :   const MCSymbol *CurrentEndLabel = nullptr;
     414             :   MachineFunction::const_iterator MFI;
     415             :   MachineFunction::const_iterator MFE;
     416             :   MachineBasicBlock::const_iterator MBBI;
     417             :   InvokeStateChange LastStateChange;
     418             :   bool VisitingInvoke = false;
     419             :   int BaseState;
     420             : };
     421             : 
     422             : } // end anonymous namespace
     423             : 
     424         385 : InvokeStateChangeIterator &InvokeStateChangeIterator::scan() {
     425             :   bool IsNewBlock = false;
     426         651 :   for (; MFI != MFE; ++MFI, IsNewBlock = true) {
     427         357 :     if (IsNewBlock)
     428         148 :       MBBI = MFI->begin();
     429        2303 :     for (auto MBBE = MFI->end(); MBBI != MBBE; ++MBBI) {
     430             :       const MachineInstr &MI = *MBBI;
     431        2129 :       if (!VisitingInvoke && LastStateChange.NewState != BaseState &&
     432        2054 :           MI.isCall() && !EHStreamer::callToNoUnwindFunction(&MI)) {
     433             :         // Indicate a change of state to the null state.  We don't have
     434             :         // start/end EH labels handy but the caller won't expect them for
     435             :         // null state regions.
     436           8 :         LastStateChange.PreviousEndLabel = CurrentEndLabel;
     437           8 :         LastStateChange.NewStartLabel = nullptr;
     438           8 :         LastStateChange.NewState = BaseState;
     439           8 :         CurrentEndLabel = nullptr;
     440             :         // Don't re-visit this instr on the next scan
     441             :         ++MBBI;
     442          91 :         return *this;
     443             :       }
     444             : 
     445             :       // All other state changes are at EH labels before/after invokes.
     446        2029 :       if (!MI.isEHLabel())
     447        1946 :         continue;
     448         184 :       MCSymbol *Label = MI.getOperand(0).getMCSymbol();
     449         184 :       if (Label == CurrentEndLabel) {
     450          92 :         VisitingInvoke = false;
     451          92 :         continue;
     452             :       }
     453          92 :       auto InvokeMapIter = EHInfo.LabelToStateMap.find(Label);
     454             :       // Ignore EH labels that aren't the ones inserted before an invoke
     455         184 :       if (InvokeMapIter == EHInfo.LabelToStateMap.end())
     456             :         continue;
     457             :       auto &StateAndEnd = InvokeMapIter->second;
     458          92 :       int NewState = StateAndEnd.first;
     459             :       // Keep track of the fact that we're between EH start/end labels so
     460             :       // we know not to treat the inoke we'll see as unwinding to caller.
     461          92 :       VisitingInvoke = true;
     462          92 :       if (NewState == LastStateChange.NewState) {
     463             :         // The state isn't actually changing here.  Record the new end and
     464             :         // keep going.
     465           9 :         CurrentEndLabel = StateAndEnd.second;
     466           9 :         continue;
     467             :       }
     468             :       // Found a state change to report
     469          83 :       LastStateChange.PreviousEndLabel = CurrentEndLabel;
     470          83 :       LastStateChange.NewStartLabel = Label;
     471          83 :       LastStateChange.NewState = NewState;
     472             :       // Start keeping track of the new current end
     473          83 :       CurrentEndLabel = StateAndEnd.second;
     474             :       // Don't re-visit this instr on the next scan
     475             :       ++MBBI;
     476          83 :       return *this;
     477             :     }
     478             :   }
     479             :   // Iteration hit the end of the block range.
     480         294 :   if (LastStateChange.NewState != BaseState) {
     481             :     // Report the end of the last new state
     482          58 :     LastStateChange.PreviousEndLabel = CurrentEndLabel;
     483          58 :     LastStateChange.NewStartLabel = nullptr;
     484          58 :     LastStateChange.NewState = BaseState;
     485             :     // Leave CurrentEndLabel non-null to distinguish this state from end.
     486             :     assert(CurrentEndLabel != nullptr);
     487          58 :     return *this;
     488             :   }
     489             :   // We've reported all state changes and hit the end state.
     490         236 :   CurrentEndLabel = nullptr;
     491         236 :   return *this;
     492             : }
     493             : 
     494             : /// Emit the language-specific data that __C_specific_handler expects.  This
     495             : /// handler lives in the x64 Microsoft C runtime and allows catching or cleaning
     496             : /// up after faults with __try, __except, and __finally.  The typeinfo values
     497             : /// are not really RTTI data, but pointers to filter functions that return an
     498             : /// integer (1, 0, or -1) indicating how to handle the exception. For __finally
     499             : /// blocks and other cleanups, the landing pad label is zero, and the filter
     500             : /// function is actually a cleanup handler with the same prototype.  A catch-all
     501             : /// entry is modeled with a null filter function field and a non-zero landing
     502             : /// pad label.
     503             : ///
     504             : /// Possible filter function return values:
     505             : ///   EXCEPTION_EXECUTE_HANDLER (1):
     506             : ///     Jump to the landing pad label after cleanups.
     507             : ///   EXCEPTION_CONTINUE_SEARCH (0):
     508             : ///     Continue searching this table or continue unwinding.
     509             : ///   EXCEPTION_CONTINUE_EXECUTION (-1):
     510             : ///     Resume execution at the trapping PC.
     511             : ///
     512             : /// Inferred table structure:
     513             : ///   struct Table {
     514             : ///     int NumEntries;
     515             : ///     struct Entry {
     516             : ///       imagerel32 LabelStart;
     517             : ///       imagerel32 LabelEnd;
     518             : ///       imagerel32 FilterOrFinally;  // One means catch-all.
     519             : ///       imagerel32 LabelLPad;        // Zero means __finally.
     520             : ///     } Entries[NumEntries];
     521             : ///   };
     522          11 : void WinException::emitCSpecificHandlerTable(const MachineFunction *MF) {
     523          11 :   auto &OS = *Asm->OutStreamer;
     524          11 :   MCContext &Ctx = Asm->OutContext;
     525          11 :   const WinEHFuncInfo &FuncInfo = *MF->getWinEHFuncInfo();
     526             : 
     527          11 :   bool VerboseAsm = OS.isVerboseAsm();
     528             :   auto AddComment = [&](const Twine &Comment) {
     529          11 :     if (VerboseAsm)
     530          11 :       OS.AddComment(Comment);
     531             :   };
     532             : 
     533             :   // Emit a label assignment with the SEH frame offset so we can use it for
     534             :   // llvm.x86.seh.recoverfp.
     535             :   StringRef FLinkageName =
     536          11 :       GlobalValue::dropLLVMManglingEscape(MF->getFunction().getName());
     537             :   MCSymbol *ParentFrameOffset =
     538          11 :       Ctx.getOrCreateParentFrameOffsetSymbol(FLinkageName);
     539             :   const MCExpr *MCOffset =
     540          11 :       MCConstantExpr::create(FuncInfo.SEHSetFrameOffset, Ctx);
     541          22 :   Asm->OutStreamer->EmitAssignment(ParentFrameOffset, MCOffset);
     542             : 
     543             :   // Use the assembler to compute the number of table entries through label
     544             :   // difference and division.
     545             :   MCSymbol *TableBegin =
     546          11 :       Ctx.createTempSymbol("lsda_begin", /*AlwaysAddSuffix=*/true);
     547             :   MCSymbol *TableEnd =
     548          11 :       Ctx.createTempSymbol("lsda_end", /*AlwaysAddSuffix=*/true);
     549          11 :   const MCExpr *LabelDiff = getOffset(TableEnd, TableBegin);
     550          11 :   const MCExpr *EntrySize = MCConstantExpr::create(16, Ctx);
     551             :   const MCExpr *EntryCount = MCBinaryExpr::createDiv(LabelDiff, EntrySize, Ctx);
     552          11 :   AddComment("Number of call sites");
     553          11 :   OS.EmitValue(EntryCount, 4);
     554             : 
     555          11 :   OS.EmitLabel(TableBegin);
     556             : 
     557             :   // Iterate over all the invoke try ranges. Unlike MSVC, LLVM currently only
     558             :   // models exceptions from invokes. LLVM also allows arbitrary reordering of
     559             :   // the code, so our tables end up looking a bit different. Rather than
     560             :   // trying to match MSVC's tables exactly, we emit a denormalized table.  For
     561             :   // each range of invokes in the same state, we emit table entries for all
     562             :   // the actions that would be taken in that state. This means our tables are
     563             :   // slightly bigger, which is OK.
     564             :   const MCSymbol *LastStartLabel = nullptr;
     565             :   int LastEHState = -1;
     566             :   // Break out before we enter into a finally funclet.
     567             :   // FIXME: We need to emit separate EH tables for cleanups.
     568             :   MachineFunction::const_iterator End = MF->end();
     569             :   MachineFunction::const_iterator Stop = std::next(MF->begin());
     570          44 :   while (Stop != End && !Stop->isEHFuncletEntry())
     571             :     ++Stop;
     572             :   for (const auto &StateChange :
     573          28 :        InvokeStateChangeIterator::range(FuncInfo, MF->begin(), Stop)) {
     574             :     // Emit all the actions for the state we just transitioned out of
     575             :     // if it was not the null state
     576          26 :     if (LastEHState != -1)
     577          15 :       emitSEHActionsForRange(FuncInfo, LastStartLabel,
     578          15 :                              StateChange.PreviousEndLabel, LastEHState);
     579          26 :     LastStartLabel = StateChange.NewStartLabel;
     580          26 :     LastEHState = StateChange.NewState;
     581             :   }
     582             : 
     583          11 :   OS.EmitLabel(TableEnd);
     584          11 : }
     585             : 
     586          15 : void WinException::emitSEHActionsForRange(const WinEHFuncInfo &FuncInfo,
     587             :                                           const MCSymbol *BeginLabel,
     588             :                                           const MCSymbol *EndLabel, int State) {
     589          15 :   auto &OS = *Asm->OutStreamer;
     590          15 :   MCContext &Ctx = Asm->OutContext;
     591             : 
     592          15 :   bool VerboseAsm = OS.isVerboseAsm();
     593             :   auto AddComment = [&](const Twine &Comment) {
     594          84 :     if (VerboseAsm)
     595          84 :       OS.AddComment(Comment);
     596             :   };
     597             : 
     598             :   assert(BeginLabel && EndLabel);
     599          36 :   while (State != -1) {
     600          21 :     const SEHUnwindMapEntry &UME = FuncInfo.SEHUnwindMap[State];
     601             :     const MCExpr *FilterOrFinally;
     602             :     const MCExpr *ExceptOrNull;
     603             :     auto *Handler = UME.Handler.get<MachineBasicBlock *>();
     604          21 :     if (UME.IsFinally) {
     605           5 :       FilterOrFinally = create32bitRef(getMCSymbolForMBB(Asm, Handler));
     606           5 :       ExceptOrNull = MCConstantExpr::create(0, Ctx);
     607             :     } else {
     608             :       // For an except, the filter can be 1 (catch-all) or a function
     609             :       // label.
     610          16 :       FilterOrFinally = UME.Filter ? create32bitRef(UME.Filter)
     611           5 :                                    : MCConstantExpr::create(1, Ctx);
     612          16 :       ExceptOrNull = create32bitRef(Handler->getSymbol());
     613             :     }
     614             : 
     615          21 :     AddComment("LabelStart");
     616          21 :     OS.EmitValue(getLabelPlusOne(BeginLabel), 4);
     617          21 :     AddComment("LabelEnd");
     618          21 :     OS.EmitValue(getLabelPlusOne(EndLabel), 4);
     619          21 :     AddComment(UME.IsFinally ? "FinallyFunclet" : UME.Filter ? "FilterFunction"
     620             :                                                              : "CatchAll");
     621          21 :     OS.EmitValue(FilterOrFinally, 4);
     622          21 :     AddComment(UME.IsFinally ? "Null" : "ExceptionHandler");
     623          21 :     OS.EmitValue(ExceptOrNull, 4);
     624             : 
     625             :     assert(UME.ToState < State && "states should decrease");
     626          21 :     State = UME.ToState;
     627             :   }
     628          15 : }
     629             : 
     630          56 : void WinException::emitCXXFrameHandler3Table(const MachineFunction *MF) {
     631          56 :   const Function &F = MF->getFunction();
     632          56 :   auto &OS = *Asm->OutStreamer;
     633          56 :   const WinEHFuncInfo &FuncInfo = *MF->getWinEHFuncInfo();
     634             : 
     635          56 :   StringRef FuncLinkageName = GlobalValue::dropLLVMManglingEscape(F.getName());
     636             : 
     637             :   SmallVector<std::pair<const MCExpr *, int>, 4> IPToStateTable;
     638             :   MCSymbol *FuncInfoXData = nullptr;
     639          56 :   if (shouldEmitPersonality) {
     640             :     // If we're 64-bit, emit a pointer to the C++ EH data, and build a map from
     641             :     // IPs to state numbers.
     642             :     FuncInfoXData =
     643          70 :         Asm->OutContext.getOrCreateSymbol(Twine("$cppxdata$", FuncLinkageName));
     644          35 :     computeIP2StateTable(MF, FuncInfo, IPToStateTable);
     645             :   } else {
     646          21 :     FuncInfoXData = Asm->OutContext.getOrCreateLSDASymbol(FuncLinkageName);
     647             :   }
     648             : 
     649             :   int UnwindHelpOffset = 0;
     650          56 :   if (Asm->MAI->usesWindowsCFI())
     651             :     UnwindHelpOffset =
     652          35 :         getFrameIndexOffset(FuncInfo.UnwindHelpFrameIdx, FuncInfo);
     653             : 
     654             :   MCSymbol *UnwindMapXData = nullptr;
     655             :   MCSymbol *TryBlockMapXData = nullptr;
     656             :   MCSymbol *IPToStateXData = nullptr;
     657          56 :   if (!FuncInfo.CxxUnwindMap.empty())
     658         112 :     UnwindMapXData = Asm->OutContext.getOrCreateSymbol(
     659          56 :         Twine("$stateUnwindMap$", FuncLinkageName));
     660          56 :   if (!FuncInfo.TryBlockMap.empty())
     661             :     TryBlockMapXData =
     662          94 :         Asm->OutContext.getOrCreateSymbol(Twine("$tryMap$", FuncLinkageName));
     663          56 :   if (!IPToStateTable.empty())
     664             :     IPToStateXData =
     665          70 :         Asm->OutContext.getOrCreateSymbol(Twine("$ip2state$", FuncLinkageName));
     666             : 
     667          56 :   bool VerboseAsm = OS.isVerboseAsm();
     668             :   auto AddComment = [&](const Twine &Comment) {
     669        1777 :     if (VerboseAsm)
     670        1755 :       OS.AddComment(Comment);
     671             :   };
     672             : 
     673             :   // FuncInfo {
     674             :   //   uint32_t           MagicNumber
     675             :   //   int32_t            MaxState;
     676             :   //   UnwindMapEntry    *UnwindMap;
     677             :   //   uint32_t           NumTryBlocks;
     678             :   //   TryBlockMapEntry  *TryBlockMap;
     679             :   //   uint32_t           IPMapEntries; // always 0 for x86
     680             :   //   IPToStateMapEntry *IPToStateMap; // always 0 for x86
     681             :   //   uint32_t           UnwindHelp;   // non-x86 only
     682             :   //   ESTypeList        *ESTypeList;
     683             :   //   int32_t            EHFlags;
     684             :   // }
     685             :   // EHFlags & 1 -> Synchronous exceptions only, no async exceptions.
     686             :   // EHFlags & 2 -> ???
     687             :   // EHFlags & 4 -> The function is noexcept(true), unwinding can't continue.
     688          56 :   OS.EmitValueToAlignment(4);
     689          56 :   OS.EmitLabel(FuncInfoXData);
     690             : 
     691          56 :   AddComment("MagicNumber");
     692          56 :   OS.EmitIntValue(0x19930522, 4);
     693             : 
     694          56 :   AddComment("MaxState");
     695         112 :   OS.EmitIntValue(FuncInfo.CxxUnwindMap.size(), 4);
     696             : 
     697          56 :   AddComment("UnwindMap");
     698          56 :   OS.EmitValue(create32bitRef(UnwindMapXData), 4);
     699             : 
     700          56 :   AddComment("NumTryBlocks");
     701         112 :   OS.EmitIntValue(FuncInfo.TryBlockMap.size(), 4);
     702             : 
     703          56 :   AddComment("TryBlockMap");
     704          56 :   OS.EmitValue(create32bitRef(TryBlockMapXData), 4);
     705             : 
     706          56 :   AddComment("IPMapEntries");
     707         112 :   OS.EmitIntValue(IPToStateTable.size(), 4);
     708             : 
     709          56 :   AddComment("IPToStateXData");
     710          56 :   OS.EmitValue(create32bitRef(IPToStateXData), 4);
     711             : 
     712          56 :   if (Asm->MAI->usesWindowsCFI()) {
     713          35 :     AddComment("UnwindHelp");
     714          35 :     OS.EmitIntValue(UnwindHelpOffset, 4);
     715             :   }
     716             : 
     717          56 :   AddComment("ESTypeList");
     718          56 :   OS.EmitIntValue(0, 4);
     719             : 
     720          56 :   AddComment("EHFlags");
     721          56 :   OS.EmitIntValue(1, 4);
     722             : 
     723             :   // UnwindMapEntry {
     724             :   //   int32_t ToState;
     725             :   //   void  (*Action)();
     726             :   // };
     727          56 :   if (UnwindMapXData) {
     728          56 :     OS.EmitLabel(UnwindMapXData);
     729         199 :     for (const CxxUnwindMapEntry &UME : FuncInfo.CxxUnwindMap) {
     730             :       MCSymbol *CleanupSym =
     731         143 :           getMCSymbolForMBB(Asm, UME.Cleanup.dyn_cast<MachineBasicBlock *>());
     732         143 :       AddComment("ToState");
     733         143 :       OS.EmitIntValue(UME.ToState, 4);
     734             : 
     735         143 :       AddComment("Action");
     736         143 :       OS.EmitValue(create32bitRef(CleanupSym), 4);
     737             :     }
     738             :   }
     739             : 
     740             :   // TryBlockMap {
     741             :   //   int32_t      TryLow;
     742             :   //   int32_t      TryHigh;
     743             :   //   int32_t      CatchHigh;
     744             :   //   int32_t      NumCatches;
     745             :   //   HandlerType *HandlerArray;
     746             :   // };
     747          56 :   if (TryBlockMapXData) {
     748          47 :     OS.EmitLabel(TryBlockMapXData);
     749             :     SmallVector<MCSymbol *, 1> HandlerMaps;
     750         107 :     for (size_t I = 0, E = FuncInfo.TryBlockMap.size(); I != E; ++I) {
     751             :       const WinEHTryBlockMapEntry &TBME = FuncInfo.TryBlockMap[I];
     752             : 
     753          60 :       MCSymbol *HandlerMapXData = nullptr;
     754          60 :       if (!TBME.HandlerArray.empty())
     755          60 :         HandlerMapXData =
     756         120 :             Asm->OutContext.getOrCreateSymbol(Twine("$handlerMap$")
     757          60 :                                                   .concat(Twine(I))
     758          60 :                                                   .concat("$")
     759         120 :                                                   .concat(FuncLinkageName));
     760          60 :       HandlerMaps.push_back(HandlerMapXData);
     761             : 
     762             :       // TBMEs should form intervals.
     763             :       assert(0 <= TBME.TryLow && "bad trymap interval");
     764             :       assert(TBME.TryLow <= TBME.TryHigh && "bad trymap interval");
     765             :       assert(TBME.TryHigh < TBME.CatchHigh && "bad trymap interval");
     766             :       assert(TBME.CatchHigh < int(FuncInfo.CxxUnwindMap.size()) &&
     767             :              "bad trymap interval");
     768             : 
     769          60 :       AddComment("TryLow");
     770          60 :       OS.EmitIntValue(TBME.TryLow, 4);
     771             : 
     772          60 :       AddComment("TryHigh");
     773          60 :       OS.EmitIntValue(TBME.TryHigh, 4);
     774             : 
     775          60 :       AddComment("CatchHigh");
     776          60 :       OS.EmitIntValue(TBME.CatchHigh, 4);
     777             : 
     778          60 :       AddComment("NumCatches");
     779         120 :       OS.EmitIntValue(TBME.HandlerArray.size(), 4);
     780             : 
     781          60 :       AddComment("HandlerArray");
     782          60 :       OS.EmitValue(create32bitRef(HandlerMapXData), 4);
     783             :     }
     784             : 
     785             :     // All funclets use the same parent frame offset currently.
     786             :     unsigned ParentFrameOffset = 0;
     787          47 :     if (shouldEmitPersonality) {
     788          31 :       const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
     789          31 :       ParentFrameOffset = TFI->getWinEHParentFrameOffset(*MF);
     790             :     }
     791             : 
     792         107 :     for (size_t I = 0, E = FuncInfo.TryBlockMap.size(); I != E; ++I) {
     793             :       const WinEHTryBlockMapEntry &TBME = FuncInfo.TryBlockMap[I];
     794          60 :       MCSymbol *HandlerMapXData = HandlerMaps[I];
     795          60 :       if (!HandlerMapXData)
     796             :         continue;
     797             :       // HandlerType {
     798             :       //   int32_t         Adjectives;
     799             :       //   TypeDescriptor *Type;
     800             :       //   int32_t         CatchObjOffset;
     801             :       //   void          (*Handler)();
     802             :       //   int32_t         ParentFrameOffset; // x64 only
     803             :       // };
     804          60 :       OS.EmitLabel(HandlerMapXData);
     805         127 :       for (const WinEHHandlerType &HT : TBME.HandlerArray) {
     806             :         // Get the frame escape label with the offset of the catch object. If
     807             :         // the index is INT_MAX, then there is no catch object, and we should
     808             :         // emit an offset of zero, indicating that no copy will occur.
     809             :         const MCExpr *FrameAllocOffsetRef = nullptr;
     810          67 :         if (HT.CatchObj.FrameIndex != INT_MAX) {
     811           8 :           int Offset = getFrameIndexOffset(HT.CatchObj.FrameIndex, FuncInfo);
     812             :           assert(Offset != 0 && "Illegal offset for catch object!");
     813           8 :           FrameAllocOffsetRef = MCConstantExpr::create(Offset, Asm->OutContext);
     814             :         } else {
     815          59 :           FrameAllocOffsetRef = MCConstantExpr::create(0, Asm->OutContext);
     816             :         }
     817             : 
     818             :         MCSymbol *HandlerSym =
     819          67 :             getMCSymbolForMBB(Asm, HT.Handler.dyn_cast<MachineBasicBlock *>());
     820             : 
     821          67 :         AddComment("Adjectives");
     822          67 :         OS.EmitIntValue(HT.Adjectives, 4);
     823             : 
     824          67 :         AddComment("Type");
     825          67 :         OS.EmitValue(create32bitRef(HT.TypeDescriptor), 4);
     826             : 
     827          67 :         AddComment("CatchObjOffset");
     828          67 :         OS.EmitValue(FrameAllocOffsetRef, 4);
     829             : 
     830          67 :         AddComment("Handler");
     831          67 :         OS.EmitValue(create32bitRef(HandlerSym), 4);
     832             : 
     833          67 :         if (shouldEmitPersonality) {
     834          46 :           AddComment("ParentFrameOffset");
     835          46 :           OS.EmitIntValue(ParentFrameOffset, 4);
     836             :         }
     837             :       }
     838             :     }
     839             :   }
     840             : 
     841             :   // IPToStateMapEntry {
     842             :   //   void   *IP;
     843             :   //   int32_t State;
     844             :   // };
     845          56 :   if (IPToStateXData) {
     846          35 :     OS.EmitLabel(IPToStateXData);
     847         204 :     for (auto &IPStatePair : IPToStateTable) {
     848         169 :       AddComment("IP");
     849         169 :       OS.EmitValue(IPStatePair.first, 4);
     850         169 :       AddComment("ToState");
     851         169 :       OS.EmitIntValue(IPStatePair.second, 4);
     852             :     }
     853             :   }
     854          56 : }
     855             : 
     856          35 : void WinException::computeIP2StateTable(
     857             :     const MachineFunction *MF, const WinEHFuncInfo &FuncInfo,
     858             :     SmallVectorImpl<std::pair<const MCExpr *, int>> &IPToStateTable) {
     859             : 
     860             :   for (MachineFunction::const_iterator FuncletStart = MF->begin(),
     861             :                                        FuncletEnd = MF->begin(),
     862             :                                        End = MF->end();
     863         131 :        FuncletStart != End; FuncletStart = FuncletEnd) {
     864             :     // Find the end of the funclet
     865         188 :     while (++FuncletEnd != End) {
     866         153 :       if (FuncletEnd->isEHFuncletEntry()) {
     867             :         break;
     868             :       }
     869             :     }
     870             : 
     871             :     // Don't emit ip2state entries for cleanup funclets. Any interesting
     872             :     // exceptional actions in cleanups must be handled in a separate IR
     873             :     // function.
     874          96 :     if (FuncletStart->isCleanupFuncletEntry())
     875             :       continue;
     876             : 
     877             :     MCSymbol *StartLabel;
     878             :     int BaseState;
     879          81 :     if (FuncletStart == MF->begin()) {
     880             :       BaseState = NullState;
     881          35 :       StartLabel = Asm->getFunctionBegin();
     882             :     } else {
     883             :       auto *FuncletPad =
     884          46 :           cast<FuncletPadInst>(FuncletStart->getBasicBlock()->getFirstNonPHI());
     885             :       assert(FuncInfo.FuncletBaseStateMap.count(FuncletPad) != 0);
     886          46 :       BaseState = FuncInfo.FuncletBaseStateMap.find(FuncletPad)->second;
     887          46 :       StartLabel = getMCSymbolForMBB(Asm, &*FuncletStart);
     888             :     }
     889             :     assert(StartLabel && "need local function start label");
     890         162 :     IPToStateTable.push_back(
     891          81 :         std::make_pair(create32bitRef(StartLabel), BaseState));
     892             : 
     893             :     for (const auto &StateChange : InvokeStateChangeIterator::range(
     894         199 :              FuncInfo, FuncletStart, FuncletEnd, BaseState)) {
     895             :       // Compute the label to report as the start of this entry; use the EH
     896             :       // start label for the invoke if we have one, otherwise (this is a call
     897             :       // which may unwind to our caller and does not have an EH start label, so)
     898             :       // use the previous end label.
     899          88 :       const MCSymbol *ChangeLabel = StateChange.NewStartLabel;
     900          88 :       if (!ChangeLabel)
     901          39 :         ChangeLabel = StateChange.PreviousEndLabel;
     902             :       // Emit an entry indicating that PCs after 'Label' have this EH state.
     903          88 :       IPToStateTable.push_back(
     904          88 :           std::make_pair(getLabelPlusOne(ChangeLabel), StateChange.NewState));
     905             :       // FIXME: assert that NewState is between CatchLow and CatchHigh.
     906             :     }
     907             :   }
     908          35 : }
     909             : 
     910          14 : void WinException::emitEHRegistrationOffsetLabel(const WinEHFuncInfo &FuncInfo,
     911             :                                                  StringRef FLinkageName) {
     912             :   // Outlined helpers called by the EH runtime need to know the offset of the EH
     913             :   // registration in order to recover the parent frame pointer. Now that we know
     914             :   // we've code generated the parent, we can emit the label assignment that
     915             :   // those helpers use to get the offset of the registration node.
     916             : 
     917             :   // Compute the parent frame offset. The EHRegNodeFrameIndex will be invalid if
     918             :   // after optimization all the invokes were eliminated. We still need to emit
     919             :   // the parent frame offset label, but it should be garbage and should never be
     920             :   // used.
     921             :   int64_t Offset = 0;
     922          14 :   int FI = FuncInfo.EHRegNodeFrameIndex;
     923          14 :   if (FI != INT_MAX) {
     924          13 :     const TargetFrameLowering *TFI = Asm->MF->getSubtarget().getFrameLowering();
     925             :     unsigned UnusedReg;
     926          13 :     Offset = TFI->getFrameIndexReference(*Asm->MF, FI, UnusedReg);
     927             :   }
     928             : 
     929          14 :   MCContext &Ctx = Asm->OutContext;
     930             :   MCSymbol *ParentFrameOffset =
     931          14 :       Ctx.getOrCreateParentFrameOffsetSymbol(FLinkageName);
     932          14 :   Asm->OutStreamer->EmitAssignment(ParentFrameOffset,
     933          14 :                                    MCConstantExpr::create(Offset, Ctx));
     934          14 : }
     935             : 
     936             : /// Emit the language-specific data that _except_handler3 and 4 expect. This is
     937             : /// functionally equivalent to the __C_specific_handler table, except it is
     938             : /// indexed by state number instead of IP.
     939          13 : void WinException::emitExceptHandlerTable(const MachineFunction *MF) {
     940          13 :   MCStreamer &OS = *Asm->OutStreamer;
     941          13 :   const Function &F = MF->getFunction();
     942          13 :   StringRef FLinkageName = GlobalValue::dropLLVMManglingEscape(F.getName());
     943             : 
     944          13 :   bool VerboseAsm = OS.isVerboseAsm();
     945             :   auto AddComment = [&](const Twine &Comment) {
     946          68 :     if (VerboseAsm)
     947          65 :       OS.AddComment(Comment);
     948             :   };
     949             : 
     950          13 :   const WinEHFuncInfo &FuncInfo = *MF->getWinEHFuncInfo();
     951          13 :   emitEHRegistrationOffsetLabel(FuncInfo, FLinkageName);
     952             : 
     953             :   // Emit the __ehtable label that we use for llvm.x86.seh.lsda.
     954          13 :   MCSymbol *LSDALabel = Asm->OutContext.getOrCreateLSDASymbol(FLinkageName);
     955          13 :   OS.EmitValueToAlignment(4);
     956          13 :   OS.EmitLabel(LSDALabel);
     957             : 
     958             :   const Function *Per =
     959          13 :       dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts());
     960          13 :   StringRef PerName = Per->getName();
     961             :   int BaseState = -1;
     962             :   if (PerName == "_except_handler4") {
     963             :     // The LSDA for _except_handler4 starts with this struct, followed by the
     964             :     // scope table:
     965             :     //
     966             :     // struct EH4ScopeTable {
     967             :     //   int32_t GSCookieOffset;
     968             :     //   int32_t GSCookieXOROffset;
     969             :     //   int32_t EHCookieOffset;
     970             :     //   int32_t EHCookieXOROffset;
     971             :     //   ScopeTableEntry ScopeRecord[];
     972             :     // };
     973             :     //
     974             :     // Offsets are %ebp relative.
     975             :     //
     976             :     // The GS cookie is present only if the function needs stack protection.
     977             :     // GSCookieOffset = -2 means that GS cookie is not used.
     978             :     //
     979             :     // The EH cookie is always present.
     980             :     //
     981             :     // Check is done the following way:
     982             :     //    (ebp+CookieXOROffset) ^ [ebp+CookieOffset] == _security_cookie
     983             : 
     984             :     // Retrieve the Guard Stack slot.
     985             :     int GSCookieOffset = -2;
     986           2 :     const MachineFrameInfo &MFI = MF->getFrameInfo();
     987           2 :     if (MFI.hasStackProtectorIndex()) {
     988             :       unsigned UnusedReg;
     989           0 :       const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
     990           0 :       int SSPIdx = MFI.getStackProtectorIndex();
     991           0 :       GSCookieOffset = TFI->getFrameIndexReference(*MF, SSPIdx, UnusedReg);
     992             :     }
     993             : 
     994             :     // Retrieve the EH Guard slot.
     995             :     // TODO(etienneb): Get rid of this value and change it for and assertion.
     996             :     int EHCookieOffset = 9999;
     997           2 :     if (FuncInfo.EHGuardFrameIndex != INT_MAX) {
     998             :       unsigned UnusedReg;
     999           2 :       const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
    1000           2 :       int EHGuardIdx = FuncInfo.EHGuardFrameIndex;
    1001           2 :       EHCookieOffset = TFI->getFrameIndexReference(*MF, EHGuardIdx, UnusedReg);
    1002             :     }
    1003             : 
    1004           2 :     AddComment("GSCookieOffset");
    1005           2 :     OS.EmitIntValue(GSCookieOffset, 4);
    1006           2 :     AddComment("GSCookieXOROffset");
    1007           2 :     OS.EmitIntValue(0, 4);
    1008           2 :     AddComment("EHCookieOffset");
    1009           2 :     OS.EmitIntValue(EHCookieOffset, 4);
    1010           2 :     AddComment("EHCookieXOROffset");
    1011           2 :     OS.EmitIntValue(0, 4);
    1012             :     BaseState = -2;
    1013             :   }
    1014             : 
    1015             :   assert(!FuncInfo.SEHUnwindMap.empty());
    1016          33 :   for (const SEHUnwindMapEntry &UME : FuncInfo.SEHUnwindMap) {
    1017             :     auto *Handler = UME.Handler.get<MachineBasicBlock *>();
    1018             :     const MCSymbol *ExceptOrFinally =
    1019          20 :         UME.IsFinally ? getMCSymbolForMBB(Asm, Handler) : Handler->getSymbol();
    1020             :     // -1 is usually the base state for "unwind to caller", but for
    1021             :     // _except_handler4 it's -2. Do that replacement here if necessary.
    1022          20 :     int ToState = UME.ToState == -1 ? BaseState : UME.ToState;
    1023          20 :     AddComment("ToState");
    1024          20 :     OS.EmitIntValue(ToState, 4);
    1025          20 :     AddComment(UME.IsFinally ? "Null" : "FilterFunction");
    1026          20 :     OS.EmitValue(create32bitRef(UME.Filter), 4);
    1027          20 :     AddComment(UME.IsFinally ? "FinallyFunclet" : "ExceptionHandler");
    1028          20 :     OS.EmitValue(create32bitRef(ExceptOrFinally), 4);
    1029             :   }
    1030          13 : }
    1031             : 
    1032             : static int getTryRank(const WinEHFuncInfo &FuncInfo, int State) {
    1033             :   int Rank = 0;
    1034         126 :   while (State != -1) {
    1035          56 :     ++Rank;
    1036         112 :     State = FuncInfo.ClrEHUnwindMap[State].TryParentState;
    1037             :   }
    1038             :   return Rank;
    1039             : }
    1040             : 
    1041          35 : static int getTryAncestor(const WinEHFuncInfo &FuncInfo, int Left, int Right) {
    1042             :   int LeftRank = getTryRank(FuncInfo, Left);
    1043             :   int RightRank = getTryRank(FuncInfo, Right);
    1044             : 
    1045          60 :   while (LeftRank < RightRank) {
    1046          25 :     Right = FuncInfo.ClrEHUnwindMap[Right].TryParentState;
    1047          25 :     --RightRank;
    1048             :   }
    1049             : 
    1050          60 :   while (RightRank < LeftRank) {
    1051          25 :     Left = FuncInfo.ClrEHUnwindMap[Left].TryParentState;
    1052          25 :     --LeftRank;
    1053             :   }
    1054             : 
    1055          36 :   while (Left != Right) {
    1056           1 :     Left = FuncInfo.ClrEHUnwindMap[Left].TryParentState;
    1057           2 :     Right = FuncInfo.ClrEHUnwindMap[Right].TryParentState;
    1058             :   }
    1059             : 
    1060          35 :   return Left;
    1061             : }
    1062             : 
    1063           7 : void WinException::emitCLRExceptionTable(const MachineFunction *MF) {
    1064             :   // CLR EH "states" are really just IDs that identify handlers/funclets;
    1065             :   // states, handlers, and funclets all have 1:1 mappings between them, and a
    1066             :   // handler/funclet's "state" is its index in the ClrEHUnwindMap.
    1067           7 :   MCStreamer &OS = *Asm->OutStreamer;
    1068           7 :   const WinEHFuncInfo &FuncInfo = *MF->getWinEHFuncInfo();
    1069           7 :   MCSymbol *FuncBeginSym = Asm->getFunctionBegin();
    1070           7 :   MCSymbol *FuncEndSym = Asm->getFunctionEnd();
    1071             : 
    1072             :   // A ClrClause describes a protected region.
    1073             :   struct ClrClause {
    1074             :     const MCSymbol *StartLabel; // Start of protected region
    1075             :     const MCSymbol *EndLabel;   // End of protected region
    1076             :     int State;          // Index of handler protecting the protected region
    1077             :     int EnclosingState; // Index of funclet enclosing the protected region
    1078             :   };
    1079           7 :   SmallVector<ClrClause, 8> Clauses;
    1080             : 
    1081             :   // Build a map from handler MBBs to their corresponding states (i.e. their
    1082             :   // indices in the ClrEHUnwindMap).
    1083           7 :   int NumStates = FuncInfo.ClrEHUnwindMap.size();
    1084             :   assert(NumStates > 0 && "Don't need exception table!");
    1085             :   DenseMap<const MachineBasicBlock *, int> HandlerStates;
    1086          26 :   for (int State = 0; State < NumStates; ++State) {
    1087             :     MachineBasicBlock *HandlerBlock =
    1088          19 :         FuncInfo.ClrEHUnwindMap[State].Handler.get<MachineBasicBlock *>();
    1089          19 :     HandlerStates[HandlerBlock] = State;
    1090             :     // Use this loop through all handlers to verify our assumption (used in
    1091             :     // the MinEnclosingState computation) that enclosing funclets have lower
    1092             :     // state numbers than their enclosed funclets.
    1093             :     assert(FuncInfo.ClrEHUnwindMap[State].HandlerParentState < State &&
    1094             :            "ill-formed state numbering");
    1095             :   }
    1096             :   // Map the main function to the NullState.
    1097           7 :   HandlerStates[&MF->front()] = NullState;
    1098             : 
    1099             :   // Write out a sentinel indicating the end of the standard (Windows) xdata
    1100             :   // and the start of the additional (CLR) info.
    1101           7 :   OS.EmitIntValue(0xffffffff, 4);
    1102             :   // Write out the number of funclets
    1103           7 :   OS.EmitIntValue(NumStates, 4);
    1104             : 
    1105             :   // Walk the machine blocks/instrs, computing and emitting a few things:
    1106             :   // 1. Emit a list of the offsets to each handler entry, in lexical order.
    1107             :   // 2. Compute a map (EndSymbolMap) from each funclet to the symbol at its end.
    1108             :   // 3. Compute the list of ClrClauses, in the required order (inner before
    1109             :   //    outer, earlier before later; the order by which a forward scan with
    1110             :   //    early termination will find the innermost enclosing clause covering
    1111             :   //    a given address).
    1112             :   // 4. A map (MinClauseMap) from each handler index to the index of the
    1113             :   //    outermost funclet/function which contains a try clause targeting the
    1114             :   //    key handler.  This will be used to determine IsDuplicate-ness when
    1115             :   //    emitting ClrClauses.  The NullState value is used to indicate that the
    1116             :   //    top-level function contains a try clause targeting the key handler.
    1117             :   // HandlerStack is a stack of (PendingStartLabel, PendingState) pairs for
    1118             :   // try regions we entered before entering the PendingState try but which
    1119             :   // we haven't yet exited.
    1120             :   SmallVector<std::pair<const MCSymbol *, int>, 4> HandlerStack;
    1121             :   // EndSymbolMap and MinClauseMap are maps described above.
    1122           7 :   std::unique_ptr<MCSymbol *[]> EndSymbolMap(new MCSymbol *[NumStates]);
    1123           7 :   SmallVector<int, 4> MinClauseMap((size_t)NumStates, NumStates);
    1124             : 
    1125             :   // Visit the root function and each funclet.
    1126          26 :   for (MachineFunction::const_iterator FuncletStart = MF->begin(),
    1127             :                                        FuncletEnd = MF->begin(),
    1128             :                                        End = MF->end();
    1129          33 :        FuncletStart != End; FuncletStart = FuncletEnd) {
    1130          26 :     int FuncletState = HandlerStates[&*FuncletStart];
    1131             :     // Find the end of the funclet
    1132             :     MCSymbol *EndSymbol = FuncEndSym;
    1133          49 :     while (++FuncletEnd != End) {
    1134          42 :       if (FuncletEnd->isEHFuncletEntry()) {
    1135          19 :         EndSymbol = getMCSymbolForMBB(Asm, &*FuncletEnd);
    1136          19 :         break;
    1137             :       }
    1138             :     }
    1139             :     // Emit the function/funclet end and, if this is a funclet (and not the
    1140             :     // root function), record it in the EndSymbolMap.
    1141          26 :     OS.EmitValue(getOffset(EndSymbol, FuncBeginSym), 4);
    1142          26 :     if (FuncletState != NullState) {
    1143             :       // Record the end of the handler.
    1144          38 :       EndSymbolMap[FuncletState] = EndSymbol;
    1145             :     }
    1146             : 
    1147             :     // Walk the state changes in this function/funclet and compute its clauses.
    1148             :     // Funclets always start in the null state.
    1149          26 :     const MCSymbol *CurrentStartLabel = nullptr;
    1150          26 :     int CurrentState = NullState;
    1151             :     assert(HandlerStack.empty());
    1152             :     for (const auto &StateChange :
    1153          67 :          InvokeStateChangeIterator::range(FuncInfo, FuncletStart, FuncletEnd)) {
    1154             :       // Close any try regions we're not still under
    1155             :       int StillPendingState =
    1156          35 :           getTryAncestor(FuncInfo, CurrentState, StateChange.NewState);
    1157          61 :       while (CurrentState != StillPendingState) {
    1158             :         assert(CurrentState != NullState &&
    1159             :                "Failed to find still-pending state!");
    1160             :         // Close the pending clause
    1161          26 :         Clauses.push_back({CurrentStartLabel, StateChange.PreviousEndLabel,
    1162             :                            CurrentState, FuncletState});
    1163             :         // Now the next-outer try region is current
    1164          52 :         CurrentState = FuncInfo.ClrEHUnwindMap[CurrentState].TryParentState;
    1165             :         // Pop the new start label from the handler stack if we've exited all
    1166             :         // inner try regions of the corresponding try region.
    1167          26 :         if (HandlerStack.back().second == CurrentState)
    1168          19 :           CurrentStartLabel = HandlerStack.pop_back_val().first;
    1169             :       }
    1170             : 
    1171          35 :       if (StateChange.NewState != CurrentState) {
    1172             :         // For each clause we're starting, update the MinClauseMap so we can
    1173             :         // know which is the topmost funclet containing a clause targeting
    1174             :         // it.
    1175          26 :         for (int EnteredState = StateChange.NewState;
    1176          45 :              EnteredState != CurrentState;
    1177          26 :              EnteredState =
    1178             :                  FuncInfo.ClrEHUnwindMap[EnteredState].TryParentState) {
    1179          26 :           int &MinEnclosingState = MinClauseMap[EnteredState];
    1180          26 :           if (FuncletState < MinEnclosingState)
    1181          22 :             MinEnclosingState = FuncletState;
    1182             :         }
    1183             :         // Save the previous current start/label on the stack and update to
    1184             :         // the newly-current start/state.
    1185          19 :         HandlerStack.emplace_back(CurrentStartLabel, CurrentState);
    1186          19 :         CurrentStartLabel = StateChange.NewStartLabel;
    1187          19 :         CurrentState = StateChange.NewState;
    1188             :       }
    1189             :     }
    1190             :     assert(HandlerStack.empty());
    1191             :   }
    1192             : 
    1193             :   // Now emit the clause info, starting with the number of clauses.
    1194          14 :   OS.EmitIntValue(Clauses.size(), 4);
    1195          33 :   for (ClrClause &Clause : Clauses) {
    1196             :     // Emit a CORINFO_EH_CLAUSE :
    1197             :     /*
    1198             :       struct CORINFO_EH_CLAUSE
    1199             :       {
    1200             :           CORINFO_EH_CLAUSE_FLAGS Flags;         // actually a CorExceptionFlag
    1201             :           DWORD                   TryOffset;
    1202             :           DWORD                   TryLength;     // actually TryEndOffset
    1203             :           DWORD                   HandlerOffset;
    1204             :           DWORD                   HandlerLength; // actually HandlerEndOffset
    1205             :           union
    1206             :           {
    1207             :               DWORD               ClassToken;   // use for catch clauses
    1208             :               DWORD               FilterOffset; // use for filter clauses
    1209             :           };
    1210             :       };
    1211             : 
    1212             :       enum CORINFO_EH_CLAUSE_FLAGS
    1213             :       {
    1214             :           CORINFO_EH_CLAUSE_NONE    = 0,
    1215             :           CORINFO_EH_CLAUSE_FILTER  = 0x0001, // This clause is for a filter
    1216             :           CORINFO_EH_CLAUSE_FINALLY = 0x0002, // This clause is a finally clause
    1217             :           CORINFO_EH_CLAUSE_FAULT   = 0x0004, // This clause is a fault clause
    1218             :       };
    1219             :       typedef enum CorExceptionFlag
    1220             :       {
    1221             :           COR_ILEXCEPTION_CLAUSE_NONE,
    1222             :           COR_ILEXCEPTION_CLAUSE_FILTER  = 0x0001, // This is a filter clause
    1223             :           COR_ILEXCEPTION_CLAUSE_FINALLY = 0x0002, // This is a finally clause
    1224             :           COR_ILEXCEPTION_CLAUSE_FAULT = 0x0004,   // This is a fault clause
    1225             :           COR_ILEXCEPTION_CLAUSE_DUPLICATED = 0x0008, // duplicated clause. This
    1226             :                                                       // clause was duplicated
    1227             :                                                       // to a funclet which was
    1228             :                                                       // pulled out of line
    1229             :       } CorExceptionFlag;
    1230             :     */
    1231             :     // Add 1 to the start/end of the EH clause; the IP associated with a
    1232             :     // call when the runtime does its scan is the IP of the next instruction
    1233             :     // (the one to which control will return after the call), so we need
    1234             :     // to add 1 to the end of the clause to cover that offset.  We also add
    1235             :     // 1 to the start of the clause to make sure that the ranges reported
    1236             :     // for all clauses are disjoint.  Note that we'll need some additional
    1237             :     // logic when machine traps are supported, since in that case the IP
    1238             :     // that the runtime uses is the offset of the faulting instruction
    1239             :     // itself; if such an instruction immediately follows a call but the
    1240             :     // two belong to different clauses, we'll need to insert a nop between
    1241             :     // them so the runtime can distinguish the point to which the call will
    1242             :     // return from the point at which the fault occurs.
    1243             : 
    1244             :     const MCExpr *ClauseBegin =
    1245          26 :         getOffsetPlusOne(Clause.StartLabel, FuncBeginSym);
    1246          26 :     const MCExpr *ClauseEnd = getOffsetPlusOne(Clause.EndLabel, FuncBeginSym);
    1247             : 
    1248          26 :     const ClrEHUnwindMapEntry &Entry = FuncInfo.ClrEHUnwindMap[Clause.State];
    1249             :     MachineBasicBlock *HandlerBlock = Entry.Handler.get<MachineBasicBlock *>();
    1250          26 :     MCSymbol *BeginSym = getMCSymbolForMBB(Asm, HandlerBlock);
    1251          26 :     const MCExpr *HandlerBegin = getOffset(BeginSym, FuncBeginSym);
    1252          26 :     MCSymbol *EndSym = EndSymbolMap[Clause.State];
    1253          26 :     const MCExpr *HandlerEnd = getOffset(EndSym, FuncBeginSym);
    1254             : 
    1255             :     uint32_t Flags = 0;
    1256          26 :     switch (Entry.HandlerType) {
    1257             :     case ClrHandlerType::Catch:
    1258             :       // Leaving bits 0-2 clear indicates catch.
    1259             :       break;
    1260             :     case ClrHandlerType::Filter:
    1261             :       Flags |= 1;
    1262             :       break;
    1263             :     case ClrHandlerType::Finally:
    1264             :       Flags |= 2;
    1265             :       break;
    1266             :     case ClrHandlerType::Fault:
    1267             :       Flags |= 4;
    1268             :       break;
    1269             :     }
    1270          52 :     if (Clause.EnclosingState != MinClauseMap[Clause.State]) {
    1271             :       // This is a "duplicate" clause; the handler needs to be entered from a
    1272             :       // frame above the one holding the invoke.
    1273             :       assert(Clause.EnclosingState > MinClauseMap[Clause.State]);
    1274           7 :       Flags |= 8;
    1275             :     }
    1276          26 :     OS.EmitIntValue(Flags, 4);
    1277             : 
    1278             :     // Write the clause start/end
    1279          26 :     OS.EmitValue(ClauseBegin, 4);
    1280          26 :     OS.EmitValue(ClauseEnd, 4);
    1281             : 
    1282             :     // Write out the handler start/end
    1283          26 :     OS.EmitValue(HandlerBegin, 4);
    1284          26 :     OS.EmitValue(HandlerEnd, 4);
    1285             : 
    1286             :     // Write out the type token or filter offset
    1287             :     assert(Entry.HandlerType != ClrHandlerType::Filter && "NYI: filters");
    1288          26 :     OS.EmitIntValue(Entry.TypeToken, 4);
    1289             :   }
    1290           7 : }

Generated by: LCOV version 1.13