"llvm/git@repo.hca.bsc.es:rferrer/llvm-epi-0.8.git" did not exist on "5053ea85fc3b3a30aceaf19c6e47596042cd71c8"
Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//===----- JITDwarfEmitter.cpp - Write dwarf tables into memory -----------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines a JITDwarfEmitter object that is used by the JIT to
// write dwarf tables to memory.
//
//===----------------------------------------------------------------------===//
#include "JIT.h"
#include "JITDwarfEmitter.h"
#include "llvm/Function.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/CodeGen/MachineCodeEmitter.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineLocation.h"
#include "llvm/CodeGen/MachineModuleInfo.h"
#include "llvm/ExecutionEngine/JITMemoryManager.h"
#include "llvm/Target/TargetAsmInfo.h"
#include "llvm/Target/TargetData.h"
#include "llvm/Target/TargetInstrInfo.h"
#include "llvm/Target/TargetFrameInfo.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Target/TargetRegisterInfo.h"
using namespace llvm;
JITDwarfEmitter::JITDwarfEmitter(JIT& theJit) : Jit(theJit) {}
unsigned char* JITDwarfEmitter::EmitDwarfTable(MachineFunction& F,
MachineCodeEmitter& mce,
unsigned char* StartFunction,
unsigned char* EndFunction) {
const TargetMachine& TM = F.getTarget();
TD = TM.getTargetData();
needsIndirectEncoding = TM.getTargetAsmInfo()->getNeedsIndirectEncoding();
stackGrowthDirection = TM.getFrameInfo()->getStackGrowthDirection();
RI = TM.getRegisterInfo();
MCE = &mce;
unsigned char* ExceptionTable = EmitExceptionTable(&F, StartFunction,
EndFunction);
unsigned char* Result = 0;
unsigned char* EHFramePtr = 0;
const std::vector<Function *> Personalities = MMI->getPersonalities();
EHFramePtr = EmitCommonEHFrame(Personalities[MMI->getPersonalityIndex()]);
Result = EmitEHFrame(Personalities[MMI->getPersonalityIndex()], EHFramePtr,
StartFunction, EndFunction, ExceptionTable);
return Result;
}
void
JITDwarfEmitter::EmitFrameMoves(intptr_t BaseLabelPtr,
const std::vector<MachineMove> &Moves) const {
unsigned PointerSize = TD->getPointerSize();
int stackGrowth = stackGrowthDirection == TargetFrameInfo::StackGrowsUp ?
PointerSize : -PointerSize;
Nicolas Geoffray
committed
bool IsLocal = false;
unsigned BaseLabelID = 0;
for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
const MachineMove &Move = Moves[i];
unsigned LabelID = Move.getLabelID();
if (LabelID) {
LabelID = MMI->MappedLabel(LabelID);
// Throw out move if the label is invalid.
if (!LabelID) continue;
}
intptr_t LabelPtr = 0;
if (LabelID) LabelPtr = MCE->getLabelAddress(LabelID);
const MachineLocation &Dst = Move.getDestination();
const MachineLocation &Src = Move.getSource();
// Advance row if new location.
Nicolas Geoffray
committed
if (BaseLabelPtr && LabelID && (BaseLabelID != LabelID || !IsLocal)) {
MCE->emitByte(dwarf::DW_CFA_advance_loc4);
if (PointerSize == 8) {
MCE->emitInt64(LabelPtr - BaseLabelPtr);
} else {
MCE->emitInt32(LabelPtr - BaseLabelPtr);
}
Nicolas Geoffray
committed
BaseLabelID = LabelID;
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
BaseLabelPtr = LabelPtr;
IsLocal = true;
}
// If advancing cfa.
if (Dst.isRegister() && Dst.getRegister() == MachineLocation::VirtualFP) {
if (!Src.isRegister()) {
if (Src.getRegister() == MachineLocation::VirtualFP) {
MCE->emitByte(dwarf::DW_CFA_def_cfa_offset);
} else {
MCE->emitByte(dwarf::DW_CFA_def_cfa);
MCE->emitULEB128Bytes(RI->getDwarfRegNum(Src.getRegister(), true));
}
int Offset = -Src.getOffset();
MCE->emitULEB128Bytes(Offset);
} else {
assert(0 && "Machine move no supported yet.");
}
} else if (Src.isRegister() &&
Src.getRegister() == MachineLocation::VirtualFP) {
if (Dst.isRegister()) {
MCE->emitByte(dwarf::DW_CFA_def_cfa_register);
MCE->emitULEB128Bytes(RI->getDwarfRegNum(Dst.getRegister(), true));
} else {
assert(0 && "Machine move no supported yet.");
}
} else {
unsigned Reg = RI->getDwarfRegNum(Src.getRegister(), true);
int Offset = Dst.getOffset() / stackGrowth;
if (Offset < 0) {
MCE->emitByte(dwarf::DW_CFA_offset_extended_sf);
MCE->emitULEB128Bytes(Reg);
MCE->emitSLEB128Bytes(Offset);
} else if (Reg < 64) {
MCE->emitByte(dwarf::DW_CFA_offset + Reg);
MCE->emitULEB128Bytes(Offset);
} else {
MCE->emitByte(dwarf::DW_CFA_offset_extended);
MCE->emitULEB128Bytes(Reg);
MCE->emitULEB128Bytes(Offset);
}
}
}
}
/// SharedTypeIds - How many leading type ids two landing pads have in common.
static unsigned SharedTypeIds(const LandingPadInfo *L,
const LandingPadInfo *R) {
const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
unsigned LSize = LIds.size(), RSize = RIds.size();
unsigned MinSize = LSize < RSize ? LSize : RSize;
unsigned Count = 0;
for (; Count != MinSize; ++Count)
if (LIds[Count] != RIds[Count])
return Count;
return Count;
}
/// PadLT - Order landing pads lexicographically by type id.
static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
unsigned LSize = LIds.size(), RSize = RIds.size();
unsigned MinSize = LSize < RSize ? LSize : RSize;
for (unsigned i = 0; i != MinSize; ++i)
if (LIds[i] != RIds[i])
return LIds[i] < RIds[i];
return LSize < RSize;
}
namespace {
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
struct KeyInfo {
static inline unsigned getEmptyKey() { return -1U; }
static inline unsigned getTombstoneKey() { return -2U; }
static unsigned getHashValue(const unsigned &Key) { return Key; }
static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
static bool isPod() { return true; }
};
/// ActionEntry - Structure describing an entry in the actions table.
struct ActionEntry {
int ValueForTypeID; // The value to write - may not be equal to the type id.
int NextAction;
struct ActionEntry *Previous;
};
/// PadRange - Structure holding a try-range and the associated landing pad.
struct PadRange {
// The index of the landing pad.
unsigned PadIndex;
// The index of the begin and end labels in the landing pad's label lists.
unsigned RangeIndex;
};
typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
/// CallSiteEntry - Structure describing an entry in the call-site table.
struct CallSiteEntry {
unsigned BeginLabel; // zero indicates the start of the function.
unsigned EndLabel; // zero indicates the end of the function.
unsigned PadLabel; // zero indicates that there is no landing pad.
unsigned Action;
};
unsigned char* JITDwarfEmitter::EmitExceptionTable(MachineFunction* MF,
unsigned char* StartFunction,
unsigned char* EndFunction) const {
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// Map all labels and get rid of any dead landing pads.
MMI->TidyLandingPads();
const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
if (PadInfos.empty()) return 0;
// Sort the landing pads in order of their type ids. This is used to fold
// duplicate actions.
SmallVector<const LandingPadInfo *, 64> LandingPads;
LandingPads.reserve(PadInfos.size());
for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
LandingPads.push_back(&PadInfos[i]);
std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
// Negative type ids index into FilterIds, positive type ids index into
// TypeInfos. The value written for a positive type id is just the type
// id itself. For a negative type id, however, the value written is the
// (negative) byte offset of the corresponding FilterIds entry. The byte
// offset is usually equal to the type id, because the FilterIds entries
// are written using a variable width encoding which outputs one byte per
// entry as long as the value written is not too large, but can differ.
// This kind of complication does not occur for positive type ids because
// type infos are output using a fixed width encoding.
// FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
SmallVector<int, 16> FilterOffsets;
FilterOffsets.reserve(FilterIds.size());
int Offset = -1;
for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
E = FilterIds.end(); I != E; ++I) {
FilterOffsets.push_back(Offset);
Anton Korobeynikov
committed
Offset -= TargetAsmInfo::getULEB128Size(*I);
}
// Compute the actions table and gather the first action index for each
// landing pad site.
SmallVector<ActionEntry, 32> Actions;
SmallVector<unsigned, 64> FirstActions;
FirstActions.reserve(LandingPads.size());
int FirstAction = 0;
unsigned SizeActions = 0;
for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
const LandingPadInfo *LP = LandingPads[i];
const std::vector<int> &TypeIds = LP->TypeIds;
const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
unsigned SizeSiteActions = 0;
if (NumShared < TypeIds.size()) {
unsigned SizeAction = 0;
ActionEntry *PrevAction = 0;
if (NumShared) {
const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
assert(Actions.size());
PrevAction = &Actions.back();
Anton Korobeynikov
committed
SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) +
TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
for (unsigned j = NumShared; j != SizePrevIds; ++j) {
Anton Korobeynikov
committed
SizeAction -= TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
SizeAction += -PrevAction->NextAction;
PrevAction = PrevAction->Previous;
}
}
// Compute the actions.
for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
int TypeID = TypeIds[I];
assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
Anton Korobeynikov
committed
unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID);
int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
Anton Korobeynikov
committed
SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction);
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
SizeSiteActions += SizeAction;
ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
Actions.push_back(Action);
PrevAction = &Actions.back();
}
// Record the first action of the landing pad site.
FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
} // else identical - re-use previous FirstAction
FirstActions.push_back(FirstAction);
// Compute this sites contribution to size.
SizeActions += SizeSiteActions;
}
// Compute the call-site table. Entries must be ordered by address.
SmallVector<CallSiteEntry, 64> CallSites;
RangeMapType PadMap;
for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
const LandingPadInfo *LandingPad = LandingPads[i];
for (unsigned j=0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
unsigned BeginLabel = LandingPad->BeginLabels[j];
assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
PadRange P = { i, j };
PadMap[BeginLabel] = P;
}
}
bool MayThrow = false;
unsigned LastLabel = 0;
for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
I != E; ++I) {
for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
MI != E; ++MI) {
if (!MI->isLabel()) {
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
MayThrow |= MI->getDesc().isCall();
continue;
}
unsigned BeginLabel = MI->getOperand(0).getImm();
assert(BeginLabel && "Invalid label!");
if (BeginLabel == LastLabel)
MayThrow = false;
RangeMapType::iterator L = PadMap.find(BeginLabel);
if (L == PadMap.end())
continue;
PadRange P = L->second;
const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
"Inconsistent landing pad map!");
// If some instruction between the previous try-range and this one may
// throw, create a call-site entry with no landing pad for the region
// between the try-ranges.
if (MayThrow) {
CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
CallSites.push_back(Site);
}
LastLabel = LandingPad->EndLabels[P.RangeIndex];
CallSiteEntry Site = {BeginLabel, LastLabel,
LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
assert(Site.BeginLabel && Site.EndLabel && Site.PadLabel &&
"Invalid landing pad!");
// Try to merge with the previous call-site.
if (CallSites.size()) {
if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
// Extend the range of the previous entry.
Prev.EndLabel = Site.EndLabel;
continue;
}
}
// Otherwise, create a new call-site.
CallSites.push_back(Site);
}
}
// If some instruction between the previous try-range and the end of the
// function may throw, create a call-site entry with no landing pad for the
// region following the try-range.
if (MayThrow) {
CallSiteEntry Site = {LastLabel, 0, 0, 0};
CallSites.push_back(Site);
}
// Final tallies.
unsigned SizeSites = CallSites.size() * (sizeof(int32_t) + // Site start.
sizeof(int32_t) + // Site length.
sizeof(int32_t)); // Landing pad.
for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
Anton Korobeynikov
committed
SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action);
unsigned SizeTypes = TypeInfos.size() * TD->getPointerSize();
unsigned TypeOffset = sizeof(int8_t) + // Call site format
// Call-site table length
Anton Korobeynikov
committed
TargetAsmInfo::getULEB128Size(SizeSites) +
SizeSites + SizeActions + SizeTypes;
unsigned TotalSize = sizeof(int8_t) + // LPStart format
sizeof(int8_t) + // TType format
Anton Korobeynikov
committed
TargetAsmInfo::getULEB128Size(TypeOffset) + // TType base offset
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
TypeOffset;
unsigned SizeAlign = (4 - TotalSize) & 3;
// Begin the exception table.
MCE->emitAlignment(4);
for (unsigned i = 0; i != SizeAlign; ++i) {
MCE->emitByte(0);
// Asm->EOL("Padding");
}
unsigned char* DwarfExceptionTable = (unsigned char*)MCE->getCurrentPCValue();
// Emit the header.
MCE->emitByte(dwarf::DW_EH_PE_omit);
// Asm->EOL("LPStart format (DW_EH_PE_omit)");
MCE->emitByte(dwarf::DW_EH_PE_absptr);
// Asm->EOL("TType format (DW_EH_PE_absptr)");
MCE->emitULEB128Bytes(TypeOffset);
// Asm->EOL("TType base offset");
MCE->emitByte(dwarf::DW_EH_PE_udata4);
// Asm->EOL("Call site format (DW_EH_PE_udata4)");
MCE->emitULEB128Bytes(SizeSites);
// Asm->EOL("Call-site table length");
// Emit the landing pad site information.
for (unsigned i = 0; i < CallSites.size(); ++i) {
CallSiteEntry &S = CallSites[i];
intptr_t BeginLabelPtr = 0;
intptr_t EndLabelPtr = 0;
if (!S.BeginLabel) {
BeginLabelPtr = (intptr_t)StartFunction;
if (TD->getPointerSize() == sizeof(int32_t))
MCE->emitInt32(0);
else
MCE->emitInt64(0);
} else {
BeginLabelPtr = MCE->getLabelAddress(S.BeginLabel);
if (TD->getPointerSize() == sizeof(int32_t))
MCE->emitInt32(BeginLabelPtr - (intptr_t)StartFunction);
else
MCE->emitInt64(BeginLabelPtr - (intptr_t)StartFunction);
}
// Asm->EOL("Region start");
if (!S.EndLabel) {
EndLabelPtr = (intptr_t)EndFunction;
if (TD->getPointerSize() == sizeof(int32_t))
MCE->emitInt32((intptr_t)EndFunction - BeginLabelPtr);
else
MCE->emitInt64((intptr_t)EndFunction - BeginLabelPtr);
} else {
EndLabelPtr = MCE->getLabelAddress(S.EndLabel);
if (TD->getPointerSize() == sizeof(int32_t))
MCE->emitInt32(EndLabelPtr - BeginLabelPtr);
else
MCE->emitInt64(EndLabelPtr - BeginLabelPtr);
}
//Asm->EOL("Region length");
if (!S.PadLabel) {
if (TD->getPointerSize() == sizeof(int32_t))
MCE->emitInt32(0);
else
MCE->emitInt64(0);
} else {
unsigned PadLabelPtr = MCE->getLabelAddress(S.PadLabel);
if (TD->getPointerSize() == sizeof(int32_t))
MCE->emitInt32(PadLabelPtr - (intptr_t)StartFunction);
else
MCE->emitInt64(PadLabelPtr - (intptr_t)StartFunction);
}
// Asm->EOL("Landing pad");
MCE->emitULEB128Bytes(S.Action);
// Asm->EOL("Action");
}
// Emit the actions.
for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
ActionEntry &Action = Actions[I];
MCE->emitSLEB128Bytes(Action.ValueForTypeID);
//Asm->EOL("TypeInfo index");
MCE->emitSLEB128Bytes(Action.NextAction);
//Asm->EOL("Next action");
}
// Emit the type ids.
for (unsigned M = TypeInfos.size(); M; --M) {
GlobalVariable *GV = TypeInfos[M - 1];
if (GV) {
if (TD->getPointerSize() == sizeof(int32_t)) {
MCE->emitInt32((intptr_t)Jit.getOrEmitGlobalVariable(GV));
} else {
MCE->emitInt64((intptr_t)Jit.getOrEmitGlobalVariable(GV));
}
} else {
if (TD->getPointerSize() == sizeof(int32_t))
MCE->emitInt32(0);
else
MCE->emitInt64(0);
}
// Asm->EOL("TypeInfo");
}
// Emit the filter typeids.
for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
unsigned TypeID = FilterIds[j];
MCE->emitULEB128Bytes(TypeID);
//Asm->EOL("Filter TypeInfo index");
}
MCE->emitAlignment(4);
return DwarfExceptionTable;
}
unsigned char*
JITDwarfEmitter::EmitCommonEHFrame(const Function* Personality) const {
unsigned PointerSize = TD->getPointerSize();
int stackGrowth = stackGrowthDirection == TargetFrameInfo::StackGrowsUp ?
PointerSize : -PointerSize;
unsigned char* StartCommonPtr = (unsigned char*)MCE->getCurrentPCValue();
// EH Common Frame header
MCE->allocateSpace(PointerSize, 0);
unsigned char* FrameCommonBeginPtr = (unsigned char*)MCE->getCurrentPCValue();
MCE->emitInt32((int)0);
MCE->emitByte(dwarf::DW_CIE_VERSION);
MCE->emitString(Personality ? "zPLR" : "zR");
MCE->emitULEB128Bytes(1);
MCE->emitSLEB128Bytes(stackGrowth);
MCE->emitByte(RI->getDwarfRegNum(RI->getRARegister(), true));
if (Personality) {
MCE->emitULEB128Bytes(7);
Nicolas Geoffray
committed
// Direct encoding, because we use the function pointer.
MCE->emitByte(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
if (PointerSize == 8)
MCE->emitInt64((intptr_t)Jit.getPointerToGlobal(Personality) -
MCE->getCurrentPCValue());
else
MCE->emitInt32((intptr_t)Jit.getPointerToGlobal(Personality) -
MCE->getCurrentPCValue());
Nicolas Geoffray
committed
MCE->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
MCE->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
} else {
MCE->emitULEB128Bytes(1);
Nicolas Geoffray
committed
MCE->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
}
std::vector<MachineMove> Moves;
RI->getInitialFrameState(Moves);
EmitFrameMoves(0, Moves);
MCE->emitAlignment(4);
MCE->emitAt((uintptr_t*)StartCommonPtr,
(uintptr_t)((unsigned char*)MCE->getCurrentPCValue() -
FrameCommonBeginPtr));
return StartCommonPtr;
}
unsigned char*
JITDwarfEmitter::EmitEHFrame(const Function* Personality,
unsigned char* StartCommonPtr,
unsigned char* StartFunction,
unsigned char* EndFunction,
unsigned char* ExceptionTable) const {
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
unsigned PointerSize = TD->getPointerSize();
// EH frame header.
unsigned char* StartEHPtr = (unsigned char*)MCE->getCurrentPCValue();
MCE->allocateSpace(PointerSize, 0);
unsigned char* FrameBeginPtr = (unsigned char*)MCE->getCurrentPCValue();
// FDE CIE Offset
if (PointerSize == 8) {
MCE->emitInt64(FrameBeginPtr - StartCommonPtr);
MCE->emitInt64(StartFunction - (unsigned char*)MCE->getCurrentPCValue());
MCE->emitInt64(EndFunction - StartFunction);
} else {
MCE->emitInt32(FrameBeginPtr - StartCommonPtr);
MCE->emitInt32(StartFunction - (unsigned char*)MCE->getCurrentPCValue());
MCE->emitInt32(EndFunction - StartFunction);
}
// If there is a personality and landing pads then point to the language
// specific data area in the exception table.
if (MMI->getPersonalityIndex()) {
MCE->emitULEB128Bytes(4);
if (!MMI->getLandingPads().empty()) {
if (PointerSize == 8)
MCE->emitInt64(ExceptionTable - (unsigned char*)MCE->getCurrentPCValue());
else
MCE->emitInt32(ExceptionTable - (unsigned char*)MCE->getCurrentPCValue());
} else if (PointerSize == 8) {
MCE->emitInt64((int)0);
} else {
MCE->emitInt32((int)0);
}
} else {
MCE->emitULEB128Bytes(0);
}
// Indicate locations of function specific callee saved registers in
// frame.
EmitFrameMoves((intptr_t)StartFunction, MMI->getFrameMoves());
MCE->emitAlignment(4);
// Indicate the size of the table
MCE->emitAt((uintptr_t*)StartEHPtr,
(uintptr_t)((unsigned char*)MCE->getCurrentPCValue() -
StartEHPtr));
// Double zeroes for the unwind runtime
if (PointerSize == 8) {
MCE->emitInt64(0);
MCE->emitInt64(0);
} else {
MCE->emitInt32(0);
MCE->emitInt32(0);
}
return StartEHPtr;
}
unsigned JITDwarfEmitter::GetDwarfTableSizeInBytes(MachineFunction& F,
MachineCodeEmitter& mce,
unsigned char* StartFunction,
unsigned char* EndFunction) {
const TargetMachine& TM = F.getTarget();
TD = TM.getTargetData();
needsIndirectEncoding = TM.getTargetAsmInfo()->getNeedsIndirectEncoding();
stackGrowthDirection = TM.getFrameInfo()->getStackGrowthDirection();
RI = TM.getRegisterInfo();
MCE = &mce;
unsigned FinalSize = 0;
FinalSize += GetExceptionTableSizeInBytes(&F);
const std::vector<Function *> Personalities = MMI->getPersonalities();
FinalSize += GetCommonEHFrameSizeInBytes(Personalities[MMI->getPersonalityIndex()]);
FinalSize += GetEHFrameSizeInBytes(Personalities[MMI->getPersonalityIndex()], StartFunction);
return FinalSize;
}
/// RoundUpToAlign - Add the specified alignment to FinalSize and returns
/// the new value.
static unsigned RoundUpToAlign(unsigned FinalSize, unsigned Alignment) {
if (Alignment == 0) Alignment = 1;
// Since we do not know where the buffer will be allocated, be pessimistic.
return FinalSize + Alignment;
unsigned
JITDwarfEmitter::GetEHFrameSizeInBytes(const Function* Personality,
unsigned char* StartFunction) const {
unsigned PointerSize = TD->getPointerSize();
unsigned FinalSize = 0;
// EH frame header.
FinalSize += PointerSize;
// FDE CIE Offset
FinalSize += 3 * PointerSize;
// If there is a personality and landing pads then point to the language
// specific data area in the exception table.
if (MMI->getPersonalityIndex()) {
Anton Korobeynikov
committed
FinalSize += TargetAsmInfo::getULEB128Size(4);
FinalSize += PointerSize;
} else {
Anton Korobeynikov
committed
FinalSize += TargetAsmInfo::getULEB128Size(0);
}
// Indicate locations of function specific callee saved registers in
// frame.
FinalSize += GetFrameMovesSizeInBytes((intptr_t)StartFunction,
MMI->getFrameMoves());
FinalSize = RoundUpToAlign(FinalSize, 4);
// Double zeroes for the unwind runtime
FinalSize += 2 * PointerSize;
return FinalSize;
}
unsigned JITDwarfEmitter::GetCommonEHFrameSizeInBytes(const Function* Personality)
const {
unsigned PointerSize = TD->getPointerSize();
int stackGrowth = stackGrowthDirection == TargetFrameInfo::StackGrowsUp ?
PointerSize : -PointerSize;
unsigned FinalSize = 0;
// EH Common Frame header
FinalSize += PointerSize;
FinalSize += 4;
FinalSize += 1;
FinalSize += Personality ? 5 : 3; // "zPLR" or "zR"
Anton Korobeynikov
committed
FinalSize += TargetAsmInfo::getULEB128Size(1);
FinalSize += TargetAsmInfo::getSLEB128Size(stackGrowth);
FinalSize += 1;
if (Personality) {
Anton Korobeynikov
committed
FinalSize += TargetAsmInfo::getULEB128Size(7);
// Encoding
FinalSize+= 1;
//Personality
FinalSize += PointerSize;
Anton Korobeynikov
committed
FinalSize += TargetAsmInfo::getULEB128Size(dwarf::DW_EH_PE_pcrel);
FinalSize += TargetAsmInfo::getULEB128Size(dwarf::DW_EH_PE_pcrel);
Anton Korobeynikov
committed
FinalSize += TargetAsmInfo::getULEB128Size(1);
FinalSize += TargetAsmInfo::getULEB128Size(dwarf::DW_EH_PE_pcrel);
}
std::vector<MachineMove> Moves;
RI->getInitialFrameState(Moves);
FinalSize += GetFrameMovesSizeInBytes(0, Moves);
FinalSize = RoundUpToAlign(FinalSize, 4);
return FinalSize;
}
unsigned
JITDwarfEmitter::GetFrameMovesSizeInBytes(intptr_t BaseLabelPtr,
const std::vector<MachineMove> &Moves) const {
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
unsigned PointerSize = TD->getPointerSize();
int stackGrowth = stackGrowthDirection == TargetFrameInfo::StackGrowsUp ?
PointerSize : -PointerSize;
bool IsLocal = BaseLabelPtr;
unsigned FinalSize = 0;
for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
const MachineMove &Move = Moves[i];
unsigned LabelID = Move.getLabelID();
if (LabelID) {
LabelID = MMI->MappedLabel(LabelID);
// Throw out move if the label is invalid.
if (!LabelID) continue;
}
intptr_t LabelPtr = 0;
if (LabelID) LabelPtr = MCE->getLabelAddress(LabelID);
const MachineLocation &Dst = Move.getDestination();
const MachineLocation &Src = Move.getSource();
// Advance row if new location.
if (BaseLabelPtr && LabelID && (BaseLabelPtr != LabelPtr || !IsLocal)) {
FinalSize++;
FinalSize += PointerSize;
BaseLabelPtr = LabelPtr;
IsLocal = true;
}
// If advancing cfa.
if (Dst.isRegister() && Dst.getRegister() == MachineLocation::VirtualFP) {
if (!Src.isRegister()) {
if (Src.getRegister() == MachineLocation::VirtualFP) {
++FinalSize;
} else {
++FinalSize;
unsigned RegNum = RI->getDwarfRegNum(Src.getRegister(), true);
Anton Korobeynikov
committed
FinalSize += TargetAsmInfo::getULEB128Size(RegNum);
}
int Offset = -Src.getOffset();
Anton Korobeynikov
committed
FinalSize += TargetAsmInfo::getULEB128Size(Offset);
} else {
assert(0 && "Machine move no supported yet.");
}
} else if (Src.isRegister() &&
Src.getRegister() == MachineLocation::VirtualFP) {
if (Dst.isRegister()) {
++FinalSize;
unsigned RegNum = RI->getDwarfRegNum(Dst.getRegister(), true);
Anton Korobeynikov
committed
FinalSize += TargetAsmInfo::getULEB128Size(RegNum);
} else {
assert(0 && "Machine move no supported yet.");
}
} else {
unsigned Reg = RI->getDwarfRegNum(Src.getRegister(), true);
int Offset = Dst.getOffset() / stackGrowth;
if (Offset < 0) {
++FinalSize;
Anton Korobeynikov
committed
FinalSize += TargetAsmInfo::getULEB128Size(Reg);
FinalSize += TargetAsmInfo::getSLEB128Size(Offset);
} else if (Reg < 64) {
++FinalSize;
Anton Korobeynikov
committed
FinalSize += TargetAsmInfo::getULEB128Size(Offset);
Anton Korobeynikov
committed
FinalSize += TargetAsmInfo::getULEB128Size(Reg);
FinalSize += TargetAsmInfo::getULEB128Size(Offset);
}
}
}
return FinalSize;
}
unsigned
JITDwarfEmitter::GetExceptionTableSizeInBytes(MachineFunction* MF) const {
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
unsigned FinalSize = 0;
// Map all labels and get rid of any dead landing pads.
MMI->TidyLandingPads();
const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
if (PadInfos.empty()) return 0;
// Sort the landing pads in order of their type ids. This is used to fold
// duplicate actions.
SmallVector<const LandingPadInfo *, 64> LandingPads;
LandingPads.reserve(PadInfos.size());
for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
LandingPads.push_back(&PadInfos[i]);
std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
// Negative type ids index into FilterIds, positive type ids index into
// TypeInfos. The value written for a positive type id is just the type
// id itself. For a negative type id, however, the value written is the
// (negative) byte offset of the corresponding FilterIds entry. The byte
// offset is usually equal to the type id, because the FilterIds entries
// are written using a variable width encoding which outputs one byte per
// entry as long as the value written is not too large, but can differ.
// This kind of complication does not occur for positive type ids because
// type infos are output using a fixed width encoding.
// FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
SmallVector<int, 16> FilterOffsets;
FilterOffsets.reserve(FilterIds.size());
int Offset = -1;
for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
E = FilterIds.end(); I != E; ++I) {
FilterOffsets.push_back(Offset);
Anton Korobeynikov
committed
Offset -= TargetAsmInfo::getULEB128Size(*I);
}
// Compute the actions table and gather the first action index for each
// landing pad site.
SmallVector<ActionEntry, 32> Actions;
SmallVector<unsigned, 64> FirstActions;
FirstActions.reserve(LandingPads.size());
int FirstAction = 0;
unsigned SizeActions = 0;
for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
const LandingPadInfo *LP = LandingPads[i];
const std::vector<int> &TypeIds = LP->TypeIds;
const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
unsigned SizeSiteActions = 0;
if (NumShared < TypeIds.size()) {
unsigned SizeAction = 0;
ActionEntry *PrevAction = 0;
if (NumShared) {
const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
assert(Actions.size());
PrevAction = &Actions.back();
Anton Korobeynikov
committed
SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) +
TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
for (unsigned j = NumShared; j != SizePrevIds; ++j) {
Anton Korobeynikov
committed
SizeAction -= TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
SizeAction += -PrevAction->NextAction;
PrevAction = PrevAction->Previous;
}
}
// Compute the actions.
for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
int TypeID = TypeIds[I];
assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
Anton Korobeynikov
committed
unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID);
int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
Anton Korobeynikov
committed
SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction);
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
SizeSiteActions += SizeAction;
ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
Actions.push_back(Action);
PrevAction = &Actions.back();
}
// Record the first action of the landing pad site.
FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
} // else identical - re-use previous FirstAction
FirstActions.push_back(FirstAction);
// Compute this sites contribution to size.
SizeActions += SizeSiteActions;
}
// Compute the call-site table. Entries must be ordered by address.
SmallVector<CallSiteEntry, 64> CallSites;
RangeMapType PadMap;
for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
const LandingPadInfo *LandingPad = LandingPads[i];
for (unsigned j=0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
unsigned BeginLabel = LandingPad->BeginLabels[j];
assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
PadRange P = { i, j };
PadMap[BeginLabel] = P;
}
}
bool MayThrow = false;
unsigned LastLabel = 0;
for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
I != E; ++I) {
for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
MI != E; ++MI) {
if (!MI->isLabel()) {
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
MayThrow |= MI->getDesc().isCall();
continue;
}
unsigned BeginLabel = MI->getOperand(0).getImm();
assert(BeginLabel && "Invalid label!");
if (BeginLabel == LastLabel)
MayThrow = false;
RangeMapType::iterator L = PadMap.find(BeginLabel);
if (L == PadMap.end())
continue;
PadRange P = L->second;
const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
"Inconsistent landing pad map!");
// If some instruction between the previous try-range and this one may
// throw, create a call-site entry with no landing pad for the region
// between the try-ranges.
if (MayThrow) {
CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
CallSites.push_back(Site);
}
LastLabel = LandingPad->EndLabels[P.RangeIndex];
CallSiteEntry Site = {BeginLabel, LastLabel,
LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
assert(Site.BeginLabel && Site.EndLabel && Site.PadLabel &&
"Invalid landing pad!");
// Try to merge with the previous call-site.
if (CallSites.size()) {
if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
// Extend the range of the previous entry.
Prev.EndLabel = Site.EndLabel;
continue;
}
}
// Otherwise, create a new call-site.
CallSites.push_back(Site);
}
}
// If some instruction between the previous try-range and the end of the
// function may throw, create a call-site entry with no landing pad for the
// region following the try-range.
if (MayThrow) {
CallSiteEntry Site = {LastLabel, 0, 0, 0};
CallSites.push_back(Site);
}
// Final tallies.