blob: 3741ef0398d609fb010f22897d74f70cfb83bf8e [file] [log] [blame]
Chris Lattner9efd1182010-04-04 19:09:29 +00001//===-- AsmPrinterDwarf.cpp - AsmPrinter Dwarf Support --------------------===//
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// This file implements the Dwarf emissions parts of AsmPrinter.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "asm-printer"
15#include "llvm/CodeGen/AsmPrinter.h"
Eric Christopher698a8ab2014-03-07 01:44:14 +000016#include "llvm/ADT/SmallBitVector.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/ADT/Twine.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/DataLayout.h"
Chris Lattner9efd1182010-04-04 19:09:29 +000019#include "llvm/MC/MCAsmInfo.h"
Chris Lattner70a4fce2010-04-04 23:25:33 +000020#include "llvm/MC/MCSection.h"
Chris Lattner9efd1182010-04-04 19:09:29 +000021#include "llvm/MC/MCStreamer.h"
Chris Lattner70a4fce2010-04-04 23:25:33 +000022#include "llvm/MC/MCSymbol.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/MC/MachineLocation.h"
24#include "llvm/Support/Dwarf.h"
25#include "llvm/Support/ErrorHandling.h"
Anton Korobeynikov2f931282011-01-10 12:39:04 +000026#include "llvm/Target/TargetFrameLowering.h"
Chris Lattnere619c0d2010-04-04 20:20:50 +000027#include "llvm/Target/TargetLoweringObjectFile.h"
28#include "llvm/Target/TargetMachine.h"
Chris Lattneraabc6042010-04-04 23:41:46 +000029#include "llvm/Target/TargetRegisterInfo.h"
Chris Lattner9efd1182010-04-04 19:09:29 +000030using namespace llvm;
31
Chris Lattneraabc6042010-04-04 23:41:46 +000032//===----------------------------------------------------------------------===//
33// Dwarf Emission Helper Routines
34//===----------------------------------------------------------------------===//
35
Chris Lattner9efd1182010-04-04 19:09:29 +000036/// EmitSLEB128 - emit the specified signed leb128 value.
David Blaikie5acff7e2013-06-23 18:31:11 +000037void AsmPrinter::EmitSLEB128(int64_t Value, const char *Desc) const {
Chris Lattner9efd1182010-04-04 19:09:29 +000038 if (isVerbose() && Desc)
39 OutStreamer.AddComment(Desc);
Chris Lattner9efd1182010-04-04 19:09:29 +000040
Benjamin Kramerc74798d2011-11-05 11:52:44 +000041 OutStreamer.EmitSLEB128IntValue(Value);
Chris Lattner9efd1182010-04-04 19:09:29 +000042}
43
44/// EmitULEB128 - emit the specified signed leb128 value.
David Blaikie5acff7e2013-06-23 18:31:11 +000045void AsmPrinter::EmitULEB128(uint64_t Value, const char *Desc,
Chris Lattner9efd1182010-04-04 19:09:29 +000046 unsigned PadTo) const {
47 if (isVerbose() && Desc)
48 OutStreamer.AddComment(Desc);
Rafael Espindola38d07562010-11-04 18:17:08 +000049
Eric Christopherbf7bc492013-01-09 03:52:05 +000050 OutStreamer.EmitULEB128IntValue(Value, PadTo);
Chris Lattner9efd1182010-04-04 19:09:29 +000051}
52
Chris Lattnerbaf2be02010-04-04 20:01:25 +000053/// EmitCFAByte - Emit a .byte 42 directive for a DW_CFA_xxx value.
54void AsmPrinter::EmitCFAByte(unsigned Val) const {
55 if (isVerbose()) {
Eric Christopher596077b2013-12-04 22:26:43 +000056 if (Val >= dwarf::DW_CFA_offset && Val < dwarf::DW_CFA_offset + 64)
Eric Christopher1d6bd412012-11-20 20:34:47 +000057 OutStreamer.AddComment("DW_CFA_offset + Reg (" +
Eric Christopher596077b2013-12-04 22:26:43 +000058 Twine(Val - dwarf::DW_CFA_offset) + ")");
Chris Lattnerbaf2be02010-04-04 20:01:25 +000059 else
60 OutStreamer.AddComment(dwarf::CallFrameString(Val));
61 }
Eric Christopherce0cfce2013-01-09 01:35:34 +000062 OutStreamer.EmitIntValue(Val, 1);
Chris Lattnerbaf2be02010-04-04 20:01:25 +000063}
64
Chris Lattnerb75af3c2010-04-04 20:04:21 +000065static const char *DecodeDWARFEncoding(unsigned Encoding) {
66 switch (Encoding) {
Eric Christopher596077b2013-12-04 22:26:43 +000067 case dwarf::DW_EH_PE_absptr:
68 return "absptr";
69 case dwarf::DW_EH_PE_omit:
70 return "omit";
71 case dwarf::DW_EH_PE_pcrel:
72 return "pcrel";
73 case dwarf::DW_EH_PE_udata4:
74 return "udata4";
75 case dwarf::DW_EH_PE_udata8:
76 return "udata8";
77 case dwarf::DW_EH_PE_sdata4:
78 return "sdata4";
79 case dwarf::DW_EH_PE_sdata8:
80 return "sdata8";
81 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
82 return "pcrel udata4";
83 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
84 return "pcrel sdata4";
85 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
86 return "pcrel udata8";
87 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
88 return "pcrel sdata8";
89 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4
90 :
Chris Lattnerb75af3c2010-04-04 20:04:21 +000091 return "indirect pcrel udata4";
Eric Christopher596077b2013-12-04 22:26:43 +000092 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4
93 :
Chris Lattnerb75af3c2010-04-04 20:04:21 +000094 return "indirect pcrel sdata4";
Eric Christopher596077b2013-12-04 22:26:43 +000095 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8
96 :
Chris Lattnerb75af3c2010-04-04 20:04:21 +000097 return "indirect pcrel udata8";
Eric Christopher596077b2013-12-04 22:26:43 +000098 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8
99 :
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000100 return "indirect pcrel sdata8";
101 }
Eric Christopher1d6bd412012-11-20 20:34:47 +0000102
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000103 return "<unknown encoding>";
104}
105
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000106/// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
107/// encoding. If verbose assembly output is enabled, we output comments
108/// describing the encoding. Desc is an optional string saying what the
109/// encoding is specifying (e.g. "LSDA").
Chris Lattneraabc6042010-04-04 23:41:46 +0000110void AsmPrinter::EmitEncodingByte(unsigned Val, const char *Desc) const {
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000111 if (isVerbose()) {
Eric Christophercb7119e2013-12-04 22:29:02 +0000112 if (Desc)
Eric Christopher596077b2013-12-04 22:26:43 +0000113 OutStreamer.AddComment(Twine(Desc) + " Encoding = " +
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000114 Twine(DecodeDWARFEncoding(Val)));
115 else
Eric Christopher596077b2013-12-04 22:26:43 +0000116 OutStreamer.AddComment(Twine("Encoding = ") + DecodeDWARFEncoding(Val));
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000117 }
Eric Christopher1d6bd412012-11-20 20:34:47 +0000118
Eric Christopherce0cfce2013-01-09 01:35:34 +0000119 OutStreamer.EmitIntValue(Val, 1);
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000120}
121
Chris Lattnere619c0d2010-04-04 20:20:50 +0000122/// GetSizeOfEncodedValue - Return the size of the encoding in bytes.
123unsigned AsmPrinter::GetSizeOfEncodedValue(unsigned Encoding) const {
124 if (Encoding == dwarf::DW_EH_PE_omit)
125 return 0;
Eric Christopher1d6bd412012-11-20 20:34:47 +0000126
Chris Lattnere619c0d2010-04-04 20:20:50 +0000127 switch (Encoding & 0x07) {
Eric Christopher596077b2013-12-04 22:26:43 +0000128 default:
129 llvm_unreachable("Invalid encoded value.");
130 case dwarf::DW_EH_PE_absptr:
131 return TM.getDataLayout()->getPointerSize();
132 case dwarf::DW_EH_PE_udata2:
133 return 2;
134 case dwarf::DW_EH_PE_udata4:
135 return 4;
136 case dwarf::DW_EH_PE_udata8:
137 return 8;
Chris Lattnere619c0d2010-04-04 20:20:50 +0000138 }
139}
140
Eric Christopher1d6bd412012-11-20 20:34:47 +0000141void AsmPrinter::EmitTTypeReference(const GlobalValue *GV,
142 unsigned Encoding) const {
Anton Korobeynikov097b0e92012-11-19 21:17:20 +0000143 if (GV) {
144 const TargetLoweringObjectFile &TLOF = getObjFileLowering();
Eric Christopher1d6bd412012-11-20 20:34:47 +0000145
Anton Korobeynikov097b0e92012-11-19 21:17:20 +0000146 const MCExpr *Exp =
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000147 TLOF.getTTypeGlobalReference(GV, Encoding, *Mang, TM, MMI, OutStreamer);
Eric Christopherce0cfce2013-01-09 01:35:34 +0000148 OutStreamer.EmitValue(Exp, GetSizeOfEncodedValue(Encoding));
Anton Korobeynikov097b0e92012-11-19 21:17:20 +0000149 } else
Eric Christopherce0cfce2013-01-09 01:35:34 +0000150 OutStreamer.EmitIntValue(0, GetSizeOfEncodedValue(Encoding));
Chris Lattnere619c0d2010-04-04 20:20:50 +0000151}
Chris Lattner70a4fce2010-04-04 23:25:33 +0000152
153/// EmitSectionOffset - Emit the 4-byte offset of Label from the start of its
154/// section. This can be done with a special directive if the target supports
155/// it (e.g. cygwin) or by emitting it as an offset from a label at the start
156/// of the section.
157///
158/// SectionLabel is a temporary label emitted at the start of the section that
159/// Label lives in.
160void AsmPrinter::EmitSectionOffset(const MCSymbol *Label,
161 const MCSymbol *SectionLabel) const {
162 // On COFF targets, we have to emit the special .secrel32 directive.
Matt Arsenault034ca0f2013-04-22 22:49:11 +0000163 if (MAI->needsDwarfSectionOffsetDirective()) {
Rafael Espindolad3df3d32011-12-17 01:14:52 +0000164 OutStreamer.EmitCOFFSecRel32(Label);
Chris Lattner70a4fce2010-04-04 23:25:33 +0000165 return;
166 }
Eric Christopher1d6bd412012-11-20 20:34:47 +0000167
Chris Lattner70a4fce2010-04-04 23:25:33 +0000168 // Get the section that we're referring to, based on SectionLabel.
169 const MCSection &Section = SectionLabel->getSection();
Eric Christopher1d6bd412012-11-20 20:34:47 +0000170
Chris Lattner70a4fce2010-04-04 23:25:33 +0000171 // If Label has already been emitted, verify that it is in the same section as
172 // section label for sanity.
173 assert((!Label->isInSection() || &Label->getSection() == &Section) &&
174 "Section offset using wrong section base for label");
Eric Christopher1d6bd412012-11-20 20:34:47 +0000175
Duncan Sandsb847bf52011-03-12 13:07:37 +0000176 // If the section in question will end up with an address of 0 anyway, we can
177 // just emit an absolute reference to save a relocation.
178 if (Section.isBaseAddressKnownZero()) {
Eric Christopherce0cfce2013-01-09 01:35:34 +0000179 OutStreamer.EmitSymbolValue(Label, 4);
Duncan Sandsb847bf52011-03-12 13:07:37 +0000180 return;
181 }
Eric Christopher1d6bd412012-11-20 20:34:47 +0000182
Chris Lattner70a4fce2010-04-04 23:25:33 +0000183 // Otherwise, emit it as a label difference from the start of the section.
184 EmitLabelDifference(Label, SectionLabel, 4);
185}
186
Eric Christopher698a8ab2014-03-07 01:44:14 +0000187/// Emit a dwarf register operation.
188static void emitDwarfRegOp(const AsmPrinter &AP, int Reg) {
189 assert(Reg >= 0);
190 if (Reg < 32) {
191 AP.OutStreamer.AddComment(
192 dwarf::OperationEncodingString(dwarf::DW_OP_reg0 + Reg));
193 AP.EmitInt8(dwarf::DW_OP_reg0 + Reg);
194 } else {
195 AP.OutStreamer.AddComment("DW_OP_regx");
196 AP.EmitInt8(dwarf::DW_OP_regx);
197 AP.OutStreamer.AddComment(Twine(Reg));
198 AP.EmitULEB128(Reg);
199 }
200}
201
202/// Emit an (double-)indirect dwarf register operation.
Eric Christopher5c303202014-03-07 21:27:42 +0000203static void emitDwarfRegOpIndirect(const AsmPrinter &AP, int Reg, int Offset,
204 bool Deref) {
Eric Christopher698a8ab2014-03-07 01:44:14 +0000205 assert(Reg >= 0);
206 if (Reg < 32) {
207 AP.OutStreamer.AddComment(
208 dwarf::OperationEncodingString(dwarf::DW_OP_breg0 + Reg));
209 AP.EmitInt8(dwarf::DW_OP_breg0 + Reg);
210 } else {
211 AP.OutStreamer.AddComment("DW_OP_bregx");
212 AP.EmitInt8(dwarf::DW_OP_bregx);
213 AP.OutStreamer.AddComment(Twine(Reg));
214 AP.EmitULEB128(Reg);
215 }
216 AP.EmitSLEB128(Offset);
217 if (Deref)
218 AP.EmitInt8(dwarf::DW_OP_deref);
219}
220
221/// Emit a dwarf register operation for describing
222/// - a small value occupying only part of a register or
223/// - a small register representing only part of a value.
Eric Christopher5c303202014-03-07 21:27:42 +0000224static void emitDwarfOpPiece(const AsmPrinter &AP, unsigned Size,
225 unsigned Offset) {
Eric Christopher698a8ab2014-03-07 01:44:14 +0000226 assert(Size > 0);
227 if (Offset > 0) {
228 AP.OutStreamer.AddComment("DW_OP_bit_piece");
229 AP.EmitInt8(dwarf::DW_OP_bit_piece);
230 AP.OutStreamer.AddComment(Twine(Size));
231 AP.EmitULEB128(Size);
232 AP.OutStreamer.AddComment(Twine(Offset));
233 AP.EmitULEB128(Offset);
234 } else {
235 AP.OutStreamer.AddComment("DW_OP_piece");
236 AP.EmitInt8(dwarf::DW_OP_piece);
237 unsigned ByteSize = Size / 8; // Assuming 8 bits per byte.
238 AP.OutStreamer.AddComment(Twine(ByteSize));
239 AP.EmitULEB128(ByteSize);
240 }
241}
242
243/// Some targets do not provide a DWARF register number for every
244/// register. This function attempts to emit a dwarf register by
245/// emitting a piece of a super-register or by piecing together
246/// multiple subregisters that alias the register.
247static void EmitDwarfRegOpPiece(const AsmPrinter &AP,
248 const MachineLocation &MLoc) {
249 assert(!MLoc.isIndirect());
250 const TargetRegisterInfo *TRI = AP.TM.getRegisterInfo();
251 int Reg = TRI->getDwarfRegNum(MLoc.getReg(), false);
252
253 // Walk up the super-register chain until we find a valid number.
254 // For example, EAX on x86_64 is a 32-bit piece of RAX with offset 0.
255 for (MCSuperRegIterator SR(MLoc.getReg(), TRI); SR.isValid(); ++SR) {
256 Reg = TRI->getDwarfRegNum(*SR, false);
257 if (Reg >= 0) {
258 unsigned Idx = TRI->getSubRegIndex(*SR, MLoc.getReg());
259 unsigned Size = TRI->getSubRegIdxSize(Idx);
260 unsigned Offset = TRI->getSubRegIdxOffset(Idx);
261 AP.OutStreamer.AddComment("super-register");
262 emitDwarfRegOp(AP, Reg);
263 emitDwarfOpPiece(AP, Size, Offset);
264 return;
265 }
266 }
267
268 // Otherwise, attempt to find a covering set of sub-register numbers.
269 // For example, Q0 on ARM is a composition of D0+D1.
270 //
271 // Keep track of the current position so we can emit the more
272 // efficient DW_OP_piece.
273 unsigned CurPos = 0;
274 // The size of the register in bits, assuming 8 bits per byte.
Eric Christopher5c303202014-03-07 21:27:42 +0000275 unsigned RegSize = TRI->getMinimalPhysRegClass(MLoc.getReg())->getSize() * 8;
Eric Christopher698a8ab2014-03-07 01:44:14 +0000276 // Keep track of the bits in the register we already emitted, so we
277 // can avoid emitting redundant aliasing subregs.
278 SmallBitVector Coverage(RegSize, false);
279 for (MCSubRegIterator SR(MLoc.getReg(), TRI); SR.isValid(); ++SR) {
280 unsigned Idx = TRI->getSubRegIndex(MLoc.getReg(), *SR);
281 unsigned Size = TRI->getSubRegIdxSize(Idx);
282 unsigned Offset = TRI->getSubRegIdxOffset(Idx);
283 Reg = TRI->getDwarfRegNum(*SR, false);
284
285 // Intersection between the bits we already emitted and the bits
286 // covered by this subregister.
287 SmallBitVector Intersection(RegSize, false);
Eric Christopher5c303202014-03-07 21:27:42 +0000288 Intersection.set(Offset, Offset + Size);
Eric Christopher698a8ab2014-03-07 01:44:14 +0000289 Intersection ^= Coverage;
290
291 // If this sub-register has a DWARF number and we haven't covered
292 // its range, emit a DWARF piece for it.
293 if (Reg >= 0 && Intersection.any()) {
294 AP.OutStreamer.AddComment("sub-register");
295 emitDwarfRegOp(AP, Reg);
296 emitDwarfOpPiece(AP, Size, Offset == CurPos ? 0 : Offset);
Eric Christopher5c303202014-03-07 21:27:42 +0000297 CurPos = Offset + Size;
Eric Christopher698a8ab2014-03-07 01:44:14 +0000298
299 // Mark it as emitted.
Eric Christopher5c303202014-03-07 21:27:42 +0000300 Coverage.set(Offset, Offset + Size);
Eric Christopher698a8ab2014-03-07 01:44:14 +0000301 }
302 }
303
304 if (CurPos == 0) {
305 // FIXME: We have no reasonable way of handling errors in here.
306 AP.OutStreamer.AddComment("nop (could not find a dwarf register number)");
307 AP.EmitInt8(dwarf::DW_OP_nop);
308 }
309}
310
311/// EmitDwarfRegOp - Emit dwarf register operation.
312void AsmPrinter::EmitDwarfRegOp(const MachineLocation &MLoc,
313 bool Indirect) const {
314 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
315 int Reg = TRI->getDwarfRegNum(MLoc.getReg(), false);
316 if (Reg < 0) {
317 // We assume that pointers are always in an addressable register.
318 if (Indirect || MLoc.isIndirect()) {
319 // FIXME: We have no reasonable way of handling errors in here. The
320 // caller might be in the middle of a dwarf expression. We should
321 // probably assert that Reg >= 0 once debug info generation is more
322 // mature.
323 OutStreamer.AddComment(
324 "nop (invalid dwarf register number for indirect loc)");
325 EmitInt8(dwarf::DW_OP_nop);
326 return;
327 }
328
329 // Attempt to find a valid super- or sub-register.
330 if (!Indirect && !MLoc.isIndirect())
331 return EmitDwarfRegOpPiece(*this, MLoc);
332 }
333
334 if (MLoc.isIndirect())
335 emitDwarfRegOpIndirect(*this, Reg, MLoc.getOffset(), Indirect);
336 else if (Indirect)
337 emitDwarfRegOpIndirect(*this, Reg, 0, false);
338 else
339 emitDwarfRegOp(*this, Reg);
340}
341
Chris Lattneraabc6042010-04-04 23:41:46 +0000342//===----------------------------------------------------------------------===//
343// Dwarf Lowering Routines
344//===----------------------------------------------------------------------===//
Chris Lattner70a4fce2010-04-04 23:25:33 +0000345
Rafael Espindola227144c2013-05-13 01:16:13 +0000346void AsmPrinter::emitCFIInstruction(const MCCFIInstruction &Inst) const {
347 switch (Inst.getOperation()) {
348 default:
349 llvm_unreachable("Unexpected instruction");
350 case MCCFIInstruction::OpDefCfaOffset:
351 OutStreamer.EmitCFIDefCfaOffset(Inst.getOffset());
352 break;
353 case MCCFIInstruction::OpDefCfa:
354 OutStreamer.EmitCFIDefCfa(Inst.getRegister(), Inst.getOffset());
355 break;
356 case MCCFIInstruction::OpDefCfaRegister:
357 OutStreamer.EmitCFIDefCfaRegister(Inst.getRegister());
358 break;
359 case MCCFIInstruction::OpOffset:
360 OutStreamer.EmitCFIOffset(Inst.getRegister(), Inst.getOffset());
361 break;
Venkatraman Govindaraju4c0cdd72013-09-26 15:11:00 +0000362 case MCCFIInstruction::OpRegister:
363 OutStreamer.EmitCFIRegister(Inst.getRegister(), Inst.getRegister2());
364 break;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000365 case MCCFIInstruction::OpWindowSave:
366 OutStreamer.EmitCFIWindowSave();
367 break;
Rafael Espindolabeb74c32011-04-15 20:32:03 +0000368 }
369}