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/MCSymbol.h"
22 #include "llvm/Target/TargetData.h"
23 #include "llvm/Target/TargetFrameInfo.h"
24 #include "llvm/Target/TargetRegisterInfo.h"
25 #include "llvm/Support/Dwarf.h"
26 #include "llvm/Support/ErrorHandling.h"
29 DwarfPrinter::DwarfPrinter(raw_ostream &OS, AsmPrinter *A, const MCAsmInfo *T,
31 : O(OS), Asm(A), MAI(T), TD(Asm->TM.getTargetData()),
32 RI(Asm->TM.getRegisterInfo()), M(NULL), MF(NULL), MMI(NULL),
33 SubprogramCount(0), Flavor(flavor), SetCounter(1) {}
35 void DwarfPrinter::PrintRelDirective(bool Force32Bit, bool isInSection) const {
36 if (isInSection && MAI->getDwarfSectionOffsetDirective())
37 O << MAI->getDwarfSectionOffsetDirective();
38 else if (Force32Bit || TD->getPointerSize() == sizeof(int32_t))
39 O << MAI->getData32bitsDirective();
41 O << MAI->getData64bitsDirective();
44 /// PrintLabelName - Print label name in form used by Dwarf writer.
46 void DwarfPrinter::PrintLabelName(const char *Tag, unsigned Number) const {
47 O << MAI->getPrivateGlobalPrefix() << Tag;
48 if (Number) O << Number;
50 void DwarfPrinter::PrintLabelName(const char *Tag, unsigned Number,
51 const char *Suffix) const {
52 O << MAI->getPrivateGlobalPrefix() << Tag;
53 if (Number) O << Number;
57 /// EmitLabel - Emit location label for internal use by Dwarf.
59 void DwarfPrinter::EmitLabel(const char *Tag, unsigned Number) const {
60 PrintLabelName(Tag, Number);
64 /// EmitReference - Emit a reference to a label.
66 void DwarfPrinter::EmitReference(const char *Tag, unsigned Number,
67 bool IsPCRelative, bool Force32Bit) const {
68 PrintRelDirective(Force32Bit);
69 PrintLabelName(Tag, Number);
70 if (IsPCRelative) O << "-" << MAI->getPCSymbol();
72 void DwarfPrinter::EmitReference(const std::string &Name, bool IsPCRelative,
73 bool Force32Bit) const {
74 PrintRelDirective(Force32Bit);
76 if (IsPCRelative) O << "-" << MAI->getPCSymbol();
79 void DwarfPrinter::EmitReference(const MCSymbol *Sym, bool IsPCRelative,
80 bool Force32Bit) const {
81 PrintRelDirective(Force32Bit);
83 if (IsPCRelative) O << "-" << MAI->getPCSymbol();
86 /// EmitDifference - Emit the difference between two labels. Some assemblers do
87 /// not behave with absolute expressions with data directives, so there is an
88 /// option (needsSet) to use an intermediary set expression.
89 void DwarfPrinter::EmitDifference(const char *TagHi, unsigned NumberHi,
90 const char *TagLo, unsigned NumberLo,
92 if (MAI->needsSet()) {
94 PrintLabelName("set", SetCounter, Flavor);
96 PrintLabelName(TagHi, NumberHi);
98 PrintLabelName(TagLo, NumberLo);
101 PrintRelDirective(IsSmall);
102 PrintLabelName("set", SetCounter, Flavor);
105 PrintRelDirective(IsSmall);
106 PrintLabelName(TagHi, NumberHi);
108 PrintLabelName(TagLo, NumberLo);
112 void DwarfPrinter::EmitSectionOffset(const char* Label, const char* Section,
113 unsigned LabelNumber,
114 unsigned SectionNumber,
115 bool IsSmall, bool isEH,
117 bool printAbsolute = false;
119 printAbsolute = MAI->isAbsoluteEHSectionOffsets();
121 printAbsolute = MAI->isAbsoluteDebugSectionOffsets();
123 if (MAI->needsSet() && useSet) {
125 PrintLabelName("set", SetCounter, Flavor);
127 PrintLabelName(Label, LabelNumber);
129 if (!printAbsolute) {
131 PrintLabelName(Section, SectionNumber);
135 PrintRelDirective(IsSmall);
136 PrintLabelName("set", SetCounter, Flavor);
139 PrintRelDirective(IsSmall, true);
140 PrintLabelName(Label, LabelNumber);
142 if (!printAbsolute) {
144 PrintLabelName(Section, SectionNumber);
149 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
151 void DwarfPrinter::EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
152 const std::vector<MachineMove> &Moves,
155 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
156 TargetFrameInfo::StackGrowsUp ?
157 TD->getPointerSize() : -TD->getPointerSize();
158 bool IsLocal = BaseLabel && strcmp(BaseLabel, "label") == 0;
160 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
161 const MachineMove &Move = Moves[i];
162 unsigned LabelID = Move.getLabelID();
165 LabelID = MMI->MappedLabel(LabelID);
167 // Throw out move if the label is invalid.
168 if (!LabelID) continue;
171 const MachineLocation &Dst = Move.getDestination();
172 const MachineLocation &Src = Move.getSource();
174 // Advance row if new location.
175 if (BaseLabel && LabelID && (BaseLabelID != LabelID || !IsLocal)) {
176 Asm->EmitInt8(dwarf::DW_CFA_advance_loc4);
177 Asm->EOL("DW_CFA_advance_loc4");
178 EmitDifference("label", LabelID, BaseLabel, BaseLabelID, true);
181 BaseLabelID = LabelID;
187 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
189 if (Src.getReg() == MachineLocation::VirtualFP) {
190 Asm->EmitInt8(dwarf::DW_CFA_def_cfa_offset);
191 Asm->EOL("DW_CFA_def_cfa_offset");
193 Asm->EmitInt8(dwarf::DW_CFA_def_cfa);
194 Asm->EOL("DW_CFA_def_cfa");
195 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Src.getReg(), isEH));
196 Asm->EOL("Register");
199 int Offset = -Src.getOffset();
201 Asm->EmitULEB128Bytes(Offset);
204 llvm_unreachable("Machine move not supported yet.");
206 } else if (Src.isReg() &&
207 Src.getReg() == MachineLocation::VirtualFP) {
209 Asm->EmitInt8(dwarf::DW_CFA_def_cfa_register);
210 Asm->EOL("DW_CFA_def_cfa_register");
211 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Dst.getReg(), isEH));
212 Asm->EOL("Register");
214 llvm_unreachable("Machine move not supported yet.");
217 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), isEH);
218 int Offset = Dst.getOffset() / stackGrowth;
221 Asm->EmitInt8(dwarf::DW_CFA_offset_extended_sf);
222 Asm->EOL("DW_CFA_offset_extended_sf");
223 Asm->EmitULEB128Bytes(Reg);
225 Asm->EmitSLEB128Bytes(Offset);
227 } else if (Reg < 64) {
228 Asm->EmitInt8(dwarf::DW_CFA_offset + Reg);
229 Asm->EOL("DW_CFA_offset + Reg (" + Twine(Reg) + ")");
230 Asm->EmitULEB128Bytes(Offset);
233 Asm->EmitInt8(dwarf::DW_CFA_offset_extended);
234 Asm->EOL("DW_CFA_offset_extended");
235 Asm->EmitULEB128Bytes(Reg);
237 Asm->EmitULEB128Bytes(Offset);