blob: a8d8bfd7590e42f3355a719fac9832cc02c50e95 [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"
28#include "llvm/Support/Timer.h"
29#include "llvm/Support/raw_ostream.h"
Bill Wendlingeb907212009-05-15 01:12:28 +000030#include "llvm/ADT/StringExtras.h"
31using namespace llvm;
32
33static TimerGroup &getDwarfTimerGroup() {
34 static TimerGroup DwarfTimerGroup("Dwarf Exception");
35 return DwarfTimerGroup;
36}
37
Bill Wendlingbc0d23a2009-05-15 01:18:50 +000038DwarfException::DwarfException(raw_ostream &OS, AsmPrinter *A,
Chris Lattneraf76e592009-08-22 20:48:53 +000039 const MCAsmInfo *T)
Bill Wendlingbc0d23a2009-05-15 01:18:50 +000040 : Dwarf(OS, A, T, "eh"), shouldEmitTable(false), shouldEmitMoves(false),
41 shouldEmitTableModule(false), shouldEmitMovesModule(false),
42 ExceptionTimer(0) {
Eric Christopherdbfcdb92009-08-28 22:33:43 +000043 if (TimePassesIsEnabled)
Bill Wendlingbc0d23a2009-05-15 01:18:50 +000044 ExceptionTimer = new Timer("Dwarf Exception Writer",
45 getDwarfTimerGroup());
46}
47
48DwarfException::~DwarfException() {
49 delete ExceptionTimer;
50}
51
Bill Wendling7ccda0f2009-08-25 08:08:33 +000052/// EmitCIE - Emit a Common Information Entry (CIE). This holds information that
53/// is shared among many Frame Description Entries. There is at least one CIE
54/// in every non-empty .debug_frame section.
55void DwarfException::EmitCIE(const Function *Personality, unsigned Index) {
Bill Wendlingeb907212009-05-15 01:12:28 +000056 // Size and sign of stack growth.
57 int stackGrowth =
58 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
59 TargetFrameInfo::StackGrowsUp ?
60 TD->getPointerSize() : -TD->getPointerSize();
61
62 // Begin eh frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +000063 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getEHFrameSection());
Bill Wendlingeb907212009-05-15 01:12:28 +000064
Chris Lattner33adcfb2009-08-22 21:43:10 +000065 if (MAI->is_EHSymbolPrivate())
66 O << MAI->getPrivateGlobalPrefix();
Bill Wendlingeb907212009-05-15 01:12:28 +000067
68 O << "EH_frame" << Index << ":\n";
69 EmitLabel("section_eh_frame", Index);
70
71 // Define base labels.
72 EmitLabel("eh_frame_common", Index);
73
74 // Define the eh frame length.
75 EmitDifference("eh_frame_common_end", Index,
76 "eh_frame_common_begin", Index, true);
77 Asm->EOL("Length of Common Information Entry");
78
79 // EH frame header.
80 EmitLabel("eh_frame_common_begin", Index);
81 Asm->EmitInt32((int)0);
82 Asm->EOL("CIE Identifier Tag");
83 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
84 Asm->EOL("CIE Version");
85
86 // The personality presence indicates that language specific information will
87 // show up in the eh frame.
88 Asm->EmitString(Personality ? "zPLR" : "zR");
89 Asm->EOL("CIE Augmentation");
90
91 // Round out reader.
92 Asm->EmitULEB128Bytes(1);
93 Asm->EOL("CIE Code Alignment Factor");
94 Asm->EmitSLEB128Bytes(stackGrowth);
95 Asm->EOL("CIE Data Alignment Factor");
96 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
97 Asm->EOL("CIE Return Address Column");
98
Bill Wendling4bda11f2009-08-25 02:32:05 +000099 // If there is a personality, we need to indicate the function's location.
Bill Wendlingeb907212009-05-15 01:12:28 +0000100 if (Personality) {
101 Asm->EmitULEB128Bytes(7);
102 Asm->EOL("Augmentation Size");
103
Chris Lattner33adcfb2009-08-22 21:43:10 +0000104 if (MAI->getNeedsIndirectEncoding()) {
Bill Wendling639217c2009-08-27 03:32:50 +0000105 Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4 |
106 dwarf::DW_EH_PE_indirect);
107 Asm->EOL("Personality (pcrel sdata4 indirect)");
Bill Wendlingeb907212009-05-15 01:12:28 +0000108 } else {
Bill Wendling639217c2009-08-27 03:32:50 +0000109 Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
110 Asm->EOL("Personality (pcrel sdata4)");
Bill Wendlingeb907212009-05-15 01:12:28 +0000111 }
112
113 PrintRelDirective(true);
Chris Lattner33adcfb2009-08-22 21:43:10 +0000114 O << MAI->getPersonalityPrefix();
Bill Wendlingeb907212009-05-15 01:12:28 +0000115 Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
Chris Lattner33adcfb2009-08-22 21:43:10 +0000116 O << MAI->getPersonalitySuffix();
117 if (strcmp(MAI->getPersonalitySuffix(), "+4@GOTPCREL"))
118 O << "-" << MAI->getPCSymbol();
Bill Wendlingeb907212009-05-15 01:12:28 +0000119 Asm->EOL("Personality");
120
Bill Wendling639217c2009-08-27 03:32:50 +0000121 Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
122 Asm->EOL("LSDA Encoding (pcrel sdata4)");
Bill Wendlingeb907212009-05-15 01:12:28 +0000123
Bill Wendling639217c2009-08-27 03:32:50 +0000124 Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
125 Asm->EOL("FDE Encoding (pcrel sdata4)");
Bill Wendlingeb907212009-05-15 01:12:28 +0000126 } else {
127 Asm->EmitULEB128Bytes(1);
128 Asm->EOL("Augmentation Size");
129
Bill Wendling639217c2009-08-27 03:32:50 +0000130 Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
131 Asm->EOL("FDE Encoding (pcrel sdata4)");
Bill Wendlingeb907212009-05-15 01:12:28 +0000132 }
133
134 // Indicate locations of general callee saved registers in frame.
135 std::vector<MachineMove> Moves;
136 RI->getInitialFrameState(Moves);
137 EmitFrameMoves(NULL, 0, Moves, true);
138
139 // On Darwin the linker honors the alignment of eh_frame, which means it must
140 // be 8-byte on 64-bit targets to match what gcc does. Otherwise you get
141 // holes which confuse readers of eh_frame.
142 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
143 0, 0, false);
144 EmitLabel("eh_frame_common_end", Index);
145
146 Asm->EOL();
147}
148
Bill Wendling7ccda0f2009-08-25 08:08:33 +0000149/// EmitFDE - Emit the Frame Description Entry (FDE) for the function.
150void DwarfException::EmitFDE(const FunctionEHFrameInfo &EHFrameInfo) {
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000151 assert(!EHFrameInfo.function->hasAvailableExternallyLinkage() &&
Bill Wendlingeb907212009-05-15 01:12:28 +0000152 "Should not emit 'available externally' functions at all");
153
Chris Lattner3e0f60b2009-07-17 21:00:50 +0000154 const Function *TheFunc = EHFrameInfo.function;
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000155
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000156 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getEHFrameSection());
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000157
Bill Wendlingeb907212009-05-15 01:12:28 +0000158 // Externally visible entry into the functions eh frame info. If the
159 // corresponding function is static, this should not be externally visible.
Chris Lattner3e0f60b2009-07-17 21:00:50 +0000160 if (!TheFunc->hasLocalLinkage())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000161 if (const char *GlobalEHDirective = MAI->getGlobalEHDirective())
Bill Wendlingeb907212009-05-15 01:12:28 +0000162 O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
Bill Wendlingeb907212009-05-15 01:12:28 +0000163
164 // If corresponding function is weak definition, this should be too.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000165 if (TheFunc->isWeakForLinker() && MAI->getWeakDefDirective())
166 O << MAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
Bill Wendlingeb907212009-05-15 01:12:28 +0000167
168 // If there are no calls then you can't unwind. This may mean we can omit the
169 // EH Frame, but some environments do not handle weak absolute symbols. If
170 // UnwindTablesMandatory is set we cannot do this optimization; the unwind
171 // info is to be available for non-EH uses.
Chris Lattner3e0f60b2009-07-17 21:00:50 +0000172 if (!EHFrameInfo.hasCalls && !UnwindTablesMandatory &&
173 (!TheFunc->isWeakForLinker() ||
Chris Lattner33adcfb2009-08-22 21:43:10 +0000174 !MAI->getWeakDefDirective() ||
175 MAI->getSupportsWeakOmittedEHFrame())) {
Bill Wendlingeb907212009-05-15 01:12:28 +0000176 O << EHFrameInfo.FnName << " = 0\n";
177 // This name has no connection to the function, so it might get
178 // dead-stripped when the function is not, erroneously. Prohibit
179 // dead-stripping unconditionally.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000180 if (const char *UsedDirective = MAI->getUsedDirective())
Bill Wendlingeb907212009-05-15 01:12:28 +0000181 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
182 } else {
183 O << EHFrameInfo.FnName << ":\n";
184
185 // EH frame header.
186 EmitDifference("eh_frame_end", EHFrameInfo.Number,
187 "eh_frame_begin", EHFrameInfo.Number, true);
188 Asm->EOL("Length of Frame Information Entry");
189
190 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
191
Chris Lattnera4ff5e42009-07-17 20:53:51 +0000192 EmitSectionOffset("eh_frame_begin", "eh_frame_common",
193 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
194 true, true, false);
Bill Wendlingeb907212009-05-15 01:12:28 +0000195
196 Asm->EOL("FDE CIE offset");
197
198 EmitReference("eh_func_begin", EHFrameInfo.Number, true, true);
199 Asm->EOL("FDE initial location");
200 EmitDifference("eh_func_end", EHFrameInfo.Number,
201 "eh_func_begin", EHFrameInfo.Number, true);
202 Asm->EOL("FDE address range");
203
204 // If there is a personality and landing pads then point to the language
205 // specific data area in the exception table.
Eric Christopherd44fff72009-08-26 21:30:49 +0000206 if (MMI->getPersonalities()[0] != NULL) {
Eric Christopher6fefceb2009-08-29 01:12:46 +0000207 bool is4Byte = TD->getPointerSize() == sizeof(int32_t);
208
209 Asm->EmitULEB128Bytes(is4Byte ? 4 : 8);
Bill Wendlingeb907212009-05-15 01:12:28 +0000210 Asm->EOL("Augmentation size");
211
212 if (EHFrameInfo.hasLandingPads)
Eric Christopher6fefceb2009-08-29 01:12:46 +0000213 EmitReference("exception", EHFrameInfo.Number, true, false);
214 else {
215 if (is4Byte)
216 Asm->EmitInt32((int)0);
217 else
218 Asm->EmitInt64((int)0);
219 }
Bill Wendlingeb907212009-05-15 01:12:28 +0000220 Asm->EOL("Language Specific Data Area");
221 } else {
222 Asm->EmitULEB128Bytes(0);
223 Asm->EOL("Augmentation size");
224 }
225
226 // Indicate locations of function specific callee saved registers in frame.
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000227 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves,
Bill Wendlingeb907212009-05-15 01:12:28 +0000228 true);
229
230 // On Darwin the linker honors the alignment of eh_frame, which means it
231 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise you
232 // get holes which confuse readers of eh_frame.
233 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
234 0, 0, false);
235 EmitLabel("eh_frame_end", EHFrameInfo.Number);
236
237 // If the function is marked used, this table should be also. We cannot
238 // make the mark unconditional in this case, since retaining the table also
239 // retains the function in this case, and there is code around that depends
240 // on unused functions (calling undefined externals) being dead-stripped to
241 // link correctly. Yes, there really is.
Chris Lattner401e10c2009-07-20 06:14:25 +0000242 if (MMI->isUsedFunction(EHFrameInfo.function))
Chris Lattner33adcfb2009-08-22 21:43:10 +0000243 if (const char *UsedDirective = MAI->getUsedDirective())
Bill Wendlingeb907212009-05-15 01:12:28 +0000244 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
245 }
Bill Wendling4bda11f2009-08-25 02:32:05 +0000246
247 Asm->EOL();
Bill Wendlingeb907212009-05-15 01:12:28 +0000248}
249
Bill Wendlingeb907212009-05-15 01:12:28 +0000250/// SharedTypeIds - How many leading type ids two landing pads have in common.
251unsigned DwarfException::SharedTypeIds(const LandingPadInfo *L,
252 const LandingPadInfo *R) {
253 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
254 unsigned LSize = LIds.size(), RSize = RIds.size();
255 unsigned MinSize = LSize < RSize ? LSize : RSize;
256 unsigned Count = 0;
257
258 for (; Count != MinSize; ++Count)
259 if (LIds[Count] != RIds[Count])
260 return Count;
261
262 return Count;
263}
264
265/// PadLT - Order landing pads lexicographically by type id.
266bool DwarfException::PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
267 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
268 unsigned LSize = LIds.size(), RSize = RIds.size();
269 unsigned MinSize = LSize < RSize ? LSize : RSize;
270
271 for (unsigned i = 0; i != MinSize; ++i)
272 if (LIds[i] != RIds[i])
273 return LIds[i] < RIds[i];
274
275 return LSize < RSize;
276}
277
Bill Wendlingd4609622009-07-28 23:23:00 +0000278/// ComputeActionsTable - Compute the actions table and gather the first action
279/// index for each landing pad site.
Bill Wendlingade025c2009-07-29 00:31:35 +0000280unsigned DwarfException::
281ComputeActionsTable(const SmallVectorImpl<const LandingPadInfo*> &LandingPads,
282 SmallVectorImpl<ActionEntry> &Actions,
283 SmallVectorImpl<unsigned> &FirstActions) {
Bill Wendlinga583c552009-08-20 22:02:24 +0000284
285 // The action table follows the call-site table in the LSDA. The individual
286 // records are of two types:
287 //
288 // * Catch clause
289 // * Exception specification
290 //
291 // The two record kinds have the same format, with only small differences.
292 // They are distinguished by the "switch value" field: Catch clauses
293 // (TypeInfos) have strictly positive switch values, and exception
294 // specifications (FilterIds) have strictly negative switch values. Value 0
295 // indicates a catch-all clause.
296 //
Bill Wendling5e953dd2009-07-28 23:22:13 +0000297 // Negative type IDs index into FilterIds. Positive type IDs index into
298 // TypeInfos. The value written for a positive type ID is just the type ID
299 // itself. For a negative type ID, however, the value written is the
Bill Wendlingeb907212009-05-15 01:12:28 +0000300 // (negative) byte offset of the corresponding FilterIds entry. The byte
Bill Wendling5e953dd2009-07-28 23:22:13 +0000301 // offset is usually equal to the type ID (because the FilterIds entries are
302 // written using a variable width encoding, which outputs one byte per entry
303 // as long as the value written is not too large) but can differ. This kind
304 // of complication does not occur for positive type IDs because type infos are
Bill Wendlingeb907212009-05-15 01:12:28 +0000305 // output using a fixed width encoding. FilterOffsets[i] holds the byte
306 // offset corresponding to FilterIds[i].
Bill Wendling409914b2009-07-29 21:19:44 +0000307
308 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
Bill Wendlingeb907212009-05-15 01:12:28 +0000309 SmallVector<int, 16> FilterOffsets;
310 FilterOffsets.reserve(FilterIds.size());
311 int Offset = -1;
Bill Wendling409914b2009-07-29 21:19:44 +0000312
313 for (std::vector<unsigned>::const_iterator
314 I = FilterIds.begin(), E = FilterIds.end(); I != E; ++I) {
Bill Wendlingeb907212009-05-15 01:12:28 +0000315 FilterOffsets.push_back(Offset);
Chris Lattneraf76e592009-08-22 20:48:53 +0000316 Offset -= MCAsmInfo::getULEB128Size(*I);
Bill Wendlingeb907212009-05-15 01:12:28 +0000317 }
318
Bill Wendlingeb907212009-05-15 01:12:28 +0000319 FirstActions.reserve(LandingPads.size());
320
321 int FirstAction = 0;
322 unsigned SizeActions = 0;
Bill Wendling5e953dd2009-07-28 23:22:13 +0000323 const LandingPadInfo *PrevLPI = 0;
Bill Wendling409914b2009-07-29 21:19:44 +0000324
Bill Wendling5cff4872009-07-28 23:44:43 +0000325 for (SmallVectorImpl<const LandingPadInfo *>::const_iterator
Bill Wendling5e953dd2009-07-28 23:22:13 +0000326 I = LandingPads.begin(), E = LandingPads.end(); I != E; ++I) {
327 const LandingPadInfo *LPI = *I;
328 const std::vector<int> &TypeIds = LPI->TypeIds;
329 const unsigned NumShared = PrevLPI ? SharedTypeIds(LPI, PrevLPI) : 0;
Bill Wendlingeb907212009-05-15 01:12:28 +0000330 unsigned SizeSiteActions = 0;
331
332 if (NumShared < TypeIds.size()) {
333 unsigned SizeAction = 0;
334 ActionEntry *PrevAction = 0;
335
336 if (NumShared) {
Bill Wendling5e953dd2009-07-28 23:22:13 +0000337 const unsigned SizePrevIds = PrevLPI->TypeIds.size();
Bill Wendlingeb907212009-05-15 01:12:28 +0000338 assert(Actions.size());
339 PrevAction = &Actions.back();
Chris Lattneraf76e592009-08-22 20:48:53 +0000340 SizeAction = MCAsmInfo::getSLEB128Size(PrevAction->NextAction) +
341 MCAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Bill Wendlingeb907212009-05-15 01:12:28 +0000342
343 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
344 SizeAction -=
Chris Lattneraf76e592009-08-22 20:48:53 +0000345 MCAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Bill Wendlingeb907212009-05-15 01:12:28 +0000346 SizeAction += -PrevAction->NextAction;
347 PrevAction = PrevAction->Previous;
348 }
349 }
350
351 // Compute the actions.
Bill Wendling5e953dd2009-07-28 23:22:13 +0000352 for (unsigned J = NumShared, M = TypeIds.size(); J != M; ++J) {
353 int TypeID = TypeIds[J];
354 assert(-1 - TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
Bill Wendlingeb907212009-05-15 01:12:28 +0000355 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
Chris Lattneraf76e592009-08-22 20:48:53 +0000356 unsigned SizeTypeID = MCAsmInfo::getSLEB128Size(ValueForTypeID);
Bill Wendlingeb907212009-05-15 01:12:28 +0000357
358 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
Chris Lattneraf76e592009-08-22 20:48:53 +0000359 SizeAction = SizeTypeID + MCAsmInfo::getSLEB128Size(NextAction);
Bill Wendlingeb907212009-05-15 01:12:28 +0000360 SizeSiteActions += SizeAction;
361
Bill Wendlinga583c552009-08-20 22:02:24 +0000362 ActionEntry Action = { ValueForTypeID, NextAction, PrevAction };
Bill Wendlingeb907212009-05-15 01:12:28 +0000363 Actions.push_back(Action);
Bill Wendlingeb907212009-05-15 01:12:28 +0000364 PrevAction = &Actions.back();
365 }
366
367 // Record the first action of the landing pad site.
368 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
369 } // else identical - re-use previous FirstAction
370
Bill Wendlinga583c552009-08-20 22:02:24 +0000371 // Information used when created the call-site table. The action record
372 // field of the call site record is the offset of the first associated
373 // action record, relative to the start of the actions table. This value is
374 // biased by 1 (1 in dicating the start of the actions table), and 0
375 // indicates that there are no actions.
Bill Wendlingeb907212009-05-15 01:12:28 +0000376 FirstActions.push_back(FirstAction);
377
378 // Compute this sites contribution to size.
379 SizeActions += SizeSiteActions;
Bill Wendling5e953dd2009-07-28 23:22:13 +0000380
381 PrevLPI = LPI;
Bill Wendlingeb907212009-05-15 01:12:28 +0000382 }
383
Bill Wendling5e953dd2009-07-28 23:22:13 +0000384 return SizeActions;
385}
386
Bill Wendlingade025c2009-07-29 00:31:35 +0000387/// ComputeCallSiteTable - Compute the call-site table. The entry for an invoke
Bill Wendlinga583c552009-08-20 22:02:24 +0000388/// has a try-range containing the call, a non-zero landing pad, and an
Bill Wendlingade025c2009-07-29 00:31:35 +0000389/// appropriate action. The entry for an ordinary call has a try-range
390/// containing the call and zero for the landing pad and the action. Calls
391/// marked 'nounwind' have no entry and must not be contained in the try-range
392/// of any entry - they form gaps in the table. Entries must be ordered by
393/// try-range address.
394void DwarfException::
395ComputeCallSiteTable(SmallVectorImpl<CallSiteEntry> &CallSites,
396 const RangeMapType &PadMap,
397 const SmallVectorImpl<const LandingPadInfo *> &LandingPads,
398 const SmallVectorImpl<unsigned> &FirstActions) {
Bill Wendlingeb907212009-05-15 01:12:28 +0000399 // The end label of the previous invoke or nounwind try-range.
400 unsigned LastLabel = 0;
401
402 // Whether there is a potentially throwing instruction (currently this means
403 // an ordinary call) between the end of the previous try-range and now.
404 bool SawPotentiallyThrowing = false;
405
Bill Wendling5cff4872009-07-28 23:44:43 +0000406 // Whether the last CallSite entry was for an invoke.
Bill Wendlingeb907212009-05-15 01:12:28 +0000407 bool PreviousIsInvoke = false;
408
409 // Visit all instructions in order of address.
410 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
411 I != E; ++I) {
412 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
413 MI != E; ++MI) {
414 if (!MI->isLabel()) {
415 SawPotentiallyThrowing |= MI->getDesc().isCall();
416 continue;
417 }
418
419 unsigned BeginLabel = MI->getOperand(0).getImm();
420 assert(BeginLabel && "Invalid label!");
421
422 // End of the previous try-range?
423 if (BeginLabel == LastLabel)
424 SawPotentiallyThrowing = false;
425
426 // Beginning of a new try-range?
427 RangeMapType::iterator L = PadMap.find(BeginLabel);
428 if (L == PadMap.end())
429 // Nope, it was just some random label.
430 continue;
431
Bill Wendlinga583c552009-08-20 22:02:24 +0000432 const PadRange &P = L->second;
Bill Wendlingeb907212009-05-15 01:12:28 +0000433 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
Bill Wendlingeb907212009-05-15 01:12:28 +0000434 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
435 "Inconsistent landing pad map!");
436
Bill Wendlinga583c552009-08-20 22:02:24 +0000437 // For Dwarf exception handling (SjLj handling doesn't use this). If some
438 // instruction between the previous try-range and this one may throw,
439 // create a call-site entry with no landing pad for the region between the
440 // try-ranges.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000441 if (SawPotentiallyThrowing &&
Chris Lattner33adcfb2009-08-22 21:43:10 +0000442 MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) {
Bill Wendlinga583c552009-08-20 22:02:24 +0000443 CallSiteEntry Site = { LastLabel, BeginLabel, 0, 0 };
Bill Wendlingeb907212009-05-15 01:12:28 +0000444 CallSites.push_back(Site);
445 PreviousIsInvoke = false;
446 }
447
448 LastLabel = LandingPad->EndLabels[P.RangeIndex];
449 assert(BeginLabel && LastLabel && "Invalid landing pad!");
450
451 if (LandingPad->LandingPadLabel) {
452 // This try-range is for an invoke.
Bill Wendlinga583c552009-08-20 22:02:24 +0000453 CallSiteEntry Site = {
454 BeginLabel,
455 LastLabel,
456 LandingPad->LandingPadLabel,
457 FirstActions[P.PadIndex]
458 };
Bill Wendlingeb907212009-05-15 01:12:28 +0000459
460 // Try to merge with the previous call-site.
461 if (PreviousIsInvoke) {
462 CallSiteEntry &Prev = CallSites.back();
463 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
464 // Extend the range of the previous entry.
465 Prev.EndLabel = Site.EndLabel;
466 continue;
467 }
468 }
469
470 // Otherwise, create a new call-site.
471 CallSites.push_back(Site);
472 PreviousIsInvoke = true;
473 } else {
474 // Create a gap.
475 PreviousIsInvoke = false;
476 }
477 }
478 }
479
480 // If some instruction between the previous try-range and the end of the
481 // function may throw, create a call-site entry with no landing pad for the
482 // region following the try-range.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000483 if (SawPotentiallyThrowing &&
Chris Lattner33adcfb2009-08-22 21:43:10 +0000484 MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) {
Bill Wendlinga583c552009-08-20 22:02:24 +0000485 CallSiteEntry Site = { LastLabel, 0, 0, 0 };
Bill Wendlingeb907212009-05-15 01:12:28 +0000486 CallSites.push_back(Site);
487 }
Bill Wendlingade025c2009-07-29 00:31:35 +0000488}
489
Bill Wendling0dafca92009-07-29 00:50:05 +0000490/// EmitExceptionTable - Emit landing pads and actions.
491///
492/// The general organization of the table is complex, but the basic concepts are
493/// easy. First there is a header which describes the location and organization
494/// of the three components that follow.
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000495///
Bill Wendling0dafca92009-07-29 00:50:05 +0000496/// 1. The landing pad site information describes the range of code covered by
497/// the try. In our case it's an accumulation of the ranges covered by the
498/// invokes in the try. There is also a reference to the landing pad that
499/// handles the exception once processed. Finally an index into the actions
500/// table.
Bill Wendlinga583c552009-08-20 22:02:24 +0000501/// 2. The action table, in our case, is composed of pairs of type IDs and next
Bill Wendling0dafca92009-07-29 00:50:05 +0000502/// action offset. Starting with the action index from the landing pad
Bill Wendlinga583c552009-08-20 22:02:24 +0000503/// site, each type ID is checked for a match to the current exception. If
Bill Wendling0dafca92009-07-29 00:50:05 +0000504/// it matches then the exception and type id are passed on to the landing
505/// pad. Otherwise the next action is looked up. This chain is terminated
506/// with a next action of zero. If no type id is found the the frame is
507/// unwound and handling continues.
Bill Wendlinga583c552009-08-20 22:02:24 +0000508/// 3. Type ID table contains references to all the C++ typeinfo for all
Bill Wendling0dafca92009-07-29 00:50:05 +0000509/// catches in the function. This tables is reversed indexed base 1.
Bill Wendlingade025c2009-07-29 00:31:35 +0000510void DwarfException::EmitExceptionTable() {
511 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
512 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
513 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
514 if (PadInfos.empty()) return;
515
516 // Sort the landing pads in order of their type ids. This is used to fold
517 // duplicate actions.
518 SmallVector<const LandingPadInfo *, 64> LandingPads;
519 LandingPads.reserve(PadInfos.size());
520
521 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
522 LandingPads.push_back(&PadInfos[i]);
523
524 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
525
526 // Compute the actions table and gather the first action index for each
527 // landing pad site.
528 SmallVector<ActionEntry, 32> Actions;
529 SmallVector<unsigned, 64> FirstActions;
530 unsigned SizeActions = ComputeActionsTable(LandingPads, Actions, FirstActions);
531
532 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
533 // by try-range labels when lowered). Ordinary calls do not, so appropriate
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000534 // try-ranges for them need be deduced when using Dwarf exception handling.
Bill Wendlingade025c2009-07-29 00:31:35 +0000535 RangeMapType PadMap;
536 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
537 const LandingPadInfo *LandingPad = LandingPads[i];
538 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
539 unsigned BeginLabel = LandingPad->BeginLabels[j];
540 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
541 PadRange P = { i, j };
542 PadMap[BeginLabel] = P;
543 }
544 }
545
546 // Compute the call-site table.
547 SmallVector<CallSiteEntry, 64> CallSites;
Jim Grosbach8b818d72009-08-17 16:41:22 +0000548 ComputeCallSiteTable(CallSites, PadMap, LandingPads, FirstActions);
Bill Wendlingeb907212009-05-15 01:12:28 +0000549
550 // Final tallies.
551
552 // Call sites.
553 const unsigned SiteStartSize = sizeof(int32_t); // DW_EH_PE_udata4
554 const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4
555 const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000556 unsigned SizeSites;
Jim Grosbachbff39232009-08-12 17:38:44 +0000557
Chris Lattner33adcfb2009-08-22 21:43:10 +0000558 bool HaveTTData = (MAI->getExceptionHandlingType() == ExceptionHandling::SjLj)
Jim Grosbachbff39232009-08-12 17:38:44 +0000559 ? (!TypeInfos.empty() || !FilterIds.empty()) : true;
560
561
Chris Lattner33adcfb2009-08-22 21:43:10 +0000562 if (MAI->getExceptionHandlingType() == ExceptionHandling::SjLj) {
Jim Grosbach8b818d72009-08-17 16:41:22 +0000563 SizeSites = 0;
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000564 } else
565 SizeSites = CallSites.size() *
566 (SiteStartSize + SiteLengthSize + LandingPadSize);
567 for (unsigned i = 0, e = CallSites.size(); i < e; ++i) {
Chris Lattneraf76e592009-08-22 20:48:53 +0000568 SizeSites += MCAsmInfo::getULEB128Size(CallSites[i].Action);
Chris Lattner33adcfb2009-08-22 21:43:10 +0000569 if (MAI->getExceptionHandlingType() == ExceptionHandling::SjLj)
Chris Lattneraf76e592009-08-22 20:48:53 +0000570 SizeSites += MCAsmInfo::getULEB128Size(i);
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000571 }
Bill Wendlingeb907212009-05-15 01:12:28 +0000572 // Type infos.
573 const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
574 unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
575
576 unsigned TypeOffset = sizeof(int8_t) + // Call site format
Chris Lattneraf76e592009-08-22 20:48:53 +0000577 MCAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
Bill Wendlingeb907212009-05-15 01:12:28 +0000578 SizeSites + SizeActions + SizeTypes;
579
580 unsigned TotalSize = sizeof(int8_t) + // LPStart format
581 sizeof(int8_t) + // TType format
Jim Grosbachbff39232009-08-12 17:38:44 +0000582 (HaveTTData ?
Chris Lattneraf76e592009-08-22 20:48:53 +0000583 MCAsmInfo::getULEB128Size(TypeOffset) : 0) + // TType base offset
Bill Wendlingeb907212009-05-15 01:12:28 +0000584 TypeOffset;
585
586 unsigned SizeAlign = (4 - TotalSize) & 3;
587
588 // Begin the exception table.
Chris Lattnerd5bbb072009-08-02 01:34:32 +0000589 const MCSection *LSDASection = Asm->getObjFileLowering().getLSDASection();
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000590 Asm->OutStreamer.SwitchSection(LSDASection);
Bill Wendlingeb907212009-05-15 01:12:28 +0000591 Asm->EmitAlignment(2, 0, 0, false);
592 O << "GCC_except_table" << SubprogramCount << ":\n";
593
594 for (unsigned i = 0; i != SizeAlign; ++i) {
595 Asm->EmitInt8(0);
596 Asm->EOL("Padding");
Bill Wendlingc5800a82009-07-28 21:54:03 +0000597 }
Bill Wendlingeb907212009-05-15 01:12:28 +0000598
599 EmitLabel("exception", SubprogramCount);
Chris Lattner33adcfb2009-08-22 21:43:10 +0000600 if (MAI->getExceptionHandlingType() == ExceptionHandling::SjLj) {
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000601 std::string SjLjName = "_lsda_";
602 SjLjName += MF->getFunction()->getName().str();
603 EmitLabel(SjLjName.c_str(), 0);
604 }
Bill Wendlingeb907212009-05-15 01:12:28 +0000605
606 // Emit the header.
Bill Wendling639217c2009-08-27 03:32:50 +0000607 Asm->EmitInt8(dwarf::DW_EH_PE_omit);
608 Asm->EOL("@LPStart format (DW_EH_PE_omit)");
Bill Wendlingb0d9c3e2009-07-28 22:23:45 +0000609
Bill Wendlingade025c2009-07-29 00:31:35 +0000610#if 0
Chris Lattner81c9a062009-07-31 22:03:47 +0000611 if (TypeInfos.empty() && FilterIds.empty()) {
Chris Lattnerad88bc42009-08-02 03:59:56 +0000612 // If there are no typeinfos or filters, there is nothing to emit, optimize
613 // by specifying the "omit" encoding.
Bill Wendling639217c2009-08-27 03:32:50 +0000614 Asm->EmitInt8(dwarf::DW_EH_PE_omit);
615 Asm->EOL("@TType format (DW_EH_PE_omit)");
Chris Lattner81c9a062009-07-31 22:03:47 +0000616 } else {
Chris Lattnerad88bc42009-08-02 03:59:56 +0000617 // Okay, we have actual filters or typeinfos to emit. As such, we need to
618 // pick a type encoding for them. We're about to emit a list of pointers to
619 // typeinfo objects at the end of the LSDA. However, unless we're in static
620 // mode, this reference will require a relocation by the dynamic linker.
Chris Lattner46b754c2009-07-31 22:18:14 +0000621 //
Chris Lattnerad88bc42009-08-02 03:59:56 +0000622 // Because of this, we have a couple of options:
623 // 1) If we are in -static mode, we can always use an absolute reference
624 // from the LSDA, because the static linker will resolve it.
625 // 2) Otherwise, if the LSDA section is writable, we can output the direct
626 // reference to the typeinfo and allow the dynamic linker to relocate
627 // it. Since it is in a writable section, the dynamic linker won't
628 // have a problem.
629 // 3) Finally, if we're in PIC mode and the LDSA section isn't writable,
630 // we need to use some form of indirection. For example, on Darwin,
631 // we can output a statically-relocatable reference to a dyld stub. The
632 // offset to the stub is constant, but the contents are in a section
633 // that is updated by the dynamic linker. This is easy enough, but we
634 // need to tell the personality function of the unwinder to indirect
635 // through the dyld stub.
636 //
637 // FIXME: When this is actually implemented, we'll have to emit the stubs
638 // somewhere. This predicate should be moved to a shared location that is
639 // in target-independent code.
640 //
641 if (LSDASection->isWritable() ||
642 Asm->TM.getRelocationModel() == Reloc::Static) {
Bill Wendling639217c2009-08-27 03:32:50 +0000643 Asm->EmitInt8(DW_EH_PE_absptr);
644 Asm->EOL("TType format (DW_EH_PE_absptr)");
Chris Lattnerad88bc42009-08-02 03:59:56 +0000645 } else {
646 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_indirect | DW_EH_PE_sdata4);
Bill Wendling639217c2009-08-27 03:32:50 +0000647 Asm->EOL("TType format (DW_EH_PE_pcrel | DW_EH_PE_indirect"
648 " | DW_EH_PE_sdata4)");
Chris Lattnerad88bc42009-08-02 03:59:56 +0000649 }
Bill Wendlingb0d9c3e2009-07-28 22:23:45 +0000650 Asm->EmitULEB128Bytes(TypeOffset);
651 Asm->EOL("TType base offset");
Bill Wendlingb0d9c3e2009-07-28 22:23:45 +0000652 }
Bill Wendlingade025c2009-07-29 00:31:35 +0000653#else
Jim Grosbachbff39232009-08-12 17:38:44 +0000654 // For SjLj exceptions, if there is no TypeInfo, then we just explicitly
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000655 // say that we're omitting that bit.
656 // FIXME: does this apply to Dwarf also? The above #if 0 implies yes?
Jim Grosbachbff39232009-08-12 17:38:44 +0000657 if (!HaveTTData) {
Bill Wendling639217c2009-08-27 03:32:50 +0000658 Asm->EmitInt8(dwarf::DW_EH_PE_omit);
659 Asm->EOL("@TType format (DW_EH_PE_omit)");
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000660 } else {
Bill Wendling639217c2009-08-27 03:32:50 +0000661 Asm->EmitInt8(dwarf::DW_EH_PE_absptr);
662 Asm->EOL("@TType format (DW_EH_PE_absptr)");
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000663 Asm->EmitULEB128Bytes(TypeOffset);
Bill Wendlinga583c552009-08-20 22:02:24 +0000664 Asm->EOL("@TType base offset");
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000665 }
Bill Wendlingade025c2009-07-29 00:31:35 +0000666#endif
Bill Wendlingb0d9c3e2009-07-28 22:23:45 +0000667
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000668 // SjLj Exception handilng
Chris Lattner33adcfb2009-08-22 21:43:10 +0000669 if (MAI->getExceptionHandlingType() == ExceptionHandling::SjLj) {
Bill Wendling639217c2009-08-27 03:32:50 +0000670 Asm->EmitInt8(dwarf::DW_EH_PE_udata4);
671 Asm->EOL("Call site format (DW_EH_PE_udata4)");
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000672 Asm->EmitULEB128Bytes(SizeSites);
Bill Wendlinga583c552009-08-20 22:02:24 +0000673 Asm->EOL("Call site table length");
Bill Wendlingeb907212009-05-15 01:12:28 +0000674
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000675 // Emit the landing pad site information.
Jim Grosbach8b818d72009-08-17 16:41:22 +0000676 unsigned idx = 0;
677 for (SmallVectorImpl<CallSiteEntry>::const_iterator
678 I = CallSites.begin(), E = CallSites.end(); I != E; ++I, ++idx) {
679 const CallSiteEntry &S = *I;
Bill Wendlinga583c552009-08-20 22:02:24 +0000680
681 // Offset of the landing pad, counted in 16-byte bundles relative to the
682 // @LPStart address.
Jim Grosbach8b818d72009-08-17 16:41:22 +0000683 Asm->EmitULEB128Bytes(idx);
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000684 Asm->EOL("Landing pad");
Bill Wendlinga583c552009-08-20 22:02:24 +0000685
686 // Offset of the first associated action record, relative to the start of
687 // the action table. This value is biased by 1 (1 indicates the start of
688 // the action table), and 0 indicates that there are no actions.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000689 Asm->EmitULEB128Bytes(S.Action);
690 Asm->EOL("Action");
Bill Wendlingeb907212009-05-15 01:12:28 +0000691 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000692 } else {
693 // DWARF Exception handling
Chris Lattner33adcfb2009-08-22 21:43:10 +0000694 assert(MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf);
Bill Wendlingeb907212009-05-15 01:12:28 +0000695
Bill Wendlinga583c552009-08-20 22:02:24 +0000696 // The call-site table is a list of all call sites that may throw an
697 // exception (including C++ 'throw' statements) in the procedure
698 // fragment. It immediately follows the LSDA header. Each entry indicates,
699 // for a given call, the first corresponding action record and corresponding
700 // landing pad.
701 //
702 // The table begins with the number of bytes, stored as an LEB128
703 // compressed, unsigned integer. The records immediately follow the record
704 // count. They are sorted in increasing call-site address. Each record
705 // indicates:
706 //
707 // * The position of the call-site.
708 // * The position of the landing pad.
709 // * The first action record for that call site.
710 //
711 // A missing entry in the call-site table indicates that a call is not
712 // supposed to throw. Such calls include:
713 //
714 // * Calls to destructors within cleanup code. C++ semantics forbids these
715 // calls to throw.
716 // * Calls to intrinsic routines in the standard library which are known
717 // not to throw (sin, memcpy, et al).
718 //
719 // If the runtime does not find the call-site entry for a given call, it
720 // will call `terminate()'.
721
722 // Emit the landing pad call site table.
Bill Wendling639217c2009-08-27 03:32:50 +0000723 Asm->EmitInt8(dwarf::DW_EH_PE_udata4);
724 Asm->EOL("Call site format (DW_EH_PE_udata4)");
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000725 Asm->EmitULEB128Bytes(SizeSites);
Bill Wendlinga583c552009-08-20 22:02:24 +0000726 Asm->EOL("Call site table size");
Bill Wendlingeb907212009-05-15 01:12:28 +0000727
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000728 for (SmallVectorImpl<CallSiteEntry>::const_iterator
729 I = CallSites.begin(), E = CallSites.end(); I != E; ++I) {
730 const CallSiteEntry &S = *I;
731 const char *BeginTag;
732 unsigned BeginNumber;
Bill Wendlingeb907212009-05-15 01:12:28 +0000733
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000734 if (!S.BeginLabel) {
735 BeginTag = "eh_func_begin";
736 BeginNumber = SubprogramCount;
737 } else {
738 BeginTag = "label";
739 BeginNumber = S.BeginLabel;
740 }
Bill Wendlingeb907212009-05-15 01:12:28 +0000741
Bill Wendlinga583c552009-08-20 22:02:24 +0000742 // Offset of the call site relative to the previous call site, counted in
743 // number of 16-byte bundles. The first call site is counted relative to
744 // the start of the procedure fragment.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000745 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
Bill Wendlingeb907212009-05-15 01:12:28 +0000746 true, true);
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000747 Asm->EOL("Region start");
Bill Wendlingeb907212009-05-15 01:12:28 +0000748
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000749 if (!S.EndLabel)
750 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
751 true);
752 else
753 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
Bill Wendlingeb907212009-05-15 01:12:28 +0000754
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000755 Asm->EOL("Region length");
756
Bill Wendlinga583c552009-08-20 22:02:24 +0000757 // Offset of the landing pad, counted in 16-byte bundles relative to the
758 // @LPStart address.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000759 if (!S.PadLabel)
760 Asm->EmitInt32(0);
761 else
762 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
763 true, true);
764
765 Asm->EOL("Landing pad");
766
Bill Wendlinga583c552009-08-20 22:02:24 +0000767 // Offset of the first associated action record, relative to the start of
768 // the action table. This value is biased by 1 (1 indicates the start of
769 // the action table), and 0 indicates that there are no actions.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000770 Asm->EmitULEB128Bytes(S.Action);
771 Asm->EOL("Action");
772 }
Bill Wendlingeb907212009-05-15 01:12:28 +0000773 }
774
Bill Wendlinga583c552009-08-20 22:02:24 +0000775 // Emit the Action Table.
Bill Wendling5cff4872009-07-28 23:44:43 +0000776 for (SmallVectorImpl<ActionEntry>::const_iterator
777 I = Actions.begin(), E = Actions.end(); I != E; ++I) {
778 const ActionEntry &Action = *I;
Bill Wendlinga583c552009-08-20 22:02:24 +0000779
780 // Type Filter
781 //
782 // Used by the runtime to match the type of the thrown exception to the
783 // type of the catch clauses or the types in the exception specification.
784
Bill Wendlingeb907212009-05-15 01:12:28 +0000785 Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
786 Asm->EOL("TypeInfo index");
Bill Wendlinga583c552009-08-20 22:02:24 +0000787
788 // Action Record
789 //
790 // Self-relative signed displacement in bytes of the next action record,
791 // or 0 if there is no next action record.
792
Bill Wendlingeb907212009-05-15 01:12:28 +0000793 Asm->EmitSLEB128Bytes(Action.NextAction);
794 Asm->EOL("Next action");
795 }
796
Bill Wendlinga583c552009-08-20 22:02:24 +0000797 // Emit the Catch Clauses. The code for the catch clauses following the same
798 // try is similar to a switch statement. The catch clause action record
799 // informs the runtime about the type of a catch clause and about the
800 // associated switch value.
801 //
802 // Action Record Fields:
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000803 //
Bill Wendlinga583c552009-08-20 22:02:24 +0000804 // * Filter Value
805 // Positive value, starting at 1. Index in the types table of the
806 // __typeinfo for the catch-clause type. 1 is the first word preceding
807 // TTBase, 2 is the second word, and so on. Used by the runtime to check
808 // if the thrown exception type matches the catch-clause type. Back-end
809 // generated switch statements check against this value.
810 //
811 // * Next
812 // Signed offset, in bytes from the start of this field, to the next
813 // chained action record, or zero if none.
814 //
815 // The order of the action records determined by the next field is the order
816 // of the catch clauses as they appear in the source code, and must be kept in
817 // the same order. As a result, changing the order of the catch clause would
818 // change the semantics of the program.
Bill Wendling5cff4872009-07-28 23:44:43 +0000819 for (std::vector<GlobalVariable *>::const_reverse_iterator
820 I = TypeInfos.rbegin(), E = TypeInfos.rend(); I != E; ++I) {
Bill Wendlinga583c552009-08-20 22:02:24 +0000821 const GlobalVariable *GV = *I;
Bill Wendlingeb907212009-05-15 01:12:28 +0000822 PrintRelDirective();
823
824 if (GV) {
825 std::string GLN;
826 O << Asm->getGlobalLinkName(GV, GLN);
827 } else {
828 O << "0";
829 }
830
831 Asm->EOL("TypeInfo");
832 }
833
Bill Wendlinga583c552009-08-20 22:02:24 +0000834 // Emit the Type Table.
Bill Wendling5cff4872009-07-28 23:44:43 +0000835 for (std::vector<unsigned>::const_iterator
836 I = FilterIds.begin(), E = FilterIds.end(); I < E; ++I) {
837 unsigned TypeID = *I;
Bill Wendlingeb907212009-05-15 01:12:28 +0000838 Asm->EmitULEB128Bytes(TypeID);
839 Asm->EOL("Filter TypeInfo index");
840 }
841
842 Asm->EmitAlignment(2, 0, 0, false);
843}
844
Bill Wendlingeb907212009-05-15 01:12:28 +0000845/// EndModule - Emit all exception information that should come after the
846/// content.
847void DwarfException::EndModule() {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000848 if (MAI->getExceptionHandlingType() != ExceptionHandling::Dwarf)
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000849 return;
Bill Wendlingeb907212009-05-15 01:12:28 +0000850 if (TimePassesIsEnabled)
851 ExceptionTimer->startTimer();
852
853 if (shouldEmitMovesModule || shouldEmitTableModule) {
854 const std::vector<Function *> Personalities = MMI->getPersonalities();
855 for (unsigned i = 0; i < Personalities.size(); ++i)
Bill Wendling7ccda0f2009-08-25 08:08:33 +0000856 EmitCIE(Personalities[i], i);
Bill Wendlingeb907212009-05-15 01:12:28 +0000857
858 for (std::vector<FunctionEHFrameInfo>::iterator I = EHFrames.begin(),
859 E = EHFrames.end(); I != E; ++I)
Bill Wendling7ccda0f2009-08-25 08:08:33 +0000860 EmitFDE(*I);
Bill Wendlingeb907212009-05-15 01:12:28 +0000861 }
862
863 if (TimePassesIsEnabled)
864 ExceptionTimer->stopTimer();
865}
866
867/// BeginFunction - Gather pre-function exception information. Assumes being
868/// emitted immediately after the function entry point.
869void DwarfException::BeginFunction(MachineFunction *MF) {
870 if (TimePassesIsEnabled)
871 ExceptionTimer->startTimer();
872
873 this->MF = MF;
874 shouldEmitTable = shouldEmitMoves = false;
875
Chris Lattner33adcfb2009-08-22 21:43:10 +0000876 if (MMI && MAI->doesSupportExceptionHandling()) {
Bill Wendlingeb907212009-05-15 01:12:28 +0000877 // Map all labels and get rid of any dead landing pads.
878 MMI->TidyLandingPads();
879
880 // If any landing pads survive, we need an EH table.
881 if (MMI->getLandingPads().size())
882 shouldEmitTable = true;
883
884 // See if we need frame move info.
885 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
886 shouldEmitMoves = true;
887
888 if (shouldEmitMoves || shouldEmitTable)
889 // Assumes in correct section after the entry point.
890 EmitLabel("eh_func_begin", ++SubprogramCount);
891 }
892
893 shouldEmitTableModule |= shouldEmitTable;
894 shouldEmitMovesModule |= shouldEmitMoves;
895
896 if (TimePassesIsEnabled)
897 ExceptionTimer->stopTimer();
898}
899
900/// EndFunction - Gather and emit post-function exception information.
901///
902void DwarfException::EndFunction() {
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000903 if (TimePassesIsEnabled)
Bill Wendlingeb907212009-05-15 01:12:28 +0000904 ExceptionTimer->startTimer();
905
906 if (shouldEmitMoves || shouldEmitTable) {
907 EmitLabel("eh_func_end", SubprogramCount);
908 EmitExceptionTable();
909
910 // Save EH frame information
Bill Wendlingeb907212009-05-15 01:12:28 +0000911 EHFrames.push_back(
Chris Lattnere2cf37b2009-07-17 20:46:40 +0000912 FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF),
Bill Wendlingeb907212009-05-15 01:12:28 +0000913 SubprogramCount,
914 MMI->getPersonalityIndex(),
915 MF->getFrameInfo()->hasCalls(),
916 !MMI->getLandingPads().empty(),
917 MMI->getFrameMoves(),
918 MF->getFunction()));
919 }
920
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000921 if (TimePassesIsEnabled)
Bill Wendlingeb907212009-05-15 01:12:28 +0000922 ExceptionTimer->stopTimer();
923}