blob: 9a3377a49b6bd15be16dac960e2ad95660b2307c [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"
Chris Lattner9efd1182010-04-04 19:09:29 +000016#include "llvm/CodeGen/AsmPrinter.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"
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 Prantlb16d9eb2015-01-12 22:19:22 +000035/// DwarfExpression implementation for .debug_loc entries.
36class DebugLocDwarfExpression : public DwarfExpression {
37 ByteStreamer &BS;
Adrian Prantl337e3602015-01-12 23:03:23 +000038
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000039public:
40 DebugLocDwarfExpression(TargetMachine &TM, ByteStreamer &BS)
Adrian Prantl337e3602015-01-12 23:03:23 +000041 : DwarfExpression(TM), BS(BS) {}
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000042
Adrian Prantl337e3602015-01-12 23:03:23 +000043 void EmitOp(uint8_t Op, const char *Comment) override;
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000044 void EmitSigned(int Value) override;
45 void EmitUnsigned(unsigned Value) override;
Adrian Prantl337e3602015-01-12 23:03:23 +000046 unsigned getFrameRegister() override { llvm_unreachable("not available"); };
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000047};
48
Adrian Prantl337e3602015-01-12 23:03:23 +000049void DebugLocDwarfExpression::EmitOp(uint8_t Op, const char *Comment) {
50 BS.EmitInt8(
51 Op, Comment ? Twine(Comment) + " " + dwarf::OperationEncodingString(Op)
52 : dwarf::OperationEncodingString(Op));
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000053}
54void DebugLocDwarfExpression::EmitSigned(int Value) {
55 BS.EmitSLEB128(Value, Twine(Value));
56}
57void DebugLocDwarfExpression::EmitUnsigned(unsigned Value) {
58 BS.EmitULEB128(Value, Twine(Value));
59}
60
Chris Lattneraabc6042010-04-04 23:41:46 +000061//===----------------------------------------------------------------------===//
62// Dwarf Emission Helper Routines
63//===----------------------------------------------------------------------===//
64
Chris Lattner9efd1182010-04-04 19:09:29 +000065/// EmitSLEB128 - emit the specified signed leb128 value.
David Blaikie5acff7e2013-06-23 18:31:11 +000066void AsmPrinter::EmitSLEB128(int64_t Value, const char *Desc) const {
Chris Lattner9efd1182010-04-04 19:09:29 +000067 if (isVerbose() && Desc)
68 OutStreamer.AddComment(Desc);
Chris Lattner9efd1182010-04-04 19:09:29 +000069
Benjamin Kramerc74798d2011-11-05 11:52:44 +000070 OutStreamer.EmitSLEB128IntValue(Value);
Chris Lattner9efd1182010-04-04 19:09:29 +000071}
72
73/// EmitULEB128 - emit the specified signed leb128 value.
David Blaikie5acff7e2013-06-23 18:31:11 +000074void AsmPrinter::EmitULEB128(uint64_t Value, const char *Desc,
Chris Lattner9efd1182010-04-04 19:09:29 +000075 unsigned PadTo) const {
76 if (isVerbose() && Desc)
77 OutStreamer.AddComment(Desc);
Rafael Espindola38d07562010-11-04 18:17:08 +000078
Eric Christopherbf7bc492013-01-09 03:52:05 +000079 OutStreamer.EmitULEB128IntValue(Value, PadTo);
Chris Lattner9efd1182010-04-04 19:09:29 +000080}
81
Chris Lattnerbaf2be02010-04-04 20:01:25 +000082/// EmitCFAByte - Emit a .byte 42 directive for a DW_CFA_xxx value.
83void AsmPrinter::EmitCFAByte(unsigned Val) const {
84 if (isVerbose()) {
Eric Christopher596077b2013-12-04 22:26:43 +000085 if (Val >= dwarf::DW_CFA_offset && Val < dwarf::DW_CFA_offset + 64)
Eric Christopher1d6bd412012-11-20 20:34:47 +000086 OutStreamer.AddComment("DW_CFA_offset + Reg (" +
Eric Christopher596077b2013-12-04 22:26:43 +000087 Twine(Val - dwarf::DW_CFA_offset) + ")");
Chris Lattnerbaf2be02010-04-04 20:01:25 +000088 else
89 OutStreamer.AddComment(dwarf::CallFrameString(Val));
90 }
Eric Christopherce0cfce2013-01-09 01:35:34 +000091 OutStreamer.EmitIntValue(Val, 1);
Chris Lattnerbaf2be02010-04-04 20:01:25 +000092}
93
Chris Lattnerb75af3c2010-04-04 20:04:21 +000094static const char *DecodeDWARFEncoding(unsigned Encoding) {
95 switch (Encoding) {
Eric Christopher596077b2013-12-04 22:26:43 +000096 case dwarf::DW_EH_PE_absptr:
97 return "absptr";
98 case dwarf::DW_EH_PE_omit:
99 return "omit";
100 case dwarf::DW_EH_PE_pcrel:
101 return "pcrel";
102 case dwarf::DW_EH_PE_udata4:
103 return "udata4";
104 case dwarf::DW_EH_PE_udata8:
105 return "udata8";
106 case dwarf::DW_EH_PE_sdata4:
107 return "sdata4";
108 case dwarf::DW_EH_PE_sdata8:
109 return "sdata8";
110 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
111 return "pcrel udata4";
112 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
113 return "pcrel sdata4";
114 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
115 return "pcrel udata8";
116 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
117 return "pcrel sdata8";
118 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4
119 :
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000120 return "indirect pcrel udata4";
Eric Christopher596077b2013-12-04 22:26:43 +0000121 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4
122 :
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000123 return "indirect pcrel sdata4";
Eric Christopher596077b2013-12-04 22:26:43 +0000124 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8
125 :
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000126 return "indirect pcrel udata8";
Eric Christopher596077b2013-12-04 22:26:43 +0000127 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8
128 :
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000129 return "indirect pcrel sdata8";
130 }
Eric Christopher1d6bd412012-11-20 20:34:47 +0000131
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000132 return "<unknown encoding>";
133}
134
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000135/// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
136/// encoding. If verbose assembly output is enabled, we output comments
137/// describing the encoding. Desc is an optional string saying what the
138/// encoding is specifying (e.g. "LSDA").
Chris Lattneraabc6042010-04-04 23:41:46 +0000139void AsmPrinter::EmitEncodingByte(unsigned Val, const char *Desc) const {
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000140 if (isVerbose()) {
Eric Christophercb7119e2013-12-04 22:29:02 +0000141 if (Desc)
Eric Christopher596077b2013-12-04 22:26:43 +0000142 OutStreamer.AddComment(Twine(Desc) + " Encoding = " +
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000143 Twine(DecodeDWARFEncoding(Val)));
144 else
Eric Christopher596077b2013-12-04 22:26:43 +0000145 OutStreamer.AddComment(Twine("Encoding = ") + DecodeDWARFEncoding(Val));
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000146 }
Eric Christopher1d6bd412012-11-20 20:34:47 +0000147
Eric Christopherce0cfce2013-01-09 01:35:34 +0000148 OutStreamer.EmitIntValue(Val, 1);
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000149}
150
Chris Lattnere619c0d2010-04-04 20:20:50 +0000151/// GetSizeOfEncodedValue - Return the size of the encoding in bytes.
152unsigned AsmPrinter::GetSizeOfEncodedValue(unsigned Encoding) const {
153 if (Encoding == dwarf::DW_EH_PE_omit)
154 return 0;
Eric Christopher1d6bd412012-11-20 20:34:47 +0000155
Chris Lattnere619c0d2010-04-04 20:20:50 +0000156 switch (Encoding & 0x07) {
Eric Christopher596077b2013-12-04 22:26:43 +0000157 default:
158 llvm_unreachable("Invalid encoded value.");
159 case dwarf::DW_EH_PE_absptr:
Eric Christopherd9134482014-08-04 21:25:23 +0000160 return TM.getSubtargetImpl()->getDataLayout()->getPointerSize();
Eric Christopher596077b2013-12-04 22:26:43 +0000161 case dwarf::DW_EH_PE_udata2:
162 return 2;
163 case dwarf::DW_EH_PE_udata4:
164 return 4;
165 case dwarf::DW_EH_PE_udata8:
166 return 8;
Chris Lattnere619c0d2010-04-04 20:20:50 +0000167 }
168}
169
Eric Christopher1d6bd412012-11-20 20:34:47 +0000170void AsmPrinter::EmitTTypeReference(const GlobalValue *GV,
171 unsigned Encoding) const {
Anton Korobeynikov097b0e92012-11-19 21:17:20 +0000172 if (GV) {
173 const TargetLoweringObjectFile &TLOF = getObjFileLowering();
Eric Christopher1d6bd412012-11-20 20:34:47 +0000174
Anton Korobeynikov097b0e92012-11-19 21:17:20 +0000175 const MCExpr *Exp =
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000176 TLOF.getTTypeGlobalReference(GV, Encoding, *Mang, TM, MMI, OutStreamer);
Eric Christopherce0cfce2013-01-09 01:35:34 +0000177 OutStreamer.EmitValue(Exp, GetSizeOfEncodedValue(Encoding));
Anton Korobeynikov097b0e92012-11-19 21:17:20 +0000178 } else
Eric Christopherce0cfce2013-01-09 01:35:34 +0000179 OutStreamer.EmitIntValue(0, GetSizeOfEncodedValue(Encoding));
Chris Lattnere619c0d2010-04-04 20:20:50 +0000180}
Chris Lattner70a4fce2010-04-04 23:25:33 +0000181
182/// EmitSectionOffset - Emit the 4-byte offset of Label from the start of its
183/// section. This can be done with a special directive if the target supports
184/// it (e.g. cygwin) or by emitting it as an offset from a label at the start
185/// of the section.
186///
187/// SectionLabel is a temporary label emitted at the start of the section that
188/// Label lives in.
189void AsmPrinter::EmitSectionOffset(const MCSymbol *Label,
190 const MCSymbol *SectionLabel) const {
191 // On COFF targets, we have to emit the special .secrel32 directive.
Matt Arsenault034ca0f2013-04-22 22:49:11 +0000192 if (MAI->needsDwarfSectionOffsetDirective()) {
Rafael Espindolad3df3d32011-12-17 01:14:52 +0000193 OutStreamer.EmitCOFFSecRel32(Label);
Chris Lattner70a4fce2010-04-04 23:25:33 +0000194 return;
195 }
Eric Christopher1d6bd412012-11-20 20:34:47 +0000196
Chris Lattner70a4fce2010-04-04 23:25:33 +0000197 // Get the section that we're referring to, based on SectionLabel.
198 const MCSection &Section = SectionLabel->getSection();
Eric Christopher1d6bd412012-11-20 20:34:47 +0000199
Chris Lattner70a4fce2010-04-04 23:25:33 +0000200 // If Label has already been emitted, verify that it is in the same section as
201 // section label for sanity.
202 assert((!Label->isInSection() || &Label->getSection() == &Section) &&
203 "Section offset using wrong section base for label");
Eric Christopher1d6bd412012-11-20 20:34:47 +0000204
Duncan Sandsb847bf52011-03-12 13:07:37 +0000205 // If the section in question will end up with an address of 0 anyway, we can
206 // just emit an absolute reference to save a relocation.
207 if (Section.isBaseAddressKnownZero()) {
Eric Christopherce0cfce2013-01-09 01:35:34 +0000208 OutStreamer.EmitSymbolValue(Label, 4);
Duncan Sandsb847bf52011-03-12 13:07:37 +0000209 return;
210 }
Eric Christopher1d6bd412012-11-20 20:34:47 +0000211
Chris Lattner70a4fce2010-04-04 23:25:33 +0000212 // Otherwise, emit it as a label difference from the start of the section.
213 EmitLabelDifference(Label, SectionLabel, 4);
214}
215
Adrian Prantl42a0d8c2014-04-27 18:50:45 +0000216// Some targets do not provide a DWARF register number for every
217// register. This function attempts to emit a DWARF register by
218// emitting a piece of a super-register or by piecing together
219// multiple subregisters that alias the register.
Adrian Prantld34db652014-04-27 18:25:45 +0000220void AsmPrinter::EmitDwarfRegOpPiece(ByteStreamer &Streamer,
221 const MachineLocation &MLoc,
222 unsigned PieceSizeInBits,
223 unsigned PieceOffsetInBits) const {
Adrian Prantl42a0d8c2014-04-27 18:50:45 +0000224 assert(MLoc.isReg() && "MLoc must be a register");
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000225 DebugLocDwarfExpression Expr(TM, Streamer);
226 Expr.AddMachineRegPiece(MLoc.getReg(), PieceSizeInBits, PieceOffsetInBits);
227}
Eric Christopher698a8ab2014-03-07 01:44:14 +0000228
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000229void AsmPrinter::EmitDwarfOpPiece(ByteStreamer &Streamer,
230 unsigned PieceSizeInBits,
231 unsigned PieceOffsetInBits) const {
232 DebugLocDwarfExpression Expr(TM, Streamer);
233 Expr.AddOpPiece(PieceSizeInBits, PieceOffsetInBits);
Eric Christopher698a8ab2014-03-07 01:44:14 +0000234}
235
236/// EmitDwarfRegOp - Emit dwarf register operation.
Eric Christopher29e874d2014-03-07 22:40:37 +0000237void AsmPrinter::EmitDwarfRegOp(ByteStreamer &Streamer,
238 const MachineLocation &MLoc,
Eric Christopher698a8ab2014-03-07 01:44:14 +0000239 bool Indirect) const {
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000240 DebugLocDwarfExpression Expr(TM, Streamer);
Eric Christopherd9134482014-08-04 21:25:23 +0000241 const TargetRegisterInfo *TRI = TM.getSubtargetImpl()->getRegisterInfo();
Eric Christopher698a8ab2014-03-07 01:44:14 +0000242 int Reg = TRI->getDwarfRegNum(MLoc.getReg(), false);
243 if (Reg < 0) {
244 // We assume that pointers are always in an addressable register.
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000245 if (Indirect || MLoc.isIndirect())
Eric Christopher698a8ab2014-03-07 01:44:14 +0000246 // FIXME: We have no reasonable way of handling errors in here. The
247 // caller might be in the middle of a dwarf expression. We should
248 // probably assert that Reg >= 0 once debug info generation is more
249 // mature.
Adrian Prantl337e3602015-01-12 23:03:23 +0000250 return Expr.EmitOp(dwarf::DW_OP_nop,
251 "nop (could not find a dwarf register number)");
Eric Christopher698a8ab2014-03-07 01:44:14 +0000252
253 // Attempt to find a valid super- or sub-register.
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000254 return Expr.AddMachineRegPiece(MLoc.getReg());
Eric Christopher698a8ab2014-03-07 01:44:14 +0000255 }
256
257 if (MLoc.isIndirect())
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000258 Expr.AddRegIndirect(Reg, MLoc.getOffset(), Indirect);
Eric Christopher698a8ab2014-03-07 01:44:14 +0000259 else if (Indirect)
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000260 Expr.AddRegIndirect(Reg, 0, false);
Eric Christopher698a8ab2014-03-07 01:44:14 +0000261 else
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000262 Expr.AddReg(Reg);
Eric Christopher698a8ab2014-03-07 01:44:14 +0000263}
264
Chris Lattneraabc6042010-04-04 23:41:46 +0000265//===----------------------------------------------------------------------===//
266// Dwarf Lowering Routines
267//===----------------------------------------------------------------------===//
Chris Lattner70a4fce2010-04-04 23:25:33 +0000268
Rafael Espindola227144c2013-05-13 01:16:13 +0000269void AsmPrinter::emitCFIInstruction(const MCCFIInstruction &Inst) const {
270 switch (Inst.getOperation()) {
271 default:
272 llvm_unreachable("Unexpected instruction");
273 case MCCFIInstruction::OpDefCfaOffset:
274 OutStreamer.EmitCFIDefCfaOffset(Inst.getOffset());
275 break;
276 case MCCFIInstruction::OpDefCfa:
277 OutStreamer.EmitCFIDefCfa(Inst.getRegister(), Inst.getOffset());
278 break;
279 case MCCFIInstruction::OpDefCfaRegister:
280 OutStreamer.EmitCFIDefCfaRegister(Inst.getRegister());
281 break;
282 case MCCFIInstruction::OpOffset:
283 OutStreamer.EmitCFIOffset(Inst.getRegister(), Inst.getOffset());
284 break;
Venkatraman Govindaraju4c0cdd72013-09-26 15:11:00 +0000285 case MCCFIInstruction::OpRegister:
286 OutStreamer.EmitCFIRegister(Inst.getRegister(), Inst.getRegister2());
287 break;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000288 case MCCFIInstruction::OpWindowSave:
289 OutStreamer.EmitCFIWindowSave();
290 break;
Oliver Stannardb14c6252014-04-02 16:10:33 +0000291 case MCCFIInstruction::OpSameValue:
292 OutStreamer.EmitCFISameValue(Inst.getRegister());
293 break;
Rafael Espindolabeb74c32011-04-15 20:32:03 +0000294 }
295}