DwarfException.h revision 218893
1193323Sed//===-- DwarfException.h - Dwarf Exception Framework -----------*- C++ -*--===//
2193323Sed//
3193323Sed//                     The LLVM Compiler Infrastructure
4193323Sed//
5193323Sed// This file is distributed under the University of Illinois Open Source
6193323Sed// License. See LICENSE.TXT for details.
7193323Sed//
8193323Sed//===----------------------------------------------------------------------===//
9193323Sed//
10193323Sed// This file contains support for writing dwarf exception info into asm files.
11193323Sed//
12193323Sed//===----------------------------------------------------------------------===//
13193323Sed
14198090Srdivacky#ifndef LLVM_CODEGEN_ASMPRINTER_DWARFEXCEPTION_H
15198090Srdivacky#define LLVM_CODEGEN_ASMPRINTER_DWARFEXCEPTION_H
16193323Sed
17193323Sed#include "llvm/ADT/DenseMap.h"
18206274Srdivacky#include <vector>
19193323Sed
20193323Sednamespace llvm {
21193323Sed
22206274Srdivackytemplate <typename T> class SmallVectorImpl;
23193323Sedstruct LandingPadInfo;
24193323Sedclass MachineModuleInfo;
25206274Srdivackyclass MachineMove;
26206274Srdivackyclass MachineInstr;
27206274Srdivackyclass MachineFunction;
28198090Srdivackyclass MCAsmInfo;
29199481Srdivackyclass MCExpr;
30206274Srdivackyclass MCSymbol;
31206274Srdivackyclass Function;
32206274Srdivackyclass AsmPrinter;
33193323Sed
34193323Sed//===----------------------------------------------------------------------===//
35193323Sed/// DwarfException - Emits Dwarf exception handling directives.
36193323Sed///
37206274Srdivackyclass DwarfException {
38218893Sdimprotected:
39206274Srdivacky  /// Asm - Target of Dwarf emission.
40206274Srdivacky  AsmPrinter *Asm;
41206274Srdivacky
42206274Srdivacky  /// MMI - Collected machine module information.
43206274Srdivacky  MachineModuleInfo *MMI;
44206274Srdivacky
45193323Sed  /// EmitExceptionTable - Emit landing pads and actions.
46193323Sed  ///
47193323Sed  /// The general organization of the table is complex, but the basic concepts
48193323Sed  /// are easy.  First there is a header which describes the location and
49193323Sed  /// organization of the three components that follow.
50193323Sed  ///  1. The landing pad site information describes the range of code covered
51193323Sed  ///     by the try.  In our case it's an accumulation of the ranges covered
52193323Sed  ///     by the invokes in the try.  There is also a reference to the landing
53193323Sed  ///     pad that handles the exception once processed.  Finally an index into
54193323Sed  ///     the actions table.
55193323Sed  ///  2. The action table, in our case, is composed of pairs of type ids
56193323Sed  ///     and next action offset.  Starting with the action index from the
57193323Sed  ///     landing pad site, each type Id is checked for a match to the current
58193323Sed  ///     exception.  If it matches then the exception and type id are passed
59193323Sed  ///     on to the landing pad.  Otherwise the next action is looked up.  This
60193323Sed  ///     chain is terminated with a next action of zero.  If no type id is
61203954Srdivacky  ///     found the frame is unwound and handling continues.
62193323Sed  ///  3. Type id table contains references to all the C++ typeinfo for all
63193323Sed  ///     catches in the function.  This tables is reversed indexed base 1.
64193323Sed
65193323Sed  /// SharedTypeIds - How many leading type ids two landing pads have in common.
66193323Sed  static unsigned SharedTypeIds(const LandingPadInfo *L,
67193323Sed                                const LandingPadInfo *R);
68193323Sed
69193323Sed  /// PadLT - Order landing pads lexicographically by type id.
70193323Sed  static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R);
71193323Sed
72193323Sed  /// PadRange - Structure holding a try-range and the associated landing pad.
73193323Sed  struct PadRange {
74193323Sed    // The index of the landing pad.
75193323Sed    unsigned PadIndex;
76193323Sed    // The index of the begin and end labels in the landing pad's label lists.
77193323Sed    unsigned RangeIndex;
78193323Sed  };
79193323Sed
80205218Srdivacky  typedef DenseMap<MCSymbol *, PadRange> RangeMapType;
81193323Sed
82198090Srdivacky  /// ActionEntry - Structure describing an entry in the actions table.
83198090Srdivacky  struct ActionEntry {
84198090Srdivacky    int ValueForTypeID; // The value to write - may not be equal to the type id.
85198090Srdivacky    int NextAction;
86203954Srdivacky    unsigned Previous;
87198090Srdivacky  };
88198090Srdivacky
89193323Sed  /// CallSiteEntry - Structure describing an entry in the call-site table.
90193323Sed  struct CallSiteEntry {
91193323Sed    // The 'try-range' is BeginLabel .. EndLabel.
92205218Srdivacky    MCSymbol *BeginLabel; // zero indicates the start of the function.
93205218Srdivacky    MCSymbol *EndLabel;   // zero indicates the end of the function.
94198090Srdivacky
95193323Sed    // The landing pad starts at PadLabel.
96205218Srdivacky    MCSymbol *PadLabel;   // zero indicates that there is no landing pad.
97193323Sed    unsigned Action;
98193323Sed  };
99193323Sed
100198090Srdivacky  /// ComputeActionsTable - Compute the actions table and gather the first
101198090Srdivacky  /// action index for each landing pad site.
102198090Srdivacky  unsigned ComputeActionsTable(const SmallVectorImpl<const LandingPadInfo*>&LPs,
103198090Srdivacky                               SmallVectorImpl<ActionEntry> &Actions,
104198090Srdivacky                               SmallVectorImpl<unsigned> &FirstActions);
105198090Srdivacky
106199481Srdivacky  /// CallToNoUnwindFunction - Return `true' if this is a call to a function
107199481Srdivacky  /// marked `nounwind'. Return `false' otherwise.
108199481Srdivacky  bool CallToNoUnwindFunction(const MachineInstr *MI);
109199481Srdivacky
110198090Srdivacky  /// ComputeCallSiteTable - Compute the call-site table.  The entry for an
111198090Srdivacky  /// invoke has a try-range containing the call, a non-zero landing pad and an
112198090Srdivacky  /// appropriate action.  The entry for an ordinary call has a try-range
113198090Srdivacky  /// containing the call and zero for the landing pad and the action.  Calls
114198090Srdivacky  /// marked 'nounwind' have no entry and must not be contained in the try-range
115198090Srdivacky  /// of any entry - they form gaps in the table.  Entries must be ordered by
116198090Srdivacky  /// try-range address.
117198090Srdivacky  void ComputeCallSiteTable(SmallVectorImpl<CallSiteEntry> &CallSites,
118198090Srdivacky                            const RangeMapType &PadMap,
119198090Srdivacky                            const SmallVectorImpl<const LandingPadInfo *> &LPs,
120198090Srdivacky                            const SmallVectorImpl<unsigned> &FirstActions);
121193323Sed  void EmitExceptionTable();
122193323Sed
123193323Sedpublic:
124193323Sed  //===--------------------------------------------------------------------===//
125193323Sed  // Main entry points.
126193323Sed  //
127206274Srdivacky  DwarfException(AsmPrinter *A);
128218893Sdim  virtual ~DwarfException();
129193323Sed
130193323Sed  /// EndModule - Emit all exception information that should come after the
131193323Sed  /// content.
132218893Sdim  virtual void EndModule();
133193323Sed
134193323Sed  /// BeginFunction - Gather pre-function exception information.  Assumes being
135193323Sed  /// emitted immediately after the function entry point.
136218893Sdim  virtual void BeginFunction(const MachineFunction *MF);
137193323Sed
138193323Sed  /// EndFunction - Gather and emit post-function exception information.
139218893Sdim  virtual void EndFunction();
140193323Sed};
141193323Sed
142218893Sdimclass DwarfCFIException : public DwarfException {
143218893Sdim  /// shouldEmitTable - Per-function flag to indicate if EH tables should
144218893Sdim  /// be emitted.
145218893Sdim  bool shouldEmitTable;
146218893Sdim
147218893Sdim  /// shouldEmitMoves - Per-function flag to indicate if frame moves info
148218893Sdim  /// should be emitted.
149218893Sdim  bool shouldEmitMoves;
150218893Sdim
151218893Sdim  /// shouldEmitTableModule - Per-module flag to indicate if EH tables
152218893Sdim  /// should be emitted.
153218893Sdim  bool shouldEmitTableModule;
154218893Sdimpublic:
155218893Sdim  //===--------------------------------------------------------------------===//
156218893Sdim  // Main entry points.
157218893Sdim  //
158218893Sdim  DwarfCFIException(AsmPrinter *A);
159218893Sdim  virtual ~DwarfCFIException();
160218893Sdim
161218893Sdim  /// EndModule - Emit all exception information that should come after the
162218893Sdim  /// content.
163218893Sdim  virtual void EndModule();
164218893Sdim
165218893Sdim  /// BeginFunction - Gather pre-function exception information.  Assumes being
166218893Sdim  /// emitted immediately after the function entry point.
167218893Sdim  virtual void BeginFunction(const MachineFunction *MF);
168218893Sdim
169218893Sdim  /// EndFunction - Gather and emit post-function exception information.
170218893Sdim  virtual void EndFunction();
171218893Sdim};
172218893Sdim
173218893Sdimclass DwarfTableException : public DwarfException {
174218893Sdim  /// shouldEmitTable - Per-function flag to indicate if EH tables should
175218893Sdim  /// be emitted.
176218893Sdim  bool shouldEmitTable;
177218893Sdim
178218893Sdim  /// shouldEmitMoves - Per-function flag to indicate if frame moves info
179218893Sdim  /// should be emitted.
180218893Sdim  bool shouldEmitMoves;
181218893Sdim
182218893Sdim  /// shouldEmitTableModule - Per-module flag to indicate if EH tables
183218893Sdim  /// should be emitted.
184218893Sdim  bool shouldEmitTableModule;
185218893Sdim
186218893Sdim  /// shouldEmitMovesModule - Per-module flag to indicate if frame moves
187218893Sdim  /// should be emitted.
188218893Sdim  bool shouldEmitMovesModule;
189218893Sdim
190218893Sdim  struct FunctionEHFrameInfo {
191218893Sdim    MCSymbol *FunctionEHSym;  // L_foo.eh
192218893Sdim    unsigned Number;
193218893Sdim    unsigned PersonalityIndex;
194218893Sdim    bool adjustsStack;
195218893Sdim    bool hasLandingPads;
196218893Sdim    std::vector<MachineMove> Moves;
197218893Sdim    const Function *function;
198218893Sdim
199218893Sdim    FunctionEHFrameInfo(MCSymbol *EHSym, unsigned Num, unsigned P,
200218893Sdim                        bool hC, bool hL,
201218893Sdim                        const std::vector<MachineMove> &M,
202218893Sdim                        const Function *f):
203218893Sdim      FunctionEHSym(EHSym), Number(Num), PersonalityIndex(P),
204218893Sdim      adjustsStack(hC), hasLandingPads(hL), Moves(M), function (f) { }
205218893Sdim  };
206218893Sdim
207218893Sdim  std::vector<FunctionEHFrameInfo> EHFrames;
208218893Sdim
209218893Sdim  /// UsesLSDA - Indicates whether an FDE that uses the CIE at the given index
210218893Sdim  /// uses an LSDA. If so, then we need to encode that information in the CIE's
211218893Sdim  /// augmentation.
212218893Sdim  DenseMap<unsigned, bool> UsesLSDA;
213218893Sdim
214218893Sdim  /// EmitCIE - Emit a Common Information Entry (CIE). This holds information
215218893Sdim  /// that is shared among many Frame Description Entries.  There is at least
216218893Sdim  /// one CIE in every non-empty .debug_frame section.
217218893Sdim  void EmitCIE(const Function *Personality, unsigned Index);
218218893Sdim
219218893Sdim  /// EmitFDE - Emit the Frame Description Entry (FDE) for the function.
220218893Sdim  void EmitFDE(const FunctionEHFrameInfo &EHFrameInfo);
221218893Sdimpublic:
222218893Sdim  //===--------------------------------------------------------------------===//
223218893Sdim  // Main entry points.
224218893Sdim  //
225218893Sdim  DwarfTableException(AsmPrinter *A);
226218893Sdim  virtual ~DwarfTableException();
227218893Sdim
228218893Sdim  /// EndModule - Emit all exception information that should come after the
229218893Sdim  /// content.
230218893Sdim  virtual void EndModule();
231218893Sdim
232218893Sdim  /// BeginFunction - Gather pre-function exception information.  Assumes being
233218893Sdim  /// emitted immediately after the function entry point.
234218893Sdim  virtual void BeginFunction(const MachineFunction *MF);
235218893Sdim
236218893Sdim  /// EndFunction - Gather and emit post-function exception information.
237218893Sdim  virtual void EndFunction();
238218893Sdim};
239218893Sdim
240193323Sed} // End of namespace llvm
241193323Sed
242193323Sed#endif
243