blob: 1299d048e4be6b3b3d410c097262b8007891dc55 [file] [log] [blame]
Bill Wendling88423ee2009-05-15 00:11:17 +00001//===--- lib/CodeGen/DwarfPrinter.cpp - Dwarf Printer ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Emit general DWARF directives.
Anton Korobeynikov9184b252010-02-15 22:35:59 +000011//
Bill Wendling88423ee2009-05-15 00:11:17 +000012//===----------------------------------------------------------------------===//
13
14#include "DwarfPrinter.h"
15#include "llvm/Module.h"
16#include "llvm/CodeGen/AsmPrinter.h"
17#include "llvm/CodeGen/MachineFrameInfo.h"
David Greeneeea1f4c2009-08-19 21:59:18 +000018#include "llvm/CodeGen/MachineFunction.h"
Bill Wendling88423ee2009-05-15 00:11:17 +000019#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000020#include "llvm/MC/MCAsmInfo.h"
Anton Korobeynikov9184b252010-02-15 22:35:59 +000021#include "llvm/MC/MCContext.h"
22#include "llvm/MC/MCExpr.h"
Chris Lattnerf61ed8e2010-01-22 22:38:16 +000023#include "llvm/MC/MCStreamer.h"
Chris Lattner858431d2010-01-16 18:50:28 +000024#include "llvm/MC/MCSymbol.h"
Bill Wendling88423ee2009-05-15 00:11:17 +000025#include "llvm/Target/TargetData.h"
26#include "llvm/Target/TargetFrameInfo.h"
Anton Korobeynikov9184b252010-02-15 22:35:59 +000027#include "llvm/Target/TargetLoweringObjectFile.h"
Bill Wendling88423ee2009-05-15 00:11:17 +000028#include "llvm/Target/TargetRegisterInfo.h"
Chris Lattner23132b12009-08-24 03:52:50 +000029#include "llvm/Support/Dwarf.h"
30#include "llvm/Support/ErrorHandling.h"
Anton Korobeynikov9184b252010-02-15 22:35:59 +000031#include "llvm/ADT/SmallString.h"
Bill Wendling88423ee2009-05-15 00:11:17 +000032using namespace llvm;
33
Chris Lattner066c9ac2010-01-22 22:23:57 +000034DwarfPrinter::DwarfPrinter(raw_ostream &OS, AsmPrinter *A, const MCAsmInfo *T,
35 const char *flavor)
Chris Lattner33adcfb2009-08-22 21:43:10 +000036: O(OS), Asm(A), MAI(T), TD(Asm->TM.getTargetData()),
Bill Wendling88423ee2009-05-15 00:11:17 +000037 RI(Asm->TM.getRegisterInfo()), M(NULL), MF(NULL), MMI(NULL),
38 SubprogramCount(0), Flavor(flavor), SetCounter(1) {}
39
Anton Korobeynikov9184b252010-02-15 22:35:59 +000040/// SizeOfEncodedValue - Return the size of the encoding in bytes.
41unsigned DwarfPrinter::SizeOfEncodedValue(unsigned Encoding) const {
42 if (Encoding == dwarf::DW_EH_PE_omit)
43 return 0;
44
45 switch (Encoding & 0x07) {
46 case dwarf::DW_EH_PE_absptr:
47 return TD->getPointerSize();
48 case dwarf::DW_EH_PE_udata2:
49 return 2;
50 case dwarf::DW_EH_PE_udata4:
51 return 4;
52 case dwarf::DW_EH_PE_udata8:
53 return 8;
54 }
55
56 assert(0 && "Invalid encoded value.");
57 return 0;
58}
59
Chris Lattner066c9ac2010-01-22 22:23:57 +000060void DwarfPrinter::PrintRelDirective(bool Force32Bit, bool isInSection) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +000061 if (isInSection && MAI->getDwarfSectionOffsetDirective())
62 O << MAI->getDwarfSectionOffsetDirective();
Bill Wendling88423ee2009-05-15 00:11:17 +000063 else if (Force32Bit || TD->getPointerSize() == sizeof(int32_t))
Chris Lattner33adcfb2009-08-22 21:43:10 +000064 O << MAI->getData32bitsDirective();
Bill Wendling88423ee2009-05-15 00:11:17 +000065 else
Chris Lattner33adcfb2009-08-22 21:43:10 +000066 O << MAI->getData64bitsDirective();
Bill Wendling88423ee2009-05-15 00:11:17 +000067}
68
Anton Korobeynikov9184b252010-02-15 22:35:59 +000069void DwarfPrinter::PrintRelDirective(unsigned Encoding) const {
70 unsigned Size = SizeOfEncodedValue(Encoding);
71 assert((Size == 4 || Size == 8) && "Do not support other types or rels!");
72
73 O << (Size == 4 ?
74 MAI->getData32bitsDirective() : MAI->getData64bitsDirective());
75}
76
Chris Lattnerfaca5492010-01-22 23:47:11 +000077/// EOL - Print a newline character to asm stream. If a comment is present
78/// then it will be printed first. Comments should not contain '\n'.
79void DwarfPrinter::EOL(const Twine &Comment) const {
80 if (Asm->VerboseAsm && !Comment.isTriviallyEmpty()) {
81 Asm->O.PadToColumn(MAI->getCommentColumn());
82 Asm->O << Asm->MAI->getCommentString() << ' ' << Comment;
83 }
84 Asm->O << '\n';
85}
86
Chris Lattnerf61ed8e2010-01-22 22:38:16 +000087static const char *DecodeDWARFEncoding(unsigned Encoding) {
88 switch (Encoding) {
89 case dwarf::DW_EH_PE_absptr: return "absptr";
90 case dwarf::DW_EH_PE_omit: return "omit";
91 case dwarf::DW_EH_PE_pcrel: return "pcrel";
92 case dwarf::DW_EH_PE_udata4: return "udata4";
93 case dwarf::DW_EH_PE_udata8: return "udata8";
94 case dwarf::DW_EH_PE_sdata4: return "sdata4";
95 case dwarf::DW_EH_PE_sdata8: return "sdata8";
96 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4: return "pcrel udata4";
97 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4: return "pcrel sdata4";
98 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8: return "pcrel udata8";
99 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8: return "pcrel sdata8";
100 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata4:
101 return "indirect pcrel udata4";
102 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata4:
103 return "indirect pcrel sdata4";
104 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata8:
105 return "indirect pcrel udata8";
106 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata8:
107 return "indirect pcrel sdata8";
108 }
109
110 return "<unknown encoding>";
111}
112
113/// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
114/// encoding. If verbose assembly output is enabled, we output comments
115/// describing the encoding. Desc is an optional string saying what the
116/// encoding is specifying (e.g. "LSDA").
117void DwarfPrinter::EmitEncodingByte(unsigned Val, const char *Desc) {
118 if (Asm->VerboseAsm) {
119 if (Desc != 0)
120 Asm->OutStreamer.AddComment(Twine(Desc)+" Encoding = " +
121 Twine(DecodeDWARFEncoding(Val)));
122 else
123 Asm->OutStreamer.AddComment(Twine("Encoding = ") +
124 DecodeDWARFEncoding(Val));
125 }
126
127 Asm->OutStreamer.EmitIntValue(Val, 1, 0/*addrspace*/);
128}
129
Chris Lattner245834d2010-01-22 23:40:08 +0000130/// EmitCFAByte - Emit a .byte 42 directive for a DW_CFA_xxx value.
131void DwarfPrinter::EmitCFAByte(unsigned Val) {
132 if (Asm->VerboseAsm) {
133 if (Val >= dwarf::DW_CFA_offset && Val < dwarf::DW_CFA_offset+64)
134 Asm->OutStreamer.AddComment("DW_CFA_offset + Reg (" +
135 Twine(Val-dwarf::DW_CFA_offset) + ")");
136 else
137 Asm->OutStreamer.AddComment(dwarf::CallFrameString(Val));
138 }
139 Asm->OutStreamer.EmitIntValue(Val, 1, 0/*addrspace*/);
140}
141
Chris Lattner894d75a2010-01-22 23:18:42 +0000142/// EmitSLEB128 - emit the specified signed leb128 value.
143void DwarfPrinter::EmitSLEB128(int Value, const char *Desc) const {
144 if (Asm->VerboseAsm && Desc)
145 Asm->OutStreamer.AddComment(Desc);
146
147 if (MAI->hasLEB128()) {
148 O << "\t.sleb128\t" << Value;
149 Asm->OutStreamer.AddBlankLine();
150 return;
151 }
152
153 // If we don't have .sleb128, emit as .bytes.
Chris Lattnerbb9078a2010-01-22 22:56:55 +0000154 int Sign = Value >> (8 * sizeof(Value) - 1);
155 bool IsMore;
156
157 do {
158 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
159 Value >>= 7;
160 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
161 if (IsMore) Byte |= 0x80;
162
Chris Lattner894d75a2010-01-22 23:18:42 +0000163 Asm->OutStreamer.EmitIntValue(Byte, 1, /*addrspace*/0);
Chris Lattnerbb9078a2010-01-22 22:56:55 +0000164 } while (IsMore);
165}
166
Chris Lattner894d75a2010-01-22 23:18:42 +0000167/// EmitULEB128 - emit the specified signed leb128 value.
168void DwarfPrinter::EmitULEB128(unsigned Value, const char *Desc) const {
Chris Lattnerbb9078a2010-01-22 22:56:55 +0000169 if (Asm->VerboseAsm && Desc)
170 Asm->OutStreamer.AddComment(Desc);
Chris Lattner894d75a2010-01-22 23:18:42 +0000171
Chris Lattnerbb9078a2010-01-22 22:56:55 +0000172 if (MAI->hasLEB128()) {
Chris Lattner894d75a2010-01-22 23:18:42 +0000173 O << "\t.uleb128\t" << Value;
Chris Lattnerbb9078a2010-01-22 22:56:55 +0000174 Asm->OutStreamer.AddBlankLine();
Chris Lattner894d75a2010-01-22 23:18:42 +0000175 return;
Chris Lattnerbb9078a2010-01-22 22:56:55 +0000176 }
Chris Lattner894d75a2010-01-22 23:18:42 +0000177
178 // If we don't have .uleb128, emit as .bytes.
179 do {
180 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
181 Value >>= 7;
182 if (Value) Byte |= 0x80;
183 Asm->OutStreamer.EmitIntValue(Byte, 1, /*addrspace*/0);
184 } while (Value);
Chris Lattnerbb9078a2010-01-22 22:56:55 +0000185}
186
187
Bill Wendling88423ee2009-05-15 00:11:17 +0000188/// PrintLabelName - Print label name in form used by Dwarf writer.
189///
Chris Lattner066c9ac2010-01-22 22:23:57 +0000190void DwarfPrinter::PrintLabelName(const char *Tag, unsigned Number) const {
Jim Grosbacha01d3462009-09-01 16:43:35 +0000191 O << MAI->getPrivateGlobalPrefix() << Tag;
Bill Wendling88423ee2009-05-15 00:11:17 +0000192 if (Number) O << Number;
193}
Chris Lattner066c9ac2010-01-22 22:23:57 +0000194void DwarfPrinter::PrintLabelName(const char *Tag, unsigned Number,
195 const char *Suffix) const {
Jim Grosbacha01d3462009-09-01 16:43:35 +0000196 O << MAI->getPrivateGlobalPrefix() << Tag;
Bill Wendling88423ee2009-05-15 00:11:17 +0000197 if (Number) O << Number;
198 O << Suffix;
199}
200
201/// EmitLabel - Emit location label for internal use by Dwarf.
202///
Chris Lattner066c9ac2010-01-22 22:23:57 +0000203void DwarfPrinter::EmitLabel(const char *Tag, unsigned Number) const {
Jim Grosbacha01d3462009-09-01 16:43:35 +0000204 PrintLabelName(Tag, Number);
Bill Wendling88423ee2009-05-15 00:11:17 +0000205 O << ":\n";
206}
207
208/// EmitReference - Emit a reference to a label.
209///
Chris Lattner066c9ac2010-01-22 22:23:57 +0000210void DwarfPrinter::EmitReference(const char *Tag, unsigned Number,
211 bool IsPCRelative, bool Force32Bit) const {
Bill Wendling88423ee2009-05-15 00:11:17 +0000212 PrintRelDirective(Force32Bit);
213 PrintLabelName(Tag, Number);
Chris Lattner33adcfb2009-08-22 21:43:10 +0000214 if (IsPCRelative) O << "-" << MAI->getPCSymbol();
Bill Wendling88423ee2009-05-15 00:11:17 +0000215}
Chris Lattner066c9ac2010-01-22 22:23:57 +0000216void DwarfPrinter::EmitReference(const std::string &Name, bool IsPCRelative,
217 bool Force32Bit) const {
Bill Wendling88423ee2009-05-15 00:11:17 +0000218 PrintRelDirective(Force32Bit);
219 O << Name;
Chris Lattner33adcfb2009-08-22 21:43:10 +0000220 if (IsPCRelative) O << "-" << MAI->getPCSymbol();
Bill Wendling88423ee2009-05-15 00:11:17 +0000221}
222
Chris Lattner066c9ac2010-01-22 22:23:57 +0000223void DwarfPrinter::EmitReference(const MCSymbol *Sym, bool IsPCRelative,
224 bool Force32Bit) const {
Chris Lattner858431d2010-01-16 18:50:28 +0000225 PrintRelDirective(Force32Bit);
Chris Lattner10b318b2010-01-17 21:43:43 +0000226 O << *Sym;
Chris Lattner858431d2010-01-16 18:50:28 +0000227 if (IsPCRelative) O << "-" << MAI->getPCSymbol();
228}
229
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000230void DwarfPrinter::EmitReference(const char *Tag, unsigned Number,
231 unsigned Encoding) const {
232 SmallString<64> Name;
233 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix()
234 << Tag << Number;
235
236 MCSymbol *Sym = Asm->OutContext.GetOrCreateSymbol(Name.str());
237 EmitReference(Sym, Encoding);
238}
239
240void DwarfPrinter::EmitReference(const MCSymbol *Sym, unsigned Encoding) const {
241 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
242
243 PrintRelDirective(Encoding);
244 O << *TLOF.getSymbolForDwarfReference(Sym, Asm->MMI, Encoding);;
245}
246
247void DwarfPrinter::EmitReference(const GlobalValue *GV, unsigned Encoding)const {
248 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
249
250 PrintRelDirective(Encoding);
251 O << *TLOF.getSymbolForDwarfGlobalReference(GV, Asm->Mang,
252 Asm->MMI, Encoding);;
253}
254
Chris Lattner6a315c32010-01-26 20:20:43 +0000255/// EmitDifference - Emit the difference between two labels. If this assembler
256/// supports .set, we emit a .set of a temporary and then use it in the .word.
Chris Lattner066c9ac2010-01-22 22:23:57 +0000257void DwarfPrinter::EmitDifference(const char *TagHi, unsigned NumberHi,
258 const char *TagLo, unsigned NumberLo,
259 bool IsSmall) {
Chris Lattnercee63322010-01-26 20:40:54 +0000260 if (MAI->hasSetDirective()) {
Chris Lattnerc618c8a2010-01-26 21:53:08 +0000261 // FIXME: switch to OutStreamer.EmitAssignment.
Bill Wendling88423ee2009-05-15 00:11:17 +0000262 O << "\t.set\t";
263 PrintLabelName("set", SetCounter, Flavor);
264 O << ",";
265 PrintLabelName(TagHi, NumberHi);
266 O << "-";
267 PrintLabelName(TagLo, NumberLo);
268 O << "\n";
269
270 PrintRelDirective(IsSmall);
271 PrintLabelName("set", SetCounter, Flavor);
272 ++SetCounter;
273 } else {
274 PrintRelDirective(IsSmall);
275 PrintLabelName(TagHi, NumberHi);
276 O << "-";
277 PrintLabelName(TagLo, NumberLo);
278 }
279}
280
Chris Lattner066c9ac2010-01-22 22:23:57 +0000281void DwarfPrinter::EmitSectionOffset(const char* Label, const char* Section,
282 unsigned LabelNumber,
283 unsigned SectionNumber,
284 bool IsSmall, bool isEH,
285 bool useSet) {
Bill Wendling88423ee2009-05-15 00:11:17 +0000286 bool printAbsolute = false;
287 if (isEH)
Chris Lattner33adcfb2009-08-22 21:43:10 +0000288 printAbsolute = MAI->isAbsoluteEHSectionOffsets();
Bill Wendling88423ee2009-05-15 00:11:17 +0000289 else
Chris Lattner33adcfb2009-08-22 21:43:10 +0000290 printAbsolute = MAI->isAbsoluteDebugSectionOffsets();
Bill Wendling88423ee2009-05-15 00:11:17 +0000291
Chris Lattnercee63322010-01-26 20:40:54 +0000292 if (MAI->hasSetDirective() && useSet) {
Chris Lattnerc618c8a2010-01-26 21:53:08 +0000293 // FIXME: switch to OutStreamer.EmitAssignment.
Bill Wendling88423ee2009-05-15 00:11:17 +0000294 O << "\t.set\t";
295 PrintLabelName("set", SetCounter, Flavor);
296 O << ",";
297 PrintLabelName(Label, LabelNumber);
298
299 if (!printAbsolute) {
300 O << "-";
301 PrintLabelName(Section, SectionNumber);
302 }
303
304 O << "\n";
305 PrintRelDirective(IsSmall);
306 PrintLabelName("set", SetCounter, Flavor);
307 ++SetCounter;
308 } else {
309 PrintRelDirective(IsSmall, true);
310 PrintLabelName(Label, LabelNumber);
311
312 if (!printAbsolute) {
313 O << "-";
314 PrintLabelName(Section, SectionNumber);
315 }
316 }
317}
318
319/// EmitFrameMoves - Emit frame instructions to describe the layout of the
320/// frame.
Chris Lattner066c9ac2010-01-22 22:23:57 +0000321void DwarfPrinter::EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
322 const std::vector<MachineMove> &Moves,
323 bool isEH) {
Bill Wendling88423ee2009-05-15 00:11:17 +0000324 int stackGrowth =
325 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
326 TargetFrameInfo::StackGrowsUp ?
327 TD->getPointerSize() : -TD->getPointerSize();
328 bool IsLocal = BaseLabel && strcmp(BaseLabel, "label") == 0;
329
330 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
331 const MachineMove &Move = Moves[i];
332 unsigned LabelID = Move.getLabelID();
333
334 if (LabelID) {
335 LabelID = MMI->MappedLabel(LabelID);
336
337 // Throw out move if the label is invalid.
338 if (!LabelID) continue;
339 }
340
341 const MachineLocation &Dst = Move.getDestination();
342 const MachineLocation &Src = Move.getSource();
343
344 // Advance row if new location.
345 if (BaseLabel && LabelID && (BaseLabelID != LabelID || !IsLocal)) {
Chris Lattner245834d2010-01-22 23:40:08 +0000346 EmitCFAByte(dwarf::DW_CFA_advance_loc4);
Bill Wendling88423ee2009-05-15 00:11:17 +0000347 EmitDifference("label", LabelID, BaseLabel, BaseLabelID, true);
Chris Lattner0ad9c912010-01-22 22:09:00 +0000348 Asm->O << '\n';
Bill Wendling88423ee2009-05-15 00:11:17 +0000349
350 BaseLabelID = LabelID;
351 BaseLabel = "label";
352 IsLocal = true;
353 }
354
355 // If advancing cfa.
356 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
357 if (!Src.isReg()) {
358 if (Src.getReg() == MachineLocation::VirtualFP) {
Chris Lattner245834d2010-01-22 23:40:08 +0000359 EmitCFAByte(dwarf::DW_CFA_def_cfa_offset);
Bill Wendling88423ee2009-05-15 00:11:17 +0000360 } else {
Chris Lattner245834d2010-01-22 23:40:08 +0000361 EmitCFAByte(dwarf::DW_CFA_def_cfa);
Chris Lattner894d75a2010-01-22 23:18:42 +0000362 EmitULEB128(RI->getDwarfRegNum(Src.getReg(), isEH), "Register");
Bill Wendling88423ee2009-05-15 00:11:17 +0000363 }
364
365 int Offset = -Src.getOffset();
Chris Lattner894d75a2010-01-22 23:18:42 +0000366 EmitULEB128(Offset, "Offset");
Bill Wendling88423ee2009-05-15 00:11:17 +0000367 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000368 llvm_unreachable("Machine move not supported yet.");
Bill Wendling88423ee2009-05-15 00:11:17 +0000369 }
370 } else if (Src.isReg() &&
371 Src.getReg() == MachineLocation::VirtualFP) {
372 if (Dst.isReg()) {
Chris Lattner245834d2010-01-22 23:40:08 +0000373 EmitCFAByte(dwarf::DW_CFA_def_cfa_register);
Chris Lattner894d75a2010-01-22 23:18:42 +0000374 EmitULEB128(RI->getDwarfRegNum(Dst.getReg(), isEH), "Register");
Bill Wendling88423ee2009-05-15 00:11:17 +0000375 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000376 llvm_unreachable("Machine move not supported yet.");
Bill Wendling88423ee2009-05-15 00:11:17 +0000377 }
378 } else {
379 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), isEH);
380 int Offset = Dst.getOffset() / stackGrowth;
381
382 if (Offset < 0) {
Chris Lattner245834d2010-01-22 23:40:08 +0000383 EmitCFAByte(dwarf::DW_CFA_offset_extended_sf);
Chris Lattner894d75a2010-01-22 23:18:42 +0000384 EmitULEB128(Reg, "Reg");
Chris Lattnerbb9078a2010-01-22 22:56:55 +0000385 EmitSLEB128(Offset, "Offset");
Bill Wendling88423ee2009-05-15 00:11:17 +0000386 } else if (Reg < 64) {
Chris Lattner245834d2010-01-22 23:40:08 +0000387 EmitCFAByte(dwarf::DW_CFA_offset + Reg);
Chris Lattner894d75a2010-01-22 23:18:42 +0000388 EmitULEB128(Offset, "Offset");
Bill Wendling88423ee2009-05-15 00:11:17 +0000389 } else {
Chris Lattner245834d2010-01-22 23:40:08 +0000390 EmitCFAByte(dwarf::DW_CFA_offset_extended);
Chris Lattner894d75a2010-01-22 23:18:42 +0000391 EmitULEB128(Reg, "Reg");
392 EmitULEB128(Offset, "Offset");
Bill Wendling88423ee2009-05-15 00:11:17 +0000393 }
394 }
395 }
396}