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/Target/TargetData.h"
22 #include "llvm/Target/TargetFrameInfo.h"
23 #include "llvm/Target/TargetRegisterInfo.h"
24 #include "llvm/Support/Dwarf.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/ADT/StringExtras.h"
29 Dwarf::Dwarf(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 Dwarf::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 Dwarf::PrintLabelName(const char *Tag, unsigned Number,
47 bool ForcePrivate) const {
49 O << MAI->getPrivateGlobalPrefix();
51 if (Number) O << Number;
53 void Dwarf::PrintLabelName(const char *Tag, unsigned Number,
54 const char *Suffix, bool ForcePrivate) const {
56 O << MAI->getPrivateGlobalPrefix();
58 if (Number) O << Number;
62 /// EmitLabel - Emit location label for internal use by Dwarf.
64 void Dwarf::EmitLabel(const char *Tag, unsigned Number,
65 bool ForcePrivate) const {
66 PrintLabelName(Tag, Number, ForcePrivate);
70 /// EmitReference - Emit a reference to a label.
72 void Dwarf::EmitReference(const char *Tag, unsigned Number,
73 bool IsPCRelative, bool Force32Bit) const {
74 PrintRelDirective(Force32Bit);
75 PrintLabelName(Tag, Number);
76 if (IsPCRelative) O << "-" << MAI->getPCSymbol();
78 void Dwarf::EmitReference(const std::string &Name, bool IsPCRelative,
79 bool Force32Bit) const {
80 PrintRelDirective(Force32Bit);
82 if (IsPCRelative) O << "-" << MAI->getPCSymbol();
85 /// EmitDifference - Emit the difference between two labels. Some assemblers do
86 /// not behave with absolute expressions with data directives, so there is an
87 /// option (needsSet) to use an intermediary set expression.
88 void Dwarf::EmitDifference(const char *TagHi, unsigned NumberHi,
89 const char *TagLo, unsigned NumberLo,
91 if (MAI->needsSet()) {
93 PrintLabelName("set", SetCounter, Flavor);
95 PrintLabelName(TagHi, NumberHi);
97 PrintLabelName(TagLo, NumberLo);
100 PrintRelDirective(IsSmall);
101 PrintLabelName("set", SetCounter, Flavor);
104 PrintRelDirective(IsSmall);
105 PrintLabelName(TagHi, NumberHi);
107 PrintLabelName(TagLo, NumberLo);
111 void Dwarf::EmitSectionOffset(const char* Label, const char* Section,
112 unsigned LabelNumber, unsigned SectionNumber,
113 bool IsSmall, bool isEH,
115 bool printAbsolute = false;
117 printAbsolute = MAI->isAbsoluteEHSectionOffsets();
119 printAbsolute = MAI->isAbsoluteDebugSectionOffsets();
121 if (MAI->needsSet() && useSet) {
123 PrintLabelName("set", SetCounter, Flavor);
125 PrintLabelName(Label, LabelNumber);
127 if (!printAbsolute) {
129 PrintLabelName(Section, SectionNumber);
133 PrintRelDirective(IsSmall);
134 PrintLabelName("set", SetCounter, Flavor);
137 PrintRelDirective(IsSmall, true);
138 PrintLabelName(Label, LabelNumber);
140 if (!printAbsolute) {
142 PrintLabelName(Section, SectionNumber);
147 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
149 void Dwarf::EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
150 const std::vector<MachineMove> &Moves, bool isEH) {
152 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
153 TargetFrameInfo::StackGrowsUp ?
154 TD->getPointerSize() : -TD->getPointerSize();
155 bool IsLocal = BaseLabel && strcmp(BaseLabel, "label") == 0;
157 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
158 const MachineMove &Move = Moves[i];
159 unsigned LabelID = Move.getLabelID();
162 LabelID = MMI->MappedLabel(LabelID);
164 // Throw out move if the label is invalid.
165 if (!LabelID) continue;
168 const MachineLocation &Dst = Move.getDestination();
169 const MachineLocation &Src = Move.getSource();
171 // Advance row if new location.
172 if (BaseLabel && LabelID && (BaseLabelID != LabelID || !IsLocal)) {
173 Asm->EmitInt8(dwarf::DW_CFA_advance_loc4);
174 Asm->EOL("DW_CFA_advance_loc4");
175 EmitDifference("label", LabelID, BaseLabel, BaseLabelID, true);
178 BaseLabelID = LabelID;
184 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
186 if (Src.getReg() == MachineLocation::VirtualFP) {
187 Asm->EmitInt8(dwarf::DW_CFA_def_cfa_offset);
188 Asm->EOL("DW_CFA_def_cfa_offset");
190 Asm->EmitInt8(dwarf::DW_CFA_def_cfa);
191 Asm->EOL("DW_CFA_def_cfa");
192 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Src.getReg(), isEH));
193 Asm->EOL("Register");
196 int Offset = -Src.getOffset();
198 Asm->EmitULEB128Bytes(Offset);
201 llvm_unreachable("Machine move not supported yet.");
203 } else if (Src.isReg() &&
204 Src.getReg() == MachineLocation::VirtualFP) {
206 Asm->EmitInt8(dwarf::DW_CFA_def_cfa_register);
207 Asm->EOL("DW_CFA_def_cfa_register");
208 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Dst.getReg(), isEH));
209 Asm->EOL("Register");
211 llvm_unreachable("Machine move not supported yet.");
214 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), isEH);
215 int Offset = Dst.getOffset() / stackGrowth;
218 Asm->EmitInt8(dwarf::DW_CFA_offset_extended_sf);
219 Asm->EOL("DW_CFA_offset_extended_sf");
220 Asm->EmitULEB128Bytes(Reg);
222 Asm->EmitSLEB128Bytes(Offset);
224 } else if (Reg < 64) {
225 Asm->EmitInt8(dwarf::DW_CFA_offset + Reg);
226 if (Asm->isVerbose())
227 Asm->EOL("DW_CFA_offset + Reg (" + utostr(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);