blob: 515c0a1e7c8258634555accc61ae51f02285f5d8 [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//
Bill Wendling639217c2009-08-27 03:32:50 +000010// This file contains support for writing dwarf exception info into asm files.
Bill Wendlingeb907212009-05-15 01:12:28 +000011//
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"
Chris Lattneraf76e592009-08-22 20:48:53 +000021#include "llvm/MC/MCAsmInfo.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"
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +000028#include "llvm/Support/Mangler.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000029#include "llvm/Support/Timer.h"
30#include "llvm/Support/raw_ostream.h"
Jim Grosbachc40d9f92009-09-01 18:49:12 +000031#include "llvm/ADT/SmallString.h"
Bill Wendlingeb907212009-05-15 01:12:28 +000032#include "llvm/ADT/StringExtras.h"
33using namespace llvm;
34
35static TimerGroup &getDwarfTimerGroup() {
36 static TimerGroup DwarfTimerGroup("Dwarf Exception");
37 return DwarfTimerGroup;
38}
39
Bill Wendlingbc0d23a2009-05-15 01:18:50 +000040DwarfException::DwarfException(raw_ostream &OS, AsmPrinter *A,
Chris Lattneraf76e592009-08-22 20:48:53 +000041 const MCAsmInfo *T)
Bill Wendlingbc0d23a2009-05-15 01:18:50 +000042 : Dwarf(OS, A, T, "eh"), shouldEmitTable(false), shouldEmitMoves(false),
43 shouldEmitTableModule(false), shouldEmitMovesModule(false),
44 ExceptionTimer(0) {
Eric Christopherdbfcdb92009-08-28 22:33:43 +000045 if (TimePassesIsEnabled)
Bill Wendlingbc0d23a2009-05-15 01:18:50 +000046 ExceptionTimer = new Timer("Dwarf Exception Writer",
47 getDwarfTimerGroup());
48}
49
50DwarfException::~DwarfException() {
51 delete ExceptionTimer;
52}
53
Bill Wendling52783c62009-09-09 23:56:55 +000054unsigned DwarfException::SizeOfEncodedValue(unsigned Encoding) {
55 if (Encoding == dwarf::DW_EH_PE_omit)
56 return 0;
57
58 switch (Encoding & 0x07) {
59 case dwarf::DW_EH_PE_absptr:
60 return TD->getPointerSize();
61 case dwarf::DW_EH_PE_udata2:
62 return 2;
63 case dwarf::DW_EH_PE_udata4:
64 return 4;
65 case dwarf::DW_EH_PE_udata8:
66 return 8;
67 }
68
69 llvm_unreachable("Invalid encoded value.");
70 return 0;
71}
72
Bill Wendling7ccda0f2009-08-25 08:08:33 +000073/// EmitCIE - Emit a Common Information Entry (CIE). This holds information that
74/// is shared among many Frame Description Entries. There is at least one CIE
75/// in every non-empty .debug_frame section.
76void DwarfException::EmitCIE(const Function *Personality, unsigned Index) {
Bill Wendlingeb907212009-05-15 01:12:28 +000077 // Size and sign of stack growth.
78 int stackGrowth =
79 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
80 TargetFrameInfo::StackGrowsUp ?
81 TD->getPointerSize() : -TD->getPointerSize();
82
83 // Begin eh frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +000084 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getEHFrameSection());
Bill Wendlingeb907212009-05-15 01:12:28 +000085
Chris Lattner33adcfb2009-08-22 21:43:10 +000086 if (MAI->is_EHSymbolPrivate())
87 O << MAI->getPrivateGlobalPrefix();
Bill Wendlingeb907212009-05-15 01:12:28 +000088
89 O << "EH_frame" << Index << ":\n";
90 EmitLabel("section_eh_frame", Index);
91
92 // Define base labels.
93 EmitLabel("eh_frame_common", Index);
94
95 // Define the eh frame length.
96 EmitDifference("eh_frame_common_end", Index,
97 "eh_frame_common_begin", Index, true);
98 Asm->EOL("Length of Common Information Entry");
99
100 // EH frame header.
101 EmitLabel("eh_frame_common_begin", Index);
102 Asm->EmitInt32((int)0);
103 Asm->EOL("CIE Identifier Tag");
104 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
105 Asm->EOL("CIE Version");
106
107 // The personality presence indicates that language specific information will
108 // show up in the eh frame.
Bill Wendling52783c62009-09-09 23:56:55 +0000109
110 // FIXME: Don't hardcode these encodings.
111 unsigned PerEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
112 if (Personality && MAI->getNeedsIndirectEncoding())
113 PerEncoding |= dwarf::DW_EH_PE_indirect;
114 unsigned LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
115 unsigned FDEEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
116
117 char Augmentation[5] = { 0 };
118 unsigned AugmentationSize = 0;
119 char *APtr = Augmentation + 1;
120
121 if (Personality) {
122 // There is a personality function.
123 *APtr++ = 'P';
124 AugmentationSize += 1 + SizeOfEncodedValue(PerEncoding);
125 }
126
127 if (UsesLSDA[Index]) {
128 // An LSDA pointer is in the FDE augmentation.
129 *APtr++ = 'L';
130 ++AugmentationSize;
131 }
132
133 if (FDEEncoding != dwarf::DW_EH_PE_absptr) {
134 // A non-default pointer encoding for the FDE.
135 *APtr++ = 'R';
136 ++AugmentationSize;
137 }
138
139 if (APtr != Augmentation + 1)
140 Augmentation[0] = 'z';
141
142 Asm->EmitString(Augmentation);
Bill Wendlingeb907212009-05-15 01:12:28 +0000143 Asm->EOL("CIE Augmentation");
144
145 // Round out reader.
146 Asm->EmitULEB128Bytes(1);
147 Asm->EOL("CIE Code Alignment Factor");
148 Asm->EmitSLEB128Bytes(stackGrowth);
149 Asm->EOL("CIE Data Alignment Factor");
150 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
151 Asm->EOL("CIE Return Address Column");
152
Bill Wendling52783c62009-09-09 23:56:55 +0000153 Asm->EmitULEB128Bytes(AugmentationSize);
154 Asm->EOL("Augmentation Size");
155
156 Asm->EmitInt8(PerEncoding);
157 Asm->EOL("Personality", PerEncoding);
158
Bill Wendling4bda11f2009-08-25 02:32:05 +0000159 // If there is a personality, we need to indicate the function's location.
Bill Wendlingeb907212009-05-15 01:12:28 +0000160 if (Personality) {
Bill Wendlingeb907212009-05-15 01:12:28 +0000161 PrintRelDirective(true);
Chris Lattner33adcfb2009-08-22 21:43:10 +0000162 O << MAI->getPersonalityPrefix();
Bill Wendlingeb907212009-05-15 01:12:28 +0000163 Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
Chris Lattner33adcfb2009-08-22 21:43:10 +0000164 O << MAI->getPersonalitySuffix();
165 if (strcmp(MAI->getPersonalitySuffix(), "+4@GOTPCREL"))
166 O << "-" << MAI->getPCSymbol();
Bill Wendlingeb907212009-05-15 01:12:28 +0000167 Asm->EOL("Personality");
168
Bill Wendling52783c62009-09-09 23:56:55 +0000169 Asm->EmitInt8(LSDAEncoding);
170 Asm->EOL("LSDA Encoding", LSDAEncoding);
Bill Wendlingeb907212009-05-15 01:12:28 +0000171
Bill Wendling52783c62009-09-09 23:56:55 +0000172 Asm->EmitInt8(FDEEncoding);
173 Asm->EOL("FDE Encoding", FDEEncoding);
Bill Wendlingeb907212009-05-15 01:12:28 +0000174 }
175
176 // Indicate locations of general callee saved registers in frame.
177 std::vector<MachineMove> Moves;
178 RI->getInitialFrameState(Moves);
179 EmitFrameMoves(NULL, 0, Moves, true);
180
181 // On Darwin the linker honors the alignment of eh_frame, which means it must
182 // be 8-byte on 64-bit targets to match what gcc does. Otherwise you get
183 // holes which confuse readers of eh_frame.
184 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
185 0, 0, false);
186 EmitLabel("eh_frame_common_end", Index);
187
188 Asm->EOL();
189}
190
Bill Wendling7ccda0f2009-08-25 08:08:33 +0000191/// EmitFDE - Emit the Frame Description Entry (FDE) for the function.
192void DwarfException::EmitFDE(const FunctionEHFrameInfo &EHFrameInfo) {
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000193 assert(!EHFrameInfo.function->hasAvailableExternallyLinkage() &&
Bill Wendlingeb907212009-05-15 01:12:28 +0000194 "Should not emit 'available externally' functions at all");
195
Chris Lattner3e0f60b2009-07-17 21:00:50 +0000196 const Function *TheFunc = EHFrameInfo.function;
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000197
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000198 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getEHFrameSection());
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000199
Bill Wendlingeb907212009-05-15 01:12:28 +0000200 // Externally visible entry into the functions eh frame info. If the
201 // corresponding function is static, this should not be externally visible.
Chris Lattner3e0f60b2009-07-17 21:00:50 +0000202 if (!TheFunc->hasLocalLinkage())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000203 if (const char *GlobalEHDirective = MAI->getGlobalEHDirective())
Bill Wendlingeb907212009-05-15 01:12:28 +0000204 O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
Bill Wendlingeb907212009-05-15 01:12:28 +0000205
206 // If corresponding function is weak definition, this should be too.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000207 if (TheFunc->isWeakForLinker() && MAI->getWeakDefDirective())
208 O << MAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
Bill Wendlingeb907212009-05-15 01:12:28 +0000209
210 // If there are no calls then you can't unwind. This may mean we can omit the
211 // EH Frame, but some environments do not handle weak absolute symbols. If
212 // UnwindTablesMandatory is set we cannot do this optimization; the unwind
213 // info is to be available for non-EH uses.
Chris Lattner3e0f60b2009-07-17 21:00:50 +0000214 if (!EHFrameInfo.hasCalls && !UnwindTablesMandatory &&
215 (!TheFunc->isWeakForLinker() ||
Chris Lattner33adcfb2009-08-22 21:43:10 +0000216 !MAI->getWeakDefDirective() ||
217 MAI->getSupportsWeakOmittedEHFrame())) {
Bill Wendlingeb907212009-05-15 01:12:28 +0000218 O << EHFrameInfo.FnName << " = 0\n";
219 // This name has no connection to the function, so it might get
220 // dead-stripped when the function is not, erroneously. Prohibit
221 // dead-stripping unconditionally.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000222 if (const char *UsedDirective = MAI->getUsedDirective())
Bill Wendlingeb907212009-05-15 01:12:28 +0000223 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
224 } else {
225 O << EHFrameInfo.FnName << ":\n";
226
227 // EH frame header.
228 EmitDifference("eh_frame_end", EHFrameInfo.Number,
229 "eh_frame_begin", EHFrameInfo.Number, true);
230 Asm->EOL("Length of Frame Information Entry");
231
232 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
233
Chris Lattnera4ff5e42009-07-17 20:53:51 +0000234 EmitSectionOffset("eh_frame_begin", "eh_frame_common",
235 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
236 true, true, false);
Bill Wendlingeb907212009-05-15 01:12:28 +0000237
238 Asm->EOL("FDE CIE offset");
239
Duncan Sandsc69d74a2009-08-31 16:45:16 +0000240 EmitReference("eh_func_begin", EHFrameInfo.Number, true, true);
Bill Wendlingeb907212009-05-15 01:12:28 +0000241 Asm->EOL("FDE initial location");
242 EmitDifference("eh_func_end", EHFrameInfo.Number,
Duncan Sandsc69d74a2009-08-31 16:45:16 +0000243 "eh_func_begin", EHFrameInfo.Number, true);
Bill Wendlingeb907212009-05-15 01:12:28 +0000244 Asm->EOL("FDE address range");
245
246 // If there is a personality and landing pads then point to the language
247 // specific data area in the exception table.
Eric Christopherd44fff72009-08-26 21:30:49 +0000248 if (MMI->getPersonalities()[0] != NULL) {
Duncan Sandsc69d74a2009-08-31 16:45:16 +0000249 bool is4Byte = TD->getPointerSize() == sizeof(int32_t);
250
Eric Christopher6fefceb2009-08-29 01:12:46 +0000251 Asm->EmitULEB128Bytes(is4Byte ? 4 : 8);
Bill Wendlingeb907212009-05-15 01:12:28 +0000252 Asm->EOL("Augmentation size");
253
Duncan Sandsc69d74a2009-08-31 16:45:16 +0000254 if (EHFrameInfo.hasLandingPads)
Eric Christopher6fefceb2009-08-29 01:12:46 +0000255 EmitReference("exception", EHFrameInfo.Number, true, false);
Duncan Sandsc69d74a2009-08-31 16:45:16 +0000256 else {
Bill Wendling52783c62009-09-09 23:56:55 +0000257 if (is4Byte)
258 Asm->EmitInt32((int)0);
259 else
260 Asm->EmitInt64((int)0);
Eric Christopher6fefceb2009-08-29 01:12:46 +0000261 }
Bill Wendlingeb907212009-05-15 01:12:28 +0000262 Asm->EOL("Language Specific Data Area");
263 } else {
264 Asm->EmitULEB128Bytes(0);
265 Asm->EOL("Augmentation size");
266 }
267
268 // Indicate locations of function specific callee saved registers in frame.
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000269 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves,
Bill Wendlingeb907212009-05-15 01:12:28 +0000270 true);
271
272 // On Darwin the linker honors the alignment of eh_frame, which means it
273 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise you
274 // get holes which confuse readers of eh_frame.
275 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
276 0, 0, false);
277 EmitLabel("eh_frame_end", EHFrameInfo.Number);
278
279 // If the function is marked used, this table should be also. We cannot
280 // make the mark unconditional in this case, since retaining the table also
281 // retains the function in this case, and there is code around that depends
282 // on unused functions (calling undefined externals) being dead-stripped to
283 // link correctly. Yes, there really is.
Chris Lattner401e10c2009-07-20 06:14:25 +0000284 if (MMI->isUsedFunction(EHFrameInfo.function))
Chris Lattner33adcfb2009-08-22 21:43:10 +0000285 if (const char *UsedDirective = MAI->getUsedDirective())
Bill Wendlingeb907212009-05-15 01:12:28 +0000286 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
287 }
Bill Wendling4bda11f2009-08-25 02:32:05 +0000288
289 Asm->EOL();
Bill Wendlingeb907212009-05-15 01:12:28 +0000290}
291
Bill Wendlingeb907212009-05-15 01:12:28 +0000292/// SharedTypeIds - How many leading type ids two landing pads have in common.
293unsigned DwarfException::SharedTypeIds(const LandingPadInfo *L,
294 const LandingPadInfo *R) {
295 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
296 unsigned LSize = LIds.size(), RSize = RIds.size();
297 unsigned MinSize = LSize < RSize ? LSize : RSize;
298 unsigned Count = 0;
299
300 for (; Count != MinSize; ++Count)
301 if (LIds[Count] != RIds[Count])
302 return Count;
303
304 return Count;
305}
306
307/// PadLT - Order landing pads lexicographically by type id.
308bool DwarfException::PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
309 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
310 unsigned LSize = LIds.size(), RSize = RIds.size();
311 unsigned MinSize = LSize < RSize ? LSize : RSize;
312
313 for (unsigned i = 0; i != MinSize; ++i)
314 if (LIds[i] != RIds[i])
315 return LIds[i] < RIds[i];
316
317 return LSize < RSize;
318}
319
Bill Wendlingd4609622009-07-28 23:23:00 +0000320/// ComputeActionsTable - Compute the actions table and gather the first action
321/// index for each landing pad site.
Bill Wendlingade025c2009-07-29 00:31:35 +0000322unsigned DwarfException::
323ComputeActionsTable(const SmallVectorImpl<const LandingPadInfo*> &LandingPads,
324 SmallVectorImpl<ActionEntry> &Actions,
325 SmallVectorImpl<unsigned> &FirstActions) {
Bill Wendlinga583c552009-08-20 22:02:24 +0000326
327 // The action table follows the call-site table in the LSDA. The individual
328 // records are of two types:
329 //
330 // * Catch clause
331 // * Exception specification
332 //
333 // The two record kinds have the same format, with only small differences.
334 // They are distinguished by the "switch value" field: Catch clauses
335 // (TypeInfos) have strictly positive switch values, and exception
336 // specifications (FilterIds) have strictly negative switch values. Value 0
337 // indicates a catch-all clause.
338 //
Bill Wendling5e953dd2009-07-28 23:22:13 +0000339 // Negative type IDs index into FilterIds. Positive type IDs index into
340 // TypeInfos. The value written for a positive type ID is just the type ID
341 // itself. For a negative type ID, however, the value written is the
Bill Wendlingeb907212009-05-15 01:12:28 +0000342 // (negative) byte offset of the corresponding FilterIds entry. The byte
Bill Wendling5e953dd2009-07-28 23:22:13 +0000343 // offset is usually equal to the type ID (because the FilterIds entries are
344 // written using a variable width encoding, which outputs one byte per entry
345 // as long as the value written is not too large) but can differ. This kind
346 // of complication does not occur for positive type IDs because type infos are
Bill Wendlingeb907212009-05-15 01:12:28 +0000347 // output using a fixed width encoding. FilterOffsets[i] holds the byte
348 // offset corresponding to FilterIds[i].
Bill Wendling409914b2009-07-29 21:19:44 +0000349
350 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
Bill Wendlingeb907212009-05-15 01:12:28 +0000351 SmallVector<int, 16> FilterOffsets;
352 FilterOffsets.reserve(FilterIds.size());
353 int Offset = -1;
Bill Wendling409914b2009-07-29 21:19:44 +0000354
355 for (std::vector<unsigned>::const_iterator
356 I = FilterIds.begin(), E = FilterIds.end(); I != E; ++I) {
Bill Wendlingeb907212009-05-15 01:12:28 +0000357 FilterOffsets.push_back(Offset);
Chris Lattneraf76e592009-08-22 20:48:53 +0000358 Offset -= MCAsmInfo::getULEB128Size(*I);
Bill Wendlingeb907212009-05-15 01:12:28 +0000359 }
360
Bill Wendlingeb907212009-05-15 01:12:28 +0000361 FirstActions.reserve(LandingPads.size());
362
363 int FirstAction = 0;
364 unsigned SizeActions = 0;
Bill Wendling5e953dd2009-07-28 23:22:13 +0000365 const LandingPadInfo *PrevLPI = 0;
Bill Wendling409914b2009-07-29 21:19:44 +0000366
Bill Wendling5cff4872009-07-28 23:44:43 +0000367 for (SmallVectorImpl<const LandingPadInfo *>::const_iterator
Bill Wendling5e953dd2009-07-28 23:22:13 +0000368 I = LandingPads.begin(), E = LandingPads.end(); I != E; ++I) {
369 const LandingPadInfo *LPI = *I;
370 const std::vector<int> &TypeIds = LPI->TypeIds;
371 const unsigned NumShared = PrevLPI ? SharedTypeIds(LPI, PrevLPI) : 0;
Bill Wendlingeb907212009-05-15 01:12:28 +0000372 unsigned SizeSiteActions = 0;
373
374 if (NumShared < TypeIds.size()) {
375 unsigned SizeAction = 0;
376 ActionEntry *PrevAction = 0;
377
378 if (NumShared) {
Bill Wendling5e953dd2009-07-28 23:22:13 +0000379 const unsigned SizePrevIds = PrevLPI->TypeIds.size();
Bill Wendlingeb907212009-05-15 01:12:28 +0000380 assert(Actions.size());
381 PrevAction = &Actions.back();
Chris Lattneraf76e592009-08-22 20:48:53 +0000382 SizeAction = MCAsmInfo::getSLEB128Size(PrevAction->NextAction) +
383 MCAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Bill Wendlingeb907212009-05-15 01:12:28 +0000384
385 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
386 SizeAction -=
Chris Lattneraf76e592009-08-22 20:48:53 +0000387 MCAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Bill Wendlingeb907212009-05-15 01:12:28 +0000388 SizeAction += -PrevAction->NextAction;
389 PrevAction = PrevAction->Previous;
390 }
391 }
392
393 // Compute the actions.
Bill Wendling5e953dd2009-07-28 23:22:13 +0000394 for (unsigned J = NumShared, M = TypeIds.size(); J != M; ++J) {
395 int TypeID = TypeIds[J];
396 assert(-1 - TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
Bill Wendlingeb907212009-05-15 01:12:28 +0000397 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
Chris Lattneraf76e592009-08-22 20:48:53 +0000398 unsigned SizeTypeID = MCAsmInfo::getSLEB128Size(ValueForTypeID);
Bill Wendlingeb907212009-05-15 01:12:28 +0000399
400 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
Chris Lattneraf76e592009-08-22 20:48:53 +0000401 SizeAction = SizeTypeID + MCAsmInfo::getSLEB128Size(NextAction);
Bill Wendlingeb907212009-05-15 01:12:28 +0000402 SizeSiteActions += SizeAction;
403
Bill Wendlinga583c552009-08-20 22:02:24 +0000404 ActionEntry Action = { ValueForTypeID, NextAction, PrevAction };
Bill Wendlingeb907212009-05-15 01:12:28 +0000405 Actions.push_back(Action);
Bill Wendlingeb907212009-05-15 01:12:28 +0000406 PrevAction = &Actions.back();
407 }
408
409 // Record the first action of the landing pad site.
410 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
411 } // else identical - re-use previous FirstAction
412
Bill Wendlinga583c552009-08-20 22:02:24 +0000413 // Information used when created the call-site table. The action record
414 // field of the call site record is the offset of the first associated
415 // action record, relative to the start of the actions table. This value is
416 // biased by 1 (1 in dicating the start of the actions table), and 0
417 // indicates that there are no actions.
Bill Wendlingeb907212009-05-15 01:12:28 +0000418 FirstActions.push_back(FirstAction);
419
420 // Compute this sites contribution to size.
421 SizeActions += SizeSiteActions;
Bill Wendling5e953dd2009-07-28 23:22:13 +0000422
423 PrevLPI = LPI;
Bill Wendlingeb907212009-05-15 01:12:28 +0000424 }
425
Bill Wendling5e953dd2009-07-28 23:22:13 +0000426 return SizeActions;
427}
428
Bill Wendlingade025c2009-07-29 00:31:35 +0000429/// ComputeCallSiteTable - Compute the call-site table. The entry for an invoke
Bill Wendlinga583c552009-08-20 22:02:24 +0000430/// has a try-range containing the call, a non-zero landing pad, and an
Bill Wendlingade025c2009-07-29 00:31:35 +0000431/// appropriate action. The entry for an ordinary call has a try-range
432/// containing the call and zero for the landing pad and the action. Calls
433/// marked 'nounwind' have no entry and must not be contained in the try-range
434/// of any entry - they form gaps in the table. Entries must be ordered by
435/// try-range address.
436void DwarfException::
437ComputeCallSiteTable(SmallVectorImpl<CallSiteEntry> &CallSites,
438 const RangeMapType &PadMap,
439 const SmallVectorImpl<const LandingPadInfo *> &LandingPads,
440 const SmallVectorImpl<unsigned> &FirstActions) {
Bill Wendlingeb907212009-05-15 01:12:28 +0000441 // The end label of the previous invoke or nounwind try-range.
442 unsigned LastLabel = 0;
443
444 // Whether there is a potentially throwing instruction (currently this means
445 // an ordinary call) between the end of the previous try-range and now.
446 bool SawPotentiallyThrowing = false;
447
Bill Wendling5cff4872009-07-28 23:44:43 +0000448 // Whether the last CallSite entry was for an invoke.
Bill Wendlingeb907212009-05-15 01:12:28 +0000449 bool PreviousIsInvoke = false;
450
451 // Visit all instructions in order of address.
452 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
453 I != E; ++I) {
454 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
455 MI != E; ++MI) {
456 if (!MI->isLabel()) {
457 SawPotentiallyThrowing |= MI->getDesc().isCall();
458 continue;
459 }
460
461 unsigned BeginLabel = MI->getOperand(0).getImm();
462 assert(BeginLabel && "Invalid label!");
463
464 // End of the previous try-range?
465 if (BeginLabel == LastLabel)
466 SawPotentiallyThrowing = false;
467
468 // Beginning of a new try-range?
469 RangeMapType::iterator L = PadMap.find(BeginLabel);
470 if (L == PadMap.end())
471 // Nope, it was just some random label.
472 continue;
473
Bill Wendlinga583c552009-08-20 22:02:24 +0000474 const PadRange &P = L->second;
Bill Wendlingeb907212009-05-15 01:12:28 +0000475 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
Bill Wendlingeb907212009-05-15 01:12:28 +0000476 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
477 "Inconsistent landing pad map!");
478
Bill Wendlinga583c552009-08-20 22:02:24 +0000479 // For Dwarf exception handling (SjLj handling doesn't use this). If some
480 // instruction between the previous try-range and this one may throw,
481 // create a call-site entry with no landing pad for the region between the
482 // try-ranges.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000483 if (SawPotentiallyThrowing &&
Chris Lattner33adcfb2009-08-22 21:43:10 +0000484 MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) {
Bill Wendlinga583c552009-08-20 22:02:24 +0000485 CallSiteEntry Site = { LastLabel, BeginLabel, 0, 0 };
Bill Wendlingeb907212009-05-15 01:12:28 +0000486 CallSites.push_back(Site);
487 PreviousIsInvoke = false;
488 }
489
490 LastLabel = LandingPad->EndLabels[P.RangeIndex];
491 assert(BeginLabel && LastLabel && "Invalid landing pad!");
492
493 if (LandingPad->LandingPadLabel) {
494 // This try-range is for an invoke.
Bill Wendlinga583c552009-08-20 22:02:24 +0000495 CallSiteEntry Site = {
496 BeginLabel,
497 LastLabel,
498 LandingPad->LandingPadLabel,
499 FirstActions[P.PadIndex]
500 };
Bill Wendlingeb907212009-05-15 01:12:28 +0000501
Jim Grosbach33668c02009-09-01 17:19:13 +0000502 // Try to merge with the previous call-site. SJLJ doesn't do this
503 if (PreviousIsInvoke &&
504 MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) {
Bill Wendlingeb907212009-05-15 01:12:28 +0000505 CallSiteEntry &Prev = CallSites.back();
506 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
507 // Extend the range of the previous entry.
508 Prev.EndLabel = Site.EndLabel;
509 continue;
510 }
511 }
512
513 // Otherwise, create a new call-site.
514 CallSites.push_back(Site);
515 PreviousIsInvoke = true;
516 } else {
517 // Create a gap.
518 PreviousIsInvoke = false;
519 }
520 }
521 }
522
523 // If some instruction between the previous try-range and the end of the
524 // function may throw, create a call-site entry with no landing pad for the
525 // region following the try-range.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000526 if (SawPotentiallyThrowing &&
Chris Lattner33adcfb2009-08-22 21:43:10 +0000527 MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) {
Bill Wendlinga583c552009-08-20 22:02:24 +0000528 CallSiteEntry Site = { LastLabel, 0, 0, 0 };
Bill Wendlingeb907212009-05-15 01:12:28 +0000529 CallSites.push_back(Site);
530 }
Bill Wendlingade025c2009-07-29 00:31:35 +0000531}
532
Bill Wendling0dafca92009-07-29 00:50:05 +0000533/// EmitExceptionTable - Emit landing pads and actions.
534///
535/// The general organization of the table is complex, but the basic concepts are
536/// easy. First there is a header which describes the location and organization
537/// of the three components that follow.
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000538///
Bill Wendling0dafca92009-07-29 00:50:05 +0000539/// 1. The landing pad site information describes the range of code covered by
540/// the try. In our case it's an accumulation of the ranges covered by the
541/// invokes in the try. There is also a reference to the landing pad that
542/// handles the exception once processed. Finally an index into the actions
543/// table.
Bill Wendlinga583c552009-08-20 22:02:24 +0000544/// 2. The action table, in our case, is composed of pairs of type IDs and next
Bill Wendling0dafca92009-07-29 00:50:05 +0000545/// action offset. Starting with the action index from the landing pad
Bill Wendlinga583c552009-08-20 22:02:24 +0000546/// site, each type ID is checked for a match to the current exception. If
Bill Wendling0dafca92009-07-29 00:50:05 +0000547/// it matches then the exception and type id are passed on to the landing
548/// pad. Otherwise the next action is looked up. This chain is terminated
549/// with a next action of zero. If no type id is found the the frame is
550/// unwound and handling continues.
Bill Wendlinga583c552009-08-20 22:02:24 +0000551/// 3. Type ID table contains references to all the C++ typeinfo for all
Bill Wendling0dafca92009-07-29 00:50:05 +0000552/// catches in the function. This tables is reversed indexed base 1.
Bill Wendlingade025c2009-07-29 00:31:35 +0000553void DwarfException::EmitExceptionTable() {
554 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
555 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
556 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
557 if (PadInfos.empty()) return;
558
559 // Sort the landing pads in order of their type ids. This is used to fold
560 // duplicate actions.
561 SmallVector<const LandingPadInfo *, 64> LandingPads;
562 LandingPads.reserve(PadInfos.size());
563
564 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
565 LandingPads.push_back(&PadInfos[i]);
566
567 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
568
569 // Compute the actions table and gather the first action index for each
570 // landing pad site.
571 SmallVector<ActionEntry, 32> Actions;
572 SmallVector<unsigned, 64> FirstActions;
573 unsigned SizeActions = ComputeActionsTable(LandingPads, Actions, FirstActions);
574
575 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
576 // by try-range labels when lowered). Ordinary calls do not, so appropriate
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000577 // try-ranges for them need be deduced when using Dwarf exception handling.
Bill Wendlingade025c2009-07-29 00:31:35 +0000578 RangeMapType PadMap;
579 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
580 const LandingPadInfo *LandingPad = LandingPads[i];
581 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
582 unsigned BeginLabel = LandingPad->BeginLabels[j];
583 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
584 PadRange P = { i, j };
585 PadMap[BeginLabel] = P;
586 }
587 }
588
589 // Compute the call-site table.
590 SmallVector<CallSiteEntry, 64> CallSites;
Jim Grosbach8b818d72009-08-17 16:41:22 +0000591 ComputeCallSiteTable(CallSites, PadMap, LandingPads, FirstActions);
Bill Wendlingeb907212009-05-15 01:12:28 +0000592
593 // Final tallies.
594
595 // Call sites.
596 const unsigned SiteStartSize = sizeof(int32_t); // DW_EH_PE_udata4
597 const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4
598 const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000599 unsigned SizeSites;
Jim Grosbachbff39232009-08-12 17:38:44 +0000600
Chris Lattner33adcfb2009-08-22 21:43:10 +0000601 bool HaveTTData = (MAI->getExceptionHandlingType() == ExceptionHandling::SjLj)
Jim Grosbachbff39232009-08-12 17:38:44 +0000602 ? (!TypeInfos.empty() || !FilterIds.empty()) : true;
603
604
Chris Lattner33adcfb2009-08-22 21:43:10 +0000605 if (MAI->getExceptionHandlingType() == ExceptionHandling::SjLj) {
Jim Grosbach8b818d72009-08-17 16:41:22 +0000606 SizeSites = 0;
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000607 } else
608 SizeSites = CallSites.size() *
609 (SiteStartSize + SiteLengthSize + LandingPadSize);
610 for (unsigned i = 0, e = CallSites.size(); i < e; ++i) {
Chris Lattneraf76e592009-08-22 20:48:53 +0000611 SizeSites += MCAsmInfo::getULEB128Size(CallSites[i].Action);
Chris Lattner33adcfb2009-08-22 21:43:10 +0000612 if (MAI->getExceptionHandlingType() == ExceptionHandling::SjLj)
Chris Lattneraf76e592009-08-22 20:48:53 +0000613 SizeSites += MCAsmInfo::getULEB128Size(i);
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000614 }
Bill Wendlingeb907212009-05-15 01:12:28 +0000615 // Type infos.
616 const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
617 unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
618
Bill Wendlingb4049fe2009-09-09 21:06:24 +0000619 unsigned TypeOffset = sizeof(int8_t) + // Call site format
Chris Lattneraf76e592009-08-22 20:48:53 +0000620 MCAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
Bill Wendlingeb907212009-05-15 01:12:28 +0000621 SizeSites + SizeActions + SizeTypes;
622
623 unsigned TotalSize = sizeof(int8_t) + // LPStart format
624 sizeof(int8_t) + // TType format
Jim Grosbachbff39232009-08-12 17:38:44 +0000625 (HaveTTData ?
Chris Lattneraf76e592009-08-22 20:48:53 +0000626 MCAsmInfo::getULEB128Size(TypeOffset) : 0) + // TType base offset
Bill Wendlingeb907212009-05-15 01:12:28 +0000627 TypeOffset;
628
629 unsigned SizeAlign = (4 - TotalSize) & 3;
630
631 // Begin the exception table.
Chris Lattnerd5bbb072009-08-02 01:34:32 +0000632 const MCSection *LSDASection = Asm->getObjFileLowering().getLSDASection();
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000633 Asm->OutStreamer.SwitchSection(LSDASection);
Bill Wendlingeb907212009-05-15 01:12:28 +0000634 Asm->EmitAlignment(2, 0, 0, false);
635 O << "GCC_except_table" << SubprogramCount << ":\n";
636
637 for (unsigned i = 0; i != SizeAlign; ++i) {
638 Asm->EmitInt8(0);
639 Asm->EOL("Padding");
Bill Wendlingc5800a82009-07-28 21:54:03 +0000640 }
Bill Wendlingeb907212009-05-15 01:12:28 +0000641
642 EmitLabel("exception", SubprogramCount);
Chris Lattner33adcfb2009-08-22 21:43:10 +0000643 if (MAI->getExceptionHandlingType() == ExceptionHandling::SjLj) {
Jim Grosbachee793a62009-09-01 18:55:08 +0000644 SmallString<16> LSDAName;
Jim Grosbachc40d9f92009-09-01 18:49:12 +0000645 raw_svector_ostream(LSDAName) << MAI->getPrivateGlobalPrefix() <<
646 "_LSDA_" << Asm->getFunctionNumber();
647 O << LSDAName.str() << ":\n";
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000648 }
Bill Wendlingeb907212009-05-15 01:12:28 +0000649
650 // Emit the header.
Bill Wendling639217c2009-08-27 03:32:50 +0000651 Asm->EmitInt8(dwarf::DW_EH_PE_omit);
Bill Wendling0734d352009-09-09 21:26:19 +0000652 Asm->EOL("@LPStart format", dwarf::DW_EH_PE_omit);
Bill Wendlingb0d9c3e2009-07-28 22:23:45 +0000653
Bill Wendlingade025c2009-07-29 00:31:35 +0000654#if 0
Chris Lattner81c9a062009-07-31 22:03:47 +0000655 if (TypeInfos.empty() && FilterIds.empty()) {
Chris Lattnerad88bc42009-08-02 03:59:56 +0000656 // If there are no typeinfos or filters, there is nothing to emit, optimize
657 // by specifying the "omit" encoding.
Bill Wendling639217c2009-08-27 03:32:50 +0000658 Asm->EmitInt8(dwarf::DW_EH_PE_omit);
Bill Wendling0734d352009-09-09 21:26:19 +0000659 Asm->EOL("@TType format", dwarf::DW_EH_PE_omit);
Chris Lattner81c9a062009-07-31 22:03:47 +0000660 } else {
Chris Lattnerad88bc42009-08-02 03:59:56 +0000661 // Okay, we have actual filters or typeinfos to emit. As such, we need to
662 // pick a type encoding for them. We're about to emit a list of pointers to
663 // typeinfo objects at the end of the LSDA. However, unless we're in static
664 // mode, this reference will require a relocation by the dynamic linker.
Chris Lattner46b754c2009-07-31 22:18:14 +0000665 //
Chris Lattnerad88bc42009-08-02 03:59:56 +0000666 // Because of this, we have a couple of options:
667 // 1) If we are in -static mode, we can always use an absolute reference
668 // from the LSDA, because the static linker will resolve it.
669 // 2) Otherwise, if the LSDA section is writable, we can output the direct
670 // reference to the typeinfo and allow the dynamic linker to relocate
671 // it. Since it is in a writable section, the dynamic linker won't
672 // have a problem.
673 // 3) Finally, if we're in PIC mode and the LDSA section isn't writable,
674 // we need to use some form of indirection. For example, on Darwin,
675 // we can output a statically-relocatable reference to a dyld stub. The
676 // offset to the stub is constant, but the contents are in a section
677 // that is updated by the dynamic linker. This is easy enough, but we
678 // need to tell the personality function of the unwinder to indirect
679 // through the dyld stub.
680 //
681 // FIXME: When this is actually implemented, we'll have to emit the stubs
682 // somewhere. This predicate should be moved to a shared location that is
683 // in target-independent code.
684 //
685 if (LSDASection->isWritable() ||
686 Asm->TM.getRelocationModel() == Reloc::Static) {
Bill Wendling639217c2009-08-27 03:32:50 +0000687 Asm->EmitInt8(DW_EH_PE_absptr);
688 Asm->EOL("TType format (DW_EH_PE_absptr)");
Chris Lattnerad88bc42009-08-02 03:59:56 +0000689 } else {
690 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_indirect | DW_EH_PE_sdata4);
Bill Wendling639217c2009-08-27 03:32:50 +0000691 Asm->EOL("TType format (DW_EH_PE_pcrel | DW_EH_PE_indirect"
692 " | DW_EH_PE_sdata4)");
Chris Lattnerad88bc42009-08-02 03:59:56 +0000693 }
Bill Wendlingb0d9c3e2009-07-28 22:23:45 +0000694 Asm->EmitULEB128Bytes(TypeOffset);
695 Asm->EOL("TType base offset");
Bill Wendlingb0d9c3e2009-07-28 22:23:45 +0000696 }
Bill Wendlingade025c2009-07-29 00:31:35 +0000697#else
Jim Grosbachbff39232009-08-12 17:38:44 +0000698 // For SjLj exceptions, if there is no TypeInfo, then we just explicitly
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000699 // say that we're omitting that bit.
700 // FIXME: does this apply to Dwarf also? The above #if 0 implies yes?
Jim Grosbachbff39232009-08-12 17:38:44 +0000701 if (!HaveTTData) {
Bill Wendling639217c2009-08-27 03:32:50 +0000702 Asm->EmitInt8(dwarf::DW_EH_PE_omit);
Bill Wendling0734d352009-09-09 21:26:19 +0000703 Asm->EOL("@TType format", dwarf::DW_EH_PE_omit);
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000704 } else {
Bill Wendling639217c2009-08-27 03:32:50 +0000705 Asm->EmitInt8(dwarf::DW_EH_PE_absptr);
Bill Wendling0734d352009-09-09 21:26:19 +0000706 Asm->EOL("@TType format", dwarf::DW_EH_PE_absptr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000707 Asm->EmitULEB128Bytes(TypeOffset);
Bill Wendlinga583c552009-08-20 22:02:24 +0000708 Asm->EOL("@TType base offset");
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000709 }
Bill Wendlingade025c2009-07-29 00:31:35 +0000710#endif
Bill Wendlingb0d9c3e2009-07-28 22:23:45 +0000711
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000712 // SjLj Exception handilng
Chris Lattner33adcfb2009-08-22 21:43:10 +0000713 if (MAI->getExceptionHandlingType() == ExceptionHandling::SjLj) {
Bill Wendling639217c2009-08-27 03:32:50 +0000714 Asm->EmitInt8(dwarf::DW_EH_PE_udata4);
Bill Wendling0734d352009-09-09 21:26:19 +0000715 Asm->EOL("Call site format", dwarf::DW_EH_PE_udata4);
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000716 Asm->EmitULEB128Bytes(SizeSites);
Bill Wendlinga583c552009-08-20 22:02:24 +0000717 Asm->EOL("Call site table length");
Bill Wendlingeb907212009-05-15 01:12:28 +0000718
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000719 // Emit the landing pad site information.
Jim Grosbach8b818d72009-08-17 16:41:22 +0000720 unsigned idx = 0;
721 for (SmallVectorImpl<CallSiteEntry>::const_iterator
722 I = CallSites.begin(), E = CallSites.end(); I != E; ++I, ++idx) {
723 const CallSiteEntry &S = *I;
Bill Wendlinga583c552009-08-20 22:02:24 +0000724
725 // Offset of the landing pad, counted in 16-byte bundles relative to the
726 // @LPStart address.
Jim Grosbach8b818d72009-08-17 16:41:22 +0000727 Asm->EmitULEB128Bytes(idx);
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000728 Asm->EOL("Landing pad");
Bill Wendlinga583c552009-08-20 22:02:24 +0000729
730 // Offset of the first associated action record, relative to the start of
731 // the action table. This value is biased by 1 (1 indicates the start of
732 // the action table), and 0 indicates that there are no actions.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000733 Asm->EmitULEB128Bytes(S.Action);
734 Asm->EOL("Action");
Bill Wendlingeb907212009-05-15 01:12:28 +0000735 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000736 } else {
737 // DWARF Exception handling
Chris Lattner33adcfb2009-08-22 21:43:10 +0000738 assert(MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf);
Bill Wendlingeb907212009-05-15 01:12:28 +0000739
Bill Wendlinga583c552009-08-20 22:02:24 +0000740 // The call-site table is a list of all call sites that may throw an
741 // exception (including C++ 'throw' statements) in the procedure
742 // fragment. It immediately follows the LSDA header. Each entry indicates,
743 // for a given call, the first corresponding action record and corresponding
744 // landing pad.
745 //
746 // The table begins with the number of bytes, stored as an LEB128
747 // compressed, unsigned integer. The records immediately follow the record
748 // count. They are sorted in increasing call-site address. Each record
749 // indicates:
750 //
751 // * The position of the call-site.
752 // * The position of the landing pad.
753 // * The first action record for that call site.
754 //
755 // A missing entry in the call-site table indicates that a call is not
756 // supposed to throw. Such calls include:
757 //
758 // * Calls to destructors within cleanup code. C++ semantics forbids these
759 // calls to throw.
760 // * Calls to intrinsic routines in the standard library which are known
761 // not to throw (sin, memcpy, et al).
762 //
763 // If the runtime does not find the call-site entry for a given call, it
764 // will call `terminate()'.
765
766 // Emit the landing pad call site table.
Bill Wendling639217c2009-08-27 03:32:50 +0000767 Asm->EmitInt8(dwarf::DW_EH_PE_udata4);
Bill Wendling0734d352009-09-09 21:26:19 +0000768 Asm->EOL("Call site format", dwarf::DW_EH_PE_udata4);
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000769 Asm->EmitULEB128Bytes(SizeSites);
Bill Wendlinga583c552009-08-20 22:02:24 +0000770 Asm->EOL("Call site table size");
Bill Wendlingeb907212009-05-15 01:12:28 +0000771
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000772 for (SmallVectorImpl<CallSiteEntry>::const_iterator
773 I = CallSites.begin(), E = CallSites.end(); I != E; ++I) {
774 const CallSiteEntry &S = *I;
775 const char *BeginTag;
776 unsigned BeginNumber;
Bill Wendlingeb907212009-05-15 01:12:28 +0000777
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000778 if (!S.BeginLabel) {
779 BeginTag = "eh_func_begin";
780 BeginNumber = SubprogramCount;
781 } else {
782 BeginTag = "label";
783 BeginNumber = S.BeginLabel;
784 }
Bill Wendlingeb907212009-05-15 01:12:28 +0000785
Bill Wendlinga583c552009-08-20 22:02:24 +0000786 // Offset of the call site relative to the previous call site, counted in
787 // number of 16-byte bundles. The first call site is counted relative to
788 // the start of the procedure fragment.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000789 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
Bill Wendlingeb907212009-05-15 01:12:28 +0000790 true, true);
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000791 Asm->EOL("Region start");
Bill Wendlingeb907212009-05-15 01:12:28 +0000792
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000793 if (!S.EndLabel)
794 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
795 true);
796 else
797 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
Bill Wendlingeb907212009-05-15 01:12:28 +0000798
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000799 Asm->EOL("Region length");
800
Bill Wendlinga583c552009-08-20 22:02:24 +0000801 // Offset of the landing pad, counted in 16-byte bundles relative to the
802 // @LPStart address.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000803 if (!S.PadLabel)
804 Asm->EmitInt32(0);
805 else
806 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
807 true, true);
808
809 Asm->EOL("Landing pad");
810
Bill Wendlinga583c552009-08-20 22:02:24 +0000811 // Offset of the first associated action record, relative to the start of
812 // the action table. This value is biased by 1 (1 indicates the start of
813 // the action table), and 0 indicates that there are no actions.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000814 Asm->EmitULEB128Bytes(S.Action);
815 Asm->EOL("Action");
816 }
Bill Wendlingeb907212009-05-15 01:12:28 +0000817 }
818
Bill Wendlinga583c552009-08-20 22:02:24 +0000819 // Emit the Action Table.
Bill Wendling5cff4872009-07-28 23:44:43 +0000820 for (SmallVectorImpl<ActionEntry>::const_iterator
821 I = Actions.begin(), E = Actions.end(); I != E; ++I) {
822 const ActionEntry &Action = *I;
Bill Wendlinga583c552009-08-20 22:02:24 +0000823
824 // Type Filter
825 //
826 // Used by the runtime to match the type of the thrown exception to the
827 // type of the catch clauses or the types in the exception specification.
828
Bill Wendlingeb907212009-05-15 01:12:28 +0000829 Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
830 Asm->EOL("TypeInfo index");
Bill Wendlinga583c552009-08-20 22:02:24 +0000831
832 // Action Record
833 //
834 // Self-relative signed displacement in bytes of the next action record,
835 // or 0 if there is no next action record.
836
Bill Wendlingeb907212009-05-15 01:12:28 +0000837 Asm->EmitSLEB128Bytes(Action.NextAction);
838 Asm->EOL("Next action");
839 }
840
Bill Wendlinga583c552009-08-20 22:02:24 +0000841 // Emit the Catch Clauses. The code for the catch clauses following the same
842 // try is similar to a switch statement. The catch clause action record
843 // informs the runtime about the type of a catch clause and about the
844 // associated switch value.
845 //
846 // Action Record Fields:
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000847 //
Bill Wendlinga583c552009-08-20 22:02:24 +0000848 // * Filter Value
849 // Positive value, starting at 1. Index in the types table of the
850 // __typeinfo for the catch-clause type. 1 is the first word preceding
851 // TTBase, 2 is the second word, and so on. Used by the runtime to check
852 // if the thrown exception type matches the catch-clause type. Back-end
853 // generated switch statements check against this value.
854 //
855 // * Next
856 // Signed offset, in bytes from the start of this field, to the next
857 // chained action record, or zero if none.
858 //
859 // The order of the action records determined by the next field is the order
860 // of the catch clauses as they appear in the source code, and must be kept in
861 // the same order. As a result, changing the order of the catch clause would
862 // change the semantics of the program.
Bill Wendling5cff4872009-07-28 23:44:43 +0000863 for (std::vector<GlobalVariable *>::const_reverse_iterator
864 I = TypeInfos.rbegin(), E = TypeInfos.rend(); I != E; ++I) {
Bill Wendlinga583c552009-08-20 22:02:24 +0000865 const GlobalVariable *GV = *I;
Bill Wendlingeb907212009-05-15 01:12:28 +0000866 PrintRelDirective();
867
868 if (GV) {
869 std::string GLN;
870 O << Asm->getGlobalLinkName(GV, GLN);
871 } else {
Bill Wendling049e98d2009-08-31 18:26:48 +0000872 O << "0x0";
Bill Wendlingeb907212009-05-15 01:12:28 +0000873 }
874
875 Asm->EOL("TypeInfo");
876 }
877
Bill Wendlinga583c552009-08-20 22:02:24 +0000878 // Emit the Type Table.
Bill Wendling5cff4872009-07-28 23:44:43 +0000879 for (std::vector<unsigned>::const_iterator
880 I = FilterIds.begin(), E = FilterIds.end(); I < E; ++I) {
881 unsigned TypeID = *I;
Bill Wendlingeb907212009-05-15 01:12:28 +0000882 Asm->EmitULEB128Bytes(TypeID);
883 Asm->EOL("Filter TypeInfo index");
884 }
885
886 Asm->EmitAlignment(2, 0, 0, false);
887}
888
Bill Wendlingeb907212009-05-15 01:12:28 +0000889/// EndModule - Emit all exception information that should come after the
890/// content.
891void DwarfException::EndModule() {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000892 if (MAI->getExceptionHandlingType() != ExceptionHandling::Dwarf)
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000893 return;
Bill Wendlingb4049fe2009-09-09 21:06:24 +0000894
Bill Wendling52783c62009-09-09 23:56:55 +0000895 if (!shouldEmitMovesModule && !shouldEmitTableModule)
896 return;
897
Bill Wendlingeb907212009-05-15 01:12:28 +0000898 if (TimePassesIsEnabled)
899 ExceptionTimer->startTimer();
900
Bill Wendling52783c62009-09-09 23:56:55 +0000901 const std::vector<Function *> Personalities = MMI->getPersonalities();
Bill Wendlingb4049fe2009-09-09 21:06:24 +0000902
Bill Wendling52783c62009-09-09 23:56:55 +0000903 for (unsigned i = 0, e = Personalities.size(); i < e; ++i)
904 EmitCIE(Personalities[i], i);
Bill Wendlingeb907212009-05-15 01:12:28 +0000905
Bill Wendling52783c62009-09-09 23:56:55 +0000906 for (std::vector<FunctionEHFrameInfo>::iterator
907 I = EHFrames.begin(), E = EHFrames.end(); I != E; ++I)
908 EmitFDE(*I);
Bill Wendlingeb907212009-05-15 01:12:28 +0000909
910 if (TimePassesIsEnabled)
911 ExceptionTimer->stopTimer();
912}
913
914/// BeginFunction - Gather pre-function exception information. Assumes being
915/// emitted immediately after the function entry point.
916void DwarfException::BeginFunction(MachineFunction *MF) {
917 if (TimePassesIsEnabled)
918 ExceptionTimer->startTimer();
919
920 this->MF = MF;
921 shouldEmitTable = shouldEmitMoves = false;
922
Chris Lattner33adcfb2009-08-22 21:43:10 +0000923 if (MMI && MAI->doesSupportExceptionHandling()) {
Bill Wendlingeb907212009-05-15 01:12:28 +0000924 // Map all labels and get rid of any dead landing pads.
925 MMI->TidyLandingPads();
926
927 // If any landing pads survive, we need an EH table.
Bill Wendlingb4049fe2009-09-09 21:06:24 +0000928 if (!MMI->getLandingPads().empty())
Bill Wendlingeb907212009-05-15 01:12:28 +0000929 shouldEmitTable = true;
930
931 // See if we need frame move info.
932 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
933 shouldEmitMoves = true;
934
935 if (shouldEmitMoves || shouldEmitTable)
936 // Assumes in correct section after the entry point.
937 EmitLabel("eh_func_begin", ++SubprogramCount);
938 }
939
940 shouldEmitTableModule |= shouldEmitTable;
941 shouldEmitMovesModule |= shouldEmitMoves;
942
943 if (TimePassesIsEnabled)
944 ExceptionTimer->stopTimer();
945}
946
947/// EndFunction - Gather and emit post-function exception information.
948///
949void DwarfException::EndFunction() {
Bill Wendling7b09a6c2009-09-09 21:08:12 +0000950 if (!shouldEmitMoves && !shouldEmitTable) return;
951
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000952 if (TimePassesIsEnabled)
Bill Wendlingeb907212009-05-15 01:12:28 +0000953 ExceptionTimer->startTimer();
954
Bill Wendling7b09a6c2009-09-09 21:08:12 +0000955 EmitLabel("eh_func_end", SubprogramCount);
956 EmitExceptionTable();
Bill Wendlingeb907212009-05-15 01:12:28 +0000957
Bill Wendling7b09a6c2009-09-09 21:08:12 +0000958 // Save EH frame information
959 EHFrames.push_back(FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF),
960 SubprogramCount,
961 MMI->getPersonalityIndex(),
962 MF->getFrameInfo()->hasCalls(),
963 !MMI->getLandingPads().empty(),
964 MMI->getFrameMoves(),
965 MF->getFunction()));
Bill Wendlingeb907212009-05-15 01:12:28 +0000966
Bill Wendling52783c62009-09-09 23:56:55 +0000967 // Record if this personality index uses a landing pad.
968 UsesLSDA[MMI->getPersonalityIndex()] |= !MMI->getLandingPads().empty();
969
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000970 if (TimePassesIsEnabled)
Bill Wendlingeb907212009-05-15 01:12:28 +0000971 ExceptionTimer->stopTimer();
972}