1 //===-- DwarfException.h - Dwarf Exception Framework -----------*- C++ -*--===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains support for writing dwarf exception info into asm files.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CODEGEN_ASMPRINTER_DWARFEXCEPTION_H
15 #define LLVM_CODEGEN_ASMPRINTER_DWARFEXCEPTION_H
18 #include "DwarfPrinter.h"
19 #include "llvm/CodeGen/AsmPrinter.h"
20 #include "llvm/ADT/DenseMap.h"
25 struct LandingPadInfo;
26 class MachineModuleInfo;
32 //===----------------------------------------------------------------------===//
33 /// DwarfException - Emits Dwarf exception handling directives.
35 class DwarfException : public DwarfPrinter {
36 struct FunctionEHFrameInfo {
37 MCSymbol *FunctionEHSym; // L_foo.eh
39 unsigned PersonalityIndex;
42 std::vector<MachineMove> Moves;
43 const Function *function;
45 FunctionEHFrameInfo(MCSymbol *EHSym, unsigned Num, unsigned P,
47 const std::vector<MachineMove> &M,
49 FunctionEHSym(EHSym), Number(Num), PersonalityIndex(P),
50 hasCalls(hC), hasLandingPads(hL), Moves(M), function (f) { }
53 std::vector<FunctionEHFrameInfo> EHFrames;
55 /// UsesLSDA - Indicates whether an FDE that uses the CIE at the given index
56 /// uses an LSDA. If so, then we need to encode that information in the CIE's
58 DenseMap<unsigned, bool> UsesLSDA;
60 /// shouldEmitTable - Per-function flag to indicate if EH tables should
64 /// shouldEmitMoves - Per-function flag to indicate if frame moves info
65 /// should be emitted.
68 /// shouldEmitTableModule - Per-module flag to indicate if EH tables
69 /// should be emitted.
70 bool shouldEmitTableModule;
72 /// shouldEmitFrameModule - Per-module flag to indicate if frame moves
73 /// should be emitted.
74 bool shouldEmitMovesModule;
76 /// ExceptionTimer - Timer for the Dwarf exception writer.
77 Timer *ExceptionTimer;
79 /// EmitCIE - Emit a Common Information Entry (CIE). This holds information
80 /// that is shared among many Frame Description Entries. There is at least
81 /// one CIE in every non-empty .debug_frame section.
82 void EmitCIE(const Function *Personality, unsigned Index);
84 /// EmitFDE - Emit the Frame Description Entry (FDE) for the function.
85 void EmitFDE(const FunctionEHFrameInfo &EHFrameInfo);
87 /// EmitExceptionTable - Emit landing pads and actions.
89 /// The general organization of the table is complex, but the basic concepts
90 /// are easy. First there is a header which describes the location and
91 /// organization of the three components that follow.
92 /// 1. The landing pad site information describes the range of code covered
93 /// by the try. In our case it's an accumulation of the ranges covered
94 /// by the invokes in the try. There is also a reference to the landing
95 /// pad that handles the exception once processed. Finally an index into
96 /// the actions table.
97 /// 2. The action table, in our case, is composed of pairs of type ids
98 /// and next action offset. Starting with the action index from the
99 /// landing pad site, each type Id is checked for a match to the current
100 /// exception. If it matches then the exception and type id are passed
101 /// on to the landing pad. Otherwise the next action is looked up. This
102 /// chain is terminated with a next action of zero. If no type id is
103 /// found the frame is unwound and handling continues.
104 /// 3. Type id table contains references to all the C++ typeinfo for all
105 /// catches in the function. This tables is reversed indexed base 1.
107 /// SharedTypeIds - How many leading type ids two landing pads have in common.
108 static unsigned SharedTypeIds(const LandingPadInfo *L,
109 const LandingPadInfo *R);
111 /// PadLT - Order landing pads lexicographically by type id.
112 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R);
115 static inline unsigned getEmptyKey() { return -1U; }
116 static inline unsigned getTombstoneKey() { return -2U; }
117 static unsigned getHashValue(const unsigned &Key) { return Key; }
118 static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
121 /// PadRange - Structure holding a try-range and the associated landing pad.
123 // The index of the landing pad.
125 // The index of the begin and end labels in the landing pad's label lists.
129 typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
131 /// ActionEntry - Structure describing an entry in the actions table.
133 int ValueForTypeID; // The value to write - may not be equal to the type id.
138 /// CallSiteEntry - Structure describing an entry in the call-site table.
139 struct CallSiteEntry {
140 // The 'try-range' is BeginLabel .. EndLabel.
141 unsigned BeginLabel; // zero indicates the start of the function.
142 unsigned EndLabel; // zero indicates the end of the function.
144 // The landing pad starts at PadLabel.
145 unsigned PadLabel; // zero indicates that there is no landing pad.
149 /// ComputeActionsTable - Compute the actions table and gather the first
150 /// action index for each landing pad site.
151 unsigned ComputeActionsTable(const SmallVectorImpl<const LandingPadInfo*>&LPs,
152 SmallVectorImpl<ActionEntry> &Actions,
153 SmallVectorImpl<unsigned> &FirstActions);
155 /// CallToNoUnwindFunction - Return `true' if this is a call to a function
156 /// marked `nounwind'. Return `false' otherwise.
157 bool CallToNoUnwindFunction(const MachineInstr *MI);
159 /// ComputeCallSiteTable - Compute the call-site table. The entry for an
160 /// invoke has a try-range containing the call, a non-zero landing pad and an
161 /// appropriate action. The entry for an ordinary call has a try-range
162 /// containing the call and zero for the landing pad and the action. Calls
163 /// marked 'nounwind' have no entry and must not be contained in the try-range
164 /// of any entry - they form gaps in the table. Entries must be ordered by
165 /// try-range address.
166 void ComputeCallSiteTable(SmallVectorImpl<CallSiteEntry> &CallSites,
167 const RangeMapType &PadMap,
168 const SmallVectorImpl<const LandingPadInfo *> &LPs,
169 const SmallVectorImpl<unsigned> &FirstActions);
170 void EmitExceptionTable();
172 /// CreateLabelDiff - Emit a label and subtract it from the expression we
173 /// already have. This is equivalent to emitting "foo - .", but we have to
174 /// emit the label for "." directly.
175 const MCExpr *CreateLabelDiff(const MCExpr *ExprRef, const char *LabelName,
178 //===--------------------------------------------------------------------===//
179 // Main entry points.
181 DwarfException(raw_ostream &OS, AsmPrinter *A, const MCAsmInfo *T);
182 virtual ~DwarfException();
184 /// BeginModule - Emit all exception information that should come prior to the
186 void BeginModule(Module *m, MachineModuleInfo *mmi) {
191 /// EndModule - Emit all exception information that should come after the
195 /// BeginFunction - Gather pre-function exception information. Assumes being
196 /// emitted immediately after the function entry point.
197 void BeginFunction(const MachineFunction *MF);
199 /// EndFunction - Gather and emit post-function exception information.
203 } // End of namespace llvm