blob: 6a8aea29e9623df6127db1245d5823dc47443a0d [file] [log] [blame]
Bill Wendlingd64cd2b2009-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 Wendling023ed642009-09-10 06:50:01 +000010// This file contains support for writing DWARF exception info into asm files.
Bill Wendlingd64cd2b2009-05-15 01:12:28 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "DwarfException.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/ADT/SmallString.h"
16#include "llvm/ADT/StringExtras.h"
17#include "llvm/ADT/Twine.h"
Chris Lattnerda790ea2010-04-05 05:28:23 +000018#include "llvm/CodeGen/AsmPrinter.h"
Bill Wendlingd64cd2b2009-05-15 01:12:28 +000019#include "llvm/CodeGen/MachineFrameInfo.h"
David Greenea5be1b12009-08-19 21:55:33 +000020#include "llvm/CodeGen/MachineFunction.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/DataLayout.h"
Rafael Espindola894843c2014-01-07 21:19:40 +000023#include "llvm/IR/Mangler.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/Module.h"
Chris Lattnerb8666022009-09-16 01:46:41 +000025#include "llvm/MC/MCAsmInfo.h"
26#include "llvm/MC/MCContext.h"
27#include "llvm/MC/MCExpr.h"
Bill Wendlingbf562682009-09-10 01:12:47 +000028#include "llvm/MC/MCSection.h"
Chris Lattner4b7dadb2009-08-19 05:49:37 +000029#include "llvm/MC/MCStreamer.h"
Chris Lattner555ceabe2010-01-16 18:37:32 +000030#include "llvm/MC/MCSymbol.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000031#include "llvm/Support/Dwarf.h"
32#include "llvm/Support/ErrorHandling.h"
33#include "llvm/Support/FormattedStream.h"
Logan Chien5b776b72014-02-22 14:00:39 +000034#include "llvm/Support/LEB128.h"
Anton Korobeynikov2f931282011-01-10 12:39:04 +000035#include "llvm/Target/TargetFrameLowering.h"
Chris Lattnerc16c75e2009-08-02 01:34:32 +000036#include "llvm/Target/TargetLoweringObjectFile.h"
Bill Wendlingd64cd2b2009-05-15 01:12:28 +000037#include "llvm/Target/TargetOptions.h"
Chris Lattnerc16c75e2009-08-02 01:34:32 +000038#include "llvm/Target/TargetRegisterInfo.h"
Bill Wendlingd64cd2b2009-05-15 01:12:28 +000039using namespace llvm;
40
Chris Lattner11980022010-04-04 07:48:20 +000041DwarfException::DwarfException(AsmPrinter *A)
Anton Korobeynikov61d167e2011-01-14 21:57:45 +000042 : Asm(A), MMI(Asm->MMI) {}
Bill Wendlingdd20b982009-05-15 01:18:50 +000043
Bill Wendlingfcc14142010-04-07 09:28:04 +000044DwarfException::~DwarfException() {}
Bill Wendlingdd20b982009-05-15 01:18:50 +000045
Bill Wendlingd64cd2b2009-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 Wendling6c574d82009-07-28 23:23:00 +000061/// ComputeActionsTable - Compute the actions table and gather the first action
62/// index for each landing pad site.
Bill Wendlingb67440e2009-07-29 00:31:35 +000063unsigned DwarfException::
64ComputeActionsTable(const SmallVectorImpl<const LandingPadInfo*> &LandingPads,
65 SmallVectorImpl<ActionEntry> &Actions,
66 SmallVectorImpl<unsigned> &FirstActions) {
Bill Wendling0e749fe2009-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 Wendling1fddd872009-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 Wendlingd64cd2b2009-05-15 01:12:28 +000083 // (negative) byte offset of the corresponding FilterIds entry. The byte
Bill Wendling1fddd872009-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 Wendlingd64cd2b2009-05-15 01:12:28 +000088 // output using a fixed width encoding. FilterOffsets[i] holds the byte
89 // offset corresponding to FilterIds[i].
Bill Wendling45057342009-07-29 21:19:44 +000090
91 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
Bill Wendlingd64cd2b2009-05-15 01:12:28 +000092 SmallVector<int, 16> FilterOffsets;
93 FilterOffsets.reserve(FilterIds.size());
94 int Offset = -1;
Bill Wendling45057342009-07-29 21:19:44 +000095
96 for (std::vector<unsigned>::const_iterator
97 I = FilterIds.begin(), E = FilterIds.end(); I != E; ++I) {
Bill Wendlingd64cd2b2009-05-15 01:12:28 +000098 FilterOffsets.push_back(Offset);
Logan Chien5b776b72014-02-22 14:00:39 +000099 Offset -= getULEB128Size(*I);
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000100 }
101
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000102 FirstActions.reserve(LandingPads.size());
103
104 int FirstAction = 0;
105 unsigned SizeActions = 0;
Craig Topper353eda42014-04-24 06:44:33 +0000106 const LandingPadInfo *PrevLPI = nullptr;
Bill Wendling45057342009-07-29 21:19:44 +0000107
Bill Wendling23b177e2009-07-28 23:44:43 +0000108 for (SmallVectorImpl<const LandingPadInfo *>::const_iterator
Bill Wendling1fddd872009-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 Lattner07c1b942010-04-04 20:10:41 +0000112 unsigned NumShared = PrevLPI ? SharedTypeIds(LPI, PrevLPI) : 0;
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000113 unsigned SizeSiteActions = 0;
114
115 if (NumShared < TypeIds.size()) {
116 unsigned SizeAction = 0;
Bill Wendling7742b642010-02-10 21:41:57 +0000117 unsigned PrevAction = (unsigned)-1;
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000118
119 if (NumShared) {
Chris Lattner07c1b942010-04-04 20:10:41 +0000120 unsigned SizePrevIds = PrevLPI->TypeIds.size();
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000121 assert(Actions.size());
Bill Wendling7742b642010-02-10 21:41:57 +0000122 PrevAction = Actions.size() - 1;
Logan Chien5b776b72014-02-22 14:00:39 +0000123 SizeAction = getSLEB128Size(Actions[PrevAction].NextAction) +
124 getSLEB128Size(Actions[PrevAction].ValueForTypeID);
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000125
126 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
Bill Wendling7742b642010-02-10 21:41:57 +0000127 assert(PrevAction != (unsigned)-1 && "PrevAction is invalid!");
Logan Chien5b776b72014-02-22 14:00:39 +0000128 SizeAction -= getSLEB128Size(Actions[PrevAction].ValueForTypeID);
Bill Wendling7742b642010-02-10 21:41:57 +0000129 SizeAction += -Actions[PrevAction].NextAction;
130 PrevAction = Actions[PrevAction].Previous;
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000131 }
132 }
133
134 // Compute the actions.
Bill Wendling1fddd872009-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 Wendlingd64cd2b2009-05-15 01:12:28 +0000138 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
Logan Chien5b776b72014-02-22 14:00:39 +0000139 unsigned SizeTypeID = getSLEB128Size(ValueForTypeID);
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000140
141 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
Logan Chien5b776b72014-02-22 14:00:39 +0000142 SizeAction = SizeTypeID + getSLEB128Size(NextAction);
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000143 SizeSiteActions += SizeAction;
144
Bill Wendling0e749fe2009-08-20 22:02:24 +0000145 ActionEntry Action = { ValueForTypeID, NextAction, PrevAction };
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000146 Actions.push_back(Action);
Bill Wendling7742b642010-02-10 21:41:57 +0000147 PrevAction = Actions.size() - 1;
Bill Wendlingd64cd2b2009-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 Wendling0e749fe2009-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 Wendling7742b642010-02-10 21:41:57 +0000157 // biased by 1 (1 indicating the start of the actions table), and 0
Bill Wendling0e749fe2009-08-20 22:02:24 +0000158 // indicates that there are no actions.
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000159 FirstActions.push_back(FirstAction);
160
161 // Compute this sites contribution to size.
162 SizeActions += SizeSiteActions;
Bill Wendling1fddd872009-07-28 23:22:13 +0000163
164 PrevLPI = LPI;
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000165 }
166
Bill Wendling1fddd872009-07-28 23:22:13 +0000167 return SizeActions;
168}
169
Bill Wendlinge4120642009-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 Cheng7f8e5632011-12-07 07:15:52 +0000173 assert(MI->isCall() && "This should be a call instruction!");
Bill Wendlinge4120642009-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 Lattner17d38592010-03-31 06:09:04 +0000181 if (!MO.isGlobal()) continue;
Anton Korobeynikov61d167e2011-01-14 21:57:45 +0000182
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000183 const Function *F = dyn_cast<Function>(MO.getGlobal());
Craig Topper353eda42014-04-24 06:44:33 +0000184 if (!F) continue;
Bill Wendlingc781d7a2009-11-12 23:13:08 +0000185
Chris Lattner17d38592010-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 Korobeynikov61d167e2011-01-14 21:57:45 +0000190 //
Chris Lattner17d38592010-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 Wendlinge4120642009-11-12 21:59:20 +0000195 }
Chris Lattner17d38592010-03-31 06:09:04 +0000196
197 MarkedNoUnwind = F->doesNotThrow();
198 SawFunc = true;
Bill Wendlinge4120642009-11-12 21:59:20 +0000199 }
200
201 return MarkedNoUnwind;
202}
203
Bill Wendlingb67440e2009-07-29 00:31:35 +0000204/// ComputeCallSiteTable - Compute the call-site table. The entry for an invoke
Bill Wendling0e749fe2009-08-20 22:02:24 +0000205/// has a try-range containing the call, a non-zero landing pad, and an
Bill Wendlingb67440e2009-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 Wendlingd64cd2b2009-05-15 01:12:28 +0000216 // The end label of the previous invoke or nounwind try-range.
Craig Topper353eda42014-04-24 06:44:33 +0000217 MCSymbol *LastLabel = nullptr;
Bill Wendlingd64cd2b2009-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 Wendling23b177e2009-07-28 23:44:43 +0000223 // Whether the last CallSite entry was for an invoke.
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000224 bool PreviousIsInvoke = false;
225
226 // Visit all instructions in order of address.
Alexey Samsonov41b977d2014-04-30 18:29:51 +0000227 for (const auto &MBB : *Asm->MF) {
228 for (MachineBasicBlock::const_iterator MI = MBB.begin(), E = MBB.end();
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000229 MI != E; ++MI) {
Rafael Espindola736bec82014-03-06 16:31:40 +0000230 if (!MI->isEHLabel()) {
Evan Cheng7f8e5632011-12-07 07:15:52 +0000231 if (MI->isCall())
Bill Wendlinge4120642009-11-12 21:59:20 +0000232 SawPotentiallyThrowing |= !CallToNoUnwindFunction(MI);
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000233 continue;
234 }
235
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000236 // End of the previous try-range?
Chris Lattnerff1fdd62010-03-14 08:17:53 +0000237 MCSymbol *BeginLabel = MI->getOperand(0).getMCSymbol();
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000238 if (BeginLabel == LastLabel)
239 SawPotentiallyThrowing = false;
240
241 // Beginning of a new try-range?
Jeffrey Yasskinb40d3f72009-11-10 01:02:17 +0000242 RangeMapType::const_iterator L = PadMap.find(BeginLabel);
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000243 if (L == PadMap.end())
244 // Nope, it was just some random label.
245 continue;
246
Bill Wendling0e749fe2009-08-20 22:02:24 +0000247 const PadRange &P = L->second;
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000248 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000249 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
250 "Inconsistent landing pad map!");
251
Bill Wendling0e749fe2009-08-20 22:02:24 +0000252 // For Dwarf exception handling (SjLj handling doesn't use this). If some
253 // instruction between the previous try-range and this one may throw,
254 // create a call-site entry with no landing pad for the region between the
255 // try-ranges.
Anton Korobeynikov9be547c2011-01-14 21:58:08 +0000256 if (SawPotentiallyThrowing && Asm->MAI->isExceptionHandlingDwarf()) {
Craig Topper353eda42014-04-24 06:44:33 +0000257 CallSiteEntry Site = { LastLabel, BeginLabel, nullptr, 0 };
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000258 CallSites.push_back(Site);
259 PreviousIsInvoke = false;
260 }
261
262 LastLabel = LandingPad->EndLabels[P.RangeIndex];
263 assert(BeginLabel && LastLabel && "Invalid landing pad!");
264
Chris Lattner17d38592010-03-31 06:09:04 +0000265 if (!LandingPad->LandingPadLabel) {
266 // Create a gap.
267 PreviousIsInvoke = false;
268 } else {
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000269 // This try-range is for an invoke.
Bill Wendling0e749fe2009-08-20 22:02:24 +0000270 CallSiteEntry Site = {
271 BeginLabel,
272 LastLabel,
273 LandingPad->LandingPadLabel,
274 FirstActions[P.PadIndex]
275 };
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000276
Jim Grosbach5afbf2b2009-09-01 17:19:13 +0000277 // Try to merge with the previous call-site. SJLJ doesn't do this
Anton Korobeynikov9be547c2011-01-14 21:58:08 +0000278 if (PreviousIsInvoke && Asm->MAI->isExceptionHandlingDwarf()) {
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000279 CallSiteEntry &Prev = CallSites.back();
280 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
281 // Extend the range of the previous entry.
282 Prev.EndLabel = Site.EndLabel;
283 continue;
284 }
285 }
286
287 // Otherwise, create a new call-site.
Anton Korobeynikov9be547c2011-01-14 21:58:08 +0000288 if (Asm->MAI->isExceptionHandlingDwarf())
Jim Grosbach54c05302010-01-28 01:45:32 +0000289 CallSites.push_back(Site);
290 else {
291 // SjLj EH must maintain the call sites in the order assigned
292 // to them by the SjLjPrepare pass.
293 unsigned SiteNo = MMI->getCallSiteBeginLabel(BeginLabel);
294 if (CallSites.size() < SiteNo)
295 CallSites.resize(SiteNo);
296 CallSites[SiteNo - 1] = Site;
297 }
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000298 PreviousIsInvoke = true;
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000299 }
300 }
301 }
302
303 // If some instruction between the previous try-range and the end of the
304 // function may throw, create a call-site entry with no landing pad for the
305 // region following the try-range.
Anton Korobeynikov9be547c2011-01-14 21:58:08 +0000306 if (SawPotentiallyThrowing && Asm->MAI->isExceptionHandlingDwarf()) {
Craig Topper353eda42014-04-24 06:44:33 +0000307 CallSiteEntry Site = { LastLabel, nullptr, nullptr, 0 };
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000308 CallSites.push_back(Site);
309 }
Bill Wendlingb67440e2009-07-29 00:31:35 +0000310}
311
Bill Wendling2cf00142009-07-29 00:50:05 +0000312/// EmitExceptionTable - Emit landing pads and actions.
313///
314/// The general organization of the table is complex, but the basic concepts are
315/// easy. First there is a header which describes the location and organization
316/// of the three components that follow.
Eric Christopherd517ac02009-08-28 22:33:43 +0000317///
Bill Wendling2cf00142009-07-29 00:50:05 +0000318/// 1. The landing pad site information describes the range of code covered by
319/// the try. In our case it's an accumulation of the ranges covered by the
320/// invokes in the try. There is also a reference to the landing pad that
321/// handles the exception once processed. Finally an index into the actions
322/// table.
Bill Wendling0e749fe2009-08-20 22:02:24 +0000323/// 2. The action table, in our case, is composed of pairs of type IDs and next
Bill Wendling2cf00142009-07-29 00:50:05 +0000324/// action offset. Starting with the action index from the landing pad
Bill Wendling0e749fe2009-08-20 22:02:24 +0000325/// site, each type ID is checked for a match to the current exception. If
Bill Wendling2cf00142009-07-29 00:50:05 +0000326/// it matches then the exception and type id are passed on to the landing
327/// pad. Otherwise the next action is looked up. This chain is terminated
Bill Wendling023ed642009-09-10 06:50:01 +0000328/// with a next action of zero. If no type id is found then the frame is
Bill Wendling2cf00142009-07-29 00:50:05 +0000329/// unwound and handling continues.
Bill Wendling0e749fe2009-08-20 22:02:24 +0000330/// 3. Type ID table contains references to all the C++ typeinfo for all
Bill Wendling023ed642009-09-10 06:50:01 +0000331/// catches in the function. This tables is reverse indexed base 1.
Bill Wendlingb67440e2009-07-29 00:31:35 +0000332void DwarfException::EmitExceptionTable() {
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000333 const std::vector<const GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
Bill Wendlingb67440e2009-07-29 00:31:35 +0000334 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
335 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
Bill Wendlingb67440e2009-07-29 00:31:35 +0000336
337 // Sort the landing pads in order of their type ids. This is used to fold
338 // duplicate actions.
339 SmallVector<const LandingPadInfo *, 64> LandingPads;
340 LandingPads.reserve(PadInfos.size());
341
342 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
343 LandingPads.push_back(&PadInfos[i]);
344
Benjamin Kramerb0f74b22014-03-07 21:35:39 +0000345 // Order landing pads lexicographically by type id.
346 std::sort(LandingPads.begin(), LandingPads.end(),
347 [](const LandingPadInfo *L,
348 const LandingPadInfo *R) { return L->TypeIds < R->TypeIds; });
Bill Wendlingb67440e2009-07-29 00:31:35 +0000349
350 // Compute the actions table and gather the first action index for each
351 // landing pad site.
352 SmallVector<ActionEntry, 32> Actions;
353 SmallVector<unsigned, 64> FirstActions;
Bill Wendling7742b642010-02-10 21:41:57 +0000354 unsigned SizeActions=ComputeActionsTable(LandingPads, Actions, FirstActions);
Bill Wendlingb67440e2009-07-29 00:31:35 +0000355
356 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
357 // by try-range labels when lowered). Ordinary calls do not, so appropriate
Bill Wendling023ed642009-09-10 06:50:01 +0000358 // try-ranges for them need be deduced when using DWARF exception handling.
Bill Wendlingb67440e2009-07-29 00:31:35 +0000359 RangeMapType PadMap;
360 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
361 const LandingPadInfo *LandingPad = LandingPads[i];
362 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
Chris Lattner34adc8d2010-03-14 01:41:15 +0000363 MCSymbol *BeginLabel = LandingPad->BeginLabels[j];
Bill Wendlingb67440e2009-07-29 00:31:35 +0000364 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
365 PadRange P = { i, j };
366 PadMap[BeginLabel] = P;
367 }
368 }
369
370 // Compute the call-site table.
371 SmallVector<CallSiteEntry, 64> CallSites;
Jim Grosbach486be662009-08-17 16:41:22 +0000372 ComputeCallSiteTable(CallSites, PadMap, LandingPads, FirstActions);
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000373
374 // Final tallies.
375
376 // Call sites.
Chris Lattnerfd795022010-04-05 00:26:50 +0000377 bool IsSJLJ = Asm->MAI->getExceptionHandlingType() == ExceptionHandling::SjLj;
Bill Wendlinga482ec82009-09-10 00:17:04 +0000378 bool HaveTTData = IsSJLJ ? (!TypeInfos.empty() || !FilterIds.empty()) : true;
Anton Korobeynikov61d167e2011-01-14 21:57:45 +0000379
Bill Wendling88fdcd32010-02-24 23:34:35 +0000380 unsigned CallSiteTableLength;
Bill Wendlinga482ec82009-09-10 00:17:04 +0000381 if (IsSJLJ)
Bill Wendling88fdcd32010-02-24 23:34:35 +0000382 CallSiteTableLength = 0;
Chris Lattner07c1b942010-04-04 20:10:41 +0000383 else {
384 unsigned SiteStartSize = 4; // dwarf::DW_EH_PE_udata4
385 unsigned SiteLengthSize = 4; // dwarf::DW_EH_PE_udata4
386 unsigned LandingPadSize = 4; // dwarf::DW_EH_PE_udata4
Anton Korobeynikov61d167e2011-01-14 21:57:45 +0000387 CallSiteTableLength =
Chris Lattner07c1b942010-04-04 20:10:41 +0000388 CallSites.size() * (SiteStartSize + SiteLengthSize + LandingPadSize);
389 }
Bill Wendlinga482ec82009-09-10 00:17:04 +0000390
Jim Grosbach693e36a2009-08-11 00:09:57 +0000391 for (unsigned i = 0, e = CallSites.size(); i < e; ++i) {
Logan Chien5b776b72014-02-22 14:00:39 +0000392 CallSiteTableLength += getULEB128Size(CallSites[i].Action);
Bill Wendlinga482ec82009-09-10 00:17:04 +0000393 if (IsSJLJ)
Logan Chien5b776b72014-02-22 14:00:39 +0000394 CallSiteTableLength += getULEB128Size(i);
Jim Grosbach693e36a2009-08-11 00:09:57 +0000395 }
Bill Wendlinga482ec82009-09-10 00:17:04 +0000396
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000397 // Type infos.
Chris Lattnerc16c75e2009-08-02 01:34:32 +0000398 const MCSection *LSDASection = Asm->getObjFileLowering().getLSDASection();
Anton Korobeynikovae4ccc12010-02-15 22:35:59 +0000399 unsigned TTypeEncoding;
Bill Wendling9e5c2062009-09-10 06:27:16 +0000400 unsigned TypeFormatSize;
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000401
Bill Wendlingbf562682009-09-10 01:12:47 +0000402 if (!HaveTTData) {
Bill Wendling023ed642009-09-10 06:50:01 +0000403 // For SjLj exceptions, if there is no TypeInfo, then we just explicitly say
404 // that we're omitting that bit.
Anton Korobeynikovae4ccc12010-02-15 22:35:59 +0000405 TTypeEncoding = dwarf::DW_EH_PE_omit;
Chris Lattnerfd795022010-04-05 00:26:50 +0000406 // dwarf::DW_EH_PE_absptr
Chandler Carruth5da3f052012-11-01 09:14:31 +0000407 TypeFormatSize = Asm->getDataLayout().getPointerSize();
Chris Lattner47fc2352009-07-31 22:03:47 +0000408 } else {
Chris Lattner7005cd32009-08-02 03:59:56 +0000409 // Okay, we have actual filters or typeinfos to emit. As such, we need to
410 // pick a type encoding for them. We're about to emit a list of pointers to
411 // typeinfo objects at the end of the LSDA. However, unless we're in static
412 // mode, this reference will require a relocation by the dynamic linker.
Chris Lattnerf4b92a82009-07-31 22:18:14 +0000413 //
Chris Lattner7005cd32009-08-02 03:59:56 +0000414 // Because of this, we have a couple of options:
Anton Korobeynikov61d167e2011-01-14 21:57:45 +0000415 //
Chris Lattner7005cd32009-08-02 03:59:56 +0000416 // 1) If we are in -static mode, we can always use an absolute reference
417 // from the LSDA, because the static linker will resolve it.
Anton Korobeynikov61d167e2011-01-14 21:57:45 +0000418 //
Chris Lattner7005cd32009-08-02 03:59:56 +0000419 // 2) Otherwise, if the LSDA section is writable, we can output the direct
420 // reference to the typeinfo and allow the dynamic linker to relocate
421 // it. Since it is in a writable section, the dynamic linker won't
422 // have a problem.
Anton Korobeynikov61d167e2011-01-14 21:57:45 +0000423 //
Chris Lattner7005cd32009-08-02 03:59:56 +0000424 // 3) Finally, if we're in PIC mode and the LDSA section isn't writable,
425 // we need to use some form of indirection. For example, on Darwin,
426 // we can output a statically-relocatable reference to a dyld stub. The
427 // offset to the stub is constant, but the contents are in a section
428 // that is updated by the dynamic linker. This is easy enough, but we
429 // need to tell the personality function of the unwinder to indirect
430 // through the dyld stub.
431 //
Bill Wendlingbf562682009-09-10 01:12:47 +0000432 // FIXME: When (3) is actually implemented, we'll have to emit the stubs
Chris Lattner7005cd32009-08-02 03:59:56 +0000433 // somewhere. This predicate should be moved to a shared location that is
434 // in target-independent code.
435 //
Anton Korobeynikovae4ccc12010-02-15 22:35:59 +0000436 TTypeEncoding = Asm->getObjFileLowering().getTTypeEncoding();
Chris Lattnere619c0d2010-04-04 20:20:50 +0000437 TypeFormatSize = Asm->GetSizeOfEncodedValue(TTypeEncoding);
Bill Wendling243ac562009-09-10 02:07:37 +0000438 }
Bill Wendlingbf562682009-09-10 01:12:47 +0000439
Bill Wendling243ac562009-09-10 02:07:37 +0000440 // Begin the exception table.
Anton Korobeynikovfe3a6e02011-01-30 22:07:31 +0000441 // Sometimes we want not to emit the data into separate section (e.g. ARM
442 // EHABI). In this case LSDASection will be NULL.
Anton Korobeynikovb15beb22011-01-24 22:38:40 +0000443 if (LSDASection)
444 Asm->OutStreamer.SwitchSection(LSDASection);
Chris Lattner9e06e532010-04-28 01:05:45 +0000445 Asm->EmitAlignment(2);
Bill Wendlingbf562682009-09-10 01:12:47 +0000446
Bill Wendling88fdcd32010-02-24 23:34:35 +0000447 // Emit the LSDA.
Anton Korobeynikov61d167e2011-01-14 21:57:45 +0000448 MCSymbol *GCCETSym =
Chris Lattnereec9bf12010-03-10 02:48:06 +0000449 Asm->OutContext.GetOrCreateSymbol(Twine("GCC_except_table")+
Chris Lattnerfd795022010-04-05 00:26:50 +0000450 Twine(Asm->getFunctionNumber()));
Chris Lattnereec9bf12010-03-10 02:48:06 +0000451 Asm->OutStreamer.EmitLabel(GCCETSym);
Chris Lattnerfd795022010-04-05 00:26:50 +0000452 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("exception",
453 Asm->getFunctionNumber()));
Bill Wendling243ac562009-09-10 02:07:37 +0000454
Chris Lattnereec9bf12010-03-10 02:48:06 +0000455 if (IsSJLJ)
Chris Lattnera179b522010-04-04 19:25:43 +0000456 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("_LSDA_",
457 Asm->getFunctionNumber()));
Bill Wendling243ac562009-09-10 02:07:37 +0000458
Bill Wendling88fdcd32010-02-24 23:34:35 +0000459 // Emit the LSDA header.
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000460 Asm->EmitEncodingByte(dwarf::DW_EH_PE_omit, "@LPStart");
461 Asm->EmitEncodingByte(TTypeEncoding, "@TType");
Bill Wendling243ac562009-09-10 02:07:37 +0000462
Bill Wendling88fdcd32010-02-24 23:34:35 +0000463 // The type infos need to be aligned. GCC does this by inserting padding just
464 // before the type infos. However, this changes the size of the exception
465 // table, so you need to take this into account when you output the exception
466 // table size. However, the size is output using a variable length encoding.
467 // So by increasing the size by inserting padding, you may increase the number
468 // of bytes used for writing the size. If it increases, say by one byte, then
469 // you now need to output one less byte of padding to get the type infos
470 // aligned. However this decreases the size of the exception table. This
471 // changes the value you have to output for the exception table size. Due to
472 // the variable length encoding, the number of bytes used for writing the
473 // length may decrease. If so, you then have to increase the amount of
474 // padding. And so on. If you look carefully at the GCC code you will see that
475 // it indeed does this in a loop, going on and on until the values stabilize.
476 // We chose another solution: don't output padding inside the table like GCC
477 // does, instead output it before the table.
478 unsigned SizeTypes = TypeInfos.size() * TypeFormatSize;
Logan Chien5b776b72014-02-22 14:00:39 +0000479 unsigned CallSiteTableLengthSize = getULEB128Size(CallSiteTableLength);
Bill Wendling88fdcd32010-02-24 23:34:35 +0000480 unsigned TTypeBaseOffset =
481 sizeof(int8_t) + // Call site format
482 CallSiteTableLengthSize + // Call site table length size
483 CallSiteTableLength + // Call site table length
484 SizeActions + // Actions size
485 SizeTypes;
Logan Chien5b776b72014-02-22 14:00:39 +0000486 unsigned TTypeBaseOffsetSize = getULEB128Size(TTypeBaseOffset);
Bill Wendling88fdcd32010-02-24 23:34:35 +0000487 unsigned TotalSize =
488 sizeof(int8_t) + // LPStart format
489 sizeof(int8_t) + // TType format
490 (HaveTTData ? TTypeBaseOffsetSize : 0) + // TType base offset size
491 TTypeBaseOffset; // TType base offset
492 unsigned SizeAlign = (4 - TotalSize) & 3;
493
Bill Wendling24c74f12010-02-25 23:52:44 +0000494 if (HaveTTData) {
Bill Wendlingfea6c4e2010-02-26 21:31:01 +0000495 // Account for any extra padding that will be added to the call site table
Bill Wendling60aa4df2010-02-25 21:19:47 +0000496 // length.
Chris Lattner9efd1182010-04-04 19:09:29 +0000497 Asm->EmitULEB128(TTypeBaseOffset, "@TType base offset", SizeAlign);
Bill Wendlingc3a93762010-02-26 22:17:52 +0000498 SizeAlign = 0;
Bill Wendling24c74f12010-02-25 23:52:44 +0000499 }
Bill Wendling15349f82009-07-28 22:23:45 +0000500
Bill Wendlinga8339eb2011-06-21 22:30:20 +0000501 bool VerboseAsm = Asm->OutStreamer.isVerboseAsm();
502
Bill Wendling023ed642009-09-10 06:50:01 +0000503 // SjLj Exception handling
Bill Wendlinga482ec82009-09-10 00:17:04 +0000504 if (IsSJLJ) {
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000505 Asm->EmitEncodingByte(dwarf::DW_EH_PE_udata4, "Call site");
Bill Wendlingc3a93762010-02-26 22:17:52 +0000506
507 // Add extra padding if it wasn't added to the TType base offset.
Chris Lattner9efd1182010-04-04 19:09:29 +0000508 Asm->EmitULEB128(CallSiteTableLength, "Call site table length", SizeAlign);
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000509
Jim Grosbach693e36a2009-08-11 00:09:57 +0000510 // Emit the landing pad site information.
Jim Grosbach486be662009-08-17 16:41:22 +0000511 unsigned idx = 0;
512 for (SmallVectorImpl<CallSiteEntry>::const_iterator
513 I = CallSites.begin(), E = CallSites.end(); I != E; ++I, ++idx) {
514 const CallSiteEntry &S = *I;
Bill Wendling0e749fe2009-08-20 22:02:24 +0000515
Renato Golinc8d40652011-08-18 23:43:14 +0000516 // Offset of the landing pad, counted in 16-byte bundles relative to the
517 // @LPStart address.
Bill Wendlingddec6832011-06-21 22:40:24 +0000518 if (VerboseAsm) {
Benjamin Kramercb6b02a2011-10-16 15:46:29 +0000519 Asm->OutStreamer.AddComment(">> Call Site " + Twine(idx) + " <<");
520 Asm->OutStreamer.AddComment(" On exception at call site "+Twine(idx));
Bill Wendlingddec6832011-06-21 22:40:24 +0000521 }
Bill Wendlingddec6832011-06-21 22:40:24 +0000522 Asm->EmitULEB128(idx);
Bill Wendling0e749fe2009-08-20 22:02:24 +0000523
524 // Offset of the first associated action record, relative to the start of
525 // the action table. This value is biased by 1 (1 indicates the start of
526 // the action table), and 0 indicates that there are no actions.
Renato Golinc8d40652011-08-18 23:43:14 +0000527 if (VerboseAsm) {
528 if (S.Action == 0)
529 Asm->OutStreamer.AddComment(" Action: cleanup");
530 else
Benjamin Kramercb6b02a2011-10-16 15:46:29 +0000531 Asm->OutStreamer.AddComment(" Action: " +
532 Twine((S.Action - 1) / 2 + 1));
Renato Golinc8d40652011-08-18 23:43:14 +0000533 }
Bill Wendlingddec6832011-06-21 22:40:24 +0000534 Asm->EmitULEB128(S.Action);
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000535 }
Jim Grosbach693e36a2009-08-11 00:09:57 +0000536 } else {
537 // DWARF Exception handling
Anton Korobeynikov9be547c2011-01-14 21:58:08 +0000538 assert(Asm->MAI->isExceptionHandlingDwarf());
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000539
Bill Wendling0e749fe2009-08-20 22:02:24 +0000540 // The call-site table is a list of all call sites that may throw an
541 // exception (including C++ 'throw' statements) in the procedure
542 // fragment. It immediately follows the LSDA header. Each entry indicates,
543 // for a given call, the first corresponding action record and corresponding
544 // landing pad.
545 //
546 // The table begins with the number of bytes, stored as an LEB128
547 // compressed, unsigned integer. The records immediately follow the record
548 // count. They are sorted in increasing call-site address. Each record
549 // indicates:
550 //
551 // * The position of the call-site.
552 // * The position of the landing pad.
553 // * The first action record for that call site.
554 //
555 // A missing entry in the call-site table indicates that a call is not
Bill Wendling023ed642009-09-10 06:50:01 +0000556 // supposed to throw.
Bill Wendling0e749fe2009-08-20 22:02:24 +0000557
558 // Emit the landing pad call site table.
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000559 Asm->EmitEncodingByte(dwarf::DW_EH_PE_udata4, "Call site");
Bill Wendlingc3a93762010-02-26 22:17:52 +0000560
561 // Add extra padding if it wasn't added to the TType base offset.
Chris Lattner9efd1182010-04-04 19:09:29 +0000562 Asm->EmitULEB128(CallSiteTableLength, "Call site table length", SizeAlign);
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000563
Bill Wendlinga8339eb2011-06-21 22:30:20 +0000564 unsigned Entry = 0;
Jim Grosbach693e36a2009-08-11 00:09:57 +0000565 for (SmallVectorImpl<CallSiteEntry>::const_iterator
566 I = CallSites.begin(), E = CallSites.end(); I != E; ++I) {
567 const CallSiteEntry &S = *I;
Anton Korobeynikov61d167e2011-01-14 21:57:45 +0000568
Chris Lattnera179b522010-04-04 19:25:43 +0000569 MCSymbol *EHFuncBeginSym =
Chris Lattnerfd795022010-04-05 00:26:50 +0000570 Asm->GetTempSymbol("eh_func_begin", Asm->getFunctionNumber());
Anton Korobeynikov61d167e2011-01-14 21:57:45 +0000571
Chris Lattner34adc8d2010-03-14 01:41:15 +0000572 MCSymbol *BeginLabel = S.BeginLabel;
Craig Topper353eda42014-04-24 06:44:33 +0000573 if (!BeginLabel)
Chris Lattner34adc8d2010-03-14 01:41:15 +0000574 BeginLabel = EHFuncBeginSym;
575 MCSymbol *EndLabel = S.EndLabel;
Craig Topper353eda42014-04-24 06:44:33 +0000576 if (!EndLabel)
Chris Lattnerfd795022010-04-05 00:26:50 +0000577 EndLabel = Asm->GetTempSymbol("eh_func_end", Asm->getFunctionNumber());
Anton Korobeynikov61d167e2011-01-14 21:57:45 +0000578
Bill Wendlinga8339eb2011-06-21 22:30:20 +0000579
Bill Wendling0e749fe2009-08-20 22:02:24 +0000580 // Offset of the call site relative to the previous call site, counted in
581 // number of 16-byte bundles. The first call site is counted relative to
582 // the start of the procedure fragment.
Renato Golinc8d40652011-08-18 23:43:14 +0000583 if (VerboseAsm)
Benjamin Kramercb6b02a2011-10-16 15:46:29 +0000584 Asm->OutStreamer.AddComment(">> Call Site " + Twine(++Entry) + " <<");
Chris Lattnerc7cc8152010-04-04 21:31:54 +0000585 Asm->EmitLabelDifference(BeginLabel, EHFuncBeginSym, 4);
Renato Golinc8d40652011-08-18 23:43:14 +0000586 if (VerboseAsm)
587 Asm->OutStreamer.AddComment(Twine(" Call between ") +
588 BeginLabel->getName() + " and " +
589 EndLabel->getName());
Chris Lattnerf1429f12010-04-04 19:58:12 +0000590 Asm->EmitLabelDifference(EndLabel, BeginLabel, 4);
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000591
Bill Wendling0e749fe2009-08-20 22:02:24 +0000592 // Offset of the landing pad, counted in 16-byte bundles relative to the
593 // @LPStart address.
Renato Golinc8d40652011-08-18 23:43:14 +0000594 if (!S.PadLabel) {
595 if (VerboseAsm)
596 Asm->OutStreamer.AddComment(" has no landing pad");
Eric Christopherce0cfce2013-01-09 01:35:34 +0000597 Asm->OutStreamer.EmitIntValue(0, 4/*size*/);
Renato Golinc8d40652011-08-18 23:43:14 +0000598 } else {
599 if (VerboseAsm)
600 Asm->OutStreamer.AddComment(Twine(" jumps to ") +
601 S.PadLabel->getName());
Chris Lattnerc7cc8152010-04-04 21:31:54 +0000602 Asm->EmitLabelDifference(S.PadLabel, EHFuncBeginSym, 4);
Renato Golinc8d40652011-08-18 23:43:14 +0000603 }
Jim Grosbach693e36a2009-08-11 00:09:57 +0000604
Bill Wendling0e749fe2009-08-20 22:02:24 +0000605 // Offset of the first associated action record, relative to the start of
606 // the action table. This value is biased by 1 (1 indicates the start of
607 // the action table), and 0 indicates that there are no actions.
Renato Golinc8d40652011-08-18 23:43:14 +0000608 if (VerboseAsm) {
609 if (S.Action == 0)
610 Asm->OutStreamer.AddComment(" On action: cleanup");
611 else
Benjamin Kramercb6b02a2011-10-16 15:46:29 +0000612 Asm->OutStreamer.AddComment(" On action: " +
613 Twine((S.Action - 1) / 2 + 1));
Renato Golinc8d40652011-08-18 23:43:14 +0000614 }
Bill Wendlinga8339eb2011-06-21 22:30:20 +0000615 Asm->EmitULEB128(S.Action);
Jim Grosbach693e36a2009-08-11 00:09:57 +0000616 }
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000617 }
618
Bill Wendling0e749fe2009-08-20 22:02:24 +0000619 // Emit the Action Table.
Bill Wendlinga8339eb2011-06-21 22:30:20 +0000620 int Entry = 0;
Bill Wendling23b177e2009-07-28 23:44:43 +0000621 for (SmallVectorImpl<ActionEntry>::const_iterator
622 I = Actions.begin(), E = Actions.end(); I != E; ++I) {
623 const ActionEntry &Action = *I;
Bill Wendlinga8339eb2011-06-21 22:30:20 +0000624
625 if (VerboseAsm) {
626 // Emit comments that decode the action table.
Benjamin Kramercb6b02a2011-10-16 15:46:29 +0000627 Asm->OutStreamer.AddComment(">> Action Record " + Twine(++Entry) + " <<");
Renato Golinc8d40652011-08-18 23:43:14 +0000628 }
629
630 // Type Filter
631 //
632 // Used by the runtime to match the type of the thrown exception to the
633 // type of the catch clauses or the types in the exception specification.
634 if (VerboseAsm) {
Duncan Sands2e679372011-09-28 09:13:02 +0000635 if (Action.ValueForTypeID > 0)
Benjamin Kramercb6b02a2011-10-16 15:46:29 +0000636 Asm->OutStreamer.AddComment(" Catch TypeInfo " +
637 Twine(Action.ValueForTypeID));
Duncan Sands2e679372011-09-28 09:13:02 +0000638 else if (Action.ValueForTypeID < 0)
Benjamin Kramercb6b02a2011-10-16 15:46:29 +0000639 Asm->OutStreamer.AddComment(" Filter TypeInfo " +
640 Twine(Action.ValueForTypeID));
Duncan Sands2e679372011-09-28 09:13:02 +0000641 else
642 Asm->OutStreamer.AddComment(" Cleanup");
Renato Golinc8d40652011-08-18 23:43:14 +0000643 }
644 Asm->EmitSLEB128(Action.ValueForTypeID);
Bill Wendlinga8339eb2011-06-21 22:30:20 +0000645
Renato Golinc8d40652011-08-18 23:43:14 +0000646 // Action Record
647 //
648 // Self-relative signed displacement in bytes of the next action record,
649 // or 0 if there is no next action record.
650 if (VerboseAsm) {
Bill Wendlinga8339eb2011-06-21 22:30:20 +0000651 if (Action.NextAction == 0) {
652 Asm->OutStreamer.AddComment(" No further actions");
653 } else {
654 unsigned NextAction = Entry + (Action.NextAction + 1) / 2;
Benjamin Kramercb6b02a2011-10-16 15:46:29 +0000655 Asm->OutStreamer.AddComment(" Continue to action "+Twine(NextAction));
Bill Wendlinga8339eb2011-06-21 22:30:20 +0000656 }
Bill Wendlinga8339eb2011-06-21 22:30:20 +0000657 }
Bill Wendlinga8339eb2011-06-21 22:30:20 +0000658 Asm->EmitSLEB128(Action.NextAction);
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000659 }
660
Anton Korobeynikovf65a6382012-11-19 21:06:26 +0000661 EmitTypeInfos(TTypeEncoding);
662
663 Asm->EmitAlignment(2);
664}
665
666void DwarfException::EmitTypeInfos(unsigned TTypeEncoding) {
667 const std::vector<const GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
668 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
669
670 bool VerboseAsm = Asm->OutStreamer.isVerboseAsm();
671
672 int Entry = 0;
Bill Wendling4f0b8d72009-10-22 20:48:59 +0000673 // Emit the Catch TypeInfos.
Bill Wendlinga8339eb2011-06-21 22:30:20 +0000674 if (VerboseAsm && !TypeInfos.empty()) {
675 Asm->OutStreamer.AddComment(">> Catch TypeInfos <<");
Chris Lattner566cae92010-03-09 23:52:58 +0000676 Asm->OutStreamer.AddBlankLine();
Bill Wendlinga8339eb2011-06-21 22:30:20 +0000677 Entry = TypeInfos.size();
Chris Lattner566cae92010-03-09 23:52:58 +0000678 }
Bill Wendlinga8339eb2011-06-21 22:30:20 +0000679
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000680 for (std::vector<const GlobalVariable *>::const_reverse_iterator
Bill Wendling4b384b02009-11-19 00:09:14 +0000681 I = TypeInfos.rbegin(), E = TypeInfos.rend(); I != E; ++I) {
Bill Wendling31c74db2009-11-19 19:21:09 +0000682 const GlobalVariable *GV = *I;
Bill Wendlinga8339eb2011-06-21 22:30:20 +0000683 if (VerboseAsm)
Benjamin Kramercb6b02a2011-10-16 15:46:29 +0000684 Asm->OutStreamer.AddComment("TypeInfo " + Twine(Entry--));
Anton Korobeynikov097b0e92012-11-19 21:17:20 +0000685 Asm->EmitTTypeReference(GV, TTypeEncoding);
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000686 }
687
Bill Wendling4f0b8d72009-10-22 20:48:59 +0000688 // Emit the Exception Specifications.
Bill Wendlinga8339eb2011-06-21 22:30:20 +0000689 if (VerboseAsm && !FilterIds.empty()) {
690 Asm->OutStreamer.AddComment(">> Filter TypeInfos <<");
Chris Lattner566cae92010-03-09 23:52:58 +0000691 Asm->OutStreamer.AddBlankLine();
Bill Wendlinga8339eb2011-06-21 22:30:20 +0000692 Entry = 0;
Chris Lattner566cae92010-03-09 23:52:58 +0000693 }
Bill Wendling23b177e2009-07-28 23:44:43 +0000694 for (std::vector<unsigned>::const_iterator
695 I = FilterIds.begin(), E = FilterIds.end(); I < E; ++I) {
696 unsigned TypeID = *I;
Bill Wendlinga8339eb2011-06-21 22:30:20 +0000697 if (VerboseAsm) {
698 --Entry;
699 if (TypeID != 0)
Benjamin Kramercb6b02a2011-10-16 15:46:29 +0000700 Asm->OutStreamer.AddComment("FilterInfo " + Twine(Entry));
Bill Wendlinga8339eb2011-06-21 22:30:20 +0000701 }
702
703 Asm->EmitULEB128(TypeID);
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000704 }
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000705}
706
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +0000707/// endModule - Emit all exception information that should come after the
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000708/// content.
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +0000709void DwarfException::endModule() {
Craig Topperee4dab52012-02-05 08:31:47 +0000710 llvm_unreachable("Should be implemented");
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000711}
712
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +0000713/// beginFunction - Gather pre-function exception information. Assumes it's
Bill Wendling023ed642009-09-10 06:50:01 +0000714/// being emitted immediately after the function entry point.
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +0000715void DwarfException::beginFunction(const MachineFunction *MF) {
Craig Topperee4dab52012-02-05 08:31:47 +0000716 llvm_unreachable("Should be implemented");
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000717}
718
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +0000719/// endFunction - Gather and emit post-function exception information.
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +0000720void DwarfException::endFunction(const MachineFunction *) {
Craig Topperee4dab52012-02-05 08:31:47 +0000721 llvm_unreachable("Should be implemented");
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000722}