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/MachineModuleInfo.h"
19 #include "llvm/Support/Dwarf.h"
20 #include "llvm/Target/TargetAsmInfo.h"
21 #include "llvm/Target/TargetData.h"
22 #include "llvm/Target/TargetFrameInfo.h"
23 #include "llvm/Target/TargetRegisterInfo.h"
27 Dwarf::Dwarf(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T,
29 : O(OS), Asm(A), TAI(T), TD(Asm->TM.getTargetData()),
30 RI(Asm->TM.getRegisterInfo()), M(NULL), MF(NULL), MMI(NULL),
31 SubprogramCount(0), Flavor(flavor), SetCounter(1) {}
33 void Dwarf::PrintRelDirective(bool Force32Bit, bool isInSection) const {
34 if (isInSection && TAI->getDwarfSectionOffsetDirective())
35 O << TAI->getDwarfSectionOffsetDirective();
36 else if (Force32Bit || TD->getPointerSize() == sizeof(int32_t))
37 O << TAI->getData32bitsDirective();
39 O << TAI->getData64bitsDirective();
42 /// PrintLabelName - Print label name in form used by Dwarf writer.
44 void Dwarf::PrintLabelName(const char *Tag, unsigned Number) const {
45 O << TAI->getPrivateGlobalPrefix() << Tag;
46 if (Number) O << Number;
48 void Dwarf::PrintLabelName(const char *Tag, unsigned Number,
49 const char *Suffix) const {
50 O << TAI->getPrivateGlobalPrefix() << Tag;
51 if (Number) O << Number;
55 /// EmitLabel - Emit location label for internal use by Dwarf.
57 void Dwarf::EmitLabel(const char *Tag, unsigned Number) const {
58 PrintLabelName(Tag, Number);
62 /// EmitReference - Emit a reference to a label.
64 void Dwarf::EmitReference(const char *Tag, unsigned Number,
65 bool IsPCRelative, bool Force32Bit) const {
66 PrintRelDirective(Force32Bit);
67 PrintLabelName(Tag, Number);
68 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
70 void Dwarf::EmitReference(const std::string &Name, bool IsPCRelative,
71 bool Force32Bit) const {
72 PrintRelDirective(Force32Bit);
74 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
77 /// EmitDifference - Emit the difference between two labels. Some assemblers do
78 /// not behave with absolute expressions with data directives, so there is an
79 /// option (needsSet) to use an intermediary set expression.
80 void Dwarf::EmitDifference(const char *TagHi, unsigned NumberHi,
81 const char *TagLo, unsigned NumberLo,
83 if (TAI->needsSet()) {
85 PrintLabelName("set", SetCounter, Flavor);
87 PrintLabelName(TagHi, NumberHi);
89 PrintLabelName(TagLo, NumberLo);
92 PrintRelDirective(IsSmall);
93 PrintLabelName("set", SetCounter, Flavor);
96 PrintRelDirective(IsSmall);
97 PrintLabelName(TagHi, NumberHi);
99 PrintLabelName(TagLo, NumberLo);
103 void Dwarf::EmitSectionOffset(const char* Label, const char* Section,
104 unsigned LabelNumber, unsigned SectionNumber,
105 bool IsSmall, bool isEH,
107 bool printAbsolute = false;
109 printAbsolute = TAI->isAbsoluteEHSectionOffsets();
111 printAbsolute = TAI->isAbsoluteDebugSectionOffsets();
113 if (TAI->needsSet() && useSet) {
115 PrintLabelName("set", SetCounter, Flavor);
117 PrintLabelName(Label, LabelNumber);
119 if (!printAbsolute) {
121 PrintLabelName(Section, SectionNumber);
125 PrintRelDirective(IsSmall);
126 PrintLabelName("set", SetCounter, Flavor);
129 PrintRelDirective(IsSmall, true);
130 PrintLabelName(Label, LabelNumber);
132 if (!printAbsolute) {
134 PrintLabelName(Section, SectionNumber);
139 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
141 void Dwarf::EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
142 const std::vector<MachineMove> &Moves, bool isEH) {
144 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
145 TargetFrameInfo::StackGrowsUp ?
146 TD->getPointerSize() : -TD->getPointerSize();
147 bool IsLocal = BaseLabel && strcmp(BaseLabel, "label") == 0;
149 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
150 const MachineMove &Move = Moves[i];
151 unsigned LabelID = Move.getLabelID();
154 LabelID = MMI->MappedLabel(LabelID);
156 // Throw out move if the label is invalid.
157 if (!LabelID) continue;
160 const MachineLocation &Dst = Move.getDestination();
161 const MachineLocation &Src = Move.getSource();
163 // Advance row if new location.
164 if (BaseLabel && LabelID && (BaseLabelID != LabelID || !IsLocal)) {
165 Asm->EmitInt8(dwarf::DW_CFA_advance_loc4);
166 Asm->EOL("DW_CFA_advance_loc4");
167 EmitDifference("label", LabelID, BaseLabel, BaseLabelID, true);
170 BaseLabelID = LabelID;
176 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
178 if (Src.getReg() == MachineLocation::VirtualFP) {
179 Asm->EmitInt8(dwarf::DW_CFA_def_cfa_offset);
180 Asm->EOL("DW_CFA_def_cfa_offset");
182 Asm->EmitInt8(dwarf::DW_CFA_def_cfa);
183 Asm->EOL("DW_CFA_def_cfa");
184 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Src.getReg(), isEH));
185 Asm->EOL("Register");
188 int Offset = -Src.getOffset();
190 Asm->EmitULEB128Bytes(Offset);
193 assert(0 && "Machine move not supported yet.");
195 } else if (Src.isReg() &&
196 Src.getReg() == MachineLocation::VirtualFP) {
198 Asm->EmitInt8(dwarf::DW_CFA_def_cfa_register);
199 Asm->EOL("DW_CFA_def_cfa_register");
200 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Dst.getReg(), isEH));
201 Asm->EOL("Register");
203 assert(0 && "Machine move not supported yet.");
206 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), isEH);
207 int Offset = Dst.getOffset() / stackGrowth;
210 Asm->EmitInt8(dwarf::DW_CFA_offset_extended_sf);
211 Asm->EOL("DW_CFA_offset_extended_sf");
212 Asm->EmitULEB128Bytes(Reg);
214 Asm->EmitSLEB128Bytes(Offset);
216 } else if (Reg < 64) {
217 Asm->EmitInt8(dwarf::DW_CFA_offset + Reg);
218 if (Asm->isVerbose())
219 Asm->EOL("DW_CFA_offset + Reg (" + utostr(Reg) + ")");
222 Asm->EmitULEB128Bytes(Offset);
225 Asm->EmitInt8(dwarf::DW_CFA_offset_extended);
226 Asm->EOL("DW_CFA_offset_extended");
227 Asm->EmitULEB128Bytes(Reg);
229 Asm->EmitULEB128Bytes(Offset);