Home | History | Annotate | Download | only in AsmPrinter
      1 //===-- DwarfException.h - Dwarf Exception Framework -----------*- C++ -*--===//
      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 dwarf exception info into asm files.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #ifndef LLVM_CODEGEN_ASMPRINTER_DWARFEXCEPTION_H
     15 #define LLVM_CODEGEN_ASMPRINTER_DWARFEXCEPTION_H
     16 
     17 #include "llvm/ADT/DenseMap.h"
     18 #include "llvm/CodeGen/AsmPrinter.h"
     19 #include <vector>
     20 
     21 namespace llvm {
     22 
     23 template <typename T> class SmallVectorImpl;
     24 struct LandingPadInfo;
     25 class MachineModuleInfo;
     26 class MachineInstr;
     27 class MachineFunction;
     28 class MCAsmInfo;
     29 class MCExpr;
     30 class MCSymbol;
     31 class Function;
     32 class AsmPrinter;
     33 
     34 //===----------------------------------------------------------------------===//
     35 /// DwarfException - Emits Dwarf exception handling directives.
     36 ///
     37 class DwarfException {
     38 protected:
     39   /// Asm - Target of Dwarf emission.
     40   AsmPrinter *Asm;
     41 
     42   /// MMI - Collected machine module information.
     43   MachineModuleInfo *MMI;
     44 
     45   /// SharedTypeIds - How many leading type ids two landing pads have in common.
     46   static unsigned SharedTypeIds(const LandingPadInfo *L,
     47                                 const LandingPadInfo *R);
     48 
     49   /// PadLT - Order landing pads lexicographically by type id.
     50   static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R);
     51 
     52   /// PadRange - Structure holding a try-range and the associated landing pad.
     53   struct PadRange {
     54     // The index of the landing pad.
     55     unsigned PadIndex;
     56     // The index of the begin and end labels in the landing pad's label lists.
     57     unsigned RangeIndex;
     58   };
     59 
     60   typedef DenseMap<MCSymbol *, PadRange> RangeMapType;
     61 
     62   /// ActionEntry - Structure describing an entry in the actions table.
     63   struct ActionEntry {
     64     int ValueForTypeID; // The value to write - may not be equal to the type id.
     65     int NextAction;
     66     unsigned Previous;
     67   };
     68 
     69   /// CallSiteEntry - Structure describing an entry in the call-site table.
     70   struct CallSiteEntry {
     71     // The 'try-range' is BeginLabel .. EndLabel.
     72     MCSymbol *BeginLabel; // zero indicates the start of the function.
     73     MCSymbol *EndLabel;   // zero indicates the end of the function.
     74 
     75     // The landing pad starts at PadLabel.
     76     MCSymbol *PadLabel;   // zero indicates that there is no landing pad.
     77     unsigned Action;
     78   };
     79 
     80   /// ComputeActionsTable - Compute the actions table and gather the first
     81   /// action index for each landing pad site.
     82   unsigned ComputeActionsTable(const SmallVectorImpl<const LandingPadInfo*>&LPs,
     83                                SmallVectorImpl<ActionEntry> &Actions,
     84                                SmallVectorImpl<unsigned> &FirstActions);
     85 
     86   /// CallToNoUnwindFunction - Return `true' if this is a call to a function
     87   /// marked `nounwind'. Return `false' otherwise.
     88   bool CallToNoUnwindFunction(const MachineInstr *MI);
     89 
     90   /// ComputeCallSiteTable - Compute the call-site table.  The entry for an
     91   /// invoke has a try-range containing the call, a non-zero landing pad and an
     92   /// appropriate action.  The entry for an ordinary call has a try-range
     93   /// containing the call and zero for the landing pad and the action.  Calls
     94   /// marked 'nounwind' have no entry and must not be contained in the try-range
     95   /// of any entry - they form gaps in the table.  Entries must be ordered by
     96   /// try-range address.
     97   void ComputeCallSiteTable(SmallVectorImpl<CallSiteEntry> &CallSites,
     98                             const RangeMapType &PadMap,
     99                             const SmallVectorImpl<const LandingPadInfo *> &LPs,
    100                             const SmallVectorImpl<unsigned> &FirstActions);
    101 
    102   /// EmitExceptionTable - Emit landing pads and actions.
    103   ///
    104   /// The general organization of the table is complex, but the basic concepts
    105   /// are easy.  First there is a header which describes the location and
    106   /// organization of the three components that follow.
    107   ///  1. The landing pad site information describes the range of code covered
    108   ///     by the try.  In our case it's an accumulation of the ranges covered
    109   ///     by the invokes in the try.  There is also a reference to the landing
    110   ///     pad that handles the exception once processed.  Finally an index into
    111   ///     the actions table.
    112   ///  2. The action table, in our case, is composed of pairs of type ids
    113   ///     and next action offset.  Starting with the action index from the
    114   ///     landing pad site, each type Id is checked for a match to the current
    115   ///     exception.  If it matches then the exception and type id are passed
    116   ///     on to the landing pad.  Otherwise the next action is looked up.  This
    117   ///     chain is terminated with a next action of zero.  If no type id is
    118   ///     found the frame is unwound and handling continues.
    119   ///  3. Type id table contains references to all the C++ typeinfo for all
    120   ///     catches in the function.  This tables is reversed indexed base 1.
    121   void EmitExceptionTable();
    122 
    123   virtual void EmitTypeInfos(unsigned TTypeEncoding);
    124 
    125 public:
    126   //===--------------------------------------------------------------------===//
    127   // Main entry points.
    128   //
    129   DwarfException(AsmPrinter *A);
    130   virtual ~DwarfException();
    131 
    132   /// EndModule - Emit all exception information that should come after the
    133   /// content.
    134   virtual void EndModule();
    135 
    136   /// BeginFunction - Gather pre-function exception information.  Assumes being
    137   /// emitted immediately after the function entry point.
    138   virtual void BeginFunction(const MachineFunction *MF);
    139 
    140   /// EndFunction - Gather and emit post-function exception information.
    141   virtual void EndFunction();
    142 };
    143 
    144 class DwarfCFIException : public DwarfException {
    145   /// shouldEmitPersonality - Per-function flag to indicate if .cfi_personality
    146   /// should be emitted.
    147   bool shouldEmitPersonality;
    148 
    149   /// shouldEmitLSDA - Per-function flag to indicate if .cfi_lsda
    150   /// should be emitted.
    151   bool shouldEmitLSDA;
    152 
    153   /// shouldEmitMoves - Per-function flag to indicate if frame moves info
    154   /// should be emitted.
    155   bool shouldEmitMoves;
    156 
    157   AsmPrinter::CFIMoveType moveTypeModule;
    158 
    159 public:
    160   //===--------------------------------------------------------------------===//
    161   // Main entry points.
    162   //
    163   DwarfCFIException(AsmPrinter *A);
    164   virtual ~DwarfCFIException();
    165 
    166   /// EndModule - Emit all exception information that should come after the
    167   /// content.
    168   virtual void EndModule();
    169 
    170   /// BeginFunction - Gather pre-function exception information.  Assumes being
    171   /// emitted immediately after the function entry point.
    172   virtual void BeginFunction(const MachineFunction *MF);
    173 
    174   /// EndFunction - Gather and emit post-function exception information.
    175   virtual void EndFunction();
    176 };
    177 
    178 class ARMException : public DwarfException {
    179   void EmitTypeInfos(unsigned TTypeEncoding);
    180 public:
    181   //===--------------------------------------------------------------------===//
    182   // Main entry points.
    183   //
    184   ARMException(AsmPrinter *A);
    185   virtual ~ARMException();
    186 
    187   /// EndModule - Emit all exception information that should come after the
    188   /// content.
    189   virtual void EndModule();
    190 
    191   /// BeginFunction - Gather pre-function exception information.  Assumes being
    192   /// emitted immediately after the function entry point.
    193   virtual void BeginFunction(const MachineFunction *MF);
    194 
    195   /// EndFunction - Gather and emit post-function exception information.
    196   virtual void EndFunction();
    197 };
    198 
    199 class Win64Exception : public DwarfException {
    200   /// shouldEmitPersonality - Per-function flag to indicate if personality
    201   /// info should be emitted.
    202   bool shouldEmitPersonality;
    203 
    204   /// shouldEmitLSDA - Per-function flag to indicate if the LSDA
    205   /// should be emitted.
    206   bool shouldEmitLSDA;
    207 
    208   /// shouldEmitMoves - Per-function flag to indicate if frame moves info
    209   /// should be emitted.
    210   bool shouldEmitMoves;
    211 
    212 public:
    213   //===--------------------------------------------------------------------===//
    214   // Main entry points.
    215   //
    216   Win64Exception(AsmPrinter *A);
    217   virtual ~Win64Exception();
    218 
    219   /// EndModule - Emit all exception information that should come after the
    220   /// content.
    221   virtual void EndModule();
    222 
    223   /// BeginFunction - Gather pre-function exception information.  Assumes being
    224   /// emitted immediately after the function entry point.
    225   virtual void BeginFunction(const MachineFunction *MF);
    226 
    227   /// EndFunction - Gather and emit post-function exception information.
    228   virtual void EndFunction();
    229 };
    230 
    231 } // End of namespace llvm
    232 
    233 #endif
    234