blob: 9de36da51a0ad48ca3f6b1369f680868bca377aa [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)
45 OutStreamer.AddComment(Desc);
Chris Lattner9efd1182010-04-04 19:09:29 +000046
Benjamin Kramerc74798d2011-11-05 11:52:44 +000047 OutStreamer.EmitSLEB128IntValue(Value);
Chris Lattner9efd1182010-04-04 19:09:29 +000048}
49
50/// EmitULEB128 - emit the specified signed 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)
54 OutStreamer.AddComment(Desc);
Rafael Espindola38d07562010-11-04 18:17:08 +000055
Eric Christopherbf7bc492013-01-09 03:52:05 +000056 OutStreamer.EmitULEB128IntValue(Value, PadTo);
Chris Lattner9efd1182010-04-04 19:09:29 +000057}
58
Chris Lattnerbaf2be02010-04-04 20:01:25 +000059/// EmitCFAByte - Emit a .byte 42 directive for a DW_CFA_xxx value.
60void AsmPrinter::EmitCFAByte(unsigned Val) const {
61 if (isVerbose()) {
Eric Christopher596077b2013-12-04 22:26:43 +000062 if (Val >= dwarf::DW_CFA_offset && Val < dwarf::DW_CFA_offset + 64)
Eric Christopher1d6bd412012-11-20 20:34:47 +000063 OutStreamer.AddComment("DW_CFA_offset + Reg (" +
Eric Christopher596077b2013-12-04 22:26:43 +000064 Twine(Val - dwarf::DW_CFA_offset) + ")");
Chris Lattnerbaf2be02010-04-04 20:01:25 +000065 else
66 OutStreamer.AddComment(dwarf::CallFrameString(Val));
67 }
Eric Christopherce0cfce2013-01-09 01:35:34 +000068 OutStreamer.EmitIntValue(Val, 1);
Chris Lattnerbaf2be02010-04-04 20:01:25 +000069}
70
Chris Lattnerb75af3c2010-04-04 20:04:21 +000071static const char *DecodeDWARFEncoding(unsigned Encoding) {
72 switch (Encoding) {
Eric Christopher596077b2013-12-04 22:26:43 +000073 case dwarf::DW_EH_PE_absptr:
74 return "absptr";
75 case dwarf::DW_EH_PE_omit:
76 return "omit";
77 case dwarf::DW_EH_PE_pcrel:
78 return "pcrel";
79 case dwarf::DW_EH_PE_udata4:
80 return "udata4";
81 case dwarf::DW_EH_PE_udata8:
82 return "udata8";
83 case dwarf::DW_EH_PE_sdata4:
84 return "sdata4";
85 case dwarf::DW_EH_PE_sdata8:
86 return "sdata8";
87 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
88 return "pcrel udata4";
89 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
90 return "pcrel sdata4";
91 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
92 return "pcrel udata8";
93 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
94 return "pcrel sdata8";
95 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4
96 :
Chris Lattnerb75af3c2010-04-04 20:04:21 +000097 return "indirect pcrel udata4";
Eric Christopher596077b2013-12-04 22:26:43 +000098 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4
99 :
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000100 return "indirect pcrel sdata4";
Eric Christopher596077b2013-12-04 22:26:43 +0000101 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8
102 :
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000103 return "indirect pcrel udata8";
Eric Christopher596077b2013-12-04 22:26:43 +0000104 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8
105 :
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000106 return "indirect pcrel sdata8";
107 }
Eric Christopher1d6bd412012-11-20 20:34:47 +0000108
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000109 return "<unknown encoding>";
110}
111
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000112/// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
113/// encoding. If verbose assembly output is enabled, we output comments
114/// describing the encoding. Desc is an optional string saying what the
115/// encoding is specifying (e.g. "LSDA").
Chris Lattneraabc6042010-04-04 23:41:46 +0000116void AsmPrinter::EmitEncodingByte(unsigned Val, const char *Desc) const {
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000117 if (isVerbose()) {
Eric Christophercb7119e2013-12-04 22:29:02 +0000118 if (Desc)
Eric Christopher596077b2013-12-04 22:26:43 +0000119 OutStreamer.AddComment(Twine(Desc) + " Encoding = " +
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000120 Twine(DecodeDWARFEncoding(Val)));
121 else
Eric Christopher596077b2013-12-04 22:26:43 +0000122 OutStreamer.AddComment(Twine("Encoding = ") + DecodeDWARFEncoding(Val));
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000123 }
Eric Christopher1d6bd412012-11-20 20:34:47 +0000124
Eric Christopherce0cfce2013-01-09 01:35:34 +0000125 OutStreamer.EmitIntValue(Val, 1);
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000126}
127
Chris Lattnere619c0d2010-04-04 20:20:50 +0000128/// GetSizeOfEncodedValue - Return the size of the encoding in bytes.
129unsigned AsmPrinter::GetSizeOfEncodedValue(unsigned Encoding) const {
130 if (Encoding == dwarf::DW_EH_PE_omit)
131 return 0;
Eric Christopher1d6bd412012-11-20 20:34:47 +0000132
Chris Lattnere619c0d2010-04-04 20:20:50 +0000133 switch (Encoding & 0x07) {
Eric Christopher596077b2013-12-04 22:26:43 +0000134 default:
135 llvm_unreachable("Invalid encoded value.");
136 case dwarf::DW_EH_PE_absptr:
Eric Christopher8b770652015-01-26 19:03:15 +0000137 return TM.getDataLayout()->getPointerSize();
Eric Christopher596077b2013-12-04 22:26:43 +0000138 case dwarf::DW_EH_PE_udata2:
139 return 2;
140 case dwarf::DW_EH_PE_udata4:
141 return 4;
142 case dwarf::DW_EH_PE_udata8:
143 return 8;
Chris Lattnere619c0d2010-04-04 20:20:50 +0000144 }
145}
146
Eric Christopher1d6bd412012-11-20 20:34:47 +0000147void AsmPrinter::EmitTTypeReference(const GlobalValue *GV,
148 unsigned Encoding) const {
Anton Korobeynikov097b0e92012-11-19 21:17:20 +0000149 if (GV) {
150 const TargetLoweringObjectFile &TLOF = getObjFileLowering();
Eric Christopher1d6bd412012-11-20 20:34:47 +0000151
Anton Korobeynikov097b0e92012-11-19 21:17:20 +0000152 const MCExpr *Exp =
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000153 TLOF.getTTypeGlobalReference(GV, Encoding, *Mang, TM, MMI, OutStreamer);
Eric Christopherce0cfce2013-01-09 01:35:34 +0000154 OutStreamer.EmitValue(Exp, GetSizeOfEncodedValue(Encoding));
Anton Korobeynikov097b0e92012-11-19 21:17:20 +0000155 } else
Eric Christopherce0cfce2013-01-09 01:35:34 +0000156 OutStreamer.EmitIntValue(0, GetSizeOfEncodedValue(Encoding));
Chris Lattnere619c0d2010-04-04 20:20:50 +0000157}
Chris Lattner70a4fce2010-04-04 23:25:33 +0000158
159/// EmitSectionOffset - Emit the 4-byte offset of Label from the start of its
160/// section. This can be done with a special directive if the target supports
161/// it (e.g. cygwin) or by emitting it as an offset from a label at the start
162/// of the section.
163///
164/// SectionLabel is a temporary label emitted at the start of the section that
165/// Label lives in.
Rafael Espindola063d7252015-03-10 16:58:10 +0000166void AsmPrinter::emitSectionOffset(const MCSymbol *Label) const {
Chris Lattner70a4fce2010-04-04 23:25:33 +0000167 // On COFF targets, we have to emit the special .secrel32 directive.
Matt Arsenault034ca0f2013-04-22 22:49:11 +0000168 if (MAI->needsDwarfSectionOffsetDirective()) {
Rafael Espindolad3df3d32011-12-17 01:14:52 +0000169 OutStreamer.EmitCOFFSecRel32(Label);
Chris Lattner70a4fce2010-04-04 23:25:33 +0000170 return;
171 }
Eric Christopher1d6bd412012-11-20 20:34:47 +0000172
Rafael Espindola760bf952015-03-10 04:11:52 +0000173 // If the format uses relocations with dwarf, refer to the symbol directly.
174 if (MAI->doesDwarfUseRelocationsAcrossSections()) {
Eric Christopherce0cfce2013-01-09 01:35:34 +0000175 OutStreamer.EmitSymbolValue(Label, 4);
Duncan Sandsb847bf52011-03-12 13:07:37 +0000176 return;
177 }
Eric Christopher1d6bd412012-11-20 20:34:47 +0000178
Chris Lattner70a4fce2010-04-04 23:25:33 +0000179 // Otherwise, emit it as a label difference from the start of the section.
Rafael Espindola063d7252015-03-10 16:58:10 +0000180 EmitLabelDifference(Label, Label->getSection().getBeginSymbol(), 4);
Chris Lattner70a4fce2010-04-04 23:25:33 +0000181}
182
Eric Christopher698a8ab2014-03-07 01:44:14 +0000183/// EmitDwarfRegOp - Emit dwarf register operation.
Eric Christopher29e874d2014-03-07 22:40:37 +0000184void AsmPrinter::EmitDwarfRegOp(ByteStreamer &Streamer,
Adrian Prantl5883af32015-01-19 17:57:29 +0000185 const MachineLocation &MLoc) const {
Adrian Prantlb846acc2015-03-02 22:02:36 +0000186 DebugLocDwarfExpression Expr(*MF->getSubtarget().getRegisterInfo(),
Adrian Prantl92da14b2015-03-02 22:02:33 +0000187 getDwarfDebug()->getDwarfVersion(), Streamer);
Eric Christopher73302642015-02-19 23:29:42 +0000188 const MCRegisterInfo *MRI = MMI->getContext().getRegisterInfo();
189 int Reg = MRI->getDwarfRegNum(MLoc.getReg(), false);
Eric Christopher698a8ab2014-03-07 01:44:14 +0000190 if (Reg < 0) {
191 // We assume that pointers are always in an addressable register.
Adrian Prantl5883af32015-01-19 17:57:29 +0000192 if (MLoc.isIndirect())
Eric Christopher698a8ab2014-03-07 01:44:14 +0000193 // FIXME: We have no reasonable way of handling errors in here. The
194 // caller might be in the middle of a dwarf expression. We should
195 // probably assert that Reg >= 0 once debug info generation is more
196 // mature.
Adrian Prantl337e3602015-01-12 23:03:23 +0000197 return Expr.EmitOp(dwarf::DW_OP_nop,
198 "nop (could not find a dwarf register number)");
Eric Christopher698a8ab2014-03-07 01:44:14 +0000199
200 // Attempt to find a valid super- or sub-register.
Adrian Prantlad768c32015-01-14 01:01:28 +0000201 if (!Expr.AddMachineRegPiece(MLoc.getReg()))
202 Expr.EmitOp(dwarf::DW_OP_nop,
203 "nop (could not find a dwarf register number)");
204 return;
Eric Christopher698a8ab2014-03-07 01:44:14 +0000205 }
206
207 if (MLoc.isIndirect())
Adrian Prantl5883af32015-01-19 17:57:29 +0000208 Expr.AddRegIndirect(Reg, MLoc.getOffset());
Eric Christopher698a8ab2014-03-07 01:44:14 +0000209 else
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000210 Expr.AddReg(Reg);
Eric Christopher698a8ab2014-03-07 01:44:14 +0000211}
212
Chris Lattneraabc6042010-04-04 23:41:46 +0000213//===----------------------------------------------------------------------===//
214// Dwarf Lowering Routines
215//===----------------------------------------------------------------------===//
Chris Lattner70a4fce2010-04-04 23:25:33 +0000216
Rafael Espindola227144c2013-05-13 01:16:13 +0000217void AsmPrinter::emitCFIInstruction(const MCCFIInstruction &Inst) const {
218 switch (Inst.getOperation()) {
219 default:
220 llvm_unreachable("Unexpected instruction");
221 case MCCFIInstruction::OpDefCfaOffset:
222 OutStreamer.EmitCFIDefCfaOffset(Inst.getOffset());
223 break;
224 case MCCFIInstruction::OpDefCfa:
225 OutStreamer.EmitCFIDefCfa(Inst.getRegister(), Inst.getOffset());
226 break;
227 case MCCFIInstruction::OpDefCfaRegister:
228 OutStreamer.EmitCFIDefCfaRegister(Inst.getRegister());
229 break;
230 case MCCFIInstruction::OpOffset:
231 OutStreamer.EmitCFIOffset(Inst.getRegister(), Inst.getOffset());
232 break;
Venkatraman Govindaraju4c0cdd72013-09-26 15:11:00 +0000233 case MCCFIInstruction::OpRegister:
234 OutStreamer.EmitCFIRegister(Inst.getRegister(), Inst.getRegister2());
235 break;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000236 case MCCFIInstruction::OpWindowSave:
237 OutStreamer.EmitCFIWindowSave();
238 break;
Oliver Stannardb14c6252014-04-02 16:10:33 +0000239 case MCCFIInstruction::OpSameValue:
240 OutStreamer.EmitCFISameValue(Inst.getRegister());
241 break;
Rafael Espindolabeb74c32011-04-15 20:32:03 +0000242 }
243}
Frederic Riss9412d632015-03-04 02:30:17 +0000244
245void AsmPrinter::emitDwarfDIE(const DIE &Die) const {
246 // Get the abbreviation for this DIE.
247 const DIEAbbrev &Abbrev = Die.getAbbrev();
248
249 // Emit the code (index) for the abbreviation.
250 if (isVerbose())
251 OutStreamer.AddComment("Abbrev [" + Twine(Abbrev.getNumber()) +
252 "] 0x" + Twine::utohexstr(Die.getOffset()) +
253 ":0x" + Twine::utohexstr(Die.getSize()) + " " +
254 dwarf::TagString(Abbrev.getTag()));
255 EmitULEB128(Abbrev.getNumber());
256
257 const SmallVectorImpl<DIEValue *> &Values = Die.getValues();
258 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
259
260 // Emit the DIE attribute values.
261 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
262 dwarf::Attribute Attr = AbbrevData[i].getAttribute();
263 dwarf::Form Form = AbbrevData[i].getForm();
264 assert(Form && "Too many attributes for DIE (check abbreviation)");
265
266 if (isVerbose()) {
267 OutStreamer.AddComment(dwarf::AttributeString(Attr));
268 if (Attr == dwarf::DW_AT_accessibility)
269 OutStreamer.AddComment(dwarf::AccessibilityString(
270 cast<DIEInteger>(Values[i])->getValue()));
271 }
272
273 // Emit an attribute using the defined form.
274 Values[i]->EmitValue(this, Form);
275 }
276
277 // Emit the DIE children if any.
278 if (Abbrev.hasChildren()) {
279 for (auto &Child : Die.getChildren())
280 emitDwarfDIE(*Child);
281
282 OutStreamer.AddComment("End Of Children Mark");
283 EmitInt8(0);
284 }
285}
286
287void
288AsmPrinter::emitDwarfAbbrevs(const std::vector<DIEAbbrev *>& Abbrevs) const {
289 // For each abbrevation.
290 for (const DIEAbbrev *Abbrev : Abbrevs) {
291 // Emit the abbrevations code (base 1 index.)
292 EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
293
294 // Emit the abbreviations data.
295 Abbrev->Emit(this);
296 }
297
298 // Mark end of abbreviations.
299 EmitULEB128(0, "EOM(3)");
300}