Some first rudimentary support for ARM EHABI: print exception table in "text mode".
[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 "llvm/ADT/DenseMap.h"
18 #include <vector>
19
20 namespace llvm {
21
22 template <typename T> class SmallVectorImpl;
23 struct LandingPadInfo;
24 class MachineModuleInfo;
25 class MachineMove;
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   /// EmitExceptionTable - Emit landing pads and actions.
46   ///
47   /// The general organization of the table is complex, but the basic concepts
48   /// are easy.  First there is a header which describes the location and
49   /// organization of the three components that follow.
50   ///  1. The landing pad site information describes the range of code covered
51   ///     by the try.  In our case it's an accumulation of the ranges covered
52   ///     by the invokes in the try.  There is also a reference to the landing
53   ///     pad that handles the exception once processed.  Finally an index into
54   ///     the actions table.
55   ///  2. The action table, in our case, is composed of pairs of type ids
56   ///     and next action offset.  Starting with the action index from the
57   ///     landing pad site, each type Id is checked for a match to the current
58   ///     exception.  If it matches then the exception and type id are passed
59   ///     on to the landing pad.  Otherwise the next action is looked up.  This
60   ///     chain is terminated with a next action of zero.  If no type id is
61   ///     found the frame is unwound and handling continues.
62   ///  3. Type id table contains references to all the C++ typeinfo for all
63   ///     catches in the function.  This tables is reversed indexed base 1.
64
65   /// SharedTypeIds - How many leading type ids two landing pads have in common.
66   static unsigned SharedTypeIds(const LandingPadInfo *L,
67                                 const LandingPadInfo *R);
68
69   /// PadLT - Order landing pads lexicographically by type id.
70   static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R);
71
72   /// PadRange - Structure holding a try-range and the associated landing pad.
73   struct PadRange {
74     // The index of the landing pad.
75     unsigned PadIndex;
76     // The index of the begin and end labels in the landing pad's label lists.
77     unsigned RangeIndex;
78   };
79
80   typedef DenseMap<MCSymbol *, PadRange> RangeMapType;
81
82   /// ActionEntry - Structure describing an entry in the actions table.
83   struct ActionEntry {
84     int ValueForTypeID; // The value to write - may not be equal to the type id.
85     int NextAction;
86     unsigned Previous;
87   };
88
89   /// CallSiteEntry - Structure describing an entry in the call-site table.
90   struct CallSiteEntry {
91     // The 'try-range' is BeginLabel .. EndLabel.
92     MCSymbol *BeginLabel; // zero indicates the start of the function.
93     MCSymbol *EndLabel;   // zero indicates the end of the function.
94
95     // The landing pad starts at PadLabel.
96     MCSymbol *PadLabel;   // zero indicates that there is no landing pad.
97     unsigned Action;
98   };
99
100   /// ComputeActionsTable - Compute the actions table and gather the first
101   /// action index for each landing pad site.
102   unsigned ComputeActionsTable(const SmallVectorImpl<const LandingPadInfo*>&LPs,
103                                SmallVectorImpl<ActionEntry> &Actions,
104                                SmallVectorImpl<unsigned> &FirstActions);
105
106   /// CallToNoUnwindFunction - Return `true' if this is a call to a function
107   /// marked `nounwind'. Return `false' otherwise.
108   bool CallToNoUnwindFunction(const MachineInstr *MI);
109
110   /// ComputeCallSiteTable - Compute the call-site table.  The entry for an
111   /// invoke has a try-range containing the call, a non-zero landing pad and an
112   /// appropriate action.  The entry for an ordinary call has a try-range
113   /// containing the call and zero for the landing pad and the action.  Calls
114   /// marked 'nounwind' have no entry and must not be contained in the try-range
115   /// of any entry - they form gaps in the table.  Entries must be ordered by
116   /// try-range address.
117   void ComputeCallSiteTable(SmallVectorImpl<CallSiteEntry> &CallSites,
118                             const RangeMapType &PadMap,
119                             const SmallVectorImpl<const LandingPadInfo *> &LPs,
120                             const SmallVectorImpl<unsigned> &FirstActions);
121   void EmitExceptionTable();
122
123 public:
124   //===--------------------------------------------------------------------===//
125   // Main entry points.
126   //
127   DwarfException(AsmPrinter *A);
128   virtual ~DwarfException();
129
130   /// EndModule - Emit all exception information that should come after the
131   /// content.
132   virtual void EndModule();
133
134   /// BeginFunction - Gather pre-function exception information.  Assumes being
135   /// emitted immediately after the function entry point.
136   virtual void BeginFunction(const MachineFunction *MF);
137
138   /// EndFunction - Gather and emit post-function exception information.
139   virtual void EndFunction();
140 };
141
142 class DwarfCFIException : public DwarfException {
143   /// shouldEmitTable - Per-function flag to indicate if EH tables should
144   /// be emitted.
145   bool shouldEmitTable;
146
147   /// shouldEmitMoves - Per-function flag to indicate if frame moves info
148   /// should be emitted.
149   bool shouldEmitMoves;
150
151   /// shouldEmitTableModule - Per-module flag to indicate if EH tables
152   /// should be emitted.
153   bool shouldEmitTableModule;
154 public:
155   //===--------------------------------------------------------------------===//
156   // Main entry points.
157   //
158   DwarfCFIException(AsmPrinter *A);
159   virtual ~DwarfCFIException();
160
161   /// EndModule - Emit all exception information that should come after the
162   /// content.
163   virtual void EndModule();
164
165   /// BeginFunction - Gather pre-function exception information.  Assumes being
166   /// emitted immediately after the function entry point.
167   virtual void BeginFunction(const MachineFunction *MF);
168
169   /// EndFunction - Gather and emit post-function exception information.
170   virtual void EndFunction();
171 };
172
173 class DwarfTableException : public DwarfException {
174   /// shouldEmitTable - Per-function flag to indicate if EH tables should
175   /// be emitted.
176   bool shouldEmitTable;
177
178   /// shouldEmitMoves - Per-function flag to indicate if frame moves info
179   /// should be emitted.
180   bool shouldEmitMoves;
181
182   /// shouldEmitTableModule - Per-module flag to indicate if EH tables
183   /// should be emitted.
184   bool shouldEmitTableModule;
185
186   /// shouldEmitMovesModule - Per-module flag to indicate if frame moves
187   /// should be emitted.
188   bool shouldEmitMovesModule;
189
190   struct FunctionEHFrameInfo {
191     MCSymbol *FunctionEHSym;  // L_foo.eh
192     unsigned Number;
193     unsigned PersonalityIndex;
194     bool adjustsStack;
195     bool hasLandingPads;
196     std::vector<MachineMove> Moves;
197     const Function *function;
198
199     FunctionEHFrameInfo(MCSymbol *EHSym, unsigned Num, unsigned P,
200                         bool hC, bool hL,
201                         const std::vector<MachineMove> &M,
202                         const Function *f):
203       FunctionEHSym(EHSym), Number(Num), PersonalityIndex(P),
204       adjustsStack(hC), hasLandingPads(hL), Moves(M), function (f) { }
205   };
206
207   std::vector<FunctionEHFrameInfo> EHFrames;
208
209   /// UsesLSDA - Indicates whether an FDE that uses the CIE at the given index
210   /// uses an LSDA. If so, then we need to encode that information in the CIE's
211   /// augmentation.
212   DenseMap<unsigned, bool> UsesLSDA;
213
214   /// EmitCIE - Emit a Common Information Entry (CIE). This holds information
215   /// that is shared among many Frame Description Entries.  There is at least
216   /// one CIE in every non-empty .debug_frame section.
217   void EmitCIE(const Function *Personality, unsigned Index);
218
219   /// EmitFDE - Emit the Frame Description Entry (FDE) for the function.
220   void EmitFDE(const FunctionEHFrameInfo &EHFrameInfo);
221 public:
222   //===--------------------------------------------------------------------===//
223   // Main entry points.
224   //
225   DwarfTableException(AsmPrinter *A);
226   virtual ~DwarfTableException();
227
228   /// EndModule - Emit all exception information that should come after the
229   /// content.
230   virtual void EndModule();
231
232   /// BeginFunction - Gather pre-function exception information.  Assumes being
233   /// emitted immediately after the function entry point.
234   virtual void BeginFunction(const MachineFunction *MF);
235
236   /// EndFunction - Gather and emit post-function exception information.
237   virtual void EndFunction();
238 };
239
240
241 class ARMException : public DwarfException {
242   /// shouldEmitTable - Per-function flag to indicate if EH tables should
243   /// be emitted.
244   bool shouldEmitTable;
245
246   /// shouldEmitMoves - Per-function flag to indicate if frame moves info
247   /// should be emitted.
248   bool shouldEmitMoves;
249
250   /// shouldEmitTableModule - Per-module flag to indicate if EH tables
251   /// should be emitted.
252   bool shouldEmitTableModule;
253 public:
254   //===--------------------------------------------------------------------===//
255   // Main entry points.
256   //
257   ARMException(AsmPrinter *A);
258   virtual ~ARMException();
259
260   /// EndModule - Emit all exception information that should come after the
261   /// content.
262   virtual void EndModule();
263
264   /// BeginFunction - Gather pre-function exception information.  Assumes being
265   /// emitted immediately after the function entry point.
266   virtual void BeginFunction(const MachineFunction *MF);
267
268   /// EndFunction - Gather and emit post-function exception information.
269   virtual void EndFunction();
270 };
271
272 } // End of namespace llvm
273
274 #endif