blob: 694da1f0b561f30a4a4961e35434c28421e880e5 [file] [log] [blame]
Nicolas Geoffray0e757e12008-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"
Nicolas Geoffray0e757e12008-02-13 18:39:37 +000019#include "llvm/CodeGen/MachineCodeEmitter.h"
20#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/MachineLocation.h"
22#include "llvm/CodeGen/MachineModuleInfo.h"
23#include "llvm/ExecutionEngine/JITMemoryManager.h"
24#include "llvm/Target/TargetAsmInfo.h"
25#include "llvm/Target/TargetData.h"
26#include "llvm/Target/TargetInstrInfo.h"
27#include "llvm/Target/TargetFrameInfo.h"
28#include "llvm/Target/TargetMachine.h"
29#include "llvm/Target/TargetRegisterInfo.h"
30
31using namespace llvm;
32
33JITDwarfEmitter::JITDwarfEmitter(JIT& theJit) : Jit(theJit) {}
34
35
36unsigned char* JITDwarfEmitter::EmitDwarfTable(MachineFunction& F,
37 MachineCodeEmitter& mce,
38 unsigned char* StartFunction,
39 unsigned char* EndFunction) {
40 const TargetMachine& TM = F.getTarget();
41 TD = TM.getTargetData();
42 needsIndirectEncoding = TM.getTargetAsmInfo()->getNeedsIndirectEncoding();
43 stackGrowthDirection = TM.getFrameInfo()->getStackGrowthDirection();
44 RI = TM.getRegisterInfo();
45 MCE = &mce;
46
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);
58
59 return Result;
60}
61
62
Nicolas Geoffrayf748af22008-04-20 17:44:19 +000063void
64JITDwarfEmitter::EmitFrameMoves(intptr_t BaseLabelPtr,
65 const std::vector<MachineMove> &Moves) const {
Nicolas Geoffray0e757e12008-02-13 18:39:37 +000066 unsigned PointerSize = TD->getPointerSize();
67 int stackGrowth = stackGrowthDirection == TargetFrameInfo::StackGrowsUp ?
68 PointerSize : -PointerSize;
Nicolas Geoffray4eae34a2008-08-19 14:48:14 +000069 bool IsLocal = false;
70 unsigned BaseLabelID = 0;
Nicolas Geoffray0e757e12008-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;
84 if (LabelID) LabelPtr = MCE->getLabelAddress(LabelID);
85
86 const MachineLocation &Dst = Move.getDestination();
87 const MachineLocation &Src = Move.getSource();
88
89 // Advance row if new location.
Nicolas Geoffray4eae34a2008-08-19 14:48:14 +000090 if (BaseLabelPtr && LabelID && (BaseLabelID != LabelID || !IsLocal)) {
Nicolas Geoffray0e757e12008-02-13 18:39:37 +000091 MCE->emitByte(dwarf::DW_CFA_advance_loc4);
Nicolas Geoffray30920192008-11-18 10:44:46 +000092 MCE->emitInt32(LabelPtr - BaseLabelPtr);
Nicolas Geoffray0e757e12008-02-13 18:39:37 +000093
Nicolas Geoffray4eae34a2008-08-19 14:48:14 +000094 BaseLabelID = LabelID;
Nicolas Geoffray0e757e12008-02-13 18:39:37 +000095 BaseLabelPtr = LabelPtr;
96 IsLocal = true;
97 }
98
99 // If advancing cfa.
Daniel Dunbar39f5de32008-10-03 17:11:57 +0000100 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
101 if (!Src.isReg()) {
102 if (Src.getReg() == MachineLocation::VirtualFP) {
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000103 MCE->emitByte(dwarf::DW_CFA_def_cfa_offset);
104 } else {
105 MCE->emitByte(dwarf::DW_CFA_def_cfa);
Daniel Dunbar39f5de32008-10-03 17:11:57 +0000106 MCE->emitULEB128Bytes(RI->getDwarfRegNum(Src.getReg(), true));
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000107 }
108
109 int Offset = -Src.getOffset();
110
111 MCE->emitULEB128Bytes(Offset);
112 } else {
113 assert(0 && "Machine move no supported yet.");
114 }
Daniel Dunbar39f5de32008-10-03 17:11:57 +0000115 } else if (Src.isReg() &&
116 Src.getReg() == MachineLocation::VirtualFP) {
117 if (Dst.isReg()) {
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000118 MCE->emitByte(dwarf::DW_CFA_def_cfa_register);
Daniel Dunbar39f5de32008-10-03 17:11:57 +0000119 MCE->emitULEB128Bytes(RI->getDwarfRegNum(Dst.getReg(), true));
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000120 } else {
121 assert(0 && "Machine move no supported yet.");
122 }
123 } else {
Daniel Dunbar39f5de32008-10-03 17:11:57 +0000124 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), true);
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000125 int Offset = Dst.getOffset() / stackGrowth;
126
127 if (Offset < 0) {
128 MCE->emitByte(dwarf::DW_CFA_offset_extended_sf);
129 MCE->emitULEB128Bytes(Reg);
130 MCE->emitSLEB128Bytes(Offset);
131 } else if (Reg < 64) {
132 MCE->emitByte(dwarf::DW_CFA_offset + Reg);
133 MCE->emitULEB128Bytes(Offset);
134 } else {
135 MCE->emitByte(dwarf::DW_CFA_offset_extended);
136 MCE->emitULEB128Bytes(Reg);
137 MCE->emitULEB128Bytes(Offset);
138 }
139 }
140 }
141}
142
143/// SharedTypeIds - How many leading type ids two landing pads have in common.
144static unsigned SharedTypeIds(const LandingPadInfo *L,
145 const LandingPadInfo *R) {
146 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
147 unsigned LSize = LIds.size(), RSize = RIds.size();
148 unsigned MinSize = LSize < RSize ? LSize : RSize;
149 unsigned Count = 0;
150
151 for (; Count != MinSize; ++Count)
152 if (LIds[Count] != RIds[Count])
153 return Count;
154
155 return Count;
156}
157
158
159/// PadLT - Order landing pads lexicographically by type id.
160static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
161 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
162 unsigned LSize = LIds.size(), RSize = RIds.size();
163 unsigned MinSize = LSize < RSize ? LSize : RSize;
164
165 for (unsigned i = 0; i != MinSize; ++i)
166 if (LIds[i] != RIds[i])
167 return LIds[i] < RIds[i];
168
169 return LSize < RSize;
170}
171
Dan Gohman089efff2008-05-13 00:00:25 +0000172namespace {
173
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000174struct KeyInfo {
175 static inline unsigned getEmptyKey() { return -1U; }
176 static inline unsigned getTombstoneKey() { return -2U; }
177 static unsigned getHashValue(const unsigned &Key) { return Key; }
178 static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
179 static bool isPod() { return true; }
180};
181
182/// ActionEntry - Structure describing an entry in the actions table.
183struct ActionEntry {
184 int ValueForTypeID; // The value to write - may not be equal to the type id.
185 int NextAction;
186 struct ActionEntry *Previous;
187};
188
189/// PadRange - Structure holding a try-range and the associated landing pad.
190struct PadRange {
191 // The index of the landing pad.
192 unsigned PadIndex;
193 // The index of the begin and end labels in the landing pad's label lists.
194 unsigned RangeIndex;
195};
196
197typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
198
199/// CallSiteEntry - Structure describing an entry in the call-site table.
200struct CallSiteEntry {
201 unsigned BeginLabel; // zero indicates the start of the function.
202 unsigned EndLabel; // zero indicates the end of the function.
203 unsigned PadLabel; // zero indicates that there is no landing pad.
204 unsigned Action;
205};
206
Dan Gohman089efff2008-05-13 00:00:25 +0000207}
208
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000209unsigned char* JITDwarfEmitter::EmitExceptionTable(MachineFunction* MF,
210 unsigned char* StartFunction,
Nicolas Geoffrayf748af22008-04-20 17:44:19 +0000211 unsigned char* EndFunction) const {
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000212 // Map all labels and get rid of any dead landing pads.
213 MMI->TidyLandingPads();
214
215 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
216 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
217 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
218 if (PadInfos.empty()) return 0;
219
220 // Sort the landing pads in order of their type ids. This is used to fold
221 // duplicate actions.
222 SmallVector<const LandingPadInfo *, 64> LandingPads;
223 LandingPads.reserve(PadInfos.size());
224 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
225 LandingPads.push_back(&PadInfos[i]);
226 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
227
228 // Negative type ids index into FilterIds, positive type ids index into
229 // TypeInfos. The value written for a positive type id is just the type
230 // id itself. For a negative type id, however, the value written is the
231 // (negative) byte offset of the corresponding FilterIds entry. The byte
232 // offset is usually equal to the type id, because the FilterIds entries
233 // are written using a variable width encoding which outputs one byte per
234 // entry as long as the value written is not too large, but can differ.
235 // This kind of complication does not occur for positive type ids because
236 // type infos are output using a fixed width encoding.
237 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
238 SmallVector<int, 16> FilterOffsets;
239 FilterOffsets.reserve(FilterIds.size());
240 int Offset = -1;
241 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
242 E = FilterIds.end(); I != E; ++I) {
243 FilterOffsets.push_back(Offset);
aslc200b112008-08-16 12:57:46 +0000244 Offset -= TargetAsmInfo::getULEB128Size(*I);
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000245 }
246
247 // Compute the actions table and gather the first action index for each
248 // landing pad site.
249 SmallVector<ActionEntry, 32> Actions;
250 SmallVector<unsigned, 64> FirstActions;
251 FirstActions.reserve(LandingPads.size());
252
253 int FirstAction = 0;
254 unsigned SizeActions = 0;
255 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
256 const LandingPadInfo *LP = LandingPads[i];
257 const std::vector<int> &TypeIds = LP->TypeIds;
258 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
259 unsigned SizeSiteActions = 0;
260
261 if (NumShared < TypeIds.size()) {
262 unsigned SizeAction = 0;
263 ActionEntry *PrevAction = 0;
264
265 if (NumShared) {
266 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
267 assert(Actions.size());
268 PrevAction = &Actions.back();
aslc200b112008-08-16 12:57:46 +0000269 SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) +
270 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000271 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
aslc200b112008-08-16 12:57:46 +0000272 SizeAction -= TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000273 SizeAction += -PrevAction->NextAction;
274 PrevAction = PrevAction->Previous;
275 }
276 }
277
278 // Compute the actions.
279 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
280 int TypeID = TypeIds[I];
281 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
282 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
aslc200b112008-08-16 12:57:46 +0000283 unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID);
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000284
285 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
aslc200b112008-08-16 12:57:46 +0000286 SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction);
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000287 SizeSiteActions += SizeAction;
288
289 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
290 Actions.push_back(Action);
291
292 PrevAction = &Actions.back();
293 }
294
295 // Record the first action of the landing pad site.
296 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
297 } // else identical - re-use previous FirstAction
298
299 FirstActions.push_back(FirstAction);
300
301 // Compute this sites contribution to size.
302 SizeActions += SizeSiteActions;
303 }
304
305 // Compute the call-site table. Entries must be ordered by address.
306 SmallVector<CallSiteEntry, 64> CallSites;
307
308 RangeMapType PadMap;
309 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
310 const LandingPadInfo *LandingPad = LandingPads[i];
311 for (unsigned j=0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
312 unsigned BeginLabel = LandingPad->BeginLabels[j];
313 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
314 PadRange P = { i, j };
315 PadMap[BeginLabel] = P;
316 }
317 }
318
319 bool MayThrow = false;
320 unsigned LastLabel = 0;
321 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
322 I != E; ++I) {
323 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
324 MI != E; ++MI) {
Dan Gohmanfa607c92008-07-01 00:05:16 +0000325 if (!MI->isLabel()) {
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000326 MayThrow |= MI->getDesc().isCall();
327 continue;
328 }
329
330 unsigned BeginLabel = MI->getOperand(0).getImm();
331 assert(BeginLabel && "Invalid label!");
332
333 if (BeginLabel == LastLabel)
334 MayThrow = false;
335
336 RangeMapType::iterator L = PadMap.find(BeginLabel);
337
338 if (L == PadMap.end())
339 continue;
340
341 PadRange P = L->second;
342 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
343
344 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
345 "Inconsistent landing pad map!");
346
347 // If some instruction between the previous try-range and this one may
348 // throw, create a call-site entry with no landing pad for the region
349 // between the try-ranges.
350 if (MayThrow) {
351 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
352 CallSites.push_back(Site);
353 }
354
355 LastLabel = LandingPad->EndLabels[P.RangeIndex];
356 CallSiteEntry Site = {BeginLabel, LastLabel,
357 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
358
359 assert(Site.BeginLabel && Site.EndLabel && Site.PadLabel &&
360 "Invalid landing pad!");
361
362 // Try to merge with the previous call-site.
363 if (CallSites.size()) {
Dan Gohman3d436002008-06-21 22:00:54 +0000364 CallSiteEntry &Prev = CallSites.back();
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000365 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
366 // Extend the range of the previous entry.
367 Prev.EndLabel = Site.EndLabel;
368 continue;
369 }
370 }
371
372 // Otherwise, create a new call-site.
373 CallSites.push_back(Site);
374 }
375 }
376 // If some instruction between the previous try-range and the end of the
377 // function may throw, create a call-site entry with no landing pad for the
378 // region following the try-range.
379 if (MayThrow) {
380 CallSiteEntry Site = {LastLabel, 0, 0, 0};
381 CallSites.push_back(Site);
382 }
383
384 // Final tallies.
385 unsigned SizeSites = CallSites.size() * (sizeof(int32_t) + // Site start.
386 sizeof(int32_t) + // Site length.
387 sizeof(int32_t)); // Landing pad.
388 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
aslc200b112008-08-16 12:57:46 +0000389 SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action);
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000390
391 unsigned SizeTypes = TypeInfos.size() * TD->getPointerSize();
392
393 unsigned TypeOffset = sizeof(int8_t) + // Call site format
394 // Call-site table length
aslc200b112008-08-16 12:57:46 +0000395 TargetAsmInfo::getULEB128Size(SizeSites) +
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000396 SizeSites + SizeActions + SizeTypes;
397
398 unsigned TotalSize = sizeof(int8_t) + // LPStart format
399 sizeof(int8_t) + // TType format
aslc200b112008-08-16 12:57:46 +0000400 TargetAsmInfo::getULEB128Size(TypeOffset) + // TType base offset
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000401 TypeOffset;
402
403 unsigned SizeAlign = (4 - TotalSize) & 3;
404
405 // Begin the exception table.
406 MCE->emitAlignment(4);
407 for (unsigned i = 0; i != SizeAlign; ++i) {
408 MCE->emitByte(0);
409 // Asm->EOL("Padding");
410 }
411
412 unsigned char* DwarfExceptionTable = (unsigned char*)MCE->getCurrentPCValue();
413
414 // Emit the header.
415 MCE->emitByte(dwarf::DW_EH_PE_omit);
416 // Asm->EOL("LPStart format (DW_EH_PE_omit)");
417 MCE->emitByte(dwarf::DW_EH_PE_absptr);
418 // Asm->EOL("TType format (DW_EH_PE_absptr)");
419 MCE->emitULEB128Bytes(TypeOffset);
420 // Asm->EOL("TType base offset");
421 MCE->emitByte(dwarf::DW_EH_PE_udata4);
422 // Asm->EOL("Call site format (DW_EH_PE_udata4)");
423 MCE->emitULEB128Bytes(SizeSites);
424 // Asm->EOL("Call-site table length");
425
426 // Emit the landing pad site information.
427 for (unsigned i = 0; i < CallSites.size(); ++i) {
428 CallSiteEntry &S = CallSites[i];
429 intptr_t BeginLabelPtr = 0;
430 intptr_t EndLabelPtr = 0;
431
432 if (!S.BeginLabel) {
433 BeginLabelPtr = (intptr_t)StartFunction;
Nicolas Geoffray30920192008-11-18 10:44:46 +0000434 MCE->emitInt32(0);
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000435 } else {
436 BeginLabelPtr = MCE->getLabelAddress(S.BeginLabel);
Nicolas Geoffray30920192008-11-18 10:44:46 +0000437 MCE->emitInt32(BeginLabelPtr - (intptr_t)StartFunction);
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000438 }
439
440 // Asm->EOL("Region start");
441
442 if (!S.EndLabel) {
443 EndLabelPtr = (intptr_t)EndFunction;
Nicolas Geoffray30920192008-11-18 10:44:46 +0000444 MCE->emitInt32((intptr_t)EndFunction - BeginLabelPtr);
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000445 } else {
446 EndLabelPtr = MCE->getLabelAddress(S.EndLabel);
Nicolas Geoffray30920192008-11-18 10:44:46 +0000447 MCE->emitInt32(EndLabelPtr - BeginLabelPtr);
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000448 }
449 //Asm->EOL("Region length");
450
451 if (!S.PadLabel) {
Nicolas Geoffray30920192008-11-18 10:44:46 +0000452 MCE->emitInt32(0);
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000453 } else {
454 unsigned PadLabelPtr = MCE->getLabelAddress(S.PadLabel);
Nicolas Geoffray30920192008-11-18 10:44:46 +0000455 MCE->emitInt32(PadLabelPtr - (intptr_t)StartFunction);
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000456 }
457 // Asm->EOL("Landing pad");
458
459 MCE->emitULEB128Bytes(S.Action);
460 // Asm->EOL("Action");
461 }
462
463 // Emit the actions.
464 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
465 ActionEntry &Action = Actions[I];
466
467 MCE->emitSLEB128Bytes(Action.ValueForTypeID);
468 //Asm->EOL("TypeInfo index");
469 MCE->emitSLEB128Bytes(Action.NextAction);
470 //Asm->EOL("Next action");
471 }
472
473 // Emit the type ids.
474 for (unsigned M = TypeInfos.size(); M; --M) {
475 GlobalVariable *GV = TypeInfos[M - 1];
476
477 if (GV) {
478 if (TD->getPointerSize() == sizeof(int32_t)) {
479 MCE->emitInt32((intptr_t)Jit.getOrEmitGlobalVariable(GV));
480 } else {
481 MCE->emitInt64((intptr_t)Jit.getOrEmitGlobalVariable(GV));
482 }
483 } else {
484 if (TD->getPointerSize() == sizeof(int32_t))
485 MCE->emitInt32(0);
486 else
487 MCE->emitInt64(0);
488 }
489 // Asm->EOL("TypeInfo");
490 }
491
492 // Emit the filter typeids.
493 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
494 unsigned TypeID = FilterIds[j];
495 MCE->emitULEB128Bytes(TypeID);
496 //Asm->EOL("Filter TypeInfo index");
497 }
498
499 MCE->emitAlignment(4);
500
501 return DwarfExceptionTable;
502}
503
Nicolas Geoffrayf748af22008-04-20 17:44:19 +0000504unsigned char*
505JITDwarfEmitter::EmitCommonEHFrame(const Function* Personality) const {
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000506 unsigned PointerSize = TD->getPointerSize();
507 int stackGrowth = stackGrowthDirection == TargetFrameInfo::StackGrowsUp ?
508 PointerSize : -PointerSize;
509
510 unsigned char* StartCommonPtr = (unsigned char*)MCE->getCurrentPCValue();
511 // EH Common Frame header
Nicolas Geoffray30920192008-11-18 10:44:46 +0000512 MCE->allocateSpace(4, 0);
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000513 unsigned char* FrameCommonBeginPtr = (unsigned char*)MCE->getCurrentPCValue();
514 MCE->emitInt32((int)0);
515 MCE->emitByte(dwarf::DW_CIE_VERSION);
516 MCE->emitString(Personality ? "zPLR" : "zR");
517 MCE->emitULEB128Bytes(1);
518 MCE->emitSLEB128Bytes(stackGrowth);
519 MCE->emitByte(RI->getDwarfRegNum(RI->getRARegister(), true));
520
521 if (Personality) {
522 MCE->emitULEB128Bytes(7);
523
Nicolas Geoffray30920192008-11-18 10:44:46 +0000524 // Direct encoding, because we use the function pointer. Not relative,
525 // because the current PC value may be bigger than the personality
526 // function pointer.
527 MCE->emitByte(dwarf::DW_EH_PE_sdata4);
528
529 MCE->emitInt32(((intptr_t)Jit.getPointerToGlobal(Personality)));
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000530
Nicolas Geoffray4eae34a2008-08-19 14:48:14 +0000531 MCE->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
532 MCE->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000533
534 } else {
535 MCE->emitULEB128Bytes(1);
Nicolas Geoffray4eae34a2008-08-19 14:48:14 +0000536 MCE->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000537 }
538
539 std::vector<MachineMove> Moves;
540 RI->getInitialFrameState(Moves);
541 EmitFrameMoves(0, Moves);
Nicolas Geoffray30920192008-11-18 10:44:46 +0000542 MCE->emitAlignment(PointerSize);
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000543
Nicolas Geoffray30920192008-11-18 10:44:46 +0000544 MCE->emitInt32At((uintptr_t*)StartCommonPtr,
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000545 (uintptr_t)((unsigned char*)MCE->getCurrentPCValue() -
546 FrameCommonBeginPtr));
547
548 return StartCommonPtr;
549}
550
551
Nicolas Geoffrayf748af22008-04-20 17:44:19 +0000552unsigned char*
553JITDwarfEmitter::EmitEHFrame(const Function* Personality,
554 unsigned char* StartCommonPtr,
555 unsigned char* StartFunction,
556 unsigned char* EndFunction,
557 unsigned char* ExceptionTable) const {
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000558 unsigned PointerSize = TD->getPointerSize();
559
560 // EH frame header.
561 unsigned char* StartEHPtr = (unsigned char*)MCE->getCurrentPCValue();
Nicolas Geoffray30920192008-11-18 10:44:46 +0000562 MCE->allocateSpace(4, 0);
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000563 unsigned char* FrameBeginPtr = (unsigned char*)MCE->getCurrentPCValue();
564 // FDE CIE Offset
Nicolas Geoffray30920192008-11-18 10:44:46 +0000565 MCE->emitInt32(FrameBeginPtr - StartCommonPtr);
566 MCE->emitInt32(StartFunction - (unsigned char*)MCE->getCurrentPCValue());
567 MCE->emitInt32(EndFunction - StartFunction);
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000568
569 // If there is a personality and landing pads then point to the language
570 // specific data area in the exception table.
571 if (MMI->getPersonalityIndex()) {
572 MCE->emitULEB128Bytes(4);
573
574 if (!MMI->getLandingPads().empty()) {
Nicolas Geoffray30920192008-11-18 10:44:46 +0000575 MCE->emitInt32(ExceptionTable - (unsigned char*)MCE->getCurrentPCValue());
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000576 } else {
577 MCE->emitInt32((int)0);
578 }
579 } else {
580 MCE->emitULEB128Bytes(0);
581 }
582
583 // Indicate locations of function specific callee saved registers in
584 // frame.
585 EmitFrameMoves((intptr_t)StartFunction, MMI->getFrameMoves());
586
Nicolas Geoffray30920192008-11-18 10:44:46 +0000587 MCE->emitAlignment(PointerSize);
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000588
589 // Indicate the size of the table
Nicolas Geoffray30920192008-11-18 10:44:46 +0000590 MCE->emitInt32At((uintptr_t*)StartEHPtr,
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000591 (uintptr_t)((unsigned char*)MCE->getCurrentPCValue() -
592 StartEHPtr));
593
594 // Double zeroes for the unwind runtime
595 if (PointerSize == 8) {
596 MCE->emitInt64(0);
597 MCE->emitInt64(0);
598 } else {
599 MCE->emitInt32(0);
600 MCE->emitInt32(0);
601 }
602
603
604 return StartEHPtr;
605}
Nicolas Geoffray68847972008-04-18 20:59:31 +0000606
Nicolas Geoffrayf748af22008-04-20 17:44:19 +0000607unsigned JITDwarfEmitter::GetDwarfTableSizeInBytes(MachineFunction& F,
Nicolas Geoffray68847972008-04-18 20:59:31 +0000608 MachineCodeEmitter& mce,
609 unsigned char* StartFunction,
610 unsigned char* EndFunction) {
611 const TargetMachine& TM = F.getTarget();
612 TD = TM.getTargetData();
613 needsIndirectEncoding = TM.getTargetAsmInfo()->getNeedsIndirectEncoding();
614 stackGrowthDirection = TM.getFrameInfo()->getStackGrowthDirection();
615 RI = TM.getRegisterInfo();
616 MCE = &mce;
617 unsigned FinalSize = 0;
618
Nicolas Geoffrayf748af22008-04-20 17:44:19 +0000619 FinalSize += GetExceptionTableSizeInBytes(&F);
Nicolas Geoffray68847972008-04-18 20:59:31 +0000620
621 const std::vector<Function *> Personalities = MMI->getPersonalities();
Nicolas Geoffray30920192008-11-18 10:44:46 +0000622 FinalSize +=
623 GetCommonEHFrameSizeInBytes(Personalities[MMI->getPersonalityIndex()]);
Nicolas Geoffray68847972008-04-18 20:59:31 +0000624
Nicolas Geoffray30920192008-11-18 10:44:46 +0000625 FinalSize += GetEHFrameSizeInBytes(Personalities[MMI->getPersonalityIndex()],
626 StartFunction);
Nicolas Geoffray68847972008-04-18 20:59:31 +0000627
628 return FinalSize;
629}
630
Nicolas Geoffrayf748af22008-04-20 17:44:19 +0000631/// RoundUpToAlign - Add the specified alignment to FinalSize and returns
632/// the new value.
633static unsigned RoundUpToAlign(unsigned FinalSize, unsigned Alignment) {
Nicolas Geoffray68847972008-04-18 20:59:31 +0000634 if (Alignment == 0) Alignment = 1;
Nicolas Geoffray5a80b292008-04-20 23:39:44 +0000635 // Since we do not know where the buffer will be allocated, be pessimistic.
636 return FinalSize + Alignment;
Nicolas Geoffray68847972008-04-18 20:59:31 +0000637}
638
Nicolas Geoffrayf748af22008-04-20 17:44:19 +0000639unsigned
640JITDwarfEmitter::GetEHFrameSizeInBytes(const Function* Personality,
641 unsigned char* StartFunction) const {
Nicolas Geoffray68847972008-04-18 20:59:31 +0000642 unsigned PointerSize = TD->getPointerSize();
643 unsigned FinalSize = 0;
644 // EH frame header.
645 FinalSize += PointerSize;
646 // FDE CIE Offset
647 FinalSize += 3 * PointerSize;
648 // If there is a personality and landing pads then point to the language
649 // specific data area in the exception table.
650 if (MMI->getPersonalityIndex()) {
aslc200b112008-08-16 12:57:46 +0000651 FinalSize += TargetAsmInfo::getULEB128Size(4);
Nicolas Geoffray68847972008-04-18 20:59:31 +0000652 FinalSize += PointerSize;
653 } else {
aslc200b112008-08-16 12:57:46 +0000654 FinalSize += TargetAsmInfo::getULEB128Size(0);
Nicolas Geoffray68847972008-04-18 20:59:31 +0000655 }
656
657 // Indicate locations of function specific callee saved registers in
658 // frame.
Nicolas Geoffrayf748af22008-04-20 17:44:19 +0000659 FinalSize += GetFrameMovesSizeInBytes((intptr_t)StartFunction,
660 MMI->getFrameMoves());
Nicolas Geoffray68847972008-04-18 20:59:31 +0000661
Nicolas Geoffrayf748af22008-04-20 17:44:19 +0000662 FinalSize = RoundUpToAlign(FinalSize, 4);
Nicolas Geoffray68847972008-04-18 20:59:31 +0000663
664 // Double zeroes for the unwind runtime
665 FinalSize += 2 * PointerSize;
666
667 return FinalSize;
668}
669
Nicolas Geoffrayf748af22008-04-20 17:44:19 +0000670unsigned JITDwarfEmitter::GetCommonEHFrameSizeInBytes(const Function* Personality)
671 const {
Nicolas Geoffray68847972008-04-18 20:59:31 +0000672
673 unsigned PointerSize = TD->getPointerSize();
674 int stackGrowth = stackGrowthDirection == TargetFrameInfo::StackGrowsUp ?
675 PointerSize : -PointerSize;
676 unsigned FinalSize = 0;
677 // EH Common Frame header
678 FinalSize += PointerSize;
679 FinalSize += 4;
680 FinalSize += 1;
Nicolas Geoffrayf748af22008-04-20 17:44:19 +0000681 FinalSize += Personality ? 5 : 3; // "zPLR" or "zR"
aslc200b112008-08-16 12:57:46 +0000682 FinalSize += TargetAsmInfo::getULEB128Size(1);
683 FinalSize += TargetAsmInfo::getSLEB128Size(stackGrowth);
Nicolas Geoffray68847972008-04-18 20:59:31 +0000684 FinalSize += 1;
685
686 if (Personality) {
aslc200b112008-08-16 12:57:46 +0000687 FinalSize += TargetAsmInfo::getULEB128Size(7);
Nicolas Geoffray68847972008-04-18 20:59:31 +0000688
689 // Encoding
690 FinalSize+= 1;
691 //Personality
692 FinalSize += PointerSize;
693
aslc200b112008-08-16 12:57:46 +0000694 FinalSize += TargetAsmInfo::getULEB128Size(dwarf::DW_EH_PE_pcrel);
695 FinalSize += TargetAsmInfo::getULEB128Size(dwarf::DW_EH_PE_pcrel);
Nicolas Geoffray68847972008-04-18 20:59:31 +0000696
697 } else {
aslc200b112008-08-16 12:57:46 +0000698 FinalSize += TargetAsmInfo::getULEB128Size(1);
699 FinalSize += TargetAsmInfo::getULEB128Size(dwarf::DW_EH_PE_pcrel);
Nicolas Geoffray68847972008-04-18 20:59:31 +0000700 }
701
702 std::vector<MachineMove> Moves;
703 RI->getInitialFrameState(Moves);
Nicolas Geoffrayf748af22008-04-20 17:44:19 +0000704 FinalSize += GetFrameMovesSizeInBytes(0, Moves);
705 FinalSize = RoundUpToAlign(FinalSize, 4);
Nicolas Geoffray68847972008-04-18 20:59:31 +0000706 return FinalSize;
707}
708
709unsigned
Nicolas Geoffrayf748af22008-04-20 17:44:19 +0000710JITDwarfEmitter::GetFrameMovesSizeInBytes(intptr_t BaseLabelPtr,
711 const std::vector<MachineMove> &Moves) const {
Nicolas Geoffray68847972008-04-18 20:59:31 +0000712 unsigned PointerSize = TD->getPointerSize();
713 int stackGrowth = stackGrowthDirection == TargetFrameInfo::StackGrowsUp ?
714 PointerSize : -PointerSize;
715 bool IsLocal = BaseLabelPtr;
716 unsigned FinalSize = 0;
717
718 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
719 const MachineMove &Move = Moves[i];
720 unsigned LabelID = Move.getLabelID();
721
722 if (LabelID) {
723 LabelID = MMI->MappedLabel(LabelID);
724
725 // Throw out move if the label is invalid.
726 if (!LabelID) continue;
727 }
728
729 intptr_t LabelPtr = 0;
730 if (LabelID) LabelPtr = MCE->getLabelAddress(LabelID);
731
732 const MachineLocation &Dst = Move.getDestination();
733 const MachineLocation &Src = Move.getSource();
734
735 // Advance row if new location.
736 if (BaseLabelPtr && LabelID && (BaseLabelPtr != LabelPtr || !IsLocal)) {
737 FinalSize++;
738 FinalSize += PointerSize;
739 BaseLabelPtr = LabelPtr;
740 IsLocal = true;
741 }
742
743 // If advancing cfa.
Daniel Dunbar39f5de32008-10-03 17:11:57 +0000744 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
745 if (!Src.isReg()) {
746 if (Src.getReg() == MachineLocation::VirtualFP) {
Nicolas Geoffray68847972008-04-18 20:59:31 +0000747 ++FinalSize;
748 } else {
749 ++FinalSize;
Daniel Dunbar39f5de32008-10-03 17:11:57 +0000750 unsigned RegNum = RI->getDwarfRegNum(Src.getReg(), true);
aslc200b112008-08-16 12:57:46 +0000751 FinalSize += TargetAsmInfo::getULEB128Size(RegNum);
Nicolas Geoffray68847972008-04-18 20:59:31 +0000752 }
753
754 int Offset = -Src.getOffset();
755
aslc200b112008-08-16 12:57:46 +0000756 FinalSize += TargetAsmInfo::getULEB128Size(Offset);
Nicolas Geoffray68847972008-04-18 20:59:31 +0000757 } else {
758 assert(0 && "Machine move no supported yet.");
759 }
Daniel Dunbar39f5de32008-10-03 17:11:57 +0000760 } else if (Src.isReg() &&
761 Src.getReg() == MachineLocation::VirtualFP) {
762 if (Dst.isReg()) {
Nicolas Geoffrayf748af22008-04-20 17:44:19 +0000763 ++FinalSize;
Daniel Dunbar39f5de32008-10-03 17:11:57 +0000764 unsigned RegNum = RI->getDwarfRegNum(Dst.getReg(), true);
aslc200b112008-08-16 12:57:46 +0000765 FinalSize += TargetAsmInfo::getULEB128Size(RegNum);
Nicolas Geoffray68847972008-04-18 20:59:31 +0000766 } else {
767 assert(0 && "Machine move no supported yet.");
768 }
769 } else {
Daniel Dunbar39f5de32008-10-03 17:11:57 +0000770 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), true);
Nicolas Geoffray68847972008-04-18 20:59:31 +0000771 int Offset = Dst.getOffset() / stackGrowth;
772
773 if (Offset < 0) {
774 ++FinalSize;
aslc200b112008-08-16 12:57:46 +0000775 FinalSize += TargetAsmInfo::getULEB128Size(Reg);
776 FinalSize += TargetAsmInfo::getSLEB128Size(Offset);
Nicolas Geoffray68847972008-04-18 20:59:31 +0000777 } else if (Reg < 64) {
778 ++FinalSize;
aslc200b112008-08-16 12:57:46 +0000779 FinalSize += TargetAsmInfo::getULEB128Size(Offset);
Nicolas Geoffray68847972008-04-18 20:59:31 +0000780 } else {
781 ++FinalSize;
aslc200b112008-08-16 12:57:46 +0000782 FinalSize += TargetAsmInfo::getULEB128Size(Reg);
783 FinalSize += TargetAsmInfo::getULEB128Size(Offset);
Nicolas Geoffray68847972008-04-18 20:59:31 +0000784 }
785 }
786 }
787 return FinalSize;
788}
789
Nicolas Geoffrayf748af22008-04-20 17:44:19 +0000790unsigned
791JITDwarfEmitter::GetExceptionTableSizeInBytes(MachineFunction* MF) const {
Nicolas Geoffray68847972008-04-18 20:59:31 +0000792 unsigned FinalSize = 0;
793
794 // Map all labels and get rid of any dead landing pads.
795 MMI->TidyLandingPads();
796
797 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
798 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
799 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
800 if (PadInfos.empty()) return 0;
801
802 // Sort the landing pads in order of their type ids. This is used to fold
803 // duplicate actions.
804 SmallVector<const LandingPadInfo *, 64> LandingPads;
805 LandingPads.reserve(PadInfos.size());
806 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
807 LandingPads.push_back(&PadInfos[i]);
808 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
809
810 // Negative type ids index into FilterIds, positive type ids index into
811 // TypeInfos. The value written for a positive type id is just the type
812 // id itself. For a negative type id, however, the value written is the
813 // (negative) byte offset of the corresponding FilterIds entry. The byte
814 // offset is usually equal to the type id, because the FilterIds entries
815 // are written using a variable width encoding which outputs one byte per
816 // entry as long as the value written is not too large, but can differ.
817 // This kind of complication does not occur for positive type ids because
818 // type infos are output using a fixed width encoding.
819 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
820 SmallVector<int, 16> FilterOffsets;
821 FilterOffsets.reserve(FilterIds.size());
822 int Offset = -1;
823 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
824 E = FilterIds.end(); I != E; ++I) {
825 FilterOffsets.push_back(Offset);
aslc200b112008-08-16 12:57:46 +0000826 Offset -= TargetAsmInfo::getULEB128Size(*I);
Nicolas Geoffray68847972008-04-18 20:59:31 +0000827 }
828
829 // Compute the actions table and gather the first action index for each
830 // landing pad site.
831 SmallVector<ActionEntry, 32> Actions;
832 SmallVector<unsigned, 64> FirstActions;
833 FirstActions.reserve(LandingPads.size());
834
835 int FirstAction = 0;
836 unsigned SizeActions = 0;
837 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
838 const LandingPadInfo *LP = LandingPads[i];
839 const std::vector<int> &TypeIds = LP->TypeIds;
840 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
841 unsigned SizeSiteActions = 0;
842
843 if (NumShared < TypeIds.size()) {
844 unsigned SizeAction = 0;
845 ActionEntry *PrevAction = 0;
846
847 if (NumShared) {
848 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
849 assert(Actions.size());
850 PrevAction = &Actions.back();
aslc200b112008-08-16 12:57:46 +0000851 SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) +
852 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Nicolas Geoffray68847972008-04-18 20:59:31 +0000853 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
aslc200b112008-08-16 12:57:46 +0000854 SizeAction -= TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Nicolas Geoffray68847972008-04-18 20:59:31 +0000855 SizeAction += -PrevAction->NextAction;
856 PrevAction = PrevAction->Previous;
857 }
858 }
859
860 // Compute the actions.
861 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
862 int TypeID = TypeIds[I];
863 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
864 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
aslc200b112008-08-16 12:57:46 +0000865 unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID);
Nicolas Geoffray68847972008-04-18 20:59:31 +0000866
867 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
aslc200b112008-08-16 12:57:46 +0000868 SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction);
Nicolas Geoffray68847972008-04-18 20:59:31 +0000869 SizeSiteActions += SizeAction;
870
871 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
872 Actions.push_back(Action);
873
874 PrevAction = &Actions.back();
875 }
876
877 // Record the first action of the landing pad site.
878 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
879 } // else identical - re-use previous FirstAction
880
881 FirstActions.push_back(FirstAction);
882
883 // Compute this sites contribution to size.
884 SizeActions += SizeSiteActions;
885 }
886
887 // Compute the call-site table. Entries must be ordered by address.
888 SmallVector<CallSiteEntry, 64> CallSites;
889
890 RangeMapType PadMap;
891 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
892 const LandingPadInfo *LandingPad = LandingPads[i];
893 for (unsigned j=0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
894 unsigned BeginLabel = LandingPad->BeginLabels[j];
895 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
896 PadRange P = { i, j };
897 PadMap[BeginLabel] = P;
898 }
899 }
900
901 bool MayThrow = false;
902 unsigned LastLabel = 0;
903 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
904 I != E; ++I) {
905 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
906 MI != E; ++MI) {
Dan Gohmanfa607c92008-07-01 00:05:16 +0000907 if (!MI->isLabel()) {
Nicolas Geoffray68847972008-04-18 20:59:31 +0000908 MayThrow |= MI->getDesc().isCall();
909 continue;
910 }
911
912 unsigned BeginLabel = MI->getOperand(0).getImm();
913 assert(BeginLabel && "Invalid label!");
914
915 if (BeginLabel == LastLabel)
916 MayThrow = false;
917
918 RangeMapType::iterator L = PadMap.find(BeginLabel);
919
920 if (L == PadMap.end())
921 continue;
922
923 PadRange P = L->second;
924 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
925
926 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
927 "Inconsistent landing pad map!");
928
929 // If some instruction between the previous try-range and this one may
930 // throw, create a call-site entry with no landing pad for the region
931 // between the try-ranges.
932 if (MayThrow) {
933 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
934 CallSites.push_back(Site);
935 }
936
937 LastLabel = LandingPad->EndLabels[P.RangeIndex];
938 CallSiteEntry Site = {BeginLabel, LastLabel,
939 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
940
941 assert(Site.BeginLabel && Site.EndLabel && Site.PadLabel &&
942 "Invalid landing pad!");
943
944 // Try to merge with the previous call-site.
945 if (CallSites.size()) {
Dan Gohman3d436002008-06-21 22:00:54 +0000946 CallSiteEntry &Prev = CallSites.back();
Nicolas Geoffray68847972008-04-18 20:59:31 +0000947 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
948 // Extend the range of the previous entry.
949 Prev.EndLabel = Site.EndLabel;
950 continue;
951 }
952 }
953
954 // Otherwise, create a new call-site.
955 CallSites.push_back(Site);
956 }
957 }
958 // If some instruction between the previous try-range and the end of the
959 // function may throw, create a call-site entry with no landing pad for the
960 // region following the try-range.
961 if (MayThrow) {
962 CallSiteEntry Site = {LastLabel, 0, 0, 0};
963 CallSites.push_back(Site);
964 }
965
966 // Final tallies.
967 unsigned SizeSites = CallSites.size() * (sizeof(int32_t) + // Site start.
968 sizeof(int32_t) + // Site length.
969 sizeof(int32_t)); // Landing pad.
970 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
aslc200b112008-08-16 12:57:46 +0000971 SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action);
Nicolas Geoffray68847972008-04-18 20:59:31 +0000972
973 unsigned SizeTypes = TypeInfos.size() * TD->getPointerSize();
974
975 unsigned TypeOffset = sizeof(int8_t) + // Call site format
976 // Call-site table length
aslc200b112008-08-16 12:57:46 +0000977 TargetAsmInfo::getULEB128Size(SizeSites) +
Nicolas Geoffray68847972008-04-18 20:59:31 +0000978 SizeSites + SizeActions + SizeTypes;
979
980 unsigned TotalSize = sizeof(int8_t) + // LPStart format
981 sizeof(int8_t) + // TType format
aslc200b112008-08-16 12:57:46 +0000982 TargetAsmInfo::getULEB128Size(TypeOffset) + // TType base offset
Nicolas Geoffray68847972008-04-18 20:59:31 +0000983 TypeOffset;
984
985 unsigned SizeAlign = (4 - TotalSize) & 3;
986
987 // Begin the exception table.
Nicolas Geoffrayf748af22008-04-20 17:44:19 +0000988 FinalSize = RoundUpToAlign(FinalSize, 4);
Nicolas Geoffray68847972008-04-18 20:59:31 +0000989 for (unsigned i = 0; i != SizeAlign; ++i) {
990 ++FinalSize;
991 }
992
993 unsigned PointerSize = TD->getPointerSize();
994
995 // Emit the header.
996 ++FinalSize;
997 // Asm->EOL("LPStart format (DW_EH_PE_omit)");
998 ++FinalSize;
999 // Asm->EOL("TType format (DW_EH_PE_absptr)");
1000 ++FinalSize;
1001 // Asm->EOL("TType base offset");
1002 ++FinalSize;
1003 // Asm->EOL("Call site format (DW_EH_PE_udata4)");
1004 ++FinalSize;
1005 // Asm->EOL("Call-site table length");
1006
1007 // Emit the landing pad site information.
1008 for (unsigned i = 0; i < CallSites.size(); ++i) {
1009 CallSiteEntry &S = CallSites[i];
1010
1011 // Asm->EOL("Region start");
1012 FinalSize += PointerSize;
1013
1014 //Asm->EOL("Region length");
1015 FinalSize += PointerSize;
1016
1017 // Asm->EOL("Landing pad");
1018 FinalSize += PointerSize;
1019
aslc200b112008-08-16 12:57:46 +00001020 FinalSize += TargetAsmInfo::getULEB128Size(S.Action);
Nicolas Geoffray68847972008-04-18 20:59:31 +00001021 // Asm->EOL("Action");
1022 }
1023
1024 // Emit the actions.
1025 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
1026 ActionEntry &Action = Actions[I];
1027
1028 //Asm->EOL("TypeInfo index");
aslc200b112008-08-16 12:57:46 +00001029 FinalSize += TargetAsmInfo::getSLEB128Size(Action.ValueForTypeID);
Nicolas Geoffray68847972008-04-18 20:59:31 +00001030 //Asm->EOL("Next action");
aslc200b112008-08-16 12:57:46 +00001031 FinalSize += TargetAsmInfo::getSLEB128Size(Action.NextAction);
Nicolas Geoffray68847972008-04-18 20:59:31 +00001032 }
1033
1034 // Emit the type ids.
1035 for (unsigned M = TypeInfos.size(); M; --M) {
1036 // Asm->EOL("TypeInfo");
1037 FinalSize += PointerSize;
1038 }
1039
1040 // Emit the filter typeids.
1041 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
1042 unsigned TypeID = FilterIds[j];
aslc200b112008-08-16 12:57:46 +00001043 FinalSize += TargetAsmInfo::getULEB128Size(TypeID);
Nicolas Geoffray68847972008-04-18 20:59:31 +00001044 //Asm->EOL("Filter TypeInfo index");
1045 }
1046
Nicolas Geoffrayf748af22008-04-20 17:44:19 +00001047 FinalSize = RoundUpToAlign(FinalSize, 4);
Nicolas Geoffray68847972008-04-18 20:59:31 +00001048
1049 return FinalSize;
1050}