blob: 3deb9fc359d1bc48372af719cedf85f36b9d179c [file] [log] [blame]
Bill Wendlingeb907212009-05-15 01:12:28 +00001//===-- CodeGen/AsmPrinter/DwarfException.cpp - Dwarf Exception Impl ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Bill Wendling639217c2009-08-27 03:32:50 +000010// This file contains support for writing dwarf exception info into asm files.
Bill Wendlingeb907212009-05-15 01:12:28 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "DwarfException.h"
15#include "llvm/Module.h"
16#include "llvm/CodeGen/MachineModuleInfo.h"
17#include "llvm/CodeGen/MachineFrameInfo.h"
David Greenefc4da0c2009-08-19 21:55:33 +000018#include "llvm/CodeGen/MachineFunction.h"
Bill Wendlingeb907212009-05-15 01:12:28 +000019#include "llvm/CodeGen/MachineLocation.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000020#include "llvm/MC/MCStreamer.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000021#include "llvm/MC/MCAsmInfo.h"
Bill Wendlingeb907212009-05-15 01:12:28 +000022#include "llvm/Target/TargetData.h"
23#include "llvm/Target/TargetFrameInfo.h"
Chris Lattnerd5bbb072009-08-02 01:34:32 +000024#include "llvm/Target/TargetLoweringObjectFile.h"
Bill Wendlingeb907212009-05-15 01:12:28 +000025#include "llvm/Target/TargetOptions.h"
Chris Lattnerd5bbb072009-08-02 01:34:32 +000026#include "llvm/Target/TargetRegisterInfo.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000027#include "llvm/Support/Dwarf.h"
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +000028#include "llvm/Support/Mangler.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000029#include "llvm/Support/Timer.h"
30#include "llvm/Support/raw_ostream.h"
Jim Grosbachc40d9f92009-09-01 18:49:12 +000031#include "llvm/ADT/SmallString.h"
Bill Wendlingeb907212009-05-15 01:12:28 +000032#include "llvm/ADT/StringExtras.h"
33using namespace llvm;
34
35static TimerGroup &getDwarfTimerGroup() {
36 static TimerGroup DwarfTimerGroup("Dwarf Exception");
37 return DwarfTimerGroup;
38}
39
Bill Wendlingbc0d23a2009-05-15 01:18:50 +000040DwarfException::DwarfException(raw_ostream &OS, AsmPrinter *A,
Chris Lattneraf76e592009-08-22 20:48:53 +000041 const MCAsmInfo *T)
Bill Wendlingbc0d23a2009-05-15 01:18:50 +000042 : Dwarf(OS, A, T, "eh"), shouldEmitTable(false), shouldEmitMoves(false),
43 shouldEmitTableModule(false), shouldEmitMovesModule(false),
44 ExceptionTimer(0) {
Eric Christopherdbfcdb92009-08-28 22:33:43 +000045 if (TimePassesIsEnabled)
Bill Wendlingbc0d23a2009-05-15 01:18:50 +000046 ExceptionTimer = new Timer("Dwarf Exception Writer",
47 getDwarfTimerGroup());
48}
49
50DwarfException::~DwarfException() {
51 delete ExceptionTimer;
52}
53
Bill Wendling7ccda0f2009-08-25 08:08:33 +000054/// EmitCIE - Emit a Common Information Entry (CIE). This holds information that
55/// is shared among many Frame Description Entries. There is at least one CIE
56/// in every non-empty .debug_frame section.
57void DwarfException::EmitCIE(const Function *Personality, unsigned Index) {
Bill Wendlingeb907212009-05-15 01:12:28 +000058 // Size and sign of stack growth.
59 int stackGrowth =
60 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
61 TargetFrameInfo::StackGrowsUp ?
62 TD->getPointerSize() : -TD->getPointerSize();
63
64 // Begin eh frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +000065 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getEHFrameSection());
Bill Wendlingeb907212009-05-15 01:12:28 +000066
Chris Lattner33adcfb2009-08-22 21:43:10 +000067 if (MAI->is_EHSymbolPrivate())
68 O << MAI->getPrivateGlobalPrefix();
Bill Wendlingeb907212009-05-15 01:12:28 +000069
70 O << "EH_frame" << Index << ":\n";
71 EmitLabel("section_eh_frame", Index);
72
73 // Define base labels.
74 EmitLabel("eh_frame_common", Index);
75
76 // Define the eh frame length.
77 EmitDifference("eh_frame_common_end", Index,
78 "eh_frame_common_begin", Index, true);
79 Asm->EOL("Length of Common Information Entry");
80
81 // EH frame header.
82 EmitLabel("eh_frame_common_begin", Index);
83 Asm->EmitInt32((int)0);
84 Asm->EOL("CIE Identifier Tag");
85 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
86 Asm->EOL("CIE Version");
87
88 // The personality presence indicates that language specific information will
89 // show up in the eh frame.
90 Asm->EmitString(Personality ? "zPLR" : "zR");
91 Asm->EOL("CIE Augmentation");
92
93 // Round out reader.
94 Asm->EmitULEB128Bytes(1);
95 Asm->EOL("CIE Code Alignment Factor");
96 Asm->EmitSLEB128Bytes(stackGrowth);
97 Asm->EOL("CIE Data Alignment Factor");
98 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
99 Asm->EOL("CIE Return Address Column");
100
Bill Wendling4bda11f2009-08-25 02:32:05 +0000101 // If there is a personality, we need to indicate the function's location.
Bill Wendlingeb907212009-05-15 01:12:28 +0000102 if (Personality) {
103 Asm->EmitULEB128Bytes(7);
104 Asm->EOL("Augmentation Size");
105
Chris Lattner33adcfb2009-08-22 21:43:10 +0000106 if (MAI->getNeedsIndirectEncoding()) {
Duncan Sandsc69d74a2009-08-31 16:45:16 +0000107 Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4 |
108 dwarf::DW_EH_PE_indirect);
Bill Wendling0734d352009-09-09 21:26:19 +0000109 Asm->EOL("Personality",
110 dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4 |
111 dwarf::DW_EH_PE_indirect);
Bill Wendlingeb907212009-05-15 01:12:28 +0000112 } else {
Duncan Sandsc69d74a2009-08-31 16:45:16 +0000113 Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
Bill Wendling0734d352009-09-09 21:26:19 +0000114 Asm->EOL("Personality", dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
Bill Wendlingeb907212009-05-15 01:12:28 +0000115 }
116
117 PrintRelDirective(true);
Chris Lattner33adcfb2009-08-22 21:43:10 +0000118 O << MAI->getPersonalityPrefix();
Bill Wendlingeb907212009-05-15 01:12:28 +0000119 Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
Chris Lattner33adcfb2009-08-22 21:43:10 +0000120 O << MAI->getPersonalitySuffix();
121 if (strcmp(MAI->getPersonalitySuffix(), "+4@GOTPCREL"))
122 O << "-" << MAI->getPCSymbol();
Bill Wendlingeb907212009-05-15 01:12:28 +0000123 Asm->EOL("Personality");
124
Duncan Sandsc69d74a2009-08-31 16:45:16 +0000125 Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
Bill Wendling0734d352009-09-09 21:26:19 +0000126 Asm->EOL("LSDA Encoding", dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
Bill Wendlingeb907212009-05-15 01:12:28 +0000127
Duncan Sandsc69d74a2009-08-31 16:45:16 +0000128 Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
Bill Wendling0734d352009-09-09 21:26:19 +0000129 Asm->EOL("FDE Encoding", dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
Bill Wendlingeb907212009-05-15 01:12:28 +0000130 } else {
131 Asm->EmitULEB128Bytes(1);
132 Asm->EOL("Augmentation Size");
133
Duncan Sandsc69d74a2009-08-31 16:45:16 +0000134 Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
Bill Wendling0734d352009-09-09 21:26:19 +0000135 Asm->EOL("FDE Encoding", dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
Bill Wendlingeb907212009-05-15 01:12:28 +0000136 }
137
138 // Indicate locations of general callee saved registers in frame.
139 std::vector<MachineMove> Moves;
140 RI->getInitialFrameState(Moves);
141 EmitFrameMoves(NULL, 0, Moves, true);
142
143 // On Darwin the linker honors the alignment of eh_frame, which means it must
144 // be 8-byte on 64-bit targets to match what gcc does. Otherwise you get
145 // holes which confuse readers of eh_frame.
146 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
147 0, 0, false);
148 EmitLabel("eh_frame_common_end", Index);
149
150 Asm->EOL();
151}
152
Bill Wendling7ccda0f2009-08-25 08:08:33 +0000153/// EmitFDE - Emit the Frame Description Entry (FDE) for the function.
154void DwarfException::EmitFDE(const FunctionEHFrameInfo &EHFrameInfo) {
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000155 assert(!EHFrameInfo.function->hasAvailableExternallyLinkage() &&
Bill Wendlingeb907212009-05-15 01:12:28 +0000156 "Should not emit 'available externally' functions at all");
157
Chris Lattner3e0f60b2009-07-17 21:00:50 +0000158 const Function *TheFunc = EHFrameInfo.function;
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000159
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000160 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getEHFrameSection());
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000161
Bill Wendlingeb907212009-05-15 01:12:28 +0000162 // Externally visible entry into the functions eh frame info. If the
163 // corresponding function is static, this should not be externally visible.
Chris Lattner3e0f60b2009-07-17 21:00:50 +0000164 if (!TheFunc->hasLocalLinkage())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000165 if (const char *GlobalEHDirective = MAI->getGlobalEHDirective())
Bill Wendlingeb907212009-05-15 01:12:28 +0000166 O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
Bill Wendlingeb907212009-05-15 01:12:28 +0000167
168 // If corresponding function is weak definition, this should be too.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000169 if (TheFunc->isWeakForLinker() && MAI->getWeakDefDirective())
170 O << MAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
Bill Wendlingeb907212009-05-15 01:12:28 +0000171
172 // If there are no calls then you can't unwind. This may mean we can omit the
173 // EH Frame, but some environments do not handle weak absolute symbols. If
174 // UnwindTablesMandatory is set we cannot do this optimization; the unwind
175 // info is to be available for non-EH uses.
Chris Lattner3e0f60b2009-07-17 21:00:50 +0000176 if (!EHFrameInfo.hasCalls && !UnwindTablesMandatory &&
177 (!TheFunc->isWeakForLinker() ||
Chris Lattner33adcfb2009-08-22 21:43:10 +0000178 !MAI->getWeakDefDirective() ||
179 MAI->getSupportsWeakOmittedEHFrame())) {
Bill Wendlingeb907212009-05-15 01:12:28 +0000180 O << EHFrameInfo.FnName << " = 0\n";
181 // This name has no connection to the function, so it might get
182 // dead-stripped when the function is not, erroneously. Prohibit
183 // dead-stripping unconditionally.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000184 if (const char *UsedDirective = MAI->getUsedDirective())
Bill Wendlingeb907212009-05-15 01:12:28 +0000185 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
186 } else {
187 O << EHFrameInfo.FnName << ":\n";
188
189 // EH frame header.
190 EmitDifference("eh_frame_end", EHFrameInfo.Number,
191 "eh_frame_begin", EHFrameInfo.Number, true);
192 Asm->EOL("Length of Frame Information Entry");
193
194 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
195
Chris Lattnera4ff5e42009-07-17 20:53:51 +0000196 EmitSectionOffset("eh_frame_begin", "eh_frame_common",
197 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
198 true, true, false);
Bill Wendlingeb907212009-05-15 01:12:28 +0000199
200 Asm->EOL("FDE CIE offset");
201
Duncan Sandsc69d74a2009-08-31 16:45:16 +0000202 EmitReference("eh_func_begin", EHFrameInfo.Number, true, true);
Bill Wendlingeb907212009-05-15 01:12:28 +0000203 Asm->EOL("FDE initial location");
204 EmitDifference("eh_func_end", EHFrameInfo.Number,
Duncan Sandsc69d74a2009-08-31 16:45:16 +0000205 "eh_func_begin", EHFrameInfo.Number, true);
Bill Wendlingeb907212009-05-15 01:12:28 +0000206 Asm->EOL("FDE address range");
207
208 // If there is a personality and landing pads then point to the language
209 // specific data area in the exception table.
Eric Christopherd44fff72009-08-26 21:30:49 +0000210 if (MMI->getPersonalities()[0] != NULL) {
Duncan Sandsc69d74a2009-08-31 16:45:16 +0000211 bool is4Byte = TD->getPointerSize() == sizeof(int32_t);
212
Eric Christopher6fefceb2009-08-29 01:12:46 +0000213 Asm->EmitULEB128Bytes(is4Byte ? 4 : 8);
Bill Wendlingeb907212009-05-15 01:12:28 +0000214 Asm->EOL("Augmentation size");
215
Duncan Sandsc69d74a2009-08-31 16:45:16 +0000216 if (EHFrameInfo.hasLandingPads)
Eric Christopher6fefceb2009-08-29 01:12:46 +0000217 EmitReference("exception", EHFrameInfo.Number, true, false);
Duncan Sandsc69d74a2009-08-31 16:45:16 +0000218 else {
Eric Christopher6fefceb2009-08-29 01:12:46 +0000219 if (is4Byte)
220 Asm->EmitInt32((int)0);
221 else
222 Asm->EmitInt64((int)0);
223 }
Bill Wendlingeb907212009-05-15 01:12:28 +0000224 Asm->EOL("Language Specific Data Area");
225 } else {
226 Asm->EmitULEB128Bytes(0);
227 Asm->EOL("Augmentation size");
228 }
229
230 // Indicate locations of function specific callee saved registers in frame.
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000231 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves,
Bill Wendlingeb907212009-05-15 01:12:28 +0000232 true);
233
234 // On Darwin the linker honors the alignment of eh_frame, which means it
235 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise you
236 // get holes which confuse readers of eh_frame.
237 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
238 0, 0, false);
239 EmitLabel("eh_frame_end", EHFrameInfo.Number);
240
241 // If the function is marked used, this table should be also. We cannot
242 // make the mark unconditional in this case, since retaining the table also
243 // retains the function in this case, and there is code around that depends
244 // on unused functions (calling undefined externals) being dead-stripped to
245 // link correctly. Yes, there really is.
Chris Lattner401e10c2009-07-20 06:14:25 +0000246 if (MMI->isUsedFunction(EHFrameInfo.function))
Chris Lattner33adcfb2009-08-22 21:43:10 +0000247 if (const char *UsedDirective = MAI->getUsedDirective())
Bill Wendlingeb907212009-05-15 01:12:28 +0000248 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
249 }
Bill Wendling4bda11f2009-08-25 02:32:05 +0000250
251 Asm->EOL();
Bill Wendlingeb907212009-05-15 01:12:28 +0000252}
253
Bill Wendlingeb907212009-05-15 01:12:28 +0000254/// SharedTypeIds - How many leading type ids two landing pads have in common.
255unsigned DwarfException::SharedTypeIds(const LandingPadInfo *L,
256 const LandingPadInfo *R) {
257 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
258 unsigned LSize = LIds.size(), RSize = RIds.size();
259 unsigned MinSize = LSize < RSize ? LSize : RSize;
260 unsigned Count = 0;
261
262 for (; Count != MinSize; ++Count)
263 if (LIds[Count] != RIds[Count])
264 return Count;
265
266 return Count;
267}
268
269/// PadLT - Order landing pads lexicographically by type id.
270bool DwarfException::PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
271 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
272 unsigned LSize = LIds.size(), RSize = RIds.size();
273 unsigned MinSize = LSize < RSize ? LSize : RSize;
274
275 for (unsigned i = 0; i != MinSize; ++i)
276 if (LIds[i] != RIds[i])
277 return LIds[i] < RIds[i];
278
279 return LSize < RSize;
280}
281
Bill Wendlingd4609622009-07-28 23:23:00 +0000282/// ComputeActionsTable - Compute the actions table and gather the first action
283/// index for each landing pad site.
Bill Wendlingade025c2009-07-29 00:31:35 +0000284unsigned DwarfException::
285ComputeActionsTable(const SmallVectorImpl<const LandingPadInfo*> &LandingPads,
286 SmallVectorImpl<ActionEntry> &Actions,
287 SmallVectorImpl<unsigned> &FirstActions) {
Bill Wendlinga583c552009-08-20 22:02:24 +0000288
289 // The action table follows the call-site table in the LSDA. The individual
290 // records are of two types:
291 //
292 // * Catch clause
293 // * Exception specification
294 //
295 // The two record kinds have the same format, with only small differences.
296 // They are distinguished by the "switch value" field: Catch clauses
297 // (TypeInfos) have strictly positive switch values, and exception
298 // specifications (FilterIds) have strictly negative switch values. Value 0
299 // indicates a catch-all clause.
300 //
Bill Wendling5e953dd2009-07-28 23:22:13 +0000301 // Negative type IDs index into FilterIds. Positive type IDs index into
302 // TypeInfos. The value written for a positive type ID is just the type ID
303 // itself. For a negative type ID, however, the value written is the
Bill Wendlingeb907212009-05-15 01:12:28 +0000304 // (negative) byte offset of the corresponding FilterIds entry. The byte
Bill Wendling5e953dd2009-07-28 23:22:13 +0000305 // offset is usually equal to the type ID (because the FilterIds entries are
306 // written using a variable width encoding, which outputs one byte per entry
307 // as long as the value written is not too large) but can differ. This kind
308 // of complication does not occur for positive type IDs because type infos are
Bill Wendlingeb907212009-05-15 01:12:28 +0000309 // output using a fixed width encoding. FilterOffsets[i] holds the byte
310 // offset corresponding to FilterIds[i].
Bill Wendling409914b2009-07-29 21:19:44 +0000311
312 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
Bill Wendlingeb907212009-05-15 01:12:28 +0000313 SmallVector<int, 16> FilterOffsets;
314 FilterOffsets.reserve(FilterIds.size());
315 int Offset = -1;
Bill Wendling409914b2009-07-29 21:19:44 +0000316
317 for (std::vector<unsigned>::const_iterator
318 I = FilterIds.begin(), E = FilterIds.end(); I != E; ++I) {
Bill Wendlingeb907212009-05-15 01:12:28 +0000319 FilterOffsets.push_back(Offset);
Chris Lattneraf76e592009-08-22 20:48:53 +0000320 Offset -= MCAsmInfo::getULEB128Size(*I);
Bill Wendlingeb907212009-05-15 01:12:28 +0000321 }
322
Bill Wendlingeb907212009-05-15 01:12:28 +0000323 FirstActions.reserve(LandingPads.size());
324
325 int FirstAction = 0;
326 unsigned SizeActions = 0;
Bill Wendling5e953dd2009-07-28 23:22:13 +0000327 const LandingPadInfo *PrevLPI = 0;
Bill Wendling409914b2009-07-29 21:19:44 +0000328
Bill Wendling5cff4872009-07-28 23:44:43 +0000329 for (SmallVectorImpl<const LandingPadInfo *>::const_iterator
Bill Wendling5e953dd2009-07-28 23:22:13 +0000330 I = LandingPads.begin(), E = LandingPads.end(); I != E; ++I) {
331 const LandingPadInfo *LPI = *I;
332 const std::vector<int> &TypeIds = LPI->TypeIds;
333 const unsigned NumShared = PrevLPI ? SharedTypeIds(LPI, PrevLPI) : 0;
Bill Wendlingeb907212009-05-15 01:12:28 +0000334 unsigned SizeSiteActions = 0;
335
336 if (NumShared < TypeIds.size()) {
337 unsigned SizeAction = 0;
338 ActionEntry *PrevAction = 0;
339
340 if (NumShared) {
Bill Wendling5e953dd2009-07-28 23:22:13 +0000341 const unsigned SizePrevIds = PrevLPI->TypeIds.size();
Bill Wendlingeb907212009-05-15 01:12:28 +0000342 assert(Actions.size());
343 PrevAction = &Actions.back();
Chris Lattneraf76e592009-08-22 20:48:53 +0000344 SizeAction = MCAsmInfo::getSLEB128Size(PrevAction->NextAction) +
345 MCAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Bill Wendlingeb907212009-05-15 01:12:28 +0000346
347 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
348 SizeAction -=
Chris Lattneraf76e592009-08-22 20:48:53 +0000349 MCAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Bill Wendlingeb907212009-05-15 01:12:28 +0000350 SizeAction += -PrevAction->NextAction;
351 PrevAction = PrevAction->Previous;
352 }
353 }
354
355 // Compute the actions.
Bill Wendling5e953dd2009-07-28 23:22:13 +0000356 for (unsigned J = NumShared, M = TypeIds.size(); J != M; ++J) {
357 int TypeID = TypeIds[J];
358 assert(-1 - TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
Bill Wendlingeb907212009-05-15 01:12:28 +0000359 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
Chris Lattneraf76e592009-08-22 20:48:53 +0000360 unsigned SizeTypeID = MCAsmInfo::getSLEB128Size(ValueForTypeID);
Bill Wendlingeb907212009-05-15 01:12:28 +0000361
362 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
Chris Lattneraf76e592009-08-22 20:48:53 +0000363 SizeAction = SizeTypeID + MCAsmInfo::getSLEB128Size(NextAction);
Bill Wendlingeb907212009-05-15 01:12:28 +0000364 SizeSiteActions += SizeAction;
365
Bill Wendlinga583c552009-08-20 22:02:24 +0000366 ActionEntry Action = { ValueForTypeID, NextAction, PrevAction };
Bill Wendlingeb907212009-05-15 01:12:28 +0000367 Actions.push_back(Action);
Bill Wendlingeb907212009-05-15 01:12:28 +0000368 PrevAction = &Actions.back();
369 }
370
371 // Record the first action of the landing pad site.
372 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
373 } // else identical - re-use previous FirstAction
374
Bill Wendlinga583c552009-08-20 22:02:24 +0000375 // Information used when created the call-site table. The action record
376 // field of the call site record is the offset of the first associated
377 // action record, relative to the start of the actions table. This value is
378 // biased by 1 (1 in dicating the start of the actions table), and 0
379 // indicates that there are no actions.
Bill Wendlingeb907212009-05-15 01:12:28 +0000380 FirstActions.push_back(FirstAction);
381
382 // Compute this sites contribution to size.
383 SizeActions += SizeSiteActions;
Bill Wendling5e953dd2009-07-28 23:22:13 +0000384
385 PrevLPI = LPI;
Bill Wendlingeb907212009-05-15 01:12:28 +0000386 }
387
Bill Wendling5e953dd2009-07-28 23:22:13 +0000388 return SizeActions;
389}
390
Bill Wendlingade025c2009-07-29 00:31:35 +0000391/// ComputeCallSiteTable - Compute the call-site table. The entry for an invoke
Bill Wendlinga583c552009-08-20 22:02:24 +0000392/// has a try-range containing the call, a non-zero landing pad, and an
Bill Wendlingade025c2009-07-29 00:31:35 +0000393/// appropriate action. The entry for an ordinary call has a try-range
394/// containing the call and zero for the landing pad and the action. Calls
395/// marked 'nounwind' have no entry and must not be contained in the try-range
396/// of any entry - they form gaps in the table. Entries must be ordered by
397/// try-range address.
398void DwarfException::
399ComputeCallSiteTable(SmallVectorImpl<CallSiteEntry> &CallSites,
400 const RangeMapType &PadMap,
401 const SmallVectorImpl<const LandingPadInfo *> &LandingPads,
402 const SmallVectorImpl<unsigned> &FirstActions) {
Bill Wendlingeb907212009-05-15 01:12:28 +0000403 // The end label of the previous invoke or nounwind try-range.
404 unsigned LastLabel = 0;
405
406 // Whether there is a potentially throwing instruction (currently this means
407 // an ordinary call) between the end of the previous try-range and now.
408 bool SawPotentiallyThrowing = false;
409
Bill Wendling5cff4872009-07-28 23:44:43 +0000410 // Whether the last CallSite entry was for an invoke.
Bill Wendlingeb907212009-05-15 01:12:28 +0000411 bool PreviousIsInvoke = false;
412
413 // Visit all instructions in order of address.
414 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
415 I != E; ++I) {
416 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
417 MI != E; ++MI) {
418 if (!MI->isLabel()) {
419 SawPotentiallyThrowing |= MI->getDesc().isCall();
420 continue;
421 }
422
423 unsigned BeginLabel = MI->getOperand(0).getImm();
424 assert(BeginLabel && "Invalid label!");
425
426 // End of the previous try-range?
427 if (BeginLabel == LastLabel)
428 SawPotentiallyThrowing = false;
429
430 // Beginning of a new try-range?
431 RangeMapType::iterator L = PadMap.find(BeginLabel);
432 if (L == PadMap.end())
433 // Nope, it was just some random label.
434 continue;
435
Bill Wendlinga583c552009-08-20 22:02:24 +0000436 const PadRange &P = L->second;
Bill Wendlingeb907212009-05-15 01:12:28 +0000437 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
Bill Wendlingeb907212009-05-15 01:12:28 +0000438 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
439 "Inconsistent landing pad map!");
440
Bill Wendlinga583c552009-08-20 22:02:24 +0000441 // For Dwarf exception handling (SjLj handling doesn't use this). If some
442 // instruction between the previous try-range and this one may throw,
443 // create a call-site entry with no landing pad for the region between the
444 // try-ranges.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000445 if (SawPotentiallyThrowing &&
Chris Lattner33adcfb2009-08-22 21:43:10 +0000446 MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) {
Bill Wendlinga583c552009-08-20 22:02:24 +0000447 CallSiteEntry Site = { LastLabel, BeginLabel, 0, 0 };
Bill Wendlingeb907212009-05-15 01:12:28 +0000448 CallSites.push_back(Site);
449 PreviousIsInvoke = false;
450 }
451
452 LastLabel = LandingPad->EndLabels[P.RangeIndex];
453 assert(BeginLabel && LastLabel && "Invalid landing pad!");
454
455 if (LandingPad->LandingPadLabel) {
456 // This try-range is for an invoke.
Bill Wendlinga583c552009-08-20 22:02:24 +0000457 CallSiteEntry Site = {
458 BeginLabel,
459 LastLabel,
460 LandingPad->LandingPadLabel,
461 FirstActions[P.PadIndex]
462 };
Bill Wendlingeb907212009-05-15 01:12:28 +0000463
Jim Grosbach33668c02009-09-01 17:19:13 +0000464 // Try to merge with the previous call-site. SJLJ doesn't do this
465 if (PreviousIsInvoke &&
466 MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) {
Bill Wendlingeb907212009-05-15 01:12:28 +0000467 CallSiteEntry &Prev = CallSites.back();
468 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
469 // Extend the range of the previous entry.
470 Prev.EndLabel = Site.EndLabel;
471 continue;
472 }
473 }
474
475 // Otherwise, create a new call-site.
476 CallSites.push_back(Site);
477 PreviousIsInvoke = true;
478 } else {
479 // Create a gap.
480 PreviousIsInvoke = false;
481 }
482 }
483 }
484
485 // If some instruction between the previous try-range and the end of the
486 // function may throw, create a call-site entry with no landing pad for the
487 // region following the try-range.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000488 if (SawPotentiallyThrowing &&
Chris Lattner33adcfb2009-08-22 21:43:10 +0000489 MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) {
Bill Wendlinga583c552009-08-20 22:02:24 +0000490 CallSiteEntry Site = { LastLabel, 0, 0, 0 };
Bill Wendlingeb907212009-05-15 01:12:28 +0000491 CallSites.push_back(Site);
492 }
Bill Wendlingade025c2009-07-29 00:31:35 +0000493}
494
Bill Wendling0dafca92009-07-29 00:50:05 +0000495/// EmitExceptionTable - Emit landing pads and actions.
496///
497/// The general organization of the table is complex, but the basic concepts are
498/// easy. First there is a header which describes the location and organization
499/// of the three components that follow.
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000500///
Bill Wendling0dafca92009-07-29 00:50:05 +0000501/// 1. The landing pad site information describes the range of code covered by
502/// the try. In our case it's an accumulation of the ranges covered by the
503/// invokes in the try. There is also a reference to the landing pad that
504/// handles the exception once processed. Finally an index into the actions
505/// table.
Bill Wendlinga583c552009-08-20 22:02:24 +0000506/// 2. The action table, in our case, is composed of pairs of type IDs and next
Bill Wendling0dafca92009-07-29 00:50:05 +0000507/// action offset. Starting with the action index from the landing pad
Bill Wendlinga583c552009-08-20 22:02:24 +0000508/// site, each type ID is checked for a match to the current exception. If
Bill Wendling0dafca92009-07-29 00:50:05 +0000509/// it matches then the exception and type id are passed on to the landing
510/// pad. Otherwise the next action is looked up. This chain is terminated
511/// with a next action of zero. If no type id is found the the frame is
512/// unwound and handling continues.
Bill Wendlinga583c552009-08-20 22:02:24 +0000513/// 3. Type ID table contains references to all the C++ typeinfo for all
Bill Wendling0dafca92009-07-29 00:50:05 +0000514/// catches in the function. This tables is reversed indexed base 1.
Bill Wendlingade025c2009-07-29 00:31:35 +0000515void DwarfException::EmitExceptionTable() {
516 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
517 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
518 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
519 if (PadInfos.empty()) return;
520
521 // Sort the landing pads in order of their type ids. This is used to fold
522 // duplicate actions.
523 SmallVector<const LandingPadInfo *, 64> LandingPads;
524 LandingPads.reserve(PadInfos.size());
525
526 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
527 LandingPads.push_back(&PadInfos[i]);
528
529 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
530
531 // Compute the actions table and gather the first action index for each
532 // landing pad site.
533 SmallVector<ActionEntry, 32> Actions;
534 SmallVector<unsigned, 64> FirstActions;
535 unsigned SizeActions = ComputeActionsTable(LandingPads, Actions, FirstActions);
536
537 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
538 // by try-range labels when lowered). Ordinary calls do not, so appropriate
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000539 // try-ranges for them need be deduced when using Dwarf exception handling.
Bill Wendlingade025c2009-07-29 00:31:35 +0000540 RangeMapType PadMap;
541 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
542 const LandingPadInfo *LandingPad = LandingPads[i];
543 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
544 unsigned BeginLabel = LandingPad->BeginLabels[j];
545 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
546 PadRange P = { i, j };
547 PadMap[BeginLabel] = P;
548 }
549 }
550
551 // Compute the call-site table.
552 SmallVector<CallSiteEntry, 64> CallSites;
Jim Grosbach8b818d72009-08-17 16:41:22 +0000553 ComputeCallSiteTable(CallSites, PadMap, LandingPads, FirstActions);
Bill Wendlingeb907212009-05-15 01:12:28 +0000554
555 // Final tallies.
556
557 // Call sites.
558 const unsigned SiteStartSize = sizeof(int32_t); // DW_EH_PE_udata4
559 const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4
560 const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000561 unsigned SizeSites;
Jim Grosbachbff39232009-08-12 17:38:44 +0000562
Chris Lattner33adcfb2009-08-22 21:43:10 +0000563 bool HaveTTData = (MAI->getExceptionHandlingType() == ExceptionHandling::SjLj)
Jim Grosbachbff39232009-08-12 17:38:44 +0000564 ? (!TypeInfos.empty() || !FilterIds.empty()) : true;
565
566
Chris Lattner33adcfb2009-08-22 21:43:10 +0000567 if (MAI->getExceptionHandlingType() == ExceptionHandling::SjLj) {
Jim Grosbach8b818d72009-08-17 16:41:22 +0000568 SizeSites = 0;
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000569 } else
570 SizeSites = CallSites.size() *
571 (SiteStartSize + SiteLengthSize + LandingPadSize);
572 for (unsigned i = 0, e = CallSites.size(); i < e; ++i) {
Chris Lattneraf76e592009-08-22 20:48:53 +0000573 SizeSites += MCAsmInfo::getULEB128Size(CallSites[i].Action);
Chris Lattner33adcfb2009-08-22 21:43:10 +0000574 if (MAI->getExceptionHandlingType() == ExceptionHandling::SjLj)
Chris Lattneraf76e592009-08-22 20:48:53 +0000575 SizeSites += MCAsmInfo::getULEB128Size(i);
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000576 }
Bill Wendlingeb907212009-05-15 01:12:28 +0000577 // Type infos.
578 const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
579 unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
580
Bill Wendlingb4049fe2009-09-09 21:06:24 +0000581 unsigned TypeOffset = sizeof(int8_t) + // Call site format
Chris Lattneraf76e592009-08-22 20:48:53 +0000582 MCAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
Bill Wendlingeb907212009-05-15 01:12:28 +0000583 SizeSites + SizeActions + SizeTypes;
584
585 unsigned TotalSize = sizeof(int8_t) + // LPStart format
586 sizeof(int8_t) + // TType format
Jim Grosbachbff39232009-08-12 17:38:44 +0000587 (HaveTTData ?
Chris Lattneraf76e592009-08-22 20:48:53 +0000588 MCAsmInfo::getULEB128Size(TypeOffset) : 0) + // TType base offset
Bill Wendlingeb907212009-05-15 01:12:28 +0000589 TypeOffset;
590
591 unsigned SizeAlign = (4 - TotalSize) & 3;
592
593 // Begin the exception table.
Chris Lattnerd5bbb072009-08-02 01:34:32 +0000594 const MCSection *LSDASection = Asm->getObjFileLowering().getLSDASection();
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000595 Asm->OutStreamer.SwitchSection(LSDASection);
Bill Wendlingeb907212009-05-15 01:12:28 +0000596 Asm->EmitAlignment(2, 0, 0, false);
597 O << "GCC_except_table" << SubprogramCount << ":\n";
598
599 for (unsigned i = 0; i != SizeAlign; ++i) {
600 Asm->EmitInt8(0);
601 Asm->EOL("Padding");
Bill Wendlingc5800a82009-07-28 21:54:03 +0000602 }
Bill Wendlingeb907212009-05-15 01:12:28 +0000603
604 EmitLabel("exception", SubprogramCount);
Chris Lattner33adcfb2009-08-22 21:43:10 +0000605 if (MAI->getExceptionHandlingType() == ExceptionHandling::SjLj) {
Jim Grosbachee793a62009-09-01 18:55:08 +0000606 SmallString<16> LSDAName;
Jim Grosbachc40d9f92009-09-01 18:49:12 +0000607 raw_svector_ostream(LSDAName) << MAI->getPrivateGlobalPrefix() <<
608 "_LSDA_" << Asm->getFunctionNumber();
609 O << LSDAName.str() << ":\n";
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000610 }
Bill Wendlingeb907212009-05-15 01:12:28 +0000611
612 // Emit the header.
Bill Wendling639217c2009-08-27 03:32:50 +0000613 Asm->EmitInt8(dwarf::DW_EH_PE_omit);
Bill Wendling0734d352009-09-09 21:26:19 +0000614 Asm->EOL("@LPStart format", dwarf::DW_EH_PE_omit);
Bill Wendlingb0d9c3e2009-07-28 22:23:45 +0000615
Bill Wendlingade025c2009-07-29 00:31:35 +0000616#if 0
Chris Lattner81c9a062009-07-31 22:03:47 +0000617 if (TypeInfos.empty() && FilterIds.empty()) {
Chris Lattnerad88bc42009-08-02 03:59:56 +0000618 // If there are no typeinfos or filters, there is nothing to emit, optimize
619 // by specifying the "omit" encoding.
Bill Wendling639217c2009-08-27 03:32:50 +0000620 Asm->EmitInt8(dwarf::DW_EH_PE_omit);
Bill Wendling0734d352009-09-09 21:26:19 +0000621 Asm->EOL("@TType format", dwarf::DW_EH_PE_omit);
Chris Lattner81c9a062009-07-31 22:03:47 +0000622 } else {
Chris Lattnerad88bc42009-08-02 03:59:56 +0000623 // Okay, we have actual filters or typeinfos to emit. As such, we need to
624 // pick a type encoding for them. We're about to emit a list of pointers to
625 // typeinfo objects at the end of the LSDA. However, unless we're in static
626 // mode, this reference will require a relocation by the dynamic linker.
Chris Lattner46b754c2009-07-31 22:18:14 +0000627 //
Chris Lattnerad88bc42009-08-02 03:59:56 +0000628 // Because of this, we have a couple of options:
629 // 1) If we are in -static mode, we can always use an absolute reference
630 // from the LSDA, because the static linker will resolve it.
631 // 2) Otherwise, if the LSDA section is writable, we can output the direct
632 // reference to the typeinfo and allow the dynamic linker to relocate
633 // it. Since it is in a writable section, the dynamic linker won't
634 // have a problem.
635 // 3) Finally, if we're in PIC mode and the LDSA section isn't writable,
636 // we need to use some form of indirection. For example, on Darwin,
637 // we can output a statically-relocatable reference to a dyld stub. The
638 // offset to the stub is constant, but the contents are in a section
639 // that is updated by the dynamic linker. This is easy enough, but we
640 // need to tell the personality function of the unwinder to indirect
641 // through the dyld stub.
642 //
643 // FIXME: When this is actually implemented, we'll have to emit the stubs
644 // somewhere. This predicate should be moved to a shared location that is
645 // in target-independent code.
646 //
647 if (LSDASection->isWritable() ||
648 Asm->TM.getRelocationModel() == Reloc::Static) {
Bill Wendling639217c2009-08-27 03:32:50 +0000649 Asm->EmitInt8(DW_EH_PE_absptr);
650 Asm->EOL("TType format (DW_EH_PE_absptr)");
Chris Lattnerad88bc42009-08-02 03:59:56 +0000651 } else {
652 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_indirect | DW_EH_PE_sdata4);
Bill Wendling639217c2009-08-27 03:32:50 +0000653 Asm->EOL("TType format (DW_EH_PE_pcrel | DW_EH_PE_indirect"
654 " | DW_EH_PE_sdata4)");
Chris Lattnerad88bc42009-08-02 03:59:56 +0000655 }
Bill Wendlingb0d9c3e2009-07-28 22:23:45 +0000656 Asm->EmitULEB128Bytes(TypeOffset);
657 Asm->EOL("TType base offset");
Bill Wendlingb0d9c3e2009-07-28 22:23:45 +0000658 }
Bill Wendlingade025c2009-07-29 00:31:35 +0000659#else
Jim Grosbachbff39232009-08-12 17:38:44 +0000660 // For SjLj exceptions, if there is no TypeInfo, then we just explicitly
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000661 // say that we're omitting that bit.
662 // FIXME: does this apply to Dwarf also? The above #if 0 implies yes?
Jim Grosbachbff39232009-08-12 17:38:44 +0000663 if (!HaveTTData) {
Bill Wendling639217c2009-08-27 03:32:50 +0000664 Asm->EmitInt8(dwarf::DW_EH_PE_omit);
Bill Wendling0734d352009-09-09 21:26:19 +0000665 Asm->EOL("@TType format", dwarf::DW_EH_PE_omit);
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000666 } else {
Bill Wendling639217c2009-08-27 03:32:50 +0000667 Asm->EmitInt8(dwarf::DW_EH_PE_absptr);
Bill Wendling0734d352009-09-09 21:26:19 +0000668 Asm->EOL("@TType format", dwarf::DW_EH_PE_absptr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000669 Asm->EmitULEB128Bytes(TypeOffset);
Bill Wendlinga583c552009-08-20 22:02:24 +0000670 Asm->EOL("@TType base offset");
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000671 }
Bill Wendlingade025c2009-07-29 00:31:35 +0000672#endif
Bill Wendlingb0d9c3e2009-07-28 22:23:45 +0000673
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000674 // SjLj Exception handilng
Chris Lattner33adcfb2009-08-22 21:43:10 +0000675 if (MAI->getExceptionHandlingType() == ExceptionHandling::SjLj) {
Bill Wendling639217c2009-08-27 03:32:50 +0000676 Asm->EmitInt8(dwarf::DW_EH_PE_udata4);
Bill Wendling0734d352009-09-09 21:26:19 +0000677 Asm->EOL("Call site format", dwarf::DW_EH_PE_udata4);
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000678 Asm->EmitULEB128Bytes(SizeSites);
Bill Wendlinga583c552009-08-20 22:02:24 +0000679 Asm->EOL("Call site table length");
Bill Wendlingeb907212009-05-15 01:12:28 +0000680
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000681 // Emit the landing pad site information.
Jim Grosbach8b818d72009-08-17 16:41:22 +0000682 unsigned idx = 0;
683 for (SmallVectorImpl<CallSiteEntry>::const_iterator
684 I = CallSites.begin(), E = CallSites.end(); I != E; ++I, ++idx) {
685 const CallSiteEntry &S = *I;
Bill Wendlinga583c552009-08-20 22:02:24 +0000686
687 // Offset of the landing pad, counted in 16-byte bundles relative to the
688 // @LPStart address.
Jim Grosbach8b818d72009-08-17 16:41:22 +0000689 Asm->EmitULEB128Bytes(idx);
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000690 Asm->EOL("Landing pad");
Bill Wendlinga583c552009-08-20 22:02:24 +0000691
692 // Offset of the first associated action record, relative to the start of
693 // the action table. This value is biased by 1 (1 indicates the start of
694 // the action table), and 0 indicates that there are no actions.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000695 Asm->EmitULEB128Bytes(S.Action);
696 Asm->EOL("Action");
Bill Wendlingeb907212009-05-15 01:12:28 +0000697 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000698 } else {
699 // DWARF Exception handling
Chris Lattner33adcfb2009-08-22 21:43:10 +0000700 assert(MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf);
Bill Wendlingeb907212009-05-15 01:12:28 +0000701
Bill Wendlinga583c552009-08-20 22:02:24 +0000702 // The call-site table is a list of all call sites that may throw an
703 // exception (including C++ 'throw' statements) in the procedure
704 // fragment. It immediately follows the LSDA header. Each entry indicates,
705 // for a given call, the first corresponding action record and corresponding
706 // landing pad.
707 //
708 // The table begins with the number of bytes, stored as an LEB128
709 // compressed, unsigned integer. The records immediately follow the record
710 // count. They are sorted in increasing call-site address. Each record
711 // indicates:
712 //
713 // * The position of the call-site.
714 // * The position of the landing pad.
715 // * The first action record for that call site.
716 //
717 // A missing entry in the call-site table indicates that a call is not
718 // supposed to throw. Such calls include:
719 //
720 // * Calls to destructors within cleanup code. C++ semantics forbids these
721 // calls to throw.
722 // * Calls to intrinsic routines in the standard library which are known
723 // not to throw (sin, memcpy, et al).
724 //
725 // If the runtime does not find the call-site entry for a given call, it
726 // will call `terminate()'.
727
728 // Emit the landing pad call site table.
Bill Wendling639217c2009-08-27 03:32:50 +0000729 Asm->EmitInt8(dwarf::DW_EH_PE_udata4);
Bill Wendling0734d352009-09-09 21:26:19 +0000730 Asm->EOL("Call site format", dwarf::DW_EH_PE_udata4);
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000731 Asm->EmitULEB128Bytes(SizeSites);
Bill Wendlinga583c552009-08-20 22:02:24 +0000732 Asm->EOL("Call site table size");
Bill Wendlingeb907212009-05-15 01:12:28 +0000733
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000734 for (SmallVectorImpl<CallSiteEntry>::const_iterator
735 I = CallSites.begin(), E = CallSites.end(); I != E; ++I) {
736 const CallSiteEntry &S = *I;
737 const char *BeginTag;
738 unsigned BeginNumber;
Bill Wendlingeb907212009-05-15 01:12:28 +0000739
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000740 if (!S.BeginLabel) {
741 BeginTag = "eh_func_begin";
742 BeginNumber = SubprogramCount;
743 } else {
744 BeginTag = "label";
745 BeginNumber = S.BeginLabel;
746 }
Bill Wendlingeb907212009-05-15 01:12:28 +0000747
Bill Wendlinga583c552009-08-20 22:02:24 +0000748 // Offset of the call site relative to the previous call site, counted in
749 // number of 16-byte bundles. The first call site is counted relative to
750 // the start of the procedure fragment.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000751 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
Bill Wendlingeb907212009-05-15 01:12:28 +0000752 true, true);
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000753 Asm->EOL("Region start");
Bill Wendlingeb907212009-05-15 01:12:28 +0000754
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000755 if (!S.EndLabel)
756 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
757 true);
758 else
759 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
Bill Wendlingeb907212009-05-15 01:12:28 +0000760
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000761 Asm->EOL("Region length");
762
Bill Wendlinga583c552009-08-20 22:02:24 +0000763 // Offset of the landing pad, counted in 16-byte bundles relative to the
764 // @LPStart address.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000765 if (!S.PadLabel)
766 Asm->EmitInt32(0);
767 else
768 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
769 true, true);
770
771 Asm->EOL("Landing pad");
772
Bill Wendlinga583c552009-08-20 22:02:24 +0000773 // Offset of the first associated action record, relative to the start of
774 // the action table. This value is biased by 1 (1 indicates the start of
775 // the action table), and 0 indicates that there are no actions.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000776 Asm->EmitULEB128Bytes(S.Action);
777 Asm->EOL("Action");
778 }
Bill Wendlingeb907212009-05-15 01:12:28 +0000779 }
780
Bill Wendlinga583c552009-08-20 22:02:24 +0000781 // Emit the Action Table.
Bill Wendling5cff4872009-07-28 23:44:43 +0000782 for (SmallVectorImpl<ActionEntry>::const_iterator
783 I = Actions.begin(), E = Actions.end(); I != E; ++I) {
784 const ActionEntry &Action = *I;
Bill Wendlinga583c552009-08-20 22:02:24 +0000785
786 // Type Filter
787 //
788 // Used by the runtime to match the type of the thrown exception to the
789 // type of the catch clauses or the types in the exception specification.
790
Bill Wendlingeb907212009-05-15 01:12:28 +0000791 Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
792 Asm->EOL("TypeInfo index");
Bill Wendlinga583c552009-08-20 22:02:24 +0000793
794 // Action Record
795 //
796 // Self-relative signed displacement in bytes of the next action record,
797 // or 0 if there is no next action record.
798
Bill Wendlingeb907212009-05-15 01:12:28 +0000799 Asm->EmitSLEB128Bytes(Action.NextAction);
800 Asm->EOL("Next action");
801 }
802
Bill Wendlinga583c552009-08-20 22:02:24 +0000803 // Emit the Catch Clauses. The code for the catch clauses following the same
804 // try is similar to a switch statement. The catch clause action record
805 // informs the runtime about the type of a catch clause and about the
806 // associated switch value.
807 //
808 // Action Record Fields:
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000809 //
Bill Wendlinga583c552009-08-20 22:02:24 +0000810 // * Filter Value
811 // Positive value, starting at 1. Index in the types table of the
812 // __typeinfo for the catch-clause type. 1 is the first word preceding
813 // TTBase, 2 is the second word, and so on. Used by the runtime to check
814 // if the thrown exception type matches the catch-clause type. Back-end
815 // generated switch statements check against this value.
816 //
817 // * Next
818 // Signed offset, in bytes from the start of this field, to the next
819 // chained action record, or zero if none.
820 //
821 // The order of the action records determined by the next field is the order
822 // of the catch clauses as they appear in the source code, and must be kept in
823 // the same order. As a result, changing the order of the catch clause would
824 // change the semantics of the program.
Bill Wendling5cff4872009-07-28 23:44:43 +0000825 for (std::vector<GlobalVariable *>::const_reverse_iterator
826 I = TypeInfos.rbegin(), E = TypeInfos.rend(); I != E; ++I) {
Bill Wendlinga583c552009-08-20 22:02:24 +0000827 const GlobalVariable *GV = *I;
Bill Wendlingeb907212009-05-15 01:12:28 +0000828 PrintRelDirective();
829
830 if (GV) {
831 std::string GLN;
832 O << Asm->getGlobalLinkName(GV, GLN);
833 } else {
Bill Wendling049e98d2009-08-31 18:26:48 +0000834 O << "0x0";
Bill Wendlingeb907212009-05-15 01:12:28 +0000835 }
836
837 Asm->EOL("TypeInfo");
838 }
839
Bill Wendlinga583c552009-08-20 22:02:24 +0000840 // Emit the Type Table.
Bill Wendling5cff4872009-07-28 23:44:43 +0000841 for (std::vector<unsigned>::const_iterator
842 I = FilterIds.begin(), E = FilterIds.end(); I < E; ++I) {
843 unsigned TypeID = *I;
Bill Wendlingeb907212009-05-15 01:12:28 +0000844 Asm->EmitULEB128Bytes(TypeID);
845 Asm->EOL("Filter TypeInfo index");
846 }
847
848 Asm->EmitAlignment(2, 0, 0, false);
849}
850
Bill Wendlingeb907212009-05-15 01:12:28 +0000851/// EndModule - Emit all exception information that should come after the
852/// content.
853void DwarfException::EndModule() {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000854 if (MAI->getExceptionHandlingType() != ExceptionHandling::Dwarf)
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000855 return;
Bill Wendlingb4049fe2009-09-09 21:06:24 +0000856
Bill Wendlingeb907212009-05-15 01:12:28 +0000857 if (TimePassesIsEnabled)
858 ExceptionTimer->startTimer();
859
860 if (shouldEmitMovesModule || shouldEmitTableModule) {
861 const std::vector<Function *> Personalities = MMI->getPersonalities();
Bill Wendlingb4049fe2009-09-09 21:06:24 +0000862
863 for (unsigned i = 0, e = Personalities.size(); i < e; ++i)
Bill Wendling7ccda0f2009-08-25 08:08:33 +0000864 EmitCIE(Personalities[i], i);
Bill Wendlingeb907212009-05-15 01:12:28 +0000865
Bill Wendlingb4049fe2009-09-09 21:06:24 +0000866 for (std::vector<FunctionEHFrameInfo>::iterator
867 I = EHFrames.begin(), E = EHFrames.end(); I != E; ++I)
Bill Wendling7ccda0f2009-08-25 08:08:33 +0000868 EmitFDE(*I);
Bill Wendlingeb907212009-05-15 01:12:28 +0000869 }
870
871 if (TimePassesIsEnabled)
872 ExceptionTimer->stopTimer();
873}
874
875/// BeginFunction - Gather pre-function exception information. Assumes being
876/// emitted immediately after the function entry point.
877void DwarfException::BeginFunction(MachineFunction *MF) {
878 if (TimePassesIsEnabled)
879 ExceptionTimer->startTimer();
880
881 this->MF = MF;
882 shouldEmitTable = shouldEmitMoves = false;
883
Chris Lattner33adcfb2009-08-22 21:43:10 +0000884 if (MMI && MAI->doesSupportExceptionHandling()) {
Bill Wendlingeb907212009-05-15 01:12:28 +0000885 // Map all labels and get rid of any dead landing pads.
886 MMI->TidyLandingPads();
887
888 // If any landing pads survive, we need an EH table.
Bill Wendlingb4049fe2009-09-09 21:06:24 +0000889 if (!MMI->getLandingPads().empty())
Bill Wendlingeb907212009-05-15 01:12:28 +0000890 shouldEmitTable = true;
891
892 // See if we need frame move info.
893 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
894 shouldEmitMoves = true;
895
896 if (shouldEmitMoves || shouldEmitTable)
897 // Assumes in correct section after the entry point.
898 EmitLabel("eh_func_begin", ++SubprogramCount);
899 }
900
901 shouldEmitTableModule |= shouldEmitTable;
902 shouldEmitMovesModule |= shouldEmitMoves;
903
904 if (TimePassesIsEnabled)
905 ExceptionTimer->stopTimer();
906}
907
908/// EndFunction - Gather and emit post-function exception information.
909///
910void DwarfException::EndFunction() {
Bill Wendling7b09a6c2009-09-09 21:08:12 +0000911 if (!shouldEmitMoves && !shouldEmitTable) return;
912
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000913 if (TimePassesIsEnabled)
Bill Wendlingeb907212009-05-15 01:12:28 +0000914 ExceptionTimer->startTimer();
915
Bill Wendling7b09a6c2009-09-09 21:08:12 +0000916 EmitLabel("eh_func_end", SubprogramCount);
917 EmitExceptionTable();
Bill Wendlingeb907212009-05-15 01:12:28 +0000918
Bill Wendling7b09a6c2009-09-09 21:08:12 +0000919 // Save EH frame information
920 EHFrames.push_back(FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF),
921 SubprogramCount,
922 MMI->getPersonalityIndex(),
923 MF->getFrameInfo()->hasCalls(),
924 !MMI->getLandingPads().empty(),
925 MMI->getFrameMoves(),
926 MF->getFunction()));
Bill Wendlingeb907212009-05-15 01:12:28 +0000927
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000928 if (TimePassesIsEnabled)
Bill Wendlingeb907212009-05-15 01:12:28 +0000929 ExceptionTimer->stopTimer();
930}