blob: 1b4e4cfc4f8133e4a4929f7824094293e4fe6ad8 [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"
Adrian Prantlb846acc2015-03-02 22:02:36 +000019#include "llvm/CodeGen/MachineFunction.h"
Eric Christopher73302642015-02-19 23:29:42 +000020#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/DataLayout.h"
Chris Lattner9efd1182010-04-04 19:09:29 +000022#include "llvm/MC/MCAsmInfo.h"
Eric Christopher73302642015-02-19 23:29:42 +000023#include "llvm/MC/MCRegisterInfo.h"
Chris Lattner70a4fce2010-04-04 23:25:33 +000024#include "llvm/MC/MCSection.h"
Chris Lattner9efd1182010-04-04 19:09:29 +000025#include "llvm/MC/MCStreamer.h"
Chris Lattner70a4fce2010-04-04 23:25:33 +000026#include "llvm/MC/MCSymbol.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000027#include "llvm/MC/MachineLocation.h"
28#include "llvm/Support/Dwarf.h"
29#include "llvm/Support/ErrorHandling.h"
Chris Lattnere619c0d2010-04-04 20:20:50 +000030#include "llvm/Target/TargetLoweringObjectFile.h"
31#include "llvm/Target/TargetMachine.h"
Adrian Prantl92da14b2015-03-02 22:02:33 +000032#include "llvm/Target/TargetSubtargetInfo.h"
Chris Lattner9efd1182010-04-04 19:09:29 +000033using namespace llvm;
34
Chandler Carruth1b9dde02014-04-22 02:02:50 +000035#define DEBUG_TYPE "asm-printer"
36
Chris Lattneraabc6042010-04-04 23:41:46 +000037//===----------------------------------------------------------------------===//
38// Dwarf Emission Helper Routines
39//===----------------------------------------------------------------------===//
40
Chris Lattner9efd1182010-04-04 19:09:29 +000041/// EmitSLEB128 - emit the specified signed leb128 value.
David Blaikie5acff7e2013-06-23 18:31:11 +000042void AsmPrinter::EmitSLEB128(int64_t Value, const char *Desc) const {
Chris Lattner9efd1182010-04-04 19:09:29 +000043 if (isVerbose() && Desc)
44 OutStreamer.AddComment(Desc);
Chris Lattner9efd1182010-04-04 19:09:29 +000045
Benjamin Kramerc74798d2011-11-05 11:52:44 +000046 OutStreamer.EmitSLEB128IntValue(Value);
Chris Lattner9efd1182010-04-04 19:09:29 +000047}
48
49/// EmitULEB128 - emit the specified signed leb128 value.
David Blaikie5acff7e2013-06-23 18:31:11 +000050void AsmPrinter::EmitULEB128(uint64_t Value, const char *Desc,
Chris Lattner9efd1182010-04-04 19:09:29 +000051 unsigned PadTo) const {
52 if (isVerbose() && Desc)
53 OutStreamer.AddComment(Desc);
Rafael Espindola38d07562010-11-04 18:17:08 +000054
Eric Christopherbf7bc492013-01-09 03:52:05 +000055 OutStreamer.EmitULEB128IntValue(Value, PadTo);
Chris Lattner9efd1182010-04-04 19:09:29 +000056}
57
Chris Lattnerbaf2be02010-04-04 20:01:25 +000058/// EmitCFAByte - Emit a .byte 42 directive for a DW_CFA_xxx value.
59void AsmPrinter::EmitCFAByte(unsigned Val) const {
60 if (isVerbose()) {
Eric Christopher596077b2013-12-04 22:26:43 +000061 if (Val >= dwarf::DW_CFA_offset && Val < dwarf::DW_CFA_offset + 64)
Eric Christopher1d6bd412012-11-20 20:34:47 +000062 OutStreamer.AddComment("DW_CFA_offset + Reg (" +
Eric Christopher596077b2013-12-04 22:26:43 +000063 Twine(Val - dwarf::DW_CFA_offset) + ")");
Chris Lattnerbaf2be02010-04-04 20:01:25 +000064 else
65 OutStreamer.AddComment(dwarf::CallFrameString(Val));
66 }
Eric Christopherce0cfce2013-01-09 01:35:34 +000067 OutStreamer.EmitIntValue(Val, 1);
Chris Lattnerbaf2be02010-04-04 20:01:25 +000068}
69
Chris Lattnerb75af3c2010-04-04 20:04:21 +000070static const char *DecodeDWARFEncoding(unsigned Encoding) {
71 switch (Encoding) {
Eric Christopher596077b2013-12-04 22:26:43 +000072 case dwarf::DW_EH_PE_absptr:
73 return "absptr";
74 case dwarf::DW_EH_PE_omit:
75 return "omit";
76 case dwarf::DW_EH_PE_pcrel:
77 return "pcrel";
78 case dwarf::DW_EH_PE_udata4:
79 return "udata4";
80 case dwarf::DW_EH_PE_udata8:
81 return "udata8";
82 case dwarf::DW_EH_PE_sdata4:
83 return "sdata4";
84 case dwarf::DW_EH_PE_sdata8:
85 return "sdata8";
86 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
87 return "pcrel udata4";
88 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
89 return "pcrel sdata4";
90 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
91 return "pcrel udata8";
92 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
93 return "pcrel sdata8";
94 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4
95 :
Chris Lattnerb75af3c2010-04-04 20:04:21 +000096 return "indirect pcrel udata4";
Eric Christopher596077b2013-12-04 22:26:43 +000097 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4
98 :
Chris Lattnerb75af3c2010-04-04 20:04:21 +000099 return "indirect pcrel sdata4";
Eric Christopher596077b2013-12-04 22:26:43 +0000100 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8
101 :
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000102 return "indirect pcrel udata8";
Eric Christopher596077b2013-12-04 22:26:43 +0000103 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8
104 :
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000105 return "indirect pcrel sdata8";
106 }
Eric Christopher1d6bd412012-11-20 20:34:47 +0000107
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000108 return "<unknown encoding>";
109}
110
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000111/// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
112/// encoding. If verbose assembly output is enabled, we output comments
113/// describing the encoding. Desc is an optional string saying what the
114/// encoding is specifying (e.g. "LSDA").
Chris Lattneraabc6042010-04-04 23:41:46 +0000115void AsmPrinter::EmitEncodingByte(unsigned Val, const char *Desc) const {
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000116 if (isVerbose()) {
Eric Christophercb7119e2013-12-04 22:29:02 +0000117 if (Desc)
Eric Christopher596077b2013-12-04 22:26:43 +0000118 OutStreamer.AddComment(Twine(Desc) + " Encoding = " +
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000119 Twine(DecodeDWARFEncoding(Val)));
120 else
Eric Christopher596077b2013-12-04 22:26:43 +0000121 OutStreamer.AddComment(Twine("Encoding = ") + DecodeDWARFEncoding(Val));
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000122 }
Eric Christopher1d6bd412012-11-20 20:34:47 +0000123
Eric Christopherce0cfce2013-01-09 01:35:34 +0000124 OutStreamer.EmitIntValue(Val, 1);
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000125}
126
Chris Lattnere619c0d2010-04-04 20:20:50 +0000127/// GetSizeOfEncodedValue - Return the size of the encoding in bytes.
128unsigned AsmPrinter::GetSizeOfEncodedValue(unsigned Encoding) const {
129 if (Encoding == dwarf::DW_EH_PE_omit)
130 return 0;
Eric Christopher1d6bd412012-11-20 20:34:47 +0000131
Chris Lattnere619c0d2010-04-04 20:20:50 +0000132 switch (Encoding & 0x07) {
Eric Christopher596077b2013-12-04 22:26:43 +0000133 default:
134 llvm_unreachable("Invalid encoded value.");
135 case dwarf::DW_EH_PE_absptr:
Eric Christopher8b770652015-01-26 19:03:15 +0000136 return TM.getDataLayout()->getPointerSize();
Eric Christopher596077b2013-12-04 22:26:43 +0000137 case dwarf::DW_EH_PE_udata2:
138 return 2;
139 case dwarf::DW_EH_PE_udata4:
140 return 4;
141 case dwarf::DW_EH_PE_udata8:
142 return 8;
Chris Lattnere619c0d2010-04-04 20:20:50 +0000143 }
144}
145
Eric Christopher1d6bd412012-11-20 20:34:47 +0000146void AsmPrinter::EmitTTypeReference(const GlobalValue *GV,
147 unsigned Encoding) const {
Anton Korobeynikov097b0e92012-11-19 21:17:20 +0000148 if (GV) {
149 const TargetLoweringObjectFile &TLOF = getObjFileLowering();
Eric Christopher1d6bd412012-11-20 20:34:47 +0000150
Anton Korobeynikov097b0e92012-11-19 21:17:20 +0000151 const MCExpr *Exp =
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000152 TLOF.getTTypeGlobalReference(GV, Encoding, *Mang, TM, MMI, OutStreamer);
Eric Christopherce0cfce2013-01-09 01:35:34 +0000153 OutStreamer.EmitValue(Exp, GetSizeOfEncodedValue(Encoding));
Anton Korobeynikov097b0e92012-11-19 21:17:20 +0000154 } else
Eric Christopherce0cfce2013-01-09 01:35:34 +0000155 OutStreamer.EmitIntValue(0, GetSizeOfEncodedValue(Encoding));
Chris Lattnere619c0d2010-04-04 20:20:50 +0000156}
Chris Lattner70a4fce2010-04-04 23:25:33 +0000157
158/// EmitSectionOffset - Emit the 4-byte offset of Label from the start of its
159/// section. This can be done with a special directive if the target supports
160/// it (e.g. cygwin) or by emitting it as an offset from a label at the start
161/// of the section.
162///
163/// SectionLabel is a temporary label emitted at the start of the section that
164/// Label lives in.
165void AsmPrinter::EmitSectionOffset(const MCSymbol *Label,
166 const MCSymbol *SectionLabel) const {
167 // 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
Chris Lattner70a4fce2010-04-04 23:25:33 +0000173 // Get the section that we're referring to, based on SectionLabel.
174 const MCSection &Section = SectionLabel->getSection();
Eric Christopher1d6bd412012-11-20 20:34:47 +0000175
Chris Lattner70a4fce2010-04-04 23:25:33 +0000176 // If Label has already been emitted, verify that it is in the same section as
177 // section label for sanity.
178 assert((!Label->isInSection() || &Label->getSection() == &Section) &&
179 "Section offset using wrong section base for label");
Eric Christopher1d6bd412012-11-20 20:34:47 +0000180
Duncan Sandsb847bf52011-03-12 13:07:37 +0000181 // If the section in question will end up with an address of 0 anyway, we can
182 // just emit an absolute reference to save a relocation.
183 if (Section.isBaseAddressKnownZero()) {
Eric Christopherce0cfce2013-01-09 01:35:34 +0000184 OutStreamer.EmitSymbolValue(Label, 4);
Duncan Sandsb847bf52011-03-12 13:07:37 +0000185 return;
186 }
Eric Christopher1d6bd412012-11-20 20:34:47 +0000187
Chris Lattner70a4fce2010-04-04 23:25:33 +0000188 // Otherwise, emit it as a label difference from the start of the section.
189 EmitLabelDifference(Label, SectionLabel, 4);
190}
191
Eric Christopher698a8ab2014-03-07 01:44:14 +0000192/// EmitDwarfRegOp - Emit dwarf register operation.
Eric Christopher29e874d2014-03-07 22:40:37 +0000193void AsmPrinter::EmitDwarfRegOp(ByteStreamer &Streamer,
Adrian Prantl5883af32015-01-19 17:57:29 +0000194 const MachineLocation &MLoc) const {
Adrian Prantlb846acc2015-03-02 22:02:36 +0000195 DebugLocDwarfExpression Expr(*MF->getSubtarget().getRegisterInfo(),
Adrian Prantl92da14b2015-03-02 22:02:33 +0000196 getDwarfDebug()->getDwarfVersion(), Streamer);
Eric Christopher73302642015-02-19 23:29:42 +0000197 const MCRegisterInfo *MRI = MMI->getContext().getRegisterInfo();
198 int Reg = MRI->getDwarfRegNum(MLoc.getReg(), false);
Eric Christopher698a8ab2014-03-07 01:44:14 +0000199 if (Reg < 0) {
200 // We assume that pointers are always in an addressable register.
Adrian Prantl5883af32015-01-19 17:57:29 +0000201 if (MLoc.isIndirect())
Eric Christopher698a8ab2014-03-07 01:44:14 +0000202 // FIXME: We have no reasonable way of handling errors in here. The
203 // caller might be in the middle of a dwarf expression. We should
204 // probably assert that Reg >= 0 once debug info generation is more
205 // mature.
Adrian Prantl337e3602015-01-12 23:03:23 +0000206 return Expr.EmitOp(dwarf::DW_OP_nop,
207 "nop (could not find a dwarf register number)");
Eric Christopher698a8ab2014-03-07 01:44:14 +0000208
209 // Attempt to find a valid super- or sub-register.
Adrian Prantlad768c32015-01-14 01:01:28 +0000210 if (!Expr.AddMachineRegPiece(MLoc.getReg()))
211 Expr.EmitOp(dwarf::DW_OP_nop,
212 "nop (could not find a dwarf register number)");
213 return;
Eric Christopher698a8ab2014-03-07 01:44:14 +0000214 }
215
216 if (MLoc.isIndirect())
Adrian Prantl5883af32015-01-19 17:57:29 +0000217 Expr.AddRegIndirect(Reg, MLoc.getOffset());
Eric Christopher698a8ab2014-03-07 01:44:14 +0000218 else
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000219 Expr.AddReg(Reg);
Eric Christopher698a8ab2014-03-07 01:44:14 +0000220}
221
Chris Lattneraabc6042010-04-04 23:41:46 +0000222//===----------------------------------------------------------------------===//
223// Dwarf Lowering Routines
224//===----------------------------------------------------------------------===//
Chris Lattner70a4fce2010-04-04 23:25:33 +0000225
Rafael Espindola227144c2013-05-13 01:16:13 +0000226void AsmPrinter::emitCFIInstruction(const MCCFIInstruction &Inst) const {
227 switch (Inst.getOperation()) {
228 default:
229 llvm_unreachable("Unexpected instruction");
230 case MCCFIInstruction::OpDefCfaOffset:
231 OutStreamer.EmitCFIDefCfaOffset(Inst.getOffset());
232 break;
233 case MCCFIInstruction::OpDefCfa:
234 OutStreamer.EmitCFIDefCfa(Inst.getRegister(), Inst.getOffset());
235 break;
236 case MCCFIInstruction::OpDefCfaRegister:
237 OutStreamer.EmitCFIDefCfaRegister(Inst.getRegister());
238 break;
239 case MCCFIInstruction::OpOffset:
240 OutStreamer.EmitCFIOffset(Inst.getRegister(), Inst.getOffset());
241 break;
Venkatraman Govindaraju4c0cdd72013-09-26 15:11:00 +0000242 case MCCFIInstruction::OpRegister:
243 OutStreamer.EmitCFIRegister(Inst.getRegister(), Inst.getRegister2());
244 break;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000245 case MCCFIInstruction::OpWindowSave:
246 OutStreamer.EmitCFIWindowSave();
247 break;
Oliver Stannardb14c6252014-04-02 16:10:33 +0000248 case MCCFIInstruction::OpSameValue:
249 OutStreamer.EmitCFISameValue(Inst.getRegister());
250 break;
Rafael Espindolabeb74c32011-04-15 20:32:03 +0000251 }
252}