blob: 7baf053e99f952b1069bcada1468a485c612845c [file] [log] [blame]
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +00001//===----- JITDwarfEmitter.cpp - Write dwarf tables into memory -----------===//
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//
10// This file defines a JITDwarfEmitter object that is used by the JIT to
11// write dwarf tables to memory.
12//
13//===----------------------------------------------------------------------===//
14
15#include "JIT.h"
16#include "JITDwarfEmitter.h"
17#include "llvm/Function.h"
18#include "llvm/ADT/DenseMap.h"
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000019#include "llvm/CodeGen/JITCodeEmitter.h"
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000020#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/MachineLocation.h"
22#include "llvm/CodeGen/MachineModuleInfo.h"
23#include "llvm/ExecutionEngine/JITMemoryManager.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000024#include "llvm/Support/ErrorHandling.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000025#include "llvm/MC/MCAsmInfo.h"
Chris Lattner16112732010-03-14 01:41:15 +000026#include "llvm/MC/MCSymbol.h"
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000027#include "llvm/Target/TargetData.h"
28#include "llvm/Target/TargetInstrInfo.h"
29#include "llvm/Target/TargetFrameInfo.h"
30#include "llvm/Target/TargetMachine.h"
31#include "llvm/Target/TargetRegisterInfo.h"
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000032using namespace llvm;
33
Daniel Dunbar003de662009-09-21 05:58:35 +000034JITDwarfEmitter::JITDwarfEmitter(JIT& theJit) : MMI(0), Jit(theJit) {}
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000035
36
37unsigned char* JITDwarfEmitter::EmitDwarfTable(MachineFunction& F,
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000038 JITCodeEmitter& jce,
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000039 unsigned char* StartFunction,
Reid Kleckner27632172009-09-20 23:52:43 +000040 unsigned char* EndFunction,
41 unsigned char* &EHFramePtr) {
Daniel Dunbar003de662009-09-21 05:58:35 +000042 assert(MMI && "MachineModuleInfo not registered!");
43
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000044 const TargetMachine& TM = F.getTarget();
45 TD = TM.getTargetData();
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000046 stackGrowthDirection = TM.getFrameInfo()->getStackGrowthDirection();
47 RI = TM.getRegisterInfo();
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000048 JCE = &jce;
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000049
50 unsigned char* ExceptionTable = EmitExceptionTable(&F, StartFunction,
51 EndFunction);
52
53 unsigned char* Result = 0;
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000054
55 const std::vector<Function *> Personalities = MMI->getPersonalities();
56 EHFramePtr = EmitCommonEHFrame(Personalities[MMI->getPersonalityIndex()]);
57
58 Result = EmitEHFrame(Personalities[MMI->getPersonalityIndex()], EHFramePtr,
59 StartFunction, EndFunction, ExceptionTable);
Bill Wendling9d48b552009-09-09 00:11:02 +000060
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000061 return Result;
62}
63
64
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +000065void
66JITDwarfEmitter::EmitFrameMoves(intptr_t BaseLabelPtr,
67 const std::vector<MachineMove> &Moves) const {
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000068 unsigned PointerSize = TD->getPointerSize();
69 int stackGrowth = stackGrowthDirection == TargetFrameInfo::StackGrowsUp ?
70 PointerSize : -PointerSize;
Chris Lattner2e9919a2010-03-14 08:12:40 +000071 MCSymbol *BaseLabel = 0;
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000072
73 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
74 const MachineMove &Move = Moves[i];
Chris Lattner2e9919a2010-03-14 08:12:40 +000075 MCSymbol *Label = Move.getLabel();
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000076
Chris Lattner16112732010-03-14 01:41:15 +000077 // Throw out move if the label is invalid.
78 if (Label && !Label->isDefined())
79 continue;
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000080
81 intptr_t LabelPtr = 0;
Chris Lattner2e9919a2010-03-14 08:12:40 +000082 if (Label) LabelPtr = JCE->getLabelAddress(Label);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000083
84 const MachineLocation &Dst = Move.getDestination();
85 const MachineLocation &Src = Move.getSource();
86
87 // Advance row if new location.
Chris Lattner2e9919a2010-03-14 08:12:40 +000088 if (BaseLabelPtr && Label && BaseLabel != Label) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000089 JCE->emitByte(dwarf::DW_CFA_advance_loc4);
90 JCE->emitInt32(LabelPtr - BaseLabelPtr);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000091
Chris Lattner2e9919a2010-03-14 08:12:40 +000092 BaseLabel = Label;
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000093 BaseLabelPtr = LabelPtr;
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000094 }
95
96 // If advancing cfa.
Daniel Dunbar489032a2008-10-03 17:11:57 +000097 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
98 if (!Src.isReg()) {
99 if (Src.getReg() == MachineLocation::VirtualFP) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000100 JCE->emitByte(dwarf::DW_CFA_def_cfa_offset);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000101 } else {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000102 JCE->emitByte(dwarf::DW_CFA_def_cfa);
103 JCE->emitULEB128Bytes(RI->getDwarfRegNum(Src.getReg(), true));
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000104 }
105
Bill Wendling9d48b552009-09-09 00:11:02 +0000106 JCE->emitULEB128Bytes(-Src.getOffset());
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000107 } else {
Bill Wendling9d48b552009-09-09 00:11:02 +0000108 llvm_unreachable("Machine move not supported yet.");
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000109 }
Daniel Dunbar489032a2008-10-03 17:11:57 +0000110 } else if (Src.isReg() &&
111 Src.getReg() == MachineLocation::VirtualFP) {
112 if (Dst.isReg()) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000113 JCE->emitByte(dwarf::DW_CFA_def_cfa_register);
114 JCE->emitULEB128Bytes(RI->getDwarfRegNum(Dst.getReg(), true));
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000115 } else {
Bill Wendling9d48b552009-09-09 00:11:02 +0000116 llvm_unreachable("Machine move not supported yet.");
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000117 }
118 } else {
Daniel Dunbar489032a2008-10-03 17:11:57 +0000119 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), true);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000120 int Offset = Dst.getOffset() / stackGrowth;
121
122 if (Offset < 0) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000123 JCE->emitByte(dwarf::DW_CFA_offset_extended_sf);
124 JCE->emitULEB128Bytes(Reg);
125 JCE->emitSLEB128Bytes(Offset);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000126 } else if (Reg < 64) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000127 JCE->emitByte(dwarf::DW_CFA_offset + Reg);
128 JCE->emitULEB128Bytes(Offset);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000129 } else {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000130 JCE->emitByte(dwarf::DW_CFA_offset_extended);
131 JCE->emitULEB128Bytes(Reg);
132 JCE->emitULEB128Bytes(Offset);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000133 }
134 }
135 }
136}
137
138/// SharedTypeIds - How many leading type ids two landing pads have in common.
139static unsigned SharedTypeIds(const LandingPadInfo *L,
140 const LandingPadInfo *R) {
141 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
142 unsigned LSize = LIds.size(), RSize = RIds.size();
143 unsigned MinSize = LSize < RSize ? LSize : RSize;
144 unsigned Count = 0;
145
146 for (; Count != MinSize; ++Count)
147 if (LIds[Count] != RIds[Count])
148 return Count;
149
150 return Count;
151}
152
153
154/// PadLT - Order landing pads lexicographically by type id.
155static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
156 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
157 unsigned LSize = LIds.size(), RSize = RIds.size();
158 unsigned MinSize = LSize < RSize ? LSize : RSize;
159
160 for (unsigned i = 0; i != MinSize; ++i)
161 if (LIds[i] != RIds[i])
162 return LIds[i] < RIds[i];
163
164 return LSize < RSize;
165}
166
Dan Gohman844731a2008-05-13 00:00:25 +0000167namespace {
168
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000169/// ActionEntry - Structure describing an entry in the actions table.
170struct ActionEntry {
171 int ValueForTypeID; // The value to write - may not be equal to the type id.
172 int NextAction;
173 struct ActionEntry *Previous;
174};
175
176/// PadRange - Structure holding a try-range and the associated landing pad.
177struct PadRange {
178 // The index of the landing pad.
179 unsigned PadIndex;
180 // The index of the begin and end labels in the landing pad's label lists.
181 unsigned RangeIndex;
182};
183
Chris Lattner16112732010-03-14 01:41:15 +0000184typedef DenseMap<MCSymbol*, PadRange> RangeMapType;
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000185
186/// CallSiteEntry - Structure describing an entry in the call-site table.
187struct CallSiteEntry {
Chris Lattner16112732010-03-14 01:41:15 +0000188 MCSymbol *BeginLabel; // zero indicates the start of the function.
189 MCSymbol *EndLabel; // zero indicates the end of the function.
190 MCSymbol *PadLabel; // zero indicates that there is no landing pad.
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000191 unsigned Action;
192};
193
Dan Gohman844731a2008-05-13 00:00:25 +0000194}
195
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000196unsigned char* JITDwarfEmitter::EmitExceptionTable(MachineFunction* MF,
197 unsigned char* StartFunction,
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000198 unsigned char* EndFunction) const {
Daniel Dunbar003de662009-09-21 05:58:35 +0000199 assert(MMI && "MachineModuleInfo not registered!");
200
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000201 // Map all labels and get rid of any dead landing pads.
202 MMI->TidyLandingPads();
203
204 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
205 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
206 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
207 if (PadInfos.empty()) return 0;
208
209 // Sort the landing pads in order of their type ids. This is used to fold
210 // duplicate actions.
211 SmallVector<const LandingPadInfo *, 64> LandingPads;
212 LandingPads.reserve(PadInfos.size());
213 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
214 LandingPads.push_back(&PadInfos[i]);
215 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
216
217 // Negative type ids index into FilterIds, positive type ids index into
218 // TypeInfos. The value written for a positive type id is just the type
219 // id itself. For a negative type id, however, the value written is the
220 // (negative) byte offset of the corresponding FilterIds entry. The byte
221 // offset is usually equal to the type id, because the FilterIds entries
222 // are written using a variable width encoding which outputs one byte per
223 // entry as long as the value written is not too large, but can differ.
224 // This kind of complication does not occur for positive type ids because
225 // type infos are output using a fixed width encoding.
226 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
227 SmallVector<int, 16> FilterOffsets;
228 FilterOffsets.reserve(FilterIds.size());
229 int Offset = -1;
230 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
231 E = FilterIds.end(); I != E; ++I) {
232 FilterOffsets.push_back(Offset);
Chris Lattneraf76e592009-08-22 20:48:53 +0000233 Offset -= MCAsmInfo::getULEB128Size(*I);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000234 }
235
236 // Compute the actions table and gather the first action index for each
237 // landing pad site.
238 SmallVector<ActionEntry, 32> Actions;
239 SmallVector<unsigned, 64> FirstActions;
240 FirstActions.reserve(LandingPads.size());
241
242 int FirstAction = 0;
243 unsigned SizeActions = 0;
244 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
245 const LandingPadInfo *LP = LandingPads[i];
246 const std::vector<int> &TypeIds = LP->TypeIds;
247 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
248 unsigned SizeSiteActions = 0;
249
250 if (NumShared < TypeIds.size()) {
251 unsigned SizeAction = 0;
252 ActionEntry *PrevAction = 0;
253
254 if (NumShared) {
255 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
256 assert(Actions.size());
257 PrevAction = &Actions.back();
Chris Lattneraf76e592009-08-22 20:48:53 +0000258 SizeAction = MCAsmInfo::getSLEB128Size(PrevAction->NextAction) +
259 MCAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000260 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
Chris Lattneraf76e592009-08-22 20:48:53 +0000261 SizeAction -= MCAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000262 SizeAction += -PrevAction->NextAction;
263 PrevAction = PrevAction->Previous;
264 }
265 }
266
267 // Compute the actions.
268 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
269 int TypeID = TypeIds[I];
270 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
271 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
Chris Lattneraf76e592009-08-22 20:48:53 +0000272 unsigned SizeTypeID = MCAsmInfo::getSLEB128Size(ValueForTypeID);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000273
274 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
Chris Lattneraf76e592009-08-22 20:48:53 +0000275 SizeAction = SizeTypeID + MCAsmInfo::getSLEB128Size(NextAction);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000276 SizeSiteActions += SizeAction;
277
278 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
279 Actions.push_back(Action);
280
281 PrevAction = &Actions.back();
282 }
283
284 // Record the first action of the landing pad site.
285 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
286 } // else identical - re-use previous FirstAction
287
288 FirstActions.push_back(FirstAction);
289
290 // Compute this sites contribution to size.
291 SizeActions += SizeSiteActions;
292 }
293
294 // Compute the call-site table. Entries must be ordered by address.
295 SmallVector<CallSiteEntry, 64> CallSites;
296
297 RangeMapType PadMap;
298 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
299 const LandingPadInfo *LandingPad = LandingPads[i];
300 for (unsigned j=0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
Chris Lattner16112732010-03-14 01:41:15 +0000301 MCSymbol *BeginLabel = LandingPad->BeginLabels[j];
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000302 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
303 PadRange P = { i, j };
304 PadMap[BeginLabel] = P;
305 }
306 }
307
308 bool MayThrow = false;
Chris Lattner16112732010-03-14 01:41:15 +0000309 MCSymbol *LastLabel = 0;
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000310 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
311 I != E; ++I) {
312 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
313 MI != E; ++MI) {
Dan Gohman44066042008-07-01 00:05:16 +0000314 if (!MI->isLabel()) {
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000315 MayThrow |= MI->getDesc().isCall();
316 continue;
317 }
318
Chris Lattner16112732010-03-14 01:41:15 +0000319 unsigned BeginLabelID = MI->getOperand(0).getImm();
320 MCSymbol *BeginLabel = MMI->getLabelSym(BeginLabelID);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000321 assert(BeginLabel && "Invalid label!");
322
323 if (BeginLabel == LastLabel)
324 MayThrow = false;
325
326 RangeMapType::iterator L = PadMap.find(BeginLabel);
327
328 if (L == PadMap.end())
329 continue;
330
331 PadRange P = L->second;
332 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
333
334 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
335 "Inconsistent landing pad map!");
336
337 // If some instruction between the previous try-range and this one may
338 // throw, create a call-site entry with no landing pad for the region
339 // between the try-ranges.
340 if (MayThrow) {
341 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
342 CallSites.push_back(Site);
343 }
344
345 LastLabel = LandingPad->EndLabels[P.RangeIndex];
346 CallSiteEntry Site = {BeginLabel, LastLabel,
347 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
348
349 assert(Site.BeginLabel && Site.EndLabel && Site.PadLabel &&
350 "Invalid landing pad!");
351
352 // Try to merge with the previous call-site.
353 if (CallSites.size()) {
Dan Gohman719de532008-06-21 22:00:54 +0000354 CallSiteEntry &Prev = CallSites.back();
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000355 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
356 // Extend the range of the previous entry.
357 Prev.EndLabel = Site.EndLabel;
358 continue;
359 }
360 }
361
362 // Otherwise, create a new call-site.
363 CallSites.push_back(Site);
364 }
365 }
366 // If some instruction between the previous try-range and the end of the
367 // function may throw, create a call-site entry with no landing pad for the
368 // region following the try-range.
369 if (MayThrow) {
370 CallSiteEntry Site = {LastLabel, 0, 0, 0};
371 CallSites.push_back(Site);
372 }
373
374 // Final tallies.
375 unsigned SizeSites = CallSites.size() * (sizeof(int32_t) + // Site start.
376 sizeof(int32_t) + // Site length.
377 sizeof(int32_t)); // Landing pad.
378 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
Chris Lattneraf76e592009-08-22 20:48:53 +0000379 SizeSites += MCAsmInfo::getULEB128Size(CallSites[i].Action);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000380
381 unsigned SizeTypes = TypeInfos.size() * TD->getPointerSize();
382
383 unsigned TypeOffset = sizeof(int8_t) + // Call site format
384 // Call-site table length
Chris Lattneraf76e592009-08-22 20:48:53 +0000385 MCAsmInfo::getULEB128Size(SizeSites) +
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000386 SizeSites + SizeActions + SizeTypes;
387
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000388 // Begin the exception table.
Reid Kleckner01248e62009-08-21 21:03:57 +0000389 JCE->emitAlignmentWithFill(4, 0);
390 // Asm->EOL("Padding");
391
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000392 unsigned char* DwarfExceptionTable = (unsigned char*)JCE->getCurrentPCValue();
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000393
394 // Emit the header.
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000395 JCE->emitByte(dwarf::DW_EH_PE_omit);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000396 // Asm->EOL("LPStart format (DW_EH_PE_omit)");
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000397 JCE->emitByte(dwarf::DW_EH_PE_absptr);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000398 // Asm->EOL("TType format (DW_EH_PE_absptr)");
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000399 JCE->emitULEB128Bytes(TypeOffset);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000400 // Asm->EOL("TType base offset");
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000401 JCE->emitByte(dwarf::DW_EH_PE_udata4);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000402 // Asm->EOL("Call site format (DW_EH_PE_udata4)");
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000403 JCE->emitULEB128Bytes(SizeSites);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000404 // Asm->EOL("Call-site table length");
405
406 // Emit the landing pad site information.
407 for (unsigned i = 0; i < CallSites.size(); ++i) {
408 CallSiteEntry &S = CallSites[i];
409 intptr_t BeginLabelPtr = 0;
410 intptr_t EndLabelPtr = 0;
411
412 if (!S.BeginLabel) {
413 BeginLabelPtr = (intptr_t)StartFunction;
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000414 JCE->emitInt32(0);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000415 } else {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000416 BeginLabelPtr = JCE->getLabelAddress(S.BeginLabel);
417 JCE->emitInt32(BeginLabelPtr - (intptr_t)StartFunction);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000418 }
419
420 // Asm->EOL("Region start");
421
Bill Wendlingabeca442009-12-28 01:53:00 +0000422 if (!S.EndLabel)
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000423 EndLabelPtr = (intptr_t)EndFunction;
Bill Wendlingabeca442009-12-28 01:53:00 +0000424 else
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000425 EndLabelPtr = JCE->getLabelAddress(S.EndLabel);
Bill Wendlingabeca442009-12-28 01:53:00 +0000426
427 JCE->emitInt32(EndLabelPtr - BeginLabelPtr);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000428 //Asm->EOL("Region length");
429
430 if (!S.PadLabel) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000431 JCE->emitInt32(0);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000432 } else {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000433 unsigned PadLabelPtr = JCE->getLabelAddress(S.PadLabel);
434 JCE->emitInt32(PadLabelPtr - (intptr_t)StartFunction);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000435 }
436 // Asm->EOL("Landing pad");
437
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000438 JCE->emitULEB128Bytes(S.Action);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000439 // Asm->EOL("Action");
440 }
441
442 // Emit the actions.
443 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
444 ActionEntry &Action = Actions[I];
445
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000446 JCE->emitSLEB128Bytes(Action.ValueForTypeID);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000447 //Asm->EOL("TypeInfo index");
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000448 JCE->emitSLEB128Bytes(Action.NextAction);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000449 //Asm->EOL("Next action");
450 }
451
452 // Emit the type ids.
453 for (unsigned M = TypeInfos.size(); M; --M) {
454 GlobalVariable *GV = TypeInfos[M - 1];
455
456 if (GV) {
Bill Wendling9d48b552009-09-09 00:11:02 +0000457 if (TD->getPointerSize() == sizeof(int32_t))
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000458 JCE->emitInt32((intptr_t)Jit.getOrEmitGlobalVariable(GV));
Bill Wendling9d48b552009-09-09 00:11:02 +0000459 else
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000460 JCE->emitInt64((intptr_t)Jit.getOrEmitGlobalVariable(GV));
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000461 } else {
462 if (TD->getPointerSize() == sizeof(int32_t))
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000463 JCE->emitInt32(0);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000464 else
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000465 JCE->emitInt64(0);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000466 }
467 // Asm->EOL("TypeInfo");
468 }
469
470 // Emit the filter typeids.
471 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
472 unsigned TypeID = FilterIds[j];
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000473 JCE->emitULEB128Bytes(TypeID);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000474 //Asm->EOL("Filter TypeInfo index");
475 }
Reid Kleckner01248e62009-08-21 21:03:57 +0000476
477 JCE->emitAlignmentWithFill(4, 0);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000478
479 return DwarfExceptionTable;
480}
481
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000482unsigned char*
483JITDwarfEmitter::EmitCommonEHFrame(const Function* Personality) const {
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000484 unsigned PointerSize = TD->getPointerSize();
485 int stackGrowth = stackGrowthDirection == TargetFrameInfo::StackGrowsUp ?
486 PointerSize : -PointerSize;
487
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000488 unsigned char* StartCommonPtr = (unsigned char*)JCE->getCurrentPCValue();
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000489 // EH Common Frame header
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000490 JCE->allocateSpace(4, 0);
491 unsigned char* FrameCommonBeginPtr = (unsigned char*)JCE->getCurrentPCValue();
492 JCE->emitInt32((int)0);
493 JCE->emitByte(dwarf::DW_CIE_VERSION);
494 JCE->emitString(Personality ? "zPLR" : "zR");
495 JCE->emitULEB128Bytes(1);
496 JCE->emitSLEB128Bytes(stackGrowth);
497 JCE->emitByte(RI->getDwarfRegNum(RI->getRARegister(), true));
Bill Wendling9d48b552009-09-09 00:11:02 +0000498
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000499 if (Personality) {
Nicolas Geoffray42cc8f12009-02-15 20:49:23 +0000500 // Augmentation Size: 3 small ULEBs of one byte each, and the personality
501 // function which size is PointerSize.
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000502 JCE->emitULEB128Bytes(3 + PointerSize);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000503
Nicolas Geoffray42cc8f12009-02-15 20:49:23 +0000504 // We set the encoding of the personality as direct encoding because we use
505 // the function pointer. The encoding is not relative because the current
506 // PC value may be bigger than the personality function pointer.
507 if (PointerSize == 4) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000508 JCE->emitByte(dwarf::DW_EH_PE_sdata4);
509 JCE->emitInt32(((intptr_t)Jit.getPointerToGlobal(Personality)));
Nicolas Geoffray42cc8f12009-02-15 20:49:23 +0000510 } else {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000511 JCE->emitByte(dwarf::DW_EH_PE_sdata8);
512 JCE->emitInt64(((intptr_t)Jit.getPointerToGlobal(Personality)));
Nicolas Geoffray42cc8f12009-02-15 20:49:23 +0000513 }
Bill Wendling9d48b552009-09-09 00:11:02 +0000514
Bill Wendling89ee7062010-02-16 00:58:02 +0000515 // LSDA encoding: This must match the encoding used in EmitEHFrame ()
516 if (PointerSize == 4)
517 JCE->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
518 else
519 JCE->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8);
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000520 JCE->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000521 } else {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000522 JCE->emitULEB128Bytes(1);
523 JCE->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000524 }
525
526 std::vector<MachineMove> Moves;
527 RI->getInitialFrameState(Moves);
528 EmitFrameMoves(0, Moves);
Reid Kleckner01248e62009-08-21 21:03:57 +0000529
530 JCE->emitAlignmentWithFill(PointerSize, dwarf::DW_CFA_nop);
531
532 JCE->emitInt32At((uintptr_t*)StartCommonPtr,
533 (uintptr_t)((unsigned char*)JCE->getCurrentPCValue() -
534 FrameCommonBeginPtr));
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000535
536 return StartCommonPtr;
537}
538
539
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000540unsigned char*
541JITDwarfEmitter::EmitEHFrame(const Function* Personality,
542 unsigned char* StartCommonPtr,
543 unsigned char* StartFunction,
544 unsigned char* EndFunction,
545 unsigned char* ExceptionTable) const {
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000546 unsigned PointerSize = TD->getPointerSize();
547
548 // EH frame header.
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000549 unsigned char* StartEHPtr = (unsigned char*)JCE->getCurrentPCValue();
550 JCE->allocateSpace(4, 0);
551 unsigned char* FrameBeginPtr = (unsigned char*)JCE->getCurrentPCValue();
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000552 // FDE CIE Offset
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000553 JCE->emitInt32(FrameBeginPtr - StartCommonPtr);
554 JCE->emitInt32(StartFunction - (unsigned char*)JCE->getCurrentPCValue());
555 JCE->emitInt32(EndFunction - StartFunction);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000556
557 // If there is a personality and landing pads then point to the language
558 // specific data area in the exception table.
Bill Wendling9d48b552009-09-09 00:11:02 +0000559 if (Personality) {
560 JCE->emitULEB128Bytes(PointerSize == 4 ? 4 : 8);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000561
Bill Wendling9d48b552009-09-09 00:11:02 +0000562 if (PointerSize == 4) {
563 if (!MMI->getLandingPads().empty())
564 JCE->emitInt32(ExceptionTable-(unsigned char*)JCE->getCurrentPCValue());
565 else
566 JCE->emitInt32((int)0);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000567 } else {
Bill Wendling9d48b552009-09-09 00:11:02 +0000568 if (!MMI->getLandingPads().empty())
569 JCE->emitInt64(ExceptionTable-(unsigned char*)JCE->getCurrentPCValue());
570 else
571 JCE->emitInt64((int)0);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000572 }
573 } else {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000574 JCE->emitULEB128Bytes(0);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000575 }
576
577 // Indicate locations of function specific callee saved registers in
578 // frame.
579 EmitFrameMoves((intptr_t)StartFunction, MMI->getFrameMoves());
Reid Kleckner01248e62009-08-21 21:03:57 +0000580
581 JCE->emitAlignmentWithFill(PointerSize, dwarf::DW_CFA_nop);
582
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000583 // Indicate the size of the table
Reid Kleckner01248e62009-08-21 21:03:57 +0000584 JCE->emitInt32At((uintptr_t*)StartEHPtr,
585 (uintptr_t)((unsigned char*)JCE->getCurrentPCValue() -
586 StartEHPtr));
587
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000588 // Double zeroes for the unwind runtime
589 if (PointerSize == 8) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000590 JCE->emitInt64(0);
591 JCE->emitInt64(0);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000592 } else {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000593 JCE->emitInt32(0);
594 JCE->emitInt32(0);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000595 }
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000596
597 return StartEHPtr;
598}
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000599
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000600unsigned JITDwarfEmitter::GetDwarfTableSizeInBytes(MachineFunction& F,
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000601 JITCodeEmitter& jce,
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000602 unsigned char* StartFunction,
603 unsigned char* EndFunction) {
604 const TargetMachine& TM = F.getTarget();
605 TD = TM.getTargetData();
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000606 stackGrowthDirection = TM.getFrameInfo()->getStackGrowthDirection();
607 RI = TM.getRegisterInfo();
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000608 JCE = &jce;
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000609 unsigned FinalSize = 0;
610
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000611 FinalSize += GetExceptionTableSizeInBytes(&F);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000612
613 const std::vector<Function *> Personalities = MMI->getPersonalities();
Nicolas Geoffray67c8c4c2008-11-18 10:44:46 +0000614 FinalSize +=
615 GetCommonEHFrameSizeInBytes(Personalities[MMI->getPersonalityIndex()]);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000616
Nicolas Geoffray67c8c4c2008-11-18 10:44:46 +0000617 FinalSize += GetEHFrameSizeInBytes(Personalities[MMI->getPersonalityIndex()],
618 StartFunction);
Bill Wendling9d48b552009-09-09 00:11:02 +0000619
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000620 return FinalSize;
621}
622
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000623/// RoundUpToAlign - Add the specified alignment to FinalSize and returns
624/// the new value.
625static unsigned RoundUpToAlign(unsigned FinalSize, unsigned Alignment) {
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000626 if (Alignment == 0) Alignment = 1;
Nicolas Geoffray580631a2008-04-20 23:39:44 +0000627 // Since we do not know where the buffer will be allocated, be pessimistic.
628 return FinalSize + Alignment;
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000629}
630
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000631unsigned
632JITDwarfEmitter::GetEHFrameSizeInBytes(const Function* Personality,
633 unsigned char* StartFunction) const {
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000634 unsigned PointerSize = TD->getPointerSize();
635 unsigned FinalSize = 0;
636 // EH frame header.
637 FinalSize += PointerSize;
638 // FDE CIE Offset
639 FinalSize += 3 * PointerSize;
640 // If there is a personality and landing pads then point to the language
641 // specific data area in the exception table.
Bill Wendling9d48b552009-09-09 00:11:02 +0000642 if (Personality) {
Chris Lattneraf76e592009-08-22 20:48:53 +0000643 FinalSize += MCAsmInfo::getULEB128Size(4);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000644 FinalSize += PointerSize;
645 } else {
Chris Lattneraf76e592009-08-22 20:48:53 +0000646 FinalSize += MCAsmInfo::getULEB128Size(0);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000647 }
648
649 // Indicate locations of function specific callee saved registers in
650 // frame.
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000651 FinalSize += GetFrameMovesSizeInBytes((intptr_t)StartFunction,
652 MMI->getFrameMoves());
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000653
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000654 FinalSize = RoundUpToAlign(FinalSize, 4);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000655
656 // Double zeroes for the unwind runtime
657 FinalSize += 2 * PointerSize;
658
659 return FinalSize;
660}
661
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000662unsigned JITDwarfEmitter::GetCommonEHFrameSizeInBytes(const Function* Personality)
663 const {
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000664
665 unsigned PointerSize = TD->getPointerSize();
666 int stackGrowth = stackGrowthDirection == TargetFrameInfo::StackGrowsUp ?
667 PointerSize : -PointerSize;
668 unsigned FinalSize = 0;
669 // EH Common Frame header
670 FinalSize += PointerSize;
671 FinalSize += 4;
672 FinalSize += 1;
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000673 FinalSize += Personality ? 5 : 3; // "zPLR" or "zR"
Chris Lattneraf76e592009-08-22 20:48:53 +0000674 FinalSize += MCAsmInfo::getULEB128Size(1);
675 FinalSize += MCAsmInfo::getSLEB128Size(stackGrowth);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000676 FinalSize += 1;
677
678 if (Personality) {
Chris Lattneraf76e592009-08-22 20:48:53 +0000679 FinalSize += MCAsmInfo::getULEB128Size(7);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000680
681 // Encoding
682 FinalSize+= 1;
683 //Personality
684 FinalSize += PointerSize;
685
Chris Lattneraf76e592009-08-22 20:48:53 +0000686 FinalSize += MCAsmInfo::getULEB128Size(dwarf::DW_EH_PE_pcrel);
687 FinalSize += MCAsmInfo::getULEB128Size(dwarf::DW_EH_PE_pcrel);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000688
689 } else {
Chris Lattneraf76e592009-08-22 20:48:53 +0000690 FinalSize += MCAsmInfo::getULEB128Size(1);
691 FinalSize += MCAsmInfo::getULEB128Size(dwarf::DW_EH_PE_pcrel);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000692 }
693
694 std::vector<MachineMove> Moves;
695 RI->getInitialFrameState(Moves);
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000696 FinalSize += GetFrameMovesSizeInBytes(0, Moves);
697 FinalSize = RoundUpToAlign(FinalSize, 4);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000698 return FinalSize;
699}
700
701unsigned
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000702JITDwarfEmitter::GetFrameMovesSizeInBytes(intptr_t BaseLabelPtr,
703 const std::vector<MachineMove> &Moves) const {
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000704 unsigned PointerSize = TD->getPointerSize();
705 int stackGrowth = stackGrowthDirection == TargetFrameInfo::StackGrowsUp ?
706 PointerSize : -PointerSize;
707 bool IsLocal = BaseLabelPtr;
708 unsigned FinalSize = 0;
709
710 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
711 const MachineMove &Move = Moves[i];
Chris Lattner2e9919a2010-03-14 08:12:40 +0000712 MCSymbol *Label = Move.getLabel();
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000713
Chris Lattner16112732010-03-14 01:41:15 +0000714 // Throw out move if the label is invalid.
715 if (Label && !Label->isDefined())
716 continue;
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000717
718 intptr_t LabelPtr = 0;
Chris Lattner2e9919a2010-03-14 08:12:40 +0000719 if (Label) LabelPtr = JCE->getLabelAddress(Label);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000720
721 const MachineLocation &Dst = Move.getDestination();
722 const MachineLocation &Src = Move.getSource();
723
724 // Advance row if new location.
Chris Lattner2e9919a2010-03-14 08:12:40 +0000725 if (BaseLabelPtr && Label && (BaseLabelPtr != LabelPtr || !IsLocal)) {
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000726 FinalSize++;
727 FinalSize += PointerSize;
728 BaseLabelPtr = LabelPtr;
729 IsLocal = true;
730 }
731
732 // If advancing cfa.
Daniel Dunbar489032a2008-10-03 17:11:57 +0000733 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
734 if (!Src.isReg()) {
735 if (Src.getReg() == MachineLocation::VirtualFP) {
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000736 ++FinalSize;
737 } else {
738 ++FinalSize;
Daniel Dunbar489032a2008-10-03 17:11:57 +0000739 unsigned RegNum = RI->getDwarfRegNum(Src.getReg(), true);
Chris Lattneraf76e592009-08-22 20:48:53 +0000740 FinalSize += MCAsmInfo::getULEB128Size(RegNum);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000741 }
742
743 int Offset = -Src.getOffset();
744
Chris Lattneraf76e592009-08-22 20:48:53 +0000745 FinalSize += MCAsmInfo::getULEB128Size(Offset);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000746 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000747 llvm_unreachable("Machine move no supported yet.");
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000748 }
Daniel Dunbar489032a2008-10-03 17:11:57 +0000749 } else if (Src.isReg() &&
750 Src.getReg() == MachineLocation::VirtualFP) {
751 if (Dst.isReg()) {
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000752 ++FinalSize;
Daniel Dunbar489032a2008-10-03 17:11:57 +0000753 unsigned RegNum = RI->getDwarfRegNum(Dst.getReg(), true);
Chris Lattneraf76e592009-08-22 20:48:53 +0000754 FinalSize += MCAsmInfo::getULEB128Size(RegNum);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000755 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000756 llvm_unreachable("Machine move no supported yet.");
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000757 }
758 } else {
Daniel Dunbar489032a2008-10-03 17:11:57 +0000759 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), true);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000760 int Offset = Dst.getOffset() / stackGrowth;
761
762 if (Offset < 0) {
763 ++FinalSize;
Chris Lattneraf76e592009-08-22 20:48:53 +0000764 FinalSize += MCAsmInfo::getULEB128Size(Reg);
765 FinalSize += MCAsmInfo::getSLEB128Size(Offset);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000766 } else if (Reg < 64) {
767 ++FinalSize;
Chris Lattneraf76e592009-08-22 20:48:53 +0000768 FinalSize += MCAsmInfo::getULEB128Size(Offset);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000769 } else {
770 ++FinalSize;
Chris Lattneraf76e592009-08-22 20:48:53 +0000771 FinalSize += MCAsmInfo::getULEB128Size(Reg);
772 FinalSize += MCAsmInfo::getULEB128Size(Offset);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000773 }
774 }
775 }
776 return FinalSize;
777}
778
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000779unsigned
780JITDwarfEmitter::GetExceptionTableSizeInBytes(MachineFunction* MF) const {
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000781 unsigned FinalSize = 0;
782
783 // Map all labels and get rid of any dead landing pads.
784 MMI->TidyLandingPads();
785
786 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
787 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
788 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
789 if (PadInfos.empty()) return 0;
790
791 // Sort the landing pads in order of their type ids. This is used to fold
792 // duplicate actions.
793 SmallVector<const LandingPadInfo *, 64> LandingPads;
794 LandingPads.reserve(PadInfos.size());
795 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
796 LandingPads.push_back(&PadInfos[i]);
797 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
798
799 // Negative type ids index into FilterIds, positive type ids index into
800 // TypeInfos. The value written for a positive type id is just the type
801 // id itself. For a negative type id, however, the value written is the
802 // (negative) byte offset of the corresponding FilterIds entry. The byte
803 // offset is usually equal to the type id, because the FilterIds entries
804 // are written using a variable width encoding which outputs one byte per
805 // entry as long as the value written is not too large, but can differ.
806 // This kind of complication does not occur for positive type ids because
807 // type infos are output using a fixed width encoding.
808 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
809 SmallVector<int, 16> FilterOffsets;
810 FilterOffsets.reserve(FilterIds.size());
811 int Offset = -1;
812 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
813 E = FilterIds.end(); I != E; ++I) {
814 FilterOffsets.push_back(Offset);
Chris Lattneraf76e592009-08-22 20:48:53 +0000815 Offset -= MCAsmInfo::getULEB128Size(*I);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000816 }
817
818 // Compute the actions table and gather the first action index for each
819 // landing pad site.
820 SmallVector<ActionEntry, 32> Actions;
821 SmallVector<unsigned, 64> FirstActions;
822 FirstActions.reserve(LandingPads.size());
823
824 int FirstAction = 0;
825 unsigned SizeActions = 0;
826 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
827 const LandingPadInfo *LP = LandingPads[i];
828 const std::vector<int> &TypeIds = LP->TypeIds;
829 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
830 unsigned SizeSiteActions = 0;
831
832 if (NumShared < TypeIds.size()) {
833 unsigned SizeAction = 0;
834 ActionEntry *PrevAction = 0;
835
836 if (NumShared) {
837 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
838 assert(Actions.size());
839 PrevAction = &Actions.back();
Chris Lattneraf76e592009-08-22 20:48:53 +0000840 SizeAction = MCAsmInfo::getSLEB128Size(PrevAction->NextAction) +
841 MCAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000842 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
Chris Lattneraf76e592009-08-22 20:48:53 +0000843 SizeAction -= MCAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000844 SizeAction += -PrevAction->NextAction;
845 PrevAction = PrevAction->Previous;
846 }
847 }
848
849 // Compute the actions.
850 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
851 int TypeID = TypeIds[I];
852 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
853 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
Chris Lattneraf76e592009-08-22 20:48:53 +0000854 unsigned SizeTypeID = MCAsmInfo::getSLEB128Size(ValueForTypeID);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000855
856 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
Chris Lattneraf76e592009-08-22 20:48:53 +0000857 SizeAction = SizeTypeID + MCAsmInfo::getSLEB128Size(NextAction);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000858 SizeSiteActions += SizeAction;
859
860 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
861 Actions.push_back(Action);
862
863 PrevAction = &Actions.back();
864 }
865
866 // Record the first action of the landing pad site.
867 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
868 } // else identical - re-use previous FirstAction
869
870 FirstActions.push_back(FirstAction);
871
872 // Compute this sites contribution to size.
873 SizeActions += SizeSiteActions;
874 }
875
876 // Compute the call-site table. Entries must be ordered by address.
877 SmallVector<CallSiteEntry, 64> CallSites;
878
879 RangeMapType PadMap;
880 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
881 const LandingPadInfo *LandingPad = LandingPads[i];
882 for (unsigned j=0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
Chris Lattner16112732010-03-14 01:41:15 +0000883 MCSymbol *BeginLabel = LandingPad->BeginLabels[j];
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000884 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
885 PadRange P = { i, j };
886 PadMap[BeginLabel] = P;
887 }
888 }
889
890 bool MayThrow = false;
Chris Lattner16112732010-03-14 01:41:15 +0000891 MCSymbol *LastLabel = 0;
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000892 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
893 I != E; ++I) {
894 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
895 MI != E; ++MI) {
Dan Gohman44066042008-07-01 00:05:16 +0000896 if (!MI->isLabel()) {
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000897 MayThrow |= MI->getDesc().isCall();
898 continue;
899 }
900
Chris Lattner16112732010-03-14 01:41:15 +0000901 unsigned BeginLabelID = MI->getOperand(0).getImm();
902 assert(BeginLabelID && "Invalid label!");
903 MCSymbol *BeginLabel = MMI->getLabelSym(BeginLabelID);
904
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000905 if (BeginLabel == LastLabel)
906 MayThrow = false;
907
908 RangeMapType::iterator L = PadMap.find(BeginLabel);
909
910 if (L == PadMap.end())
911 continue;
912
913 PadRange P = L->second;
914 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
915
916 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
917 "Inconsistent landing pad map!");
918
919 // If some instruction between the previous try-range and this one may
920 // throw, create a call-site entry with no landing pad for the region
921 // between the try-ranges.
922 if (MayThrow) {
923 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
924 CallSites.push_back(Site);
925 }
926
927 LastLabel = LandingPad->EndLabels[P.RangeIndex];
928 CallSiteEntry Site = {BeginLabel, LastLabel,
929 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
930
931 assert(Site.BeginLabel && Site.EndLabel && Site.PadLabel &&
932 "Invalid landing pad!");
933
934 // Try to merge with the previous call-site.
935 if (CallSites.size()) {
Dan Gohman719de532008-06-21 22:00:54 +0000936 CallSiteEntry &Prev = CallSites.back();
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000937 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
938 // Extend the range of the previous entry.
939 Prev.EndLabel = Site.EndLabel;
940 continue;
941 }
942 }
943
944 // Otherwise, create a new call-site.
945 CallSites.push_back(Site);
946 }
947 }
948 // If some instruction between the previous try-range and the end of the
949 // function may throw, create a call-site entry with no landing pad for the
950 // region following the try-range.
951 if (MayThrow) {
952 CallSiteEntry Site = {LastLabel, 0, 0, 0};
953 CallSites.push_back(Site);
954 }
955
956 // Final tallies.
957 unsigned SizeSites = CallSites.size() * (sizeof(int32_t) + // Site start.
958 sizeof(int32_t) + // Site length.
959 sizeof(int32_t)); // Landing pad.
960 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
Chris Lattneraf76e592009-08-22 20:48:53 +0000961 SizeSites += MCAsmInfo::getULEB128Size(CallSites[i].Action);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000962
963 unsigned SizeTypes = TypeInfos.size() * TD->getPointerSize();
964
965 unsigned TypeOffset = sizeof(int8_t) + // Call site format
966 // Call-site table length
Chris Lattneraf76e592009-08-22 20:48:53 +0000967 MCAsmInfo::getULEB128Size(SizeSites) +
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000968 SizeSites + SizeActions + SizeTypes;
969
970 unsigned TotalSize = sizeof(int8_t) + // LPStart format
971 sizeof(int8_t) + // TType format
Chris Lattneraf76e592009-08-22 20:48:53 +0000972 MCAsmInfo::getULEB128Size(TypeOffset) + // TType base offset
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000973 TypeOffset;
974
975 unsigned SizeAlign = (4 - TotalSize) & 3;
976
977 // Begin the exception table.
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000978 FinalSize = RoundUpToAlign(FinalSize, 4);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000979 for (unsigned i = 0; i != SizeAlign; ++i) {
980 ++FinalSize;
981 }
982
983 unsigned PointerSize = TD->getPointerSize();
984
985 // Emit the header.
986 ++FinalSize;
987 // Asm->EOL("LPStart format (DW_EH_PE_omit)");
988 ++FinalSize;
989 // Asm->EOL("TType format (DW_EH_PE_absptr)");
990 ++FinalSize;
991 // Asm->EOL("TType base offset");
992 ++FinalSize;
993 // Asm->EOL("Call site format (DW_EH_PE_udata4)");
994 ++FinalSize;
995 // Asm->EOL("Call-site table length");
996
997 // Emit the landing pad site information.
998 for (unsigned i = 0; i < CallSites.size(); ++i) {
999 CallSiteEntry &S = CallSites[i];
1000
1001 // Asm->EOL("Region start");
1002 FinalSize += PointerSize;
1003
1004 //Asm->EOL("Region length");
1005 FinalSize += PointerSize;
1006
1007 // Asm->EOL("Landing pad");
1008 FinalSize += PointerSize;
1009
Chris Lattneraf76e592009-08-22 20:48:53 +00001010 FinalSize += MCAsmInfo::getULEB128Size(S.Action);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +00001011 // Asm->EOL("Action");
1012 }
1013
1014 // Emit the actions.
1015 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
1016 ActionEntry &Action = Actions[I];
1017
1018 //Asm->EOL("TypeInfo index");
Chris Lattneraf76e592009-08-22 20:48:53 +00001019 FinalSize += MCAsmInfo::getSLEB128Size(Action.ValueForTypeID);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +00001020 //Asm->EOL("Next action");
Chris Lattneraf76e592009-08-22 20:48:53 +00001021 FinalSize += MCAsmInfo::getSLEB128Size(Action.NextAction);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +00001022 }
1023
1024 // Emit the type ids.
1025 for (unsigned M = TypeInfos.size(); M; --M) {
1026 // Asm->EOL("TypeInfo");
1027 FinalSize += PointerSize;
1028 }
1029
1030 // Emit the filter typeids.
1031 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
1032 unsigned TypeID = FilterIds[j];
Chris Lattneraf76e592009-08-22 20:48:53 +00001033 FinalSize += MCAsmInfo::getULEB128Size(TypeID);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +00001034 //Asm->EOL("Filter TypeInfo index");
1035 }
1036
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +00001037 FinalSize = RoundUpToAlign(FinalSize, 4);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +00001038
1039 return FinalSize;
1040}