blob: d0958c1f641a81ce53a56f587075b4062a4dee66 [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"
Eric Christopher73302642015-02-19 23:29:42 +000018#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/DataLayout.h"
Chris Lattner9efd1182010-04-04 19:09:29 +000020#include "llvm/MC/MCAsmInfo.h"
Eric Christopher73302642015-02-19 23:29:42 +000021#include "llvm/MC/MCRegisterInfo.h"
Chris Lattner70a4fce2010-04-04 23:25:33 +000022#include "llvm/MC/MCSection.h"
Chris Lattner9efd1182010-04-04 19:09:29 +000023#include "llvm/MC/MCStreamer.h"
Chris Lattner70a4fce2010-04-04 23:25:33 +000024#include "llvm/MC/MCSymbol.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/MC/MachineLocation.h"
26#include "llvm/Support/Dwarf.h"
27#include "llvm/Support/ErrorHandling.h"
Chris Lattnere619c0d2010-04-04 20:20:50 +000028#include "llvm/Target/TargetLoweringObjectFile.h"
29#include "llvm/Target/TargetMachine.h"
Chris Lattner9efd1182010-04-04 19:09:29 +000030using namespace llvm;
31
Chandler Carruth1b9dde02014-04-22 02:02:50 +000032#define DEBUG_TYPE "asm-printer"
33
Adrian Prantl337e3602015-01-12 23:03:23 +000034void DebugLocDwarfExpression::EmitOp(uint8_t Op, const char *Comment) {
35 BS.EmitInt8(
36 Op, Comment ? Twine(Comment) + " " + dwarf::OperationEncodingString(Op)
37 : dwarf::OperationEncodingString(Op));
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000038}
Adrian Prantl66f25952015-01-13 00:04:06 +000039
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000040void DebugLocDwarfExpression::EmitSigned(int Value) {
41 BS.EmitSLEB128(Value, Twine(Value));
42}
Adrian Prantl66f25952015-01-13 00:04:06 +000043
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000044void DebugLocDwarfExpression::EmitUnsigned(unsigned Value) {
45 BS.EmitULEB128(Value, Twine(Value));
46}
47
Adrian Prantl8995f5c2015-01-13 23:10:43 +000048bool DebugLocDwarfExpression::isFrameRegister(unsigned MachineReg) {
49 // This information is not available while emitting .debug_loc entries.
50 return false;
Adrian Prantl66f25952015-01-13 00:04:06 +000051}
52
Chris Lattneraabc6042010-04-04 23:41:46 +000053//===----------------------------------------------------------------------===//
54// Dwarf Emission Helper Routines
55//===----------------------------------------------------------------------===//
56
Chris Lattner9efd1182010-04-04 19:09:29 +000057/// EmitSLEB128 - emit the specified signed leb128 value.
David Blaikie5acff7e2013-06-23 18:31:11 +000058void AsmPrinter::EmitSLEB128(int64_t Value, const char *Desc) const {
Chris Lattner9efd1182010-04-04 19:09:29 +000059 if (isVerbose() && Desc)
60 OutStreamer.AddComment(Desc);
Chris Lattner9efd1182010-04-04 19:09:29 +000061
Benjamin Kramerc74798d2011-11-05 11:52:44 +000062 OutStreamer.EmitSLEB128IntValue(Value);
Chris Lattner9efd1182010-04-04 19:09:29 +000063}
64
65/// EmitULEB128 - emit the specified signed leb128 value.
David Blaikie5acff7e2013-06-23 18:31:11 +000066void AsmPrinter::EmitULEB128(uint64_t Value, const char *Desc,
Chris Lattner9efd1182010-04-04 19:09:29 +000067 unsigned PadTo) const {
68 if (isVerbose() && Desc)
69 OutStreamer.AddComment(Desc);
Rafael Espindola38d07562010-11-04 18:17:08 +000070
Eric Christopherbf7bc492013-01-09 03:52:05 +000071 OutStreamer.EmitULEB128IntValue(Value, PadTo);
Chris Lattner9efd1182010-04-04 19:09:29 +000072}
73
Chris Lattnerbaf2be02010-04-04 20:01:25 +000074/// EmitCFAByte - Emit a .byte 42 directive for a DW_CFA_xxx value.
75void AsmPrinter::EmitCFAByte(unsigned Val) const {
76 if (isVerbose()) {
Eric Christopher596077b2013-12-04 22:26:43 +000077 if (Val >= dwarf::DW_CFA_offset && Val < dwarf::DW_CFA_offset + 64)
Eric Christopher1d6bd412012-11-20 20:34:47 +000078 OutStreamer.AddComment("DW_CFA_offset + Reg (" +
Eric Christopher596077b2013-12-04 22:26:43 +000079 Twine(Val - dwarf::DW_CFA_offset) + ")");
Chris Lattnerbaf2be02010-04-04 20:01:25 +000080 else
81 OutStreamer.AddComment(dwarf::CallFrameString(Val));
82 }
Eric Christopherce0cfce2013-01-09 01:35:34 +000083 OutStreamer.EmitIntValue(Val, 1);
Chris Lattnerbaf2be02010-04-04 20:01:25 +000084}
85
Chris Lattnerb75af3c2010-04-04 20:04:21 +000086static const char *DecodeDWARFEncoding(unsigned Encoding) {
87 switch (Encoding) {
Eric Christopher596077b2013-12-04 22:26:43 +000088 case dwarf::DW_EH_PE_absptr:
89 return "absptr";
90 case dwarf::DW_EH_PE_omit:
91 return "omit";
92 case dwarf::DW_EH_PE_pcrel:
93 return "pcrel";
94 case dwarf::DW_EH_PE_udata4:
95 return "udata4";
96 case dwarf::DW_EH_PE_udata8:
97 return "udata8";
98 case dwarf::DW_EH_PE_sdata4:
99 return "sdata4";
100 case dwarf::DW_EH_PE_sdata8:
101 return "sdata8";
102 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
103 return "pcrel udata4";
104 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
105 return "pcrel sdata4";
106 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
107 return "pcrel udata8";
108 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
109 return "pcrel sdata8";
110 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4
111 :
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000112 return "indirect pcrel udata4";
Eric Christopher596077b2013-12-04 22:26:43 +0000113 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4
114 :
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000115 return "indirect pcrel sdata4";
Eric Christopher596077b2013-12-04 22:26:43 +0000116 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8
117 :
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000118 return "indirect pcrel udata8";
Eric Christopher596077b2013-12-04 22:26:43 +0000119 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8
120 :
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000121 return "indirect pcrel sdata8";
122 }
Eric Christopher1d6bd412012-11-20 20:34:47 +0000123
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000124 return "<unknown encoding>";
125}
126
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000127/// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
128/// encoding. If verbose assembly output is enabled, we output comments
129/// describing the encoding. Desc is an optional string saying what the
130/// encoding is specifying (e.g. "LSDA").
Chris Lattneraabc6042010-04-04 23:41:46 +0000131void AsmPrinter::EmitEncodingByte(unsigned Val, const char *Desc) const {
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000132 if (isVerbose()) {
Eric Christophercb7119e2013-12-04 22:29:02 +0000133 if (Desc)
Eric Christopher596077b2013-12-04 22:26:43 +0000134 OutStreamer.AddComment(Twine(Desc) + " Encoding = " +
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000135 Twine(DecodeDWARFEncoding(Val)));
136 else
Eric Christopher596077b2013-12-04 22:26:43 +0000137 OutStreamer.AddComment(Twine("Encoding = ") + DecodeDWARFEncoding(Val));
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000138 }
Eric Christopher1d6bd412012-11-20 20:34:47 +0000139
Eric Christopherce0cfce2013-01-09 01:35:34 +0000140 OutStreamer.EmitIntValue(Val, 1);
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000141}
142
Chris Lattnere619c0d2010-04-04 20:20:50 +0000143/// GetSizeOfEncodedValue - Return the size of the encoding in bytes.
144unsigned AsmPrinter::GetSizeOfEncodedValue(unsigned Encoding) const {
145 if (Encoding == dwarf::DW_EH_PE_omit)
146 return 0;
Eric Christopher1d6bd412012-11-20 20:34:47 +0000147
Chris Lattnere619c0d2010-04-04 20:20:50 +0000148 switch (Encoding & 0x07) {
Eric Christopher596077b2013-12-04 22:26:43 +0000149 default:
150 llvm_unreachable("Invalid encoded value.");
151 case dwarf::DW_EH_PE_absptr:
Eric Christopher8b770652015-01-26 19:03:15 +0000152 return TM.getDataLayout()->getPointerSize();
Eric Christopher596077b2013-12-04 22:26:43 +0000153 case dwarf::DW_EH_PE_udata2:
154 return 2;
155 case dwarf::DW_EH_PE_udata4:
156 return 4;
157 case dwarf::DW_EH_PE_udata8:
158 return 8;
Chris Lattnere619c0d2010-04-04 20:20:50 +0000159 }
160}
161
Eric Christopher1d6bd412012-11-20 20:34:47 +0000162void AsmPrinter::EmitTTypeReference(const GlobalValue *GV,
163 unsigned Encoding) const {
Anton Korobeynikov097b0e92012-11-19 21:17:20 +0000164 if (GV) {
165 const TargetLoweringObjectFile &TLOF = getObjFileLowering();
Eric Christopher1d6bd412012-11-20 20:34:47 +0000166
Anton Korobeynikov097b0e92012-11-19 21:17:20 +0000167 const MCExpr *Exp =
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000168 TLOF.getTTypeGlobalReference(GV, Encoding, *Mang, TM, MMI, OutStreamer);
Eric Christopherce0cfce2013-01-09 01:35:34 +0000169 OutStreamer.EmitValue(Exp, GetSizeOfEncodedValue(Encoding));
Anton Korobeynikov097b0e92012-11-19 21:17:20 +0000170 } else
Eric Christopherce0cfce2013-01-09 01:35:34 +0000171 OutStreamer.EmitIntValue(0, GetSizeOfEncodedValue(Encoding));
Chris Lattnere619c0d2010-04-04 20:20:50 +0000172}
Chris Lattner70a4fce2010-04-04 23:25:33 +0000173
174/// EmitSectionOffset - Emit the 4-byte offset of Label from the start of its
175/// section. This can be done with a special directive if the target supports
176/// it (e.g. cygwin) or by emitting it as an offset from a label at the start
177/// of the section.
178///
179/// SectionLabel is a temporary label emitted at the start of the section that
180/// Label lives in.
181void AsmPrinter::EmitSectionOffset(const MCSymbol *Label,
182 const MCSymbol *SectionLabel) const {
183 // On COFF targets, we have to emit the special .secrel32 directive.
Matt Arsenault034ca0f2013-04-22 22:49:11 +0000184 if (MAI->needsDwarfSectionOffsetDirective()) {
Rafael Espindolad3df3d32011-12-17 01:14:52 +0000185 OutStreamer.EmitCOFFSecRel32(Label);
Chris Lattner70a4fce2010-04-04 23:25:33 +0000186 return;
187 }
Eric Christopher1d6bd412012-11-20 20:34:47 +0000188
Chris Lattner70a4fce2010-04-04 23:25:33 +0000189 // Get the section that we're referring to, based on SectionLabel.
190 const MCSection &Section = SectionLabel->getSection();
Eric Christopher1d6bd412012-11-20 20:34:47 +0000191
Chris Lattner70a4fce2010-04-04 23:25:33 +0000192 // If Label has already been emitted, verify that it is in the same section as
193 // section label for sanity.
194 assert((!Label->isInSection() || &Label->getSection() == &Section) &&
195 "Section offset using wrong section base for label");
Eric Christopher1d6bd412012-11-20 20:34:47 +0000196
Duncan Sandsb847bf52011-03-12 13:07:37 +0000197 // If the section in question will end up with an address of 0 anyway, we can
198 // just emit an absolute reference to save a relocation.
199 if (Section.isBaseAddressKnownZero()) {
Eric Christopherce0cfce2013-01-09 01:35:34 +0000200 OutStreamer.EmitSymbolValue(Label, 4);
Duncan Sandsb847bf52011-03-12 13:07:37 +0000201 return;
202 }
Eric Christopher1d6bd412012-11-20 20:34:47 +0000203
Chris Lattner70a4fce2010-04-04 23:25:33 +0000204 // Otherwise, emit it as a label difference from the start of the section.
205 EmitLabelDifference(Label, SectionLabel, 4);
206}
207
Adrian Prantl42a0d8c2014-04-27 18:50:45 +0000208// Some targets do not provide a DWARF register number for every
209// register. This function attempts to emit a DWARF register by
210// emitting a piece of a super-register or by piecing together
211// multiple subregisters that alias the register.
Adrian Prantld34db652014-04-27 18:25:45 +0000212void AsmPrinter::EmitDwarfRegOpPiece(ByteStreamer &Streamer,
213 const MachineLocation &MLoc,
214 unsigned PieceSizeInBits,
215 unsigned PieceOffsetInBits) const {
Adrian Prantl42a0d8c2014-04-27 18:50:45 +0000216 assert(MLoc.isReg() && "MLoc must be a register");
Adrian Prantla4c30d62015-01-12 23:36:56 +0000217 DebugLocDwarfExpression Expr(*this, Streamer);
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000218 Expr.AddMachineRegPiece(MLoc.getReg(), PieceSizeInBits, PieceOffsetInBits);
219}
Eric Christopher698a8ab2014-03-07 01:44:14 +0000220
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000221void AsmPrinter::EmitDwarfOpPiece(ByteStreamer &Streamer,
222 unsigned PieceSizeInBits,
223 unsigned PieceOffsetInBits) const {
Adrian Prantla4c30d62015-01-12 23:36:56 +0000224 DebugLocDwarfExpression Expr(*this, Streamer);
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000225 Expr.AddOpPiece(PieceSizeInBits, PieceOffsetInBits);
Eric Christopher698a8ab2014-03-07 01:44:14 +0000226}
227
228/// EmitDwarfRegOp - Emit dwarf register operation.
Eric Christopher29e874d2014-03-07 22:40:37 +0000229void AsmPrinter::EmitDwarfRegOp(ByteStreamer &Streamer,
Adrian Prantl5883af32015-01-19 17:57:29 +0000230 const MachineLocation &MLoc) const {
Adrian Prantla4c30d62015-01-12 23:36:56 +0000231 DebugLocDwarfExpression Expr(*this, Streamer);
Eric Christopher73302642015-02-19 23:29:42 +0000232 const MCRegisterInfo *MRI = MMI->getContext().getRegisterInfo();
233 int Reg = MRI->getDwarfRegNum(MLoc.getReg(), false);
Eric Christopher698a8ab2014-03-07 01:44:14 +0000234 if (Reg < 0) {
235 // We assume that pointers are always in an addressable register.
Adrian Prantl5883af32015-01-19 17:57:29 +0000236 if (MLoc.isIndirect())
Eric Christopher698a8ab2014-03-07 01:44:14 +0000237 // FIXME: We have no reasonable way of handling errors in here. The
238 // caller might be in the middle of a dwarf expression. We should
239 // probably assert that Reg >= 0 once debug info generation is more
240 // mature.
Adrian Prantl337e3602015-01-12 23:03:23 +0000241 return Expr.EmitOp(dwarf::DW_OP_nop,
242 "nop (could not find a dwarf register number)");
Eric Christopher698a8ab2014-03-07 01:44:14 +0000243
244 // Attempt to find a valid super- or sub-register.
Adrian Prantlad768c32015-01-14 01:01:28 +0000245 if (!Expr.AddMachineRegPiece(MLoc.getReg()))
246 Expr.EmitOp(dwarf::DW_OP_nop,
247 "nop (could not find a dwarf register number)");
248 return;
Eric Christopher698a8ab2014-03-07 01:44:14 +0000249 }
250
251 if (MLoc.isIndirect())
Adrian Prantl5883af32015-01-19 17:57:29 +0000252 Expr.AddRegIndirect(Reg, MLoc.getOffset());
Eric Christopher698a8ab2014-03-07 01:44:14 +0000253 else
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000254 Expr.AddReg(Reg);
Eric Christopher698a8ab2014-03-07 01:44:14 +0000255}
256
Chris Lattneraabc6042010-04-04 23:41:46 +0000257//===----------------------------------------------------------------------===//
258// Dwarf Lowering Routines
259//===----------------------------------------------------------------------===//
Chris Lattner70a4fce2010-04-04 23:25:33 +0000260
Rafael Espindola227144c2013-05-13 01:16:13 +0000261void AsmPrinter::emitCFIInstruction(const MCCFIInstruction &Inst) const {
262 switch (Inst.getOperation()) {
263 default:
264 llvm_unreachable("Unexpected instruction");
265 case MCCFIInstruction::OpDefCfaOffset:
266 OutStreamer.EmitCFIDefCfaOffset(Inst.getOffset());
267 break;
268 case MCCFIInstruction::OpDefCfa:
269 OutStreamer.EmitCFIDefCfa(Inst.getRegister(), Inst.getOffset());
270 break;
271 case MCCFIInstruction::OpDefCfaRegister:
272 OutStreamer.EmitCFIDefCfaRegister(Inst.getRegister());
273 break;
274 case MCCFIInstruction::OpOffset:
275 OutStreamer.EmitCFIOffset(Inst.getRegister(), Inst.getOffset());
276 break;
Venkatraman Govindaraju4c0cdd72013-09-26 15:11:00 +0000277 case MCCFIInstruction::OpRegister:
278 OutStreamer.EmitCFIRegister(Inst.getRegister(), Inst.getRegister2());
279 break;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000280 case MCCFIInstruction::OpWindowSave:
281 OutStreamer.EmitCFIWindowSave();
282 break;
Oliver Stannardb14c6252014-04-02 16:10:33 +0000283 case MCCFIInstruction::OpSameValue:
284 OutStreamer.EmitCFISameValue(Inst.getRegister());
285 break;
Rafael Espindolabeb74c32011-04-15 20:32:03 +0000286 }
287}