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/DerivedTypes.h"
18 #include "llvm/Function.h"
19 #include "llvm/GlobalVariable.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/CodeGen/JITCodeEmitter.h"
22 #include "llvm/CodeGen/MachineFunction.h"
23 #include "llvm/CodeGen/MachineModuleInfo.h"
24 #include "llvm/ExecutionEngine/JITMemoryManager.h"
25 #include "llvm/MC/MachineLocation.h"
26 #include "llvm/MC/MCAsmInfo.h"
27 #include "llvm/MC/MCSymbol.h"
28 #include "llvm/Support/ErrorHandling.h"
29 #include "llvm/DataLayout.h"
30 #include "llvm/Target/TargetInstrInfo.h"
31 #include "llvm/Target/TargetFrameLowering.h"
32 #include "llvm/Target/TargetMachine.h"
33 #include "llvm/Target/TargetRegisterInfo.h"
36 JITDwarfEmitter::JITDwarfEmitter(JIT& theJit) : MMI(0), Jit(theJit) {}
39 unsigned char* JITDwarfEmitter::EmitDwarfTable(MachineFunction& F,
41 unsigned char* StartFunction,
42 unsigned char* EndFunction,
43 unsigned char* &EHFramePtr) {
44 assert(MMI && "MachineModuleInfo not registered!");
46 const TargetMachine& TM = F.getTarget();
47 TD = TM.getDataLayout();
48 stackGrowthDirection = TM.getFrameLowering()->getStackGrowthDirection();
49 RI = TM.getRegisterInfo();
50 MAI = TM.getMCAsmInfo();
53 unsigned char* ExceptionTable = EmitExceptionTable(&F, StartFunction,
56 unsigned char* Result = 0;
58 const std::vector<const Function *> Personalities = MMI->getPersonalities();
59 EHFramePtr = EmitCommonEHFrame(Personalities[MMI->getPersonalityIndex()]);
61 Result = EmitEHFrame(Personalities[MMI->getPersonalityIndex()], EHFramePtr,
62 StartFunction, EndFunction, ExceptionTable);
69 JITDwarfEmitter::EmitFrameMoves(intptr_t BaseLabelPtr,
70 const std::vector<MachineMove> &Moves) const {
71 unsigned PointerSize = TD->getPointerSize(0);
72 int stackGrowth = stackGrowthDirection == TargetFrameLowering::StackGrowsUp ?
73 PointerSize : -PointerSize;
74 MCSymbol *BaseLabel = 0;
76 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
77 const MachineMove &Move = Moves[i];
78 MCSymbol *Label = Move.getLabel();
80 // Throw out move if the label is invalid.
81 if (Label && (*JCE->getLabelLocations())[Label] == 0)
84 intptr_t LabelPtr = 0;
85 if (Label) LabelPtr = JCE->getLabelAddress(Label);
87 const MachineLocation &Dst = Move.getDestination();
88 const MachineLocation &Src = Move.getSource();
90 // Advance row if new location.
91 if (BaseLabelPtr && Label && BaseLabel != Label) {
92 JCE->emitByte(dwarf::DW_CFA_advance_loc4);
93 JCE->emitInt32(LabelPtr - BaseLabelPtr);
96 BaseLabelPtr = LabelPtr;
100 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
102 if (Src.getReg() == MachineLocation::VirtualFP) {
103 JCE->emitByte(dwarf::DW_CFA_def_cfa_offset);
105 JCE->emitByte(dwarf::DW_CFA_def_cfa);
106 JCE->emitULEB128Bytes(RI->getDwarfRegNum(Src.getReg(), true));
109 JCE->emitULEB128Bytes(-Src.getOffset());
111 llvm_unreachable("Machine move not supported yet.");
113 } else if (Src.isReg() &&
114 Src.getReg() == MachineLocation::VirtualFP) {
116 JCE->emitByte(dwarf::DW_CFA_def_cfa_register);
117 JCE->emitULEB128Bytes(RI->getDwarfRegNum(Dst.getReg(), true));
119 llvm_unreachable("Machine move not supported yet.");
122 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), true);
123 int Offset = Dst.getOffset() / stackGrowth;
126 JCE->emitByte(dwarf::DW_CFA_offset_extended_sf);
127 JCE->emitULEB128Bytes(Reg);
128 JCE->emitSLEB128Bytes(Offset);
129 } else if (Reg < 64) {
130 JCE->emitByte(dwarf::DW_CFA_offset + Reg);
131 JCE->emitULEB128Bytes(Offset);
133 JCE->emitByte(dwarf::DW_CFA_offset_extended);
134 JCE->emitULEB128Bytes(Reg);
135 JCE->emitULEB128Bytes(Offset);
141 /// SharedTypeIds - How many leading type ids two landing pads have in common.
142 static unsigned SharedTypeIds(const LandingPadInfo *L,
143 const LandingPadInfo *R) {
144 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
145 unsigned LSize = LIds.size(), RSize = RIds.size();
146 unsigned MinSize = LSize < RSize ? LSize : RSize;
149 for (; Count != MinSize; ++Count)
150 if (LIds[Count] != RIds[Count])
157 /// PadLT - Order landing pads lexicographically by type id.
158 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
159 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
160 unsigned LSize = LIds.size(), RSize = RIds.size();
161 unsigned MinSize = LSize < RSize ? LSize : RSize;
163 for (unsigned i = 0; i != MinSize; ++i)
164 if (LIds[i] != RIds[i])
165 return LIds[i] < RIds[i];
167 return LSize < RSize;
172 /// ActionEntry - Structure describing an entry in the actions table.
174 int ValueForTypeID; // The value to write - may not be equal to the type id.
176 struct ActionEntry *Previous;
179 /// PadRange - Structure holding a try-range and the associated landing pad.
181 // The index of the landing pad.
183 // The index of the begin and end labels in the landing pad's label lists.
187 typedef DenseMap<MCSymbol*, PadRange> RangeMapType;
189 /// CallSiteEntry - Structure describing an entry in the call-site table.
190 struct CallSiteEntry {
191 MCSymbol *BeginLabel; // zero indicates the start of the function.
192 MCSymbol *EndLabel; // zero indicates the end of the function.
193 MCSymbol *PadLabel; // zero indicates that there is no landing pad.
199 unsigned char* JITDwarfEmitter::EmitExceptionTable(MachineFunction* MF,
200 unsigned char* StartFunction,
201 unsigned char* EndFunction) const {
202 assert(MMI && "MachineModuleInfo not registered!");
204 // Map all labels and get rid of any dead landing pads.
205 MMI->TidyLandingPads(JCE->getLabelLocations());
207 const std::vector<const GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
208 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
209 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
210 if (PadInfos.empty()) return 0;
212 // Sort the landing pads in order of their type ids. This is used to fold
213 // duplicate actions.
214 SmallVector<const LandingPadInfo *, 64> LandingPads;
215 LandingPads.reserve(PadInfos.size());
216 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
217 LandingPads.push_back(&PadInfos[i]);
218 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
220 // Negative type ids index into FilterIds, positive type ids index into
221 // TypeInfos. The value written for a positive type id is just the type
222 // id itself. For a negative type id, however, the value written is the
223 // (negative) byte offset of the corresponding FilterIds entry. The byte
224 // offset is usually equal to the type id, because the FilterIds entries
225 // are written using a variable width encoding which outputs one byte per
226 // entry as long as the value written is not too large, but can differ.
227 // This kind of complication does not occur for positive type ids because
228 // type infos are output using a fixed width encoding.
229 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
230 SmallVector<int, 16> FilterOffsets;
231 FilterOffsets.reserve(FilterIds.size());
233 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
234 E = FilterIds.end(); I != E; ++I) {
235 FilterOffsets.push_back(Offset);
236 Offset -= MCAsmInfo::getULEB128Size(*I);
239 // Compute the actions table and gather the first action index for each
241 SmallVector<ActionEntry, 32> Actions;
242 SmallVector<unsigned, 64> FirstActions;
243 FirstActions.reserve(LandingPads.size());
246 unsigned SizeActions = 0;
247 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
248 const LandingPadInfo *LP = LandingPads[i];
249 const std::vector<int> &TypeIds = LP->TypeIds;
250 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
251 unsigned SizeSiteActions = 0;
253 if (NumShared < TypeIds.size()) {
254 unsigned SizeAction = 0;
255 ActionEntry *PrevAction = 0;
258 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
259 assert(Actions.size());
260 PrevAction = &Actions.back();
261 SizeAction = MCAsmInfo::getSLEB128Size(PrevAction->NextAction) +
262 MCAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
263 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
264 SizeAction -= MCAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
265 SizeAction += -PrevAction->NextAction;
266 PrevAction = PrevAction->Previous;
270 // Compute the actions.
271 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
272 int TypeID = TypeIds[I];
273 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
274 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
275 unsigned SizeTypeID = MCAsmInfo::getSLEB128Size(ValueForTypeID);
277 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
278 SizeAction = SizeTypeID + MCAsmInfo::getSLEB128Size(NextAction);
279 SizeSiteActions += SizeAction;
281 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
282 Actions.push_back(Action);
284 PrevAction = &Actions.back();
287 // Record the first action of the landing pad site.
288 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
289 } // else identical - re-use previous FirstAction
291 FirstActions.push_back(FirstAction);
293 // Compute this sites contribution to size.
294 SizeActions += SizeSiteActions;
297 // Compute the call-site table. Entries must be ordered by address.
298 SmallVector<CallSiteEntry, 64> CallSites;
301 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
302 const LandingPadInfo *LandingPad = LandingPads[i];
303 for (unsigned j=0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
304 MCSymbol *BeginLabel = LandingPad->BeginLabels[j];
305 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
306 PadRange P = { i, j };
307 PadMap[BeginLabel] = P;
311 bool MayThrow = false;
312 MCSymbol *LastLabel = 0;
313 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
315 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
317 if (!MI->isLabel()) {
318 MayThrow |= MI->isCall();
322 MCSymbol *BeginLabel = MI->getOperand(0).getMCSymbol();
323 assert(BeginLabel && "Invalid label!");
325 if (BeginLabel == LastLabel)
328 RangeMapType::iterator L = PadMap.find(BeginLabel);
330 if (L == PadMap.end())
333 PadRange P = L->second;
334 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
336 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
337 "Inconsistent landing pad map!");
339 // If some instruction between the previous try-range and this one may
340 // throw, create a call-site entry with no landing pad for the region
341 // between the try-ranges.
343 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
344 CallSites.push_back(Site);
347 LastLabel = LandingPad->EndLabels[P.RangeIndex];
348 CallSiteEntry Site = {BeginLabel, LastLabel,
349 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
351 assert(Site.BeginLabel && Site.EndLabel && Site.PadLabel &&
352 "Invalid landing pad!");
354 // Try to merge with the previous call-site.
355 if (CallSites.size()) {
356 CallSiteEntry &Prev = CallSites.back();
357 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
358 // Extend the range of the previous entry.
359 Prev.EndLabel = Site.EndLabel;
364 // Otherwise, create a new call-site.
365 CallSites.push_back(Site);
368 // If some instruction between the previous try-range and the end of the
369 // function may throw, create a call-site entry with no landing pad for the
370 // region following the try-range.
372 CallSiteEntry Site = {LastLabel, 0, 0, 0};
373 CallSites.push_back(Site);
377 unsigned SizeSites = CallSites.size() * (sizeof(int32_t) + // Site start.
378 sizeof(int32_t) + // Site length.
379 sizeof(int32_t)); // Landing pad.
380 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
381 SizeSites += MCAsmInfo::getULEB128Size(CallSites[i].Action);
383 unsigned SizeTypes = TypeInfos.size() * TD->getPointerSize(0);
385 unsigned TypeOffset = sizeof(int8_t) + // Call site format
386 // Call-site table length
387 MCAsmInfo::getULEB128Size(SizeSites) +
388 SizeSites + SizeActions + SizeTypes;
390 // Begin the exception table.
391 JCE->emitAlignmentWithFill(4, 0);
392 // Asm->EOL("Padding");
394 unsigned char* DwarfExceptionTable = (unsigned char*)JCE->getCurrentPCValue();
397 JCE->emitByte(dwarf::DW_EH_PE_omit);
398 // Asm->EOL("LPStart format (DW_EH_PE_omit)");
399 JCE->emitByte(dwarf::DW_EH_PE_absptr);
400 // Asm->EOL("TType format (DW_EH_PE_absptr)");
401 JCE->emitULEB128Bytes(TypeOffset);
402 // Asm->EOL("TType base offset");
403 JCE->emitByte(dwarf::DW_EH_PE_udata4);
404 // Asm->EOL("Call site format (DW_EH_PE_udata4)");
405 JCE->emitULEB128Bytes(SizeSites);
406 // Asm->EOL("Call-site table length");
408 // Emit the landing pad site information.
409 for (unsigned i = 0; i < CallSites.size(); ++i) {
410 CallSiteEntry &S = CallSites[i];
411 intptr_t BeginLabelPtr = 0;
412 intptr_t EndLabelPtr = 0;
415 BeginLabelPtr = (intptr_t)StartFunction;
418 BeginLabelPtr = JCE->getLabelAddress(S.BeginLabel);
419 JCE->emitInt32(BeginLabelPtr - (intptr_t)StartFunction);
422 // Asm->EOL("Region start");
425 EndLabelPtr = (intptr_t)EndFunction;
427 EndLabelPtr = JCE->getLabelAddress(S.EndLabel);
429 JCE->emitInt32(EndLabelPtr - BeginLabelPtr);
430 //Asm->EOL("Region length");
435 unsigned PadLabelPtr = JCE->getLabelAddress(S.PadLabel);
436 JCE->emitInt32(PadLabelPtr - (intptr_t)StartFunction);
438 // Asm->EOL("Landing pad");
440 JCE->emitULEB128Bytes(S.Action);
441 // Asm->EOL("Action");
445 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
446 ActionEntry &Action = Actions[I];
448 JCE->emitSLEB128Bytes(Action.ValueForTypeID);
449 //Asm->EOL("TypeInfo index");
450 JCE->emitSLEB128Bytes(Action.NextAction);
451 //Asm->EOL("Next action");
454 // Emit the type ids.
455 for (unsigned M = TypeInfos.size(); M; --M) {
456 const GlobalVariable *GV = TypeInfos[M - 1];
459 if (TD->getPointerSize(GV->getType()->getAddressSpace()) == sizeof(int32_t))
460 JCE->emitInt32((intptr_t)Jit.getOrEmitGlobalVariable(GV));
462 JCE->emitInt64((intptr_t)Jit.getOrEmitGlobalVariable(GV));
464 if (TD->getPointerSize(0) == sizeof(int32_t))
469 // Asm->EOL("TypeInfo");
472 // Emit the filter typeids.
473 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
474 unsigned TypeID = FilterIds[j];
475 JCE->emitULEB128Bytes(TypeID);
476 //Asm->EOL("Filter TypeInfo index");
479 JCE->emitAlignmentWithFill(4, 0);
481 return DwarfExceptionTable;
485 JITDwarfEmitter::EmitCommonEHFrame(const Function* Personality) const {
486 unsigned PointerSize = TD->getPointerSize(0);
487 int stackGrowth = stackGrowthDirection == TargetFrameLowering::StackGrowsUp ?
488 PointerSize : -PointerSize;
490 unsigned char* StartCommonPtr = (unsigned char*)JCE->getCurrentPCValue();
491 // EH Common Frame header
492 JCE->allocateSpace(4, 0);
493 unsigned char* FrameCommonBeginPtr = (unsigned char*)JCE->getCurrentPCValue();
494 JCE->emitInt32((int)0);
495 JCE->emitByte(dwarf::DW_CIE_VERSION);
496 JCE->emitString(Personality ? "zPLR" : "zR");
497 JCE->emitULEB128Bytes(1);
498 JCE->emitSLEB128Bytes(stackGrowth);
499 JCE->emitByte(RI->getDwarfRegNum(RI->getRARegister(), true));
502 // Augmentation Size: 3 small ULEBs of one byte each, and the personality
503 // function which size is PointerSize.
504 JCE->emitULEB128Bytes(3 + PointerSize);
506 // We set the encoding of the personality as direct encoding because we use
507 // the function pointer. The encoding is not relative because the current
508 // PC value may be bigger than the personality function pointer.
509 if (PointerSize == 4) {
510 JCE->emitByte(dwarf::DW_EH_PE_sdata4);
511 JCE->emitInt32(((intptr_t)Jit.getPointerToGlobal(Personality)));
513 JCE->emitByte(dwarf::DW_EH_PE_sdata8);
514 JCE->emitInt64(((intptr_t)Jit.getPointerToGlobal(Personality)));
517 // LSDA encoding: This must match the encoding used in EmitEHFrame ()
518 if (PointerSize == 4)
519 JCE->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
521 JCE->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8);
522 JCE->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
524 JCE->emitULEB128Bytes(1);
525 JCE->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
528 EmitFrameMoves(0, MAI->getInitialFrameState());
530 JCE->emitAlignmentWithFill(PointerSize, dwarf::DW_CFA_nop);
532 JCE->emitInt32At((uintptr_t*)StartCommonPtr,
533 (uintptr_t)((unsigned char*)JCE->getCurrentPCValue() -
534 FrameCommonBeginPtr));
536 return StartCommonPtr;
541 JITDwarfEmitter::EmitEHFrame(const Function* Personality,
542 unsigned char* StartCommonPtr,
543 unsigned char* StartFunction,
544 unsigned char* EndFunction,
545 unsigned char* ExceptionTable) const {
546 unsigned PointerSize = TD->getPointerSize(0);
549 unsigned char* StartEHPtr = (unsigned char*)JCE->getCurrentPCValue();
550 JCE->allocateSpace(4, 0);
551 unsigned char* FrameBeginPtr = (unsigned char*)JCE->getCurrentPCValue();
553 JCE->emitInt32(FrameBeginPtr - StartCommonPtr);
554 JCE->emitInt32(StartFunction - (unsigned char*)JCE->getCurrentPCValue());
555 JCE->emitInt32(EndFunction - StartFunction);
557 // If there is a personality and landing pads then point to the language
558 // specific data area in the exception table.
560 JCE->emitULEB128Bytes(PointerSize == 4 ? 4 : 8);
562 if (PointerSize == 4) {
563 if (!MMI->getLandingPads().empty())
564 JCE->emitInt32(ExceptionTable-(unsigned char*)JCE->getCurrentPCValue());
566 JCE->emitInt32((int)0);
568 if (!MMI->getLandingPads().empty())
569 JCE->emitInt64(ExceptionTable-(unsigned char*)JCE->getCurrentPCValue());
571 JCE->emitInt64((int)0);
574 JCE->emitULEB128Bytes(0);
577 // Indicate locations of function specific callee saved registers in
579 EmitFrameMoves((intptr_t)StartFunction, MMI->getFrameMoves());
581 JCE->emitAlignmentWithFill(PointerSize, dwarf::DW_CFA_nop);
583 // Indicate the size of the table
584 JCE->emitInt32At((uintptr_t*)StartEHPtr,
585 (uintptr_t)((unsigned char*)JCE->getCurrentPCValue() -
588 // Double zeroes for the unwind runtime
589 if (PointerSize == 8) {