blob: 15bbc15021afbb73d48e0fb8c8154e02ad1e7eda [file] [log] [blame]
Bill Wendling47054f32009-05-15 00:11:17 +00001//===--- lib/CodeGen/DIE.cpp - DWARF Info Entries -------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Data structures for DWARF info entries.
Eric Christopherb800ff72013-01-07 22:40:45 +000011//
Bill Wendling47054f32009-05-15 00:11:17 +000012//===----------------------------------------------------------------------===//
13
Frederic Risse541e0b2015-01-05 21:29:41 +000014#include "llvm/CodeGen/DIE.h"
David Blaikie37c52312014-10-04 15:49:50 +000015#include "DwarfCompileUnit.h"
Manman Renac8062b2013-07-02 23:40:10 +000016#include "DwarfDebug.h"
David Blaikie47f615e2013-12-17 23:32:35 +000017#include "DwarfUnit.h"
Benjamin Kramer4d128a22010-01-17 07:46:39 +000018#include "llvm/ADT/Twine.h"
Bill Wendling47054f32009-05-15 00:11:17 +000019#include "llvm/CodeGen/AsmPrinter.h"
Nico Weber432a3882018-04-30 14:59:11 +000020#include "llvm/Config/llvm-config.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/DataLayout.h"
Chris Lattner7b26fce2009-08-22 20:48:53 +000022#include "llvm/MC/MCAsmInfo.h"
Rafael Espindola74dd8542014-10-21 00:25:49 +000023#include "llvm/MC/MCContext.h"
Chris Lattner71601e82010-01-20 07:41:15 +000024#include "llvm/MC/MCStreamer.h"
Chris Lattner06d45f62010-01-16 18:50:28 +000025#include "llvm/MC/MCSymbol.h"
David Greene8bc072c2009-12-24 00:27:55 +000026#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000027#include "llvm/Support/ErrorHandling.h"
Chris Lattner74725712009-08-23 01:01:17 +000028#include "llvm/Support/Format.h"
Chris Lattnerf5c834f2010-01-22 22:09:00 +000029#include "llvm/Support/FormattedStream.h"
Logan Chien5b776b72014-02-22 14:00:39 +000030#include "llvm/Support/LEB128.h"
Eric Christopher67646432013-07-26 17:02:41 +000031#include "llvm/Support/MD5.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000032#include "llvm/Support/raw_ostream.h"
Bill Wendling47054f32009-05-15 00:11:17 +000033using namespace llvm;
34
Paul Robinson70b34532017-04-20 19:16:51 +000035#define DEBUG_TYPE "dwarfdebug"
36
Bill Wendling47054f32009-05-15 00:11:17 +000037//===----------------------------------------------------------------------===//
38// DIEAbbrevData Implementation
39//===----------------------------------------------------------------------===//
40
41/// Profile - Used to gather unique data for the abbreviation folding set.
42///
43void DIEAbbrevData::Profile(FoldingSetNodeID &ID) const {
Reid Klecknerad65f102013-10-21 19:18:31 +000044 // Explicitly cast to an integer type for which FoldingSetNodeID has
45 // overloads. Otherwise MSVC 2010 thinks this call is ambiguous.
46 ID.AddInteger(unsigned(Attribute));
47 ID.AddInteger(unsigned(Form));
Victor Leschukd7bfa402017-03-01 22:13:42 +000048 if (Form == dwarf::DW_FORM_implicit_const)
49 ID.AddInteger(Value);
Bill Wendling47054f32009-05-15 00:11:17 +000050}
51
52//===----------------------------------------------------------------------===//
53// DIEAbbrev Implementation
54//===----------------------------------------------------------------------===//
55
56/// Profile - Used to gather unique data for the abbreviation folding set.
57///
58void DIEAbbrev::Profile(FoldingSetNodeID &ID) const {
Reid Klecknerad65f102013-10-21 19:18:31 +000059 ID.AddInteger(unsigned(Tag));
Eric Christophere8f10722014-03-05 01:44:58 +000060 ID.AddInteger(unsigned(Children));
Bill Wendling47054f32009-05-15 00:11:17 +000061
62 // For each attribute description.
63 for (unsigned i = 0, N = Data.size(); i < N; ++i)
64 Data[i].Profile(ID);
65}
66
67/// Emit - Print the abbreviation using the specified asm printer.
68///
Frederic Risscd044342015-03-04 02:30:08 +000069void DIEAbbrev::Emit(const AsmPrinter *AP) const {
Bill Wendling47054f32009-05-15 00:11:17 +000070 // Emit its Dwarf tag type.
Mehdi Amini149f6ea2016-10-05 05:59:29 +000071 AP->EmitULEB128(Tag, dwarf::TagString(Tag).data());
Bill Wendling47054f32009-05-15 00:11:17 +000072
73 // Emit whether it has children DIEs.
Mehdi Amini149f6ea2016-10-05 05:59:29 +000074 AP->EmitULEB128((unsigned)Children, dwarf::ChildrenString(Children).data());
Bill Wendling47054f32009-05-15 00:11:17 +000075
76 // For each attribute description.
77 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
78 const DIEAbbrevData &AttrData = Data[i];
79
80 // Emit attribute type.
Chris Lattner3a383cb2010-04-05 00:13:49 +000081 AP->EmitULEB128(AttrData.getAttribute(),
Mehdi Amini149f6ea2016-10-05 05:59:29 +000082 dwarf::AttributeString(AttrData.getAttribute()).data());
Bill Wendling47054f32009-05-15 00:11:17 +000083
84 // Emit form type.
Paul Robinson70b34532017-04-20 19:16:51 +000085#ifndef NDEBUG
86 // Could be an assertion, but this way we can see the failing form code
87 // easily, which helps track down where it came from.
88 if (!dwarf::isValidFormForVersion(AttrData.getForm(),
89 AP->getDwarfVersion())) {
90 DEBUG(dbgs() << "Invalid form " << format("0x%x", AttrData.getForm())
91 << " for DWARF version " << AP->getDwarfVersion() << "\n");
92 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:
Greg Clayton3462a422016-12-08 01:03:48 +0000416 case dwarf::DW_FORM_udata:
417 Asm->EmitULEB128(Integer);
418 return;
419 case dwarf::DW_FORM_sdata:
420 Asm->EmitSLEB128(Integer);
421 return;
Torok Edwinfbcc6632009-07-14 16:55:14 +0000422 default: llvm_unreachable("DIE Value form not supported yet");
Bill Wendling47054f32009-05-15 00:11:17 +0000423 }
424}
425
426/// SizeOf - Determine size of integer value in bytes.
427///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000428unsigned DIEInteger::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
Pavel Labath8ed6582b2018-03-14 11:31:17 +0000429 dwarf::FormParams Params = {0, 0, dwarf::DWARF32};
Pavel Labath322711f2018-03-14 09:39:54 +0000430 if (AP)
431 Params = {AP->getDwarfVersion(), uint8_t(AP->getPointerSize()),
432 AP->OutStreamer->getContext().getDwarfFormat()};
433
434 if (Optional<uint8_t> FixedSize = dwarf::getFixedFormByteSize(Form, Params))
435 return *FixedSize;
436
Bill Wendling47054f32009-05-15 00:11:17 +0000437 switch (Form) {
Greg Clayton3462a422016-12-08 01:03:48 +0000438 case dwarf::DW_FORM_GNU_str_index:
Greg Clayton3462a422016-12-08 01:03:48 +0000439 case dwarf::DW_FORM_GNU_addr_index:
Greg Clayton3462a422016-12-08 01:03:48 +0000440 case dwarf::DW_FORM_ref_udata:
Wolfgang Pieb456b5552018-01-26 18:52:58 +0000441 case dwarf::DW_FORM_strx:
Greg Clayton3462a422016-12-08 01:03:48 +0000442 case dwarf::DW_FORM_udata:
443 return getULEB128Size(Integer);
444 case dwarf::DW_FORM_sdata:
445 return getSLEB128Size(Integer);
David Blaikie46a9f012012-01-20 21:51:11 +0000446 default: llvm_unreachable("DIE Value form not supported yet");
Bill Wendling47054f32009-05-15 00:11:17 +0000447 }
Bill Wendling47054f32009-05-15 00:11:17 +0000448}
449
Davide Italianoc304a0d2015-11-24 02:21:43 +0000450LLVM_DUMP_METHOD
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000451void DIEInteger::print(raw_ostream &O) const {
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000452 O << "Int: " << (int64_t)Integer << " 0x";
453 O.write_hex(Integer);
Bill Wendling47054f32009-05-15 00:11:17 +0000454}
Bill Wendling47054f32009-05-15 00:11:17 +0000455
456//===----------------------------------------------------------------------===//
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000457// DIEExpr Implementation
458//===----------------------------------------------------------------------===//
459
460/// EmitValue - Emit expression value.
461///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000462void DIEExpr::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
Simon Dardis2e8cdbd2017-02-08 19:03:46 +0000463 AP->EmitDebugThreadLocal(Expr, SizeOf(AP, Form));
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000464}
465
466/// SizeOf - Determine size of expression value in bytes.
467///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000468unsigned DIEExpr::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000469 if (Form == dwarf::DW_FORM_data4) return 4;
470 if (Form == dwarf::DW_FORM_sec_offset) return 4;
471 if (Form == dwarf::DW_FORM_strp) return 4;
Mehdi Amini1660cab2015-07-16 05:59:25 +0000472 return AP->getPointerSize();
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000473}
474
Davide Italianoc304a0d2015-11-24 02:21:43 +0000475LLVM_DUMP_METHOD
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000476void DIEExpr::print(raw_ostream &O) const { O << "Expr: " << *Expr; }
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000477
478//===----------------------------------------------------------------------===//
Chris Lattner8dcf41e2010-03-08 22:31:46 +0000479// DIELabel Implementation
Bill Wendling47054f32009-05-15 00:11:17 +0000480//===----------------------------------------------------------------------===//
481
482/// EmitValue - Emit label value.
483///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000484void DIELabel::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
David Blaikief2443192013-10-21 17:28:37 +0000485 AP->EmitLabelReference(Label, SizeOf(AP, Form),
486 Form == dwarf::DW_FORM_strp ||
487 Form == dwarf::DW_FORM_sec_offset ||
Keno Fischerf7d84ee2017-01-02 03:00:19 +0000488 Form == dwarf::DW_FORM_ref_addr ||
489 Form == dwarf::DW_FORM_data4);
Bill Wendling47054f32009-05-15 00:11:17 +0000490}
491
492/// SizeOf - Determine size of label value in bytes.
493///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000494unsigned DIELabel::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
Bill Wendling47054f32009-05-15 00:11:17 +0000495 if (Form == dwarf::DW_FORM_data4) return 4;
Eric Christopher4c7765f2013-01-17 03:00:04 +0000496 if (Form == dwarf::DW_FORM_sec_offset) return 4;
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000497 if (Form == dwarf::DW_FORM_strp) return 4;
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +0000498 return AP->MAI->getCodePointerSize();
Bill Wendling47054f32009-05-15 00:11:17 +0000499}
500
Davide Italianoc304a0d2015-11-24 02:21:43 +0000501LLVM_DUMP_METHOD
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000502void DIELabel::print(raw_ostream &O) const { O << "Lbl: " << Label->getName(); }
Bill Wendling47054f32009-05-15 00:11:17 +0000503
504//===----------------------------------------------------------------------===//
Bill Wendling47054f32009-05-15 00:11:17 +0000505// DIEDelta Implementation
506//===----------------------------------------------------------------------===//
507
508/// EmitValue - Emit delta value.
509///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000510void DIEDelta::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
Chris Lattner5a00dea2010-04-05 00:18:22 +0000511 AP->EmitLabelDifference(LabelHi, LabelLo, SizeOf(AP, Form));
Bill Wendling47054f32009-05-15 00:11:17 +0000512}
513
514/// SizeOf - Determine size of delta value in bytes.
515///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000516unsigned DIEDelta::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
Bill Wendling47054f32009-05-15 00:11:17 +0000517 if (Form == dwarf::DW_FORM_data4) return 4;
Eric Christopher33ff6972013-11-21 23:46:41 +0000518 if (Form == dwarf::DW_FORM_sec_offset) return 4;
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000519 if (Form == dwarf::DW_FORM_strp) return 4;
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +0000520 return AP->MAI->getCodePointerSize();
Bill Wendling47054f32009-05-15 00:11:17 +0000521}
522
Davide Italianoc304a0d2015-11-24 02:21:43 +0000523LLVM_DUMP_METHOD
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000524void DIEDelta::print(raw_ostream &O) const {
Chris Lattnerbc9210c2010-03-08 22:23:36 +0000525 O << "Del: " << LabelHi->getName() << "-" << LabelLo->getName();
Bill Wendling47054f32009-05-15 00:11:17 +0000526}
Bill Wendling47054f32009-05-15 00:11:17 +0000527
528//===----------------------------------------------------------------------===//
Eric Christopher67646432013-07-26 17:02:41 +0000529// DIEString Implementation
530//===----------------------------------------------------------------------===//
531
532/// EmitValue - Emit string value.
533///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000534void DIEString::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
Duncan P. N. Exon Smithf73bcf42015-05-24 16:40:47 +0000535 // Index of string in symbol table.
Wolfgang Pieb456b5552018-01-26 18:52:58 +0000536 switch (Form) {
537 case dwarf::DW_FORM_GNU_str_index:
538 case dwarf::DW_FORM_strx:
539 case dwarf::DW_FORM_strx1:
540 case dwarf::DW_FORM_strx2:
541 case dwarf::DW_FORM_strx3:
542 case dwarf::DW_FORM_strx4:
Duncan P. N. Exon Smithf73bcf42015-05-24 16:40:47 +0000543 DIEInteger(S.getIndex()).EmitValue(AP, Form);
544 return;
Wolfgang Pieb456b5552018-01-26 18:52:58 +0000545 case dwarf::DW_FORM_strp:
546 if (AP->MAI->doesDwarfUseRelocationsAcrossSections())
547 DIELabel(S.getSymbol()).EmitValue(AP, Form);
548 else
549 DIEInteger(S.getOffset()).EmitValue(AP, Form);
Duncan P. N. Exon Smithf73bcf42015-05-24 16:40:47 +0000550 return;
Wolfgang Pieb456b5552018-01-26 18:52:58 +0000551 default:
552 llvm_unreachable("Expected valid string form");
Duncan P. N. Exon Smithf73bcf42015-05-24 16:40:47 +0000553 }
Eric Christopher67646432013-07-26 17:02:41 +0000554}
555
556/// SizeOf - Determine size of delta value in bytes.
557///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000558unsigned DIEString::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
Duncan P. N. Exon Smithf73bcf42015-05-24 16:40:47 +0000559 // Index of string in symbol table.
Wolfgang Pieb456b5552018-01-26 18:52:58 +0000560 switch (Form) {
561 case dwarf::DW_FORM_GNU_str_index:
562 case dwarf::DW_FORM_strx:
563 case dwarf::DW_FORM_strx1:
564 case dwarf::DW_FORM_strx2:
565 case dwarf::DW_FORM_strx3:
566 case dwarf::DW_FORM_strx4:
Duncan P. N. Exon Smithf73bcf42015-05-24 16:40:47 +0000567 return DIEInteger(S.getIndex()).SizeOf(AP, Form);
Wolfgang Pieb456b5552018-01-26 18:52:58 +0000568 case dwarf::DW_FORM_strp:
569 if (AP->MAI->doesDwarfUseRelocationsAcrossSections())
570 return DIELabel(S.getSymbol()).SizeOf(AP, Form);
571 return DIEInteger(S.getOffset()).SizeOf(AP, Form);
572 default:
573 llvm_unreachable("Expected valid string form");
574 }
Eric Christopher67646432013-07-26 17:02:41 +0000575}
576
Davide Italianoc304a0d2015-11-24 02:21:43 +0000577LLVM_DUMP_METHOD
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000578void DIEString::print(raw_ostream &O) const {
Duncan P. N. Exon Smithf73bcf42015-05-24 16:40:47 +0000579 O << "String: " << S.getString();
Eric Christopher67646432013-07-26 17:02:41 +0000580}
Eric Christopher67646432013-07-26 17:02:41 +0000581
582//===----------------------------------------------------------------------===//
Greg Clayton3462a422016-12-08 01:03:48 +0000583// DIEInlineString Implementation
584//===----------------------------------------------------------------------===//
585void DIEInlineString::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
586 if (Form == dwarf::DW_FORM_string) {
587 for (char ch : S)
Rafael Espindola4b4d85f2018-03-29 23:32:54 +0000588 AP->emitInt8(ch);
589 AP->emitInt8(0);
Greg Clayton3462a422016-12-08 01:03:48 +0000590 return;
591 }
592 llvm_unreachable("Expected valid string form");
593}
594
595unsigned DIEInlineString::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
596 // Emit string bytes + NULL byte.
597 return S.size() + 1;
598}
599
600LLVM_DUMP_METHOD
601void DIEInlineString::print(raw_ostream &O) const {
Benjamin Kramereedc4052016-12-09 13:33:41 +0000602 O << "InlineString: " << S;
Greg Clayton3462a422016-12-08 01:03:48 +0000603}
604
605//===----------------------------------------------------------------------===//
Bill Wendling47054f32009-05-15 00:11:17 +0000606// DIEEntry Implementation
607//===----------------------------------------------------------------------===//
608
609/// EmitValue - Emit debug information entry offset.
610///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000611void DIEEntry::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
Eric Christopherdd508382014-03-06 00:00:56 +0000612
Greg Clayton3462a422016-12-08 01:03:48 +0000613 switch (Form) {
614 case dwarf::DW_FORM_ref1:
615 case dwarf::DW_FORM_ref2:
616 case dwarf::DW_FORM_ref4:
617 case dwarf::DW_FORM_ref8:
618 AP->OutStreamer->EmitIntValue(Entry->getOffset(), SizeOf(AP, Form));
619 return;
620
621 case dwarf::DW_FORM_ref_udata:
622 AP->EmitULEB128(Entry->getOffset());
623 return;
624
625 case dwarf::DW_FORM_ref_addr: {
Greg Clayton35630c32016-12-01 18:56:29 +0000626 // Get the absolute offset for this DIE within the debug info/types section.
627 unsigned Addr = Entry->getDebugSectionOffset();
David Blaikie85366ac2017-04-22 07:53:44 +0000628 if (const MCSymbol *SectionSym =
629 Entry->getUnit()->getCrossSectionRelativeBaseAddress()) {
630 AP->EmitLabelPlusOffset(SectionSym, Addr, SizeOf(AP, Form), true);
631 return;
Greg Clayton3462a422016-12-08 01:03:48 +0000632 }
David Blaikie85366ac2017-04-22 07:53:44 +0000633
Greg Clayton3462a422016-12-08 01:03:48 +0000634 AP->OutStreamer->EmitIntValue(Addr, SizeOf(AP, Form));
635 return;
636 }
637 default:
638 llvm_unreachable("Improper form for DIE reference");
639 }
Bill Wendling47054f32009-05-15 00:11:17 +0000640}
641
Greg Clayton3462a422016-12-08 01:03:48 +0000642unsigned DIEEntry::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
643 switch (Form) {
644 case dwarf::DW_FORM_ref1:
645 return 1;
646 case dwarf::DW_FORM_ref2:
647 return 2;
648 case dwarf::DW_FORM_ref4:
649 return 4;
650 case dwarf::DW_FORM_ref8:
651 return 8;
652 case dwarf::DW_FORM_ref_udata:
653 return getULEB128Size(Entry->getOffset());
654 case dwarf::DW_FORM_ref_addr:
655 if (AP->getDwarfVersion() == 2)
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +0000656 return AP->MAI->getCodePointerSize();
Greg Clayton3462a422016-12-08 01:03:48 +0000657 switch (AP->OutStreamer->getContext().getDwarfFormat()) {
658 case dwarf::DWARF32:
659 return 4;
660 case dwarf::DWARF64:
661 return 8;
662 }
663 llvm_unreachable("Invalid DWARF format");
664
665 default:
666 llvm_unreachable("Improper form for DIE reference");
667 }
Manman Renac8062b2013-07-02 23:40:10 +0000668}
669
Davide Italianoc304a0d2015-11-24 02:21:43 +0000670LLVM_DUMP_METHOD
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000671void DIEEntry::print(raw_ostream &O) const {
David Blaikie8dbcc3f2014-04-25 19:33:43 +0000672 O << format("Die: 0x%lx", (long)(intptr_t)&Entry);
Bill Wendling47054f32009-05-15 00:11:17 +0000673}
Bill Wendling47054f32009-05-15 00:11:17 +0000674
675//===----------------------------------------------------------------------===//
Eric Christopher4a741042014-02-16 08:46:55 +0000676// DIELoc Implementation
677//===----------------------------------------------------------------------===//
678
679/// ComputeSize - calculate the size of the location expression.
680///
Frederic Risscd044342015-03-04 02:30:08 +0000681unsigned DIELoc::ComputeSize(const AsmPrinter *AP) const {
Eric Christopher8bdab432014-02-27 18:36:10 +0000682 if (!Size) {
Duncan P. N. Exon Smith1ad5ebc2015-08-02 20:42:45 +0000683 for (const auto &V : values())
Duncan P. N. Exon Smith9dbb5012015-06-24 18:48:11 +0000684 Size += V.SizeOf(AP);
Eric Christopher8bdab432014-02-27 18:36:10 +0000685 }
Eric Christopher4a741042014-02-16 08:46:55 +0000686
Eric Christopher8bdab432014-02-27 18:36:10 +0000687 return Size;
Eric Christopher4a741042014-02-16 08:46:55 +0000688}
689
690/// EmitValue - Emit location data.
691///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000692void DIELoc::EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
Eric Christopher4a741042014-02-16 08:46:55 +0000693 switch (Form) {
694 default: llvm_unreachable("Improper form for block");
Rafael Espindola4b4d85f2018-03-29 23:32:54 +0000695 case dwarf::DW_FORM_block1: Asm->emitInt8(Size); break;
696 case dwarf::DW_FORM_block2: Asm->emitInt16(Size); break;
697 case dwarf::DW_FORM_block4: Asm->emitInt32(Size); break;
Eric Christopher4a741042014-02-16 08:46:55 +0000698 case dwarf::DW_FORM_block:
699 case dwarf::DW_FORM_exprloc:
700 Asm->EmitULEB128(Size); break;
701 }
702
Duncan P. N. Exon Smith1ad5ebc2015-08-02 20:42:45 +0000703 for (const auto &V : values())
Duncan P. N. Exon Smith9dbb5012015-06-24 18:48:11 +0000704 V.EmitValue(Asm);
Eric Christopher4a741042014-02-16 08:46:55 +0000705}
706
707/// SizeOf - Determine size of location data in bytes.
708///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000709unsigned DIELoc::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
Eric Christopher4a741042014-02-16 08:46:55 +0000710 switch (Form) {
711 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
712 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
713 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
714 case dwarf::DW_FORM_block:
715 case dwarf::DW_FORM_exprloc:
Logan Chien5b776b72014-02-22 14:00:39 +0000716 return Size + getULEB128Size(Size);
Eric Christopher4a741042014-02-16 08:46:55 +0000717 default: llvm_unreachable("Improper form for block");
718 }
719}
720
Davide Italianoc304a0d2015-11-24 02:21:43 +0000721LLVM_DUMP_METHOD
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000722void DIELoc::print(raw_ostream &O) const {
Duncan P. N. Exon Smithc5821142015-08-02 20:46:49 +0000723 printValues(O, *this, "ExprLoc", Size, 5);
Eric Christopher4a741042014-02-16 08:46:55 +0000724}
Eric Christopher4a741042014-02-16 08:46:55 +0000725
726//===----------------------------------------------------------------------===//
Bill Wendling47054f32009-05-15 00:11:17 +0000727// DIEBlock Implementation
728//===----------------------------------------------------------------------===//
729
730/// ComputeSize - calculate the size of the block.
731///
Frederic Risscd044342015-03-04 02:30:08 +0000732unsigned DIEBlock::ComputeSize(const AsmPrinter *AP) const {
Eric Christopher8bdab432014-02-27 18:36:10 +0000733 if (!Size) {
Duncan P. N. Exon Smith1ad5ebc2015-08-02 20:42:45 +0000734 for (const auto &V : values())
Duncan P. N. Exon Smith9dbb5012015-06-24 18:48:11 +0000735 Size += V.SizeOf(AP);
Eric Christopher8bdab432014-02-27 18:36:10 +0000736 }
Bill Wendling47054f32009-05-15 00:11:17 +0000737
Eric Christopher8bdab432014-02-27 18:36:10 +0000738 return Size;
Bill Wendling47054f32009-05-15 00:11:17 +0000739}
740
741/// EmitValue - Emit block data.
742///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000743void DIEBlock::EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
Bill Wendling47054f32009-05-15 00:11:17 +0000744 switch (Form) {
Craig Topperee4dab52012-02-05 08:31:47 +0000745 default: llvm_unreachable("Improper form for block");
Rafael Espindola4b4d85f2018-03-29 23:32:54 +0000746 case dwarf::DW_FORM_block1: Asm->emitInt8(Size); break;
747 case dwarf::DW_FORM_block2: Asm->emitInt16(Size); break;
748 case dwarf::DW_FORM_block4: Asm->emitInt32(Size); break;
Chris Lattner9efd1182010-04-04 19:09:29 +0000749 case dwarf::DW_FORM_block: Asm->EmitULEB128(Size); break;
Jonas Devlieghere9e3e7a92018-04-02 10:40:43 +0000750 case dwarf::DW_FORM_string: break;
Paul Robinsona06f8dc2017-12-18 19:08:35 +0000751 case dwarf::DW_FORM_data16: break;
Bill Wendling47054f32009-05-15 00:11:17 +0000752 }
753
Duncan P. N. Exon Smith1ad5ebc2015-08-02 20:42:45 +0000754 for (const auto &V : values())
Duncan P. N. Exon Smith9dbb5012015-06-24 18:48:11 +0000755 V.EmitValue(Asm);
Bill Wendling47054f32009-05-15 00:11:17 +0000756}
757
758/// SizeOf - Determine size of block data in bytes.
759///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000760unsigned DIEBlock::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
Bill Wendling47054f32009-05-15 00:11:17 +0000761 switch (Form) {
762 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
763 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
764 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
Logan Chien5b776b72014-02-22 14:00:39 +0000765 case dwarf::DW_FORM_block: return Size + getULEB128Size(Size);
Paul Robinsona06f8dc2017-12-18 19:08:35 +0000766 case dwarf::DW_FORM_data16: return 16;
David Blaikie46a9f012012-01-20 21:51:11 +0000767 default: llvm_unreachable("Improper form for block");
Bill Wendling47054f32009-05-15 00:11:17 +0000768 }
Bill Wendling47054f32009-05-15 00:11:17 +0000769}
770
Davide Italianoc304a0d2015-11-24 02:21:43 +0000771LLVM_DUMP_METHOD
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000772void DIEBlock::print(raw_ostream &O) const {
Duncan P. N. Exon Smithc5821142015-08-02 20:46:49 +0000773 printValues(O, *this, "Blk", Size, 5);
Bill Wendling47054f32009-05-15 00:11:17 +0000774}
Eric Christophera27220f2014-03-05 22:41:20 +0000775
776//===----------------------------------------------------------------------===//
777// DIELocList Implementation
778//===----------------------------------------------------------------------===//
779
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000780unsigned DIELocList::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
Eric Christophera27220f2014-03-05 22:41:20 +0000781 if (Form == dwarf::DW_FORM_data4)
782 return 4;
783 if (Form == dwarf::DW_FORM_sec_offset)
784 return 4;
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +0000785 return AP->MAI->getCodePointerSize();
Eric Christophera27220f2014-03-05 22:41:20 +0000786}
787
788/// EmitValue - Emit label value.
789///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000790void DIELocList::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
David Blaikie9c550ac2014-03-25 01:44:02 +0000791 DwarfDebug *DD = AP->getDwarfDebug();
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +0000792 MCSymbol *Label = DD->getDebugLocs().getList(Index).Label;
Rafael Espindola857546e2015-06-16 23:22:02 +0000793 AP->emitDwarfSymbolReference(Label, /*ForceOffset*/ DD->useSplitDwarf());
Eric Christophera27220f2014-03-05 22:41:20 +0000794}
795
Davide Italianoc304a0d2015-11-24 02:21:43 +0000796LLVM_DUMP_METHOD
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000797void DIELocList::print(raw_ostream &O) const { O << "LocList: " << Index; }