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/JITCodeEmitter.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/Support/ErrorHandling.h"
25 #include "llvm/MC/MCAsmInfo.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"
33 JITDwarfEmitter::JITDwarfEmitter(JIT& theJit) : MMI(0), Jit(theJit) {}
36 unsigned char* JITDwarfEmitter::EmitDwarfTable(MachineFunction& F,
38 unsigned char* StartFunction,
39 unsigned char* EndFunction,
40 unsigned char* &EHFramePtr) {
41 assert(MMI && "MachineModuleInfo not registered!");
43 const TargetMachine& TM = F.getTarget();
44 TD = TM.getTargetData();
45 stackGrowthDirection = TM.getFrameInfo()->getStackGrowthDirection();
46 RI = TM.getRegisterInfo();
49 unsigned char* ExceptionTable = EmitExceptionTable(&F, StartFunction,
52 unsigned char* Result = 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);
65 JITDwarfEmitter::EmitFrameMoves(intptr_t BaseLabelPtr,
66 const std::vector<MachineMove> &Moves) const {
67 unsigned PointerSize = TD->getPointerSize();
68 int stackGrowth = stackGrowthDirection == TargetFrameInfo::StackGrowsUp ?
69 PointerSize : -PointerSize;
71 unsigned BaseLabelID = 0;
73 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
74 const MachineMove &Move = Moves[i];
75 unsigned LabelID = Move.getLabelID();
78 LabelID = MMI->MappedLabel(LabelID);
80 // Throw out move if the label is invalid.
81 if (!LabelID) continue;
84 intptr_t LabelPtr = 0;
85 if (LabelID) LabelPtr = JCE->getLabelAddress(LabelID);
87 const MachineLocation &Dst = Move.getDestination();
88 const MachineLocation &Src = Move.getSource();
90 // Advance row if new location.
91 if (BaseLabelPtr && LabelID && (BaseLabelID != LabelID || !IsLocal)) {
92 JCE->emitByte(dwarf::DW_CFA_advance_loc4);
93 JCE->emitInt32(LabelPtr - BaseLabelPtr);
95 BaseLabelID = LabelID;
96 BaseLabelPtr = LabelPtr;
101 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
103 if (Src.getReg() == MachineLocation::VirtualFP) {
104 JCE->emitByte(dwarf::DW_CFA_def_cfa_offset);
106 JCE->emitByte(dwarf::DW_CFA_def_cfa);
107 JCE->emitULEB128Bytes(RI->getDwarfRegNum(Src.getReg(), true));
110 JCE->emitULEB128Bytes(-Src.getOffset());
112 llvm_unreachable("Machine move not supported yet.");
114 } else if (Src.isReg() &&
115 Src.getReg() == MachineLocation::VirtualFP) {
117 JCE->emitByte(dwarf::DW_CFA_def_cfa_register);
118 JCE->emitULEB128Bytes(RI->getDwarfRegNum(Dst.getReg(), true));
120 llvm_unreachable("Machine move not supported yet.");
123 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), true);
124 int Offset = Dst.getOffset() / stackGrowth;
127 JCE->emitByte(dwarf::DW_CFA_offset_extended_sf);
128 JCE->emitULEB128Bytes(Reg);
129 JCE->emitSLEB128Bytes(Offset);
130 } else if (Reg < 64) {
131 JCE->emitByte(dwarf::DW_CFA_offset + Reg);
132 JCE->emitULEB128Bytes(Offset);
134 JCE->emitByte(dwarf::DW_CFA_offset_extended);
135 JCE->emitULEB128Bytes(Reg);
136 JCE->emitULEB128Bytes(Offset);
142 /// SharedTypeIds - How many leading type ids two landing pads have in common.
143 static unsigned SharedTypeIds(const LandingPadInfo *L,
144 const LandingPadInfo *R) {
145 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
146 unsigned LSize = LIds.size(), RSize = RIds.size();
147 unsigned MinSize = LSize < RSize ? LSize : RSize;
150 for (; Count != MinSize; ++Count)
151 if (LIds[Count] != RIds[Count])
158 /// PadLT - Order landing pads lexicographically by type id.
159 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
160 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
161 unsigned LSize = LIds.size(), RSize = RIds.size();
162 unsigned MinSize = LSize < RSize ? LSize : RSize;
164 for (unsigned i = 0; i != MinSize; ++i)
165 if (LIds[i] != RIds[i])
166 return LIds[i] < RIds[i];
168 return LSize < RSize;
174 static inline unsigned getEmptyKey() { return -1U; }
175 static inline unsigned getTombstoneKey() { return -2U; }
176 static unsigned getHashValue(const unsigned &Key) { return Key; }
177 static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
180 /// ActionEntry - Structure describing an entry in the actions table.
182 int ValueForTypeID; // The value to write - may not be equal to the type id.
184 struct ActionEntry *Previous;
187 /// PadRange - Structure holding a try-range and the associated landing pad.
189 // The index of the landing pad.
191 // The index of the begin and end labels in the landing pad's label lists.
195 typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
197 /// CallSiteEntry - Structure describing an entry in the call-site table.
198 struct CallSiteEntry {
199 unsigned BeginLabel; // zero indicates the start of the function.
200 unsigned EndLabel; // zero indicates the end of the function.
201 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) const {
210 assert(MMI && "MachineModuleInfo not registered!");
212 // Map all labels and get rid of any dead landing pads.
213 MMI->TidyLandingPads();
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;
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);
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());
241 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
242 E = FilterIds.end(); I != E; ++I) {
243 FilterOffsets.push_back(Offset);
244 Offset -= MCAsmInfo::getULEB128Size(*I);
247 // Compute the actions table and gather the first action index for each
249 SmallVector<ActionEntry, 32> Actions;
250 SmallVector<unsigned, 64> FirstActions;
251 FirstActions.reserve(LandingPads.size());
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;
261 if (NumShared < TypeIds.size()) {
262 unsigned SizeAction = 0;
263 ActionEntry *PrevAction = 0;
266 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
267 assert(Actions.size());
268 PrevAction = &Actions.back();
269 SizeAction = MCAsmInfo::getSLEB128Size(PrevAction->NextAction) +
270 MCAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
271 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
272 SizeAction -= MCAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
273 SizeAction += -PrevAction->NextAction;
274 PrevAction = PrevAction->Previous;
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;
283 unsigned SizeTypeID = MCAsmInfo::getSLEB128Size(ValueForTypeID);
285 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
286 SizeAction = SizeTypeID + MCAsmInfo::getSLEB128Size(NextAction);
287 SizeSiteActions += SizeAction;
289 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
290 Actions.push_back(Action);
292 PrevAction = &Actions.back();
295 // Record the first action of the landing pad site.
296 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
297 } // else identical - re-use previous FirstAction
299 FirstActions.push_back(FirstAction);
301 // Compute this sites contribution to size.
302 SizeActions += SizeSiteActions;
305 // Compute the call-site table. Entries must be ordered by address.
306 SmallVector<CallSiteEntry, 64> CallSites;
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;
319 bool MayThrow = false;
320 unsigned LastLabel = 0;
321 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
323 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
325 if (!MI->isLabel()) {
326 MayThrow |= MI->getDesc().isCall();
330 unsigned BeginLabel = MI->getOperand(0).getImm();
331 assert(BeginLabel && "Invalid label!");
333 if (BeginLabel == LastLabel)
336 RangeMapType::iterator L = PadMap.find(BeginLabel);
338 if (L == PadMap.end())
341 PadRange P = L->second;
342 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
344 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
345 "Inconsistent landing pad map!");
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.
351 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
352 CallSites.push_back(Site);
355 LastLabel = LandingPad->EndLabels[P.RangeIndex];
356 CallSiteEntry Site = {BeginLabel, LastLabel,
357 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
359 assert(Site.BeginLabel && Site.EndLabel && Site.PadLabel &&
360 "Invalid landing pad!");
362 // Try to merge with the previous call-site.
363 if (CallSites.size()) {
364 CallSiteEntry &Prev = CallSites.back();
365 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
366 // Extend the range of the previous entry.
367 Prev.EndLabel = Site.EndLabel;
372 // Otherwise, create a new call-site.
373 CallSites.push_back(Site);
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.
380 CallSiteEntry Site = {LastLabel, 0, 0, 0};
381 CallSites.push_back(Site);
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)
389 SizeSites += MCAsmInfo::getULEB128Size(CallSites[i].Action);
391 unsigned SizeTypes = TypeInfos.size() * TD->getPointerSize();
393 unsigned TypeOffset = sizeof(int8_t) + // Call site format
394 // Call-site table length
395 MCAsmInfo::getULEB128Size(SizeSites) +
396 SizeSites + SizeActions + SizeTypes;
398 // Begin the exception table.
399 JCE->emitAlignmentWithFill(4, 0);
400 // Asm->EOL("Padding");
402 unsigned char* DwarfExceptionTable = (unsigned char*)JCE->getCurrentPCValue();
405 JCE->emitByte(dwarf::DW_EH_PE_omit);
406 // Asm->EOL("LPStart format (DW_EH_PE_omit)");
407 JCE->emitByte(dwarf::DW_EH_PE_absptr);
408 // Asm->EOL("TType format (DW_EH_PE_absptr)");
409 JCE->emitULEB128Bytes(TypeOffset);
410 // Asm->EOL("TType base offset");
411 JCE->emitByte(dwarf::DW_EH_PE_udata4);
412 // Asm->EOL("Call site format (DW_EH_PE_udata4)");
413 JCE->emitULEB128Bytes(SizeSites);
414 // Asm->EOL("Call-site table length");
416 // Emit the landing pad site information.
417 for (unsigned i = 0; i < CallSites.size(); ++i) {
418 CallSiteEntry &S = CallSites[i];
419 intptr_t BeginLabelPtr = 0;
420 intptr_t EndLabelPtr = 0;
423 BeginLabelPtr = (intptr_t)StartFunction;
426 BeginLabelPtr = JCE->getLabelAddress(S.BeginLabel);
427 JCE->emitInt32(BeginLabelPtr - (intptr_t)StartFunction);
430 // Asm->EOL("Region start");
433 EndLabelPtr = (intptr_t)EndFunction;
435 EndLabelPtr = JCE->getLabelAddress(S.EndLabel);
437 JCE->emitInt32(EndLabelPtr - BeginLabelPtr);
438 //Asm->EOL("Region length");
443 unsigned PadLabelPtr = JCE->getLabelAddress(S.PadLabel);
444 JCE->emitInt32(PadLabelPtr - (intptr_t)StartFunction);
446 // Asm->EOL("Landing pad");
448 JCE->emitULEB128Bytes(S.Action);
449 // Asm->EOL("Action");
453 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
454 ActionEntry &Action = Actions[I];
456 JCE->emitSLEB128Bytes(Action.ValueForTypeID);
457 //Asm->EOL("TypeInfo index");
458 JCE->emitSLEB128Bytes(Action.NextAction);
459 //Asm->EOL("Next action");
462 // Emit the type ids.
463 for (unsigned M = TypeInfos.size(); M; --M) {
464 GlobalVariable *GV = TypeInfos[M - 1];
467 if (TD->getPointerSize() == sizeof(int32_t))
468 JCE->emitInt32((intptr_t)Jit.getOrEmitGlobalVariable(GV));
470 JCE->emitInt64((intptr_t)Jit.getOrEmitGlobalVariable(GV));
472 if (TD->getPointerSize() == sizeof(int32_t))
477 // Asm->EOL("TypeInfo");
480 // Emit the filter typeids.
481 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
482 unsigned TypeID = FilterIds[j];
483 JCE->emitULEB128Bytes(TypeID);
484 //Asm->EOL("Filter TypeInfo index");
487 JCE->emitAlignmentWithFill(4, 0);
489 return DwarfExceptionTable;
493 JITDwarfEmitter::EmitCommonEHFrame(const Function* Personality) const {
494 unsigned PointerSize = TD->getPointerSize();
495 int stackGrowth = stackGrowthDirection == TargetFrameInfo::StackGrowsUp ?
496 PointerSize : -PointerSize;
498 unsigned char* StartCommonPtr = (unsigned char*)JCE->getCurrentPCValue();
499 // EH Common Frame header
500 JCE->allocateSpace(4, 0);
501 unsigned char* FrameCommonBeginPtr = (unsigned char*)JCE->getCurrentPCValue();
502 JCE->emitInt32((int)0);
503 JCE->emitByte(dwarf::DW_CIE_VERSION);
504 JCE->emitString(Personality ? "zPLR" : "zR");
505 JCE->emitULEB128Bytes(1);
506 JCE->emitSLEB128Bytes(stackGrowth);
507 JCE->emitByte(RI->getDwarfRegNum(RI->getRARegister(), true));
510 // Augmentation Size: 3 small ULEBs of one byte each, and the personality
511 // function which size is PointerSize.
512 JCE->emitULEB128Bytes(3 + PointerSize);
514 // We set the encoding of the personality as direct encoding because we use
515 // the function pointer. The encoding is not relative because the current
516 // PC value may be bigger than the personality function pointer.
517 if (PointerSize == 4) {
518 JCE->emitByte(dwarf::DW_EH_PE_sdata4);
519 JCE->emitInt32(((intptr_t)Jit.getPointerToGlobal(Personality)));
521 JCE->emitByte(dwarf::DW_EH_PE_sdata8);
522 JCE->emitInt64(((intptr_t)Jit.getPointerToGlobal(Personality)));
525 JCE->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
526 JCE->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
528 JCE->emitULEB128Bytes(1);
529 JCE->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
532 std::vector<MachineMove> Moves;
533 RI->getInitialFrameState(Moves);
534 EmitFrameMoves(0, Moves);
536 JCE->emitAlignmentWithFill(PointerSize, dwarf::DW_CFA_nop);
538 JCE->emitInt32At((uintptr_t*)StartCommonPtr,
539 (uintptr_t)((unsigned char*)JCE->getCurrentPCValue() -
540 FrameCommonBeginPtr));
542 return StartCommonPtr;
547 JITDwarfEmitter::EmitEHFrame(const Function* Personality,
548 unsigned char* StartCommonPtr,
549 unsigned char* StartFunction,
550 unsigned char* EndFunction,
551 unsigned char* ExceptionTable) const {
552 unsigned PointerSize = TD->getPointerSize();
555 unsigned char* StartEHPtr = (unsigned char*)JCE->getCurrentPCValue();
556 JCE->allocateSpace(4, 0);
557 unsigned char* FrameBeginPtr = (unsigned char*)JCE->getCurrentPCValue();
559 JCE->emitInt32(FrameBeginPtr - StartCommonPtr);
560 JCE->emitInt32(StartFunction - (unsigned char*)JCE->getCurrentPCValue());
561 JCE->emitInt32(EndFunction - StartFunction);
563 // If there is a personality and landing pads then point to the language
564 // specific data area in the exception table.
566 JCE->emitULEB128Bytes(PointerSize == 4 ? 4 : 8);
568 if (PointerSize == 4) {
569 if (!MMI->getLandingPads().empty())
570 JCE->emitInt32(ExceptionTable-(unsigned char*)JCE->getCurrentPCValue());
572 JCE->emitInt32((int)0);
574 if (!MMI->getLandingPads().empty())
575 JCE->emitInt64(ExceptionTable-(unsigned char*)JCE->getCurrentPCValue());
577 JCE->emitInt64((int)0);
580 JCE->emitULEB128Bytes(0);
583 // Indicate locations of function specific callee saved registers in
585 EmitFrameMoves((intptr_t)StartFunction, MMI->getFrameMoves());
587 JCE->emitAlignmentWithFill(PointerSize, dwarf::DW_CFA_nop);
589 // Indicate the size of the table
590 JCE->emitInt32At((uintptr_t*)StartEHPtr,
591 (uintptr_t)((unsigned char*)JCE->getCurrentPCValue() -
594 // Double zeroes for the unwind runtime
595 if (PointerSize == 8) {
606 unsigned JITDwarfEmitter::GetDwarfTableSizeInBytes(MachineFunction& F,
608 unsigned char* StartFunction,
609 unsigned char* EndFunction) {
610 const TargetMachine& TM = F.getTarget();
611 TD = TM.getTargetData();
612 stackGrowthDirection = TM.getFrameInfo()->getStackGrowthDirection();
613 RI = TM.getRegisterInfo();
615 unsigned FinalSize = 0;
617 FinalSize += GetExceptionTableSizeInBytes(&F);
619 const std::vector<Function *> Personalities = MMI->getPersonalities();
621 GetCommonEHFrameSizeInBytes(Personalities[MMI->getPersonalityIndex()]);
623 FinalSize += GetEHFrameSizeInBytes(Personalities[MMI->getPersonalityIndex()],
629 /// RoundUpToAlign - Add the specified alignment to FinalSize and returns
631 static unsigned RoundUpToAlign(unsigned FinalSize, unsigned Alignment) {
632 if (Alignment == 0) Alignment = 1;
633 // Since we do not know where the buffer will be allocated, be pessimistic.
634 return FinalSize + Alignment;
638 JITDwarfEmitter::GetEHFrameSizeInBytes(const Function* Personality,
639 unsigned char* StartFunction) const {
640 unsigned PointerSize = TD->getPointerSize();
641 unsigned FinalSize = 0;
643 FinalSize += PointerSize;
645 FinalSize += 3 * PointerSize;
646 // If there is a personality and landing pads then point to the language
647 // specific data area in the exception table.
649 FinalSize += MCAsmInfo::getULEB128Size(4);
650 FinalSize += PointerSize;
652 FinalSize += MCAsmInfo::getULEB128Size(0);
655 // Indicate locations of function specific callee saved registers in
657 FinalSize += GetFrameMovesSizeInBytes((intptr_t)StartFunction,
658 MMI->getFrameMoves());
660 FinalSize = RoundUpToAlign(FinalSize, 4);
662 // Double zeroes for the unwind runtime
663 FinalSize += 2 * PointerSize;
668 unsigned JITDwarfEmitter::GetCommonEHFrameSizeInBytes(const Function* Personality)
671 unsigned PointerSize = TD->getPointerSize();
672 int stackGrowth = stackGrowthDirection == TargetFrameInfo::StackGrowsUp ?
673 PointerSize : -PointerSize;
674 unsigned FinalSize = 0;
675 // EH Common Frame header
676 FinalSize += PointerSize;
679 FinalSize += Personality ? 5 : 3; // "zPLR" or "zR"
680 FinalSize += MCAsmInfo::getULEB128Size(1);
681 FinalSize += MCAsmInfo::getSLEB128Size(stackGrowth);
685 FinalSize += MCAsmInfo::getULEB128Size(7);
690 FinalSize += PointerSize;
692 FinalSize += MCAsmInfo::getULEB128Size(dwarf::DW_EH_PE_pcrel);
693 FinalSize += MCAsmInfo::getULEB128Size(dwarf::DW_EH_PE_pcrel);
696 FinalSize += MCAsmInfo::getULEB128Size(1);
697 FinalSize += MCAsmInfo::getULEB128Size(dwarf::DW_EH_PE_pcrel);
700 std::vector<MachineMove> Moves;
701 RI->getInitialFrameState(Moves);
702 FinalSize += GetFrameMovesSizeInBytes(0, Moves);
703 FinalSize = RoundUpToAlign(FinalSize, 4);
708 JITDwarfEmitter::GetFrameMovesSizeInBytes(intptr_t BaseLabelPtr,
709 const std::vector<MachineMove> &Moves) const {
710 unsigned PointerSize = TD->getPointerSize();
711 int stackGrowth = stackGrowthDirection == TargetFrameInfo::StackGrowsUp ?
712 PointerSize : -PointerSize;
713 bool IsLocal = BaseLabelPtr;
714 unsigned FinalSize = 0;
716 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
717 const MachineMove &Move = Moves[i];
718 unsigned LabelID = Move.getLabelID();
721 LabelID = MMI->MappedLabel(LabelID);
723 // Throw out move if the label is invalid.
724 if (!LabelID) continue;
727 intptr_t LabelPtr = 0;
728 if (LabelID) LabelPtr = JCE->getLabelAddress(LabelID);
730 const MachineLocation &Dst = Move.getDestination();
731 const MachineLocation &Src = Move.getSource();
733 // Advance row if new location.
734 if (BaseLabelPtr && LabelID && (BaseLabelPtr != LabelPtr || !IsLocal)) {
736 FinalSize += PointerSize;
737 BaseLabelPtr = LabelPtr;
742 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
744 if (Src.getReg() == MachineLocation::VirtualFP) {
748 unsigned RegNum = RI->getDwarfRegNum(Src.getReg(), true);
749 FinalSize += MCAsmInfo::getULEB128Size(RegNum);
752 int Offset = -Src.getOffset();
754 FinalSize += MCAsmInfo::getULEB128Size(Offset);
756 llvm_unreachable("Machine move no supported yet.");
758 } else if (Src.isReg() &&
759 Src.getReg() == MachineLocation::VirtualFP) {
762 unsigned RegNum = RI->getDwarfRegNum(Dst.getReg(), true);
763 FinalSize += MCAsmInfo::getULEB128Size(RegNum);
765 llvm_unreachable("Machine move no supported yet.");
768 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), true);
769 int Offset = Dst.getOffset() / stackGrowth;
773 FinalSize += MCAsmInfo::getULEB128Size(Reg);
774 FinalSize += MCAsmInfo::getSLEB128Size(Offset);
775 } else if (Reg < 64) {
777 FinalSize += MCAsmInfo::getULEB128Size(Offset);
780 FinalSize += MCAsmInfo::getULEB128Size(Reg);
781 FinalSize += MCAsmInfo::getULEB128Size(Offset);
789 JITDwarfEmitter::GetExceptionTableSizeInBytes(MachineFunction* MF) const {
790 unsigned FinalSize = 0;
792 // Map all labels and get rid of any dead landing pads.
793 MMI->TidyLandingPads();
795 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
796 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
797 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
798 if (PadInfos.empty()) return 0;
800 // Sort the landing pads in order of their type ids. This is used to fold
801 // duplicate actions.
802 SmallVector<const LandingPadInfo *, 64> LandingPads;
803 LandingPads.reserve(PadInfos.size());
804 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
805 LandingPads.push_back(&PadInfos[i]);
806 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
808 // Negative type ids index into FilterIds, positive type ids index into
809 // TypeInfos. The value written for a positive type id is just the type
810 // id itself. For a negative type id, however, the value written is the
811 // (negative) byte offset of the corresponding FilterIds entry. The byte
812 // offset is usually equal to the type id, because the FilterIds entries
813 // are written using a variable width encoding which outputs one byte per
814 // entry as long as the value written is not too large, but can differ.
815 // This kind of complication does not occur for positive type ids because
816 // type infos are output using a fixed width encoding.
817 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
818 SmallVector<int, 16> FilterOffsets;
819 FilterOffsets.reserve(FilterIds.size());
821 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
822 E = FilterIds.end(); I != E; ++I) {
823 FilterOffsets.push_back(Offset);
824 Offset -= MCAsmInfo::getULEB128Size(*I);
827 // Compute the actions table and gather the first action index for each
829 SmallVector<ActionEntry, 32> Actions;
830 SmallVector<unsigned, 64> FirstActions;
831 FirstActions.reserve(LandingPads.size());
834 unsigned SizeActions = 0;
835 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
836 const LandingPadInfo *LP = LandingPads[i];
837 const std::vector<int> &TypeIds = LP->TypeIds;
838 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
839 unsigned SizeSiteActions = 0;
841 if (NumShared < TypeIds.size()) {
842 unsigned SizeAction = 0;
843 ActionEntry *PrevAction = 0;
846 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
847 assert(Actions.size());
848 PrevAction = &Actions.back();
849 SizeAction = MCAsmInfo::getSLEB128Size(PrevAction->NextAction) +
850 MCAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
851 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
852 SizeAction -= MCAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
853 SizeAction += -PrevAction->NextAction;
854 PrevAction = PrevAction->Previous;
858 // Compute the actions.
859 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
860 int TypeID = TypeIds[I];
861 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
862 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
863 unsigned SizeTypeID = MCAsmInfo::getSLEB128Size(ValueForTypeID);
865 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
866 SizeAction = SizeTypeID + MCAsmInfo::getSLEB128Size(NextAction);
867 SizeSiteActions += SizeAction;
869 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
870 Actions.push_back(Action);
872 PrevAction = &Actions.back();
875 // Record the first action of the landing pad site.
876 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
877 } // else identical - re-use previous FirstAction
879 FirstActions.push_back(FirstAction);
881 // Compute this sites contribution to size.
882 SizeActions += SizeSiteActions;
885 // Compute the call-site table. Entries must be ordered by address.
886 SmallVector<CallSiteEntry, 64> CallSites;
889 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
890 const LandingPadInfo *LandingPad = LandingPads[i];
891 for (unsigned j=0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
892 unsigned BeginLabel = LandingPad->BeginLabels[j];
893 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
894 PadRange P = { i, j };
895 PadMap[BeginLabel] = P;
899 bool MayThrow = false;
900 unsigned LastLabel = 0;
901 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
903 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
905 if (!MI->isLabel()) {
906 MayThrow |= MI->getDesc().isCall();
910 unsigned BeginLabel = MI->getOperand(0).getImm();
911 assert(BeginLabel && "Invalid label!");
913 if (BeginLabel == LastLabel)
916 RangeMapType::iterator L = PadMap.find(BeginLabel);
918 if (L == PadMap.end())
921 PadRange P = L->second;
922 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
924 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
925 "Inconsistent landing pad map!");
927 // If some instruction between the previous try-range and this one may
928 // throw, create a call-site entry with no landing pad for the region
929 // between the try-ranges.
931 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
932 CallSites.push_back(Site);
935 LastLabel = LandingPad->EndLabels[P.RangeIndex];
936 CallSiteEntry Site = {BeginLabel, LastLabel,
937 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
939 assert(Site.BeginLabel && Site.EndLabel && Site.PadLabel &&
940 "Invalid landing pad!");
942 // Try to merge with the previous call-site.
943 if (CallSites.size()) {
944 CallSiteEntry &Prev = CallSites.back();
945 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
946 // Extend the range of the previous entry.
947 Prev.EndLabel = Site.EndLabel;
952 // Otherwise, create a new call-site.
953 CallSites.push_back(Site);
956 // If some instruction between the previous try-range and the end of the
957 // function may throw, create a call-site entry with no landing pad for the
958 // region following the try-range.
960 CallSiteEntry Site = {LastLabel, 0, 0, 0};
961 CallSites.push_back(Site);
965 unsigned SizeSites = CallSites.size() * (sizeof(int32_t) + // Site start.
966 sizeof(int32_t) + // Site length.
967 sizeof(int32_t)); // Landing pad.
968 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
969 SizeSites += MCAsmInfo::getULEB128Size(CallSites[i].Action);
971 unsigned SizeTypes = TypeInfos.size() * TD->getPointerSize();
973 unsigned TypeOffset = sizeof(int8_t) + // Call site format
974 // Call-site table length
975 MCAsmInfo::getULEB128Size(SizeSites) +
976 SizeSites + SizeActions + SizeTypes;
978 unsigned TotalSize = sizeof(int8_t) + // LPStart format
979 sizeof(int8_t) + // TType format
980 MCAsmInfo::getULEB128Size(TypeOffset) + // TType base offset
983 unsigned SizeAlign = (4 - TotalSize) & 3;
985 // Begin the exception table.
986 FinalSize = RoundUpToAlign(FinalSize, 4);
987 for (unsigned i = 0; i != SizeAlign; ++i) {
991 unsigned PointerSize = TD->getPointerSize();
995 // Asm->EOL("LPStart format (DW_EH_PE_omit)");
997 // Asm->EOL("TType format (DW_EH_PE_absptr)");
999 // Asm->EOL("TType base offset");
1001 // Asm->EOL("Call site format (DW_EH_PE_udata4)");
1003 // Asm->EOL("Call-site table length");
1005 // Emit the landing pad site information.
1006 for (unsigned i = 0; i < CallSites.size(); ++i) {
1007 CallSiteEntry &S = CallSites[i];
1009 // Asm->EOL("Region start");
1010 FinalSize += PointerSize;
1012 //Asm->EOL("Region length");
1013 FinalSize += PointerSize;
1015 // Asm->EOL("Landing pad");
1016 FinalSize += PointerSize;
1018 FinalSize += MCAsmInfo::getULEB128Size(S.Action);
1019 // Asm->EOL("Action");
1022 // Emit the actions.
1023 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
1024 ActionEntry &Action = Actions[I];
1026 //Asm->EOL("TypeInfo index");
1027 FinalSize += MCAsmInfo::getSLEB128Size(Action.ValueForTypeID);
1028 //Asm->EOL("Next action");
1029 FinalSize += MCAsmInfo::getSLEB128Size(Action.NextAction);
1032 // Emit the type ids.
1033 for (unsigned M = TypeInfos.size(); M; --M) {
1034 // Asm->EOL("TypeInfo");
1035 FinalSize += PointerSize;
1038 // Emit the filter typeids.
1039 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
1040 unsigned TypeID = FilterIds[j];
1041 FinalSize += MCAsmInfo::getULEB128Size(TypeID);
1042 //Asm->EOL("Filter TypeInfo index");
1045 FinalSize = RoundUpToAlign(FinalSize, 4);