blob: a07b54773039b5a9fd35ff5310c3246d94fec7b1 [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.
Mehdi Amini149f6ea2016-10-05 05:59:29 +000070 AP->EmitULEB128(Tag, dwarf::TagString(Tag).data());
Bill Wendling47054f32009-05-15 00:11:17 +000071
72 // Emit whether it has children DIEs.
Mehdi Amini149f6ea2016-10-05 05:59:29 +000073 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.
Chris Lattner3a383cb2010-04-05 00:13:49 +000080 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
Chris Lattner3a383cb2010-04-05 00:13:49 +000095 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)
Victor Leschukcbddae72017-01-10 21:18:26 +0000100 AP->EmitSLEB128(AttrData.getValue());
Bill Wendling47054f32009-05-15 00:11:17 +0000101 }
102
103 // Mark end of abbreviation.
Chris Lattner3a383cb2010-04-05 00:13:49 +0000104 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
Greg Clayton35630c32016-12-01 18:56:29 +0000197unsigned DIE::getDebugSectionOffset() const {
198 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
Greg Clayton35630c32016-12-01 18:56:29 +0000214const DIEUnit *DIE::getUnit() const {
215 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//===----------------------------------------------------------------------===//
Greg Clayton35630c32016-12-01 18:56:29 +0000316DIEUnit::DIEUnit(uint16_t V, uint8_t A, dwarf::Tag UnitTag)
317 : Die(UnitTag), Section(nullptr), Offset(0), Length(0), Version(V),
318 AddrSize(A)
319{
David Blaikiee40caae2016-12-01 21:59:09 +0000320 Die.Owner = this;
Greg Clayton35630c32016-12-01 18:56:29 +0000321 assert((UnitTag == dwarf::DW_TAG_compile_unit ||
322 UnitTag == dwarf::DW_TAG_type_unit ||
323 UnitTag == dwarf::DW_TAG_partial_unit) && "expected a unit TAG");
324}
325
Duncan P. N. Exon Smith9dbb5012015-06-24 18:48:11 +0000326void 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: \
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000332 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///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000375void 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:
Greg Clayton3462a422016-12-08 01:03:48 +0000410 Asm->OutStreamer->EmitIntValue(Integer, SizeOf(Asm, Form));
411 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:
419 Asm->EmitULEB128(Integer);
420 return;
421 case dwarf::DW_FORM_sdata:
422 Asm->EmitSLEB128(Integer);
423 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 {
Pavel Labath8ed6582b2018-03-14 11:31:17 +0000431 dwarf::FormParams Params = {0, 0, dwarf::DWARF32};
Pavel Labath322711f2018-03-14 09:39:54 +0000432 if (AP)
433 Params = {AP->getDwarfVersion(), uint8_t(AP->getPointerSize()),
434 AP->OutStreamer->getContext().getDwarfFormat()};
435
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///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000466void DIEExpr::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
Simon Atanasyanaf860d42018-11-28 11:48:07 +0000467 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 {
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000473 if (Form == dwarf::DW_FORM_data4) return 4;
474 if (Form == dwarf::DW_FORM_sec_offset) return 4;
475 if (Form == dwarf::DW_FORM_strp) return 4;
Mehdi Amini1660cab2015-07-16 05:59:25 +0000476 return AP->getPointerSize();
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000477}
478
Davide Italianoc304a0d2015-11-24 02:21:43 +0000479LLVM_DUMP_METHOD
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000480void DIEExpr::print(raw_ostream &O) const { O << "Expr: " << *Expr; }
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000481
482//===----------------------------------------------------------------------===//
Chris Lattner8dcf41e2010-03-08 22:31:46 +0000483// DIELabel Implementation
Bill Wendling47054f32009-05-15 00:11:17 +0000484//===----------------------------------------------------------------------===//
485
486/// EmitValue - Emit label value.
487///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000488void DIELabel::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
David Blaikief2443192013-10-21 17:28:37 +0000489 AP->EmitLabelReference(Label, SizeOf(AP, Form),
490 Form == dwarf::DW_FORM_strp ||
491 Form == dwarf::DW_FORM_sec_offset ||
Keno Fischerf7d84ee2017-01-02 03:00:19 +0000492 Form == dwarf::DW_FORM_ref_addr ||
493 Form == dwarf::DW_FORM_data4);
Bill Wendling47054f32009-05-15 00:11:17 +0000494}
495
496/// SizeOf - Determine size of label value in bytes.
497///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000498unsigned DIELabel::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
Bill Wendling47054f32009-05-15 00:11:17 +0000499 if (Form == dwarf::DW_FORM_data4) return 4;
Eric Christopher4c7765f2013-01-17 03:00:04 +0000500 if (Form == dwarf::DW_FORM_sec_offset) return 4;
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000501 if (Form == dwarf::DW_FORM_strp) return 4;
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +0000502 return AP->MAI->getCodePointerSize();
Bill Wendling47054f32009-05-15 00:11:17 +0000503}
504
Davide Italianoc304a0d2015-11-24 02:21:43 +0000505LLVM_DUMP_METHOD
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000506void DIELabel::print(raw_ostream &O) const { O << "Lbl: " << Label->getName(); }
Bill Wendling47054f32009-05-15 00:11:17 +0000507
508//===----------------------------------------------------------------------===//
Bill Wendling47054f32009-05-15 00:11:17 +0000509// DIEDelta Implementation
510//===----------------------------------------------------------------------===//
511
512/// EmitValue - Emit delta value.
513///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000514void DIEDelta::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
Chris Lattner5a00dea2010-04-05 00:18:22 +0000515 AP->EmitLabelDifference(LabelHi, LabelLo, SizeOf(AP, Form));
Bill Wendling47054f32009-05-15 00:11:17 +0000516}
517
518/// SizeOf - Determine size of delta value in bytes.
519///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000520unsigned DIEDelta::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
Bill Wendling47054f32009-05-15 00:11:17 +0000521 if (Form == dwarf::DW_FORM_data4) return 4;
Eric Christopher33ff6972013-11-21 23:46:41 +0000522 if (Form == dwarf::DW_FORM_sec_offset) return 4;
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000523 if (Form == dwarf::DW_FORM_strp) return 4;
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +0000524 return AP->MAI->getCodePointerSize();
Bill Wendling47054f32009-05-15 00:11:17 +0000525}
526
Davide Italianoc304a0d2015-11-24 02:21:43 +0000527LLVM_DUMP_METHOD
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000528void DIEDelta::print(raw_ostream &O) const {
Chris Lattnerbc9210c2010-03-08 22:23:36 +0000529 O << "Del: " << LabelHi->getName() << "-" << LabelLo->getName();
Bill Wendling47054f32009-05-15 00:11:17 +0000530}
Bill Wendling47054f32009-05-15 00:11:17 +0000531
532//===----------------------------------------------------------------------===//
Eric Christopher67646432013-07-26 17:02:41 +0000533// DIEString Implementation
534//===----------------------------------------------------------------------===//
535
536/// EmitValue - Emit string value.
537///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000538void DIEString::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
Duncan P. N. Exon Smithf73bcf42015-05-24 16:40:47 +0000539 // Index of string in symbol table.
Wolfgang Pieb456b5552018-01-26 18:52:58 +0000540 switch (Form) {
541 case dwarf::DW_FORM_GNU_str_index:
542 case dwarf::DW_FORM_strx:
543 case dwarf::DW_FORM_strx1:
544 case dwarf::DW_FORM_strx2:
545 case dwarf::DW_FORM_strx3:
546 case dwarf::DW_FORM_strx4:
Duncan P. N. Exon Smithf73bcf42015-05-24 16:40:47 +0000547 DIEInteger(S.getIndex()).EmitValue(AP, Form);
548 return;
Wolfgang Pieb456b5552018-01-26 18:52:58 +0000549 case dwarf::DW_FORM_strp:
550 if (AP->MAI->doesDwarfUseRelocationsAcrossSections())
551 DIELabel(S.getSymbol()).EmitValue(AP, Form);
552 else
553 DIEInteger(S.getOffset()).EmitValue(AP, Form);
Duncan P. N. Exon Smithf73bcf42015-05-24 16:40:47 +0000554 return;
Wolfgang Pieb456b5552018-01-26 18:52:58 +0000555 default:
556 llvm_unreachable("Expected valid string form");
Duncan P. N. Exon Smithf73bcf42015-05-24 16:40:47 +0000557 }
Eric Christopher67646432013-07-26 17:02:41 +0000558}
559
560/// SizeOf - Determine size of delta value in bytes.
561///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000562unsigned DIEString::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
Duncan P. N. Exon Smithf73bcf42015-05-24 16:40:47 +0000563 // Index of string in symbol table.
Wolfgang Pieb456b5552018-01-26 18:52:58 +0000564 switch (Form) {
565 case dwarf::DW_FORM_GNU_str_index:
566 case dwarf::DW_FORM_strx:
567 case dwarf::DW_FORM_strx1:
568 case dwarf::DW_FORM_strx2:
569 case dwarf::DW_FORM_strx3:
570 case dwarf::DW_FORM_strx4:
Duncan P. N. Exon Smithf73bcf42015-05-24 16:40:47 +0000571 return DIEInteger(S.getIndex()).SizeOf(AP, Form);
Wolfgang Pieb456b5552018-01-26 18:52:58 +0000572 case dwarf::DW_FORM_strp:
573 if (AP->MAI->doesDwarfUseRelocationsAcrossSections())
574 return DIELabel(S.getSymbol()).SizeOf(AP, Form);
575 return DIEInteger(S.getOffset()).SizeOf(AP, Form);
576 default:
577 llvm_unreachable("Expected valid string form");
578 }
Eric Christopher67646432013-07-26 17:02:41 +0000579}
580
Davide Italianoc304a0d2015-11-24 02:21:43 +0000581LLVM_DUMP_METHOD
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000582void DIEString::print(raw_ostream &O) const {
Duncan P. N. Exon Smithf73bcf42015-05-24 16:40:47 +0000583 O << "String: " << S.getString();
Eric Christopher67646432013-07-26 17:02:41 +0000584}
Eric Christopher67646432013-07-26 17:02:41 +0000585
586//===----------------------------------------------------------------------===//
Greg Clayton3462a422016-12-08 01:03:48 +0000587// DIEInlineString Implementation
588//===----------------------------------------------------------------------===//
589void DIEInlineString::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
590 if (Form == dwarf::DW_FORM_string) {
Alexey Bataevc15c8532018-10-24 14:04:00 +0000591 AP->OutStreamer->EmitBytes(S);
Rafael Espindola4b4d85f2018-03-29 23:32:54 +0000592 AP->emitInt8(0);
Greg Clayton3462a422016-12-08 01:03:48 +0000593 return;
594 }
595 llvm_unreachable("Expected valid string form");
596}
597
598unsigned DIEInlineString::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
599 // Emit string bytes + NULL byte.
600 return S.size() + 1;
601}
602
603LLVM_DUMP_METHOD
604void DIEInlineString::print(raw_ostream &O) const {
Benjamin Kramereedc4052016-12-09 13:33:41 +0000605 O << "InlineString: " << S;
Greg Clayton3462a422016-12-08 01:03:48 +0000606}
607
608//===----------------------------------------------------------------------===//
Bill Wendling47054f32009-05-15 00:11:17 +0000609// DIEEntry Implementation
610//===----------------------------------------------------------------------===//
611
612/// EmitValue - Emit debug information entry offset.
613///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000614void DIEEntry::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
Eric Christopherdd508382014-03-06 00:00:56 +0000615
Greg Clayton3462a422016-12-08 01:03:48 +0000616 switch (Form) {
617 case dwarf::DW_FORM_ref1:
618 case dwarf::DW_FORM_ref2:
619 case dwarf::DW_FORM_ref4:
620 case dwarf::DW_FORM_ref8:
621 AP->OutStreamer->EmitIntValue(Entry->getOffset(), SizeOf(AP, Form));
622 return;
623
624 case dwarf::DW_FORM_ref_udata:
625 AP->EmitULEB128(Entry->getOffset());
626 return;
627
628 case dwarf::DW_FORM_ref_addr: {
Greg Clayton35630c32016-12-01 18:56:29 +0000629 // Get the absolute offset for this DIE within the debug info/types section.
630 unsigned Addr = Entry->getDebugSectionOffset();
David Blaikie85366ac2017-04-22 07:53:44 +0000631 if (const MCSymbol *SectionSym =
632 Entry->getUnit()->getCrossSectionRelativeBaseAddress()) {
633 AP->EmitLabelPlusOffset(SectionSym, Addr, SizeOf(AP, Form), true);
634 return;
Greg Clayton3462a422016-12-08 01:03:48 +0000635 }
David Blaikie85366ac2017-04-22 07:53:44 +0000636
Greg Clayton3462a422016-12-08 01:03:48 +0000637 AP->OutStreamer->EmitIntValue(Addr, SizeOf(AP, Form));
638 return;
639 }
640 default:
641 llvm_unreachable("Improper form for DIE reference");
642 }
Bill Wendling47054f32009-05-15 00:11:17 +0000643}
644
Greg Clayton3462a422016-12-08 01:03:48 +0000645unsigned DIEEntry::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
646 switch (Form) {
647 case dwarf::DW_FORM_ref1:
648 return 1;
649 case dwarf::DW_FORM_ref2:
650 return 2;
651 case dwarf::DW_FORM_ref4:
652 return 4;
653 case dwarf::DW_FORM_ref8:
654 return 8;
655 case dwarf::DW_FORM_ref_udata:
656 return getULEB128Size(Entry->getOffset());
657 case dwarf::DW_FORM_ref_addr:
658 if (AP->getDwarfVersion() == 2)
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +0000659 return AP->MAI->getCodePointerSize();
Greg Clayton3462a422016-12-08 01:03:48 +0000660 switch (AP->OutStreamer->getContext().getDwarfFormat()) {
661 case dwarf::DWARF32:
662 return 4;
663 case dwarf::DWARF64:
664 return 8;
665 }
666 llvm_unreachable("Invalid DWARF format");
667
668 default:
669 llvm_unreachable("Improper form for DIE reference");
670 }
Manman Renac8062b2013-07-02 23:40:10 +0000671}
672
Davide Italianoc304a0d2015-11-24 02:21:43 +0000673LLVM_DUMP_METHOD
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000674void DIEEntry::print(raw_ostream &O) const {
David Blaikie8dbcc3f2014-04-25 19:33:43 +0000675 O << format("Die: 0x%lx", (long)(intptr_t)&Entry);
Bill Wendling47054f32009-05-15 00:11:17 +0000676}
Bill Wendling47054f32009-05-15 00:11:17 +0000677
678//===----------------------------------------------------------------------===//
Eric Christopher4a741042014-02-16 08:46:55 +0000679// DIELoc Implementation
680//===----------------------------------------------------------------------===//
681
682/// ComputeSize - calculate the size of the location expression.
683///
Frederic Risscd044342015-03-04 02:30:08 +0000684unsigned DIELoc::ComputeSize(const AsmPrinter *AP) const {
Eric Christopher8bdab432014-02-27 18:36:10 +0000685 if (!Size) {
Duncan P. N. Exon Smith1ad5ebc2015-08-02 20:42:45 +0000686 for (const auto &V : values())
Duncan P. N. Exon Smith9dbb5012015-06-24 18:48:11 +0000687 Size += V.SizeOf(AP);
Eric Christopher8bdab432014-02-27 18:36:10 +0000688 }
Eric Christopher4a741042014-02-16 08:46:55 +0000689
Eric Christopher8bdab432014-02-27 18:36:10 +0000690 return Size;
Eric Christopher4a741042014-02-16 08:46:55 +0000691}
692
693/// EmitValue - Emit location data.
694///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000695void DIELoc::EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
Eric Christopher4a741042014-02-16 08:46:55 +0000696 switch (Form) {
697 default: llvm_unreachable("Improper form for block");
Rafael Espindola4b4d85f2018-03-29 23:32:54 +0000698 case dwarf::DW_FORM_block1: Asm->emitInt8(Size); break;
699 case dwarf::DW_FORM_block2: Asm->emitInt16(Size); break;
700 case dwarf::DW_FORM_block4: Asm->emitInt32(Size); break;
Eric Christopher4a741042014-02-16 08:46:55 +0000701 case dwarf::DW_FORM_block:
702 case dwarf::DW_FORM_exprloc:
703 Asm->EmitULEB128(Size); break;
704 }
705
Duncan P. N. Exon Smith1ad5ebc2015-08-02 20:42:45 +0000706 for (const auto &V : values())
Duncan P. N. Exon Smith9dbb5012015-06-24 18:48:11 +0000707 V.EmitValue(Asm);
Eric Christopher4a741042014-02-16 08:46:55 +0000708}
709
710/// SizeOf - Determine size of location data in bytes.
711///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000712unsigned DIELoc::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
Eric Christopher4a741042014-02-16 08:46:55 +0000713 switch (Form) {
714 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
715 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
716 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
717 case dwarf::DW_FORM_block:
718 case dwarf::DW_FORM_exprloc:
Logan Chien5b776b72014-02-22 14:00:39 +0000719 return Size + getULEB128Size(Size);
Eric Christopher4a741042014-02-16 08:46:55 +0000720 default: llvm_unreachable("Improper form for block");
721 }
722}
723
Davide Italianoc304a0d2015-11-24 02:21:43 +0000724LLVM_DUMP_METHOD
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000725void DIELoc::print(raw_ostream &O) const {
Duncan P. N. Exon Smithc5821142015-08-02 20:46:49 +0000726 printValues(O, *this, "ExprLoc", Size, 5);
Eric Christopher4a741042014-02-16 08:46:55 +0000727}
Eric Christopher4a741042014-02-16 08:46:55 +0000728
729//===----------------------------------------------------------------------===//
Bill Wendling47054f32009-05-15 00:11:17 +0000730// DIEBlock Implementation
731//===----------------------------------------------------------------------===//
732
733/// ComputeSize - calculate the size of the block.
734///
Frederic Risscd044342015-03-04 02:30:08 +0000735unsigned DIEBlock::ComputeSize(const AsmPrinter *AP) const {
Eric Christopher8bdab432014-02-27 18:36:10 +0000736 if (!Size) {
Duncan P. N. Exon Smith1ad5ebc2015-08-02 20:42:45 +0000737 for (const auto &V : values())
Duncan P. N. Exon Smith9dbb5012015-06-24 18:48:11 +0000738 Size += V.SizeOf(AP);
Eric Christopher8bdab432014-02-27 18:36:10 +0000739 }
Bill Wendling47054f32009-05-15 00:11:17 +0000740
Eric Christopher8bdab432014-02-27 18:36:10 +0000741 return Size;
Bill Wendling47054f32009-05-15 00:11:17 +0000742}
743
744/// EmitValue - Emit block data.
745///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000746void DIEBlock::EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
Bill Wendling47054f32009-05-15 00:11:17 +0000747 switch (Form) {
Craig Topperee4dab52012-02-05 08:31:47 +0000748 default: llvm_unreachable("Improper form for block");
Rafael Espindola4b4d85f2018-03-29 23:32:54 +0000749 case dwarf::DW_FORM_block1: Asm->emitInt8(Size); break;
750 case dwarf::DW_FORM_block2: Asm->emitInt16(Size); break;
751 case dwarf::DW_FORM_block4: Asm->emitInt32(Size); break;
Chris Lattner9efd1182010-04-04 19:09:29 +0000752 case dwarf::DW_FORM_block: Asm->EmitULEB128(Size); break;
Jonas Devlieghere9e3e7a92018-04-02 10:40:43 +0000753 case dwarf::DW_FORM_string: break;
Paul Robinsona06f8dc2017-12-18 19:08:35 +0000754 case dwarf::DW_FORM_data16: break;
Bill Wendling47054f32009-05-15 00:11:17 +0000755 }
756
Duncan P. N. Exon Smith1ad5ebc2015-08-02 20:42:45 +0000757 for (const auto &V : values())
Duncan P. N. Exon Smith9dbb5012015-06-24 18:48:11 +0000758 V.EmitValue(Asm);
Bill Wendling47054f32009-05-15 00:11:17 +0000759}
760
761/// SizeOf - Determine size of block data in bytes.
762///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000763unsigned DIEBlock::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
Bill Wendling47054f32009-05-15 00:11:17 +0000764 switch (Form) {
765 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
766 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
767 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
Logan Chien5b776b72014-02-22 14:00:39 +0000768 case dwarf::DW_FORM_block: return Size + getULEB128Size(Size);
Paul Robinsona06f8dc2017-12-18 19:08:35 +0000769 case dwarf::DW_FORM_data16: return 16;
David Blaikie46a9f012012-01-20 21:51:11 +0000770 default: llvm_unreachable("Improper form for block");
Bill Wendling47054f32009-05-15 00:11:17 +0000771 }
Bill Wendling47054f32009-05-15 00:11:17 +0000772}
773
Davide Italianoc304a0d2015-11-24 02:21:43 +0000774LLVM_DUMP_METHOD
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000775void DIEBlock::print(raw_ostream &O) const {
Duncan P. N. Exon Smithc5821142015-08-02 20:46:49 +0000776 printValues(O, *this, "Blk", Size, 5);
Bill Wendling47054f32009-05-15 00:11:17 +0000777}
Eric Christophera27220f2014-03-05 22:41:20 +0000778
779//===----------------------------------------------------------------------===//
780// DIELocList Implementation
781//===----------------------------------------------------------------------===//
782
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000783unsigned DIELocList::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
Eric Christophera27220f2014-03-05 22:41:20 +0000784 if (Form == dwarf::DW_FORM_data4)
785 return 4;
786 if (Form == dwarf::DW_FORM_sec_offset)
787 return 4;
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +0000788 return AP->MAI->getCodePointerSize();
Eric Christophera27220f2014-03-05 22:41:20 +0000789}
790
791/// EmitValue - Emit label value.
792///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000793void DIELocList::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
David Blaikie9c550ac2014-03-25 01:44:02 +0000794 DwarfDebug *DD = AP->getDwarfDebug();
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +0000795 MCSymbol *Label = DD->getDebugLocs().getList(Index).Label;
Rafael Espindola857546e2015-06-16 23:22:02 +0000796 AP->emitDwarfSymbolReference(Label, /*ForceOffset*/ DD->useSplitDwarf());
Eric Christophera27220f2014-03-05 22:41:20 +0000797}
798
Davide Italianoc304a0d2015-11-24 02:21:43 +0000799LLVM_DUMP_METHOD
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000800void DIELocList::print(raw_ostream &O) const { O << "LocList: " << Index; }