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