blob: 8a9d56808cf1214d22d2bc8f7bc46ce315c343c1 [file] [log] [blame]
Bill Wendlingeb907212009-05-15 01:12:28 +00001//===-- 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
Chris Lattner6f37f8f2009-07-17 20:32:07 +000014#ifndef LLVM_CODEGEN_ASMPRINTER_DWARFEXCEPTION_H
15#define LLVM_CODEGEN_ASMPRINTER_DWARFEXCEPTION_H
Bill Wendlingeb907212009-05-15 01:12:28 +000016
17#include "DIE.h"
18#include "DwarfPrinter.h"
19#include "llvm/CodeGen/AsmPrinter.h"
20#include "llvm/ADT/DenseMap.h"
21#include <string>
22
23namespace llvm {
24
25struct LandingPadInfo;
26class MachineModuleInfo;
Chris Lattneraf76e592009-08-22 20:48:53 +000027class MCAsmInfo;
Bill Wendlingeb907212009-05-15 01:12:28 +000028class Timer;
29class raw_ostream;
30
31//===----------------------------------------------------------------------===//
32/// DwarfException - Emits Dwarf exception handling directives.
33///
34class 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 /// shouldEmitTable - Per-function flag to indicate if EH tables should
55 /// be emitted.
56 bool shouldEmitTable;
57
58 /// shouldEmitMoves - Per-function flag to indicate if frame moves info
59 /// should be emitted.
60 bool shouldEmitMoves;
61
62 /// shouldEmitTableModule - Per-module flag to indicate if EH tables
63 /// should be emitted.
64 bool shouldEmitTableModule;
65
66 /// shouldEmitFrameModule - Per-module flag to indicate if frame moves
67 /// should be emitted.
68 bool shouldEmitMovesModule;
69
70 /// ExceptionTimer - Timer for the Dwarf exception writer.
71 Timer *ExceptionTimer;
72
Bill Wendlinga6f91da2009-08-25 02:27:42 +000073 /// EmitCommonInformationEntry - Emit a Common Information Entry (CIE). This
74 /// holds information that is shared among many Frame Description Entries.
75 /// There is at least one CIE in every non-empty .debug_frame section.
76 void EmitCommonInformationEntry(const Function *Personality, unsigned Index);
Bill Wendlingeb907212009-05-15 01:12:28 +000077
Bill Wendlinga6f91da2009-08-25 02:27:42 +000078 /// EmitFrameDescriptionEntry - Emit the Frame Description Entry (FDE) for the
79 /// function.
80 void EmitFrameDescriptionEntry(const FunctionEHFrameInfo &EHFrameInfo);
Bill Wendlingeb907212009-05-15 01:12:28 +000081
82 /// EmitExceptionTable - Emit landing pads and actions.
83 ///
84 /// The general organization of the table is complex, but the basic concepts
85 /// are easy. First there is a header which describes the location and
86 /// organization of the three components that follow.
87 /// 1. The landing pad site information describes the range of code covered
88 /// by the try. In our case it's an accumulation of the ranges covered
89 /// by the invokes in the try. There is also a reference to the landing
90 /// pad that handles the exception once processed. Finally an index into
91 /// the actions table.
92 /// 2. The action table, in our case, is composed of pairs of type ids
93 /// and next action offset. Starting with the action index from the
94 /// landing pad site, each type Id is checked for a match to the current
95 /// exception. If it matches then the exception and type id are passed
96 /// on to the landing pad. Otherwise the next action is looked up. This
97 /// chain is terminated with a next action of zero. If no type id is
98 /// found the the frame is unwound and handling continues.
99 /// 3. Type id table contains references to all the C++ typeinfo for all
100 /// catches in the function. This tables is reversed indexed base 1.
101
102 /// SharedTypeIds - How many leading type ids two landing pads have in common.
103 static unsigned SharedTypeIds(const LandingPadInfo *L,
104 const LandingPadInfo *R);
105
106 /// PadLT - Order landing pads lexicographically by type id.
107 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R);
108
109 struct KeyInfo {
110 static inline unsigned getEmptyKey() { return -1U; }
111 static inline unsigned getTombstoneKey() { return -2U; }
112 static unsigned getHashValue(const unsigned &Key) { return Key; }
113 static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
114 static bool isPod() { return true; }
115 };
116
Bill Wendlingeb907212009-05-15 01:12:28 +0000117 /// PadRange - Structure holding a try-range and the associated landing pad.
118 struct PadRange {
119 // The index of the landing pad.
120 unsigned PadIndex;
121 // The index of the begin and end labels in the landing pad's label lists.
122 unsigned RangeIndex;
123 };
124
125 typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
126
Bill Wendling5e953dd2009-07-28 23:22:13 +0000127 /// ActionEntry - Structure describing an entry in the actions table.
128 struct ActionEntry {
129 int ValueForTypeID; // The value to write - may not be equal to the type id.
130 int NextAction;
131 struct ActionEntry *Previous;
132 };
133
Bill Wendlingeb907212009-05-15 01:12:28 +0000134 /// CallSiteEntry - Structure describing an entry in the call-site table.
135 struct CallSiteEntry {
136 // The 'try-range' is BeginLabel .. EndLabel.
137 unsigned BeginLabel; // zero indicates the start of the function.
138 unsigned EndLabel; // zero indicates the end of the function.
Bill Wendling5e953dd2009-07-28 23:22:13 +0000139
Bill Wendlingeb907212009-05-15 01:12:28 +0000140 // The landing pad starts at PadLabel.
141 unsigned PadLabel; // zero indicates that there is no landing pad.
142 unsigned Action;
143 };
144
Bill Wendlingd4609622009-07-28 23:23:00 +0000145 /// ComputeActionsTable - Compute the actions table and gather the first
146 /// action index for each landing pad site.
Bill Wendlingade025c2009-07-29 00:31:35 +0000147 unsigned ComputeActionsTable(const SmallVectorImpl<const LandingPadInfo*>&LPs,
Bill Wendling5e953dd2009-07-28 23:22:13 +0000148 SmallVectorImpl<ActionEntry> &Actions,
149 SmallVectorImpl<unsigned> &FirstActions);
Bill Wendlingade025c2009-07-29 00:31:35 +0000150
151 /// ComputeCallSiteTable - Compute the call-site table. The entry for an
152 /// invoke has a try-range containing the call, a non-zero landing pad and an
153 /// appropriate action. The entry for an ordinary call has a try-range
154 /// containing the call and zero for the landing pad and the action. Calls
155 /// marked 'nounwind' have no entry and must not be contained in the try-range
156 /// of any entry - they form gaps in the table. Entries must be ordered by
157 /// try-range address.
158 void ComputeCallSiteTable(SmallVectorImpl<CallSiteEntry> &CallSites,
159 const RangeMapType &PadMap,
160 const SmallVectorImpl<const LandingPadInfo *> &LPs,
161 const SmallVectorImpl<unsigned> &FirstActions);
Bill Wendlingeb907212009-05-15 01:12:28 +0000162 void EmitExceptionTable();
163
164public:
165 //===--------------------------------------------------------------------===//
166 // Main entry points.
167 //
Chris Lattneraf76e592009-08-22 20:48:53 +0000168 DwarfException(raw_ostream &OS, AsmPrinter *A, const MCAsmInfo *T);
Bill Wendlingeb907212009-05-15 01:12:28 +0000169 virtual ~DwarfException();
170
Bill Wendlingeb907212009-05-15 01:12:28 +0000171 /// BeginModule - Emit all exception information that should come prior to the
172 /// content.
Devang Patel208622d2009-06-25 22:36:02 +0000173 void BeginModule(Module *m, MachineModuleInfo *mmi) {
174 this->M = m;
175 this->MMI = mmi;
Bill Wendlingeb907212009-05-15 01:12:28 +0000176 }
177
178 /// EndModule - Emit all exception information that should come after the
179 /// content.
180 void EndModule();
181
182 /// BeginFunction - Gather pre-function exception information. Assumes being
183 /// emitted immediately after the function entry point.
184 void BeginFunction(MachineFunction *MF);
185
186 /// EndFunction - Gather and emit post-function exception information.
187 void EndFunction();
188};
189
190} // End of namespace llvm
191
192#endif