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/BinaryFormat/Dwarf.h"
19 #include "llvm/CodeGen/AsmPrinter.h"
20 #include "llvm/CodeGen/DIE.h"
21 #include "llvm/CodeGen/MachineFunction.h"
22 #include "llvm/CodeGen/MachineModuleInfo.h"
23 #include "llvm/IR/DataLayout.h"
24 #include "llvm/MC/MCAsmInfo.h"
25 #include "llvm/MC/MCRegisterInfo.h"
26 #include "llvm/MC/MCSection.h"
27 #include "llvm/MC/MCStreamer.h"
28 #include "llvm/MC/MCSymbol.h"
29 #include "llvm/MC/MachineLocation.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 unsigned 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 static const char *DecodeDWARFEncoding(unsigned Encoding) {
61 case dwarf::DW_EH_PE_absptr:
63 case dwarf::DW_EH_PE_omit:
65 case dwarf::DW_EH_PE_pcrel:
67 case dwarf::DW_EH_PE_udata4:
69 case dwarf::DW_EH_PE_udata8:
71 case dwarf::DW_EH_PE_sdata4:
73 case dwarf::DW_EH_PE_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
85 return "indirect pcrel udata4";
86 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4
88 return "indirect pcrel sdata4";
89 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8
91 return "indirect pcrel udata8";
92 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8
94 return "indirect pcrel sdata8";
97 return "<unknown encoding>";
100 /// 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").
104 void AsmPrinter::EmitEncodingByte(unsigned Val, const char *Desc) const {
107 OutStreamer->AddComment(Twine(Desc) + " Encoding = " +
108 Twine(DecodeDWARFEncoding(Val)));
110 OutStreamer->AddComment(Twine("Encoding = ") + DecodeDWARFEncoding(Val));
113 OutStreamer->EmitIntValue(Val, 1);
116 /// GetSizeOfEncodedValue - Return the size of the encoding in bytes.
117 unsigned AsmPrinter::GetSizeOfEncodedValue(unsigned Encoding) const {
118 if (Encoding == dwarf::DW_EH_PE_omit)
121 switch (Encoding & 0x07) {
123 llvm_unreachable("Invalid encoded value.");
124 case dwarf::DW_EH_PE_absptr:
125 return MF->getDataLayout().getPointerSize();
126 case dwarf::DW_EH_PE_udata2:
128 case dwarf::DW_EH_PE_udata4:
130 case dwarf::DW_EH_PE_udata8:
135 void AsmPrinter::EmitTTypeReference(const GlobalValue *GV,
136 unsigned Encoding) const {
138 const TargetLoweringObjectFile &TLOF = getObjFileLowering();
141 TLOF.getTTypeGlobalReference(GV, Encoding, TM, MMI, *OutStreamer);
142 OutStreamer->EmitValue(Exp, GetSizeOfEncodedValue(Encoding));
144 OutStreamer->EmitIntValue(0, GetSizeOfEncodedValue(Encoding));
147 void AsmPrinter::emitDwarfSymbolReference(const MCSymbol *Label,
148 bool ForceOffset) const {
150 // On COFF targets, we have to emit the special .secrel32 directive.
151 if (MAI->needsDwarfSectionOffsetDirective()) {
152 OutStreamer->EmitCOFFSecRel32(Label, /*Offset=*/0);
156 // If the format uses relocations with dwarf, refer to the symbol directly.
157 if (MAI->doesDwarfUseRelocationsAcrossSections()) {
158 OutStreamer->EmitSymbolValue(Label, 4);
163 // Otherwise, emit it as a label difference from the start of the section.
164 EmitLabelDifference(Label, Label->getSection().getBeginSymbol(), 4);
167 void AsmPrinter::emitDwarfStringOffset(DwarfStringPoolEntryRef S) const {
168 if (MAI->doesDwarfUseRelocationsAcrossSections()) {
169 emitDwarfSymbolReference(S.getSymbol());
173 // Just emit the offset directly; no need for symbol math.
174 EmitInt32(S.getOffset());
177 //===----------------------------------------------------------------------===//
178 // Dwarf Lowering Routines
179 //===----------------------------------------------------------------------===//
181 void AsmPrinter::emitCFIInstruction(const MCCFIInstruction &Inst) const {
182 switch (Inst.getOperation()) {
184 llvm_unreachable("Unexpected instruction");
185 case MCCFIInstruction::OpDefCfaOffset:
186 OutStreamer->EmitCFIDefCfaOffset(Inst.getOffset());
188 case MCCFIInstruction::OpAdjustCfaOffset:
189 OutStreamer->EmitCFIAdjustCfaOffset(Inst.getOffset());
191 case MCCFIInstruction::OpDefCfa:
192 OutStreamer->EmitCFIDefCfa(Inst.getRegister(), Inst.getOffset());
194 case MCCFIInstruction::OpDefCfaRegister:
195 OutStreamer->EmitCFIDefCfaRegister(Inst.getRegister());
197 case MCCFIInstruction::OpOffset:
198 OutStreamer->EmitCFIOffset(Inst.getRegister(), Inst.getOffset());
200 case MCCFIInstruction::OpRegister:
201 OutStreamer->EmitCFIRegister(Inst.getRegister(), Inst.getRegister2());
203 case MCCFIInstruction::OpWindowSave:
204 OutStreamer->EmitCFIWindowSave();
206 case MCCFIInstruction::OpSameValue:
207 OutStreamer->EmitCFISameValue(Inst.getRegister());
209 case MCCFIInstruction::OpGnuArgsSize:
210 OutStreamer->EmitCFIGnuArgsSize(Inst.getOffset());
212 case MCCFIInstruction::OpEscape:
213 OutStreamer->EmitCFIEscape(Inst.getValues());
218 void AsmPrinter::emitDwarfDIE(const DIE &Die) const {
219 // Emit the code (index) for the abbreviation.
221 OutStreamer->AddComment("Abbrev [" + Twine(Die.getAbbrevNumber()) + "] 0x" +
222 Twine::utohexstr(Die.getOffset()) + ":0x" +
223 Twine::utohexstr(Die.getSize()) + " " +
224 dwarf::TagString(Die.getTag()));
225 EmitULEB128(Die.getAbbrevNumber());
227 // Emit the DIE attribute values.
228 for (const auto &V : Die.values()) {
229 dwarf::Attribute Attr = V.getAttribute();
230 assert(V.getForm() && "Too many attributes for DIE (check abbreviation)");
233 OutStreamer->AddComment(dwarf::AttributeString(Attr));
234 if (Attr == dwarf::DW_AT_accessibility)
235 OutStreamer->AddComment(
236 dwarf::AccessibilityString(V.getDIEInteger().getValue()));
239 // Emit an attribute using the defined form.
243 // Emit the DIE children if any.
244 if (Die.hasChildren()) {
245 for (auto &Child : Die.children())
248 OutStreamer->AddComment("End Of Children Mark");
253 void AsmPrinter::emitDwarfAbbrev(const DIEAbbrev &Abbrev) const {
254 // Emit the abbreviations code (base 1 index.)
255 EmitULEB128(Abbrev.getNumber(), "Abbreviation Code");
257 // Emit the abbreviations data.