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,
36 : O(OS), Asm(A), MAI(T), TD(Asm->TM.getTargetData()),
37 RI(Asm->TM.getRegisterInfo()), M(NULL), MF(NULL), MMI(NULL),
38 SubprogramCount(0), Flavor(flavor), SetCounter(1) {}
41 /// getDWLabel - Return the MCSymbol corresponding to the assembler temporary
42 /// label with the specified stem and unique ID.
43 MCSymbol *DwarfPrinter::getDWLabel(const char *Name, unsigned ID) const {
44 // FIXME: REMOVE this. However, there is stuff in EH that passes counters in
45 // here that can be zero.
47 //assert(ID && "Should use getTempLabel if no ID");
48 if (ID == 0) return getTempLabel(Name);
49 return Asm->OutContext.GetOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix())
50 + Twine(Name) + Twine(ID));
53 /// getTempLabel - Return the MCSymbol corresponding to the assembler temporary
54 /// label with the specified name.
55 MCSymbol *DwarfPrinter::getTempLabel(const char *Name) const {
56 return Asm->OutContext.GetOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix())
61 /// SizeOfEncodedValue - Return the size of the encoding in bytes.
62 unsigned DwarfPrinter::SizeOfEncodedValue(unsigned Encoding) const {
63 if (Encoding == dwarf::DW_EH_PE_omit)
66 switch (Encoding & 0x07) {
67 case dwarf::DW_EH_PE_absptr:
68 return TD->getPointerSize();
69 case dwarf::DW_EH_PE_udata2:
71 case dwarf::DW_EH_PE_udata4:
73 case dwarf::DW_EH_PE_udata8:
77 assert(0 && "Invalid encoded value.");
81 void DwarfPrinter::PrintRelDirective(bool Force32Bit, bool isInSection) const {
82 if (isInSection && MAI->getDwarfSectionOffsetDirective())
83 O << MAI->getDwarfSectionOffsetDirective();
84 else if (Force32Bit || TD->getPointerSize() == sizeof(int32_t))
85 O << MAI->getData32bitsDirective();
87 O << MAI->getData64bitsDirective();
90 void DwarfPrinter::PrintRelDirective(unsigned Encoding) const {
91 unsigned Size = SizeOfEncodedValue(Encoding);
92 assert((Size == 4 || Size == 8) && "Do not support other types or rels!");
95 MAI->getData32bitsDirective() : MAI->getData64bitsDirective());
98 /// EOL - Print a newline character to asm stream. If a comment is present
99 /// then it will be printed first. Comments should not contain '\n'.
100 void DwarfPrinter::EOL(const Twine &Comment) const {
101 if (Asm->VerboseAsm && !Comment.isTriviallyEmpty()) {
102 Asm->O.PadToColumn(MAI->getCommentColumn());
103 Asm->O << Asm->MAI->getCommentString() << ' ' << Comment;
108 static const char *DecodeDWARFEncoding(unsigned Encoding) {
110 case dwarf::DW_EH_PE_absptr: return "absptr";
111 case dwarf::DW_EH_PE_omit: return "omit";
112 case dwarf::DW_EH_PE_pcrel: return "pcrel";
113 case dwarf::DW_EH_PE_udata4: return "udata4";
114 case dwarf::DW_EH_PE_udata8: return "udata8";
115 case dwarf::DW_EH_PE_sdata4: return "sdata4";
116 case dwarf::DW_EH_PE_sdata8: return "sdata8";
117 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4: return "pcrel udata4";
118 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4: return "pcrel sdata4";
119 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8: return "pcrel udata8";
120 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8: return "pcrel sdata8";
121 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata4:
122 return "indirect pcrel udata4";
123 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata4:
124 return "indirect pcrel sdata4";
125 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata8:
126 return "indirect pcrel udata8";
127 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata8:
128 return "indirect pcrel sdata8";
131 return "<unknown encoding>";
134 /// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
135 /// encoding. If verbose assembly output is enabled, we output comments
136 /// describing the encoding. Desc is an optional string saying what the
137 /// encoding is specifying (e.g. "LSDA").
138 void DwarfPrinter::EmitEncodingByte(unsigned Val, const char *Desc) {
139 if (Asm->VerboseAsm) {
141 Asm->OutStreamer.AddComment(Twine(Desc)+" Encoding = " +
142 Twine(DecodeDWARFEncoding(Val)));
144 Asm->OutStreamer.AddComment(Twine("Encoding = ") +
145 DecodeDWARFEncoding(Val));
148 Asm->OutStreamer.EmitIntValue(Val, 1, 0/*addrspace*/);
151 /// EmitCFAByte - Emit a .byte 42 directive for a DW_CFA_xxx value.
152 void DwarfPrinter::EmitCFAByte(unsigned Val) {
153 if (Asm->VerboseAsm) {
154 if (Val >= dwarf::DW_CFA_offset && Val < dwarf::DW_CFA_offset+64)
155 Asm->OutStreamer.AddComment("DW_CFA_offset + Reg (" +
156 Twine(Val-dwarf::DW_CFA_offset) + ")");
158 Asm->OutStreamer.AddComment(dwarf::CallFrameString(Val));
160 Asm->OutStreamer.EmitIntValue(Val, 1, 0/*addrspace*/);
163 /// EmitSLEB128 - emit the specified signed leb128 value.
164 void DwarfPrinter::EmitSLEB128(int Value, const char *Desc) const {
165 if (Asm->VerboseAsm && Desc)
166 Asm->OutStreamer.AddComment(Desc);
168 if (MAI->hasLEB128()) {
169 O << "\t.sleb128\t" << Value;
170 Asm->OutStreamer.AddBlankLine();
174 // If we don't have .sleb128, emit as .bytes.
175 int Sign = Value >> (8 * sizeof(Value) - 1);
179 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
181 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
182 if (IsMore) Byte |= 0x80;
183 Asm->OutStreamer.EmitIntValue(Byte, 1, /*addrspace*/0);
187 /// EmitULEB128 - emit the specified signed leb128 value.
188 void DwarfPrinter::EmitULEB128(unsigned Value, const char *Desc,
189 unsigned PadTo) const {
190 if (Asm->VerboseAsm && Desc)
191 Asm->OutStreamer.AddComment(Desc);
193 if (MAI->hasLEB128() && PadTo == 0) {
194 O << "\t.uleb128\t" << Value;
195 Asm->OutStreamer.AddBlankLine();
199 // If we don't have .uleb128 or we want to emit padding, emit as .bytes.
201 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
203 if (Value || PadTo != 0) Byte |= 0x80;
204 Asm->OutStreamer.EmitIntValue(Byte, 1, /*addrspace*/0);
209 Asm->OutStreamer.EmitFill(PadTo - 1, 0x80/*fillval*/, 0/*addrspace*/);
210 Asm->OutStreamer.EmitFill(1, 0/*fillval*/, 0/*addrspace*/);
215 /// PrintLabelName - Print label name in form used by Dwarf writer.
217 void DwarfPrinter::PrintLabelName(const MCSymbol *Label) const {
219 O << Label->getName();
222 void DwarfPrinter::PrintLabelName(const char *Tag, unsigned Number) const {
224 O << MAI->getPrivateGlobalPrefix() << Tag;
225 if (Number) O << Number;
227 void DwarfPrinter::PrintLabelName(const char *Tag, unsigned Number,
228 const char *Suffix) const {
230 O << MAI->getPrivateGlobalPrefix() << Tag;
231 if (Number) O << Number;
235 /// EmitReference - Emit a reference to a label.
237 void DwarfPrinter::EmitReference(const MCSymbol *Sym, bool IsPCRelative,
238 bool Force32Bit) const {
239 PrintRelDirective(Force32Bit);
241 if (IsPCRelative) O << "-" << MAI->getPCSymbol();
244 void DwarfPrinter::EmitReference(const char *Tag, unsigned Number,
245 unsigned Encoding) const {
247 EmitReference(getDWLabel(Tag, Number), Encoding);
250 void DwarfPrinter::EmitReference(const MCSymbol *Sym, unsigned Encoding) const {
251 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
253 PrintRelDirective(Encoding);
254 O << *TLOF.getSymbolForDwarfReference(Sym, Asm->MMI, Encoding);;
257 void DwarfPrinter::EmitReference(const GlobalValue *GV, unsigned Encoding)const {
258 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
260 PrintRelDirective(Encoding);
261 O << *TLOF.getSymbolForDwarfGlobalReference(GV, Asm->Mang,
262 Asm->MMI, Encoding);;
265 /// EmitDifference - Emit the difference between two labels. If this assembler
266 /// supports .set, we emit a .set of a temporary and then use it in the .word.
267 void DwarfPrinter::EmitDifference(const MCSymbol *TagHi, const MCSymbol *TagLo,
269 if (MAI->hasSetDirective()) {
270 // FIXME: switch to OutStreamer.EmitAssignment.
272 PrintLabelName("set", SetCounter, Flavor);
274 PrintLabelName(TagHi);
276 PrintLabelName(TagLo);
279 PrintRelDirective(IsSmall);
280 PrintLabelName("set", SetCounter, Flavor);
283 PrintRelDirective(IsSmall);
284 PrintLabelName(TagHi);
286 PrintLabelName(TagLo);
290 /// EmitDifference - Emit the difference between two labels. If this assembler
291 /// supports .set, we emit a .set of a temporary and then use it in the .word.
292 void DwarfPrinter::EmitDifference(const char *TagHi, unsigned NumberHi,
293 const char *TagLo, unsigned NumberLo,
295 if (MAI->hasSetDirective()) {
296 // FIXME: switch to OutStreamer.EmitAssignment.
298 PrintLabelName("set", SetCounter, Flavor);
300 PrintLabelName(TagHi, NumberHi);
302 PrintLabelName(TagLo, NumberLo);
305 PrintRelDirective(IsSmall);
306 PrintLabelName("set", SetCounter, Flavor);
309 PrintRelDirective(IsSmall);
310 PrintLabelName(TagHi, NumberHi);
312 PrintLabelName(TagLo, NumberLo);
316 void DwarfPrinter::EmitSectionOffset(const MCSymbol *Label,
317 const MCSymbol *Section,
318 bool IsSmall, bool isEH,
320 bool printAbsolute = false;
322 printAbsolute = MAI->isAbsoluteEHSectionOffsets();
324 printAbsolute = MAI->isAbsoluteDebugSectionOffsets();
326 if (MAI->hasSetDirective() && useSet) {
327 // FIXME: switch to OutStreamer.EmitAssignment.
329 PrintLabelName("set", SetCounter, Flavor);
331 PrintLabelName(Label);
333 if (!printAbsolute) {
335 PrintLabelName(Section);
339 PrintRelDirective(IsSmall);
340 PrintLabelName("set", SetCounter, Flavor);
343 PrintRelDirective(IsSmall, true);
344 PrintLabelName(Label);
346 if (!printAbsolute) {
348 PrintLabelName(Section);
353 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
355 void DwarfPrinter::EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
356 const std::vector<MachineMove> &Moves,
359 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
360 TargetFrameInfo::StackGrowsUp ?
361 TD->getPointerSize() : -TD->getPointerSize();
362 bool IsLocal = BaseLabel && strcmp(BaseLabel, "label") == 0;
364 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
365 const MachineMove &Move = Moves[i];
366 unsigned LabelID = Move.getLabelID();
369 LabelID = MMI->MappedLabel(LabelID);
371 // Throw out move if the label is invalid.
372 if (!LabelID) continue;
375 const MachineLocation &Dst = Move.getDestination();
376 const MachineLocation &Src = Move.getSource();
378 // Advance row if new location.
379 if (BaseLabel && LabelID && (BaseLabelID != LabelID || !IsLocal)) {
380 EmitCFAByte(dwarf::DW_CFA_advance_loc4);
381 EmitDifference("label", LabelID, BaseLabel, BaseLabelID, true);
384 BaseLabelID = LabelID;
390 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
392 if (Src.getReg() == MachineLocation::VirtualFP) {
393 EmitCFAByte(dwarf::DW_CFA_def_cfa_offset);
395 EmitCFAByte(dwarf::DW_CFA_def_cfa);
396 EmitULEB128(RI->getDwarfRegNum(Src.getReg(), isEH), "Register");
399 int Offset = -Src.getOffset();
400 EmitULEB128(Offset, "Offset");
402 llvm_unreachable("Machine move not supported yet.");
404 } else if (Src.isReg() &&
405 Src.getReg() == MachineLocation::VirtualFP) {
407 EmitCFAByte(dwarf::DW_CFA_def_cfa_register);
408 EmitULEB128(RI->getDwarfRegNum(Dst.getReg(), isEH), "Register");
410 llvm_unreachable("Machine move not supported yet.");
413 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), isEH);
414 int Offset = Dst.getOffset() / stackGrowth;
417 EmitCFAByte(dwarf::DW_CFA_offset_extended_sf);
418 EmitULEB128(Reg, "Reg");
419 EmitSLEB128(Offset, "Offset");
420 } else if (Reg < 64) {
421 EmitCFAByte(dwarf::DW_CFA_offset + Reg);
422 EmitULEB128(Offset, "Offset");
424 EmitCFAByte(dwarf::DW_CFA_offset_extended);
425 EmitULEB128(Reg, "Reg");
426 EmitULEB128(Offset, "Offset");