DwarfException.h revision 206274
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;
31193323Sedclass Timer;
32206274Srdivackyclass Function;
33206274Srdivackyclass AsmPrinter;
34193323Sed
35193323Sed//===----------------------------------------------------------------------===//
36193323Sed/// DwarfException - Emits Dwarf exception handling directives.
37193323Sed///
38206274Srdivackyclass DwarfException {
39206274Srdivacky  /// Asm - Target of Dwarf emission.
40206274Srdivacky  AsmPrinter *Asm;
41206274Srdivacky
42206274Srdivacky  /// MMI - Collected machine module information.
43206274Srdivacky  MachineModuleInfo *MMI;
44206274Srdivacky
45193323Sed  struct FunctionEHFrameInfo {
46202878Srdivacky    MCSymbol *FunctionEHSym;  // L_foo.eh
47193323Sed    unsigned Number;
48193323Sed    unsigned PersonalityIndex;
49193323Sed    bool hasCalls;
50193323Sed    bool hasLandingPads;
51193323Sed    std::vector<MachineMove> Moves;
52202878Srdivacky    const Function *function;
53193323Sed
54202878Srdivacky    FunctionEHFrameInfo(MCSymbol *EHSym, unsigned Num, unsigned P,
55193323Sed                        bool hC, bool hL,
56193323Sed                        const std::vector<MachineMove> &M,
57193323Sed                        const Function *f):
58202878Srdivacky      FunctionEHSym(EHSym), Number(Num), PersonalityIndex(P),
59193323Sed      hasCalls(hC), hasLandingPads(hL), Moves(M), function (f) { }
60193323Sed  };
61193323Sed
62193323Sed  std::vector<FunctionEHFrameInfo> EHFrames;
63193323Sed
64198090Srdivacky  /// UsesLSDA - Indicates whether an FDE that uses the CIE at the given index
65198090Srdivacky  /// uses an LSDA. If so, then we need to encode that information in the CIE's
66198090Srdivacky  /// augmentation.
67198090Srdivacky  DenseMap<unsigned, bool> UsesLSDA;
68198090Srdivacky
69193323Sed  /// shouldEmitTable - Per-function flag to indicate if EH tables should
70193323Sed  /// be emitted.
71193323Sed  bool shouldEmitTable;
72193323Sed
73193323Sed  /// shouldEmitMoves - Per-function flag to indicate if frame moves info
74193323Sed  /// should be emitted.
75193323Sed  bool shouldEmitMoves;
76193323Sed
77193323Sed  /// shouldEmitTableModule - Per-module flag to indicate if EH tables
78193323Sed  /// should be emitted.
79193323Sed  bool shouldEmitTableModule;
80193323Sed
81193323Sed  /// shouldEmitFrameModule - Per-module flag to indicate if frame moves
82193323Sed  /// should be emitted.
83193323Sed  bool shouldEmitMovesModule;
84193323Sed
85193323Sed  /// ExceptionTimer - Timer for the Dwarf exception writer.
86193323Sed  Timer *ExceptionTimer;
87193323Sed
88198090Srdivacky  /// EmitCIE - Emit a Common Information Entry (CIE). This holds information
89198090Srdivacky  /// that is shared among many Frame Description Entries.  There is at least
90198090Srdivacky  /// one CIE in every non-empty .debug_frame section.
91198090Srdivacky  void EmitCIE(const Function *Personality, unsigned Index);
92193323Sed
93198090Srdivacky  /// EmitFDE - Emit the Frame Description Entry (FDE) for the function.
94198090Srdivacky  void EmitFDE(const FunctionEHFrameInfo &EHFrameInfo);
95198090Srdivacky
96193323Sed  /// EmitExceptionTable - Emit landing pads and actions.
97193323Sed  ///
98193323Sed  /// The general organization of the table is complex, but the basic concepts
99193323Sed  /// are easy.  First there is a header which describes the location and
100193323Sed  /// organization of the three components that follow.
101193323Sed  ///  1. The landing pad site information describes the range of code covered
102193323Sed  ///     by the try.  In our case it's an accumulation of the ranges covered
103193323Sed  ///     by the invokes in the try.  There is also a reference to the landing
104193323Sed  ///     pad that handles the exception once processed.  Finally an index into
105193323Sed  ///     the actions table.
106193323Sed  ///  2. The action table, in our case, is composed of pairs of type ids
107193323Sed  ///     and next action offset.  Starting with the action index from the
108193323Sed  ///     landing pad site, each type Id is checked for a match to the current
109193323Sed  ///     exception.  If it matches then the exception and type id are passed
110193323Sed  ///     on to the landing pad.  Otherwise the next action is looked up.  This
111193323Sed  ///     chain is terminated with a next action of zero.  If no type id is
112203954Srdivacky  ///     found the frame is unwound and handling continues.
113193323Sed  ///  3. Type id table contains references to all the C++ typeinfo for all
114193323Sed  ///     catches in the function.  This tables is reversed indexed base 1.
115193323Sed
116193323Sed  /// SharedTypeIds - How many leading type ids two landing pads have in common.
117193323Sed  static unsigned SharedTypeIds(const LandingPadInfo *L,
118193323Sed                                const LandingPadInfo *R);
119193323Sed
120193323Sed  /// PadLT - Order landing pads lexicographically by type id.
121193323Sed  static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R);
122193323Sed
123193323Sed  /// PadRange - Structure holding a try-range and the associated landing pad.
124193323Sed  struct PadRange {
125193323Sed    // The index of the landing pad.
126193323Sed    unsigned PadIndex;
127193323Sed    // The index of the begin and end labels in the landing pad's label lists.
128193323Sed    unsigned RangeIndex;
129193323Sed  };
130193323Sed
131205218Srdivacky  typedef DenseMap<MCSymbol *, PadRange> RangeMapType;
132193323Sed
133198090Srdivacky  /// ActionEntry - Structure describing an entry in the actions table.
134198090Srdivacky  struct ActionEntry {
135198090Srdivacky    int ValueForTypeID; // The value to write - may not be equal to the type id.
136198090Srdivacky    int NextAction;
137203954Srdivacky    unsigned Previous;
138198090Srdivacky  };
139198090Srdivacky
140193323Sed  /// CallSiteEntry - Structure describing an entry in the call-site table.
141193323Sed  struct CallSiteEntry {
142193323Sed    // The 'try-range' is BeginLabel .. EndLabel.
143205218Srdivacky    MCSymbol *BeginLabel; // zero indicates the start of the function.
144205218Srdivacky    MCSymbol *EndLabel;   // zero indicates the end of the function.
145198090Srdivacky
146193323Sed    // The landing pad starts at PadLabel.
147205218Srdivacky    MCSymbol *PadLabel;   // zero indicates that there is no landing pad.
148193323Sed    unsigned Action;
149193323Sed  };
150193323Sed
151198090Srdivacky  /// ComputeActionsTable - Compute the actions table and gather the first
152198090Srdivacky  /// action index for each landing pad site.
153198090Srdivacky  unsigned ComputeActionsTable(const SmallVectorImpl<const LandingPadInfo*>&LPs,
154198090Srdivacky                               SmallVectorImpl<ActionEntry> &Actions,
155198090Srdivacky                               SmallVectorImpl<unsigned> &FirstActions);
156198090Srdivacky
157199481Srdivacky  /// CallToNoUnwindFunction - Return `true' if this is a call to a function
158199481Srdivacky  /// marked `nounwind'. Return `false' otherwise.
159199481Srdivacky  bool CallToNoUnwindFunction(const MachineInstr *MI);
160199481Srdivacky
161198090Srdivacky  /// ComputeCallSiteTable - Compute the call-site table.  The entry for an
162198090Srdivacky  /// invoke has a try-range containing the call, a non-zero landing pad and an
163198090Srdivacky  /// appropriate action.  The entry for an ordinary call has a try-range
164198090Srdivacky  /// containing the call and zero for the landing pad and the action.  Calls
165198090Srdivacky  /// marked 'nounwind' have no entry and must not be contained in the try-range
166198090Srdivacky  /// of any entry - they form gaps in the table.  Entries must be ordered by
167198090Srdivacky  /// try-range address.
168198090Srdivacky  void ComputeCallSiteTable(SmallVectorImpl<CallSiteEntry> &CallSites,
169198090Srdivacky                            const RangeMapType &PadMap,
170198090Srdivacky                            const SmallVectorImpl<const LandingPadInfo *> &LPs,
171198090Srdivacky                            const SmallVectorImpl<unsigned> &FirstActions);
172193323Sed  void EmitExceptionTable();
173193323Sed
174193323Sedpublic:
175193323Sed  //===--------------------------------------------------------------------===//
176193323Sed  // Main entry points.
177193323Sed  //
178206274Srdivacky  DwarfException(AsmPrinter *A);
179206274Srdivacky  ~DwarfException();
180193323Sed
181193323Sed  /// EndModule - Emit all exception information that should come after the
182193323Sed  /// content.
183193323Sed  void EndModule();
184193323Sed
185193323Sed  /// BeginFunction - Gather pre-function exception information.  Assumes being
186193323Sed  /// emitted immediately after the function entry point.
187203954Srdivacky  void BeginFunction(const MachineFunction *MF);
188193323Sed
189193323Sed  /// EndFunction - Gather and emit post-function exception information.
190193323Sed  void EndFunction();
191193323Sed};
192193323Sed
193193323Sed} // End of namespace llvm
194193323Sed
195193323Sed#endif
196