blob: b68f5213d1c9b53ca4d0be2c2a4bd277498ac5bc [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"
18#include "llvm/CodeGen/MachineLocation.h"
19#include "llvm/Support/Dwarf.h"
20#include "llvm/Support/Timer.h"
21#include "llvm/Support/raw_ostream.h"
22#include "llvm/Target/TargetAsmInfo.h"
23#include "llvm/Target/TargetRegisterInfo.h"
24#include "llvm/Target/TargetData.h"
25#include "llvm/Target/TargetFrameInfo.h"
26#include "llvm/Target/TargetOptions.h"
27#include "llvm/ADT/StringExtras.h"
28using namespace llvm;
29
30static TimerGroup &getDwarfTimerGroup() {
31 static TimerGroup DwarfTimerGroup("Dwarf Exception");
32 return DwarfTimerGroup;
33}
34
35void DwarfException::EmitCommonEHFrame(const Function *Personality,
36 unsigned Index) {
37 // Size and sign of stack growth.
38 int stackGrowth =
39 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
40 TargetFrameInfo::StackGrowsUp ?
41 TD->getPointerSize() : -TD->getPointerSize();
42
43 // Begin eh frame section.
44 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
45
46 if (!TAI->doesRequireNonLocalEHFrameLabel())
47 O << TAI->getEHGlobalPrefix();
48
49 O << "EH_frame" << Index << ":\n";
50 EmitLabel("section_eh_frame", Index);
51
52 // Define base labels.
53 EmitLabel("eh_frame_common", Index);
54
55 // Define the eh frame length.
56 EmitDifference("eh_frame_common_end", Index,
57 "eh_frame_common_begin", Index, true);
58 Asm->EOL("Length of Common Information Entry");
59
60 // EH frame header.
61 EmitLabel("eh_frame_common_begin", Index);
62 Asm->EmitInt32((int)0);
63 Asm->EOL("CIE Identifier Tag");
64 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
65 Asm->EOL("CIE Version");
66
67 // The personality presence indicates that language specific information will
68 // show up in the eh frame.
69 Asm->EmitString(Personality ? "zPLR" : "zR");
70 Asm->EOL("CIE Augmentation");
71
72 // Round out reader.
73 Asm->EmitULEB128Bytes(1);
74 Asm->EOL("CIE Code Alignment Factor");
75 Asm->EmitSLEB128Bytes(stackGrowth);
76 Asm->EOL("CIE Data Alignment Factor");
77 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
78 Asm->EOL("CIE Return Address Column");
79
80 // If there is a personality, we need to indicate the functions location.
81 if (Personality) {
82 Asm->EmitULEB128Bytes(7);
83 Asm->EOL("Augmentation Size");
84
85 if (TAI->getNeedsIndirectEncoding()) {
86 Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4 |
87 dwarf::DW_EH_PE_indirect);
88 Asm->EOL("Personality (pcrel sdata4 indirect)");
89 } else {
90 Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
91 Asm->EOL("Personality (pcrel sdata4)");
92 }
93
94 PrintRelDirective(true);
95 O << TAI->getPersonalityPrefix();
96 Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
97 O << TAI->getPersonalitySuffix();
98 if (strcmp(TAI->getPersonalitySuffix(), "+4@GOTPCREL"))
99 O << "-" << TAI->getPCSymbol();
100 Asm->EOL("Personality");
101
102 Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
103 Asm->EOL("LSDA Encoding (pcrel sdata4)");
104
105 Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
106 Asm->EOL("FDE Encoding (pcrel sdata4)");
107 } else {
108 Asm->EmitULEB128Bytes(1);
109 Asm->EOL("Augmentation Size");
110
111 Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
112 Asm->EOL("FDE Encoding (pcrel sdata4)");
113 }
114
115 // Indicate locations of general callee saved registers in frame.
116 std::vector<MachineMove> Moves;
117 RI->getInitialFrameState(Moves);
118 EmitFrameMoves(NULL, 0, Moves, true);
119
120 // On Darwin the linker honors the alignment of eh_frame, which means it must
121 // be 8-byte on 64-bit targets to match what gcc does. Otherwise you get
122 // holes which confuse readers of eh_frame.
123 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
124 0, 0, false);
125 EmitLabel("eh_frame_common_end", Index);
126
127 Asm->EOL();
128}
129
130/// EmitEHFrame - Emit function exception frame information.
131///
132void DwarfException::EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) {
133 assert(!EHFrameInfo.function->hasAvailableExternallyLinkage() &&
134 "Should not emit 'available externally' functions at all");
135
136 Function::LinkageTypes linkage = EHFrameInfo.function->getLinkage();
137 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
138
139 // Externally visible entry into the functions eh frame info. If the
140 // corresponding function is static, this should not be externally visible.
141 if (linkage != Function::InternalLinkage &&
142 linkage != Function::PrivateLinkage) {
143 if (const char *GlobalEHDirective = TAI->getGlobalEHDirective())
144 O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
145 }
146
147 // If corresponding function is weak definition, this should be too.
148 if ((linkage == Function::WeakAnyLinkage ||
149 linkage == Function::WeakODRLinkage ||
150 linkage == Function::LinkOnceAnyLinkage ||
151 linkage == Function::LinkOnceODRLinkage) &&
152 TAI->getWeakDefDirective())
153 O << TAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
154
155 // If there are no calls then you can't unwind. This may mean we can omit the
156 // EH Frame, but some environments do not handle weak absolute symbols. If
157 // UnwindTablesMandatory is set we cannot do this optimization; the unwind
158 // info is to be available for non-EH uses.
159 if (!EHFrameInfo.hasCalls &&
160 !UnwindTablesMandatory &&
161 ((linkage != Function::WeakAnyLinkage &&
162 linkage != Function::WeakODRLinkage &&
163 linkage != Function::LinkOnceAnyLinkage &&
164 linkage != Function::LinkOnceODRLinkage) ||
165 !TAI->getWeakDefDirective() ||
166 TAI->getSupportsWeakOmittedEHFrame())) {
167 O << EHFrameInfo.FnName << " = 0\n";
168 // This name has no connection to the function, so it might get
169 // dead-stripped when the function is not, erroneously. Prohibit
170 // dead-stripping unconditionally.
171 if (const char *UsedDirective = TAI->getUsedDirective())
172 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
173 } else {
174 O << EHFrameInfo.FnName << ":\n";
175
176 // EH frame header.
177 EmitDifference("eh_frame_end", EHFrameInfo.Number,
178 "eh_frame_begin", EHFrameInfo.Number, true);
179 Asm->EOL("Length of Frame Information Entry");
180
181 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
182
183 if (TAI->doesRequireNonLocalEHFrameLabel()) {
184 PrintRelDirective(true, true);
185 PrintLabelName("eh_frame_begin", EHFrameInfo.Number);
186
187 if (!TAI->isAbsoluteEHSectionOffsets())
188 O << "-EH_frame" << EHFrameInfo.PersonalityIndex;
189 } else {
190 EmitSectionOffset("eh_frame_begin", "eh_frame_common",
191 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
192 true, true, false);
193 }
194
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.
235 if (MMI->getUsedFunctions().count(EHFrameInfo.function))
236 if (const char *UsedDirective = TAI->getUsedDirective())
237 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
238 }
239}
240
241/// EmitExceptionTable - Emit landing pads and actions.
242///
243/// The general organization of the table is complex, but the basic concepts are
244/// easy. First there is a header which describes the location and organization
245/// of the three components that follow.
246///
247/// 1. The landing pad site information describes the range of code covered by
248/// the try. In our case it's an accumulation of the ranges covered by the
249/// invokes in the try. There is also a reference to the landing pad that
250/// handles the exception once processed. Finally an index into the actions
251/// table.
252/// 2. The action table, in our case, is composed of pairs of type ids and next
253/// action offset. Starting with the action index from the landing pad
254/// site, each type Id is checked for a match to the current exception. If
255/// it matches then the exception and type id are passed on to the landing
256/// pad. Otherwise the next action is looked up. This chain is terminated
257/// with a next action of zero. If no type id is found the the frame is
258/// unwound and handling continues.
259/// 3. Type id table contains references to all the C++ typeinfo for all
260/// catches in the function. This tables is reversed indexed base 1.
261
262/// SharedTypeIds - How many leading type ids two landing pads have in common.
263unsigned DwarfException::SharedTypeIds(const LandingPadInfo *L,
264 const LandingPadInfo *R) {
265 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
266 unsigned LSize = LIds.size(), RSize = RIds.size();
267 unsigned MinSize = LSize < RSize ? LSize : RSize;
268 unsigned Count = 0;
269
270 for (; Count != MinSize; ++Count)
271 if (LIds[Count] != RIds[Count])
272 return Count;
273
274 return Count;
275}
276
277/// PadLT - Order landing pads lexicographically by type id.
278bool DwarfException::PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
279 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
280 unsigned LSize = LIds.size(), RSize = RIds.size();
281 unsigned MinSize = LSize < RSize ? LSize : RSize;
282
283 for (unsigned i = 0; i != MinSize; ++i)
284 if (LIds[i] != RIds[i])
285 return LIds[i] < RIds[i];
286
287 return LSize < RSize;
288}
289
290void DwarfException::EmitExceptionTable() {
291 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
292 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
293 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
294 if (PadInfos.empty()) return;
295
296 // Sort the landing pads in order of their type ids. This is used to fold
297 // duplicate actions.
298 SmallVector<const LandingPadInfo *, 64> LandingPads;
299 LandingPads.reserve(PadInfos.size());
300 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
301 LandingPads.push_back(&PadInfos[i]);
302 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
303
304 // Negative type ids index into FilterIds, positive type ids index into
305 // TypeInfos. The value written for a positive type id is just the type id
306 // itself. For a negative type id, however, the value written is the
307 // (negative) byte offset of the corresponding FilterIds entry. The byte
308 // offset is usually equal to the type id, because the FilterIds entries are
309 // written using a variable width encoding which outputs one byte per entry as
310 // long as the value written is not too large, but can differ. This kind of
311 // complication does not occur for positive type ids because type infos are
312 // output using a fixed width encoding. FilterOffsets[i] holds the byte
313 // offset corresponding to FilterIds[i].
314 SmallVector<int, 16> FilterOffsets;
315 FilterOffsets.reserve(FilterIds.size());
316 int Offset = -1;
317 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
318 E = FilterIds.end(); I != E; ++I) {
319 FilterOffsets.push_back(Offset);
320 Offset -= TargetAsmInfo::getULEB128Size(*I);
321 }
322
323 // Compute the actions table and gather the first action index for each
324 // landing pad site.
325 SmallVector<ActionEntry, 32> Actions;
326 SmallVector<unsigned, 64> FirstActions;
327 FirstActions.reserve(LandingPads.size());
328
329 int FirstAction = 0;
330 unsigned SizeActions = 0;
331 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
332 const LandingPadInfo *LP = LandingPads[i];
333 const std::vector<int> &TypeIds = LP->TypeIds;
334 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
335 unsigned SizeSiteActions = 0;
336
337 if (NumShared < TypeIds.size()) {
338 unsigned SizeAction = 0;
339 ActionEntry *PrevAction = 0;
340
341 if (NumShared) {
342 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
343 assert(Actions.size());
344 PrevAction = &Actions.back();
345 SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) +
346 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
347
348 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
349 SizeAction -=
350 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
351 SizeAction += -PrevAction->NextAction;
352 PrevAction = PrevAction->Previous;
353 }
354 }
355
356 // Compute the actions.
357 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
358 int TypeID = TypeIds[I];
359 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
360 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
361 unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID);
362
363 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
364 SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction);
365 SizeSiteActions += SizeAction;
366
367 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
368 Actions.push_back(Action);
369
370 PrevAction = &Actions.back();
371 }
372
373 // Record the first action of the landing pad site.
374 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
375 } // else identical - re-use previous FirstAction
376
377 FirstActions.push_back(FirstAction);
378
379 // Compute this sites contribution to size.
380 SizeActions += SizeSiteActions;
381 }
382
383 // Compute the call-site table. The entry for an invoke has a try-range
384 // containing the call, a non-zero landing pad and an appropriate action. The
385 // entry for an ordinary call has a try-range containing the call and zero for
386 // the landing pad and the action. Calls marked 'nounwind' have no entry and
387 // must not be contained in the try-range of any entry - they form gaps in the
388 // table. Entries must be ordered by try-range address.
389 SmallVector<CallSiteEntry, 64> CallSites;
390
391 RangeMapType PadMap;
392
393 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
394 // by try-range labels when lowered). Ordinary calls do not, so appropriate
395 // try-ranges for them need be deduced.
396 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
397 const LandingPadInfo *LandingPad = LandingPads[i];
398 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
399 unsigned BeginLabel = LandingPad->BeginLabels[j];
400 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
401 PadRange P = { i, j };
402 PadMap[BeginLabel] = P;
403 }
404 }
405
406 // The end label of the previous invoke or nounwind try-range.
407 unsigned LastLabel = 0;
408
409 // Whether there is a potentially throwing instruction (currently this means
410 // an ordinary call) between the end of the previous try-range and now.
411 bool SawPotentiallyThrowing = false;
412
413 // Whether the last callsite entry was for an invoke.
414 bool PreviousIsInvoke = false;
415
416 // Visit all instructions in order of address.
417 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
418 I != E; ++I) {
419 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
420 MI != E; ++MI) {
421 if (!MI->isLabel()) {
422 SawPotentiallyThrowing |= MI->getDesc().isCall();
423 continue;
424 }
425
426 unsigned BeginLabel = MI->getOperand(0).getImm();
427 assert(BeginLabel && "Invalid label!");
428
429 // End of the previous try-range?
430 if (BeginLabel == LastLabel)
431 SawPotentiallyThrowing = false;
432
433 // Beginning of a new try-range?
434 RangeMapType::iterator L = PadMap.find(BeginLabel);
435 if (L == PadMap.end())
436 // Nope, it was just some random label.
437 continue;
438
439 PadRange P = L->second;
440 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
441
442 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
443 "Inconsistent landing pad map!");
444
445 // If some instruction between the previous try-range and this one may
446 // throw, create a call-site entry with no landing pad for the region
447 // between the try-ranges.
448 if (SawPotentiallyThrowing) {
449 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
450 CallSites.push_back(Site);
451 PreviousIsInvoke = false;
452 }
453
454 LastLabel = LandingPad->EndLabels[P.RangeIndex];
455 assert(BeginLabel && LastLabel && "Invalid landing pad!");
456
457 if (LandingPad->LandingPadLabel) {
458 // This try-range is for an invoke.
459 CallSiteEntry Site = {BeginLabel, LastLabel,
460 LandingPad->LandingPadLabel,
461 FirstActions[P.PadIndex]};
462
463 // Try to merge with the previous call-site.
464 if (PreviousIsInvoke) {
465 CallSiteEntry &Prev = CallSites.back();
466 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
467 // Extend the range of the previous entry.
468 Prev.EndLabel = Site.EndLabel;
469 continue;
470 }
471 }
472
473 // Otherwise, create a new call-site.
474 CallSites.push_back(Site);
475 PreviousIsInvoke = true;
476 } else {
477 // Create a gap.
478 PreviousIsInvoke = false;
479 }
480 }
481 }
482
483 // If some instruction between the previous try-range and the end of the
484 // function may throw, create a call-site entry with no landing pad for the
485 // region following the try-range.
486 if (SawPotentiallyThrowing) {
487 CallSiteEntry Site = {LastLabel, 0, 0, 0};
488 CallSites.push_back(Site);
489 }
490
491 // Final tallies.
492
493 // Call sites.
494 const unsigned SiteStartSize = sizeof(int32_t); // DW_EH_PE_udata4
495 const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4
496 const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4
497 unsigned SizeSites = CallSites.size() * (SiteStartSize +
498 SiteLengthSize +
499 LandingPadSize);
500 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
501 SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action);
502
503 // Type infos.
504 const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
505 unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
506
507 unsigned TypeOffset = sizeof(int8_t) + // Call site format
508 TargetAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
509 SizeSites + SizeActions + SizeTypes;
510
511 unsigned TotalSize = sizeof(int8_t) + // LPStart format
512 sizeof(int8_t) + // TType format
513 TargetAsmInfo::getULEB128Size(TypeOffset) + // TType base offset
514 TypeOffset;
515
516 unsigned SizeAlign = (4 - TotalSize) & 3;
517
518 // Begin the exception table.
519 Asm->SwitchToDataSection(TAI->getDwarfExceptionSection());
520 Asm->EmitAlignment(2, 0, 0, false);
521 O << "GCC_except_table" << SubprogramCount << ":\n";
522
523 for (unsigned i = 0; i != SizeAlign; ++i) {
524 Asm->EmitInt8(0);
525 Asm->EOL("Padding");
526 }
527
528 EmitLabel("exception", SubprogramCount);
529
530 // Emit the header.
531 Asm->EmitInt8(dwarf::DW_EH_PE_omit);
532 Asm->EOL("LPStart format (DW_EH_PE_omit)");
533 Asm->EmitInt8(dwarf::DW_EH_PE_absptr);
534 Asm->EOL("TType format (DW_EH_PE_absptr)");
535 Asm->EmitULEB128Bytes(TypeOffset);
536 Asm->EOL("TType base offset");
537 Asm->EmitInt8(dwarf::DW_EH_PE_udata4);
538 Asm->EOL("Call site format (DW_EH_PE_udata4)");
539 Asm->EmitULEB128Bytes(SizeSites);
540 Asm->EOL("Call-site table length");
541
542 // Emit the landing pad site information.
543 for (unsigned i = 0; i < CallSites.size(); ++i) {
544 CallSiteEntry &S = CallSites[i];
545 const char *BeginTag;
546 unsigned BeginNumber;
547
548 if (!S.BeginLabel) {
549 BeginTag = "eh_func_begin";
550 BeginNumber = SubprogramCount;
551 } else {
552 BeginTag = "label";
553 BeginNumber = S.BeginLabel;
554 }
555
556 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
557 true, true);
558 Asm->EOL("Region start");
559
560 if (!S.EndLabel)
561 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
562 true);
563 else
564 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
565
566 Asm->EOL("Region length");
567
568 if (!S.PadLabel)
569 Asm->EmitInt32(0);
570 else
571 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
572 true, true);
573
574 Asm->EOL("Landing pad");
575
576 Asm->EmitULEB128Bytes(S.Action);
577 Asm->EOL("Action");
578 }
579
580 // Emit the actions.
581 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
582 ActionEntry &Action = Actions[I];
583
584 Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
585 Asm->EOL("TypeInfo index");
586 Asm->EmitSLEB128Bytes(Action.NextAction);
587 Asm->EOL("Next action");
588 }
589
590 // Emit the type ids.
591 for (unsigned M = TypeInfos.size(); M; --M) {
592 GlobalVariable *GV = TypeInfos[M - 1];
593 PrintRelDirective();
594
595 if (GV) {
596 std::string GLN;
597 O << Asm->getGlobalLinkName(GV, GLN);
598 } else {
599 O << "0";
600 }
601
602 Asm->EOL("TypeInfo");
603 }
604
605 // Emit the filter typeids.
606 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
607 unsigned TypeID = FilterIds[j];
608 Asm->EmitULEB128Bytes(TypeID);
609 Asm->EOL("Filter TypeInfo index");
610 }
611
612 Asm->EmitAlignment(2, 0, 0, false);
613}
614
615 //===--------------------------------------------------------------------===//
616 // Main entry points.
617 //
618DwarfException::DwarfException(raw_ostream &OS, AsmPrinter *A,
619 const TargetAsmInfo *T)
620 : Dwarf(OS, A, T, "eh"), shouldEmitTable(false), shouldEmitMoves(false),
621 shouldEmitTableModule(false), shouldEmitMovesModule(false),
622 ExceptionTimer(0) {
623 if (TimePassesIsEnabled)
624 ExceptionTimer = new Timer("Dwarf Exception Writer",
625 getDwarfTimerGroup());
626}
627
628DwarfException::~DwarfException() {
629 delete ExceptionTimer;
630}
631
632/// EndModule - Emit all exception information that should come after the
633/// content.
634void DwarfException::EndModule() {
635 if (TimePassesIsEnabled)
636 ExceptionTimer->startTimer();
637
638 if (shouldEmitMovesModule || shouldEmitTableModule) {
639 const std::vector<Function *> Personalities = MMI->getPersonalities();
640 for (unsigned i = 0; i < Personalities.size(); ++i)
641 EmitCommonEHFrame(Personalities[i], i);
642
643 for (std::vector<FunctionEHFrameInfo>::iterator I = EHFrames.begin(),
644 E = EHFrames.end(); I != E; ++I)
645 EmitEHFrame(*I);
646 }
647
648 if (TimePassesIsEnabled)
649 ExceptionTimer->stopTimer();
650}
651
652/// BeginFunction - Gather pre-function exception information. Assumes being
653/// emitted immediately after the function entry point.
654void DwarfException::BeginFunction(MachineFunction *MF) {
655 if (TimePassesIsEnabled)
656 ExceptionTimer->startTimer();
657
658 this->MF = MF;
659 shouldEmitTable = shouldEmitMoves = false;
660
661 if (MMI && TAI->doesSupportExceptionHandling()) {
662 // Map all labels and get rid of any dead landing pads.
663 MMI->TidyLandingPads();
664
665 // If any landing pads survive, we need an EH table.
666 if (MMI->getLandingPads().size())
667 shouldEmitTable = true;
668
669 // See if we need frame move info.
670 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
671 shouldEmitMoves = true;
672
673 if (shouldEmitMoves || shouldEmitTable)
674 // Assumes in correct section after the entry point.
675 EmitLabel("eh_func_begin", ++SubprogramCount);
676 }
677
678 shouldEmitTableModule |= shouldEmitTable;
679 shouldEmitMovesModule |= shouldEmitMoves;
680
681 if (TimePassesIsEnabled)
682 ExceptionTimer->stopTimer();
683}
684
685/// EndFunction - Gather and emit post-function exception information.
686///
687void DwarfException::EndFunction() {
688 if (TimePassesIsEnabled)
689 ExceptionTimer->startTimer();
690
691 if (shouldEmitMoves || shouldEmitTable) {
692 EmitLabel("eh_func_end", SubprogramCount);
693 EmitExceptionTable();
694
695 // Save EH frame information
696 std::string Name;
697 EHFrames.push_back(
698 FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF, Name),
699 SubprogramCount,
700 MMI->getPersonalityIndex(),
701 MF->getFrameInfo()->hasCalls(),
702 !MMI->getLandingPads().empty(),
703 MMI->getFrameMoves(),
704 MF->getFunction()));
705 }
706
707 if (TimePassesIsEnabled)
708 ExceptionTimer->stopTimer();
709}