1 //===----- JITDwarfEmitter.cpp - Write dwarf tables into memory -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines a JITDwarfEmitter object that is used by the JIT to
11 // write dwarf tables to memory.
13 //===----------------------------------------------------------------------===//
16 #include "JITDwarfEmitter.h"
17 #include "llvm/Function.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/CodeGen/AsmPrinter.h"
20 #include "llvm/CodeGen/MachineCodeEmitter.h"
21 #include "llvm/CodeGen/MachineFunction.h"
22 #include "llvm/CodeGen/MachineLocation.h"
23 #include "llvm/CodeGen/MachineModuleInfo.h"
24 #include "llvm/ExecutionEngine/JITMemoryManager.h"
25 #include "llvm/Target/TargetAsmInfo.h"
26 #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"
34 JITDwarfEmitter::JITDwarfEmitter(JIT& theJit) : Jit(theJit) {}
37 unsigned char* JITDwarfEmitter::EmitDwarfTable(MachineFunction& F,
38 MachineCodeEmitter& mce,
39 unsigned char* StartFunction,
40 unsigned char* EndFunction) {
41 const TargetMachine& TM = F.getTarget();
42 TD = TM.getTargetData();
43 needsIndirectEncoding = TM.getTargetAsmInfo()->getNeedsIndirectEncoding();
44 stackGrowthDirection = TM.getFrameInfo()->getStackGrowthDirection();
45 RI = TM.getRegisterInfo();
48 unsigned char* ExceptionTable = EmitExceptionTable(&F, StartFunction,
51 unsigned char* Result = 0;
52 unsigned char* EHFramePtr = 0;
54 const std::vector<Function *> Personalities = MMI->getPersonalities();
55 EHFramePtr = EmitCommonEHFrame(Personalities[MMI->getPersonalityIndex()]);
57 Result = EmitEHFrame(Personalities[MMI->getPersonalityIndex()], EHFramePtr,
58 StartFunction, EndFunction, ExceptionTable);
64 void JITDwarfEmitter::EmitFrameMoves(intptr_t BaseLabelPtr,
65 const std::vector<MachineMove> &Moves) {
66 unsigned PointerSize = TD->getPointerSize();
67 int stackGrowth = stackGrowthDirection == TargetFrameInfo::StackGrowsUp ?
68 PointerSize : -PointerSize;
69 bool IsLocal = BaseLabelPtr;
71 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
72 const MachineMove &Move = Moves[i];
73 unsigned LabelID = Move.getLabelID();
76 LabelID = MMI->MappedLabel(LabelID);
78 // Throw out move if the label is invalid.
79 if (!LabelID) continue;
82 intptr_t LabelPtr = 0;
83 if (LabelID) LabelPtr = MCE->getLabelAddress(LabelID);
85 const MachineLocation &Dst = Move.getDestination();
86 const MachineLocation &Src = Move.getSource();
88 // Advance row if new location.
89 if (BaseLabelPtr && LabelID && (BaseLabelPtr != LabelPtr || !IsLocal)) {
90 MCE->emitByte(dwarf::DW_CFA_advance_loc4);
91 if (PointerSize == 8) {
92 MCE->emitInt64(LabelPtr - BaseLabelPtr);
94 MCE->emitInt32(LabelPtr - BaseLabelPtr);
97 BaseLabelPtr = LabelPtr;
102 if (Dst.isRegister() && Dst.getRegister() == MachineLocation::VirtualFP) {
103 if (!Src.isRegister()) {
104 if (Src.getRegister() == MachineLocation::VirtualFP) {
105 MCE->emitByte(dwarf::DW_CFA_def_cfa_offset);
107 MCE->emitByte(dwarf::DW_CFA_def_cfa);
108 MCE->emitULEB128Bytes(RI->getDwarfRegNum(Src.getRegister(), true));
111 int Offset = -Src.getOffset();
113 MCE->emitULEB128Bytes(Offset);
115 assert(0 && "Machine move no supported yet.");
117 } else if (Src.isRegister() &&
118 Src.getRegister() == MachineLocation::VirtualFP) {
119 if (Dst.isRegister()) {
120 MCE->emitByte(dwarf::DW_CFA_def_cfa_register);
121 MCE->emitULEB128Bytes(RI->getDwarfRegNum(Dst.getRegister(), true));
123 assert(0 && "Machine move no supported yet.");
126 unsigned Reg = RI->getDwarfRegNum(Src.getRegister(), true);
127 int Offset = Dst.getOffset() / stackGrowth;
130 MCE->emitByte(dwarf::DW_CFA_offset_extended_sf);
131 MCE->emitULEB128Bytes(Reg);
132 MCE->emitSLEB128Bytes(Offset);
133 } else if (Reg < 64) {
134 MCE->emitByte(dwarf::DW_CFA_offset + Reg);
135 MCE->emitULEB128Bytes(Offset);
137 MCE->emitByte(dwarf::DW_CFA_offset_extended);
138 MCE->emitULEB128Bytes(Reg);
139 MCE->emitULEB128Bytes(Offset);
145 /// SharedTypeIds - How many leading type ids two landing pads have in common.
146 static unsigned SharedTypeIds(const LandingPadInfo *L,
147 const LandingPadInfo *R) {
148 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
149 unsigned LSize = LIds.size(), RSize = RIds.size();
150 unsigned MinSize = LSize < RSize ? LSize : RSize;
153 for (; Count != MinSize; ++Count)
154 if (LIds[Count] != RIds[Count])
161 /// PadLT - Order landing pads lexicographically by type id.
162 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
163 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
164 unsigned LSize = LIds.size(), RSize = RIds.size();
165 unsigned MinSize = LSize < RSize ? LSize : RSize;
167 for (unsigned i = 0; i != MinSize; ++i)
168 if (LIds[i] != RIds[i])
169 return LIds[i] < RIds[i];
171 return LSize < RSize;
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; }
182 /// ActionEntry - Structure describing an entry in the actions table.
184 int ValueForTypeID; // The value to write - may not be equal to the type id.
186 struct ActionEntry *Previous;
189 /// PadRange - Structure holding a try-range and the associated landing pad.
191 // The index of the landing pad.
193 // The index of the begin and end labels in the landing pad's label lists.
197 typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
199 /// CallSiteEntry - Structure describing an entry in the call-site table.
200 struct 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.
207 unsigned char* JITDwarfEmitter::EmitExceptionTable(MachineFunction* MF,
208 unsigned char* StartFunction,
209 unsigned char* EndFunction) {
210 // Map all labels and get rid of any dead landing pads.
211 MMI->TidyLandingPads();
213 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
214 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
215 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
216 if (PadInfos.empty()) return 0;
218 // Sort the landing pads in order of their type ids. This is used to fold
219 // duplicate actions.
220 SmallVector<const LandingPadInfo *, 64> LandingPads;
221 LandingPads.reserve(PadInfos.size());
222 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
223 LandingPads.push_back(&PadInfos[i]);
224 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
226 // Negative type ids index into FilterIds, positive type ids index into
227 // TypeInfos. The value written for a positive type id is just the type
228 // id itself. For a negative type id, however, the value written is the
229 // (negative) byte offset of the corresponding FilterIds entry. The byte
230 // offset is usually equal to the type id, because the FilterIds entries
231 // are written using a variable width encoding which outputs one byte per
232 // entry as long as the value written is not too large, but can differ.
233 // This kind of complication does not occur for positive type ids because
234 // type infos are output using a fixed width encoding.
235 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
236 SmallVector<int, 16> FilterOffsets;
237 FilterOffsets.reserve(FilterIds.size());
239 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
240 E = FilterIds.end(); I != E; ++I) {
241 FilterOffsets.push_back(Offset);
242 Offset -= AsmPrinter::SizeULEB128(*I);
245 // Compute the actions table and gather the first action index for each
247 SmallVector<ActionEntry, 32> Actions;
248 SmallVector<unsigned, 64> FirstActions;
249 FirstActions.reserve(LandingPads.size());
252 unsigned SizeActions = 0;
253 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
254 const LandingPadInfo *LP = LandingPads[i];
255 const std::vector<int> &TypeIds = LP->TypeIds;
256 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
257 unsigned SizeSiteActions = 0;
259 if (NumShared < TypeIds.size()) {
260 unsigned SizeAction = 0;
261 ActionEntry *PrevAction = 0;
264 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
265 assert(Actions.size());
266 PrevAction = &Actions.back();
267 SizeAction = AsmPrinter::SizeSLEB128(PrevAction->NextAction) +
268 AsmPrinter::SizeSLEB128(PrevAction->ValueForTypeID);
269 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
270 SizeAction -= AsmPrinter::SizeSLEB128(PrevAction->ValueForTypeID);
271 SizeAction += -PrevAction->NextAction;
272 PrevAction = PrevAction->Previous;
276 // Compute the actions.
277 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
278 int TypeID = TypeIds[I];
279 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
280 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
281 unsigned SizeTypeID = AsmPrinter::SizeSLEB128(ValueForTypeID);
283 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
284 SizeAction = SizeTypeID + AsmPrinter::SizeSLEB128(NextAction);
285 SizeSiteActions += SizeAction;
287 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
288 Actions.push_back(Action);
290 PrevAction = &Actions.back();
293 // Record the first action of the landing pad site.
294 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
295 } // else identical - re-use previous FirstAction
297 FirstActions.push_back(FirstAction);
299 // Compute this sites contribution to size.
300 SizeActions += SizeSiteActions;
303 // Compute the call-site table. Entries must be ordered by address.
304 SmallVector<CallSiteEntry, 64> CallSites;
307 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
308 const LandingPadInfo *LandingPad = LandingPads[i];
309 for (unsigned j=0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
310 unsigned BeginLabel = LandingPad->BeginLabels[j];
311 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
312 PadRange P = { i, j };
313 PadMap[BeginLabel] = P;
317 bool MayThrow = false;
318 unsigned LastLabel = 0;
319 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
321 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
323 if (MI->getOpcode() != TargetInstrInfo::LABEL) {
324 MayThrow |= MI->getDesc().isCall();
328 unsigned BeginLabel = MI->getOperand(0).getImm();
329 assert(BeginLabel && "Invalid label!");
331 if (BeginLabel == LastLabel)
334 RangeMapType::iterator L = PadMap.find(BeginLabel);
336 if (L == PadMap.end())
339 PadRange P = L->second;
340 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
342 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
343 "Inconsistent landing pad map!");
345 // If some instruction between the previous try-range and this one may
346 // throw, create a call-site entry with no landing pad for the region
347 // between the try-ranges.
349 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
350 CallSites.push_back(Site);
353 LastLabel = LandingPad->EndLabels[P.RangeIndex];
354 CallSiteEntry Site = {BeginLabel, LastLabel,
355 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
357 assert(Site.BeginLabel && Site.EndLabel && Site.PadLabel &&
358 "Invalid landing pad!");
360 // Try to merge with the previous call-site.
361 if (CallSites.size()) {
362 CallSiteEntry &Prev = CallSites[CallSites.size()-1];
363 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
364 // Extend the range of the previous entry.
365 Prev.EndLabel = Site.EndLabel;
370 // Otherwise, create a new call-site.
371 CallSites.push_back(Site);
374 // If some instruction between the previous try-range and the end of the
375 // function may throw, create a call-site entry with no landing pad for the
376 // region following the try-range.
378 CallSiteEntry Site = {LastLabel, 0, 0, 0};
379 CallSites.push_back(Site);
383 unsigned SizeSites = CallSites.size() * (sizeof(int32_t) + // Site start.
384 sizeof(int32_t) + // Site length.
385 sizeof(int32_t)); // Landing pad.
386 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
387 SizeSites += AsmPrinter::SizeULEB128(CallSites[i].Action);
389 unsigned SizeTypes = TypeInfos.size() * TD->getPointerSize();
391 unsigned TypeOffset = sizeof(int8_t) + // Call site format
392 // Call-site table length
393 AsmPrinter::SizeULEB128(SizeSites) +
394 SizeSites + SizeActions + SizeTypes;
396 unsigned TotalSize = sizeof(int8_t) + // LPStart format
397 sizeof(int8_t) + // TType format
398 AsmPrinter::SizeULEB128(TypeOffset) + // TType base offset
401 unsigned SizeAlign = (4 - TotalSize) & 3;
403 // Begin the exception table.
404 MCE->emitAlignment(4);
405 for (unsigned i = 0; i != SizeAlign; ++i) {
407 // Asm->EOL("Padding");
410 unsigned char* DwarfExceptionTable = (unsigned char*)MCE->getCurrentPCValue();
413 MCE->emitByte(dwarf::DW_EH_PE_omit);
414 // Asm->EOL("LPStart format (DW_EH_PE_omit)");
415 MCE->emitByte(dwarf::DW_EH_PE_absptr);
416 // Asm->EOL("TType format (DW_EH_PE_absptr)");
417 MCE->emitULEB128Bytes(TypeOffset);
418 // Asm->EOL("TType base offset");
419 MCE->emitByte(dwarf::DW_EH_PE_udata4);
420 // Asm->EOL("Call site format (DW_EH_PE_udata4)");
421 MCE->emitULEB128Bytes(SizeSites);
422 // Asm->EOL("Call-site table length");
424 // Emit the landing pad site information.
425 for (unsigned i = 0; i < CallSites.size(); ++i) {
426 CallSiteEntry &S = CallSites[i];
427 intptr_t BeginLabelPtr = 0;
428 intptr_t EndLabelPtr = 0;
431 BeginLabelPtr = (intptr_t)StartFunction;
432 if (TD->getPointerSize() == sizeof(int32_t))
437 BeginLabelPtr = MCE->getLabelAddress(S.BeginLabel);
438 if (TD->getPointerSize() == sizeof(int32_t))
439 MCE->emitInt32(BeginLabelPtr - (intptr_t)StartFunction);
441 MCE->emitInt64(BeginLabelPtr - (intptr_t)StartFunction);
444 // Asm->EOL("Region start");
447 EndLabelPtr = (intptr_t)EndFunction;
448 if (TD->getPointerSize() == sizeof(int32_t))
449 MCE->emitInt32((intptr_t)EndFunction - BeginLabelPtr);
451 MCE->emitInt64((intptr_t)EndFunction - BeginLabelPtr);
453 EndLabelPtr = MCE->getLabelAddress(S.EndLabel);
454 if (TD->getPointerSize() == sizeof(int32_t))
455 MCE->emitInt32(EndLabelPtr - BeginLabelPtr);
457 MCE->emitInt64(EndLabelPtr - BeginLabelPtr);
459 //Asm->EOL("Region length");
462 if (TD->getPointerSize() == sizeof(int32_t))
467 unsigned PadLabelPtr = MCE->getLabelAddress(S.PadLabel);
468 if (TD->getPointerSize() == sizeof(int32_t))
469 MCE->emitInt32(PadLabelPtr - (intptr_t)StartFunction);
471 MCE->emitInt64(PadLabelPtr - (intptr_t)StartFunction);
473 // Asm->EOL("Landing pad");
475 MCE->emitULEB128Bytes(S.Action);
476 // Asm->EOL("Action");
480 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
481 ActionEntry &Action = Actions[I];
483 MCE->emitSLEB128Bytes(Action.ValueForTypeID);
484 //Asm->EOL("TypeInfo index");
485 MCE->emitSLEB128Bytes(Action.NextAction);
486 //Asm->EOL("Next action");
489 // Emit the type ids.
490 for (unsigned M = TypeInfos.size(); M; --M) {
491 GlobalVariable *GV = TypeInfos[M - 1];
494 if (TD->getPointerSize() == sizeof(int32_t)) {
495 MCE->emitInt32((intptr_t)Jit.getOrEmitGlobalVariable(GV));
497 MCE->emitInt64((intptr_t)Jit.getOrEmitGlobalVariable(GV));
500 if (TD->getPointerSize() == sizeof(int32_t))
505 // Asm->EOL("TypeInfo");
508 // Emit the filter typeids.
509 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
510 unsigned TypeID = FilterIds[j];
511 MCE->emitULEB128Bytes(TypeID);
512 //Asm->EOL("Filter TypeInfo index");
515 MCE->emitAlignment(4);
517 return DwarfExceptionTable;
520 unsigned char* JITDwarfEmitter::EmitCommonEHFrame(const Function* Personality) {
521 unsigned PointerSize = TD->getPointerSize();
522 int stackGrowth = stackGrowthDirection == TargetFrameInfo::StackGrowsUp ?
523 PointerSize : -PointerSize;
525 unsigned char* StartCommonPtr = (unsigned char*)MCE->getCurrentPCValue();
526 // EH Common Frame header
527 MCE->allocateSpace(PointerSize, 0);
528 unsigned char* FrameCommonBeginPtr = (unsigned char*)MCE->getCurrentPCValue();
529 MCE->emitInt32((int)0);
530 MCE->emitByte(dwarf::DW_CIE_VERSION);
531 MCE->emitString(Personality ? "zPLR" : "zR");
532 MCE->emitULEB128Bytes(1);
533 MCE->emitSLEB128Bytes(stackGrowth);
534 MCE->emitByte(RI->getDwarfRegNum(RI->getRARegister(), true));
537 MCE->emitULEB128Bytes(7);
539 if (needsIndirectEncoding)
540 MCE->emitByte(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4 |
541 dwarf::DW_EH_PE_indirect);
543 MCE->emitByte(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
545 if (PointerSize == 8)
546 MCE->emitInt64((intptr_t)Jit.getPointerToGlobal(Personality) -
547 MCE->getCurrentPCValue());
549 MCE->emitInt32((intptr_t)Jit.getPointerToGlobal(Personality) -
550 MCE->getCurrentPCValue());
552 MCE->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel);
553 MCE->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel);
556 MCE->emitULEB128Bytes(1);
557 MCE->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel);
560 std::vector<MachineMove> Moves;
561 RI->getInitialFrameState(Moves);
562 EmitFrameMoves(0, Moves);
563 MCE->emitAlignment(4);
565 MCE->emitAt((uintptr_t*)StartCommonPtr,
566 (uintptr_t)((unsigned char*)MCE->getCurrentPCValue() -
567 FrameCommonBeginPtr));
569 return StartCommonPtr;
573 unsigned char* JITDwarfEmitter::EmitEHFrame(const Function* Personality,
574 unsigned char* StartCommonPtr,
575 unsigned char* StartFunction,
576 unsigned char* EndFunction,
577 unsigned char* ExceptionTable) {
578 unsigned PointerSize = TD->getPointerSize();
581 unsigned char* StartEHPtr = (unsigned char*)MCE->getCurrentPCValue();
582 MCE->allocateSpace(PointerSize, 0);
583 unsigned char* FrameBeginPtr = (unsigned char*)MCE->getCurrentPCValue();
585 if (PointerSize == 8) {
586 MCE->emitInt64(FrameBeginPtr - StartCommonPtr);
587 MCE->emitInt64(StartFunction - (unsigned char*)MCE->getCurrentPCValue());
588 MCE->emitInt64(EndFunction - StartFunction);
590 MCE->emitInt32(FrameBeginPtr - StartCommonPtr);
591 MCE->emitInt32(StartFunction - (unsigned char*)MCE->getCurrentPCValue());
592 MCE->emitInt32(EndFunction - StartFunction);
595 // If there is a personality and landing pads then point to the language
596 // specific data area in the exception table.
597 if (MMI->getPersonalityIndex()) {
598 MCE->emitULEB128Bytes(4);
600 if (!MMI->getLandingPads().empty()) {
601 if (PointerSize == 8)
602 MCE->emitInt64(ExceptionTable - (unsigned char*)MCE->getCurrentPCValue());
604 MCE->emitInt32(ExceptionTable - (unsigned char*)MCE->getCurrentPCValue());
605 } else if (PointerSize == 8) {
606 MCE->emitInt64((int)0);
608 MCE->emitInt32((int)0);
611 MCE->emitULEB128Bytes(0);
614 // Indicate locations of function specific callee saved registers in
616 EmitFrameMoves((intptr_t)StartFunction, MMI->getFrameMoves());
618 MCE->emitAlignment(4);
620 // Indicate the size of the table
621 MCE->emitAt((uintptr_t*)StartEHPtr,
622 (uintptr_t)((unsigned char*)MCE->getCurrentPCValue() -
625 // Double zeroes for the unwind runtime
626 if (PointerSize == 8) {