LCOV - code coverage report
Current view: top level - lib/CodeGen/AsmPrinter - EHStreamer.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 217 219 99.1 %
Date: 2018-06-17 00:07:59 Functions: 7 8 87.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- CodeGen/AsmPrinter/EHStreamer.cpp - Exception Directive Streamer ---===//
       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 exception info into assembly files.
      11             : //
      12             : //===----------------------------------------------------------------------===//
      13             : 
      14             : #include "EHStreamer.h"
      15             : #include "llvm/ADT/SmallVector.h"
      16             : #include "llvm/ADT/Twine.h"
      17             : #include "llvm/ADT/iterator_range.h"
      18             : #include "llvm/BinaryFormat/Dwarf.h"
      19             : #include "llvm/CodeGen/AsmPrinter.h"
      20             : #include "llvm/CodeGen/MachineFunction.h"
      21             : #include "llvm/CodeGen/MachineInstr.h"
      22             : #include "llvm/CodeGen/MachineOperand.h"
      23             : #include "llvm/IR/DataLayout.h"
      24             : #include "llvm/IR/Function.h"
      25             : #include "llvm/MC/MCAsmInfo.h"
      26             : #include "llvm/MC/MCContext.h"
      27             : #include "llvm/MC/MCStreamer.h"
      28             : #include "llvm/MC/MCSymbol.h"
      29             : #include "llvm/MC/MCTargetOptions.h"
      30             : #include "llvm/Support/Casting.h"
      31             : #include "llvm/Support/LEB128.h"
      32             : #include "llvm/Target/TargetLoweringObjectFile.h"
      33             : #include <algorithm>
      34             : #include <cassert>
      35             : #include <cstdint>
      36             : #include <vector>
      37             : 
      38             : using namespace llvm;
      39             : 
      40       19877 : EHStreamer::EHStreamer(AsmPrinter *A) : Asm(A), MMI(Asm->MMI) {}
      41             : 
      42             : EHStreamer::~EHStreamer() = default;
      43             : 
      44             : /// How many leading type ids two landing pads have in common.
      45           0 : unsigned EHStreamer::sharedTypeIDs(const LandingPadInfo *L,
      46             :                                    const LandingPadInfo *R) {
      47             :   const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
      48       65616 :   unsigned LSize = LIds.size(), RSize = RIds.size();
      49       21872 :   unsigned MinSize = LSize < RSize ? LSize : RSize;
      50             :   unsigned Count = 0;
      51             : 
      52       33628 :   for (; Count != MinSize; ++Count)
      53       17667 :     if (LIds[Count] != RIds[Count])
      54             :       return Count;
      55             : 
      56             :   return Count;
      57             : }
      58             : 
      59             : /// Compute the actions table and gather the first action index for each landing
      60             : /// pad site.
      61        6540 : void EHStreamer::computeActionsTable(
      62             :     const SmallVectorImpl<const LandingPadInfo *> &LandingPads,
      63             :     SmallVectorImpl<ActionEntry> &Actions,
      64             :     SmallVectorImpl<unsigned> &FirstActions) {
      65             :   // The action table follows the call-site table in the LSDA. The individual
      66             :   // records are of two types:
      67             :   //
      68             :   //   * Catch clause
      69             :   //   * Exception specification
      70             :   //
      71             :   // The two record kinds have the same format, with only small differences.
      72             :   // They are distinguished by the "switch value" field: Catch clauses
      73             :   // (TypeInfos) have strictly positive switch values, and exception
      74             :   // specifications (FilterIds) have strictly negative switch values. Value 0
      75             :   // indicates a catch-all clause.
      76             :   //
      77             :   // Negative type IDs index into FilterIds. Positive type IDs index into
      78             :   // TypeInfos.  The value written for a positive type ID is just the type ID
      79             :   // itself.  For a negative type ID, however, the value written is the
      80             :   // (negative) byte offset of the corresponding FilterIds entry.  The byte
      81             :   // offset is usually equal to the type ID (because the FilterIds entries are
      82             :   // written using a variable width encoding, which outputs one byte per entry
      83             :   // as long as the value written is not too large) but can differ.  This kind
      84             :   // of complication does not occur for positive type IDs because type infos are
      85             :   // output using a fixed width encoding.  FilterOffsets[i] holds the byte
      86             :   // offset corresponding to FilterIds[i].
      87             : 
      88        6540 :   const std::vector<unsigned> &FilterIds = Asm->MF->getFilterIds();
      89             :   SmallVector<int, 16> FilterOffsets;
      90       13080 :   FilterOffsets.reserve(FilterIds.size());
      91        6540 :   int Offset = -1;
      92             : 
      93             :   for (std::vector<unsigned>::const_iterator
      94        6540 :          I = FilterIds.begin(), E = FilterIds.end(); I != E; ++I) {
      95          20 :     FilterOffsets.push_back(Offset);
      96          20 :     Offset -= getULEB128Size(*I);
      97             :   }
      98             : 
      99        6540 :   FirstActions.reserve(LandingPads.size());
     100             : 
     101             :   int FirstAction = 0;
     102             :   unsigned SizeActions = 0;
     103             :   const LandingPadInfo *PrevLPI = nullptr;
     104             : 
     105       28407 :   for (SmallVectorImpl<const LandingPadInfo *>::const_iterator
     106       34947 :          I = LandingPads.begin(), E = LandingPads.end(); I != E; ++I) {
     107       28407 :     const LandingPadInfo *LPI = *I;
     108             :     const std::vector<int> &TypeIds = LPI->TypeIds;
     109       28407 :     unsigned NumShared = PrevLPI ? sharedTypeIDs(LPI, PrevLPI) : 0;
     110             :     unsigned SizeSiteActions = 0;
     111             : 
     112       56814 :     if (NumShared < TypeIds.size()) {
     113             :       unsigned SizeAction = 0;
     114             :       unsigned PrevAction = (unsigned)-1;
     115             : 
     116        3885 :       if (NumShared) {
     117          90 :         unsigned SizePrevIds = PrevLPI->TypeIds.size();
     118             :         assert(Actions.size());
     119          45 :         PrevAction = Actions.size() - 1;
     120         135 :         SizeAction = getSLEB128Size(Actions[PrevAction].NextAction) +
     121          45 :                      getSLEB128Size(Actions[PrevAction].ValueForTypeID);
     122             : 
     123          47 :         for (unsigned j = NumShared; j != SizePrevIds; ++j) {
     124             :           assert(PrevAction != (unsigned)-1 && "PrevAction is invalid!");
     125           2 :           SizeAction -= getSLEB128Size(Actions[PrevAction].ValueForTypeID);
     126           1 :           SizeAction += -Actions[PrevAction].NextAction;
     127           1 :           PrevAction = Actions[PrevAction].Previous;
     128             :         }
     129             :       }
     130             : 
     131             :       // Compute the actions.
     132       11699 :       for (unsigned J = NumShared, M = TypeIds.size(); J != M; ++J) {
     133        7858 :         int TypeID = TypeIds[J];
     134             :         assert(-1 - TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
     135             :         int ValueForTypeID =
     136        3946 :             isFilterEHSelector(TypeID) ? FilterOffsets[-1 - TypeID] : TypeID;
     137        3929 :         unsigned SizeTypeID = getSLEB128Size(ValueForTypeID);
     138             : 
     139        3929 :         int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
     140        3929 :         SizeAction = SizeTypeID + getSLEB128Size(NextAction);
     141        3929 :         SizeSiteActions += SizeAction;
     142             : 
     143        3929 :         ActionEntry Action = { ValueForTypeID, NextAction, PrevAction };
     144        3929 :         Actions.push_back(Action);
     145        3929 :         PrevAction = Actions.size() - 1;
     146             :       }
     147             : 
     148             :       // Record the first action of the landing pad site.
     149        3885 :       FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
     150             :     } // else identical - re-use previous FirstAction
     151             : 
     152             :     // Information used when creating the call-site table. The action record
     153             :     // field of the call site record is the offset of the first associated
     154             :     // action record, relative to the start of the actions table. This value is
     155             :     // biased by 1 (1 indicating the start of the actions table), and 0
     156             :     // indicates that there are no actions.
     157       28407 :     FirstActions.push_back(FirstAction);
     158             : 
     159             :     // Compute this sites contribution to size.
     160       28407 :     SizeActions += SizeSiteActions;
     161             : 
     162             :     PrevLPI = LPI;
     163             :   }
     164        6540 : }
     165             : 
     166             : /// Return `true' if this is a call to a function marked `nounwind'. Return
     167             : /// `false' otherwise.
     168      147019 : bool EHStreamer::callToNoUnwindFunction(const MachineInstr *MI) {
     169             :   assert(MI->isCall() && "This should be a call instruction!");
     170             : 
     171             :   bool MarkedNoUnwind = false;
     172             :   bool SawFunc = false;
     173             : 
     174     1306479 :   for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
     175     1159460 :     const MachineOperand &MO = MI->getOperand(I);
     176             : 
     177     1159460 :     if (!MO.isGlobal()) continue;
     178             : 
     179      145832 :     const Function *F = dyn_cast<Function>(MO.getGlobal());
     180          39 :     if (!F) continue;
     181             : 
     182      145793 :     if (SawFunc) {
     183             :       // Be conservative. If we have more than one function operand for this
     184             :       // call, then we can't make the assumption that it's the callee and
     185             :       // not a parameter to the call.
     186             :       //
     187             :       // FIXME: Determine if there's a way to say that `F' is the callee or
     188             :       // parameter.
     189             :       MarkedNoUnwind = false;
     190             :       break;
     191             :     }
     192             : 
     193             :     MarkedNoUnwind = F->doesNotThrow();
     194             :     SawFunc = true;
     195             :   }
     196             : 
     197      147019 :   return MarkedNoUnwind;
     198             : }
     199             : 
     200        6540 : void EHStreamer::computePadMap(
     201             :     const SmallVectorImpl<const LandingPadInfo *> &LandingPads,
     202             :     RangeMapType &PadMap) {
     203             :   // Invokes and nounwind calls have entries in PadMap (due to being bracketed
     204             :   // by try-range labels when lowered).  Ordinary calls do not, so appropriate
     205             :   // try-ranges for them need be deduced so we can put them in the LSDA.
     206       34947 :   for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
     207       56814 :     const LandingPadInfo *LandingPad = LandingPads[i];
     208       70988 :     for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
     209       85162 :       MCSymbol *BeginLabel = LandingPad->BeginLabels[j];
     210             :       assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
     211             :       PadRange P = { i, j };
     212       42581 :       PadMap[BeginLabel] = P;
     213             :     }
     214             :   }
     215        6540 : }
     216             : 
     217             : /// Compute the call-site table.  The entry for an invoke has a try-range
     218             : /// containing the call, a non-zero landing pad, and an appropriate action.  The
     219             : /// entry for an ordinary call has a try-range containing the call and zero for
     220             : /// the landing pad and the action.  Calls marked 'nounwind' have no entry and
     221             : /// must not be contained in the try-range of any entry - they form gaps in the
     222             : /// table.  Entries must be ordered by try-range address.
     223        6540 : void EHStreamer::
     224             : computeCallSiteTable(SmallVectorImpl<CallSiteEntry> &CallSites,
     225             :                      const SmallVectorImpl<const LandingPadInfo *> &LandingPads,
     226             :                      const SmallVectorImpl<unsigned> &FirstActions) {
     227             :   RangeMapType PadMap;
     228        6540 :   computePadMap(LandingPads, PadMap);
     229             : 
     230             :   // The end label of the previous invoke or nounwind try-range.
     231             :   MCSymbol *LastLabel = nullptr;
     232             : 
     233             :   // Whether there is a potentially throwing instruction (currently this means
     234             :   // an ordinary call) between the end of the previous try-range and now.
     235             :   bool SawPotentiallyThrowing = false;
     236             : 
     237             :   // Whether the last CallSite entry was for an invoke.
     238             :   bool PreviousIsInvoke = false;
     239             : 
     240        6540 :   bool IsSJLJ = Asm->MAI->getExceptionHandlingType() == ExceptionHandling::SjLj;
     241             : 
     242             :   // Visit all instructions in order of address.
     243      157000 :   for (const auto &MBB : *Asm->MF) {
     244     1766682 :     for (const auto &MI : MBB) {
     245     2844113 :       if (!MI.isEHLabel()) {
     246     1365271 :         if (MI.isCall())
     247      147002 :           SawPotentiallyThrowing |= !callToNoUnwindFunction(&MI);
     248     1365271 :         continue;
     249             :       }
     250             : 
     251             :       // End of the previous try-range?
     252      113571 :       MCSymbol *BeginLabel = MI.getOperand(0).getMCSymbol();
     253      113571 :       if (BeginLabel == LastLabel)
     254             :         SawPotentiallyThrowing = false;
     255             : 
     256             :       // Beginning of a new try-range?
     257      227142 :       RangeMapType::const_iterator L = PadMap.find(BeginLabel);
     258      113571 :       if (L == PadMap.end())
     259             :         // Nope, it was just some random label.
     260       70990 :         continue;
     261             : 
     262             :       const PadRange &P = L->second;
     263       85162 :       const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
     264             :       assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
     265             :              "Inconsistent landing pad map!");
     266             : 
     267             :       // For Dwarf exception handling (SjLj handling doesn't use this). If some
     268             :       // instruction between the previous try-range and this one may throw,
     269             :       // create a call-site entry with no landing pad for the region between the
     270             :       // try-ranges.
     271       42581 :       if (SawPotentiallyThrowing && Asm->MAI->usesCFIForEH()) {
     272        5050 :         CallSiteEntry Site = { LastLabel, BeginLabel, nullptr, 0 };
     273        5050 :         CallSites.push_back(Site);
     274             :         PreviousIsInvoke = false;
     275             :       }
     276             : 
     277       85162 :       LastLabel = LandingPad->EndLabels[P.RangeIndex];
     278             :       assert(BeginLabel && LastLabel && "Invalid landing pad!");
     279             : 
     280       42581 :       if (!LandingPad->LandingPadLabel) {
     281             :         // Create a gap.
     282             :         PreviousIsInvoke = false;
     283             :       } else {
     284             :         // This try-range is for an invoke.
     285             :         CallSiteEntry Site = {
     286             :           BeginLabel,
     287             :           LastLabel,
     288             :           LandingPad,
     289       42581 :           FirstActions[P.PadIndex]
     290       85162 :         };
     291             : 
     292             :         // Try to merge with the previous call-site. SJLJ doesn't do this
     293       42581 :         if (PreviousIsInvoke && !IsSJLJ) {
     294       33624 :           CallSiteEntry &Prev = CallSites.back();
     295       46886 :           if (Site.LPad == Prev.LPad && Site.Action == Prev.Action) {
     296             :             // Extend the range of the previous entry.
     297       13262 :             Prev.EndLabel = Site.EndLabel;
     298       13262 :             continue;
     299             :           }
     300             :         }
     301             : 
     302             :         // Otherwise, create a new call-site.
     303       29319 :         if (!IsSJLJ)
     304       29144 :           CallSites.push_back(Site);
     305             :         else {
     306             :           // SjLj EH must maintain the call sites in the order assigned
     307             :           // to them by the SjLjPrepare pass.
     308         175 :           unsigned SiteNo = Asm->MF->getCallSiteBeginLabel(BeginLabel);
     309         175 :           if (CallSites.size() < SiteNo)
     310         140 :             CallSites.resize(SiteNo);
     311         350 :           CallSites[SiteNo - 1] = Site;
     312             :         }
     313             :         PreviousIsInvoke = true;
     314             :       }
     315             :     }
     316             :   }
     317             : 
     318             :   // If some instruction between the previous try-range and the end of the
     319             :   // function may throw, create a call-site entry with no landing pad for the
     320             :   // region following the try-range.
     321        6540 :   if (SawPotentiallyThrowing && !IsSJLJ) {
     322        4016 :     CallSiteEntry Site = { LastLabel, nullptr, nullptr, 0 };
     323        4016 :     CallSites.push_back(Site);
     324             :   }
     325        6540 : }
     326             : 
     327             : /// Emit landing pads and actions.
     328             : ///
     329             : /// The general organization of the table is complex, but the basic concepts are
     330             : /// easy.  First there is a header which describes the location and organization
     331             : /// of the three components that follow.
     332             : ///
     333             : ///  1. The landing pad site information describes the range of code covered by
     334             : ///     the try.  In our case it's an accumulation of the ranges covered by the
     335             : ///     invokes in the try.  There is also a reference to the landing pad that
     336             : ///     handles the exception once processed.  Finally an index into the actions
     337             : ///     table.
     338             : ///  2. The action table, in our case, is composed of pairs of type IDs and next
     339             : ///     action offset.  Starting with the action index from the landing pad
     340             : ///     site, each type ID is checked for a match to the current exception.  If
     341             : ///     it matches then the exception and type id are passed on to the landing
     342             : ///     pad.  Otherwise the next action is looked up.  This chain is terminated
     343             : ///     with a next action of zero.  If no type id is found then the frame is
     344             : ///     unwound and handling continues.
     345             : ///  3. Type ID table contains references to all the C++ typeinfo for all
     346             : ///     catches in the function.  This tables is reverse indexed base 1.
     347        6540 : void EHStreamer::emitExceptionTable() {
     348        6540 :   const MachineFunction *MF = Asm->MF;
     349             :   const std::vector<const GlobalValue *> &TypeInfos = MF->getTypeInfos();
     350             :   const std::vector<unsigned> &FilterIds = MF->getFilterIds();
     351             :   const std::vector<LandingPadInfo> &PadInfos = MF->getLandingPads();
     352             : 
     353             :   // Sort the landing pads in order of their type ids.  This is used to fold
     354             :   // duplicate actions.
     355             :   SmallVector<const LandingPadInfo *, 64> LandingPads;
     356       13080 :   LandingPads.reserve(PadInfos.size());
     357             : 
     358       41487 :   for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
     359       56814 :     LandingPads.push_back(&PadInfos[i]);
     360             : 
     361             :   // Order landing pads lexicographically by type id.
     362             :   llvm::sort(LandingPads.begin(), LandingPads.end(),
     363             :              [](const LandingPadInfo *L,
     364       86701 :                 const LandingPadInfo *R) { return L->TypeIds < R->TypeIds; });
     365             : 
     366             :   // Compute the actions table and gather the first action index for each
     367             :   // landing pad site.
     368             :   SmallVector<ActionEntry, 32> Actions;
     369             :   SmallVector<unsigned, 64> FirstActions;
     370        6540 :   computeActionsTable(LandingPads, Actions, FirstActions);
     371             : 
     372             :   // Compute the call-site table.
     373             :   SmallVector<CallSiteEntry, 64> CallSites;
     374        6540 :   computeCallSiteTable(CallSites, LandingPads, FirstActions);
     375             : 
     376        6540 :   bool IsSJLJ = Asm->MAI->getExceptionHandlingType() == ExceptionHandling::SjLj;
     377             :   unsigned CallSiteEncoding =
     378        6540 :       IsSJLJ ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_uleb128;
     379        9265 :   bool HaveTTData = !TypeInfos.empty() || !FilterIds.empty();
     380             : 
     381             :   // Type infos.
     382        6540 :   MCSection *LSDASection = Asm->getObjFileLowering().getLSDASection();
     383             :   unsigned TTypeEncoding;
     384             : 
     385        6540 :   if (!HaveTTData) {
     386             :     // If there is no TypeInfo, then we just explicitly say that we're omitting
     387             :     // that bit.
     388             :     TTypeEncoding = dwarf::DW_EH_PE_omit;
     389             :   } else {
     390             :     // Okay, we have actual filters or typeinfos to emit.  As such, we need to
     391             :     // pick a type encoding for them.  We're about to emit a list of pointers to
     392             :     // typeinfo objects at the end of the LSDA.  However, unless we're in static
     393             :     // mode, this reference will require a relocation by the dynamic linker.
     394             :     //
     395             :     // Because of this, we have a couple of options:
     396             :     //
     397             :     //   1) If we are in -static mode, we can always use an absolute reference
     398             :     //      from the LSDA, because the static linker will resolve it.
     399             :     //
     400             :     //   2) Otherwise, if the LSDA section is writable, we can output the direct
     401             :     //      reference to the typeinfo and allow the dynamic linker to relocate
     402             :     //      it.  Since it is in a writable section, the dynamic linker won't
     403             :     //      have a problem.
     404             :     //
     405             :     //   3) Finally, if we're in PIC mode and the LDSA section isn't writable,
     406             :     //      we need to use some form of indirection.  For example, on Darwin,
     407             :     //      we can output a statically-relocatable reference to a dyld stub. The
     408             :     //      offset to the stub is constant, but the contents are in a section
     409             :     //      that is updated by the dynamic linker.  This is easy enough, but we
     410             :     //      need to tell the personality function of the unwinder to indirect
     411             :     //      through the dyld stub.
     412             :     //
     413             :     // FIXME: When (3) is actually implemented, we'll have to emit the stubs
     414             :     // somewhere.  This predicate should be moved to a shared location that is
     415             :     // in target-independent code.
     416             :     //
     417        3831 :     TTypeEncoding = Asm->getObjFileLowering().getTTypeEncoding();
     418             :   }
     419             : 
     420             :   // Begin the exception table.
     421             :   // Sometimes we want not to emit the data into separate section (e.g. ARM
     422             :   // EHABI). In this case LSDASection will be NULL.
     423        6540 :   if (LSDASection)
     424       12964 :     Asm->OutStreamer->SwitchSection(LSDASection);
     425        6540 :   Asm->EmitAlignment(2);
     426             : 
     427             :   // Emit the LSDA.
     428             :   MCSymbol *GCCETSym =
     429       13080 :     Asm->OutContext.getOrCreateSymbol(Twine("GCC_except_table")+
     430       19620 :                                       Twine(Asm->getFunctionNumber()));
     431       13080 :   Asm->OutStreamer->EmitLabel(GCCETSym);
     432       13080 :   Asm->OutStreamer->EmitLabel(Asm->getCurExceptionSym());
     433             : 
     434             :   // Emit the LSDA header.
     435        6540 :   Asm->EmitEncodingByte(dwarf::DW_EH_PE_omit, "@LPStart");
     436        6540 :   Asm->EmitEncodingByte(TTypeEncoding, "@TType");
     437             : 
     438             :   MCSymbol *TTBaseLabel = nullptr;
     439        6540 :   if (HaveTTData) {
     440             :     // N.B.: There is a dependency loop between the size of the TTBase uleb128
     441             :     // here and the amount of padding before the aligned type table. The
     442             :     // assembler must sometimes pad this uleb128 or insert extra padding before
     443             :     // the type table. See PR35809 or GNU as bug 4029.
     444        7662 :     MCSymbol *TTBaseRefLabel = Asm->createTempSymbol("ttbaseref");
     445        7662 :     TTBaseLabel = Asm->createTempSymbol("ttbase");
     446        3831 :     Asm->EmitLabelDifferenceAsULEB128(TTBaseLabel, TTBaseRefLabel);
     447        7662 :     Asm->OutStreamer->EmitLabel(TTBaseRefLabel);
     448             :   }
     449             : 
     450       13080 :   bool VerboseAsm = Asm->OutStreamer->isVerboseAsm();
     451             : 
     452             :   // Emit the landing pad call site table.
     453       13080 :   MCSymbol *CstBeginLabel = Asm->createTempSymbol("cst_begin");
     454       13080 :   MCSymbol *CstEndLabel = Asm->createTempSymbol("cst_end");
     455        6540 :   Asm->EmitEncodingByte(CallSiteEncoding, "Call site");
     456        6540 :   Asm->EmitLabelDifferenceAsULEB128(CstEndLabel, CstBeginLabel);
     457       13080 :   Asm->OutStreamer->EmitLabel(CstBeginLabel);
     458             : 
     459             :   // SjLj Exception handling
     460        6540 :   if (IsSJLJ) {
     461             :     unsigned idx = 0;
     462         175 :     for (SmallVectorImpl<CallSiteEntry>::const_iterator
     463         211 :          I = CallSites.begin(), E = CallSites.end(); I != E; ++I, ++idx) {
     464             :       const CallSiteEntry &S = *I;
     465             : 
     466             :       // Index of the call site entry.
     467         175 :       if (VerboseAsm) {
     468         141 :         Asm->OutStreamer->AddComment(">> Call Site " + Twine(idx) + " <<");
     469         141 :         Asm->OutStreamer->AddComment("  On exception at call site "+Twine(idx));
     470             :       }
     471         175 :       Asm->EmitULEB128(idx);
     472             : 
     473             :       // Offset of the first associated action record, relative to the start of
     474             :       // the action table. This value is biased by 1 (1 indicates the start of
     475             :       // the action table), and 0 indicates that there are no actions.
     476         175 :       if (VerboseAsm) {
     477          47 :         if (S.Action == 0)
     478          60 :           Asm->OutStreamer->AddComment("  Action: cleanup");
     479             :         else
     480          54 :           Asm->OutStreamer->AddComment("  Action: " +
     481          54 :                                        Twine((S.Action - 1) / 2 + 1));
     482             :       }
     483         175 :       Asm->EmitULEB128(S.Action);
     484             :     }
     485             :   } else {
     486             :     // Itanium LSDA exception handling
     487             : 
     488             :     // The call-site table is a list of all call sites that may throw an
     489             :     // exception (including C++ 'throw' statements) in the procedure
     490             :     // fragment. It immediately follows the LSDA header. Each entry indicates,
     491             :     // for a given call, the first corresponding action record and corresponding
     492             :     // landing pad.
     493             :     //
     494             :     // The table begins with the number of bytes, stored as an LEB128
     495             :     // compressed, unsigned integer. The records immediately follow the record
     496             :     // count. They are sorted in increasing call-site address. Each record
     497             :     // indicates:
     498             :     //
     499             :     //   * The position of the call-site.
     500             :     //   * The position of the landing pad.
     501             :     //   * The first action record for that call site.
     502             :     //
     503             :     // A missing entry in the call-site table indicates that a call is not
     504             :     // supposed to throw.
     505             : 
     506             :     unsigned Entry = 0;
     507       38210 :     for (SmallVectorImpl<CallSiteEntry>::const_iterator
     508       44714 :          I = CallSites.begin(), E = CallSites.end(); I != E; ++I) {
     509             :       const CallSiteEntry &S = *I;
     510             : 
     511       38210 :       MCSymbol *EHFuncBeginSym = Asm->getFunctionBegin();
     512             : 
     513       38210 :       MCSymbol *BeginLabel = S.BeginLabel;
     514       38210 :       if (!BeginLabel)
     515             :         BeginLabel = EHFuncBeginSym;
     516       38210 :       MCSymbol *EndLabel = S.EndLabel;
     517       38210 :       if (!EndLabel)
     518        4016 :         EndLabel = Asm->getFunctionEnd();
     519             : 
     520             :       // Offset of the call site relative to the start of the procedure.
     521       38210 :       if (VerboseAsm)
     522        1764 :         Asm->OutStreamer->AddComment(">> Call Site " + Twine(++Entry) + " <<");
     523       38210 :       Asm->EmitLabelDifferenceAsULEB128(BeginLabel, EHFuncBeginSym);
     524       38210 :       if (VerboseAsm)
     525        1764 :         Asm->OutStreamer->AddComment(Twine("  Call between ") +
     526        1764 :                                      BeginLabel->getName() + " and " +
     527        1764 :                                      EndLabel->getName());
     528       38210 :       Asm->EmitLabelDifferenceAsULEB128(EndLabel, BeginLabel);
     529             : 
     530             :       // Offset of the landing pad relative to the start of the procedure.
     531       38210 :       if (!S.LPad) {
     532        9066 :         if (VerboseAsm)
     533         648 :           Asm->OutStreamer->AddComment("    has no landing pad");
     534        9066 :         Asm->EmitULEB128(0);
     535             :       } else {
     536       29144 :         if (VerboseAsm)
     537         744 :           Asm->OutStreamer->AddComment(Twine("    jumps to ") +
     538        1116 :                                        S.LPad->LandingPadLabel->getName());
     539       29144 :         Asm->EmitLabelDifferenceAsULEB128(S.LPad->LandingPadLabel,
     540             :                                           EHFuncBeginSym);
     541             :       }
     542             : 
     543             :       // Offset of the first associated action record, relative to the start of
     544             :       // the action table. This value is biased by 1 (1 indicates the start of
     545             :       // the action table), and 0 indicates that there are no actions.
     546       38210 :       if (VerboseAsm) {
     547         588 :         if (S.Action == 0)
     548        1128 :           Asm->OutStreamer->AddComment("  On action: cleanup");
     549             :         else
     550         424 :           Asm->OutStreamer->AddComment("  On action: " +
     551         424 :                                        Twine((S.Action - 1) / 2 + 1));
     552             :       }
     553       38210 :       Asm->EmitULEB128(S.Action);
     554             :     }
     555             :   }
     556       13080 :   Asm->OutStreamer->EmitLabel(CstEndLabel);
     557             : 
     558             :   // Emit the Action Table.
     559             :   int Entry = 0;
     560        3929 :   for (SmallVectorImpl<ActionEntry>::const_iterator
     561       10469 :          I = Actions.begin(), E = Actions.end(); I != E; ++I) {
     562             :     const ActionEntry &Action = *I;
     563             : 
     564        3929 :     if (VerboseAsm) {
     565             :       // Emit comments that decode the action table.
     566         868 :       Asm->OutStreamer->AddComment(">> Action Record " + Twine(++Entry) + " <<");
     567             :     }
     568             : 
     569             :     // Type Filter
     570             :     //
     571             :     //   Used by the runtime to match the type of the thrown exception to the
     572             :     //   type of the catch clauses or the types in the exception specification.
     573        3929 :     if (VerboseAsm) {
     574         217 :       if (Action.ValueForTypeID > 0)
     575         410 :         Asm->OutStreamer->AddComment("  Catch TypeInfo " +
     576         410 :                                      Twine(Action.ValueForTypeID));
     577          12 :       else if (Action.ValueForTypeID < 0)
     578           4 :         Asm->OutStreamer->AddComment("  Filter TypeInfo " +
     579           4 :                                      Twine(Action.ValueForTypeID));
     580             :       else
     581          30 :         Asm->OutStreamer->AddComment("  Cleanup");
     582             :     }
     583        3929 :     Asm->EmitSLEB128(Action.ValueForTypeID);
     584             : 
     585             :     // Action Record
     586             :     //
     587             :     //   Self-relative signed displacement in bytes of the next action record,
     588             :     //   or 0 if there is no next action record.
     589        3929 :     if (VerboseAsm) {
     590         217 :       if (Action.NextAction == 0) {
     591         564 :         Asm->OutStreamer->AddComment("  No further actions");
     592             :       } else {
     593          29 :         unsigned NextAction = Entry + (Action.NextAction + 1) / 2;
     594          87 :         Asm->OutStreamer->AddComment("  Continue to action "+Twine(NextAction));
     595             :       }
     596             :     }
     597        3929 :     Asm->EmitSLEB128(Action.NextAction);
     598             :   }
     599             : 
     600        6540 :   if (HaveTTData) {
     601        3831 :     Asm->EmitAlignment(2);
     602        3831 :     emitTypeInfos(TTypeEncoding, TTBaseLabel);
     603             :   }
     604             : 
     605        6540 :   Asm->EmitAlignment(2);
     606        6540 : }
     607             : 
     608        3801 : void EHStreamer::emitTypeInfos(unsigned TTypeEncoding, MCSymbol *TTBaseLabel) {
     609        3801 :   const MachineFunction *MF = Asm->MF;
     610             :   const std::vector<const GlobalValue *> &TypeInfos = MF->getTypeInfos();
     611             :   const std::vector<unsigned> &FilterIds = MF->getFilterIds();
     612             : 
     613        3801 :   bool VerboseAsm = Asm->OutStreamer->isVerboseAsm();
     614             : 
     615             :   int Entry = 0;
     616             :   // Emit the Catch TypeInfos.
     617        3961 :   if (VerboseAsm && !TypeInfos.empty()) {
     618         477 :     Asm->OutStreamer->AddComment(">> Catch TypeInfos <<");
     619         318 :     Asm->OutStreamer->AddBlankLine();
     620         318 :     Entry = TypeInfos.size();
     621             :   }
     622             : 
     623             :   for (const GlobalValue *GV : make_range(TypeInfos.rbegin(),
     624        7663 :                                           TypeInfos.rend())) {
     625        3862 :     if (VerboseAsm)
     626         537 :       Asm->OutStreamer->AddComment("TypeInfo " + Twine(Entry--));
     627        3862 :     Asm->EmitTTypeReference(GV, TTypeEncoding);
     628             :   }
     629             : 
     630        7602 :   Asm->OutStreamer->EmitLabel(TTBaseLabel);
     631             : 
     632             :   // Emit the Exception Specifications.
     633        3961 :   if (VerboseAsm && !FilterIds.empty()) {
     634           6 :     Asm->OutStreamer->AddComment(">> Filter TypeInfos <<");
     635           4 :     Asm->OutStreamer->AddBlankLine();
     636             :     Entry = 0;
     637             :   }
     638             :   for (std::vector<unsigned>::const_iterator
     639        3801 :          I = FilterIds.begin(), E = FilterIds.end(); I < E; ++I) {
     640          18 :     unsigned TypeID = *I;
     641          18 :     if (VerboseAsm) {
     642           3 :       --Entry;
     643           3 :       if (isFilterEHSelector(TypeID))
     644           0 :         Asm->OutStreamer->AddComment("FilterInfo " + Twine(Entry));
     645             :     }
     646             : 
     647          18 :     Asm->EmitULEB128(TypeID);
     648             :   }
     649        3801 : }

Generated by: LCOV version 1.13