blob: b8f9ba193c571a6f256673dd980afd2eb884b1b1 [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 Lattneref6b14f2010-03-09 00:17:58 +000080void DwarfPrinter::PrintRelDirective(bool Force32Bit) const {
81 if (Force32Bit || TD->getPointerSize() == sizeof(int32_t))
Chris Lattner33adcfb2009-08-22 21:43:10 +000082 O << MAI->getData32bitsDirective();
Bill Wendling88423ee2009-05-15 00:11:17 +000083 else
Chris Lattner33adcfb2009-08-22 21:43:10 +000084 O << MAI->getData64bitsDirective();
Bill Wendling88423ee2009-05-15 00:11:17 +000085}
86
Anton Korobeynikov9184b252010-02-15 22:35:59 +000087void DwarfPrinter::PrintRelDirective(unsigned Encoding) const {
88 unsigned Size = SizeOfEncodedValue(Encoding);
89 assert((Size == 4 || Size == 8) && "Do not support other types or rels!");
90
91 O << (Size == 4 ?
92 MAI->getData32bitsDirective() : MAI->getData64bitsDirective());
93}
94
Chris Lattnerf61ed8e2010-01-22 22:38:16 +000095static const char *DecodeDWARFEncoding(unsigned Encoding) {
96 switch (Encoding) {
97 case dwarf::DW_EH_PE_absptr: return "absptr";
98 case dwarf::DW_EH_PE_omit: return "omit";
99 case dwarf::DW_EH_PE_pcrel: return "pcrel";
100 case dwarf::DW_EH_PE_udata4: return "udata4";
101 case dwarf::DW_EH_PE_udata8: return "udata8";
102 case dwarf::DW_EH_PE_sdata4: return "sdata4";
103 case dwarf::DW_EH_PE_sdata8: return "sdata8";
104 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4: return "pcrel udata4";
105 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4: return "pcrel sdata4";
106 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8: return "pcrel udata8";
107 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8: return "pcrel sdata8";
108 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata4:
109 return "indirect pcrel udata4";
110 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata4:
111 return "indirect pcrel sdata4";
112 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata8:
113 return "indirect pcrel udata8";
114 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata8:
115 return "indirect pcrel sdata8";
116 }
117
118 return "<unknown encoding>";
119}
120
121/// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
122/// encoding. If verbose assembly output is enabled, we output comments
123/// describing the encoding. Desc is an optional string saying what the
124/// encoding is specifying (e.g. "LSDA").
125void DwarfPrinter::EmitEncodingByte(unsigned Val, const char *Desc) {
126 if (Asm->VerboseAsm) {
127 if (Desc != 0)
128 Asm->OutStreamer.AddComment(Twine(Desc)+" Encoding = " +
129 Twine(DecodeDWARFEncoding(Val)));
130 else
131 Asm->OutStreamer.AddComment(Twine("Encoding = ") +
132 DecodeDWARFEncoding(Val));
133 }
134
135 Asm->OutStreamer.EmitIntValue(Val, 1, 0/*addrspace*/);
136}
137
Chris Lattner245834d2010-01-22 23:40:08 +0000138/// EmitCFAByte - Emit a .byte 42 directive for a DW_CFA_xxx value.
139void DwarfPrinter::EmitCFAByte(unsigned Val) {
140 if (Asm->VerboseAsm) {
141 if (Val >= dwarf::DW_CFA_offset && Val < dwarf::DW_CFA_offset+64)
142 Asm->OutStreamer.AddComment("DW_CFA_offset + Reg (" +
143 Twine(Val-dwarf::DW_CFA_offset) + ")");
144 else
145 Asm->OutStreamer.AddComment(dwarf::CallFrameString(Val));
146 }
147 Asm->OutStreamer.EmitIntValue(Val, 1, 0/*addrspace*/);
148}
149
Chris Lattner894d75a2010-01-22 23:18:42 +0000150/// EmitSLEB128 - emit the specified signed leb128 value.
151void DwarfPrinter::EmitSLEB128(int Value, const char *Desc) const {
152 if (Asm->VerboseAsm && Desc)
153 Asm->OutStreamer.AddComment(Desc);
154
155 if (MAI->hasLEB128()) {
156 O << "\t.sleb128\t" << Value;
157 Asm->OutStreamer.AddBlankLine();
158 return;
159 }
160
161 // If we don't have .sleb128, emit as .bytes.
Chris Lattnerbb9078a2010-01-22 22:56:55 +0000162 int Sign = Value >> (8 * sizeof(Value) - 1);
163 bool IsMore;
164
165 do {
166 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
167 Value >>= 7;
168 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
169 if (IsMore) Byte |= 0x80;
Chris Lattner894d75a2010-01-22 23:18:42 +0000170 Asm->OutStreamer.EmitIntValue(Byte, 1, /*addrspace*/0);
Chris Lattnerbb9078a2010-01-22 22:56:55 +0000171 } while (IsMore);
172}
173
Chris Lattner894d75a2010-01-22 23:18:42 +0000174/// EmitULEB128 - emit the specified signed leb128 value.
Bill Wendling3dc9b482010-02-24 23:34:35 +0000175void DwarfPrinter::EmitULEB128(unsigned Value, const char *Desc,
176 unsigned PadTo) const {
Chris Lattnerbb9078a2010-01-22 22:56:55 +0000177 if (Asm->VerboseAsm && Desc)
178 Asm->OutStreamer.AddComment(Desc);
Chris Lattner894d75a2010-01-22 23:18:42 +0000179
Bill Wendling3dc9b482010-02-24 23:34:35 +0000180 if (MAI->hasLEB128() && PadTo == 0) {
Chris Lattner894d75a2010-01-22 23:18:42 +0000181 O << "\t.uleb128\t" << Value;
Chris Lattnerbb9078a2010-01-22 22:56:55 +0000182 Asm->OutStreamer.AddBlankLine();
Chris Lattner894d75a2010-01-22 23:18:42 +0000183 return;
Chris Lattnerbb9078a2010-01-22 22:56:55 +0000184 }
Chris Lattner894d75a2010-01-22 23:18:42 +0000185
Bill Wendling3dc9b482010-02-24 23:34:35 +0000186 // If we don't have .uleb128 or we want to emit padding, emit as .bytes.
Chris Lattner894d75a2010-01-22 23:18:42 +0000187 do {
188 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
189 Value >>= 7;
Bill Wendling3dc9b482010-02-24 23:34:35 +0000190 if (Value || PadTo != 0) Byte |= 0x80;
Chris Lattner894d75a2010-01-22 23:18:42 +0000191 Asm->OutStreamer.EmitIntValue(Byte, 1, /*addrspace*/0);
192 } while (Value);
Bill Wendling3dc9b482010-02-24 23:34:35 +0000193
Bill Wendlingf0bd4cc2010-02-25 00:24:52 +0000194 if (PadTo) {
195 if (PadTo > 1)
196 Asm->OutStreamer.EmitFill(PadTo - 1, 0x80/*fillval*/, 0/*addrspace*/);
197 Asm->OutStreamer.EmitFill(1, 0/*fillval*/, 0/*addrspace*/);
198 }
Chris Lattnerbb9078a2010-01-22 22:56:55 +0000199}
200
201
Bill Wendling88423ee2009-05-15 00:11:17 +0000202/// EmitReference - Emit a reference to a label.
203///
Chris Lattner066c9ac2010-01-22 22:23:57 +0000204void DwarfPrinter::EmitReference(const MCSymbol *Sym, bool IsPCRelative,
205 bool Force32Bit) const {
Chris Lattnerfcdae492010-03-09 23:19:15 +0000206 unsigned Size = Force32Bit ? 4 : TD->getPointerSize();
207
208 if (!IsPCRelative) {
209 Asm->OutStreamer.EmitSymbolValue(Sym, Size, 0/*AddrSpace*/);
210 return;
211 }
212
213 // FIXME: Need an MCExpr for ".".
Chris Lattner858431d2010-01-16 18:50:28 +0000214 PrintRelDirective(Force32Bit);
Chris Lattner10b318b2010-01-17 21:43:43 +0000215 O << *Sym;
Chris Lattner858431d2010-01-16 18:50:28 +0000216 if (IsPCRelative) O << "-" << MAI->getPCSymbol();
217}
218
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000219void DwarfPrinter::EmitReference(const MCSymbol *Sym, unsigned Encoding) const {
220 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
221
222 PrintRelDirective(Encoding);
Chris Lattnerb9dc5f72010-03-09 00:26:09 +0000223 O << *TLOF.getSymbolForDwarfReference(Sym, Asm->MMI, Encoding);
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000224}
225
Chris Lattner0d50c762010-03-08 23:58:37 +0000226void DwarfPrinter::EmitReference(const GlobalValue *GV, unsigned Encoding)const{
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000227 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
228
229 PrintRelDirective(Encoding);
230 O << *TLOF.getSymbolForDwarfGlobalReference(GV, Asm->Mang,
Chris Lattner326861c2010-03-08 22:50:36 +0000231 Asm->MMI, Encoding);
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000232}
233
Chris Lattner6a315c32010-01-26 20:20:43 +0000234/// EmitDifference - Emit the difference between two labels. If this assembler
235/// supports .set, we emit a .set of a temporary and then use it in the .word.
Chris Lattnerb98b1bf2010-03-08 22:23:36 +0000236void DwarfPrinter::EmitDifference(const MCSymbol *TagHi, const MCSymbol *TagLo,
237 bool IsSmall) {
Chris Lattner0d50c762010-03-08 23:58:37 +0000238 unsigned Size = IsSmall ? 4 : TD->getPointerSize();
239 Asm->EmitLabelDifference(TagHi, TagLo, Size);
Chris Lattnerb98b1bf2010-03-08 22:23:36 +0000240}
241
Chris Lattnerb98b1bf2010-03-08 22:23:36 +0000242void DwarfPrinter::EmitSectionOffset(const MCSymbol *Label,
243 const MCSymbol *Section,
Chris Lattner57578762010-03-08 23:23:25 +0000244 bool IsSmall, bool isEH) {
Chris Lattneref6b14f2010-03-09 00:17:58 +0000245 bool isAbsolute;
Bill Wendling88423ee2009-05-15 00:11:17 +0000246 if (isEH)
Chris Lattneref6b14f2010-03-09 00:17:58 +0000247 isAbsolute = MAI->isAbsoluteEHSectionOffsets();
Bill Wendling88423ee2009-05-15 00:11:17 +0000248 else
Chris Lattneref6b14f2010-03-09 00:17:58 +0000249 isAbsolute = MAI->isAbsoluteDebugSectionOffsets();
Bill Wendling88423ee2009-05-15 00:11:17 +0000250
Chris Lattneref6b14f2010-03-09 00:17:58 +0000251 if (!isAbsolute)
Chris Lattner57578762010-03-08 23:23:25 +0000252 return EmitDifference(Label, Section, IsSmall);
253
Chris Lattneref6b14f2010-03-09 00:17:58 +0000254 // On COFF targets, we have to emit the weird .secrel32 directive.
255 if (const char *SecOffDir = MAI->getDwarfSectionOffsetDirective())
256 O << SecOffDir << Label->getName();
257 else {
258 unsigned Size = IsSmall ? 4 : TD->getPointerSize();
Chris Lattner6cde3e62010-03-09 00:39:24 +0000259 Asm->OutStreamer.EmitSymbolValue(Label, Size, 0/*AddrSpace*/);
Chris Lattneref6b14f2010-03-09 00:17:58 +0000260 }
Bill Wendling88423ee2009-05-15 00:11:17 +0000261}
262
263/// EmitFrameMoves - Emit frame instructions to describe the layout of the
264/// frame.
Chris Lattner066c9ac2010-01-22 22:23:57 +0000265void DwarfPrinter::EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
266 const std::vector<MachineMove> &Moves,
267 bool isEH) {
Bill Wendling88423ee2009-05-15 00:11:17 +0000268 int stackGrowth =
269 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
270 TargetFrameInfo::StackGrowsUp ?
271 TD->getPointerSize() : -TD->getPointerSize();
272 bool IsLocal = BaseLabel && strcmp(BaseLabel, "label") == 0;
273
274 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
275 const MachineMove &Move = Moves[i];
276 unsigned LabelID = Move.getLabelID();
277
Chris Lattnera34ec2292010-03-09 01:51:43 +0000278 // Throw out move if the label is invalid.
279 if (LabelID && MMI->isLabelDeleted(LabelID))
280 continue;
Bill Wendling88423ee2009-05-15 00:11:17 +0000281
282 const MachineLocation &Dst = Move.getDestination();
283 const MachineLocation &Src = Move.getSource();
284
285 // Advance row if new location.
286 if (BaseLabel && LabelID && (BaseLabelID != LabelID || !IsLocal)) {
Chris Lattner245834d2010-01-22 23:40:08 +0000287 EmitCFAByte(dwarf::DW_CFA_advance_loc4);
Chris Lattnereffa8682010-03-08 23:02:59 +0000288 EmitDifference(getDWLabel("label", LabelID),
289 getDWLabel(BaseLabel, BaseLabelID), true);
Bill Wendling88423ee2009-05-15 00:11:17 +0000290 BaseLabelID = LabelID;
291 BaseLabel = "label";
292 IsLocal = true;
293 }
294
295 // If advancing cfa.
296 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
297 if (!Src.isReg()) {
298 if (Src.getReg() == MachineLocation::VirtualFP) {
Chris Lattner245834d2010-01-22 23:40:08 +0000299 EmitCFAByte(dwarf::DW_CFA_def_cfa_offset);
Bill Wendling88423ee2009-05-15 00:11:17 +0000300 } else {
Chris Lattner245834d2010-01-22 23:40:08 +0000301 EmitCFAByte(dwarf::DW_CFA_def_cfa);
Chris Lattner894d75a2010-01-22 23:18:42 +0000302 EmitULEB128(RI->getDwarfRegNum(Src.getReg(), isEH), "Register");
Bill Wendling88423ee2009-05-15 00:11:17 +0000303 }
304
305 int Offset = -Src.getOffset();
Chris Lattner894d75a2010-01-22 23:18:42 +0000306 EmitULEB128(Offset, "Offset");
Bill Wendling88423ee2009-05-15 00:11:17 +0000307 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000308 llvm_unreachable("Machine move not supported yet.");
Bill Wendling88423ee2009-05-15 00:11:17 +0000309 }
310 } else if (Src.isReg() &&
311 Src.getReg() == MachineLocation::VirtualFP) {
312 if (Dst.isReg()) {
Chris Lattner245834d2010-01-22 23:40:08 +0000313 EmitCFAByte(dwarf::DW_CFA_def_cfa_register);
Chris Lattner894d75a2010-01-22 23:18:42 +0000314 EmitULEB128(RI->getDwarfRegNum(Dst.getReg(), isEH), "Register");
Bill Wendling88423ee2009-05-15 00:11:17 +0000315 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000316 llvm_unreachable("Machine move not supported yet.");
Bill Wendling88423ee2009-05-15 00:11:17 +0000317 }
318 } else {
319 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), isEH);
320 int Offset = Dst.getOffset() / stackGrowth;
321
322 if (Offset < 0) {
Chris Lattner245834d2010-01-22 23:40:08 +0000323 EmitCFAByte(dwarf::DW_CFA_offset_extended_sf);
Chris Lattner894d75a2010-01-22 23:18:42 +0000324 EmitULEB128(Reg, "Reg");
Chris Lattnerbb9078a2010-01-22 22:56:55 +0000325 EmitSLEB128(Offset, "Offset");
Bill Wendling88423ee2009-05-15 00:11:17 +0000326 } else if (Reg < 64) {
Chris Lattner245834d2010-01-22 23:40:08 +0000327 EmitCFAByte(dwarf::DW_CFA_offset + Reg);
Chris Lattner894d75a2010-01-22 23:18:42 +0000328 EmitULEB128(Offset, "Offset");
Bill Wendling88423ee2009-05-15 00:11:17 +0000329 } else {
Chris Lattner245834d2010-01-22 23:40:08 +0000330 EmitCFAByte(dwarf::DW_CFA_offset_extended);
Chris Lattner894d75a2010-01-22 23:18:42 +0000331 EmitULEB128(Reg, "Reg");
332 EmitULEB128(Offset, "Offset");
Bill Wendling88423ee2009-05-15 00:11:17 +0000333 }
334 }
335 }
336}