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

Generated by: LCOV version 1.13