blob: d9651f9c3b362428134c1bd5aa3e8a5090c3603f [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,
Reid Kleckner27632172009-09-20 23:52:43 +000039 unsigned char* EndFunction,
40 unsigned char* &EHFramePtr) {
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000041 const TargetMachine& TM = F.getTarget();
42 TD = TM.getTargetData();
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;
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000051
52 const std::vector<Function *> Personalities = MMI->getPersonalities();
53 EHFramePtr = EmitCommonEHFrame(Personalities[MMI->getPersonalityIndex()]);
54
55 Result = EmitEHFrame(Personalities[MMI->getPersonalityIndex()], EHFramePtr,
56 StartFunction, EndFunction, ExceptionTable);
Bill Wendling9d48b552009-09-09 00:11:02 +000057
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000058 return Result;
59}
60
61
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +000062void
63JITDwarfEmitter::EmitFrameMoves(intptr_t BaseLabelPtr,
64 const std::vector<MachineMove> &Moves) const {
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000065 unsigned PointerSize = TD->getPointerSize();
66 int stackGrowth = stackGrowthDirection == TargetFrameInfo::StackGrowsUp ?
67 PointerSize : -PointerSize;
Nicolas Geoffray2d450eb2008-08-19 14:48:14 +000068 bool IsLocal = false;
69 unsigned BaseLabelID = 0;
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000070
71 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
72 const MachineMove &Move = Moves[i];
73 unsigned LabelID = Move.getLabelID();
74
75 if (LabelID) {
76 LabelID = MMI->MappedLabel(LabelID);
77
78 // Throw out move if the label is invalid.
79 if (!LabelID) continue;
80 }
81
82 intptr_t LabelPtr = 0;
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000083 if (LabelID) LabelPtr = JCE->getLabelAddress(LabelID);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000084
85 const MachineLocation &Dst = Move.getDestination();
86 const MachineLocation &Src = Move.getSource();
87
88 // Advance row if new location.
Nicolas Geoffray2d450eb2008-08-19 14:48:14 +000089 if (BaseLabelPtr && LabelID && (BaseLabelID != LabelID || !IsLocal)) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000090 JCE->emitByte(dwarf::DW_CFA_advance_loc4);
91 JCE->emitInt32(LabelPtr - BaseLabelPtr);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000092
Nicolas Geoffray2d450eb2008-08-19 14:48:14 +000093 BaseLabelID = LabelID;
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000094 BaseLabelPtr = LabelPtr;
95 IsLocal = true;
96 }
97
98 // If advancing cfa.
Daniel Dunbar489032a2008-10-03 17:11:57 +000099 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
100 if (!Src.isReg()) {
101 if (Src.getReg() == MachineLocation::VirtualFP) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000102 JCE->emitByte(dwarf::DW_CFA_def_cfa_offset);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000103 } else {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000104 JCE->emitByte(dwarf::DW_CFA_def_cfa);
105 JCE->emitULEB128Bytes(RI->getDwarfRegNum(Src.getReg(), true));
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000106 }
107
Bill Wendling9d48b552009-09-09 00:11:02 +0000108 JCE->emitULEB128Bytes(-Src.getOffset());
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000109 } else {
Bill Wendling9d48b552009-09-09 00:11:02 +0000110 llvm_unreachable("Machine move not supported yet.");
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000111 }
Daniel Dunbar489032a2008-10-03 17:11:57 +0000112 } else if (Src.isReg() &&
113 Src.getReg() == MachineLocation::VirtualFP) {
114 if (Dst.isReg()) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000115 JCE->emitByte(dwarf::DW_CFA_def_cfa_register);
116 JCE->emitULEB128Bytes(RI->getDwarfRegNum(Dst.getReg(), true));
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000117 } else {
Bill Wendling9d48b552009-09-09 00:11:02 +0000118 llvm_unreachable("Machine move not supported yet.");
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000119 }
120 } else {
Daniel Dunbar489032a2008-10-03 17:11:57 +0000121 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), true);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000122 int Offset = Dst.getOffset() / stackGrowth;
123
124 if (Offset < 0) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000125 JCE->emitByte(dwarf::DW_CFA_offset_extended_sf);
126 JCE->emitULEB128Bytes(Reg);
127 JCE->emitSLEB128Bytes(Offset);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000128 } else if (Reg < 64) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000129 JCE->emitByte(dwarf::DW_CFA_offset + Reg);
130 JCE->emitULEB128Bytes(Offset);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000131 } else {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000132 JCE->emitByte(dwarf::DW_CFA_offset_extended);
133 JCE->emitULEB128Bytes(Reg);
134 JCE->emitULEB128Bytes(Offset);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000135 }
136 }
137 }
138}
139
140/// SharedTypeIds - How many leading type ids two landing pads have in common.
141static unsigned SharedTypeIds(const LandingPadInfo *L,
142 const LandingPadInfo *R) {
143 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
144 unsigned LSize = LIds.size(), RSize = RIds.size();
145 unsigned MinSize = LSize < RSize ? LSize : RSize;
146 unsigned Count = 0;
147
148 for (; Count != MinSize; ++Count)
149 if (LIds[Count] != RIds[Count])
150 return Count;
151
152 return Count;
153}
154
155
156/// PadLT - Order landing pads lexicographically by type id.
157static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
158 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
159 unsigned LSize = LIds.size(), RSize = RIds.size();
160 unsigned MinSize = LSize < RSize ? LSize : RSize;
161
162 for (unsigned i = 0; i != MinSize; ++i)
163 if (LIds[i] != RIds[i])
164 return LIds[i] < RIds[i];
165
166 return LSize < RSize;
167}
168
Dan Gohman844731a2008-05-13 00:00:25 +0000169namespace {
170
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000171struct KeyInfo {
172 static inline unsigned getEmptyKey() { return -1U; }
173 static inline unsigned getTombstoneKey() { return -2U; }
174 static unsigned getHashValue(const unsigned &Key) { return Key; }
175 static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
176 static bool isPod() { return true; }
177};
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 {
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000209 // Map all labels and get rid of any dead landing pads.
210 MMI->TidyLandingPads();
211
212 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
213 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
214 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
215 if (PadInfos.empty()) return 0;
216
217 // Sort the landing pads in order of their type ids. This is used to fold
218 // duplicate actions.
219 SmallVector<const LandingPadInfo *, 64> LandingPads;
220 LandingPads.reserve(PadInfos.size());
221 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
222 LandingPads.push_back(&PadInfos[i]);
223 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
224
225 // Negative type ids index into FilterIds, positive type ids index into
226 // TypeInfos. The value written for a positive type id is just the type
227 // id itself. For a negative type id, however, the value written is the
228 // (negative) byte offset of the corresponding FilterIds entry. The byte
229 // offset is usually equal to the type id, because the FilterIds entries
230 // are written using a variable width encoding which outputs one byte per
231 // entry as long as the value written is not too large, but can differ.
232 // This kind of complication does not occur for positive type ids because
233 // type infos are output using a fixed width encoding.
234 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
235 SmallVector<int, 16> FilterOffsets;
236 FilterOffsets.reserve(FilterIds.size());
237 int Offset = -1;
238 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
239 E = FilterIds.end(); I != E; ++I) {
240 FilterOffsets.push_back(Offset);
Chris Lattneraf76e592009-08-22 20:48:53 +0000241 Offset -= MCAsmInfo::getULEB128Size(*I);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000242 }
243
244 // Compute the actions table and gather the first action index for each
245 // landing pad site.
246 SmallVector<ActionEntry, 32> Actions;
247 SmallVector<unsigned, 64> FirstActions;
248 FirstActions.reserve(LandingPads.size());
249
250 int FirstAction = 0;
251 unsigned SizeActions = 0;
252 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
253 const LandingPadInfo *LP = LandingPads[i];
254 const std::vector<int> &TypeIds = LP->TypeIds;
255 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
256 unsigned SizeSiteActions = 0;
257
258 if (NumShared < TypeIds.size()) {
259 unsigned SizeAction = 0;
260 ActionEntry *PrevAction = 0;
261
262 if (NumShared) {
263 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
264 assert(Actions.size());
265 PrevAction = &Actions.back();
Chris Lattneraf76e592009-08-22 20:48:53 +0000266 SizeAction = MCAsmInfo::getSLEB128Size(PrevAction->NextAction) +
267 MCAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000268 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
Chris Lattneraf76e592009-08-22 20:48:53 +0000269 SizeAction -= MCAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000270 SizeAction += -PrevAction->NextAction;
271 PrevAction = PrevAction->Previous;
272 }
273 }
274
275 // Compute the actions.
276 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
277 int TypeID = TypeIds[I];
278 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
279 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
Chris Lattneraf76e592009-08-22 20:48:53 +0000280 unsigned SizeTypeID = MCAsmInfo::getSLEB128Size(ValueForTypeID);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000281
282 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
Chris Lattneraf76e592009-08-22 20:48:53 +0000283 SizeAction = SizeTypeID + MCAsmInfo::getSLEB128Size(NextAction);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000284 SizeSiteActions += SizeAction;
285
286 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
287 Actions.push_back(Action);
288
289 PrevAction = &Actions.back();
290 }
291
292 // Record the first action of the landing pad site.
293 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
294 } // else identical - re-use previous FirstAction
295
296 FirstActions.push_back(FirstAction);
297
298 // Compute this sites contribution to size.
299 SizeActions += SizeSiteActions;
300 }
301
302 // Compute the call-site table. Entries must be ordered by address.
303 SmallVector<CallSiteEntry, 64> CallSites;
304
305 RangeMapType PadMap;
306 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
307 const LandingPadInfo *LandingPad = LandingPads[i];
308 for (unsigned j=0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
309 unsigned BeginLabel = LandingPad->BeginLabels[j];
310 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
311 PadRange P = { i, j };
312 PadMap[BeginLabel] = P;
313 }
314 }
315
316 bool MayThrow = false;
317 unsigned LastLabel = 0;
318 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
319 I != E; ++I) {
320 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
321 MI != E; ++MI) {
Dan Gohman44066042008-07-01 00:05:16 +0000322 if (!MI->isLabel()) {
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000323 MayThrow |= MI->getDesc().isCall();
324 continue;
325 }
326
327 unsigned BeginLabel = MI->getOperand(0).getImm();
328 assert(BeginLabel && "Invalid label!");
329
330 if (BeginLabel == LastLabel)
331 MayThrow = false;
332
333 RangeMapType::iterator L = PadMap.find(BeginLabel);
334
335 if (L == PadMap.end())
336 continue;
337
338 PadRange P = L->second;
339 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
340
341 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
342 "Inconsistent landing pad map!");
343
344 // If some instruction between the previous try-range and this one may
345 // throw, create a call-site entry with no landing pad for the region
346 // between the try-ranges.
347 if (MayThrow) {
348 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
349 CallSites.push_back(Site);
350 }
351
352 LastLabel = LandingPad->EndLabels[P.RangeIndex];
353 CallSiteEntry Site = {BeginLabel, LastLabel,
354 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
355
356 assert(Site.BeginLabel && Site.EndLabel && Site.PadLabel &&
357 "Invalid landing pad!");
358
359 // Try to merge with the previous call-site.
360 if (CallSites.size()) {
Dan Gohman719de532008-06-21 22:00:54 +0000361 CallSiteEntry &Prev = CallSites.back();
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000362 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
363 // Extend the range of the previous entry.
364 Prev.EndLabel = Site.EndLabel;
365 continue;
366 }
367 }
368
369 // Otherwise, create a new call-site.
370 CallSites.push_back(Site);
371 }
372 }
373 // If some instruction between the previous try-range and the end of the
374 // function may throw, create a call-site entry with no landing pad for the
375 // region following the try-range.
376 if (MayThrow) {
377 CallSiteEntry Site = {LastLabel, 0, 0, 0};
378 CallSites.push_back(Site);
379 }
380
381 // Final tallies.
382 unsigned SizeSites = CallSites.size() * (sizeof(int32_t) + // Site start.
383 sizeof(int32_t) + // Site length.
384 sizeof(int32_t)); // Landing pad.
385 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
Chris Lattneraf76e592009-08-22 20:48:53 +0000386 SizeSites += MCAsmInfo::getULEB128Size(CallSites[i].Action);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000387
388 unsigned SizeTypes = TypeInfos.size() * TD->getPointerSize();
389
390 unsigned TypeOffset = sizeof(int8_t) + // Call site format
391 // Call-site table length
Chris Lattneraf76e592009-08-22 20:48:53 +0000392 MCAsmInfo::getULEB128Size(SizeSites) +
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000393 SizeSites + SizeActions + SizeTypes;
394
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000395 // Begin the exception table.
Reid Kleckner01248e62009-08-21 21:03:57 +0000396 JCE->emitAlignmentWithFill(4, 0);
397 // Asm->EOL("Padding");
398
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000399 unsigned char* DwarfExceptionTable = (unsigned char*)JCE->getCurrentPCValue();
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000400
401 // Emit the header.
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000402 JCE->emitByte(dwarf::DW_EH_PE_omit);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000403 // Asm->EOL("LPStart format (DW_EH_PE_omit)");
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000404 JCE->emitByte(dwarf::DW_EH_PE_absptr);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000405 // Asm->EOL("TType format (DW_EH_PE_absptr)");
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000406 JCE->emitULEB128Bytes(TypeOffset);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000407 // Asm->EOL("TType base offset");
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000408 JCE->emitByte(dwarf::DW_EH_PE_udata4);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000409 // Asm->EOL("Call site format (DW_EH_PE_udata4)");
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000410 JCE->emitULEB128Bytes(SizeSites);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000411 // Asm->EOL("Call-site table length");
412
413 // Emit the landing pad site information.
414 for (unsigned i = 0; i < CallSites.size(); ++i) {
415 CallSiteEntry &S = CallSites[i];
416 intptr_t BeginLabelPtr = 0;
417 intptr_t EndLabelPtr = 0;
418
419 if (!S.BeginLabel) {
420 BeginLabelPtr = (intptr_t)StartFunction;
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000421 JCE->emitInt32(0);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000422 } else {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000423 BeginLabelPtr = JCE->getLabelAddress(S.BeginLabel);
424 JCE->emitInt32(BeginLabelPtr - (intptr_t)StartFunction);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000425 }
426
427 // Asm->EOL("Region start");
428
429 if (!S.EndLabel) {
430 EndLabelPtr = (intptr_t)EndFunction;
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000431 JCE->emitInt32((intptr_t)EndFunction - BeginLabelPtr);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000432 } else {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000433 EndLabelPtr = JCE->getLabelAddress(S.EndLabel);
434 JCE->emitInt32(EndLabelPtr - BeginLabelPtr);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000435 }
436 //Asm->EOL("Region length");
437
438 if (!S.PadLabel) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000439 JCE->emitInt32(0);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000440 } else {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000441 unsigned PadLabelPtr = JCE->getLabelAddress(S.PadLabel);
442 JCE->emitInt32(PadLabelPtr - (intptr_t)StartFunction);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000443 }
444 // Asm->EOL("Landing pad");
445
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000446 JCE->emitULEB128Bytes(S.Action);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000447 // Asm->EOL("Action");
448 }
449
450 // Emit the actions.
451 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
452 ActionEntry &Action = Actions[I];
453
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000454 JCE->emitSLEB128Bytes(Action.ValueForTypeID);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000455 //Asm->EOL("TypeInfo index");
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000456 JCE->emitSLEB128Bytes(Action.NextAction);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000457 //Asm->EOL("Next action");
458 }
459
460 // Emit the type ids.
461 for (unsigned M = TypeInfos.size(); M; --M) {
462 GlobalVariable *GV = TypeInfos[M - 1];
463
464 if (GV) {
Bill Wendling9d48b552009-09-09 00:11:02 +0000465 if (TD->getPointerSize() == sizeof(int32_t))
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000466 JCE->emitInt32((intptr_t)Jit.getOrEmitGlobalVariable(GV));
Bill Wendling9d48b552009-09-09 00:11:02 +0000467 else
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000468 JCE->emitInt64((intptr_t)Jit.getOrEmitGlobalVariable(GV));
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000469 } else {
470 if (TD->getPointerSize() == sizeof(int32_t))
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000471 JCE->emitInt32(0);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000472 else
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000473 JCE->emitInt64(0);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000474 }
475 // Asm->EOL("TypeInfo");
476 }
477
478 // Emit the filter typeids.
479 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
480 unsigned TypeID = FilterIds[j];
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000481 JCE->emitULEB128Bytes(TypeID);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000482 //Asm->EOL("Filter TypeInfo index");
483 }
Reid Kleckner01248e62009-08-21 21:03:57 +0000484
485 JCE->emitAlignmentWithFill(4, 0);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000486
487 return DwarfExceptionTable;
488}
489
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000490unsigned char*
491JITDwarfEmitter::EmitCommonEHFrame(const Function* Personality) const {
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000492 unsigned PointerSize = TD->getPointerSize();
493 int stackGrowth = stackGrowthDirection == TargetFrameInfo::StackGrowsUp ?
494 PointerSize : -PointerSize;
495
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000496 unsigned char* StartCommonPtr = (unsigned char*)JCE->getCurrentPCValue();
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000497 // EH Common Frame header
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000498 JCE->allocateSpace(4, 0);
499 unsigned char* FrameCommonBeginPtr = (unsigned char*)JCE->getCurrentPCValue();
500 JCE->emitInt32((int)0);
501 JCE->emitByte(dwarf::DW_CIE_VERSION);
502 JCE->emitString(Personality ? "zPLR" : "zR");
503 JCE->emitULEB128Bytes(1);
504 JCE->emitSLEB128Bytes(stackGrowth);
505 JCE->emitByte(RI->getDwarfRegNum(RI->getRARegister(), true));
Bill Wendling9d48b552009-09-09 00:11:02 +0000506
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000507 if (Personality) {
Nicolas Geoffray42cc8f12009-02-15 20:49:23 +0000508 // Augmentation Size: 3 small ULEBs of one byte each, and the personality
509 // function which size is PointerSize.
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000510 JCE->emitULEB128Bytes(3 + PointerSize);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000511
Nicolas Geoffray42cc8f12009-02-15 20:49:23 +0000512 // We set the encoding of the personality as direct encoding because we use
513 // the function pointer. The encoding is not relative because the current
514 // PC value may be bigger than the personality function pointer.
515 if (PointerSize == 4) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000516 JCE->emitByte(dwarf::DW_EH_PE_sdata4);
517 JCE->emitInt32(((intptr_t)Jit.getPointerToGlobal(Personality)));
Nicolas Geoffray42cc8f12009-02-15 20:49:23 +0000518 } else {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000519 JCE->emitByte(dwarf::DW_EH_PE_sdata8);
520 JCE->emitInt64(((intptr_t)Jit.getPointerToGlobal(Personality)));
Nicolas Geoffray42cc8f12009-02-15 20:49:23 +0000521 }
Bill Wendling9d48b552009-09-09 00:11:02 +0000522
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000523 JCE->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
524 JCE->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000525 } else {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000526 JCE->emitULEB128Bytes(1);
527 JCE->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000528 }
529
530 std::vector<MachineMove> Moves;
531 RI->getInitialFrameState(Moves);
532 EmitFrameMoves(0, Moves);
Reid Kleckner01248e62009-08-21 21:03:57 +0000533
534 JCE->emitAlignmentWithFill(PointerSize, dwarf::DW_CFA_nop);
535
536 JCE->emitInt32At((uintptr_t*)StartCommonPtr,
537 (uintptr_t)((unsigned char*)JCE->getCurrentPCValue() -
538 FrameCommonBeginPtr));
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000539
540 return StartCommonPtr;
541}
542
543
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000544unsigned char*
545JITDwarfEmitter::EmitEHFrame(const Function* Personality,
546 unsigned char* StartCommonPtr,
547 unsigned char* StartFunction,
548 unsigned char* EndFunction,
549 unsigned char* ExceptionTable) const {
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000550 unsigned PointerSize = TD->getPointerSize();
551
552 // EH frame header.
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000553 unsigned char* StartEHPtr = (unsigned char*)JCE->getCurrentPCValue();
554 JCE->allocateSpace(4, 0);
555 unsigned char* FrameBeginPtr = (unsigned char*)JCE->getCurrentPCValue();
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000556 // FDE CIE Offset
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000557 JCE->emitInt32(FrameBeginPtr - StartCommonPtr);
558 JCE->emitInt32(StartFunction - (unsigned char*)JCE->getCurrentPCValue());
559 JCE->emitInt32(EndFunction - StartFunction);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000560
561 // If there is a personality and landing pads then point to the language
562 // specific data area in the exception table.
Bill Wendling9d48b552009-09-09 00:11:02 +0000563 if (Personality) {
564 JCE->emitULEB128Bytes(PointerSize == 4 ? 4 : 8);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000565
Bill Wendling9d48b552009-09-09 00:11:02 +0000566 if (PointerSize == 4) {
567 if (!MMI->getLandingPads().empty())
568 JCE->emitInt32(ExceptionTable-(unsigned char*)JCE->getCurrentPCValue());
569 else
570 JCE->emitInt32((int)0);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000571 } else {
Bill Wendling9d48b552009-09-09 00:11:02 +0000572 if (!MMI->getLandingPads().empty())
573 JCE->emitInt64(ExceptionTable-(unsigned char*)JCE->getCurrentPCValue());
574 else
575 JCE->emitInt64((int)0);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000576 }
577 } else {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000578 JCE->emitULEB128Bytes(0);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000579 }
580
581 // Indicate locations of function specific callee saved registers in
582 // frame.
583 EmitFrameMoves((intptr_t)StartFunction, MMI->getFrameMoves());
Reid Kleckner01248e62009-08-21 21:03:57 +0000584
585 JCE->emitAlignmentWithFill(PointerSize, dwarf::DW_CFA_nop);
586
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000587 // Indicate the size of the table
Reid Kleckner01248e62009-08-21 21:03:57 +0000588 JCE->emitInt32At((uintptr_t*)StartEHPtr,
589 (uintptr_t)((unsigned char*)JCE->getCurrentPCValue() -
590 StartEHPtr));
591
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000592 // Double zeroes for the unwind runtime
593 if (PointerSize == 8) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000594 JCE->emitInt64(0);
595 JCE->emitInt64(0);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000596 } else {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000597 JCE->emitInt32(0);
598 JCE->emitInt32(0);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000599 }
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000600
601 return StartEHPtr;
602}
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000603
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000604unsigned JITDwarfEmitter::GetDwarfTableSizeInBytes(MachineFunction& F,
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000605 JITCodeEmitter& jce,
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000606 unsigned char* StartFunction,
607 unsigned char* EndFunction) {
608 const TargetMachine& TM = F.getTarget();
609 TD = TM.getTargetData();
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000610 stackGrowthDirection = TM.getFrameInfo()->getStackGrowthDirection();
611 RI = TM.getRegisterInfo();
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000612 JCE = &jce;
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000613 unsigned FinalSize = 0;
614
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000615 FinalSize += GetExceptionTableSizeInBytes(&F);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000616
617 const std::vector<Function *> Personalities = MMI->getPersonalities();
Nicolas Geoffray67c8c4c2008-11-18 10:44:46 +0000618 FinalSize +=
619 GetCommonEHFrameSizeInBytes(Personalities[MMI->getPersonalityIndex()]);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000620
Nicolas Geoffray67c8c4c2008-11-18 10:44:46 +0000621 FinalSize += GetEHFrameSizeInBytes(Personalities[MMI->getPersonalityIndex()],
622 StartFunction);
Bill Wendling9d48b552009-09-09 00:11:02 +0000623
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000624 return FinalSize;
625}
626
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000627/// RoundUpToAlign - Add the specified alignment to FinalSize and returns
628/// the new value.
629static unsigned RoundUpToAlign(unsigned FinalSize, unsigned Alignment) {
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000630 if (Alignment == 0) Alignment = 1;
Nicolas Geoffray580631a2008-04-20 23:39:44 +0000631 // Since we do not know where the buffer will be allocated, be pessimistic.
632 return FinalSize + Alignment;
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000633}
634
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000635unsigned
636JITDwarfEmitter::GetEHFrameSizeInBytes(const Function* Personality,
637 unsigned char* StartFunction) const {
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000638 unsigned PointerSize = TD->getPointerSize();
639 unsigned FinalSize = 0;
640 // EH frame header.
641 FinalSize += PointerSize;
642 // FDE CIE Offset
643 FinalSize += 3 * PointerSize;
644 // If there is a personality and landing pads then point to the language
645 // specific data area in the exception table.
Bill Wendling9d48b552009-09-09 00:11:02 +0000646 if (Personality) {
Chris Lattneraf76e592009-08-22 20:48:53 +0000647 FinalSize += MCAsmInfo::getULEB128Size(4);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000648 FinalSize += PointerSize;
649 } else {
Chris Lattneraf76e592009-08-22 20:48:53 +0000650 FinalSize += MCAsmInfo::getULEB128Size(0);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000651 }
652
653 // Indicate locations of function specific callee saved registers in
654 // frame.
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000655 FinalSize += GetFrameMovesSizeInBytes((intptr_t)StartFunction,
656 MMI->getFrameMoves());
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000657
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000658 FinalSize = RoundUpToAlign(FinalSize, 4);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000659
660 // Double zeroes for the unwind runtime
661 FinalSize += 2 * PointerSize;
662
663 return FinalSize;
664}
665
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000666unsigned JITDwarfEmitter::GetCommonEHFrameSizeInBytes(const Function* Personality)
667 const {
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000668
669 unsigned PointerSize = TD->getPointerSize();
670 int stackGrowth = stackGrowthDirection == TargetFrameInfo::StackGrowsUp ?
671 PointerSize : -PointerSize;
672 unsigned FinalSize = 0;
673 // EH Common Frame header
674 FinalSize += PointerSize;
675 FinalSize += 4;
676 FinalSize += 1;
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000677 FinalSize += Personality ? 5 : 3; // "zPLR" or "zR"
Chris Lattneraf76e592009-08-22 20:48:53 +0000678 FinalSize += MCAsmInfo::getULEB128Size(1);
679 FinalSize += MCAsmInfo::getSLEB128Size(stackGrowth);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000680 FinalSize += 1;
681
682 if (Personality) {
Chris Lattneraf76e592009-08-22 20:48:53 +0000683 FinalSize += MCAsmInfo::getULEB128Size(7);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000684
685 // Encoding
686 FinalSize+= 1;
687 //Personality
688 FinalSize += PointerSize;
689
Chris Lattneraf76e592009-08-22 20:48:53 +0000690 FinalSize += MCAsmInfo::getULEB128Size(dwarf::DW_EH_PE_pcrel);
691 FinalSize += MCAsmInfo::getULEB128Size(dwarf::DW_EH_PE_pcrel);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000692
693 } else {
Chris Lattneraf76e592009-08-22 20:48:53 +0000694 FinalSize += MCAsmInfo::getULEB128Size(1);
695 FinalSize += MCAsmInfo::getULEB128Size(dwarf::DW_EH_PE_pcrel);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000696 }
697
698 std::vector<MachineMove> Moves;
699 RI->getInitialFrameState(Moves);
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000700 FinalSize += GetFrameMovesSizeInBytes(0, Moves);
701 FinalSize = RoundUpToAlign(FinalSize, 4);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000702 return FinalSize;
703}
704
705unsigned
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000706JITDwarfEmitter::GetFrameMovesSizeInBytes(intptr_t BaseLabelPtr,
707 const std::vector<MachineMove> &Moves) const {
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000708 unsigned PointerSize = TD->getPointerSize();
709 int stackGrowth = stackGrowthDirection == TargetFrameInfo::StackGrowsUp ?
710 PointerSize : -PointerSize;
711 bool IsLocal = BaseLabelPtr;
712 unsigned FinalSize = 0;
713
714 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
715 const MachineMove &Move = Moves[i];
716 unsigned LabelID = Move.getLabelID();
717
718 if (LabelID) {
719 LabelID = MMI->MappedLabel(LabelID);
720
721 // Throw out move if the label is invalid.
722 if (!LabelID) continue;
723 }
724
725 intptr_t LabelPtr = 0;
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000726 if (LabelID) LabelPtr = JCE->getLabelAddress(LabelID);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000727
728 const MachineLocation &Dst = Move.getDestination();
729 const MachineLocation &Src = Move.getSource();
730
731 // Advance row if new location.
732 if (BaseLabelPtr && LabelID && (BaseLabelPtr != LabelPtr || !IsLocal)) {
733 FinalSize++;
734 FinalSize += PointerSize;
735 BaseLabelPtr = LabelPtr;
736 IsLocal = true;
737 }
738
739 // If advancing cfa.
Daniel Dunbar489032a2008-10-03 17:11:57 +0000740 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
741 if (!Src.isReg()) {
742 if (Src.getReg() == MachineLocation::VirtualFP) {
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000743 ++FinalSize;
744 } else {
745 ++FinalSize;
Daniel Dunbar489032a2008-10-03 17:11:57 +0000746 unsigned RegNum = RI->getDwarfRegNum(Src.getReg(), true);
Chris Lattneraf76e592009-08-22 20:48:53 +0000747 FinalSize += MCAsmInfo::getULEB128Size(RegNum);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000748 }
749
750 int Offset = -Src.getOffset();
751
Chris Lattneraf76e592009-08-22 20:48:53 +0000752 FinalSize += MCAsmInfo::getULEB128Size(Offset);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000753 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000754 llvm_unreachable("Machine move no supported yet.");
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000755 }
Daniel Dunbar489032a2008-10-03 17:11:57 +0000756 } else if (Src.isReg() &&
757 Src.getReg() == MachineLocation::VirtualFP) {
758 if (Dst.isReg()) {
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000759 ++FinalSize;
Daniel Dunbar489032a2008-10-03 17:11:57 +0000760 unsigned RegNum = RI->getDwarfRegNum(Dst.getReg(), true);
Chris Lattneraf76e592009-08-22 20:48:53 +0000761 FinalSize += MCAsmInfo::getULEB128Size(RegNum);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000762 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000763 llvm_unreachable("Machine move no supported yet.");
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000764 }
765 } else {
Daniel Dunbar489032a2008-10-03 17:11:57 +0000766 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), true);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000767 int Offset = Dst.getOffset() / stackGrowth;
768
769 if (Offset < 0) {
770 ++FinalSize;
Chris Lattneraf76e592009-08-22 20:48:53 +0000771 FinalSize += MCAsmInfo::getULEB128Size(Reg);
772 FinalSize += MCAsmInfo::getSLEB128Size(Offset);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000773 } else if (Reg < 64) {
774 ++FinalSize;
Chris Lattneraf76e592009-08-22 20:48:53 +0000775 FinalSize += MCAsmInfo::getULEB128Size(Offset);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000776 } else {
777 ++FinalSize;
Chris Lattneraf76e592009-08-22 20:48:53 +0000778 FinalSize += MCAsmInfo::getULEB128Size(Reg);
779 FinalSize += MCAsmInfo::getULEB128Size(Offset);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000780 }
781 }
782 }
783 return FinalSize;
784}
785
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000786unsigned
787JITDwarfEmitter::GetExceptionTableSizeInBytes(MachineFunction* MF) const {
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000788 unsigned FinalSize = 0;
789
790 // Map all labels and get rid of any dead landing pads.
791 MMI->TidyLandingPads();
792
793 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
794 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
795 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
796 if (PadInfos.empty()) return 0;
797
798 // Sort the landing pads in order of their type ids. This is used to fold
799 // duplicate actions.
800 SmallVector<const LandingPadInfo *, 64> LandingPads;
801 LandingPads.reserve(PadInfos.size());
802 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
803 LandingPads.push_back(&PadInfos[i]);
804 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
805
806 // Negative type ids index into FilterIds, positive type ids index into
807 // TypeInfos. The value written for a positive type id is just the type
808 // id itself. For a negative type id, however, the value written is the
809 // (negative) byte offset of the corresponding FilterIds entry. The byte
810 // offset is usually equal to the type id, because the FilterIds entries
811 // are written using a variable width encoding which outputs one byte per
812 // entry as long as the value written is not too large, but can differ.
813 // This kind of complication does not occur for positive type ids because
814 // type infos are output using a fixed width encoding.
815 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
816 SmallVector<int, 16> FilterOffsets;
817 FilterOffsets.reserve(FilterIds.size());
818 int Offset = -1;
819 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
820 E = FilterIds.end(); I != E; ++I) {
821 FilterOffsets.push_back(Offset);
Chris Lattneraf76e592009-08-22 20:48:53 +0000822 Offset -= MCAsmInfo::getULEB128Size(*I);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000823 }
824
825 // Compute the actions table and gather the first action index for each
826 // landing pad site.
827 SmallVector<ActionEntry, 32> Actions;
828 SmallVector<unsigned, 64> FirstActions;
829 FirstActions.reserve(LandingPads.size());
830
831 int FirstAction = 0;
832 unsigned SizeActions = 0;
833 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
834 const LandingPadInfo *LP = LandingPads[i];
835 const std::vector<int> &TypeIds = LP->TypeIds;
836 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
837 unsigned SizeSiteActions = 0;
838
839 if (NumShared < TypeIds.size()) {
840 unsigned SizeAction = 0;
841 ActionEntry *PrevAction = 0;
842
843 if (NumShared) {
844 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
845 assert(Actions.size());
846 PrevAction = &Actions.back();
Chris Lattneraf76e592009-08-22 20:48:53 +0000847 SizeAction = MCAsmInfo::getSLEB128Size(PrevAction->NextAction) +
848 MCAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000849 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
Chris Lattneraf76e592009-08-22 20:48:53 +0000850 SizeAction -= MCAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000851 SizeAction += -PrevAction->NextAction;
852 PrevAction = PrevAction->Previous;
853 }
854 }
855
856 // Compute the actions.
857 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
858 int TypeID = TypeIds[I];
859 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
860 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
Chris Lattneraf76e592009-08-22 20:48:53 +0000861 unsigned SizeTypeID = MCAsmInfo::getSLEB128Size(ValueForTypeID);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000862
863 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
Chris Lattneraf76e592009-08-22 20:48:53 +0000864 SizeAction = SizeTypeID + MCAsmInfo::getSLEB128Size(NextAction);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000865 SizeSiteActions += SizeAction;
866
867 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
868 Actions.push_back(Action);
869
870 PrevAction = &Actions.back();
871 }
872
873 // Record the first action of the landing pad site.
874 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
875 } // else identical - re-use previous FirstAction
876
877 FirstActions.push_back(FirstAction);
878
879 // Compute this sites contribution to size.
880 SizeActions += SizeSiteActions;
881 }
882
883 // Compute the call-site table. Entries must be ordered by address.
884 SmallVector<CallSiteEntry, 64> CallSites;
885
886 RangeMapType PadMap;
887 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
888 const LandingPadInfo *LandingPad = LandingPads[i];
889 for (unsigned j=0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
890 unsigned BeginLabel = LandingPad->BeginLabels[j];
891 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
892 PadRange P = { i, j };
893 PadMap[BeginLabel] = P;
894 }
895 }
896
897 bool MayThrow = false;
898 unsigned LastLabel = 0;
899 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
900 I != E; ++I) {
901 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
902 MI != E; ++MI) {
Dan Gohman44066042008-07-01 00:05:16 +0000903 if (!MI->isLabel()) {
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000904 MayThrow |= MI->getDesc().isCall();
905 continue;
906 }
907
908 unsigned BeginLabel = MI->getOperand(0).getImm();
909 assert(BeginLabel && "Invalid label!");
910
911 if (BeginLabel == LastLabel)
912 MayThrow = false;
913
914 RangeMapType::iterator L = PadMap.find(BeginLabel);
915
916 if (L == PadMap.end())
917 continue;
918
919 PadRange P = L->second;
920 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
921
922 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
923 "Inconsistent landing pad map!");
924
925 // If some instruction between the previous try-range and this one may
926 // throw, create a call-site entry with no landing pad for the region
927 // between the try-ranges.
928 if (MayThrow) {
929 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
930 CallSites.push_back(Site);
931 }
932
933 LastLabel = LandingPad->EndLabels[P.RangeIndex];
934 CallSiteEntry Site = {BeginLabel, LastLabel,
935 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
936
937 assert(Site.BeginLabel && Site.EndLabel && Site.PadLabel &&
938 "Invalid landing pad!");
939
940 // Try to merge with the previous call-site.
941 if (CallSites.size()) {
Dan Gohman719de532008-06-21 22:00:54 +0000942 CallSiteEntry &Prev = CallSites.back();
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000943 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
944 // Extend the range of the previous entry.
945 Prev.EndLabel = Site.EndLabel;
946 continue;
947 }
948 }
949
950 // Otherwise, create a new call-site.
951 CallSites.push_back(Site);
952 }
953 }
954 // If some instruction between the previous try-range and the end of the
955 // function may throw, create a call-site entry with no landing pad for the
956 // region following the try-range.
957 if (MayThrow) {
958 CallSiteEntry Site = {LastLabel, 0, 0, 0};
959 CallSites.push_back(Site);
960 }
961
962 // Final tallies.
963 unsigned SizeSites = CallSites.size() * (sizeof(int32_t) + // Site start.
964 sizeof(int32_t) + // Site length.
965 sizeof(int32_t)); // Landing pad.
966 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
Chris Lattneraf76e592009-08-22 20:48:53 +0000967 SizeSites += MCAsmInfo::getULEB128Size(CallSites[i].Action);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000968
969 unsigned SizeTypes = TypeInfos.size() * TD->getPointerSize();
970
971 unsigned TypeOffset = sizeof(int8_t) + // Call site format
972 // Call-site table length
Chris Lattneraf76e592009-08-22 20:48:53 +0000973 MCAsmInfo::getULEB128Size(SizeSites) +
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000974 SizeSites + SizeActions + SizeTypes;
975
976 unsigned TotalSize = sizeof(int8_t) + // LPStart format
977 sizeof(int8_t) + // TType format
Chris Lattneraf76e592009-08-22 20:48:53 +0000978 MCAsmInfo::getULEB128Size(TypeOffset) + // TType base offset
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000979 TypeOffset;
980
981 unsigned SizeAlign = (4 - TotalSize) & 3;
982
983 // Begin the exception table.
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000984 FinalSize = RoundUpToAlign(FinalSize, 4);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000985 for (unsigned i = 0; i != SizeAlign; ++i) {
986 ++FinalSize;
987 }
988
989 unsigned PointerSize = TD->getPointerSize();
990
991 // Emit the header.
992 ++FinalSize;
993 // Asm->EOL("LPStart format (DW_EH_PE_omit)");
994 ++FinalSize;
995 // Asm->EOL("TType format (DW_EH_PE_absptr)");
996 ++FinalSize;
997 // Asm->EOL("TType base offset");
998 ++FinalSize;
999 // Asm->EOL("Call site format (DW_EH_PE_udata4)");
1000 ++FinalSize;
1001 // Asm->EOL("Call-site table length");
1002
1003 // Emit the landing pad site information.
1004 for (unsigned i = 0; i < CallSites.size(); ++i) {
1005 CallSiteEntry &S = CallSites[i];
1006
1007 // Asm->EOL("Region start");
1008 FinalSize += PointerSize;
1009
1010 //Asm->EOL("Region length");
1011 FinalSize += PointerSize;
1012
1013 // Asm->EOL("Landing pad");
1014 FinalSize += PointerSize;
1015
Chris Lattneraf76e592009-08-22 20:48:53 +00001016 FinalSize += MCAsmInfo::getULEB128Size(S.Action);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +00001017 // Asm->EOL("Action");
1018 }
1019
1020 // Emit the actions.
1021 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
1022 ActionEntry &Action = Actions[I];
1023
1024 //Asm->EOL("TypeInfo index");
Chris Lattneraf76e592009-08-22 20:48:53 +00001025 FinalSize += MCAsmInfo::getSLEB128Size(Action.ValueForTypeID);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +00001026 //Asm->EOL("Next action");
Chris Lattneraf76e592009-08-22 20:48:53 +00001027 FinalSize += MCAsmInfo::getSLEB128Size(Action.NextAction);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +00001028 }
1029
1030 // Emit the type ids.
1031 for (unsigned M = TypeInfos.size(); M; --M) {
1032 // Asm->EOL("TypeInfo");
1033 FinalSize += PointerSize;
1034 }
1035
1036 // Emit the filter typeids.
1037 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
1038 unsigned TypeID = FilterIds[j];
Chris Lattneraf76e592009-08-22 20:48:53 +00001039 FinalSize += MCAsmInfo::getULEB128Size(TypeID);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +00001040 //Asm->EOL("Filter TypeInfo index");
1041 }
1042
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +00001043 FinalSize = RoundUpToAlign(FinalSize, 4);
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +00001044
1045 return FinalSize;
1046}