blob: 85bf581990573a0083be1347425a5588b5415235 [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 Wendling7378b1b2009-11-17 01:23:53 +000028class MCExpr;
Bill Wendlingeb907212009-05-15 01:12:28 +000029class Timer;
30class raw_ostream;
31
32//===----------------------------------------------------------------------===//
33/// DwarfException - Emits Dwarf exception handling directives.
34///
Chris Lattner066c9ac2010-01-22 22:23:57 +000035class DwarfException : public DwarfPrinter {
Bill Wendlingeb907212009-05-15 01:12:28 +000036 struct FunctionEHFrameInfo {
Chris Lattner3a9be0e2010-01-23 05:51:36 +000037 MCSymbol *FunctionEHSym; // L_foo.eh
Bill Wendlingeb907212009-05-15 01:12:28 +000038 unsigned Number;
39 unsigned PersonalityIndex;
40 bool hasCalls;
41 bool hasLandingPads;
42 std::vector<MachineMove> Moves;
Chris Lattner7a2ba942010-01-16 18:37:32 +000043 const Function *function;
Bill Wendlingeb907212009-05-15 01:12:28 +000044
Chris Lattner3a9be0e2010-01-23 05:51:36 +000045 FunctionEHFrameInfo(MCSymbol *EHSym, unsigned Num, unsigned P,
Bill Wendlingeb907212009-05-15 01:12:28 +000046 bool hC, bool hL,
47 const std::vector<MachineMove> &M,
48 const Function *f):
Chris Lattner7a2ba942010-01-16 18:37:32 +000049 FunctionEHSym(EHSym), Number(Num), PersonalityIndex(P),
Bill Wendlingeb907212009-05-15 01:12:28 +000050 hasCalls(hC), hasLandingPads(hL), Moves(M), function (f) { }
51 };
52
53 std::vector<FunctionEHFrameInfo> EHFrames;
54
Bill Wendling52783c62009-09-09 23:56:55 +000055 /// 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
57 /// augmentation.
58 DenseMap<unsigned, bool> UsesLSDA;
59
Bill Wendlingeb907212009-05-15 01:12:28 +000060 /// shouldEmitTable - Per-function flag to indicate if EH tables should
61 /// be emitted.
62 bool shouldEmitTable;
63
64 /// shouldEmitMoves - Per-function flag to indicate if frame moves info
65 /// should be emitted.
66 bool shouldEmitMoves;
67
68 /// shouldEmitTableModule - Per-module flag to indicate if EH tables
69 /// should be emitted.
70 bool shouldEmitTableModule;
71
72 /// shouldEmitFrameModule - Per-module flag to indicate if frame moves
73 /// should be emitted.
74 bool shouldEmitMovesModule;
75
76 /// ExceptionTimer - Timer for the Dwarf exception writer.
77 Timer *ExceptionTimer;
78
Bill Wendling7ccda0f2009-08-25 08:08:33 +000079 /// 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);
Bill Wendlingeb907212009-05-15 01:12:28 +000083
Bill Wendling7ccda0f2009-08-25 08:08:33 +000084 /// EmitFDE - Emit the Frame Description Entry (FDE) for the function.
85 void EmitFDE(const FunctionEHFrameInfo &EHFrameInfo);
Bill Wendlingeb907212009-05-15 01:12:28 +000086
87 /// EmitExceptionTable - Emit landing pads and actions.
88 ///
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
Dan Gohmanf451cb82010-02-10 16:03:48 +0000103 /// found the frame is unwound and handling continues.
Bill Wendlingeb907212009-05-15 01:12:28 +0000104 /// 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.
106
107 /// SharedTypeIds - How many leading type ids two landing pads have in common.
108 static unsigned SharedTypeIds(const LandingPadInfo *L,
109 const LandingPadInfo *R);
110
111 /// PadLT - Order landing pads lexicographically by type id.
112 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R);
113
Bill Wendlingeb907212009-05-15 01:12:28 +0000114 /// PadRange - Structure holding a try-range and the associated landing pad.
115 struct PadRange {
116 // The index of the landing pad.
117 unsigned PadIndex;
118 // The index of the begin and end labels in the landing pad's label lists.
119 unsigned RangeIndex;
120 };
121
Chris Lattner16112732010-03-14 01:41:15 +0000122 typedef DenseMap<MCSymbol *, PadRange> RangeMapType;
Bill Wendlingeb907212009-05-15 01:12:28 +0000123
Bill Wendling5e953dd2009-07-28 23:22:13 +0000124 /// ActionEntry - Structure describing an entry in the actions table.
125 struct ActionEntry {
126 int ValueForTypeID; // The value to write - may not be equal to the type id.
127 int NextAction;
Bill Wendling0a9abcb2010-02-10 21:41:57 +0000128 unsigned Previous;
Bill Wendling5e953dd2009-07-28 23:22:13 +0000129 };
130
Bill Wendlingeb907212009-05-15 01:12:28 +0000131 /// CallSiteEntry - Structure describing an entry in the call-site table.
132 struct CallSiteEntry {
133 // The 'try-range' is BeginLabel .. EndLabel.
Chris Lattner16112732010-03-14 01:41:15 +0000134 MCSymbol *BeginLabel; // zero indicates the start of the function.
135 MCSymbol *EndLabel; // zero indicates the end of the function.
Bill Wendling5e953dd2009-07-28 23:22:13 +0000136
Bill Wendlingeb907212009-05-15 01:12:28 +0000137 // The landing pad starts at PadLabel.
Chris Lattner16112732010-03-14 01:41:15 +0000138 MCSymbol *PadLabel; // zero indicates that there is no landing pad.
Bill Wendlingeb907212009-05-15 01:12:28 +0000139 unsigned Action;
140 };
141
Bill Wendlingd4609622009-07-28 23:23:00 +0000142 /// ComputeActionsTable - Compute the actions table and gather the first
143 /// action index for each landing pad site.
Bill Wendlingade025c2009-07-29 00:31:35 +0000144 unsigned ComputeActionsTable(const SmallVectorImpl<const LandingPadInfo*>&LPs,
Bill Wendling5e953dd2009-07-28 23:22:13 +0000145 SmallVectorImpl<ActionEntry> &Actions,
146 SmallVectorImpl<unsigned> &FirstActions);
Bill Wendlingade025c2009-07-29 00:31:35 +0000147
Bill Wendlinged060dc2009-11-12 21:59:20 +0000148 /// CallToNoUnwindFunction - Return `true' if this is a call to a function
149 /// marked `nounwind'. Return `false' otherwise.
150 bool CallToNoUnwindFunction(const MachineInstr *MI);
151
Bill Wendlingade025c2009-07-29 00:31:35 +0000152 /// ComputeCallSiteTable - Compute the call-site table. The entry for an
153 /// invoke has a try-range containing the call, a non-zero landing pad and an
154 /// appropriate action. The entry for an ordinary call has a try-range
155 /// containing the call and zero for the landing pad and the action. Calls
156 /// marked 'nounwind' have no entry and must not be contained in the try-range
157 /// of any entry - they form gaps in the table. Entries must be ordered by
158 /// try-range address.
159 void ComputeCallSiteTable(SmallVectorImpl<CallSiteEntry> &CallSites,
160 const RangeMapType &PadMap,
161 const SmallVectorImpl<const LandingPadInfo *> &LPs,
162 const SmallVectorImpl<unsigned> &FirstActions);
Bill Wendlingeb907212009-05-15 01:12:28 +0000163 void EmitExceptionTable();
164
165public:
166 //===--------------------------------------------------------------------===//
167 // Main entry points.
168 //
Chris Lattner75f50722010-04-04 07:48:20 +0000169 DwarfException(AsmPrinter *A);
Bill Wendlingeb907212009-05-15 01:12:28 +0000170 virtual ~DwarfException();
171
Bill Wendlingeb907212009-05-15 01:12:28 +0000172 /// BeginModule - Emit all exception information that should come prior to the
173 /// content.
Chris Lattner75f50722010-04-04 07:48:20 +0000174 void BeginModule(Module *m) {
Devang Patel208622d2009-06-25 22:36:02 +0000175 this->M = m;
Chris Lattner75f50722010-04-04 07:48:20 +0000176 this->MMI = Asm->MMI;
Bill Wendlingeb907212009-05-15 01:12:28 +0000177 }
178
179 /// EndModule - Emit all exception information that should come after the
180 /// content.
181 void EndModule();
182
183 /// BeginFunction - Gather pre-function exception information. Assumes being
184 /// emitted immediately after the function entry point.
Chris Lattnereec791a2010-01-26 23:18:02 +0000185 void BeginFunction(const MachineFunction *MF);
Bill Wendlingeb907212009-05-15 01:12:28 +0000186
187 /// EndFunction - Gather and emit post-function exception information.
188 void EndFunction();
189};
190
191} // End of namespace llvm
192
193#endif