blob: 06f9590dd6c5ccdd8c524785d90cecb0ad6138cc [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 Wendling28275fd2009-09-10 06:50:01 +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"
Bill Wendling43e484f2009-09-10 01:12:47 +000020#include "llvm/MC/MCSection.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000021#include "llvm/MC/MCStreamer.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000022#include "llvm/MC/MCAsmInfo.h"
Bill Wendlingeb907212009-05-15 01:12:28 +000023#include "llvm/Target/TargetData.h"
24#include "llvm/Target/TargetFrameInfo.h"
Chris Lattnerd5bbb072009-08-02 01:34:32 +000025#include "llvm/Target/TargetLoweringObjectFile.h"
Bill Wendlingeb907212009-05-15 01:12:28 +000026#include "llvm/Target/TargetOptions.h"
Chris Lattnerd5bbb072009-08-02 01:34:32 +000027#include "llvm/Target/TargetRegisterInfo.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000028#include "llvm/Support/Dwarf.h"
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +000029#include "llvm/Support/Mangler.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000030#include "llvm/Support/Timer.h"
31#include "llvm/Support/raw_ostream.h"
Jim Grosbachc40d9f92009-09-01 18:49:12 +000032#include "llvm/ADT/SmallString.h"
Bill Wendlingeb907212009-05-15 01:12:28 +000033#include "llvm/ADT/StringExtras.h"
34using namespace llvm;
35
36static TimerGroup &getDwarfTimerGroup() {
Bill Wendling28275fd2009-09-10 06:50:01 +000037 static TimerGroup DwarfTimerGroup("DWARF Exception");
Bill Wendlingeb907212009-05-15 01:12:28 +000038 return DwarfTimerGroup;
39}
40
Bill Wendlingbc0d23a2009-05-15 01:18:50 +000041DwarfException::DwarfException(raw_ostream &OS, AsmPrinter *A,
Chris Lattneraf76e592009-08-22 20:48:53 +000042 const MCAsmInfo *T)
Bill Wendlingbc0d23a2009-05-15 01:18:50 +000043 : Dwarf(OS, A, T, "eh"), shouldEmitTable(false), shouldEmitMoves(false),
44 shouldEmitTableModule(false), shouldEmitMovesModule(false),
45 ExceptionTimer(0) {
Eric Christopherdbfcdb92009-08-28 22:33:43 +000046 if (TimePassesIsEnabled)
Bill Wendling28275fd2009-09-10 06:50:01 +000047 ExceptionTimer = new Timer("DWARF Exception Writer",
Bill Wendlingbc0d23a2009-05-15 01:18:50 +000048 getDwarfTimerGroup());
49}
50
51DwarfException::~DwarfException() {
52 delete ExceptionTimer;
53}
54
Bill Wendlingbb3e2992009-09-10 00:04:48 +000055/// SizeOfEncodedValue - Return the size of the encoding in bytes.
Bill Wendling52783c62009-09-09 23:56:55 +000056unsigned DwarfException::SizeOfEncodedValue(unsigned Encoding) {
57 if (Encoding == dwarf::DW_EH_PE_omit)
58 return 0;
59
60 switch (Encoding & 0x07) {
61 case dwarf::DW_EH_PE_absptr:
62 return TD->getPointerSize();
63 case dwarf::DW_EH_PE_udata2:
64 return 2;
65 case dwarf::DW_EH_PE_udata4:
66 return 4;
67 case dwarf::DW_EH_PE_udata8:
68 return 8;
69 }
70
Bill Wendling28275fd2009-09-10 06:50:01 +000071 assert(0 && "Invalid encoded value.");
Bill Wendling52783c62009-09-09 23:56:55 +000072 return 0;
73}
74
Bill Wendling7ccda0f2009-08-25 08:08:33 +000075/// EmitCIE - Emit a Common Information Entry (CIE). This holds information that
76/// is shared among many Frame Description Entries. There is at least one CIE
77/// in every non-empty .debug_frame section.
78void DwarfException::EmitCIE(const Function *Personality, unsigned Index) {
Bill Wendlingeb907212009-05-15 01:12:28 +000079 // Size and sign of stack growth.
80 int stackGrowth =
81 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
82 TargetFrameInfo::StackGrowsUp ?
83 TD->getPointerSize() : -TD->getPointerSize();
84
85 // Begin eh frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +000086 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getEHFrameSection());
Bill Wendlingeb907212009-05-15 01:12:28 +000087
Chris Lattner33adcfb2009-08-22 21:43:10 +000088 if (MAI->is_EHSymbolPrivate())
89 O << MAI->getPrivateGlobalPrefix();
Bill Wendlingeb907212009-05-15 01:12:28 +000090
91 O << "EH_frame" << Index << ":\n";
92 EmitLabel("section_eh_frame", Index);
93
94 // Define base labels.
95 EmitLabel("eh_frame_common", Index);
96
97 // Define the eh frame length.
98 EmitDifference("eh_frame_common_end", Index,
99 "eh_frame_common_begin", Index, true);
100 Asm->EOL("Length of Common Information Entry");
101
102 // EH frame header.
103 EmitLabel("eh_frame_common_begin", Index);
104 Asm->EmitInt32((int)0);
105 Asm->EOL("CIE Identifier Tag");
106 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
107 Asm->EOL("CIE Version");
108
109 // The personality presence indicates that language specific information will
110 // show up in the eh frame.
Bill Wendling52783c62009-09-09 23:56:55 +0000111
112 // FIXME: Don't hardcode these encodings.
113 unsigned PerEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
114 if (Personality && MAI->getNeedsIndirectEncoding())
115 PerEncoding |= dwarf::DW_EH_PE_indirect;
116 unsigned LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
117 unsigned FDEEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
118
119 char Augmentation[5] = { 0 };
120 unsigned AugmentationSize = 0;
121 char *APtr = Augmentation + 1;
122
123 if (Personality) {
124 // There is a personality function.
125 *APtr++ = 'P';
126 AugmentationSize += 1 + SizeOfEncodedValue(PerEncoding);
127 }
128
129 if (UsesLSDA[Index]) {
130 // An LSDA pointer is in the FDE augmentation.
131 *APtr++ = 'L';
132 ++AugmentationSize;
133 }
134
135 if (FDEEncoding != dwarf::DW_EH_PE_absptr) {
136 // A non-default pointer encoding for the FDE.
137 *APtr++ = 'R';
138 ++AugmentationSize;
139 }
140
141 if (APtr != Augmentation + 1)
142 Augmentation[0] = 'z';
143
144 Asm->EmitString(Augmentation);
Bill Wendlingeb907212009-05-15 01:12:28 +0000145 Asm->EOL("CIE Augmentation");
146
147 // Round out reader.
148 Asm->EmitULEB128Bytes(1);
149 Asm->EOL("CIE Code Alignment Factor");
150 Asm->EmitSLEB128Bytes(stackGrowth);
151 Asm->EOL("CIE Data Alignment Factor");
152 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
153 Asm->EOL("CIE Return Address Column");
154
Bill Wendling52783c62009-09-09 23:56:55 +0000155 Asm->EmitULEB128Bytes(AugmentationSize);
156 Asm->EOL("Augmentation Size");
157
158 Asm->EmitInt8(PerEncoding);
159 Asm->EOL("Personality", PerEncoding);
160
Bill Wendling4bda11f2009-08-25 02:32:05 +0000161 // If there is a personality, we need to indicate the function's location.
Bill Wendlingeb907212009-05-15 01:12:28 +0000162 if (Personality) {
Chris Lattnerf60d3eb2009-09-15 22:58:35 +0000163 O << MAI->getData32bitsDirective();
164
Chris Lattner33adcfb2009-08-22 21:43:10 +0000165 O << MAI->getPersonalityPrefix();
Chris Lattner1c90c6f2009-09-16 00:17:39 +0000166 O << Asm->Mang->getMangledName(Personality);
Chris Lattner33adcfb2009-08-22 21:43:10 +0000167 O << MAI->getPersonalitySuffix();
Chris Lattnerf60d3eb2009-09-15 22:58:35 +0000168
Chris Lattner33adcfb2009-08-22 21:43:10 +0000169 if (strcmp(MAI->getPersonalitySuffix(), "+4@GOTPCREL"))
170 O << "-" << MAI->getPCSymbol();
Bill Wendlingeb907212009-05-15 01:12:28 +0000171 Asm->EOL("Personality");
172
Bill Wendling52783c62009-09-09 23:56:55 +0000173 Asm->EmitInt8(LSDAEncoding);
174 Asm->EOL("LSDA Encoding", LSDAEncoding);
Bill Wendlingeb907212009-05-15 01:12:28 +0000175
Bill Wendling52783c62009-09-09 23:56:55 +0000176 Asm->EmitInt8(FDEEncoding);
177 Asm->EOL("FDE Encoding", FDEEncoding);
Bill Wendlingeb907212009-05-15 01:12:28 +0000178 }
179
180 // Indicate locations of general callee saved registers in frame.
181 std::vector<MachineMove> Moves;
182 RI->getInitialFrameState(Moves);
183 EmitFrameMoves(NULL, 0, Moves, true);
184
185 // On Darwin the linker honors the alignment of eh_frame, which means it must
186 // be 8-byte on 64-bit targets to match what gcc does. Otherwise you get
187 // holes which confuse readers of eh_frame.
188 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
189 0, 0, false);
190 EmitLabel("eh_frame_common_end", Index);
191
192 Asm->EOL();
193}
194
Bill Wendling7ccda0f2009-08-25 08:08:33 +0000195/// EmitFDE - Emit the Frame Description Entry (FDE) for the function.
196void DwarfException::EmitFDE(const FunctionEHFrameInfo &EHFrameInfo) {
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000197 assert(!EHFrameInfo.function->hasAvailableExternallyLinkage() &&
Bill Wendlingeb907212009-05-15 01:12:28 +0000198 "Should not emit 'available externally' functions at all");
199
Chris Lattner3e0f60b2009-07-17 21:00:50 +0000200 const Function *TheFunc = EHFrameInfo.function;
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000201
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000202 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getEHFrameSection());
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000203
Bill Wendlingeb907212009-05-15 01:12:28 +0000204 // Externally visible entry into the functions eh frame info. If the
205 // corresponding function is static, this should not be externally visible.
Chris Lattner3e0f60b2009-07-17 21:00:50 +0000206 if (!TheFunc->hasLocalLinkage())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000207 if (const char *GlobalEHDirective = MAI->getGlobalEHDirective())
Bill Wendlingeb907212009-05-15 01:12:28 +0000208 O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
Bill Wendlingeb907212009-05-15 01:12:28 +0000209
210 // If corresponding function is weak definition, this should be too.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000211 if (TheFunc->isWeakForLinker() && MAI->getWeakDefDirective())
212 O << MAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
Bill Wendlingeb907212009-05-15 01:12:28 +0000213
214 // If there are no calls then you can't unwind. This may mean we can omit the
215 // EH Frame, but some environments do not handle weak absolute symbols. If
216 // UnwindTablesMandatory is set we cannot do this optimization; the unwind
217 // info is to be available for non-EH uses.
Chris Lattner3e0f60b2009-07-17 21:00:50 +0000218 if (!EHFrameInfo.hasCalls && !UnwindTablesMandatory &&
219 (!TheFunc->isWeakForLinker() ||
Chris Lattner33adcfb2009-08-22 21:43:10 +0000220 !MAI->getWeakDefDirective() ||
221 MAI->getSupportsWeakOmittedEHFrame())) {
Bill Wendlingeb907212009-05-15 01:12:28 +0000222 O << EHFrameInfo.FnName << " = 0\n";
223 // This name has no connection to the function, so it might get
224 // dead-stripped when the function is not, erroneously. Prohibit
225 // dead-stripping unconditionally.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000226 if (const char *UsedDirective = MAI->getUsedDirective())
Bill Wendlingeb907212009-05-15 01:12:28 +0000227 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
228 } else {
229 O << EHFrameInfo.FnName << ":\n";
230
231 // EH frame header.
232 EmitDifference("eh_frame_end", EHFrameInfo.Number,
233 "eh_frame_begin", EHFrameInfo.Number, true);
234 Asm->EOL("Length of Frame Information Entry");
235
236 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
237
Chris Lattnera4ff5e42009-07-17 20:53:51 +0000238 EmitSectionOffset("eh_frame_begin", "eh_frame_common",
239 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
240 true, true, false);
Bill Wendlingeb907212009-05-15 01:12:28 +0000241
242 Asm->EOL("FDE CIE offset");
243
Duncan Sandsc69d74a2009-08-31 16:45:16 +0000244 EmitReference("eh_func_begin", EHFrameInfo.Number, true, true);
Bill Wendlingeb907212009-05-15 01:12:28 +0000245 Asm->EOL("FDE initial location");
246 EmitDifference("eh_func_end", EHFrameInfo.Number,
Duncan Sandsc69d74a2009-08-31 16:45:16 +0000247 "eh_func_begin", EHFrameInfo.Number, true);
Bill Wendlingeb907212009-05-15 01:12:28 +0000248 Asm->EOL("FDE address range");
249
250 // If there is a personality and landing pads then point to the language
251 // specific data area in the exception table.
Eric Christopherd44fff72009-08-26 21:30:49 +0000252 if (MMI->getPersonalities()[0] != NULL) {
Duncan Sandsc69d74a2009-08-31 16:45:16 +0000253 bool is4Byte = TD->getPointerSize() == sizeof(int32_t);
254
Eric Christopher6fefceb2009-08-29 01:12:46 +0000255 Asm->EmitULEB128Bytes(is4Byte ? 4 : 8);
Bill Wendlingeb907212009-05-15 01:12:28 +0000256 Asm->EOL("Augmentation size");
257
Duncan Sandsc69d74a2009-08-31 16:45:16 +0000258 if (EHFrameInfo.hasLandingPads)
Eric Christopher6fefceb2009-08-29 01:12:46 +0000259 EmitReference("exception", EHFrameInfo.Number, true, false);
Duncan Sandsc69d74a2009-08-31 16:45:16 +0000260 else {
Bill Wendling52783c62009-09-09 23:56:55 +0000261 if (is4Byte)
262 Asm->EmitInt32((int)0);
263 else
264 Asm->EmitInt64((int)0);
Eric Christopher6fefceb2009-08-29 01:12:46 +0000265 }
Bill Wendlingeb907212009-05-15 01:12:28 +0000266 Asm->EOL("Language Specific Data Area");
267 } else {
268 Asm->EmitULEB128Bytes(0);
269 Asm->EOL("Augmentation size");
270 }
271
272 // Indicate locations of function specific callee saved registers in frame.
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000273 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves,
Bill Wendlingeb907212009-05-15 01:12:28 +0000274 true);
275
276 // On Darwin the linker honors the alignment of eh_frame, which means it
277 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise you
278 // get holes which confuse readers of eh_frame.
279 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
280 0, 0, false);
281 EmitLabel("eh_frame_end", EHFrameInfo.Number);
282
283 // If the function is marked used, this table should be also. We cannot
284 // make the mark unconditional in this case, since retaining the table also
285 // retains the function in this case, and there is code around that depends
286 // on unused functions (calling undefined externals) being dead-stripped to
287 // link correctly. Yes, there really is.
Chris Lattner401e10c2009-07-20 06:14:25 +0000288 if (MMI->isUsedFunction(EHFrameInfo.function))
Chris Lattner33adcfb2009-08-22 21:43:10 +0000289 if (const char *UsedDirective = MAI->getUsedDirective())
Bill Wendlingeb907212009-05-15 01:12:28 +0000290 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
291 }
Bill Wendling4bda11f2009-08-25 02:32:05 +0000292
293 Asm->EOL();
Bill Wendlingeb907212009-05-15 01:12:28 +0000294}
295
Bill Wendlingeb907212009-05-15 01:12:28 +0000296/// SharedTypeIds - How many leading type ids two landing pads have in common.
297unsigned DwarfException::SharedTypeIds(const LandingPadInfo *L,
298 const LandingPadInfo *R) {
299 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
300 unsigned LSize = LIds.size(), RSize = RIds.size();
301 unsigned MinSize = LSize < RSize ? LSize : RSize;
302 unsigned Count = 0;
303
304 for (; Count != MinSize; ++Count)
305 if (LIds[Count] != RIds[Count])
306 return Count;
307
308 return Count;
309}
310
311/// PadLT - Order landing pads lexicographically by type id.
312bool DwarfException::PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
313 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
314 unsigned LSize = LIds.size(), RSize = RIds.size();
315 unsigned MinSize = LSize < RSize ? LSize : RSize;
316
317 for (unsigned i = 0; i != MinSize; ++i)
318 if (LIds[i] != RIds[i])
319 return LIds[i] < RIds[i];
320
321 return LSize < RSize;
322}
323
Bill Wendlingd4609622009-07-28 23:23:00 +0000324/// ComputeActionsTable - Compute the actions table and gather the first action
325/// index for each landing pad site.
Bill Wendlingade025c2009-07-29 00:31:35 +0000326unsigned DwarfException::
327ComputeActionsTable(const SmallVectorImpl<const LandingPadInfo*> &LandingPads,
328 SmallVectorImpl<ActionEntry> &Actions,
329 SmallVectorImpl<unsigned> &FirstActions) {
Bill Wendlinga583c552009-08-20 22:02:24 +0000330
331 // The action table follows the call-site table in the LSDA. The individual
332 // records are of two types:
333 //
334 // * Catch clause
335 // * Exception specification
336 //
337 // The two record kinds have the same format, with only small differences.
338 // They are distinguished by the "switch value" field: Catch clauses
339 // (TypeInfos) have strictly positive switch values, and exception
340 // specifications (FilterIds) have strictly negative switch values. Value 0
341 // indicates a catch-all clause.
342 //
Bill Wendling5e953dd2009-07-28 23:22:13 +0000343 // Negative type IDs index into FilterIds. Positive type IDs index into
344 // TypeInfos. The value written for a positive type ID is just the type ID
345 // itself. For a negative type ID, however, the value written is the
Bill Wendlingeb907212009-05-15 01:12:28 +0000346 // (negative) byte offset of the corresponding FilterIds entry. The byte
Bill Wendling5e953dd2009-07-28 23:22:13 +0000347 // offset is usually equal to the type ID (because the FilterIds entries are
348 // written using a variable width encoding, which outputs one byte per entry
349 // as long as the value written is not too large) but can differ. This kind
350 // of complication does not occur for positive type IDs because type infos are
Bill Wendlingeb907212009-05-15 01:12:28 +0000351 // output using a fixed width encoding. FilterOffsets[i] holds the byte
352 // offset corresponding to FilterIds[i].
Bill Wendling409914b2009-07-29 21:19:44 +0000353
354 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
Bill Wendlingeb907212009-05-15 01:12:28 +0000355 SmallVector<int, 16> FilterOffsets;
356 FilterOffsets.reserve(FilterIds.size());
357 int Offset = -1;
Bill Wendling409914b2009-07-29 21:19:44 +0000358
359 for (std::vector<unsigned>::const_iterator
360 I = FilterIds.begin(), E = FilterIds.end(); I != E; ++I) {
Bill Wendlingeb907212009-05-15 01:12:28 +0000361 FilterOffsets.push_back(Offset);
Chris Lattneraf76e592009-08-22 20:48:53 +0000362 Offset -= MCAsmInfo::getULEB128Size(*I);
Bill Wendlingeb907212009-05-15 01:12:28 +0000363 }
364
Bill Wendlingeb907212009-05-15 01:12:28 +0000365 FirstActions.reserve(LandingPads.size());
366
367 int FirstAction = 0;
368 unsigned SizeActions = 0;
Bill Wendling5e953dd2009-07-28 23:22:13 +0000369 const LandingPadInfo *PrevLPI = 0;
Bill Wendling409914b2009-07-29 21:19:44 +0000370
Bill Wendling5cff4872009-07-28 23:44:43 +0000371 for (SmallVectorImpl<const LandingPadInfo *>::const_iterator
Bill Wendling5e953dd2009-07-28 23:22:13 +0000372 I = LandingPads.begin(), E = LandingPads.end(); I != E; ++I) {
373 const LandingPadInfo *LPI = *I;
374 const std::vector<int> &TypeIds = LPI->TypeIds;
375 const unsigned NumShared = PrevLPI ? SharedTypeIds(LPI, PrevLPI) : 0;
Bill Wendlingeb907212009-05-15 01:12:28 +0000376 unsigned SizeSiteActions = 0;
377
378 if (NumShared < TypeIds.size()) {
379 unsigned SizeAction = 0;
380 ActionEntry *PrevAction = 0;
381
382 if (NumShared) {
Bill Wendling5e953dd2009-07-28 23:22:13 +0000383 const unsigned SizePrevIds = PrevLPI->TypeIds.size();
Bill Wendlingeb907212009-05-15 01:12:28 +0000384 assert(Actions.size());
385 PrevAction = &Actions.back();
Chris Lattneraf76e592009-08-22 20:48:53 +0000386 SizeAction = MCAsmInfo::getSLEB128Size(PrevAction->NextAction) +
387 MCAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Bill Wendlingeb907212009-05-15 01:12:28 +0000388
389 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
390 SizeAction -=
Chris Lattneraf76e592009-08-22 20:48:53 +0000391 MCAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Bill Wendlingeb907212009-05-15 01:12:28 +0000392 SizeAction += -PrevAction->NextAction;
393 PrevAction = PrevAction->Previous;
394 }
395 }
396
397 // Compute the actions.
Bill Wendling5e953dd2009-07-28 23:22:13 +0000398 for (unsigned J = NumShared, M = TypeIds.size(); J != M; ++J) {
399 int TypeID = TypeIds[J];
400 assert(-1 - TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
Bill Wendlingeb907212009-05-15 01:12:28 +0000401 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
Chris Lattneraf76e592009-08-22 20:48:53 +0000402 unsigned SizeTypeID = MCAsmInfo::getSLEB128Size(ValueForTypeID);
Bill Wendlingeb907212009-05-15 01:12:28 +0000403
404 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
Chris Lattneraf76e592009-08-22 20:48:53 +0000405 SizeAction = SizeTypeID + MCAsmInfo::getSLEB128Size(NextAction);
Bill Wendlingeb907212009-05-15 01:12:28 +0000406 SizeSiteActions += SizeAction;
407
Bill Wendlinga583c552009-08-20 22:02:24 +0000408 ActionEntry Action = { ValueForTypeID, NextAction, PrevAction };
Bill Wendlingeb907212009-05-15 01:12:28 +0000409 Actions.push_back(Action);
Bill Wendlingeb907212009-05-15 01:12:28 +0000410 PrevAction = &Actions.back();
411 }
412
413 // Record the first action of the landing pad site.
414 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
415 } // else identical - re-use previous FirstAction
416
Bill Wendlinga583c552009-08-20 22:02:24 +0000417 // Information used when created the call-site table. The action record
418 // field of the call site record is the offset of the first associated
419 // action record, relative to the start of the actions table. This value is
420 // biased by 1 (1 in dicating the start of the actions table), and 0
421 // indicates that there are no actions.
Bill Wendlingeb907212009-05-15 01:12:28 +0000422 FirstActions.push_back(FirstAction);
423
424 // Compute this sites contribution to size.
425 SizeActions += SizeSiteActions;
Bill Wendling5e953dd2009-07-28 23:22:13 +0000426
427 PrevLPI = LPI;
Bill Wendlingeb907212009-05-15 01:12:28 +0000428 }
429
Bill Wendling5e953dd2009-07-28 23:22:13 +0000430 return SizeActions;
431}
432
Bill Wendlingade025c2009-07-29 00:31:35 +0000433/// ComputeCallSiteTable - Compute the call-site table. The entry for an invoke
Bill Wendlinga583c552009-08-20 22:02:24 +0000434/// has a try-range containing the call, a non-zero landing pad, and an
Bill Wendlingade025c2009-07-29 00:31:35 +0000435/// appropriate action. The entry for an ordinary call has a try-range
436/// containing the call and zero for the landing pad and the action. Calls
437/// marked 'nounwind' have no entry and must not be contained in the try-range
438/// of any entry - they form gaps in the table. Entries must be ordered by
439/// try-range address.
440void DwarfException::
441ComputeCallSiteTable(SmallVectorImpl<CallSiteEntry> &CallSites,
442 const RangeMapType &PadMap,
443 const SmallVectorImpl<const LandingPadInfo *> &LandingPads,
444 const SmallVectorImpl<unsigned> &FirstActions) {
Bill Wendlingeb907212009-05-15 01:12:28 +0000445 // The end label of the previous invoke or nounwind try-range.
446 unsigned LastLabel = 0;
447
448 // Whether there is a potentially throwing instruction (currently this means
449 // an ordinary call) between the end of the previous try-range and now.
450 bool SawPotentiallyThrowing = false;
451
Bill Wendling5cff4872009-07-28 23:44:43 +0000452 // Whether the last CallSite entry was for an invoke.
Bill Wendlingeb907212009-05-15 01:12:28 +0000453 bool PreviousIsInvoke = false;
454
455 // Visit all instructions in order of address.
456 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
457 I != E; ++I) {
458 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
459 MI != E; ++MI) {
460 if (!MI->isLabel()) {
461 SawPotentiallyThrowing |= MI->getDesc().isCall();
462 continue;
463 }
464
465 unsigned BeginLabel = MI->getOperand(0).getImm();
466 assert(BeginLabel && "Invalid label!");
467
468 // End of the previous try-range?
469 if (BeginLabel == LastLabel)
470 SawPotentiallyThrowing = false;
471
472 // Beginning of a new try-range?
473 RangeMapType::iterator L = PadMap.find(BeginLabel);
474 if (L == PadMap.end())
475 // Nope, it was just some random label.
476 continue;
477
Bill Wendlinga583c552009-08-20 22:02:24 +0000478 const PadRange &P = L->second;
Bill Wendlingeb907212009-05-15 01:12:28 +0000479 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
Bill Wendlingeb907212009-05-15 01:12:28 +0000480 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
481 "Inconsistent landing pad map!");
482
Bill Wendlinga583c552009-08-20 22:02:24 +0000483 // For Dwarf exception handling (SjLj handling doesn't use this). If some
484 // instruction between the previous try-range and this one may throw,
485 // create a call-site entry with no landing pad for the region between the
486 // try-ranges.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000487 if (SawPotentiallyThrowing &&
Chris Lattner33adcfb2009-08-22 21:43:10 +0000488 MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) {
Bill Wendlinga583c552009-08-20 22:02:24 +0000489 CallSiteEntry Site = { LastLabel, BeginLabel, 0, 0 };
Bill Wendlingeb907212009-05-15 01:12:28 +0000490 CallSites.push_back(Site);
491 PreviousIsInvoke = false;
492 }
493
494 LastLabel = LandingPad->EndLabels[P.RangeIndex];
495 assert(BeginLabel && LastLabel && "Invalid landing pad!");
496
497 if (LandingPad->LandingPadLabel) {
498 // This try-range is for an invoke.
Bill Wendlinga583c552009-08-20 22:02:24 +0000499 CallSiteEntry Site = {
500 BeginLabel,
501 LastLabel,
502 LandingPad->LandingPadLabel,
503 FirstActions[P.PadIndex]
504 };
Bill Wendlingeb907212009-05-15 01:12:28 +0000505
Jim Grosbach33668c02009-09-01 17:19:13 +0000506 // Try to merge with the previous call-site. SJLJ doesn't do this
507 if (PreviousIsInvoke &&
508 MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) {
Bill Wendlingeb907212009-05-15 01:12:28 +0000509 CallSiteEntry &Prev = CallSites.back();
510 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
511 // Extend the range of the previous entry.
512 Prev.EndLabel = Site.EndLabel;
513 continue;
514 }
515 }
516
517 // Otherwise, create a new call-site.
518 CallSites.push_back(Site);
519 PreviousIsInvoke = true;
520 } else {
521 // Create a gap.
522 PreviousIsInvoke = false;
523 }
524 }
525 }
526
527 // If some instruction between the previous try-range and the end of the
528 // function may throw, create a call-site entry with no landing pad for the
529 // region following the try-range.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000530 if (SawPotentiallyThrowing &&
Chris Lattner33adcfb2009-08-22 21:43:10 +0000531 MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) {
Bill Wendlinga583c552009-08-20 22:02:24 +0000532 CallSiteEntry Site = { LastLabel, 0, 0, 0 };
Bill Wendlingeb907212009-05-15 01:12:28 +0000533 CallSites.push_back(Site);
534 }
Bill Wendlingade025c2009-07-29 00:31:35 +0000535}
536
Bill Wendling0dafca92009-07-29 00:50:05 +0000537/// EmitExceptionTable - Emit landing pads and actions.
538///
539/// The general organization of the table is complex, but the basic concepts are
540/// easy. First there is a header which describes the location and organization
541/// of the three components that follow.
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000542///
Bill Wendling0dafca92009-07-29 00:50:05 +0000543/// 1. The landing pad site information describes the range of code covered by
544/// the try. In our case it's an accumulation of the ranges covered by the
545/// invokes in the try. There is also a reference to the landing pad that
546/// handles the exception once processed. Finally an index into the actions
547/// table.
Bill Wendlinga583c552009-08-20 22:02:24 +0000548/// 2. The action table, in our case, is composed of pairs of type IDs and next
Bill Wendling0dafca92009-07-29 00:50:05 +0000549/// action offset. Starting with the action index from the landing pad
Bill Wendlinga583c552009-08-20 22:02:24 +0000550/// site, each type ID is checked for a match to the current exception. If
Bill Wendling0dafca92009-07-29 00:50:05 +0000551/// it matches then the exception and type id are passed on to the landing
552/// pad. Otherwise the next action is looked up. This chain is terminated
Bill Wendling28275fd2009-09-10 06:50:01 +0000553/// with a next action of zero. If no type id is found then the frame is
Bill Wendling0dafca92009-07-29 00:50:05 +0000554/// unwound and handling continues.
Bill Wendlinga583c552009-08-20 22:02:24 +0000555/// 3. Type ID table contains references to all the C++ typeinfo for all
Bill Wendling28275fd2009-09-10 06:50:01 +0000556/// catches in the function. This tables is reverse indexed base 1.
Bill Wendlingade025c2009-07-29 00:31:35 +0000557void DwarfException::EmitExceptionTable() {
558 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
559 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
560 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
561 if (PadInfos.empty()) return;
562
563 // Sort the landing pads in order of their type ids. This is used to fold
564 // duplicate actions.
565 SmallVector<const LandingPadInfo *, 64> LandingPads;
566 LandingPads.reserve(PadInfos.size());
567
568 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
569 LandingPads.push_back(&PadInfos[i]);
570
571 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
572
573 // Compute the actions table and gather the first action index for each
574 // landing pad site.
575 SmallVector<ActionEntry, 32> Actions;
576 SmallVector<unsigned, 64> FirstActions;
Bill Wendling28275fd2009-09-10 06:50:01 +0000577 unsigned SizeActions = ComputeActionsTable(LandingPads, Actions,
578 FirstActions);
Bill Wendlingade025c2009-07-29 00:31:35 +0000579
580 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
581 // by try-range labels when lowered). Ordinary calls do not, so appropriate
Bill Wendling28275fd2009-09-10 06:50:01 +0000582 // try-ranges for them need be deduced when using DWARF exception handling.
Bill Wendlingade025c2009-07-29 00:31:35 +0000583 RangeMapType PadMap;
584 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
585 const LandingPadInfo *LandingPad = LandingPads[i];
586 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
587 unsigned BeginLabel = LandingPad->BeginLabels[j];
588 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
589 PadRange P = { i, j };
590 PadMap[BeginLabel] = P;
591 }
592 }
593
594 // Compute the call-site table.
595 SmallVector<CallSiteEntry, 64> CallSites;
Jim Grosbach8b818d72009-08-17 16:41:22 +0000596 ComputeCallSiteTable(CallSites, PadMap, LandingPads, FirstActions);
Bill Wendlingeb907212009-05-15 01:12:28 +0000597
598 // Final tallies.
599
600 // Call sites.
Bill Wendling40121bc2009-09-10 00:13:16 +0000601 const unsigned SiteStartSize = SizeOfEncodedValue(dwarf::DW_EH_PE_udata4);
602 const unsigned SiteLengthSize = SizeOfEncodedValue(dwarf::DW_EH_PE_udata4);
603 const unsigned LandingPadSize = SizeOfEncodedValue(dwarf::DW_EH_PE_udata4);
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000604 bool IsSJLJ = MAI->getExceptionHandlingType() == ExceptionHandling::SjLj;
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000605 bool HaveTTData = IsSJLJ ? (!TypeInfos.empty() || !FilterIds.empty()) : true;
Bill Wendling28275fd2009-09-10 06:50:01 +0000606 unsigned SizeSites;
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000607
608 if (IsSJLJ)
Jim Grosbach8b818d72009-08-17 16:41:22 +0000609 SizeSites = 0;
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000610 else
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000611 SizeSites = CallSites.size() *
612 (SiteStartSize + SiteLengthSize + LandingPadSize);
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000613
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000614 for (unsigned i = 0, e = CallSites.size(); i < e; ++i) {
Chris Lattneraf76e592009-08-22 20:48:53 +0000615 SizeSites += MCAsmInfo::getULEB128Size(CallSites[i].Action);
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000616 if (IsSJLJ)
Chris Lattneraf76e592009-08-22 20:48:53 +0000617 SizeSites += MCAsmInfo::getULEB128Size(i);
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000618 }
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000619
Bill Wendlingeb907212009-05-15 01:12:28 +0000620 // Type infos.
Chris Lattnerd5bbb072009-08-02 01:34:32 +0000621 const MCSection *LSDASection = Asm->getObjFileLowering().getLSDASection();
Bill Wendlingfe220282009-09-10 02:07:37 +0000622 unsigned TTypeFormat;
Bill Wendlinga2f64492009-09-10 06:27:16 +0000623 unsigned TypeFormatSize;
Bill Wendlingeb907212009-05-15 01:12:28 +0000624
Bill Wendling43e484f2009-09-10 01:12:47 +0000625 if (!HaveTTData) {
Bill Wendling28275fd2009-09-10 06:50:01 +0000626 // For SjLj exceptions, if there is no TypeInfo, then we just explicitly say
627 // that we're omitting that bit.
Bill Wendlingfe220282009-09-10 02:07:37 +0000628 TTypeFormat = dwarf::DW_EH_PE_omit;
Bill Wendlinga2f64492009-09-10 06:27:16 +0000629 TypeFormatSize = SizeOfEncodedValue(dwarf::DW_EH_PE_absptr);
Chris Lattner81c9a062009-07-31 22:03:47 +0000630 } else {
Chris Lattnerad88bc42009-08-02 03:59:56 +0000631 // Okay, we have actual filters or typeinfos to emit. As such, we need to
632 // pick a type encoding for them. We're about to emit a list of pointers to
633 // typeinfo objects at the end of the LSDA. However, unless we're in static
634 // mode, this reference will require a relocation by the dynamic linker.
Chris Lattner46b754c2009-07-31 22:18:14 +0000635 //
Chris Lattnerad88bc42009-08-02 03:59:56 +0000636 // Because of this, we have a couple of options:
Bill Wendling28275fd2009-09-10 06:50:01 +0000637 //
Chris Lattnerad88bc42009-08-02 03:59:56 +0000638 // 1) If we are in -static mode, we can always use an absolute reference
639 // from the LSDA, because the static linker will resolve it.
Bill Wendling28275fd2009-09-10 06:50:01 +0000640 //
Chris Lattnerad88bc42009-08-02 03:59:56 +0000641 // 2) Otherwise, if the LSDA section is writable, we can output the direct
642 // reference to the typeinfo and allow the dynamic linker to relocate
643 // it. Since it is in a writable section, the dynamic linker won't
644 // have a problem.
Bill Wendling28275fd2009-09-10 06:50:01 +0000645 //
Chris Lattnerad88bc42009-08-02 03:59:56 +0000646 // 3) Finally, if we're in PIC mode and the LDSA section isn't writable,
647 // we need to use some form of indirection. For example, on Darwin,
648 // we can output a statically-relocatable reference to a dyld stub. The
649 // offset to the stub is constant, but the contents are in a section
650 // that is updated by the dynamic linker. This is easy enough, but we
651 // need to tell the personality function of the unwinder to indirect
652 // through the dyld stub.
653 //
Bill Wendling43e484f2009-09-10 01:12:47 +0000654 // FIXME: When (3) is actually implemented, we'll have to emit the stubs
Chris Lattnerad88bc42009-08-02 03:59:56 +0000655 // somewhere. This predicate should be moved to a shared location that is
656 // in target-independent code.
657 //
Bill Wendling43e484f2009-09-10 01:12:47 +0000658 if (LSDASection->getKind().isWriteable() ||
659 Asm->TM.getRelocationModel() == Reloc::Static)
660 TTypeFormat = dwarf::DW_EH_PE_absptr;
661 else
662 TTypeFormat = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
663 dwarf::DW_EH_PE_sdata4;
Bill Wendlinga2f64492009-09-10 06:27:16 +0000664
665 TypeFormatSize = SizeOfEncodedValue(TTypeFormat);
Bill Wendlingfe220282009-09-10 02:07:37 +0000666 }
Bill Wendling43e484f2009-09-10 01:12:47 +0000667
Bill Wendlingfe220282009-09-10 02:07:37 +0000668 // Begin the exception table.
669 Asm->OutStreamer.SwitchSection(LSDASection);
670 Asm->EmitAlignment(2, 0, 0, false);
Bill Wendling43e484f2009-09-10 01:12:47 +0000671
Bill Wendlingfe220282009-09-10 02:07:37 +0000672 O << "GCC_except_table" << SubprogramCount << ":\n";
Bill Wendlinga2f64492009-09-10 06:27:16 +0000673
674 // The type infos need to be aligned. GCC does this by inserting padding just
675 // before the type infos. However, this changes the size of the exception
676 // table, so you need to take this into account when you output the exception
677 // table size. However, the size is output using a variable length encoding.
678 // So by increasing the size by inserting padding, you may increase the number
679 // of bytes used for writing the size. If it increases, say by one byte, then
680 // you now need to output one less byte of padding to get the type infos
681 // aligned. However this decreases the size of the exception table. This
682 // changes the value you have to output for the exception table size. Due to
683 // the variable length encoding, the number of bytes used for writing the
684 // length may decrease. If so, you then have to increase the amount of
685 // padding. And so on. If you look carefully at the GCC code you will see that
686 // it indeed does this in a loop, going on and on until the values stabilize.
687 // We chose another solution: don't output padding inside the table like GCC
688 // does, instead output it before the table.
689 unsigned SizeTypes = TypeInfos.size() * TypeFormatSize;
690 unsigned TyOffset = sizeof(int8_t) + // Call site format
691 MCAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
692 SizeSites + SizeActions + SizeTypes;
693 unsigned TotalSize = sizeof(int8_t) + // LPStart format
694 sizeof(int8_t) + // TType format
695 (HaveTTData ?
696 MCAsmInfo::getULEB128Size(TyOffset) : 0) + // TType base offset
697 TyOffset;
698 unsigned SizeAlign = (4 - TotalSize) & 3;
699
700 for (unsigned i = 0; i != SizeAlign; ++i) {
701 Asm->EmitInt8(0);
702 Asm->EOL("Padding");
703 }
704
Bill Wendlingfe220282009-09-10 02:07:37 +0000705 EmitLabel("exception", SubprogramCount);
706
707 if (IsSJLJ) {
708 SmallString<16> LSDAName;
709 raw_svector_ostream(LSDAName) << MAI->getPrivateGlobalPrefix() <<
710 "_LSDA_" << Asm->getFunctionNumber();
711 O << LSDAName.str() << ":\n";
712 }
713
714 // Emit the header.
715 Asm->EmitInt8(dwarf::DW_EH_PE_omit);
716 Asm->EOL("@LPStart format", dwarf::DW_EH_PE_omit);
717
Bill Wendlingfe220282009-09-10 02:07:37 +0000718 Asm->EmitInt8(TTypeFormat);
719 Asm->EOL("@TType format", TTypeFormat);
720
721 if (HaveTTData) {
Bill Wendlinga2f64492009-09-10 06:27:16 +0000722 Asm->EmitULEB128Bytes(TyOffset);
Bill Wendlinga583c552009-08-20 22:02:24 +0000723 Asm->EOL("@TType base offset");
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000724 }
Bill Wendlingb0d9c3e2009-07-28 22:23:45 +0000725
Bill Wendling28275fd2009-09-10 06:50:01 +0000726 // SjLj Exception handling
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000727 if (IsSJLJ) {
Bill Wendling639217c2009-08-27 03:32:50 +0000728 Asm->EmitInt8(dwarf::DW_EH_PE_udata4);
Bill Wendling0734d352009-09-09 21:26:19 +0000729 Asm->EOL("Call site format", dwarf::DW_EH_PE_udata4);
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000730 Asm->EmitULEB128Bytes(SizeSites);
Bill Wendlinga583c552009-08-20 22:02:24 +0000731 Asm->EOL("Call site table length");
Bill Wendlingeb907212009-05-15 01:12:28 +0000732
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000733 // Emit the landing pad site information.
Jim Grosbach8b818d72009-08-17 16:41:22 +0000734 unsigned idx = 0;
735 for (SmallVectorImpl<CallSiteEntry>::const_iterator
736 I = CallSites.begin(), E = CallSites.end(); I != E; ++I, ++idx) {
737 const CallSiteEntry &S = *I;
Bill Wendlinga583c552009-08-20 22:02:24 +0000738
739 // Offset of the landing pad, counted in 16-byte bundles relative to the
740 // @LPStart address.
Jim Grosbach8b818d72009-08-17 16:41:22 +0000741 Asm->EmitULEB128Bytes(idx);
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000742 Asm->EOL("Landing pad");
Bill Wendlinga583c552009-08-20 22:02:24 +0000743
744 // Offset of the first associated action record, relative to the start of
745 // the action table. This value is biased by 1 (1 indicates the start of
746 // the action table), and 0 indicates that there are no actions.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000747 Asm->EmitULEB128Bytes(S.Action);
748 Asm->EOL("Action");
Bill Wendlingeb907212009-05-15 01:12:28 +0000749 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000750 } else {
751 // DWARF Exception handling
Chris Lattner33adcfb2009-08-22 21:43:10 +0000752 assert(MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf);
Bill Wendlingeb907212009-05-15 01:12:28 +0000753
Bill Wendlinga583c552009-08-20 22:02:24 +0000754 // The call-site table is a list of all call sites that may throw an
755 // exception (including C++ 'throw' statements) in the procedure
756 // fragment. It immediately follows the LSDA header. Each entry indicates,
757 // for a given call, the first corresponding action record and corresponding
758 // landing pad.
759 //
760 // The table begins with the number of bytes, stored as an LEB128
761 // compressed, unsigned integer. The records immediately follow the record
762 // count. They are sorted in increasing call-site address. Each record
763 // indicates:
764 //
765 // * The position of the call-site.
766 // * The position of the landing pad.
767 // * The first action record for that call site.
768 //
769 // A missing entry in the call-site table indicates that a call is not
Bill Wendling28275fd2009-09-10 06:50:01 +0000770 // supposed to throw.
Bill Wendlinga583c552009-08-20 22:02:24 +0000771
772 // Emit the landing pad call site table.
Bill Wendling639217c2009-08-27 03:32:50 +0000773 Asm->EmitInt8(dwarf::DW_EH_PE_udata4);
Bill Wendling0734d352009-09-09 21:26:19 +0000774 Asm->EOL("Call site format", dwarf::DW_EH_PE_udata4);
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000775 Asm->EmitULEB128Bytes(SizeSites);
Bill Wendlinga583c552009-08-20 22:02:24 +0000776 Asm->EOL("Call site table size");
Bill Wendlingeb907212009-05-15 01:12:28 +0000777
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000778 for (SmallVectorImpl<CallSiteEntry>::const_iterator
779 I = CallSites.begin(), E = CallSites.end(); I != E; ++I) {
780 const CallSiteEntry &S = *I;
781 const char *BeginTag;
782 unsigned BeginNumber;
Bill Wendlingeb907212009-05-15 01:12:28 +0000783
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000784 if (!S.BeginLabel) {
785 BeginTag = "eh_func_begin";
786 BeginNumber = SubprogramCount;
787 } else {
788 BeginTag = "label";
789 BeginNumber = S.BeginLabel;
790 }
Bill Wendlingeb907212009-05-15 01:12:28 +0000791
Bill Wendlinga583c552009-08-20 22:02:24 +0000792 // Offset of the call site relative to the previous call site, counted in
793 // number of 16-byte bundles. The first call site is counted relative to
794 // the start of the procedure fragment.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000795 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
Bill Wendlingeb907212009-05-15 01:12:28 +0000796 true, true);
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000797 Asm->EOL("Region start");
Bill Wendlingeb907212009-05-15 01:12:28 +0000798
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000799 if (!S.EndLabel)
800 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
801 true);
802 else
803 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
Bill Wendlingeb907212009-05-15 01:12:28 +0000804
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000805 Asm->EOL("Region length");
806
Bill Wendlinga583c552009-08-20 22:02:24 +0000807 // Offset of the landing pad, counted in 16-byte bundles relative to the
808 // @LPStart address.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000809 if (!S.PadLabel)
810 Asm->EmitInt32(0);
811 else
812 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
813 true, true);
814
815 Asm->EOL("Landing pad");
816
Bill Wendlinga583c552009-08-20 22:02:24 +0000817 // Offset of the first associated action record, relative to the start of
818 // the action table. This value is biased by 1 (1 indicates the start of
819 // the action table), and 0 indicates that there are no actions.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000820 Asm->EmitULEB128Bytes(S.Action);
821 Asm->EOL("Action");
822 }
Bill Wendlingeb907212009-05-15 01:12:28 +0000823 }
824
Bill Wendlinga583c552009-08-20 22:02:24 +0000825 // Emit the Action Table.
Bill Wendling5cff4872009-07-28 23:44:43 +0000826 for (SmallVectorImpl<ActionEntry>::const_iterator
827 I = Actions.begin(), E = Actions.end(); I != E; ++I) {
828 const ActionEntry &Action = *I;
Bill Wendlinga583c552009-08-20 22:02:24 +0000829
830 // Type Filter
831 //
832 // Used by the runtime to match the type of the thrown exception to the
833 // type of the catch clauses or the types in the exception specification.
834
Bill Wendlingeb907212009-05-15 01:12:28 +0000835 Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
836 Asm->EOL("TypeInfo index");
Bill Wendlinga583c552009-08-20 22:02:24 +0000837
838 // Action Record
839 //
840 // Self-relative signed displacement in bytes of the next action record,
841 // or 0 if there is no next action record.
842
Bill Wendlingeb907212009-05-15 01:12:28 +0000843 Asm->EmitSLEB128Bytes(Action.NextAction);
844 Asm->EOL("Next action");
845 }
846
Bill Wendlinga583c552009-08-20 22:02:24 +0000847 // Emit the Catch Clauses. The code for the catch clauses following the same
848 // try is similar to a switch statement. The catch clause action record
849 // informs the runtime about the type of a catch clause and about the
850 // associated switch value.
851 //
852 // Action Record Fields:
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000853 //
Bill Wendlinga583c552009-08-20 22:02:24 +0000854 // * Filter Value
855 // Positive value, starting at 1. Index in the types table of the
856 // __typeinfo for the catch-clause type. 1 is the first word preceding
857 // TTBase, 2 is the second word, and so on. Used by the runtime to check
858 // if the thrown exception type matches the catch-clause type. Back-end
859 // generated switch statements check against this value.
860 //
861 // * Next
862 // Signed offset, in bytes from the start of this field, to the next
863 // chained action record, or zero if none.
864 //
865 // The order of the action records determined by the next field is the order
866 // of the catch clauses as they appear in the source code, and must be kept in
867 // the same order. As a result, changing the order of the catch clause would
868 // change the semantics of the program.
Bill Wendling5cff4872009-07-28 23:44:43 +0000869 for (std::vector<GlobalVariable *>::const_reverse_iterator
870 I = TypeInfos.rbegin(), E = TypeInfos.rend(); I != E; ++I) {
Bill Wendlinga583c552009-08-20 22:02:24 +0000871 const GlobalVariable *GV = *I;
Bill Wendlingeb907212009-05-15 01:12:28 +0000872 PrintRelDirective();
873
874 if (GV) {
Chris Lattner334fd1f2009-09-16 00:08:41 +0000875 O << Asm->Mang->getMangledName(GV);
Bill Wendlingeb907212009-05-15 01:12:28 +0000876 } else {
Bill Wendling049e98d2009-08-31 18:26:48 +0000877 O << "0x0";
Bill Wendlingeb907212009-05-15 01:12:28 +0000878 }
879
880 Asm->EOL("TypeInfo");
881 }
882
Bill Wendlinga583c552009-08-20 22:02:24 +0000883 // Emit the Type Table.
Bill Wendling5cff4872009-07-28 23:44:43 +0000884 for (std::vector<unsigned>::const_iterator
885 I = FilterIds.begin(), E = FilterIds.end(); I < E; ++I) {
886 unsigned TypeID = *I;
Bill Wendlingeb907212009-05-15 01:12:28 +0000887 Asm->EmitULEB128Bytes(TypeID);
888 Asm->EOL("Filter TypeInfo index");
889 }
890
891 Asm->EmitAlignment(2, 0, 0, false);
892}
893
Bill Wendlingeb907212009-05-15 01:12:28 +0000894/// EndModule - Emit all exception information that should come after the
895/// content.
896void DwarfException::EndModule() {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000897 if (MAI->getExceptionHandlingType() != ExceptionHandling::Dwarf)
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000898 return;
Bill Wendlingb4049fe2009-09-09 21:06:24 +0000899
Bill Wendling52783c62009-09-09 23:56:55 +0000900 if (!shouldEmitMovesModule && !shouldEmitTableModule)
901 return;
902
Bill Wendlingeb907212009-05-15 01:12:28 +0000903 if (TimePassesIsEnabled)
904 ExceptionTimer->startTimer();
905
Bill Wendling52783c62009-09-09 23:56:55 +0000906 const std::vector<Function *> Personalities = MMI->getPersonalities();
Bill Wendlingb4049fe2009-09-09 21:06:24 +0000907
Bill Wendling28275fd2009-09-10 06:50:01 +0000908 for (unsigned I = 0, E = Personalities.size(); I < E; ++I)
909 EmitCIE(Personalities[I], I);
Bill Wendlingeb907212009-05-15 01:12:28 +0000910
Bill Wendling52783c62009-09-09 23:56:55 +0000911 for (std::vector<FunctionEHFrameInfo>::iterator
912 I = EHFrames.begin(), E = EHFrames.end(); I != E; ++I)
913 EmitFDE(*I);
Bill Wendlingeb907212009-05-15 01:12:28 +0000914
915 if (TimePassesIsEnabled)
916 ExceptionTimer->stopTimer();
917}
918
Bill Wendling28275fd2009-09-10 06:50:01 +0000919/// BeginFunction - Gather pre-function exception information. Assumes it's
920/// being emitted immediately after the function entry point.
Bill Wendlingeb907212009-05-15 01:12:28 +0000921void DwarfException::BeginFunction(MachineFunction *MF) {
Bill Wendling73c5a612009-09-10 18:28:06 +0000922 if (!MMI || !MAI->doesSupportExceptionHandling()) return;
923
Bill Wendlingeb907212009-05-15 01:12:28 +0000924 if (TimePassesIsEnabled)
925 ExceptionTimer->startTimer();
926
927 this->MF = MF;
928 shouldEmitTable = shouldEmitMoves = false;
929
Bill Wendling73c5a612009-09-10 18:28:06 +0000930 // Map all labels and get rid of any dead landing pads.
931 MMI->TidyLandingPads();
Bill Wendlingeb907212009-05-15 01:12:28 +0000932
Bill Wendling73c5a612009-09-10 18:28:06 +0000933 // If any landing pads survive, we need an EH table.
934 if (!MMI->getLandingPads().empty())
935 shouldEmitTable = true;
Bill Wendlingeb907212009-05-15 01:12:28 +0000936
Bill Wendling73c5a612009-09-10 18:28:06 +0000937 // See if we need frame move info.
938 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
939 shouldEmitMoves = true;
Bill Wendlingeb907212009-05-15 01:12:28 +0000940
Bill Wendling73c5a612009-09-10 18:28:06 +0000941 if (shouldEmitMoves || shouldEmitTable)
942 // Assumes in correct section after the entry point.
943 EmitLabel("eh_func_begin", ++SubprogramCount);
Bill Wendlingeb907212009-05-15 01:12:28 +0000944
945 shouldEmitTableModule |= shouldEmitTable;
946 shouldEmitMovesModule |= shouldEmitMoves;
947
948 if (TimePassesIsEnabled)
949 ExceptionTimer->stopTimer();
950}
951
952/// EndFunction - Gather and emit post-function exception information.
953///
954void DwarfException::EndFunction() {
Bill Wendling7b09a6c2009-09-09 21:08:12 +0000955 if (!shouldEmitMoves && !shouldEmitTable) return;
956
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000957 if (TimePassesIsEnabled)
Bill Wendlingeb907212009-05-15 01:12:28 +0000958 ExceptionTimer->startTimer();
959
Bill Wendling7b09a6c2009-09-09 21:08:12 +0000960 EmitLabel("eh_func_end", SubprogramCount);
961 EmitExceptionTable();
Bill Wendlingeb907212009-05-15 01:12:28 +0000962
Chris Lattner25d812b2009-09-16 00:35:39 +0000963 std::string FunctionEHName =
964 Asm->Mang->getMangledName(MF->getFunction(), ".eh",
965 Asm->MAI->is_EHSymbolPrivate());
966
Bill Wendling7b09a6c2009-09-09 21:08:12 +0000967 // Save EH frame information
Chris Lattner25d812b2009-09-16 00:35:39 +0000968 EHFrames.push_back(FunctionEHFrameInfo(FunctionEHName, SubprogramCount,
Bill Wendling7b09a6c2009-09-09 21:08:12 +0000969 MMI->getPersonalityIndex(),
970 MF->getFrameInfo()->hasCalls(),
971 !MMI->getLandingPads().empty(),
972 MMI->getFrameMoves(),
973 MF->getFunction()));
Bill Wendlingeb907212009-05-15 01:12:28 +0000974
Bill Wendling52783c62009-09-09 23:56:55 +0000975 // Record if this personality index uses a landing pad.
976 UsesLSDA[MMI->getPersonalityIndex()] |= !MMI->getLandingPads().empty();
977
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000978 if (TimePassesIsEnabled)
Bill Wendlingeb907212009-05-15 01:12:28 +0000979 ExceptionTimer->stopTimer();
980}