10b57cec5SDimitry Andric //===- EHStreamer.h - Exception Handling Directive Streamer -----*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file contains support for writing exception info into assembly files.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_EHSTREAMER_H
140b57cec5SDimitry Andric #define LLVM_LIB_CODEGEN_ASMPRINTER_EHSTREAMER_H
150b57cec5SDimitry Andric 
160b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h"
170b57cec5SDimitry Andric #include "llvm/CodeGen/AsmPrinterHandler.h"
180b57cec5SDimitry Andric #include "llvm/Support/Compiler.h"
190b57cec5SDimitry Andric 
200b57cec5SDimitry Andric namespace llvm {
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric class AsmPrinter;
230b57cec5SDimitry Andric struct LandingPadInfo;
240b57cec5SDimitry Andric class MachineInstr;
250b57cec5SDimitry Andric class MachineModuleInfo;
260b57cec5SDimitry Andric class MCSymbol;
270b57cec5SDimitry Andric template <typename T> class SmallVectorImpl;
280b57cec5SDimitry Andric 
290b57cec5SDimitry Andric /// Emits exception handling directives.
300b57cec5SDimitry Andric class LLVM_LIBRARY_VISIBILITY EHStreamer : public AsmPrinterHandler {
310b57cec5SDimitry Andric protected:
320b57cec5SDimitry Andric   /// Target of directive emission.
330b57cec5SDimitry Andric   AsmPrinter *Asm;
340b57cec5SDimitry Andric 
350b57cec5SDimitry Andric   /// Collected machine module information.
360b57cec5SDimitry Andric   MachineModuleInfo *MMI;
370b57cec5SDimitry Andric 
380b57cec5SDimitry Andric   /// How many leading type ids two landing pads have in common.
390b57cec5SDimitry Andric   static unsigned sharedTypeIDs(const LandingPadInfo *L,
400b57cec5SDimitry Andric                                 const LandingPadInfo *R);
410b57cec5SDimitry Andric 
420b57cec5SDimitry Andric   /// Structure holding a try-range and the associated landing pad.
430b57cec5SDimitry Andric   struct PadRange {
440b57cec5SDimitry Andric     // The index of the landing pad.
450b57cec5SDimitry Andric     unsigned PadIndex;
460b57cec5SDimitry Andric 
470b57cec5SDimitry Andric     // The index of the begin and end labels in the landing pad's label lists.
480b57cec5SDimitry Andric     unsigned RangeIndex;
490b57cec5SDimitry Andric   };
500b57cec5SDimitry Andric 
510b57cec5SDimitry Andric   using RangeMapType = DenseMap<MCSymbol *, PadRange>;
520b57cec5SDimitry Andric 
530b57cec5SDimitry Andric   /// Structure describing an entry in the actions table.
540b57cec5SDimitry Andric   struct ActionEntry {
550b57cec5SDimitry Andric     int ValueForTypeID; // The value to write - may not be equal to the type id.
560b57cec5SDimitry Andric     int NextAction;
570b57cec5SDimitry Andric     unsigned Previous;
580b57cec5SDimitry Andric   };
590b57cec5SDimitry Andric 
600b57cec5SDimitry Andric   /// Structure describing an entry in the call-site table.
610b57cec5SDimitry Andric   struct CallSiteEntry {
620b57cec5SDimitry Andric     // The 'try-range' is BeginLabel .. EndLabel.
630b57cec5SDimitry Andric     MCSymbol *BeginLabel; // Null indicates the start of the function.
640b57cec5SDimitry Andric     MCSymbol *EndLabel;   // Null indicates the end of the function.
650b57cec5SDimitry Andric 
660b57cec5SDimitry Andric     // LPad contains the landing pad start labels.
670b57cec5SDimitry Andric     const LandingPadInfo *LPad; // Null indicates that there is no landing pad.
680b57cec5SDimitry Andric 
690b57cec5SDimitry Andric     unsigned Action;
700b57cec5SDimitry Andric   };
710b57cec5SDimitry Andric 
720b57cec5SDimitry Andric   /// Structure describing a contiguous range of call-sites which reside
730b57cec5SDimitry Andric   /// in the same procedure fragment. With -fbasic-block-sections, there will
740b57cec5SDimitry Andric   /// be one call site range per basic block section. Otherwise, we will have
750b57cec5SDimitry Andric   /// one call site range containing all the call sites in the function.
760b57cec5SDimitry Andric   struct CallSiteRange {
770b57cec5SDimitry Andric     // Symbol marking the beginning of the precedure fragment.
780b57cec5SDimitry Andric     MCSymbol *FragmentBeginLabel = nullptr;
790b57cec5SDimitry Andric     // Symbol marking the end of the procedure fragment.
800b57cec5SDimitry Andric     MCSymbol *FragmentEndLabel = nullptr;
810b57cec5SDimitry Andric     // LSDA symbol for this call-site range.
820b57cec5SDimitry Andric     MCSymbol *ExceptionLabel = nullptr;
830b57cec5SDimitry Andric     // Index of the first call-site entry in the call-site table which
840b57cec5SDimitry Andric     // belongs to this range.
850b57cec5SDimitry Andric     size_t CallSiteBeginIdx = 0;
860b57cec5SDimitry Andric     // Index just after the last call-site entry in the call-site table which
870b57cec5SDimitry Andric     // belongs to this range.
880b57cec5SDimitry Andric     size_t CallSiteEndIdx = 0;
890b57cec5SDimitry Andric     // Whether this is the call-site range containing all the landing pads.
900b57cec5SDimitry Andric     bool IsLPRange = false;
910b57cec5SDimitry Andric   };
920b57cec5SDimitry Andric 
930b57cec5SDimitry Andric   /// Compute the actions table and gather the first action index for each
940b57cec5SDimitry Andric   /// landing pad site.
950b57cec5SDimitry Andric   void computeActionsTable(
960b57cec5SDimitry Andric       const SmallVectorImpl<const LandingPadInfo *> &LandingPads,
970b57cec5SDimitry Andric       SmallVectorImpl<ActionEntry> &Actions,
980b57cec5SDimitry Andric       SmallVectorImpl<unsigned> &FirstActions);
990b57cec5SDimitry Andric 
1000b57cec5SDimitry Andric   void computePadMap(const SmallVectorImpl<const LandingPadInfo *> &LandingPads,
1010b57cec5SDimitry Andric                      RangeMapType &PadMap);
1020b57cec5SDimitry Andric 
1030b57cec5SDimitry Andric   /// Compute the call-site table and the call-site ranges. The entry for an
1040b57cec5SDimitry Andric   /// invoke has a try-range containing the call, a non-zero landing pad and an
1050b57cec5SDimitry Andric   /// appropriate action. The entry for an ordinary call has a try-range
1060b57cec5SDimitry Andric   /// containing the call and zero for the landing pad and the action.  Calls
1070b57cec5SDimitry Andric   /// marked 'nounwind' have no entry and must not be contained in the try-range
1080b57cec5SDimitry Andric   /// of any entry - they form gaps in the table.  Entries must be ordered by
1090b57cec5SDimitry Andric   /// try-range address. CallSiteRanges vector is only populated for Itanium
1100b57cec5SDimitry Andric   /// exception handling.
1110b57cec5SDimitry Andric   virtual void computeCallSiteTable(
1120b57cec5SDimitry Andric       SmallVectorImpl<CallSiteEntry> &CallSites,
1130b57cec5SDimitry Andric       SmallVectorImpl<CallSiteRange> &CallSiteRanges,
1140b57cec5SDimitry Andric       const SmallVectorImpl<const LandingPadInfo *> &LandingPads,
1150b57cec5SDimitry Andric       const SmallVectorImpl<unsigned> &FirstActions);
1160b57cec5SDimitry Andric 
1170b57cec5SDimitry Andric   /// Emit landing pads and actions.
1180b57cec5SDimitry Andric   ///
1190b57cec5SDimitry Andric   /// The general organization of the table is complex, but the basic concepts
1200b57cec5SDimitry Andric   /// are easy.  First there is a header which describes the location and
1210b57cec5SDimitry Andric   /// organization of the three components that follow.
1220b57cec5SDimitry Andric   ///  1. The landing pad site information describes the range of code covered
1230b57cec5SDimitry Andric   ///     by the try.  In our case it's an accumulation of the ranges covered
1240b57cec5SDimitry Andric   ///     by the invokes in the try.  There is also a reference to the landing
1250b57cec5SDimitry Andric   ///     pad that handles the exception once processed.  Finally an index into
1260b57cec5SDimitry Andric   ///     the actions table.
1270b57cec5SDimitry Andric   ///  2. The action table, in our case, is composed of pairs of type ids
1280b57cec5SDimitry Andric   ///     and next action offset.  Starting with the action index from the
1290b57cec5SDimitry Andric   ///     landing pad site, each type Id is checked for a match to the current
1300b57cec5SDimitry Andric   ///     exception.  If it matches then the exception and type id are passed
1310b57cec5SDimitry Andric   ///     on to the landing pad.  Otherwise the next action is looked up.  This
1320b57cec5SDimitry Andric   ///     chain is terminated with a next action of zero.  If no type id is
1330b57cec5SDimitry Andric   ///     found the frame is unwound and handling continues.
1340b57cec5SDimitry Andric   ///  3. Type id table contains references to all the C++ typeinfo for all
1350b57cec5SDimitry Andric   ///     catches in the function.  This tables is reversed indexed base 1.
1360b57cec5SDimitry Andric   ///
1370b57cec5SDimitry Andric   /// Returns the starting symbol of an exception table.
1380b57cec5SDimitry Andric   MCSymbol *emitExceptionTable();
1390b57cec5SDimitry Andric 
1400b57cec5SDimitry Andric   virtual void emitTypeInfos(unsigned TTypeEncoding, MCSymbol *TTBaseLabel);
141 
142   // Helpers for identifying what kind of clause an EH typeid or selector
143   // corresponds to. Negative selectors are for filter clauses, the zero
144   // selector is for cleanups, and positive selectors are for catch clauses.
isFilterEHSelector(int Selector)145   static bool isFilterEHSelector(int Selector) { return Selector < 0; }
isCleanupEHSelector(int Selector)146   static bool isCleanupEHSelector(int Selector) { return Selector == 0; }
isCatchEHSelector(int Selector)147   static bool isCatchEHSelector(int Selector) { return Selector > 0; }
148 
149 public:
150   EHStreamer(AsmPrinter *A);
151   ~EHStreamer() override;
152 
153   // Unused.
setSymbolSize(const MCSymbol * Sym,uint64_t Size)154   void setSymbolSize(const MCSymbol *Sym, uint64_t Size) override {}
beginInstruction(const MachineInstr * MI)155   void beginInstruction(const MachineInstr *MI) override {}
endInstruction()156   void endInstruction() override {}
157 
158   /// Return `true' if this is a call to a function marked `nounwind'. Return
159   /// `false' otherwise.
160   static bool callToNoUnwindFunction(const MachineInstr *MI);
161 };
162 
163 } // end namespace llvm
164 
165 #endif // LLVM_LIB_CODEGEN_ASMPRINTER_EHSTREAMER_H
166