blob: 39b0b027c76577ac3a4efa946a179fabaab2e85a [file] [log] [blame]
Bill Wendling47054f32009-05-15 00:11:17 +00001//===--- lib/CodeGen/DIE.cpp - DWARF Info Entries -------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Bill Wendling47054f32009-05-15 00:11:17 +00006//
7//===----------------------------------------------------------------------===//
8//
9// Data structures for DWARF info entries.
Eric Christopherb800ff72013-01-07 22:40:45 +000010//
Bill Wendling47054f32009-05-15 00:11:17 +000011//===----------------------------------------------------------------------===//
12
Frederic Risse541e0b2015-01-05 21:29:41 +000013#include "llvm/CodeGen/DIE.h"
David Blaikie37c52312014-10-04 15:49:50 +000014#include "DwarfCompileUnit.h"
Manman Renac8062b2013-07-02 23:40:10 +000015#include "DwarfDebug.h"
David Blaikie47f615e2013-12-17 23:32:35 +000016#include "DwarfUnit.h"
Benjamin Kramer4d128a22010-01-17 07:46:39 +000017#include "llvm/ADT/Twine.h"
Bill Wendling47054f32009-05-15 00:11:17 +000018#include "llvm/CodeGen/AsmPrinter.h"
Nico Weber432a3882018-04-30 14:59:11 +000019#include "llvm/Config/llvm-config.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#include "llvm/IR/DataLayout.h"
Chris Lattner7b26fce2009-08-22 20:48:53 +000021#include "llvm/MC/MCAsmInfo.h"
Rafael Espindola74dd8542014-10-21 00:25:49 +000022#include "llvm/MC/MCContext.h"
Chris Lattner71601e82010-01-20 07:41:15 +000023#include "llvm/MC/MCStreamer.h"
Chris Lattner06d45f62010-01-16 18:50:28 +000024#include "llvm/MC/MCSymbol.h"
David Greene8bc072c2009-12-24 00:27:55 +000025#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000026#include "llvm/Support/ErrorHandling.h"
Chris Lattner74725712009-08-23 01:01:17 +000027#include "llvm/Support/Format.h"
Chris Lattnerf5c834f2010-01-22 22:09:00 +000028#include "llvm/Support/FormattedStream.h"
Logan Chien5b776b72014-02-22 14:00:39 +000029#include "llvm/Support/LEB128.h"
Eric Christopher67646432013-07-26 17:02:41 +000030#include "llvm/Support/MD5.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000031#include "llvm/Support/raw_ostream.h"
Bill Wendling47054f32009-05-15 00:11:17 +000032using namespace llvm;
33
Paul Robinson70b34532017-04-20 19:16:51 +000034#define DEBUG_TYPE "dwarfdebug"
35
Bill Wendling47054f32009-05-15 00:11:17 +000036//===----------------------------------------------------------------------===//
37// DIEAbbrevData Implementation
38//===----------------------------------------------------------------------===//
39
40/// Profile - Used to gather unique data for the abbreviation folding set.
41///
42void DIEAbbrevData::Profile(FoldingSetNodeID &ID) const {
Reid Klecknerad65f102013-10-21 19:18:31 +000043 // Explicitly cast to an integer type for which FoldingSetNodeID has
44 // overloads. Otherwise MSVC 2010 thinks this call is ambiguous.
45 ID.AddInteger(unsigned(Attribute));
46 ID.AddInteger(unsigned(Form));
Victor Leschukd7bfa402017-03-01 22:13:42 +000047 if (Form == dwarf::DW_FORM_implicit_const)
48 ID.AddInteger(Value);
Bill Wendling47054f32009-05-15 00:11:17 +000049}
50
51//===----------------------------------------------------------------------===//
52// DIEAbbrev Implementation
53//===----------------------------------------------------------------------===//
54
55/// Profile - Used to gather unique data for the abbreviation folding set.
56///
57void DIEAbbrev::Profile(FoldingSetNodeID &ID) const {
Reid Klecknerad65f102013-10-21 19:18:31 +000058 ID.AddInteger(unsigned(Tag));
Eric Christophere8f10722014-03-05 01:44:58 +000059 ID.AddInteger(unsigned(Children));
Bill Wendling47054f32009-05-15 00:11:17 +000060
61 // For each attribute description.
62 for (unsigned i = 0, N = Data.size(); i < N; ++i)
63 Data[i].Profile(ID);
64}
65
66/// Emit - Print the abbreviation using the specified asm printer.
67///
Frederic Risscd044342015-03-04 02:30:08 +000068void DIEAbbrev::Emit(const AsmPrinter *AP) const {
Bill Wendling47054f32009-05-15 00:11:17 +000069 // Emit its Dwarf tag type.
Fangrui Song0bc77a02020-02-13 13:26:21 -080070 AP->emitULEB128(Tag, dwarf::TagString(Tag).data());
Bill Wendling47054f32009-05-15 00:11:17 +000071
72 // Emit whether it has children DIEs.
Fangrui Song0bc77a02020-02-13 13:26:21 -080073 AP->emitULEB128((unsigned)Children, dwarf::ChildrenString(Children).data());
Bill Wendling47054f32009-05-15 00:11:17 +000074
75 // For each attribute description.
76 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
77 const DIEAbbrevData &AttrData = Data[i];
78
79 // Emit attribute type.
Fangrui Song0bc77a02020-02-13 13:26:21 -080080 AP->emitULEB128(AttrData.getAttribute(),
Mehdi Amini149f6ea2016-10-05 05:59:29 +000081 dwarf::AttributeString(AttrData.getAttribute()).data());
Bill Wendling47054f32009-05-15 00:11:17 +000082
83 // Emit form type.
Paul Robinson70b34532017-04-20 19:16:51 +000084#ifndef NDEBUG
85 // Could be an assertion, but this way we can see the failing form code
86 // easily, which helps track down where it came from.
87 if (!dwarf::isValidFormForVersion(AttrData.getForm(),
88 AP->getDwarfVersion())) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +000089 LLVM_DEBUG(dbgs() << "Invalid form " << format("0x%x", AttrData.getForm())
90 << " for DWARF version " << AP->getDwarfVersion()
91 << "\n");
Paul Robinson70b34532017-04-20 19:16:51 +000092 llvm_unreachable("Invalid form for specified DWARF version");
93 }
94#endif
Fangrui Song0bc77a02020-02-13 13:26:21 -080095 AP->emitULEB128(AttrData.getForm(),
Mehdi Amini149f6ea2016-10-05 05:59:29 +000096 dwarf::FormEncodingString(AttrData.getForm()).data());
Victor Leschukcbddae72017-01-10 21:18:26 +000097
98 // Emit value for DW_FORM_implicit_const.
Paul Robinson70b34532017-04-20 19:16:51 +000099 if (AttrData.getForm() == dwarf::DW_FORM_implicit_const)
Fangrui Song0bc77a02020-02-13 13:26:21 -0800100 AP->emitSLEB128(AttrData.getValue());
Bill Wendling47054f32009-05-15 00:11:17 +0000101 }
102
103 // Mark end of abbreviation.
Fangrui Song0bc77a02020-02-13 13:26:21 -0800104 AP->emitULEB128(0, "EOM(1)");
105 AP->emitULEB128(0, "EOM(2)");
Bill Wendling47054f32009-05-15 00:11:17 +0000106}
107
Davide Italianoc304a0d2015-11-24 02:21:43 +0000108LLVM_DUMP_METHOD
Sam Clegg705f7982017-06-21 22:19:17 +0000109void DIEAbbrev::print(raw_ostream &O) const {
Bill Wendling47054f32009-05-15 00:11:17 +0000110 O << "Abbreviation @"
Chris Lattner74725712009-08-23 01:01:17 +0000111 << format("0x%lx", (long)(intptr_t)this)
Bill Wendling47054f32009-05-15 00:11:17 +0000112 << " "
113 << dwarf::TagString(Tag)
114 << " "
Eric Christophere8f10722014-03-05 01:44:58 +0000115 << dwarf::ChildrenString(Children)
Chris Lattner74725712009-08-23 01:01:17 +0000116 << '\n';
Bill Wendling47054f32009-05-15 00:11:17 +0000117
118 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
119 O << " "
120 << dwarf::AttributeString(Data[i].getAttribute())
121 << " "
Victor Leschukd7bfa402017-03-01 22:13:42 +0000122 << dwarf::FormEncodingString(Data[i].getForm());
123
124 if (Data[i].getForm() == dwarf::DW_FORM_implicit_const)
125 O << " " << Data[i].getValue();
126
127 O << '\n';
Bill Wendling47054f32009-05-15 00:11:17 +0000128 }
129}
Davide Italianoc304a0d2015-11-24 02:21:43 +0000130
Aaron Ballman615eb472017-10-15 14:32:27 +0000131#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Sam Clegg705f7982017-06-21 22:19:17 +0000132LLVM_DUMP_METHOD void DIEAbbrev::dump() const {
Matthias Braun8c209aa2017-01-28 02:02:38 +0000133 print(dbgs());
134}
135#endif
Bill Wendling47054f32009-05-15 00:11:17 +0000136
Greg Clayton3462a422016-12-08 01:03:48 +0000137//===----------------------------------------------------------------------===//
138// DIEAbbrevSet Implementation
139//===----------------------------------------------------------------------===//
140
141DIEAbbrevSet::~DIEAbbrevSet() {
142 for (DIEAbbrev *Abbrev : Abbreviations)
143 Abbrev->~DIEAbbrev();
144}
145
146DIEAbbrev &DIEAbbrevSet::uniqueAbbreviation(DIE &Die) {
147
148 FoldingSetNodeID ID;
149 DIEAbbrev Abbrev = Die.generateAbbrev();
150 Abbrev.Profile(ID);
151
152 void *InsertPos;
153 if (DIEAbbrev *Existing =
154 AbbreviationsSet.FindNodeOrInsertPos(ID, InsertPos)) {
155 Die.setAbbrevNumber(Existing->getNumber());
156 return *Existing;
157 }
158
159 // Move the abbreviation to the heap and assign a number.
160 DIEAbbrev *New = new (Alloc) DIEAbbrev(std::move(Abbrev));
161 Abbreviations.push_back(New);
162 New->setNumber(Abbreviations.size());
163 Die.setAbbrevNumber(Abbreviations.size());
164
165 // Store it for lookup.
166 AbbreviationsSet.InsertNode(New, InsertPos);
167 return *New;
168}
169
170void DIEAbbrevSet::Emit(const AsmPrinter *AP, MCSection *Section) const {
171 if (!Abbreviations.empty()) {
172 // Start the debug abbrev section.
173 AP->OutStreamer->SwitchSection(Section);
174 AP->emitDwarfAbbrevs(Abbreviations);
175 }
176}
177
178//===----------------------------------------------------------------------===//
179// DIE Implementation
180//===----------------------------------------------------------------------===//
181
Greg Clayton35630c32016-12-01 18:56:29 +0000182DIE *DIE::getParent() const {
183 return Owner.dyn_cast<DIE*>();
184}
185
Duncan P. N. Exon Smith815a6eb52015-05-27 22:31:41 +0000186DIEAbbrev DIE::generateAbbrev() const {
187 DIEAbbrev Abbrev(Tag, hasChildren());
Duncan P. N. Exon Smith1ad5ebc2015-08-02 20:42:45 +0000188 for (const DIEValue &V : values())
Victor Leschukcbddae72017-01-10 21:18:26 +0000189 if (V.getForm() == dwarf::DW_FORM_implicit_const)
190 Abbrev.AddImplicitConstAttribute(V.getAttribute(),
191 V.getDIEInteger().getValue());
192 else
193 Abbrev.AddAttribute(V.getAttribute(), V.getForm());
Duncan P. N. Exon Smith815a6eb52015-05-27 22:31:41 +0000194 return Abbrev;
195}
196
Igor Kudrina845ebd2020-09-15 11:32:08 +0700197uint64_t DIE::getDebugSectionOffset() const {
Greg Clayton35630c32016-12-01 18:56:29 +0000198 const DIEUnit *Unit = getUnit();
199 assert(Unit && "DIE must be owned by a DIEUnit to get its absolute offset");
Benjamin Kramer6a8704c2016-12-01 19:10:10 +0000200 return Unit->getDebugSectionOffset() + getOffset();
Manman Ren4dbdc902013-10-31 17:54:35 +0000201}
202
Greg Clayton35630c32016-12-01 18:56:29 +0000203const DIE *DIE::getUnitDie() const {
Manman Rence20d462013-10-29 22:57:10 +0000204 const DIE *p = this;
205 while (p) {
David Blaikie409dd9c2013-11-19 23:08:21 +0000206 if (p->getTag() == dwarf::DW_TAG_compile_unit ||
207 p->getTag() == dwarf::DW_TAG_type_unit)
Manman Rence20d462013-10-29 22:57:10 +0000208 return p;
209 p = p->getParent();
210 }
Craig Topper353eda42014-04-24 06:44:33 +0000211 return nullptr;
Manman Rence20d462013-10-29 22:57:10 +0000212}
213
Eugene Leviant7f78d472019-02-27 14:46:59 +0000214DIEUnit *DIE::getUnit() const {
Greg Clayton35630c32016-12-01 18:56:29 +0000215 const DIE *UnitDie = getUnitDie();
216 if (UnitDie)
217 return UnitDie->Owner.dyn_cast<DIEUnit*>();
218 return nullptr;
219}
220
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000221DIEValue DIE::findAttribute(dwarf::Attribute Attribute) const {
Eric Christopher8552e222013-08-07 01:18:33 +0000222 // Iterate through all the attributes until we find the one we're
223 // looking for, if we can't find it return NULL.
Duncan P. N. Exon Smith88a8fc52015-05-27 22:44:06 +0000224 for (const auto &V : values())
225 if (V.getAttribute() == Attribute)
226 return V;
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000227 return DIEValue();
Eric Christopher8552e222013-08-07 01:18:33 +0000228}
229
Davide Italianoc304a0d2015-11-24 02:21:43 +0000230LLVM_DUMP_METHOD
Duncan P. N. Exon Smithc5821142015-08-02 20:46:49 +0000231static void printValues(raw_ostream &O, const DIEValueList &Values,
232 StringRef Type, unsigned Size, unsigned IndentCount) {
233 O << Type << ": Size: " << Size << "\n";
234
235 unsigned I = 0;
236 const std::string Indent(IndentCount, ' ');
237 for (const auto &V : Values.values()) {
238 O << Indent;
239 O << "Blk[" << I++ << "]";
240 O << " " << dwarf::FormEncodingString(V.getForm()) << " ";
241 V.print(O);
242 O << "\n";
243 }
244}
245
Davide Italianoc304a0d2015-11-24 02:21:43 +0000246LLVM_DUMP_METHOD
Eric Christopher6c6de842013-05-06 17:50:50 +0000247void DIE::print(raw_ostream &O, unsigned IndentCount) const {
Bill Wendling47054f32009-05-15 00:11:17 +0000248 const std::string Indent(IndentCount, ' ');
Duncan P. N. Exon Smithc5821142015-08-02 20:46:49 +0000249 O << Indent << "Die: " << format("0x%lx", (long)(intptr_t) this)
250 << ", Offset: " << Offset << ", Size: " << Size << "\n";
Bill Wendling47054f32009-05-15 00:11:17 +0000251
Duncan P. N. Exon Smithc5821142015-08-02 20:46:49 +0000252 O << Indent << dwarf::TagString(getTag()) << " "
253 << dwarf::ChildrenString(hasChildren()) << "\n";
Bill Wendling47054f32009-05-15 00:11:17 +0000254
Bill Wendling47054f32009-05-15 00:11:17 +0000255 IndentCount += 2;
Duncan P. N. Exon Smith1ad5ebc2015-08-02 20:42:45 +0000256 for (const auto &V : values()) {
Bill Wendling47054f32009-05-15 00:11:17 +0000257 O << Indent;
Duncan P. N. Exon Smithc5821142015-08-02 20:46:49 +0000258 O << dwarf::AttributeString(V.getAttribute());
Duncan P. N. Exon Smith4fb1f9c2015-06-25 23:46:41 +0000259 O << " " << dwarf::FormEncodingString(V.getForm()) << " ";
260 V.print(O);
Bill Wendling47054f32009-05-15 00:11:17 +0000261 O << "\n";
262 }
263 IndentCount -= 2;
264
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000265 for (const auto &Child : children())
266 Child.print(O, IndentCount + 4);
Bill Wendling47054f32009-05-15 00:11:17 +0000267
Duncan P. N. Exon Smithc5821142015-08-02 20:46:49 +0000268 O << "\n";
Bill Wendling47054f32009-05-15 00:11:17 +0000269}
270
Aaron Ballman615eb472017-10-15 14:32:27 +0000271#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Sam Clegg705f7982017-06-21 22:19:17 +0000272LLVM_DUMP_METHOD void DIE::dump() const {
David Greene8bc072c2009-12-24 00:27:55 +0000273 print(dbgs());
Bill Wendling47054f32009-05-15 00:11:17 +0000274}
Matthias Braun8c209aa2017-01-28 02:02:38 +0000275#endif
Bill Wendling47054f32009-05-15 00:11:17 +0000276
Greg Clayton3462a422016-12-08 01:03:48 +0000277unsigned DIE::computeOffsetsAndAbbrevs(const AsmPrinter *AP,
278 DIEAbbrevSet &AbbrevSet,
279 unsigned CUOffset) {
280 // Unique the abbreviation and fill in the abbreviation number so this DIE
281 // can be emitted.
282 const DIEAbbrev &Abbrev = AbbrevSet.uniqueAbbreviation(*this);
283
284 // Set compile/type unit relative offset of this DIE.
285 setOffset(CUOffset);
286
287 // Add the byte size of the abbreviation code.
288 CUOffset += getULEB128Size(getAbbrevNumber());
289
290 // Add the byte size of all the DIE attribute values.
291 for (const auto &V : values())
292 CUOffset += V.SizeOf(AP);
293
294 // Let the children compute their offsets and abbreviation numbers.
295 if (hasChildren()) {
296 (void)Abbrev;
297 assert(Abbrev.hasChildren() && "Children flag not set");
298
299 for (auto &Child : children())
300 CUOffset = Child.computeOffsetsAndAbbrevs(AP, AbbrevSet, CUOffset);
301
302 // Each child chain is terminated with a zero byte, adjust the offset.
303 CUOffset += sizeof(int8_t);
304 }
305
306 // Compute the byte size of this DIE and all of its children correctly. This
307 // is needed so that top level DIE can help the compile unit set its length
308 // correctly.
309 setSize(CUOffset - getOffset());
310 return CUOffset;
311}
312
313//===----------------------------------------------------------------------===//
314// DIEUnit Implementation
315//===----------------------------------------------------------------------===//
Igor Kudrinb6b0ff12020-08-06 17:49:05 +0700316DIEUnit::DIEUnit(dwarf::Tag UnitTag)
317 : Die(UnitTag), Section(nullptr), Offset(0) {
David Blaikiee40caae2016-12-01 21:59:09 +0000318 Die.Owner = this;
Greg Clayton35630c32016-12-01 18:56:29 +0000319 assert((UnitTag == dwarf::DW_TAG_compile_unit ||
Alexey Lapshin789e2572019-11-30 23:48:32 +0300320 UnitTag == dwarf::DW_TAG_skeleton_unit ||
Greg Clayton35630c32016-12-01 18:56:29 +0000321 UnitTag == dwarf::DW_TAG_type_unit ||
Alexey Lapshin789e2572019-11-30 23:48:32 +0300322 UnitTag == dwarf::DW_TAG_partial_unit) &&
323 "expected a unit TAG");
Greg Clayton35630c32016-12-01 18:56:29 +0000324}
325
Fangrui Song77497102020-02-14 22:40:47 -0800326void DIEValue::emitValue(const AsmPrinter *AP) const {
Duncan P. N. Exon Smith68b3f302015-05-23 01:45:07 +0000327 switch (Ty) {
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000328 case isNone:
329 llvm_unreachable("Expected valid DIEValue");
Duncan P. N. Exon Smithff189272015-05-27 21:15:43 +0000330#define HANDLE_DIEVALUE(T) \
331 case is##T: \
Fangrui Song77497102020-02-14 22:40:47 -0800332 getDIE##T().emitValue(AP, Form); \
Duncan P. N. Exon Smith68b3f302015-05-23 01:45:07 +0000333 break;
Duncan P. N. Exon Smithff189272015-05-27 21:15:43 +0000334#include "llvm/CodeGen/DIEValue.def"
Duncan P. N. Exon Smith68b3f302015-05-23 01:45:07 +0000335 }
336}
337
Duncan P. N. Exon Smith9dbb5012015-06-24 18:48:11 +0000338unsigned DIEValue::SizeOf(const AsmPrinter *AP) const {
Duncan P. N. Exon Smith68b3f302015-05-23 01:45:07 +0000339 switch (Ty) {
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000340 case isNone:
341 llvm_unreachable("Expected valid DIEValue");
Duncan P. N. Exon Smithff189272015-05-27 21:15:43 +0000342#define HANDLE_DIEVALUE(T) \
343 case is##T: \
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000344 return getDIE##T().SizeOf(AP, Form);
Duncan P. N. Exon Smithff189272015-05-27 21:15:43 +0000345#include "llvm/CodeGen/DIEValue.def"
Duncan P. N. Exon Smith68b3f302015-05-23 01:45:07 +0000346 }
Aaron Ballmanc681c3d2015-05-23 14:46:49 +0000347 llvm_unreachable("Unknown DIE kind");
Duncan P. N. Exon Smith68b3f302015-05-23 01:45:07 +0000348}
Bill Wendling47054f32009-05-15 00:11:17 +0000349
Davide Italianoc304a0d2015-11-24 02:21:43 +0000350LLVM_DUMP_METHOD
Duncan P. N. Exon Smith68b3f302015-05-23 01:45:07 +0000351void DIEValue::print(raw_ostream &O) const {
352 switch (Ty) {
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000353 case isNone:
354 llvm_unreachable("Expected valid DIEValue");
Duncan P. N. Exon Smithff189272015-05-27 21:15:43 +0000355#define HANDLE_DIEVALUE(T) \
356 case is##T: \
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000357 getDIE##T().print(O); \
Duncan P. N. Exon Smith68b3f302015-05-23 01:45:07 +0000358 break;
Duncan P. N. Exon Smithff189272015-05-27 21:15:43 +0000359#include "llvm/CodeGen/DIEValue.def"
Duncan P. N. Exon Smith68b3f302015-05-23 01:45:07 +0000360 }
361}
362
Aaron Ballman615eb472017-10-15 14:32:27 +0000363#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Matthias Braun8c209aa2017-01-28 02:02:38 +0000364LLVM_DUMP_METHOD void DIEValue::dump() const {
David Greene8bc072c2009-12-24 00:27:55 +0000365 print(dbgs());
Bill Wendling47054f32009-05-15 00:11:17 +0000366}
Matthias Braun8c209aa2017-01-28 02:02:38 +0000367#endif
Bill Wendling47054f32009-05-15 00:11:17 +0000368
369//===----------------------------------------------------------------------===//
370// DIEInteger Implementation
371//===----------------------------------------------------------------------===//
372
373/// EmitValue - Emit integer of appropriate size.
374///
Fangrui Song77497102020-02-14 22:40:47 -0800375void DIEInteger::emitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
Bill Wendling47054f32009-05-15 00:11:17 +0000376 switch (Form) {
Victor Leschukcbddae72017-01-10 21:18:26 +0000377 case dwarf::DW_FORM_implicit_const:
Eric Christopherbb69a272012-08-24 01:14:27 +0000378 case dwarf::DW_FORM_flag_present:
379 // Emit something to keep the lines and comments in sync.
380 // FIXME: Is there a better way to do this?
Lang Hames9ff69c82015-04-24 19:11:51 +0000381 Asm->OutStreamer->AddBlankLine();
Eric Christopherbb69a272012-08-24 01:14:27 +0000382 return;
Greg Clayton3462a422016-12-08 01:03:48 +0000383 case dwarf::DW_FORM_flag:
Greg Clayton3462a422016-12-08 01:03:48 +0000384 case dwarf::DW_FORM_ref1:
Greg Clayton3462a422016-12-08 01:03:48 +0000385 case dwarf::DW_FORM_data1:
Paul Robinsonf96e21a2017-03-06 22:20:03 +0000386 case dwarf::DW_FORM_strx1:
387 case dwarf::DW_FORM_addrx1:
Greg Clayton3462a422016-12-08 01:03:48 +0000388 case dwarf::DW_FORM_ref2:
Greg Clayton3462a422016-12-08 01:03:48 +0000389 case dwarf::DW_FORM_data2:
Paul Robinsonf96e21a2017-03-06 22:20:03 +0000390 case dwarf::DW_FORM_strx2:
391 case dwarf::DW_FORM_addrx2:
Wolfgang Pieb456b5552018-01-26 18:52:58 +0000392 case dwarf::DW_FORM_strx3:
Greg Clayton3462a422016-12-08 01:03:48 +0000393 case dwarf::DW_FORM_strp:
Greg Clayton3462a422016-12-08 01:03:48 +0000394 case dwarf::DW_FORM_ref4:
Greg Clayton3462a422016-12-08 01:03:48 +0000395 case dwarf::DW_FORM_data4:
Paul Robinsonf96e21a2017-03-06 22:20:03 +0000396 case dwarf::DW_FORM_ref_sup4:
397 case dwarf::DW_FORM_strx4:
398 case dwarf::DW_FORM_addrx4:
Greg Clayton3462a422016-12-08 01:03:48 +0000399 case dwarf::DW_FORM_ref8:
Greg Clayton3462a422016-12-08 01:03:48 +0000400 case dwarf::DW_FORM_ref_sig8:
Greg Clayton3462a422016-12-08 01:03:48 +0000401 case dwarf::DW_FORM_data8:
Paul Robinsonf96e21a2017-03-06 22:20:03 +0000402 case dwarf::DW_FORM_ref_sup8:
Greg Clayton3462a422016-12-08 01:03:48 +0000403 case dwarf::DW_FORM_GNU_ref_alt:
Greg Clayton3462a422016-12-08 01:03:48 +0000404 case dwarf::DW_FORM_GNU_strp_alt:
Greg Clayton3462a422016-12-08 01:03:48 +0000405 case dwarf::DW_FORM_line_strp:
Greg Clayton3462a422016-12-08 01:03:48 +0000406 case dwarf::DW_FORM_sec_offset:
Greg Clayton3462a422016-12-08 01:03:48 +0000407 case dwarf::DW_FORM_strp_sup:
Eric Christophere8a7b1b2012-09-10 23:34:03 +0000408 case dwarf::DW_FORM_addr:
Frederic Rissee17fb9b2015-03-04 22:07:36 +0000409 case dwarf::DW_FORM_ref_addr:
Fangrui Song77497102020-02-14 22:40:47 -0800410 Asm->OutStreamer->emitIntValue(Integer, SizeOf(Asm, Form));
Greg Clayton3462a422016-12-08 01:03:48 +0000411 return;
412 case dwarf::DW_FORM_GNU_str_index:
Greg Clayton3462a422016-12-08 01:03:48 +0000413 case dwarf::DW_FORM_GNU_addr_index:
Greg Clayton3462a422016-12-08 01:03:48 +0000414 case dwarf::DW_FORM_ref_udata:
Wolfgang Pieb456b5552018-01-26 18:52:58 +0000415 case dwarf::DW_FORM_strx:
David Blaikie161dd3c2018-10-20 06:02:15 +0000416 case dwarf::DW_FORM_addrx:
David Blaikiec4af8bf2018-10-20 07:36:39 +0000417 case dwarf::DW_FORM_rnglistx:
Greg Clayton3462a422016-12-08 01:03:48 +0000418 case dwarf::DW_FORM_udata:
Fangrui Song0bc77a02020-02-13 13:26:21 -0800419 Asm->emitULEB128(Integer);
Greg Clayton3462a422016-12-08 01:03:48 +0000420 return;
421 case dwarf::DW_FORM_sdata:
Fangrui Song0bc77a02020-02-13 13:26:21 -0800422 Asm->emitSLEB128(Integer);
Greg Clayton3462a422016-12-08 01:03:48 +0000423 return;
Torok Edwinfbcc6632009-07-14 16:55:14 +0000424 default: llvm_unreachable("DIE Value form not supported yet");
Bill Wendling47054f32009-05-15 00:11:17 +0000425 }
426}
427
428/// SizeOf - Determine size of integer value in bytes.
429///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000430unsigned DIEInteger::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
Igor Kudrin027d47d2020-09-17 12:47:38 +0700431 assert(AP && "AsmPrinter is required to set FormParams");
432 dwarf::FormParams Params = {AP->getDwarfVersion(),
433 uint8_t(AP->getPointerSize()),
434 AP->OutStreamer->getContext().getDwarfFormat()};
Pavel Labath322711f2018-03-14 09:39:54 +0000435
436 if (Optional<uint8_t> FixedSize = dwarf::getFixedFormByteSize(Form, Params))
437 return *FixedSize;
438
Bill Wendling47054f32009-05-15 00:11:17 +0000439 switch (Form) {
Greg Clayton3462a422016-12-08 01:03:48 +0000440 case dwarf::DW_FORM_GNU_str_index:
Greg Clayton3462a422016-12-08 01:03:48 +0000441 case dwarf::DW_FORM_GNU_addr_index:
Greg Clayton3462a422016-12-08 01:03:48 +0000442 case dwarf::DW_FORM_ref_udata:
Wolfgang Pieb456b5552018-01-26 18:52:58 +0000443 case dwarf::DW_FORM_strx:
David Blaikie161dd3c2018-10-20 06:02:15 +0000444 case dwarf::DW_FORM_addrx:
David Blaikiec4af8bf2018-10-20 07:36:39 +0000445 case dwarf::DW_FORM_rnglistx:
Greg Clayton3462a422016-12-08 01:03:48 +0000446 case dwarf::DW_FORM_udata:
447 return getULEB128Size(Integer);
448 case dwarf::DW_FORM_sdata:
449 return getSLEB128Size(Integer);
David Blaikie46a9f012012-01-20 21:51:11 +0000450 default: llvm_unreachable("DIE Value form not supported yet");
Bill Wendling47054f32009-05-15 00:11:17 +0000451 }
Bill Wendling47054f32009-05-15 00:11:17 +0000452}
453
Davide Italianoc304a0d2015-11-24 02:21:43 +0000454LLVM_DUMP_METHOD
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000455void DIEInteger::print(raw_ostream &O) const {
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000456 O << "Int: " << (int64_t)Integer << " 0x";
457 O.write_hex(Integer);
Bill Wendling47054f32009-05-15 00:11:17 +0000458}
Bill Wendling47054f32009-05-15 00:11:17 +0000459
460//===----------------------------------------------------------------------===//
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000461// DIEExpr Implementation
462//===----------------------------------------------------------------------===//
463
464/// EmitValue - Emit expression value.
465///
Fangrui Song77497102020-02-14 22:40:47 -0800466void DIEExpr::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {
Fangrui Song0bc77a02020-02-13 13:26:21 -0800467 AP->emitDebugValue(Expr, SizeOf(AP, Form));
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000468}
469
470/// SizeOf - Determine size of expression value in bytes.
471///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000472unsigned DIEExpr::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
Igor Kudrinf76a0cd2020-07-17 12:46:47 +0700473 switch (Form) {
474 case dwarf::DW_FORM_data4:
475 return 4;
476 case dwarf::DW_FORM_data8:
477 return 8;
478 case dwarf::DW_FORM_sec_offset:
Igor Kudrina8058c62020-09-15 11:30:02 +0700479 return AP->getDwarfOffsetByteSize();
Igor Kudrinf76a0cd2020-07-17 12:46:47 +0700480 default:
481 llvm_unreachable("DIE Value form not supported yet");
482 }
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000483}
484
Davide Italianoc304a0d2015-11-24 02:21:43 +0000485LLVM_DUMP_METHOD
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000486void DIEExpr::print(raw_ostream &O) const { O << "Expr: " << *Expr; }
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000487
488//===----------------------------------------------------------------------===//
Chris Lattner8dcf41e2010-03-08 22:31:46 +0000489// DIELabel Implementation
Bill Wendling47054f32009-05-15 00:11:17 +0000490//===----------------------------------------------------------------------===//
491
492/// EmitValue - Emit label value.
493///
Fangrui Song77497102020-02-14 22:40:47 -0800494void DIELabel::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {
Igor Kudrinf98e03a2020-08-03 15:04:08 +0700495 bool IsSectionRelative = Form != dwarf::DW_FORM_addr;
496 AP->emitLabelReference(Label, SizeOf(AP, Form), IsSectionRelative);
Bill Wendling47054f32009-05-15 00:11:17 +0000497}
498
499/// SizeOf - Determine size of label value in bytes.
500///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000501unsigned DIELabel::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
Igor Kudrinf98e03a2020-08-03 15:04:08 +0700502 switch (Form) {
503 case dwarf::DW_FORM_data4:
504 return 4;
Igor Kudrin5dd1c592020-09-15 11:30:30 +0700505 case dwarf::DW_FORM_data8:
506 return 8;
Igor Kudrinf98e03a2020-08-03 15:04:08 +0700507 case dwarf::DW_FORM_sec_offset:
508 case dwarf::DW_FORM_strp:
Igor Kudrina8058c62020-09-15 11:30:02 +0700509 return AP->getDwarfOffsetByteSize();
Igor Kudrinf98e03a2020-08-03 15:04:08 +0700510 case dwarf::DW_FORM_addr:
511 return AP->MAI->getCodePointerSize();
512 default:
513 llvm_unreachable("DIE Value form not supported yet");
514 }
Bill Wendling47054f32009-05-15 00:11:17 +0000515}
516
Davide Italianoc304a0d2015-11-24 02:21:43 +0000517LLVM_DUMP_METHOD
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000518void DIELabel::print(raw_ostream &O) const { O << "Lbl: " << Label->getName(); }
Bill Wendling47054f32009-05-15 00:11:17 +0000519
520//===----------------------------------------------------------------------===//
Markus Lavinb86ce212019-03-19 13:16:28 +0000521// DIEBaseTypeRef Implementation
522//===----------------------------------------------------------------------===//
523
Fangrui Song77497102020-02-14 22:40:47 -0800524void DIEBaseTypeRef::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {
Markus Lavinb86ce212019-03-19 13:16:28 +0000525 uint64_t Offset = CU->ExprRefedBaseTypes[Index].Die->getOffset();
526 assert(Offset < (1ULL << (ULEB128PadSize * 7)) && "Offset wont fit");
Fangrui Song0bc77a02020-02-13 13:26:21 -0800527 AP->emitULEB128(Offset, nullptr, ULEB128PadSize);
Markus Lavinb86ce212019-03-19 13:16:28 +0000528}
529
530unsigned DIEBaseTypeRef::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
531 return ULEB128PadSize;
532}
533
534LLVM_DUMP_METHOD
535void DIEBaseTypeRef::print(raw_ostream &O) const { O << "BaseTypeRef: " << Index; }
536
537//===----------------------------------------------------------------------===//
Bill Wendling47054f32009-05-15 00:11:17 +0000538// DIEDelta Implementation
539//===----------------------------------------------------------------------===//
540
541/// EmitValue - Emit delta value.
542///
Fangrui Song77497102020-02-14 22:40:47 -0800543void DIEDelta::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {
Fangrui Song0bc77a02020-02-13 13:26:21 -0800544 AP->emitLabelDifference(LabelHi, LabelLo, SizeOf(AP, Form));
Bill Wendling47054f32009-05-15 00:11:17 +0000545}
546
547/// SizeOf - Determine size of delta value in bytes.
548///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000549unsigned DIEDelta::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
Igor Kudrin414b9be2020-08-03 15:04:15 +0700550 switch (Form) {
551 case dwarf::DW_FORM_data4:
552 return 4;
Igor Kudrincae7c1e2020-09-15 11:30:38 +0700553 case dwarf::DW_FORM_data8:
554 return 8;
Igor Kudrin414b9be2020-08-03 15:04:15 +0700555 case dwarf::DW_FORM_sec_offset:
Igor Kudrina8058c62020-09-15 11:30:02 +0700556 return AP->getDwarfOffsetByteSize();
Igor Kudrin414b9be2020-08-03 15:04:15 +0700557 default:
558 llvm_unreachable("DIE Value form not supported yet");
559 }
Bill Wendling47054f32009-05-15 00:11:17 +0000560}
561
Davide Italianoc304a0d2015-11-24 02:21:43 +0000562LLVM_DUMP_METHOD
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000563void DIEDelta::print(raw_ostream &O) const {
Chris Lattnerbc9210c2010-03-08 22:23:36 +0000564 O << "Del: " << LabelHi->getName() << "-" << LabelLo->getName();
Bill Wendling47054f32009-05-15 00:11:17 +0000565}
Bill Wendling47054f32009-05-15 00:11:17 +0000566
567//===----------------------------------------------------------------------===//
Eric Christopher67646432013-07-26 17:02:41 +0000568// DIEString Implementation
569//===----------------------------------------------------------------------===//
570
571/// EmitValue - Emit string value.
572///
Fangrui Song77497102020-02-14 22:40:47 -0800573void DIEString::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {
Duncan P. N. Exon Smithf73bcf42015-05-24 16:40:47 +0000574 // Index of string in symbol table.
Wolfgang Pieb456b5552018-01-26 18:52:58 +0000575 switch (Form) {
576 case dwarf::DW_FORM_GNU_str_index:
577 case dwarf::DW_FORM_strx:
578 case dwarf::DW_FORM_strx1:
579 case dwarf::DW_FORM_strx2:
580 case dwarf::DW_FORM_strx3:
581 case dwarf::DW_FORM_strx4:
Fangrui Song77497102020-02-14 22:40:47 -0800582 DIEInteger(S.getIndex()).emitValue(AP, Form);
Duncan P. N. Exon Smithf73bcf42015-05-24 16:40:47 +0000583 return;
Wolfgang Pieb456b5552018-01-26 18:52:58 +0000584 case dwarf::DW_FORM_strp:
585 if (AP->MAI->doesDwarfUseRelocationsAcrossSections())
Fangrui Song77497102020-02-14 22:40:47 -0800586 DIELabel(S.getSymbol()).emitValue(AP, Form);
Wolfgang Pieb456b5552018-01-26 18:52:58 +0000587 else
Fangrui Song77497102020-02-14 22:40:47 -0800588 DIEInteger(S.getOffset()).emitValue(AP, Form);
Duncan P. N. Exon Smithf73bcf42015-05-24 16:40:47 +0000589 return;
Wolfgang Pieb456b5552018-01-26 18:52:58 +0000590 default:
591 llvm_unreachable("Expected valid string form");
Duncan P. N. Exon Smithf73bcf42015-05-24 16:40:47 +0000592 }
Eric Christopher67646432013-07-26 17:02:41 +0000593}
594
595/// SizeOf - Determine size of delta value in bytes.
596///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000597unsigned DIEString::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
Duncan P. N. Exon Smithf73bcf42015-05-24 16:40:47 +0000598 // Index of string in symbol table.
Wolfgang Pieb456b5552018-01-26 18:52:58 +0000599 switch (Form) {
600 case dwarf::DW_FORM_GNU_str_index:
601 case dwarf::DW_FORM_strx:
602 case dwarf::DW_FORM_strx1:
603 case dwarf::DW_FORM_strx2:
604 case dwarf::DW_FORM_strx3:
605 case dwarf::DW_FORM_strx4:
Duncan P. N. Exon Smithf73bcf42015-05-24 16:40:47 +0000606 return DIEInteger(S.getIndex()).SizeOf(AP, Form);
Wolfgang Pieb456b5552018-01-26 18:52:58 +0000607 case dwarf::DW_FORM_strp:
608 if (AP->MAI->doesDwarfUseRelocationsAcrossSections())
609 return DIELabel(S.getSymbol()).SizeOf(AP, Form);
610 return DIEInteger(S.getOffset()).SizeOf(AP, Form);
611 default:
612 llvm_unreachable("Expected valid string form");
613 }
Eric Christopher67646432013-07-26 17:02:41 +0000614}
615
Davide Italianoc304a0d2015-11-24 02:21:43 +0000616LLVM_DUMP_METHOD
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000617void DIEString::print(raw_ostream &O) const {
Duncan P. N. Exon Smithf73bcf42015-05-24 16:40:47 +0000618 O << "String: " << S.getString();
Eric Christopher67646432013-07-26 17:02:41 +0000619}
Eric Christopher67646432013-07-26 17:02:41 +0000620
621//===----------------------------------------------------------------------===//
Greg Clayton3462a422016-12-08 01:03:48 +0000622// DIEInlineString Implementation
623//===----------------------------------------------------------------------===//
Fangrui Song77497102020-02-14 22:40:47 -0800624void DIEInlineString::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {
Greg Clayton3462a422016-12-08 01:03:48 +0000625 if (Form == dwarf::DW_FORM_string) {
Fangrui Songa55daa12020-02-14 18:16:24 -0800626 AP->OutStreamer->emitBytes(S);
Rafael Espindola4b4d85f2018-03-29 23:32:54 +0000627 AP->emitInt8(0);
Greg Clayton3462a422016-12-08 01:03:48 +0000628 return;
629 }
630 llvm_unreachable("Expected valid string form");
631}
632
633unsigned DIEInlineString::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
634 // Emit string bytes + NULL byte.
635 return S.size() + 1;
636}
637
638LLVM_DUMP_METHOD
639void DIEInlineString::print(raw_ostream &O) const {
Benjamin Kramereedc4052016-12-09 13:33:41 +0000640 O << "InlineString: " << S;
Greg Clayton3462a422016-12-08 01:03:48 +0000641}
642
643//===----------------------------------------------------------------------===//
Bill Wendling47054f32009-05-15 00:11:17 +0000644// DIEEntry Implementation
645//===----------------------------------------------------------------------===//
646
647/// EmitValue - Emit debug information entry offset.
648///
Fangrui Song77497102020-02-14 22:40:47 -0800649void DIEEntry::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {
Eric Christopherdd508382014-03-06 00:00:56 +0000650
Greg Clayton3462a422016-12-08 01:03:48 +0000651 switch (Form) {
652 case dwarf::DW_FORM_ref1:
653 case dwarf::DW_FORM_ref2:
654 case dwarf::DW_FORM_ref4:
655 case dwarf::DW_FORM_ref8:
Fangrui Song77497102020-02-14 22:40:47 -0800656 AP->OutStreamer->emitIntValue(Entry->getOffset(), SizeOf(AP, Form));
Greg Clayton3462a422016-12-08 01:03:48 +0000657 return;
658
659 case dwarf::DW_FORM_ref_udata:
Fangrui Song0bc77a02020-02-13 13:26:21 -0800660 AP->emitULEB128(Entry->getOffset());
Greg Clayton3462a422016-12-08 01:03:48 +0000661 return;
662
663 case dwarf::DW_FORM_ref_addr: {
Greg Clayton35630c32016-12-01 18:56:29 +0000664 // Get the absolute offset for this DIE within the debug info/types section.
Igor Kudrina845ebd2020-09-15 11:32:08 +0700665 uint64_t Addr = Entry->getDebugSectionOffset();
David Blaikie85366ac2017-04-22 07:53:44 +0000666 if (const MCSymbol *SectionSym =
667 Entry->getUnit()->getCrossSectionRelativeBaseAddress()) {
Fangrui Song0bc77a02020-02-13 13:26:21 -0800668 AP->emitLabelPlusOffset(SectionSym, Addr, SizeOf(AP, Form), true);
David Blaikie85366ac2017-04-22 07:53:44 +0000669 return;
Greg Clayton3462a422016-12-08 01:03:48 +0000670 }
David Blaikie85366ac2017-04-22 07:53:44 +0000671
Fangrui Song77497102020-02-14 22:40:47 -0800672 AP->OutStreamer->emitIntValue(Addr, SizeOf(AP, Form));
Greg Clayton3462a422016-12-08 01:03:48 +0000673 return;
674 }
675 default:
676 llvm_unreachable("Improper form for DIE reference");
677 }
Bill Wendling47054f32009-05-15 00:11:17 +0000678}
679
Greg Clayton3462a422016-12-08 01:03:48 +0000680unsigned DIEEntry::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
681 switch (Form) {
682 case dwarf::DW_FORM_ref1:
683 return 1;
684 case dwarf::DW_FORM_ref2:
685 return 2;
686 case dwarf::DW_FORM_ref4:
687 return 4;
688 case dwarf::DW_FORM_ref8:
689 return 8;
690 case dwarf::DW_FORM_ref_udata:
691 return getULEB128Size(Entry->getOffset());
692 case dwarf::DW_FORM_ref_addr:
693 if (AP->getDwarfVersion() == 2)
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +0000694 return AP->MAI->getCodePointerSize();
Greg Clayton3462a422016-12-08 01:03:48 +0000695 switch (AP->OutStreamer->getContext().getDwarfFormat()) {
696 case dwarf::DWARF32:
697 return 4;
698 case dwarf::DWARF64:
699 return 8;
700 }
701 llvm_unreachable("Invalid DWARF format");
702
703 default:
704 llvm_unreachable("Improper form for DIE reference");
705 }
Manman Renac8062b2013-07-02 23:40:10 +0000706}
707
Davide Italianoc304a0d2015-11-24 02:21:43 +0000708LLVM_DUMP_METHOD
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000709void DIEEntry::print(raw_ostream &O) const {
David Blaikie8dbcc3f2014-04-25 19:33:43 +0000710 O << format("Die: 0x%lx", (long)(intptr_t)&Entry);
Bill Wendling47054f32009-05-15 00:11:17 +0000711}
Bill Wendling47054f32009-05-15 00:11:17 +0000712
713//===----------------------------------------------------------------------===//
Eric Christopher4a741042014-02-16 08:46:55 +0000714// DIELoc Implementation
715//===----------------------------------------------------------------------===//
716
717/// ComputeSize - calculate the size of the location expression.
718///
Frederic Risscd044342015-03-04 02:30:08 +0000719unsigned DIELoc::ComputeSize(const AsmPrinter *AP) const {
Eric Christopher8bdab432014-02-27 18:36:10 +0000720 if (!Size) {
Duncan P. N. Exon Smith1ad5ebc2015-08-02 20:42:45 +0000721 for (const auto &V : values())
Duncan P. N. Exon Smith9dbb5012015-06-24 18:48:11 +0000722 Size += V.SizeOf(AP);
Eric Christopher8bdab432014-02-27 18:36:10 +0000723 }
Eric Christopher4a741042014-02-16 08:46:55 +0000724
Eric Christopher8bdab432014-02-27 18:36:10 +0000725 return Size;
Eric Christopher4a741042014-02-16 08:46:55 +0000726}
727
728/// EmitValue - Emit location data.
729///
Fangrui Song77497102020-02-14 22:40:47 -0800730void DIELoc::emitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
Eric Christopher4a741042014-02-16 08:46:55 +0000731 switch (Form) {
732 default: llvm_unreachable("Improper form for block");
Rafael Espindola4b4d85f2018-03-29 23:32:54 +0000733 case dwarf::DW_FORM_block1: Asm->emitInt8(Size); break;
734 case dwarf::DW_FORM_block2: Asm->emitInt16(Size); break;
735 case dwarf::DW_FORM_block4: Asm->emitInt32(Size); break;
Eric Christopher4a741042014-02-16 08:46:55 +0000736 case dwarf::DW_FORM_block:
737 case dwarf::DW_FORM_exprloc:
Fangrui Song0bc77a02020-02-13 13:26:21 -0800738 Asm->emitULEB128(Size);
739 break;
Eric Christopher4a741042014-02-16 08:46:55 +0000740 }
741
Duncan P. N. Exon Smith1ad5ebc2015-08-02 20:42:45 +0000742 for (const auto &V : values())
Fangrui Song77497102020-02-14 22:40:47 -0800743 V.emitValue(Asm);
Eric Christopher4a741042014-02-16 08:46:55 +0000744}
745
746/// SizeOf - Determine size of location data in bytes.
747///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000748unsigned DIELoc::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
Eric Christopher4a741042014-02-16 08:46:55 +0000749 switch (Form) {
750 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
751 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
752 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
753 case dwarf::DW_FORM_block:
754 case dwarf::DW_FORM_exprloc:
Logan Chien5b776b72014-02-22 14:00:39 +0000755 return Size + getULEB128Size(Size);
Eric Christopher4a741042014-02-16 08:46:55 +0000756 default: llvm_unreachable("Improper form for block");
757 }
758}
759
Davide Italianoc304a0d2015-11-24 02:21:43 +0000760LLVM_DUMP_METHOD
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000761void DIELoc::print(raw_ostream &O) const {
Duncan P. N. Exon Smithc5821142015-08-02 20:46:49 +0000762 printValues(O, *this, "ExprLoc", Size, 5);
Eric Christopher4a741042014-02-16 08:46:55 +0000763}
Eric Christopher4a741042014-02-16 08:46:55 +0000764
765//===----------------------------------------------------------------------===//
Bill Wendling47054f32009-05-15 00:11:17 +0000766// DIEBlock Implementation
767//===----------------------------------------------------------------------===//
768
769/// ComputeSize - calculate the size of the block.
770///
Frederic Risscd044342015-03-04 02:30:08 +0000771unsigned DIEBlock::ComputeSize(const AsmPrinter *AP) const {
Eric Christopher8bdab432014-02-27 18:36:10 +0000772 if (!Size) {
Duncan P. N. Exon Smith1ad5ebc2015-08-02 20:42:45 +0000773 for (const auto &V : values())
Duncan P. N. Exon Smith9dbb5012015-06-24 18:48:11 +0000774 Size += V.SizeOf(AP);
Eric Christopher8bdab432014-02-27 18:36:10 +0000775 }
Bill Wendling47054f32009-05-15 00:11:17 +0000776
Eric Christopher8bdab432014-02-27 18:36:10 +0000777 return Size;
Bill Wendling47054f32009-05-15 00:11:17 +0000778}
779
780/// EmitValue - Emit block data.
781///
Fangrui Song77497102020-02-14 22:40:47 -0800782void DIEBlock::emitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
Bill Wendling47054f32009-05-15 00:11:17 +0000783 switch (Form) {
Craig Topperee4dab52012-02-05 08:31:47 +0000784 default: llvm_unreachable("Improper form for block");
Rafael Espindola4b4d85f2018-03-29 23:32:54 +0000785 case dwarf::DW_FORM_block1: Asm->emitInt8(Size); break;
786 case dwarf::DW_FORM_block2: Asm->emitInt16(Size); break;
787 case dwarf::DW_FORM_block4: Asm->emitInt32(Size); break;
Fangrui Song0bc77a02020-02-13 13:26:21 -0800788 case dwarf::DW_FORM_block:
789 Asm->emitULEB128(Size);
790 break;
Jonas Devlieghere9e3e7a92018-04-02 10:40:43 +0000791 case dwarf::DW_FORM_string: break;
Paul Robinsona06f8dc2017-12-18 19:08:35 +0000792 case dwarf::DW_FORM_data16: break;
Bill Wendling47054f32009-05-15 00:11:17 +0000793 }
794
Duncan P. N. Exon Smith1ad5ebc2015-08-02 20:42:45 +0000795 for (const auto &V : values())
Fangrui Song77497102020-02-14 22:40:47 -0800796 V.emitValue(Asm);
Bill Wendling47054f32009-05-15 00:11:17 +0000797}
798
799/// SizeOf - Determine size of block data in bytes.
800///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000801unsigned DIEBlock::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
Bill Wendling47054f32009-05-15 00:11:17 +0000802 switch (Form) {
803 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
804 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
805 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
Logan Chien5b776b72014-02-22 14:00:39 +0000806 case dwarf::DW_FORM_block: return Size + getULEB128Size(Size);
Paul Robinsona06f8dc2017-12-18 19:08:35 +0000807 case dwarf::DW_FORM_data16: return 16;
David Blaikie46a9f012012-01-20 21:51:11 +0000808 default: llvm_unreachable("Improper form for block");
Bill Wendling47054f32009-05-15 00:11:17 +0000809 }
Bill Wendling47054f32009-05-15 00:11:17 +0000810}
811
Davide Italianoc304a0d2015-11-24 02:21:43 +0000812LLVM_DUMP_METHOD
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000813void DIEBlock::print(raw_ostream &O) const {
Duncan P. N. Exon Smithc5821142015-08-02 20:46:49 +0000814 printValues(O, *this, "Blk", Size, 5);
Bill Wendling47054f32009-05-15 00:11:17 +0000815}
Eric Christophera27220f2014-03-05 22:41:20 +0000816
817//===----------------------------------------------------------------------===//
818// DIELocList Implementation
819//===----------------------------------------------------------------------===//
820
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000821unsigned DIELocList::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
Igor Kudrin4e10a182020-08-03 15:03:37 +0700822 switch (Form) {
823 case dwarf::DW_FORM_loclistx:
David Blaikie77cfcd72019-11-12 14:15:37 -0800824 return getULEB128Size(Index);
Igor Kudrin4e10a182020-08-03 15:03:37 +0700825 case dwarf::DW_FORM_data4:
Igor Kudrina8058c62020-09-15 11:30:02 +0700826 assert(!AP->isDwarf64() &&
827 "DW_FORM_data4 is not suitable to emit a pointer to a location list "
828 "in the 64-bit DWARF format");
Eric Christophera27220f2014-03-05 22:41:20 +0000829 return 4;
Igor Kudrina8058c62020-09-15 11:30:02 +0700830 case dwarf::DW_FORM_data8:
831 assert(AP->isDwarf64() &&
832 "DW_FORM_data8 is not suitable to emit a pointer to a location list "
833 "in the 32-bit DWARF format");
834 return 8;
Igor Kudrin4e10a182020-08-03 15:03:37 +0700835 case dwarf::DW_FORM_sec_offset:
Igor Kudrina8058c62020-09-15 11:30:02 +0700836 return AP->getDwarfOffsetByteSize();
Igor Kudrin4e10a182020-08-03 15:03:37 +0700837 default:
838 llvm_unreachable("DIE Value form not supported yet");
839 }
Eric Christophera27220f2014-03-05 22:41:20 +0000840}
841
842/// EmitValue - Emit label value.
843///
Fangrui Song77497102020-02-14 22:40:47 -0800844void DIELocList::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {
David Blaikie77cfcd72019-11-12 14:15:37 -0800845 if (Form == dwarf::DW_FORM_loclistx) {
Fangrui Song0bc77a02020-02-13 13:26:21 -0800846 AP->emitULEB128(Index);
David Blaikie77cfcd72019-11-12 14:15:37 -0800847 return;
848 }
David Blaikie9c550ac2014-03-25 01:44:02 +0000849 DwarfDebug *DD = AP->getDwarfDebug();
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +0000850 MCSymbol *Label = DD->getDebugLocs().getList(Index).Label;
Rafael Espindola857546e2015-06-16 23:22:02 +0000851 AP->emitDwarfSymbolReference(Label, /*ForceOffset*/ DD->useSplitDwarf());
Eric Christophera27220f2014-03-05 22:41:20 +0000852}
853
Davide Italianoc304a0d2015-11-24 02:21:43 +0000854LLVM_DUMP_METHOD
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000855void DIELocList::print(raw_ostream &O) const { O << "LocList: " << Index; }