DwarfException.h revision 2eb6c429c6f9344cefa506da783e0d373f64268c
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 <vector>
19
20namespace llvm {
21
22template <typename T> class SmallVectorImpl;
23struct LandingPadInfo;
24class MachineModuleInfo;
25class MachineMove;
26class MachineInstr;
27class MachineFunction;
28class MCAsmInfo;
29class MCExpr;
30class MCSymbol;
31class Timer;
32class Function;
33class AsmPrinter;
34
35//===----------------------------------------------------------------------===//
36/// DwarfException - Emits Dwarf exception handling directives.
37///
38class DwarfException {
39  /// Asm - Target of Dwarf emission.
40  AsmPrinter *Asm;
41
42  /// MMI - Collected machine module information.
43  MachineModuleInfo *MMI;
44
45  struct FunctionEHFrameInfo {
46    MCSymbol *FunctionEHSym;  // L_foo.eh
47    unsigned Number;
48    unsigned PersonalityIndex;
49    bool hasCalls;
50    bool hasLandingPads;
51    std::vector<MachineMove> Moves;
52    const Function *function;
53
54    FunctionEHFrameInfo(MCSymbol *EHSym, unsigned Num, unsigned P,
55                        bool hC, bool hL,
56                        const std::vector<MachineMove> &M,
57                        const Function *f):
58      FunctionEHSym(EHSym), Number(Num), PersonalityIndex(P),
59      hasCalls(hC), hasLandingPads(hL), Moves(M), function (f) { }
60  };
61
62  std::vector<FunctionEHFrameInfo> EHFrames;
63
64  /// UsesLSDA - Indicates whether an FDE that uses the CIE at the given index
65  /// uses an LSDA. If so, then we need to encode that information in the CIE's
66  /// augmentation.
67  DenseMap<unsigned, bool> UsesLSDA;
68
69  /// shouldEmitTable - Per-function flag to indicate if EH tables should
70  /// be emitted.
71  bool shouldEmitTable;
72
73  /// shouldEmitMoves - Per-function flag to indicate if frame moves info
74  /// should be emitted.
75  bool shouldEmitMoves;
76
77  /// shouldEmitTableModule - Per-module flag to indicate if EH tables
78  /// should be emitted.
79  bool shouldEmitTableModule;
80
81  /// shouldEmitFrameModule - Per-module flag to indicate if frame moves
82  /// should be emitted.
83  bool shouldEmitMovesModule;
84
85  /// ExceptionTimer - Timer for the Dwarf exception writer.
86  Timer *ExceptionTimer;
87
88  /// EmitCIE - Emit a Common Information Entry (CIE). This holds information
89  /// that is shared among many Frame Description Entries.  There is at least
90  /// one CIE in every non-empty .debug_frame section.
91  void EmitCIE(const Function *Personality, unsigned Index);
92
93  /// EmitFDE - Emit the Frame Description Entry (FDE) for the function.
94  void EmitFDE(const FunctionEHFrameInfo &EHFrameInfo);
95
96  /// EmitExceptionTable - Emit landing pads and actions.
97  ///
98  /// The general organization of the table is complex, but the basic concepts
99  /// are easy.  First there is a header which describes the location and
100  /// organization of the three components that follow.
101  ///  1. The landing pad site information describes the range of code covered
102  ///     by the try.  In our case it's an accumulation of the ranges covered
103  ///     by the invokes in the try.  There is also a reference to the landing
104  ///     pad that handles the exception once processed.  Finally an index into
105  ///     the actions table.
106  ///  2. The action table, in our case, is composed of pairs of type ids
107  ///     and next action offset.  Starting with the action index from the
108  ///     landing pad site, each type Id is checked for a match to the current
109  ///     exception.  If it matches then the exception and type id are passed
110  ///     on to the landing pad.  Otherwise the next action is looked up.  This
111  ///     chain is terminated with a next action of zero.  If no type id is
112  ///     found the frame is unwound and handling continues.
113  ///  3. Type id table contains references to all the C++ typeinfo for all
114  ///     catches in the function.  This tables is reversed indexed base 1.
115
116  /// SharedTypeIds - How many leading type ids two landing pads have in common.
117  static unsigned SharedTypeIds(const LandingPadInfo *L,
118                                const LandingPadInfo *R);
119
120  /// PadLT - Order landing pads lexicographically by type id.
121  static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R);
122
123  /// PadRange - Structure holding a try-range and the associated landing pad.
124  struct PadRange {
125    // The index of the landing pad.
126    unsigned PadIndex;
127    // The index of the begin and end labels in the landing pad's label lists.
128    unsigned RangeIndex;
129  };
130
131  typedef DenseMap<MCSymbol *, PadRange> RangeMapType;
132
133  /// ActionEntry - Structure describing an entry in the actions table.
134  struct ActionEntry {
135    int ValueForTypeID; // The value to write - may not be equal to the type id.
136    int NextAction;
137    unsigned Previous;
138  };
139
140  /// CallSiteEntry - Structure describing an entry in the call-site table.
141  struct CallSiteEntry {
142    // The 'try-range' is BeginLabel .. EndLabel.
143    MCSymbol *BeginLabel; // zero indicates the start of the function.
144    MCSymbol *EndLabel;   // zero indicates the end of the function.
145
146    // The landing pad starts at PadLabel.
147    MCSymbol *PadLabel;   // zero indicates that there is no landing pad.
148    unsigned Action;
149  };
150
151  /// ComputeActionsTable - Compute the actions table and gather the first
152  /// action index for each landing pad site.
153  unsigned ComputeActionsTable(const SmallVectorImpl<const LandingPadInfo*>&LPs,
154                               SmallVectorImpl<ActionEntry> &Actions,
155                               SmallVectorImpl<unsigned> &FirstActions);
156
157  /// CallToNoUnwindFunction - Return `true' if this is a call to a function
158  /// marked `nounwind'. Return `false' otherwise.
159  bool CallToNoUnwindFunction(const MachineInstr *MI);
160
161  /// ComputeCallSiteTable - Compute the call-site table.  The entry for an
162  /// invoke has a try-range containing the call, a non-zero landing pad and an
163  /// appropriate action.  The entry for an ordinary call has a try-range
164  /// containing the call and zero for the landing pad and the action.  Calls
165  /// marked 'nounwind' have no entry and must not be contained in the try-range
166  /// of any entry - they form gaps in the table.  Entries must be ordered by
167  /// try-range address.
168  void ComputeCallSiteTable(SmallVectorImpl<CallSiteEntry> &CallSites,
169                            const RangeMapType &PadMap,
170                            const SmallVectorImpl<const LandingPadInfo *> &LPs,
171                            const SmallVectorImpl<unsigned> &FirstActions);
172  void EmitExceptionTable();
173
174public:
175  //===--------------------------------------------------------------------===//
176  // Main entry points.
177  //
178  DwarfException(AsmPrinter *A);
179  virtual ~DwarfException();
180
181  /// EndModule - Emit all exception information that should come after the
182  /// content.
183  void EndModule();
184
185  /// BeginFunction - Gather pre-function exception information.  Assumes being
186  /// emitted immediately after the function entry point.
187  void BeginFunction(const MachineFunction *MF);
188
189  /// EndFunction - Gather and emit post-function exception information.
190  void EndFunction();
191};
192
193} // End of namespace llvm
194
195#endif
196