Revert r354244 "[DAGCombiner] Eliminate dead stores to stack."
[llvm-complete.git] / lib / CodeGen / AsmPrinter / AsmPrinterDwarf.cpp
blob7a62b6bfc6a4502424f9f72b8534eb8b16f63a97
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.
11 //===----------------------------------------------------------------------===//
13 #include "ByteStreamer.h"
14 #include "llvm/ADT/Twine.h"
15 #include "llvm/BinaryFormat/Dwarf.h"
16 #include "llvm/CodeGen/AsmPrinter.h"
17 #include "llvm/CodeGen/DIE.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/IR/DataLayout.h"
20 #include "llvm/MC/MCAsmInfo.h"
21 #include "llvm/MC/MCRegisterInfo.h"
22 #include "llvm/MC/MCSection.h"
23 #include "llvm/MC/MCStreamer.h"
24 #include "llvm/MC/MCSymbol.h"
25 #include "llvm/MC/MachineLocation.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include "llvm/Target/TargetLoweringObjectFile.h"
28 #include "llvm/Target/TargetMachine.h"
29 using namespace llvm;
31 #define DEBUG_TYPE "asm-printer"
33 //===----------------------------------------------------------------------===//
34 // Dwarf Emission Helper Routines
35 //===----------------------------------------------------------------------===//
37 /// EmitSLEB128 - emit the specified signed leb128 value.
38 void AsmPrinter::EmitSLEB128(int64_t Value, const char *Desc) const {
39 if (isVerbose() && Desc)
40 OutStreamer->AddComment(Desc);
42 OutStreamer->EmitSLEB128IntValue(Value);
45 void AsmPrinter::EmitULEB128(uint64_t Value, const char *Desc) const {
46 if (isVerbose() && Desc)
47 OutStreamer->AddComment(Desc);
49 OutStreamer->EmitULEB128IntValue(Value);
52 /// Emit something like ".uleb128 Hi-Lo".
53 void AsmPrinter::EmitLabelDifferenceAsULEB128(const MCSymbol *Hi,
54 const MCSymbol *Lo) const {
55 OutStreamer->emitAbsoluteSymbolDiffAsULEB128(Hi, Lo);
58 static const char *DecodeDWARFEncoding(unsigned Encoding) {
59 switch (Encoding) {
60 case dwarf::DW_EH_PE_absptr:
61 return "absptr";
62 case dwarf::DW_EH_PE_omit:
63 return "omit";
64 case dwarf::DW_EH_PE_pcrel:
65 return "pcrel";
66 case dwarf::DW_EH_PE_uleb128:
67 return "uleb128";
68 case dwarf::DW_EH_PE_sleb128:
69 return "sleb128";
70 case dwarf::DW_EH_PE_udata4:
71 return "udata4";
72 case dwarf::DW_EH_PE_udata8:
73 return "udata8";
74 case dwarf::DW_EH_PE_sdata4:
75 return "sdata4";
76 case dwarf::DW_EH_PE_sdata8:
77 return "sdata8";
78 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
79 return "pcrel udata4";
80 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
81 return "pcrel sdata4";
82 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
83 return "pcrel udata8";
84 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
85 return "pcrel sdata8";
86 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4
88 return "indirect pcrel udata4";
89 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4
91 return "indirect pcrel sdata4";
92 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8
94 return "indirect pcrel udata8";
95 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8
97 return "indirect pcrel sdata8";
100 return "<unknown encoding>";
103 /// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
104 /// encoding. If verbose assembly output is enabled, we output comments
105 /// describing the encoding. Desc is an optional string saying what the
106 /// encoding is specifying (e.g. "LSDA").
107 void AsmPrinter::EmitEncodingByte(unsigned Val, const char *Desc) const {
108 if (isVerbose()) {
109 if (Desc)
110 OutStreamer->AddComment(Twine(Desc) + " Encoding = " +
111 Twine(DecodeDWARFEncoding(Val)));
112 else
113 OutStreamer->AddComment(Twine("Encoding = ") + DecodeDWARFEncoding(Val));
116 OutStreamer->EmitIntValue(Val, 1);
119 /// GetSizeOfEncodedValue - Return the size of the encoding in bytes.
120 unsigned AsmPrinter::GetSizeOfEncodedValue(unsigned Encoding) const {
121 if (Encoding == dwarf::DW_EH_PE_omit)
122 return 0;
124 switch (Encoding & 0x07) {
125 default:
126 llvm_unreachable("Invalid encoded value.");
127 case dwarf::DW_EH_PE_absptr:
128 return MF->getDataLayout().getPointerSize();
129 case dwarf::DW_EH_PE_udata2:
130 return 2;
131 case dwarf::DW_EH_PE_udata4:
132 return 4;
133 case dwarf::DW_EH_PE_udata8:
134 return 8;
138 void AsmPrinter::EmitTTypeReference(const GlobalValue *GV,
139 unsigned Encoding) const {
140 if (GV) {
141 const TargetLoweringObjectFile &TLOF = getObjFileLowering();
143 const MCExpr *Exp =
144 TLOF.getTTypeGlobalReference(GV, Encoding, TM, MMI, *OutStreamer);
145 OutStreamer->EmitValue(Exp, GetSizeOfEncodedValue(Encoding));
146 } else
147 OutStreamer->EmitIntValue(0, GetSizeOfEncodedValue(Encoding));
150 void AsmPrinter::emitDwarfSymbolReference(const MCSymbol *Label,
151 bool ForceOffset) const {
152 if (!ForceOffset) {
153 // On COFF targets, we have to emit the special .secrel32 directive.
154 if (MAI->needsDwarfSectionOffsetDirective()) {
155 OutStreamer->EmitCOFFSecRel32(Label, /*Offset=*/0);
156 return;
159 // If the format uses relocations with dwarf, refer to the symbol directly.
160 if (MAI->doesDwarfUseRelocationsAcrossSections()) {
161 OutStreamer->EmitSymbolValue(Label, 4);
162 return;
166 // Otherwise, emit it as a label difference from the start of the section.
167 EmitLabelDifference(Label, Label->getSection().getBeginSymbol(), 4);
170 void AsmPrinter::emitDwarfStringOffset(DwarfStringPoolEntry S) const {
171 if (MAI->doesDwarfUseRelocationsAcrossSections()) {
172 assert(S.Symbol && "No symbol available");
173 emitDwarfSymbolReference(S.Symbol);
174 return;
177 // Just emit the offset directly; no need for symbol math.
178 emitInt32(S.Offset);
181 void AsmPrinter::EmitDwarfOffset(const MCSymbol *Label, uint64_t Offset) const {
182 EmitLabelPlusOffset(Label, Offset, MAI->getCodePointerSize());
185 //===----------------------------------------------------------------------===//
186 // Dwarf Lowering Routines
187 //===----------------------------------------------------------------------===//
189 void AsmPrinter::emitCFIInstruction(const MCCFIInstruction &Inst) const {
190 switch (Inst.getOperation()) {
191 default:
192 llvm_unreachable("Unexpected instruction");
193 case MCCFIInstruction::OpDefCfaOffset:
194 OutStreamer->EmitCFIDefCfaOffset(Inst.getOffset());
195 break;
196 case MCCFIInstruction::OpAdjustCfaOffset:
197 OutStreamer->EmitCFIAdjustCfaOffset(Inst.getOffset());
198 break;
199 case MCCFIInstruction::OpDefCfa:
200 OutStreamer->EmitCFIDefCfa(Inst.getRegister(), Inst.getOffset());
201 break;
202 case MCCFIInstruction::OpDefCfaRegister:
203 OutStreamer->EmitCFIDefCfaRegister(Inst.getRegister());
204 break;
205 case MCCFIInstruction::OpOffset:
206 OutStreamer->EmitCFIOffset(Inst.getRegister(), Inst.getOffset());
207 break;
208 case MCCFIInstruction::OpRegister:
209 OutStreamer->EmitCFIRegister(Inst.getRegister(), Inst.getRegister2());
210 break;
211 case MCCFIInstruction::OpWindowSave:
212 OutStreamer->EmitCFIWindowSave();
213 break;
214 case MCCFIInstruction::OpNegateRAState:
215 OutStreamer->EmitCFINegateRAState();
216 break;
217 case MCCFIInstruction::OpSameValue:
218 OutStreamer->EmitCFISameValue(Inst.getRegister());
219 break;
220 case MCCFIInstruction::OpGnuArgsSize:
221 OutStreamer->EmitCFIGnuArgsSize(Inst.getOffset());
222 break;
223 case MCCFIInstruction::OpEscape:
224 OutStreamer->EmitCFIEscape(Inst.getValues());
225 break;
226 case MCCFIInstruction::OpRestore:
227 OutStreamer->EmitCFIRestore(Inst.getRegister());
228 break;
232 void AsmPrinter::emitDwarfDIE(const DIE &Die) const {
233 // Emit the code (index) for the abbreviation.
234 if (isVerbose())
235 OutStreamer->AddComment("Abbrev [" + Twine(Die.getAbbrevNumber()) + "] 0x" +
236 Twine::utohexstr(Die.getOffset()) + ":0x" +
237 Twine::utohexstr(Die.getSize()) + " " +
238 dwarf::TagString(Die.getTag()));
239 EmitULEB128(Die.getAbbrevNumber());
241 // Emit the DIE attribute values.
242 for (const auto &V : Die.values()) {
243 dwarf::Attribute Attr = V.getAttribute();
244 assert(V.getForm() && "Too many attributes for DIE (check abbreviation)");
246 if (isVerbose()) {
247 OutStreamer->AddComment(dwarf::AttributeString(Attr));
248 if (Attr == dwarf::DW_AT_accessibility)
249 OutStreamer->AddComment(
250 dwarf::AccessibilityString(V.getDIEInteger().getValue()));
253 // Emit an attribute using the defined form.
254 V.EmitValue(this);
257 // Emit the DIE children if any.
258 if (Die.hasChildren()) {
259 for (auto &Child : Die.children())
260 emitDwarfDIE(Child);
262 OutStreamer->AddComment("End Of Children Mark");
263 emitInt8(0);
267 void AsmPrinter::emitDwarfAbbrev(const DIEAbbrev &Abbrev) const {
268 // Emit the abbreviations code (base 1 index.)
269 EmitULEB128(Abbrev.getNumber(), "Abbreviation Code");
271 // Emit the abbreviations data.
272 Abbrev.Emit(this);