DwarfException.h revision 203954
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 "DIE.h"
18193323Sed#include "DwarfPrinter.h"
19193323Sed#include "llvm/CodeGen/AsmPrinter.h"
20193323Sed#include "llvm/ADT/DenseMap.h"
21193323Sed#include <string>
22193323Sed
23193323Sednamespace llvm {
24193323Sed
25193323Sedstruct LandingPadInfo;
26193323Sedclass MachineModuleInfo;
27198090Srdivackyclass MCAsmInfo;
28199481Srdivackyclass MCExpr;
29193323Sedclass Timer;
30193323Sedclass raw_ostream;
31193323Sed
32193323Sed//===----------------------------------------------------------------------===//
33193323Sed/// DwarfException - Emits Dwarf exception handling directives.
34193323Sed///
35202878Srdivackyclass DwarfException : public DwarfPrinter {
36193323Sed  struct FunctionEHFrameInfo {
37202878Srdivacky    MCSymbol *FunctionEHSym;  // L_foo.eh
38193323Sed    unsigned Number;
39193323Sed    unsigned PersonalityIndex;
40193323Sed    bool hasCalls;
41193323Sed    bool hasLandingPads;
42193323Sed    std::vector<MachineMove> Moves;
43202878Srdivacky    const Function *function;
44193323Sed
45202878Srdivacky    FunctionEHFrameInfo(MCSymbol *EHSym, unsigned Num, unsigned P,
46193323Sed                        bool hC, bool hL,
47193323Sed                        const std::vector<MachineMove> &M,
48193323Sed                        const Function *f):
49202878Srdivacky      FunctionEHSym(EHSym), Number(Num), PersonalityIndex(P),
50193323Sed      hasCalls(hC), hasLandingPads(hL), Moves(M), function (f) { }
51193323Sed  };
52193323Sed
53193323Sed  std::vector<FunctionEHFrameInfo> EHFrames;
54193323Sed
55198090Srdivacky  /// UsesLSDA - Indicates whether an FDE that uses the CIE at the given index
56198090Srdivacky  /// uses an LSDA. If so, then we need to encode that information in the CIE's
57198090Srdivacky  /// augmentation.
58198090Srdivacky  DenseMap<unsigned, bool> UsesLSDA;
59198090Srdivacky
60193323Sed  /// shouldEmitTable - Per-function flag to indicate if EH tables should
61193323Sed  /// be emitted.
62193323Sed  bool shouldEmitTable;
63193323Sed
64193323Sed  /// shouldEmitMoves - Per-function flag to indicate if frame moves info
65193323Sed  /// should be emitted.
66193323Sed  bool shouldEmitMoves;
67193323Sed
68193323Sed  /// shouldEmitTableModule - Per-module flag to indicate if EH tables
69193323Sed  /// should be emitted.
70193323Sed  bool shouldEmitTableModule;
71193323Sed
72193323Sed  /// shouldEmitFrameModule - Per-module flag to indicate if frame moves
73193323Sed  /// should be emitted.
74193323Sed  bool shouldEmitMovesModule;
75193323Sed
76193323Sed  /// ExceptionTimer - Timer for the Dwarf exception writer.
77193323Sed  Timer *ExceptionTimer;
78193323Sed
79198090Srdivacky  /// EmitCIE - Emit a Common Information Entry (CIE). This holds information
80198090Srdivacky  /// that is shared among many Frame Description Entries.  There is at least
81198090Srdivacky  /// one CIE in every non-empty .debug_frame section.
82198090Srdivacky  void EmitCIE(const Function *Personality, unsigned Index);
83193323Sed
84198090Srdivacky  /// EmitFDE - Emit the Frame Description Entry (FDE) for the function.
85198090Srdivacky  void EmitFDE(const FunctionEHFrameInfo &EHFrameInfo);
86198090Srdivacky
87193323Sed  /// EmitExceptionTable - Emit landing pads and actions.
88193323Sed  ///
89193323Sed  /// The general organization of the table is complex, but the basic concepts
90193323Sed  /// are easy.  First there is a header which describes the location and
91193323Sed  /// organization of the three components that follow.
92193323Sed  ///  1. The landing pad site information describes the range of code covered
93193323Sed  ///     by the try.  In our case it's an accumulation of the ranges covered
94193323Sed  ///     by the invokes in the try.  There is also a reference to the landing
95193323Sed  ///     pad that handles the exception once processed.  Finally an index into
96193323Sed  ///     the actions table.
97193323Sed  ///  2. The action table, in our case, is composed of pairs of type ids
98193323Sed  ///     and next action offset.  Starting with the action index from the
99193323Sed  ///     landing pad site, each type Id is checked for a match to the current
100193323Sed  ///     exception.  If it matches then the exception and type id are passed
101193323Sed  ///     on to the landing pad.  Otherwise the next action is looked up.  This
102193323Sed  ///     chain is terminated with a next action of zero.  If no type id is
103203954Srdivacky  ///     found the frame is unwound and handling continues.
104193323Sed  ///  3. Type id table contains references to all the C++ typeinfo for all
105193323Sed  ///     catches in the function.  This tables is reversed indexed base 1.
106193323Sed
107193323Sed  /// SharedTypeIds - How many leading type ids two landing pads have in common.
108193323Sed  static unsigned SharedTypeIds(const LandingPadInfo *L,
109193323Sed                                const LandingPadInfo *R);
110193323Sed
111193323Sed  /// PadLT - Order landing pads lexicographically by type id.
112193323Sed  static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R);
113193323Sed
114193323Sed  struct KeyInfo {
115193323Sed    static inline unsigned getEmptyKey() { return -1U; }
116193323Sed    static inline unsigned getTombstoneKey() { return -2U; }
117193323Sed    static unsigned getHashValue(const unsigned &Key) { return Key; }
118193323Sed    static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
119193323Sed  };
120193323Sed
121193323Sed  /// PadRange - Structure holding a try-range and the associated landing pad.
122193323Sed  struct PadRange {
123193323Sed    // The index of the landing pad.
124193323Sed    unsigned PadIndex;
125193323Sed    // The index of the begin and end labels in the landing pad's label lists.
126193323Sed    unsigned RangeIndex;
127193323Sed  };
128193323Sed
129193323Sed  typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
130193323Sed
131198090Srdivacky  /// ActionEntry - Structure describing an entry in the actions table.
132198090Srdivacky  struct ActionEntry {
133198090Srdivacky    int ValueForTypeID; // The value to write - may not be equal to the type id.
134198090Srdivacky    int NextAction;
135203954Srdivacky    unsigned Previous;
136198090Srdivacky  };
137198090Srdivacky
138193323Sed  /// CallSiteEntry - Structure describing an entry in the call-site table.
139193323Sed  struct CallSiteEntry {
140193323Sed    // The 'try-range' is BeginLabel .. EndLabel.
141193323Sed    unsigned BeginLabel; // zero indicates the start of the function.
142193323Sed    unsigned EndLabel;   // zero indicates the end of the function.
143198090Srdivacky
144193323Sed    // The landing pad starts at PadLabel.
145193323Sed    unsigned PadLabel;   // zero indicates that there is no landing pad.
146193323Sed    unsigned Action;
147193323Sed  };
148193323Sed
149198090Srdivacky  /// ComputeActionsTable - Compute the actions table and gather the first
150198090Srdivacky  /// action index for each landing pad site.
151198090Srdivacky  unsigned ComputeActionsTable(const SmallVectorImpl<const LandingPadInfo*>&LPs,
152198090Srdivacky                               SmallVectorImpl<ActionEntry> &Actions,
153198090Srdivacky                               SmallVectorImpl<unsigned> &FirstActions);
154198090Srdivacky
155199481Srdivacky  /// CallToNoUnwindFunction - Return `true' if this is a call to a function
156199481Srdivacky  /// marked `nounwind'. Return `false' otherwise.
157199481Srdivacky  bool CallToNoUnwindFunction(const MachineInstr *MI);
158199481Srdivacky
159198090Srdivacky  /// ComputeCallSiteTable - Compute the call-site table.  The entry for an
160198090Srdivacky  /// invoke has a try-range containing the call, a non-zero landing pad and an
161198090Srdivacky  /// appropriate action.  The entry for an ordinary call has a try-range
162198090Srdivacky  /// containing the call and zero for the landing pad and the action.  Calls
163198090Srdivacky  /// marked 'nounwind' have no entry and must not be contained in the try-range
164198090Srdivacky  /// of any entry - they form gaps in the table.  Entries must be ordered by
165198090Srdivacky  /// try-range address.
166198090Srdivacky  void ComputeCallSiteTable(SmallVectorImpl<CallSiteEntry> &CallSites,
167198090Srdivacky                            const RangeMapType &PadMap,
168198090Srdivacky                            const SmallVectorImpl<const LandingPadInfo *> &LPs,
169198090Srdivacky                            const SmallVectorImpl<unsigned> &FirstActions);
170193323Sed  void EmitExceptionTable();
171193323Sed
172199481Srdivacky  /// CreateLabelDiff - Emit a label and subtract it from the expression we
173199481Srdivacky  /// already have.  This is equivalent to emitting "foo - .", but we have to
174199481Srdivacky  /// emit the label for "." directly.
175199481Srdivacky  const MCExpr *CreateLabelDiff(const MCExpr *ExprRef, const char *LabelName,
176199481Srdivacky                                unsigned Index);
177193323Sedpublic:
178193323Sed  //===--------------------------------------------------------------------===//
179193323Sed  // Main entry points.
180193323Sed  //
181198090Srdivacky  DwarfException(raw_ostream &OS, AsmPrinter *A, const MCAsmInfo *T);
182193323Sed  virtual ~DwarfException();
183193323Sed
184193323Sed  /// BeginModule - Emit all exception information that should come prior to the
185193323Sed  /// content.
186195098Sed  void BeginModule(Module *m, MachineModuleInfo *mmi) {
187195098Sed    this->M = m;
188195098Sed    this->MMI = mmi;
189193323Sed  }
190193323Sed
191193323Sed  /// EndModule - Emit all exception information that should come after the
192193323Sed  /// content.
193193323Sed  void EndModule();
194193323Sed
195193323Sed  /// BeginFunction - Gather pre-function exception information.  Assumes being
196193323Sed  /// emitted immediately after the function entry point.
197203954Srdivacky  void BeginFunction(const MachineFunction *MF);
198193323Sed
199193323Sed  /// EndFunction - Gather and emit post-function exception information.
200193323Sed  void EndFunction();
201193323Sed};
202193323Sed
203193323Sed} // End of namespace llvm
204193323Sed
205193323Sed#endif
206