blob: 3e82b0fc868016d6ef520cd6db99cc4a5392c1a9 [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);
109 Asm->EOL("Personality (pcrel sdata4 indirect)");
Bill Wendlingeb907212009-05-15 01:12:28 +0000110 } else {
Duncan Sandsc69d74a2009-08-31 16:45:16 +0000111 Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
112 Asm->EOL("Personality (pcrel sdata4)");
Bill Wendlingeb907212009-05-15 01:12:28 +0000113 }
114
115 PrintRelDirective(true);
Chris Lattner33adcfb2009-08-22 21:43:10 +0000116 O << MAI->getPersonalityPrefix();
Bill Wendlingeb907212009-05-15 01:12:28 +0000117 Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
Chris Lattner33adcfb2009-08-22 21:43:10 +0000118 O << MAI->getPersonalitySuffix();
119 if (strcmp(MAI->getPersonalitySuffix(), "+4@GOTPCREL"))
120 O << "-" << MAI->getPCSymbol();
Bill Wendlingeb907212009-05-15 01:12:28 +0000121 Asm->EOL("Personality");
122
Duncan Sandsc69d74a2009-08-31 16:45:16 +0000123 Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
124 Asm->EOL("LSDA Encoding (pcrel sdata4)");
Bill Wendlingeb907212009-05-15 01:12:28 +0000125
Duncan Sandsc69d74a2009-08-31 16:45:16 +0000126 Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
127 Asm->EOL("FDE Encoding (pcrel sdata4)");
Bill Wendlingeb907212009-05-15 01:12:28 +0000128 } else {
129 Asm->EmitULEB128Bytes(1);
130 Asm->EOL("Augmentation Size");
131
Duncan Sandsc69d74a2009-08-31 16:45:16 +0000132 Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
133 Asm->EOL("FDE Encoding (pcrel sdata4)");
Bill Wendlingeb907212009-05-15 01:12:28 +0000134 }
135
136 // Indicate locations of general callee saved registers in frame.
137 std::vector<MachineMove> Moves;
138 RI->getInitialFrameState(Moves);
139 EmitFrameMoves(NULL, 0, Moves, true);
140
141 // On Darwin the linker honors the alignment of eh_frame, which means it must
142 // be 8-byte on 64-bit targets to match what gcc does. Otherwise you get
143 // holes which confuse readers of eh_frame.
144 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
145 0, 0, false);
146 EmitLabel("eh_frame_common_end", Index);
147
148 Asm->EOL();
149}
150
Bill Wendling7ccda0f2009-08-25 08:08:33 +0000151/// EmitFDE - Emit the Frame Description Entry (FDE) for the function.
152void DwarfException::EmitFDE(const FunctionEHFrameInfo &EHFrameInfo) {
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000153 assert(!EHFrameInfo.function->hasAvailableExternallyLinkage() &&
Bill Wendlingeb907212009-05-15 01:12:28 +0000154 "Should not emit 'available externally' functions at all");
155
Chris Lattner3e0f60b2009-07-17 21:00:50 +0000156 const Function *TheFunc = EHFrameInfo.function;
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000157
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000158 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getEHFrameSection());
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000159
Bill Wendlingeb907212009-05-15 01:12:28 +0000160 // Externally visible entry into the functions eh frame info. If the
161 // corresponding function is static, this should not be externally visible.
Chris Lattner3e0f60b2009-07-17 21:00:50 +0000162 if (!TheFunc->hasLocalLinkage())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000163 if (const char *GlobalEHDirective = MAI->getGlobalEHDirective())
Bill Wendlingeb907212009-05-15 01:12:28 +0000164 O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
Bill Wendlingeb907212009-05-15 01:12:28 +0000165
166 // If corresponding function is weak definition, this should be too.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000167 if (TheFunc->isWeakForLinker() && MAI->getWeakDefDirective())
168 O << MAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
Bill Wendlingeb907212009-05-15 01:12:28 +0000169
170 // If there are no calls then you can't unwind. This may mean we can omit the
171 // EH Frame, but some environments do not handle weak absolute symbols. If
172 // UnwindTablesMandatory is set we cannot do this optimization; the unwind
173 // info is to be available for non-EH uses.
Chris Lattner3e0f60b2009-07-17 21:00:50 +0000174 if (!EHFrameInfo.hasCalls && !UnwindTablesMandatory &&
175 (!TheFunc->isWeakForLinker() ||
Chris Lattner33adcfb2009-08-22 21:43:10 +0000176 !MAI->getWeakDefDirective() ||
177 MAI->getSupportsWeakOmittedEHFrame())) {
Bill Wendlingeb907212009-05-15 01:12:28 +0000178 O << EHFrameInfo.FnName << " = 0\n";
179 // This name has no connection to the function, so it might get
180 // dead-stripped when the function is not, erroneously. Prohibit
181 // dead-stripping unconditionally.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000182 if (const char *UsedDirective = MAI->getUsedDirective())
Bill Wendlingeb907212009-05-15 01:12:28 +0000183 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
184 } else {
185 O << EHFrameInfo.FnName << ":\n";
186
187 // EH frame header.
188 EmitDifference("eh_frame_end", EHFrameInfo.Number,
189 "eh_frame_begin", EHFrameInfo.Number, true);
190 Asm->EOL("Length of Frame Information Entry");
191
192 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
193
Chris Lattnera4ff5e42009-07-17 20:53:51 +0000194 EmitSectionOffset("eh_frame_begin", "eh_frame_common",
195 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
196 true, true, false);
Bill Wendlingeb907212009-05-15 01:12:28 +0000197
198 Asm->EOL("FDE CIE offset");
199
Duncan Sandsc69d74a2009-08-31 16:45:16 +0000200 EmitReference("eh_func_begin", EHFrameInfo.Number, true, true);
Bill Wendlingeb907212009-05-15 01:12:28 +0000201 Asm->EOL("FDE initial location");
202 EmitDifference("eh_func_end", EHFrameInfo.Number,
Duncan Sandsc69d74a2009-08-31 16:45:16 +0000203 "eh_func_begin", EHFrameInfo.Number, true);
Bill Wendlingeb907212009-05-15 01:12:28 +0000204 Asm->EOL("FDE address range");
205
206 // If there is a personality and landing pads then point to the language
207 // specific data area in the exception table.
Eric Christopherd44fff72009-08-26 21:30:49 +0000208 if (MMI->getPersonalities()[0] != NULL) {
Duncan Sandsc69d74a2009-08-31 16:45:16 +0000209 bool is4Byte = TD->getPointerSize() == sizeof(int32_t);
210
Eric Christopher6fefceb2009-08-29 01:12:46 +0000211 Asm->EmitULEB128Bytes(is4Byte ? 4 : 8);
Bill Wendlingeb907212009-05-15 01:12:28 +0000212 Asm->EOL("Augmentation size");
213
Duncan Sandsc69d74a2009-08-31 16:45:16 +0000214 if (EHFrameInfo.hasLandingPads)
Eric Christopher6fefceb2009-08-29 01:12:46 +0000215 EmitReference("exception", EHFrameInfo.Number, true, false);
Duncan Sandsc69d74a2009-08-31 16:45:16 +0000216 else {
Eric Christopher6fefceb2009-08-29 01:12:46 +0000217 if (is4Byte)
218 Asm->EmitInt32((int)0);
219 else
220 Asm->EmitInt64((int)0);
221 }
Bill Wendlingeb907212009-05-15 01:12:28 +0000222 Asm->EOL("Language Specific Data Area");
223 } else {
224 Asm->EmitULEB128Bytes(0);
225 Asm->EOL("Augmentation size");
226 }
227
228 // Indicate locations of function specific callee saved registers in frame.
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000229 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves,
Bill Wendlingeb907212009-05-15 01:12:28 +0000230 true);
231
232 // On Darwin the linker honors the alignment of eh_frame, which means it
233 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise you
234 // get holes which confuse readers of eh_frame.
235 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
236 0, 0, false);
237 EmitLabel("eh_frame_end", EHFrameInfo.Number);
238
239 // If the function is marked used, this table should be also. We cannot
240 // make the mark unconditional in this case, since retaining the table also
241 // retains the function in this case, and there is code around that depends
242 // on unused functions (calling undefined externals) being dead-stripped to
243 // link correctly. Yes, there really is.
Chris Lattner401e10c2009-07-20 06:14:25 +0000244 if (MMI->isUsedFunction(EHFrameInfo.function))
Chris Lattner33adcfb2009-08-22 21:43:10 +0000245 if (const char *UsedDirective = MAI->getUsedDirective())
Bill Wendlingeb907212009-05-15 01:12:28 +0000246 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
247 }
Bill Wendling4bda11f2009-08-25 02:32:05 +0000248
249 Asm->EOL();
Bill Wendlingeb907212009-05-15 01:12:28 +0000250}
251
Bill Wendlingeb907212009-05-15 01:12:28 +0000252/// SharedTypeIds - How many leading type ids two landing pads have in common.
253unsigned DwarfException::SharedTypeIds(const LandingPadInfo *L,
254 const LandingPadInfo *R) {
255 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
256 unsigned LSize = LIds.size(), RSize = RIds.size();
257 unsigned MinSize = LSize < RSize ? LSize : RSize;
258 unsigned Count = 0;
259
260 for (; Count != MinSize; ++Count)
261 if (LIds[Count] != RIds[Count])
262 return Count;
263
264 return Count;
265}
266
267/// PadLT - Order landing pads lexicographically by type id.
268bool DwarfException::PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
269 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
270 unsigned LSize = LIds.size(), RSize = RIds.size();
271 unsigned MinSize = LSize < RSize ? LSize : RSize;
272
273 for (unsigned i = 0; i != MinSize; ++i)
274 if (LIds[i] != RIds[i])
275 return LIds[i] < RIds[i];
276
277 return LSize < RSize;
278}
279
Bill Wendlingd4609622009-07-28 23:23:00 +0000280/// ComputeActionsTable - Compute the actions table and gather the first action
281/// index for each landing pad site.
Bill Wendlingade025c2009-07-29 00:31:35 +0000282unsigned DwarfException::
283ComputeActionsTable(const SmallVectorImpl<const LandingPadInfo*> &LandingPads,
284 SmallVectorImpl<ActionEntry> &Actions,
285 SmallVectorImpl<unsigned> &FirstActions) {
Bill Wendlinga583c552009-08-20 22:02:24 +0000286
287 // The action table follows the call-site table in the LSDA. The individual
288 // records are of two types:
289 //
290 // * Catch clause
291 // * Exception specification
292 //
293 // The two record kinds have the same format, with only small differences.
294 // They are distinguished by the "switch value" field: Catch clauses
295 // (TypeInfos) have strictly positive switch values, and exception
296 // specifications (FilterIds) have strictly negative switch values. Value 0
297 // indicates a catch-all clause.
298 //
Bill Wendling5e953dd2009-07-28 23:22:13 +0000299 // Negative type IDs index into FilterIds. Positive type IDs index into
300 // TypeInfos. The value written for a positive type ID is just the type ID
301 // itself. For a negative type ID, however, the value written is the
Bill Wendlingeb907212009-05-15 01:12:28 +0000302 // (negative) byte offset of the corresponding FilterIds entry. The byte
Bill Wendling5e953dd2009-07-28 23:22:13 +0000303 // offset is usually equal to the type ID (because the FilterIds entries are
304 // written using a variable width encoding, which outputs one byte per entry
305 // as long as the value written is not too large) but can differ. This kind
306 // of complication does not occur for positive type IDs because type infos are
Bill Wendlingeb907212009-05-15 01:12:28 +0000307 // output using a fixed width encoding. FilterOffsets[i] holds the byte
308 // offset corresponding to FilterIds[i].
Bill Wendling409914b2009-07-29 21:19:44 +0000309
310 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
Bill Wendlingeb907212009-05-15 01:12:28 +0000311 SmallVector<int, 16> FilterOffsets;
312 FilterOffsets.reserve(FilterIds.size());
313 int Offset = -1;
Bill Wendling409914b2009-07-29 21:19:44 +0000314
315 for (std::vector<unsigned>::const_iterator
316 I = FilterIds.begin(), E = FilterIds.end(); I != E; ++I) {
Bill Wendlingeb907212009-05-15 01:12:28 +0000317 FilterOffsets.push_back(Offset);
Chris Lattneraf76e592009-08-22 20:48:53 +0000318 Offset -= MCAsmInfo::getULEB128Size(*I);
Bill Wendlingeb907212009-05-15 01:12:28 +0000319 }
320
Bill Wendlingeb907212009-05-15 01:12:28 +0000321 FirstActions.reserve(LandingPads.size());
322
323 int FirstAction = 0;
324 unsigned SizeActions = 0;
Bill Wendling5e953dd2009-07-28 23:22:13 +0000325 const LandingPadInfo *PrevLPI = 0;
Bill Wendling409914b2009-07-29 21:19:44 +0000326
Bill Wendling5cff4872009-07-28 23:44:43 +0000327 for (SmallVectorImpl<const LandingPadInfo *>::const_iterator
Bill Wendling5e953dd2009-07-28 23:22:13 +0000328 I = LandingPads.begin(), E = LandingPads.end(); I != E; ++I) {
329 const LandingPadInfo *LPI = *I;
330 const std::vector<int> &TypeIds = LPI->TypeIds;
331 const unsigned NumShared = PrevLPI ? SharedTypeIds(LPI, PrevLPI) : 0;
Bill Wendlingeb907212009-05-15 01:12:28 +0000332 unsigned SizeSiteActions = 0;
333
334 if (NumShared < TypeIds.size()) {
335 unsigned SizeAction = 0;
336 ActionEntry *PrevAction = 0;
337
338 if (NumShared) {
Bill Wendling5e953dd2009-07-28 23:22:13 +0000339 const unsigned SizePrevIds = PrevLPI->TypeIds.size();
Bill Wendlingeb907212009-05-15 01:12:28 +0000340 assert(Actions.size());
341 PrevAction = &Actions.back();
Chris Lattneraf76e592009-08-22 20:48:53 +0000342 SizeAction = MCAsmInfo::getSLEB128Size(PrevAction->NextAction) +
343 MCAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Bill Wendlingeb907212009-05-15 01:12:28 +0000344
345 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
346 SizeAction -=
Chris Lattneraf76e592009-08-22 20:48:53 +0000347 MCAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Bill Wendlingeb907212009-05-15 01:12:28 +0000348 SizeAction += -PrevAction->NextAction;
349 PrevAction = PrevAction->Previous;
350 }
351 }
352
353 // Compute the actions.
Bill Wendling5e953dd2009-07-28 23:22:13 +0000354 for (unsigned J = NumShared, M = TypeIds.size(); J != M; ++J) {
355 int TypeID = TypeIds[J];
356 assert(-1 - TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
Bill Wendlingeb907212009-05-15 01:12:28 +0000357 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
Chris Lattneraf76e592009-08-22 20:48:53 +0000358 unsigned SizeTypeID = MCAsmInfo::getSLEB128Size(ValueForTypeID);
Bill Wendlingeb907212009-05-15 01:12:28 +0000359
360 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
Chris Lattneraf76e592009-08-22 20:48:53 +0000361 SizeAction = SizeTypeID + MCAsmInfo::getSLEB128Size(NextAction);
Bill Wendlingeb907212009-05-15 01:12:28 +0000362 SizeSiteActions += SizeAction;
363
Bill Wendlinga583c552009-08-20 22:02:24 +0000364 ActionEntry Action = { ValueForTypeID, NextAction, PrevAction };
Bill Wendlingeb907212009-05-15 01:12:28 +0000365 Actions.push_back(Action);
Bill Wendlingeb907212009-05-15 01:12:28 +0000366 PrevAction = &Actions.back();
367 }
368
369 // Record the first action of the landing pad site.
370 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
371 } // else identical - re-use previous FirstAction
372
Bill Wendlinga583c552009-08-20 22:02:24 +0000373 // Information used when created the call-site table. The action record
374 // field of the call site record is the offset of the first associated
375 // action record, relative to the start of the actions table. This value is
376 // biased by 1 (1 in dicating the start of the actions table), and 0
377 // indicates that there are no actions.
Bill Wendlingeb907212009-05-15 01:12:28 +0000378 FirstActions.push_back(FirstAction);
379
380 // Compute this sites contribution to size.
381 SizeActions += SizeSiteActions;
Bill Wendling5e953dd2009-07-28 23:22:13 +0000382
383 PrevLPI = LPI;
Bill Wendlingeb907212009-05-15 01:12:28 +0000384 }
385
Bill Wendling5e953dd2009-07-28 23:22:13 +0000386 return SizeActions;
387}
388
Bill Wendlingade025c2009-07-29 00:31:35 +0000389/// ComputeCallSiteTable - Compute the call-site table. The entry for an invoke
Bill Wendlinga583c552009-08-20 22:02:24 +0000390/// has a try-range containing the call, a non-zero landing pad, and an
Bill Wendlingade025c2009-07-29 00:31:35 +0000391/// appropriate action. The entry for an ordinary call has a try-range
392/// containing the call and zero for the landing pad and the action. Calls
393/// marked 'nounwind' have no entry and must not be contained in the try-range
394/// of any entry - they form gaps in the table. Entries must be ordered by
395/// try-range address.
396void DwarfException::
397ComputeCallSiteTable(SmallVectorImpl<CallSiteEntry> &CallSites,
398 const RangeMapType &PadMap,
399 const SmallVectorImpl<const LandingPadInfo *> &LandingPads,
400 const SmallVectorImpl<unsigned> &FirstActions) {
Bill Wendlingeb907212009-05-15 01:12:28 +0000401 // The end label of the previous invoke or nounwind try-range.
402 unsigned LastLabel = 0;
403
404 // Whether there is a potentially throwing instruction (currently this means
405 // an ordinary call) between the end of the previous try-range and now.
406 bool SawPotentiallyThrowing = false;
407
Bill Wendling5cff4872009-07-28 23:44:43 +0000408 // Whether the last CallSite entry was for an invoke.
Bill Wendlingeb907212009-05-15 01:12:28 +0000409 bool PreviousIsInvoke = false;
410
411 // Visit all instructions in order of address.
412 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
413 I != E; ++I) {
414 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
415 MI != E; ++MI) {
416 if (!MI->isLabel()) {
417 SawPotentiallyThrowing |= MI->getDesc().isCall();
418 continue;
419 }
420
421 unsigned BeginLabel = MI->getOperand(0).getImm();
422 assert(BeginLabel && "Invalid label!");
423
424 // End of the previous try-range?
425 if (BeginLabel == LastLabel)
426 SawPotentiallyThrowing = false;
427
428 // Beginning of a new try-range?
429 RangeMapType::iterator L = PadMap.find(BeginLabel);
430 if (L == PadMap.end())
431 // Nope, it was just some random label.
432 continue;
433
Bill Wendlinga583c552009-08-20 22:02:24 +0000434 const PadRange &P = L->second;
Bill Wendlingeb907212009-05-15 01:12:28 +0000435 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
Bill Wendlingeb907212009-05-15 01:12:28 +0000436 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
437 "Inconsistent landing pad map!");
438
Bill Wendlinga583c552009-08-20 22:02:24 +0000439 // For Dwarf exception handling (SjLj handling doesn't use this). If some
440 // instruction between the previous try-range and this one may throw,
441 // create a call-site entry with no landing pad for the region between the
442 // try-ranges.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000443 if (SawPotentiallyThrowing &&
Chris Lattner33adcfb2009-08-22 21:43:10 +0000444 MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) {
Bill Wendlinga583c552009-08-20 22:02:24 +0000445 CallSiteEntry Site = { LastLabel, BeginLabel, 0, 0 };
Bill Wendlingeb907212009-05-15 01:12:28 +0000446 CallSites.push_back(Site);
447 PreviousIsInvoke = false;
448 }
449
450 LastLabel = LandingPad->EndLabels[P.RangeIndex];
451 assert(BeginLabel && LastLabel && "Invalid landing pad!");
452
453 if (LandingPad->LandingPadLabel) {
454 // This try-range is for an invoke.
Bill Wendlinga583c552009-08-20 22:02:24 +0000455 CallSiteEntry Site = {
456 BeginLabel,
457 LastLabel,
458 LandingPad->LandingPadLabel,
459 FirstActions[P.PadIndex]
460 };
Bill Wendlingeb907212009-05-15 01:12:28 +0000461
Jim Grosbach33668c02009-09-01 17:19:13 +0000462 // Try to merge with the previous call-site. SJLJ doesn't do this
463 if (PreviousIsInvoke &&
464 MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) {
Bill Wendlingeb907212009-05-15 01:12:28 +0000465 CallSiteEntry &Prev = CallSites.back();
466 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
467 // Extend the range of the previous entry.
468 Prev.EndLabel = Site.EndLabel;
469 continue;
470 }
471 }
472
473 // Otherwise, create a new call-site.
474 CallSites.push_back(Site);
475 PreviousIsInvoke = true;
476 } else {
477 // Create a gap.
478 PreviousIsInvoke = false;
479 }
480 }
481 }
482
483 // If some instruction between the previous try-range and the end of the
484 // function may throw, create a call-site entry with no landing pad for the
485 // region following the try-range.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000486 if (SawPotentiallyThrowing &&
Chris Lattner33adcfb2009-08-22 21:43:10 +0000487 MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) {
Bill Wendlinga583c552009-08-20 22:02:24 +0000488 CallSiteEntry Site = { LastLabel, 0, 0, 0 };
Bill Wendlingeb907212009-05-15 01:12:28 +0000489 CallSites.push_back(Site);
490 }
Bill Wendlingade025c2009-07-29 00:31:35 +0000491}
492
Bill Wendling0dafca92009-07-29 00:50:05 +0000493/// EmitExceptionTable - Emit landing pads and actions.
494///
495/// The general organization of the table is complex, but the basic concepts are
496/// easy. First there is a header which describes the location and organization
497/// of the three components that follow.
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000498///
Bill Wendling0dafca92009-07-29 00:50:05 +0000499/// 1. The landing pad site information describes the range of code covered by
500/// the try. In our case it's an accumulation of the ranges covered by the
501/// invokes in the try. There is also a reference to the landing pad that
502/// handles the exception once processed. Finally an index into the actions
503/// table.
Bill Wendlinga583c552009-08-20 22:02:24 +0000504/// 2. The action table, in our case, is composed of pairs of type IDs and next
Bill Wendling0dafca92009-07-29 00:50:05 +0000505/// action offset. Starting with the action index from the landing pad
Bill Wendlinga583c552009-08-20 22:02:24 +0000506/// site, each type ID is checked for a match to the current exception. If
Bill Wendling0dafca92009-07-29 00:50:05 +0000507/// it matches then the exception and type id are passed on to the landing
508/// pad. Otherwise the next action is looked up. This chain is terminated
509/// with a next action of zero. If no type id is found the the frame is
510/// unwound and handling continues.
Bill Wendlinga583c552009-08-20 22:02:24 +0000511/// 3. Type ID table contains references to all the C++ typeinfo for all
Bill Wendling0dafca92009-07-29 00:50:05 +0000512/// catches in the function. This tables is reversed indexed base 1.
Bill Wendlingade025c2009-07-29 00:31:35 +0000513void DwarfException::EmitExceptionTable() {
514 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
515 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
516 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
517 if (PadInfos.empty()) return;
518
519 // Sort the landing pads in order of their type ids. This is used to fold
520 // duplicate actions.
521 SmallVector<const LandingPadInfo *, 64> LandingPads;
522 LandingPads.reserve(PadInfos.size());
523
524 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
525 LandingPads.push_back(&PadInfos[i]);
526
527 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
528
529 // Compute the actions table and gather the first action index for each
530 // landing pad site.
531 SmallVector<ActionEntry, 32> Actions;
532 SmallVector<unsigned, 64> FirstActions;
533 unsigned SizeActions = ComputeActionsTable(LandingPads, Actions, FirstActions);
534
535 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
536 // by try-range labels when lowered). Ordinary calls do not, so appropriate
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000537 // try-ranges for them need be deduced when using Dwarf exception handling.
Bill Wendlingade025c2009-07-29 00:31:35 +0000538 RangeMapType PadMap;
539 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
540 const LandingPadInfo *LandingPad = LandingPads[i];
541 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
542 unsigned BeginLabel = LandingPad->BeginLabels[j];
543 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
544 PadRange P = { i, j };
545 PadMap[BeginLabel] = P;
546 }
547 }
548
549 // Compute the call-site table.
550 SmallVector<CallSiteEntry, 64> CallSites;
Jim Grosbach8b818d72009-08-17 16:41:22 +0000551 ComputeCallSiteTable(CallSites, PadMap, LandingPads, FirstActions);
Bill Wendlingeb907212009-05-15 01:12:28 +0000552
553 // Final tallies.
554
555 // Call sites.
556 const unsigned SiteStartSize = sizeof(int32_t); // DW_EH_PE_udata4
557 const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4
558 const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000559 unsigned SizeSites;
Jim Grosbachbff39232009-08-12 17:38:44 +0000560
Chris Lattner33adcfb2009-08-22 21:43:10 +0000561 bool HaveTTData = (MAI->getExceptionHandlingType() == ExceptionHandling::SjLj)
Jim Grosbachbff39232009-08-12 17:38:44 +0000562 ? (!TypeInfos.empty() || !FilterIds.empty()) : true;
563
564
Chris Lattner33adcfb2009-08-22 21:43:10 +0000565 if (MAI->getExceptionHandlingType() == ExceptionHandling::SjLj) {
Jim Grosbach8b818d72009-08-17 16:41:22 +0000566 SizeSites = 0;
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000567 } else
568 SizeSites = CallSites.size() *
569 (SiteStartSize + SiteLengthSize + LandingPadSize);
570 for (unsigned i = 0, e = CallSites.size(); i < e; ++i) {
Chris Lattneraf76e592009-08-22 20:48:53 +0000571 SizeSites += MCAsmInfo::getULEB128Size(CallSites[i].Action);
Chris Lattner33adcfb2009-08-22 21:43:10 +0000572 if (MAI->getExceptionHandlingType() == ExceptionHandling::SjLj)
Chris Lattneraf76e592009-08-22 20:48:53 +0000573 SizeSites += MCAsmInfo::getULEB128Size(i);
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000574 }
Bill Wendlingeb907212009-05-15 01:12:28 +0000575 // Type infos.
576 const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
577 unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
578
Bill Wendlingb4049fe2009-09-09 21:06:24 +0000579 unsigned TypeOffset = sizeof(int8_t) + // Call site format
Chris Lattneraf76e592009-08-22 20:48:53 +0000580 MCAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
Bill Wendlingeb907212009-05-15 01:12:28 +0000581 SizeSites + SizeActions + SizeTypes;
582
583 unsigned TotalSize = sizeof(int8_t) + // LPStart format
584 sizeof(int8_t) + // TType format
Jim Grosbachbff39232009-08-12 17:38:44 +0000585 (HaveTTData ?
Chris Lattneraf76e592009-08-22 20:48:53 +0000586 MCAsmInfo::getULEB128Size(TypeOffset) : 0) + // TType base offset
Bill Wendlingeb907212009-05-15 01:12:28 +0000587 TypeOffset;
588
589 unsigned SizeAlign = (4 - TotalSize) & 3;
590
591 // Begin the exception table.
Chris Lattnerd5bbb072009-08-02 01:34:32 +0000592 const MCSection *LSDASection = Asm->getObjFileLowering().getLSDASection();
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000593 Asm->OutStreamer.SwitchSection(LSDASection);
Bill Wendlingeb907212009-05-15 01:12:28 +0000594 Asm->EmitAlignment(2, 0, 0, false);
595 O << "GCC_except_table" << SubprogramCount << ":\n";
596
597 for (unsigned i = 0; i != SizeAlign; ++i) {
598 Asm->EmitInt8(0);
599 Asm->EOL("Padding");
Bill Wendlingc5800a82009-07-28 21:54:03 +0000600 }
Bill Wendlingeb907212009-05-15 01:12:28 +0000601
602 EmitLabel("exception", SubprogramCount);
Chris Lattner33adcfb2009-08-22 21:43:10 +0000603 if (MAI->getExceptionHandlingType() == ExceptionHandling::SjLj) {
Jim Grosbachee793a62009-09-01 18:55:08 +0000604 SmallString<16> LSDAName;
Jim Grosbachc40d9f92009-09-01 18:49:12 +0000605 raw_svector_ostream(LSDAName) << MAI->getPrivateGlobalPrefix() <<
606 "_LSDA_" << Asm->getFunctionNumber();
607 O << LSDAName.str() << ":\n";
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000608 }
Bill Wendlingeb907212009-05-15 01:12:28 +0000609
610 // Emit the header.
Bill Wendling639217c2009-08-27 03:32:50 +0000611 Asm->EmitInt8(dwarf::DW_EH_PE_omit);
612 Asm->EOL("@LPStart format (DW_EH_PE_omit)");
Bill Wendlingb0d9c3e2009-07-28 22:23:45 +0000613
Bill Wendlingade025c2009-07-29 00:31:35 +0000614#if 0
Chris Lattner81c9a062009-07-31 22:03:47 +0000615 if (TypeInfos.empty() && FilterIds.empty()) {
Chris Lattnerad88bc42009-08-02 03:59:56 +0000616 // If there are no typeinfos or filters, there is nothing to emit, optimize
617 // by specifying the "omit" encoding.
Bill Wendling639217c2009-08-27 03:32:50 +0000618 Asm->EmitInt8(dwarf::DW_EH_PE_omit);
619 Asm->EOL("@TType format (DW_EH_PE_omit)");
Chris Lattner81c9a062009-07-31 22:03:47 +0000620 } else {
Chris Lattnerad88bc42009-08-02 03:59:56 +0000621 // Okay, we have actual filters or typeinfos to emit. As such, we need to
622 // pick a type encoding for them. We're about to emit a list of pointers to
623 // typeinfo objects at the end of the LSDA. However, unless we're in static
624 // mode, this reference will require a relocation by the dynamic linker.
Chris Lattner46b754c2009-07-31 22:18:14 +0000625 //
Chris Lattnerad88bc42009-08-02 03:59:56 +0000626 // Because of this, we have a couple of options:
627 // 1) If we are in -static mode, we can always use an absolute reference
628 // from the LSDA, because the static linker will resolve it.
629 // 2) Otherwise, if the LSDA section is writable, we can output the direct
630 // reference to the typeinfo and allow the dynamic linker to relocate
631 // it. Since it is in a writable section, the dynamic linker won't
632 // have a problem.
633 // 3) Finally, if we're in PIC mode and the LDSA section isn't writable,
634 // we need to use some form of indirection. For example, on Darwin,
635 // we can output a statically-relocatable reference to a dyld stub. The
636 // offset to the stub is constant, but the contents are in a section
637 // that is updated by the dynamic linker. This is easy enough, but we
638 // need to tell the personality function of the unwinder to indirect
639 // through the dyld stub.
640 //
641 // FIXME: When this is actually implemented, we'll have to emit the stubs
642 // somewhere. This predicate should be moved to a shared location that is
643 // in target-independent code.
644 //
645 if (LSDASection->isWritable() ||
646 Asm->TM.getRelocationModel() == Reloc::Static) {
Bill Wendling639217c2009-08-27 03:32:50 +0000647 Asm->EmitInt8(DW_EH_PE_absptr);
648 Asm->EOL("TType format (DW_EH_PE_absptr)");
Chris Lattnerad88bc42009-08-02 03:59:56 +0000649 } else {
650 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_indirect | DW_EH_PE_sdata4);
Bill Wendling639217c2009-08-27 03:32:50 +0000651 Asm->EOL("TType format (DW_EH_PE_pcrel | DW_EH_PE_indirect"
652 " | DW_EH_PE_sdata4)");
Chris Lattnerad88bc42009-08-02 03:59:56 +0000653 }
Bill Wendlingb0d9c3e2009-07-28 22:23:45 +0000654 Asm->EmitULEB128Bytes(TypeOffset);
655 Asm->EOL("TType base offset");
Bill Wendlingb0d9c3e2009-07-28 22:23:45 +0000656 }
Bill Wendlingade025c2009-07-29 00:31:35 +0000657#else
Jim Grosbachbff39232009-08-12 17:38:44 +0000658 // For SjLj exceptions, if there is no TypeInfo, then we just explicitly
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000659 // say that we're omitting that bit.
660 // FIXME: does this apply to Dwarf also? The above #if 0 implies yes?
Jim Grosbachbff39232009-08-12 17:38:44 +0000661 if (!HaveTTData) {
Bill Wendling639217c2009-08-27 03:32:50 +0000662 Asm->EmitInt8(dwarf::DW_EH_PE_omit);
663 Asm->EOL("@TType format (DW_EH_PE_omit)");
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000664 } else {
Bill Wendling639217c2009-08-27 03:32:50 +0000665 Asm->EmitInt8(dwarf::DW_EH_PE_absptr);
666 Asm->EOL("@TType format (DW_EH_PE_absptr)");
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000667 Asm->EmitULEB128Bytes(TypeOffset);
Bill Wendlinga583c552009-08-20 22:02:24 +0000668 Asm->EOL("@TType base offset");
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000669 }
Bill Wendlingade025c2009-07-29 00:31:35 +0000670#endif
Bill Wendlingb0d9c3e2009-07-28 22:23:45 +0000671
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000672 // SjLj Exception handilng
Chris Lattner33adcfb2009-08-22 21:43:10 +0000673 if (MAI->getExceptionHandlingType() == ExceptionHandling::SjLj) {
Bill Wendling639217c2009-08-27 03:32:50 +0000674 Asm->EmitInt8(dwarf::DW_EH_PE_udata4);
675 Asm->EOL("Call site format (DW_EH_PE_udata4)");
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000676 Asm->EmitULEB128Bytes(SizeSites);
Bill Wendlinga583c552009-08-20 22:02:24 +0000677 Asm->EOL("Call site table length");
Bill Wendlingeb907212009-05-15 01:12:28 +0000678
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000679 // Emit the landing pad site information.
Jim Grosbach8b818d72009-08-17 16:41:22 +0000680 unsigned idx = 0;
681 for (SmallVectorImpl<CallSiteEntry>::const_iterator
682 I = CallSites.begin(), E = CallSites.end(); I != E; ++I, ++idx) {
683 const CallSiteEntry &S = *I;
Bill Wendlinga583c552009-08-20 22:02:24 +0000684
685 // Offset of the landing pad, counted in 16-byte bundles relative to the
686 // @LPStart address.
Jim Grosbach8b818d72009-08-17 16:41:22 +0000687 Asm->EmitULEB128Bytes(idx);
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000688 Asm->EOL("Landing pad");
Bill Wendlinga583c552009-08-20 22:02:24 +0000689
690 // Offset of the first associated action record, relative to the start of
691 // the action table. This value is biased by 1 (1 indicates the start of
692 // the action table), and 0 indicates that there are no actions.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000693 Asm->EmitULEB128Bytes(S.Action);
694 Asm->EOL("Action");
Bill Wendlingeb907212009-05-15 01:12:28 +0000695 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000696 } else {
697 // DWARF Exception handling
Chris Lattner33adcfb2009-08-22 21:43:10 +0000698 assert(MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf);
Bill Wendlingeb907212009-05-15 01:12:28 +0000699
Bill Wendlinga583c552009-08-20 22:02:24 +0000700 // The call-site table is a list of all call sites that may throw an
701 // exception (including C++ 'throw' statements) in the procedure
702 // fragment. It immediately follows the LSDA header. Each entry indicates,
703 // for a given call, the first corresponding action record and corresponding
704 // landing pad.
705 //
706 // The table begins with the number of bytes, stored as an LEB128
707 // compressed, unsigned integer. The records immediately follow the record
708 // count. They are sorted in increasing call-site address. Each record
709 // indicates:
710 //
711 // * The position of the call-site.
712 // * The position of the landing pad.
713 // * The first action record for that call site.
714 //
715 // A missing entry in the call-site table indicates that a call is not
716 // supposed to throw. Such calls include:
717 //
718 // * Calls to destructors within cleanup code. C++ semantics forbids these
719 // calls to throw.
720 // * Calls to intrinsic routines in the standard library which are known
721 // not to throw (sin, memcpy, et al).
722 //
723 // If the runtime does not find the call-site entry for a given call, it
724 // will call `terminate()'.
725
726 // Emit the landing pad call site table.
Bill Wendling639217c2009-08-27 03:32:50 +0000727 Asm->EmitInt8(dwarf::DW_EH_PE_udata4);
728 Asm->EOL("Call site format (DW_EH_PE_udata4)");
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000729 Asm->EmitULEB128Bytes(SizeSites);
Bill Wendlinga583c552009-08-20 22:02:24 +0000730 Asm->EOL("Call site table size");
Bill Wendlingeb907212009-05-15 01:12:28 +0000731
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000732 for (SmallVectorImpl<CallSiteEntry>::const_iterator
733 I = CallSites.begin(), E = CallSites.end(); I != E; ++I) {
734 const CallSiteEntry &S = *I;
735 const char *BeginTag;
736 unsigned BeginNumber;
Bill Wendlingeb907212009-05-15 01:12:28 +0000737
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000738 if (!S.BeginLabel) {
739 BeginTag = "eh_func_begin";
740 BeginNumber = SubprogramCount;
741 } else {
742 BeginTag = "label";
743 BeginNumber = S.BeginLabel;
744 }
Bill Wendlingeb907212009-05-15 01:12:28 +0000745
Bill Wendlinga583c552009-08-20 22:02:24 +0000746 // Offset of the call site relative to the previous call site, counted in
747 // number of 16-byte bundles. The first call site is counted relative to
748 // the start of the procedure fragment.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000749 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
Bill Wendlingeb907212009-05-15 01:12:28 +0000750 true, true);
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000751 Asm->EOL("Region start");
Bill Wendlingeb907212009-05-15 01:12:28 +0000752
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000753 if (!S.EndLabel)
754 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
755 true);
756 else
757 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
Bill Wendlingeb907212009-05-15 01:12:28 +0000758
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000759 Asm->EOL("Region length");
760
Bill Wendlinga583c552009-08-20 22:02:24 +0000761 // Offset of the landing pad, counted in 16-byte bundles relative to the
762 // @LPStart address.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000763 if (!S.PadLabel)
764 Asm->EmitInt32(0);
765 else
766 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
767 true, true);
768
769 Asm->EOL("Landing pad");
770
Bill Wendlinga583c552009-08-20 22:02:24 +0000771 // Offset of the first associated action record, relative to the start of
772 // the action table. This value is biased by 1 (1 indicates the start of
773 // the action table), and 0 indicates that there are no actions.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000774 Asm->EmitULEB128Bytes(S.Action);
775 Asm->EOL("Action");
776 }
Bill Wendlingeb907212009-05-15 01:12:28 +0000777 }
778
Bill Wendlinga583c552009-08-20 22:02:24 +0000779 // Emit the Action Table.
Bill Wendling5cff4872009-07-28 23:44:43 +0000780 for (SmallVectorImpl<ActionEntry>::const_iterator
781 I = Actions.begin(), E = Actions.end(); I != E; ++I) {
782 const ActionEntry &Action = *I;
Bill Wendlinga583c552009-08-20 22:02:24 +0000783
784 // Type Filter
785 //
786 // Used by the runtime to match the type of the thrown exception to the
787 // type of the catch clauses or the types in the exception specification.
788
Bill Wendlingeb907212009-05-15 01:12:28 +0000789 Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
790 Asm->EOL("TypeInfo index");
Bill Wendlinga583c552009-08-20 22:02:24 +0000791
792 // Action Record
793 //
794 // Self-relative signed displacement in bytes of the next action record,
795 // or 0 if there is no next action record.
796
Bill Wendlingeb907212009-05-15 01:12:28 +0000797 Asm->EmitSLEB128Bytes(Action.NextAction);
798 Asm->EOL("Next action");
799 }
800
Bill Wendlinga583c552009-08-20 22:02:24 +0000801 // Emit the Catch Clauses. The code for the catch clauses following the same
802 // try is similar to a switch statement. The catch clause action record
803 // informs the runtime about the type of a catch clause and about the
804 // associated switch value.
805 //
806 // Action Record Fields:
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000807 //
Bill Wendlinga583c552009-08-20 22:02:24 +0000808 // * Filter Value
809 // Positive value, starting at 1. Index in the types table of the
810 // __typeinfo for the catch-clause type. 1 is the first word preceding
811 // TTBase, 2 is the second word, and so on. Used by the runtime to check
812 // if the thrown exception type matches the catch-clause type. Back-end
813 // generated switch statements check against this value.
814 //
815 // * Next
816 // Signed offset, in bytes from the start of this field, to the next
817 // chained action record, or zero if none.
818 //
819 // The order of the action records determined by the next field is the order
820 // of the catch clauses as they appear in the source code, and must be kept in
821 // the same order. As a result, changing the order of the catch clause would
822 // change the semantics of the program.
Bill Wendling5cff4872009-07-28 23:44:43 +0000823 for (std::vector<GlobalVariable *>::const_reverse_iterator
824 I = TypeInfos.rbegin(), E = TypeInfos.rend(); I != E; ++I) {
Bill Wendlinga583c552009-08-20 22:02:24 +0000825 const GlobalVariable *GV = *I;
Bill Wendlingeb907212009-05-15 01:12:28 +0000826 PrintRelDirective();
827
828 if (GV) {
829 std::string GLN;
830 O << Asm->getGlobalLinkName(GV, GLN);
831 } else {
Bill Wendling049e98d2009-08-31 18:26:48 +0000832 O << "0x0";
Bill Wendlingeb907212009-05-15 01:12:28 +0000833 }
834
835 Asm->EOL("TypeInfo");
836 }
837
Bill Wendlinga583c552009-08-20 22:02:24 +0000838 // Emit the Type Table.
Bill Wendling5cff4872009-07-28 23:44:43 +0000839 for (std::vector<unsigned>::const_iterator
840 I = FilterIds.begin(), E = FilterIds.end(); I < E; ++I) {
841 unsigned TypeID = *I;
Bill Wendlingeb907212009-05-15 01:12:28 +0000842 Asm->EmitULEB128Bytes(TypeID);
843 Asm->EOL("Filter TypeInfo index");
844 }
845
846 Asm->EmitAlignment(2, 0, 0, false);
847}
848
Bill Wendlingeb907212009-05-15 01:12:28 +0000849/// EndModule - Emit all exception information that should come after the
850/// content.
851void DwarfException::EndModule() {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000852 if (MAI->getExceptionHandlingType() != ExceptionHandling::Dwarf)
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000853 return;
Bill Wendlingb4049fe2009-09-09 21:06:24 +0000854
Bill Wendlingeb907212009-05-15 01:12:28 +0000855 if (TimePassesIsEnabled)
856 ExceptionTimer->startTimer();
857
858 if (shouldEmitMovesModule || shouldEmitTableModule) {
859 const std::vector<Function *> Personalities = MMI->getPersonalities();
Bill Wendlingb4049fe2009-09-09 21:06:24 +0000860
861 for (unsigned i = 0, e = Personalities.size(); i < e; ++i)
Bill Wendling7ccda0f2009-08-25 08:08:33 +0000862 EmitCIE(Personalities[i], i);
Bill Wendlingeb907212009-05-15 01:12:28 +0000863
Bill Wendlingb4049fe2009-09-09 21:06:24 +0000864 for (std::vector<FunctionEHFrameInfo>::iterator
865 I = EHFrames.begin(), E = EHFrames.end(); I != E; ++I)
Bill Wendling7ccda0f2009-08-25 08:08:33 +0000866 EmitFDE(*I);
Bill Wendlingeb907212009-05-15 01:12:28 +0000867 }
868
869 if (TimePassesIsEnabled)
870 ExceptionTimer->stopTimer();
871}
872
873/// BeginFunction - Gather pre-function exception information. Assumes being
874/// emitted immediately after the function entry point.
875void DwarfException::BeginFunction(MachineFunction *MF) {
876 if (TimePassesIsEnabled)
877 ExceptionTimer->startTimer();
878
879 this->MF = MF;
880 shouldEmitTable = shouldEmitMoves = false;
881
Chris Lattner33adcfb2009-08-22 21:43:10 +0000882 if (MMI && MAI->doesSupportExceptionHandling()) {
Bill Wendlingeb907212009-05-15 01:12:28 +0000883 // Map all labels and get rid of any dead landing pads.
884 MMI->TidyLandingPads();
885
886 // If any landing pads survive, we need an EH table.
Bill Wendlingb4049fe2009-09-09 21:06:24 +0000887 if (!MMI->getLandingPads().empty())
Bill Wendlingeb907212009-05-15 01:12:28 +0000888 shouldEmitTable = true;
889
890 // See if we need frame move info.
891 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
892 shouldEmitMoves = true;
893
894 if (shouldEmitMoves || shouldEmitTable)
895 // Assumes in correct section after the entry point.
896 EmitLabel("eh_func_begin", ++SubprogramCount);
897 }
898
899 shouldEmitTableModule |= shouldEmitTable;
900 shouldEmitMovesModule |= shouldEmitMoves;
901
902 if (TimePassesIsEnabled)
903 ExceptionTimer->stopTimer();
904}
905
906/// EndFunction - Gather and emit post-function exception information.
907///
908void DwarfException::EndFunction() {
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000909 if (TimePassesIsEnabled)
Bill Wendlingeb907212009-05-15 01:12:28 +0000910 ExceptionTimer->startTimer();
911
912 if (shouldEmitMoves || shouldEmitTable) {
913 EmitLabel("eh_func_end", SubprogramCount);
914 EmitExceptionTable();
915
916 // Save EH frame information
Bill Wendlingeb907212009-05-15 01:12:28 +0000917 EHFrames.push_back(
Chris Lattnere2cf37b2009-07-17 20:46:40 +0000918 FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF),
Bill Wendlingeb907212009-05-15 01:12:28 +0000919 SubprogramCount,
920 MMI->getPersonalityIndex(),
921 MF->getFrameInfo()->hasCalls(),
922 !MMI->getLandingPads().empty(),
923 MMI->getFrameMoves(),
924 MF->getFunction()));
925 }
926
Eric Christopherdbfcdb92009-08-28 22:33:43 +0000927 if (TimePassesIsEnabled)
Bill Wendlingeb907212009-05-15 01:12:28 +0000928 ExceptionTimer->stopTimer();
929}