1 //===--- lib/CodeGen/DwarfPrinter.cpp - Dwarf Printer ---------------------===//
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 // Emit general DWARF directives.
12 //===----------------------------------------------------------------------===//
14 #include "DwarfPrinter.h"
15 #include "llvm/Module.h"
16 #include "llvm/CodeGen/AsmPrinter.h"
17 #include "llvm/CodeGen/MachineFrameInfo.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/CodeGen/MachineModuleInfo.h"
20 #include "llvm/MC/MCAsmInfo.h"
21 #include "llvm/MC/MCContext.h"
22 #include "llvm/MC/MCExpr.h"
23 #include "llvm/MC/MCStreamer.h"
24 #include "llvm/MC/MCSymbol.h"
25 #include "llvm/Target/TargetData.h"
26 #include "llvm/Target/TargetFrameInfo.h"
27 #include "llvm/Target/TargetLoweringObjectFile.h"
28 #include "llvm/Target/TargetRegisterInfo.h"
29 #include "llvm/Support/Dwarf.h"
30 #include "llvm/Support/ErrorHandling.h"
31 #include "llvm/ADT/SmallString.h"
34 DwarfPrinter::DwarfPrinter(raw_ostream &OS, AsmPrinter *A, const MCAsmInfo *T)
35 : O(OS), Asm(A), MAI(T), TD(Asm->TM.getTargetData()),
36 RI(Asm->TM.getRegisterInfo()), M(NULL), MF(NULL), MMI(NULL),
40 /// getDWLabel - Return the MCSymbol corresponding to the assembler temporary
41 /// label with the specified stem and unique ID.
42 MCSymbol *DwarfPrinter::getDWLabel(const char *Name, unsigned ID) const {
43 // FIXME: REMOVE this. However, there is stuff in EH that passes counters in
44 // here that can be zero.
46 //assert(ID && "Should use getTempLabel if no ID");
47 if (ID == 0) return getTempLabel(Name);
48 return Asm->OutContext.GetOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix())
49 + Twine(Name) + Twine(ID));
52 /// getTempLabel - Return the MCSymbol corresponding to the assembler temporary
53 /// label with the specified name.
54 MCSymbol *DwarfPrinter::getTempLabel(const char *Name) const {
55 return Asm->OutContext.GetOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix())
60 /// SizeOfEncodedValue - Return the size of the encoding in bytes.
61 unsigned DwarfPrinter::SizeOfEncodedValue(unsigned Encoding) const {
62 if (Encoding == dwarf::DW_EH_PE_omit)
65 switch (Encoding & 0x07) {
66 case dwarf::DW_EH_PE_absptr:
67 return TD->getPointerSize();
68 case dwarf::DW_EH_PE_udata2:
70 case dwarf::DW_EH_PE_udata4:
72 case dwarf::DW_EH_PE_udata8:
76 assert(0 && "Invalid encoded value.");
80 void DwarfPrinter::PrintRelDirective(bool Force32Bit) const {
81 if (Force32Bit || TD->getPointerSize() == sizeof(int32_t))
82 O << MAI->getData32bitsDirective();
84 O << MAI->getData64bitsDirective();
87 void DwarfPrinter::PrintRelDirective(unsigned Encoding) const {
88 unsigned Size = SizeOfEncodedValue(Encoding);
89 assert((Size == 4 || Size == 8) && "Do not support other types or rels!");
92 MAI->getData32bitsDirective() : MAI->getData64bitsDirective());
95 /// EOL - Print a newline character to asm stream. If a comment is present
96 /// then it will be printed first. Comments should not contain '\n'.
97 void DwarfPrinter::EOL(const Twine &Comment) const {
98 if (Asm->VerboseAsm && !Comment.isTriviallyEmpty()) {
99 Asm->O.PadToColumn(MAI->getCommentColumn());
100 Asm->O << Asm->MAI->getCommentString() << ' ' << Comment;
105 static const char *DecodeDWARFEncoding(unsigned Encoding) {
107 case dwarf::DW_EH_PE_absptr: return "absptr";
108 case dwarf::DW_EH_PE_omit: return "omit";
109 case dwarf::DW_EH_PE_pcrel: return "pcrel";
110 case dwarf::DW_EH_PE_udata4: return "udata4";
111 case dwarf::DW_EH_PE_udata8: return "udata8";
112 case dwarf::DW_EH_PE_sdata4: return "sdata4";
113 case dwarf::DW_EH_PE_sdata8: return "sdata8";
114 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4: return "pcrel udata4";
115 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4: return "pcrel sdata4";
116 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8: return "pcrel udata8";
117 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8: return "pcrel sdata8";
118 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata4:
119 return "indirect pcrel udata4";
120 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata4:
121 return "indirect pcrel sdata4";
122 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata8:
123 return "indirect pcrel udata8";
124 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata8:
125 return "indirect pcrel sdata8";
128 return "<unknown encoding>";
131 /// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
132 /// encoding. If verbose assembly output is enabled, we output comments
133 /// describing the encoding. Desc is an optional string saying what the
134 /// encoding is specifying (e.g. "LSDA").
135 void DwarfPrinter::EmitEncodingByte(unsigned Val, const char *Desc) {
136 if (Asm->VerboseAsm) {
138 Asm->OutStreamer.AddComment(Twine(Desc)+" Encoding = " +
139 Twine(DecodeDWARFEncoding(Val)));
141 Asm->OutStreamer.AddComment(Twine("Encoding = ") +
142 DecodeDWARFEncoding(Val));
145 Asm->OutStreamer.EmitIntValue(Val, 1, 0/*addrspace*/);
148 /// EmitCFAByte - Emit a .byte 42 directive for a DW_CFA_xxx value.
149 void DwarfPrinter::EmitCFAByte(unsigned Val) {
150 if (Asm->VerboseAsm) {
151 if (Val >= dwarf::DW_CFA_offset && Val < dwarf::DW_CFA_offset+64)
152 Asm->OutStreamer.AddComment("DW_CFA_offset + Reg (" +
153 Twine(Val-dwarf::DW_CFA_offset) + ")");
155 Asm->OutStreamer.AddComment(dwarf::CallFrameString(Val));
157 Asm->OutStreamer.EmitIntValue(Val, 1, 0/*addrspace*/);
160 /// EmitSLEB128 - emit the specified signed leb128 value.
161 void DwarfPrinter::EmitSLEB128(int Value, const char *Desc) const {
162 if (Asm->VerboseAsm && Desc)
163 Asm->OutStreamer.AddComment(Desc);
165 if (MAI->hasLEB128()) {
166 O << "\t.sleb128\t" << Value;
167 Asm->OutStreamer.AddBlankLine();
171 // If we don't have .sleb128, emit as .bytes.
172 int Sign = Value >> (8 * sizeof(Value) - 1);
176 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
178 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
179 if (IsMore) Byte |= 0x80;
180 Asm->OutStreamer.EmitIntValue(Byte, 1, /*addrspace*/0);
184 /// EmitULEB128 - emit the specified signed leb128 value.
185 void DwarfPrinter::EmitULEB128(unsigned Value, const char *Desc,
186 unsigned PadTo) const {
187 if (Asm->VerboseAsm && Desc)
188 Asm->OutStreamer.AddComment(Desc);
190 if (MAI->hasLEB128() && PadTo == 0) {
191 O << "\t.uleb128\t" << Value;
192 Asm->OutStreamer.AddBlankLine();
196 // If we don't have .uleb128 or we want to emit padding, emit as .bytes.
198 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
200 if (Value || PadTo != 0) Byte |= 0x80;
201 Asm->OutStreamer.EmitIntValue(Byte, 1, /*addrspace*/0);
206 Asm->OutStreamer.EmitFill(PadTo - 1, 0x80/*fillval*/, 0/*addrspace*/);
207 Asm->OutStreamer.EmitFill(1, 0/*fillval*/, 0/*addrspace*/);
212 /// EmitReference - Emit a reference to a label.
214 void DwarfPrinter::EmitReference(const MCSymbol *Sym, bool IsPCRelative,
215 bool Force32Bit) const {
216 PrintRelDirective(Force32Bit);
218 if (IsPCRelative) O << "-" << MAI->getPCSymbol();
221 void DwarfPrinter::EmitReference(const MCSymbol *Sym, unsigned Encoding) const {
222 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
224 PrintRelDirective(Encoding);
225 O << *TLOF.getSymbolForDwarfReference(Sym, Asm->MMI, Encoding);
228 void DwarfPrinter::EmitReference(const GlobalValue *GV, unsigned Encoding)const{
229 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
231 PrintRelDirective(Encoding);
232 O << *TLOF.getSymbolForDwarfGlobalReference(GV, Asm->Mang,
236 /// EmitDifference - Emit the difference between two labels. If this assembler
237 /// supports .set, we emit a .set of a temporary and then use it in the .word.
238 void DwarfPrinter::EmitDifference(const MCSymbol *TagHi, const MCSymbol *TagLo,
240 unsigned Size = IsSmall ? 4 : TD->getPointerSize();
241 Asm->EmitLabelDifference(TagHi, TagLo, Size);
244 void DwarfPrinter::EmitSectionOffset(const MCSymbol *Label,
245 const MCSymbol *Section,
246 bool IsSmall, bool isEH) {
249 isAbsolute = MAI->isAbsoluteEHSectionOffsets();
251 isAbsolute = MAI->isAbsoluteDebugSectionOffsets();
254 return EmitDifference(Label, Section, IsSmall);
256 // On COFF targets, we have to emit the weird .secrel32 directive.
257 if (const char *SecOffDir = MAI->getDwarfSectionOffsetDirective())
258 O << SecOffDir << Label->getName();
260 unsigned Size = IsSmall ? 4 : TD->getPointerSize();
261 Asm->OutStreamer.EmitSymbolValue(Label, Size, 0/*AddrSpace*/);
265 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
267 void DwarfPrinter::EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
268 const std::vector<MachineMove> &Moves,
271 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
272 TargetFrameInfo::StackGrowsUp ?
273 TD->getPointerSize() : -TD->getPointerSize();
274 bool IsLocal = BaseLabel && strcmp(BaseLabel, "label") == 0;
276 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
277 const MachineMove &Move = Moves[i];
278 unsigned LabelID = Move.getLabelID();
281 LabelID = MMI->MappedLabel(LabelID);
283 // Throw out move if the label is invalid.
284 if (!LabelID) continue;
287 const MachineLocation &Dst = Move.getDestination();
288 const MachineLocation &Src = Move.getSource();
290 // Advance row if new location.
291 if (BaseLabel && LabelID && (BaseLabelID != LabelID || !IsLocal)) {
292 EmitCFAByte(dwarf::DW_CFA_advance_loc4);
293 EmitDifference(getDWLabel("label", LabelID),
294 getDWLabel(BaseLabel, BaseLabelID), true);
295 BaseLabelID = LabelID;
301 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
303 if (Src.getReg() == MachineLocation::VirtualFP) {
304 EmitCFAByte(dwarf::DW_CFA_def_cfa_offset);
306 EmitCFAByte(dwarf::DW_CFA_def_cfa);
307 EmitULEB128(RI->getDwarfRegNum(Src.getReg(), isEH), "Register");
310 int Offset = -Src.getOffset();
311 EmitULEB128(Offset, "Offset");
313 llvm_unreachable("Machine move not supported yet.");
315 } else if (Src.isReg() &&
316 Src.getReg() == MachineLocation::VirtualFP) {
318 EmitCFAByte(dwarf::DW_CFA_def_cfa_register);
319 EmitULEB128(RI->getDwarfRegNum(Dst.getReg(), isEH), "Register");
321 llvm_unreachable("Machine move not supported yet.");
324 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), isEH);
325 int Offset = Dst.getOffset() / stackGrowth;
328 EmitCFAByte(dwarf::DW_CFA_offset_extended_sf);
329 EmitULEB128(Reg, "Reg");
330 EmitSLEB128(Offset, "Offset");
331 } else if (Reg < 64) {
332 EmitCFAByte(dwarf::DW_CFA_offset + Reg);
333 EmitULEB128(Offset, "Offset");
335 EmitCFAByte(dwarf::DW_CFA_offset_extended);
336 EmitULEB128(Reg, "Reg");
337 EmitULEB128(Offset, "Offset");