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