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.GetOrCreateTemporarySymbol
49 (Twine(MAI->getPrivateGlobalPrefix()) + 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.GetOrCreateTemporarySymbol
56 (Twine(MAI->getPrivateGlobalPrefix()) + Name);
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 static const char *DecodeDWARFEncoding(unsigned Encoding) {
82 case dwarf::DW_EH_PE_absptr: return "absptr";
83 case dwarf::DW_EH_PE_omit: return "omit";
84 case dwarf::DW_EH_PE_pcrel: return "pcrel";
85 case dwarf::DW_EH_PE_udata4: return "udata4";
86 case dwarf::DW_EH_PE_udata8: return "udata8";
87 case dwarf::DW_EH_PE_sdata4: return "sdata4";
88 case dwarf::DW_EH_PE_sdata8: return "sdata8";
89 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4: return "pcrel udata4";
90 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4: return "pcrel sdata4";
91 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8: return "pcrel udata8";
92 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8: return "pcrel sdata8";
93 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata4:
94 return "indirect pcrel udata4";
95 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata4:
96 return "indirect pcrel sdata4";
97 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata8:
98 return "indirect pcrel udata8";
99 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata8:
100 return "indirect pcrel sdata8";
103 return "<unknown encoding>";
106 /// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
107 /// encoding. If verbose assembly output is enabled, we output comments
108 /// describing the encoding. Desc is an optional string saying what the
109 /// encoding is specifying (e.g. "LSDA").
110 void DwarfPrinter::EmitEncodingByte(unsigned Val, const char *Desc) {
111 if (Asm->VerboseAsm) {
113 Asm->OutStreamer.AddComment(Twine(Desc)+" Encoding = " +
114 Twine(DecodeDWARFEncoding(Val)));
116 Asm->OutStreamer.AddComment(Twine("Encoding = ") +
117 DecodeDWARFEncoding(Val));
120 Asm->OutStreamer.EmitIntValue(Val, 1, 0/*addrspace*/);
123 /// EmitCFAByte - Emit a .byte 42 directive for a DW_CFA_xxx value.
124 void DwarfPrinter::EmitCFAByte(unsigned Val) {
125 if (Asm->VerboseAsm) {
126 if (Val >= dwarf::DW_CFA_offset && Val < dwarf::DW_CFA_offset+64)
127 Asm->OutStreamer.AddComment("DW_CFA_offset + Reg (" +
128 Twine(Val-dwarf::DW_CFA_offset) + ")");
130 Asm->OutStreamer.AddComment(dwarf::CallFrameString(Val));
132 Asm->OutStreamer.EmitIntValue(Val, 1, 0/*addrspace*/);
135 /// EmitSLEB128 - emit the specified signed leb128 value.
136 void DwarfPrinter::EmitSLEB128(int Value, const char *Desc) const {
137 if (Asm->VerboseAsm && Desc)
138 Asm->OutStreamer.AddComment(Desc);
140 if (MAI->hasLEB128()) {
142 O << "\t.sleb128\t" << Value;
143 Asm->OutStreamer.AddBlankLine();
147 // If we don't have .sleb128, emit as .bytes.
148 int Sign = Value >> (8 * sizeof(Value) - 1);
152 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
154 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
155 if (IsMore) Byte |= 0x80;
156 Asm->OutStreamer.EmitIntValue(Byte, 1, /*addrspace*/0);
160 /// EmitULEB128 - emit the specified signed leb128 value.
161 void DwarfPrinter::EmitULEB128(unsigned Value, const char *Desc,
162 unsigned PadTo) const {
163 if (Asm->VerboseAsm && Desc)
164 Asm->OutStreamer.AddComment(Desc);
166 if (MAI->hasLEB128() && PadTo == 0) {
168 O << "\t.uleb128\t" << Value;
169 Asm->OutStreamer.AddBlankLine();
173 // If we don't have .uleb128 or we want to emit padding, emit as .bytes.
175 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
177 if (Value || PadTo != 0) Byte |= 0x80;
178 Asm->OutStreamer.EmitIntValue(Byte, 1, /*addrspace*/0);
183 Asm->OutStreamer.EmitFill(PadTo - 1, 0x80/*fillval*/, 0/*addrspace*/);
184 Asm->OutStreamer.EmitFill(1, 0/*fillval*/, 0/*addrspace*/);
189 void DwarfPrinter::EmitReference(const MCSymbol *Sym, unsigned Encoding) const {
190 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
192 const MCExpr *Exp = TLOF.getExprForDwarfReference(Sym, Asm->Mang,
195 Asm->OutStreamer.EmitValue(Exp, SizeOfEncodedValue(Encoding), /*addrspace*/0);
198 void DwarfPrinter::EmitReference(const GlobalValue *GV, unsigned Encoding)const{
199 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
202 TLOF.getExprForDwarfGlobalReference(GV, Asm->Mang, Asm->MMI, Encoding,
204 Asm->OutStreamer.EmitValue(Exp, SizeOfEncodedValue(Encoding), /*addrspace*/0);
207 /// EmitDifference - Emit the difference between two labels. If this assembler
208 /// supports .set, we emit a .set of a temporary and then use it in the .word.
209 void DwarfPrinter::EmitDifference(const MCSymbol *TagHi, const MCSymbol *TagLo,
211 unsigned Size = IsSmall ? 4 : TD->getPointerSize();
212 Asm->EmitLabelDifference(TagHi, TagLo, Size);
215 void DwarfPrinter::EmitSectionOffset(const MCSymbol *Label,
216 const MCSymbol *Section,
217 bool IsSmall, bool isEH) {
220 isAbsolute = MAI->isAbsoluteEHSectionOffsets();
222 isAbsolute = MAI->isAbsoluteDebugSectionOffsets();
225 return EmitDifference(Label, Section, IsSmall);
227 // On COFF targets, we have to emit the weird .secrel32 directive.
228 if (const char *SecOffDir = MAI->getDwarfSectionOffsetDirective()) {
230 Asm->O << SecOffDir << Label->getName();
231 Asm->OutStreamer.AddBlankLine();
233 unsigned Size = IsSmall ? 4 : TD->getPointerSize();
234 Asm->OutStreamer.EmitSymbolValue(Label, Size, 0/*AddrSpace*/);
238 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
240 void DwarfPrinter::EmitFrameMoves(MCSymbol *BaseLabel,
241 const std::vector<MachineMove> &Moves,
243 int stackGrowth = TD->getPointerSize();
244 if (Asm->TM.getFrameInfo()->getStackGrowthDirection() !=
245 TargetFrameInfo::StackGrowsUp)
248 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
249 const MachineMove &Move = Moves[i];
250 unsigned LabelID = Move.getLabelID();
252 // Throw out move if the label is invalid.
253 if (LabelID && MMI->isLabelDeleted(LabelID))
256 const MachineLocation &Dst = Move.getDestination();
257 const MachineLocation &Src = Move.getSource();
259 // Advance row if new location.
260 if (BaseLabel && LabelID) {
261 MCSymbol *ThisSym = getDWLabel("label", LabelID);
262 if (ThisSym != BaseLabel) {
263 EmitCFAByte(dwarf::DW_CFA_advance_loc4);
264 EmitDifference(ThisSym, BaseLabel, true);
270 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
272 if (Src.getReg() == MachineLocation::VirtualFP) {
273 EmitCFAByte(dwarf::DW_CFA_def_cfa_offset);
275 EmitCFAByte(dwarf::DW_CFA_def_cfa);
276 EmitULEB128(RI->getDwarfRegNum(Src.getReg(), isEH), "Register");
279 int Offset = -Src.getOffset();
280 EmitULEB128(Offset, "Offset");
282 llvm_unreachable("Machine move not supported yet.");
284 } else if (Src.isReg() &&
285 Src.getReg() == MachineLocation::VirtualFP) {
287 EmitCFAByte(dwarf::DW_CFA_def_cfa_register);
288 EmitULEB128(RI->getDwarfRegNum(Dst.getReg(), isEH), "Register");
290 llvm_unreachable("Machine move not supported yet.");
293 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), isEH);
294 int Offset = Dst.getOffset() / stackGrowth;
297 EmitCFAByte(dwarf::DW_CFA_offset_extended_sf);
298 EmitULEB128(Reg, "Reg");
299 EmitSLEB128(Offset, "Offset");
300 } else if (Reg < 64) {
301 EmitCFAByte(dwarf::DW_CFA_offset + Reg);
302 EmitULEB128(Offset, "Offset");
304 EmitCFAByte(dwarf::DW_CFA_offset_extended);
305 EmitULEB128(Reg, "Reg");
306 EmitULEB128(Offset, "Offset");