b1668658815e876962b6aa833ae1eede48964dd0
[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     assert(Abbrev.hasChildren() && "Children flag not set");
111
112     for (auto &Child : Children)
113       Offset = computeSizeAndOffset(*Child, Offset);
114
115     // End of children marker.
116     Offset += sizeof(int8_t);
117   }
118
119   Die.setSize(Offset - Die.getOffset());
120   return Offset;
121 }
122
123 void DwarfFile::emitAbbrevs(MCSection *Section) {
124   // Check to see if it is worth the effort.
125   if (!Abbreviations.empty()) {
126     // Start the debug abbrev section.
127     Asm->OutStreamer->SwitchSection(Section);
128     Asm->emitDwarfAbbrevs(Abbreviations);
129   }
130 }
131
132 // Emit strings into a string section.
133 void DwarfFile::emitStrings(MCSection *StrSection, MCSection *OffsetSection) {
134   StrPool.emit(*Asm, StrSection, OffsetSection);
135 }
136
137 bool DwarfFile::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
138   SmallVectorImpl<DbgVariable *> &Vars = ScopeVariables[LS];
139   const DILocalVariable *DV = Var->getVariable();
140   // Variables with positive arg numbers are parameters.
141   if (unsigned ArgNum = DV->getArg()) {
142     // Keep all parameters in order at the start of the variable list to ensure
143     // function types are correct (no out-of-order parameters)
144     //
145     // This could be improved by only doing it for optimized builds (unoptimized
146     // builds have the right order to begin with), searching from the back (this
147     // would catch the unoptimized case quickly), or doing a binary search
148     // rather than linear search.
149     auto I = Vars.begin();
150     while (I != Vars.end()) {
151       unsigned CurNum = (*I)->getVariable()->getArg();
152       // A local (non-parameter) variable has been found, insert immediately
153       // before it.
154       if (CurNum == 0)
155         break;
156       // A later indexed parameter has been found, insert immediately before it.
157       if (CurNum > ArgNum)
158         break;
159       if (CurNum == ArgNum) {
160         (*I)->addMMIEntry(*Var);
161         return false;
162       }
163       ++I;
164     }
165     Vars.insert(I, Var);
166     return true;
167   }
168
169   Vars.push_back(Var);
170   return true;
171 }
172 }