blob: 31dc0dd503b59b10827301d172725eee283765e9 [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
Eric Christopher29e874d2014-03-07 22:40:37 +000014#include "ByteStreamer.h"
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000015#include "DwarfExpression.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/ADT/Twine.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000017#include "llvm/CodeGen/AsmPrinter.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"
Eric Christopherd9134482014-08-04 21:25:23 +000030#include "llvm/Target/TargetSubtargetInfo.h"
Chris Lattner9efd1182010-04-04 19:09:29 +000031using namespace llvm;
32
Chandler Carruth1b9dde02014-04-22 02:02:50 +000033#define DEBUG_TYPE "asm-printer"
34
Adrian Prantl337e3602015-01-12 23:03:23 +000035void DebugLocDwarfExpression::EmitOp(uint8_t Op, const char *Comment) {
36 BS.EmitInt8(
37 Op, Comment ? Twine(Comment) + " " + dwarf::OperationEncodingString(Op)
38 : dwarf::OperationEncodingString(Op));
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000039}
Adrian Prantl66f25952015-01-13 00:04:06 +000040
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000041void DebugLocDwarfExpression::EmitSigned(int Value) {
42 BS.EmitSLEB128(Value, Twine(Value));
43}
Adrian Prantl66f25952015-01-13 00:04:06 +000044
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000045void DebugLocDwarfExpression::EmitUnsigned(unsigned Value) {
46 BS.EmitULEB128(Value, Twine(Value));
47}
48
Adrian Prantl8995f5c2015-01-13 23:10:43 +000049bool DebugLocDwarfExpression::isFrameRegister(unsigned MachineReg) {
50 // This information is not available while emitting .debug_loc entries.
51 return false;
Adrian Prantl66f25952015-01-13 00:04:06 +000052}
53
Chris Lattneraabc6042010-04-04 23:41:46 +000054//===----------------------------------------------------------------------===//
55// Dwarf Emission Helper Routines
56//===----------------------------------------------------------------------===//
57
Chris Lattner9efd1182010-04-04 19:09:29 +000058/// EmitSLEB128 - emit the specified signed leb128 value.
David Blaikie5acff7e2013-06-23 18:31:11 +000059void AsmPrinter::EmitSLEB128(int64_t Value, const char *Desc) const {
Chris Lattner9efd1182010-04-04 19:09:29 +000060 if (isVerbose() && Desc)
61 OutStreamer.AddComment(Desc);
Chris Lattner9efd1182010-04-04 19:09:29 +000062
Benjamin Kramerc74798d2011-11-05 11:52:44 +000063 OutStreamer.EmitSLEB128IntValue(Value);
Chris Lattner9efd1182010-04-04 19:09:29 +000064}
65
66/// EmitULEB128 - emit the specified signed leb128 value.
David Blaikie5acff7e2013-06-23 18:31:11 +000067void AsmPrinter::EmitULEB128(uint64_t Value, const char *Desc,
Chris Lattner9efd1182010-04-04 19:09:29 +000068 unsigned PadTo) const {
69 if (isVerbose() && Desc)
70 OutStreamer.AddComment(Desc);
Rafael Espindola38d07562010-11-04 18:17:08 +000071
Eric Christopherbf7bc492013-01-09 03:52:05 +000072 OutStreamer.EmitULEB128IntValue(Value, PadTo);
Chris Lattner9efd1182010-04-04 19:09:29 +000073}
74
Chris Lattnerbaf2be02010-04-04 20:01:25 +000075/// EmitCFAByte - Emit a .byte 42 directive for a DW_CFA_xxx value.
76void AsmPrinter::EmitCFAByte(unsigned Val) const {
77 if (isVerbose()) {
Eric Christopher596077b2013-12-04 22:26:43 +000078 if (Val >= dwarf::DW_CFA_offset && Val < dwarf::DW_CFA_offset + 64)
Eric Christopher1d6bd412012-11-20 20:34:47 +000079 OutStreamer.AddComment("DW_CFA_offset + Reg (" +
Eric Christopher596077b2013-12-04 22:26:43 +000080 Twine(Val - dwarf::DW_CFA_offset) + ")");
Chris Lattnerbaf2be02010-04-04 20:01:25 +000081 else
82 OutStreamer.AddComment(dwarf::CallFrameString(Val));
83 }
Eric Christopherce0cfce2013-01-09 01:35:34 +000084 OutStreamer.EmitIntValue(Val, 1);
Chris Lattnerbaf2be02010-04-04 20:01:25 +000085}
86
Chris Lattnerb75af3c2010-04-04 20:04:21 +000087static const char *DecodeDWARFEncoding(unsigned Encoding) {
88 switch (Encoding) {
Eric Christopher596077b2013-12-04 22:26:43 +000089 case dwarf::DW_EH_PE_absptr:
90 return "absptr";
91 case dwarf::DW_EH_PE_omit:
92 return "omit";
93 case dwarf::DW_EH_PE_pcrel:
94 return "pcrel";
95 case dwarf::DW_EH_PE_udata4:
96 return "udata4";
97 case dwarf::DW_EH_PE_udata8:
98 return "udata8";
99 case dwarf::DW_EH_PE_sdata4:
100 return "sdata4";
101 case dwarf::DW_EH_PE_sdata8:
102 return "sdata8";
103 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
104 return "pcrel udata4";
105 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
106 return "pcrel sdata4";
107 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
108 return "pcrel udata8";
109 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
110 return "pcrel sdata8";
111 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4
112 :
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000113 return "indirect pcrel udata4";
Eric Christopher596077b2013-12-04 22:26:43 +0000114 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4
115 :
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000116 return "indirect pcrel sdata4";
Eric Christopher596077b2013-12-04 22:26:43 +0000117 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8
118 :
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000119 return "indirect pcrel udata8";
Eric Christopher596077b2013-12-04 22:26:43 +0000120 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8
121 :
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000122 return "indirect pcrel sdata8";
123 }
Eric Christopher1d6bd412012-11-20 20:34:47 +0000124
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000125 return "<unknown encoding>";
126}
127
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000128/// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
129/// encoding. If verbose assembly output is enabled, we output comments
130/// describing the encoding. Desc is an optional string saying what the
131/// encoding is specifying (e.g. "LSDA").
Chris Lattneraabc6042010-04-04 23:41:46 +0000132void AsmPrinter::EmitEncodingByte(unsigned Val, const char *Desc) const {
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000133 if (isVerbose()) {
Eric Christophercb7119e2013-12-04 22:29:02 +0000134 if (Desc)
Eric Christopher596077b2013-12-04 22:26:43 +0000135 OutStreamer.AddComment(Twine(Desc) + " Encoding = " +
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000136 Twine(DecodeDWARFEncoding(Val)));
137 else
Eric Christopher596077b2013-12-04 22:26:43 +0000138 OutStreamer.AddComment(Twine("Encoding = ") + DecodeDWARFEncoding(Val));
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000139 }
Eric Christopher1d6bd412012-11-20 20:34:47 +0000140
Eric Christopherce0cfce2013-01-09 01:35:34 +0000141 OutStreamer.EmitIntValue(Val, 1);
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000142}
143
Chris Lattnere619c0d2010-04-04 20:20:50 +0000144/// GetSizeOfEncodedValue - Return the size of the encoding in bytes.
145unsigned AsmPrinter::GetSizeOfEncodedValue(unsigned Encoding) const {
146 if (Encoding == dwarf::DW_EH_PE_omit)
147 return 0;
Eric Christopher1d6bd412012-11-20 20:34:47 +0000148
Chris Lattnere619c0d2010-04-04 20:20:50 +0000149 switch (Encoding & 0x07) {
Eric Christopher596077b2013-12-04 22:26:43 +0000150 default:
151 llvm_unreachable("Invalid encoded value.");
152 case dwarf::DW_EH_PE_absptr:
Eric Christopher8b770652015-01-26 19:03:15 +0000153 return TM.getDataLayout()->getPointerSize();
Eric Christopher596077b2013-12-04 22:26:43 +0000154 case dwarf::DW_EH_PE_udata2:
155 return 2;
156 case dwarf::DW_EH_PE_udata4:
157 return 4;
158 case dwarf::DW_EH_PE_udata8:
159 return 8;
Chris Lattnere619c0d2010-04-04 20:20:50 +0000160 }
161}
162
Eric Christopher1d6bd412012-11-20 20:34:47 +0000163void AsmPrinter::EmitTTypeReference(const GlobalValue *GV,
164 unsigned Encoding) const {
Anton Korobeynikov097b0e92012-11-19 21:17:20 +0000165 if (GV) {
166 const TargetLoweringObjectFile &TLOF = getObjFileLowering();
Eric Christopher1d6bd412012-11-20 20:34:47 +0000167
Anton Korobeynikov097b0e92012-11-19 21:17:20 +0000168 const MCExpr *Exp =
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000169 TLOF.getTTypeGlobalReference(GV, Encoding, *Mang, TM, MMI, OutStreamer);
Eric Christopherce0cfce2013-01-09 01:35:34 +0000170 OutStreamer.EmitValue(Exp, GetSizeOfEncodedValue(Encoding));
Anton Korobeynikov097b0e92012-11-19 21:17:20 +0000171 } else
Eric Christopherce0cfce2013-01-09 01:35:34 +0000172 OutStreamer.EmitIntValue(0, GetSizeOfEncodedValue(Encoding));
Chris Lattnere619c0d2010-04-04 20:20:50 +0000173}
Chris Lattner70a4fce2010-04-04 23:25:33 +0000174
175/// EmitSectionOffset - Emit the 4-byte offset of Label from the start of its
176/// section. This can be done with a special directive if the target supports
177/// it (e.g. cygwin) or by emitting it as an offset from a label at the start
178/// of the section.
179///
180/// SectionLabel is a temporary label emitted at the start of the section that
181/// Label lives in.
182void AsmPrinter::EmitSectionOffset(const MCSymbol *Label,
183 const MCSymbol *SectionLabel) const {
184 // On COFF targets, we have to emit the special .secrel32 directive.
Matt Arsenault034ca0f2013-04-22 22:49:11 +0000185 if (MAI->needsDwarfSectionOffsetDirective()) {
Rafael Espindolad3df3d32011-12-17 01:14:52 +0000186 OutStreamer.EmitCOFFSecRel32(Label);
Chris Lattner70a4fce2010-04-04 23:25:33 +0000187 return;
188 }
Eric Christopher1d6bd412012-11-20 20:34:47 +0000189
Chris Lattner70a4fce2010-04-04 23:25:33 +0000190 // Get the section that we're referring to, based on SectionLabel.
191 const MCSection &Section = SectionLabel->getSection();
Eric Christopher1d6bd412012-11-20 20:34:47 +0000192
Chris Lattner70a4fce2010-04-04 23:25:33 +0000193 // If Label has already been emitted, verify that it is in the same section as
194 // section label for sanity.
195 assert((!Label->isInSection() || &Label->getSection() == &Section) &&
196 "Section offset using wrong section base for label");
Eric Christopher1d6bd412012-11-20 20:34:47 +0000197
Duncan Sandsb847bf52011-03-12 13:07:37 +0000198 // If the section in question will end up with an address of 0 anyway, we can
199 // just emit an absolute reference to save a relocation.
200 if (Section.isBaseAddressKnownZero()) {
Eric Christopherce0cfce2013-01-09 01:35:34 +0000201 OutStreamer.EmitSymbolValue(Label, 4);
Duncan Sandsb847bf52011-03-12 13:07:37 +0000202 return;
203 }
Eric Christopher1d6bd412012-11-20 20:34:47 +0000204
Chris Lattner70a4fce2010-04-04 23:25:33 +0000205 // Otherwise, emit it as a label difference from the start of the section.
206 EmitLabelDifference(Label, SectionLabel, 4);
207}
208
Adrian Prantl42a0d8c2014-04-27 18:50:45 +0000209// Some targets do not provide a DWARF register number for every
210// register. This function attempts to emit a DWARF register by
211// emitting a piece of a super-register or by piecing together
212// multiple subregisters that alias the register.
Adrian Prantld34db652014-04-27 18:25:45 +0000213void AsmPrinter::EmitDwarfRegOpPiece(ByteStreamer &Streamer,
214 const MachineLocation &MLoc,
215 unsigned PieceSizeInBits,
216 unsigned PieceOffsetInBits) const {
Adrian Prantl42a0d8c2014-04-27 18:50:45 +0000217 assert(MLoc.isReg() && "MLoc must be a register");
Adrian Prantla4c30d62015-01-12 23:36:56 +0000218 DebugLocDwarfExpression Expr(*this, Streamer);
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000219 Expr.AddMachineRegPiece(MLoc.getReg(), PieceSizeInBits, PieceOffsetInBits);
220}
Eric Christopher698a8ab2014-03-07 01:44:14 +0000221
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000222void AsmPrinter::EmitDwarfOpPiece(ByteStreamer &Streamer,
223 unsigned PieceSizeInBits,
224 unsigned PieceOffsetInBits) const {
Adrian Prantla4c30d62015-01-12 23:36:56 +0000225 DebugLocDwarfExpression Expr(*this, Streamer);
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000226 Expr.AddOpPiece(PieceSizeInBits, PieceOffsetInBits);
Eric Christopher698a8ab2014-03-07 01:44:14 +0000227}
228
229/// EmitDwarfRegOp - Emit dwarf register operation.
Eric Christopher29e874d2014-03-07 22:40:37 +0000230void AsmPrinter::EmitDwarfRegOp(ByteStreamer &Streamer,
Adrian Prantl5883af32015-01-19 17:57:29 +0000231 const MachineLocation &MLoc) const {
Adrian Prantla4c30d62015-01-12 23:36:56 +0000232 DebugLocDwarfExpression Expr(*this, Streamer);
Eric Christopherd9134482014-08-04 21:25:23 +0000233 const TargetRegisterInfo *TRI = TM.getSubtargetImpl()->getRegisterInfo();
Eric Christopher698a8ab2014-03-07 01:44:14 +0000234 int Reg = TRI->getDwarfRegNum(MLoc.getReg(), false);
235 if (Reg < 0) {
236 // We assume that pointers are always in an addressable register.
Adrian Prantl5883af32015-01-19 17:57:29 +0000237 if (MLoc.isIndirect())
Eric Christopher698a8ab2014-03-07 01:44:14 +0000238 // FIXME: We have no reasonable way of handling errors in here. The
239 // caller might be in the middle of a dwarf expression. We should
240 // probably assert that Reg >= 0 once debug info generation is more
241 // mature.
Adrian Prantl337e3602015-01-12 23:03:23 +0000242 return Expr.EmitOp(dwarf::DW_OP_nop,
243 "nop (could not find a dwarf register number)");
Eric Christopher698a8ab2014-03-07 01:44:14 +0000244
245 // Attempt to find a valid super- or sub-register.
Adrian Prantlad768c32015-01-14 01:01:28 +0000246 if (!Expr.AddMachineRegPiece(MLoc.getReg()))
247 Expr.EmitOp(dwarf::DW_OP_nop,
248 "nop (could not find a dwarf register number)");
249 return;
Eric Christopher698a8ab2014-03-07 01:44:14 +0000250 }
251
252 if (MLoc.isIndirect())
Adrian Prantl5883af32015-01-19 17:57:29 +0000253 Expr.AddRegIndirect(Reg, MLoc.getOffset());
Eric Christopher698a8ab2014-03-07 01:44:14 +0000254 else
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000255 Expr.AddReg(Reg);
Eric Christopher698a8ab2014-03-07 01:44:14 +0000256}
257
Chris Lattneraabc6042010-04-04 23:41:46 +0000258//===----------------------------------------------------------------------===//
259// Dwarf Lowering Routines
260//===----------------------------------------------------------------------===//
Chris Lattner70a4fce2010-04-04 23:25:33 +0000261
Rafael Espindola227144c2013-05-13 01:16:13 +0000262void AsmPrinter::emitCFIInstruction(const MCCFIInstruction &Inst) const {
263 switch (Inst.getOperation()) {
264 default:
265 llvm_unreachable("Unexpected instruction");
266 case MCCFIInstruction::OpDefCfaOffset:
267 OutStreamer.EmitCFIDefCfaOffset(Inst.getOffset());
268 break;
269 case MCCFIInstruction::OpDefCfa:
270 OutStreamer.EmitCFIDefCfa(Inst.getRegister(), Inst.getOffset());
271 break;
272 case MCCFIInstruction::OpDefCfaRegister:
273 OutStreamer.EmitCFIDefCfaRegister(Inst.getRegister());
274 break;
275 case MCCFIInstruction::OpOffset:
276 OutStreamer.EmitCFIOffset(Inst.getRegister(), Inst.getOffset());
277 break;
Venkatraman Govindaraju4c0cdd72013-09-26 15:11:00 +0000278 case MCCFIInstruction::OpRegister:
279 OutStreamer.EmitCFIRegister(Inst.getRegister(), Inst.getRegister2());
280 break;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000281 case MCCFIInstruction::OpWindowSave:
282 OutStreamer.EmitCFIWindowSave();
283 break;
Oliver Stannardb14c6252014-04-02 16:10:33 +0000284 case MCCFIInstruction::OpSameValue:
285 OutStreamer.EmitCFISameValue(Inst.getRegister());
286 break;
Rafael Espindolabeb74c32011-04-15 20:32:03 +0000287 }
288}