LLVM 24.0.0git
AsmPrinterDwarf.cpp
Go to the documentation of this file.
1//===-- AsmPrinterDwarf.cpp - AsmPrinter Dwarf Support --------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the Dwarf emissions parts of AsmPrinter.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/ADT/Twine.h"
16#include "llvm/CodeGen/DIE.h"
18#include "llvm/MC/MCAsmInfo.h"
19#include "llvm/MC/MCDwarf.h"
20#include "llvm/MC/MCSection.h"
21#include "llvm/MC/MCStreamer.h"
22#include "llvm/MC/MCSymbol.h"
25#include <cstdint>
26using namespace llvm;
27
28#define DEBUG_TYPE "asm-printer"
29
30//===----------------------------------------------------------------------===//
31// Dwarf Emission Helper Routines
32//===----------------------------------------------------------------------===//
33
34static const char *DecodeDWARFEncoding(unsigned Encoding) {
35 switch (Encoding) {
37 return "absptr";
39 return "omit";
41 return "pcrel";
43 return "uleb128";
45 return "sleb128";
47 return "udata4";
49 return "udata8";
51 return "sdata4";
53 return "sdata8";
55 return "pcrel udata4";
57 return "pcrel sdata4";
59 return "pcrel udata8";
61 return "pcrel sdata8";
63 :
64 return "indirect pcrel udata4";
66 :
67 return "indirect pcrel sdata4";
69 :
70 return "indirect pcrel udata8";
72 :
73 return "indirect pcrel sdata8";
76 return "indirect datarel sdata4";
79 return "indirect datarel sdata8";
80 }
81
82 return "<unknown encoding>";
83}
84
85/// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
86/// encoding. If verbose assembly output is enabled, we output comments
87/// describing the encoding. Desc is an optional string saying what the
88/// encoding is specifying (e.g. "LSDA").
89void AsmPrinter::emitEncodingByte(unsigned Val, const char *Desc) const {
90 if (isVerbose()) {
91 if (Desc)
92 OutStreamer->AddComment(Twine(Desc) + " Encoding = " +
94 else
95 OutStreamer->AddComment(Twine("Encoding = ") + DecodeDWARFEncoding(Val));
96 }
97
98 OutStreamer->emitIntValue(Val, 1);
99}
100
101/// GetSizeOfEncodedValue - Return the size of the encoding in bytes.
102unsigned AsmPrinter::GetSizeOfEncodedValue(unsigned Encoding) const {
103 if (Encoding == dwarf::DW_EH_PE_omit)
104 return 0;
105
106 switch (Encoding & 0x07) {
107 default:
108 llvm_unreachable("Invalid encoded value.");
110 return MAI.getCodePointerSize();
112 return 2;
114 return 4;
116 return 8;
117 }
118}
119
120void AsmPrinter::emitTTypeReference(const GlobalValue *GV, unsigned Encoding) {
121 if (GV) {
123
124 const MCExpr *Exp =
125 TLOF.getTTypeGlobalReference(GV, Encoding, TM, MMI, *OutStreamer);
126 OutStreamer->emitValue(Exp, GetSizeOfEncodedValue(Encoding));
127 } else
128 OutStreamer->emitIntValue(0, GetSizeOfEncodedValue(Encoding));
129}
130
132 bool ForceOffset) const {
133 if (!ForceOffset) {
134 // On COFF targets, we have to emit the special .secrel32 directive.
135 if (MAI.needsDwarfSectionOffsetDirective()) {
136 assert(!isDwarf64() &&
137 "emitting DWARF64 is not implemented for COFF targets");
138 OutStreamer->emitDwarfSectionOffset(Label, /*Offset=*/0,
140 return;
141 }
142
143 // If the format uses relocations with dwarf, refer to the symbol directly.
145 OutStreamer->emitDwarfSectionOffset(Label, /*Offset=*/0,
147 return;
148 }
149 }
150
151 // Otherwise, emit it as a label difference from the start of the section.
152 emitLabelDifference(Label, Label->getSection().getBeginSymbol(),
154}
155
158 assert(S.Symbol && "No symbol available");
160 return;
161 }
162
163 // Just emit the offset directly; no need for symbol math.
164 OutStreamer->emitIntValue(S.Offset, getDwarfOffsetByteSize());
165}
166
167void AsmPrinter::emitDwarfOffset(const MCSymbol *Label, uint64_t Offset) const {
169}
170
172 assert(isDwarf64() || Value <= UINT32_MAX);
173 OutStreamer->emitIntValue(Value, getDwarfOffsetByteSize());
174}
175
177 const Twine &Comment) const {
178 OutStreamer->emitDwarfUnitLength(Length, Comment);
179}
180
182 const Twine &Comment) const {
183 return OutStreamer->emitDwarfUnitLength(Prefix, Comment);
184}
185
187 unsigned Encoding) const {
188 // The least significant 3 bits specify the width of the encoding
189 if ((Encoding & 0x7) == dwarf::DW_EH_PE_uleb128)
191 else
193}
194
195void AsmPrinter::emitCallSiteValue(uint64_t Value, unsigned Encoding) const {
196 // The least significant 3 bits specify the width of the encoding
197 if ((Encoding & 0x7) == dwarf::DW_EH_PE_uleb128)
199 else
200 OutStreamer->emitIntValue(Value, GetSizeOfEncodedValue(Encoding));
201}
202
203//===----------------------------------------------------------------------===//
204// Dwarf Lowering Routines
205//===----------------------------------------------------------------------===//
206
208 SMLoc Loc = Inst.getLoc();
209 switch (Inst.getOperation()) {
210 default:
211 llvm_unreachable("Unexpected instruction");
213 OutStreamer->emitCFIDefCfaOffset(Inst.getOffset(), Loc);
214 break;
216 OutStreamer->emitCFIAdjustCfaOffset(Inst.getOffset(), Loc);
217 break;
219 OutStreamer->emitCFIDefCfa(Inst.getRegister(), Inst.getOffset(), Loc);
220 break;
222 OutStreamer->emitCFIDefCfaRegister(Inst.getRegister(), Loc);
223 break;
225 OutStreamer->emitCFILLVMDefAspaceCfa(Inst.getRegister(), Inst.getOffset(),
226 Inst.getAddressSpace(), Loc);
227 break;
229 OutStreamer->emitCFIOffset(Inst.getRegister(), Inst.getOffset(), Loc);
230 break;
232 OutStreamer->emitCFIRegister(Inst.getRegister(), Inst.getRegister2(), Loc);
233 break;
235 OutStreamer->emitCFIWindowSave(Loc);
236 break;
238 OutStreamer->emitCFINegateRAState(Loc);
239 break;
241 OutStreamer->emitCFINegateRAStateWithPC(Loc);
242 break;
244 if (MCSymbol *Symbol = Inst.getRASignSymbol())
245 OutStreamer->emitCFILLVMSetRAState(Inst.getRASignState(), Symbol, Loc);
246 else
247 OutStreamer->emitCFILLVMSetRAState(Inst.getRASignState(),
248 Inst.getRASignOffset(), Loc);
249 break;
250 }
252 OutStreamer->emitCFISameValue(Inst.getRegister(), Loc);
253 break;
255 OutStreamer->emitCFIGnuArgsSize(Inst.getOffset(), Loc);
256 break;
258 OutStreamer->AddComment(Inst.getComment());
259 OutStreamer->emitCFIEscape(Inst.getValues(), Loc);
260 break;
262 OutStreamer->emitCFIRestore(Inst.getRegister(), Loc);
263 break;
265 OutStreamer->emitCFIUndefined(Inst.getRegister(), Loc);
266 break;
268 OutStreamer->emitCFIRememberState(Loc);
269 break;
271 OutStreamer->emitCFIRestoreState(Loc);
272 break;
274 const auto &Fields =
276 OutStreamer->emitCFILLVMRegisterPair(Fields.Register, Fields.Reg1,
277 Fields.Reg1SizeInBits, Fields.Reg2,
278 Fields.Reg2SizeInBits, Loc);
279 break;
280 }
282 const auto &Fields =
284 OutStreamer->emitCFILLVMVectorRegisters(Fields.Register,
285 Fields.VectorRegisters, Loc);
286 break;
287 }
289 const auto &Fields =
291 OutStreamer->emitCFILLVMVectorOffset(
292 Fields.Register, Fields.RegisterSizeInBits, Fields.MaskRegister,
293 Fields.MaskRegisterSizeInBits, Fields.Offset, Loc);
294 break;
295 }
297 const auto &Fields =
299 OutStreamer->emitCFILLVMVectorRegisterMask(
300 Fields.Register, Fields.SpillRegister,
301 Fields.SpillRegisterLaneSizeInBits, Fields.MaskRegister,
302 Fields.MaskRegisterSizeInBits);
303 break;
304 }
305
307 OutStreamer->emitCFIValOffset(Inst.getRegister(), Inst.getOffset(), Loc);
308 break;
309 }
310}
311
312void AsmPrinter::emitDwarfDIE(const DIE &Die) const {
313 assert(!Die.isPlaceholder() && "Cannot emit a placeholder DIE");
314
315 // Emit the code (index) for the abbreviation.
316 if (isVerbose())
317 OutStreamer->AddComment("Abbrev [" + Twine(Die.getAbbrevNumber()) + "] 0x" +
318 Twine::utohexstr(Die.getOffset()) + ":0x" +
319 Twine::utohexstr(Die.getSize()) + " " +
320 dwarf::TagString(Die.getTag()));
322
323 // Emit the DIE attribute values.
324 for (const auto &V : Die.values()) {
325 dwarf::Attribute Attr = V.getAttribute();
326 assert(V.getForm() && "Too many attributes for DIE (check abbreviation)");
327
328 if (isVerbose()) {
329 OutStreamer->AddComment(dwarf::AttributeString(Attr));
330 if (Attr == dwarf::DW_AT_accessibility)
331 OutStreamer->AddComment(
332 dwarf::AccessibilityString(V.getDIEInteger().getValue()));
333 }
334
335 // Emit an attribute using the defined form.
336 V.emitValue(this);
337 }
338
339 // Emit the DIE children if any.
340 if (Die.hasChildren()) {
341 for (const auto &Child : Die.children())
342 emitDwarfDIE(Child);
343
344 OutStreamer->AddComment("End Of Children Mark");
345 emitInt8(0);
346 }
347}
348
349void AsmPrinter::emitDwarfAbbrev(const DIEAbbrev &Abbrev) const {
350 // Emit the abbreviations code (base 1 index.)
351 emitULEB128(Abbrev.getNumber(), "Abbreviation Code");
352
353 // Emit the abbreviations data.
354 Abbrev.Emit(this);
355}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static const char * DecodeDWARFEncoding(unsigned Encoding)
This file contains constants used for implementing Dwarf debug support.
const TargetLoweringObjectFile & getObjFileLowering() const
Return information about object file lowering.
void emitULEB128(uint64_t Value, const char *Desc=nullptr, unsigned PadTo=0) const
Emit the specified unsigned leb128 value.
void emitDwarfSymbolReference(const MCSymbol *Label, bool ForceOffset=false) const
Emit a reference to a symbol for use in dwarf.
bool isDwarf64() const
unsigned GetSizeOfEncodedValue(unsigned Encoding) const
Return the size of the encoding in bytes.
void emitDwarfDIE(const DIE &Die) const
Recursively emit Dwarf DIE tree.
void emitDwarfLengthOrOffset(uint64_t Value) const
Emit 32- or 64-bit value depending on the DWARF format.
void emitLabelPlusOffset(const MCSymbol *Label, uint64_t Offset, unsigned Size, bool IsSectionRelative=false) const
Emit something like ".long Label+Offset" where the size in bytes of the directive is specified by Siz...
TargetMachine & TM
Target machine description.
Definition AsmPrinter.h:98
void emitDwarfStringOffset(DwarfStringPoolEntry S) const
Emit the 4- or 8-byte offset of a string from the start of its section.
void emitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo, unsigned Size) const
Emit something like ".long Hi-Lo" where the size in bytes of the directive is specified by Size and H...
virtual void emitTTypeReference(const GlobalValue *GV, unsigned Encoding)
Emit reference to a ttype global with a specified encoding.
void emitDwarfOffset(const MCSymbol *Label, uint64_t Offset) const
Emit something like ".long Label + Offset" or ".quad Label + Offset" depending on the DWARF format.
void emitInt8(int Value) const
Emit a byte directive and value.
void emitDwarfUnitLength(uint64_t Length, const Twine &Comment) const
Emit a unit length field.
void emitEncodingByte(unsigned Val, const char *Desc=nullptr) const
Emit a .byte 42 directive that corresponds to an encoding.
MachineModuleInfo * MMI
This is a pointer to the current MachineModuleInfo.
Definition AsmPrinter.h:116
void emitCFIInstruction(const MachineInstr &MI)
void emitCallSiteOffset(const MCSymbol *Hi, const MCSymbol *Lo, unsigned Encoding) const
Emit reference to a call site with a specified encoding.
void emitDwarfAbbrev(const DIEAbbrev &Abbrev) const
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition AsmPrinter.h:110
const MCAsmInfo & MAI
Target Asm Printer information.
Definition AsmPrinter.h:101
unsigned int getDwarfOffsetByteSize() const
Returns 4 for DWARF32 and 8 for DWARF64.
bool isVerbose() const
Return true if assembly output should contain comments.
Definition AsmPrinter.h:335
bool doesDwarfUseRelocationsAcrossSections() const
Definition AsmPrinter.h:401
void emitCallSiteValue(uint64_t Value, unsigned Encoding) const
Emit an integer value corresponding to the call site encoding.
void emitLabelDifferenceAsULEB128(const MCSymbol *Hi, const MCSymbol *Lo) const
Emit something like ".uleb128 Hi-Lo".
Dwarf abbreviation, describes the organization of a debug information object.
Definition DIE.h:80
unsigned getNumber() const
Definition DIE.h:102
LLVM_ABI void Emit(const AsmPrinter *AP) const
Print the abbreviation using the specified asm printer.
Definition DIE.cpp:62
value_range values()
Definition DIE.h:823
A structured debug information entry.
Definition DIE.h:835
unsigned getAbbrevNumber() const
Definition DIE.h:879
unsigned getSize() const
Definition DIE.h:887
bool isPlaceholder() const
Returns true if DIE is a temporary placeholder.
Definition DIE.h:895
child_range children()
Definition DIE.h:902
unsigned getOffset() const
Get the compile/type unit relative offset of this DIE.
Definition DIE.h:882
bool hasChildren() const
Definition DIE.h:892
dwarf::Tag getTag() const
Definition DIE.h:880
unsigned getRASignState() const
Definition MCDwarf.h:863
unsigned getAddressSpace() const
Definition MCDwarf.h:850
MCSymbol * getRASignSymbol() const
Definition MCDwarf.h:868
unsigned getRegister2() const
Definition MCDwarf.h:845
unsigned getRegister() const
Definition MCDwarf.h:836
SMLoc getLoc() const
Definition MCDwarf.h:893
OpType getOperation() const
Definition MCDwarf.h:833
StringRef getComment() const
Definition MCDwarf.h:889
int64_t getRASignOffset() const
Definition MCDwarf.h:873
StringRef getValues() const
Definition MCDwarf.h:883
ExtraFieldsTy & getExtraFields()
Definition MCDwarf.h:819
int64_t getOffset() const
Definition MCDwarf.h:855
Base class for the full range of assembler expressions which are needed for parsing.
Definition MCExpr.h:34
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
Represents a location in source code.
Definition SMLoc.h:22
virtual const MCExpr * getTTypeGlobalReference(const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM, MachineModuleInfo *MMI, MCStreamer &Streamer) const
Return an MCExpr to use for a reference to the specified global variable from exception handling info...
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
static Twine utohexstr(uint64_t Val)
Definition Twine.h:385
LLVM Value Representation.
Definition Value.h:75
LLVM_ABI StringRef AttributeString(unsigned Attribute)
Definition Dwarf.cpp:72
LLVM_ABI StringRef TagString(unsigned Tag)
Definition Dwarf.cpp:21
LLVM_ABI StringRef AccessibilityString(unsigned Access)
Definition Dwarf.cpp:337
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Attribute
Attributes.
Definition Dwarf.h:125
@ DW_EH_PE_datarel
Definition Dwarf.h:984
@ DW_EH_PE_pcrel
Definition Dwarf.h:982
@ DW_EH_PE_sdata4
Definition Dwarf.h:979
@ DW_EH_PE_udata2
Definition Dwarf.h:974
@ DW_EH_PE_sdata8
Definition Dwarf.h:980
@ DW_EH_PE_absptr
Definition Dwarf.h:971
@ DW_EH_PE_udata4
Definition Dwarf.h:975
@ DW_EH_PE_udata8
Definition Dwarf.h:976
@ DW_EH_PE_uleb128
Definition Dwarf.h:973
@ DW_EH_PE_indirect
Definition Dwarf.h:987
@ DW_EH_PE_sleb128
Definition Dwarf.h:977
@ DW_EH_PE_omit
Definition Dwarf.h:972
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
@ Length
Definition DWP.cpp:577
Op::Description Desc
Data for a string pool entry.
Held in ExtraFields when OpLLVMRegisterPair.
Definition MCDwarf.h:567
Held in ExtraFields when OpLLVMVectorOffset.
Definition MCDwarf.h:583
Held in ExtraFields when OpLLVMVectorRegisterMask.
Definition MCDwarf.h:591
Held in ExtraFields when OpLLVMVectorRegisters.
Definition MCDwarf.h:578