blob: 81536894e09e63dbfeeffe8beb1fd2d02ba4eca7 [file] [log] [blame]
Bill Wendlingeb907212009-05-15 01:12:28 +00001//===-- CodeGen/AsmPrinter/DwarfException.cpp - Dwarf Exception Impl ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains support for writing dwarf exception info into asm files.
11//
12//===----------------------------------------------------------------------===//
13
14#include "DwarfException.h"
15#include "llvm/Module.h"
16#include "llvm/CodeGen/MachineModuleInfo.h"
17#include "llvm/CodeGen/MachineFrameInfo.h"
David Greenefc4da0c2009-08-19 21:55:33 +000018#include "llvm/CodeGen/MachineFunction.h"
Bill Wendlingeb907212009-05-15 01:12:28 +000019#include "llvm/CodeGen/MachineLocation.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000020#include "llvm/MC/MCStreamer.h"
Bill Wendlingeb907212009-05-15 01:12:28 +000021#include "llvm/Target/TargetAsmInfo.h"
Bill Wendlingeb907212009-05-15 01:12:28 +000022#include "llvm/Target/TargetData.h"
23#include "llvm/Target/TargetFrameInfo.h"
Chris Lattnerd5bbb072009-08-02 01:34:32 +000024#include "llvm/Target/TargetLoweringObjectFile.h"
Bill Wendlingeb907212009-05-15 01:12:28 +000025#include "llvm/Target/TargetOptions.h"
Chris Lattnerd5bbb072009-08-02 01:34:32 +000026#include "llvm/Target/TargetRegisterInfo.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000027#include "llvm/Support/Dwarf.h"
28#include "llvm/Support/Timer.h"
29#include "llvm/Support/raw_ostream.h"
Bill Wendlingeb907212009-05-15 01:12:28 +000030#include "llvm/ADT/StringExtras.h"
31using namespace llvm;
32
33static TimerGroup &getDwarfTimerGroup() {
34 static TimerGroup DwarfTimerGroup("Dwarf Exception");
35 return DwarfTimerGroup;
36}
37
Bill Wendlingbc0d23a2009-05-15 01:18:50 +000038DwarfException::DwarfException(raw_ostream &OS, AsmPrinter *A,
39 const TargetAsmInfo *T)
40 : Dwarf(OS, A, T, "eh"), shouldEmitTable(false), shouldEmitMoves(false),
41 shouldEmitTableModule(false), shouldEmitMovesModule(false),
42 ExceptionTimer(0) {
43 if (TimePassesIsEnabled)
44 ExceptionTimer = new Timer("Dwarf Exception Writer",
45 getDwarfTimerGroup());
46}
47
48DwarfException::~DwarfException() {
49 delete ExceptionTimer;
50}
51
Bill Wendlingeb907212009-05-15 01:12:28 +000052void DwarfException::EmitCommonEHFrame(const Function *Personality,
53 unsigned Index) {
54 // Size and sign of stack growth.
55 int stackGrowth =
56 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
57 TargetFrameInfo::StackGrowsUp ?
58 TD->getPointerSize() : -TD->getPointerSize();
59
60 // Begin eh frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +000061 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getEHFrameSection());
Bill Wendlingeb907212009-05-15 01:12:28 +000062
Chris Lattnere2cf37b2009-07-17 20:46:40 +000063 if (TAI->is_EHSymbolPrivate())
64 O << TAI->getPrivateGlobalPrefix();
Bill Wendlingeb907212009-05-15 01:12:28 +000065
66 O << "EH_frame" << Index << ":\n";
67 EmitLabel("section_eh_frame", Index);
68
69 // Define base labels.
70 EmitLabel("eh_frame_common", Index);
71
72 // Define the eh frame length.
73 EmitDifference("eh_frame_common_end", Index,
74 "eh_frame_common_begin", Index, true);
75 Asm->EOL("Length of Common Information Entry");
76
77 // EH frame header.
78 EmitLabel("eh_frame_common_begin", Index);
79 Asm->EmitInt32((int)0);
80 Asm->EOL("CIE Identifier Tag");
81 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
82 Asm->EOL("CIE Version");
83
84 // The personality presence indicates that language specific information will
85 // show up in the eh frame.
86 Asm->EmitString(Personality ? "zPLR" : "zR");
87 Asm->EOL("CIE Augmentation");
88
89 // Round out reader.
90 Asm->EmitULEB128Bytes(1);
91 Asm->EOL("CIE Code Alignment Factor");
92 Asm->EmitSLEB128Bytes(stackGrowth);
93 Asm->EOL("CIE Data Alignment Factor");
94 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
95 Asm->EOL("CIE Return Address Column");
96
97 // If there is a personality, we need to indicate the functions location.
98 if (Personality) {
99 Asm->EmitULEB128Bytes(7);
100 Asm->EOL("Augmentation Size");
101
102 if (TAI->getNeedsIndirectEncoding()) {
103 Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4 |
104 dwarf::DW_EH_PE_indirect);
105 Asm->EOL("Personality (pcrel sdata4 indirect)");
106 } else {
107 Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
108 Asm->EOL("Personality (pcrel sdata4)");
109 }
110
111 PrintRelDirective(true);
112 O << TAI->getPersonalityPrefix();
113 Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
114 O << TAI->getPersonalitySuffix();
115 if (strcmp(TAI->getPersonalitySuffix(), "+4@GOTPCREL"))
116 O << "-" << TAI->getPCSymbol();
117 Asm->EOL("Personality");
118
119 Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
120 Asm->EOL("LSDA Encoding (pcrel sdata4)");
121
122 Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
123 Asm->EOL("FDE Encoding (pcrel sdata4)");
124 } else {
125 Asm->EmitULEB128Bytes(1);
126 Asm->EOL("Augmentation Size");
127
128 Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
129 Asm->EOL("FDE Encoding (pcrel sdata4)");
130 }
131
132 // Indicate locations of general callee saved registers in frame.
133 std::vector<MachineMove> Moves;
134 RI->getInitialFrameState(Moves);
135 EmitFrameMoves(NULL, 0, Moves, true);
136
137 // On Darwin the linker honors the alignment of eh_frame, which means it must
138 // be 8-byte on 64-bit targets to match what gcc does. Otherwise you get
139 // holes which confuse readers of eh_frame.
140 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
141 0, 0, false);
142 EmitLabel("eh_frame_common_end", Index);
143
144 Asm->EOL();
145}
146
147/// EmitEHFrame - Emit function exception frame information.
148///
149void DwarfException::EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) {
150 assert(!EHFrameInfo.function->hasAvailableExternallyLinkage() &&
151 "Should not emit 'available externally' functions at all");
152
Chris Lattner3e0f60b2009-07-17 21:00:50 +0000153 const Function *TheFunc = EHFrameInfo.function;
154
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000155 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getEHFrameSection());
Chris Lattner6a66e582009-08-18 06:13:03 +0000156
Bill Wendlingeb907212009-05-15 01:12:28 +0000157 // Externally visible entry into the functions eh frame info. If the
158 // corresponding function is static, this should not be externally visible.
Chris Lattner3e0f60b2009-07-17 21:00:50 +0000159 if (!TheFunc->hasLocalLinkage())
Bill Wendlingeb907212009-05-15 01:12:28 +0000160 if (const char *GlobalEHDirective = TAI->getGlobalEHDirective())
161 O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
Bill Wendlingeb907212009-05-15 01:12:28 +0000162
163 // If corresponding function is weak definition, this should be too.
Chris Lattner3e0f60b2009-07-17 21:00:50 +0000164 if (TheFunc->isWeakForLinker() && TAI->getWeakDefDirective())
Bill Wendlingeb907212009-05-15 01:12:28 +0000165 O << TAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
166
167 // If there are no calls then you can't unwind. This may mean we can omit the
168 // EH Frame, but some environments do not handle weak absolute symbols. If
169 // UnwindTablesMandatory is set we cannot do this optimization; the unwind
170 // info is to be available for non-EH uses.
Chris Lattner3e0f60b2009-07-17 21:00:50 +0000171 if (!EHFrameInfo.hasCalls && !UnwindTablesMandatory &&
172 (!TheFunc->isWeakForLinker() ||
Bill Wendlingeb907212009-05-15 01:12:28 +0000173 !TAI->getWeakDefDirective() ||
174 TAI->getSupportsWeakOmittedEHFrame())) {
175 O << EHFrameInfo.FnName << " = 0\n";
176 // This name has no connection to the function, so it might get
177 // dead-stripped when the function is not, erroneously. Prohibit
178 // dead-stripping unconditionally.
179 if (const char *UsedDirective = TAI->getUsedDirective())
180 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
181 } else {
182 O << EHFrameInfo.FnName << ":\n";
183
184 // EH frame header.
185 EmitDifference("eh_frame_end", EHFrameInfo.Number,
186 "eh_frame_begin", EHFrameInfo.Number, true);
187 Asm->EOL("Length of Frame Information Entry");
188
189 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
190
Chris Lattnera4ff5e42009-07-17 20:53:51 +0000191 EmitSectionOffset("eh_frame_begin", "eh_frame_common",
192 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
193 true, true, false);
Bill Wendlingeb907212009-05-15 01:12:28 +0000194
195 Asm->EOL("FDE CIE offset");
196
197 EmitReference("eh_func_begin", EHFrameInfo.Number, true, true);
198 Asm->EOL("FDE initial location");
199 EmitDifference("eh_func_end", EHFrameInfo.Number,
200 "eh_func_begin", EHFrameInfo.Number, true);
201 Asm->EOL("FDE address range");
202
203 // If there is a personality and landing pads then point to the language
204 // specific data area in the exception table.
205 if (EHFrameInfo.PersonalityIndex) {
206 Asm->EmitULEB128Bytes(4);
207 Asm->EOL("Augmentation size");
208
209 if (EHFrameInfo.hasLandingPads)
210 EmitReference("exception", EHFrameInfo.Number, true, true);
211 else
212 Asm->EmitInt32((int)0);
213 Asm->EOL("Language Specific Data Area");
214 } else {
215 Asm->EmitULEB128Bytes(0);
216 Asm->EOL("Augmentation size");
217 }
218
219 // Indicate locations of function specific callee saved registers in frame.
220 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves,
221 true);
222
223 // On Darwin the linker honors the alignment of eh_frame, which means it
224 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise you
225 // get holes which confuse readers of eh_frame.
226 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
227 0, 0, false);
228 EmitLabel("eh_frame_end", EHFrameInfo.Number);
229
230 // If the function is marked used, this table should be also. We cannot
231 // make the mark unconditional in this case, since retaining the table also
232 // retains the function in this case, and there is code around that depends
233 // on unused functions (calling undefined externals) being dead-stripped to
234 // link correctly. Yes, there really is.
Chris Lattner401e10c2009-07-20 06:14:25 +0000235 if (MMI->isUsedFunction(EHFrameInfo.function))
Bill Wendlingeb907212009-05-15 01:12:28 +0000236 if (const char *UsedDirective = TAI->getUsedDirective())
237 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
238 }
239}
240
Bill Wendlingeb907212009-05-15 01:12:28 +0000241/// SharedTypeIds - How many leading type ids two landing pads have in common.
242unsigned DwarfException::SharedTypeIds(const LandingPadInfo *L,
243 const LandingPadInfo *R) {
244 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
245 unsigned LSize = LIds.size(), RSize = RIds.size();
246 unsigned MinSize = LSize < RSize ? LSize : RSize;
247 unsigned Count = 0;
248
249 for (; Count != MinSize; ++Count)
250 if (LIds[Count] != RIds[Count])
251 return Count;
252
253 return Count;
254}
255
256/// PadLT - Order landing pads lexicographically by type id.
257bool DwarfException::PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
258 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
259 unsigned LSize = LIds.size(), RSize = RIds.size();
260 unsigned MinSize = LSize < RSize ? LSize : RSize;
261
262 for (unsigned i = 0; i != MinSize; ++i)
263 if (LIds[i] != RIds[i])
264 return LIds[i] < RIds[i];
265
266 return LSize < RSize;
267}
268
Bill Wendlingd4609622009-07-28 23:23:00 +0000269/// ComputeActionsTable - Compute the actions table and gather the first action
270/// index for each landing pad site.
Bill Wendlingade025c2009-07-29 00:31:35 +0000271unsigned DwarfException::
272ComputeActionsTable(const SmallVectorImpl<const LandingPadInfo*> &LandingPads,
273 SmallVectorImpl<ActionEntry> &Actions,
274 SmallVectorImpl<unsigned> &FirstActions) {
Bill Wendlinga583c552009-08-20 22:02:24 +0000275
276 // The action table follows the call-site table in the LSDA. The individual
277 // records are of two types:
278 //
279 // * Catch clause
280 // * Exception specification
281 //
282 // The two record kinds have the same format, with only small differences.
283 // They are distinguished by the "switch value" field: Catch clauses
284 // (TypeInfos) have strictly positive switch values, and exception
285 // specifications (FilterIds) have strictly negative switch values. Value 0
286 // indicates a catch-all clause.
287 //
Bill Wendling5e953dd2009-07-28 23:22:13 +0000288 // Negative type IDs index into FilterIds. Positive type IDs index into
289 // TypeInfos. The value written for a positive type ID is just the type ID
290 // itself. For a negative type ID, however, the value written is the
Bill Wendlingeb907212009-05-15 01:12:28 +0000291 // (negative) byte offset of the corresponding FilterIds entry. The byte
Bill Wendling5e953dd2009-07-28 23:22:13 +0000292 // offset is usually equal to the type ID (because the FilterIds entries are
293 // written using a variable width encoding, which outputs one byte per entry
294 // as long as the value written is not too large) but can differ. This kind
295 // of complication does not occur for positive type IDs because type infos are
Bill Wendlingeb907212009-05-15 01:12:28 +0000296 // output using a fixed width encoding. FilterOffsets[i] holds the byte
297 // offset corresponding to FilterIds[i].
Bill Wendling409914b2009-07-29 21:19:44 +0000298
299 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
Bill Wendlingeb907212009-05-15 01:12:28 +0000300 SmallVector<int, 16> FilterOffsets;
301 FilterOffsets.reserve(FilterIds.size());
302 int Offset = -1;
Bill Wendling409914b2009-07-29 21:19:44 +0000303
304 for (std::vector<unsigned>::const_iterator
305 I = FilterIds.begin(), E = FilterIds.end(); I != E; ++I) {
Bill Wendlingeb907212009-05-15 01:12:28 +0000306 FilterOffsets.push_back(Offset);
307 Offset -= TargetAsmInfo::getULEB128Size(*I);
308 }
309
Bill Wendlingeb907212009-05-15 01:12:28 +0000310 FirstActions.reserve(LandingPads.size());
311
312 int FirstAction = 0;
313 unsigned SizeActions = 0;
Bill Wendling5e953dd2009-07-28 23:22:13 +0000314 const LandingPadInfo *PrevLPI = 0;
Bill Wendling409914b2009-07-29 21:19:44 +0000315
Bill Wendling5cff4872009-07-28 23:44:43 +0000316 for (SmallVectorImpl<const LandingPadInfo *>::const_iterator
Bill Wendling5e953dd2009-07-28 23:22:13 +0000317 I = LandingPads.begin(), E = LandingPads.end(); I != E; ++I) {
318 const LandingPadInfo *LPI = *I;
319 const std::vector<int> &TypeIds = LPI->TypeIds;
320 const unsigned NumShared = PrevLPI ? SharedTypeIds(LPI, PrevLPI) : 0;
Bill Wendlingeb907212009-05-15 01:12:28 +0000321 unsigned SizeSiteActions = 0;
322
323 if (NumShared < TypeIds.size()) {
324 unsigned SizeAction = 0;
325 ActionEntry *PrevAction = 0;
326
327 if (NumShared) {
Bill Wendling5e953dd2009-07-28 23:22:13 +0000328 const unsigned SizePrevIds = PrevLPI->TypeIds.size();
Bill Wendlingeb907212009-05-15 01:12:28 +0000329 assert(Actions.size());
330 PrevAction = &Actions.back();
331 SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) +
332 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
333
334 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
335 SizeAction -=
336 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
337 SizeAction += -PrevAction->NextAction;
338 PrevAction = PrevAction->Previous;
339 }
340 }
341
342 // Compute the actions.
Bill Wendling5e953dd2009-07-28 23:22:13 +0000343 for (unsigned J = NumShared, M = TypeIds.size(); J != M; ++J) {
344 int TypeID = TypeIds[J];
345 assert(-1 - TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
Bill Wendlingeb907212009-05-15 01:12:28 +0000346 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
347 unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID);
348
349 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
350 SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction);
351 SizeSiteActions += SizeAction;
352
Bill Wendlinga583c552009-08-20 22:02:24 +0000353 ActionEntry Action = { ValueForTypeID, NextAction, PrevAction };
Bill Wendlingeb907212009-05-15 01:12:28 +0000354 Actions.push_back(Action);
Bill Wendlingeb907212009-05-15 01:12:28 +0000355 PrevAction = &Actions.back();
356 }
357
358 // Record the first action of the landing pad site.
359 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
360 } // else identical - re-use previous FirstAction
361
Bill Wendlinga583c552009-08-20 22:02:24 +0000362 // Information used when created the call-site table. The action record
363 // field of the call site record is the offset of the first associated
364 // action record, relative to the start of the actions table. This value is
365 // biased by 1 (1 in dicating the start of the actions table), and 0
366 // indicates that there are no actions.
Bill Wendlingeb907212009-05-15 01:12:28 +0000367 FirstActions.push_back(FirstAction);
368
369 // Compute this sites contribution to size.
370 SizeActions += SizeSiteActions;
Bill Wendling5e953dd2009-07-28 23:22:13 +0000371
372 PrevLPI = LPI;
Bill Wendlingeb907212009-05-15 01:12:28 +0000373 }
374
Bill Wendling5e953dd2009-07-28 23:22:13 +0000375 return SizeActions;
376}
377
Bill Wendlingade025c2009-07-29 00:31:35 +0000378/// ComputeCallSiteTable - Compute the call-site table. The entry for an invoke
Bill Wendlinga583c552009-08-20 22:02:24 +0000379/// has a try-range containing the call, a non-zero landing pad, and an
Bill Wendlingade025c2009-07-29 00:31:35 +0000380/// appropriate action. The entry for an ordinary call has a try-range
381/// containing the call and zero for the landing pad and the action. Calls
382/// marked 'nounwind' have no entry and must not be contained in the try-range
383/// of any entry - they form gaps in the table. Entries must be ordered by
384/// try-range address.
385void DwarfException::
386ComputeCallSiteTable(SmallVectorImpl<CallSiteEntry> &CallSites,
387 const RangeMapType &PadMap,
388 const SmallVectorImpl<const LandingPadInfo *> &LandingPads,
389 const SmallVectorImpl<unsigned> &FirstActions) {
Bill Wendlingeb907212009-05-15 01:12:28 +0000390 // The end label of the previous invoke or nounwind try-range.
391 unsigned LastLabel = 0;
392
393 // Whether there is a potentially throwing instruction (currently this means
394 // an ordinary call) between the end of the previous try-range and now.
395 bool SawPotentiallyThrowing = false;
396
Bill Wendling5cff4872009-07-28 23:44:43 +0000397 // Whether the last CallSite entry was for an invoke.
Bill Wendlingeb907212009-05-15 01:12:28 +0000398 bool PreviousIsInvoke = false;
399
400 // Visit all instructions in order of address.
401 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
402 I != E; ++I) {
403 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
404 MI != E; ++MI) {
405 if (!MI->isLabel()) {
406 SawPotentiallyThrowing |= MI->getDesc().isCall();
407 continue;
408 }
409
410 unsigned BeginLabel = MI->getOperand(0).getImm();
411 assert(BeginLabel && "Invalid label!");
412
413 // End of the previous try-range?
414 if (BeginLabel == LastLabel)
415 SawPotentiallyThrowing = false;
416
417 // Beginning of a new try-range?
418 RangeMapType::iterator L = PadMap.find(BeginLabel);
419 if (L == PadMap.end())
420 // Nope, it was just some random label.
421 continue;
422
Bill Wendlinga583c552009-08-20 22:02:24 +0000423 const PadRange &P = L->second;
Bill Wendlingeb907212009-05-15 01:12:28 +0000424 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
Bill Wendlingeb907212009-05-15 01:12:28 +0000425 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
426 "Inconsistent landing pad map!");
427
Bill Wendlinga583c552009-08-20 22:02:24 +0000428 // For Dwarf exception handling (SjLj handling doesn't use this). If some
429 // instruction between the previous try-range and this one may throw,
430 // create a call-site entry with no landing pad for the region between the
431 // try-ranges.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000432 if (SawPotentiallyThrowing &&
433 TAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) {
Bill Wendlinga583c552009-08-20 22:02:24 +0000434 CallSiteEntry Site = { LastLabel, BeginLabel, 0, 0 };
Bill Wendlingeb907212009-05-15 01:12:28 +0000435 CallSites.push_back(Site);
436 PreviousIsInvoke = false;
437 }
438
439 LastLabel = LandingPad->EndLabels[P.RangeIndex];
440 assert(BeginLabel && LastLabel && "Invalid landing pad!");
441
442 if (LandingPad->LandingPadLabel) {
443 // This try-range is for an invoke.
Bill Wendlinga583c552009-08-20 22:02:24 +0000444 CallSiteEntry Site = {
445 BeginLabel,
446 LastLabel,
447 LandingPad->LandingPadLabel,
448 FirstActions[P.PadIndex]
449 };
Bill Wendlingeb907212009-05-15 01:12:28 +0000450
451 // Try to merge with the previous call-site.
452 if (PreviousIsInvoke) {
453 CallSiteEntry &Prev = CallSites.back();
454 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
455 // Extend the range of the previous entry.
456 Prev.EndLabel = Site.EndLabel;
457 continue;
458 }
459 }
460
461 // Otherwise, create a new call-site.
462 CallSites.push_back(Site);
463 PreviousIsInvoke = true;
464 } else {
465 // Create a gap.
466 PreviousIsInvoke = false;
467 }
468 }
469 }
470
471 // If some instruction between the previous try-range and the end of the
472 // function may throw, create a call-site entry with no landing pad for the
473 // region following the try-range.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000474 if (SawPotentiallyThrowing &&
475 TAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) {
Bill Wendlinga583c552009-08-20 22:02:24 +0000476 CallSiteEntry Site = { LastLabel, 0, 0, 0 };
Bill Wendlingeb907212009-05-15 01:12:28 +0000477 CallSites.push_back(Site);
478 }
Bill Wendlingade025c2009-07-29 00:31:35 +0000479}
480
Bill Wendling0dafca92009-07-29 00:50:05 +0000481/// EmitExceptionTable - Emit landing pads and actions.
482///
483/// The general organization of the table is complex, but the basic concepts are
484/// easy. First there is a header which describes the location and organization
485/// of the three components that follow.
486///
487/// 1. The landing pad site information describes the range of code covered by
488/// the try. In our case it's an accumulation of the ranges covered by the
489/// invokes in the try. There is also a reference to the landing pad that
490/// handles the exception once processed. Finally an index into the actions
491/// table.
Bill Wendlinga583c552009-08-20 22:02:24 +0000492/// 2. The action table, in our case, is composed of pairs of type IDs and next
Bill Wendling0dafca92009-07-29 00:50:05 +0000493/// action offset. Starting with the action index from the landing pad
Bill Wendlinga583c552009-08-20 22:02:24 +0000494/// site, each type ID is checked for a match to the current exception. If
Bill Wendling0dafca92009-07-29 00:50:05 +0000495/// it matches then the exception and type id are passed on to the landing
496/// pad. Otherwise the next action is looked up. This chain is terminated
497/// with a next action of zero. If no type id is found the the frame is
498/// unwound and handling continues.
Bill Wendlinga583c552009-08-20 22:02:24 +0000499/// 3. Type ID table contains references to all the C++ typeinfo for all
Bill Wendling0dafca92009-07-29 00:50:05 +0000500/// catches in the function. This tables is reversed indexed base 1.
Bill Wendlingade025c2009-07-29 00:31:35 +0000501void DwarfException::EmitExceptionTable() {
502 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
503 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
504 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
505 if (PadInfos.empty()) return;
506
507 // Sort the landing pads in order of their type ids. This is used to fold
508 // duplicate actions.
509 SmallVector<const LandingPadInfo *, 64> LandingPads;
510 LandingPads.reserve(PadInfos.size());
511
512 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
513 LandingPads.push_back(&PadInfos[i]);
514
515 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
516
517 // Compute the actions table and gather the first action index for each
518 // landing pad site.
519 SmallVector<ActionEntry, 32> Actions;
520 SmallVector<unsigned, 64> FirstActions;
521 unsigned SizeActions = ComputeActionsTable(LandingPads, Actions, FirstActions);
522
523 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
524 // by try-range labels when lowered). Ordinary calls do not, so appropriate
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000525 // try-ranges for them need be deduced when using Dwarf exception handling.
Bill Wendlingade025c2009-07-29 00:31:35 +0000526 RangeMapType PadMap;
527 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
528 const LandingPadInfo *LandingPad = LandingPads[i];
529 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
530 unsigned BeginLabel = LandingPad->BeginLabels[j];
531 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
532 PadRange P = { i, j };
533 PadMap[BeginLabel] = P;
534 }
535 }
536
537 // Compute the call-site table.
538 SmallVector<CallSiteEntry, 64> CallSites;
Jim Grosbach8b818d72009-08-17 16:41:22 +0000539 ComputeCallSiteTable(CallSites, PadMap, LandingPads, FirstActions);
Bill Wendlingeb907212009-05-15 01:12:28 +0000540
541 // Final tallies.
542
543 // Call sites.
544 const unsigned SiteStartSize = sizeof(int32_t); // DW_EH_PE_udata4
545 const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4
546 const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000547 unsigned SizeSites;
Jim Grosbachbff39232009-08-12 17:38:44 +0000548
549 bool HaveTTData = (TAI->getExceptionHandlingType() == ExceptionHandling::SjLj)
550 ? (!TypeInfos.empty() || !FilterIds.empty()) : true;
551
552
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000553 if (TAI->getExceptionHandlingType() == ExceptionHandling::SjLj) {
Jim Grosbach8b818d72009-08-17 16:41:22 +0000554 SizeSites = 0;
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000555 } else
556 SizeSites = CallSites.size() *
557 (SiteStartSize + SiteLengthSize + LandingPadSize);
558 for (unsigned i = 0, e = CallSites.size(); i < e; ++i) {
Bill Wendlingeb907212009-05-15 01:12:28 +0000559 SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action);
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000560 if (TAI->getExceptionHandlingType() == ExceptionHandling::SjLj)
561 SizeSites += TargetAsmInfo::getULEB128Size(i);
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000562 }
Bill Wendlingeb907212009-05-15 01:12:28 +0000563 // Type infos.
564 const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
565 unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
566
567 unsigned TypeOffset = sizeof(int8_t) + // Call site format
568 TargetAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
569 SizeSites + SizeActions + SizeTypes;
570
571 unsigned TotalSize = sizeof(int8_t) + // LPStart format
572 sizeof(int8_t) + // TType format
Jim Grosbachbff39232009-08-12 17:38:44 +0000573 (HaveTTData ?
574 TargetAsmInfo::getULEB128Size(TypeOffset) : 0) + // TType base offset
Bill Wendlingeb907212009-05-15 01:12:28 +0000575 TypeOffset;
576
577 unsigned SizeAlign = (4 - TotalSize) & 3;
578
579 // Begin the exception table.
Chris Lattnerd5bbb072009-08-02 01:34:32 +0000580 const MCSection *LSDASection = Asm->getObjFileLowering().getLSDASection();
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000581 Asm->OutStreamer.SwitchSection(LSDASection);
Bill Wendlingeb907212009-05-15 01:12:28 +0000582 Asm->EmitAlignment(2, 0, 0, false);
583 O << "GCC_except_table" << SubprogramCount << ":\n";
584
585 for (unsigned i = 0; i != SizeAlign; ++i) {
586 Asm->EmitInt8(0);
587 Asm->EOL("Padding");
Bill Wendlingc5800a82009-07-28 21:54:03 +0000588 }
Bill Wendlingeb907212009-05-15 01:12:28 +0000589
590 EmitLabel("exception", SubprogramCount);
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000591 if (TAI->getExceptionHandlingType() == ExceptionHandling::SjLj) {
592 std::string SjLjName = "_lsda_";
593 SjLjName += MF->getFunction()->getName().str();
594 EmitLabel(SjLjName.c_str(), 0);
595 }
Bill Wendlingeb907212009-05-15 01:12:28 +0000596
597 // Emit the header.
598 Asm->EmitInt8(dwarf::DW_EH_PE_omit);
Bill Wendlinga583c552009-08-20 22:02:24 +0000599 Asm->EOL("@LPStart format (DW_EH_PE_omit)");
Bill Wendlingb0d9c3e2009-07-28 22:23:45 +0000600
Bill Wendlingade025c2009-07-29 00:31:35 +0000601#if 0
Chris Lattner81c9a062009-07-31 22:03:47 +0000602 if (TypeInfos.empty() && FilterIds.empty()) {
Chris Lattnerad88bc42009-08-02 03:59:56 +0000603 // If there are no typeinfos or filters, there is nothing to emit, optimize
604 // by specifying the "omit" encoding.
Chris Lattner81c9a062009-07-31 22:03:47 +0000605 Asm->EmitInt8(dwarf::DW_EH_PE_omit);
Bill Wendlinga583c552009-08-20 22:02:24 +0000606 Asm->EOL("@TType format (DW_EH_PE_omit)");
Chris Lattner81c9a062009-07-31 22:03:47 +0000607 } else {
Chris Lattnerad88bc42009-08-02 03:59:56 +0000608 // Okay, we have actual filters or typeinfos to emit. As such, we need to
609 // pick a type encoding for them. We're about to emit a list of pointers to
610 // typeinfo objects at the end of the LSDA. However, unless we're in static
611 // mode, this reference will require a relocation by the dynamic linker.
Chris Lattner46b754c2009-07-31 22:18:14 +0000612 //
Chris Lattnerad88bc42009-08-02 03:59:56 +0000613 // Because of this, we have a couple of options:
614 // 1) If we are in -static mode, we can always use an absolute reference
615 // from the LSDA, because the static linker will resolve it.
616 // 2) Otherwise, if the LSDA section is writable, we can output the direct
617 // reference to the typeinfo and allow the dynamic linker to relocate
618 // it. Since it is in a writable section, the dynamic linker won't
619 // have a problem.
620 // 3) Finally, if we're in PIC mode and the LDSA section isn't writable,
621 // we need to use some form of indirection. For example, on Darwin,
622 // we can output a statically-relocatable reference to a dyld stub. The
623 // offset to the stub is constant, but the contents are in a section
624 // that is updated by the dynamic linker. This is easy enough, but we
625 // need to tell the personality function of the unwinder to indirect
626 // through the dyld stub.
627 //
628 // FIXME: When this is actually implemented, we'll have to emit the stubs
629 // somewhere. This predicate should be moved to a shared location that is
630 // in target-independent code.
631 //
632 if (LSDASection->isWritable() ||
633 Asm->TM.getRelocationModel() == Reloc::Static) {
634 Asm->EmitInt8(DW_EH_PE_absptr);
635 Asm->EOL("TType format (DW_EH_PE_absptr)");
636 } else {
637 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_indirect | DW_EH_PE_sdata4);
638 Asm->EOL("TType format (DW_EH_PE_pcrel | DW_EH_PE_indirect"
639 " | DW_EH_PE_sdata4)");
640 }
Bill Wendlingb0d9c3e2009-07-28 22:23:45 +0000641 Asm->EmitULEB128Bytes(TypeOffset);
642 Asm->EOL("TType base offset");
Bill Wendlingb0d9c3e2009-07-28 22:23:45 +0000643 }
Bill Wendlingade025c2009-07-29 00:31:35 +0000644#else
Jim Grosbachbff39232009-08-12 17:38:44 +0000645 // For SjLj exceptions, if there is no TypeInfo, then we just explicitly
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000646 // say that we're omitting that bit.
647 // FIXME: does this apply to Dwarf also? The above #if 0 implies yes?
Jim Grosbachbff39232009-08-12 17:38:44 +0000648 if (!HaveTTData) {
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000649 Asm->EmitInt8(dwarf::DW_EH_PE_omit);
Bill Wendlinga583c552009-08-20 22:02:24 +0000650 Asm->EOL("@TType format (DW_EH_PE_omit)");
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000651 } else {
652 Asm->EmitInt8(dwarf::DW_EH_PE_absptr);
Bill Wendlinga583c552009-08-20 22:02:24 +0000653 Asm->EOL("@TType format (DW_EH_PE_absptr)");
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000654 Asm->EmitULEB128Bytes(TypeOffset);
Bill Wendlinga583c552009-08-20 22:02:24 +0000655 Asm->EOL("@TType base offset");
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000656 }
Bill Wendlingade025c2009-07-29 00:31:35 +0000657#endif
Bill Wendlingb0d9c3e2009-07-28 22:23:45 +0000658
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000659 // SjLj Exception handilng
660 if (TAI->getExceptionHandlingType() == ExceptionHandling::SjLj) {
661 Asm->EmitInt8(dwarf::DW_EH_PE_udata4);
662 Asm->EOL("Call site format (DW_EH_PE_udata4)");
663 Asm->EmitULEB128Bytes(SizeSites);
Bill Wendlinga583c552009-08-20 22:02:24 +0000664 Asm->EOL("Call site table length");
Bill Wendlingeb907212009-05-15 01:12:28 +0000665
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000666 // Emit the landing pad site information.
Jim Grosbach8b818d72009-08-17 16:41:22 +0000667 unsigned idx = 0;
668 for (SmallVectorImpl<CallSiteEntry>::const_iterator
669 I = CallSites.begin(), E = CallSites.end(); I != E; ++I, ++idx) {
670 const CallSiteEntry &S = *I;
Bill Wendlinga583c552009-08-20 22:02:24 +0000671
672 // Offset of the landing pad, counted in 16-byte bundles relative to the
673 // @LPStart address.
Jim Grosbach8b818d72009-08-17 16:41:22 +0000674 Asm->EmitULEB128Bytes(idx);
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000675 Asm->EOL("Landing pad");
Bill Wendlinga583c552009-08-20 22:02:24 +0000676
677 // Offset of the first associated action record, relative to the start of
678 // the action table. This value is biased by 1 (1 indicates the start of
679 // the action table), and 0 indicates that there are no actions.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000680 Asm->EmitULEB128Bytes(S.Action);
681 Asm->EOL("Action");
Bill Wendlingeb907212009-05-15 01:12:28 +0000682 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000683 } else {
684 // DWARF Exception handling
685 assert(TAI->getExceptionHandlingType() == ExceptionHandling::Dwarf);
Bill Wendlingeb907212009-05-15 01:12:28 +0000686
Bill Wendlinga583c552009-08-20 22:02:24 +0000687 // The call-site table is a list of all call sites that may throw an
688 // exception (including C++ 'throw' statements) in the procedure
689 // fragment. It immediately follows the LSDA header. Each entry indicates,
690 // for a given call, the first corresponding action record and corresponding
691 // landing pad.
692 //
693 // The table begins with the number of bytes, stored as an LEB128
694 // compressed, unsigned integer. The records immediately follow the record
695 // count. They are sorted in increasing call-site address. Each record
696 // indicates:
697 //
698 // * The position of the call-site.
699 // * The position of the landing pad.
700 // * The first action record for that call site.
701 //
702 // A missing entry in the call-site table indicates that a call is not
703 // supposed to throw. Such calls include:
704 //
705 // * Calls to destructors within cleanup code. C++ semantics forbids these
706 // calls to throw.
707 // * Calls to intrinsic routines in the standard library which are known
708 // not to throw (sin, memcpy, et al).
709 //
710 // If the runtime does not find the call-site entry for a given call, it
711 // will call `terminate()'.
712
713 // Emit the landing pad call site table.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000714 Asm->EmitInt8(dwarf::DW_EH_PE_udata4);
715 Asm->EOL("Call site format (DW_EH_PE_udata4)");
716 Asm->EmitULEB128Bytes(SizeSites);
Bill Wendlinga583c552009-08-20 22:02:24 +0000717 Asm->EOL("Call site table size");
Bill Wendlingeb907212009-05-15 01:12:28 +0000718
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000719 for (SmallVectorImpl<CallSiteEntry>::const_iterator
720 I = CallSites.begin(), E = CallSites.end(); I != E; ++I) {
721 const CallSiteEntry &S = *I;
722 const char *BeginTag;
723 unsigned BeginNumber;
Bill Wendlingeb907212009-05-15 01:12:28 +0000724
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000725 if (!S.BeginLabel) {
726 BeginTag = "eh_func_begin";
727 BeginNumber = SubprogramCount;
728 } else {
729 BeginTag = "label";
730 BeginNumber = S.BeginLabel;
731 }
Bill Wendlingeb907212009-05-15 01:12:28 +0000732
Bill Wendlinga583c552009-08-20 22:02:24 +0000733 // Offset of the call site relative to the previous call site, counted in
734 // number of 16-byte bundles. The first call site is counted relative to
735 // the start of the procedure fragment.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000736 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
Bill Wendlingeb907212009-05-15 01:12:28 +0000737 true, true);
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000738 Asm->EOL("Region start");
Bill Wendlingeb907212009-05-15 01:12:28 +0000739
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000740 if (!S.EndLabel)
741 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
742 true);
743 else
744 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
Bill Wendlingeb907212009-05-15 01:12:28 +0000745
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000746 Asm->EOL("Region length");
747
Bill Wendlinga583c552009-08-20 22:02:24 +0000748 // Offset of the landing pad, counted in 16-byte bundles relative to the
749 // @LPStart address.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000750 if (!S.PadLabel)
751 Asm->EmitInt32(0);
752 else
753 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
754 true, true);
755
756 Asm->EOL("Landing pad");
757
Bill Wendlinga583c552009-08-20 22:02:24 +0000758 // Offset of the first associated action record, relative to the start of
759 // the action table. This value is biased by 1 (1 indicates the start of
760 // the action table), and 0 indicates that there are no actions.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000761 Asm->EmitULEB128Bytes(S.Action);
762 Asm->EOL("Action");
763 }
Bill Wendlingeb907212009-05-15 01:12:28 +0000764 }
765
Bill Wendlinga583c552009-08-20 22:02:24 +0000766 // Emit the Action Table.
Bill Wendling5cff4872009-07-28 23:44:43 +0000767 for (SmallVectorImpl<ActionEntry>::const_iterator
768 I = Actions.begin(), E = Actions.end(); I != E; ++I) {
769 const ActionEntry &Action = *I;
Bill Wendlinga583c552009-08-20 22:02:24 +0000770
771 // Type Filter
772 //
773 // Used by the runtime to match the type of the thrown exception to the
774 // type of the catch clauses or the types in the exception specification.
775
Bill Wendlingeb907212009-05-15 01:12:28 +0000776 Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
777 Asm->EOL("TypeInfo index");
Bill Wendlinga583c552009-08-20 22:02:24 +0000778
779 // Action Record
780 //
781 // Self-relative signed displacement in bytes of the next action record,
782 // or 0 if there is no next action record.
783
Bill Wendlingeb907212009-05-15 01:12:28 +0000784 Asm->EmitSLEB128Bytes(Action.NextAction);
785 Asm->EOL("Next action");
786 }
787
Bill Wendlinga583c552009-08-20 22:02:24 +0000788 // Emit the Catch Clauses. The code for the catch clauses following the same
789 // try is similar to a switch statement. The catch clause action record
790 // informs the runtime about the type of a catch clause and about the
791 // associated switch value.
792 //
793 // Action Record Fields:
794 //
795 // * Filter Value
796 // Positive value, starting at 1. Index in the types table of the
797 // __typeinfo for the catch-clause type. 1 is the first word preceding
798 // TTBase, 2 is the second word, and so on. Used by the runtime to check
799 // if the thrown exception type matches the catch-clause type. Back-end
800 // generated switch statements check against this value.
801 //
802 // * Next
803 // Signed offset, in bytes from the start of this field, to the next
804 // chained action record, or zero if none.
805 //
806 // The order of the action records determined by the next field is the order
807 // of the catch clauses as they appear in the source code, and must be kept in
808 // the same order. As a result, changing the order of the catch clause would
809 // change the semantics of the program.
Bill Wendling5cff4872009-07-28 23:44:43 +0000810 for (std::vector<GlobalVariable *>::const_reverse_iterator
811 I = TypeInfos.rbegin(), E = TypeInfos.rend(); I != E; ++I) {
Bill Wendlinga583c552009-08-20 22:02:24 +0000812 const GlobalVariable *GV = *I;
Bill Wendlingeb907212009-05-15 01:12:28 +0000813 PrintRelDirective();
814
815 if (GV) {
816 std::string GLN;
817 O << Asm->getGlobalLinkName(GV, GLN);
818 } else {
819 O << "0";
820 }
821
822 Asm->EOL("TypeInfo");
823 }
824
Bill Wendlinga583c552009-08-20 22:02:24 +0000825 // Emit the Type Table.
Bill Wendling5cff4872009-07-28 23:44:43 +0000826 for (std::vector<unsigned>::const_iterator
827 I = FilterIds.begin(), E = FilterIds.end(); I < E; ++I) {
828 unsigned TypeID = *I;
Bill Wendlingeb907212009-05-15 01:12:28 +0000829 Asm->EmitULEB128Bytes(TypeID);
830 Asm->EOL("Filter TypeInfo index");
831 }
832
833 Asm->EmitAlignment(2, 0, 0, false);
834}
835
Bill Wendlingeb907212009-05-15 01:12:28 +0000836/// EndModule - Emit all exception information that should come after the
837/// content.
838void DwarfException::EndModule() {
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000839 if (TAI->getExceptionHandlingType() != ExceptionHandling::Dwarf)
840 return;
Bill Wendlingeb907212009-05-15 01:12:28 +0000841 if (TimePassesIsEnabled)
842 ExceptionTimer->startTimer();
843
844 if (shouldEmitMovesModule || shouldEmitTableModule) {
845 const std::vector<Function *> Personalities = MMI->getPersonalities();
846 for (unsigned i = 0; i < Personalities.size(); ++i)
847 EmitCommonEHFrame(Personalities[i], i);
848
849 for (std::vector<FunctionEHFrameInfo>::iterator I = EHFrames.begin(),
850 E = EHFrames.end(); I != E; ++I)
851 EmitEHFrame(*I);
852 }
853
854 if (TimePassesIsEnabled)
855 ExceptionTimer->stopTimer();
856}
857
858/// BeginFunction - Gather pre-function exception information. Assumes being
859/// emitted immediately after the function entry point.
860void DwarfException::BeginFunction(MachineFunction *MF) {
861 if (TimePassesIsEnabled)
862 ExceptionTimer->startTimer();
863
864 this->MF = MF;
865 shouldEmitTable = shouldEmitMoves = false;
866
867 if (MMI && TAI->doesSupportExceptionHandling()) {
868 // Map all labels and get rid of any dead landing pads.
869 MMI->TidyLandingPads();
870
871 // If any landing pads survive, we need an EH table.
872 if (MMI->getLandingPads().size())
873 shouldEmitTable = true;
874
875 // See if we need frame move info.
876 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
877 shouldEmitMoves = true;
878
879 if (shouldEmitMoves || shouldEmitTable)
880 // Assumes in correct section after the entry point.
881 EmitLabel("eh_func_begin", ++SubprogramCount);
882 }
883
884 shouldEmitTableModule |= shouldEmitTable;
885 shouldEmitMovesModule |= shouldEmitMoves;
886
887 if (TimePassesIsEnabled)
888 ExceptionTimer->stopTimer();
889}
890
891/// EndFunction - Gather and emit post-function exception information.
892///
893void DwarfException::EndFunction() {
894 if (TimePassesIsEnabled)
895 ExceptionTimer->startTimer();
896
897 if (shouldEmitMoves || shouldEmitTable) {
898 EmitLabel("eh_func_end", SubprogramCount);
899 EmitExceptionTable();
900
901 // Save EH frame information
Bill Wendlingeb907212009-05-15 01:12:28 +0000902 EHFrames.push_back(
Chris Lattnere2cf37b2009-07-17 20:46:40 +0000903 FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF),
Bill Wendlingeb907212009-05-15 01:12:28 +0000904 SubprogramCount,
905 MMI->getPersonalityIndex(),
906 MF->getFrameInfo()->hasCalls(),
907 !MMI->getLandingPads().empty(),
908 MMI->getFrameMoves(),
909 MF->getFunction()));
910 }
911
912 if (TimePassesIsEnabled)
913 ExceptionTimer->stopTimer();
914}