AsmPrinter: Avoid a warning in NDEBUG, NFC
[oota-llvm.git] / lib / CodeGen / AsmPrinter / DwarfFile.cpp
1 //===-- llvm/CodeGen/DwarfFile.cpp - Dwarf Debug Framework ----------------===//
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 #include "DwarfFile.h"
11 #include "DwarfDebug.h"
12 #include "DwarfUnit.h"
13 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/IR/DataLayout.h"
15 #include "llvm/MC/MCStreamer.h"
16 #include "llvm/Support/LEB128.h"
17 #include "llvm/Target/TargetLoweringObjectFile.h"
18
19 namespace llvm {
20 DwarfFile::DwarfFile(AsmPrinter *AP, StringRef Pref, BumpPtrAllocator &DA)
21     : Asm(AP), StrPool(DA, *Asm, Pref) {}
22
23 DwarfFile::~DwarfFile() {}
24
25 // Define a unique number for the abbreviation.
26 //
27 DIEAbbrev &DwarfFile::assignAbbrevNumber(DIE &Die) {
28   FoldingSetNodeID ID;
29   DIEAbbrev Abbrev = Die.generateAbbrev();
30   Abbrev.Profile(ID);
31
32   void *InsertPos;
33   if (DIEAbbrev *Existing =
34           AbbreviationsSet.FindNodeOrInsertPos(ID, InsertPos)) {
35     Die.setAbbrevNumber(Existing->getNumber());
36     return *Existing;
37   }
38
39   // Move the abbreviation to the heap and assign a number.
40   DIEAbbrev *New = new (AbbrevAllocator) DIEAbbrev(std::move(Abbrev));
41   Abbreviations.push_back(New);
42   New->setNumber(Abbreviations.size());
43   Die.setAbbrevNumber(Abbreviations.size());
44
45   // Store it for lookup.
46   AbbreviationsSet.InsertNode(New, InsertPos);
47   return *New;
48 }
49
50 void DwarfFile::addUnit(std::unique_ptr<DwarfUnit> U) {
51   CUs.push_back(std::move(U));
52 }
53
54 // Emit the various dwarf units to the unit section USection with
55 // the abbreviations going into ASection.
56 void DwarfFile::emitUnits(bool UseOffsets) {
57   for (const auto &TheU : CUs) {
58     DIE &Die = TheU->getUnitDie();
59     MCSection *USection = TheU->getSection();
60     Asm->OutStreamer->SwitchSection(USection);
61
62     TheU->emitHeader(UseOffsets);
63
64     Asm->emitDwarfDIE(Die);
65   }
66 }
67
68 // Compute the size and offset for each DIE.
69 void DwarfFile::computeSizeAndOffsets() {
70   // Offset from the first CU in the debug info section is 0 initially.
71   unsigned SecOffset = 0;
72
73   // Iterate over each compile unit and set the size and offsets for each
74   // DIE within each compile unit. All offsets are CU relative.
75   for (const auto &TheU : CUs) {
76     TheU->setDebugInfoOffset(SecOffset);
77
78     // CU-relative offset is reset to 0 here.
79     unsigned Offset = sizeof(int32_t) +      // Length of Unit Info
80                       TheU->getHeaderSize(); // Unit-specific headers
81
82     // EndOffset here is CU-relative, after laying out
83     // all of the CU DIE.
84     unsigned EndOffset = computeSizeAndOffset(TheU->getUnitDie(), Offset);
85     SecOffset += EndOffset;
86   }
87 }
88 // Compute the size and offset of a DIE. The offset is relative to start of the
89 // CU. It returns the offset after laying out the DIE.
90 unsigned DwarfFile::computeSizeAndOffset(DIE &Die, unsigned Offset) {
91   // Record the abbreviation.
92   const DIEAbbrev &Abbrev = assignAbbrevNumber(Die);
93
94   // Set DIE offset
95   Die.setOffset(Offset);
96
97   // Start the size with the size of abbreviation code.
98   Offset += getULEB128Size(Die.getAbbrevNumber());
99
100   // Size the DIE attribute values.
101   for (const auto &V : Die.values())
102     // Size attribute value.
103     Offset += V.SizeOf(Asm, V.getForm());
104
105   // Get the children.
106   const auto &Children = Die.getChildren();
107
108   // Size the DIE children if any.
109   if (!Children.empty()) {
110     (void)Abbrev;
111     assert(Abbrev.hasChildren() && "Children flag not set");
112
113     for (auto &Child : Children)
114       Offset = computeSizeAndOffset(*Child, Offset);
115
116     // End of children marker.
117     Offset += sizeof(int8_t);
118   }
119
120   Die.setSize(Offset - Die.getOffset());
121   return Offset;
122 }
123
124 void DwarfFile::emitAbbrevs(MCSection *Section) {
125   // Check to see if it is worth the effort.
126   if (!Abbreviations.empty()) {
127     // Start the debug abbrev section.
128     Asm->OutStreamer->SwitchSection(Section);
129     Asm->emitDwarfAbbrevs(Abbreviations);
130   }
131 }
132
133 // Emit strings into a string section.
134 void DwarfFile::emitStrings(MCSection *StrSection, MCSection *OffsetSection) {
135   StrPool.emit(*Asm, StrSection, OffsetSection);
136 }
137
138 bool DwarfFile::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
139   SmallVectorImpl<DbgVariable *> &Vars = ScopeVariables[LS];
140   const DILocalVariable *DV = Var->getVariable();
141   // Variables with positive arg numbers are parameters.
142   if (unsigned ArgNum = DV->getArg()) {
143     // Keep all parameters in order at the start of the variable list to ensure
144     // function types are correct (no out-of-order parameters)
145     //
146     // This could be improved by only doing it for optimized builds (unoptimized
147     // builds have the right order to begin with), searching from the back (this
148     // would catch the unoptimized case quickly), or doing a binary search
149     // rather than linear search.
150     auto I = Vars.begin();
151     while (I != Vars.end()) {
152       unsigned CurNum = (*I)->getVariable()->getArg();
153       // A local (non-parameter) variable has been found, insert immediately
154       // before it.
155       if (CurNum == 0)
156         break;
157       // A later indexed parameter has been found, insert immediately before it.
158       if (CurNum > ArgNum)
159         break;
160       if (CurNum == ArgNum) {
161         (*I)->addMMIEntry(*Var);
162         return false;
163       }
164       ++I;
165     }
166     Vars.insert(I, Var);
167     return true;
168   }
169
170   Vars.push_back(Var);
171   return true;
172 }
173 }