blob: a40bd54f8cfbc55fe33f9c0f24b23b1fda332041 [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 Lattnerc3421bb2010-03-09 00:00:15 +000034DwarfPrinter::DwarfPrinter(raw_ostream &OS, AsmPrinter *A, const MCAsmInfo *T)
Chris Lattner33adcfb2009-08-22 21:43:10 +000035: O(OS), Asm(A), MAI(T), TD(Asm->TM.getTargetData()),
Bill Wendling88423ee2009-05-15 00:11:17 +000036 RI(Asm->TM.getRegisterInfo()), M(NULL), MF(NULL), MMI(NULL),
Chris Lattnerc3421bb2010-03-09 00:00:15 +000037 SubprogramCount(0) {}
Bill Wendling88423ee2009-05-15 00:11:17 +000038
Chris Lattnerb98b1bf2010-03-08 22:23:36 +000039
40/// getDWLabel - Return the MCSymbol corresponding to the assembler temporary
41/// label with the specified stem and unique ID.
42MCSymbol *DwarfPrinter::getDWLabel(const char *Name, unsigned ID) const {
43 // FIXME: REMOVE this. However, there is stuff in EH that passes counters in
44 // here that can be zero.
45
46 //assert(ID && "Should use getTempLabel if no ID");
47 if (ID == 0) return getTempLabel(Name);
48 return Asm->OutContext.GetOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix())
49 + Twine(Name) + Twine(ID));
50}
51
52/// getTempLabel - Return the MCSymbol corresponding to the assembler temporary
53/// label with the specified name.
54MCSymbol *DwarfPrinter::getTempLabel(const char *Name) const {
55 return Asm->OutContext.GetOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix())
56 + Name);
57}
58
59
Anton Korobeynikov9184b252010-02-15 22:35:59 +000060/// SizeOfEncodedValue - Return the size of the encoding in bytes.
61unsigned DwarfPrinter::SizeOfEncodedValue(unsigned Encoding) const {
62 if (Encoding == dwarf::DW_EH_PE_omit)
63 return 0;
64
65 switch (Encoding & 0x07) {
66 case dwarf::DW_EH_PE_absptr:
67 return TD->getPointerSize();
68 case dwarf::DW_EH_PE_udata2:
69 return 2;
70 case dwarf::DW_EH_PE_udata4:
71 return 4;
72 case dwarf::DW_EH_PE_udata8:
73 return 8;
74 }
75
76 assert(0 && "Invalid encoded value.");
77 return 0;
78}
79
Chris Lattner066c9ac2010-01-22 22:23:57 +000080void DwarfPrinter::PrintRelDirective(bool Force32Bit, bool isInSection) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +000081 if (isInSection && MAI->getDwarfSectionOffsetDirective())
82 O << MAI->getDwarfSectionOffsetDirective();
Bill Wendling88423ee2009-05-15 00:11:17 +000083 else if (Force32Bit || TD->getPointerSize() == sizeof(int32_t))
Chris Lattner33adcfb2009-08-22 21:43:10 +000084 O << MAI->getData32bitsDirective();
Bill Wendling88423ee2009-05-15 00:11:17 +000085 else
Chris Lattner33adcfb2009-08-22 21:43:10 +000086 O << MAI->getData64bitsDirective();
Bill Wendling88423ee2009-05-15 00:11:17 +000087}
88
Anton Korobeynikov9184b252010-02-15 22:35:59 +000089void DwarfPrinter::PrintRelDirective(unsigned Encoding) const {
90 unsigned Size = SizeOfEncodedValue(Encoding);
91 assert((Size == 4 || Size == 8) && "Do not support other types or rels!");
92
93 O << (Size == 4 ?
94 MAI->getData32bitsDirective() : MAI->getData64bitsDirective());
95}
96
Chris Lattnerfaca5492010-01-22 23:47:11 +000097/// EOL - Print a newline character to asm stream. If a comment is present
98/// then it will be printed first. Comments should not contain '\n'.
99void DwarfPrinter::EOL(const Twine &Comment) const {
100 if (Asm->VerboseAsm && !Comment.isTriviallyEmpty()) {
101 Asm->O.PadToColumn(MAI->getCommentColumn());
102 Asm->O << Asm->MAI->getCommentString() << ' ' << Comment;
103 }
104 Asm->O << '\n';
105}
106
Chris Lattnerf61ed8e2010-01-22 22:38:16 +0000107static const char *DecodeDWARFEncoding(unsigned Encoding) {
108 switch (Encoding) {
109 case dwarf::DW_EH_PE_absptr: return "absptr";
110 case dwarf::DW_EH_PE_omit: return "omit";
111 case dwarf::DW_EH_PE_pcrel: return "pcrel";
112 case dwarf::DW_EH_PE_udata4: return "udata4";
113 case dwarf::DW_EH_PE_udata8: return "udata8";
114 case dwarf::DW_EH_PE_sdata4: return "sdata4";
115 case dwarf::DW_EH_PE_sdata8: return "sdata8";
116 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4: return "pcrel udata4";
117 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4: return "pcrel sdata4";
118 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8: return "pcrel udata8";
119 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8: return "pcrel sdata8";
120 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata4:
121 return "indirect pcrel udata4";
122 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata4:
123 return "indirect pcrel sdata4";
124 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata8:
125 return "indirect pcrel udata8";
126 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata8:
127 return "indirect pcrel sdata8";
128 }
129
130 return "<unknown encoding>";
131}
132
133/// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
134/// encoding. If verbose assembly output is enabled, we output comments
135/// describing the encoding. Desc is an optional string saying what the
136/// encoding is specifying (e.g. "LSDA").
137void DwarfPrinter::EmitEncodingByte(unsigned Val, const char *Desc) {
138 if (Asm->VerboseAsm) {
139 if (Desc != 0)
140 Asm->OutStreamer.AddComment(Twine(Desc)+" Encoding = " +
141 Twine(DecodeDWARFEncoding(Val)));
142 else
143 Asm->OutStreamer.AddComment(Twine("Encoding = ") +
144 DecodeDWARFEncoding(Val));
145 }
146
147 Asm->OutStreamer.EmitIntValue(Val, 1, 0/*addrspace*/);
148}
149
Chris Lattner245834d2010-01-22 23:40:08 +0000150/// EmitCFAByte - Emit a .byte 42 directive for a DW_CFA_xxx value.
151void DwarfPrinter::EmitCFAByte(unsigned Val) {
152 if (Asm->VerboseAsm) {
153 if (Val >= dwarf::DW_CFA_offset && Val < dwarf::DW_CFA_offset+64)
154 Asm->OutStreamer.AddComment("DW_CFA_offset + Reg (" +
155 Twine(Val-dwarf::DW_CFA_offset) + ")");
156 else
157 Asm->OutStreamer.AddComment(dwarf::CallFrameString(Val));
158 }
159 Asm->OutStreamer.EmitIntValue(Val, 1, 0/*addrspace*/);
160}
161
Chris Lattner894d75a2010-01-22 23:18:42 +0000162/// EmitSLEB128 - emit the specified signed leb128 value.
163void DwarfPrinter::EmitSLEB128(int Value, const char *Desc) const {
164 if (Asm->VerboseAsm && Desc)
165 Asm->OutStreamer.AddComment(Desc);
166
167 if (MAI->hasLEB128()) {
168 O << "\t.sleb128\t" << Value;
169 Asm->OutStreamer.AddBlankLine();
170 return;
171 }
172
173 // If we don't have .sleb128, emit as .bytes.
Chris Lattnerbb9078a2010-01-22 22:56:55 +0000174 int Sign = Value >> (8 * sizeof(Value) - 1);
175 bool IsMore;
176
177 do {
178 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
179 Value >>= 7;
180 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
181 if (IsMore) Byte |= 0x80;
Chris Lattner894d75a2010-01-22 23:18:42 +0000182 Asm->OutStreamer.EmitIntValue(Byte, 1, /*addrspace*/0);
Chris Lattnerbb9078a2010-01-22 22:56:55 +0000183 } while (IsMore);
184}
185
Chris Lattner894d75a2010-01-22 23:18:42 +0000186/// EmitULEB128 - emit the specified signed leb128 value.
Bill Wendling3dc9b482010-02-24 23:34:35 +0000187void DwarfPrinter::EmitULEB128(unsigned Value, const char *Desc,
188 unsigned PadTo) const {
Chris Lattnerbb9078a2010-01-22 22:56:55 +0000189 if (Asm->VerboseAsm && Desc)
190 Asm->OutStreamer.AddComment(Desc);
Chris Lattner894d75a2010-01-22 23:18:42 +0000191
Bill Wendling3dc9b482010-02-24 23:34:35 +0000192 if (MAI->hasLEB128() && PadTo == 0) {
Chris Lattner894d75a2010-01-22 23:18:42 +0000193 O << "\t.uleb128\t" << Value;
Chris Lattnerbb9078a2010-01-22 22:56:55 +0000194 Asm->OutStreamer.AddBlankLine();
Chris Lattner894d75a2010-01-22 23:18:42 +0000195 return;
Chris Lattnerbb9078a2010-01-22 22:56:55 +0000196 }
Chris Lattner894d75a2010-01-22 23:18:42 +0000197
Bill Wendling3dc9b482010-02-24 23:34:35 +0000198 // If we don't have .uleb128 or we want to emit padding, emit as .bytes.
Chris Lattner894d75a2010-01-22 23:18:42 +0000199 do {
200 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
201 Value >>= 7;
Bill Wendling3dc9b482010-02-24 23:34:35 +0000202 if (Value || PadTo != 0) Byte |= 0x80;
Chris Lattner894d75a2010-01-22 23:18:42 +0000203 Asm->OutStreamer.EmitIntValue(Byte, 1, /*addrspace*/0);
204 } while (Value);
Bill Wendling3dc9b482010-02-24 23:34:35 +0000205
Bill Wendlingf0bd4cc2010-02-25 00:24:52 +0000206 if (PadTo) {
207 if (PadTo > 1)
208 Asm->OutStreamer.EmitFill(PadTo - 1, 0x80/*fillval*/, 0/*addrspace*/);
209 Asm->OutStreamer.EmitFill(1, 0/*fillval*/, 0/*addrspace*/);
210 }
Chris Lattnerbb9078a2010-01-22 22:56:55 +0000211}
212
213
Bill Wendling88423ee2009-05-15 00:11:17 +0000214/// PrintLabelName - Print label name in form used by Dwarf writer.
215///
Chris Lattnerb98b1bf2010-03-08 22:23:36 +0000216void DwarfPrinter::PrintLabelName(const MCSymbol *Label) const {
217 // FIXME: REMOVE.
218 O << Label->getName();
219}
220
Bill Wendling88423ee2009-05-15 00:11:17 +0000221/// EmitReference - Emit a reference to a label.
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 MCSymbol *Sym, unsigned Encoding) const {
231 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
232
233 PrintRelDirective(Encoding);
234 O << *TLOF.getSymbolForDwarfReference(Sym, Asm->MMI, Encoding);;
235}
236
Chris Lattner0d50c762010-03-08 23:58:37 +0000237void DwarfPrinter::EmitReference(const GlobalValue *GV, unsigned Encoding)const{
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000238 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
239
240 PrintRelDirective(Encoding);
241 O << *TLOF.getSymbolForDwarfGlobalReference(GV, Asm->Mang,
Chris Lattner326861c2010-03-08 22:50:36 +0000242 Asm->MMI, Encoding);
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000243}
244
Chris Lattner6a315c32010-01-26 20:20:43 +0000245/// EmitDifference - Emit the difference between two labels. If this assembler
246/// supports .set, we emit a .set of a temporary and then use it in the .word.
Chris Lattnerb98b1bf2010-03-08 22:23:36 +0000247void DwarfPrinter::EmitDifference(const MCSymbol *TagHi, const MCSymbol *TagLo,
248 bool IsSmall) {
Chris Lattner0d50c762010-03-08 23:58:37 +0000249 unsigned Size = IsSmall ? 4 : TD->getPointerSize();
250 Asm->EmitLabelDifference(TagHi, TagLo, Size);
Chris Lattnerb98b1bf2010-03-08 22:23:36 +0000251}
252
Chris Lattnerb98b1bf2010-03-08 22:23:36 +0000253void DwarfPrinter::EmitSectionOffset(const MCSymbol *Label,
254 const MCSymbol *Section,
Chris Lattner57578762010-03-08 23:23:25 +0000255 bool IsSmall, bool isEH) {
Bill Wendling88423ee2009-05-15 00:11:17 +0000256 bool printAbsolute = false;
257 if (isEH)
Chris Lattner33adcfb2009-08-22 21:43:10 +0000258 printAbsolute = MAI->isAbsoluteEHSectionOffsets();
Bill Wendling88423ee2009-05-15 00:11:17 +0000259 else
Chris Lattner33adcfb2009-08-22 21:43:10 +0000260 printAbsolute = MAI->isAbsoluteDebugSectionOffsets();
Bill Wendling88423ee2009-05-15 00:11:17 +0000261
Chris Lattner57578762010-03-08 23:23:25 +0000262 if (!printAbsolute)
263 return EmitDifference(Label, Section, IsSmall);
264
265 PrintRelDirective(IsSmall, true);
266 PrintLabelName(Label);
Bill Wendling88423ee2009-05-15 00:11:17 +0000267}
268
269/// EmitFrameMoves - Emit frame instructions to describe the layout of the
270/// frame.
Chris Lattner066c9ac2010-01-22 22:23:57 +0000271void DwarfPrinter::EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
272 const std::vector<MachineMove> &Moves,
273 bool isEH) {
Bill Wendling88423ee2009-05-15 00:11:17 +0000274 int stackGrowth =
275 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
276 TargetFrameInfo::StackGrowsUp ?
277 TD->getPointerSize() : -TD->getPointerSize();
278 bool IsLocal = BaseLabel && strcmp(BaseLabel, "label") == 0;
279
280 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
281 const MachineMove &Move = Moves[i];
282 unsigned LabelID = Move.getLabelID();
283
284 if (LabelID) {
285 LabelID = MMI->MappedLabel(LabelID);
286
287 // Throw out move if the label is invalid.
288 if (!LabelID) continue;
289 }
290
291 const MachineLocation &Dst = Move.getDestination();
292 const MachineLocation &Src = Move.getSource();
293
294 // Advance row if new location.
295 if (BaseLabel && LabelID && (BaseLabelID != LabelID || !IsLocal)) {
Chris Lattner245834d2010-01-22 23:40:08 +0000296 EmitCFAByte(dwarf::DW_CFA_advance_loc4);
Chris Lattnereffa8682010-03-08 23:02:59 +0000297 EmitDifference(getDWLabel("label", LabelID),
298 getDWLabel(BaseLabel, BaseLabelID), true);
Chris Lattner0ad9c912010-01-22 22:09:00 +0000299 Asm->O << '\n';
Bill Wendling88423ee2009-05-15 00:11:17 +0000300
301 BaseLabelID = LabelID;
302 BaseLabel = "label";
303 IsLocal = true;
304 }
305
306 // If advancing cfa.
307 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
308 if (!Src.isReg()) {
309 if (Src.getReg() == MachineLocation::VirtualFP) {
Chris Lattner245834d2010-01-22 23:40:08 +0000310 EmitCFAByte(dwarf::DW_CFA_def_cfa_offset);
Bill Wendling88423ee2009-05-15 00:11:17 +0000311 } else {
Chris Lattner245834d2010-01-22 23:40:08 +0000312 EmitCFAByte(dwarf::DW_CFA_def_cfa);
Chris Lattner894d75a2010-01-22 23:18:42 +0000313 EmitULEB128(RI->getDwarfRegNum(Src.getReg(), isEH), "Register");
Bill Wendling88423ee2009-05-15 00:11:17 +0000314 }
315
316 int Offset = -Src.getOffset();
Chris Lattner894d75a2010-01-22 23:18:42 +0000317 EmitULEB128(Offset, "Offset");
Bill Wendling88423ee2009-05-15 00:11:17 +0000318 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000319 llvm_unreachable("Machine move not supported yet.");
Bill Wendling88423ee2009-05-15 00:11:17 +0000320 }
321 } else if (Src.isReg() &&
322 Src.getReg() == MachineLocation::VirtualFP) {
323 if (Dst.isReg()) {
Chris Lattner245834d2010-01-22 23:40:08 +0000324 EmitCFAByte(dwarf::DW_CFA_def_cfa_register);
Chris Lattner894d75a2010-01-22 23:18:42 +0000325 EmitULEB128(RI->getDwarfRegNum(Dst.getReg(), isEH), "Register");
Bill Wendling88423ee2009-05-15 00:11:17 +0000326 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000327 llvm_unreachable("Machine move not supported yet.");
Bill Wendling88423ee2009-05-15 00:11:17 +0000328 }
329 } else {
330 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), isEH);
331 int Offset = Dst.getOffset() / stackGrowth;
332
333 if (Offset < 0) {
Chris Lattner245834d2010-01-22 23:40:08 +0000334 EmitCFAByte(dwarf::DW_CFA_offset_extended_sf);
Chris Lattner894d75a2010-01-22 23:18:42 +0000335 EmitULEB128(Reg, "Reg");
Chris Lattnerbb9078a2010-01-22 22:56:55 +0000336 EmitSLEB128(Offset, "Offset");
Bill Wendling88423ee2009-05-15 00:11:17 +0000337 } else if (Reg < 64) {
Chris Lattner245834d2010-01-22 23:40:08 +0000338 EmitCFAByte(dwarf::DW_CFA_offset + Reg);
Chris Lattner894d75a2010-01-22 23:18:42 +0000339 EmitULEB128(Offset, "Offset");
Bill Wendling88423ee2009-05-15 00:11:17 +0000340 } else {
Chris Lattner245834d2010-01-22 23:40:08 +0000341 EmitCFAByte(dwarf::DW_CFA_offset_extended);
Chris Lattner894d75a2010-01-22 23:18:42 +0000342 EmitULEB128(Reg, "Reg");
343 EmitULEB128(Offset, "Offset");
Bill Wendling88423ee2009-05-15 00:11:17 +0000344 }
345 }
346 }
347}