1*9880d681SAndroid Build Coastguard Worker //===-- CodeGen/AsmPrinter/EHStreamer.cpp - Exception Directive Streamer --===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file contains support for writing exception info into assembly files.
11*9880d681SAndroid Build Coastguard Worker //
12*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
13*9880d681SAndroid Build Coastguard Worker
14*9880d681SAndroid Build Coastguard Worker #include "EHStreamer.h"
15*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/AsmPrinter.h"
16*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFunction.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineInstr.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineModuleInfo.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Function.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCAsmInfo.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCStreamer.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCSymbol.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/LEB128.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetLoweringObjectFile.h"
25*9880d681SAndroid Build Coastguard Worker
26*9880d681SAndroid Build Coastguard Worker using namespace llvm;
27*9880d681SAndroid Build Coastguard Worker
EHStreamer(AsmPrinter * A)28*9880d681SAndroid Build Coastguard Worker EHStreamer::EHStreamer(AsmPrinter *A) : Asm(A), MMI(Asm->MMI) {}
29*9880d681SAndroid Build Coastguard Worker
~EHStreamer()30*9880d681SAndroid Build Coastguard Worker EHStreamer::~EHStreamer() {}
31*9880d681SAndroid Build Coastguard Worker
32*9880d681SAndroid Build Coastguard Worker /// How many leading type ids two landing pads have in common.
sharedTypeIDs(const LandingPadInfo * L,const LandingPadInfo * R)33*9880d681SAndroid Build Coastguard Worker unsigned EHStreamer::sharedTypeIDs(const LandingPadInfo *L,
34*9880d681SAndroid Build Coastguard Worker const LandingPadInfo *R) {
35*9880d681SAndroid Build Coastguard Worker const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
36*9880d681SAndroid Build Coastguard Worker unsigned LSize = LIds.size(), RSize = RIds.size();
37*9880d681SAndroid Build Coastguard Worker unsigned MinSize = LSize < RSize ? LSize : RSize;
38*9880d681SAndroid Build Coastguard Worker unsigned Count = 0;
39*9880d681SAndroid Build Coastguard Worker
40*9880d681SAndroid Build Coastguard Worker for (; Count != MinSize; ++Count)
41*9880d681SAndroid Build Coastguard Worker if (LIds[Count] != RIds[Count])
42*9880d681SAndroid Build Coastguard Worker return Count;
43*9880d681SAndroid Build Coastguard Worker
44*9880d681SAndroid Build Coastguard Worker return Count;
45*9880d681SAndroid Build Coastguard Worker }
46*9880d681SAndroid Build Coastguard Worker
47*9880d681SAndroid Build Coastguard Worker /// Compute the actions table and gather the first action index for each landing
48*9880d681SAndroid Build Coastguard Worker /// pad site.
49*9880d681SAndroid Build Coastguard Worker unsigned EHStreamer::
computeActionsTable(const SmallVectorImpl<const LandingPadInfo * > & LandingPads,SmallVectorImpl<ActionEntry> & Actions,SmallVectorImpl<unsigned> & FirstActions)50*9880d681SAndroid Build Coastguard Worker computeActionsTable(const SmallVectorImpl<const LandingPadInfo*> &LandingPads,
51*9880d681SAndroid Build Coastguard Worker SmallVectorImpl<ActionEntry> &Actions,
52*9880d681SAndroid Build Coastguard Worker SmallVectorImpl<unsigned> &FirstActions) {
53*9880d681SAndroid Build Coastguard Worker
54*9880d681SAndroid Build Coastguard Worker // The action table follows the call-site table in the LSDA. The individual
55*9880d681SAndroid Build Coastguard Worker // records are of two types:
56*9880d681SAndroid Build Coastguard Worker //
57*9880d681SAndroid Build Coastguard Worker // * Catch clause
58*9880d681SAndroid Build Coastguard Worker // * Exception specification
59*9880d681SAndroid Build Coastguard Worker //
60*9880d681SAndroid Build Coastguard Worker // The two record kinds have the same format, with only small differences.
61*9880d681SAndroid Build Coastguard Worker // They are distinguished by the "switch value" field: Catch clauses
62*9880d681SAndroid Build Coastguard Worker // (TypeInfos) have strictly positive switch values, and exception
63*9880d681SAndroid Build Coastguard Worker // specifications (FilterIds) have strictly negative switch values. Value 0
64*9880d681SAndroid Build Coastguard Worker // indicates a catch-all clause.
65*9880d681SAndroid Build Coastguard Worker //
66*9880d681SAndroid Build Coastguard Worker // Negative type IDs index into FilterIds. Positive type IDs index into
67*9880d681SAndroid Build Coastguard Worker // TypeInfos. The value written for a positive type ID is just the type ID
68*9880d681SAndroid Build Coastguard Worker // itself. For a negative type ID, however, the value written is the
69*9880d681SAndroid Build Coastguard Worker // (negative) byte offset of the corresponding FilterIds entry. The byte
70*9880d681SAndroid Build Coastguard Worker // offset is usually equal to the type ID (because the FilterIds entries are
71*9880d681SAndroid Build Coastguard Worker // written using a variable width encoding, which outputs one byte per entry
72*9880d681SAndroid Build Coastguard Worker // as long as the value written is not too large) but can differ. This kind
73*9880d681SAndroid Build Coastguard Worker // of complication does not occur for positive type IDs because type infos are
74*9880d681SAndroid Build Coastguard Worker // output using a fixed width encoding. FilterOffsets[i] holds the byte
75*9880d681SAndroid Build Coastguard Worker // offset corresponding to FilterIds[i].
76*9880d681SAndroid Build Coastguard Worker
77*9880d681SAndroid Build Coastguard Worker const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
78*9880d681SAndroid Build Coastguard Worker SmallVector<int, 16> FilterOffsets;
79*9880d681SAndroid Build Coastguard Worker FilterOffsets.reserve(FilterIds.size());
80*9880d681SAndroid Build Coastguard Worker int Offset = -1;
81*9880d681SAndroid Build Coastguard Worker
82*9880d681SAndroid Build Coastguard Worker for (std::vector<unsigned>::const_iterator
83*9880d681SAndroid Build Coastguard Worker I = FilterIds.begin(), E = FilterIds.end(); I != E; ++I) {
84*9880d681SAndroid Build Coastguard Worker FilterOffsets.push_back(Offset);
85*9880d681SAndroid Build Coastguard Worker Offset -= getULEB128Size(*I);
86*9880d681SAndroid Build Coastguard Worker }
87*9880d681SAndroid Build Coastguard Worker
88*9880d681SAndroid Build Coastguard Worker FirstActions.reserve(LandingPads.size());
89*9880d681SAndroid Build Coastguard Worker
90*9880d681SAndroid Build Coastguard Worker int FirstAction = 0;
91*9880d681SAndroid Build Coastguard Worker unsigned SizeActions = 0;
92*9880d681SAndroid Build Coastguard Worker const LandingPadInfo *PrevLPI = nullptr;
93*9880d681SAndroid Build Coastguard Worker
94*9880d681SAndroid Build Coastguard Worker for (SmallVectorImpl<const LandingPadInfo *>::const_iterator
95*9880d681SAndroid Build Coastguard Worker I = LandingPads.begin(), E = LandingPads.end(); I != E; ++I) {
96*9880d681SAndroid Build Coastguard Worker const LandingPadInfo *LPI = *I;
97*9880d681SAndroid Build Coastguard Worker const std::vector<int> &TypeIds = LPI->TypeIds;
98*9880d681SAndroid Build Coastguard Worker unsigned NumShared = PrevLPI ? sharedTypeIDs(LPI, PrevLPI) : 0;
99*9880d681SAndroid Build Coastguard Worker unsigned SizeSiteActions = 0;
100*9880d681SAndroid Build Coastguard Worker
101*9880d681SAndroid Build Coastguard Worker if (NumShared < TypeIds.size()) {
102*9880d681SAndroid Build Coastguard Worker unsigned SizeAction = 0;
103*9880d681SAndroid Build Coastguard Worker unsigned PrevAction = (unsigned)-1;
104*9880d681SAndroid Build Coastguard Worker
105*9880d681SAndroid Build Coastguard Worker if (NumShared) {
106*9880d681SAndroid Build Coastguard Worker unsigned SizePrevIds = PrevLPI->TypeIds.size();
107*9880d681SAndroid Build Coastguard Worker assert(Actions.size());
108*9880d681SAndroid Build Coastguard Worker PrevAction = Actions.size() - 1;
109*9880d681SAndroid Build Coastguard Worker SizeAction = getSLEB128Size(Actions[PrevAction].NextAction) +
110*9880d681SAndroid Build Coastguard Worker getSLEB128Size(Actions[PrevAction].ValueForTypeID);
111*9880d681SAndroid Build Coastguard Worker
112*9880d681SAndroid Build Coastguard Worker for (unsigned j = NumShared; j != SizePrevIds; ++j) {
113*9880d681SAndroid Build Coastguard Worker assert(PrevAction != (unsigned)-1 && "PrevAction is invalid!");
114*9880d681SAndroid Build Coastguard Worker SizeAction -= getSLEB128Size(Actions[PrevAction].ValueForTypeID);
115*9880d681SAndroid Build Coastguard Worker SizeAction += -Actions[PrevAction].NextAction;
116*9880d681SAndroid Build Coastguard Worker PrevAction = Actions[PrevAction].Previous;
117*9880d681SAndroid Build Coastguard Worker }
118*9880d681SAndroid Build Coastguard Worker }
119*9880d681SAndroid Build Coastguard Worker
120*9880d681SAndroid Build Coastguard Worker // Compute the actions.
121*9880d681SAndroid Build Coastguard Worker for (unsigned J = NumShared, M = TypeIds.size(); J != M; ++J) {
122*9880d681SAndroid Build Coastguard Worker int TypeID = TypeIds[J];
123*9880d681SAndroid Build Coastguard Worker assert(-1 - TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
124*9880d681SAndroid Build Coastguard Worker int ValueForTypeID =
125*9880d681SAndroid Build Coastguard Worker isFilterEHSelector(TypeID) ? FilterOffsets[-1 - TypeID] : TypeID;
126*9880d681SAndroid Build Coastguard Worker unsigned SizeTypeID = getSLEB128Size(ValueForTypeID);
127*9880d681SAndroid Build Coastguard Worker
128*9880d681SAndroid Build Coastguard Worker int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
129*9880d681SAndroid Build Coastguard Worker SizeAction = SizeTypeID + getSLEB128Size(NextAction);
130*9880d681SAndroid Build Coastguard Worker SizeSiteActions += SizeAction;
131*9880d681SAndroid Build Coastguard Worker
132*9880d681SAndroid Build Coastguard Worker ActionEntry Action = { ValueForTypeID, NextAction, PrevAction };
133*9880d681SAndroid Build Coastguard Worker Actions.push_back(Action);
134*9880d681SAndroid Build Coastguard Worker PrevAction = Actions.size() - 1;
135*9880d681SAndroid Build Coastguard Worker }
136*9880d681SAndroid Build Coastguard Worker
137*9880d681SAndroid Build Coastguard Worker // Record the first action of the landing pad site.
138*9880d681SAndroid Build Coastguard Worker FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
139*9880d681SAndroid Build Coastguard Worker } // else identical - re-use previous FirstAction
140*9880d681SAndroid Build Coastguard Worker
141*9880d681SAndroid Build Coastguard Worker // Information used when created the call-site table. The action record
142*9880d681SAndroid Build Coastguard Worker // field of the call site record is the offset of the first associated
143*9880d681SAndroid Build Coastguard Worker // action record, relative to the start of the actions table. This value is
144*9880d681SAndroid Build Coastguard Worker // biased by 1 (1 indicating the start of the actions table), and 0
145*9880d681SAndroid Build Coastguard Worker // indicates that there are no actions.
146*9880d681SAndroid Build Coastguard Worker FirstActions.push_back(FirstAction);
147*9880d681SAndroid Build Coastguard Worker
148*9880d681SAndroid Build Coastguard Worker // Compute this sites contribution to size.
149*9880d681SAndroid Build Coastguard Worker SizeActions += SizeSiteActions;
150*9880d681SAndroid Build Coastguard Worker
151*9880d681SAndroid Build Coastguard Worker PrevLPI = LPI;
152*9880d681SAndroid Build Coastguard Worker }
153*9880d681SAndroid Build Coastguard Worker
154*9880d681SAndroid Build Coastguard Worker return SizeActions;
155*9880d681SAndroid Build Coastguard Worker }
156*9880d681SAndroid Build Coastguard Worker
157*9880d681SAndroid Build Coastguard Worker /// Return `true' if this is a call to a function marked `nounwind'. Return
158*9880d681SAndroid Build Coastguard Worker /// `false' otherwise.
callToNoUnwindFunction(const MachineInstr * MI)159*9880d681SAndroid Build Coastguard Worker bool EHStreamer::callToNoUnwindFunction(const MachineInstr *MI) {
160*9880d681SAndroid Build Coastguard Worker assert(MI->isCall() && "This should be a call instruction!");
161*9880d681SAndroid Build Coastguard Worker
162*9880d681SAndroid Build Coastguard Worker bool MarkedNoUnwind = false;
163*9880d681SAndroid Build Coastguard Worker bool SawFunc = false;
164*9880d681SAndroid Build Coastguard Worker
165*9880d681SAndroid Build Coastguard Worker for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
166*9880d681SAndroid Build Coastguard Worker const MachineOperand &MO = MI->getOperand(I);
167*9880d681SAndroid Build Coastguard Worker
168*9880d681SAndroid Build Coastguard Worker if (!MO.isGlobal()) continue;
169*9880d681SAndroid Build Coastguard Worker
170*9880d681SAndroid Build Coastguard Worker const Function *F = dyn_cast<Function>(MO.getGlobal());
171*9880d681SAndroid Build Coastguard Worker if (!F) continue;
172*9880d681SAndroid Build Coastguard Worker
173*9880d681SAndroid Build Coastguard Worker if (SawFunc) {
174*9880d681SAndroid Build Coastguard Worker // Be conservative. If we have more than one function operand for this
175*9880d681SAndroid Build Coastguard Worker // call, then we can't make the assumption that it's the callee and
176*9880d681SAndroid Build Coastguard Worker // not a parameter to the call.
177*9880d681SAndroid Build Coastguard Worker //
178*9880d681SAndroid Build Coastguard Worker // FIXME: Determine if there's a way to say that `F' is the callee or
179*9880d681SAndroid Build Coastguard Worker // parameter.
180*9880d681SAndroid Build Coastguard Worker MarkedNoUnwind = false;
181*9880d681SAndroid Build Coastguard Worker break;
182*9880d681SAndroid Build Coastguard Worker }
183*9880d681SAndroid Build Coastguard Worker
184*9880d681SAndroid Build Coastguard Worker MarkedNoUnwind = F->doesNotThrow();
185*9880d681SAndroid Build Coastguard Worker SawFunc = true;
186*9880d681SAndroid Build Coastguard Worker }
187*9880d681SAndroid Build Coastguard Worker
188*9880d681SAndroid Build Coastguard Worker return MarkedNoUnwind;
189*9880d681SAndroid Build Coastguard Worker }
190*9880d681SAndroid Build Coastguard Worker
computePadMap(const SmallVectorImpl<const LandingPadInfo * > & LandingPads,RangeMapType & PadMap)191*9880d681SAndroid Build Coastguard Worker void EHStreamer::computePadMap(
192*9880d681SAndroid Build Coastguard Worker const SmallVectorImpl<const LandingPadInfo *> &LandingPads,
193*9880d681SAndroid Build Coastguard Worker RangeMapType &PadMap) {
194*9880d681SAndroid Build Coastguard Worker // Invokes and nounwind calls have entries in PadMap (due to being bracketed
195*9880d681SAndroid Build Coastguard Worker // by try-range labels when lowered). Ordinary calls do not, so appropriate
196*9880d681SAndroid Build Coastguard Worker // try-ranges for them need be deduced so we can put them in the LSDA.
197*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
198*9880d681SAndroid Build Coastguard Worker const LandingPadInfo *LandingPad = LandingPads[i];
199*9880d681SAndroid Build Coastguard Worker for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
200*9880d681SAndroid Build Coastguard Worker MCSymbol *BeginLabel = LandingPad->BeginLabels[j];
201*9880d681SAndroid Build Coastguard Worker assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
202*9880d681SAndroid Build Coastguard Worker PadRange P = { i, j };
203*9880d681SAndroid Build Coastguard Worker PadMap[BeginLabel] = P;
204*9880d681SAndroid Build Coastguard Worker }
205*9880d681SAndroid Build Coastguard Worker }
206*9880d681SAndroid Build Coastguard Worker }
207*9880d681SAndroid Build Coastguard Worker
208*9880d681SAndroid Build Coastguard Worker /// Compute the call-site table. The entry for an invoke has a try-range
209*9880d681SAndroid Build Coastguard Worker /// containing the call, a non-zero landing pad, and an appropriate action. The
210*9880d681SAndroid Build Coastguard Worker /// entry for an ordinary call has a try-range containing the call and zero for
211*9880d681SAndroid Build Coastguard Worker /// the landing pad and the action. Calls marked 'nounwind' have no entry and
212*9880d681SAndroid Build Coastguard Worker /// must not be contained in the try-range of any entry - they form gaps in the
213*9880d681SAndroid Build Coastguard Worker /// table. Entries must be ordered by try-range address.
214*9880d681SAndroid Build Coastguard Worker void EHStreamer::
computeCallSiteTable(SmallVectorImpl<CallSiteEntry> & CallSites,const SmallVectorImpl<const LandingPadInfo * > & LandingPads,const SmallVectorImpl<unsigned> & FirstActions)215*9880d681SAndroid Build Coastguard Worker computeCallSiteTable(SmallVectorImpl<CallSiteEntry> &CallSites,
216*9880d681SAndroid Build Coastguard Worker const SmallVectorImpl<const LandingPadInfo *> &LandingPads,
217*9880d681SAndroid Build Coastguard Worker const SmallVectorImpl<unsigned> &FirstActions) {
218*9880d681SAndroid Build Coastguard Worker RangeMapType PadMap;
219*9880d681SAndroid Build Coastguard Worker computePadMap(LandingPads, PadMap);
220*9880d681SAndroid Build Coastguard Worker
221*9880d681SAndroid Build Coastguard Worker // The end label of the previous invoke or nounwind try-range.
222*9880d681SAndroid Build Coastguard Worker MCSymbol *LastLabel = nullptr;
223*9880d681SAndroid Build Coastguard Worker
224*9880d681SAndroid Build Coastguard Worker // Whether there is a potentially throwing instruction (currently this means
225*9880d681SAndroid Build Coastguard Worker // an ordinary call) between the end of the previous try-range and now.
226*9880d681SAndroid Build Coastguard Worker bool SawPotentiallyThrowing = false;
227*9880d681SAndroid Build Coastguard Worker
228*9880d681SAndroid Build Coastguard Worker // Whether the last CallSite entry was for an invoke.
229*9880d681SAndroid Build Coastguard Worker bool PreviousIsInvoke = false;
230*9880d681SAndroid Build Coastguard Worker
231*9880d681SAndroid Build Coastguard Worker bool IsSJLJ = Asm->MAI->getExceptionHandlingType() == ExceptionHandling::SjLj;
232*9880d681SAndroid Build Coastguard Worker
233*9880d681SAndroid Build Coastguard Worker // Visit all instructions in order of address.
234*9880d681SAndroid Build Coastguard Worker for (const auto &MBB : *Asm->MF) {
235*9880d681SAndroid Build Coastguard Worker for (const auto &MI : MBB) {
236*9880d681SAndroid Build Coastguard Worker if (!MI.isEHLabel()) {
237*9880d681SAndroid Build Coastguard Worker if (MI.isCall())
238*9880d681SAndroid Build Coastguard Worker SawPotentiallyThrowing |= !callToNoUnwindFunction(&MI);
239*9880d681SAndroid Build Coastguard Worker continue;
240*9880d681SAndroid Build Coastguard Worker }
241*9880d681SAndroid Build Coastguard Worker
242*9880d681SAndroid Build Coastguard Worker // End of the previous try-range?
243*9880d681SAndroid Build Coastguard Worker MCSymbol *BeginLabel = MI.getOperand(0).getMCSymbol();
244*9880d681SAndroid Build Coastguard Worker if (BeginLabel == LastLabel)
245*9880d681SAndroid Build Coastguard Worker SawPotentiallyThrowing = false;
246*9880d681SAndroid Build Coastguard Worker
247*9880d681SAndroid Build Coastguard Worker // Beginning of a new try-range?
248*9880d681SAndroid Build Coastguard Worker RangeMapType::const_iterator L = PadMap.find(BeginLabel);
249*9880d681SAndroid Build Coastguard Worker if (L == PadMap.end())
250*9880d681SAndroid Build Coastguard Worker // Nope, it was just some random label.
251*9880d681SAndroid Build Coastguard Worker continue;
252*9880d681SAndroid Build Coastguard Worker
253*9880d681SAndroid Build Coastguard Worker const PadRange &P = L->second;
254*9880d681SAndroid Build Coastguard Worker const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
255*9880d681SAndroid Build Coastguard Worker assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
256*9880d681SAndroid Build Coastguard Worker "Inconsistent landing pad map!");
257*9880d681SAndroid Build Coastguard Worker
258*9880d681SAndroid Build Coastguard Worker // For Dwarf exception handling (SjLj handling doesn't use this). If some
259*9880d681SAndroid Build Coastguard Worker // instruction between the previous try-range and this one may throw,
260*9880d681SAndroid Build Coastguard Worker // create a call-site entry with no landing pad for the region between the
261*9880d681SAndroid Build Coastguard Worker // try-ranges.
262*9880d681SAndroid Build Coastguard Worker if (SawPotentiallyThrowing && Asm->MAI->usesCFIForEH()) {
263*9880d681SAndroid Build Coastguard Worker CallSiteEntry Site = { LastLabel, BeginLabel, nullptr, 0 };
264*9880d681SAndroid Build Coastguard Worker CallSites.push_back(Site);
265*9880d681SAndroid Build Coastguard Worker PreviousIsInvoke = false;
266*9880d681SAndroid Build Coastguard Worker }
267*9880d681SAndroid Build Coastguard Worker
268*9880d681SAndroid Build Coastguard Worker LastLabel = LandingPad->EndLabels[P.RangeIndex];
269*9880d681SAndroid Build Coastguard Worker assert(BeginLabel && LastLabel && "Invalid landing pad!");
270*9880d681SAndroid Build Coastguard Worker
271*9880d681SAndroid Build Coastguard Worker if (!LandingPad->LandingPadLabel) {
272*9880d681SAndroid Build Coastguard Worker // Create a gap.
273*9880d681SAndroid Build Coastguard Worker PreviousIsInvoke = false;
274*9880d681SAndroid Build Coastguard Worker } else {
275*9880d681SAndroid Build Coastguard Worker // This try-range is for an invoke.
276*9880d681SAndroid Build Coastguard Worker CallSiteEntry Site = {
277*9880d681SAndroid Build Coastguard Worker BeginLabel,
278*9880d681SAndroid Build Coastguard Worker LastLabel,
279*9880d681SAndroid Build Coastguard Worker LandingPad,
280*9880d681SAndroid Build Coastguard Worker FirstActions[P.PadIndex]
281*9880d681SAndroid Build Coastguard Worker };
282*9880d681SAndroid Build Coastguard Worker
283*9880d681SAndroid Build Coastguard Worker // Try to merge with the previous call-site. SJLJ doesn't do this
284*9880d681SAndroid Build Coastguard Worker if (PreviousIsInvoke && !IsSJLJ) {
285*9880d681SAndroid Build Coastguard Worker CallSiteEntry &Prev = CallSites.back();
286*9880d681SAndroid Build Coastguard Worker if (Site.LPad == Prev.LPad && Site.Action == Prev.Action) {
287*9880d681SAndroid Build Coastguard Worker // Extend the range of the previous entry.
288*9880d681SAndroid Build Coastguard Worker Prev.EndLabel = Site.EndLabel;
289*9880d681SAndroid Build Coastguard Worker continue;
290*9880d681SAndroid Build Coastguard Worker }
291*9880d681SAndroid Build Coastguard Worker }
292*9880d681SAndroid Build Coastguard Worker
293*9880d681SAndroid Build Coastguard Worker // Otherwise, create a new call-site.
294*9880d681SAndroid Build Coastguard Worker if (!IsSJLJ)
295*9880d681SAndroid Build Coastguard Worker CallSites.push_back(Site);
296*9880d681SAndroid Build Coastguard Worker else {
297*9880d681SAndroid Build Coastguard Worker // SjLj EH must maintain the call sites in the order assigned
298*9880d681SAndroid Build Coastguard Worker // to them by the SjLjPrepare pass.
299*9880d681SAndroid Build Coastguard Worker unsigned SiteNo = MMI->getCallSiteBeginLabel(BeginLabel);
300*9880d681SAndroid Build Coastguard Worker if (CallSites.size() < SiteNo)
301*9880d681SAndroid Build Coastguard Worker CallSites.resize(SiteNo);
302*9880d681SAndroid Build Coastguard Worker CallSites[SiteNo - 1] = Site;
303*9880d681SAndroid Build Coastguard Worker }
304*9880d681SAndroid Build Coastguard Worker PreviousIsInvoke = true;
305*9880d681SAndroid Build Coastguard Worker }
306*9880d681SAndroid Build Coastguard Worker }
307*9880d681SAndroid Build Coastguard Worker }
308*9880d681SAndroid Build Coastguard Worker
309*9880d681SAndroid Build Coastguard Worker // If some instruction between the previous try-range and the end of the
310*9880d681SAndroid Build Coastguard Worker // function may throw, create a call-site entry with no landing pad for the
311*9880d681SAndroid Build Coastguard Worker // region following the try-range.
312*9880d681SAndroid Build Coastguard Worker if (SawPotentiallyThrowing && !IsSJLJ && LastLabel != nullptr) {
313*9880d681SAndroid Build Coastguard Worker CallSiteEntry Site = { LastLabel, nullptr, nullptr, 0 };
314*9880d681SAndroid Build Coastguard Worker CallSites.push_back(Site);
315*9880d681SAndroid Build Coastguard Worker }
316*9880d681SAndroid Build Coastguard Worker }
317*9880d681SAndroid Build Coastguard Worker
318*9880d681SAndroid Build Coastguard Worker /// Emit landing pads and actions.
319*9880d681SAndroid Build Coastguard Worker ///
320*9880d681SAndroid Build Coastguard Worker /// The general organization of the table is complex, but the basic concepts are
321*9880d681SAndroid Build Coastguard Worker /// easy. First there is a header which describes the location and organization
322*9880d681SAndroid Build Coastguard Worker /// of the three components that follow.
323*9880d681SAndroid Build Coastguard Worker ///
324*9880d681SAndroid Build Coastguard Worker /// 1. The landing pad site information describes the range of code covered by
325*9880d681SAndroid Build Coastguard Worker /// the try. In our case it's an accumulation of the ranges covered by the
326*9880d681SAndroid Build Coastguard Worker /// invokes in the try. There is also a reference to the landing pad that
327*9880d681SAndroid Build Coastguard Worker /// handles the exception once processed. Finally an index into the actions
328*9880d681SAndroid Build Coastguard Worker /// table.
329*9880d681SAndroid Build Coastguard Worker /// 2. The action table, in our case, is composed of pairs of type IDs and next
330*9880d681SAndroid Build Coastguard Worker /// action offset. Starting with the action index from the landing pad
331*9880d681SAndroid Build Coastguard Worker /// site, each type ID is checked for a match to the current exception. If
332*9880d681SAndroid Build Coastguard Worker /// it matches then the exception and type id are passed on to the landing
333*9880d681SAndroid Build Coastguard Worker /// pad. Otherwise the next action is looked up. This chain is terminated
334*9880d681SAndroid Build Coastguard Worker /// with a next action of zero. If no type id is found then the frame is
335*9880d681SAndroid Build Coastguard Worker /// unwound and handling continues.
336*9880d681SAndroid Build Coastguard Worker /// 3. Type ID table contains references to all the C++ typeinfo for all
337*9880d681SAndroid Build Coastguard Worker /// catches in the function. This tables is reverse indexed base 1.
emitExceptionTable()338*9880d681SAndroid Build Coastguard Worker void EHStreamer::emitExceptionTable() {
339*9880d681SAndroid Build Coastguard Worker const std::vector<const GlobalValue *> &TypeInfos = MMI->getTypeInfos();
340*9880d681SAndroid Build Coastguard Worker const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
341*9880d681SAndroid Build Coastguard Worker const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
342*9880d681SAndroid Build Coastguard Worker
343*9880d681SAndroid Build Coastguard Worker // Sort the landing pads in order of their type ids. This is used to fold
344*9880d681SAndroid Build Coastguard Worker // duplicate actions.
345*9880d681SAndroid Build Coastguard Worker SmallVector<const LandingPadInfo *, 64> LandingPads;
346*9880d681SAndroid Build Coastguard Worker LandingPads.reserve(PadInfos.size());
347*9880d681SAndroid Build Coastguard Worker
348*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
349*9880d681SAndroid Build Coastguard Worker LandingPads.push_back(&PadInfos[i]);
350*9880d681SAndroid Build Coastguard Worker
351*9880d681SAndroid Build Coastguard Worker // Order landing pads lexicographically by type id.
352*9880d681SAndroid Build Coastguard Worker std::sort(LandingPads.begin(), LandingPads.end(),
353*9880d681SAndroid Build Coastguard Worker [](const LandingPadInfo *L,
354*9880d681SAndroid Build Coastguard Worker const LandingPadInfo *R) { return L->TypeIds < R->TypeIds; });
355*9880d681SAndroid Build Coastguard Worker
356*9880d681SAndroid Build Coastguard Worker // Compute the actions table and gather the first action index for each
357*9880d681SAndroid Build Coastguard Worker // landing pad site.
358*9880d681SAndroid Build Coastguard Worker SmallVector<ActionEntry, 32> Actions;
359*9880d681SAndroid Build Coastguard Worker SmallVector<unsigned, 64> FirstActions;
360*9880d681SAndroid Build Coastguard Worker unsigned SizeActions =
361*9880d681SAndroid Build Coastguard Worker computeActionsTable(LandingPads, Actions, FirstActions);
362*9880d681SAndroid Build Coastguard Worker
363*9880d681SAndroid Build Coastguard Worker // Compute the call-site table.
364*9880d681SAndroid Build Coastguard Worker SmallVector<CallSiteEntry, 64> CallSites;
365*9880d681SAndroid Build Coastguard Worker computeCallSiteTable(CallSites, LandingPads, FirstActions);
366*9880d681SAndroid Build Coastguard Worker
367*9880d681SAndroid Build Coastguard Worker // Final tallies.
368*9880d681SAndroid Build Coastguard Worker
369*9880d681SAndroid Build Coastguard Worker // Call sites.
370*9880d681SAndroid Build Coastguard Worker bool IsSJLJ = Asm->MAI->getExceptionHandlingType() == ExceptionHandling::SjLj;
371*9880d681SAndroid Build Coastguard Worker bool HaveTTData = IsSJLJ ? (!TypeInfos.empty() || !FilterIds.empty()) : true;
372*9880d681SAndroid Build Coastguard Worker
373*9880d681SAndroid Build Coastguard Worker unsigned CallSiteTableLength;
374*9880d681SAndroid Build Coastguard Worker if (IsSJLJ)
375*9880d681SAndroid Build Coastguard Worker CallSiteTableLength = 0;
376*9880d681SAndroid Build Coastguard Worker else {
377*9880d681SAndroid Build Coastguard Worker unsigned SiteStartSize = 4; // dwarf::DW_EH_PE_udata4
378*9880d681SAndroid Build Coastguard Worker unsigned SiteLengthSize = 4; // dwarf::DW_EH_PE_udata4
379*9880d681SAndroid Build Coastguard Worker unsigned LandingPadSize = 4; // dwarf::DW_EH_PE_udata4
380*9880d681SAndroid Build Coastguard Worker CallSiteTableLength =
381*9880d681SAndroid Build Coastguard Worker CallSites.size() * (SiteStartSize + SiteLengthSize + LandingPadSize);
382*9880d681SAndroid Build Coastguard Worker }
383*9880d681SAndroid Build Coastguard Worker
384*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = CallSites.size(); i < e; ++i) {
385*9880d681SAndroid Build Coastguard Worker CallSiteTableLength += getULEB128Size(CallSites[i].Action);
386*9880d681SAndroid Build Coastguard Worker if (IsSJLJ)
387*9880d681SAndroid Build Coastguard Worker CallSiteTableLength += getULEB128Size(i);
388*9880d681SAndroid Build Coastguard Worker }
389*9880d681SAndroid Build Coastguard Worker
390*9880d681SAndroid Build Coastguard Worker // Type infos.
391*9880d681SAndroid Build Coastguard Worker MCSection *LSDASection = Asm->getObjFileLowering().getLSDASection();
392*9880d681SAndroid Build Coastguard Worker unsigned TTypeEncoding;
393*9880d681SAndroid Build Coastguard Worker unsigned TypeFormatSize;
394*9880d681SAndroid Build Coastguard Worker
395*9880d681SAndroid Build Coastguard Worker if (!HaveTTData) {
396*9880d681SAndroid Build Coastguard Worker // For SjLj exceptions, if there is no TypeInfo, then we just explicitly say
397*9880d681SAndroid Build Coastguard Worker // that we're omitting that bit.
398*9880d681SAndroid Build Coastguard Worker TTypeEncoding = dwarf::DW_EH_PE_omit;
399*9880d681SAndroid Build Coastguard Worker // dwarf::DW_EH_PE_absptr
400*9880d681SAndroid Build Coastguard Worker TypeFormatSize = Asm->getDataLayout().getPointerSize();
401*9880d681SAndroid Build Coastguard Worker } else {
402*9880d681SAndroid Build Coastguard Worker // Okay, we have actual filters or typeinfos to emit. As such, we need to
403*9880d681SAndroid Build Coastguard Worker // pick a type encoding for them. We're about to emit a list of pointers to
404*9880d681SAndroid Build Coastguard Worker // typeinfo objects at the end of the LSDA. However, unless we're in static
405*9880d681SAndroid Build Coastguard Worker // mode, this reference will require a relocation by the dynamic linker.
406*9880d681SAndroid Build Coastguard Worker //
407*9880d681SAndroid Build Coastguard Worker // Because of this, we have a couple of options:
408*9880d681SAndroid Build Coastguard Worker //
409*9880d681SAndroid Build Coastguard Worker // 1) If we are in -static mode, we can always use an absolute reference
410*9880d681SAndroid Build Coastguard Worker // from the LSDA, because the static linker will resolve it.
411*9880d681SAndroid Build Coastguard Worker //
412*9880d681SAndroid Build Coastguard Worker // 2) Otherwise, if the LSDA section is writable, we can output the direct
413*9880d681SAndroid Build Coastguard Worker // reference to the typeinfo and allow the dynamic linker to relocate
414*9880d681SAndroid Build Coastguard Worker // it. Since it is in a writable section, the dynamic linker won't
415*9880d681SAndroid Build Coastguard Worker // have a problem.
416*9880d681SAndroid Build Coastguard Worker //
417*9880d681SAndroid Build Coastguard Worker // 3) Finally, if we're in PIC mode and the LDSA section isn't writable,
418*9880d681SAndroid Build Coastguard Worker // we need to use some form of indirection. For example, on Darwin,
419*9880d681SAndroid Build Coastguard Worker // we can output a statically-relocatable reference to a dyld stub. The
420*9880d681SAndroid Build Coastguard Worker // offset to the stub is constant, but the contents are in a section
421*9880d681SAndroid Build Coastguard Worker // that is updated by the dynamic linker. This is easy enough, but we
422*9880d681SAndroid Build Coastguard Worker // need to tell the personality function of the unwinder to indirect
423*9880d681SAndroid Build Coastguard Worker // through the dyld stub.
424*9880d681SAndroid Build Coastguard Worker //
425*9880d681SAndroid Build Coastguard Worker // FIXME: When (3) is actually implemented, we'll have to emit the stubs
426*9880d681SAndroid Build Coastguard Worker // somewhere. This predicate should be moved to a shared location that is
427*9880d681SAndroid Build Coastguard Worker // in target-independent code.
428*9880d681SAndroid Build Coastguard Worker //
429*9880d681SAndroid Build Coastguard Worker TTypeEncoding = Asm->getObjFileLowering().getTTypeEncoding();
430*9880d681SAndroid Build Coastguard Worker TypeFormatSize = Asm->GetSizeOfEncodedValue(TTypeEncoding);
431*9880d681SAndroid Build Coastguard Worker }
432*9880d681SAndroid Build Coastguard Worker
433*9880d681SAndroid Build Coastguard Worker // Begin the exception table.
434*9880d681SAndroid Build Coastguard Worker // Sometimes we want not to emit the data into separate section (e.g. ARM
435*9880d681SAndroid Build Coastguard Worker // EHABI). In this case LSDASection will be NULL.
436*9880d681SAndroid Build Coastguard Worker if (LSDASection)
437*9880d681SAndroid Build Coastguard Worker Asm->OutStreamer->SwitchSection(LSDASection);
438*9880d681SAndroid Build Coastguard Worker Asm->EmitAlignment(2);
439*9880d681SAndroid Build Coastguard Worker
440*9880d681SAndroid Build Coastguard Worker // Emit the LSDA.
441*9880d681SAndroid Build Coastguard Worker MCSymbol *GCCETSym =
442*9880d681SAndroid Build Coastguard Worker Asm->OutContext.getOrCreateSymbol(Twine("GCC_except_table")+
443*9880d681SAndroid Build Coastguard Worker Twine(Asm->getFunctionNumber()));
444*9880d681SAndroid Build Coastguard Worker Asm->OutStreamer->EmitLabel(GCCETSym);
445*9880d681SAndroid Build Coastguard Worker Asm->OutStreamer->EmitLabel(Asm->getCurExceptionSym());
446*9880d681SAndroid Build Coastguard Worker
447*9880d681SAndroid Build Coastguard Worker // Emit the LSDA header.
448*9880d681SAndroid Build Coastguard Worker Asm->EmitEncodingByte(dwarf::DW_EH_PE_omit, "@LPStart");
449*9880d681SAndroid Build Coastguard Worker Asm->EmitEncodingByte(TTypeEncoding, "@TType");
450*9880d681SAndroid Build Coastguard Worker
451*9880d681SAndroid Build Coastguard Worker // The type infos need to be aligned. GCC does this by inserting padding just
452*9880d681SAndroid Build Coastguard Worker // before the type infos. However, this changes the size of the exception
453*9880d681SAndroid Build Coastguard Worker // table, so you need to take this into account when you output the exception
454*9880d681SAndroid Build Coastguard Worker // table size. However, the size is output using a variable length encoding.
455*9880d681SAndroid Build Coastguard Worker // So by increasing the size by inserting padding, you may increase the number
456*9880d681SAndroid Build Coastguard Worker // of bytes used for writing the size. If it increases, say by one byte, then
457*9880d681SAndroid Build Coastguard Worker // you now need to output one less byte of padding to get the type infos
458*9880d681SAndroid Build Coastguard Worker // aligned. However this decreases the size of the exception table. This
459*9880d681SAndroid Build Coastguard Worker // changes the value you have to output for the exception table size. Due to
460*9880d681SAndroid Build Coastguard Worker // the variable length encoding, the number of bytes used for writing the
461*9880d681SAndroid Build Coastguard Worker // length may decrease. If so, you then have to increase the amount of
462*9880d681SAndroid Build Coastguard Worker // padding. And so on. If you look carefully at the GCC code you will see that
463*9880d681SAndroid Build Coastguard Worker // it indeed does this in a loop, going on and on until the values stabilize.
464*9880d681SAndroid Build Coastguard Worker // We chose another solution: don't output padding inside the table like GCC
465*9880d681SAndroid Build Coastguard Worker // does, instead output it before the table.
466*9880d681SAndroid Build Coastguard Worker unsigned SizeTypes = TypeInfos.size() * TypeFormatSize;
467*9880d681SAndroid Build Coastguard Worker unsigned CallSiteTableLengthSize = getULEB128Size(CallSiteTableLength);
468*9880d681SAndroid Build Coastguard Worker unsigned TTypeBaseOffset =
469*9880d681SAndroid Build Coastguard Worker sizeof(int8_t) + // Call site format
470*9880d681SAndroid Build Coastguard Worker CallSiteTableLengthSize + // Call site table length size
471*9880d681SAndroid Build Coastguard Worker CallSiteTableLength + // Call site table length
472*9880d681SAndroid Build Coastguard Worker SizeActions + // Actions size
473*9880d681SAndroid Build Coastguard Worker SizeTypes;
474*9880d681SAndroid Build Coastguard Worker unsigned TTypeBaseOffsetSize = getULEB128Size(TTypeBaseOffset);
475*9880d681SAndroid Build Coastguard Worker unsigned TotalSize =
476*9880d681SAndroid Build Coastguard Worker sizeof(int8_t) + // LPStart format
477*9880d681SAndroid Build Coastguard Worker sizeof(int8_t) + // TType format
478*9880d681SAndroid Build Coastguard Worker (HaveTTData ? TTypeBaseOffsetSize : 0) + // TType base offset size
479*9880d681SAndroid Build Coastguard Worker TTypeBaseOffset; // TType base offset
480*9880d681SAndroid Build Coastguard Worker unsigned SizeAlign = (4 - TotalSize) & 3;
481*9880d681SAndroid Build Coastguard Worker
482*9880d681SAndroid Build Coastguard Worker if (HaveTTData) {
483*9880d681SAndroid Build Coastguard Worker // Account for any extra padding that will be added to the call site table
484*9880d681SAndroid Build Coastguard Worker // length.
485*9880d681SAndroid Build Coastguard Worker Asm->EmitULEB128(TTypeBaseOffset, "@TType base offset", SizeAlign);
486*9880d681SAndroid Build Coastguard Worker SizeAlign = 0;
487*9880d681SAndroid Build Coastguard Worker }
488*9880d681SAndroid Build Coastguard Worker
489*9880d681SAndroid Build Coastguard Worker bool VerboseAsm = Asm->OutStreamer->isVerboseAsm();
490*9880d681SAndroid Build Coastguard Worker
491*9880d681SAndroid Build Coastguard Worker // SjLj Exception handling
492*9880d681SAndroid Build Coastguard Worker if (IsSJLJ) {
493*9880d681SAndroid Build Coastguard Worker Asm->EmitEncodingByte(dwarf::DW_EH_PE_udata4, "Call site");
494*9880d681SAndroid Build Coastguard Worker
495*9880d681SAndroid Build Coastguard Worker // Add extra padding if it wasn't added to the TType base offset.
496*9880d681SAndroid Build Coastguard Worker Asm->EmitULEB128(CallSiteTableLength, "Call site table length", SizeAlign);
497*9880d681SAndroid Build Coastguard Worker
498*9880d681SAndroid Build Coastguard Worker // Emit the landing pad site information.
499*9880d681SAndroid Build Coastguard Worker unsigned idx = 0;
500*9880d681SAndroid Build Coastguard Worker for (SmallVectorImpl<CallSiteEntry>::const_iterator
501*9880d681SAndroid Build Coastguard Worker I = CallSites.begin(), E = CallSites.end(); I != E; ++I, ++idx) {
502*9880d681SAndroid Build Coastguard Worker const CallSiteEntry &S = *I;
503*9880d681SAndroid Build Coastguard Worker
504*9880d681SAndroid Build Coastguard Worker // Offset of the landing pad, counted in 16-byte bundles relative to the
505*9880d681SAndroid Build Coastguard Worker // @LPStart address.
506*9880d681SAndroid Build Coastguard Worker if (VerboseAsm) {
507*9880d681SAndroid Build Coastguard Worker Asm->OutStreamer->AddComment(">> Call Site " + Twine(idx) + " <<");
508*9880d681SAndroid Build Coastguard Worker Asm->OutStreamer->AddComment(" On exception at call site "+Twine(idx));
509*9880d681SAndroid Build Coastguard Worker }
510*9880d681SAndroid Build Coastguard Worker Asm->EmitULEB128(idx);
511*9880d681SAndroid Build Coastguard Worker
512*9880d681SAndroid Build Coastguard Worker // Offset of the first associated action record, relative to the start of
513*9880d681SAndroid Build Coastguard Worker // the action table. This value is biased by 1 (1 indicates the start of
514*9880d681SAndroid Build Coastguard Worker // the action table), and 0 indicates that there are no actions.
515*9880d681SAndroid Build Coastguard Worker if (VerboseAsm) {
516*9880d681SAndroid Build Coastguard Worker if (S.Action == 0)
517*9880d681SAndroid Build Coastguard Worker Asm->OutStreamer->AddComment(" Action: cleanup");
518*9880d681SAndroid Build Coastguard Worker else
519*9880d681SAndroid Build Coastguard Worker Asm->OutStreamer->AddComment(" Action: " +
520*9880d681SAndroid Build Coastguard Worker Twine((S.Action - 1) / 2 + 1));
521*9880d681SAndroid Build Coastguard Worker }
522*9880d681SAndroid Build Coastguard Worker Asm->EmitULEB128(S.Action);
523*9880d681SAndroid Build Coastguard Worker }
524*9880d681SAndroid Build Coastguard Worker } else {
525*9880d681SAndroid Build Coastguard Worker // Itanium LSDA exception handling
526*9880d681SAndroid Build Coastguard Worker
527*9880d681SAndroid Build Coastguard Worker // The call-site table is a list of all call sites that may throw an
528*9880d681SAndroid Build Coastguard Worker // exception (including C++ 'throw' statements) in the procedure
529*9880d681SAndroid Build Coastguard Worker // fragment. It immediately follows the LSDA header. Each entry indicates,
530*9880d681SAndroid Build Coastguard Worker // for a given call, the first corresponding action record and corresponding
531*9880d681SAndroid Build Coastguard Worker // landing pad.
532*9880d681SAndroid Build Coastguard Worker //
533*9880d681SAndroid Build Coastguard Worker // The table begins with the number of bytes, stored as an LEB128
534*9880d681SAndroid Build Coastguard Worker // compressed, unsigned integer. The records immediately follow the record
535*9880d681SAndroid Build Coastguard Worker // count. They are sorted in increasing call-site address. Each record
536*9880d681SAndroid Build Coastguard Worker // indicates:
537*9880d681SAndroid Build Coastguard Worker //
538*9880d681SAndroid Build Coastguard Worker // * The position of the call-site.
539*9880d681SAndroid Build Coastguard Worker // * The position of the landing pad.
540*9880d681SAndroid Build Coastguard Worker // * The first action record for that call site.
541*9880d681SAndroid Build Coastguard Worker //
542*9880d681SAndroid Build Coastguard Worker // A missing entry in the call-site table indicates that a call is not
543*9880d681SAndroid Build Coastguard Worker // supposed to throw.
544*9880d681SAndroid Build Coastguard Worker
545*9880d681SAndroid Build Coastguard Worker // Emit the landing pad call site table.
546*9880d681SAndroid Build Coastguard Worker Asm->EmitEncodingByte(dwarf::DW_EH_PE_udata4, "Call site");
547*9880d681SAndroid Build Coastguard Worker
548*9880d681SAndroid Build Coastguard Worker // Add extra padding if it wasn't added to the TType base offset.
549*9880d681SAndroid Build Coastguard Worker Asm->EmitULEB128(CallSiteTableLength, "Call site table length", SizeAlign);
550*9880d681SAndroid Build Coastguard Worker
551*9880d681SAndroid Build Coastguard Worker unsigned Entry = 0;
552*9880d681SAndroid Build Coastguard Worker for (SmallVectorImpl<CallSiteEntry>::const_iterator
553*9880d681SAndroid Build Coastguard Worker I = CallSites.begin(), E = CallSites.end(); I != E; ++I) {
554*9880d681SAndroid Build Coastguard Worker const CallSiteEntry &S = *I;
555*9880d681SAndroid Build Coastguard Worker
556*9880d681SAndroid Build Coastguard Worker MCSymbol *EHFuncBeginSym = Asm->getFunctionBegin();
557*9880d681SAndroid Build Coastguard Worker
558*9880d681SAndroid Build Coastguard Worker MCSymbol *BeginLabel = S.BeginLabel;
559*9880d681SAndroid Build Coastguard Worker if (!BeginLabel)
560*9880d681SAndroid Build Coastguard Worker BeginLabel = EHFuncBeginSym;
561*9880d681SAndroid Build Coastguard Worker MCSymbol *EndLabel = S.EndLabel;
562*9880d681SAndroid Build Coastguard Worker if (!EndLabel)
563*9880d681SAndroid Build Coastguard Worker EndLabel = Asm->getFunctionEnd();
564*9880d681SAndroid Build Coastguard Worker
565*9880d681SAndroid Build Coastguard Worker // Offset of the call site relative to the previous call site, counted in
566*9880d681SAndroid Build Coastguard Worker // number of 16-byte bundles. The first call site is counted relative to
567*9880d681SAndroid Build Coastguard Worker // the start of the procedure fragment.
568*9880d681SAndroid Build Coastguard Worker if (VerboseAsm)
569*9880d681SAndroid Build Coastguard Worker Asm->OutStreamer->AddComment(">> Call Site " + Twine(++Entry) + " <<");
570*9880d681SAndroid Build Coastguard Worker Asm->EmitLabelDifference(BeginLabel, EHFuncBeginSym, 4);
571*9880d681SAndroid Build Coastguard Worker if (VerboseAsm)
572*9880d681SAndroid Build Coastguard Worker Asm->OutStreamer->AddComment(Twine(" Call between ") +
573*9880d681SAndroid Build Coastguard Worker BeginLabel->getName() + " and " +
574*9880d681SAndroid Build Coastguard Worker EndLabel->getName());
575*9880d681SAndroid Build Coastguard Worker Asm->EmitLabelDifference(EndLabel, BeginLabel, 4);
576*9880d681SAndroid Build Coastguard Worker
577*9880d681SAndroid Build Coastguard Worker // Offset of the landing pad, counted in 16-byte bundles relative to the
578*9880d681SAndroid Build Coastguard Worker // @LPStart address.
579*9880d681SAndroid Build Coastguard Worker if (!S.LPad) {
580*9880d681SAndroid Build Coastguard Worker if (VerboseAsm)
581*9880d681SAndroid Build Coastguard Worker Asm->OutStreamer->AddComment(" has no landing pad");
582*9880d681SAndroid Build Coastguard Worker Asm->OutStreamer->EmitIntValue(0, 4/*size*/);
583*9880d681SAndroid Build Coastguard Worker } else {
584*9880d681SAndroid Build Coastguard Worker if (VerboseAsm)
585*9880d681SAndroid Build Coastguard Worker Asm->OutStreamer->AddComment(Twine(" jumps to ") +
586*9880d681SAndroid Build Coastguard Worker S.LPad->LandingPadLabel->getName());
587*9880d681SAndroid Build Coastguard Worker Asm->EmitLabelDifference(S.LPad->LandingPadLabel, EHFuncBeginSym, 4);
588*9880d681SAndroid Build Coastguard Worker }
589*9880d681SAndroid Build Coastguard Worker
590*9880d681SAndroid Build Coastguard Worker // Offset of the first associated action record, relative to the start of
591*9880d681SAndroid Build Coastguard Worker // the action table. This value is biased by 1 (1 indicates the start of
592*9880d681SAndroid Build Coastguard Worker // the action table), and 0 indicates that there are no actions.
593*9880d681SAndroid Build Coastguard Worker if (VerboseAsm) {
594*9880d681SAndroid Build Coastguard Worker if (S.Action == 0)
595*9880d681SAndroid Build Coastguard Worker Asm->OutStreamer->AddComment(" On action: cleanup");
596*9880d681SAndroid Build Coastguard Worker else
597*9880d681SAndroid Build Coastguard Worker Asm->OutStreamer->AddComment(" On action: " +
598*9880d681SAndroid Build Coastguard Worker Twine((S.Action - 1) / 2 + 1));
599*9880d681SAndroid Build Coastguard Worker }
600*9880d681SAndroid Build Coastguard Worker Asm->EmitULEB128(S.Action);
601*9880d681SAndroid Build Coastguard Worker }
602*9880d681SAndroid Build Coastguard Worker }
603*9880d681SAndroid Build Coastguard Worker
604*9880d681SAndroid Build Coastguard Worker // Emit the Action Table.
605*9880d681SAndroid Build Coastguard Worker int Entry = 0;
606*9880d681SAndroid Build Coastguard Worker for (SmallVectorImpl<ActionEntry>::const_iterator
607*9880d681SAndroid Build Coastguard Worker I = Actions.begin(), E = Actions.end(); I != E; ++I) {
608*9880d681SAndroid Build Coastguard Worker const ActionEntry &Action = *I;
609*9880d681SAndroid Build Coastguard Worker
610*9880d681SAndroid Build Coastguard Worker if (VerboseAsm) {
611*9880d681SAndroid Build Coastguard Worker // Emit comments that decode the action table.
612*9880d681SAndroid Build Coastguard Worker Asm->OutStreamer->AddComment(">> Action Record " + Twine(++Entry) + " <<");
613*9880d681SAndroid Build Coastguard Worker }
614*9880d681SAndroid Build Coastguard Worker
615*9880d681SAndroid Build Coastguard Worker // Type Filter
616*9880d681SAndroid Build Coastguard Worker //
617*9880d681SAndroid Build Coastguard Worker // Used by the runtime to match the type of the thrown exception to the
618*9880d681SAndroid Build Coastguard Worker // type of the catch clauses or the types in the exception specification.
619*9880d681SAndroid Build Coastguard Worker if (VerboseAsm) {
620*9880d681SAndroid Build Coastguard Worker if (Action.ValueForTypeID > 0)
621*9880d681SAndroid Build Coastguard Worker Asm->OutStreamer->AddComment(" Catch TypeInfo " +
622*9880d681SAndroid Build Coastguard Worker Twine(Action.ValueForTypeID));
623*9880d681SAndroid Build Coastguard Worker else if (Action.ValueForTypeID < 0)
624*9880d681SAndroid Build Coastguard Worker Asm->OutStreamer->AddComment(" Filter TypeInfo " +
625*9880d681SAndroid Build Coastguard Worker Twine(Action.ValueForTypeID));
626*9880d681SAndroid Build Coastguard Worker else
627*9880d681SAndroid Build Coastguard Worker Asm->OutStreamer->AddComment(" Cleanup");
628*9880d681SAndroid Build Coastguard Worker }
629*9880d681SAndroid Build Coastguard Worker Asm->EmitSLEB128(Action.ValueForTypeID);
630*9880d681SAndroid Build Coastguard Worker
631*9880d681SAndroid Build Coastguard Worker // Action Record
632*9880d681SAndroid Build Coastguard Worker //
633*9880d681SAndroid Build Coastguard Worker // Self-relative signed displacement in bytes of the next action record,
634*9880d681SAndroid Build Coastguard Worker // or 0 if there is no next action record.
635*9880d681SAndroid Build Coastguard Worker if (VerboseAsm) {
636*9880d681SAndroid Build Coastguard Worker if (Action.NextAction == 0) {
637*9880d681SAndroid Build Coastguard Worker Asm->OutStreamer->AddComment(" No further actions");
638*9880d681SAndroid Build Coastguard Worker } else {
639*9880d681SAndroid Build Coastguard Worker unsigned NextAction = Entry + (Action.NextAction + 1) / 2;
640*9880d681SAndroid Build Coastguard Worker Asm->OutStreamer->AddComment(" Continue to action "+Twine(NextAction));
641*9880d681SAndroid Build Coastguard Worker }
642*9880d681SAndroid Build Coastguard Worker }
643*9880d681SAndroid Build Coastguard Worker Asm->EmitSLEB128(Action.NextAction);
644*9880d681SAndroid Build Coastguard Worker }
645*9880d681SAndroid Build Coastguard Worker
646*9880d681SAndroid Build Coastguard Worker emitTypeInfos(TTypeEncoding);
647*9880d681SAndroid Build Coastguard Worker
648*9880d681SAndroid Build Coastguard Worker Asm->EmitAlignment(2);
649*9880d681SAndroid Build Coastguard Worker }
650*9880d681SAndroid Build Coastguard Worker
emitTypeInfos(unsigned TTypeEncoding)651*9880d681SAndroid Build Coastguard Worker void EHStreamer::emitTypeInfos(unsigned TTypeEncoding) {
652*9880d681SAndroid Build Coastguard Worker const std::vector<const GlobalValue *> &TypeInfos = MMI->getTypeInfos();
653*9880d681SAndroid Build Coastguard Worker const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
654*9880d681SAndroid Build Coastguard Worker
655*9880d681SAndroid Build Coastguard Worker bool VerboseAsm = Asm->OutStreamer->isVerboseAsm();
656*9880d681SAndroid Build Coastguard Worker
657*9880d681SAndroid Build Coastguard Worker int Entry = 0;
658*9880d681SAndroid Build Coastguard Worker // Emit the Catch TypeInfos.
659*9880d681SAndroid Build Coastguard Worker if (VerboseAsm && !TypeInfos.empty()) {
660*9880d681SAndroid Build Coastguard Worker Asm->OutStreamer->AddComment(">> Catch TypeInfos <<");
661*9880d681SAndroid Build Coastguard Worker Asm->OutStreamer->AddBlankLine();
662*9880d681SAndroid Build Coastguard Worker Entry = TypeInfos.size();
663*9880d681SAndroid Build Coastguard Worker }
664*9880d681SAndroid Build Coastguard Worker
665*9880d681SAndroid Build Coastguard Worker for (const GlobalValue *GV : make_range(TypeInfos.rbegin(),
666*9880d681SAndroid Build Coastguard Worker TypeInfos.rend())) {
667*9880d681SAndroid Build Coastguard Worker if (VerboseAsm)
668*9880d681SAndroid Build Coastguard Worker Asm->OutStreamer->AddComment("TypeInfo " + Twine(Entry--));
669*9880d681SAndroid Build Coastguard Worker Asm->EmitTTypeReference(GV, TTypeEncoding);
670*9880d681SAndroid Build Coastguard Worker }
671*9880d681SAndroid Build Coastguard Worker
672*9880d681SAndroid Build Coastguard Worker // Emit the Exception Specifications.
673*9880d681SAndroid Build Coastguard Worker if (VerboseAsm && !FilterIds.empty()) {
674*9880d681SAndroid Build Coastguard Worker Asm->OutStreamer->AddComment(">> Filter TypeInfos <<");
675*9880d681SAndroid Build Coastguard Worker Asm->OutStreamer->AddBlankLine();
676*9880d681SAndroid Build Coastguard Worker Entry = 0;
677*9880d681SAndroid Build Coastguard Worker }
678*9880d681SAndroid Build Coastguard Worker for (std::vector<unsigned>::const_iterator
679*9880d681SAndroid Build Coastguard Worker I = FilterIds.begin(), E = FilterIds.end(); I < E; ++I) {
680*9880d681SAndroid Build Coastguard Worker unsigned TypeID = *I;
681*9880d681SAndroid Build Coastguard Worker if (VerboseAsm) {
682*9880d681SAndroid Build Coastguard Worker --Entry;
683*9880d681SAndroid Build Coastguard Worker if (isFilterEHSelector(TypeID))
684*9880d681SAndroid Build Coastguard Worker Asm->OutStreamer->AddComment("FilterInfo " + Twine(Entry));
685*9880d681SAndroid Build Coastguard Worker }
686*9880d681SAndroid Build Coastguard Worker
687*9880d681SAndroid Build Coastguard Worker Asm->EmitULEB128(TypeID);
688*9880d681SAndroid Build Coastguard Worker }
689*9880d681SAndroid Build Coastguard Worker }
690