blob: 2ae7d476b6a8e54452cd5bb5d0620b277bda2f89 [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 Lattner9d1c1ad2010-04-04 18:06:11 +000029#include "llvm/Target/TargetMachine.h"
Chris Lattner23132b12009-08-24 03:52:50 +000030#include "llvm/Support/Dwarf.h"
31#include "llvm/Support/ErrorHandling.h"
Anton Korobeynikov9184b252010-02-15 22:35:59 +000032#include "llvm/ADT/SmallString.h"
Bill Wendling88423ee2009-05-15 00:11:17 +000033using namespace llvm;
34
Chris Lattner75f50722010-04-04 07:48:20 +000035DwarfPrinter::DwarfPrinter(AsmPrinter *A)
36: Asm(A), MAI(A->MAI), TD(Asm->TM.getTargetData()),
Bill Wendling88423ee2009-05-15 00:11:17 +000037 RI(Asm->TM.getRegisterInfo()), M(NULL), MF(NULL), MMI(NULL),
Chris Lattnerc3421bb2010-03-09 00:00:15 +000038 SubprogramCount(0) {}
Bill Wendling88423ee2009-05-15 00:11:17 +000039
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);
Chris Lattner9b97a732010-03-30 18:10:53 +000049 return Asm->OutContext.GetOrCreateSymbol
Chris Lattner98cdab52010-03-10 02:25:11 +000050 (Twine(MAI->getPrivateGlobalPrefix()) + Twine(Name) + Twine(ID));
Chris Lattnerb98b1bf2010-03-08 22:23:36 +000051}
52
53/// getTempLabel - Return the MCSymbol corresponding to the assembler temporary
54/// label with the specified name.
55MCSymbol *DwarfPrinter::getTempLabel(const char *Name) const {
Chris Lattner9b97a732010-03-30 18:10:53 +000056 return Asm->OutContext.GetOrCreateSymbol
Chris Lattner98cdab52010-03-10 02:25:11 +000057 (Twine(MAI->getPrivateGlobalPrefix()) + Name);
Chris Lattnerb98b1bf2010-03-08 22:23:36 +000058}
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 Lattnerf61ed8e2010-01-22 22:38:16 +000081static const char *DecodeDWARFEncoding(unsigned Encoding) {
82 switch (Encoding) {
83 case dwarf::DW_EH_PE_absptr: return "absptr";
84 case dwarf::DW_EH_PE_omit: return "omit";
85 case dwarf::DW_EH_PE_pcrel: return "pcrel";
86 case dwarf::DW_EH_PE_udata4: return "udata4";
87 case dwarf::DW_EH_PE_udata8: return "udata8";
88 case dwarf::DW_EH_PE_sdata4: return "sdata4";
89 case dwarf::DW_EH_PE_sdata8: return "sdata8";
90 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4: return "pcrel udata4";
91 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4: return "pcrel sdata4";
92 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8: return "pcrel udata8";
93 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8: return "pcrel sdata8";
94 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata4:
95 return "indirect pcrel udata4";
96 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata4:
97 return "indirect pcrel sdata4";
98 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata8:
99 return "indirect pcrel udata8";
100 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata8:
101 return "indirect pcrel sdata8";
102 }
103
104 return "<unknown encoding>";
105}
106
107/// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
108/// encoding. If verbose assembly output is enabled, we output comments
109/// describing the encoding. Desc is an optional string saying what the
110/// encoding is specifying (e.g. "LSDA").
111void DwarfPrinter::EmitEncodingByte(unsigned Val, const char *Desc) {
112 if (Asm->VerboseAsm) {
113 if (Desc != 0)
114 Asm->OutStreamer.AddComment(Twine(Desc)+" Encoding = " +
115 Twine(DecodeDWARFEncoding(Val)));
116 else
117 Asm->OutStreamer.AddComment(Twine("Encoding = ") +
118 DecodeDWARFEncoding(Val));
119 }
120
121 Asm->OutStreamer.EmitIntValue(Val, 1, 0/*addrspace*/);
122}
123
Chris Lattner245834d2010-01-22 23:40:08 +0000124/// EmitCFAByte - Emit a .byte 42 directive for a DW_CFA_xxx value.
125void DwarfPrinter::EmitCFAByte(unsigned Val) {
126 if (Asm->VerboseAsm) {
127 if (Val >= dwarf::DW_CFA_offset && Val < dwarf::DW_CFA_offset+64)
128 Asm->OutStreamer.AddComment("DW_CFA_offset + Reg (" +
129 Twine(Val-dwarf::DW_CFA_offset) + ")");
130 else
131 Asm->OutStreamer.AddComment(dwarf::CallFrameString(Val));
132 }
133 Asm->OutStreamer.EmitIntValue(Val, 1, 0/*addrspace*/);
134}
135
Chris Lattner894d75a2010-01-22 23:18:42 +0000136/// EmitSLEB128 - emit the specified signed leb128 value.
137void DwarfPrinter::EmitSLEB128(int Value, const char *Desc) const {
138 if (Asm->VerboseAsm && Desc)
139 Asm->OutStreamer.AddComment(Desc);
140
141 if (MAI->hasLEB128()) {
Chris Lattner188a87d2010-03-10 01:04:13 +0000142 // FIXME: MCize.
Chris Lattner75f50722010-04-04 07:48:20 +0000143 Asm->OutStreamer.EmitRawText("\t.sleb128\t" + Twine(Value));
Chris Lattner894d75a2010-01-22 23:18:42 +0000144 return;
145 }
146
147 // If we don't have .sleb128, emit as .bytes.
Chris Lattnerbb9078a2010-01-22 22:56:55 +0000148 int Sign = Value >> (8 * sizeof(Value) - 1);
149 bool IsMore;
150
151 do {
152 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
153 Value >>= 7;
154 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
155 if (IsMore) Byte |= 0x80;
Chris Lattner894d75a2010-01-22 23:18:42 +0000156 Asm->OutStreamer.EmitIntValue(Byte, 1, /*addrspace*/0);
Chris Lattnerbb9078a2010-01-22 22:56:55 +0000157 } while (IsMore);
158}
159
Chris Lattner894d75a2010-01-22 23:18:42 +0000160/// EmitULEB128 - emit the specified signed leb128 value.
Bill Wendling3dc9b482010-02-24 23:34:35 +0000161void DwarfPrinter::EmitULEB128(unsigned Value, const char *Desc,
162 unsigned PadTo) const {
Chris Lattnerbb9078a2010-01-22 22:56:55 +0000163 if (Asm->VerboseAsm && Desc)
164 Asm->OutStreamer.AddComment(Desc);
Chris Lattner894d75a2010-01-22 23:18:42 +0000165
Bill Wendling3dc9b482010-02-24 23:34:35 +0000166 if (MAI->hasLEB128() && PadTo == 0) {
Chris Lattner188a87d2010-03-10 01:04:13 +0000167 // FIXME: MCize.
Chris Lattner75f50722010-04-04 07:48:20 +0000168 Asm->OutStreamer.EmitRawText("\t.uleb128\t" + Twine(Value));
Chris Lattner894d75a2010-01-22 23:18:42 +0000169 return;
Chris Lattnerbb9078a2010-01-22 22:56:55 +0000170 }
Chris Lattner894d75a2010-01-22 23:18:42 +0000171
Bill Wendling3dc9b482010-02-24 23:34:35 +0000172 // If we don't have .uleb128 or we want to emit padding, emit as .bytes.
Chris Lattner894d75a2010-01-22 23:18:42 +0000173 do {
174 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
175 Value >>= 7;
Bill Wendling3dc9b482010-02-24 23:34:35 +0000176 if (Value || PadTo != 0) Byte |= 0x80;
Chris Lattner894d75a2010-01-22 23:18:42 +0000177 Asm->OutStreamer.EmitIntValue(Byte, 1, /*addrspace*/0);
178 } while (Value);
Bill Wendling3dc9b482010-02-24 23:34:35 +0000179
Bill Wendlingf0bd4cc2010-02-25 00:24:52 +0000180 if (PadTo) {
181 if (PadTo > 1)
182 Asm->OutStreamer.EmitFill(PadTo - 1, 0x80/*fillval*/, 0/*addrspace*/);
183 Asm->OutStreamer.EmitFill(1, 0/*fillval*/, 0/*addrspace*/);
184 }
Chris Lattnerbb9078a2010-01-22 22:56:55 +0000185}
186
187
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000188void DwarfPrinter::EmitReference(const MCSymbol *Sym, unsigned Encoding) const {
189 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
190
Chris Lattner42263e22010-03-11 21:55:20 +0000191 const MCExpr *Exp = TLOF.getExprForDwarfReference(Sym, Asm->Mang,
192 Asm->MMI, Encoding,
Chris Lattner3192d142010-03-11 19:41:58 +0000193 Asm->OutStreamer);
Chris Lattner90e4af72010-03-10 00:09:21 +0000194 Asm->OutStreamer.EmitValue(Exp, SizeOfEncodedValue(Encoding), /*addrspace*/0);
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000195}
196
Chris Lattner0d50c762010-03-08 23:58:37 +0000197void DwarfPrinter::EmitReference(const GlobalValue *GV, unsigned Encoding)const{
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000198 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
199
Chris Lattner90e4af72010-03-10 00:09:21 +0000200 const MCExpr *Exp =
Chris Lattner3192d142010-03-11 19:41:58 +0000201 TLOF.getExprForDwarfGlobalReference(GV, Asm->Mang, Asm->MMI, Encoding,
202 Asm->OutStreamer);
Chris Lattner90e4af72010-03-10 00:09:21 +0000203 Asm->OutStreamer.EmitValue(Exp, SizeOfEncodedValue(Encoding), /*addrspace*/0);
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000204}
205
Chris Lattner6a315c32010-01-26 20:20:43 +0000206/// EmitDifference - Emit the difference between two labels. If this assembler
207/// supports .set, we emit a .set of a temporary and then use it in the .word.
Chris Lattnerb98b1bf2010-03-08 22:23:36 +0000208void DwarfPrinter::EmitDifference(const MCSymbol *TagHi, const MCSymbol *TagLo,
209 bool IsSmall) {
Chris Lattner0d50c762010-03-08 23:58:37 +0000210 unsigned Size = IsSmall ? 4 : TD->getPointerSize();
211 Asm->EmitLabelDifference(TagHi, TagLo, Size);
Chris Lattnerb98b1bf2010-03-08 22:23:36 +0000212}
213
Chris Lattnerb98b1bf2010-03-08 22:23:36 +0000214void DwarfPrinter::EmitSectionOffset(const MCSymbol *Label,
215 const MCSymbol *Section,
Chris Lattner57578762010-03-08 23:23:25 +0000216 bool IsSmall, bool isEH) {
Chris Lattneref6b14f2010-03-09 00:17:58 +0000217 bool isAbsolute;
Bill Wendling88423ee2009-05-15 00:11:17 +0000218 if (isEH)
Chris Lattneref6b14f2010-03-09 00:17:58 +0000219 isAbsolute = MAI->isAbsoluteEHSectionOffsets();
Bill Wendling88423ee2009-05-15 00:11:17 +0000220 else
Chris Lattneref6b14f2010-03-09 00:17:58 +0000221 isAbsolute = MAI->isAbsoluteDebugSectionOffsets();
Bill Wendling88423ee2009-05-15 00:11:17 +0000222
Chris Lattneref6b14f2010-03-09 00:17:58 +0000223 if (!isAbsolute)
Chris Lattner57578762010-03-08 23:23:25 +0000224 return EmitDifference(Label, Section, IsSmall);
225
Chris Lattneref6b14f2010-03-09 00:17:58 +0000226 // On COFF targets, we have to emit the weird .secrel32 directive.
Chris Lattner20e32802010-03-12 18:10:35 +0000227 if (const char *SecOffDir = MAI->getDwarfSectionOffsetDirective()) {
Chris Lattner188a87d2010-03-10 01:04:13 +0000228 // FIXME: MCize.
Chris Lattner0b9bdb42010-04-04 07:25:52 +0000229 Asm->OutStreamer.EmitRawText(SecOffDir + Twine(Label->getName()));
Chris Lattner20e32802010-03-12 18:10:35 +0000230 } else {
Chris Lattneref6b14f2010-03-09 00:17:58 +0000231 unsigned Size = IsSmall ? 4 : TD->getPointerSize();
Chris Lattner6cde3e62010-03-09 00:39:24 +0000232 Asm->OutStreamer.EmitSymbolValue(Label, Size, 0/*AddrSpace*/);
Chris Lattneref6b14f2010-03-09 00:17:58 +0000233 }
Bill Wendling88423ee2009-05-15 00:11:17 +0000234}
235
236/// EmitFrameMoves - Emit frame instructions to describe the layout of the
237/// frame.
Chris Lattner5e6cbe02010-03-13 08:05:25 +0000238void DwarfPrinter::EmitFrameMoves(MCSymbol *BaseLabel,
Chris Lattner066c9ac2010-01-22 22:23:57 +0000239 const std::vector<MachineMove> &Moves,
240 bool isEH) {
Chris Lattnerfb658072010-03-13 07:40:56 +0000241 int stackGrowth = TD->getPointerSize();
242 if (Asm->TM.getFrameInfo()->getStackGrowthDirection() !=
243 TargetFrameInfo::StackGrowsUp)
244 stackGrowth *= -1;
245
Bill Wendling88423ee2009-05-15 00:11:17 +0000246 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
247 const MachineMove &Move = Moves[i];
Chris Lattner2e9919a2010-03-14 08:12:40 +0000248 MCSymbol *Label = Move.getLabel();
Chris Lattnera34ec2292010-03-09 01:51:43 +0000249 // Throw out move if the label is invalid.
Chris Lattner2e9919a2010-03-14 08:12:40 +0000250 if (Label && !Label->isDefined()) continue; // Not emitted, in dead code.
Bill Wendling88423ee2009-05-15 00:11:17 +0000251
252 const MachineLocation &Dst = Move.getDestination();
253 const MachineLocation &Src = Move.getSource();
254
255 // Advance row if new location.
Chris Lattnerf14645c2010-03-14 07:02:50 +0000256 if (BaseLabel && Label) {
Chris Lattnerb9130602010-03-14 02:20:58 +0000257 MCSymbol *ThisSym = Label;
Chris Lattner5e6cbe02010-03-13 08:05:25 +0000258 if (ThisSym != BaseLabel) {
259 EmitCFAByte(dwarf::DW_CFA_advance_loc4);
260 EmitDifference(ThisSym, BaseLabel, true);
261 BaseLabel = ThisSym;
262 }
Bill Wendling88423ee2009-05-15 00:11:17 +0000263 }
264
265 // If advancing cfa.
266 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
267 if (!Src.isReg()) {
268 if (Src.getReg() == MachineLocation::VirtualFP) {
Chris Lattner245834d2010-01-22 23:40:08 +0000269 EmitCFAByte(dwarf::DW_CFA_def_cfa_offset);
Bill Wendling88423ee2009-05-15 00:11:17 +0000270 } else {
Chris Lattner245834d2010-01-22 23:40:08 +0000271 EmitCFAByte(dwarf::DW_CFA_def_cfa);
Chris Lattner894d75a2010-01-22 23:18:42 +0000272 EmitULEB128(RI->getDwarfRegNum(Src.getReg(), isEH), "Register");
Bill Wendling88423ee2009-05-15 00:11:17 +0000273 }
274
275 int Offset = -Src.getOffset();
Chris Lattner894d75a2010-01-22 23:18:42 +0000276 EmitULEB128(Offset, "Offset");
Bill Wendling88423ee2009-05-15 00:11:17 +0000277 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000278 llvm_unreachable("Machine move not supported yet.");
Bill Wendling88423ee2009-05-15 00:11:17 +0000279 }
280 } else if (Src.isReg() &&
281 Src.getReg() == MachineLocation::VirtualFP) {
282 if (Dst.isReg()) {
Chris Lattner245834d2010-01-22 23:40:08 +0000283 EmitCFAByte(dwarf::DW_CFA_def_cfa_register);
Chris Lattner894d75a2010-01-22 23:18:42 +0000284 EmitULEB128(RI->getDwarfRegNum(Dst.getReg(), isEH), "Register");
Bill Wendling88423ee2009-05-15 00:11:17 +0000285 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000286 llvm_unreachable("Machine move not supported yet.");
Bill Wendling88423ee2009-05-15 00:11:17 +0000287 }
288 } else {
289 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), isEH);
290 int Offset = Dst.getOffset() / stackGrowth;
291
292 if (Offset < 0) {
Chris Lattner245834d2010-01-22 23:40:08 +0000293 EmitCFAByte(dwarf::DW_CFA_offset_extended_sf);
Chris Lattner894d75a2010-01-22 23:18:42 +0000294 EmitULEB128(Reg, "Reg");
Chris Lattnerbb9078a2010-01-22 22:56:55 +0000295 EmitSLEB128(Offset, "Offset");
Bill Wendling88423ee2009-05-15 00:11:17 +0000296 } else if (Reg < 64) {
Chris Lattner245834d2010-01-22 23:40:08 +0000297 EmitCFAByte(dwarf::DW_CFA_offset + Reg);
Chris Lattner894d75a2010-01-22 23:18:42 +0000298 EmitULEB128(Offset, "Offset");
Bill Wendling88423ee2009-05-15 00:11:17 +0000299 } else {
Chris Lattner245834d2010-01-22 23:40:08 +0000300 EmitCFAByte(dwarf::DW_CFA_offset_extended);
Chris Lattner894d75a2010-01-22 23:18:42 +0000301 EmitULEB128(Reg, "Reg");
302 EmitULEB128(Offset, "Offset");
Bill Wendling88423ee2009-05-15 00:11:17 +0000303 }
304 }
305 }
306}