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/MC/MCSymbol.h"
27 #include "llvm/Target/TargetData.h"
28 #include "llvm/Target/TargetInstrInfo.h"
29 #include "llvm/Target/TargetFrameInfo.h"
30 #include "llvm/Target/TargetMachine.h"
31 #include "llvm/Target/TargetRegisterInfo.h"
34 JITDwarfEmitter::JITDwarfEmitter(JIT
& theJit
) : MMI(0), Jit(theJit
) {}
37 unsigned char* JITDwarfEmitter::EmitDwarfTable(MachineFunction
& F
,
39 unsigned char* StartFunction
,
40 unsigned char* EndFunction
,
41 unsigned char* &EHFramePtr
) {
42 assert(MMI
&& "MachineModuleInfo not registered!");
44 const TargetMachine
& TM
= F
.getTarget();
45 TD
= TM
.getTargetData();
46 stackGrowthDirection
= TM
.getFrameInfo()->getStackGrowthDirection();
47 RI
= TM
.getRegisterInfo();
50 unsigned char* ExceptionTable
= EmitExceptionTable(&F
, StartFunction
,
53 unsigned char* Result
= 0;
55 const std::vector
<const Function
*> Personalities
= MMI
->getPersonalities();
56 EHFramePtr
= EmitCommonEHFrame(Personalities
[MMI
->getPersonalityIndex()]);
58 Result
= EmitEHFrame(Personalities
[MMI
->getPersonalityIndex()], EHFramePtr
,
59 StartFunction
, EndFunction
, ExceptionTable
);
66 JITDwarfEmitter::EmitFrameMoves(intptr_t BaseLabelPtr
,
67 const std::vector
<MachineMove
> &Moves
) const {
68 unsigned PointerSize
= TD
->getPointerSize();
69 int stackGrowth
= stackGrowthDirection
== TargetFrameInfo::StackGrowsUp
?
70 PointerSize
: -PointerSize
;
71 MCSymbol
*BaseLabel
= 0;
73 for (unsigned i
= 0, N
= Moves
.size(); i
< N
; ++i
) {
74 const MachineMove
&Move
= Moves
[i
];
75 MCSymbol
*Label
= Move
.getLabel();
77 // Throw out move if the label is invalid.
78 if (Label
&& (*JCE
->getLabelLocations())[Label
] == 0)
81 intptr_t LabelPtr
= 0;
82 if (Label
) LabelPtr
= JCE
->getLabelAddress(Label
);
84 const MachineLocation
&Dst
= Move
.getDestination();
85 const MachineLocation
&Src
= Move
.getSource();
87 // Advance row if new location.
88 if (BaseLabelPtr
&& Label
&& BaseLabel
!= Label
) {
89 JCE
->emitByte(dwarf::DW_CFA_advance_loc4
);
90 JCE
->emitInt32(LabelPtr
- BaseLabelPtr
);
93 BaseLabelPtr
= LabelPtr
;
97 if (Dst
.isReg() && Dst
.getReg() == MachineLocation::VirtualFP
) {
99 if (Src
.getReg() == MachineLocation::VirtualFP
) {
100 JCE
->emitByte(dwarf::DW_CFA_def_cfa_offset
);
102 JCE
->emitByte(dwarf::DW_CFA_def_cfa
);
103 JCE
->emitULEB128Bytes(RI
->getDwarfRegNum(Src
.getReg(), true));
106 JCE
->emitULEB128Bytes(-Src
.getOffset());
108 llvm_unreachable("Machine move not supported yet.");
110 } else if (Src
.isReg() &&
111 Src
.getReg() == MachineLocation::VirtualFP
) {
113 JCE
->emitByte(dwarf::DW_CFA_def_cfa_register
);
114 JCE
->emitULEB128Bytes(RI
->getDwarfRegNum(Dst
.getReg(), true));
116 llvm_unreachable("Machine move not supported yet.");
119 unsigned Reg
= RI
->getDwarfRegNum(Src
.getReg(), true);
120 int Offset
= Dst
.getOffset() / stackGrowth
;
123 JCE
->emitByte(dwarf::DW_CFA_offset_extended_sf
);
124 JCE
->emitULEB128Bytes(Reg
);
125 JCE
->emitSLEB128Bytes(Offset
);
126 } else if (Reg
< 64) {
127 JCE
->emitByte(dwarf::DW_CFA_offset
+ Reg
);
128 JCE
->emitULEB128Bytes(Offset
);
130 JCE
->emitByte(dwarf::DW_CFA_offset_extended
);
131 JCE
->emitULEB128Bytes(Reg
);
132 JCE
->emitULEB128Bytes(Offset
);
138 /// SharedTypeIds - How many leading type ids two landing pads have in common.
139 static unsigned SharedTypeIds(const LandingPadInfo
*L
,
140 const LandingPadInfo
*R
) {
141 const std::vector
<int> &LIds
= L
->TypeIds
, &RIds
= R
->TypeIds
;
142 unsigned LSize
= LIds
.size(), RSize
= RIds
.size();
143 unsigned MinSize
= LSize
< RSize
? LSize
: RSize
;
146 for (; Count
!= MinSize
; ++Count
)
147 if (LIds
[Count
] != RIds
[Count
])
154 /// PadLT - Order landing pads lexicographically by type id.
155 static bool PadLT(const LandingPadInfo
*L
, const LandingPadInfo
*R
) {
156 const std::vector
<int> &LIds
= L
->TypeIds
, &RIds
= R
->TypeIds
;
157 unsigned LSize
= LIds
.size(), RSize
= RIds
.size();
158 unsigned MinSize
= LSize
< RSize
? LSize
: RSize
;
160 for (unsigned i
= 0; i
!= MinSize
; ++i
)
161 if (LIds
[i
] != RIds
[i
])
162 return LIds
[i
] < RIds
[i
];
164 return LSize
< RSize
;
169 /// ActionEntry - Structure describing an entry in the actions table.
171 int ValueForTypeID
; // The value to write - may not be equal to the type id.
173 struct ActionEntry
*Previous
;
176 /// PadRange - Structure holding a try-range and the associated landing pad.
178 // The index of the landing pad.
180 // The index of the begin and end labels in the landing pad's label lists.
184 typedef DenseMap
<MCSymbol
*, PadRange
> RangeMapType
;
186 /// CallSiteEntry - Structure describing an entry in the call-site table.
187 struct CallSiteEntry
{
188 MCSymbol
*BeginLabel
; // zero indicates the start of the function.
189 MCSymbol
*EndLabel
; // zero indicates the end of the function.
190 MCSymbol
*PadLabel
; // zero indicates that there is no landing pad.
196 unsigned char* JITDwarfEmitter::EmitExceptionTable(MachineFunction
* MF
,
197 unsigned char* StartFunction
,
198 unsigned char* EndFunction
) const {
199 assert(MMI
&& "MachineModuleInfo not registered!");
201 // Map all labels and get rid of any dead landing pads.
202 MMI
->TidyLandingPads(JCE
->getLabelLocations());
204 const std::vector
<const GlobalVariable
*> &TypeInfos
= MMI
->getTypeInfos();
205 const std::vector
<unsigned> &FilterIds
= MMI
->getFilterIds();
206 const std::vector
<LandingPadInfo
> &PadInfos
= MMI
->getLandingPads();
207 if (PadInfos
.empty()) return 0;
209 // Sort the landing pads in order of their type ids. This is used to fold
210 // duplicate actions.
211 SmallVector
<const LandingPadInfo
*, 64> LandingPads
;
212 LandingPads
.reserve(PadInfos
.size());
213 for (unsigned i
= 0, N
= PadInfos
.size(); i
!= N
; ++i
)
214 LandingPads
.push_back(&PadInfos
[i
]);
215 std::sort(LandingPads
.begin(), LandingPads
.end(), PadLT
);
217 // Negative type ids index into FilterIds, positive type ids index into
218 // TypeInfos. The value written for a positive type id is just the type
219 // id itself. For a negative type id, however, the value written is the
220 // (negative) byte offset of the corresponding FilterIds entry. The byte
221 // offset is usually equal to the type id, because the FilterIds entries
222 // are written using a variable width encoding which outputs one byte per
223 // entry as long as the value written is not too large, but can differ.
224 // This kind of complication does not occur for positive type ids because
225 // type infos are output using a fixed width encoding.
226 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
227 SmallVector
<int, 16> FilterOffsets
;
228 FilterOffsets
.reserve(FilterIds
.size());
230 for(std::vector
<unsigned>::const_iterator I
= FilterIds
.begin(),
231 E
= FilterIds
.end(); I
!= E
; ++I
) {
232 FilterOffsets
.push_back(Offset
);
233 Offset
-= MCAsmInfo::getULEB128Size(*I
);
236 // Compute the actions table and gather the first action index for each
238 SmallVector
<ActionEntry
, 32> Actions
;
239 SmallVector
<unsigned, 64> FirstActions
;
240 FirstActions
.reserve(LandingPads
.size());
243 unsigned SizeActions
= 0;
244 for (unsigned i
= 0, N
= LandingPads
.size(); i
!= N
; ++i
) {
245 const LandingPadInfo
*LP
= LandingPads
[i
];
246 const std::vector
<int> &TypeIds
= LP
->TypeIds
;
247 const unsigned NumShared
= i
? SharedTypeIds(LP
, LandingPads
[i
-1]) : 0;
248 unsigned SizeSiteActions
= 0;
250 if (NumShared
< TypeIds
.size()) {
251 unsigned SizeAction
= 0;
252 ActionEntry
*PrevAction
= 0;
255 const unsigned SizePrevIds
= LandingPads
[i
-1]->TypeIds
.size();
256 assert(Actions
.size());
257 PrevAction
= &Actions
.back();
258 SizeAction
= MCAsmInfo::getSLEB128Size(PrevAction
->NextAction
) +
259 MCAsmInfo::getSLEB128Size(PrevAction
->ValueForTypeID
);
260 for (unsigned j
= NumShared
; j
!= SizePrevIds
; ++j
) {
261 SizeAction
-= MCAsmInfo::getSLEB128Size(PrevAction
->ValueForTypeID
);
262 SizeAction
+= -PrevAction
->NextAction
;
263 PrevAction
= PrevAction
->Previous
;
267 // Compute the actions.
268 for (unsigned I
= NumShared
, M
= TypeIds
.size(); I
!= M
; ++I
) {
269 int TypeID
= TypeIds
[I
];
270 assert(-1-TypeID
< (int)FilterOffsets
.size() && "Unknown filter id!");
271 int ValueForTypeID
= TypeID
< 0 ? FilterOffsets
[-1 - TypeID
] : TypeID
;
272 unsigned SizeTypeID
= MCAsmInfo::getSLEB128Size(ValueForTypeID
);
274 int NextAction
= SizeAction
? -(SizeAction
+ SizeTypeID
) : 0;
275 SizeAction
= SizeTypeID
+ MCAsmInfo::getSLEB128Size(NextAction
);
276 SizeSiteActions
+= SizeAction
;
278 ActionEntry Action
= {ValueForTypeID
, NextAction
, PrevAction
};
279 Actions
.push_back(Action
);
281 PrevAction
= &Actions
.back();
284 // Record the first action of the landing pad site.
285 FirstAction
= SizeActions
+ SizeSiteActions
- SizeAction
+ 1;
286 } // else identical - re-use previous FirstAction
288 FirstActions
.push_back(FirstAction
);
290 // Compute this sites contribution to size.
291 SizeActions
+= SizeSiteActions
;
294 // Compute the call-site table. Entries must be ordered by address.
295 SmallVector
<CallSiteEntry
, 64> CallSites
;
298 for (unsigned i
= 0, N
= LandingPads
.size(); i
!= N
; ++i
) {
299 const LandingPadInfo
*LandingPad
= LandingPads
[i
];
300 for (unsigned j
=0, E
= LandingPad
->BeginLabels
.size(); j
!= E
; ++j
) {
301 MCSymbol
*BeginLabel
= LandingPad
->BeginLabels
[j
];
302 assert(!PadMap
.count(BeginLabel
) && "Duplicate landing pad labels!");
303 PadRange P
= { i
, j
};
304 PadMap
[BeginLabel
] = P
;
308 bool MayThrow
= false;
309 MCSymbol
*LastLabel
= 0;
310 for (MachineFunction::const_iterator I
= MF
->begin(), E
= MF
->end();
312 for (MachineBasicBlock::const_iterator MI
= I
->begin(), E
= I
->end();
314 if (!MI
->isLabel()) {
315 MayThrow
|= MI
->getDesc().isCall();
319 MCSymbol
*BeginLabel
= MI
->getOperand(0).getMCSymbol();
320 assert(BeginLabel
&& "Invalid label!");
322 if (BeginLabel
== LastLabel
)
325 RangeMapType::iterator L
= PadMap
.find(BeginLabel
);
327 if (L
== PadMap
.end())
330 PadRange P
= L
->second
;
331 const LandingPadInfo
*LandingPad
= LandingPads
[P
.PadIndex
];
333 assert(BeginLabel
== LandingPad
->BeginLabels
[P
.RangeIndex
] &&
334 "Inconsistent landing pad map!");
336 // If some instruction between the previous try-range and this one may
337 // throw, create a call-site entry with no landing pad for the region
338 // between the try-ranges.
340 CallSiteEntry Site
= {LastLabel
, BeginLabel
, 0, 0};
341 CallSites
.push_back(Site
);
344 LastLabel
= LandingPad
->EndLabels
[P
.RangeIndex
];
345 CallSiteEntry Site
= {BeginLabel
, LastLabel
,
346 LandingPad
->LandingPadLabel
, FirstActions
[P
.PadIndex
]};
348 assert(Site
.BeginLabel
&& Site
.EndLabel
&& Site
.PadLabel
&&
349 "Invalid landing pad!");
351 // Try to merge with the previous call-site.
352 if (CallSites
.size()) {
353 CallSiteEntry
&Prev
= CallSites
.back();
354 if (Site
.PadLabel
== Prev
.PadLabel
&& Site
.Action
== Prev
.Action
) {
355 // Extend the range of the previous entry.
356 Prev
.EndLabel
= Site
.EndLabel
;
361 // Otherwise, create a new call-site.
362 CallSites
.push_back(Site
);
365 // If some instruction between the previous try-range and the end of the
366 // function may throw, create a call-site entry with no landing pad for the
367 // region following the try-range.
369 CallSiteEntry Site
= {LastLabel
, 0, 0, 0};
370 CallSites
.push_back(Site
);
374 unsigned SizeSites
= CallSites
.size() * (sizeof(int32_t) + // Site start.
375 sizeof(int32_t) + // Site length.
376 sizeof(int32_t)); // Landing pad.
377 for (unsigned i
= 0, e
= CallSites
.size(); i
< e
; ++i
)
378 SizeSites
+= MCAsmInfo::getULEB128Size(CallSites
[i
].Action
);
380 unsigned SizeTypes
= TypeInfos
.size() * TD
->getPointerSize();
382 unsigned TypeOffset
= sizeof(int8_t) + // Call site format
383 // Call-site table length
384 MCAsmInfo::getULEB128Size(SizeSites
) +
385 SizeSites
+ SizeActions
+ SizeTypes
;
387 // Begin the exception table.
388 JCE
->emitAlignmentWithFill(4, 0);
389 // Asm->EOL("Padding");
391 unsigned char* DwarfExceptionTable
= (unsigned char*)JCE
->getCurrentPCValue();
394 JCE
->emitByte(dwarf::DW_EH_PE_omit
);
395 // Asm->EOL("LPStart format (DW_EH_PE_omit)");
396 JCE
->emitByte(dwarf::DW_EH_PE_absptr
);
397 // Asm->EOL("TType format (DW_EH_PE_absptr)");
398 JCE
->emitULEB128Bytes(TypeOffset
);
399 // Asm->EOL("TType base offset");
400 JCE
->emitByte(dwarf::DW_EH_PE_udata4
);
401 // Asm->EOL("Call site format (DW_EH_PE_udata4)");
402 JCE
->emitULEB128Bytes(SizeSites
);
403 // Asm->EOL("Call-site table length");
405 // Emit the landing pad site information.
406 for (unsigned i
= 0; i
< CallSites
.size(); ++i
) {
407 CallSiteEntry
&S
= CallSites
[i
];
408 intptr_t BeginLabelPtr
= 0;
409 intptr_t EndLabelPtr
= 0;
412 BeginLabelPtr
= (intptr_t)StartFunction
;
415 BeginLabelPtr
= JCE
->getLabelAddress(S
.BeginLabel
);
416 JCE
->emitInt32(BeginLabelPtr
- (intptr_t)StartFunction
);
419 // Asm->EOL("Region start");
422 EndLabelPtr
= (intptr_t)EndFunction
;
424 EndLabelPtr
= JCE
->getLabelAddress(S
.EndLabel
);
426 JCE
->emitInt32(EndLabelPtr
- BeginLabelPtr
);
427 //Asm->EOL("Region length");
432 unsigned PadLabelPtr
= JCE
->getLabelAddress(S
.PadLabel
);
433 JCE
->emitInt32(PadLabelPtr
- (intptr_t)StartFunction
);
435 // Asm->EOL("Landing pad");
437 JCE
->emitULEB128Bytes(S
.Action
);
438 // Asm->EOL("Action");
442 for (unsigned I
= 0, N
= Actions
.size(); I
!= N
; ++I
) {
443 ActionEntry
&Action
= Actions
[I
];
445 JCE
->emitSLEB128Bytes(Action
.ValueForTypeID
);
446 //Asm->EOL("TypeInfo index");
447 JCE
->emitSLEB128Bytes(Action
.NextAction
);
448 //Asm->EOL("Next action");
451 // Emit the type ids.
452 for (unsigned M
= TypeInfos
.size(); M
; --M
) {
453 const GlobalVariable
*GV
= TypeInfos
[M
- 1];
456 if (TD
->getPointerSize() == sizeof(int32_t))
457 JCE
->emitInt32((intptr_t)Jit
.getOrEmitGlobalVariable(GV
));
459 JCE
->emitInt64((intptr_t)Jit
.getOrEmitGlobalVariable(GV
));
461 if (TD
->getPointerSize() == sizeof(int32_t))
466 // Asm->EOL("TypeInfo");
469 // Emit the filter typeids.
470 for (unsigned j
= 0, M
= FilterIds
.size(); j
< M
; ++j
) {
471 unsigned TypeID
= FilterIds
[j
];
472 JCE
->emitULEB128Bytes(TypeID
);
473 //Asm->EOL("Filter TypeInfo index");
476 JCE
->emitAlignmentWithFill(4, 0);
478 return DwarfExceptionTable
;
482 JITDwarfEmitter::EmitCommonEHFrame(const Function
* Personality
) const {
483 unsigned PointerSize
= TD
->getPointerSize();
484 int stackGrowth
= stackGrowthDirection
== TargetFrameInfo::StackGrowsUp
?
485 PointerSize
: -PointerSize
;
487 unsigned char* StartCommonPtr
= (unsigned char*)JCE
->getCurrentPCValue();
488 // EH Common Frame header
489 JCE
->allocateSpace(4, 0);
490 unsigned char* FrameCommonBeginPtr
= (unsigned char*)JCE
->getCurrentPCValue();
491 JCE
->emitInt32((int)0);
492 JCE
->emitByte(dwarf::DW_CIE_VERSION
);
493 JCE
->emitString(Personality
? "zPLR" : "zR");
494 JCE
->emitULEB128Bytes(1);
495 JCE
->emitSLEB128Bytes(stackGrowth
);
496 JCE
->emitByte(RI
->getDwarfRegNum(RI
->getRARegister(), true));
499 // Augmentation Size: 3 small ULEBs of one byte each, and the personality
500 // function which size is PointerSize.
501 JCE
->emitULEB128Bytes(3 + PointerSize
);
503 // We set the encoding of the personality as direct encoding because we use
504 // the function pointer. The encoding is not relative because the current
505 // PC value may be bigger than the personality function pointer.
506 if (PointerSize
== 4) {
507 JCE
->emitByte(dwarf::DW_EH_PE_sdata4
);
508 JCE
->emitInt32(((intptr_t)Jit
.getPointerToGlobal(Personality
)));
510 JCE
->emitByte(dwarf::DW_EH_PE_sdata8
);
511 JCE
->emitInt64(((intptr_t)Jit
.getPointerToGlobal(Personality
)));
514 // LSDA encoding: This must match the encoding used in EmitEHFrame ()
515 if (PointerSize
== 4)
516 JCE
->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel
| dwarf::DW_EH_PE_sdata4
);
518 JCE
->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel
| dwarf::DW_EH_PE_sdata8
);
519 JCE
->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel
| dwarf::DW_EH_PE_sdata4
);
521 JCE
->emitULEB128Bytes(1);
522 JCE
->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel
| dwarf::DW_EH_PE_sdata4
);
525 std::vector
<MachineMove
> Moves
;
526 RI
->getInitialFrameState(Moves
);
527 EmitFrameMoves(0, Moves
);
529 JCE
->emitAlignmentWithFill(PointerSize
, dwarf::DW_CFA_nop
);
531 JCE
->emitInt32At((uintptr_t*)StartCommonPtr
,
532 (uintptr_t)((unsigned char*)JCE
->getCurrentPCValue() -
533 FrameCommonBeginPtr
));
535 return StartCommonPtr
;
540 JITDwarfEmitter::EmitEHFrame(const Function
* Personality
,
541 unsigned char* StartCommonPtr
,
542 unsigned char* StartFunction
,
543 unsigned char* EndFunction
,
544 unsigned char* ExceptionTable
) const {
545 unsigned PointerSize
= TD
->getPointerSize();
548 unsigned char* StartEHPtr
= (unsigned char*)JCE
->getCurrentPCValue();
549 JCE
->allocateSpace(4, 0);
550 unsigned char* FrameBeginPtr
= (unsigned char*)JCE
->getCurrentPCValue();
552 JCE
->emitInt32(FrameBeginPtr
- StartCommonPtr
);
553 JCE
->emitInt32(StartFunction
- (unsigned char*)JCE
->getCurrentPCValue());
554 JCE
->emitInt32(EndFunction
- StartFunction
);
556 // If there is a personality and landing pads then point to the language
557 // specific data area in the exception table.
559 JCE
->emitULEB128Bytes(PointerSize
== 4 ? 4 : 8);
561 if (PointerSize
== 4) {
562 if (!MMI
->getLandingPads().empty())
563 JCE
->emitInt32(ExceptionTable
-(unsigned char*)JCE
->getCurrentPCValue());
565 JCE
->emitInt32((int)0);
567 if (!MMI
->getLandingPads().empty())
568 JCE
->emitInt64(ExceptionTable
-(unsigned char*)JCE
->getCurrentPCValue());
570 JCE
->emitInt64((int)0);
573 JCE
->emitULEB128Bytes(0);
576 // Indicate locations of function specific callee saved registers in
578 EmitFrameMoves((intptr_t)StartFunction
, MMI
->getFrameMoves());
580 JCE
->emitAlignmentWithFill(PointerSize
, dwarf::DW_CFA_nop
);
582 // Indicate the size of the table
583 JCE
->emitInt32At((uintptr_t*)StartEHPtr
,
584 (uintptr_t)((unsigned char*)JCE
->getCurrentPCValue() -
587 // Double zeroes for the unwind runtime
588 if (PointerSize
== 8) {