blob: 25974cdcde1d825dd3f1644cc4390e2ab9526b4f [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
33JITDwarfEmitter::JITDwarfEmitter(JIT& theJit) : Jit(theJit) {}
34
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,
39 unsigned char* EndFunction) {
40 const TargetMachine& TM = F.getTarget();
41 TD = TM.getTargetData();
Chris Lattneraf76e592009-08-22 20:48:53 +000042 needsIndirectEncoding = TM.getMCAsmInfo()->getNeedsIndirectEncoding();
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000043 stackGrowthDirection = TM.getFrameInfo()->getStackGrowthDirection();
44 RI = TM.getRegisterInfo();
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000045 JCE = &jce;
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000046
47 unsigned char* ExceptionTable = EmitExceptionTable(&F, StartFunction,
48 EndFunction);
49
50 unsigned char* Result = 0;
51 unsigned char* EHFramePtr = 0;
52
53 const std::vector<Function *> Personalities = MMI->getPersonalities();
54 EHFramePtr = EmitCommonEHFrame(Personalities[MMI->getPersonalityIndex()]);
55
56 Result = EmitEHFrame(Personalities[MMI->getPersonalityIndex()], EHFramePtr,
57 StartFunction, EndFunction, ExceptionTable);
Bill Wendling9d48b552009-09-09 00:11:02 +000058
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000059 return Result;
60}
61
62
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +000063void
64JITDwarfEmitter::EmitFrameMoves(intptr_t BaseLabelPtr,
65 const std::vector<MachineMove> &Moves) const {
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000066 unsigned PointerSize = TD->getPointerSize();
67 int stackGrowth = stackGrowthDirection == TargetFrameInfo::StackGrowsUp ?
68 PointerSize : -PointerSize;
Nicolas Geoffray2d450eb2008-08-19 14:48:14 +000069 bool IsLocal = false;
70 unsigned BaseLabelID = 0;
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000071
72 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
73 const MachineMove &Move = Moves[i];
74 unsigned LabelID = Move.getLabelID();
75
76 if (LabelID) {
77 LabelID = MMI->MappedLabel(LabelID);
78
79 // Throw out move if the label is invalid.
80 if (!LabelID) continue;
81 }
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; }
177 static bool isPod() { return true; }
178};
179
180/// ActionEntry - Structure describing an entry in the actions table.
181struct ActionEntry {
182 int ValueForTypeID; // The value to write - may not be equal to the type id.
183 int NextAction;
184 struct ActionEntry *Previous;
185};
186
187/// PadRange - Structure holding a try-range and the associated landing pad.
188struct PadRange {
189 // The index of the landing pad.
190 unsigned PadIndex;
191 // The index of the begin and end labels in the landing pad's label lists.
192 unsigned RangeIndex;
193};
194
195typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
196
197/// CallSiteEntry - Structure describing an entry in the call-site table.
198struct CallSiteEntry {
199 unsigned BeginLabel; // zero indicates the start of the function.
200 unsigned EndLabel; // zero indicates the end of the function.
201 unsigned PadLabel; // zero indicates that there is no landing pad.
202 unsigned Action;
203};
204
Dan Gohman844731a2008-05-13 00:00:25 +0000205}
206
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000207unsigned char* JITDwarfEmitter::EmitExceptionTable(MachineFunction* MF,
208 unsigned char* StartFunction,
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000209 unsigned char* EndFunction) const {
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000210 // Map all labels and get rid of any dead landing pads.
211 MMI->TidyLandingPads();
212
213 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
214 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
215 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
216 if (PadInfos.empty()) return 0;
217
218 // Sort the landing pads in order of their type ids. This is used to fold
219 // duplicate actions.
220 SmallVector<const LandingPadInfo *, 64> LandingPads;
221 LandingPads.reserve(PadInfos.size());
222 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
223 LandingPads.push_back(&PadInfos[i]);
224 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
225
226 // Negative type ids index into FilterIds, positive type ids index into
227 // TypeInfos. The value written for a positive type id is just the type
228 // id itself. For a negative type id, however, the value written is the
229 // (negative) byte offset of the corresponding FilterIds entry. The byte
230 // offset is usually equal to the type id, because the FilterIds entries
231 // are written using a variable width encoding which outputs one byte per
232 // entry as long as the value written is not too large, but can differ.
233 // This kind of complication does not occur for positive type ids because
234 // type infos are output using a fixed width encoding.
235 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
236 SmallVector<int, 16> FilterOffsets;
237 FilterOffsets.reserve(FilterIds.size());
238 int Offset = -1;
239 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
240 E = FilterIds.end(); I != E; ++I) {
241 FilterOffsets.push_back(Offset);
Chris Lattneraf76e592009-08-22 20:48:53 +0000242 Offset -= MCAsmInfo::getULEB128Size(*I);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000243 }
244
245 // Compute the actions table and gather the first action index for each
246 // landing pad site.
247 SmallVector<ActionEntry, 32> Actions;
248 SmallVector<unsigned, 64> FirstActions;
249 FirstActions.reserve(LandingPads.size());
250
251 int FirstAction = 0;
252 unsigned SizeActions = 0;
253 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
254 const LandingPadInfo *LP = LandingPads[i];
255 const std::vector<int> &TypeIds = LP->TypeIds;
256 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
257 unsigned SizeSiteActions = 0;
258
259 if (NumShared < TypeIds.size()) {
260 unsigned SizeAction = 0;
261 ActionEntry *PrevAction = 0;
262
263 if (NumShared) {
264 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
265 assert(Actions.size());
266 PrevAction = &Actions.back();
Chris Lattneraf76e592009-08-22 20:48:53 +0000267 SizeAction = MCAsmInfo::getSLEB128Size(PrevAction->NextAction) +
268 MCAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000269 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
Chris Lattneraf76e592009-08-22 20:48:53 +0000270 SizeAction -= MCAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000271 SizeAction += -PrevAction->NextAction;
272 PrevAction = PrevAction->Previous;
273 }
274 }
275
276 // Compute the actions.
277 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
278 int TypeID = TypeIds[I];
279 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
280 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
Chris Lattneraf76e592009-08-22 20:48:53 +0000281 unsigned SizeTypeID = MCAsmInfo::getSLEB128Size(ValueForTypeID);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000282
283 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
Chris Lattneraf76e592009-08-22 20:48:53 +0000284 SizeAction = SizeTypeID + MCAsmInfo::getSLEB128Size(NextAction);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000285 SizeSiteActions += SizeAction;
286
287 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
288 Actions.push_back(Action);
289
290 PrevAction = &Actions.back();
291 }
292
293 // Record the first action of the landing pad site.
294 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
295 } // else identical - re-use previous FirstAction
296
297 FirstActions.push_back(FirstAction);
298
299 // Compute this sites contribution to size.
300 SizeActions += SizeSiteActions;
301 }
302
303 // Compute the call-site table. Entries must be ordered by address.
304 SmallVector<CallSiteEntry, 64> CallSites;
305
306 RangeMapType PadMap;
307 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
308 const LandingPadInfo *LandingPad = LandingPads[i];
309 for (unsigned j=0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
310 unsigned BeginLabel = LandingPad->BeginLabels[j];
311 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
312 PadRange P = { i, j };
313 PadMap[BeginLabel] = P;
314 }
315 }
316
317 bool MayThrow = false;
318 unsigned LastLabel = 0;
319 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
320 I != E; ++I) {
321 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
322 MI != E; ++MI) {
Dan Gohman44066042008-07-01 00:05:16 +0000323 if (!MI->isLabel()) {
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000324 MayThrow |= MI->getDesc().isCall();
325 continue;
326 }
327
328 unsigned BeginLabel = MI->getOperand(0).getImm();
329 assert(BeginLabel && "Invalid label!");
330
331 if (BeginLabel == LastLabel)
332 MayThrow = false;
333
334 RangeMapType::iterator L = PadMap.find(BeginLabel);
335
336 if (L == PadMap.end())
337 continue;
338
339 PadRange P = L->second;
340 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
341
342 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
343 "Inconsistent landing pad map!");
344
345 // If some instruction between the previous try-range and this one may
346 // throw, create a call-site entry with no landing pad for the region
347 // between the try-ranges.
348 if (MayThrow) {
349 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
350 CallSites.push_back(Site);
351 }
352
353 LastLabel = LandingPad->EndLabels[P.RangeIndex];
354 CallSiteEntry Site = {BeginLabel, LastLabel,
355 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
356
357 assert(Site.BeginLabel && Site.EndLabel && Site.PadLabel &&
358 "Invalid landing pad!");
359
360 // Try to merge with the previous call-site.
361 if (CallSites.size()) {
Dan Gohman719de532008-06-21 22:00:54 +0000362 CallSiteEntry &Prev = CallSites.back();
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000363 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
364 // Extend the range of the previous entry.
365 Prev.EndLabel = Site.EndLabel;
366 continue;
367 }
368 }
369
370 // Otherwise, create a new call-site.
371 CallSites.push_back(Site);
372 }
373 }
374 // If some instruction between the previous try-range and the end of the
375 // function may throw, create a call-site entry with no landing pad for the
376 // region following the try-range.
377 if (MayThrow) {
378 CallSiteEntry Site = {LastLabel, 0, 0, 0};
379 CallSites.push_back(Site);
380 }
381
382 // Final tallies.
383 unsigned SizeSites = CallSites.size() * (sizeof(int32_t) + // Site start.
384 sizeof(int32_t) + // Site length.
385 sizeof(int32_t)); // Landing pad.
386 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
Chris Lattneraf76e592009-08-22 20:48:53 +0000387 SizeSites += MCAsmInfo::getULEB128Size(CallSites[i].Action);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000388
389 unsigned SizeTypes = TypeInfos.size() * TD->getPointerSize();
390
391 unsigned TypeOffset = sizeof(int8_t) + // Call site format
392 // Call-site table length
Chris Lattneraf76e592009-08-22 20:48:53 +0000393 MCAsmInfo::getULEB128Size(SizeSites) +
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000394 SizeSites + SizeActions + SizeTypes;
395
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000396 // Begin the exception table.
Reid Kleckner01248e62009-08-21 21:03:57 +0000397 JCE->emitAlignmentWithFill(4, 0);
398 // Asm->EOL("Padding");
399
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000400 unsigned char* DwarfExceptionTable = (unsigned char*)JCE->getCurrentPCValue();
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000401
402 // Emit the header.
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000403 JCE->emitByte(dwarf::DW_EH_PE_omit);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000404 // Asm->EOL("LPStart format (DW_EH_PE_omit)");
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000405 JCE->emitByte(dwarf::DW_EH_PE_absptr);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000406 // Asm->EOL("TType format (DW_EH_PE_absptr)");
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000407 JCE->emitULEB128Bytes(TypeOffset);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000408 // Asm->EOL("TType base offset");
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000409 JCE->emitByte(dwarf::DW_EH_PE_udata4);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000410 // Asm->EOL("Call site format (DW_EH_PE_udata4)");
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000411 JCE->emitULEB128Bytes(SizeSites);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000412 // Asm->EOL("Call-site table length");
413
414 // Emit the landing pad site information.
415 for (unsigned i = 0; i < CallSites.size(); ++i) {
416 CallSiteEntry &S = CallSites[i];
417 intptr_t BeginLabelPtr = 0;
418 intptr_t EndLabelPtr = 0;
419
420 if (!S.BeginLabel) {
421 BeginLabelPtr = (intptr_t)StartFunction;
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000422 JCE->emitInt32(0);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000423 } else {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000424 BeginLabelPtr = JCE->getLabelAddress(S.BeginLabel);
425 JCE->emitInt32(BeginLabelPtr - (intptr_t)StartFunction);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000426 }
427
428 // Asm->EOL("Region start");
429
430 if (!S.EndLabel) {
431 EndLabelPtr = (intptr_t)EndFunction;
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000432 JCE->emitInt32((intptr_t)EndFunction - BeginLabelPtr);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000433 } else {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000434 EndLabelPtr = JCE->getLabelAddress(S.EndLabel);
435 JCE->emitInt32(EndLabelPtr - BeginLabelPtr);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000436 }
437 //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
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000524 JCE->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
525 JCE->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000526 } else {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000527 JCE->emitULEB128Bytes(1);
528 JCE->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000529 }
530
531 std::vector<MachineMove> Moves;
532 RI->getInitialFrameState(Moves);
533 EmitFrameMoves(0, Moves);
Reid Kleckner01248e62009-08-21 21:03:57 +0000534
535 JCE->emitAlignmentWithFill(PointerSize, dwarf::DW_CFA_nop);
536
537 JCE->emitInt32At((uintptr_t*)StartCommonPtr,
538 (uintptr_t)((unsigned char*)JCE->getCurrentPCValue() -
539 FrameCommonBeginPtr));
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000540
541 return StartCommonPtr;
542}
543
544
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000545unsigned char*
546JITDwarfEmitter::EmitEHFrame(const Function* Personality,
547 unsigned char* StartCommonPtr,
548 unsigned char* StartFunction,
549 unsigned char* EndFunction,
550 unsigned char* ExceptionTable) const {
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000551 unsigned PointerSize = TD->getPointerSize();
552
553 // EH frame header.
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000554 unsigned char* StartEHPtr = (unsigned char*)JCE->getCurrentPCValue();
555 JCE->allocateSpace(4, 0);
556 unsigned char* FrameBeginPtr = (unsigned char*)JCE->getCurrentPCValue();
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000557 // FDE CIE Offset
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000558 JCE->emitInt32(FrameBeginPtr - StartCommonPtr);
559 JCE->emitInt32(StartFunction - (unsigned char*)JCE->getCurrentPCValue());
560 JCE->emitInt32(EndFunction - StartFunction);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000561
562 // If there is a personality and landing pads then point to the language
563 // specific data area in the exception table.
Bill Wendling9d48b552009-09-09 00:11:02 +0000564 if (Personality) {
565 JCE->emitULEB128Bytes(PointerSize == 4 ? 4 : 8);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000566
Bill Wendling9d48b552009-09-09 00:11:02 +0000567 if (PointerSize == 4) {
568 if (!MMI->getLandingPads().empty())
569 JCE->emitInt32(ExceptionTable-(unsigned char*)JCE->getCurrentPCValue());
570 else
571 JCE->emitInt32((int)0);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000572 } else {
Bill Wendling9d48b552009-09-09 00:11:02 +0000573 if (!MMI->getLandingPads().empty())
574 JCE->emitInt64(ExceptionTable-(unsigned char*)JCE->getCurrentPCValue());
575 else
576 JCE->emitInt64((int)0);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000577 }
578 } else {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000579 JCE->emitULEB128Bytes(0);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000580 }
581
582 // Indicate locations of function specific callee saved registers in
583 // frame.
584 EmitFrameMoves((intptr_t)StartFunction, MMI->getFrameMoves());
Reid Kleckner01248e62009-08-21 21:03:57 +0000585
586 JCE->emitAlignmentWithFill(PointerSize, dwarf::DW_CFA_nop);
587
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000588 // Indicate the size of the table
Reid Kleckner01248e62009-08-21 21:03:57 +0000589 JCE->emitInt32At((uintptr_t*)StartEHPtr,
590 (uintptr_t)((unsigned char*)JCE->getCurrentPCValue() -
591 StartEHPtr));
592
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000593 // Double zeroes for the unwind runtime
594 if (PointerSize == 8) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000595 JCE->emitInt64(0);
596 JCE->emitInt64(0);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000597 } else {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000598 JCE->emitInt32(0);
599 JCE->emitInt32(0);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000600 }
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000601
602 return StartEHPtr;
603}
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000604
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000605unsigned JITDwarfEmitter::GetDwarfTableSizeInBytes(MachineFunction& F,
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000606 JITCodeEmitter& jce,
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000607 unsigned char* StartFunction,
608 unsigned char* EndFunction) {
609 const TargetMachine& TM = F.getTarget();
610 TD = TM.getTargetData();
Chris Lattneraf76e592009-08-22 20:48:53 +0000611 needsIndirectEncoding = TM.getMCAsmInfo()->getNeedsIndirectEncoding();
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000612 stackGrowthDirection = TM.getFrameInfo()->getStackGrowthDirection();
613 RI = TM.getRegisterInfo();
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000614 JCE = &jce;
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000615 unsigned FinalSize = 0;
616
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000617 FinalSize += GetExceptionTableSizeInBytes(&F);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000618
619 const std::vector<Function *> Personalities = MMI->getPersonalities();
Nicolas Geoffray67c8c4c2008-11-18 10:44:46 +0000620 FinalSize +=
621 GetCommonEHFrameSizeInBytes(Personalities[MMI->getPersonalityIndex()]);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000622
Nicolas Geoffray67c8c4c2008-11-18 10:44:46 +0000623 FinalSize += GetEHFrameSizeInBytes(Personalities[MMI->getPersonalityIndex()],
624 StartFunction);
Bill Wendling9d48b552009-09-09 00:11:02 +0000625
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000626 return FinalSize;
627}
628
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000629/// RoundUpToAlign - Add the specified alignment to FinalSize and returns
630/// the new value.
631static unsigned RoundUpToAlign(unsigned FinalSize, unsigned Alignment) {
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000632 if (Alignment == 0) Alignment = 1;
Nicolas Geoffray580631a2008-04-20 23:39:44 +0000633 // Since we do not know where the buffer will be allocated, be pessimistic.
634 return FinalSize + Alignment;
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000635}
636
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000637unsigned
638JITDwarfEmitter::GetEHFrameSizeInBytes(const Function* Personality,
639 unsigned char* StartFunction) const {
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000640 unsigned PointerSize = TD->getPointerSize();
641 unsigned FinalSize = 0;
642 // EH frame header.
643 FinalSize += PointerSize;
644 // FDE CIE Offset
645 FinalSize += 3 * PointerSize;
646 // If there is a personality and landing pads then point to the language
647 // specific data area in the exception table.
Bill Wendling9d48b552009-09-09 00:11:02 +0000648 if (Personality) {
Chris Lattneraf76e592009-08-22 20:48:53 +0000649 FinalSize += MCAsmInfo::getULEB128Size(4);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000650 FinalSize += PointerSize;
651 } else {
Chris Lattneraf76e592009-08-22 20:48:53 +0000652 FinalSize += MCAsmInfo::getULEB128Size(0);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000653 }
654
655 // Indicate locations of function specific callee saved registers in
656 // frame.
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000657 FinalSize += GetFrameMovesSizeInBytes((intptr_t)StartFunction,
658 MMI->getFrameMoves());
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000659
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000660 FinalSize = RoundUpToAlign(FinalSize, 4);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000661
662 // Double zeroes for the unwind runtime
663 FinalSize += 2 * PointerSize;
664
665 return FinalSize;
666}
667
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000668unsigned JITDwarfEmitter::GetCommonEHFrameSizeInBytes(const Function* Personality)
669 const {
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000670
671 unsigned PointerSize = TD->getPointerSize();
672 int stackGrowth = stackGrowthDirection == TargetFrameInfo::StackGrowsUp ?
673 PointerSize : -PointerSize;
674 unsigned FinalSize = 0;
675 // EH Common Frame header
676 FinalSize += PointerSize;
677 FinalSize += 4;
678 FinalSize += 1;
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000679 FinalSize += Personality ? 5 : 3; // "zPLR" or "zR"
Chris Lattneraf76e592009-08-22 20:48:53 +0000680 FinalSize += MCAsmInfo::getULEB128Size(1);
681 FinalSize += MCAsmInfo::getSLEB128Size(stackGrowth);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000682 FinalSize += 1;
683
684 if (Personality) {
Chris Lattneraf76e592009-08-22 20:48:53 +0000685 FinalSize += MCAsmInfo::getULEB128Size(7);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000686
687 // Encoding
688 FinalSize+= 1;
689 //Personality
690 FinalSize += PointerSize;
691
Chris Lattneraf76e592009-08-22 20:48:53 +0000692 FinalSize += MCAsmInfo::getULEB128Size(dwarf::DW_EH_PE_pcrel);
693 FinalSize += MCAsmInfo::getULEB128Size(dwarf::DW_EH_PE_pcrel);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000694
695 } else {
Chris Lattneraf76e592009-08-22 20:48:53 +0000696 FinalSize += MCAsmInfo::getULEB128Size(1);
697 FinalSize += MCAsmInfo::getULEB128Size(dwarf::DW_EH_PE_pcrel);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000698 }
699
700 std::vector<MachineMove> Moves;
701 RI->getInitialFrameState(Moves);
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000702 FinalSize += GetFrameMovesSizeInBytes(0, Moves);
703 FinalSize = RoundUpToAlign(FinalSize, 4);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000704 return FinalSize;
705}
706
707unsigned
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000708JITDwarfEmitter::GetFrameMovesSizeInBytes(intptr_t BaseLabelPtr,
709 const std::vector<MachineMove> &Moves) const {
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000710 unsigned PointerSize = TD->getPointerSize();
711 int stackGrowth = stackGrowthDirection == TargetFrameInfo::StackGrowsUp ?
712 PointerSize : -PointerSize;
713 bool IsLocal = BaseLabelPtr;
714 unsigned FinalSize = 0;
715
716 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
717 const MachineMove &Move = Moves[i];
718 unsigned LabelID = Move.getLabelID();
719
720 if (LabelID) {
721 LabelID = MMI->MappedLabel(LabelID);
722
723 // Throw out move if the label is invalid.
724 if (!LabelID) continue;
725 }
726
727 intptr_t LabelPtr = 0;
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000728 if (LabelID) LabelPtr = JCE->getLabelAddress(LabelID);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000729
730 const MachineLocation &Dst = Move.getDestination();
731 const MachineLocation &Src = Move.getSource();
732
733 // Advance row if new location.
734 if (BaseLabelPtr && LabelID && (BaseLabelPtr != LabelPtr || !IsLocal)) {
735 FinalSize++;
736 FinalSize += PointerSize;
737 BaseLabelPtr = LabelPtr;
738 IsLocal = true;
739 }
740
741 // If advancing cfa.
Daniel Dunbar489032a2008-10-03 17:11:57 +0000742 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
743 if (!Src.isReg()) {
744 if (Src.getReg() == MachineLocation::VirtualFP) {
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000745 ++FinalSize;
746 } else {
747 ++FinalSize;
Daniel Dunbar489032a2008-10-03 17:11:57 +0000748 unsigned RegNum = RI->getDwarfRegNum(Src.getReg(), true);
Chris Lattneraf76e592009-08-22 20:48:53 +0000749 FinalSize += MCAsmInfo::getULEB128Size(RegNum);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000750 }
751
752 int Offset = -Src.getOffset();
753
Chris Lattneraf76e592009-08-22 20:48:53 +0000754 FinalSize += MCAsmInfo::getULEB128Size(Offset);
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 }
Daniel Dunbar489032a2008-10-03 17:11:57 +0000758 } else if (Src.isReg() &&
759 Src.getReg() == MachineLocation::VirtualFP) {
760 if (Dst.isReg()) {
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000761 ++FinalSize;
Daniel Dunbar489032a2008-10-03 17:11:57 +0000762 unsigned RegNum = RI->getDwarfRegNum(Dst.getReg(), true);
Chris Lattneraf76e592009-08-22 20:48:53 +0000763 FinalSize += MCAsmInfo::getULEB128Size(RegNum);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000764 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000765 llvm_unreachable("Machine move no supported yet.");
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000766 }
767 } else {
Daniel Dunbar489032a2008-10-03 17:11:57 +0000768 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), true);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000769 int Offset = Dst.getOffset() / stackGrowth;
770
771 if (Offset < 0) {
772 ++FinalSize;
Chris Lattneraf76e592009-08-22 20:48:53 +0000773 FinalSize += MCAsmInfo::getULEB128Size(Reg);
774 FinalSize += MCAsmInfo::getSLEB128Size(Offset);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000775 } else if (Reg < 64) {
776 ++FinalSize;
Chris Lattneraf76e592009-08-22 20:48:53 +0000777 FinalSize += MCAsmInfo::getULEB128Size(Offset);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000778 } else {
779 ++FinalSize;
Chris Lattneraf76e592009-08-22 20:48:53 +0000780 FinalSize += MCAsmInfo::getULEB128Size(Reg);
781 FinalSize += MCAsmInfo::getULEB128Size(Offset);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000782 }
783 }
784 }
785 return FinalSize;
786}
787
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000788unsigned
789JITDwarfEmitter::GetExceptionTableSizeInBytes(MachineFunction* MF) const {
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000790 unsigned FinalSize = 0;
791
792 // Map all labels and get rid of any dead landing pads.
793 MMI->TidyLandingPads();
794
795 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
796 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
797 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
798 if (PadInfos.empty()) return 0;
799
800 // Sort the landing pads in order of their type ids. This is used to fold
801 // duplicate actions.
802 SmallVector<const LandingPadInfo *, 64> LandingPads;
803 LandingPads.reserve(PadInfos.size());
804 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
805 LandingPads.push_back(&PadInfos[i]);
806 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
807
808 // Negative type ids index into FilterIds, positive type ids index into
809 // TypeInfos. The value written for a positive type id is just the type
810 // id itself. For a negative type id, however, the value written is the
811 // (negative) byte offset of the corresponding FilterIds entry. The byte
812 // offset is usually equal to the type id, because the FilterIds entries
813 // are written using a variable width encoding which outputs one byte per
814 // entry as long as the value written is not too large, but can differ.
815 // This kind of complication does not occur for positive type ids because
816 // type infos are output using a fixed width encoding.
817 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
818 SmallVector<int, 16> FilterOffsets;
819 FilterOffsets.reserve(FilterIds.size());
820 int Offset = -1;
821 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
822 E = FilterIds.end(); I != E; ++I) {
823 FilterOffsets.push_back(Offset);
Chris Lattneraf76e592009-08-22 20:48:53 +0000824 Offset -= MCAsmInfo::getULEB128Size(*I);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000825 }
826
827 // Compute the actions table and gather the first action index for each
828 // landing pad site.
829 SmallVector<ActionEntry, 32> Actions;
830 SmallVector<unsigned, 64> FirstActions;
831 FirstActions.reserve(LandingPads.size());
832
833 int FirstAction = 0;
834 unsigned SizeActions = 0;
835 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
836 const LandingPadInfo *LP = LandingPads[i];
837 const std::vector<int> &TypeIds = LP->TypeIds;
838 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
839 unsigned SizeSiteActions = 0;
840
841 if (NumShared < TypeIds.size()) {
842 unsigned SizeAction = 0;
843 ActionEntry *PrevAction = 0;
844
845 if (NumShared) {
846 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
847 assert(Actions.size());
848 PrevAction = &Actions.back();
Chris Lattneraf76e592009-08-22 20:48:53 +0000849 SizeAction = MCAsmInfo::getSLEB128Size(PrevAction->NextAction) +
850 MCAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000851 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
Chris Lattneraf76e592009-08-22 20:48:53 +0000852 SizeAction -= MCAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000853 SizeAction += -PrevAction->NextAction;
854 PrevAction = PrevAction->Previous;
855 }
856 }
857
858 // Compute the actions.
859 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
860 int TypeID = TypeIds[I];
861 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
862 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
Chris Lattneraf76e592009-08-22 20:48:53 +0000863 unsigned SizeTypeID = MCAsmInfo::getSLEB128Size(ValueForTypeID);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000864
865 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
Chris Lattneraf76e592009-08-22 20:48:53 +0000866 SizeAction = SizeTypeID + MCAsmInfo::getSLEB128Size(NextAction);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000867 SizeSiteActions += SizeAction;
868
869 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
870 Actions.push_back(Action);
871
872 PrevAction = &Actions.back();
873 }
874
875 // Record the first action of the landing pad site.
876 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
877 } // else identical - re-use previous FirstAction
878
879 FirstActions.push_back(FirstAction);
880
881 // Compute this sites contribution to size.
882 SizeActions += SizeSiteActions;
883 }
884
885 // Compute the call-site table. Entries must be ordered by address.
886 SmallVector<CallSiteEntry, 64> CallSites;
887
888 RangeMapType PadMap;
889 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
890 const LandingPadInfo *LandingPad = LandingPads[i];
891 for (unsigned j=0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
892 unsigned BeginLabel = LandingPad->BeginLabels[j];
893 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
894 PadRange P = { i, j };
895 PadMap[BeginLabel] = P;
896 }
897 }
898
899 bool MayThrow = false;
900 unsigned LastLabel = 0;
901 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
902 I != E; ++I) {
903 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
904 MI != E; ++MI) {
Dan Gohman44066042008-07-01 00:05:16 +0000905 if (!MI->isLabel()) {
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000906 MayThrow |= MI->getDesc().isCall();
907 continue;
908 }
909
910 unsigned BeginLabel = MI->getOperand(0).getImm();
911 assert(BeginLabel && "Invalid label!");
912
913 if (BeginLabel == LastLabel)
914 MayThrow = false;
915
916 RangeMapType::iterator L = PadMap.find(BeginLabel);
917
918 if (L == PadMap.end())
919 continue;
920
921 PadRange P = L->second;
922 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
923
924 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
925 "Inconsistent landing pad map!");
926
927 // If some instruction between the previous try-range and this one may
928 // throw, create a call-site entry with no landing pad for the region
929 // between the try-ranges.
930 if (MayThrow) {
931 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
932 CallSites.push_back(Site);
933 }
934
935 LastLabel = LandingPad->EndLabels[P.RangeIndex];
936 CallSiteEntry Site = {BeginLabel, LastLabel,
937 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
938
939 assert(Site.BeginLabel && Site.EndLabel && Site.PadLabel &&
940 "Invalid landing pad!");
941
942 // Try to merge with the previous call-site.
943 if (CallSites.size()) {
Dan Gohman719de532008-06-21 22:00:54 +0000944 CallSiteEntry &Prev = CallSites.back();
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000945 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
946 // Extend the range of the previous entry.
947 Prev.EndLabel = Site.EndLabel;
948 continue;
949 }
950 }
951
952 // Otherwise, create a new call-site.
953 CallSites.push_back(Site);
954 }
955 }
956 // If some instruction between the previous try-range and the end of the
957 // function may throw, create a call-site entry with no landing pad for the
958 // region following the try-range.
959 if (MayThrow) {
960 CallSiteEntry Site = {LastLabel, 0, 0, 0};
961 CallSites.push_back(Site);
962 }
963
964 // Final tallies.
965 unsigned SizeSites = CallSites.size() * (sizeof(int32_t) + // Site start.
966 sizeof(int32_t) + // Site length.
967 sizeof(int32_t)); // Landing pad.
968 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
Chris Lattneraf76e592009-08-22 20:48:53 +0000969 SizeSites += MCAsmInfo::getULEB128Size(CallSites[i].Action);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000970
971 unsigned SizeTypes = TypeInfos.size() * TD->getPointerSize();
972
973 unsigned TypeOffset = sizeof(int8_t) + // Call site format
974 // Call-site table length
Chris Lattneraf76e592009-08-22 20:48:53 +0000975 MCAsmInfo::getULEB128Size(SizeSites) +
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000976 SizeSites + SizeActions + SizeTypes;
977
978 unsigned TotalSize = sizeof(int8_t) + // LPStart format
979 sizeof(int8_t) + // TType format
Chris Lattneraf76e592009-08-22 20:48:53 +0000980 MCAsmInfo::getULEB128Size(TypeOffset) + // TType base offset
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000981 TypeOffset;
982
983 unsigned SizeAlign = (4 - TotalSize) & 3;
984
985 // Begin the exception table.
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000986 FinalSize = RoundUpToAlign(FinalSize, 4);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000987 for (unsigned i = 0; i != SizeAlign; ++i) {
988 ++FinalSize;
989 }
990
991 unsigned PointerSize = TD->getPointerSize();
992
993 // Emit the header.
994 ++FinalSize;
995 // Asm->EOL("LPStart format (DW_EH_PE_omit)");
996 ++FinalSize;
997 // Asm->EOL("TType format (DW_EH_PE_absptr)");
998 ++FinalSize;
999 // Asm->EOL("TType base offset");
1000 ++FinalSize;
1001 // Asm->EOL("Call site format (DW_EH_PE_udata4)");
1002 ++FinalSize;
1003 // Asm->EOL("Call-site table length");
1004
1005 // Emit the landing pad site information.
1006 for (unsigned i = 0; i < CallSites.size(); ++i) {
1007 CallSiteEntry &S = CallSites[i];
1008
1009 // Asm->EOL("Region start");
1010 FinalSize += PointerSize;
1011
1012 //Asm->EOL("Region length");
1013 FinalSize += PointerSize;
1014
1015 // Asm->EOL("Landing pad");
1016 FinalSize += PointerSize;
1017
Chris Lattneraf76e592009-08-22 20:48:53 +00001018 FinalSize += MCAsmInfo::getULEB128Size(S.Action);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +00001019 // Asm->EOL("Action");
1020 }
1021
1022 // Emit the actions.
1023 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
1024 ActionEntry &Action = Actions[I];
1025
1026 //Asm->EOL("TypeInfo index");
Chris Lattneraf76e592009-08-22 20:48:53 +00001027 FinalSize += MCAsmInfo::getSLEB128Size(Action.ValueForTypeID);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +00001028 //Asm->EOL("Next action");
Chris Lattneraf76e592009-08-22 20:48:53 +00001029 FinalSize += MCAsmInfo::getSLEB128Size(Action.NextAction);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +00001030 }
1031
1032 // Emit the type ids.
1033 for (unsigned M = TypeInfos.size(); M; --M) {
1034 // Asm->EOL("TypeInfo");
1035 FinalSize += PointerSize;
1036 }
1037
1038 // Emit the filter typeids.
1039 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
1040 unsigned TypeID = FilterIds[j];
Chris Lattneraf76e592009-08-22 20:48:53 +00001041 FinalSize += MCAsmInfo::getULEB128Size(TypeID);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +00001042 //Asm->EOL("Filter TypeInfo index");
1043 }
1044
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +00001045 FinalSize = RoundUpToAlign(FinalSize, 4);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +00001046
1047 return FinalSize;
1048}