blob: 15020b86c97fbacb50db6d9f6c6634c90ea7098b [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
Bill Wendlingeb907212009-05-15 01:12:28 +000017#include "llvm/ADT/DenseMap.h"
Chris Lattner6d733782010-04-05 05:28:23 +000018#include <vector>
Bill Wendlingeb907212009-05-15 01:12:28 +000019
20namespace llvm {
21
Chris Lattner6d733782010-04-05 05:28:23 +000022template <typename T> class SmallVectorImpl;
Bill Wendlingeb907212009-05-15 01:12:28 +000023struct LandingPadInfo;
24class MachineModuleInfo;
Chris Lattner6d733782010-04-05 05:28:23 +000025class MachineMove;
26class MachineInstr;
27class MachineFunction;
Chris Lattneraf76e592009-08-22 20:48:53 +000028class MCAsmInfo;
Bill Wendling7378b1b2009-11-17 01:23:53 +000029class MCExpr;
Chris Lattner6d733782010-04-05 05:28:23 +000030class MCSymbol;
Bill Wendlingeb907212009-05-15 01:12:28 +000031class Timer;
Chris Lattner6d733782010-04-05 05:28:23 +000032class Function;
33class AsmPrinter;
Bill Wendlingeb907212009-05-15 01:12:28 +000034
35//===----------------------------------------------------------------------===//
36/// DwarfException - Emits Dwarf exception handling directives.
37///
Chris Lattner84ac8b72010-04-05 00:26:50 +000038class DwarfException {
39 /// Asm - Target of Dwarf emission.
40 AsmPrinter *Asm;
41public:
42 /// MMI - Collected machine module information.
43 MachineModuleInfo *MMI;
44private:
45
Bill Wendlingeb907212009-05-15 01:12:28 +000046 struct FunctionEHFrameInfo {
Chris Lattner3a9be0e2010-01-23 05:51:36 +000047 MCSymbol *FunctionEHSym; // L_foo.eh
Bill Wendlingeb907212009-05-15 01:12:28 +000048 unsigned Number;
49 unsigned PersonalityIndex;
50 bool hasCalls;
51 bool hasLandingPads;
52 std::vector<MachineMove> Moves;
Chris Lattner7a2ba942010-01-16 18:37:32 +000053 const Function *function;
Bill Wendlingeb907212009-05-15 01:12:28 +000054
Chris Lattner3a9be0e2010-01-23 05:51:36 +000055 FunctionEHFrameInfo(MCSymbol *EHSym, unsigned Num, unsigned P,
Bill Wendlingeb907212009-05-15 01:12:28 +000056 bool hC, bool hL,
57 const std::vector<MachineMove> &M,
58 const Function *f):
Chris Lattner7a2ba942010-01-16 18:37:32 +000059 FunctionEHSym(EHSym), Number(Num), PersonalityIndex(P),
Bill Wendlingeb907212009-05-15 01:12:28 +000060 hasCalls(hC), hasLandingPads(hL), Moves(M), function (f) { }
61 };
62
63 std::vector<FunctionEHFrameInfo> EHFrames;
64
Bill Wendling52783c62009-09-09 23:56:55 +000065 /// UsesLSDA - Indicates whether an FDE that uses the CIE at the given index
66 /// uses an LSDA. If so, then we need to encode that information in the CIE's
67 /// augmentation.
68 DenseMap<unsigned, bool> UsesLSDA;
69
Bill Wendlingeb907212009-05-15 01:12:28 +000070 /// shouldEmitTable - Per-function flag to indicate if EH tables should
71 /// be emitted.
72 bool shouldEmitTable;
73
74 /// shouldEmitMoves - Per-function flag to indicate if frame moves info
75 /// should be emitted.
76 bool shouldEmitMoves;
77
78 /// shouldEmitTableModule - Per-module flag to indicate if EH tables
79 /// should be emitted.
80 bool shouldEmitTableModule;
81
82 /// shouldEmitFrameModule - Per-module flag to indicate if frame moves
83 /// should be emitted.
84 bool shouldEmitMovesModule;
85
86 /// ExceptionTimer - Timer for the Dwarf exception writer.
87 Timer *ExceptionTimer;
88
Bill Wendling7ccda0f2009-08-25 08:08:33 +000089 /// EmitCIE - Emit a Common Information Entry (CIE). This holds information
90 /// that is shared among many Frame Description Entries. There is at least
91 /// one CIE in every non-empty .debug_frame section.
92 void EmitCIE(const Function *Personality, unsigned Index);
Bill Wendlingeb907212009-05-15 01:12:28 +000093
Bill Wendling7ccda0f2009-08-25 08:08:33 +000094 /// EmitFDE - Emit the Frame Description Entry (FDE) for the function.
95 void EmitFDE(const FunctionEHFrameInfo &EHFrameInfo);
Bill Wendlingeb907212009-05-15 01:12:28 +000096
97 /// EmitExceptionTable - Emit landing pads and actions.
98 ///
99 /// The general organization of the table is complex, but the basic concepts
100 /// are easy. First there is a header which describes the location and
101 /// organization of the three components that follow.
102 /// 1. The landing pad site information describes the range of code covered
103 /// by the try. In our case it's an accumulation of the ranges covered
104 /// by the invokes in the try. There is also a reference to the landing
105 /// pad that handles the exception once processed. Finally an index into
106 /// the actions table.
107 /// 2. The action table, in our case, is composed of pairs of type ids
108 /// and next action offset. Starting with the action index from the
109 /// landing pad site, each type Id is checked for a match to the current
110 /// exception. If it matches then the exception and type id are passed
111 /// on to the landing pad. Otherwise the next action is looked up. This
112 /// chain is terminated with a next action of zero. If no type id is
Dan Gohmanf451cb82010-02-10 16:03:48 +0000113 /// found the frame is unwound and handling continues.
Bill Wendlingeb907212009-05-15 01:12:28 +0000114 /// 3. Type id table contains references to all the C++ typeinfo for all
115 /// catches in the function. This tables is reversed indexed base 1.
116
117 /// SharedTypeIds - How many leading type ids two landing pads have in common.
118 static unsigned SharedTypeIds(const LandingPadInfo *L,
119 const LandingPadInfo *R);
120
121 /// PadLT - Order landing pads lexicographically by type id.
122 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R);
123
Bill Wendlingeb907212009-05-15 01:12:28 +0000124 /// 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
Chris Lattner16112732010-03-14 01:41:15 +0000132 typedef DenseMap<MCSymbol *, PadRange> RangeMapType;
Bill Wendlingeb907212009-05-15 01:12:28 +0000133
Bill Wendling5e953dd2009-07-28 23:22:13 +0000134 /// 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;
Bill Wendling0a9abcb2010-02-10 21:41:57 +0000138 unsigned Previous;
Bill Wendling5e953dd2009-07-28 23:22:13 +0000139 };
140
Bill Wendlingeb907212009-05-15 01:12:28 +0000141 /// CallSiteEntry - Structure describing an entry in the call-site table.
142 struct CallSiteEntry {
143 // The 'try-range' is BeginLabel .. EndLabel.
Chris Lattner16112732010-03-14 01:41:15 +0000144 MCSymbol *BeginLabel; // zero indicates the start of the function.
145 MCSymbol *EndLabel; // zero indicates the end of the function.
Bill Wendling5e953dd2009-07-28 23:22:13 +0000146
Bill Wendlingeb907212009-05-15 01:12:28 +0000147 // The landing pad starts at PadLabel.
Chris Lattner16112732010-03-14 01:41:15 +0000148 MCSymbol *PadLabel; // zero indicates that there is no landing pad.
Bill Wendlingeb907212009-05-15 01:12:28 +0000149 unsigned Action;
150 };
151
Bill Wendlingd4609622009-07-28 23:23:00 +0000152 /// ComputeActionsTable - Compute the actions table and gather the first
153 /// action index for each landing pad site.
Bill Wendlingade025c2009-07-29 00:31:35 +0000154 unsigned ComputeActionsTable(const SmallVectorImpl<const LandingPadInfo*>&LPs,
Bill Wendling5e953dd2009-07-28 23:22:13 +0000155 SmallVectorImpl<ActionEntry> &Actions,
156 SmallVectorImpl<unsigned> &FirstActions);
Bill Wendlingade025c2009-07-29 00:31:35 +0000157
Bill Wendlinged060dc2009-11-12 21:59:20 +0000158 /// CallToNoUnwindFunction - Return `true' if this is a call to a function
159 /// marked `nounwind'. Return `false' otherwise.
160 bool CallToNoUnwindFunction(const MachineInstr *MI);
161
Bill Wendlingade025c2009-07-29 00:31:35 +0000162 /// ComputeCallSiteTable - Compute the call-site table. The entry for an
163 /// invoke has a try-range containing the call, a non-zero landing pad and an
164 /// appropriate action. The entry for an ordinary call has a try-range
165 /// containing the call and zero for the landing pad and the action. Calls
166 /// marked 'nounwind' have no entry and must not be contained in the try-range
167 /// of any entry - they form gaps in the table. Entries must be ordered by
168 /// try-range address.
169 void ComputeCallSiteTable(SmallVectorImpl<CallSiteEntry> &CallSites,
170 const RangeMapType &PadMap,
171 const SmallVectorImpl<const LandingPadInfo *> &LPs,
172 const SmallVectorImpl<unsigned> &FirstActions);
Bill Wendlingeb907212009-05-15 01:12:28 +0000173 void EmitExceptionTable();
174
175public:
176 //===--------------------------------------------------------------------===//
177 // Main entry points.
178 //
Chris Lattner75f50722010-04-04 07:48:20 +0000179 DwarfException(AsmPrinter *A);
Bill Wendlingeb907212009-05-15 01:12:28 +0000180 virtual ~DwarfException();
181
Bill Wendlingeb907212009-05-15 01:12:28 +0000182 /// EndModule - Emit all exception information that should come after the
183 /// content.
184 void EndModule();
185
186 /// BeginFunction - Gather pre-function exception information. Assumes being
187 /// emitted immediately after the function entry point.
Chris Lattnereec791a2010-01-26 23:18:02 +0000188 void BeginFunction(const MachineFunction *MF);
Bill Wendlingeb907212009-05-15 01:12:28 +0000189
190 /// EndFunction - Gather and emit post-function exception information.
191 void EndFunction();
192};
193
194} // End of namespace llvm
195
196#endif