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,
223 const char *Suffix) const {
225 O << MAI->getPrivateGlobalPrefix() << Tag;
226 if (Number) O << Number;
230 /// EmitReference - Emit a reference to a label.
232 void DwarfPrinter::EmitReference(const MCSymbol *Sym, bool IsPCRelative,
233 bool Force32Bit) const {
234 PrintRelDirective(Force32Bit);
236 if (IsPCRelative) O << "-" << MAI->getPCSymbol();
239 void DwarfPrinter::EmitReference(const MCSymbol *Sym, unsigned Encoding) const {
240 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
242 PrintRelDirective(Encoding);
243 O << *TLOF.getSymbolForDwarfReference(Sym, Asm->MMI, Encoding);;
246 void DwarfPrinter::EmitReference(const GlobalValue *GV, unsigned Encoding)const {
247 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
249 PrintRelDirective(Encoding);
250 O << *TLOF.getSymbolForDwarfGlobalReference(GV, Asm->Mang,
254 /// EmitDifference - Emit the difference between two labels. If this assembler
255 /// supports .set, we emit a .set of a temporary and then use it in the .word.
256 void DwarfPrinter::EmitDifference(const MCSymbol *TagHi, const MCSymbol *TagLo,
258 if (MAI->hasSetDirective()) {
259 // FIXME: switch to OutStreamer.EmitAssignment.
261 PrintLabelName("set", SetCounter, Flavor);
263 PrintLabelName(TagHi);
265 PrintLabelName(TagLo);
268 PrintRelDirective(IsSmall);
269 PrintLabelName("set", SetCounter, Flavor);
272 PrintRelDirective(IsSmall);
273 PrintLabelName(TagHi);
275 PrintLabelName(TagLo);
279 void DwarfPrinter::EmitSectionOffset(const MCSymbol *Label,
280 const MCSymbol *Section,
281 bool IsSmall, bool isEH,
283 bool printAbsolute = false;
285 printAbsolute = MAI->isAbsoluteEHSectionOffsets();
287 printAbsolute = MAI->isAbsoluteDebugSectionOffsets();
289 if (MAI->hasSetDirective() && useSet) {
290 // FIXME: switch to OutStreamer.EmitAssignment.
292 PrintLabelName("set", SetCounter, Flavor);
294 PrintLabelName(Label);
296 if (!printAbsolute) {
298 PrintLabelName(Section);
302 PrintRelDirective(IsSmall);
303 PrintLabelName("set", SetCounter, Flavor);
306 PrintRelDirective(IsSmall, true);
307 PrintLabelName(Label);
309 if (!printAbsolute) {
311 PrintLabelName(Section);
316 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
318 void DwarfPrinter::EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
319 const std::vector<MachineMove> &Moves,
322 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
323 TargetFrameInfo::StackGrowsUp ?
324 TD->getPointerSize() : -TD->getPointerSize();
325 bool IsLocal = BaseLabel && strcmp(BaseLabel, "label") == 0;
327 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
328 const MachineMove &Move = Moves[i];
329 unsigned LabelID = Move.getLabelID();
332 LabelID = MMI->MappedLabel(LabelID);
334 // Throw out move if the label is invalid.
335 if (!LabelID) continue;
338 const MachineLocation &Dst = Move.getDestination();
339 const MachineLocation &Src = Move.getSource();
341 // Advance row if new location.
342 if (BaseLabel && LabelID && (BaseLabelID != LabelID || !IsLocal)) {
343 EmitCFAByte(dwarf::DW_CFA_advance_loc4);
344 EmitDifference(getDWLabel("label", LabelID),
345 getDWLabel(BaseLabel, BaseLabelID), true);
348 BaseLabelID = LabelID;
354 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
356 if (Src.getReg() == MachineLocation::VirtualFP) {
357 EmitCFAByte(dwarf::DW_CFA_def_cfa_offset);
359 EmitCFAByte(dwarf::DW_CFA_def_cfa);
360 EmitULEB128(RI->getDwarfRegNum(Src.getReg(), isEH), "Register");
363 int Offset = -Src.getOffset();
364 EmitULEB128(Offset, "Offset");
366 llvm_unreachable("Machine move not supported yet.");
368 } else if (Src.isReg() &&
369 Src.getReg() == MachineLocation::VirtualFP) {
371 EmitCFAByte(dwarf::DW_CFA_def_cfa_register);
372 EmitULEB128(RI->getDwarfRegNum(Dst.getReg(), isEH), "Register");
374 llvm_unreachable("Machine move not supported yet.");
377 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), isEH);
378 int Offset = Dst.getOffset() / stackGrowth;
381 EmitCFAByte(dwarf::DW_CFA_offset_extended_sf);
382 EmitULEB128(Reg, "Reg");
383 EmitSLEB128(Offset, "Offset");
384 } else if (Reg < 64) {
385 EmitCFAByte(dwarf::DW_CFA_offset + Reg);
386 EmitULEB128(Offset, "Offset");
388 EmitCFAByte(dwarf::DW_CFA_offset_extended);
389 EmitULEB128(Reg, "Reg");
390 EmitULEB128(Offset, "Offset");