blob: a6c6373447bd1171e5f5841cfbcf903ab7766779 [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 Prantl92da14b2015-03-02 22:02:33 +000015#include "DwarfDebug.h"
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000016#include "DwarfExpression.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/ADT/Twine.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000018#include "llvm/CodeGen/AsmPrinter.h"
Frederic Riss9412d632015-03-04 02:30:17 +000019#include "llvm/CodeGen/DIE.h"
Adrian Prantlb846acc2015-03-02 22:02:36 +000020#include "llvm/CodeGen/MachineFunction.h"
Eric Christopher73302642015-02-19 23:29:42 +000021#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/DataLayout.h"
Chris Lattner9efd1182010-04-04 19:09:29 +000023#include "llvm/MC/MCAsmInfo.h"
Eric Christopher73302642015-02-19 23:29:42 +000024#include "llvm/MC/MCRegisterInfo.h"
Chris Lattner70a4fce2010-04-04 23:25:33 +000025#include "llvm/MC/MCSection.h"
Chris Lattner9efd1182010-04-04 19:09:29 +000026#include "llvm/MC/MCStreamer.h"
Chris Lattner70a4fce2010-04-04 23:25:33 +000027#include "llvm/MC/MCSymbol.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000028#include "llvm/MC/MachineLocation.h"
29#include "llvm/Support/Dwarf.h"
30#include "llvm/Support/ErrorHandling.h"
Chris Lattnere619c0d2010-04-04 20:20:50 +000031#include "llvm/Target/TargetLoweringObjectFile.h"
32#include "llvm/Target/TargetMachine.h"
Adrian Prantl92da14b2015-03-02 22:02:33 +000033#include "llvm/Target/TargetSubtargetInfo.h"
Chris Lattner9efd1182010-04-04 19:09:29 +000034using namespace llvm;
35
Chandler Carruth1b9dde02014-04-22 02:02:50 +000036#define DEBUG_TYPE "asm-printer"
37
Chris Lattneraabc6042010-04-04 23:41:46 +000038//===----------------------------------------------------------------------===//
39// Dwarf Emission Helper Routines
40//===----------------------------------------------------------------------===//
41
Chris Lattner9efd1182010-04-04 19:09:29 +000042/// EmitSLEB128 - emit the specified signed leb128 value.
David Blaikie5acff7e2013-06-23 18:31:11 +000043void AsmPrinter::EmitSLEB128(int64_t Value, const char *Desc) const {
Chris Lattner9efd1182010-04-04 19:09:29 +000044 if (isVerbose() && Desc)
Lang Hames9ff69c82015-04-24 19:11:51 +000045 OutStreamer->AddComment(Desc);
Chris Lattner9efd1182010-04-04 19:09:29 +000046
Lang Hames9ff69c82015-04-24 19:11:51 +000047 OutStreamer->EmitSLEB128IntValue(Value);
Chris Lattner9efd1182010-04-04 19:09:29 +000048}
49
Maksim Panchenko07b754d2015-09-19 04:01:19 +000050/// EmitULEB128 - emit the specified unsigned leb128 value.
David Blaikie5acff7e2013-06-23 18:31:11 +000051void AsmPrinter::EmitULEB128(uint64_t Value, const char *Desc,
Chris Lattner9efd1182010-04-04 19:09:29 +000052 unsigned PadTo) const {
53 if (isVerbose() && Desc)
Lang Hames9ff69c82015-04-24 19:11:51 +000054 OutStreamer->AddComment(Desc);
Rafael Espindola38d07562010-11-04 18:17:08 +000055
Lang Hames9ff69c82015-04-24 19:11:51 +000056 OutStreamer->EmitULEB128IntValue(Value, PadTo);
Chris Lattner9efd1182010-04-04 19:09:29 +000057}
58
Chris Lattnerb75af3c2010-04-04 20:04:21 +000059static const char *DecodeDWARFEncoding(unsigned Encoding) {
60 switch (Encoding) {
Eric Christopher596077b2013-12-04 22:26:43 +000061 case dwarf::DW_EH_PE_absptr:
62 return "absptr";
63 case dwarf::DW_EH_PE_omit:
64 return "omit";
65 case dwarf::DW_EH_PE_pcrel:
66 return "pcrel";
67 case dwarf::DW_EH_PE_udata4:
68 return "udata4";
69 case dwarf::DW_EH_PE_udata8:
70 return "udata8";
71 case dwarf::DW_EH_PE_sdata4:
72 return "sdata4";
73 case dwarf::DW_EH_PE_sdata8:
74 return "sdata8";
75 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
76 return "pcrel udata4";
77 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
78 return "pcrel sdata4";
79 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
80 return "pcrel udata8";
81 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
82 return "pcrel sdata8";
83 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4
84 :
Chris Lattnerb75af3c2010-04-04 20:04:21 +000085 return "indirect pcrel udata4";
Eric Christopher596077b2013-12-04 22:26:43 +000086 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4
87 :
Chris Lattnerb75af3c2010-04-04 20:04:21 +000088 return "indirect pcrel sdata4";
Eric Christopher596077b2013-12-04 22:26:43 +000089 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8
90 :
Chris Lattnerb75af3c2010-04-04 20:04:21 +000091 return "indirect pcrel udata8";
Eric Christopher596077b2013-12-04 22:26:43 +000092 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8
93 :
Chris Lattnerb75af3c2010-04-04 20:04:21 +000094 return "indirect pcrel sdata8";
95 }
Eric Christopher1d6bd412012-11-20 20:34:47 +000096
Chris Lattnerb75af3c2010-04-04 20:04:21 +000097 return "<unknown encoding>";
98}
99
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000100/// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
101/// encoding. If verbose assembly output is enabled, we output comments
102/// describing the encoding. Desc is an optional string saying what the
103/// encoding is specifying (e.g. "LSDA").
Chris Lattneraabc6042010-04-04 23:41:46 +0000104void AsmPrinter::EmitEncodingByte(unsigned Val, const char *Desc) const {
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000105 if (isVerbose()) {
Eric Christophercb7119e2013-12-04 22:29:02 +0000106 if (Desc)
Lang Hames9ff69c82015-04-24 19:11:51 +0000107 OutStreamer->AddComment(Twine(Desc) + " Encoding = " +
108 Twine(DecodeDWARFEncoding(Val)));
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000109 else
Lang Hames9ff69c82015-04-24 19:11:51 +0000110 OutStreamer->AddComment(Twine("Encoding = ") + DecodeDWARFEncoding(Val));
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000111 }
Eric Christopher1d6bd412012-11-20 20:34:47 +0000112
Lang Hames9ff69c82015-04-24 19:11:51 +0000113 OutStreamer->EmitIntValue(Val, 1);
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000114}
115
Chris Lattnere619c0d2010-04-04 20:20:50 +0000116/// GetSizeOfEncodedValue - Return the size of the encoding in bytes.
117unsigned AsmPrinter::GetSizeOfEncodedValue(unsigned Encoding) const {
118 if (Encoding == dwarf::DW_EH_PE_omit)
119 return 0;
Eric Christopher1d6bd412012-11-20 20:34:47 +0000120
Chris Lattnere619c0d2010-04-04 20:20:50 +0000121 switch (Encoding & 0x07) {
Eric Christopher596077b2013-12-04 22:26:43 +0000122 default:
123 llvm_unreachable("Invalid encoded value.");
124 case dwarf::DW_EH_PE_absptr:
Mehdi Aminibd7287e2015-07-16 06:11:10 +0000125 return MF->getDataLayout().getPointerSize();
Eric Christopher596077b2013-12-04 22:26:43 +0000126 case dwarf::DW_EH_PE_udata2:
127 return 2;
128 case dwarf::DW_EH_PE_udata4:
129 return 4;
130 case dwarf::DW_EH_PE_udata8:
131 return 8;
Chris Lattnere619c0d2010-04-04 20:20:50 +0000132 }
133}
134
Eric Christopher1d6bd412012-11-20 20:34:47 +0000135void AsmPrinter::EmitTTypeReference(const GlobalValue *GV,
136 unsigned Encoding) const {
Anton Korobeynikov097b0e92012-11-19 21:17:20 +0000137 if (GV) {
138 const TargetLoweringObjectFile &TLOF = getObjFileLowering();
Eric Christopher1d6bd412012-11-20 20:34:47 +0000139
Anton Korobeynikov097b0e92012-11-19 21:17:20 +0000140 const MCExpr *Exp =
Eric Christopher4367c7f2016-09-16 07:33:15 +0000141 TLOF.getTTypeGlobalReference(GV, Encoding, TM, MMI, *OutStreamer);
Lang Hames9ff69c82015-04-24 19:11:51 +0000142 OutStreamer->EmitValue(Exp, GetSizeOfEncodedValue(Encoding));
Anton Korobeynikov097b0e92012-11-19 21:17:20 +0000143 } else
Lang Hames9ff69c82015-04-24 19:11:51 +0000144 OutStreamer->EmitIntValue(0, GetSizeOfEncodedValue(Encoding));
Chris Lattnere619c0d2010-04-04 20:20:50 +0000145}
Chris Lattner70a4fce2010-04-04 23:25:33 +0000146
Rafael Espindola857546e2015-06-16 23:22:02 +0000147void AsmPrinter::emitDwarfSymbolReference(const MCSymbol *Label,
148 bool ForceOffset) const {
149 if (!ForceOffset) {
150 // On COFF targets, we have to emit the special .secrel32 directive.
151 if (MAI->needsDwarfSectionOffsetDirective()) {
152 OutStreamer->EmitCOFFSecRel32(Label);
153 return;
154 }
Eric Christopher1d6bd412012-11-20 20:34:47 +0000155
Rafael Espindola857546e2015-06-16 23:22:02 +0000156 // If the format uses relocations with dwarf, refer to the symbol directly.
157 if (MAI->doesDwarfUseRelocationsAcrossSections()) {
158 OutStreamer->EmitSymbolValue(Label, 4);
159 return;
160 }
Duncan Sandsb847bf52011-03-12 13:07:37 +0000161 }
Eric Christopher1d6bd412012-11-20 20:34:47 +0000162
Chris Lattner70a4fce2010-04-04 23:25:33 +0000163 // Otherwise, emit it as a label difference from the start of the section.
Rafael Espindola063d7252015-03-10 16:58:10 +0000164 EmitLabelDifference(Label, Label->getSection().getBeginSymbol(), 4);
Chris Lattner70a4fce2010-04-04 23:25:33 +0000165}
166
Duncan P. N. Exon Smith1e0d94e2015-05-24 16:48:54 +0000167void AsmPrinter::emitDwarfStringOffset(DwarfStringPoolEntryRef S) const {
168 if (MAI->doesDwarfUseRelocationsAcrossSections()) {
Rafael Espindola857546e2015-06-16 23:22:02 +0000169 emitDwarfSymbolReference(S.getSymbol());
Duncan P. N. Exon Smith1e0d94e2015-05-24 16:48:54 +0000170 return;
171 }
172
173 // Just emit the offset directly; no need for symbol math.
174 EmitInt32(S.getOffset());
175}
176
Eric Christopher698a8ab2014-03-07 01:44:14 +0000177/// EmitDwarfRegOp - Emit dwarf register operation.
Eric Christopher29e874d2014-03-07 22:40:37 +0000178void AsmPrinter::EmitDwarfRegOp(ByteStreamer &Streamer,
Adrian Prantl5883af32015-01-19 17:57:29 +0000179 const MachineLocation &MLoc) const {
Peter Collingbourne96c9ae62016-05-20 19:35:17 +0000180 DebugLocDwarfExpression Expr(getDwarfDebug()->getDwarfVersion(), Streamer);
Eric Christopher73302642015-02-19 23:29:42 +0000181 const MCRegisterInfo *MRI = MMI->getContext().getRegisterInfo();
182 int Reg = MRI->getDwarfRegNum(MLoc.getReg(), false);
Eric Christopher698a8ab2014-03-07 01:44:14 +0000183 if (Reg < 0) {
184 // We assume that pointers are always in an addressable register.
Adrian Prantl5883af32015-01-19 17:57:29 +0000185 if (MLoc.isIndirect())
Eric Christopher698a8ab2014-03-07 01:44:14 +0000186 // FIXME: We have no reasonable way of handling errors in here. The
187 // caller might be in the middle of a dwarf expression. We should
188 // probably assert that Reg >= 0 once debug info generation is more
189 // mature.
Adrian Prantl337e3602015-01-12 23:03:23 +0000190 return Expr.EmitOp(dwarf::DW_OP_nop,
191 "nop (could not find a dwarf register number)");
Eric Christopher698a8ab2014-03-07 01:44:14 +0000192
193 // Attempt to find a valid super- or sub-register.
Peter Collingbourne96c9ae62016-05-20 19:35:17 +0000194 if (!Expr.AddMachineRegPiece(*MF->getSubtarget().getRegisterInfo(),
195 MLoc.getReg()))
Adrian Prantlad768c32015-01-14 01:01:28 +0000196 Expr.EmitOp(dwarf::DW_OP_nop,
197 "nop (could not find a dwarf register number)");
198 return;
Eric Christopher698a8ab2014-03-07 01:44:14 +0000199 }
200
201 if (MLoc.isIndirect())
Adrian Prantl5883af32015-01-19 17:57:29 +0000202 Expr.AddRegIndirect(Reg, MLoc.getOffset());
Eric Christopher698a8ab2014-03-07 01:44:14 +0000203 else
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000204 Expr.AddReg(Reg);
Eric Christopher698a8ab2014-03-07 01:44:14 +0000205}
206
Chris Lattneraabc6042010-04-04 23:41:46 +0000207//===----------------------------------------------------------------------===//
208// Dwarf Lowering Routines
209//===----------------------------------------------------------------------===//
Chris Lattner70a4fce2010-04-04 23:25:33 +0000210
Rafael Espindola227144c2013-05-13 01:16:13 +0000211void AsmPrinter::emitCFIInstruction(const MCCFIInstruction &Inst) const {
212 switch (Inst.getOperation()) {
213 default:
214 llvm_unreachable("Unexpected instruction");
215 case MCCFIInstruction::OpDefCfaOffset:
Lang Hames9ff69c82015-04-24 19:11:51 +0000216 OutStreamer->EmitCFIDefCfaOffset(Inst.getOffset());
Rafael Espindola227144c2013-05-13 01:16:13 +0000217 break;
Michael Kuperstein73dc8522015-11-03 08:17:25 +0000218 case MCCFIInstruction::OpAdjustCfaOffset:
219 OutStreamer->EmitCFIAdjustCfaOffset(Inst.getOffset());
220 break;
Rafael Espindola227144c2013-05-13 01:16:13 +0000221 case MCCFIInstruction::OpDefCfa:
Lang Hames9ff69c82015-04-24 19:11:51 +0000222 OutStreamer->EmitCFIDefCfa(Inst.getRegister(), Inst.getOffset());
Rafael Espindola227144c2013-05-13 01:16:13 +0000223 break;
224 case MCCFIInstruction::OpDefCfaRegister:
Lang Hames9ff69c82015-04-24 19:11:51 +0000225 OutStreamer->EmitCFIDefCfaRegister(Inst.getRegister());
Rafael Espindola227144c2013-05-13 01:16:13 +0000226 break;
227 case MCCFIInstruction::OpOffset:
Lang Hames9ff69c82015-04-24 19:11:51 +0000228 OutStreamer->EmitCFIOffset(Inst.getRegister(), Inst.getOffset());
Rafael Espindola227144c2013-05-13 01:16:13 +0000229 break;
Venkatraman Govindaraju4c0cdd72013-09-26 15:11:00 +0000230 case MCCFIInstruction::OpRegister:
Lang Hames9ff69c82015-04-24 19:11:51 +0000231 OutStreamer->EmitCFIRegister(Inst.getRegister(), Inst.getRegister2());
Venkatraman Govindaraju4c0cdd72013-09-26 15:11:00 +0000232 break;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000233 case MCCFIInstruction::OpWindowSave:
Lang Hames9ff69c82015-04-24 19:11:51 +0000234 OutStreamer->EmitCFIWindowSave();
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000235 break;
Oliver Stannardb14c6252014-04-02 16:10:33 +0000236 case MCCFIInstruction::OpSameValue:
Lang Hames9ff69c82015-04-24 19:11:51 +0000237 OutStreamer->EmitCFISameValue(Inst.getRegister());
Oliver Stannardb14c6252014-04-02 16:10:33 +0000238 break;
Michael Kuperstein259f1502015-10-07 07:01:31 +0000239 case MCCFIInstruction::OpGnuArgsSize:
240 OutStreamer->EmitCFIGnuArgsSize(Inst.getOffset());
241 break;
242 case MCCFIInstruction::OpEscape:
243 OutStreamer->EmitCFIEscape(Inst.getValues());
244 break;
Rafael Espindolabeb74c32011-04-15 20:32:03 +0000245 }
246}
Frederic Riss9412d632015-03-04 02:30:17 +0000247
248void AsmPrinter::emitDwarfDIE(const DIE &Die) const {
Frederic Riss9412d632015-03-04 02:30:17 +0000249 // Emit the code (index) for the abbreviation.
250 if (isVerbose())
Duncan P. N. Exon Smith815a6eb52015-05-27 22:31:41 +0000251 OutStreamer->AddComment("Abbrev [" + Twine(Die.getAbbrevNumber()) + "] 0x" +
252 Twine::utohexstr(Die.getOffset()) + ":0x" +
253 Twine::utohexstr(Die.getSize()) + " " +
254 dwarf::TagString(Die.getTag()));
255 EmitULEB128(Die.getAbbrevNumber());
Frederic Riss9412d632015-03-04 02:30:17 +0000256
Frederic Riss9412d632015-03-04 02:30:17 +0000257 // Emit the DIE attribute values.
Duncan P. N. Exon Smith88a8fc52015-05-27 22:44:06 +0000258 for (const auto &V : Die.values()) {
259 dwarf::Attribute Attr = V.getAttribute();
Duncan P. N. Exon Smith9dbb5012015-06-24 18:48:11 +0000260 assert(V.getForm() && "Too many attributes for DIE (check abbreviation)");
Frederic Riss9412d632015-03-04 02:30:17 +0000261
262 if (isVerbose()) {
Lang Hames9ff69c82015-04-24 19:11:51 +0000263 OutStreamer->AddComment(dwarf::AttributeString(Attr));
Frederic Riss9412d632015-03-04 02:30:17 +0000264 if (Attr == dwarf::DW_AT_accessibility)
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000265 OutStreamer->AddComment(
Duncan P. N. Exon Smith88a8fc52015-05-27 22:44:06 +0000266 dwarf::AccessibilityString(V.getDIEInteger().getValue()));
Frederic Riss9412d632015-03-04 02:30:17 +0000267 }
268
269 // Emit an attribute using the defined form.
Duncan P. N. Exon Smith9dbb5012015-06-24 18:48:11 +0000270 V.EmitValue(this);
Frederic Riss9412d632015-03-04 02:30:17 +0000271 }
272
273 // Emit the DIE children if any.
Duncan P. N. Exon Smith815a6eb52015-05-27 22:31:41 +0000274 if (Die.hasChildren()) {
Duncan P. N. Exon Smith8d3197f2015-05-28 19:56:34 +0000275 for (auto &Child : Die.children())
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000276 emitDwarfDIE(Child);
Frederic Riss9412d632015-03-04 02:30:17 +0000277
Lang Hames9ff69c82015-04-24 19:11:51 +0000278 OutStreamer->AddComment("End Of Children Mark");
Frederic Riss9412d632015-03-04 02:30:17 +0000279 EmitInt8(0);
280 }
281}
282
David Blaikie6196aa02015-11-18 00:34:10 +0000283void AsmPrinter::emitDwarfAbbrev(const DIEAbbrev &Abbrev) const {
284 // Emit the abbreviations code (base 1 index.)
285 EmitULEB128(Abbrev.getNumber(), "Abbreviation Code");
Frederic Riss9412d632015-03-04 02:30:17 +0000286
David Blaikie6196aa02015-11-18 00:34:10 +0000287 // Emit the abbreviations data.
288 Abbrev.Emit(this);
Frederic Riss9412d632015-03-04 02:30:17 +0000289}