blob: da5435a0c70d26c2e23a33c831e228a8e9216f1e [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"
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000026#include "llvm/Target/TargetData.h"
27#include "llvm/Target/TargetInstrInfo.h"
28#include "llvm/Target/TargetFrameInfo.h"
29#include "llvm/Target/TargetMachine.h"
30#include "llvm/Target/TargetRegisterInfo.h"
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000031using namespace llvm;
32
Daniel Dunbar003de662009-09-21 05:58:35 +000033JITDwarfEmitter::JITDwarfEmitter(JIT& theJit) : MMI(0), Jit(theJit) {}
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000034
35
36unsigned char* JITDwarfEmitter::EmitDwarfTable(MachineFunction& F,
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000037 JITCodeEmitter& jce,
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000038 unsigned char* StartFunction,
Reid Kleckner27632172009-09-20 23:52:43 +000039 unsigned char* EndFunction,
40 unsigned char* &EHFramePtr) {
Daniel Dunbar003de662009-09-21 05:58:35 +000041 assert(MMI && "MachineModuleInfo not registered!");
42
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000043 const TargetMachine& TM = F.getTarget();
44 TD = TM.getTargetData();
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000045 stackGrowthDirection = TM.getFrameInfo()->getStackGrowthDirection();
46 RI = TM.getRegisterInfo();
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000047 JCE = &jce;
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000048
49 unsigned char* ExceptionTable = EmitExceptionTable(&F, StartFunction,
50 EndFunction);
51
52 unsigned char* Result = 0;
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000053
54 const std::vector<Function *> Personalities = MMI->getPersonalities();
55 EHFramePtr = EmitCommonEHFrame(Personalities[MMI->getPersonalityIndex()]);
56
57 Result = EmitEHFrame(Personalities[MMI->getPersonalityIndex()], EHFramePtr,
58 StartFunction, EndFunction, ExceptionTable);
Bill Wendling9d48b552009-09-09 00:11:02 +000059
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000060 return Result;
61}
62
63
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +000064void
65JITDwarfEmitter::EmitFrameMoves(intptr_t BaseLabelPtr,
66 const std::vector<MachineMove> &Moves) const {
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000067 unsigned PointerSize = TD->getPointerSize();
68 int stackGrowth = stackGrowthDirection == TargetFrameInfo::StackGrowsUp ?
69 PointerSize : -PointerSize;
Nicolas Geoffray2d450eb2008-08-19 14:48:14 +000070 bool IsLocal = false;
71 unsigned BaseLabelID = 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];
75 unsigned LabelID = Move.getLabelID();
76
77 if (LabelID) {
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000078 // Throw out move if the label is invalid.
Chris Lattnera34ec2292010-03-09 01:51:43 +000079 if (MMI->isLabelDeleted(LabelID))
80 continue;
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000081 }
82
83 intptr_t LabelPtr = 0;
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000084 if (LabelID) LabelPtr = JCE->getLabelAddress(LabelID);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000085
86 const MachineLocation &Dst = Move.getDestination();
87 const MachineLocation &Src = Move.getSource();
88
89 // Advance row if new location.
Nicolas Geoffray2d450eb2008-08-19 14:48:14 +000090 if (BaseLabelPtr && LabelID && (BaseLabelID != LabelID || !IsLocal)) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000091 JCE->emitByte(dwarf::DW_CFA_advance_loc4);
92 JCE->emitInt32(LabelPtr - BaseLabelPtr);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000093
Nicolas Geoffray2d450eb2008-08-19 14:48:14 +000094 BaseLabelID = LabelID;
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000095 BaseLabelPtr = LabelPtr;
96 IsLocal = true;
97 }
98
99 // If advancing cfa.
Daniel Dunbar489032a2008-10-03 17:11:57 +0000100 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
101 if (!Src.isReg()) {
102 if (Src.getReg() == MachineLocation::VirtualFP) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000103 JCE->emitByte(dwarf::DW_CFA_def_cfa_offset);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000104 } else {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000105 JCE->emitByte(dwarf::DW_CFA_def_cfa);
106 JCE->emitULEB128Bytes(RI->getDwarfRegNum(Src.getReg(), true));
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000107 }
108
Bill Wendling9d48b552009-09-09 00:11:02 +0000109 JCE->emitULEB128Bytes(-Src.getOffset());
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000110 } else {
Bill Wendling9d48b552009-09-09 00:11:02 +0000111 llvm_unreachable("Machine move not supported yet.");
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000112 }
Daniel Dunbar489032a2008-10-03 17:11:57 +0000113 } else if (Src.isReg() &&
114 Src.getReg() == MachineLocation::VirtualFP) {
115 if (Dst.isReg()) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000116 JCE->emitByte(dwarf::DW_CFA_def_cfa_register);
117 JCE->emitULEB128Bytes(RI->getDwarfRegNum(Dst.getReg(), true));
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000118 } else {
Bill Wendling9d48b552009-09-09 00:11:02 +0000119 llvm_unreachable("Machine move not supported yet.");
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000120 }
121 } else {
Daniel Dunbar489032a2008-10-03 17:11:57 +0000122 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), true);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000123 int Offset = Dst.getOffset() / stackGrowth;
124
125 if (Offset < 0) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000126 JCE->emitByte(dwarf::DW_CFA_offset_extended_sf);
127 JCE->emitULEB128Bytes(Reg);
128 JCE->emitSLEB128Bytes(Offset);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000129 } else if (Reg < 64) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000130 JCE->emitByte(dwarf::DW_CFA_offset + Reg);
131 JCE->emitULEB128Bytes(Offset);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000132 } else {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000133 JCE->emitByte(dwarf::DW_CFA_offset_extended);
134 JCE->emitULEB128Bytes(Reg);
135 JCE->emitULEB128Bytes(Offset);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000136 }
137 }
138 }
139}
140
141/// SharedTypeIds - How many leading type ids two landing pads have in common.
142static unsigned SharedTypeIds(const LandingPadInfo *L,
143 const LandingPadInfo *R) {
144 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
145 unsigned LSize = LIds.size(), RSize = RIds.size();
146 unsigned MinSize = LSize < RSize ? LSize : RSize;
147 unsigned Count = 0;
148
149 for (; Count != MinSize; ++Count)
150 if (LIds[Count] != RIds[Count])
151 return Count;
152
153 return Count;
154}
155
156
157/// PadLT - Order landing pads lexicographically by type id.
158static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
159 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
160 unsigned LSize = LIds.size(), RSize = RIds.size();
161 unsigned MinSize = LSize < RSize ? LSize : RSize;
162
163 for (unsigned i = 0; i != MinSize; ++i)
164 if (LIds[i] != RIds[i])
165 return LIds[i] < RIds[i];
166
167 return LSize < RSize;
168}
169
Dan Gohman844731a2008-05-13 00:00:25 +0000170namespace {
171
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000172struct KeyInfo {
173 static inline unsigned getEmptyKey() { return -1U; }
174 static inline unsigned getTombstoneKey() { return -2U; }
175 static unsigned getHashValue(const unsigned &Key) { return Key; }
176 static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000177};
178
179/// ActionEntry - Structure describing an entry in the actions table.
180struct ActionEntry {
181 int ValueForTypeID; // The value to write - may not be equal to the type id.
182 int NextAction;
183 struct ActionEntry *Previous;
184};
185
186/// PadRange - Structure holding a try-range and the associated landing pad.
187struct PadRange {
188 // The index of the landing pad.
189 unsigned PadIndex;
190 // The index of the begin and end labels in the landing pad's label lists.
191 unsigned RangeIndex;
192};
193
194typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
195
196/// CallSiteEntry - Structure describing an entry in the call-site table.
197struct CallSiteEntry {
198 unsigned BeginLabel; // zero indicates the start of the function.
199 unsigned EndLabel; // zero indicates the end of the function.
200 unsigned PadLabel; // zero indicates that there is no landing pad.
201 unsigned Action;
202};
203
Dan Gohman844731a2008-05-13 00:00:25 +0000204}
205
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000206unsigned char* JITDwarfEmitter::EmitExceptionTable(MachineFunction* MF,
207 unsigned char* StartFunction,
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000208 unsigned char* EndFunction) const {
Daniel Dunbar003de662009-09-21 05:58:35 +0000209 assert(MMI && "MachineModuleInfo not registered!");
210
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000211 // Map all labels and get rid of any dead landing pads.
212 MMI->TidyLandingPads();
213
214 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
215 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
216 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
217 if (PadInfos.empty()) return 0;
218
219 // Sort the landing pads in order of their type ids. This is used to fold
220 // duplicate actions.
221 SmallVector<const LandingPadInfo *, 64> LandingPads;
222 LandingPads.reserve(PadInfos.size());
223 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
224 LandingPads.push_back(&PadInfos[i]);
225 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
226
227 // Negative type ids index into FilterIds, positive type ids index into
228 // TypeInfos. The value written for a positive type id is just the type
229 // id itself. For a negative type id, however, the value written is the
230 // (negative) byte offset of the corresponding FilterIds entry. The byte
231 // offset is usually equal to the type id, because the FilterIds entries
232 // are written using a variable width encoding which outputs one byte per
233 // entry as long as the value written is not too large, but can differ.
234 // This kind of complication does not occur for positive type ids because
235 // type infos are output using a fixed width encoding.
236 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
237 SmallVector<int, 16> FilterOffsets;
238 FilterOffsets.reserve(FilterIds.size());
239 int Offset = -1;
240 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
241 E = FilterIds.end(); I != E; ++I) {
242 FilterOffsets.push_back(Offset);
Chris Lattneraf76e592009-08-22 20:48:53 +0000243 Offset -= MCAsmInfo::getULEB128Size(*I);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000244 }
245
246 // Compute the actions table and gather the first action index for each
247 // landing pad site.
248 SmallVector<ActionEntry, 32> Actions;
249 SmallVector<unsigned, 64> FirstActions;
250 FirstActions.reserve(LandingPads.size());
251
252 int FirstAction = 0;
253 unsigned SizeActions = 0;
254 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
255 const LandingPadInfo *LP = LandingPads[i];
256 const std::vector<int> &TypeIds = LP->TypeIds;
257 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
258 unsigned SizeSiteActions = 0;
259
260 if (NumShared < TypeIds.size()) {
261 unsigned SizeAction = 0;
262 ActionEntry *PrevAction = 0;
263
264 if (NumShared) {
265 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
266 assert(Actions.size());
267 PrevAction = &Actions.back();
Chris Lattneraf76e592009-08-22 20:48:53 +0000268 SizeAction = MCAsmInfo::getSLEB128Size(PrevAction->NextAction) +
269 MCAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000270 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
Chris Lattneraf76e592009-08-22 20:48:53 +0000271 SizeAction -= MCAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000272 SizeAction += -PrevAction->NextAction;
273 PrevAction = PrevAction->Previous;
274 }
275 }
276
277 // Compute the actions.
278 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
279 int TypeID = TypeIds[I];
280 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
281 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
Chris Lattneraf76e592009-08-22 20:48:53 +0000282 unsigned SizeTypeID = MCAsmInfo::getSLEB128Size(ValueForTypeID);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000283
284 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
Chris Lattneraf76e592009-08-22 20:48:53 +0000285 SizeAction = SizeTypeID + MCAsmInfo::getSLEB128Size(NextAction);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000286 SizeSiteActions += SizeAction;
287
288 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
289 Actions.push_back(Action);
290
291 PrevAction = &Actions.back();
292 }
293
294 // Record the first action of the landing pad site.
295 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
296 } // else identical - re-use previous FirstAction
297
298 FirstActions.push_back(FirstAction);
299
300 // Compute this sites contribution to size.
301 SizeActions += SizeSiteActions;
302 }
303
304 // Compute the call-site table. Entries must be ordered by address.
305 SmallVector<CallSiteEntry, 64> CallSites;
306
307 RangeMapType PadMap;
308 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
309 const LandingPadInfo *LandingPad = LandingPads[i];
310 for (unsigned j=0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
311 unsigned BeginLabel = LandingPad->BeginLabels[j];
312 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
313 PadRange P = { i, j };
314 PadMap[BeginLabel] = P;
315 }
316 }
317
318 bool MayThrow = false;
319 unsigned LastLabel = 0;
320 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
321 I != E; ++I) {
322 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
323 MI != E; ++MI) {
Dan Gohman44066042008-07-01 00:05:16 +0000324 if (!MI->isLabel()) {
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000325 MayThrow |= MI->getDesc().isCall();
326 continue;
327 }
328
329 unsigned BeginLabel = MI->getOperand(0).getImm();
330 assert(BeginLabel && "Invalid label!");
331
332 if (BeginLabel == LastLabel)
333 MayThrow = false;
334
335 RangeMapType::iterator L = PadMap.find(BeginLabel);
336
337 if (L == PadMap.end())
338 continue;
339
340 PadRange P = L->second;
341 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
342
343 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
344 "Inconsistent landing pad map!");
345
346 // If some instruction between the previous try-range and this one may
347 // throw, create a call-site entry with no landing pad for the region
348 // between the try-ranges.
349 if (MayThrow) {
350 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
351 CallSites.push_back(Site);
352 }
353
354 LastLabel = LandingPad->EndLabels[P.RangeIndex];
355 CallSiteEntry Site = {BeginLabel, LastLabel,
356 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
357
358 assert(Site.BeginLabel && Site.EndLabel && Site.PadLabel &&
359 "Invalid landing pad!");
360
361 // Try to merge with the previous call-site.
362 if (CallSites.size()) {
Dan Gohman719de532008-06-21 22:00:54 +0000363 CallSiteEntry &Prev = CallSites.back();
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000364 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
365 // Extend the range of the previous entry.
366 Prev.EndLabel = Site.EndLabel;
367 continue;
368 }
369 }
370
371 // Otherwise, create a new call-site.
372 CallSites.push_back(Site);
373 }
374 }
375 // If some instruction between the previous try-range and the end of the
376 // function may throw, create a call-site entry with no landing pad for the
377 // region following the try-range.
378 if (MayThrow) {
379 CallSiteEntry Site = {LastLabel, 0, 0, 0};
380 CallSites.push_back(Site);
381 }
382
383 // Final tallies.
384 unsigned SizeSites = CallSites.size() * (sizeof(int32_t) + // Site start.
385 sizeof(int32_t) + // Site length.
386 sizeof(int32_t)); // Landing pad.
387 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
Chris Lattneraf76e592009-08-22 20:48:53 +0000388 SizeSites += MCAsmInfo::getULEB128Size(CallSites[i].Action);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000389
390 unsigned SizeTypes = TypeInfos.size() * TD->getPointerSize();
391
392 unsigned TypeOffset = sizeof(int8_t) + // Call site format
393 // Call-site table length
Chris Lattneraf76e592009-08-22 20:48:53 +0000394 MCAsmInfo::getULEB128Size(SizeSites) +
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000395 SizeSites + SizeActions + SizeTypes;
396
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000397 // Begin the exception table.
Reid Kleckner01248e62009-08-21 21:03:57 +0000398 JCE->emitAlignmentWithFill(4, 0);
399 // Asm->EOL("Padding");
400
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000401 unsigned char* DwarfExceptionTable = (unsigned char*)JCE->getCurrentPCValue();
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000402
403 // Emit the header.
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000404 JCE->emitByte(dwarf::DW_EH_PE_omit);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000405 // Asm->EOL("LPStart format (DW_EH_PE_omit)");
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000406 JCE->emitByte(dwarf::DW_EH_PE_absptr);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000407 // Asm->EOL("TType format (DW_EH_PE_absptr)");
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000408 JCE->emitULEB128Bytes(TypeOffset);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000409 // Asm->EOL("TType base offset");
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000410 JCE->emitByte(dwarf::DW_EH_PE_udata4);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000411 // Asm->EOL("Call site format (DW_EH_PE_udata4)");
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000412 JCE->emitULEB128Bytes(SizeSites);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000413 // Asm->EOL("Call-site table length");
414
415 // Emit the landing pad site information.
416 for (unsigned i = 0; i < CallSites.size(); ++i) {
417 CallSiteEntry &S = CallSites[i];
418 intptr_t BeginLabelPtr = 0;
419 intptr_t EndLabelPtr = 0;
420
421 if (!S.BeginLabel) {
422 BeginLabelPtr = (intptr_t)StartFunction;
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000423 JCE->emitInt32(0);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000424 } else {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000425 BeginLabelPtr = JCE->getLabelAddress(S.BeginLabel);
426 JCE->emitInt32(BeginLabelPtr - (intptr_t)StartFunction);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000427 }
428
429 // Asm->EOL("Region start");
430
Bill Wendlingabeca442009-12-28 01:53:00 +0000431 if (!S.EndLabel)
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000432 EndLabelPtr = (intptr_t)EndFunction;
Bill Wendlingabeca442009-12-28 01:53:00 +0000433 else
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000434 EndLabelPtr = JCE->getLabelAddress(S.EndLabel);
Bill Wendlingabeca442009-12-28 01:53:00 +0000435
436 JCE->emitInt32(EndLabelPtr - BeginLabelPtr);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000437 //Asm->EOL("Region length");
438
439 if (!S.PadLabel) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000440 JCE->emitInt32(0);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000441 } else {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000442 unsigned PadLabelPtr = JCE->getLabelAddress(S.PadLabel);
443 JCE->emitInt32(PadLabelPtr - (intptr_t)StartFunction);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000444 }
445 // Asm->EOL("Landing pad");
446
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000447 JCE->emitULEB128Bytes(S.Action);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000448 // Asm->EOL("Action");
449 }
450
451 // Emit the actions.
452 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
453 ActionEntry &Action = Actions[I];
454
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000455 JCE->emitSLEB128Bytes(Action.ValueForTypeID);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000456 //Asm->EOL("TypeInfo index");
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000457 JCE->emitSLEB128Bytes(Action.NextAction);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000458 //Asm->EOL("Next action");
459 }
460
461 // Emit the type ids.
462 for (unsigned M = TypeInfos.size(); M; --M) {
463 GlobalVariable *GV = TypeInfos[M - 1];
464
465 if (GV) {
Bill Wendling9d48b552009-09-09 00:11:02 +0000466 if (TD->getPointerSize() == sizeof(int32_t))
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000467 JCE->emitInt32((intptr_t)Jit.getOrEmitGlobalVariable(GV));
Bill Wendling9d48b552009-09-09 00:11:02 +0000468 else
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000469 JCE->emitInt64((intptr_t)Jit.getOrEmitGlobalVariable(GV));
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000470 } else {
471 if (TD->getPointerSize() == sizeof(int32_t))
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000472 JCE->emitInt32(0);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000473 else
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000474 JCE->emitInt64(0);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000475 }
476 // Asm->EOL("TypeInfo");
477 }
478
479 // Emit the filter typeids.
480 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
481 unsigned TypeID = FilterIds[j];
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000482 JCE->emitULEB128Bytes(TypeID);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000483 //Asm->EOL("Filter TypeInfo index");
484 }
Reid Kleckner01248e62009-08-21 21:03:57 +0000485
486 JCE->emitAlignmentWithFill(4, 0);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000487
488 return DwarfExceptionTable;
489}
490
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000491unsigned char*
492JITDwarfEmitter::EmitCommonEHFrame(const Function* Personality) const {
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000493 unsigned PointerSize = TD->getPointerSize();
494 int stackGrowth = stackGrowthDirection == TargetFrameInfo::StackGrowsUp ?
495 PointerSize : -PointerSize;
496
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000497 unsigned char* StartCommonPtr = (unsigned char*)JCE->getCurrentPCValue();
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000498 // EH Common Frame header
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000499 JCE->allocateSpace(4, 0);
500 unsigned char* FrameCommonBeginPtr = (unsigned char*)JCE->getCurrentPCValue();
501 JCE->emitInt32((int)0);
502 JCE->emitByte(dwarf::DW_CIE_VERSION);
503 JCE->emitString(Personality ? "zPLR" : "zR");
504 JCE->emitULEB128Bytes(1);
505 JCE->emitSLEB128Bytes(stackGrowth);
506 JCE->emitByte(RI->getDwarfRegNum(RI->getRARegister(), true));
Bill Wendling9d48b552009-09-09 00:11:02 +0000507
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000508 if (Personality) {
Nicolas Geoffray42cc8f12009-02-15 20:49:23 +0000509 // Augmentation Size: 3 small ULEBs of one byte each, and the personality
510 // function which size is PointerSize.
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000511 JCE->emitULEB128Bytes(3 + PointerSize);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000512
Nicolas Geoffray42cc8f12009-02-15 20:49:23 +0000513 // We set the encoding of the personality as direct encoding because we use
514 // the function pointer. The encoding is not relative because the current
515 // PC value may be bigger than the personality function pointer.
516 if (PointerSize == 4) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000517 JCE->emitByte(dwarf::DW_EH_PE_sdata4);
518 JCE->emitInt32(((intptr_t)Jit.getPointerToGlobal(Personality)));
Nicolas Geoffray42cc8f12009-02-15 20:49:23 +0000519 } else {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000520 JCE->emitByte(dwarf::DW_EH_PE_sdata8);
521 JCE->emitInt64(((intptr_t)Jit.getPointerToGlobal(Personality)));
Nicolas Geoffray42cc8f12009-02-15 20:49:23 +0000522 }
Bill Wendling9d48b552009-09-09 00:11:02 +0000523
Bill Wendling89ee7062010-02-16 00:58:02 +0000524 // LSDA encoding: This must match the encoding used in EmitEHFrame ()
525 if (PointerSize == 4)
526 JCE->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
527 else
528 JCE->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8);
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000529 JCE->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000530 } else {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000531 JCE->emitULEB128Bytes(1);
532 JCE->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000533 }
534
535 std::vector<MachineMove> Moves;
536 RI->getInitialFrameState(Moves);
537 EmitFrameMoves(0, Moves);
Reid Kleckner01248e62009-08-21 21:03:57 +0000538
539 JCE->emitAlignmentWithFill(PointerSize, dwarf::DW_CFA_nop);
540
541 JCE->emitInt32At((uintptr_t*)StartCommonPtr,
542 (uintptr_t)((unsigned char*)JCE->getCurrentPCValue() -
543 FrameCommonBeginPtr));
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000544
545 return StartCommonPtr;
546}
547
548
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000549unsigned char*
550JITDwarfEmitter::EmitEHFrame(const Function* Personality,
551 unsigned char* StartCommonPtr,
552 unsigned char* StartFunction,
553 unsigned char* EndFunction,
554 unsigned char* ExceptionTable) const {
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000555 unsigned PointerSize = TD->getPointerSize();
556
557 // EH frame header.
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000558 unsigned char* StartEHPtr = (unsigned char*)JCE->getCurrentPCValue();
559 JCE->allocateSpace(4, 0);
560 unsigned char* FrameBeginPtr = (unsigned char*)JCE->getCurrentPCValue();
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000561 // FDE CIE Offset
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000562 JCE->emitInt32(FrameBeginPtr - StartCommonPtr);
563 JCE->emitInt32(StartFunction - (unsigned char*)JCE->getCurrentPCValue());
564 JCE->emitInt32(EndFunction - StartFunction);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000565
566 // If there is a personality and landing pads then point to the language
567 // specific data area in the exception table.
Bill Wendling9d48b552009-09-09 00:11:02 +0000568 if (Personality) {
569 JCE->emitULEB128Bytes(PointerSize == 4 ? 4 : 8);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000570
Bill Wendling9d48b552009-09-09 00:11:02 +0000571 if (PointerSize == 4) {
572 if (!MMI->getLandingPads().empty())
573 JCE->emitInt32(ExceptionTable-(unsigned char*)JCE->getCurrentPCValue());
574 else
575 JCE->emitInt32((int)0);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000576 } else {
Bill Wendling9d48b552009-09-09 00:11:02 +0000577 if (!MMI->getLandingPads().empty())
578 JCE->emitInt64(ExceptionTable-(unsigned char*)JCE->getCurrentPCValue());
579 else
580 JCE->emitInt64((int)0);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000581 }
582 } else {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000583 JCE->emitULEB128Bytes(0);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000584 }
585
586 // Indicate locations of function specific callee saved registers in
587 // frame.
588 EmitFrameMoves((intptr_t)StartFunction, MMI->getFrameMoves());
Reid Kleckner01248e62009-08-21 21:03:57 +0000589
590 JCE->emitAlignmentWithFill(PointerSize, dwarf::DW_CFA_nop);
591
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000592 // Indicate the size of the table
Reid Kleckner01248e62009-08-21 21:03:57 +0000593 JCE->emitInt32At((uintptr_t*)StartEHPtr,
594 (uintptr_t)((unsigned char*)JCE->getCurrentPCValue() -
595 StartEHPtr));
596
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000597 // Double zeroes for the unwind runtime
598 if (PointerSize == 8) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000599 JCE->emitInt64(0);
600 JCE->emitInt64(0);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000601 } else {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000602 JCE->emitInt32(0);
603 JCE->emitInt32(0);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000604 }
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000605
606 return StartEHPtr;
607}
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000608
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000609unsigned JITDwarfEmitter::GetDwarfTableSizeInBytes(MachineFunction& F,
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000610 JITCodeEmitter& jce,
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000611 unsigned char* StartFunction,
612 unsigned char* EndFunction) {
613 const TargetMachine& TM = F.getTarget();
614 TD = TM.getTargetData();
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000615 stackGrowthDirection = TM.getFrameInfo()->getStackGrowthDirection();
616 RI = TM.getRegisterInfo();
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000617 JCE = &jce;
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000618 unsigned FinalSize = 0;
619
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000620 FinalSize += GetExceptionTableSizeInBytes(&F);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000621
622 const std::vector<Function *> Personalities = MMI->getPersonalities();
Nicolas Geoffray67c8c4c2008-11-18 10:44:46 +0000623 FinalSize +=
624 GetCommonEHFrameSizeInBytes(Personalities[MMI->getPersonalityIndex()]);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000625
Nicolas Geoffray67c8c4c2008-11-18 10:44:46 +0000626 FinalSize += GetEHFrameSizeInBytes(Personalities[MMI->getPersonalityIndex()],
627 StartFunction);
Bill Wendling9d48b552009-09-09 00:11:02 +0000628
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000629 return FinalSize;
630}
631
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000632/// RoundUpToAlign - Add the specified alignment to FinalSize and returns
633/// the new value.
634static unsigned RoundUpToAlign(unsigned FinalSize, unsigned Alignment) {
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000635 if (Alignment == 0) Alignment = 1;
Nicolas Geoffray580631a2008-04-20 23:39:44 +0000636 // Since we do not know where the buffer will be allocated, be pessimistic.
637 return FinalSize + Alignment;
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000638}
639
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000640unsigned
641JITDwarfEmitter::GetEHFrameSizeInBytes(const Function* Personality,
642 unsigned char* StartFunction) const {
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000643 unsigned PointerSize = TD->getPointerSize();
644 unsigned FinalSize = 0;
645 // EH frame header.
646 FinalSize += PointerSize;
647 // FDE CIE Offset
648 FinalSize += 3 * PointerSize;
649 // If there is a personality and landing pads then point to the language
650 // specific data area in the exception table.
Bill Wendling9d48b552009-09-09 00:11:02 +0000651 if (Personality) {
Chris Lattneraf76e592009-08-22 20:48:53 +0000652 FinalSize += MCAsmInfo::getULEB128Size(4);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000653 FinalSize += PointerSize;
654 } else {
Chris Lattneraf76e592009-08-22 20:48:53 +0000655 FinalSize += MCAsmInfo::getULEB128Size(0);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000656 }
657
658 // Indicate locations of function specific callee saved registers in
659 // frame.
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000660 FinalSize += GetFrameMovesSizeInBytes((intptr_t)StartFunction,
661 MMI->getFrameMoves());
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000662
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000663 FinalSize = RoundUpToAlign(FinalSize, 4);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000664
665 // Double zeroes for the unwind runtime
666 FinalSize += 2 * PointerSize;
667
668 return FinalSize;
669}
670
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000671unsigned JITDwarfEmitter::GetCommonEHFrameSizeInBytes(const Function* Personality)
672 const {
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000673
674 unsigned PointerSize = TD->getPointerSize();
675 int stackGrowth = stackGrowthDirection == TargetFrameInfo::StackGrowsUp ?
676 PointerSize : -PointerSize;
677 unsigned FinalSize = 0;
678 // EH Common Frame header
679 FinalSize += PointerSize;
680 FinalSize += 4;
681 FinalSize += 1;
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000682 FinalSize += Personality ? 5 : 3; // "zPLR" or "zR"
Chris Lattneraf76e592009-08-22 20:48:53 +0000683 FinalSize += MCAsmInfo::getULEB128Size(1);
684 FinalSize += MCAsmInfo::getSLEB128Size(stackGrowth);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000685 FinalSize += 1;
686
687 if (Personality) {
Chris Lattneraf76e592009-08-22 20:48:53 +0000688 FinalSize += MCAsmInfo::getULEB128Size(7);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000689
690 // Encoding
691 FinalSize+= 1;
692 //Personality
693 FinalSize += PointerSize;
694
Chris Lattneraf76e592009-08-22 20:48:53 +0000695 FinalSize += MCAsmInfo::getULEB128Size(dwarf::DW_EH_PE_pcrel);
696 FinalSize += MCAsmInfo::getULEB128Size(dwarf::DW_EH_PE_pcrel);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000697
698 } else {
Chris Lattneraf76e592009-08-22 20:48:53 +0000699 FinalSize += MCAsmInfo::getULEB128Size(1);
700 FinalSize += MCAsmInfo::getULEB128Size(dwarf::DW_EH_PE_pcrel);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000701 }
702
703 std::vector<MachineMove> Moves;
704 RI->getInitialFrameState(Moves);
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000705 FinalSize += GetFrameMovesSizeInBytes(0, Moves);
706 FinalSize = RoundUpToAlign(FinalSize, 4);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000707 return FinalSize;
708}
709
710unsigned
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000711JITDwarfEmitter::GetFrameMovesSizeInBytes(intptr_t BaseLabelPtr,
712 const std::vector<MachineMove> &Moves) const {
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000713 unsigned PointerSize = TD->getPointerSize();
714 int stackGrowth = stackGrowthDirection == TargetFrameInfo::StackGrowsUp ?
715 PointerSize : -PointerSize;
716 bool IsLocal = BaseLabelPtr;
717 unsigned FinalSize = 0;
718
719 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
720 const MachineMove &Move = Moves[i];
721 unsigned LabelID = Move.getLabelID();
722
723 if (LabelID) {
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000724 // Throw out move if the label is invalid.
Chris Lattnera34ec2292010-03-09 01:51:43 +0000725 if (MMI->isLabelDeleted(LabelID))
726 continue;
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000727 }
728
729 intptr_t LabelPtr = 0;
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000730 if (LabelID) LabelPtr = JCE->getLabelAddress(LabelID);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000731
732 const MachineLocation &Dst = Move.getDestination();
733 const MachineLocation &Src = Move.getSource();
734
735 // Advance row if new location.
736 if (BaseLabelPtr && LabelID && (BaseLabelPtr != LabelPtr || !IsLocal)) {
737 FinalSize++;
738 FinalSize += PointerSize;
739 BaseLabelPtr = LabelPtr;
740 IsLocal = true;
741 }
742
743 // If advancing cfa.
Daniel Dunbar489032a2008-10-03 17:11:57 +0000744 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
745 if (!Src.isReg()) {
746 if (Src.getReg() == MachineLocation::VirtualFP) {
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000747 ++FinalSize;
748 } else {
749 ++FinalSize;
Daniel Dunbar489032a2008-10-03 17:11:57 +0000750 unsigned RegNum = RI->getDwarfRegNum(Src.getReg(), true);
Chris Lattneraf76e592009-08-22 20:48:53 +0000751 FinalSize += MCAsmInfo::getULEB128Size(RegNum);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000752 }
753
754 int Offset = -Src.getOffset();
755
Chris Lattneraf76e592009-08-22 20:48:53 +0000756 FinalSize += MCAsmInfo::getULEB128Size(Offset);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000757 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000758 llvm_unreachable("Machine move no supported yet.");
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000759 }
Daniel Dunbar489032a2008-10-03 17:11:57 +0000760 } else if (Src.isReg() &&
761 Src.getReg() == MachineLocation::VirtualFP) {
762 if (Dst.isReg()) {
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000763 ++FinalSize;
Daniel Dunbar489032a2008-10-03 17:11:57 +0000764 unsigned RegNum = RI->getDwarfRegNum(Dst.getReg(), true);
Chris Lattneraf76e592009-08-22 20:48:53 +0000765 FinalSize += MCAsmInfo::getULEB128Size(RegNum);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000766 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000767 llvm_unreachable("Machine move no supported yet.");
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000768 }
769 } else {
Daniel Dunbar489032a2008-10-03 17:11:57 +0000770 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), true);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000771 int Offset = Dst.getOffset() / stackGrowth;
772
773 if (Offset < 0) {
774 ++FinalSize;
Chris Lattneraf76e592009-08-22 20:48:53 +0000775 FinalSize += MCAsmInfo::getULEB128Size(Reg);
776 FinalSize += MCAsmInfo::getSLEB128Size(Offset);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000777 } else if (Reg < 64) {
778 ++FinalSize;
Chris Lattneraf76e592009-08-22 20:48:53 +0000779 FinalSize += MCAsmInfo::getULEB128Size(Offset);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000780 } else {
781 ++FinalSize;
Chris Lattneraf76e592009-08-22 20:48:53 +0000782 FinalSize += MCAsmInfo::getULEB128Size(Reg);
783 FinalSize += MCAsmInfo::getULEB128Size(Offset);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000784 }
785 }
786 }
787 return FinalSize;
788}
789
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000790unsigned
791JITDwarfEmitter::GetExceptionTableSizeInBytes(MachineFunction* MF) const {
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000792 unsigned FinalSize = 0;
793
794 // Map all labels and get rid of any dead landing pads.
795 MMI->TidyLandingPads();
796
797 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
798 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
799 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
800 if (PadInfos.empty()) return 0;
801
802 // Sort the landing pads in order of their type ids. This is used to fold
803 // duplicate actions.
804 SmallVector<const LandingPadInfo *, 64> LandingPads;
805 LandingPads.reserve(PadInfos.size());
806 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
807 LandingPads.push_back(&PadInfos[i]);
808 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
809
810 // Negative type ids index into FilterIds, positive type ids index into
811 // TypeInfos. The value written for a positive type id is just the type
812 // id itself. For a negative type id, however, the value written is the
813 // (negative) byte offset of the corresponding FilterIds entry. The byte
814 // offset is usually equal to the type id, because the FilterIds entries
815 // are written using a variable width encoding which outputs one byte per
816 // entry as long as the value written is not too large, but can differ.
817 // This kind of complication does not occur for positive type ids because
818 // type infos are output using a fixed width encoding.
819 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
820 SmallVector<int, 16> FilterOffsets;
821 FilterOffsets.reserve(FilterIds.size());
822 int Offset = -1;
823 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
824 E = FilterIds.end(); I != E; ++I) {
825 FilterOffsets.push_back(Offset);
Chris Lattneraf76e592009-08-22 20:48:53 +0000826 Offset -= MCAsmInfo::getULEB128Size(*I);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000827 }
828
829 // Compute the actions table and gather the first action index for each
830 // landing pad site.
831 SmallVector<ActionEntry, 32> Actions;
832 SmallVector<unsigned, 64> FirstActions;
833 FirstActions.reserve(LandingPads.size());
834
835 int FirstAction = 0;
836 unsigned SizeActions = 0;
837 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
838 const LandingPadInfo *LP = LandingPads[i];
839 const std::vector<int> &TypeIds = LP->TypeIds;
840 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
841 unsigned SizeSiteActions = 0;
842
843 if (NumShared < TypeIds.size()) {
844 unsigned SizeAction = 0;
845 ActionEntry *PrevAction = 0;
846
847 if (NumShared) {
848 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
849 assert(Actions.size());
850 PrevAction = &Actions.back();
Chris Lattneraf76e592009-08-22 20:48:53 +0000851 SizeAction = MCAsmInfo::getSLEB128Size(PrevAction->NextAction) +
852 MCAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000853 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
Chris Lattneraf76e592009-08-22 20:48:53 +0000854 SizeAction -= MCAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000855 SizeAction += -PrevAction->NextAction;
856 PrevAction = PrevAction->Previous;
857 }
858 }
859
860 // Compute the actions.
861 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
862 int TypeID = TypeIds[I];
863 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
864 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
Chris Lattneraf76e592009-08-22 20:48:53 +0000865 unsigned SizeTypeID = MCAsmInfo::getSLEB128Size(ValueForTypeID);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000866
867 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
Chris Lattneraf76e592009-08-22 20:48:53 +0000868 SizeAction = SizeTypeID + MCAsmInfo::getSLEB128Size(NextAction);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000869 SizeSiteActions += SizeAction;
870
871 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
872 Actions.push_back(Action);
873
874 PrevAction = &Actions.back();
875 }
876
877 // Record the first action of the landing pad site.
878 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
879 } // else identical - re-use previous FirstAction
880
881 FirstActions.push_back(FirstAction);
882
883 // Compute this sites contribution to size.
884 SizeActions += SizeSiteActions;
885 }
886
887 // Compute the call-site table. Entries must be ordered by address.
888 SmallVector<CallSiteEntry, 64> CallSites;
889
890 RangeMapType PadMap;
891 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
892 const LandingPadInfo *LandingPad = LandingPads[i];
893 for (unsigned j=0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
894 unsigned BeginLabel = LandingPad->BeginLabels[j];
895 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
896 PadRange P = { i, j };
897 PadMap[BeginLabel] = P;
898 }
899 }
900
901 bool MayThrow = false;
902 unsigned LastLabel = 0;
903 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
904 I != E; ++I) {
905 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
906 MI != E; ++MI) {
Dan Gohman44066042008-07-01 00:05:16 +0000907 if (!MI->isLabel()) {
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000908 MayThrow |= MI->getDesc().isCall();
909 continue;
910 }
911
912 unsigned BeginLabel = MI->getOperand(0).getImm();
913 assert(BeginLabel && "Invalid label!");
914
915 if (BeginLabel == LastLabel)
916 MayThrow = false;
917
918 RangeMapType::iterator L = PadMap.find(BeginLabel);
919
920 if (L == PadMap.end())
921 continue;
922
923 PadRange P = L->second;
924 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
925
926 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
927 "Inconsistent landing pad map!");
928
929 // If some instruction between the previous try-range and this one may
930 // throw, create a call-site entry with no landing pad for the region
931 // between the try-ranges.
932 if (MayThrow) {
933 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
934 CallSites.push_back(Site);
935 }
936
937 LastLabel = LandingPad->EndLabels[P.RangeIndex];
938 CallSiteEntry Site = {BeginLabel, LastLabel,
939 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
940
941 assert(Site.BeginLabel && Site.EndLabel && Site.PadLabel &&
942 "Invalid landing pad!");
943
944 // Try to merge with the previous call-site.
945 if (CallSites.size()) {
Dan Gohman719de532008-06-21 22:00:54 +0000946 CallSiteEntry &Prev = CallSites.back();
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000947 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
948 // Extend the range of the previous entry.
949 Prev.EndLabel = Site.EndLabel;
950 continue;
951 }
952 }
953
954 // Otherwise, create a new call-site.
955 CallSites.push_back(Site);
956 }
957 }
958 // If some instruction between the previous try-range and the end of the
959 // function may throw, create a call-site entry with no landing pad for the
960 // region following the try-range.
961 if (MayThrow) {
962 CallSiteEntry Site = {LastLabel, 0, 0, 0};
963 CallSites.push_back(Site);
964 }
965
966 // Final tallies.
967 unsigned SizeSites = CallSites.size() * (sizeof(int32_t) + // Site start.
968 sizeof(int32_t) + // Site length.
969 sizeof(int32_t)); // Landing pad.
970 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
Chris Lattneraf76e592009-08-22 20:48:53 +0000971 SizeSites += MCAsmInfo::getULEB128Size(CallSites[i].Action);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000972
973 unsigned SizeTypes = TypeInfos.size() * TD->getPointerSize();
974
975 unsigned TypeOffset = sizeof(int8_t) + // Call site format
976 // Call-site table length
Chris Lattneraf76e592009-08-22 20:48:53 +0000977 MCAsmInfo::getULEB128Size(SizeSites) +
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000978 SizeSites + SizeActions + SizeTypes;
979
980 unsigned TotalSize = sizeof(int8_t) + // LPStart format
981 sizeof(int8_t) + // TType format
Chris Lattneraf76e592009-08-22 20:48:53 +0000982 MCAsmInfo::getULEB128Size(TypeOffset) + // TType base offset
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000983 TypeOffset;
984
985 unsigned SizeAlign = (4 - TotalSize) & 3;
986
987 // Begin the exception table.
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000988 FinalSize = RoundUpToAlign(FinalSize, 4);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000989 for (unsigned i = 0; i != SizeAlign; ++i) {
990 ++FinalSize;
991 }
992
993 unsigned PointerSize = TD->getPointerSize();
994
995 // Emit the header.
996 ++FinalSize;
997 // Asm->EOL("LPStart format (DW_EH_PE_omit)");
998 ++FinalSize;
999 // Asm->EOL("TType format (DW_EH_PE_absptr)");
1000 ++FinalSize;
1001 // Asm->EOL("TType base offset");
1002 ++FinalSize;
1003 // Asm->EOL("Call site format (DW_EH_PE_udata4)");
1004 ++FinalSize;
1005 // Asm->EOL("Call-site table length");
1006
1007 // Emit the landing pad site information.
1008 for (unsigned i = 0; i < CallSites.size(); ++i) {
1009 CallSiteEntry &S = CallSites[i];
1010
1011 // Asm->EOL("Region start");
1012 FinalSize += PointerSize;
1013
1014 //Asm->EOL("Region length");
1015 FinalSize += PointerSize;
1016
1017 // Asm->EOL("Landing pad");
1018 FinalSize += PointerSize;
1019
Chris Lattneraf76e592009-08-22 20:48:53 +00001020 FinalSize += MCAsmInfo::getULEB128Size(S.Action);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +00001021 // Asm->EOL("Action");
1022 }
1023
1024 // Emit the actions.
1025 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
1026 ActionEntry &Action = Actions[I];
1027
1028 //Asm->EOL("TypeInfo index");
Chris Lattneraf76e592009-08-22 20:48:53 +00001029 FinalSize += MCAsmInfo::getSLEB128Size(Action.ValueForTypeID);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +00001030 //Asm->EOL("Next action");
Chris Lattneraf76e592009-08-22 20:48:53 +00001031 FinalSize += MCAsmInfo::getSLEB128Size(Action.NextAction);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +00001032 }
1033
1034 // Emit the type ids.
1035 for (unsigned M = TypeInfos.size(); M; --M) {
1036 // Asm->EOL("TypeInfo");
1037 FinalSize += PointerSize;
1038 }
1039
1040 // Emit the filter typeids.
1041 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
1042 unsigned TypeID = FilterIds[j];
Chris Lattneraf76e592009-08-22 20:48:53 +00001043 FinalSize += MCAsmInfo::getULEB128Size(TypeID);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +00001044 //Asm->EOL("Filter TypeInfo index");
1045 }
1046
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +00001047 FinalSize = RoundUpToAlign(FinalSize, 4);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +00001048
1049 return FinalSize;
1050}