blob: 42895300fc685ebe1b12dfaf1150595dee2cace7 [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
61/// PadLT - Order landing pads lexicographically by type id.
62bool DwarfException::PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
63 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
64 unsigned LSize = LIds.size(), RSize = RIds.size();
65 unsigned MinSize = LSize < RSize ? LSize : RSize;
66
67 for (unsigned i = 0; i != MinSize; ++i)
68 if (LIds[i] != RIds[i])
69 return LIds[i] < RIds[i];
70
71 return LSize < RSize;
72}
73
Bill Wendling6c574d82009-07-28 23:23:00 +000074/// ComputeActionsTable - Compute the actions table and gather the first action
75/// index for each landing pad site.
Bill Wendlingb67440e2009-07-29 00:31:35 +000076unsigned DwarfException::
77ComputeActionsTable(const SmallVectorImpl<const LandingPadInfo*> &LandingPads,
78 SmallVectorImpl<ActionEntry> &Actions,
79 SmallVectorImpl<unsigned> &FirstActions) {
Bill Wendling0e749fe2009-08-20 22:02:24 +000080
81 // The action table follows the call-site table in the LSDA. The individual
82 // records are of two types:
83 //
84 // * Catch clause
85 // * Exception specification
86 //
87 // The two record kinds have the same format, with only small differences.
88 // They are distinguished by the "switch value" field: Catch clauses
89 // (TypeInfos) have strictly positive switch values, and exception
90 // specifications (FilterIds) have strictly negative switch values. Value 0
91 // indicates a catch-all clause.
92 //
Bill Wendling1fddd872009-07-28 23:22:13 +000093 // Negative type IDs index into FilterIds. Positive type IDs index into
94 // TypeInfos. The value written for a positive type ID is just the type ID
95 // itself. For a negative type ID, however, the value written is the
Bill Wendlingd64cd2b2009-05-15 01:12:28 +000096 // (negative) byte offset of the corresponding FilterIds entry. The byte
Bill Wendling1fddd872009-07-28 23:22:13 +000097 // offset is usually equal to the type ID (because the FilterIds entries are
98 // written using a variable width encoding, which outputs one byte per entry
99 // as long as the value written is not too large) but can differ. This kind
100 // of complication does not occur for positive type IDs because type infos are
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000101 // output using a fixed width encoding. FilterOffsets[i] holds the byte
102 // offset corresponding to FilterIds[i].
Bill Wendling45057342009-07-29 21:19:44 +0000103
104 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000105 SmallVector<int, 16> FilterOffsets;
106 FilterOffsets.reserve(FilterIds.size());
107 int Offset = -1;
Bill Wendling45057342009-07-29 21:19:44 +0000108
109 for (std::vector<unsigned>::const_iterator
110 I = FilterIds.begin(), E = FilterIds.end(); I != E; ++I) {
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000111 FilterOffsets.push_back(Offset);
Logan Chien5b776b72014-02-22 14:00:39 +0000112 Offset -= getULEB128Size(*I);
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000113 }
114
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000115 FirstActions.reserve(LandingPads.size());
116
117 int FirstAction = 0;
118 unsigned SizeActions = 0;
Bill Wendling1fddd872009-07-28 23:22:13 +0000119 const LandingPadInfo *PrevLPI = 0;
Bill Wendling45057342009-07-29 21:19:44 +0000120
Bill Wendling23b177e2009-07-28 23:44:43 +0000121 for (SmallVectorImpl<const LandingPadInfo *>::const_iterator
Bill Wendling1fddd872009-07-28 23:22:13 +0000122 I = LandingPads.begin(), E = LandingPads.end(); I != E; ++I) {
123 const LandingPadInfo *LPI = *I;
124 const std::vector<int> &TypeIds = LPI->TypeIds;
Chris Lattner07c1b942010-04-04 20:10:41 +0000125 unsigned NumShared = PrevLPI ? SharedTypeIds(LPI, PrevLPI) : 0;
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000126 unsigned SizeSiteActions = 0;
127
128 if (NumShared < TypeIds.size()) {
129 unsigned SizeAction = 0;
Bill Wendling7742b642010-02-10 21:41:57 +0000130 unsigned PrevAction = (unsigned)-1;
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000131
132 if (NumShared) {
Chris Lattner07c1b942010-04-04 20:10:41 +0000133 unsigned SizePrevIds = PrevLPI->TypeIds.size();
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000134 assert(Actions.size());
Bill Wendling7742b642010-02-10 21:41:57 +0000135 PrevAction = Actions.size() - 1;
Logan Chien5b776b72014-02-22 14:00:39 +0000136 SizeAction = getSLEB128Size(Actions[PrevAction].NextAction) +
137 getSLEB128Size(Actions[PrevAction].ValueForTypeID);
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000138
139 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
Bill Wendling7742b642010-02-10 21:41:57 +0000140 assert(PrevAction != (unsigned)-1 && "PrevAction is invalid!");
Logan Chien5b776b72014-02-22 14:00:39 +0000141 SizeAction -= getSLEB128Size(Actions[PrevAction].ValueForTypeID);
Bill Wendling7742b642010-02-10 21:41:57 +0000142 SizeAction += -Actions[PrevAction].NextAction;
143 PrevAction = Actions[PrevAction].Previous;
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000144 }
145 }
146
147 // Compute the actions.
Bill Wendling1fddd872009-07-28 23:22:13 +0000148 for (unsigned J = NumShared, M = TypeIds.size(); J != M; ++J) {
149 int TypeID = TypeIds[J];
150 assert(-1 - TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000151 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
Logan Chien5b776b72014-02-22 14:00:39 +0000152 unsigned SizeTypeID = getSLEB128Size(ValueForTypeID);
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000153
154 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
Logan Chien5b776b72014-02-22 14:00:39 +0000155 SizeAction = SizeTypeID + getSLEB128Size(NextAction);
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000156 SizeSiteActions += SizeAction;
157
Bill Wendling0e749fe2009-08-20 22:02:24 +0000158 ActionEntry Action = { ValueForTypeID, NextAction, PrevAction };
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000159 Actions.push_back(Action);
Bill Wendling7742b642010-02-10 21:41:57 +0000160 PrevAction = Actions.size() - 1;
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000161 }
162
163 // Record the first action of the landing pad site.
164 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
165 } // else identical - re-use previous FirstAction
166
Bill Wendling0e749fe2009-08-20 22:02:24 +0000167 // Information used when created the call-site table. The action record
168 // field of the call site record is the offset of the first associated
169 // action record, relative to the start of the actions table. This value is
Bill Wendling7742b642010-02-10 21:41:57 +0000170 // biased by 1 (1 indicating the start of the actions table), and 0
Bill Wendling0e749fe2009-08-20 22:02:24 +0000171 // indicates that there are no actions.
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000172 FirstActions.push_back(FirstAction);
173
174 // Compute this sites contribution to size.
175 SizeActions += SizeSiteActions;
Bill Wendling1fddd872009-07-28 23:22:13 +0000176
177 PrevLPI = LPI;
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000178 }
179
Bill Wendling1fddd872009-07-28 23:22:13 +0000180 return SizeActions;
181}
182
Bill Wendlinge4120642009-11-12 21:59:20 +0000183/// CallToNoUnwindFunction - Return `true' if this is a call to a function
184/// marked `nounwind'. Return `false' otherwise.
185bool DwarfException::CallToNoUnwindFunction(const MachineInstr *MI) {
Evan Cheng7f8e5632011-12-07 07:15:52 +0000186 assert(MI->isCall() && "This should be a call instruction!");
Bill Wendlinge4120642009-11-12 21:59:20 +0000187
188 bool MarkedNoUnwind = false;
189 bool SawFunc = false;
190
191 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
192 const MachineOperand &MO = MI->getOperand(I);
193
Chris Lattner17d38592010-03-31 06:09:04 +0000194 if (!MO.isGlobal()) continue;
Anton Korobeynikov61d167e2011-01-14 21:57:45 +0000195
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000196 const Function *F = dyn_cast<Function>(MO.getGlobal());
Chris Lattner17d38592010-03-31 06:09:04 +0000197 if (F == 0) continue;
Bill Wendlingc781d7a2009-11-12 23:13:08 +0000198
Chris Lattner17d38592010-03-31 06:09:04 +0000199 if (SawFunc) {
200 // Be conservative. If we have more than one function operand for this
201 // call, then we can't make the assumption that it's the callee and
202 // not a parameter to the call.
Anton Korobeynikov61d167e2011-01-14 21:57:45 +0000203 //
Chris Lattner17d38592010-03-31 06:09:04 +0000204 // FIXME: Determine if there's a way to say that `F' is the callee or
205 // parameter.
206 MarkedNoUnwind = false;
207 break;
Bill Wendlinge4120642009-11-12 21:59:20 +0000208 }
Chris Lattner17d38592010-03-31 06:09:04 +0000209
210 MarkedNoUnwind = F->doesNotThrow();
211 SawFunc = true;
Bill Wendlinge4120642009-11-12 21:59:20 +0000212 }
213
214 return MarkedNoUnwind;
215}
216
Bill Wendlingb67440e2009-07-29 00:31:35 +0000217/// ComputeCallSiteTable - Compute the call-site table. The entry for an invoke
Bill Wendling0e749fe2009-08-20 22:02:24 +0000218/// has a try-range containing the call, a non-zero landing pad, and an
Bill Wendlingb67440e2009-07-29 00:31:35 +0000219/// appropriate action. The entry for an ordinary call has a try-range
220/// containing the call and zero for the landing pad and the action. Calls
221/// marked 'nounwind' have no entry and must not be contained in the try-range
222/// of any entry - they form gaps in the table. Entries must be ordered by
223/// try-range address.
224void DwarfException::
225ComputeCallSiteTable(SmallVectorImpl<CallSiteEntry> &CallSites,
226 const RangeMapType &PadMap,
227 const SmallVectorImpl<const LandingPadInfo *> &LandingPads,
228 const SmallVectorImpl<unsigned> &FirstActions) {
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000229 // The end label of the previous invoke or nounwind try-range.
Chris Lattner34adc8d2010-03-14 01:41:15 +0000230 MCSymbol *LastLabel = 0;
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000231
232 // Whether there is a potentially throwing instruction (currently this means
233 // an ordinary call) between the end of the previous try-range and now.
234 bool SawPotentiallyThrowing = false;
235
Bill Wendling23b177e2009-07-28 23:44:43 +0000236 // Whether the last CallSite entry was for an invoke.
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000237 bool PreviousIsInvoke = false;
238
239 // Visit all instructions in order of address.
Chris Lattnerfd795022010-04-05 00:26:50 +0000240 for (MachineFunction::const_iterator I = Asm->MF->begin(), E = Asm->MF->end();
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000241 I != E; ++I) {
242 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
243 MI != E; ++MI) {
Rafael Espindola736bec82014-03-06 16:31:40 +0000244 if (!MI->isEHLabel()) {
Evan Cheng7f8e5632011-12-07 07:15:52 +0000245 if (MI->isCall())
Bill Wendlinge4120642009-11-12 21:59:20 +0000246 SawPotentiallyThrowing |= !CallToNoUnwindFunction(MI);
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000247 continue;
248 }
249
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000250 // End of the previous try-range?
Chris Lattnerff1fdd62010-03-14 08:17:53 +0000251 MCSymbol *BeginLabel = MI->getOperand(0).getMCSymbol();
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000252 if (BeginLabel == LastLabel)
253 SawPotentiallyThrowing = false;
254
255 // Beginning of a new try-range?
Jeffrey Yasskinb40d3f72009-11-10 01:02:17 +0000256 RangeMapType::const_iterator L = PadMap.find(BeginLabel);
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000257 if (L == PadMap.end())
258 // Nope, it was just some random label.
259 continue;
260
Bill Wendling0e749fe2009-08-20 22:02:24 +0000261 const PadRange &P = L->second;
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000262 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000263 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
264 "Inconsistent landing pad map!");
265
Bill Wendling0e749fe2009-08-20 22:02:24 +0000266 // For Dwarf exception handling (SjLj handling doesn't use this). If some
267 // instruction between the previous try-range and this one may throw,
268 // create a call-site entry with no landing pad for the region between the
269 // try-ranges.
Anton Korobeynikov9be547c2011-01-14 21:58:08 +0000270 if (SawPotentiallyThrowing && Asm->MAI->isExceptionHandlingDwarf()) {
Bill Wendling0e749fe2009-08-20 22:02:24 +0000271 CallSiteEntry Site = { LastLabel, BeginLabel, 0, 0 };
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000272 CallSites.push_back(Site);
273 PreviousIsInvoke = false;
274 }
275
276 LastLabel = LandingPad->EndLabels[P.RangeIndex];
277 assert(BeginLabel && LastLabel && "Invalid landing pad!");
278
Chris Lattner17d38592010-03-31 06:09:04 +0000279 if (!LandingPad->LandingPadLabel) {
280 // Create a gap.
281 PreviousIsInvoke = false;
282 } else {
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000283 // This try-range is for an invoke.
Bill Wendling0e749fe2009-08-20 22:02:24 +0000284 CallSiteEntry Site = {
285 BeginLabel,
286 LastLabel,
287 LandingPad->LandingPadLabel,
288 FirstActions[P.PadIndex]
289 };
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000290
Jim Grosbach5afbf2b2009-09-01 17:19:13 +0000291 // Try to merge with the previous call-site. SJLJ doesn't do this
Anton Korobeynikov9be547c2011-01-14 21:58:08 +0000292 if (PreviousIsInvoke && Asm->MAI->isExceptionHandlingDwarf()) {
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000293 CallSiteEntry &Prev = CallSites.back();
294 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
295 // Extend the range of the previous entry.
296 Prev.EndLabel = Site.EndLabel;
297 continue;
298 }
299 }
300
301 // Otherwise, create a new call-site.
Anton Korobeynikov9be547c2011-01-14 21:58:08 +0000302 if (Asm->MAI->isExceptionHandlingDwarf())
Jim Grosbach54c05302010-01-28 01:45:32 +0000303 CallSites.push_back(Site);
304 else {
305 // SjLj EH must maintain the call sites in the order assigned
306 // to them by the SjLjPrepare pass.
307 unsigned SiteNo = MMI->getCallSiteBeginLabel(BeginLabel);
308 if (CallSites.size() < SiteNo)
309 CallSites.resize(SiteNo);
310 CallSites[SiteNo - 1] = Site;
311 }
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000312 PreviousIsInvoke = true;
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000313 }
314 }
315 }
316
317 // If some instruction between the previous try-range and the end of the
318 // function may throw, create a call-site entry with no landing pad for the
319 // region following the try-range.
Anton Korobeynikov9be547c2011-01-14 21:58:08 +0000320 if (SawPotentiallyThrowing && Asm->MAI->isExceptionHandlingDwarf()) {
Bill Wendling0e749fe2009-08-20 22:02:24 +0000321 CallSiteEntry Site = { LastLabel, 0, 0, 0 };
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000322 CallSites.push_back(Site);
323 }
Bill Wendlingb67440e2009-07-29 00:31:35 +0000324}
325
Bill Wendling2cf00142009-07-29 00:50:05 +0000326/// EmitExceptionTable - Emit landing pads and actions.
327///
328/// The general organization of the table is complex, but the basic concepts are
329/// easy. First there is a header which describes the location and organization
330/// of the three components that follow.
Eric Christopherd517ac02009-08-28 22:33:43 +0000331///
Bill Wendling2cf00142009-07-29 00:50:05 +0000332/// 1. The landing pad site information describes the range of code covered by
333/// the try. In our case it's an accumulation of the ranges covered by the
334/// invokes in the try. There is also a reference to the landing pad that
335/// handles the exception once processed. Finally an index into the actions
336/// table.
Bill Wendling0e749fe2009-08-20 22:02:24 +0000337/// 2. The action table, in our case, is composed of pairs of type IDs and next
Bill Wendling2cf00142009-07-29 00:50:05 +0000338/// action offset. Starting with the action index from the landing pad
Bill Wendling0e749fe2009-08-20 22:02:24 +0000339/// site, each type ID is checked for a match to the current exception. If
Bill Wendling2cf00142009-07-29 00:50:05 +0000340/// it matches then the exception and type id are passed on to the landing
341/// pad. Otherwise the next action is looked up. This chain is terminated
Bill Wendling023ed642009-09-10 06:50:01 +0000342/// with a next action of zero. If no type id is found then the frame is
Bill Wendling2cf00142009-07-29 00:50:05 +0000343/// unwound and handling continues.
Bill Wendling0e749fe2009-08-20 22:02:24 +0000344/// 3. Type ID table contains references to all the C++ typeinfo for all
Bill Wendling023ed642009-09-10 06:50:01 +0000345/// catches in the function. This tables is reverse indexed base 1.
Bill Wendlingb67440e2009-07-29 00:31:35 +0000346void DwarfException::EmitExceptionTable() {
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000347 const std::vector<const GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
Bill Wendlingb67440e2009-07-29 00:31:35 +0000348 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
349 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
Bill Wendlingb67440e2009-07-29 00:31:35 +0000350
351 // Sort the landing pads in order of their type ids. This is used to fold
352 // duplicate actions.
353 SmallVector<const LandingPadInfo *, 64> LandingPads;
354 LandingPads.reserve(PadInfos.size());
355
356 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
357 LandingPads.push_back(&PadInfos[i]);
358
359 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
360
361 // Compute the actions table and gather the first action index for each
362 // landing pad site.
363 SmallVector<ActionEntry, 32> Actions;
364 SmallVector<unsigned, 64> FirstActions;
Bill Wendling7742b642010-02-10 21:41:57 +0000365 unsigned SizeActions=ComputeActionsTable(LandingPads, Actions, FirstActions);
Bill Wendlingb67440e2009-07-29 00:31:35 +0000366
367 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
368 // by try-range labels when lowered). Ordinary calls do not, so appropriate
Bill Wendling023ed642009-09-10 06:50:01 +0000369 // try-ranges for them need be deduced when using DWARF exception handling.
Bill Wendlingb67440e2009-07-29 00:31:35 +0000370 RangeMapType PadMap;
371 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
372 const LandingPadInfo *LandingPad = LandingPads[i];
373 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
Chris Lattner34adc8d2010-03-14 01:41:15 +0000374 MCSymbol *BeginLabel = LandingPad->BeginLabels[j];
Bill Wendlingb67440e2009-07-29 00:31:35 +0000375 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
376 PadRange P = { i, j };
377 PadMap[BeginLabel] = P;
378 }
379 }
380
381 // Compute the call-site table.
382 SmallVector<CallSiteEntry, 64> CallSites;
Jim Grosbach486be662009-08-17 16:41:22 +0000383 ComputeCallSiteTable(CallSites, PadMap, LandingPads, FirstActions);
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000384
385 // Final tallies.
386
387 // Call sites.
Chris Lattnerfd795022010-04-05 00:26:50 +0000388 bool IsSJLJ = Asm->MAI->getExceptionHandlingType() == ExceptionHandling::SjLj;
Bill Wendlinga482ec82009-09-10 00:17:04 +0000389 bool HaveTTData = IsSJLJ ? (!TypeInfos.empty() || !FilterIds.empty()) : true;
Anton Korobeynikov61d167e2011-01-14 21:57:45 +0000390
Bill Wendling88fdcd32010-02-24 23:34:35 +0000391 unsigned CallSiteTableLength;
Bill Wendlinga482ec82009-09-10 00:17:04 +0000392 if (IsSJLJ)
Bill Wendling88fdcd32010-02-24 23:34:35 +0000393 CallSiteTableLength = 0;
Chris Lattner07c1b942010-04-04 20:10:41 +0000394 else {
395 unsigned SiteStartSize = 4; // dwarf::DW_EH_PE_udata4
396 unsigned SiteLengthSize = 4; // dwarf::DW_EH_PE_udata4
397 unsigned LandingPadSize = 4; // dwarf::DW_EH_PE_udata4
Anton Korobeynikov61d167e2011-01-14 21:57:45 +0000398 CallSiteTableLength =
Chris Lattner07c1b942010-04-04 20:10:41 +0000399 CallSites.size() * (SiteStartSize + SiteLengthSize + LandingPadSize);
400 }
Bill Wendlinga482ec82009-09-10 00:17:04 +0000401
Jim Grosbach693e36a2009-08-11 00:09:57 +0000402 for (unsigned i = 0, e = CallSites.size(); i < e; ++i) {
Logan Chien5b776b72014-02-22 14:00:39 +0000403 CallSiteTableLength += getULEB128Size(CallSites[i].Action);
Bill Wendlinga482ec82009-09-10 00:17:04 +0000404 if (IsSJLJ)
Logan Chien5b776b72014-02-22 14:00:39 +0000405 CallSiteTableLength += getULEB128Size(i);
Jim Grosbach693e36a2009-08-11 00:09:57 +0000406 }
Bill Wendlinga482ec82009-09-10 00:17:04 +0000407
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000408 // Type infos.
Chris Lattnerc16c75e2009-08-02 01:34:32 +0000409 const MCSection *LSDASection = Asm->getObjFileLowering().getLSDASection();
Anton Korobeynikovae4ccc12010-02-15 22:35:59 +0000410 unsigned TTypeEncoding;
Bill Wendling9e5c2062009-09-10 06:27:16 +0000411 unsigned TypeFormatSize;
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000412
Bill Wendlingbf562682009-09-10 01:12:47 +0000413 if (!HaveTTData) {
Bill Wendling023ed642009-09-10 06:50:01 +0000414 // For SjLj exceptions, if there is no TypeInfo, then we just explicitly say
415 // that we're omitting that bit.
Anton Korobeynikovae4ccc12010-02-15 22:35:59 +0000416 TTypeEncoding = dwarf::DW_EH_PE_omit;
Chris Lattnerfd795022010-04-05 00:26:50 +0000417 // dwarf::DW_EH_PE_absptr
Chandler Carruth5da3f052012-11-01 09:14:31 +0000418 TypeFormatSize = Asm->getDataLayout().getPointerSize();
Chris Lattner47fc2352009-07-31 22:03:47 +0000419 } else {
Chris Lattner7005cd32009-08-02 03:59:56 +0000420 // Okay, we have actual filters or typeinfos to emit. As such, we need to
421 // pick a type encoding for them. We're about to emit a list of pointers to
422 // typeinfo objects at the end of the LSDA. However, unless we're in static
423 // mode, this reference will require a relocation by the dynamic linker.
Chris Lattnerf4b92a82009-07-31 22:18:14 +0000424 //
Chris Lattner7005cd32009-08-02 03:59:56 +0000425 // Because of this, we have a couple of options:
Anton Korobeynikov61d167e2011-01-14 21:57:45 +0000426 //
Chris Lattner7005cd32009-08-02 03:59:56 +0000427 // 1) If we are in -static mode, we can always use an absolute reference
428 // from the LSDA, because the static linker will resolve it.
Anton Korobeynikov61d167e2011-01-14 21:57:45 +0000429 //
Chris Lattner7005cd32009-08-02 03:59:56 +0000430 // 2) Otherwise, if the LSDA section is writable, we can output the direct
431 // reference to the typeinfo and allow the dynamic linker to relocate
432 // it. Since it is in a writable section, the dynamic linker won't
433 // have a problem.
Anton Korobeynikov61d167e2011-01-14 21:57:45 +0000434 //
Chris Lattner7005cd32009-08-02 03:59:56 +0000435 // 3) Finally, if we're in PIC mode and the LDSA section isn't writable,
436 // we need to use some form of indirection. For example, on Darwin,
437 // we can output a statically-relocatable reference to a dyld stub. The
438 // offset to the stub is constant, but the contents are in a section
439 // that is updated by the dynamic linker. This is easy enough, but we
440 // need to tell the personality function of the unwinder to indirect
441 // through the dyld stub.
442 //
Bill Wendlingbf562682009-09-10 01:12:47 +0000443 // FIXME: When (3) is actually implemented, we'll have to emit the stubs
Chris Lattner7005cd32009-08-02 03:59:56 +0000444 // somewhere. This predicate should be moved to a shared location that is
445 // in target-independent code.
446 //
Anton Korobeynikovae4ccc12010-02-15 22:35:59 +0000447 TTypeEncoding = Asm->getObjFileLowering().getTTypeEncoding();
Chris Lattnere619c0d2010-04-04 20:20:50 +0000448 TypeFormatSize = Asm->GetSizeOfEncodedValue(TTypeEncoding);
Bill Wendling243ac562009-09-10 02:07:37 +0000449 }
Bill Wendlingbf562682009-09-10 01:12:47 +0000450
Bill Wendling243ac562009-09-10 02:07:37 +0000451 // Begin the exception table.
Anton Korobeynikovfe3a6e02011-01-30 22:07:31 +0000452 // Sometimes we want not to emit the data into separate section (e.g. ARM
453 // EHABI). In this case LSDASection will be NULL.
Anton Korobeynikovb15beb22011-01-24 22:38:40 +0000454 if (LSDASection)
455 Asm->OutStreamer.SwitchSection(LSDASection);
Chris Lattner9e06e532010-04-28 01:05:45 +0000456 Asm->EmitAlignment(2);
Bill Wendlingbf562682009-09-10 01:12:47 +0000457
Bill Wendling88fdcd32010-02-24 23:34:35 +0000458 // Emit the LSDA.
Anton Korobeynikov61d167e2011-01-14 21:57:45 +0000459 MCSymbol *GCCETSym =
Chris Lattnereec9bf12010-03-10 02:48:06 +0000460 Asm->OutContext.GetOrCreateSymbol(Twine("GCC_except_table")+
Chris Lattnerfd795022010-04-05 00:26:50 +0000461 Twine(Asm->getFunctionNumber()));
Chris Lattnereec9bf12010-03-10 02:48:06 +0000462 Asm->OutStreamer.EmitLabel(GCCETSym);
Chris Lattnerfd795022010-04-05 00:26:50 +0000463 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("exception",
464 Asm->getFunctionNumber()));
Bill Wendling243ac562009-09-10 02:07:37 +0000465
Chris Lattnereec9bf12010-03-10 02:48:06 +0000466 if (IsSJLJ)
Chris Lattnera179b522010-04-04 19:25:43 +0000467 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("_LSDA_",
468 Asm->getFunctionNumber()));
Bill Wendling243ac562009-09-10 02:07:37 +0000469
Bill Wendling88fdcd32010-02-24 23:34:35 +0000470 // Emit the LSDA header.
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000471 Asm->EmitEncodingByte(dwarf::DW_EH_PE_omit, "@LPStart");
472 Asm->EmitEncodingByte(TTypeEncoding, "@TType");
Bill Wendling243ac562009-09-10 02:07:37 +0000473
Bill Wendling88fdcd32010-02-24 23:34:35 +0000474 // The type infos need to be aligned. GCC does this by inserting padding just
475 // before the type infos. However, this changes the size of the exception
476 // table, so you need to take this into account when you output the exception
477 // table size. However, the size is output using a variable length encoding.
478 // So by increasing the size by inserting padding, you may increase the number
479 // of bytes used for writing the size. If it increases, say by one byte, then
480 // you now need to output one less byte of padding to get the type infos
481 // aligned. However this decreases the size of the exception table. This
482 // changes the value you have to output for the exception table size. Due to
483 // the variable length encoding, the number of bytes used for writing the
484 // length may decrease. If so, you then have to increase the amount of
485 // padding. And so on. If you look carefully at the GCC code you will see that
486 // it indeed does this in a loop, going on and on until the values stabilize.
487 // We chose another solution: don't output padding inside the table like GCC
488 // does, instead output it before the table.
489 unsigned SizeTypes = TypeInfos.size() * TypeFormatSize;
Logan Chien5b776b72014-02-22 14:00:39 +0000490 unsigned CallSiteTableLengthSize = getULEB128Size(CallSiteTableLength);
Bill Wendling88fdcd32010-02-24 23:34:35 +0000491 unsigned TTypeBaseOffset =
492 sizeof(int8_t) + // Call site format
493 CallSiteTableLengthSize + // Call site table length size
494 CallSiteTableLength + // Call site table length
495 SizeActions + // Actions size
496 SizeTypes;
Logan Chien5b776b72014-02-22 14:00:39 +0000497 unsigned TTypeBaseOffsetSize = getULEB128Size(TTypeBaseOffset);
Bill Wendling88fdcd32010-02-24 23:34:35 +0000498 unsigned TotalSize =
499 sizeof(int8_t) + // LPStart format
500 sizeof(int8_t) + // TType format
501 (HaveTTData ? TTypeBaseOffsetSize : 0) + // TType base offset size
502 TTypeBaseOffset; // TType base offset
503 unsigned SizeAlign = (4 - TotalSize) & 3;
504
Bill Wendling24c74f12010-02-25 23:52:44 +0000505 if (HaveTTData) {
Bill Wendlingfea6c4e2010-02-26 21:31:01 +0000506 // Account for any extra padding that will be added to the call site table
Bill Wendling60aa4df2010-02-25 21:19:47 +0000507 // length.
Chris Lattner9efd1182010-04-04 19:09:29 +0000508 Asm->EmitULEB128(TTypeBaseOffset, "@TType base offset", SizeAlign);
Bill Wendlingc3a93762010-02-26 22:17:52 +0000509 SizeAlign = 0;
Bill Wendling24c74f12010-02-25 23:52:44 +0000510 }
Bill Wendling15349f82009-07-28 22:23:45 +0000511
Bill Wendlinga8339eb2011-06-21 22:30:20 +0000512 bool VerboseAsm = Asm->OutStreamer.isVerboseAsm();
513
Bill Wendling023ed642009-09-10 06:50:01 +0000514 // SjLj Exception handling
Bill Wendlinga482ec82009-09-10 00:17:04 +0000515 if (IsSJLJ) {
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000516 Asm->EmitEncodingByte(dwarf::DW_EH_PE_udata4, "Call site");
Bill Wendlingc3a93762010-02-26 22:17:52 +0000517
518 // Add extra padding if it wasn't added to the TType base offset.
Chris Lattner9efd1182010-04-04 19:09:29 +0000519 Asm->EmitULEB128(CallSiteTableLength, "Call site table length", SizeAlign);
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000520
Jim Grosbach693e36a2009-08-11 00:09:57 +0000521 // Emit the landing pad site information.
Jim Grosbach486be662009-08-17 16:41:22 +0000522 unsigned idx = 0;
523 for (SmallVectorImpl<CallSiteEntry>::const_iterator
524 I = CallSites.begin(), E = CallSites.end(); I != E; ++I, ++idx) {
525 const CallSiteEntry &S = *I;
Bill Wendling0e749fe2009-08-20 22:02:24 +0000526
Renato Golinc8d40652011-08-18 23:43:14 +0000527 // Offset of the landing pad, counted in 16-byte bundles relative to the
528 // @LPStart address.
Bill Wendlingddec6832011-06-21 22:40:24 +0000529 if (VerboseAsm) {
Benjamin Kramercb6b02a2011-10-16 15:46:29 +0000530 Asm->OutStreamer.AddComment(">> Call Site " + Twine(idx) + " <<");
531 Asm->OutStreamer.AddComment(" On exception at call site "+Twine(idx));
Bill Wendlingddec6832011-06-21 22:40:24 +0000532 }
Bill Wendlingddec6832011-06-21 22:40:24 +0000533 Asm->EmitULEB128(idx);
Bill Wendling0e749fe2009-08-20 22:02:24 +0000534
535 // Offset of the first associated action record, relative to the start of
536 // the action table. This value is biased by 1 (1 indicates the start of
537 // the action table), and 0 indicates that there are no actions.
Renato Golinc8d40652011-08-18 23:43:14 +0000538 if (VerboseAsm) {
539 if (S.Action == 0)
540 Asm->OutStreamer.AddComment(" Action: cleanup");
541 else
Benjamin Kramercb6b02a2011-10-16 15:46:29 +0000542 Asm->OutStreamer.AddComment(" Action: " +
543 Twine((S.Action - 1) / 2 + 1));
Renato Golinc8d40652011-08-18 23:43:14 +0000544 }
Bill Wendlingddec6832011-06-21 22:40:24 +0000545 Asm->EmitULEB128(S.Action);
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000546 }
Jim Grosbach693e36a2009-08-11 00:09:57 +0000547 } else {
548 // DWARF Exception handling
Anton Korobeynikov9be547c2011-01-14 21:58:08 +0000549 assert(Asm->MAI->isExceptionHandlingDwarf());
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000550
Bill Wendling0e749fe2009-08-20 22:02:24 +0000551 // The call-site table is a list of all call sites that may throw an
552 // exception (including C++ 'throw' statements) in the procedure
553 // fragment. It immediately follows the LSDA header. Each entry indicates,
554 // for a given call, the first corresponding action record and corresponding
555 // landing pad.
556 //
557 // The table begins with the number of bytes, stored as an LEB128
558 // compressed, unsigned integer. The records immediately follow the record
559 // count. They are sorted in increasing call-site address. Each record
560 // indicates:
561 //
562 // * The position of the call-site.
563 // * The position of the landing pad.
564 // * The first action record for that call site.
565 //
566 // A missing entry in the call-site table indicates that a call is not
Bill Wendling023ed642009-09-10 06:50:01 +0000567 // supposed to throw.
Bill Wendling0e749fe2009-08-20 22:02:24 +0000568
569 // Emit the landing pad call site table.
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000570 Asm->EmitEncodingByte(dwarf::DW_EH_PE_udata4, "Call site");
Bill Wendlingc3a93762010-02-26 22:17:52 +0000571
572 // Add extra padding if it wasn't added to the TType base offset.
Chris Lattner9efd1182010-04-04 19:09:29 +0000573 Asm->EmitULEB128(CallSiteTableLength, "Call site table length", SizeAlign);
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000574
Bill Wendlinga8339eb2011-06-21 22:30:20 +0000575 unsigned Entry = 0;
Jim Grosbach693e36a2009-08-11 00:09:57 +0000576 for (SmallVectorImpl<CallSiteEntry>::const_iterator
577 I = CallSites.begin(), E = CallSites.end(); I != E; ++I) {
578 const CallSiteEntry &S = *I;
Anton Korobeynikov61d167e2011-01-14 21:57:45 +0000579
Chris Lattnera179b522010-04-04 19:25:43 +0000580 MCSymbol *EHFuncBeginSym =
Chris Lattnerfd795022010-04-05 00:26:50 +0000581 Asm->GetTempSymbol("eh_func_begin", Asm->getFunctionNumber());
Anton Korobeynikov61d167e2011-01-14 21:57:45 +0000582
Chris Lattner34adc8d2010-03-14 01:41:15 +0000583 MCSymbol *BeginLabel = S.BeginLabel;
584 if (BeginLabel == 0)
585 BeginLabel = EHFuncBeginSym;
586 MCSymbol *EndLabel = S.EndLabel;
587 if (EndLabel == 0)
Chris Lattnerfd795022010-04-05 00:26:50 +0000588 EndLabel = Asm->GetTempSymbol("eh_func_end", Asm->getFunctionNumber());
Anton Korobeynikov61d167e2011-01-14 21:57:45 +0000589
Bill Wendlinga8339eb2011-06-21 22:30:20 +0000590
Bill Wendling0e749fe2009-08-20 22:02:24 +0000591 // Offset of the call site relative to the previous call site, counted in
592 // number of 16-byte bundles. The first call site is counted relative to
593 // the start of the procedure fragment.
Renato Golinc8d40652011-08-18 23:43:14 +0000594 if (VerboseAsm)
Benjamin Kramercb6b02a2011-10-16 15:46:29 +0000595 Asm->OutStreamer.AddComment(">> Call Site " + Twine(++Entry) + " <<");
Chris Lattnerc7cc8152010-04-04 21:31:54 +0000596 Asm->EmitLabelDifference(BeginLabel, EHFuncBeginSym, 4);
Renato Golinc8d40652011-08-18 23:43:14 +0000597 if (VerboseAsm)
598 Asm->OutStreamer.AddComment(Twine(" Call between ") +
599 BeginLabel->getName() + " and " +
600 EndLabel->getName());
Chris Lattnerf1429f12010-04-04 19:58:12 +0000601 Asm->EmitLabelDifference(EndLabel, BeginLabel, 4);
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000602
Bill Wendling0e749fe2009-08-20 22:02:24 +0000603 // Offset of the landing pad, counted in 16-byte bundles relative to the
604 // @LPStart address.
Renato Golinc8d40652011-08-18 23:43:14 +0000605 if (!S.PadLabel) {
606 if (VerboseAsm)
607 Asm->OutStreamer.AddComment(" has no landing pad");
Eric Christopherce0cfce2013-01-09 01:35:34 +0000608 Asm->OutStreamer.EmitIntValue(0, 4/*size*/);
Renato Golinc8d40652011-08-18 23:43:14 +0000609 } else {
610 if (VerboseAsm)
611 Asm->OutStreamer.AddComment(Twine(" jumps to ") +
612 S.PadLabel->getName());
Chris Lattnerc7cc8152010-04-04 21:31:54 +0000613 Asm->EmitLabelDifference(S.PadLabel, EHFuncBeginSym, 4);
Renato Golinc8d40652011-08-18 23:43:14 +0000614 }
Jim Grosbach693e36a2009-08-11 00:09:57 +0000615
Bill Wendling0e749fe2009-08-20 22:02:24 +0000616 // Offset of the first associated action record, relative to the start of
617 // the action table. This value is biased by 1 (1 indicates the start of
618 // the action table), and 0 indicates that there are no actions.
Renato Golinc8d40652011-08-18 23:43:14 +0000619 if (VerboseAsm) {
620 if (S.Action == 0)
621 Asm->OutStreamer.AddComment(" On action: cleanup");
622 else
Benjamin Kramercb6b02a2011-10-16 15:46:29 +0000623 Asm->OutStreamer.AddComment(" On action: " +
624 Twine((S.Action - 1) / 2 + 1));
Renato Golinc8d40652011-08-18 23:43:14 +0000625 }
Bill Wendlinga8339eb2011-06-21 22:30:20 +0000626 Asm->EmitULEB128(S.Action);
Jim Grosbach693e36a2009-08-11 00:09:57 +0000627 }
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000628 }
629
Bill Wendling0e749fe2009-08-20 22:02:24 +0000630 // Emit the Action Table.
Bill Wendlinga8339eb2011-06-21 22:30:20 +0000631 int Entry = 0;
Bill Wendling23b177e2009-07-28 23:44:43 +0000632 for (SmallVectorImpl<ActionEntry>::const_iterator
633 I = Actions.begin(), E = Actions.end(); I != E; ++I) {
634 const ActionEntry &Action = *I;
Bill Wendlinga8339eb2011-06-21 22:30:20 +0000635
636 if (VerboseAsm) {
637 // Emit comments that decode the action table.
Benjamin Kramercb6b02a2011-10-16 15:46:29 +0000638 Asm->OutStreamer.AddComment(">> Action Record " + Twine(++Entry) + " <<");
Renato Golinc8d40652011-08-18 23:43:14 +0000639 }
640
641 // Type Filter
642 //
643 // Used by the runtime to match the type of the thrown exception to the
644 // type of the catch clauses or the types in the exception specification.
645 if (VerboseAsm) {
Duncan Sands2e679372011-09-28 09:13:02 +0000646 if (Action.ValueForTypeID > 0)
Benjamin Kramercb6b02a2011-10-16 15:46:29 +0000647 Asm->OutStreamer.AddComment(" Catch TypeInfo " +
648 Twine(Action.ValueForTypeID));
Duncan Sands2e679372011-09-28 09:13:02 +0000649 else if (Action.ValueForTypeID < 0)
Benjamin Kramercb6b02a2011-10-16 15:46:29 +0000650 Asm->OutStreamer.AddComment(" Filter TypeInfo " +
651 Twine(Action.ValueForTypeID));
Duncan Sands2e679372011-09-28 09:13:02 +0000652 else
653 Asm->OutStreamer.AddComment(" Cleanup");
Renato Golinc8d40652011-08-18 23:43:14 +0000654 }
655 Asm->EmitSLEB128(Action.ValueForTypeID);
Bill Wendlinga8339eb2011-06-21 22:30:20 +0000656
Renato Golinc8d40652011-08-18 23:43:14 +0000657 // Action Record
658 //
659 // Self-relative signed displacement in bytes of the next action record,
660 // or 0 if there is no next action record.
661 if (VerboseAsm) {
Bill Wendlinga8339eb2011-06-21 22:30:20 +0000662 if (Action.NextAction == 0) {
663 Asm->OutStreamer.AddComment(" No further actions");
664 } else {
665 unsigned NextAction = Entry + (Action.NextAction + 1) / 2;
Benjamin Kramercb6b02a2011-10-16 15:46:29 +0000666 Asm->OutStreamer.AddComment(" Continue to action "+Twine(NextAction));
Bill Wendlinga8339eb2011-06-21 22:30:20 +0000667 }
Bill Wendlinga8339eb2011-06-21 22:30:20 +0000668 }
Bill Wendlinga8339eb2011-06-21 22:30:20 +0000669 Asm->EmitSLEB128(Action.NextAction);
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000670 }
671
Anton Korobeynikovf65a6382012-11-19 21:06:26 +0000672 EmitTypeInfos(TTypeEncoding);
673
674 Asm->EmitAlignment(2);
675}
676
677void DwarfException::EmitTypeInfos(unsigned TTypeEncoding) {
678 const std::vector<const GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
679 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
680
681 bool VerboseAsm = Asm->OutStreamer.isVerboseAsm();
682
683 int Entry = 0;
Bill Wendling4f0b8d72009-10-22 20:48:59 +0000684 // Emit the Catch TypeInfos.
Bill Wendlinga8339eb2011-06-21 22:30:20 +0000685 if (VerboseAsm && !TypeInfos.empty()) {
686 Asm->OutStreamer.AddComment(">> Catch TypeInfos <<");
Chris Lattner566cae92010-03-09 23:52:58 +0000687 Asm->OutStreamer.AddBlankLine();
Bill Wendlinga8339eb2011-06-21 22:30:20 +0000688 Entry = TypeInfos.size();
Chris Lattner566cae92010-03-09 23:52:58 +0000689 }
Bill Wendlinga8339eb2011-06-21 22:30:20 +0000690
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000691 for (std::vector<const GlobalVariable *>::const_reverse_iterator
Bill Wendling4b384b02009-11-19 00:09:14 +0000692 I = TypeInfos.rbegin(), E = TypeInfos.rend(); I != E; ++I) {
Bill Wendling31c74db2009-11-19 19:21:09 +0000693 const GlobalVariable *GV = *I;
Bill Wendlinga8339eb2011-06-21 22:30:20 +0000694 if (VerboseAsm)
Benjamin Kramercb6b02a2011-10-16 15:46:29 +0000695 Asm->OutStreamer.AddComment("TypeInfo " + Twine(Entry--));
Anton Korobeynikov097b0e92012-11-19 21:17:20 +0000696 Asm->EmitTTypeReference(GV, TTypeEncoding);
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000697 }
698
Bill Wendling4f0b8d72009-10-22 20:48:59 +0000699 // Emit the Exception Specifications.
Bill Wendlinga8339eb2011-06-21 22:30:20 +0000700 if (VerboseAsm && !FilterIds.empty()) {
701 Asm->OutStreamer.AddComment(">> Filter TypeInfos <<");
Chris Lattner566cae92010-03-09 23:52:58 +0000702 Asm->OutStreamer.AddBlankLine();
Bill Wendlinga8339eb2011-06-21 22:30:20 +0000703 Entry = 0;
Chris Lattner566cae92010-03-09 23:52:58 +0000704 }
Bill Wendling23b177e2009-07-28 23:44:43 +0000705 for (std::vector<unsigned>::const_iterator
706 I = FilterIds.begin(), E = FilterIds.end(); I < E; ++I) {
707 unsigned TypeID = *I;
Bill Wendlinga8339eb2011-06-21 22:30:20 +0000708 if (VerboseAsm) {
709 --Entry;
710 if (TypeID != 0)
Benjamin Kramercb6b02a2011-10-16 15:46:29 +0000711 Asm->OutStreamer.AddComment("FilterInfo " + Twine(Entry));
Bill Wendlinga8339eb2011-06-21 22:30:20 +0000712 }
713
714 Asm->EmitULEB128(TypeID);
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000715 }
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000716}
717
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +0000718/// endModule - Emit all exception information that should come after the
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000719/// content.
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +0000720void DwarfException::endModule() {
Craig Topperee4dab52012-02-05 08:31:47 +0000721 llvm_unreachable("Should be implemented");
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000722}
723
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +0000724/// beginFunction - Gather pre-function exception information. Assumes it's
Bill Wendling023ed642009-09-10 06:50:01 +0000725/// being emitted immediately after the function entry point.
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +0000726void DwarfException::beginFunction(const MachineFunction *MF) {
Craig Topperee4dab52012-02-05 08:31:47 +0000727 llvm_unreachable("Should be implemented");
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000728}
729
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +0000730/// endFunction - Gather and emit post-function exception information.
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +0000731void DwarfException::endFunction(const MachineFunction *) {
Craig Topperee4dab52012-02-05 08:31:47 +0000732 llvm_unreachable("Should be implemented");
Bill Wendlingd64cd2b2009-05-15 01:12:28 +0000733}