blob: 113a9e4cd4c62c717f8098780f829a6aa7d3fe5e [file] [log] [blame]
Bill Wendlingeb907212009-05-15 01:12:28 +00001//===-- CodeGen/AsmPrinter/DwarfException.cpp - Dwarf Exception Impl ------===//
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//
Bill Wendling28275fd2009-09-10 06:50:01 +000010// This file contains support for writing DWARF exception info into asm files.
Bill Wendlingeb907212009-05-15 01:12:28 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "DwarfException.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000015#include "llvm/ADT/SmallString.h"
16#include "llvm/ADT/StringExtras.h"
17#include "llvm/ADT/Twine.h"
Chris Lattner6d733782010-04-05 05:28:23 +000018#include "llvm/CodeGen/AsmPrinter.h"
Bill Wendlingeb907212009-05-15 01:12:28 +000019#include "llvm/CodeGen/MachineFrameInfo.h"
David Greenefc4da0c2009-08-19 21:55:33 +000020#include "llvm/CodeGen/MachineFunction.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000021#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000022#include "llvm/IR/DataLayout.h"
Stephen Hines36b56882014-04-23 16:57:46 -070023#include "llvm/IR/Mangler.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000024#include "llvm/IR/Module.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000025#include "llvm/MC/MCAsmInfo.h"
26#include "llvm/MC/MCContext.h"
27#include "llvm/MC/MCExpr.h"
Bill Wendling43e484f2009-09-10 01:12:47 +000028#include "llvm/MC/MCSection.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000029#include "llvm/MC/MCStreamer.h"
Chris Lattner7a2ba942010-01-16 18:37:32 +000030#include "llvm/MC/MCSymbol.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000031#include "llvm/Support/Dwarf.h"
32#include "llvm/Support/ErrorHandling.h"
33#include "llvm/Support/FormattedStream.h"
Stephen Hines36b56882014-04-23 16:57:46 -070034#include "llvm/Support/LEB128.h"
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000035#include "llvm/Target/TargetFrameLowering.h"
Chris Lattnerd5bbb072009-08-02 01:34:32 +000036#include "llvm/Target/TargetLoweringObjectFile.h"
Bill Wendlingeb907212009-05-15 01:12:28 +000037#include "llvm/Target/TargetOptions.h"
Chris Lattnerd5bbb072009-08-02 01:34:32 +000038#include "llvm/Target/TargetRegisterInfo.h"
Bill Wendlingeb907212009-05-15 01:12:28 +000039using namespace llvm;
40
Chris Lattner75f50722010-04-04 07:48:20 +000041DwarfException::DwarfException(AsmPrinter *A)
Anton Korobeynikovd7e8ddc2011-01-14 21:57:45 +000042 : Asm(A), MMI(Asm->MMI) {}
Bill Wendlingbc0d23a2009-05-15 01:18:50 +000043
Bill Wendling5f017e82010-04-07 09:28:04 +000044DwarfException::~DwarfException() {}
Bill Wendlingbc0d23a2009-05-15 01:18:50 +000045
Bill Wendlingeb907212009-05-15 01:12:28 +000046/// SharedTypeIds - How many leading type ids two landing pads have in common.
47unsigned DwarfException::SharedTypeIds(const LandingPadInfo *L,
48 const LandingPadInfo *R) {
49 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
50 unsigned LSize = LIds.size(), RSize = RIds.size();
51 unsigned MinSize = LSize < RSize ? LSize : RSize;
52 unsigned Count = 0;
53
54 for (; Count != MinSize; ++Count)
55 if (LIds[Count] != RIds[Count])
56 return Count;
57
58 return Count;
59}
60
Bill Wendlingd4609622009-07-28 23:23:00 +000061/// ComputeActionsTable - Compute the actions table and gather the first action
62/// index for each landing pad site.
Bill Wendlingade025c2009-07-29 00:31:35 +000063unsigned DwarfException::
64ComputeActionsTable(const SmallVectorImpl<const LandingPadInfo*> &LandingPads,
65 SmallVectorImpl<ActionEntry> &Actions,
66 SmallVectorImpl<unsigned> &FirstActions) {
Bill Wendlinga583c552009-08-20 22:02:24 +000067
68 // The action table follows the call-site table in the LSDA. The individual
69 // records are of two types:
70 //
71 // * Catch clause
72 // * Exception specification
73 //
74 // The two record kinds have the same format, with only small differences.
75 // They are distinguished by the "switch value" field: Catch clauses
76 // (TypeInfos) have strictly positive switch values, and exception
77 // specifications (FilterIds) have strictly negative switch values. Value 0
78 // indicates a catch-all clause.
79 //
Bill Wendling5e953dd2009-07-28 23:22:13 +000080 // Negative type IDs index into FilterIds. Positive type IDs index into
81 // TypeInfos. The value written for a positive type ID is just the type ID
82 // itself. For a negative type ID, however, the value written is the
Bill Wendlingeb907212009-05-15 01:12:28 +000083 // (negative) byte offset of the corresponding FilterIds entry. The byte
Bill Wendling5e953dd2009-07-28 23:22:13 +000084 // offset is usually equal to the type ID (because the FilterIds entries are
85 // written using a variable width encoding, which outputs one byte per entry
86 // as long as the value written is not too large) but can differ. This kind
87 // of complication does not occur for positive type IDs because type infos are
Bill Wendlingeb907212009-05-15 01:12:28 +000088 // output using a fixed width encoding. FilterOffsets[i] holds the byte
89 // offset corresponding to FilterIds[i].
Bill Wendling409914b2009-07-29 21:19:44 +000090
91 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
Bill Wendlingeb907212009-05-15 01:12:28 +000092 SmallVector<int, 16> FilterOffsets;
93 FilterOffsets.reserve(FilterIds.size());
94 int Offset = -1;
Bill Wendling409914b2009-07-29 21:19:44 +000095
96 for (std::vector<unsigned>::const_iterator
97 I = FilterIds.begin(), E = FilterIds.end(); I != E; ++I) {
Bill Wendlingeb907212009-05-15 01:12:28 +000098 FilterOffsets.push_back(Offset);
Stephen Hines36b56882014-04-23 16:57:46 -070099 Offset -= getULEB128Size(*I);
Bill Wendlingeb907212009-05-15 01:12:28 +0000100 }
101
Bill Wendlingeb907212009-05-15 01:12:28 +0000102 FirstActions.reserve(LandingPads.size());
103
104 int FirstAction = 0;
105 unsigned SizeActions = 0;
Bill Wendling5e953dd2009-07-28 23:22:13 +0000106 const LandingPadInfo *PrevLPI = 0;
Bill Wendling409914b2009-07-29 21:19:44 +0000107
Bill Wendling5cff4872009-07-28 23:44:43 +0000108 for (SmallVectorImpl<const LandingPadInfo *>::const_iterator
Bill Wendling5e953dd2009-07-28 23:22:13 +0000109 I = LandingPads.begin(), E = LandingPads.end(); I != E; ++I) {
110 const LandingPadInfo *LPI = *I;
111 const std::vector<int> &TypeIds = LPI->TypeIds;
Chris Lattner9be49132010-04-04 20:10:41 +0000112 unsigned NumShared = PrevLPI ? SharedTypeIds(LPI, PrevLPI) : 0;
Bill Wendlingeb907212009-05-15 01:12:28 +0000113 unsigned SizeSiteActions = 0;
114
115 if (NumShared < TypeIds.size()) {
116 unsigned SizeAction = 0;
Bill Wendling0a9abcb2010-02-10 21:41:57 +0000117 unsigned PrevAction = (unsigned)-1;
Bill Wendlingeb907212009-05-15 01:12:28 +0000118
119 if (NumShared) {
Chris Lattner9be49132010-04-04 20:10:41 +0000120 unsigned SizePrevIds = PrevLPI->TypeIds.size();
Bill Wendlingeb907212009-05-15 01:12:28 +0000121 assert(Actions.size());
Bill Wendling0a9abcb2010-02-10 21:41:57 +0000122 PrevAction = Actions.size() - 1;
Stephen Hines36b56882014-04-23 16:57:46 -0700123 SizeAction = getSLEB128Size(Actions[PrevAction].NextAction) +
124 getSLEB128Size(Actions[PrevAction].ValueForTypeID);
Bill Wendlingeb907212009-05-15 01:12:28 +0000125
126 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
Bill Wendling0a9abcb2010-02-10 21:41:57 +0000127 assert(PrevAction != (unsigned)-1 && "PrevAction is invalid!");
Stephen Hines36b56882014-04-23 16:57:46 -0700128 SizeAction -= getSLEB128Size(Actions[PrevAction].ValueForTypeID);
Bill Wendling0a9abcb2010-02-10 21:41:57 +0000129 SizeAction += -Actions[PrevAction].NextAction;
130 PrevAction = Actions[PrevAction].Previous;
Bill Wendlingeb907212009-05-15 01:12:28 +0000131 }
132 }
133
134 // Compute the actions.
Bill Wendling5e953dd2009-07-28 23:22:13 +0000135 for (unsigned J = NumShared, M = TypeIds.size(); J != M; ++J) {
136 int TypeID = TypeIds[J];
137 assert(-1 - TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
Bill Wendlingeb907212009-05-15 01:12:28 +0000138 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
Stephen Hines36b56882014-04-23 16:57:46 -0700139 unsigned SizeTypeID = getSLEB128Size(ValueForTypeID);
Bill Wendlingeb907212009-05-15 01:12:28 +0000140
141 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
Stephen Hines36b56882014-04-23 16:57:46 -0700142 SizeAction = SizeTypeID + getSLEB128Size(NextAction);
Bill Wendlingeb907212009-05-15 01:12:28 +0000143 SizeSiteActions += SizeAction;
144
Bill Wendlinga583c552009-08-20 22:02:24 +0000145 ActionEntry Action = { ValueForTypeID, NextAction, PrevAction };
Bill Wendlingeb907212009-05-15 01:12:28 +0000146 Actions.push_back(Action);
Bill Wendling0a9abcb2010-02-10 21:41:57 +0000147 PrevAction = Actions.size() - 1;
Bill Wendlingeb907212009-05-15 01:12:28 +0000148 }
149
150 // Record the first action of the landing pad site.
151 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
152 } // else identical - re-use previous FirstAction
153
Bill Wendlinga583c552009-08-20 22:02:24 +0000154 // Information used when created the call-site table. The action record
155 // field of the call site record is the offset of the first associated
156 // action record, relative to the start of the actions table. This value is
Bill Wendling0a9abcb2010-02-10 21:41:57 +0000157 // biased by 1 (1 indicating the start of the actions table), and 0
Bill Wendlinga583c552009-08-20 22:02:24 +0000158 // indicates that there are no actions.
Bill Wendlingeb907212009-05-15 01:12:28 +0000159 FirstActions.push_back(FirstAction);
160
161 // Compute this sites contribution to size.
162 SizeActions += SizeSiteActions;
Bill Wendling5e953dd2009-07-28 23:22:13 +0000163
164 PrevLPI = LPI;
Bill Wendlingeb907212009-05-15 01:12:28 +0000165 }
166
Bill Wendling5e953dd2009-07-28 23:22:13 +0000167 return SizeActions;
168}
169
Bill Wendlinged060dc2009-11-12 21:59:20 +0000170/// CallToNoUnwindFunction - Return `true' if this is a call to a function
171/// marked `nounwind'. Return `false' otherwise.
172bool DwarfException::CallToNoUnwindFunction(const MachineInstr *MI) {
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000173 assert(MI->isCall() && "This should be a call instruction!");
Bill Wendlinged060dc2009-11-12 21:59:20 +0000174
175 bool MarkedNoUnwind = false;
176 bool SawFunc = false;
177
178 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
179 const MachineOperand &MO = MI->getOperand(I);
180
Chris Lattnercfd31882010-03-31 06:09:04 +0000181 if (!MO.isGlobal()) continue;
Anton Korobeynikovd7e8ddc2011-01-14 21:57:45 +0000182
Dan Gohman46510a72010-04-15 01:51:59 +0000183 const Function *F = dyn_cast<Function>(MO.getGlobal());
Chris Lattnercfd31882010-03-31 06:09:04 +0000184 if (F == 0) continue;
Bill Wendlingecc260e2009-11-12 23:13:08 +0000185
Chris Lattnercfd31882010-03-31 06:09:04 +0000186 if (SawFunc) {
187 // Be conservative. If we have more than one function operand for this
188 // call, then we can't make the assumption that it's the callee and
189 // not a parameter to the call.
Anton Korobeynikovd7e8ddc2011-01-14 21:57:45 +0000190 //
Chris Lattnercfd31882010-03-31 06:09:04 +0000191 // FIXME: Determine if there's a way to say that `F' is the callee or
192 // parameter.
193 MarkedNoUnwind = false;
194 break;
Bill Wendlinged060dc2009-11-12 21:59:20 +0000195 }
Chris Lattnercfd31882010-03-31 06:09:04 +0000196
197 MarkedNoUnwind = F->doesNotThrow();
198 SawFunc = true;
Bill Wendlinged060dc2009-11-12 21:59:20 +0000199 }
200
201 return MarkedNoUnwind;
202}
203
Bill Wendlingade025c2009-07-29 00:31:35 +0000204/// ComputeCallSiteTable - Compute the call-site table. The entry for an invoke
Bill Wendlinga583c552009-08-20 22:02:24 +0000205/// has a try-range containing the call, a non-zero landing pad, and an
Bill Wendlingade025c2009-07-29 00:31:35 +0000206/// appropriate action. The entry for an ordinary call has a try-range
207/// containing the call and zero for the landing pad and the action. Calls
208/// marked 'nounwind' have no entry and must not be contained in the try-range
209/// of any entry - they form gaps in the table. Entries must be ordered by
210/// try-range address.
211void DwarfException::
212ComputeCallSiteTable(SmallVectorImpl<CallSiteEntry> &CallSites,
213 const RangeMapType &PadMap,
214 const SmallVectorImpl<const LandingPadInfo *> &LandingPads,
215 const SmallVectorImpl<unsigned> &FirstActions) {
Bill Wendlingeb907212009-05-15 01:12:28 +0000216 // The end label of the previous invoke or nounwind try-range.
Chris Lattner16112732010-03-14 01:41:15 +0000217 MCSymbol *LastLabel = 0;
Bill Wendlingeb907212009-05-15 01:12:28 +0000218
219 // Whether there is a potentially throwing instruction (currently this means
220 // an ordinary call) between the end of the previous try-range and now.
221 bool SawPotentiallyThrowing = false;
222
Bill Wendling5cff4872009-07-28 23:44:43 +0000223 // Whether the last CallSite entry was for an invoke.
Bill Wendlingeb907212009-05-15 01:12:28 +0000224 bool PreviousIsInvoke = false;
225
226 // Visit all instructions in order of address.
Chris Lattner84ac8b72010-04-05 00:26:50 +0000227 for (MachineFunction::const_iterator I = Asm->MF->begin(), E = Asm->MF->end();
Bill Wendlingeb907212009-05-15 01:12:28 +0000228 I != E; ++I) {
229 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
230 MI != E; ++MI) {
Stephen Hines36b56882014-04-23 16:57:46 -0700231 if (!MI->isEHLabel()) {
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000232 if (MI->isCall())
Bill Wendlinged060dc2009-11-12 21:59:20 +0000233 SawPotentiallyThrowing |= !CallToNoUnwindFunction(MI);
Bill Wendlingeb907212009-05-15 01:12:28 +0000234 continue;
235 }
236
Bill Wendlingeb907212009-05-15 01:12:28 +0000237 // End of the previous try-range?
Chris Lattner0397ada2010-03-14 08:17:53 +0000238 MCSymbol *BeginLabel = MI->getOperand(0).getMCSymbol();
Bill Wendlingeb907212009-05-15 01:12:28 +0000239 if (BeginLabel == LastLabel)
240 SawPotentiallyThrowing = false;
241
242 // Beginning of a new try-range?
Jeffrey Yasskin81cf4322009-11-10 01:02:17 +0000243 RangeMapType::const_iterator L = PadMap.find(BeginLabel);
Bill Wendlingeb907212009-05-15 01:12:28 +0000244 if (L == PadMap.end())
245 // Nope, it was just some random label.
246 continue;
247
Bill Wendlinga583c552009-08-20 22:02:24 +0000248 const PadRange &P = L->second;
Bill Wendlingeb907212009-05-15 01:12:28 +0000249 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
Bill Wendlingeb907212009-05-15 01:12:28 +0000250 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
251 "Inconsistent landing pad map!");
252
Bill Wendlinga583c552009-08-20 22:02:24 +0000253 // For Dwarf exception handling (SjLj handling doesn't use this). If some
254 // instruction between the previous try-range and this one may throw,
255 // create a call-site entry with no landing pad for the region between the
256 // try-ranges.
Anton Korobeynikov3965b5e2011-01-14 21:58:08 +0000257 if (SawPotentiallyThrowing && Asm->MAI->isExceptionHandlingDwarf()) {
Bill Wendlinga583c552009-08-20 22:02:24 +0000258 CallSiteEntry Site = { LastLabel, BeginLabel, 0, 0 };
Bill Wendlingeb907212009-05-15 01:12:28 +0000259 CallSites.push_back(Site);
260 PreviousIsInvoke = false;
261 }
262
263 LastLabel = LandingPad->EndLabels[P.RangeIndex];
264 assert(BeginLabel && LastLabel && "Invalid landing pad!");
265
Chris Lattnercfd31882010-03-31 06:09:04 +0000266 if (!LandingPad->LandingPadLabel) {
267 // Create a gap.
268 PreviousIsInvoke = false;
269 } else {
Bill Wendlingeb907212009-05-15 01:12:28 +0000270 // This try-range is for an invoke.
Bill Wendlinga583c552009-08-20 22:02:24 +0000271 CallSiteEntry Site = {
272 BeginLabel,
273 LastLabel,
274 LandingPad->LandingPadLabel,
275 FirstActions[P.PadIndex]
276 };
Bill Wendlingeb907212009-05-15 01:12:28 +0000277
Jim Grosbach33668c02009-09-01 17:19:13 +0000278 // Try to merge with the previous call-site. SJLJ doesn't do this
Anton Korobeynikov3965b5e2011-01-14 21:58:08 +0000279 if (PreviousIsInvoke && Asm->MAI->isExceptionHandlingDwarf()) {
Bill Wendlingeb907212009-05-15 01:12:28 +0000280 CallSiteEntry &Prev = CallSites.back();
281 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
282 // Extend the range of the previous entry.
283 Prev.EndLabel = Site.EndLabel;
284 continue;
285 }
286 }
287
288 // Otherwise, create a new call-site.
Anton Korobeynikov3965b5e2011-01-14 21:58:08 +0000289 if (Asm->MAI->isExceptionHandlingDwarf())
Jim Grosbachca752c92010-01-28 01:45:32 +0000290 CallSites.push_back(Site);
291 else {
292 // SjLj EH must maintain the call sites in the order assigned
293 // to them by the SjLjPrepare pass.
294 unsigned SiteNo = MMI->getCallSiteBeginLabel(BeginLabel);
295 if (CallSites.size() < SiteNo)
296 CallSites.resize(SiteNo);
297 CallSites[SiteNo - 1] = Site;
298 }
Bill Wendlingeb907212009-05-15 01:12:28 +0000299 PreviousIsInvoke = true;
Bill Wendlingeb907212009-05-15 01:12:28 +0000300 }
301 }
302 }
303
304 // If some instruction between the previous try-range and the end of the
305 // function may throw, create a call-site entry with no landing pad for the
306 // region following the try-range.
Anton Korobeynikov3965b5e2011-01-14 21:58:08 +0000307 if (SawPotentiallyThrowing && Asm->MAI->isExceptionHandlingDwarf()) {
Bill Wendlinga583c552009-08-20 22:02:24 +0000308 CallSiteEntry Site = { LastLabel, 0, 0, 0 };
Bill Wendlingeb907212009-05-15 01:12:28 +0000309 CallSites.push_back(Site);
310 }
Bill Wendlingade025c2009-07-29 00:31:35 +0000311}
312
Bill Wendling0dafca92009-07-29 00:50:05 +0000313/// EmitExceptionTable - Emit landing pads and actions.
314///
315/// The general organization of the table is complex, but the basic concepts are
316/// easy. First there is a header which describes the location and organization
317/// of the three components that follow.
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000318///
Bill Wendling0dafca92009-07-29 00:50:05 +0000319/// 1. The landing pad site information describes the range of code covered by
320/// the try. In our case it's an accumulation of the ranges covered by the
321/// invokes in the try. There is also a reference to the landing pad that
322/// handles the exception once processed. Finally an index into the actions
323/// table.
Bill Wendlinga583c552009-08-20 22:02:24 +0000324/// 2. The action table, in our case, is composed of pairs of type IDs and next
Bill Wendling0dafca92009-07-29 00:50:05 +0000325/// action offset. Starting with the action index from the landing pad
Bill Wendlinga583c552009-08-20 22:02:24 +0000326/// site, each type ID is checked for a match to the current exception. If
Bill Wendling0dafca92009-07-29 00:50:05 +0000327/// it matches then the exception and type id are passed on to the landing
328/// pad. Otherwise the next action is looked up. This chain is terminated
Bill Wendling28275fd2009-09-10 06:50:01 +0000329/// with a next action of zero. If no type id is found then the frame is
Bill Wendling0dafca92009-07-29 00:50:05 +0000330/// unwound and handling continues.
Bill Wendlinga583c552009-08-20 22:02:24 +0000331/// 3. Type ID table contains references to all the C++ typeinfo for all
Bill Wendling28275fd2009-09-10 06:50:01 +0000332/// catches in the function. This tables is reverse indexed base 1.
Bill Wendlingade025c2009-07-29 00:31:35 +0000333void DwarfException::EmitExceptionTable() {
Dan Gohman46510a72010-04-15 01:51:59 +0000334 const std::vector<const GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
Bill Wendlingade025c2009-07-29 00:31:35 +0000335 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
336 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
Bill Wendlingade025c2009-07-29 00:31:35 +0000337
338 // Sort the landing pads in order of their type ids. This is used to fold
339 // duplicate actions.
340 SmallVector<const LandingPadInfo *, 64> LandingPads;
341 LandingPads.reserve(PadInfos.size());
342
343 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
344 LandingPads.push_back(&PadInfos[i]);
345
Stephen Hines36b56882014-04-23 16:57:46 -0700346 // Order landing pads lexicographically by type id.
347 std::sort(LandingPads.begin(), LandingPads.end(),
348 [](const LandingPadInfo *L,
349 const LandingPadInfo *R) { return L->TypeIds < R->TypeIds; });
Bill Wendlingade025c2009-07-29 00:31:35 +0000350
351 // Compute the actions table and gather the first action index for each
352 // landing pad site.
353 SmallVector<ActionEntry, 32> Actions;
354 SmallVector<unsigned, 64> FirstActions;
Bill Wendling0a9abcb2010-02-10 21:41:57 +0000355 unsigned SizeActions=ComputeActionsTable(LandingPads, Actions, FirstActions);
Bill Wendlingade025c2009-07-29 00:31:35 +0000356
357 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
358 // by try-range labels when lowered). Ordinary calls do not, so appropriate
Bill Wendling28275fd2009-09-10 06:50:01 +0000359 // try-ranges for them need be deduced when using DWARF exception handling.
Bill Wendlingade025c2009-07-29 00:31:35 +0000360 RangeMapType PadMap;
361 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
362 const LandingPadInfo *LandingPad = LandingPads[i];
363 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
Chris Lattner16112732010-03-14 01:41:15 +0000364 MCSymbol *BeginLabel = LandingPad->BeginLabels[j];
Bill Wendlingade025c2009-07-29 00:31:35 +0000365 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
366 PadRange P = { i, j };
367 PadMap[BeginLabel] = P;
368 }
369 }
370
371 // Compute the call-site table.
372 SmallVector<CallSiteEntry, 64> CallSites;
Jim Grosbach8b818d72009-08-17 16:41:22 +0000373 ComputeCallSiteTable(CallSites, PadMap, LandingPads, FirstActions);
Bill Wendlingeb907212009-05-15 01:12:28 +0000374
375 // Final tallies.
376
377 // Call sites.
Chris Lattner84ac8b72010-04-05 00:26:50 +0000378 bool IsSJLJ = Asm->MAI->getExceptionHandlingType() == ExceptionHandling::SjLj;
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000379 bool HaveTTData = IsSJLJ ? (!TypeInfos.empty() || !FilterIds.empty()) : true;
Anton Korobeynikovd7e8ddc2011-01-14 21:57:45 +0000380
Bill Wendling3dc9b482010-02-24 23:34:35 +0000381 unsigned CallSiteTableLength;
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000382 if (IsSJLJ)
Bill Wendling3dc9b482010-02-24 23:34:35 +0000383 CallSiteTableLength = 0;
Chris Lattner9be49132010-04-04 20:10:41 +0000384 else {
385 unsigned SiteStartSize = 4; // dwarf::DW_EH_PE_udata4
386 unsigned SiteLengthSize = 4; // dwarf::DW_EH_PE_udata4
387 unsigned LandingPadSize = 4; // dwarf::DW_EH_PE_udata4
Anton Korobeynikovd7e8ddc2011-01-14 21:57:45 +0000388 CallSiteTableLength =
Chris Lattner9be49132010-04-04 20:10:41 +0000389 CallSites.size() * (SiteStartSize + SiteLengthSize + LandingPadSize);
390 }
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000391
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000392 for (unsigned i = 0, e = CallSites.size(); i < e; ++i) {
Stephen Hines36b56882014-04-23 16:57:46 -0700393 CallSiteTableLength += getULEB128Size(CallSites[i].Action);
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000394 if (IsSJLJ)
Stephen Hines36b56882014-04-23 16:57:46 -0700395 CallSiteTableLength += getULEB128Size(i);
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000396 }
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000397
Bill Wendlingeb907212009-05-15 01:12:28 +0000398 // Type infos.
Chris Lattnerd5bbb072009-08-02 01:34:32 +0000399 const MCSection *LSDASection = Asm->getObjFileLowering().getLSDASection();
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000400 unsigned TTypeEncoding;
Bill Wendlinga2f64492009-09-10 06:27:16 +0000401 unsigned TypeFormatSize;
Bill Wendlingeb907212009-05-15 01:12:28 +0000402
Bill Wendling43e484f2009-09-10 01:12:47 +0000403 if (!HaveTTData) {
Bill Wendling28275fd2009-09-10 06:50:01 +0000404 // For SjLj exceptions, if there is no TypeInfo, then we just explicitly say
405 // that we're omitting that bit.
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000406 TTypeEncoding = dwarf::DW_EH_PE_omit;
Chris Lattner84ac8b72010-04-05 00:26:50 +0000407 // dwarf::DW_EH_PE_absptr
Chandler Carruth426c2bf2012-11-01 09:14:31 +0000408 TypeFormatSize = Asm->getDataLayout().getPointerSize();
Chris Lattner81c9a062009-07-31 22:03:47 +0000409 } else {
Chris Lattnerad88bc42009-08-02 03:59:56 +0000410 // Okay, we have actual filters or typeinfos to emit. As such, we need to
411 // pick a type encoding for them. We're about to emit a list of pointers to
412 // typeinfo objects at the end of the LSDA. However, unless we're in static
413 // mode, this reference will require a relocation by the dynamic linker.
Chris Lattner46b754c2009-07-31 22:18:14 +0000414 //
Chris Lattnerad88bc42009-08-02 03:59:56 +0000415 // Because of this, we have a couple of options:
Anton Korobeynikovd7e8ddc2011-01-14 21:57:45 +0000416 //
Chris Lattnerad88bc42009-08-02 03:59:56 +0000417 // 1) If we are in -static mode, we can always use an absolute reference
418 // from the LSDA, because the static linker will resolve it.
Anton Korobeynikovd7e8ddc2011-01-14 21:57:45 +0000419 //
Chris Lattnerad88bc42009-08-02 03:59:56 +0000420 // 2) Otherwise, if the LSDA section is writable, we can output the direct
421 // reference to the typeinfo and allow the dynamic linker to relocate
422 // it. Since it is in a writable section, the dynamic linker won't
423 // have a problem.
Anton Korobeynikovd7e8ddc2011-01-14 21:57:45 +0000424 //
Chris Lattnerad88bc42009-08-02 03:59:56 +0000425 // 3) Finally, if we're in PIC mode and the LDSA section isn't writable,
426 // we need to use some form of indirection. For example, on Darwin,
427 // we can output a statically-relocatable reference to a dyld stub. The
428 // offset to the stub is constant, but the contents are in a section
429 // that is updated by the dynamic linker. This is easy enough, but we
430 // need to tell the personality function of the unwinder to indirect
431 // through the dyld stub.
432 //
Bill Wendling43e484f2009-09-10 01:12:47 +0000433 // FIXME: When (3) is actually implemented, we'll have to emit the stubs
Chris Lattnerad88bc42009-08-02 03:59:56 +0000434 // somewhere. This predicate should be moved to a shared location that is
435 // in target-independent code.
436 //
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000437 TTypeEncoding = Asm->getObjFileLowering().getTTypeEncoding();
Chris Lattnerd2af7852010-04-04 20:20:50 +0000438 TypeFormatSize = Asm->GetSizeOfEncodedValue(TTypeEncoding);
Bill Wendlingfe220282009-09-10 02:07:37 +0000439 }
Bill Wendling43e484f2009-09-10 01:12:47 +0000440
Bill Wendlingfe220282009-09-10 02:07:37 +0000441 // Begin the exception table.
Anton Korobeynikoved299f62011-01-30 22:07:31 +0000442 // Sometimes we want not to emit the data into separate section (e.g. ARM
443 // EHABI). In this case LSDASection will be NULL.
Anton Korobeynikovd4e09782011-01-24 22:38:40 +0000444 if (LSDASection)
445 Asm->OutStreamer.SwitchSection(LSDASection);
Chris Lattner059ea132010-04-28 01:05:45 +0000446 Asm->EmitAlignment(2);
Bill Wendling43e484f2009-09-10 01:12:47 +0000447
Bill Wendling3dc9b482010-02-24 23:34:35 +0000448 // Emit the LSDA.
Anton Korobeynikovd7e8ddc2011-01-14 21:57:45 +0000449 MCSymbol *GCCETSym =
Chris Lattner974c0fb2010-03-10 02:48:06 +0000450 Asm->OutContext.GetOrCreateSymbol(Twine("GCC_except_table")+
Chris Lattner84ac8b72010-04-05 00:26:50 +0000451 Twine(Asm->getFunctionNumber()));
Chris Lattner974c0fb2010-03-10 02:48:06 +0000452 Asm->OutStreamer.EmitLabel(GCCETSym);
Chris Lattner84ac8b72010-04-05 00:26:50 +0000453 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("exception",
454 Asm->getFunctionNumber()));
Bill Wendlingfe220282009-09-10 02:07:37 +0000455
Chris Lattner974c0fb2010-03-10 02:48:06 +0000456 if (IsSJLJ)
Chris Lattnerc0215722010-04-04 19:25:43 +0000457 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("_LSDA_",
458 Asm->getFunctionNumber()));
Bill Wendlingfe220282009-09-10 02:07:37 +0000459
Bill Wendling3dc9b482010-02-24 23:34:35 +0000460 // Emit the LSDA header.
Chris Lattnerca6190b2010-04-04 20:04:21 +0000461 Asm->EmitEncodingByte(dwarf::DW_EH_PE_omit, "@LPStart");
462 Asm->EmitEncodingByte(TTypeEncoding, "@TType");
Bill Wendlingfe220282009-09-10 02:07:37 +0000463
Bill Wendling3dc9b482010-02-24 23:34:35 +0000464 // The type infos need to be aligned. GCC does this by inserting padding just
465 // before the type infos. However, this changes the size of the exception
466 // table, so you need to take this into account when you output the exception
467 // table size. However, the size is output using a variable length encoding.
468 // So by increasing the size by inserting padding, you may increase the number
469 // of bytes used for writing the size. If it increases, say by one byte, then
470 // you now need to output one less byte of padding to get the type infos
471 // aligned. However this decreases the size of the exception table. This
472 // changes the value you have to output for the exception table size. Due to
473 // the variable length encoding, the number of bytes used for writing the
474 // length may decrease. If so, you then have to increase the amount of
475 // padding. And so on. If you look carefully at the GCC code you will see that
476 // it indeed does this in a loop, going on and on until the values stabilize.
477 // We chose another solution: don't output padding inside the table like GCC
478 // does, instead output it before the table.
479 unsigned SizeTypes = TypeInfos.size() * TypeFormatSize;
Stephen Hines36b56882014-04-23 16:57:46 -0700480 unsigned CallSiteTableLengthSize = getULEB128Size(CallSiteTableLength);
Bill Wendling3dc9b482010-02-24 23:34:35 +0000481 unsigned TTypeBaseOffset =
482 sizeof(int8_t) + // Call site format
483 CallSiteTableLengthSize + // Call site table length size
484 CallSiteTableLength + // Call site table length
485 SizeActions + // Actions size
486 SizeTypes;
Stephen Hines36b56882014-04-23 16:57:46 -0700487 unsigned TTypeBaseOffsetSize = getULEB128Size(TTypeBaseOffset);
Bill Wendling3dc9b482010-02-24 23:34:35 +0000488 unsigned TotalSize =
489 sizeof(int8_t) + // LPStart format
490 sizeof(int8_t) + // TType format
491 (HaveTTData ? TTypeBaseOffsetSize : 0) + // TType base offset size
492 TTypeBaseOffset; // TType base offset
493 unsigned SizeAlign = (4 - TotalSize) & 3;
494
Bill Wendling86f0d332010-02-25 23:52:44 +0000495 if (HaveTTData) {
Bill Wendling6507eca2010-02-26 21:31:01 +0000496 // Account for any extra padding that will be added to the call site table
Bill Wendlingf7e90ae2010-02-25 21:19:47 +0000497 // length.
Chris Lattner7e1a8f82010-04-04 19:09:29 +0000498 Asm->EmitULEB128(TTypeBaseOffset, "@TType base offset", SizeAlign);
Bill Wendling1869ac82010-02-26 22:17:52 +0000499 SizeAlign = 0;
Bill Wendling86f0d332010-02-25 23:52:44 +0000500 }
Bill Wendlingb0d9c3e2009-07-28 22:23:45 +0000501
Bill Wendling8fcd3e62011-06-21 22:30:20 +0000502 bool VerboseAsm = Asm->OutStreamer.isVerboseAsm();
503
Bill Wendling28275fd2009-09-10 06:50:01 +0000504 // SjLj Exception handling
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000505 if (IsSJLJ) {
Chris Lattnerca6190b2010-04-04 20:04:21 +0000506 Asm->EmitEncodingByte(dwarf::DW_EH_PE_udata4, "Call site");
Bill Wendling1869ac82010-02-26 22:17:52 +0000507
508 // Add extra padding if it wasn't added to the TType base offset.
Chris Lattner7e1a8f82010-04-04 19:09:29 +0000509 Asm->EmitULEB128(CallSiteTableLength, "Call site table length", SizeAlign);
Bill Wendlingeb907212009-05-15 01:12:28 +0000510
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000511 // Emit the landing pad site information.
Jim Grosbach8b818d72009-08-17 16:41:22 +0000512 unsigned idx = 0;
513 for (SmallVectorImpl<CallSiteEntry>::const_iterator
514 I = CallSites.begin(), E = CallSites.end(); I != E; ++I, ++idx) {
515 const CallSiteEntry &S = *I;
Bill Wendlinga583c552009-08-20 22:02:24 +0000516
Renato Golinf1f6de12011-08-18 23:43:14 +0000517 // Offset of the landing pad, counted in 16-byte bundles relative to the
518 // @LPStart address.
Bill Wendling0c112182011-06-21 22:40:24 +0000519 if (VerboseAsm) {
Benjamin Kramer47b87982011-10-16 15:46:29 +0000520 Asm->OutStreamer.AddComment(">> Call Site " + Twine(idx) + " <<");
521 Asm->OutStreamer.AddComment(" On exception at call site "+Twine(idx));
Bill Wendling0c112182011-06-21 22:40:24 +0000522 }
Bill Wendling0c112182011-06-21 22:40:24 +0000523 Asm->EmitULEB128(idx);
Bill Wendlinga583c552009-08-20 22:02:24 +0000524
525 // Offset of the first associated action record, relative to the start of
526 // the action table. This value is biased by 1 (1 indicates the start of
527 // the action table), and 0 indicates that there are no actions.
Renato Golinf1f6de12011-08-18 23:43:14 +0000528 if (VerboseAsm) {
529 if (S.Action == 0)
530 Asm->OutStreamer.AddComment(" Action: cleanup");
531 else
Benjamin Kramer47b87982011-10-16 15:46:29 +0000532 Asm->OutStreamer.AddComment(" Action: " +
533 Twine((S.Action - 1) / 2 + 1));
Renato Golinf1f6de12011-08-18 23:43:14 +0000534 }
Bill Wendling0c112182011-06-21 22:40:24 +0000535 Asm->EmitULEB128(S.Action);
Bill Wendlingeb907212009-05-15 01:12:28 +0000536 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000537 } else {
538 // DWARF Exception handling
Anton Korobeynikov3965b5e2011-01-14 21:58:08 +0000539 assert(Asm->MAI->isExceptionHandlingDwarf());
Bill Wendlingeb907212009-05-15 01:12:28 +0000540
Bill Wendlinga583c552009-08-20 22:02:24 +0000541 // The call-site table is a list of all call sites that may throw an
542 // exception (including C++ 'throw' statements) in the procedure
543 // fragment. It immediately follows the LSDA header. Each entry indicates,
544 // for a given call, the first corresponding action record and corresponding
545 // landing pad.
546 //
547 // The table begins with the number of bytes, stored as an LEB128
548 // compressed, unsigned integer. The records immediately follow the record
549 // count. They are sorted in increasing call-site address. Each record
550 // indicates:
551 //
552 // * The position of the call-site.
553 // * The position of the landing pad.
554 // * The first action record for that call site.
555 //
556 // A missing entry in the call-site table indicates that a call is not
Bill Wendling28275fd2009-09-10 06:50:01 +0000557 // supposed to throw.
Bill Wendlinga583c552009-08-20 22:02:24 +0000558
559 // Emit the landing pad call site table.
Chris Lattnerca6190b2010-04-04 20:04:21 +0000560 Asm->EmitEncodingByte(dwarf::DW_EH_PE_udata4, "Call site");
Bill Wendling1869ac82010-02-26 22:17:52 +0000561
562 // Add extra padding if it wasn't added to the TType base offset.
Chris Lattner7e1a8f82010-04-04 19:09:29 +0000563 Asm->EmitULEB128(CallSiteTableLength, "Call site table length", SizeAlign);
Bill Wendlingeb907212009-05-15 01:12:28 +0000564
Bill Wendling8fcd3e62011-06-21 22:30:20 +0000565 unsigned Entry = 0;
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000566 for (SmallVectorImpl<CallSiteEntry>::const_iterator
567 I = CallSites.begin(), E = CallSites.end(); I != E; ++I) {
568 const CallSiteEntry &S = *I;
Anton Korobeynikovd7e8ddc2011-01-14 21:57:45 +0000569
Chris Lattnerc0215722010-04-04 19:25:43 +0000570 MCSymbol *EHFuncBeginSym =
Chris Lattner84ac8b72010-04-05 00:26:50 +0000571 Asm->GetTempSymbol("eh_func_begin", Asm->getFunctionNumber());
Anton Korobeynikovd7e8ddc2011-01-14 21:57:45 +0000572
Chris Lattner16112732010-03-14 01:41:15 +0000573 MCSymbol *BeginLabel = S.BeginLabel;
574 if (BeginLabel == 0)
575 BeginLabel = EHFuncBeginSym;
576 MCSymbol *EndLabel = S.EndLabel;
577 if (EndLabel == 0)
Chris Lattner84ac8b72010-04-05 00:26:50 +0000578 EndLabel = Asm->GetTempSymbol("eh_func_end", Asm->getFunctionNumber());
Anton Korobeynikovd7e8ddc2011-01-14 21:57:45 +0000579
Bill Wendling8fcd3e62011-06-21 22:30:20 +0000580
Bill Wendlinga583c552009-08-20 22:02:24 +0000581 // Offset of the call site relative to the previous call site, counted in
582 // number of 16-byte bundles. The first call site is counted relative to
583 // the start of the procedure fragment.
Renato Golinf1f6de12011-08-18 23:43:14 +0000584 if (VerboseAsm)
Benjamin Kramer47b87982011-10-16 15:46:29 +0000585 Asm->OutStreamer.AddComment(">> Call Site " + Twine(++Entry) + " <<");
Chris Lattnerf88dce12010-04-04 21:31:54 +0000586 Asm->EmitLabelDifference(BeginLabel, EHFuncBeginSym, 4);
Renato Golinf1f6de12011-08-18 23:43:14 +0000587 if (VerboseAsm)
588 Asm->OutStreamer.AddComment(Twine(" Call between ") +
589 BeginLabel->getName() + " and " +
590 EndLabel->getName());
Chris Lattnera6437182010-04-04 19:58:12 +0000591 Asm->EmitLabelDifference(EndLabel, BeginLabel, 4);
Bill Wendlingeb907212009-05-15 01:12:28 +0000592
Bill Wendlinga583c552009-08-20 22:02:24 +0000593 // Offset of the landing pad, counted in 16-byte bundles relative to the
594 // @LPStart address.
Renato Golinf1f6de12011-08-18 23:43:14 +0000595 if (!S.PadLabel) {
596 if (VerboseAsm)
597 Asm->OutStreamer.AddComment(" has no landing pad");
Eric Christopherca1dd052013-01-09 01:35:34 +0000598 Asm->OutStreamer.EmitIntValue(0, 4/*size*/);
Renato Golinf1f6de12011-08-18 23:43:14 +0000599 } else {
600 if (VerboseAsm)
601 Asm->OutStreamer.AddComment(Twine(" jumps to ") +
602 S.PadLabel->getName());
Chris Lattnerf88dce12010-04-04 21:31:54 +0000603 Asm->EmitLabelDifference(S.PadLabel, EHFuncBeginSym, 4);
Renato Golinf1f6de12011-08-18 23:43:14 +0000604 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000605
Bill Wendlinga583c552009-08-20 22:02:24 +0000606 // Offset of the first associated action record, relative to the start of
607 // the action table. This value is biased by 1 (1 indicates the start of
608 // the action table), and 0 indicates that there are no actions.
Renato Golinf1f6de12011-08-18 23:43:14 +0000609 if (VerboseAsm) {
610 if (S.Action == 0)
611 Asm->OutStreamer.AddComment(" On action: cleanup");
612 else
Benjamin Kramer47b87982011-10-16 15:46:29 +0000613 Asm->OutStreamer.AddComment(" On action: " +
614 Twine((S.Action - 1) / 2 + 1));
Renato Golinf1f6de12011-08-18 23:43:14 +0000615 }
Bill Wendling8fcd3e62011-06-21 22:30:20 +0000616 Asm->EmitULEB128(S.Action);
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000617 }
Bill Wendlingeb907212009-05-15 01:12:28 +0000618 }
619
Bill Wendlinga583c552009-08-20 22:02:24 +0000620 // Emit the Action Table.
Bill Wendling8fcd3e62011-06-21 22:30:20 +0000621 int Entry = 0;
Bill Wendling5cff4872009-07-28 23:44:43 +0000622 for (SmallVectorImpl<ActionEntry>::const_iterator
623 I = Actions.begin(), E = Actions.end(); I != E; ++I) {
624 const ActionEntry &Action = *I;
Bill Wendling8fcd3e62011-06-21 22:30:20 +0000625
626 if (VerboseAsm) {
627 // Emit comments that decode the action table.
Benjamin Kramer47b87982011-10-16 15:46:29 +0000628 Asm->OutStreamer.AddComment(">> Action Record " + Twine(++Entry) + " <<");
Renato Golinf1f6de12011-08-18 23:43:14 +0000629 }
630
631 // Type Filter
632 //
633 // Used by the runtime to match the type of the thrown exception to the
634 // type of the catch clauses or the types in the exception specification.
635 if (VerboseAsm) {
Duncan Sands42e5c792011-09-28 09:13:02 +0000636 if (Action.ValueForTypeID > 0)
Benjamin Kramer47b87982011-10-16 15:46:29 +0000637 Asm->OutStreamer.AddComment(" Catch TypeInfo " +
638 Twine(Action.ValueForTypeID));
Duncan Sands42e5c792011-09-28 09:13:02 +0000639 else if (Action.ValueForTypeID < 0)
Benjamin Kramer47b87982011-10-16 15:46:29 +0000640 Asm->OutStreamer.AddComment(" Filter TypeInfo " +
641 Twine(Action.ValueForTypeID));
Duncan Sands42e5c792011-09-28 09:13:02 +0000642 else
643 Asm->OutStreamer.AddComment(" Cleanup");
Renato Golinf1f6de12011-08-18 23:43:14 +0000644 }
645 Asm->EmitSLEB128(Action.ValueForTypeID);
Bill Wendling8fcd3e62011-06-21 22:30:20 +0000646
Renato Golinf1f6de12011-08-18 23:43:14 +0000647 // Action Record
648 //
649 // Self-relative signed displacement in bytes of the next action record,
650 // or 0 if there is no next action record.
651 if (VerboseAsm) {
Bill Wendling8fcd3e62011-06-21 22:30:20 +0000652 if (Action.NextAction == 0) {
653 Asm->OutStreamer.AddComment(" No further actions");
654 } else {
655 unsigned NextAction = Entry + (Action.NextAction + 1) / 2;
Benjamin Kramer47b87982011-10-16 15:46:29 +0000656 Asm->OutStreamer.AddComment(" Continue to action "+Twine(NextAction));
Bill Wendling8fcd3e62011-06-21 22:30:20 +0000657 }
Bill Wendling8fcd3e62011-06-21 22:30:20 +0000658 }
Bill Wendling8fcd3e62011-06-21 22:30:20 +0000659 Asm->EmitSLEB128(Action.NextAction);
Bill Wendlingeb907212009-05-15 01:12:28 +0000660 }
661
Anton Korobeynikov2386fc82012-11-19 21:06:26 +0000662 EmitTypeInfos(TTypeEncoding);
663
664 Asm->EmitAlignment(2);
665}
666
667void DwarfException::EmitTypeInfos(unsigned TTypeEncoding) {
668 const std::vector<const GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
669 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
670
671 bool VerboseAsm = Asm->OutStreamer.isVerboseAsm();
672
673 int Entry = 0;
Bill Wendling48dc29e2009-10-22 20:48:59 +0000674 // Emit the Catch TypeInfos.
Bill Wendling8fcd3e62011-06-21 22:30:20 +0000675 if (VerboseAsm && !TypeInfos.empty()) {
676 Asm->OutStreamer.AddComment(">> Catch TypeInfos <<");
Chris Lattner233f52b2010-03-09 23:52:58 +0000677 Asm->OutStreamer.AddBlankLine();
Bill Wendling8fcd3e62011-06-21 22:30:20 +0000678 Entry = TypeInfos.size();
Chris Lattner233f52b2010-03-09 23:52:58 +0000679 }
Bill Wendling8fcd3e62011-06-21 22:30:20 +0000680
Dan Gohman46510a72010-04-15 01:51:59 +0000681 for (std::vector<const GlobalVariable *>::const_reverse_iterator
Bill Wendling01c69372009-11-19 00:09:14 +0000682 I = TypeInfos.rbegin(), E = TypeInfos.rend(); I != E; ++I) {
Bill Wendlingfb7634f2009-11-19 19:21:09 +0000683 const GlobalVariable *GV = *I;
Bill Wendling8fcd3e62011-06-21 22:30:20 +0000684 if (VerboseAsm)
Benjamin Kramer47b87982011-10-16 15:46:29 +0000685 Asm->OutStreamer.AddComment("TypeInfo " + Twine(Entry--));
Anton Korobeynikov239938f2012-11-19 21:17:20 +0000686 Asm->EmitTTypeReference(GV, TTypeEncoding);
Bill Wendlingeb907212009-05-15 01:12:28 +0000687 }
688
Bill Wendling48dc29e2009-10-22 20:48:59 +0000689 // Emit the Exception Specifications.
Bill Wendling8fcd3e62011-06-21 22:30:20 +0000690 if (VerboseAsm && !FilterIds.empty()) {
691 Asm->OutStreamer.AddComment(">> Filter TypeInfos <<");
Chris Lattner233f52b2010-03-09 23:52:58 +0000692 Asm->OutStreamer.AddBlankLine();
Bill Wendling8fcd3e62011-06-21 22:30:20 +0000693 Entry = 0;
Chris Lattner233f52b2010-03-09 23:52:58 +0000694 }
Bill Wendling5cff4872009-07-28 23:44:43 +0000695 for (std::vector<unsigned>::const_iterator
696 I = FilterIds.begin(), E = FilterIds.end(); I < E; ++I) {
697 unsigned TypeID = *I;
Bill Wendling8fcd3e62011-06-21 22:30:20 +0000698 if (VerboseAsm) {
699 --Entry;
700 if (TypeID != 0)
Benjamin Kramer47b87982011-10-16 15:46:29 +0000701 Asm->OutStreamer.AddComment("FilterInfo " + Twine(Entry));
Bill Wendling8fcd3e62011-06-21 22:30:20 +0000702 }
703
704 Asm->EmitULEB128(TypeID);
Bill Wendlingeb907212009-05-15 01:12:28 +0000705 }
Bill Wendlingeb907212009-05-15 01:12:28 +0000706}
707
Stephen Hines36b56882014-04-23 16:57:46 -0700708/// endModule - Emit all exception information that should come after the
Bill Wendlingeb907212009-05-15 01:12:28 +0000709/// content.
Stephen Hines36b56882014-04-23 16:57:46 -0700710void DwarfException::endModule() {
Craig Topper5e25ee82012-02-05 08:31:47 +0000711 llvm_unreachable("Should be implemented");
Bill Wendlingeb907212009-05-15 01:12:28 +0000712}
713
Stephen Hines36b56882014-04-23 16:57:46 -0700714/// beginFunction - Gather pre-function exception information. Assumes it's
Bill Wendling28275fd2009-09-10 06:50:01 +0000715/// being emitted immediately after the function entry point.
Stephen Hines36b56882014-04-23 16:57:46 -0700716void DwarfException::beginFunction(const MachineFunction *MF) {
Craig Topper5e25ee82012-02-05 08:31:47 +0000717 llvm_unreachable("Should be implemented");
Bill Wendlingeb907212009-05-15 01:12:28 +0000718}
719
Stephen Hines36b56882014-04-23 16:57:46 -0700720/// endFunction - Gather and emit post-function exception information.
721void DwarfException::endFunction(const MachineFunction *) {
Craig Topper5e25ee82012-02-05 08:31:47 +0000722 llvm_unreachable("Should be implemented");
Bill Wendlingeb907212009-05-15 01:12:28 +0000723}