• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===--- 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.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/CodeGen/DIE.h"
15 #include "DwarfCompileUnit.h"
16 #include "DwarfDebug.h"
17 #include "DwarfUnit.h"
18 #include "llvm/ADT/Twine.h"
19 #include "llvm/CodeGen/AsmPrinter.h"
20 #include "llvm/IR/DataLayout.h"
21 #include "llvm/MC/MCAsmInfo.h"
22 #include "llvm/MC/MCContext.h"
23 #include "llvm/MC/MCStreamer.h"
24 #include "llvm/MC/MCSymbol.h"
25 #include "llvm/Support/Debug.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include "llvm/Support/Format.h"
28 #include "llvm/Support/FormattedStream.h"
29 #include "llvm/Support/LEB128.h"
30 #include "llvm/Support/MD5.h"
31 #include "llvm/Support/raw_ostream.h"
32 using namespace llvm;
33 
34 //===----------------------------------------------------------------------===//
35 // DIEAbbrevData Implementation
36 //===----------------------------------------------------------------------===//
37 
38 /// Profile - Used to gather unique data for the abbreviation folding set.
39 ///
Profile(FoldingSetNodeID & ID) const40 void DIEAbbrevData::Profile(FoldingSetNodeID &ID) const {
41   // Explicitly cast to an integer type for which FoldingSetNodeID has
42   // overloads.  Otherwise MSVC 2010 thinks this call is ambiguous.
43   ID.AddInteger(unsigned(Attribute));
44   ID.AddInteger(unsigned(Form));
45 }
46 
47 //===----------------------------------------------------------------------===//
48 // DIEAbbrev Implementation
49 //===----------------------------------------------------------------------===//
50 
51 /// Profile - Used to gather unique data for the abbreviation folding set.
52 ///
Profile(FoldingSetNodeID & ID) const53 void DIEAbbrev::Profile(FoldingSetNodeID &ID) const {
54   ID.AddInteger(unsigned(Tag));
55   ID.AddInteger(unsigned(Children));
56 
57   // For each attribute description.
58   for (unsigned i = 0, N = Data.size(); i < N; ++i)
59     Data[i].Profile(ID);
60 }
61 
62 /// Emit - Print the abbreviation using the specified asm printer.
63 ///
Emit(const AsmPrinter * AP) const64 void DIEAbbrev::Emit(const AsmPrinter *AP) const {
65   // Emit its Dwarf tag type.
66   AP->EmitULEB128(Tag, dwarf::TagString(Tag));
67 
68   // Emit whether it has children DIEs.
69   AP->EmitULEB128((unsigned)Children, dwarf::ChildrenString(Children));
70 
71   // For each attribute description.
72   for (unsigned i = 0, N = Data.size(); i < N; ++i) {
73     const DIEAbbrevData &AttrData = Data[i];
74 
75     // Emit attribute type.
76     AP->EmitULEB128(AttrData.getAttribute(),
77                     dwarf::AttributeString(AttrData.getAttribute()));
78 
79     // Emit form type.
80     AP->EmitULEB128(AttrData.getForm(),
81                     dwarf::FormEncodingString(AttrData.getForm()));
82   }
83 
84   // Mark end of abbreviation.
85   AP->EmitULEB128(0, "EOM(1)");
86   AP->EmitULEB128(0, "EOM(2)");
87 }
88 
89 LLVM_DUMP_METHOD
print(raw_ostream & O)90 void DIEAbbrev::print(raw_ostream &O) {
91   O << "Abbreviation @"
92     << format("0x%lx", (long)(intptr_t)this)
93     << "  "
94     << dwarf::TagString(Tag)
95     << " "
96     << dwarf::ChildrenString(Children)
97     << '\n';
98 
99   for (unsigned i = 0, N = Data.size(); i < N; ++i) {
100     O << "  "
101       << dwarf::AttributeString(Data[i].getAttribute())
102       << "  "
103       << dwarf::FormEncodingString(Data[i].getForm())
104       << '\n';
105   }
106 }
107 
108 LLVM_DUMP_METHOD
dump()109 void DIEAbbrev::dump() { print(dbgs()); }
110 
generateAbbrev() const111 DIEAbbrev DIE::generateAbbrev() const {
112   DIEAbbrev Abbrev(Tag, hasChildren());
113   for (const DIEValue &V : values())
114     Abbrev.AddAttribute(V.getAttribute(), V.getForm());
115   return Abbrev;
116 }
117 
118 /// Climb up the parent chain to get the unit DIE to which this DIE
119 /// belongs.
getUnit() const120 const DIE *DIE::getUnit() const {
121   const DIE *Cu = getUnitOrNull();
122   assert(Cu && "We should not have orphaned DIEs.");
123   return Cu;
124 }
125 
126 /// Climb up the parent chain to get the unit DIE this DIE belongs
127 /// to. Return NULL if DIE is not added to an owner yet.
getUnitOrNull() const128 const DIE *DIE::getUnitOrNull() const {
129   const DIE *p = this;
130   while (p) {
131     if (p->getTag() == dwarf::DW_TAG_compile_unit ||
132         p->getTag() == dwarf::DW_TAG_type_unit)
133       return p;
134     p = p->getParent();
135   }
136   return nullptr;
137 }
138 
findAttribute(dwarf::Attribute Attribute) const139 DIEValue DIE::findAttribute(dwarf::Attribute Attribute) const {
140   // Iterate through all the attributes until we find the one we're
141   // looking for, if we can't find it return NULL.
142   for (const auto &V : values())
143     if (V.getAttribute() == Attribute)
144       return V;
145   return DIEValue();
146 }
147 
148 LLVM_DUMP_METHOD
printValues(raw_ostream & O,const DIEValueList & Values,StringRef Type,unsigned Size,unsigned IndentCount)149 static void printValues(raw_ostream &O, const DIEValueList &Values,
150                         StringRef Type, unsigned Size, unsigned IndentCount) {
151   O << Type << ": Size: " << Size << "\n";
152 
153   unsigned I = 0;
154   const std::string Indent(IndentCount, ' ');
155   for (const auto &V : Values.values()) {
156     O << Indent;
157     O << "Blk[" << I++ << "]";
158     O << "  " << dwarf::FormEncodingString(V.getForm()) << " ";
159     V.print(O);
160     O << "\n";
161   }
162 }
163 
164 LLVM_DUMP_METHOD
print(raw_ostream & O,unsigned IndentCount) const165 void DIE::print(raw_ostream &O, unsigned IndentCount) const {
166   const std::string Indent(IndentCount, ' ');
167   O << Indent << "Die: " << format("0x%lx", (long)(intptr_t) this)
168     << ", Offset: " << Offset << ", Size: " << Size << "\n";
169 
170   O << Indent << dwarf::TagString(getTag()) << " "
171     << dwarf::ChildrenString(hasChildren()) << "\n";
172 
173   IndentCount += 2;
174   for (const auto &V : values()) {
175     O << Indent;
176     O << dwarf::AttributeString(V.getAttribute());
177     O << "  " << dwarf::FormEncodingString(V.getForm()) << " ";
178     V.print(O);
179     O << "\n";
180   }
181   IndentCount -= 2;
182 
183   for (const auto &Child : children())
184     Child.print(O, IndentCount + 4);
185 
186   O << "\n";
187 }
188 
189 LLVM_DUMP_METHOD
dump()190 void DIE::dump() {
191   print(dbgs());
192 }
193 
EmitValue(const AsmPrinter * AP) const194 void DIEValue::EmitValue(const AsmPrinter *AP) const {
195   switch (Ty) {
196   case isNone:
197     llvm_unreachable("Expected valid DIEValue");
198 #define HANDLE_DIEVALUE(T)                                                     \
199   case is##T:                                                                  \
200     getDIE##T().EmitValue(AP, Form);                                           \
201     break;
202 #include "llvm/CodeGen/DIEValue.def"
203   }
204 }
205 
SizeOf(const AsmPrinter * AP) const206 unsigned DIEValue::SizeOf(const AsmPrinter *AP) const {
207   switch (Ty) {
208   case isNone:
209     llvm_unreachable("Expected valid DIEValue");
210 #define HANDLE_DIEVALUE(T)                                                     \
211   case is##T:                                                                  \
212     return getDIE##T().SizeOf(AP, Form);
213 #include "llvm/CodeGen/DIEValue.def"
214   }
215   llvm_unreachable("Unknown DIE kind");
216 }
217 
218 LLVM_DUMP_METHOD
print(raw_ostream & O) const219 void DIEValue::print(raw_ostream &O) const {
220   switch (Ty) {
221   case isNone:
222     llvm_unreachable("Expected valid DIEValue");
223 #define HANDLE_DIEVALUE(T)                                                     \
224   case is##T:                                                                  \
225     getDIE##T().print(O);                                                      \
226     break;
227 #include "llvm/CodeGen/DIEValue.def"
228   }
229 }
230 
231 LLVM_DUMP_METHOD
dump() const232 void DIEValue::dump() const {
233   print(dbgs());
234 }
235 
236 //===----------------------------------------------------------------------===//
237 // DIEInteger Implementation
238 //===----------------------------------------------------------------------===//
239 
240 /// EmitValue - Emit integer of appropriate size.
241 ///
EmitValue(const AsmPrinter * Asm,dwarf::Form Form) const242 void DIEInteger::EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
243   unsigned Size = ~0U;
244   switch (Form) {
245   case dwarf::DW_FORM_flag_present:
246     // Emit something to keep the lines and comments in sync.
247     // FIXME: Is there a better way to do this?
248     Asm->OutStreamer->AddBlankLine();
249     return;
250   case dwarf::DW_FORM_flag:  // Fall thru
251   case dwarf::DW_FORM_ref1:  // Fall thru
252   case dwarf::DW_FORM_data1: Size = 1; break;
253   case dwarf::DW_FORM_ref2:  // Fall thru
254   case dwarf::DW_FORM_data2: Size = 2; break;
255   case dwarf::DW_FORM_sec_offset: // Fall thru
256   case dwarf::DW_FORM_strp: // Fall thru
257   case dwarf::DW_FORM_ref4:  // Fall thru
258   case dwarf::DW_FORM_data4: Size = 4; break;
259   case dwarf::DW_FORM_ref8:  // Fall thru
260   case dwarf::DW_FORM_ref_sig8:  // Fall thru
261   case dwarf::DW_FORM_data8: Size = 8; break;
262   case dwarf::DW_FORM_GNU_str_index: Asm->EmitULEB128(Integer); return;
263   case dwarf::DW_FORM_GNU_addr_index: Asm->EmitULEB128(Integer); return;
264   case dwarf::DW_FORM_udata: Asm->EmitULEB128(Integer); return;
265   case dwarf::DW_FORM_sdata: Asm->EmitSLEB128(Integer); return;
266   case dwarf::DW_FORM_addr:
267     Size = Asm->getPointerSize();
268     break;
269   case dwarf::DW_FORM_ref_addr:
270     Size = SizeOf(Asm, dwarf::DW_FORM_ref_addr);
271     break;
272   default: llvm_unreachable("DIE Value form not supported yet");
273   }
274   Asm->OutStreamer->EmitIntValue(Integer, Size);
275 }
276 
277 /// SizeOf - Determine size of integer value in bytes.
278 ///
SizeOf(const AsmPrinter * AP,dwarf::Form Form) const279 unsigned DIEInteger::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
280   switch (Form) {
281   case dwarf::DW_FORM_flag_present: return 0;
282   case dwarf::DW_FORM_flag:  // Fall thru
283   case dwarf::DW_FORM_ref1:  // Fall thru
284   case dwarf::DW_FORM_data1: return sizeof(int8_t);
285   case dwarf::DW_FORM_ref2:  // Fall thru
286   case dwarf::DW_FORM_data2: return sizeof(int16_t);
287   case dwarf::DW_FORM_sec_offset: // Fall thru
288   case dwarf::DW_FORM_strp: // Fall thru
289   case dwarf::DW_FORM_ref4:  // Fall thru
290   case dwarf::DW_FORM_data4: return sizeof(int32_t);
291   case dwarf::DW_FORM_ref8:  // Fall thru
292   case dwarf::DW_FORM_ref_sig8:  // Fall thru
293   case dwarf::DW_FORM_data8: return sizeof(int64_t);
294   case dwarf::DW_FORM_GNU_str_index: return getULEB128Size(Integer);
295   case dwarf::DW_FORM_GNU_addr_index: return getULEB128Size(Integer);
296   case dwarf::DW_FORM_udata: return getULEB128Size(Integer);
297   case dwarf::DW_FORM_sdata: return getSLEB128Size(Integer);
298   case dwarf::DW_FORM_addr:
299     return AP->getPointerSize();
300   case dwarf::DW_FORM_ref_addr:
301     if (AP->OutStreamer->getContext().getDwarfVersion() == 2)
302       return AP->getPointerSize();
303     return sizeof(int32_t);
304   default: llvm_unreachable("DIE Value form not supported yet");
305   }
306 }
307 
308 LLVM_DUMP_METHOD
print(raw_ostream & O) const309 void DIEInteger::print(raw_ostream &O) const {
310   O << "Int: " << (int64_t)Integer << "  0x";
311   O.write_hex(Integer);
312 }
313 
314 //===----------------------------------------------------------------------===//
315 // DIEExpr Implementation
316 //===----------------------------------------------------------------------===//
317 
318 /// EmitValue - Emit expression value.
319 ///
EmitValue(const AsmPrinter * AP,dwarf::Form Form) const320 void DIEExpr::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
321   AP->OutStreamer->EmitValue(Expr, SizeOf(AP, Form));
322 }
323 
324 /// SizeOf - Determine size of expression value in bytes.
325 ///
SizeOf(const AsmPrinter * AP,dwarf::Form Form) const326 unsigned DIEExpr::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
327   if (Form == dwarf::DW_FORM_data4) return 4;
328   if (Form == dwarf::DW_FORM_sec_offset) return 4;
329   if (Form == dwarf::DW_FORM_strp) return 4;
330   return AP->getPointerSize();
331 }
332 
333 LLVM_DUMP_METHOD
print(raw_ostream & O) const334 void DIEExpr::print(raw_ostream &O) const { O << "Expr: " << *Expr; }
335 
336 //===----------------------------------------------------------------------===//
337 // DIELabel Implementation
338 //===----------------------------------------------------------------------===//
339 
340 /// EmitValue - Emit label value.
341 ///
EmitValue(const AsmPrinter * AP,dwarf::Form Form) const342 void DIELabel::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
343   AP->EmitLabelReference(Label, SizeOf(AP, Form),
344                          Form == dwarf::DW_FORM_strp ||
345                              Form == dwarf::DW_FORM_sec_offset ||
346                              Form == dwarf::DW_FORM_ref_addr);
347 }
348 
349 /// SizeOf - Determine size of label value in bytes.
350 ///
SizeOf(const AsmPrinter * AP,dwarf::Form Form) const351 unsigned DIELabel::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
352   if (Form == dwarf::DW_FORM_data4) return 4;
353   if (Form == dwarf::DW_FORM_sec_offset) return 4;
354   if (Form == dwarf::DW_FORM_strp) return 4;
355   return AP->getPointerSize();
356 }
357 
358 LLVM_DUMP_METHOD
print(raw_ostream & O) const359 void DIELabel::print(raw_ostream &O) const { O << "Lbl: " << Label->getName(); }
360 
361 //===----------------------------------------------------------------------===//
362 // DIEDelta Implementation
363 //===----------------------------------------------------------------------===//
364 
365 /// EmitValue - Emit delta value.
366 ///
EmitValue(const AsmPrinter * AP,dwarf::Form Form) const367 void DIEDelta::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
368   AP->EmitLabelDifference(LabelHi, LabelLo, SizeOf(AP, Form));
369 }
370 
371 /// SizeOf - Determine size of delta value in bytes.
372 ///
SizeOf(const AsmPrinter * AP,dwarf::Form Form) const373 unsigned DIEDelta::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
374   if (Form == dwarf::DW_FORM_data4) return 4;
375   if (Form == dwarf::DW_FORM_sec_offset) return 4;
376   if (Form == dwarf::DW_FORM_strp) return 4;
377   return AP->getPointerSize();
378 }
379 
380 LLVM_DUMP_METHOD
print(raw_ostream & O) const381 void DIEDelta::print(raw_ostream &O) const {
382   O << "Del: " << LabelHi->getName() << "-" << LabelLo->getName();
383 }
384 
385 //===----------------------------------------------------------------------===//
386 // DIEString Implementation
387 //===----------------------------------------------------------------------===//
388 
389 /// EmitValue - Emit string value.
390 ///
EmitValue(const AsmPrinter * AP,dwarf::Form Form) const391 void DIEString::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
392   assert(
393       (Form == dwarf::DW_FORM_strp || Form == dwarf::DW_FORM_GNU_str_index) &&
394       "Expected valid string form");
395 
396   // Index of string in symbol table.
397   if (Form == dwarf::DW_FORM_GNU_str_index) {
398     DIEInteger(S.getIndex()).EmitValue(AP, Form);
399     return;
400   }
401 
402   // Relocatable symbol.
403   assert(Form == dwarf::DW_FORM_strp);
404   if (AP->MAI->doesDwarfUseRelocationsAcrossSections()) {
405     DIELabel(S.getSymbol()).EmitValue(AP, Form);
406     return;
407   }
408 
409   // Offset into symbol table.
410   DIEInteger(S.getOffset()).EmitValue(AP, Form);
411 }
412 
413 /// SizeOf - Determine size of delta value in bytes.
414 ///
SizeOf(const AsmPrinter * AP,dwarf::Form Form) const415 unsigned DIEString::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
416   assert(
417       (Form == dwarf::DW_FORM_strp || Form == dwarf::DW_FORM_GNU_str_index) &&
418       "Expected valid string form");
419 
420   // Index of string in symbol table.
421   if (Form == dwarf::DW_FORM_GNU_str_index)
422     return DIEInteger(S.getIndex()).SizeOf(AP, Form);
423 
424   // Relocatable symbol.
425   if (AP->MAI->doesDwarfUseRelocationsAcrossSections())
426     return DIELabel(S.getSymbol()).SizeOf(AP, Form);
427 
428   // Offset into symbol table.
429   return DIEInteger(S.getOffset()).SizeOf(AP, Form);
430 }
431 
432 LLVM_DUMP_METHOD
print(raw_ostream & O) const433 void DIEString::print(raw_ostream &O) const {
434   O << "String: " << S.getString();
435 }
436 
437 //===----------------------------------------------------------------------===//
438 // DIEEntry Implementation
439 //===----------------------------------------------------------------------===//
440 
441 /// EmitValue - Emit debug information entry offset.
442 ///
EmitValue(const AsmPrinter * AP,dwarf::Form Form) const443 void DIEEntry::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
444 
445   if (Form == dwarf::DW_FORM_ref_addr) {
446     const DwarfDebug *DD = AP->getDwarfDebug();
447     unsigned Addr = Entry->getOffset();
448     assert(!DD->useSplitDwarf() && "TODO: dwo files can't have relocations.");
449     // For DW_FORM_ref_addr, output the offset from beginning of debug info
450     // section. Entry->getOffset() returns the offset from start of the
451     // compile unit.
452     DwarfCompileUnit *CU = DD->lookupUnit(Entry->getUnit());
453     assert(CU && "CUDie should belong to a CU.");
454     Addr += CU->getDebugInfoOffset();
455     if (AP->MAI->doesDwarfUseRelocationsAcrossSections())
456       AP->EmitLabelPlusOffset(CU->getSectionSym(), Addr,
457                               DIEEntry::getRefAddrSize(AP));
458     else
459       AP->OutStreamer->EmitIntValue(Addr, DIEEntry::getRefAddrSize(AP));
460   } else
461     AP->EmitInt32(Entry->getOffset());
462 }
463 
getRefAddrSize(const AsmPrinter * AP)464 unsigned DIEEntry::getRefAddrSize(const AsmPrinter *AP) {
465   // DWARF4: References that use the attribute form DW_FORM_ref_addr are
466   // specified to be four bytes in the DWARF 32-bit format and eight bytes
467   // in the DWARF 64-bit format, while DWARF Version 2 specifies that such
468   // references have the same size as an address on the target system.
469   const DwarfDebug *DD = AP->getDwarfDebug();
470   assert(DD && "Expected Dwarf Debug info to be available");
471   if (DD->getDwarfVersion() == 2)
472     return AP->getPointerSize();
473   return sizeof(int32_t);
474 }
475 
476 LLVM_DUMP_METHOD
print(raw_ostream & O) const477 void DIEEntry::print(raw_ostream &O) const {
478   O << format("Die: 0x%lx", (long)(intptr_t)&Entry);
479 }
480 
481 //===----------------------------------------------------------------------===//
482 // DIETypeSignature Implementation
483 //===----------------------------------------------------------------------===//
EmitValue(const AsmPrinter * Asm,dwarf::Form Form) const484 void DIETypeSignature::EmitValue(const AsmPrinter *Asm,
485                                  dwarf::Form Form) const {
486   assert(Form == dwarf::DW_FORM_ref_sig8);
487   Asm->OutStreamer->EmitIntValue(Unit->getTypeSignature(), 8);
488 }
489 
490 LLVM_DUMP_METHOD
print(raw_ostream & O) const491 void DIETypeSignature::print(raw_ostream &O) const {
492   O << format("Type Unit: 0x%lx", Unit->getTypeSignature());
493 }
494 
495 //===----------------------------------------------------------------------===//
496 // DIELoc Implementation
497 //===----------------------------------------------------------------------===//
498 
499 /// ComputeSize - calculate the size of the location expression.
500 ///
ComputeSize(const AsmPrinter * AP) const501 unsigned DIELoc::ComputeSize(const AsmPrinter *AP) const {
502   if (!Size) {
503     for (const auto &V : values())
504       Size += V.SizeOf(AP);
505   }
506 
507   return Size;
508 }
509 
510 /// EmitValue - Emit location data.
511 ///
EmitValue(const AsmPrinter * Asm,dwarf::Form Form) const512 void DIELoc::EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
513   switch (Form) {
514   default: llvm_unreachable("Improper form for block");
515   case dwarf::DW_FORM_block1: Asm->EmitInt8(Size);    break;
516   case dwarf::DW_FORM_block2: Asm->EmitInt16(Size);   break;
517   case dwarf::DW_FORM_block4: Asm->EmitInt32(Size);   break;
518   case dwarf::DW_FORM_block:
519   case dwarf::DW_FORM_exprloc:
520     Asm->EmitULEB128(Size); break;
521   }
522 
523   for (const auto &V : values())
524     V.EmitValue(Asm);
525 }
526 
527 /// SizeOf - Determine size of location data in bytes.
528 ///
SizeOf(const AsmPrinter * AP,dwarf::Form Form) const529 unsigned DIELoc::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
530   switch (Form) {
531   case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
532   case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
533   case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
534   case dwarf::DW_FORM_block:
535   case dwarf::DW_FORM_exprloc:
536     return Size + getULEB128Size(Size);
537   default: llvm_unreachable("Improper form for block");
538   }
539 }
540 
541 LLVM_DUMP_METHOD
print(raw_ostream & O) const542 void DIELoc::print(raw_ostream &O) const {
543   printValues(O, *this, "ExprLoc", Size, 5);
544 }
545 
546 //===----------------------------------------------------------------------===//
547 // DIEBlock Implementation
548 //===----------------------------------------------------------------------===//
549 
550 /// ComputeSize - calculate the size of the block.
551 ///
ComputeSize(const AsmPrinter * AP) const552 unsigned DIEBlock::ComputeSize(const AsmPrinter *AP) const {
553   if (!Size) {
554     for (const auto &V : values())
555       Size += V.SizeOf(AP);
556   }
557 
558   return Size;
559 }
560 
561 /// EmitValue - Emit block data.
562 ///
EmitValue(const AsmPrinter * Asm,dwarf::Form Form) const563 void DIEBlock::EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
564   switch (Form) {
565   default: llvm_unreachable("Improper form for block");
566   case dwarf::DW_FORM_block1: Asm->EmitInt8(Size);    break;
567   case dwarf::DW_FORM_block2: Asm->EmitInt16(Size);   break;
568   case dwarf::DW_FORM_block4: Asm->EmitInt32(Size);   break;
569   case dwarf::DW_FORM_block:  Asm->EmitULEB128(Size); break;
570   }
571 
572   for (const auto &V : values())
573     V.EmitValue(Asm);
574 }
575 
576 /// SizeOf - Determine size of block data in bytes.
577 ///
SizeOf(const AsmPrinter * AP,dwarf::Form Form) const578 unsigned DIEBlock::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
579   switch (Form) {
580   case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
581   case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
582   case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
583   case dwarf::DW_FORM_block:  return Size + getULEB128Size(Size);
584   default: llvm_unreachable("Improper form for block");
585   }
586 }
587 
588 LLVM_DUMP_METHOD
print(raw_ostream & O) const589 void DIEBlock::print(raw_ostream &O) const {
590   printValues(O, *this, "Blk", Size, 5);
591 }
592 
593 //===----------------------------------------------------------------------===//
594 // DIELocList Implementation
595 //===----------------------------------------------------------------------===//
596 
SizeOf(const AsmPrinter * AP,dwarf::Form Form) const597 unsigned DIELocList::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
598   if (Form == dwarf::DW_FORM_data4)
599     return 4;
600   if (Form == dwarf::DW_FORM_sec_offset)
601     return 4;
602   return AP->getPointerSize();
603 }
604 
605 /// EmitValue - Emit label value.
606 ///
EmitValue(const AsmPrinter * AP,dwarf::Form Form) const607 void DIELocList::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
608   DwarfDebug *DD = AP->getDwarfDebug();
609   MCSymbol *Label = DD->getDebugLocs().getList(Index).Label;
610   AP->emitDwarfSymbolReference(Label, /*ForceOffset*/ DD->useSplitDwarf());
611 }
612 
613 LLVM_DUMP_METHOD
print(raw_ostream & O) const614 void DIELocList::print(raw_ostream &O) const { O << "LocList: " << Index; }
615