blob: 4ee8d8c6d58cb6d104a11664587809641294b90b [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"
Chris Lattner8c6ed052009-09-16 01:46:41 +000020#include "llvm/MC/MCAsmInfo.h"
21#include "llvm/MC/MCContext.h"
22#include "llvm/MC/MCExpr.h"
Bill Wendling43e484f2009-09-10 01:12:47 +000023#include "llvm/MC/MCSection.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000024#include "llvm/MC/MCStreamer.h"
Bill Wendlingeb907212009-05-15 01:12:28 +000025#include "llvm/Target/TargetData.h"
26#include "llvm/Target/TargetFrameInfo.h"
Chris Lattnerd5bbb072009-08-02 01:34:32 +000027#include "llvm/Target/TargetLoweringObjectFile.h"
Bill Wendlingeb907212009-05-15 01:12:28 +000028#include "llvm/Target/TargetOptions.h"
Chris Lattnerd5bbb072009-08-02 01:34:32 +000029#include "llvm/Target/TargetRegisterInfo.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000030#include "llvm/Support/Dwarf.h"
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +000031#include "llvm/Support/Mangler.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000032#include "llvm/Support/Timer.h"
33#include "llvm/Support/raw_ostream.h"
Jim Grosbachc40d9f92009-09-01 18:49:12 +000034#include "llvm/ADT/SmallString.h"
Bill Wendlingeb907212009-05-15 01:12:28 +000035#include "llvm/ADT/StringExtras.h"
36using namespace llvm;
37
38static TimerGroup &getDwarfTimerGroup() {
Bill Wendling28275fd2009-09-10 06:50:01 +000039 static TimerGroup DwarfTimerGroup("DWARF Exception");
Bill Wendlingeb907212009-05-15 01:12:28 +000040 return DwarfTimerGroup;
41}
42
Bill Wendlingbc0d23a2009-05-15 01:18:50 +000043DwarfException::DwarfException(raw_ostream &OS, AsmPrinter *A,
Chris Lattneraf76e592009-08-22 20:48:53 +000044 const MCAsmInfo *T)
Bill Wendlingbc0d23a2009-05-15 01:18:50 +000045 : Dwarf(OS, A, T, "eh"), shouldEmitTable(false), shouldEmitMoves(false),
46 shouldEmitTableModule(false), shouldEmitMovesModule(false),
47 ExceptionTimer(0) {
Eric Christopherdbfcdb92009-08-28 22:33:43 +000048 if (TimePassesIsEnabled)
Bill Wendling28275fd2009-09-10 06:50:01 +000049 ExceptionTimer = new Timer("DWARF Exception Writer",
Bill Wendlingbc0d23a2009-05-15 01:18:50 +000050 getDwarfTimerGroup());
51}
52
53DwarfException::~DwarfException() {
54 delete ExceptionTimer;
55}
56
Bill Wendlingbb3e2992009-09-10 00:04:48 +000057/// SizeOfEncodedValue - Return the size of the encoding in bytes.
Bill Wendling52783c62009-09-09 23:56:55 +000058unsigned DwarfException::SizeOfEncodedValue(unsigned Encoding) {
59 if (Encoding == dwarf::DW_EH_PE_omit)
60 return 0;
61
62 switch (Encoding & 0x07) {
63 case dwarf::DW_EH_PE_absptr:
64 return TD->getPointerSize();
65 case dwarf::DW_EH_PE_udata2:
66 return 2;
67 case dwarf::DW_EH_PE_udata4:
68 return 4;
69 case dwarf::DW_EH_PE_udata8:
70 return 8;
71 }
72
Bill Wendling28275fd2009-09-10 06:50:01 +000073 assert(0 && "Invalid encoded value.");
Bill Wendling52783c62009-09-09 23:56:55 +000074 return 0;
75}
76
Bill Wendling7ccda0f2009-08-25 08:08:33 +000077/// EmitCIE - Emit a Common Information Entry (CIE). This holds information that
78/// is shared among many Frame Description Entries. There is at least one CIE
79/// in every non-empty .debug_frame section.
Chris Lattner8c6ed052009-09-16 01:46:41 +000080void DwarfException::EmitCIE(const Function *PersonalityFn, unsigned Index) {
Bill Wendlingeb907212009-05-15 01:12:28 +000081 // Size and sign of stack growth.
82 int stackGrowth =
83 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
84 TargetFrameInfo::StackGrowsUp ?
85 TD->getPointerSize() : -TD->getPointerSize();
86
Chris Lattner8c6ed052009-09-16 01:46:41 +000087 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
88
Bill Wendlingeb907212009-05-15 01:12:28 +000089 // Begin eh frame section.
Chris Lattner8c6ed052009-09-16 01:46:41 +000090 Asm->OutStreamer.SwitchSection(TLOF.getEHFrameSection());
Bill Wendlingeb907212009-05-15 01:12:28 +000091
Chris Lattner33adcfb2009-08-22 21:43:10 +000092 if (MAI->is_EHSymbolPrivate())
93 O << MAI->getPrivateGlobalPrefix();
Bill Wendlingeb907212009-05-15 01:12:28 +000094 O << "EH_frame" << Index << ":\n";
Chris Lattner8c6ed052009-09-16 01:46:41 +000095
Bill Wendlingeb907212009-05-15 01:12:28 +000096 EmitLabel("section_eh_frame", Index);
97
98 // Define base labels.
99 EmitLabel("eh_frame_common", Index);
100
101 // Define the eh frame length.
102 EmitDifference("eh_frame_common_end", Index,
103 "eh_frame_common_begin", Index, true);
104 Asm->EOL("Length of Common Information Entry");
105
106 // EH frame header.
107 EmitLabel("eh_frame_common_begin", Index);
108 Asm->EmitInt32((int)0);
109 Asm->EOL("CIE Identifier Tag");
110 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
111 Asm->EOL("CIE Version");
112
113 // The personality presence indicates that language specific information will
Chris Lattner8c6ed052009-09-16 01:46:41 +0000114 // show up in the eh frame. Find out how we are supposed to lower the
115 // personality function reference:
116 const MCExpr *PersonalityRef = 0;
117 bool IsPersonalityIndirect = false, IsPersonalityPCRel = false;
118 if (PersonalityFn) {
119 // FIXME: HANDLE STATIC CODEGEN MODEL HERE.
120
121 // In non-static mode, ask the object file how to represent this reference.
122 PersonalityRef =
123 TLOF.getSymbolForDwarfGlobalReference(PersonalityFn, Asm->Mang,
124 IsPersonalityIndirect,
125 IsPersonalityPCRel);
126 }
127
Bill Wendling52783c62009-09-09 23:56:55 +0000128 unsigned PerEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
Chris Lattner8c6ed052009-09-16 01:46:41 +0000129 if (IsPersonalityIndirect)
Bill Wendling52783c62009-09-09 23:56:55 +0000130 PerEncoding |= dwarf::DW_EH_PE_indirect;
131 unsigned LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
132 unsigned FDEEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
133
134 char Augmentation[5] = { 0 };
135 unsigned AugmentationSize = 0;
136 char *APtr = Augmentation + 1;
137
Chris Lattner8c6ed052009-09-16 01:46:41 +0000138 if (PersonalityRef) {
Bill Wendling52783c62009-09-09 23:56:55 +0000139 // There is a personality function.
140 *APtr++ = 'P';
141 AugmentationSize += 1 + SizeOfEncodedValue(PerEncoding);
142 }
143
144 if (UsesLSDA[Index]) {
145 // An LSDA pointer is in the FDE augmentation.
146 *APtr++ = 'L';
147 ++AugmentationSize;
148 }
149
150 if (FDEEncoding != dwarf::DW_EH_PE_absptr) {
151 // A non-default pointer encoding for the FDE.
152 *APtr++ = 'R';
153 ++AugmentationSize;
154 }
155
156 if (APtr != Augmentation + 1)
157 Augmentation[0] = 'z';
158
159 Asm->EmitString(Augmentation);
Bill Wendlingeb907212009-05-15 01:12:28 +0000160 Asm->EOL("CIE Augmentation");
161
162 // Round out reader.
163 Asm->EmitULEB128Bytes(1);
164 Asm->EOL("CIE Code Alignment Factor");
165 Asm->EmitSLEB128Bytes(stackGrowth);
166 Asm->EOL("CIE Data Alignment Factor");
167 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
168 Asm->EOL("CIE Return Address Column");
169
Bill Wendling52783c62009-09-09 23:56:55 +0000170 Asm->EmitULEB128Bytes(AugmentationSize);
171 Asm->EOL("Augmentation Size");
172
173 Asm->EmitInt8(PerEncoding);
174 Asm->EOL("Personality", PerEncoding);
175
Bill Wendling4bda11f2009-08-25 02:32:05 +0000176 // If there is a personality, we need to indicate the function's location.
Chris Lattner8c6ed052009-09-16 01:46:41 +0000177 if (PersonalityRef) {
178 // If the reference to the personality function symbol is not already
179 // pc-relative, then we need to subtract our current address from it. Do
180 // this by emitting a label and subtracting it from the expression we
181 // already have. This is equivalent to emitting "foo - .", but we have to
182 // emit the label for "." directly.
183 if (!IsPersonalityPCRel) {
184 SmallString<64> Name;
185 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix()
186 << "personalityref_addr" << Asm->getFunctionNumber() << "_" << Index;
187 MCSymbol *DotSym = Asm->OutContext.GetOrCreateSymbol(Name.str());
188 Asm->OutStreamer.EmitLabel(DotSym);
189
190 PersonalityRef =
191 MCBinaryExpr::CreateSub(PersonalityRef,
192 MCSymbolRefExpr::Create(DotSym,Asm->OutContext),
193 Asm->OutContext);
194 }
195
Chris Lattnerf60d3eb2009-09-15 22:58:35 +0000196 O << MAI->getData32bitsDirective();
Chris Lattner8c6ed052009-09-16 01:46:41 +0000197 PersonalityRef->print(O, MAI);
Bill Wendlingeb907212009-05-15 01:12:28 +0000198 Asm->EOL("Personality");
199
Bill Wendling52783c62009-09-09 23:56:55 +0000200 Asm->EmitInt8(LSDAEncoding);
201 Asm->EOL("LSDA Encoding", LSDAEncoding);
Bill Wendlingeb907212009-05-15 01:12:28 +0000202
Bill Wendling52783c62009-09-09 23:56:55 +0000203 Asm->EmitInt8(FDEEncoding);
204 Asm->EOL("FDE Encoding", FDEEncoding);
Bill Wendlingeb907212009-05-15 01:12:28 +0000205 }
206
207 // Indicate locations of general callee saved registers in frame.
208 std::vector<MachineMove> Moves;
209 RI->getInitialFrameState(Moves);
210 EmitFrameMoves(NULL, 0, Moves, true);
211
212 // On Darwin the linker honors the alignment of eh_frame, which means it must
213 // be 8-byte on 64-bit targets to match what gcc does. Otherwise you get
214 // holes which confuse readers of eh_frame.
Chris Lattner8c6ed052009-09-16 01:46:41 +0000215 Asm->EmitAlignment(TD->getPointerSize() == 4 ? 2 : 3, 0, 0, false);
Bill Wendlingeb907212009-05-15 01:12:28 +0000216 EmitLabel("eh_frame_common_end", Index);
217
218 Asm->EOL();
219}
220
Bill Wendling7ccda0f2009-08-25 08:08:33 +0000221/// EmitFDE - Emit the Frame Description Entry (FDE) for the function.
222void DwarfException::EmitFDE(const FunctionEHFrameInfo &EHFrameInfo) {
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000223 assert(!EHFrameInfo.function->hasAvailableExternallyLinkage() &&
Bill Wendlingeb907212009-05-15 01:12:28 +0000224 "Should not emit 'available externally' functions at all");
225
Chris Lattner3e0f60b2009-07-17 21:00:50 +0000226 const Function *TheFunc = EHFrameInfo.function;
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000227
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000228 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getEHFrameSection());
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000229
Bill Wendlingeb907212009-05-15 01:12:28 +0000230 // Externally visible entry into the functions eh frame info. If the
231 // corresponding function is static, this should not be externally visible.
Chris Lattner3e0f60b2009-07-17 21:00:50 +0000232 if (!TheFunc->hasLocalLinkage())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000233 if (const char *GlobalEHDirective = MAI->getGlobalEHDirective())
Bill Wendlingeb907212009-05-15 01:12:28 +0000234 O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
Bill Wendlingeb907212009-05-15 01:12:28 +0000235
236 // If corresponding function is weak definition, this should be too.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000237 if (TheFunc->isWeakForLinker() && MAI->getWeakDefDirective())
238 O << MAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
Bill Wendlingeb907212009-05-15 01:12:28 +0000239
240 // If there are no calls then you can't unwind. This may mean we can omit the
241 // EH Frame, but some environments do not handle weak absolute symbols. If
242 // UnwindTablesMandatory is set we cannot do this optimization; the unwind
243 // info is to be available for non-EH uses.
Chris Lattner3e0f60b2009-07-17 21:00:50 +0000244 if (!EHFrameInfo.hasCalls && !UnwindTablesMandatory &&
245 (!TheFunc->isWeakForLinker() ||
Chris Lattner33adcfb2009-08-22 21:43:10 +0000246 !MAI->getWeakDefDirective() ||
247 MAI->getSupportsWeakOmittedEHFrame())) {
Bill Wendlingeb907212009-05-15 01:12:28 +0000248 O << EHFrameInfo.FnName << " = 0\n";
249 // This name has no connection to the function, so it might get
250 // dead-stripped when the function is not, erroneously. Prohibit
251 // dead-stripping unconditionally.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000252 if (const char *UsedDirective = MAI->getUsedDirective())
Bill Wendlingeb907212009-05-15 01:12:28 +0000253 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
254 } else {
255 O << EHFrameInfo.FnName << ":\n";
256
257 // EH frame header.
258 EmitDifference("eh_frame_end", EHFrameInfo.Number,
259 "eh_frame_begin", EHFrameInfo.Number, true);
260 Asm->EOL("Length of Frame Information Entry");
261
262 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
263
Chris Lattnera4ff5e42009-07-17 20:53:51 +0000264 EmitSectionOffset("eh_frame_begin", "eh_frame_common",
265 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
266 true, true, false);
Bill Wendlingeb907212009-05-15 01:12:28 +0000267
268 Asm->EOL("FDE CIE offset");
269
Duncan Sandsc69d74a2009-08-31 16:45:16 +0000270 EmitReference("eh_func_begin", EHFrameInfo.Number, true, true);
Bill Wendlingeb907212009-05-15 01:12:28 +0000271 Asm->EOL("FDE initial location");
272 EmitDifference("eh_func_end", EHFrameInfo.Number,
Duncan Sandsc69d74a2009-08-31 16:45:16 +0000273 "eh_func_begin", EHFrameInfo.Number, true);
Bill Wendlingeb907212009-05-15 01:12:28 +0000274 Asm->EOL("FDE address range");
275
276 // If there is a personality and landing pads then point to the language
277 // specific data area in the exception table.
Eric Christopherd44fff72009-08-26 21:30:49 +0000278 if (MMI->getPersonalities()[0] != NULL) {
Duncan Sandsc69d74a2009-08-31 16:45:16 +0000279 bool is4Byte = TD->getPointerSize() == sizeof(int32_t);
280
Eric Christopher6fefceb2009-08-29 01:12:46 +0000281 Asm->EmitULEB128Bytes(is4Byte ? 4 : 8);
Bill Wendlingeb907212009-05-15 01:12:28 +0000282 Asm->EOL("Augmentation size");
283
Duncan Sandsc69d74a2009-08-31 16:45:16 +0000284 if (EHFrameInfo.hasLandingPads)
Eric Christopher6fefceb2009-08-29 01:12:46 +0000285 EmitReference("exception", EHFrameInfo.Number, true, false);
Duncan Sandsc69d74a2009-08-31 16:45:16 +0000286 else {
Bill Wendling52783c62009-09-09 23:56:55 +0000287 if (is4Byte)
288 Asm->EmitInt32((int)0);
289 else
290 Asm->EmitInt64((int)0);
Eric Christopher6fefceb2009-08-29 01:12:46 +0000291 }
Bill Wendlingeb907212009-05-15 01:12:28 +0000292 Asm->EOL("Language Specific Data Area");
293 } else {
294 Asm->EmitULEB128Bytes(0);
295 Asm->EOL("Augmentation size");
296 }
297
298 // Indicate locations of function specific callee saved registers in frame.
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000299 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves,
Bill Wendlingeb907212009-05-15 01:12:28 +0000300 true);
301
302 // On Darwin the linker honors the alignment of eh_frame, which means it
303 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise you
304 // get holes which confuse readers of eh_frame.
305 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
306 0, 0, false);
307 EmitLabel("eh_frame_end", EHFrameInfo.Number);
308
309 // If the function is marked used, this table should be also. We cannot
310 // make the mark unconditional in this case, since retaining the table also
311 // retains the function in this case, and there is code around that depends
312 // on unused functions (calling undefined externals) being dead-stripped to
313 // link correctly. Yes, there really is.
Chris Lattner401e10c2009-07-20 06:14:25 +0000314 if (MMI->isUsedFunction(EHFrameInfo.function))
Chris Lattner33adcfb2009-08-22 21:43:10 +0000315 if (const char *UsedDirective = MAI->getUsedDirective())
Bill Wendlingeb907212009-05-15 01:12:28 +0000316 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
317 }
Bill Wendling4bda11f2009-08-25 02:32:05 +0000318
319 Asm->EOL();
Bill Wendlingeb907212009-05-15 01:12:28 +0000320}
321
Bill Wendlingeb907212009-05-15 01:12:28 +0000322/// SharedTypeIds - How many leading type ids two landing pads have in common.
323unsigned DwarfException::SharedTypeIds(const LandingPadInfo *L,
324 const LandingPadInfo *R) {
325 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
326 unsigned LSize = LIds.size(), RSize = RIds.size();
327 unsigned MinSize = LSize < RSize ? LSize : RSize;
328 unsigned Count = 0;
329
330 for (; Count != MinSize; ++Count)
331 if (LIds[Count] != RIds[Count])
332 return Count;
333
334 return Count;
335}
336
337/// PadLT - Order landing pads lexicographically by type id.
338bool DwarfException::PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
339 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
340 unsigned LSize = LIds.size(), RSize = RIds.size();
341 unsigned MinSize = LSize < RSize ? LSize : RSize;
342
343 for (unsigned i = 0; i != MinSize; ++i)
344 if (LIds[i] != RIds[i])
345 return LIds[i] < RIds[i];
346
347 return LSize < RSize;
348}
349
Bill Wendlingd4609622009-07-28 23:23:00 +0000350/// ComputeActionsTable - Compute the actions table and gather the first action
351/// index for each landing pad site.
Bill Wendlingade025c2009-07-29 00:31:35 +0000352unsigned DwarfException::
353ComputeActionsTable(const SmallVectorImpl<const LandingPadInfo*> &LandingPads,
354 SmallVectorImpl<ActionEntry> &Actions,
355 SmallVectorImpl<unsigned> &FirstActions) {
Bill Wendlinga583c552009-08-20 22:02:24 +0000356
357 // The action table follows the call-site table in the LSDA. The individual
358 // records are of two types:
359 //
360 // * Catch clause
361 // * Exception specification
362 //
363 // The two record kinds have the same format, with only small differences.
364 // They are distinguished by the "switch value" field: Catch clauses
365 // (TypeInfos) have strictly positive switch values, and exception
366 // specifications (FilterIds) have strictly negative switch values. Value 0
367 // indicates a catch-all clause.
368 //
Bill Wendling5e953dd2009-07-28 23:22:13 +0000369 // Negative type IDs index into FilterIds. Positive type IDs index into
370 // TypeInfos. The value written for a positive type ID is just the type ID
371 // itself. For a negative type ID, however, the value written is the
Bill Wendlingeb907212009-05-15 01:12:28 +0000372 // (negative) byte offset of the corresponding FilterIds entry. The byte
Bill Wendling5e953dd2009-07-28 23:22:13 +0000373 // offset is usually equal to the type ID (because the FilterIds entries are
374 // written using a variable width encoding, which outputs one byte per entry
375 // as long as the value written is not too large) but can differ. This kind
376 // of complication does not occur for positive type IDs because type infos are
Bill Wendlingeb907212009-05-15 01:12:28 +0000377 // output using a fixed width encoding. FilterOffsets[i] holds the byte
378 // offset corresponding to FilterIds[i].
Bill Wendling409914b2009-07-29 21:19:44 +0000379
380 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
Bill Wendlingeb907212009-05-15 01:12:28 +0000381 SmallVector<int, 16> FilterOffsets;
382 FilterOffsets.reserve(FilterIds.size());
383 int Offset = -1;
Bill Wendling409914b2009-07-29 21:19:44 +0000384
385 for (std::vector<unsigned>::const_iterator
386 I = FilterIds.begin(), E = FilterIds.end(); I != E; ++I) {
Bill Wendlingeb907212009-05-15 01:12:28 +0000387 FilterOffsets.push_back(Offset);
Chris Lattneraf76e592009-08-22 20:48:53 +0000388 Offset -= MCAsmInfo::getULEB128Size(*I);
Bill Wendlingeb907212009-05-15 01:12:28 +0000389 }
390
Bill Wendlingeb907212009-05-15 01:12:28 +0000391 FirstActions.reserve(LandingPads.size());
392
393 int FirstAction = 0;
394 unsigned SizeActions = 0;
Bill Wendling5e953dd2009-07-28 23:22:13 +0000395 const LandingPadInfo *PrevLPI = 0;
Bill Wendling409914b2009-07-29 21:19:44 +0000396
Bill Wendling5cff4872009-07-28 23:44:43 +0000397 for (SmallVectorImpl<const LandingPadInfo *>::const_iterator
Bill Wendling5e953dd2009-07-28 23:22:13 +0000398 I = LandingPads.begin(), E = LandingPads.end(); I != E; ++I) {
399 const LandingPadInfo *LPI = *I;
400 const std::vector<int> &TypeIds = LPI->TypeIds;
401 const unsigned NumShared = PrevLPI ? SharedTypeIds(LPI, PrevLPI) : 0;
Bill Wendlingeb907212009-05-15 01:12:28 +0000402 unsigned SizeSiteActions = 0;
403
404 if (NumShared < TypeIds.size()) {
405 unsigned SizeAction = 0;
406 ActionEntry *PrevAction = 0;
407
408 if (NumShared) {
Bill Wendling5e953dd2009-07-28 23:22:13 +0000409 const unsigned SizePrevIds = PrevLPI->TypeIds.size();
Bill Wendlingeb907212009-05-15 01:12:28 +0000410 assert(Actions.size());
411 PrevAction = &Actions.back();
Chris Lattneraf76e592009-08-22 20:48:53 +0000412 SizeAction = MCAsmInfo::getSLEB128Size(PrevAction->NextAction) +
413 MCAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Bill Wendlingeb907212009-05-15 01:12:28 +0000414
415 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
416 SizeAction -=
Chris Lattneraf76e592009-08-22 20:48:53 +0000417 MCAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Bill Wendlingeb907212009-05-15 01:12:28 +0000418 SizeAction += -PrevAction->NextAction;
419 PrevAction = PrevAction->Previous;
420 }
421 }
422
423 // Compute the actions.
Bill Wendling5e953dd2009-07-28 23:22:13 +0000424 for (unsigned J = NumShared, M = TypeIds.size(); J != M; ++J) {
425 int TypeID = TypeIds[J];
426 assert(-1 - TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
Bill Wendlingeb907212009-05-15 01:12:28 +0000427 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
Chris Lattneraf76e592009-08-22 20:48:53 +0000428 unsigned SizeTypeID = MCAsmInfo::getSLEB128Size(ValueForTypeID);
Bill Wendlingeb907212009-05-15 01:12:28 +0000429
430 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
Chris Lattneraf76e592009-08-22 20:48:53 +0000431 SizeAction = SizeTypeID + MCAsmInfo::getSLEB128Size(NextAction);
Bill Wendlingeb907212009-05-15 01:12:28 +0000432 SizeSiteActions += SizeAction;
433
Bill Wendlinga583c552009-08-20 22:02:24 +0000434 ActionEntry Action = { ValueForTypeID, NextAction, PrevAction };
Bill Wendlingeb907212009-05-15 01:12:28 +0000435 Actions.push_back(Action);
Bill Wendlingeb907212009-05-15 01:12:28 +0000436 PrevAction = &Actions.back();
437 }
438
439 // Record the first action of the landing pad site.
440 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
441 } // else identical - re-use previous FirstAction
442
Bill Wendlinga583c552009-08-20 22:02:24 +0000443 // Information used when created the call-site table. The action record
444 // field of the call site record is the offset of the first associated
445 // action record, relative to the start of the actions table. This value is
446 // biased by 1 (1 in dicating the start of the actions table), and 0
447 // indicates that there are no actions.
Bill Wendlingeb907212009-05-15 01:12:28 +0000448 FirstActions.push_back(FirstAction);
449
450 // Compute this sites contribution to size.
451 SizeActions += SizeSiteActions;
Bill Wendling5e953dd2009-07-28 23:22:13 +0000452
453 PrevLPI = LPI;
Bill Wendlingeb907212009-05-15 01:12:28 +0000454 }
455
Bill Wendling5e953dd2009-07-28 23:22:13 +0000456 return SizeActions;
457}
458
Bill Wendlingade025c2009-07-29 00:31:35 +0000459/// ComputeCallSiteTable - Compute the call-site table. The entry for an invoke
Bill Wendlinga583c552009-08-20 22:02:24 +0000460/// has a try-range containing the call, a non-zero landing pad, and an
Bill Wendlingade025c2009-07-29 00:31:35 +0000461/// appropriate action. The entry for an ordinary call has a try-range
462/// containing the call and zero for the landing pad and the action. Calls
463/// marked 'nounwind' have no entry and must not be contained in the try-range
464/// of any entry - they form gaps in the table. Entries must be ordered by
465/// try-range address.
466void DwarfException::
467ComputeCallSiteTable(SmallVectorImpl<CallSiteEntry> &CallSites,
468 const RangeMapType &PadMap,
469 const SmallVectorImpl<const LandingPadInfo *> &LandingPads,
470 const SmallVectorImpl<unsigned> &FirstActions) {
Bill Wendlingeb907212009-05-15 01:12:28 +0000471 // The end label of the previous invoke or nounwind try-range.
472 unsigned LastLabel = 0;
473
474 // Whether there is a potentially throwing instruction (currently this means
475 // an ordinary call) between the end of the previous try-range and now.
476 bool SawPotentiallyThrowing = false;
477
Bill Wendling5cff4872009-07-28 23:44:43 +0000478 // Whether the last CallSite entry was for an invoke.
Bill Wendlingeb907212009-05-15 01:12:28 +0000479 bool PreviousIsInvoke = false;
480
481 // Visit all instructions in order of address.
482 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
483 I != E; ++I) {
484 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
485 MI != E; ++MI) {
486 if (!MI->isLabel()) {
487 SawPotentiallyThrowing |= MI->getDesc().isCall();
488 continue;
489 }
490
491 unsigned BeginLabel = MI->getOperand(0).getImm();
492 assert(BeginLabel && "Invalid label!");
493
494 // End of the previous try-range?
495 if (BeginLabel == LastLabel)
496 SawPotentiallyThrowing = false;
497
498 // Beginning of a new try-range?
499 RangeMapType::iterator L = PadMap.find(BeginLabel);
500 if (L == PadMap.end())
501 // Nope, it was just some random label.
502 continue;
503
Bill Wendlinga583c552009-08-20 22:02:24 +0000504 const PadRange &P = L->second;
Bill Wendlingeb907212009-05-15 01:12:28 +0000505 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
Bill Wendlingeb907212009-05-15 01:12:28 +0000506 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
507 "Inconsistent landing pad map!");
508
Bill Wendlinga583c552009-08-20 22:02:24 +0000509 // For Dwarf exception handling (SjLj handling doesn't use this). If some
510 // instruction between the previous try-range and this one may throw,
511 // create a call-site entry with no landing pad for the region between the
512 // try-ranges.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000513 if (SawPotentiallyThrowing &&
Chris Lattner33adcfb2009-08-22 21:43:10 +0000514 MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) {
Bill Wendlinga583c552009-08-20 22:02:24 +0000515 CallSiteEntry Site = { LastLabel, BeginLabel, 0, 0 };
Bill Wendlingeb907212009-05-15 01:12:28 +0000516 CallSites.push_back(Site);
517 PreviousIsInvoke = false;
518 }
519
520 LastLabel = LandingPad->EndLabels[P.RangeIndex];
521 assert(BeginLabel && LastLabel && "Invalid landing pad!");
522
523 if (LandingPad->LandingPadLabel) {
524 // This try-range is for an invoke.
Bill Wendlinga583c552009-08-20 22:02:24 +0000525 CallSiteEntry Site = {
526 BeginLabel,
527 LastLabel,
528 LandingPad->LandingPadLabel,
529 FirstActions[P.PadIndex]
530 };
Bill Wendlingeb907212009-05-15 01:12:28 +0000531
Jim Grosbach33668c02009-09-01 17:19:13 +0000532 // Try to merge with the previous call-site. SJLJ doesn't do this
533 if (PreviousIsInvoke &&
534 MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) {
Bill Wendlingeb907212009-05-15 01:12:28 +0000535 CallSiteEntry &Prev = CallSites.back();
536 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
537 // Extend the range of the previous entry.
538 Prev.EndLabel = Site.EndLabel;
539 continue;
540 }
541 }
542
543 // Otherwise, create a new call-site.
544 CallSites.push_back(Site);
545 PreviousIsInvoke = true;
546 } else {
547 // Create a gap.
548 PreviousIsInvoke = false;
549 }
550 }
551 }
552
553 // If some instruction between the previous try-range and the end of the
554 // function may throw, create a call-site entry with no landing pad for the
555 // region following the try-range.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000556 if (SawPotentiallyThrowing &&
Chris Lattner33adcfb2009-08-22 21:43:10 +0000557 MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) {
Bill Wendlinga583c552009-08-20 22:02:24 +0000558 CallSiteEntry Site = { LastLabel, 0, 0, 0 };
Bill Wendlingeb907212009-05-15 01:12:28 +0000559 CallSites.push_back(Site);
560 }
Bill Wendlingade025c2009-07-29 00:31:35 +0000561}
562
Bill Wendling0dafca92009-07-29 00:50:05 +0000563/// EmitExceptionTable - Emit landing pads and actions.
564///
565/// The general organization of the table is complex, but the basic concepts are
566/// easy. First there is a header which describes the location and organization
567/// of the three components that follow.
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000568///
Bill Wendling0dafca92009-07-29 00:50:05 +0000569/// 1. The landing pad site information describes the range of code covered by
570/// the try. In our case it's an accumulation of the ranges covered by the
571/// invokes in the try. There is also a reference to the landing pad that
572/// handles the exception once processed. Finally an index into the actions
573/// table.
Bill Wendlinga583c552009-08-20 22:02:24 +0000574/// 2. The action table, in our case, is composed of pairs of type IDs and next
Bill Wendling0dafca92009-07-29 00:50:05 +0000575/// action offset. Starting with the action index from the landing pad
Bill Wendlinga583c552009-08-20 22:02:24 +0000576/// site, each type ID is checked for a match to the current exception. If
Bill Wendling0dafca92009-07-29 00:50:05 +0000577/// it matches then the exception and type id are passed on to the landing
578/// pad. Otherwise the next action is looked up. This chain is terminated
Bill Wendling28275fd2009-09-10 06:50:01 +0000579/// with a next action of zero. If no type id is found then the frame is
Bill Wendling0dafca92009-07-29 00:50:05 +0000580/// unwound and handling continues.
Bill Wendlinga583c552009-08-20 22:02:24 +0000581/// 3. Type ID table contains references to all the C++ typeinfo for all
Bill Wendling28275fd2009-09-10 06:50:01 +0000582/// catches in the function. This tables is reverse indexed base 1.
Bill Wendlingade025c2009-07-29 00:31:35 +0000583void DwarfException::EmitExceptionTable() {
584 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
585 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
586 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
587 if (PadInfos.empty()) return;
588
589 // Sort the landing pads in order of their type ids. This is used to fold
590 // duplicate actions.
591 SmallVector<const LandingPadInfo *, 64> LandingPads;
592 LandingPads.reserve(PadInfos.size());
593
594 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
595 LandingPads.push_back(&PadInfos[i]);
596
597 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
598
599 // Compute the actions table and gather the first action index for each
600 // landing pad site.
601 SmallVector<ActionEntry, 32> Actions;
602 SmallVector<unsigned, 64> FirstActions;
Bill Wendling28275fd2009-09-10 06:50:01 +0000603 unsigned SizeActions = ComputeActionsTable(LandingPads, Actions,
604 FirstActions);
Bill Wendlingade025c2009-07-29 00:31:35 +0000605
606 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
607 // by try-range labels when lowered). Ordinary calls do not, so appropriate
Bill Wendling28275fd2009-09-10 06:50:01 +0000608 // try-ranges for them need be deduced when using DWARF exception handling.
Bill Wendlingade025c2009-07-29 00:31:35 +0000609 RangeMapType PadMap;
610 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
611 const LandingPadInfo *LandingPad = LandingPads[i];
612 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
613 unsigned BeginLabel = LandingPad->BeginLabels[j];
614 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
615 PadRange P = { i, j };
616 PadMap[BeginLabel] = P;
617 }
618 }
619
620 // Compute the call-site table.
621 SmallVector<CallSiteEntry, 64> CallSites;
Jim Grosbach8b818d72009-08-17 16:41:22 +0000622 ComputeCallSiteTable(CallSites, PadMap, LandingPads, FirstActions);
Bill Wendlingeb907212009-05-15 01:12:28 +0000623
624 // Final tallies.
625
626 // Call sites.
Bill Wendling40121bc2009-09-10 00:13:16 +0000627 const unsigned SiteStartSize = SizeOfEncodedValue(dwarf::DW_EH_PE_udata4);
628 const unsigned SiteLengthSize = SizeOfEncodedValue(dwarf::DW_EH_PE_udata4);
629 const unsigned LandingPadSize = SizeOfEncodedValue(dwarf::DW_EH_PE_udata4);
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000630 bool IsSJLJ = MAI->getExceptionHandlingType() == ExceptionHandling::SjLj;
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000631 bool HaveTTData = IsSJLJ ? (!TypeInfos.empty() || !FilterIds.empty()) : true;
Bill Wendling28275fd2009-09-10 06:50:01 +0000632 unsigned SizeSites;
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000633
634 if (IsSJLJ)
Jim Grosbach8b818d72009-08-17 16:41:22 +0000635 SizeSites = 0;
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000636 else
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000637 SizeSites = CallSites.size() *
638 (SiteStartSize + SiteLengthSize + LandingPadSize);
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000639
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000640 for (unsigned i = 0, e = CallSites.size(); i < e; ++i) {
Chris Lattneraf76e592009-08-22 20:48:53 +0000641 SizeSites += MCAsmInfo::getULEB128Size(CallSites[i].Action);
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000642 if (IsSJLJ)
Chris Lattneraf76e592009-08-22 20:48:53 +0000643 SizeSites += MCAsmInfo::getULEB128Size(i);
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000644 }
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000645
Bill Wendlingeb907212009-05-15 01:12:28 +0000646 // Type infos.
Chris Lattnerd5bbb072009-08-02 01:34:32 +0000647 const MCSection *LSDASection = Asm->getObjFileLowering().getLSDASection();
Bill Wendlingfe220282009-09-10 02:07:37 +0000648 unsigned TTypeFormat;
Bill Wendlinga2f64492009-09-10 06:27:16 +0000649 unsigned TypeFormatSize;
Bill Wendlingeb907212009-05-15 01:12:28 +0000650
Bill Wendling43e484f2009-09-10 01:12:47 +0000651 if (!HaveTTData) {
Bill Wendling28275fd2009-09-10 06:50:01 +0000652 // For SjLj exceptions, if there is no TypeInfo, then we just explicitly say
653 // that we're omitting that bit.
Bill Wendlingfe220282009-09-10 02:07:37 +0000654 TTypeFormat = dwarf::DW_EH_PE_omit;
Bill Wendlinga2f64492009-09-10 06:27:16 +0000655 TypeFormatSize = SizeOfEncodedValue(dwarf::DW_EH_PE_absptr);
Chris Lattner81c9a062009-07-31 22:03:47 +0000656 } else {
Chris Lattnerad88bc42009-08-02 03:59:56 +0000657 // Okay, we have actual filters or typeinfos to emit. As such, we need to
658 // pick a type encoding for them. We're about to emit a list of pointers to
659 // typeinfo objects at the end of the LSDA. However, unless we're in static
660 // mode, this reference will require a relocation by the dynamic linker.
Chris Lattner46b754c2009-07-31 22:18:14 +0000661 //
Chris Lattnerad88bc42009-08-02 03:59:56 +0000662 // Because of this, we have a couple of options:
Bill Wendling28275fd2009-09-10 06:50:01 +0000663 //
Chris Lattnerad88bc42009-08-02 03:59:56 +0000664 // 1) If we are in -static mode, we can always use an absolute reference
665 // from the LSDA, because the static linker will resolve it.
Bill Wendling28275fd2009-09-10 06:50:01 +0000666 //
Chris Lattnerad88bc42009-08-02 03:59:56 +0000667 // 2) Otherwise, if the LSDA section is writable, we can output the direct
668 // reference to the typeinfo and allow the dynamic linker to relocate
669 // it. Since it is in a writable section, the dynamic linker won't
670 // have a problem.
Bill Wendling28275fd2009-09-10 06:50:01 +0000671 //
Chris Lattnerad88bc42009-08-02 03:59:56 +0000672 // 3) Finally, if we're in PIC mode and the LDSA section isn't writable,
673 // we need to use some form of indirection. For example, on Darwin,
674 // we can output a statically-relocatable reference to a dyld stub. The
675 // offset to the stub is constant, but the contents are in a section
676 // that is updated by the dynamic linker. This is easy enough, but we
677 // need to tell the personality function of the unwinder to indirect
678 // through the dyld stub.
679 //
Bill Wendling43e484f2009-09-10 01:12:47 +0000680 // FIXME: When (3) is actually implemented, we'll have to emit the stubs
Chris Lattnerad88bc42009-08-02 03:59:56 +0000681 // somewhere. This predicate should be moved to a shared location that is
682 // in target-independent code.
683 //
Bill Wendling43e484f2009-09-10 01:12:47 +0000684 if (LSDASection->getKind().isWriteable() ||
685 Asm->TM.getRelocationModel() == Reloc::Static)
686 TTypeFormat = dwarf::DW_EH_PE_absptr;
687 else
688 TTypeFormat = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
689 dwarf::DW_EH_PE_sdata4;
Bill Wendlinga2f64492009-09-10 06:27:16 +0000690
691 TypeFormatSize = SizeOfEncodedValue(TTypeFormat);
Bill Wendlingfe220282009-09-10 02:07:37 +0000692 }
Bill Wendling43e484f2009-09-10 01:12:47 +0000693
Bill Wendlingfe220282009-09-10 02:07:37 +0000694 // Begin the exception table.
695 Asm->OutStreamer.SwitchSection(LSDASection);
696 Asm->EmitAlignment(2, 0, 0, false);
Bill Wendling43e484f2009-09-10 01:12:47 +0000697
Bill Wendlingfe220282009-09-10 02:07:37 +0000698 O << "GCC_except_table" << SubprogramCount << ":\n";
Bill Wendlinga2f64492009-09-10 06:27:16 +0000699
700 // The type infos need to be aligned. GCC does this by inserting padding just
701 // before the type infos. However, this changes the size of the exception
702 // table, so you need to take this into account when you output the exception
703 // table size. However, the size is output using a variable length encoding.
704 // So by increasing the size by inserting padding, you may increase the number
705 // of bytes used for writing the size. If it increases, say by one byte, then
706 // you now need to output one less byte of padding to get the type infos
707 // aligned. However this decreases the size of the exception table. This
708 // changes the value you have to output for the exception table size. Due to
709 // the variable length encoding, the number of bytes used for writing the
710 // length may decrease. If so, you then have to increase the amount of
711 // padding. And so on. If you look carefully at the GCC code you will see that
712 // it indeed does this in a loop, going on and on until the values stabilize.
713 // We chose another solution: don't output padding inside the table like GCC
714 // does, instead output it before the table.
715 unsigned SizeTypes = TypeInfos.size() * TypeFormatSize;
716 unsigned TyOffset = sizeof(int8_t) + // Call site format
717 MCAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
718 SizeSites + SizeActions + SizeTypes;
719 unsigned TotalSize = sizeof(int8_t) + // LPStart format
720 sizeof(int8_t) + // TType format
721 (HaveTTData ?
722 MCAsmInfo::getULEB128Size(TyOffset) : 0) + // TType base offset
723 TyOffset;
724 unsigned SizeAlign = (4 - TotalSize) & 3;
725
726 for (unsigned i = 0; i != SizeAlign; ++i) {
727 Asm->EmitInt8(0);
728 Asm->EOL("Padding");
729 }
730
Bill Wendlingfe220282009-09-10 02:07:37 +0000731 EmitLabel("exception", SubprogramCount);
732
733 if (IsSJLJ) {
734 SmallString<16> LSDAName;
735 raw_svector_ostream(LSDAName) << MAI->getPrivateGlobalPrefix() <<
736 "_LSDA_" << Asm->getFunctionNumber();
737 O << LSDAName.str() << ":\n";
738 }
739
740 // Emit the header.
741 Asm->EmitInt8(dwarf::DW_EH_PE_omit);
742 Asm->EOL("@LPStart format", dwarf::DW_EH_PE_omit);
743
Bill Wendlingfe220282009-09-10 02:07:37 +0000744 Asm->EmitInt8(TTypeFormat);
745 Asm->EOL("@TType format", TTypeFormat);
746
747 if (HaveTTData) {
Bill Wendlinga2f64492009-09-10 06:27:16 +0000748 Asm->EmitULEB128Bytes(TyOffset);
Bill Wendlinga583c552009-08-20 22:02:24 +0000749 Asm->EOL("@TType base offset");
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000750 }
Bill Wendlingb0d9c3e2009-07-28 22:23:45 +0000751
Bill Wendling28275fd2009-09-10 06:50:01 +0000752 // SjLj Exception handling
Bill Wendlingd1a5b372009-09-10 00:17:04 +0000753 if (IsSJLJ) {
Bill Wendling639217c2009-08-27 03:32:50 +0000754 Asm->EmitInt8(dwarf::DW_EH_PE_udata4);
Bill Wendling0734d352009-09-09 21:26:19 +0000755 Asm->EOL("Call site format", dwarf::DW_EH_PE_udata4);
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000756 Asm->EmitULEB128Bytes(SizeSites);
Bill Wendlinga583c552009-08-20 22:02:24 +0000757 Asm->EOL("Call site table length");
Bill Wendlingeb907212009-05-15 01:12:28 +0000758
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000759 // Emit the landing pad site information.
Jim Grosbach8b818d72009-08-17 16:41:22 +0000760 unsigned idx = 0;
761 for (SmallVectorImpl<CallSiteEntry>::const_iterator
762 I = CallSites.begin(), E = CallSites.end(); I != E; ++I, ++idx) {
763 const CallSiteEntry &S = *I;
Bill Wendlinga583c552009-08-20 22:02:24 +0000764
765 // Offset of the landing pad, counted in 16-byte bundles relative to the
766 // @LPStart address.
Jim Grosbach8b818d72009-08-17 16:41:22 +0000767 Asm->EmitULEB128Bytes(idx);
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000768 Asm->EOL("Landing pad");
Bill Wendlinga583c552009-08-20 22:02:24 +0000769
770 // Offset of the first associated action record, relative to the start of
771 // the action table. This value is biased by 1 (1 indicates the start of
772 // the action table), and 0 indicates that there are no actions.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000773 Asm->EmitULEB128Bytes(S.Action);
774 Asm->EOL("Action");
Bill Wendlingeb907212009-05-15 01:12:28 +0000775 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000776 } else {
777 // DWARF Exception handling
Chris Lattner33adcfb2009-08-22 21:43:10 +0000778 assert(MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf);
Bill Wendlingeb907212009-05-15 01:12:28 +0000779
Bill Wendlinga583c552009-08-20 22:02:24 +0000780 // The call-site table is a list of all call sites that may throw an
781 // exception (including C++ 'throw' statements) in the procedure
782 // fragment. It immediately follows the LSDA header. Each entry indicates,
783 // for a given call, the first corresponding action record and corresponding
784 // landing pad.
785 //
786 // The table begins with the number of bytes, stored as an LEB128
787 // compressed, unsigned integer. The records immediately follow the record
788 // count. They are sorted in increasing call-site address. Each record
789 // indicates:
790 //
791 // * The position of the call-site.
792 // * The position of the landing pad.
793 // * The first action record for that call site.
794 //
795 // A missing entry in the call-site table indicates that a call is not
Bill Wendling28275fd2009-09-10 06:50:01 +0000796 // supposed to throw.
Bill Wendlinga583c552009-08-20 22:02:24 +0000797
798 // Emit the landing pad call site table.
Bill Wendling639217c2009-08-27 03:32:50 +0000799 Asm->EmitInt8(dwarf::DW_EH_PE_udata4);
Bill Wendling0734d352009-09-09 21:26:19 +0000800 Asm->EOL("Call site format", dwarf::DW_EH_PE_udata4);
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000801 Asm->EmitULEB128Bytes(SizeSites);
Bill Wendlinga583c552009-08-20 22:02:24 +0000802 Asm->EOL("Call site table size");
Bill Wendlingeb907212009-05-15 01:12:28 +0000803
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000804 for (SmallVectorImpl<CallSiteEntry>::const_iterator
805 I = CallSites.begin(), E = CallSites.end(); I != E; ++I) {
806 const CallSiteEntry &S = *I;
807 const char *BeginTag;
808 unsigned BeginNumber;
Bill Wendlingeb907212009-05-15 01:12:28 +0000809
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000810 if (!S.BeginLabel) {
811 BeginTag = "eh_func_begin";
812 BeginNumber = SubprogramCount;
813 } else {
814 BeginTag = "label";
815 BeginNumber = S.BeginLabel;
816 }
Bill Wendlingeb907212009-05-15 01:12:28 +0000817
Bill Wendlinga583c552009-08-20 22:02:24 +0000818 // Offset of the call site relative to the previous call site, counted in
819 // number of 16-byte bundles. The first call site is counted relative to
820 // the start of the procedure fragment.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000821 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
Bill Wendlingeb907212009-05-15 01:12:28 +0000822 true, true);
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000823 Asm->EOL("Region start");
Bill Wendlingeb907212009-05-15 01:12:28 +0000824
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000825 if (!S.EndLabel)
826 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
827 true);
828 else
829 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
Bill Wendlingeb907212009-05-15 01:12:28 +0000830
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000831 Asm->EOL("Region length");
832
Bill Wendlinga583c552009-08-20 22:02:24 +0000833 // Offset of the landing pad, counted in 16-byte bundles relative to the
834 // @LPStart address.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000835 if (!S.PadLabel)
836 Asm->EmitInt32(0);
837 else
838 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
839 true, true);
840
841 Asm->EOL("Landing pad");
842
Bill Wendlinga583c552009-08-20 22:02:24 +0000843 // Offset of the first associated action record, relative to the start of
844 // the action table. This value is biased by 1 (1 indicates the start of
845 // the action table), and 0 indicates that there are no actions.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000846 Asm->EmitULEB128Bytes(S.Action);
847 Asm->EOL("Action");
848 }
Bill Wendlingeb907212009-05-15 01:12:28 +0000849 }
850
Bill Wendlinga583c552009-08-20 22:02:24 +0000851 // Emit the Action Table.
Bill Wendling5cff4872009-07-28 23:44:43 +0000852 for (SmallVectorImpl<ActionEntry>::const_iterator
853 I = Actions.begin(), E = Actions.end(); I != E; ++I) {
854 const ActionEntry &Action = *I;
Bill Wendlinga583c552009-08-20 22:02:24 +0000855
856 // Type Filter
857 //
858 // Used by the runtime to match the type of the thrown exception to the
859 // type of the catch clauses or the types in the exception specification.
860
Bill Wendlingeb907212009-05-15 01:12:28 +0000861 Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
862 Asm->EOL("TypeInfo index");
Bill Wendlinga583c552009-08-20 22:02:24 +0000863
864 // Action Record
865 //
866 // Self-relative signed displacement in bytes of the next action record,
867 // or 0 if there is no next action record.
868
Bill Wendlingeb907212009-05-15 01:12:28 +0000869 Asm->EmitSLEB128Bytes(Action.NextAction);
870 Asm->EOL("Next action");
871 }
872
Bill Wendlinga583c552009-08-20 22:02:24 +0000873 // Emit the Catch Clauses. The code for the catch clauses following the same
874 // try is similar to a switch statement. The catch clause action record
875 // informs the runtime about the type of a catch clause and about the
876 // associated switch value.
877 //
878 // Action Record Fields:
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000879 //
Bill Wendlinga583c552009-08-20 22:02:24 +0000880 // * Filter Value
881 // Positive value, starting at 1. Index in the types table of the
882 // __typeinfo for the catch-clause type. 1 is the first word preceding
883 // TTBase, 2 is the second word, and so on. Used by the runtime to check
884 // if the thrown exception type matches the catch-clause type. Back-end
885 // generated switch statements check against this value.
886 //
887 // * Next
888 // Signed offset, in bytes from the start of this field, to the next
889 // chained action record, or zero if none.
890 //
891 // The order of the action records determined by the next field is the order
892 // of the catch clauses as they appear in the source code, and must be kept in
893 // the same order. As a result, changing the order of the catch clause would
894 // change the semantics of the program.
Bill Wendling5cff4872009-07-28 23:44:43 +0000895 for (std::vector<GlobalVariable *>::const_reverse_iterator
896 I = TypeInfos.rbegin(), E = TypeInfos.rend(); I != E; ++I) {
Bill Wendlinga583c552009-08-20 22:02:24 +0000897 const GlobalVariable *GV = *I;
Bill Wendlingeb907212009-05-15 01:12:28 +0000898 PrintRelDirective();
899
900 if (GV) {
Chris Lattner334fd1f2009-09-16 00:08:41 +0000901 O << Asm->Mang->getMangledName(GV);
Bill Wendlingeb907212009-05-15 01:12:28 +0000902 } else {
Bill Wendling049e98d2009-08-31 18:26:48 +0000903 O << "0x0";
Bill Wendlingeb907212009-05-15 01:12:28 +0000904 }
905
906 Asm->EOL("TypeInfo");
907 }
908
Bill Wendlinga583c552009-08-20 22:02:24 +0000909 // Emit the Type Table.
Bill Wendling5cff4872009-07-28 23:44:43 +0000910 for (std::vector<unsigned>::const_iterator
911 I = FilterIds.begin(), E = FilterIds.end(); I < E; ++I) {
912 unsigned TypeID = *I;
Bill Wendlingeb907212009-05-15 01:12:28 +0000913 Asm->EmitULEB128Bytes(TypeID);
914 Asm->EOL("Filter TypeInfo index");
915 }
916
917 Asm->EmitAlignment(2, 0, 0, false);
918}
919
Bill Wendlingeb907212009-05-15 01:12:28 +0000920/// EndModule - Emit all exception information that should come after the
921/// content.
922void DwarfException::EndModule() {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000923 if (MAI->getExceptionHandlingType() != ExceptionHandling::Dwarf)
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000924 return;
Bill Wendlingb4049fe2009-09-09 21:06:24 +0000925
Bill Wendling52783c62009-09-09 23:56:55 +0000926 if (!shouldEmitMovesModule && !shouldEmitTableModule)
927 return;
928
Bill Wendlingeb907212009-05-15 01:12:28 +0000929 if (TimePassesIsEnabled)
930 ExceptionTimer->startTimer();
931
Bill Wendling52783c62009-09-09 23:56:55 +0000932 const std::vector<Function *> Personalities = MMI->getPersonalities();
Bill Wendlingb4049fe2009-09-09 21:06:24 +0000933
Bill Wendling28275fd2009-09-10 06:50:01 +0000934 for (unsigned I = 0, E = Personalities.size(); I < E; ++I)
935 EmitCIE(Personalities[I], I);
Bill Wendlingeb907212009-05-15 01:12:28 +0000936
Bill Wendling52783c62009-09-09 23:56:55 +0000937 for (std::vector<FunctionEHFrameInfo>::iterator
938 I = EHFrames.begin(), E = EHFrames.end(); I != E; ++I)
939 EmitFDE(*I);
Bill Wendlingeb907212009-05-15 01:12:28 +0000940
941 if (TimePassesIsEnabled)
942 ExceptionTimer->stopTimer();
943}
944
Bill Wendling28275fd2009-09-10 06:50:01 +0000945/// BeginFunction - Gather pre-function exception information. Assumes it's
946/// being emitted immediately after the function entry point.
Bill Wendlingeb907212009-05-15 01:12:28 +0000947void DwarfException::BeginFunction(MachineFunction *MF) {
Bill Wendling73c5a612009-09-10 18:28:06 +0000948 if (!MMI || !MAI->doesSupportExceptionHandling()) return;
949
Bill Wendlingeb907212009-05-15 01:12:28 +0000950 if (TimePassesIsEnabled)
951 ExceptionTimer->startTimer();
952
953 this->MF = MF;
954 shouldEmitTable = shouldEmitMoves = false;
955
Bill Wendling73c5a612009-09-10 18:28:06 +0000956 // Map all labels and get rid of any dead landing pads.
957 MMI->TidyLandingPads();
Bill Wendlingeb907212009-05-15 01:12:28 +0000958
Bill Wendling73c5a612009-09-10 18:28:06 +0000959 // If any landing pads survive, we need an EH table.
960 if (!MMI->getLandingPads().empty())
961 shouldEmitTable = true;
Bill Wendlingeb907212009-05-15 01:12:28 +0000962
Bill Wendling73c5a612009-09-10 18:28:06 +0000963 // See if we need frame move info.
964 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
965 shouldEmitMoves = true;
Bill Wendlingeb907212009-05-15 01:12:28 +0000966
Bill Wendling73c5a612009-09-10 18:28:06 +0000967 if (shouldEmitMoves || shouldEmitTable)
968 // Assumes in correct section after the entry point.
969 EmitLabel("eh_func_begin", ++SubprogramCount);
Bill Wendlingeb907212009-05-15 01:12:28 +0000970
971 shouldEmitTableModule |= shouldEmitTable;
972 shouldEmitMovesModule |= shouldEmitMoves;
973
974 if (TimePassesIsEnabled)
975 ExceptionTimer->stopTimer();
976}
977
978/// EndFunction - Gather and emit post-function exception information.
979///
980void DwarfException::EndFunction() {
Bill Wendling7b09a6c2009-09-09 21:08:12 +0000981 if (!shouldEmitMoves && !shouldEmitTable) return;
982
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000983 if (TimePassesIsEnabled)
Bill Wendlingeb907212009-05-15 01:12:28 +0000984 ExceptionTimer->startTimer();
985
Bill Wendling7b09a6c2009-09-09 21:08:12 +0000986 EmitLabel("eh_func_end", SubprogramCount);
987 EmitExceptionTable();
Bill Wendlingeb907212009-05-15 01:12:28 +0000988
Chris Lattner25d812b2009-09-16 00:35:39 +0000989 std::string FunctionEHName =
990 Asm->Mang->getMangledName(MF->getFunction(), ".eh",
991 Asm->MAI->is_EHSymbolPrivate());
992
Bill Wendling7b09a6c2009-09-09 21:08:12 +0000993 // Save EH frame information
Chris Lattner25d812b2009-09-16 00:35:39 +0000994 EHFrames.push_back(FunctionEHFrameInfo(FunctionEHName, SubprogramCount,
Bill Wendling7b09a6c2009-09-09 21:08:12 +0000995 MMI->getPersonalityIndex(),
996 MF->getFrameInfo()->hasCalls(),
997 !MMI->getLandingPads().empty(),
998 MMI->getFrameMoves(),
999 MF->getFunction()));
Bill Wendlingeb907212009-05-15 01:12:28 +00001000
Bill Wendling52783c62009-09-09 23:56:55 +00001001 // Record if this personality index uses a landing pad.
1002 UsesLSDA[MMI->getPersonalityIndex()] |= !MMI->getLandingPads().empty();
1003
Eric Christopherdbfcdb92009-08-28 22:33:43 +00001004 if (TimePassesIsEnabled)
Bill Wendlingeb907212009-05-15 01:12:28 +00001005 ExceptionTimer->stopTimer();
1006}