1 //===-- AsmPrinterDwarf.cpp - AsmPrinter Dwarf Support --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the Dwarf emissions parts of AsmPrinter.
12 //===----------------------------------------------------------------------===//
14 #include "ByteStreamer.h"
15 #include "DwarfDebug.h"
16 #include "DwarfExpression.h"
17 #include "llvm/ADT/Twine.h"
18 #include "llvm/CodeGen/AsmPrinter.h"
19 #include "llvm/CodeGen/DIE.h"
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/CodeGen/MachineModuleInfo.h"
22 #include "llvm/IR/DataLayout.h"
23 #include "llvm/MC/MCAsmInfo.h"
24 #include "llvm/MC/MCRegisterInfo.h"
25 #include "llvm/MC/MCSection.h"
26 #include "llvm/MC/MCStreamer.h"
27 #include "llvm/MC/MCSymbol.h"
28 #include "llvm/MC/MachineLocation.h"
29 #include "llvm/Support/Dwarf.h"
30 #include "llvm/Support/ErrorHandling.h"
31 #include "llvm/Target/TargetLoweringObjectFile.h"
32 #include "llvm/Target/TargetMachine.h"
33 #include "llvm/Target/TargetSubtargetInfo.h"
36 #define DEBUG_TYPE "asm-printer"
38 //===----------------------------------------------------------------------===//
39 // Dwarf Emission Helper Routines
40 //===----------------------------------------------------------------------===//
42 /// EmitSLEB128 - emit the specified signed leb128 value.
43 void AsmPrinter::EmitSLEB128(int64_t Value, const char *Desc) const {
44 if (isVerbose() && Desc)
45 OutStreamer.AddComment(Desc);
47 OutStreamer.EmitSLEB128IntValue(Value);
50 /// EmitULEB128 - emit the specified signed leb128 value.
51 void AsmPrinter::EmitULEB128(uint64_t Value, const char *Desc,
52 unsigned PadTo) const {
53 if (isVerbose() && Desc)
54 OutStreamer.AddComment(Desc);
56 OutStreamer.EmitULEB128IntValue(Value, PadTo);
59 /// EmitCFAByte - Emit a .byte 42 directive for a DW_CFA_xxx value.
60 void AsmPrinter::EmitCFAByte(unsigned Val) const {
62 if (Val >= dwarf::DW_CFA_offset && Val < dwarf::DW_CFA_offset + 64)
63 OutStreamer.AddComment("DW_CFA_offset + Reg (" +
64 Twine(Val - dwarf::DW_CFA_offset) + ")");
66 OutStreamer.AddComment(dwarf::CallFrameString(Val));
68 OutStreamer.EmitIntValue(Val, 1);
71 static const char *DecodeDWARFEncoding(unsigned Encoding) {
73 case dwarf::DW_EH_PE_absptr:
75 case dwarf::DW_EH_PE_omit:
77 case dwarf::DW_EH_PE_pcrel:
79 case dwarf::DW_EH_PE_udata4:
81 case dwarf::DW_EH_PE_udata8:
83 case dwarf::DW_EH_PE_sdata4:
85 case dwarf::DW_EH_PE_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
97 return "indirect pcrel udata4";
98 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4
100 return "indirect pcrel sdata4";
101 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8
103 return "indirect pcrel udata8";
104 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8
106 return "indirect pcrel sdata8";
109 return "<unknown encoding>";
112 /// 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").
116 void AsmPrinter::EmitEncodingByte(unsigned Val, const char *Desc) const {
119 OutStreamer.AddComment(Twine(Desc) + " Encoding = " +
120 Twine(DecodeDWARFEncoding(Val)));
122 OutStreamer.AddComment(Twine("Encoding = ") + DecodeDWARFEncoding(Val));
125 OutStreamer.EmitIntValue(Val, 1);
128 /// GetSizeOfEncodedValue - Return the size of the encoding in bytes.
129 unsigned AsmPrinter::GetSizeOfEncodedValue(unsigned Encoding) const {
130 if (Encoding == dwarf::DW_EH_PE_omit)
133 switch (Encoding & 0x07) {
135 llvm_unreachable("Invalid encoded value.");
136 case dwarf::DW_EH_PE_absptr:
137 return TM.getDataLayout()->getPointerSize();
138 case dwarf::DW_EH_PE_udata2:
140 case dwarf::DW_EH_PE_udata4:
142 case dwarf::DW_EH_PE_udata8:
147 void AsmPrinter::EmitTTypeReference(const GlobalValue *GV,
148 unsigned Encoding) const {
150 const TargetLoweringObjectFile &TLOF = getObjFileLowering();
153 TLOF.getTTypeGlobalReference(GV, Encoding, *Mang, TM, MMI, OutStreamer);
154 OutStreamer.EmitValue(Exp, GetSizeOfEncodedValue(Encoding));
156 OutStreamer.EmitIntValue(0, GetSizeOfEncodedValue(Encoding));
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
164 /// SectionLabel is a temporary label emitted at the start of the section that
166 void AsmPrinter::EmitSectionOffset(const MCSymbol *Label,
167 const MCSymbol *SectionLabel) const {
168 // On COFF targets, we have to emit the special .secrel32 directive.
169 if (MAI->needsDwarfSectionOffsetDirective()) {
170 OutStreamer.EmitCOFFSecRel32(Label);
174 // Get the section that we're referring to, based on SectionLabel.
175 const MCSection &Section = SectionLabel->getSection();
177 // If Label has already been emitted, verify that it is in the same section as
178 // section label for sanity.
179 assert((!Label->isInSection() || &Label->getSection() == &Section) &&
180 "Section offset using wrong section base for label");
182 // If the section in question will end up with an address of 0 anyway, we can
183 // just emit an absolute reference to save a relocation.
184 if (Section.isBaseAddressKnownZero()) {
185 OutStreamer.EmitSymbolValue(Label, 4);
189 // Otherwise, emit it as a label difference from the start of the section.
190 EmitLabelDifference(Label, SectionLabel, 4);
193 /// EmitDwarfRegOp - Emit dwarf register operation.
194 void AsmPrinter::EmitDwarfRegOp(ByteStreamer &Streamer,
195 const MachineLocation &MLoc) const {
196 DebugLocDwarfExpression Expr(*MF->getSubtarget().getRegisterInfo(),
197 getDwarfDebug()->getDwarfVersion(), Streamer);
198 const MCRegisterInfo *MRI = MMI->getContext().getRegisterInfo();
199 int Reg = MRI->getDwarfRegNum(MLoc.getReg(), false);
201 // We assume that pointers are always in an addressable register.
202 if (MLoc.isIndirect())
203 // FIXME: We have no reasonable way of handling errors in here. The
204 // caller might be in the middle of a dwarf expression. We should
205 // probably assert that Reg >= 0 once debug info generation is more
207 return Expr.EmitOp(dwarf::DW_OP_nop,
208 "nop (could not find a dwarf register number)");
210 // Attempt to find a valid super- or sub-register.
211 if (!Expr.AddMachineRegPiece(MLoc.getReg()))
212 Expr.EmitOp(dwarf::DW_OP_nop,
213 "nop (could not find a dwarf register number)");
217 if (MLoc.isIndirect())
218 Expr.AddRegIndirect(Reg, MLoc.getOffset());
223 //===----------------------------------------------------------------------===//
224 // Dwarf Lowering Routines
225 //===----------------------------------------------------------------------===//
227 void AsmPrinter::emitCFIInstruction(const MCCFIInstruction &Inst) const {
228 switch (Inst.getOperation()) {
230 llvm_unreachable("Unexpected instruction");
231 case MCCFIInstruction::OpDefCfaOffset:
232 OutStreamer.EmitCFIDefCfaOffset(Inst.getOffset());
234 case MCCFIInstruction::OpDefCfa:
235 OutStreamer.EmitCFIDefCfa(Inst.getRegister(), Inst.getOffset());
237 case MCCFIInstruction::OpDefCfaRegister:
238 OutStreamer.EmitCFIDefCfaRegister(Inst.getRegister());
240 case MCCFIInstruction::OpOffset:
241 OutStreamer.EmitCFIOffset(Inst.getRegister(), Inst.getOffset());
243 case MCCFIInstruction::OpRegister:
244 OutStreamer.EmitCFIRegister(Inst.getRegister(), Inst.getRegister2());
246 case MCCFIInstruction::OpWindowSave:
247 OutStreamer.EmitCFIWindowSave();
249 case MCCFIInstruction::OpSameValue:
250 OutStreamer.EmitCFISameValue(Inst.getRegister());
255 void AsmPrinter::emitDwarfDIE(const DIE &Die) const {
256 // Get the abbreviation for this DIE.
257 const DIEAbbrev &Abbrev = Die.getAbbrev();
259 // Emit the code (index) for the abbreviation.
261 OutStreamer.AddComment("Abbrev [" + Twine(Abbrev.getNumber()) +
262 "] 0x" + Twine::utohexstr(Die.getOffset()) +
263 ":0x" + Twine::utohexstr(Die.getSize()) + " " +
264 dwarf::TagString(Abbrev.getTag()));
265 EmitULEB128(Abbrev.getNumber());
267 const SmallVectorImpl<DIEValue *> &Values = Die.getValues();
268 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
270 // Emit the DIE attribute values.
271 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
272 dwarf::Attribute Attr = AbbrevData[i].getAttribute();
273 dwarf::Form Form = AbbrevData[i].getForm();
274 assert(Form && "Too many attributes for DIE (check abbreviation)");
277 OutStreamer.AddComment(dwarf::AttributeString(Attr));
278 if (Attr == dwarf::DW_AT_accessibility)
279 OutStreamer.AddComment(dwarf::AccessibilityString(
280 cast<DIEInteger>(Values[i])->getValue()));
283 // Emit an attribute using the defined form.
284 Values[i]->EmitValue(this, Form);
287 // Emit the DIE children if any.
288 if (Abbrev.hasChildren()) {
289 for (auto &Child : Die.getChildren())
290 emitDwarfDIE(*Child);
292 OutStreamer.AddComment("End Of Children Mark");
298 AsmPrinter::emitDwarfAbbrevs(const std::vector<DIEAbbrev *>& Abbrevs) const {
299 // For each abbrevation.
300 for (const DIEAbbrev *Abbrev : Abbrevs) {
301 // Emit the abbrevations code (base 1 index.)
302 EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
304 // Emit the abbreviations data.
308 // Mark end of abbreviations.
309 EmitULEB128(0, "EOM(3)");