blob: 52a4055d157b4341dbb4193579a407b3a245b161 [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
Chris Lattnerb98b1bf2010-03-08 22:23:36 +000040
41/// getDWLabel - Return the MCSymbol corresponding to the assembler temporary
42/// label with the specified stem and unique ID.
43MCSymbol *DwarfPrinter::getDWLabel(const char *Name, unsigned ID) const {
44 // FIXME: REMOVE this. However, there is stuff in EH that passes counters in
45 // here that can be zero.
46
47 //assert(ID && "Should use getTempLabel if no ID");
48 if (ID == 0) return getTempLabel(Name);
49 return Asm->OutContext.GetOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix())
50 + Twine(Name) + Twine(ID));
51}
52
53/// getTempLabel - Return the MCSymbol corresponding to the assembler temporary
54/// label with the specified name.
55MCSymbol *DwarfPrinter::getTempLabel(const char *Name) const {
56 return Asm->OutContext.GetOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix())
57 + Name);
58}
59
60
Anton Korobeynikov9184b252010-02-15 22:35:59 +000061/// SizeOfEncodedValue - Return the size of the encoding in bytes.
62unsigned DwarfPrinter::SizeOfEncodedValue(unsigned Encoding) const {
63 if (Encoding == dwarf::DW_EH_PE_omit)
64 return 0;
65
66 switch (Encoding & 0x07) {
67 case dwarf::DW_EH_PE_absptr:
68 return TD->getPointerSize();
69 case dwarf::DW_EH_PE_udata2:
70 return 2;
71 case dwarf::DW_EH_PE_udata4:
72 return 4;
73 case dwarf::DW_EH_PE_udata8:
74 return 8;
75 }
76
77 assert(0 && "Invalid encoded value.");
78 return 0;
79}
80
Chris Lattner066c9ac2010-01-22 22:23:57 +000081void DwarfPrinter::PrintRelDirective(bool Force32Bit, bool isInSection) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +000082 if (isInSection && MAI->getDwarfSectionOffsetDirective())
83 O << MAI->getDwarfSectionOffsetDirective();
Bill Wendling88423ee2009-05-15 00:11:17 +000084 else if (Force32Bit || TD->getPointerSize() == sizeof(int32_t))
Chris Lattner33adcfb2009-08-22 21:43:10 +000085 O << MAI->getData32bitsDirective();
Bill Wendling88423ee2009-05-15 00:11:17 +000086 else
Chris Lattner33adcfb2009-08-22 21:43:10 +000087 O << MAI->getData64bitsDirective();
Bill Wendling88423ee2009-05-15 00:11:17 +000088}
89
Anton Korobeynikov9184b252010-02-15 22:35:59 +000090void DwarfPrinter::PrintRelDirective(unsigned Encoding) const {
91 unsigned Size = SizeOfEncodedValue(Encoding);
92 assert((Size == 4 || Size == 8) && "Do not support other types or rels!");
93
94 O << (Size == 4 ?
95 MAI->getData32bitsDirective() : MAI->getData64bitsDirective());
96}
97
Chris Lattnerfaca5492010-01-22 23:47:11 +000098/// EOL - Print a newline character to asm stream. If a comment is present
99/// then it will be printed first. Comments should not contain '\n'.
100void DwarfPrinter::EOL(const Twine &Comment) const {
101 if (Asm->VerboseAsm && !Comment.isTriviallyEmpty()) {
102 Asm->O.PadToColumn(MAI->getCommentColumn());
103 Asm->O << Asm->MAI->getCommentString() << ' ' << Comment;
104 }
105 Asm->O << '\n';
106}
107
Chris Lattnerf61ed8e2010-01-22 22:38:16 +0000108static const char *DecodeDWARFEncoding(unsigned Encoding) {
109 switch (Encoding) {
110 case dwarf::DW_EH_PE_absptr: return "absptr";
111 case dwarf::DW_EH_PE_omit: return "omit";
112 case dwarf::DW_EH_PE_pcrel: return "pcrel";
113 case dwarf::DW_EH_PE_udata4: return "udata4";
114 case dwarf::DW_EH_PE_udata8: return "udata8";
115 case dwarf::DW_EH_PE_sdata4: return "sdata4";
116 case dwarf::DW_EH_PE_sdata8: return "sdata8";
117 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4: return "pcrel udata4";
118 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4: return "pcrel sdata4";
119 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8: return "pcrel udata8";
120 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8: return "pcrel sdata8";
121 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata4:
122 return "indirect pcrel udata4";
123 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata4:
124 return "indirect pcrel sdata4";
125 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata8:
126 return "indirect pcrel udata8";
127 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata8:
128 return "indirect pcrel sdata8";
129 }
130
131 return "<unknown encoding>";
132}
133
134/// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
135/// encoding. If verbose assembly output is enabled, we output comments
136/// describing the encoding. Desc is an optional string saying what the
137/// encoding is specifying (e.g. "LSDA").
138void DwarfPrinter::EmitEncodingByte(unsigned Val, const char *Desc) {
139 if (Asm->VerboseAsm) {
140 if (Desc != 0)
141 Asm->OutStreamer.AddComment(Twine(Desc)+" Encoding = " +
142 Twine(DecodeDWARFEncoding(Val)));
143 else
144 Asm->OutStreamer.AddComment(Twine("Encoding = ") +
145 DecodeDWARFEncoding(Val));
146 }
147
148 Asm->OutStreamer.EmitIntValue(Val, 1, 0/*addrspace*/);
149}
150
Chris Lattner245834d2010-01-22 23:40:08 +0000151/// EmitCFAByte - Emit a .byte 42 directive for a DW_CFA_xxx value.
152void DwarfPrinter::EmitCFAByte(unsigned Val) {
153 if (Asm->VerboseAsm) {
154 if (Val >= dwarf::DW_CFA_offset && Val < dwarf::DW_CFA_offset+64)
155 Asm->OutStreamer.AddComment("DW_CFA_offset + Reg (" +
156 Twine(Val-dwarf::DW_CFA_offset) + ")");
157 else
158 Asm->OutStreamer.AddComment(dwarf::CallFrameString(Val));
159 }
160 Asm->OutStreamer.EmitIntValue(Val, 1, 0/*addrspace*/);
161}
162
Chris Lattner894d75a2010-01-22 23:18:42 +0000163/// EmitSLEB128 - emit the specified signed leb128 value.
164void DwarfPrinter::EmitSLEB128(int Value, const char *Desc) const {
165 if (Asm->VerboseAsm && Desc)
166 Asm->OutStreamer.AddComment(Desc);
167
168 if (MAI->hasLEB128()) {
169 O << "\t.sleb128\t" << Value;
170 Asm->OutStreamer.AddBlankLine();
171 return;
172 }
173
174 // If we don't have .sleb128, emit as .bytes.
Chris Lattnerbb9078a2010-01-22 22:56:55 +0000175 int Sign = Value >> (8 * sizeof(Value) - 1);
176 bool IsMore;
177
178 do {
179 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
180 Value >>= 7;
181 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
182 if (IsMore) Byte |= 0x80;
Chris Lattner894d75a2010-01-22 23:18:42 +0000183 Asm->OutStreamer.EmitIntValue(Byte, 1, /*addrspace*/0);
Chris Lattnerbb9078a2010-01-22 22:56:55 +0000184 } while (IsMore);
185}
186
Chris Lattner894d75a2010-01-22 23:18:42 +0000187/// EmitULEB128 - emit the specified signed leb128 value.
Bill Wendling3dc9b482010-02-24 23:34:35 +0000188void DwarfPrinter::EmitULEB128(unsigned Value, const char *Desc,
189 unsigned PadTo) const {
Chris Lattnerbb9078a2010-01-22 22:56:55 +0000190 if (Asm->VerboseAsm && Desc)
191 Asm->OutStreamer.AddComment(Desc);
Chris Lattner894d75a2010-01-22 23:18:42 +0000192
Bill Wendling3dc9b482010-02-24 23:34:35 +0000193 if (MAI->hasLEB128() && PadTo == 0) {
Chris Lattner894d75a2010-01-22 23:18:42 +0000194 O << "\t.uleb128\t" << Value;
Chris Lattnerbb9078a2010-01-22 22:56:55 +0000195 Asm->OutStreamer.AddBlankLine();
Chris Lattner894d75a2010-01-22 23:18:42 +0000196 return;
Chris Lattnerbb9078a2010-01-22 22:56:55 +0000197 }
Chris Lattner894d75a2010-01-22 23:18:42 +0000198
Bill Wendling3dc9b482010-02-24 23:34:35 +0000199 // If we don't have .uleb128 or we want to emit padding, emit as .bytes.
Chris Lattner894d75a2010-01-22 23:18:42 +0000200 do {
201 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
202 Value >>= 7;
Bill Wendling3dc9b482010-02-24 23:34:35 +0000203 if (Value || PadTo != 0) Byte |= 0x80;
Chris Lattner894d75a2010-01-22 23:18:42 +0000204 Asm->OutStreamer.EmitIntValue(Byte, 1, /*addrspace*/0);
205 } while (Value);
Bill Wendling3dc9b482010-02-24 23:34:35 +0000206
Bill Wendlingf0bd4cc2010-02-25 00:24:52 +0000207 if (PadTo) {
208 if (PadTo > 1)
209 Asm->OutStreamer.EmitFill(PadTo - 1, 0x80/*fillval*/, 0/*addrspace*/);
210 Asm->OutStreamer.EmitFill(1, 0/*fillval*/, 0/*addrspace*/);
211 }
Chris Lattnerbb9078a2010-01-22 22:56:55 +0000212}
213
214
Bill Wendling88423ee2009-05-15 00:11:17 +0000215/// PrintLabelName - Print label name in form used by Dwarf writer.
216///
Chris Lattnerb98b1bf2010-03-08 22:23:36 +0000217void DwarfPrinter::PrintLabelName(const MCSymbol *Label) const {
218 // FIXME: REMOVE.
219 O << Label->getName();
220}
221
Chris Lattner066c9ac2010-01-22 22:23:57 +0000222void DwarfPrinter::PrintLabelName(const char *Tag, unsigned Number,
223 const char *Suffix) const {
Chris Lattnerb98b1bf2010-03-08 22:23:36 +0000224 // FIXME: REMOVE.
Jim Grosbacha01d3462009-09-01 16:43:35 +0000225 O << MAI->getPrivateGlobalPrefix() << Tag;
Bill Wendling88423ee2009-05-15 00:11:17 +0000226 if (Number) O << Number;
227 O << Suffix;
228}
229
Bill Wendling88423ee2009-05-15 00:11:17 +0000230/// EmitReference - Emit a reference to a label.
231///
Chris Lattner066c9ac2010-01-22 22:23:57 +0000232void DwarfPrinter::EmitReference(const MCSymbol *Sym, bool IsPCRelative,
233 bool Force32Bit) const {
Chris Lattner858431d2010-01-16 18:50:28 +0000234 PrintRelDirective(Force32Bit);
Chris Lattner10b318b2010-01-17 21:43:43 +0000235 O << *Sym;
Chris Lattner858431d2010-01-16 18:50:28 +0000236 if (IsPCRelative) O << "-" << MAI->getPCSymbol();
237}
238
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000239void DwarfPrinter::EmitReference(const MCSymbol *Sym, unsigned Encoding) const {
240 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
241
242 PrintRelDirective(Encoding);
243 O << *TLOF.getSymbolForDwarfReference(Sym, Asm->MMI, Encoding);;
244}
245
246void DwarfPrinter::EmitReference(const GlobalValue *GV, unsigned Encoding)const {
247 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
248
249 PrintRelDirective(Encoding);
250 O << *TLOF.getSymbolForDwarfGlobalReference(GV, Asm->Mang,
Chris Lattner326861c2010-03-08 22:50:36 +0000251 Asm->MMI, Encoding);
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000252}
253
Chris Lattner6a315c32010-01-26 20:20:43 +0000254/// EmitDifference - Emit the difference between two labels. If this assembler
255/// supports .set, we emit a .set of a temporary and then use it in the .word.
Chris Lattnerb98b1bf2010-03-08 22:23:36 +0000256void DwarfPrinter::EmitDifference(const MCSymbol *TagHi, const MCSymbol *TagLo,
257 bool IsSmall) {
258 if (MAI->hasSetDirective()) {
259 // FIXME: switch to OutStreamer.EmitAssignment.
260 O << "\t.set\t";
261 PrintLabelName("set", SetCounter, Flavor);
262 O << ",";
263 PrintLabelName(TagHi);
264 O << "-";
265 PrintLabelName(TagLo);
266 O << "\n";
267
268 PrintRelDirective(IsSmall);
269 PrintLabelName("set", SetCounter, Flavor);
270 ++SetCounter;
271 } else {
272 PrintRelDirective(IsSmall);
273 PrintLabelName(TagHi);
274 O << "-";
275 PrintLabelName(TagLo);
276 }
277}
278
Chris Lattnerb98b1bf2010-03-08 22:23:36 +0000279void DwarfPrinter::EmitSectionOffset(const MCSymbol *Label,
280 const MCSymbol *Section,
Chris Lattner57578762010-03-08 23:23:25 +0000281 bool IsSmall, bool isEH) {
Bill Wendling88423ee2009-05-15 00:11:17 +0000282 bool printAbsolute = false;
283 if (isEH)
Chris Lattner33adcfb2009-08-22 21:43:10 +0000284 printAbsolute = MAI->isAbsoluteEHSectionOffsets();
Bill Wendling88423ee2009-05-15 00:11:17 +0000285 else
Chris Lattner33adcfb2009-08-22 21:43:10 +0000286 printAbsolute = MAI->isAbsoluteDebugSectionOffsets();
Bill Wendling88423ee2009-05-15 00:11:17 +0000287
Chris Lattner57578762010-03-08 23:23:25 +0000288 if (!printAbsolute)
289 return EmitDifference(Label, Section, IsSmall);
290
291 PrintRelDirective(IsSmall, true);
292 PrintLabelName(Label);
Bill Wendling88423ee2009-05-15 00:11:17 +0000293}
294
295/// EmitFrameMoves - Emit frame instructions to describe the layout of the
296/// frame.
Chris Lattner066c9ac2010-01-22 22:23:57 +0000297void DwarfPrinter::EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
298 const std::vector<MachineMove> &Moves,
299 bool isEH) {
Bill Wendling88423ee2009-05-15 00:11:17 +0000300 int stackGrowth =
301 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
302 TargetFrameInfo::StackGrowsUp ?
303 TD->getPointerSize() : -TD->getPointerSize();
304 bool IsLocal = BaseLabel && strcmp(BaseLabel, "label") == 0;
305
306 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
307 const MachineMove &Move = Moves[i];
308 unsigned LabelID = Move.getLabelID();
309
310 if (LabelID) {
311 LabelID = MMI->MappedLabel(LabelID);
312
313 // Throw out move if the label is invalid.
314 if (!LabelID) continue;
315 }
316
317 const MachineLocation &Dst = Move.getDestination();
318 const MachineLocation &Src = Move.getSource();
319
320 // Advance row if new location.
321 if (BaseLabel && LabelID && (BaseLabelID != LabelID || !IsLocal)) {
Chris Lattner245834d2010-01-22 23:40:08 +0000322 EmitCFAByte(dwarf::DW_CFA_advance_loc4);
Chris Lattnereffa8682010-03-08 23:02:59 +0000323 EmitDifference(getDWLabel("label", LabelID),
324 getDWLabel(BaseLabel, BaseLabelID), true);
Chris Lattner0ad9c912010-01-22 22:09:00 +0000325 Asm->O << '\n';
Bill Wendling88423ee2009-05-15 00:11:17 +0000326
327 BaseLabelID = LabelID;
328 BaseLabel = "label";
329 IsLocal = true;
330 }
331
332 // If advancing cfa.
333 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
334 if (!Src.isReg()) {
335 if (Src.getReg() == MachineLocation::VirtualFP) {
Chris Lattner245834d2010-01-22 23:40:08 +0000336 EmitCFAByte(dwarf::DW_CFA_def_cfa_offset);
Bill Wendling88423ee2009-05-15 00:11:17 +0000337 } else {
Chris Lattner245834d2010-01-22 23:40:08 +0000338 EmitCFAByte(dwarf::DW_CFA_def_cfa);
Chris Lattner894d75a2010-01-22 23:18:42 +0000339 EmitULEB128(RI->getDwarfRegNum(Src.getReg(), isEH), "Register");
Bill Wendling88423ee2009-05-15 00:11:17 +0000340 }
341
342 int Offset = -Src.getOffset();
Chris Lattner894d75a2010-01-22 23:18:42 +0000343 EmitULEB128(Offset, "Offset");
Bill Wendling88423ee2009-05-15 00:11:17 +0000344 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000345 llvm_unreachable("Machine move not supported yet.");
Bill Wendling88423ee2009-05-15 00:11:17 +0000346 }
347 } else if (Src.isReg() &&
348 Src.getReg() == MachineLocation::VirtualFP) {
349 if (Dst.isReg()) {
Chris Lattner245834d2010-01-22 23:40:08 +0000350 EmitCFAByte(dwarf::DW_CFA_def_cfa_register);
Chris Lattner894d75a2010-01-22 23:18:42 +0000351 EmitULEB128(RI->getDwarfRegNum(Dst.getReg(), isEH), "Register");
Bill Wendling88423ee2009-05-15 00:11:17 +0000352 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000353 llvm_unreachable("Machine move not supported yet.");
Bill Wendling88423ee2009-05-15 00:11:17 +0000354 }
355 } else {
356 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), isEH);
357 int Offset = Dst.getOffset() / stackGrowth;
358
359 if (Offset < 0) {
Chris Lattner245834d2010-01-22 23:40:08 +0000360 EmitCFAByte(dwarf::DW_CFA_offset_extended_sf);
Chris Lattner894d75a2010-01-22 23:18:42 +0000361 EmitULEB128(Reg, "Reg");
Chris Lattnerbb9078a2010-01-22 22:56:55 +0000362 EmitSLEB128(Offset, "Offset");
Bill Wendling88423ee2009-05-15 00:11:17 +0000363 } else if (Reg < 64) {
Chris Lattner245834d2010-01-22 23:40:08 +0000364 EmitCFAByte(dwarf::DW_CFA_offset + Reg);
Chris Lattner894d75a2010-01-22 23:18:42 +0000365 EmitULEB128(Offset, "Offset");
Bill Wendling88423ee2009-05-15 00:11:17 +0000366 } else {
Chris Lattner245834d2010-01-22 23:40:08 +0000367 EmitCFAByte(dwarf::DW_CFA_offset_extended);
Chris Lattner894d75a2010-01-22 23:18:42 +0000368 EmitULEB128(Reg, "Reg");
369 EmitULEB128(Offset, "Offset");
Bill Wendling88423ee2009-05-15 00:11:17 +0000370 }
371 }
372 }
373}